st_rc.c 11 KB

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