mtk-cir.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*
  2. * Driver for Mediatek IR Receiver Controller
  3. *
  4. * Copyright (C) 2017 Sean Wang <sean.wang@mediatek.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 as
  8. * published by the Free Software Foundation; either version 2 of
  9. * the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/clk.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/module.h>
  19. #include <linux/of_platform.h>
  20. #include <linux/reset.h>
  21. #include <media/rc-core.h>
  22. #define MTK_IR_DEV KBUILD_MODNAME
  23. /* Register to enable PWM and IR */
  24. #define MTK_CONFIG_HIGH_REG 0x0c
  25. /* Enable IR pulse width detection */
  26. #define MTK_PWM_EN BIT(13)
  27. /* Enable IR hardware function */
  28. #define MTK_IR_EN BIT(0)
  29. /* Register to setting sample period */
  30. #define MTK_CONFIG_LOW_REG 0x10
  31. /* Field to set sample period */
  32. #define CHK_PERIOD DIV_ROUND_CLOSEST(MTK_IR_SAMPLE, \
  33. MTK_IR_CLK_PERIOD)
  34. #define MTK_CHK_PERIOD (((CHK_PERIOD) << 8) & (GENMASK(20, 8)))
  35. #define MTK_CHK_PERIOD_MASK (GENMASK(20, 8))
  36. /* Register to clear state of state machine */
  37. #define MTK_IRCLR_REG 0x20
  38. /* Bit to restart IR receiving */
  39. #define MTK_IRCLR BIT(0)
  40. /* Register containing pulse width data */
  41. #define MTK_CHKDATA_REG(i) (0x88 + 4 * (i))
  42. #define MTK_WIDTH_MASK (GENMASK(7, 0))
  43. /* Register to enable IR interrupt */
  44. #define MTK_IRINT_EN_REG 0xcc
  45. /* Bit to enable interrupt */
  46. #define MTK_IRINT_EN BIT(0)
  47. /* Register to ack IR interrupt */
  48. #define MTK_IRINT_CLR_REG 0xd0
  49. /* Bit to clear interrupt status */
  50. #define MTK_IRINT_CLR BIT(0)
  51. /* Maximum count of samples */
  52. #define MTK_MAX_SAMPLES 0xff
  53. /* Indicate the end of IR message */
  54. #define MTK_IR_END(v, p) ((v) == MTK_MAX_SAMPLES && (p) == 0)
  55. /* Number of registers to record the pulse width */
  56. #define MTK_CHKDATA_SZ 17
  57. /* Source clock frequency */
  58. #define MTK_IR_BASE_CLK 273000000
  59. /* Frequency after IR internal divider */
  60. #define MTK_IR_CLK_FREQ (MTK_IR_BASE_CLK / 4)
  61. /* Period for MTK_IR_CLK in ns*/
  62. #define MTK_IR_CLK_PERIOD DIV_ROUND_CLOSEST(1000000000ul, \
  63. MTK_IR_CLK_FREQ)
  64. /* Sample period in ns */
  65. #define MTK_IR_SAMPLE (MTK_IR_CLK_PERIOD * 0xc00)
  66. /*
  67. * struct mtk_ir - This is the main datasructure for holding the state
  68. * of the driver
  69. * @dev: The device pointer
  70. * @rc: The rc instrance
  71. * @irq: The IRQ that we are using
  72. * @base: The mapped register i/o base
  73. * @clk: The clock that we are using
  74. */
  75. struct mtk_ir {
  76. struct device *dev;
  77. struct rc_dev *rc;
  78. void __iomem *base;
  79. int irq;
  80. struct clk *clk;
  81. };
  82. static void mtk_w32_mask(struct mtk_ir *ir, u32 val, u32 mask, unsigned int reg)
  83. {
  84. u32 tmp;
  85. tmp = __raw_readl(ir->base + reg);
  86. tmp = (tmp & ~mask) | val;
  87. __raw_writel(tmp, ir->base + reg);
  88. }
  89. static void mtk_w32(struct mtk_ir *ir, u32 val, unsigned int reg)
  90. {
  91. __raw_writel(val, ir->base + reg);
  92. }
  93. static u32 mtk_r32(struct mtk_ir *ir, unsigned int reg)
  94. {
  95. return __raw_readl(ir->base + reg);
  96. }
  97. static inline void mtk_irq_disable(struct mtk_ir *ir, u32 mask)
  98. {
  99. u32 val;
  100. val = mtk_r32(ir, MTK_IRINT_EN_REG);
  101. mtk_w32(ir, val & ~mask, MTK_IRINT_EN_REG);
  102. }
  103. static inline void mtk_irq_enable(struct mtk_ir *ir, u32 mask)
  104. {
  105. u32 val;
  106. val = mtk_r32(ir, MTK_IRINT_EN_REG);
  107. mtk_w32(ir, val | mask, MTK_IRINT_EN_REG);
  108. }
  109. static irqreturn_t mtk_ir_irq(int irqno, void *dev_id)
  110. {
  111. struct mtk_ir *ir = dev_id;
  112. u8 wid = 0;
  113. u32 i, j, val;
  114. DEFINE_IR_RAW_EVENT(rawir);
  115. /*
  116. * Reset decoder state machine explicitly is required
  117. * because 1) the longest duration for space MTK IR hardware
  118. * could record is not safely long. e.g 12ms if rx resolution
  119. * is 46us by default. There is still the risk to satisfying
  120. * every decoder to reset themselves through long enough
  121. * trailing spaces and 2) the IRQ handler guarantees that
  122. * start of IR message is always contained in and starting
  123. * from register MTK_CHKDATA_REG(0).
  124. */
  125. ir_raw_event_reset(ir->rc);
  126. /* First message must be pulse */
  127. rawir.pulse = false;
  128. /* Handle all pulse and space IR controller captures */
  129. for (i = 0 ; i < MTK_CHKDATA_SZ ; i++) {
  130. val = mtk_r32(ir, MTK_CHKDATA_REG(i));
  131. dev_dbg(ir->dev, "@reg%d=0x%08x\n", i, val);
  132. for (j = 0 ; j < 4 ; j++) {
  133. wid = (val & (MTK_WIDTH_MASK << j * 8)) >> j * 8;
  134. rawir.pulse = !rawir.pulse;
  135. rawir.duration = wid * (MTK_IR_SAMPLE + 1);
  136. ir_raw_event_store_with_filter(ir->rc, &rawir);
  137. }
  138. }
  139. /*
  140. * The maximum number of edges the IR controller can
  141. * hold is MTK_CHKDATA_SZ * 4. So if received IR messages
  142. * is over the limit, the last incomplete IR message would
  143. * be appended trailing space and still would be sent into
  144. * ir-rc-raw to decode. That helps it is possible that it
  145. * has enough information to decode a scancode even if the
  146. * trailing end of the message is missing.
  147. */
  148. if (!MTK_IR_END(wid, rawir.pulse)) {
  149. rawir.pulse = false;
  150. rawir.duration = MTK_MAX_SAMPLES * (MTK_IR_SAMPLE + 1);
  151. ir_raw_event_store_with_filter(ir->rc, &rawir);
  152. }
  153. ir_raw_event_handle(ir->rc);
  154. /*
  155. * Restart controller for the next receive that would
  156. * clear up all CHKDATA registers
  157. */
  158. mtk_w32_mask(ir, 0x1, MTK_IRCLR, MTK_IRCLR_REG);
  159. /* Clear interrupt status */
  160. mtk_w32_mask(ir, 0x1, MTK_IRINT_CLR, MTK_IRINT_CLR_REG);
  161. return IRQ_HANDLED;
  162. }
  163. static int mtk_ir_probe(struct platform_device *pdev)
  164. {
  165. struct device *dev = &pdev->dev;
  166. struct device_node *dn = dev->of_node;
  167. struct resource *res;
  168. struct mtk_ir *ir;
  169. u32 val;
  170. int ret = 0;
  171. const char *map_name;
  172. ir = devm_kzalloc(dev, sizeof(struct mtk_ir), GFP_KERNEL);
  173. if (!ir)
  174. return -ENOMEM;
  175. ir->dev = dev;
  176. if (!of_device_is_compatible(dn, "mediatek,mt7623-cir"))
  177. return -ENODEV;
  178. ir->clk = devm_clk_get(dev, "clk");
  179. if (IS_ERR(ir->clk)) {
  180. dev_err(dev, "failed to get a ir clock.\n");
  181. return PTR_ERR(ir->clk);
  182. }
  183. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  184. ir->base = devm_ioremap_resource(dev, res);
  185. if (IS_ERR(ir->base)) {
  186. dev_err(dev, "failed to map registers\n");
  187. return PTR_ERR(ir->base);
  188. }
  189. ir->rc = devm_rc_allocate_device(dev, RC_DRIVER_IR_RAW);
  190. if (!ir->rc) {
  191. dev_err(dev, "failed to allocate device\n");
  192. return -ENOMEM;
  193. }
  194. ir->rc->priv = ir;
  195. ir->rc->input_name = MTK_IR_DEV;
  196. ir->rc->input_phys = MTK_IR_DEV "/input0";
  197. ir->rc->input_id.bustype = BUS_HOST;
  198. ir->rc->input_id.vendor = 0x0001;
  199. ir->rc->input_id.product = 0x0001;
  200. ir->rc->input_id.version = 0x0001;
  201. map_name = of_get_property(dn, "linux,rc-map-name", NULL);
  202. ir->rc->map_name = map_name ?: RC_MAP_EMPTY;
  203. ir->rc->dev.parent = dev;
  204. ir->rc->driver_name = MTK_IR_DEV;
  205. ir->rc->allowed_protocols = RC_BIT_ALL;
  206. ir->rc->rx_resolution = MTK_IR_SAMPLE;
  207. ir->rc->timeout = MTK_MAX_SAMPLES * (MTK_IR_SAMPLE + 1);
  208. ret = devm_rc_register_device(dev, ir->rc);
  209. if (ret) {
  210. dev_err(dev, "failed to register rc device\n");
  211. return ret;
  212. }
  213. platform_set_drvdata(pdev, ir);
  214. ir->irq = platform_get_irq(pdev, 0);
  215. if (ir->irq < 0) {
  216. dev_err(dev, "no irq resource\n");
  217. return -ENODEV;
  218. }
  219. /*
  220. * Enable interrupt after proper hardware
  221. * setup and IRQ handler registration
  222. */
  223. if (clk_prepare_enable(ir->clk)) {
  224. dev_err(dev, "try to enable ir_clk failed\n");
  225. ret = -EINVAL;
  226. goto exit_clkdisable_clk;
  227. }
  228. mtk_irq_disable(ir, MTK_IRINT_EN);
  229. ret = devm_request_irq(dev, ir->irq, mtk_ir_irq, 0, MTK_IR_DEV, ir);
  230. if (ret) {
  231. dev_err(dev, "failed request irq\n");
  232. goto exit_clkdisable_clk;
  233. }
  234. /* Enable IR and PWM */
  235. val = mtk_r32(ir, MTK_CONFIG_HIGH_REG);
  236. val |= MTK_PWM_EN | MTK_IR_EN;
  237. mtk_w32(ir, val, MTK_CONFIG_HIGH_REG);
  238. /* Setting sample period */
  239. mtk_w32_mask(ir, MTK_CHK_PERIOD, MTK_CHK_PERIOD_MASK,
  240. MTK_CONFIG_LOW_REG);
  241. mtk_irq_enable(ir, MTK_IRINT_EN);
  242. dev_info(dev, "Initialized MT7623 IR driver, sample period = %luus\n",
  243. DIV_ROUND_CLOSEST(MTK_IR_SAMPLE, 1000));
  244. return 0;
  245. exit_clkdisable_clk:
  246. clk_disable_unprepare(ir->clk);
  247. return ret;
  248. }
  249. static int mtk_ir_remove(struct platform_device *pdev)
  250. {
  251. struct mtk_ir *ir = platform_get_drvdata(pdev);
  252. /*
  253. * Avoid contention between remove handler and
  254. * IRQ handler so that disabling IR interrupt and
  255. * waiting for pending IRQ handler to complete
  256. */
  257. mtk_irq_disable(ir, MTK_IRINT_EN);
  258. synchronize_irq(ir->irq);
  259. clk_disable_unprepare(ir->clk);
  260. return 0;
  261. }
  262. static const struct of_device_id mtk_ir_match[] = {
  263. { .compatible = "mediatek,mt7623-cir" },
  264. {},
  265. };
  266. MODULE_DEVICE_TABLE(of, mtk_ir_match);
  267. static struct platform_driver mtk_ir_driver = {
  268. .probe = mtk_ir_probe,
  269. .remove = mtk_ir_remove,
  270. .driver = {
  271. .name = MTK_IR_DEV,
  272. .of_match_table = mtk_ir_match,
  273. },
  274. };
  275. module_platform_driver(mtk_ir_driver);
  276. MODULE_DESCRIPTION("Mediatek IR Receiver Controller Driver");
  277. MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>");
  278. MODULE_LICENSE("GPL");