meson-ir.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 <media/rc-core.h>
  22. #define DRIVER_NAME "meson-ir"
  23. /* valid on all Meson platforms */
  24. #define IR_DEC_LDR_ACTIVE 0x00
  25. #define IR_DEC_LDR_IDLE 0x04
  26. #define IR_DEC_LDR_REPEAT 0x08
  27. #define IR_DEC_BIT_0 0x0c
  28. #define IR_DEC_REG0 0x10
  29. #define IR_DEC_FRAME 0x14
  30. #define IR_DEC_STATUS 0x18
  31. #define IR_DEC_REG1 0x1c
  32. /* only available on Meson 8b and newer */
  33. #define IR_DEC_REG2 0x20
  34. #define REG0_RATE_MASK (BIT(11) - 1)
  35. #define DECODE_MODE_NEC 0x0
  36. #define DECODE_MODE_RAW 0x2
  37. /* Meson 6b uses REG1 to configure the mode */
  38. #define REG1_MODE_MASK GENMASK(8, 7)
  39. #define REG1_MODE_SHIFT 7
  40. /* Meson 8b / GXBB use REG2 to configure the mode */
  41. #define REG2_MODE_MASK GENMASK(3, 0)
  42. #define REG2_MODE_SHIFT 0
  43. #define REG1_TIME_IV_SHIFT 16
  44. #define REG1_TIME_IV_MASK ((BIT(13) - 1) << REG1_TIME_IV_SHIFT)
  45. #define REG1_IRQSEL_MASK (BIT(2) | BIT(3))
  46. #define REG1_IRQSEL_NEC_MODE (0 << 2)
  47. #define REG1_IRQSEL_RISE_FALL (1 << 2)
  48. #define REG1_IRQSEL_FALL (2 << 2)
  49. #define REG1_IRQSEL_RISE (3 << 2)
  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. int irq;
  58. spinlock_t lock;
  59. };
  60. static void meson_ir_set_mask(struct meson_ir *ir, unsigned int reg,
  61. u32 mask, u32 value)
  62. {
  63. u32 data;
  64. data = readl(ir->reg + reg);
  65. data &= ~mask;
  66. data |= (value & mask);
  67. writel(data, ir->reg + reg);
  68. }
  69. static irqreturn_t meson_ir_irq(int irqno, void *dev_id)
  70. {
  71. struct meson_ir *ir = dev_id;
  72. u32 duration;
  73. DEFINE_IR_RAW_EVENT(rawir);
  74. spin_lock(&ir->lock);
  75. duration = readl(ir->reg + IR_DEC_REG1);
  76. duration = (duration & REG1_TIME_IV_MASK) >> REG1_TIME_IV_SHIFT;
  77. rawir.duration = US_TO_NS(duration * MESON_TRATE);
  78. rawir.pulse = !!(readl(ir->reg + IR_DEC_STATUS) & STATUS_IR_DEC_IN);
  79. ir_raw_event_store_with_filter(ir->rc, &rawir);
  80. ir_raw_event_handle(ir->rc);
  81. spin_unlock(&ir->lock);
  82. return IRQ_HANDLED;
  83. }
  84. static int meson_ir_probe(struct platform_device *pdev)
  85. {
  86. struct device *dev = &pdev->dev;
  87. struct device_node *node = dev->of_node;
  88. struct resource *res;
  89. const char *map_name;
  90. struct meson_ir *ir;
  91. int ret;
  92. ir = devm_kzalloc(dev, sizeof(struct meson_ir), GFP_KERNEL);
  93. if (!ir)
  94. return -ENOMEM;
  95. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  96. ir->reg = devm_ioremap_resource(dev, res);
  97. if (IS_ERR(ir->reg)) {
  98. dev_err(dev, "failed to map registers\n");
  99. return PTR_ERR(ir->reg);
  100. }
  101. ir->irq = platform_get_irq(pdev, 0);
  102. if (ir->irq < 0) {
  103. dev_err(dev, "no irq resource\n");
  104. return ir->irq;
  105. }
  106. ir->rc = rc_allocate_device();
  107. if (!ir->rc) {
  108. dev_err(dev, "failed to allocate rc device\n");
  109. return -ENOMEM;
  110. }
  111. ir->rc->priv = ir;
  112. ir->rc->input_name = DRIVER_NAME;
  113. ir->rc->input_phys = DRIVER_NAME "/input0";
  114. ir->rc->input_id.bustype = BUS_HOST;
  115. map_name = of_get_property(node, "linux,rc-map-name", NULL);
  116. ir->rc->map_name = map_name ? map_name : RC_MAP_EMPTY;
  117. ir->rc->dev.parent = dev;
  118. ir->rc->driver_type = RC_DRIVER_IR_RAW;
  119. ir->rc->allowed_protocols = RC_BIT_ALL;
  120. ir->rc->rx_resolution = US_TO_NS(MESON_TRATE);
  121. ir->rc->timeout = MS_TO_NS(200);
  122. ir->rc->driver_name = DRIVER_NAME;
  123. spin_lock_init(&ir->lock);
  124. platform_set_drvdata(pdev, ir);
  125. ret = rc_register_device(ir->rc);
  126. if (ret) {
  127. dev_err(dev, "failed to register rc device\n");
  128. goto out_free;
  129. }
  130. ret = devm_request_irq(dev, ir->irq, meson_ir_irq, 0, "ir-meson", ir);
  131. if (ret) {
  132. dev_err(dev, "failed to request irq\n");
  133. goto out_unreg;
  134. }
  135. /* Reset the decoder */
  136. meson_ir_set_mask(ir, IR_DEC_REG1, REG1_RESET, REG1_RESET);
  137. meson_ir_set_mask(ir, IR_DEC_REG1, REG1_RESET, 0);
  138. /* Set general operation mode (= raw/software decoding) */
  139. if (of_device_is_compatible(node, "amlogic,meson6-ir"))
  140. meson_ir_set_mask(ir, IR_DEC_REG1, REG1_MODE_MASK,
  141. DECODE_MODE_RAW << REG1_MODE_SHIFT);
  142. else
  143. meson_ir_set_mask(ir, IR_DEC_REG2, REG2_MODE_MASK,
  144. DECODE_MODE_RAW << REG2_MODE_SHIFT);
  145. /* Set rate */
  146. meson_ir_set_mask(ir, IR_DEC_REG0, REG0_RATE_MASK, MESON_TRATE - 1);
  147. /* IRQ on rising and falling edges */
  148. meson_ir_set_mask(ir, IR_DEC_REG1, REG1_IRQSEL_MASK,
  149. REG1_IRQSEL_RISE_FALL);
  150. /* Enable the decoder */
  151. meson_ir_set_mask(ir, IR_DEC_REG1, REG1_ENABLE, REG1_ENABLE);
  152. dev_info(dev, "receiver initialized\n");
  153. return 0;
  154. out_unreg:
  155. rc_unregister_device(ir->rc);
  156. ir->rc = NULL;
  157. out_free:
  158. rc_free_device(ir->rc);
  159. return ret;
  160. }
  161. static int meson_ir_remove(struct platform_device *pdev)
  162. {
  163. struct meson_ir *ir = platform_get_drvdata(pdev);
  164. unsigned long flags;
  165. /* Disable the decoder */
  166. spin_lock_irqsave(&ir->lock, flags);
  167. meson_ir_set_mask(ir, IR_DEC_REG1, REG1_ENABLE, 0);
  168. spin_unlock_irqrestore(&ir->lock, flags);
  169. rc_unregister_device(ir->rc);
  170. return 0;
  171. }
  172. static const struct of_device_id meson_ir_match[] = {
  173. { .compatible = "amlogic,meson6-ir" },
  174. { .compatible = "amlogic,meson8b-ir" },
  175. { .compatible = "amlogic,meson-gxbb-ir" },
  176. { },
  177. };
  178. static struct platform_driver meson_ir_driver = {
  179. .probe = meson_ir_probe,
  180. .remove = meson_ir_remove,
  181. .driver = {
  182. .name = DRIVER_NAME,
  183. .of_match_table = meson_ir_match,
  184. },
  185. };
  186. module_platform_driver(meson_ir_driver);
  187. MODULE_DESCRIPTION("Amlogic Meson IR remote receiver driver");
  188. MODULE_AUTHOR("Beniamino Galvani <b.galvani@gmail.com>");
  189. MODULE_LICENSE("GPL v2");