sunxi-cir.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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 */
  66. #define SUNXI_IR_BASE_CLK 8000000
  67. /* Frequency after IR internal divider */
  68. #define SUNXI_IR_CLK (SUNXI_IR_BASE_CLK / 64)
  69. /* Sample period in ns */
  70. #define SUNXI_IR_SAMPLE (1000000000ul / SUNXI_IR_CLK)
  71. /* Noise threshold in samples */
  72. #define SUNXI_IR_RXNOISE 1
  73. /* Idle Threshold in samples */
  74. #define SUNXI_IR_RXIDLE 20
  75. /* Time after which device stops sending data in ms */
  76. #define SUNXI_IR_TIMEOUT 120
  77. struct sunxi_ir {
  78. spinlock_t ir_lock;
  79. struct rc_dev *rc;
  80. void __iomem *base;
  81. int irq;
  82. int fifo_size;
  83. struct clk *clk;
  84. struct clk *apb_clk;
  85. struct reset_control *rst;
  86. const char *map_name;
  87. };
  88. static irqreturn_t sunxi_ir_irq(int irqno, void *dev_id)
  89. {
  90. unsigned long status;
  91. unsigned char dt;
  92. unsigned int cnt, rc;
  93. struct sunxi_ir *ir = dev_id;
  94. DEFINE_IR_RAW_EVENT(rawir);
  95. spin_lock(&ir->ir_lock);
  96. status = readl(ir->base + SUNXI_IR_RXSTA_REG);
  97. /* clean all pending statuses */
  98. writel(status | REG_RXSTA_CLEARALL, ir->base + SUNXI_IR_RXSTA_REG);
  99. if (status & (REG_RXINT_RAI_EN | REG_RXINT_RPEI_EN)) {
  100. /* How many messages in fifo */
  101. rc = REG_RXSTA_GET_AC(status);
  102. /* Sanity check */
  103. rc = rc > ir->fifo_size ? ir->fifo_size : rc;
  104. /* If we have data */
  105. for (cnt = 0; cnt < rc; cnt++) {
  106. /* for each bit in fifo */
  107. dt = readb(ir->base + SUNXI_IR_RXFIFO_REG);
  108. rawir.pulse = (dt & 0x80) != 0;
  109. rawir.duration = ((dt & 0x7f) + 1) * SUNXI_IR_SAMPLE;
  110. ir_raw_event_store_with_filter(ir->rc, &rawir);
  111. }
  112. }
  113. if (status & REG_RXINT_ROI_EN) {
  114. ir_raw_event_reset(ir->rc);
  115. } else if (status & REG_RXINT_RPEI_EN) {
  116. ir_raw_event_set_idle(ir->rc, true);
  117. ir_raw_event_handle(ir->rc);
  118. }
  119. spin_unlock(&ir->ir_lock);
  120. return IRQ_HANDLED;
  121. }
  122. static int sunxi_ir_probe(struct platform_device *pdev)
  123. {
  124. int ret = 0;
  125. unsigned long tmp = 0;
  126. struct device *dev = &pdev->dev;
  127. struct device_node *dn = dev->of_node;
  128. struct resource *res;
  129. struct sunxi_ir *ir;
  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. /* Reset (optional) */
  150. ir->rst = devm_reset_control_get_optional(dev, NULL);
  151. if (IS_ERR(ir->rst)) {
  152. ret = PTR_ERR(ir->rst);
  153. if (ret == -EPROBE_DEFER)
  154. return ret;
  155. ir->rst = NULL;
  156. } else {
  157. ret = reset_control_deassert(ir->rst);
  158. if (ret)
  159. return ret;
  160. }
  161. ret = clk_set_rate(ir->clk, SUNXI_IR_BASE_CLK);
  162. if (ret) {
  163. dev_err(dev, "set ir base clock failed!\n");
  164. goto exit_reset_assert;
  165. }
  166. if (clk_prepare_enable(ir->apb_clk)) {
  167. dev_err(dev, "try to enable apb_ir_clk failed\n");
  168. ret = -EINVAL;
  169. goto exit_reset_assert;
  170. }
  171. if (clk_prepare_enable(ir->clk)) {
  172. dev_err(dev, "try to enable ir_clk failed\n");
  173. ret = -EINVAL;
  174. goto exit_clkdisable_apb_clk;
  175. }
  176. /* IO */
  177. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  178. ir->base = devm_ioremap_resource(dev, res);
  179. if (IS_ERR(ir->base)) {
  180. dev_err(dev, "failed to map registers\n");
  181. ret = PTR_ERR(ir->base);
  182. goto exit_clkdisable_clk;
  183. }
  184. ir->rc = rc_allocate_device();
  185. if (!ir->rc) {
  186. dev_err(dev, "failed to allocate device\n");
  187. ret = -ENOMEM;
  188. goto exit_clkdisable_clk;
  189. }
  190. ir->rc->priv = ir;
  191. ir->rc->input_name = SUNXI_IR_DEV;
  192. ir->rc->input_phys = "sunxi-ir/input0";
  193. ir->rc->input_id.bustype = BUS_HOST;
  194. ir->rc->input_id.vendor = 0x0001;
  195. ir->rc->input_id.product = 0x0001;
  196. ir->rc->input_id.version = 0x0100;
  197. ir->map_name = of_get_property(dn, "linux,rc-map-name", NULL);
  198. ir->rc->map_name = ir->map_name ?: RC_MAP_EMPTY;
  199. ir->rc->dev.parent = dev;
  200. ir->rc->driver_type = RC_DRIVER_IR_RAW;
  201. ir->rc->allowed_protocols = RC_BIT_ALL;
  202. ir->rc->rx_resolution = SUNXI_IR_SAMPLE;
  203. ir->rc->timeout = MS_TO_NS(SUNXI_IR_TIMEOUT);
  204. ir->rc->driver_name = SUNXI_IR_DEV;
  205. ret = rc_register_device(ir->rc);
  206. if (ret) {
  207. dev_err(dev, "failed to register rc device\n");
  208. goto exit_free_dev;
  209. }
  210. platform_set_drvdata(pdev, ir);
  211. /* IRQ */
  212. ir->irq = platform_get_irq(pdev, 0);
  213. if (ir->irq < 0) {
  214. dev_err(dev, "no irq resource\n");
  215. ret = ir->irq;
  216. goto exit_free_dev;
  217. }
  218. ret = devm_request_irq(dev, ir->irq, sunxi_ir_irq, 0, SUNXI_IR_DEV, ir);
  219. if (ret) {
  220. dev_err(dev, "failed request irq\n");
  221. goto exit_free_dev;
  222. }
  223. /* Enable CIR Mode */
  224. writel(REG_CTL_MD, ir->base+SUNXI_IR_CTL_REG);
  225. /* Set noise threshold and idle threshold */
  226. writel(REG_CIR_NTHR(SUNXI_IR_RXNOISE)|REG_CIR_ITHR(SUNXI_IR_RXIDLE),
  227. ir->base + SUNXI_IR_CIR_REG);
  228. /* Invert Input Signal */
  229. writel(REG_RXCTL_RPPI, ir->base + SUNXI_IR_RXCTL_REG);
  230. /* Clear All Rx Interrupt Status */
  231. writel(REG_RXSTA_CLEARALL, ir->base + SUNXI_IR_RXSTA_REG);
  232. /*
  233. * Enable IRQ on overflow, packet end, FIFO available with trigger
  234. * level
  235. */
  236. writel(REG_RXINT_ROI_EN | REG_RXINT_RPEI_EN |
  237. REG_RXINT_RAI_EN | REG_RXINT_RAL(ir->fifo_size / 2 - 1),
  238. ir->base + SUNXI_IR_RXINT_REG);
  239. /* Enable IR Module */
  240. tmp = readl(ir->base + SUNXI_IR_CTL_REG);
  241. writel(tmp | REG_CTL_GEN | REG_CTL_RXEN, ir->base + SUNXI_IR_CTL_REG);
  242. dev_info(dev, "initialized sunXi IR driver\n");
  243. return 0;
  244. exit_free_dev:
  245. rc_free_device(ir->rc);
  246. exit_clkdisable_clk:
  247. clk_disable_unprepare(ir->clk);
  248. exit_clkdisable_apb_clk:
  249. clk_disable_unprepare(ir->apb_clk);
  250. exit_reset_assert:
  251. if (ir->rst)
  252. reset_control_assert(ir->rst);
  253. return ret;
  254. }
  255. static int sunxi_ir_remove(struct platform_device *pdev)
  256. {
  257. unsigned long flags;
  258. struct sunxi_ir *ir = platform_get_drvdata(pdev);
  259. clk_disable_unprepare(ir->clk);
  260. clk_disable_unprepare(ir->apb_clk);
  261. if (ir->rst)
  262. reset_control_assert(ir->rst);
  263. spin_lock_irqsave(&ir->ir_lock, flags);
  264. /* disable IR IRQ */
  265. writel(0, ir->base + SUNXI_IR_RXINT_REG);
  266. /* clear All Rx Interrupt Status */
  267. writel(REG_RXSTA_CLEARALL, ir->base + SUNXI_IR_RXSTA_REG);
  268. /* disable IR */
  269. writel(0, ir->base + SUNXI_IR_CTL_REG);
  270. spin_unlock_irqrestore(&ir->ir_lock, flags);
  271. rc_unregister_device(ir->rc);
  272. return 0;
  273. }
  274. static const struct of_device_id sunxi_ir_match[] = {
  275. { .compatible = "allwinner,sun4i-a10-ir", },
  276. { .compatible = "allwinner,sun5i-a13-ir", },
  277. {},
  278. };
  279. static struct platform_driver sunxi_ir_driver = {
  280. .probe = sunxi_ir_probe,
  281. .remove = sunxi_ir_remove,
  282. .driver = {
  283. .name = SUNXI_IR_DEV,
  284. .of_match_table = sunxi_ir_match,
  285. },
  286. };
  287. module_platform_driver(sunxi_ir_driver);
  288. MODULE_DESCRIPTION("Allwinner sunXi IR controller driver");
  289. MODULE_AUTHOR("Alexsey Shestacov <wingrime@linux-sunxi.org>");
  290. MODULE_LICENSE("GPL");