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