sunxi-cir.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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 <linux/reset.h>
  26. #include <media/rc-core.h>
  27. #define SUNXI_IR_DEV "sunxi-ir"
  28. /* Registers */
  29. /* IR Control */
  30. #define SUNXI_IR_CTL_REG 0x00
  31. /* Global Enable */
  32. #define REG_CTL_GEN BIT(0)
  33. /* RX block enable */
  34. #define REG_CTL_RXEN BIT(1)
  35. /* CIR mode */
  36. #define REG_CTL_MD (BIT(4) | BIT(5))
  37. /* Rx Config */
  38. #define SUNXI_IR_RXCTL_REG 0x10
  39. /* Pulse Polarity Invert flag */
  40. #define REG_RXCTL_RPPI BIT(2)
  41. /* Rx Data */
  42. #define SUNXI_IR_RXFIFO_REG 0x20
  43. /* Rx Interrupt Enable */
  44. #define SUNXI_IR_RXINT_REG 0x2C
  45. /* Rx FIFO Overflow */
  46. #define REG_RXINT_ROI_EN BIT(0)
  47. /* Rx Packet End */
  48. #define REG_RXINT_RPEI_EN BIT(1)
  49. /* Rx FIFO Data Available */
  50. #define REG_RXINT_RAI_EN BIT(4)
  51. /* Rx FIFO available byte level */
  52. #define REG_RXINT_RAL(val) ((val) << 8)
  53. /* Rx Interrupt Status */
  54. #define SUNXI_IR_RXSTA_REG 0x30
  55. /* RX FIFO Get Available Counter */
  56. #define REG_RXSTA_GET_AC(val) (((val) >> 8) & (ir->fifo_size * 2 - 1))
  57. /* Clear all interrupt status value */
  58. #define REG_RXSTA_CLEARALL 0xff
  59. /* IR Sample Config */
  60. #define SUNXI_IR_CIR_REG 0x34
  61. /* CIR_REG register noise threshold */
  62. #define REG_CIR_NTHR(val) (((val) << 2) & (GENMASK(7, 2)))
  63. /* CIR_REG register idle threshold */
  64. #define REG_CIR_ITHR(val) (((val) << 8) & (GENMASK(15, 8)))
  65. /* Required frequency for IR0 or IR1 clock in CIR mode (default) */
  66. #define SUNXI_IR_BASE_CLK 8000000
  67. /* Noise threshold in samples */
  68. #define SUNXI_IR_RXNOISE 1
  69. /* Idle Threshold in samples */
  70. #define SUNXI_IR_RXIDLE 20
  71. /* Time after which device stops sending data in ms */
  72. #define SUNXI_IR_TIMEOUT 120
  73. struct sunxi_ir {
  74. spinlock_t ir_lock;
  75. struct rc_dev *rc;
  76. void __iomem *base;
  77. int irq;
  78. int fifo_size;
  79. struct clk *clk;
  80. struct clk *apb_clk;
  81. struct reset_control *rst;
  82. const char *map_name;
  83. };
  84. static irqreturn_t sunxi_ir_irq(int irqno, void *dev_id)
  85. {
  86. unsigned long status;
  87. unsigned char dt;
  88. unsigned int cnt, rc;
  89. struct sunxi_ir *ir = dev_id;
  90. DEFINE_IR_RAW_EVENT(rawir);
  91. spin_lock(&ir->ir_lock);
  92. status = readl(ir->base + SUNXI_IR_RXSTA_REG);
  93. /* clean all pending statuses */
  94. writel(status | REG_RXSTA_CLEARALL, ir->base + SUNXI_IR_RXSTA_REG);
  95. if (status & (REG_RXINT_RAI_EN | REG_RXINT_RPEI_EN)) {
  96. /* How many messages in fifo */
  97. rc = REG_RXSTA_GET_AC(status);
  98. /* Sanity check */
  99. rc = rc > ir->fifo_size ? ir->fifo_size : rc;
  100. /* If we have data */
  101. for (cnt = 0; cnt < rc; cnt++) {
  102. /* for each bit in fifo */
  103. dt = readb(ir->base + SUNXI_IR_RXFIFO_REG);
  104. rawir.pulse = (dt & 0x80) != 0;
  105. rawir.duration = ((dt & 0x7f) + 1) *
  106. ir->rc->rx_resolution;
  107. ir_raw_event_store_with_filter(ir->rc, &rawir);
  108. }
  109. }
  110. if (status & REG_RXINT_ROI_EN) {
  111. ir_raw_event_reset(ir->rc);
  112. } else if (status & REG_RXINT_RPEI_EN) {
  113. ir_raw_event_set_idle(ir->rc, true);
  114. ir_raw_event_handle(ir->rc);
  115. } else {
  116. ir_raw_event_handle(ir->rc);
  117. }
  118. spin_unlock(&ir->ir_lock);
  119. return IRQ_HANDLED;
  120. }
  121. static int sunxi_ir_probe(struct platform_device *pdev)
  122. {
  123. int ret = 0;
  124. unsigned long tmp = 0;
  125. struct device *dev = &pdev->dev;
  126. struct device_node *dn = dev->of_node;
  127. struct resource *res;
  128. struct sunxi_ir *ir;
  129. u32 b_clk_freq = SUNXI_IR_BASE_CLK;
  130. ir = devm_kzalloc(dev, sizeof(struct sunxi_ir), GFP_KERNEL);
  131. if (!ir)
  132. return -ENOMEM;
  133. spin_lock_init(&ir->ir_lock);
  134. if (of_device_is_compatible(dn, "allwinner,sun5i-a13-ir"))
  135. ir->fifo_size = 64;
  136. else
  137. ir->fifo_size = 16;
  138. /* Clock */
  139. ir->apb_clk = devm_clk_get(dev, "apb");
  140. if (IS_ERR(ir->apb_clk)) {
  141. dev_err(dev, "failed to get a apb clock.\n");
  142. return PTR_ERR(ir->apb_clk);
  143. }
  144. ir->clk = devm_clk_get(dev, "ir");
  145. if (IS_ERR(ir->clk)) {
  146. dev_err(dev, "failed to get a ir clock.\n");
  147. return PTR_ERR(ir->clk);
  148. }
  149. /* Base clock frequency (optional) */
  150. of_property_read_u32(dn, "clock-frequency", &b_clk_freq);
  151. /* Reset (optional) */
  152. ir->rst = devm_reset_control_get_optional_exclusive(dev, NULL);
  153. if (IS_ERR(ir->rst))
  154. return PTR_ERR(ir->rst);
  155. ret = reset_control_deassert(ir->rst);
  156. if (ret)
  157. return ret;
  158. ret = clk_set_rate(ir->clk, b_clk_freq);
  159. if (ret) {
  160. dev_err(dev, "set ir base clock failed!\n");
  161. goto exit_reset_assert;
  162. }
  163. dev_dbg(dev, "set base clock frequency to %d Hz.\n", b_clk_freq);
  164. if (clk_prepare_enable(ir->apb_clk)) {
  165. dev_err(dev, "try to enable apb_ir_clk failed\n");
  166. ret = -EINVAL;
  167. goto exit_reset_assert;
  168. }
  169. if (clk_prepare_enable(ir->clk)) {
  170. dev_err(dev, "try to enable ir_clk failed\n");
  171. ret = -EINVAL;
  172. goto exit_clkdisable_apb_clk;
  173. }
  174. /* IO */
  175. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  176. ir->base = devm_ioremap_resource(dev, res);
  177. if (IS_ERR(ir->base)) {
  178. dev_err(dev, "failed to map registers\n");
  179. ret = PTR_ERR(ir->base);
  180. goto exit_clkdisable_clk;
  181. }
  182. ir->rc = rc_allocate_device(RC_DRIVER_IR_RAW);
  183. if (!ir->rc) {
  184. dev_err(dev, "failed to allocate device\n");
  185. ret = -ENOMEM;
  186. goto exit_clkdisable_clk;
  187. }
  188. ir->rc->priv = ir;
  189. ir->rc->device_name = SUNXI_IR_DEV;
  190. ir->rc->input_phys = "sunxi-ir/input0";
  191. ir->rc->input_id.bustype = BUS_HOST;
  192. ir->rc->input_id.vendor = 0x0001;
  193. ir->rc->input_id.product = 0x0001;
  194. ir->rc->input_id.version = 0x0100;
  195. ir->map_name = of_get_property(dn, "linux,rc-map-name", NULL);
  196. ir->rc->map_name = ir->map_name ?: RC_MAP_EMPTY;
  197. ir->rc->dev.parent = dev;
  198. ir->rc->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER;
  199. /* Frequency after IR internal divider with sample period in ns */
  200. ir->rc->rx_resolution = (1000000000ul / (b_clk_freq / 64));
  201. ir->rc->timeout = MS_TO_NS(SUNXI_IR_TIMEOUT);
  202. ir->rc->driver_name = SUNXI_IR_DEV;
  203. ret = rc_register_device(ir->rc);
  204. if (ret) {
  205. dev_err(dev, "failed to register rc device\n");
  206. goto exit_free_dev;
  207. }
  208. platform_set_drvdata(pdev, ir);
  209. /* IRQ */
  210. ir->irq = platform_get_irq(pdev, 0);
  211. if (ir->irq < 0) {
  212. dev_err(dev, "no irq resource\n");
  213. ret = ir->irq;
  214. goto exit_free_dev;
  215. }
  216. ret = devm_request_irq(dev, ir->irq, sunxi_ir_irq, 0, SUNXI_IR_DEV, ir);
  217. if (ret) {
  218. dev_err(dev, "failed request irq\n");
  219. goto exit_free_dev;
  220. }
  221. /* Enable CIR Mode */
  222. writel(REG_CTL_MD, ir->base+SUNXI_IR_CTL_REG);
  223. /* Set noise threshold and idle threshold */
  224. writel(REG_CIR_NTHR(SUNXI_IR_RXNOISE)|REG_CIR_ITHR(SUNXI_IR_RXIDLE),
  225. ir->base + SUNXI_IR_CIR_REG);
  226. /* Invert Input Signal */
  227. writel(REG_RXCTL_RPPI, ir->base + SUNXI_IR_RXCTL_REG);
  228. /* Clear All Rx Interrupt Status */
  229. writel(REG_RXSTA_CLEARALL, ir->base + SUNXI_IR_RXSTA_REG);
  230. /*
  231. * Enable IRQ on overflow, packet end, FIFO available with trigger
  232. * level
  233. */
  234. writel(REG_RXINT_ROI_EN | REG_RXINT_RPEI_EN |
  235. REG_RXINT_RAI_EN | REG_RXINT_RAL(ir->fifo_size / 2 - 1),
  236. ir->base + SUNXI_IR_RXINT_REG);
  237. /* Enable IR Module */
  238. tmp = readl(ir->base + SUNXI_IR_CTL_REG);
  239. writel(tmp | REG_CTL_GEN | REG_CTL_RXEN, ir->base + SUNXI_IR_CTL_REG);
  240. dev_info(dev, "initialized sunXi IR driver\n");
  241. return 0;
  242. exit_free_dev:
  243. rc_free_device(ir->rc);
  244. exit_clkdisable_clk:
  245. clk_disable_unprepare(ir->clk);
  246. exit_clkdisable_apb_clk:
  247. clk_disable_unprepare(ir->apb_clk);
  248. exit_reset_assert:
  249. reset_control_assert(ir->rst);
  250. return ret;
  251. }
  252. static int sunxi_ir_remove(struct platform_device *pdev)
  253. {
  254. unsigned long flags;
  255. struct sunxi_ir *ir = platform_get_drvdata(pdev);
  256. clk_disable_unprepare(ir->clk);
  257. clk_disable_unprepare(ir->apb_clk);
  258. reset_control_assert(ir->rst);
  259. spin_lock_irqsave(&ir->ir_lock, flags);
  260. /* disable IR IRQ */
  261. writel(0, ir->base + SUNXI_IR_RXINT_REG);
  262. /* clear All Rx Interrupt Status */
  263. writel(REG_RXSTA_CLEARALL, ir->base + SUNXI_IR_RXSTA_REG);
  264. /* disable IR */
  265. writel(0, ir->base + SUNXI_IR_CTL_REG);
  266. spin_unlock_irqrestore(&ir->ir_lock, flags);
  267. rc_unregister_device(ir->rc);
  268. return 0;
  269. }
  270. static const struct of_device_id sunxi_ir_match[] = {
  271. { .compatible = "allwinner,sun4i-a10-ir", },
  272. { .compatible = "allwinner,sun5i-a13-ir", },
  273. {},
  274. };
  275. MODULE_DEVICE_TABLE(of, sunxi_ir_match);
  276. static struct platform_driver sunxi_ir_driver = {
  277. .probe = sunxi_ir_probe,
  278. .remove = sunxi_ir_remove,
  279. .driver = {
  280. .name = SUNXI_IR_DEV,
  281. .of_match_table = sunxi_ir_match,
  282. },
  283. };
  284. module_platform_driver(sunxi_ir_driver);
  285. MODULE_DESCRIPTION("Allwinner sunXi IR controller driver");
  286. MODULE_AUTHOR("Alexsey Shestacov <wingrime@linux-sunxi.org>");
  287. MODULE_LICENSE("GPL");