meson-ir.c 5.3 KB

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