meson-ir.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * Driver for Amlogic Meson IR remote receiver
  3. *
  4. * Copyright (C) 2014 Beniamino Galvani <b.galvani@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * version 2 as published by the Free Software Foundation.
  9. *
  10. * You should have received a copy of the GNU General Public License
  11. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. */
  13. #include <linux/device.h>
  14. #include <linux/err.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/io.h>
  17. #include <linux/module.h>
  18. #include <linux/of_platform.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/bitfield.h>
  22. #include <media/rc-core.h>
  23. #define DRIVER_NAME "meson-ir"
  24. /* valid on all Meson platforms */
  25. #define IR_DEC_LDR_ACTIVE 0x00
  26. #define IR_DEC_LDR_IDLE 0x04
  27. #define IR_DEC_LDR_REPEAT 0x08
  28. #define IR_DEC_BIT_0 0x0c
  29. #define IR_DEC_REG0 0x10
  30. #define IR_DEC_FRAME 0x14
  31. #define IR_DEC_STATUS 0x18
  32. #define IR_DEC_REG1 0x1c
  33. /* only available on Meson 8b and newer */
  34. #define IR_DEC_REG2 0x20
  35. #define REG0_RATE_MASK GENMASK(11, 0)
  36. #define DECODE_MODE_NEC 0x0
  37. #define DECODE_MODE_RAW 0x2
  38. /* Meson 6b uses REG1 to configure the mode */
  39. #define REG1_MODE_MASK GENMASK(8, 7)
  40. #define REG1_MODE_SHIFT 7
  41. /* Meson 8b / GXBB use REG2 to configure the mode */
  42. #define REG2_MODE_MASK GENMASK(3, 0)
  43. #define REG2_MODE_SHIFT 0
  44. #define REG1_TIME_IV_MASK GENMASK(28, 16)
  45. #define REG1_IRQSEL_MASK GENMASK(3, 2)
  46. #define REG1_IRQSEL_NEC_MODE 0
  47. #define REG1_IRQSEL_RISE_FALL 1
  48. #define REG1_IRQSEL_FALL 2
  49. #define REG1_IRQSEL_RISE 3
  50. #define REG1_RESET BIT(0)
  51. #define REG1_ENABLE BIT(15)
  52. #define STATUS_IR_DEC_IN BIT(8)
  53. #define MESON_TRATE 10 /* us */
  54. struct meson_ir {
  55. void __iomem *reg;
  56. struct rc_dev *rc;
  57. spinlock_t lock;
  58. };
  59. static void meson_ir_set_mask(struct meson_ir *ir, unsigned int reg,
  60. u32 mask, u32 value)
  61. {
  62. u32 data;
  63. data = readl(ir->reg + reg);
  64. data &= ~mask;
  65. data |= (value & mask);
  66. writel(data, ir->reg + reg);
  67. }
  68. static irqreturn_t meson_ir_irq(int irqno, void *dev_id)
  69. {
  70. struct meson_ir *ir = dev_id;
  71. u32 duration, status;
  72. DEFINE_IR_RAW_EVENT(rawir);
  73. spin_lock(&ir->lock);
  74. duration = readl_relaxed(ir->reg + IR_DEC_REG1);
  75. duration = FIELD_GET(REG1_TIME_IV_MASK, duration);
  76. rawir.duration = US_TO_NS(duration * MESON_TRATE);
  77. status = readl_relaxed(ir->reg + IR_DEC_STATUS);
  78. rawir.pulse = !!(status & STATUS_IR_DEC_IN);
  79. ir_raw_event_store_with_timeout(ir->rc, &rawir);
  80. spin_unlock(&ir->lock);
  81. return IRQ_HANDLED;
  82. }
  83. static int meson_ir_probe(struct platform_device *pdev)
  84. {
  85. struct device *dev = &pdev->dev;
  86. struct device_node *node = dev->of_node;
  87. struct resource *res;
  88. const char *map_name;
  89. struct meson_ir *ir;
  90. int irq, ret;
  91. ir = devm_kzalloc(dev, sizeof(struct meson_ir), GFP_KERNEL);
  92. if (!ir)
  93. return -ENOMEM;
  94. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  95. ir->reg = devm_ioremap_resource(dev, res);
  96. if (IS_ERR(ir->reg)) {
  97. dev_err(dev, "failed to map registers\n");
  98. return PTR_ERR(ir->reg);
  99. }
  100. irq = platform_get_irq(pdev, 0);
  101. if (irq < 0) {
  102. dev_err(dev, "no irq resource\n");
  103. return irq;
  104. }
  105. ir->rc = devm_rc_allocate_device(dev, RC_DRIVER_IR_RAW);
  106. if (!ir->rc) {
  107. dev_err(dev, "failed to allocate rc device\n");
  108. return -ENOMEM;
  109. }
  110. ir->rc->priv = ir;
  111. ir->rc->device_name = DRIVER_NAME;
  112. ir->rc->input_phys = DRIVER_NAME "/input0";
  113. ir->rc->input_id.bustype = BUS_HOST;
  114. map_name = of_get_property(node, "linux,rc-map-name", NULL);
  115. ir->rc->map_name = map_name ? map_name : RC_MAP_EMPTY;
  116. ir->rc->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER;
  117. ir->rc->rx_resolution = US_TO_NS(MESON_TRATE);
  118. ir->rc->min_timeout = 1;
  119. ir->rc->timeout = IR_DEFAULT_TIMEOUT;
  120. ir->rc->max_timeout = 10 * IR_DEFAULT_TIMEOUT;
  121. ir->rc->driver_name = DRIVER_NAME;
  122. spin_lock_init(&ir->lock);
  123. platform_set_drvdata(pdev, ir);
  124. ret = devm_rc_register_device(dev, ir->rc);
  125. if (ret) {
  126. dev_err(dev, "failed to register rc device\n");
  127. return ret;
  128. }
  129. ret = devm_request_irq(dev, irq, meson_ir_irq, 0, NULL, ir);
  130. if (ret) {
  131. dev_err(dev, "failed to request irq\n");
  132. return ret;
  133. }
  134. /* Reset the decoder */
  135. meson_ir_set_mask(ir, IR_DEC_REG1, REG1_RESET, REG1_RESET);
  136. meson_ir_set_mask(ir, IR_DEC_REG1, REG1_RESET, 0);
  137. /* Set general operation mode (= raw/software decoding) */
  138. if (of_device_is_compatible(node, "amlogic,meson6-ir"))
  139. meson_ir_set_mask(ir, IR_DEC_REG1, REG1_MODE_MASK,
  140. FIELD_PREP(REG1_MODE_MASK, DECODE_MODE_RAW));
  141. else
  142. meson_ir_set_mask(ir, IR_DEC_REG2, REG2_MODE_MASK,
  143. FIELD_PREP(REG2_MODE_MASK, DECODE_MODE_RAW));
  144. /* Set rate */
  145. meson_ir_set_mask(ir, IR_DEC_REG0, REG0_RATE_MASK, MESON_TRATE - 1);
  146. /* IRQ on rising and falling edges */
  147. meson_ir_set_mask(ir, IR_DEC_REG1, REG1_IRQSEL_MASK,
  148. FIELD_PREP(REG1_IRQSEL_MASK, REG1_IRQSEL_RISE_FALL));
  149. /* Enable the decoder */
  150. meson_ir_set_mask(ir, IR_DEC_REG1, REG1_ENABLE, REG1_ENABLE);
  151. dev_info(dev, "receiver initialized\n");
  152. return 0;
  153. }
  154. static int meson_ir_remove(struct platform_device *pdev)
  155. {
  156. struct meson_ir *ir = platform_get_drvdata(pdev);
  157. unsigned long flags;
  158. /* Disable the decoder */
  159. spin_lock_irqsave(&ir->lock, flags);
  160. meson_ir_set_mask(ir, IR_DEC_REG1, REG1_ENABLE, 0);
  161. spin_unlock_irqrestore(&ir->lock, flags);
  162. return 0;
  163. }
  164. static void meson_ir_shutdown(struct platform_device *pdev)
  165. {
  166. struct device *dev = &pdev->dev;
  167. struct device_node *node = dev->of_node;
  168. struct meson_ir *ir = platform_get_drvdata(pdev);
  169. unsigned long flags;
  170. spin_lock_irqsave(&ir->lock, flags);
  171. /*
  172. * Set operation mode to NEC/hardware decoding to give
  173. * bootloader a chance to power the system back on
  174. */
  175. if (of_device_is_compatible(node, "amlogic,meson6-ir"))
  176. meson_ir_set_mask(ir, IR_DEC_REG1, REG1_MODE_MASK,
  177. DECODE_MODE_NEC << REG1_MODE_SHIFT);
  178. else
  179. meson_ir_set_mask(ir, IR_DEC_REG2, REG2_MODE_MASK,
  180. DECODE_MODE_NEC << REG2_MODE_SHIFT);
  181. /* Set rate to default value */
  182. meson_ir_set_mask(ir, IR_DEC_REG0, REG0_RATE_MASK, 0x13);
  183. spin_unlock_irqrestore(&ir->lock, flags);
  184. }
  185. static const struct of_device_id meson_ir_match[] = {
  186. { .compatible = "amlogic,meson6-ir" },
  187. { .compatible = "amlogic,meson8b-ir" },
  188. { .compatible = "amlogic,meson-gxbb-ir" },
  189. { },
  190. };
  191. MODULE_DEVICE_TABLE(of, meson_ir_match);
  192. static struct platform_driver meson_ir_driver = {
  193. .probe = meson_ir_probe,
  194. .remove = meson_ir_remove,
  195. .shutdown = meson_ir_shutdown,
  196. .driver = {
  197. .name = DRIVER_NAME,
  198. .of_match_table = meson_ir_match,
  199. },
  200. };
  201. module_platform_driver(meson_ir_driver);
  202. MODULE_DESCRIPTION("Amlogic Meson IR remote receiver driver");
  203. MODULE_AUTHOR("Beniamino Galvani <b.galvani@gmail.com>");
  204. MODULE_LICENSE("GPL v2");