sunxi-cir.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*
  2. * Driver for Allwinner sunXi IR controller
  3. *
  4. * Copyright (C) 2014 Alexsey Shestacov <wingrime@linux-sunxi.org>
  5. * Copyright (C) 2014 Alexander Bersenev <bay@hackerdom.ru>
  6. *
  7. * Based on sun5i-ir.c:
  8. * Copyright (C) 2007-2012 Daniel Wang
  9. * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2 of
  14. * the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. */
  21. #include <linux/clk.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/module.h>
  24. #include <linux/of_platform.h>
  25. #include <media/rc-core.h>
  26. #define SUNXI_IR_DEV "sunxi-ir"
  27. /* Registers */
  28. /* IR Control */
  29. #define SUNXI_IR_CTL_REG 0x00
  30. /* Global Enable */
  31. #define REG_CTL_GEN BIT(0)
  32. /* RX block enable */
  33. #define REG_CTL_RXEN BIT(1)
  34. /* CIR mode */
  35. #define REG_CTL_MD (BIT(4) | BIT(5))
  36. /* Rx Config */
  37. #define SUNXI_IR_RXCTL_REG 0x10
  38. /* Pulse Polarity Invert flag */
  39. #define REG_RXCTL_RPPI BIT(2)
  40. /* Rx Data */
  41. #define SUNXI_IR_RXFIFO_REG 0x20
  42. /* Rx Interrupt Enable */
  43. #define SUNXI_IR_RXINT_REG 0x2C
  44. /* Rx FIFO Overflow */
  45. #define REG_RXINT_ROI_EN BIT(0)
  46. /* Rx Packet End */
  47. #define REG_RXINT_RPEI_EN BIT(1)
  48. /* Rx FIFO Data Available */
  49. #define REG_RXINT_RAI_EN BIT(4)
  50. /* Rx FIFO available byte level */
  51. #define REG_RXINT_RAL(val) (((val) << 8) & (GENMASK(11, 8)))
  52. /* Rx Interrupt Status */
  53. #define SUNXI_IR_RXSTA_REG 0x30
  54. /* RX FIFO Get Available Counter */
  55. #define REG_RXSTA_GET_AC(val) (((val) >> 8) & (GENMASK(5, 0)))
  56. /* Clear all interrupt status value */
  57. #define REG_RXSTA_CLEARALL 0xff
  58. /* IR Sample Config */
  59. #define SUNXI_IR_CIR_REG 0x34
  60. /* CIR_REG register noise threshold */
  61. #define REG_CIR_NTHR(val) (((val) << 2) & (GENMASK(7, 2)))
  62. /* CIR_REG register idle threshold */
  63. #define REG_CIR_ITHR(val) (((val) << 8) & (GENMASK(15, 8)))
  64. /* Hardware supported fifo size */
  65. #define SUNXI_IR_FIFO_SIZE 16
  66. /* How many messages in FIFO trigger IRQ */
  67. #define TRIGGER_LEVEL 8
  68. /* Required frequency for IR0 or IR1 clock in CIR mode */
  69. #define SUNXI_IR_BASE_CLK 8000000
  70. /* Frequency after IR internal divider */
  71. #define SUNXI_IR_CLK (SUNXI_IR_BASE_CLK / 64)
  72. /* Sample period in ns */
  73. #define SUNXI_IR_SAMPLE (1000000000ul / SUNXI_IR_CLK)
  74. /* Noise threshold in samples */
  75. #define SUNXI_IR_RXNOISE 1
  76. /* Idle Threshold in samples */
  77. #define SUNXI_IR_RXIDLE 20
  78. /* Time after which device stops sending data in ms */
  79. #define SUNXI_IR_TIMEOUT 120
  80. struct sunxi_ir {
  81. spinlock_t ir_lock;
  82. struct rc_dev *rc;
  83. void __iomem *base;
  84. int irq;
  85. struct clk *clk;
  86. struct clk *apb_clk;
  87. const char *map_name;
  88. };
  89. static irqreturn_t sunxi_ir_irq(int irqno, void *dev_id)
  90. {
  91. unsigned long status;
  92. unsigned char dt;
  93. unsigned int cnt, rc;
  94. struct sunxi_ir *ir = dev_id;
  95. DEFINE_IR_RAW_EVENT(rawir);
  96. spin_lock(&ir->ir_lock);
  97. status = readl(ir->base + SUNXI_IR_RXSTA_REG);
  98. /* clean all pending statuses */
  99. writel(status | REG_RXSTA_CLEARALL, ir->base + SUNXI_IR_RXSTA_REG);
  100. if (status & REG_RXINT_RAI_EN) {
  101. /* How many messages in fifo */
  102. rc = REG_RXSTA_GET_AC(status);
  103. /* Sanity check */
  104. rc = rc > SUNXI_IR_FIFO_SIZE ? SUNXI_IR_FIFO_SIZE : rc;
  105. /* If we have data */
  106. for (cnt = 0; cnt < rc; cnt++) {
  107. /* for each bit in fifo */
  108. dt = readb(ir->base + SUNXI_IR_RXFIFO_REG);
  109. rawir.pulse = (dt & 0x80) != 0;
  110. rawir.duration = ((dt & 0x7f) + 1) * SUNXI_IR_SAMPLE;
  111. ir_raw_event_store_with_filter(ir->rc, &rawir);
  112. }
  113. }
  114. if (status & REG_RXINT_ROI_EN) {
  115. ir_raw_event_reset(ir->rc);
  116. } else if (status & REG_RXINT_RPEI_EN) {
  117. ir_raw_event_set_idle(ir->rc, true);
  118. ir_raw_event_handle(ir->rc);
  119. }
  120. spin_unlock(&ir->ir_lock);
  121. return IRQ_HANDLED;
  122. }
  123. static int sunxi_ir_probe(struct platform_device *pdev)
  124. {
  125. int ret = 0;
  126. unsigned long tmp = 0;
  127. struct device *dev = &pdev->dev;
  128. struct device_node *dn = dev->of_node;
  129. struct resource *res;
  130. struct sunxi_ir *ir;
  131. ir = devm_kzalloc(dev, sizeof(struct sunxi_ir), GFP_KERNEL);
  132. if (!ir)
  133. return -ENOMEM;
  134. /* Clock */
  135. ir->apb_clk = devm_clk_get(dev, "apb");
  136. if (IS_ERR(ir->apb_clk)) {
  137. dev_err(dev, "failed to get a apb clock.\n");
  138. return PTR_ERR(ir->apb_clk);
  139. }
  140. ir->clk = devm_clk_get(dev, "ir");
  141. if (IS_ERR(ir->clk)) {
  142. dev_err(dev, "failed to get a ir clock.\n");
  143. return PTR_ERR(ir->clk);
  144. }
  145. ret = clk_set_rate(ir->clk, SUNXI_IR_BASE_CLK);
  146. if (ret) {
  147. dev_err(dev, "set ir base clock failed!\n");
  148. return ret;
  149. }
  150. if (clk_prepare_enable(ir->apb_clk)) {
  151. dev_err(dev, "try to enable apb_ir_clk failed\n");
  152. return -EINVAL;
  153. }
  154. if (clk_prepare_enable(ir->clk)) {
  155. dev_err(dev, "try to enable ir_clk failed\n");
  156. ret = -EINVAL;
  157. goto exit_clkdisable_apb_clk;
  158. }
  159. /* IO */
  160. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  161. ir->base = devm_ioremap_resource(dev, res);
  162. if (IS_ERR(ir->base)) {
  163. dev_err(dev, "failed to map registers\n");
  164. ret = PTR_ERR(ir->base);
  165. goto exit_clkdisable_clk;
  166. }
  167. ir->rc = rc_allocate_device();
  168. if (!ir->rc) {
  169. dev_err(dev, "failed to allocate device\n");
  170. ret = -ENOMEM;
  171. goto exit_clkdisable_clk;
  172. }
  173. ir->rc->priv = ir;
  174. ir->rc->input_name = SUNXI_IR_DEV;
  175. ir->rc->input_phys = "sunxi-ir/input0";
  176. ir->rc->input_id.bustype = BUS_HOST;
  177. ir->rc->input_id.vendor = 0x0001;
  178. ir->rc->input_id.product = 0x0001;
  179. ir->rc->input_id.version = 0x0100;
  180. ir->map_name = of_get_property(dn, "linux,rc-map-name", NULL);
  181. ir->rc->map_name = ir->map_name ?: RC_MAP_EMPTY;
  182. ir->rc->dev.parent = dev;
  183. ir->rc->driver_type = RC_DRIVER_IR_RAW;
  184. ir->rc->allowed_protocols = RC_BIT_ALL;
  185. ir->rc->rx_resolution = SUNXI_IR_SAMPLE;
  186. ir->rc->timeout = MS_TO_NS(SUNXI_IR_TIMEOUT);
  187. ir->rc->driver_name = SUNXI_IR_DEV;
  188. ret = rc_register_device(ir->rc);
  189. if (ret) {
  190. dev_err(dev, "failed to register rc device\n");
  191. goto exit_free_dev;
  192. }
  193. platform_set_drvdata(pdev, ir);
  194. /* IRQ */
  195. ir->irq = platform_get_irq(pdev, 0);
  196. if (ir->irq < 0) {
  197. dev_err(dev, "no irq resource\n");
  198. ret = ir->irq;
  199. goto exit_free_dev;
  200. }
  201. ret = devm_request_irq(dev, ir->irq, sunxi_ir_irq, 0, SUNXI_IR_DEV, ir);
  202. if (ret) {
  203. dev_err(dev, "failed request irq\n");
  204. goto exit_free_dev;
  205. }
  206. /* Enable CIR Mode */
  207. writel(REG_CTL_MD, ir->base+SUNXI_IR_CTL_REG);
  208. /* Set noise threshold and idle threshold */
  209. writel(REG_CIR_NTHR(SUNXI_IR_RXNOISE)|REG_CIR_ITHR(SUNXI_IR_RXIDLE),
  210. ir->base + SUNXI_IR_CIR_REG);
  211. /* Invert Input Signal */
  212. writel(REG_RXCTL_RPPI, ir->base + SUNXI_IR_RXCTL_REG);
  213. /* Clear All Rx Interrupt Status */
  214. writel(REG_RXSTA_CLEARALL, ir->base + SUNXI_IR_RXSTA_REG);
  215. /*
  216. * Enable IRQ on overflow, packet end, FIFO available with trigger
  217. * level
  218. */
  219. writel(REG_RXINT_ROI_EN | REG_RXINT_RPEI_EN |
  220. REG_RXINT_RAI_EN | REG_RXINT_RAL(TRIGGER_LEVEL - 1),
  221. ir->base + SUNXI_IR_RXINT_REG);
  222. /* Enable IR Module */
  223. tmp = readl(ir->base + SUNXI_IR_CTL_REG);
  224. writel(tmp | REG_CTL_GEN | REG_CTL_RXEN, ir->base + SUNXI_IR_CTL_REG);
  225. dev_info(dev, "initialized sunXi IR driver\n");
  226. return 0;
  227. exit_free_dev:
  228. rc_free_device(ir->rc);
  229. exit_clkdisable_clk:
  230. clk_disable_unprepare(ir->clk);
  231. exit_clkdisable_apb_clk:
  232. clk_disable_unprepare(ir->apb_clk);
  233. return ret;
  234. }
  235. static int sunxi_ir_remove(struct platform_device *pdev)
  236. {
  237. unsigned long flags;
  238. struct sunxi_ir *ir = platform_get_drvdata(pdev);
  239. clk_disable_unprepare(ir->clk);
  240. clk_disable_unprepare(ir->apb_clk);
  241. spin_lock_irqsave(&ir->ir_lock, flags);
  242. /* disable IR IRQ */
  243. writel(0, ir->base + SUNXI_IR_RXINT_REG);
  244. /* clear All Rx Interrupt Status */
  245. writel(REG_RXSTA_CLEARALL, ir->base + SUNXI_IR_RXSTA_REG);
  246. /* disable IR */
  247. writel(0, ir->base + SUNXI_IR_CTL_REG);
  248. spin_unlock_irqrestore(&ir->ir_lock, flags);
  249. rc_unregister_device(ir->rc);
  250. return 0;
  251. }
  252. static const struct of_device_id sunxi_ir_match[] = {
  253. { .compatible = "allwinner,sun4i-a10-ir", },
  254. {},
  255. };
  256. static struct platform_driver sunxi_ir_driver = {
  257. .probe = sunxi_ir_probe,
  258. .remove = sunxi_ir_remove,
  259. .driver = {
  260. .name = SUNXI_IR_DEV,
  261. .of_match_table = sunxi_ir_match,
  262. },
  263. };
  264. module_platform_driver(sunxi_ir_driver);
  265. MODULE_DESCRIPTION("Allwinner sunXi IR controller driver");
  266. MODULE_AUTHOR("Alexsey Shestacov <wingrime@linux-sunxi.org>");
  267. MODULE_LICENSE("GPL");