meson-ir.c 6.2 KB

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