sunxi-cir.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Driver for Allwinner sunXi IR controller
  4. *
  5. * Copyright (C) 2014 Alexsey Shestacov <wingrime@linux-sunxi.org>
  6. * Copyright (C) 2014 Alexander Bersenev <bay@hackerdom.ru>
  7. *
  8. * Based on sun5i-ir.c:
  9. * Copyright (C) 2007-2012 Daniel Wang
  10. * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
  11. */
  12. #include <linux/clk.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/io.h>
  15. #include <linux/module.h>
  16. #include <linux/of.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/reset.h>
  19. #include <media/rc-core.h>
  20. #define SUNXI_IR_DEV "sunxi-ir"
  21. /* Registers */
  22. /* IR Control */
  23. #define SUNXI_IR_CTL_REG 0x00
  24. /* Global Enable */
  25. #define REG_CTL_GEN BIT(0)
  26. /* RX block enable */
  27. #define REG_CTL_RXEN BIT(1)
  28. /* CIR mode */
  29. #define REG_CTL_MD (BIT(4) | BIT(5))
  30. /* Rx Config */
  31. #define SUNXI_IR_RXCTL_REG 0x10
  32. /* Pulse Polarity Invert flag */
  33. #define REG_RXCTL_RPPI BIT(2)
  34. /* Rx Data */
  35. #define SUNXI_IR_RXFIFO_REG 0x20
  36. /* Rx Interrupt Enable */
  37. #define SUNXI_IR_RXINT_REG 0x2C
  38. /* Rx FIFO Overflow Interrupt Enable */
  39. #define REG_RXINT_ROI_EN BIT(0)
  40. /* Rx Packet End Interrupt Enable */
  41. #define REG_RXINT_RPEI_EN BIT(1)
  42. /* Rx FIFO Data Available Interrupt Enable */
  43. #define REG_RXINT_RAI_EN BIT(4)
  44. /* Rx FIFO available byte level */
  45. #define REG_RXINT_RAL(val) ((val) << 8)
  46. /* Rx Interrupt Status */
  47. #define SUNXI_IR_RXSTA_REG 0x30
  48. /* Rx FIFO Overflow */
  49. #define REG_RXSTA_ROI REG_RXINT_ROI_EN
  50. /* Rx Packet End */
  51. #define REG_RXSTA_RPE REG_RXINT_RPEI_EN
  52. /* Rx FIFO Data Available */
  53. #define REG_RXSTA_RA REG_RXINT_RAI_EN
  54. /* RX FIFO Get Available Counter */
  55. #define REG_RXSTA_GET_AC(val) (((val) >> 8) & (ir->fifo_size * 2 - 1))
  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. /* Required frequency for IR0 or IR1 clock in CIR mode (default) */
  65. #define SUNXI_IR_BASE_CLK 8000000
  66. /* Noise threshold in samples */
  67. #define SUNXI_IR_RXNOISE 1
  68. /**
  69. * struct sunxi_ir_quirks - Differences between SoC variants.
  70. *
  71. * @has_reset: SoC needs reset deasserted.
  72. * @fifo_size: size of the fifo.
  73. */
  74. struct sunxi_ir_quirks {
  75. bool has_reset;
  76. int fifo_size;
  77. };
  78. struct sunxi_ir {
  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. struct ir_raw_event rawir = {};
  95. status = readl(ir->base + SUNXI_IR_RXSTA_REG);
  96. /* clean all pending statuses */
  97. writel(status | REG_RXSTA_CLEARALL, ir->base + SUNXI_IR_RXSTA_REG);
  98. if (status & (REG_RXSTA_RA | REG_RXSTA_RPE)) {
  99. /* How many messages in fifo */
  100. rc = REG_RXSTA_GET_AC(status);
  101. /* Sanity check */
  102. rc = rc > ir->fifo_size ? ir->fifo_size : rc;
  103. /* If we have data */
  104. for (cnt = 0; cnt < rc; cnt++) {
  105. /* for each bit in fifo */
  106. dt = readb(ir->base + SUNXI_IR_RXFIFO_REG);
  107. rawir.pulse = (dt & 0x80) != 0;
  108. rawir.duration = ((dt & 0x7f) + 1) *
  109. ir->rc->rx_resolution;
  110. ir_raw_event_store_with_filter(ir->rc, &rawir);
  111. }
  112. }
  113. if (status & REG_RXSTA_ROI) {
  114. ir_raw_event_overflow(ir->rc);
  115. } else if (status & REG_RXSTA_RPE) {
  116. ir_raw_event_set_idle(ir->rc, true);
  117. ir_raw_event_handle(ir->rc);
  118. } else {
  119. ir_raw_event_handle(ir->rc);
  120. }
  121. return IRQ_HANDLED;
  122. }
  123. /* Convert idle threshold to usec */
  124. static unsigned int sunxi_ithr_to_usec(unsigned int base_clk, unsigned int ithr)
  125. {
  126. return DIV_ROUND_CLOSEST(USEC_PER_SEC * (ithr + 1),
  127. base_clk / (128 * 64));
  128. }
  129. /* Convert usec to idle threshold */
  130. static unsigned int sunxi_usec_to_ithr(unsigned int base_clk, unsigned int usec)
  131. {
  132. /* make sure we don't end up with a timeout less than requested */
  133. return DIV_ROUND_UP((base_clk / (128 * 64)) * usec, USEC_PER_SEC) - 1;
  134. }
  135. static int sunxi_ir_set_timeout(struct rc_dev *rc_dev, unsigned int timeout)
  136. {
  137. struct sunxi_ir *ir = rc_dev->priv;
  138. unsigned int base_clk = clk_get_rate(ir->clk);
  139. unsigned int ithr = sunxi_usec_to_ithr(base_clk, timeout);
  140. dev_dbg(rc_dev->dev.parent, "setting idle threshold to %u\n", ithr);
  141. /* Set noise threshold and idle threshold */
  142. writel(REG_CIR_NTHR(SUNXI_IR_RXNOISE) | REG_CIR_ITHR(ithr),
  143. ir->base + SUNXI_IR_CIR_REG);
  144. rc_dev->timeout = sunxi_ithr_to_usec(base_clk, ithr);
  145. return 0;
  146. }
  147. static int sunxi_ir_hw_init(struct device *dev)
  148. {
  149. struct sunxi_ir *ir = dev_get_drvdata(dev);
  150. u32 tmp;
  151. int ret;
  152. ret = reset_control_deassert(ir->rst);
  153. if (ret)
  154. return ret;
  155. ret = clk_prepare_enable(ir->apb_clk);
  156. if (ret) {
  157. dev_err(dev, "failed to enable apb clk\n");
  158. goto exit_assert_reset;
  159. }
  160. ret = clk_prepare_enable(ir->clk);
  161. if (ret) {
  162. dev_err(dev, "failed to enable ir clk\n");
  163. goto exit_disable_apb_clk;
  164. }
  165. /* Enable CIR Mode */
  166. writel(REG_CTL_MD, ir->base + SUNXI_IR_CTL_REG);
  167. /* Set noise threshold and idle threshold */
  168. sunxi_ir_set_timeout(ir->rc, ir->rc->timeout);
  169. /* Invert Input Signal */
  170. writel(REG_RXCTL_RPPI, ir->base + SUNXI_IR_RXCTL_REG);
  171. /* Clear All Rx Interrupt Status */
  172. writel(REG_RXSTA_CLEARALL, ir->base + SUNXI_IR_RXSTA_REG);
  173. /*
  174. * Enable IRQ on overflow, packet end, FIFO available with trigger
  175. * level
  176. */
  177. writel(REG_RXINT_ROI_EN | REG_RXINT_RPEI_EN |
  178. REG_RXINT_RAI_EN | REG_RXINT_RAL(ir->fifo_size / 2 - 1),
  179. ir->base + SUNXI_IR_RXINT_REG);
  180. /* Enable IR Module */
  181. tmp = readl(ir->base + SUNXI_IR_CTL_REG);
  182. writel(tmp | REG_CTL_GEN | REG_CTL_RXEN, ir->base + SUNXI_IR_CTL_REG);
  183. return 0;
  184. exit_disable_apb_clk:
  185. clk_disable_unprepare(ir->apb_clk);
  186. exit_assert_reset:
  187. reset_control_assert(ir->rst);
  188. return ret;
  189. }
  190. static void sunxi_ir_hw_exit(struct device *dev)
  191. {
  192. struct sunxi_ir *ir = dev_get_drvdata(dev);
  193. clk_disable_unprepare(ir->clk);
  194. clk_disable_unprepare(ir->apb_clk);
  195. reset_control_assert(ir->rst);
  196. }
  197. static int __maybe_unused sunxi_ir_suspend(struct device *dev)
  198. {
  199. sunxi_ir_hw_exit(dev);
  200. return 0;
  201. }
  202. static int __maybe_unused sunxi_ir_resume(struct device *dev)
  203. {
  204. return sunxi_ir_hw_init(dev);
  205. }
  206. static SIMPLE_DEV_PM_OPS(sunxi_ir_pm_ops, sunxi_ir_suspend, sunxi_ir_resume);
  207. static int sunxi_ir_probe(struct platform_device *pdev)
  208. {
  209. int ret = 0;
  210. struct device *dev = &pdev->dev;
  211. struct device_node *dn = dev->of_node;
  212. const struct sunxi_ir_quirks *quirks;
  213. struct sunxi_ir *ir;
  214. u32 b_clk_freq = SUNXI_IR_BASE_CLK;
  215. ir = devm_kzalloc(dev, sizeof(struct sunxi_ir), GFP_KERNEL);
  216. if (!ir)
  217. return -ENOMEM;
  218. quirks = of_device_get_match_data(&pdev->dev);
  219. if (!quirks) {
  220. dev_err(&pdev->dev, "Failed to determine the quirks to use\n");
  221. return -ENODEV;
  222. }
  223. ir->fifo_size = quirks->fifo_size;
  224. /* Clock */
  225. ir->apb_clk = devm_clk_get(dev, "apb");
  226. if (IS_ERR(ir->apb_clk)) {
  227. dev_err(dev, "failed to get a apb clock.\n");
  228. return PTR_ERR(ir->apb_clk);
  229. }
  230. ir->clk = devm_clk_get(dev, "ir");
  231. if (IS_ERR(ir->clk)) {
  232. dev_err(dev, "failed to get a ir clock.\n");
  233. return PTR_ERR(ir->clk);
  234. }
  235. /* Base clock frequency (optional) */
  236. of_property_read_u32(dn, "clock-frequency", &b_clk_freq);
  237. /* Reset */
  238. if (quirks->has_reset) {
  239. ir->rst = devm_reset_control_get_exclusive(dev, NULL);
  240. if (IS_ERR(ir->rst))
  241. return PTR_ERR(ir->rst);
  242. }
  243. ret = clk_set_rate(ir->clk, b_clk_freq);
  244. if (ret) {
  245. dev_err(dev, "set ir base clock failed!\n");
  246. return ret;
  247. }
  248. dev_dbg(dev, "set base clock frequency to %d Hz.\n", b_clk_freq);
  249. /* IO */
  250. ir->base = devm_platform_ioremap_resource(pdev, 0);
  251. if (IS_ERR(ir->base)) {
  252. return PTR_ERR(ir->base);
  253. }
  254. ir->rc = rc_allocate_device(RC_DRIVER_IR_RAW);
  255. if (!ir->rc) {
  256. dev_err(dev, "failed to allocate device\n");
  257. return -ENOMEM;
  258. }
  259. ir->rc->priv = ir;
  260. ir->rc->device_name = SUNXI_IR_DEV;
  261. ir->rc->input_phys = "sunxi-ir/input0";
  262. ir->rc->input_id.bustype = BUS_HOST;
  263. ir->rc->input_id.vendor = 0x0001;
  264. ir->rc->input_id.product = 0x0001;
  265. ir->rc->input_id.version = 0x0100;
  266. ir->map_name = of_get_property(dn, "linux,rc-map-name", NULL);
  267. ir->rc->map_name = ir->map_name ?: RC_MAP_EMPTY;
  268. ir->rc->dev.parent = dev;
  269. ir->rc->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER;
  270. /* Frequency after IR internal divider with sample period in us */
  271. ir->rc->rx_resolution = (USEC_PER_SEC / (b_clk_freq / 64));
  272. ir->rc->timeout = IR_DEFAULT_TIMEOUT;
  273. ir->rc->min_timeout = sunxi_ithr_to_usec(b_clk_freq, 0);
  274. ir->rc->max_timeout = sunxi_ithr_to_usec(b_clk_freq, 255);
  275. ir->rc->s_timeout = sunxi_ir_set_timeout;
  276. ir->rc->driver_name = SUNXI_IR_DEV;
  277. ret = rc_register_device(ir->rc);
  278. if (ret) {
  279. dev_err(dev, "failed to register rc device\n");
  280. goto exit_free_dev;
  281. }
  282. platform_set_drvdata(pdev, ir);
  283. /* IRQ */
  284. ir->irq = platform_get_irq(pdev, 0);
  285. if (ir->irq < 0) {
  286. ret = ir->irq;
  287. goto exit_free_dev;
  288. }
  289. ret = devm_request_irq(dev, ir->irq, sunxi_ir_irq, 0, SUNXI_IR_DEV, ir);
  290. if (ret) {
  291. dev_err(dev, "failed request irq\n");
  292. goto exit_free_dev;
  293. }
  294. ret = sunxi_ir_hw_init(dev);
  295. if (ret)
  296. goto exit_free_dev;
  297. dev_info(dev, "initialized sunXi IR driver\n");
  298. return 0;
  299. exit_free_dev:
  300. rc_free_device(ir->rc);
  301. return ret;
  302. }
  303. static void sunxi_ir_remove(struct platform_device *pdev)
  304. {
  305. struct sunxi_ir *ir = platform_get_drvdata(pdev);
  306. rc_unregister_device(ir->rc);
  307. sunxi_ir_hw_exit(&pdev->dev);
  308. }
  309. static void sunxi_ir_shutdown(struct platform_device *pdev)
  310. {
  311. sunxi_ir_hw_exit(&pdev->dev);
  312. }
  313. static const struct sunxi_ir_quirks sun4i_a10_ir_quirks = {
  314. .has_reset = false,
  315. .fifo_size = 16,
  316. };
  317. static const struct sunxi_ir_quirks sun5i_a13_ir_quirks = {
  318. .has_reset = false,
  319. .fifo_size = 64,
  320. };
  321. static const struct sunxi_ir_quirks sun6i_a31_ir_quirks = {
  322. .has_reset = true,
  323. .fifo_size = 64,
  324. };
  325. static const struct of_device_id sunxi_ir_match[] = {
  326. {
  327. .compatible = "allwinner,sun4i-a10-ir",
  328. .data = &sun4i_a10_ir_quirks,
  329. },
  330. {
  331. .compatible = "allwinner,sun5i-a13-ir",
  332. .data = &sun5i_a13_ir_quirks,
  333. },
  334. {
  335. .compatible = "allwinner,sun6i-a31-ir",
  336. .data = &sun6i_a31_ir_quirks,
  337. },
  338. {}
  339. };
  340. MODULE_DEVICE_TABLE(of, sunxi_ir_match);
  341. static struct platform_driver sunxi_ir_driver = {
  342. .probe = sunxi_ir_probe,
  343. .remove = sunxi_ir_remove,
  344. .shutdown = sunxi_ir_shutdown,
  345. .driver = {
  346. .name = SUNXI_IR_DEV,
  347. .of_match_table = sunxi_ir_match,
  348. .pm = &sunxi_ir_pm_ops,
  349. },
  350. };
  351. module_platform_driver(sunxi_ir_driver);
  352. MODULE_DESCRIPTION("Allwinner sunXi IR controller driver");
  353. MODULE_AUTHOR("Alexsey Shestacov <wingrime@linux-sunxi.org>");
  354. MODULE_LICENSE("GPL");