st_rc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2013 STMicroelectronics Limited
  4. * Author: Srinivas Kandagatla <[email protected]>
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/clk.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/module.h>
  10. #include <linux/of.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/reset.h>
  13. #include <media/rc-core.h>
  14. #include <linux/pinctrl/consumer.h>
  15. #include <linux/pm_wakeirq.h>
  16. struct st_rc_device {
  17. struct device *dev;
  18. int irq;
  19. int irq_wake;
  20. struct clk *sys_clock;
  21. void __iomem *base; /* Register base address */
  22. void __iomem *rx_base;/* RX Register base address */
  23. struct rc_dev *rdev;
  24. bool overclocking;
  25. int sample_mult;
  26. int sample_div;
  27. bool rxuhfmode;
  28. struct reset_control *rstc;
  29. };
  30. /* Registers */
  31. #define IRB_SAMPLE_RATE_COMM 0x64 /* sample freq divisor*/
  32. #define IRB_CLOCK_SEL 0x70 /* clock select */
  33. #define IRB_CLOCK_SEL_STATUS 0x74 /* clock status */
  34. /* IRB IR/UHF receiver registers */
  35. #define IRB_RX_ON 0x40 /* pulse time capture */
  36. #define IRB_RX_SYS 0X44 /* sym period capture */
  37. #define IRB_RX_INT_EN 0x48 /* IRQ enable (R/W) */
  38. #define IRB_RX_INT_STATUS 0x4c /* IRQ status (R/W) */
  39. #define IRB_RX_EN 0x50 /* Receive enable */
  40. #define IRB_MAX_SYM_PERIOD 0x54 /* max sym value */
  41. #define IRB_RX_INT_CLEAR 0x58 /* overrun status */
  42. #define IRB_RX_STATUS 0x6c /* receive status */
  43. #define IRB_RX_NOISE_SUPPR 0x5c /* noise suppression */
  44. #define IRB_RX_POLARITY_INV 0x68 /* polarity inverter */
  45. /*
  46. * IRQ set: Enable full FIFO 1 -> bit 3;
  47. * Enable overrun IRQ 1 -> bit 2;
  48. * Enable last symbol IRQ 1 -> bit 1:
  49. * Enable RX interrupt 1 -> bit 0;
  50. */
  51. #define IRB_RX_INTS 0x0f
  52. #define IRB_RX_OVERRUN_INT 0x04
  53. /* maximum symbol period (microsecs),timeout to detect end of symbol train */
  54. #define MAX_SYMB_TIME 0x5000
  55. #define IRB_SAMPLE_FREQ 10000000
  56. #define IRB_FIFO_NOT_EMPTY 0xff00
  57. #define IRB_OVERFLOW 0x4
  58. #define IRB_TIMEOUT 0xffff
  59. #define IR_ST_NAME "st-rc"
  60. static void st_rc_send_lirc_timeout(struct rc_dev *rdev)
  61. {
  62. struct ir_raw_event ev = { .timeout = true, .duration = rdev->timeout };
  63. ir_raw_event_store(rdev, &ev);
  64. }
  65. /*
  66. * RX graphical example to better understand the difference between ST IR block
  67. * output and standard definition used by LIRC (and most of the world!)
  68. *
  69. * mark mark
  70. * |-IRB_RX_ON-| |-IRB_RX_ON-|
  71. * ___ ___ ___ ___ ___ ___ _
  72. * | | | | | | | | | | | | |
  73. * | | | | | | space 0 | | | | | | space 1 |
  74. * _____| |__| |__| |____________________________| |__| |__| |_____________|
  75. *
  76. * |--------------- IRB_RX_SYS -------------|------ IRB_RX_SYS -------|
  77. *
  78. * |------------- encoding bit 0 -----------|---- encoding bit 1 -----|
  79. *
  80. * ST hardware returns mark (IRB_RX_ON) and total symbol time (IRB_RX_SYS), so
  81. * convert to standard mark/space we have to calculate space=(IRB_RX_SYS-mark)
  82. * The mark time represents the amount of time the carrier (usually 36-40kHz)
  83. * is detected.The above examples shows Pulse Width Modulation encoding where
  84. * bit 0 is represented by space>mark.
  85. */
  86. static irqreturn_t st_rc_rx_interrupt(int irq, void *data)
  87. {
  88. unsigned long timeout;
  89. unsigned int symbol, mark = 0;
  90. struct st_rc_device *dev = data;
  91. int last_symbol = 0;
  92. u32 status, int_status;
  93. struct ir_raw_event ev = {};
  94. if (dev->irq_wake)
  95. pm_wakeup_event(dev->dev, 0);
  96. /* FIXME: is 10ms good enough ? */
  97. timeout = jiffies + msecs_to_jiffies(10);
  98. do {
  99. status = readl(dev->rx_base + IRB_RX_STATUS);
  100. if (!(status & (IRB_FIFO_NOT_EMPTY | IRB_OVERFLOW)))
  101. break;
  102. int_status = readl(dev->rx_base + IRB_RX_INT_STATUS);
  103. if (unlikely(int_status & IRB_RX_OVERRUN_INT)) {
  104. /* discard the entire collection in case of errors! */
  105. ir_raw_event_overflow(dev->rdev);
  106. dev_info(dev->dev, "IR RX overrun\n");
  107. writel(IRB_RX_OVERRUN_INT,
  108. dev->rx_base + IRB_RX_INT_CLEAR);
  109. continue;
  110. }
  111. symbol = readl(dev->rx_base + IRB_RX_SYS);
  112. mark = readl(dev->rx_base + IRB_RX_ON);
  113. if (symbol == IRB_TIMEOUT)
  114. last_symbol = 1;
  115. /* Ignore any noise */
  116. if ((mark > 2) && (symbol > 1)) {
  117. symbol -= mark;
  118. if (dev->overclocking) { /* adjustments to timings */
  119. symbol *= dev->sample_mult;
  120. symbol /= dev->sample_div;
  121. mark *= dev->sample_mult;
  122. mark /= dev->sample_div;
  123. }
  124. ev.duration = mark;
  125. ev.pulse = true;
  126. ir_raw_event_store(dev->rdev, &ev);
  127. if (!last_symbol) {
  128. ev.duration = symbol;
  129. ev.pulse = false;
  130. ir_raw_event_store(dev->rdev, &ev);
  131. } else {
  132. st_rc_send_lirc_timeout(dev->rdev);
  133. }
  134. }
  135. last_symbol = 0;
  136. } while (time_is_after_jiffies(timeout));
  137. writel(IRB_RX_INTS, dev->rx_base + IRB_RX_INT_CLEAR);
  138. /* Empty software fifo */
  139. ir_raw_event_handle(dev->rdev);
  140. return IRQ_HANDLED;
  141. }
  142. static int st_rc_hardware_init(struct st_rc_device *dev)
  143. {
  144. int ret;
  145. int baseclock, freqdiff;
  146. unsigned int rx_max_symbol_per = MAX_SYMB_TIME;
  147. unsigned int rx_sampling_freq_div;
  148. /* Enable the IP */
  149. reset_control_deassert(dev->rstc);
  150. ret = clk_prepare_enable(dev->sys_clock);
  151. if (ret) {
  152. dev_err(dev->dev, "Failed to prepare/enable system clock\n");
  153. return ret;
  154. }
  155. baseclock = clk_get_rate(dev->sys_clock);
  156. /* IRB input pins are inverted internally from high to low. */
  157. writel(1, dev->rx_base + IRB_RX_POLARITY_INV);
  158. rx_sampling_freq_div = baseclock / IRB_SAMPLE_FREQ;
  159. writel(rx_sampling_freq_div, dev->base + IRB_SAMPLE_RATE_COMM);
  160. freqdiff = baseclock - (rx_sampling_freq_div * IRB_SAMPLE_FREQ);
  161. if (freqdiff) { /* over clocking, workout the adjustment factors */
  162. dev->overclocking = true;
  163. dev->sample_mult = 1000;
  164. dev->sample_div = baseclock / (10000 * rx_sampling_freq_div);
  165. rx_max_symbol_per = (rx_max_symbol_per * 1000)/dev->sample_div;
  166. }
  167. writel(rx_max_symbol_per, dev->rx_base + IRB_MAX_SYM_PERIOD);
  168. return 0;
  169. }
  170. static int st_rc_remove(struct platform_device *pdev)
  171. {
  172. struct st_rc_device *rc_dev = platform_get_drvdata(pdev);
  173. dev_pm_clear_wake_irq(&pdev->dev);
  174. device_init_wakeup(&pdev->dev, false);
  175. clk_disable_unprepare(rc_dev->sys_clock);
  176. rc_unregister_device(rc_dev->rdev);
  177. return 0;
  178. }
  179. static int st_rc_open(struct rc_dev *rdev)
  180. {
  181. struct st_rc_device *dev = rdev->priv;
  182. unsigned long flags;
  183. local_irq_save(flags);
  184. /* enable interrupts and receiver */
  185. writel(IRB_RX_INTS, dev->rx_base + IRB_RX_INT_EN);
  186. writel(0x01, dev->rx_base + IRB_RX_EN);
  187. local_irq_restore(flags);
  188. return 0;
  189. }
  190. static void st_rc_close(struct rc_dev *rdev)
  191. {
  192. struct st_rc_device *dev = rdev->priv;
  193. /* disable interrupts and receiver */
  194. writel(0x00, dev->rx_base + IRB_RX_EN);
  195. writel(0x00, dev->rx_base + IRB_RX_INT_EN);
  196. }
  197. static int st_rc_probe(struct platform_device *pdev)
  198. {
  199. int ret = -EINVAL;
  200. struct rc_dev *rdev;
  201. struct device *dev = &pdev->dev;
  202. struct st_rc_device *rc_dev;
  203. struct device_node *np = pdev->dev.of_node;
  204. const char *rx_mode;
  205. rc_dev = devm_kzalloc(dev, sizeof(struct st_rc_device), GFP_KERNEL);
  206. if (!rc_dev)
  207. return -ENOMEM;
  208. rdev = rc_allocate_device(RC_DRIVER_IR_RAW);
  209. if (!rdev)
  210. return -ENOMEM;
  211. if (np && !of_property_read_string(np, "rx-mode", &rx_mode)) {
  212. if (!strcmp(rx_mode, "uhf")) {
  213. rc_dev->rxuhfmode = true;
  214. } else if (!strcmp(rx_mode, "infrared")) {
  215. rc_dev->rxuhfmode = false;
  216. } else {
  217. dev_err(dev, "Unsupported rx mode [%s]\n", rx_mode);
  218. goto err;
  219. }
  220. } else {
  221. goto err;
  222. }
  223. rc_dev->sys_clock = devm_clk_get(dev, NULL);
  224. if (IS_ERR(rc_dev->sys_clock)) {
  225. dev_err(dev, "System clock not found\n");
  226. ret = PTR_ERR(rc_dev->sys_clock);
  227. goto err;
  228. }
  229. rc_dev->irq = platform_get_irq(pdev, 0);
  230. if (rc_dev->irq < 0) {
  231. ret = rc_dev->irq;
  232. goto err;
  233. }
  234. rc_dev->base = devm_platform_ioremap_resource(pdev, 0);
  235. if (IS_ERR(rc_dev->base)) {
  236. ret = PTR_ERR(rc_dev->base);
  237. goto err;
  238. }
  239. if (rc_dev->rxuhfmode)
  240. rc_dev->rx_base = rc_dev->base + 0x40;
  241. else
  242. rc_dev->rx_base = rc_dev->base;
  243. rc_dev->rstc = reset_control_get_optional_exclusive(dev, NULL);
  244. if (IS_ERR(rc_dev->rstc)) {
  245. ret = PTR_ERR(rc_dev->rstc);
  246. goto err;
  247. }
  248. rc_dev->dev = dev;
  249. platform_set_drvdata(pdev, rc_dev);
  250. ret = st_rc_hardware_init(rc_dev);
  251. if (ret)
  252. goto err;
  253. rdev->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER;
  254. /* rx sampling rate is 10Mhz */
  255. rdev->rx_resolution = 100;
  256. rdev->timeout = MAX_SYMB_TIME;
  257. rdev->priv = rc_dev;
  258. rdev->open = st_rc_open;
  259. rdev->close = st_rc_close;
  260. rdev->driver_name = IR_ST_NAME;
  261. rdev->map_name = RC_MAP_EMPTY;
  262. rdev->device_name = "ST Remote Control Receiver";
  263. ret = rc_register_device(rdev);
  264. if (ret < 0)
  265. goto clkerr;
  266. rc_dev->rdev = rdev;
  267. if (devm_request_irq(dev, rc_dev->irq, st_rc_rx_interrupt,
  268. 0, IR_ST_NAME, rc_dev) < 0) {
  269. dev_err(dev, "IRQ %d register failed\n", rc_dev->irq);
  270. ret = -EINVAL;
  271. goto rcerr;
  272. }
  273. /* enable wake via this device */
  274. device_init_wakeup(dev, true);
  275. dev_pm_set_wake_irq(dev, rc_dev->irq);
  276. /*
  277. * for LIRC_MODE_MODE2 or LIRC_MODE_PULSE or LIRC_MODE_RAW
  278. * lircd expects a long space first before a signal train to sync.
  279. */
  280. st_rc_send_lirc_timeout(rdev);
  281. dev_info(dev, "setup in %s mode\n", rc_dev->rxuhfmode ? "UHF" : "IR");
  282. return ret;
  283. rcerr:
  284. rc_unregister_device(rdev);
  285. rdev = NULL;
  286. clkerr:
  287. clk_disable_unprepare(rc_dev->sys_clock);
  288. err:
  289. rc_free_device(rdev);
  290. dev_err(dev, "Unable to register device (%d)\n", ret);
  291. return ret;
  292. }
  293. #ifdef CONFIG_PM_SLEEP
  294. static int st_rc_suspend(struct device *dev)
  295. {
  296. struct st_rc_device *rc_dev = dev_get_drvdata(dev);
  297. if (device_may_wakeup(dev)) {
  298. if (!enable_irq_wake(rc_dev->irq))
  299. rc_dev->irq_wake = 1;
  300. else
  301. return -EINVAL;
  302. } else {
  303. pinctrl_pm_select_sleep_state(dev);
  304. writel(0x00, rc_dev->rx_base + IRB_RX_EN);
  305. writel(0x00, rc_dev->rx_base + IRB_RX_INT_EN);
  306. clk_disable_unprepare(rc_dev->sys_clock);
  307. reset_control_assert(rc_dev->rstc);
  308. }
  309. return 0;
  310. }
  311. static int st_rc_resume(struct device *dev)
  312. {
  313. int ret;
  314. struct st_rc_device *rc_dev = dev_get_drvdata(dev);
  315. struct rc_dev *rdev = rc_dev->rdev;
  316. if (rc_dev->irq_wake) {
  317. disable_irq_wake(rc_dev->irq);
  318. rc_dev->irq_wake = 0;
  319. } else {
  320. pinctrl_pm_select_default_state(dev);
  321. ret = st_rc_hardware_init(rc_dev);
  322. if (ret)
  323. return ret;
  324. if (rdev->users) {
  325. writel(IRB_RX_INTS, rc_dev->rx_base + IRB_RX_INT_EN);
  326. writel(0x01, rc_dev->rx_base + IRB_RX_EN);
  327. }
  328. }
  329. return 0;
  330. }
  331. #endif
  332. static SIMPLE_DEV_PM_OPS(st_rc_pm_ops, st_rc_suspend, st_rc_resume);
  333. #ifdef CONFIG_OF
  334. static const struct of_device_id st_rc_match[] = {
  335. { .compatible = "st,comms-irb", },
  336. {},
  337. };
  338. MODULE_DEVICE_TABLE(of, st_rc_match);
  339. #endif
  340. static struct platform_driver st_rc_driver = {
  341. .driver = {
  342. .name = IR_ST_NAME,
  343. .of_match_table = of_match_ptr(st_rc_match),
  344. .pm = &st_rc_pm_ops,
  345. },
  346. .probe = st_rc_probe,
  347. .remove = st_rc_remove,
  348. };
  349. module_platform_driver(st_rc_driver);
  350. MODULE_DESCRIPTION("RC Transceiver driver for STMicroelectronics platforms");
  351. MODULE_AUTHOR("STMicroelectronics (R&D) Ltd");
  352. MODULE_LICENSE("GPL");