st_rc.c 11 KB

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