ir-rx51.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. /*
  2. * Copyright (C) 2008 Nokia Corporation
  3. *
  4. * Based on lirc_serial.c
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. */
  21. #include <linux/module.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/uaccess.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/sched.h>
  26. #include <linux/wait.h>
  27. #include <plat/dmtimer.h>
  28. #include <plat/clock.h>
  29. #include <plat/omap-pm.h>
  30. #include <media/lirc.h>
  31. #include <media/lirc_dev.h>
  32. #include <media/ir-rx51.h>
  33. #define LIRC_RX51_DRIVER_FEATURES (LIRC_CAN_SET_SEND_DUTY_CYCLE | \
  34. LIRC_CAN_SET_SEND_CARRIER | \
  35. LIRC_CAN_SEND_PULSE)
  36. #define DRIVER_NAME "lirc_rx51"
  37. #define WBUF_LEN 256
  38. #define TIMER_MAX_VALUE 0xffffffff
  39. struct lirc_rx51 {
  40. struct omap_dm_timer *pwm_timer;
  41. struct omap_dm_timer *pulse_timer;
  42. struct device *dev;
  43. struct lirc_rx51_platform_data *pdata;
  44. wait_queue_head_t wqueue;
  45. unsigned long fclk_khz;
  46. unsigned int freq; /* carrier frequency */
  47. unsigned int duty_cycle; /* carrier duty cycle */
  48. unsigned int irq_num;
  49. unsigned int match;
  50. int wbuf[WBUF_LEN];
  51. int wbuf_index;
  52. unsigned long device_is_open;
  53. int pwm_timer_num;
  54. };
  55. static void lirc_rx51_on(struct lirc_rx51 *lirc_rx51)
  56. {
  57. omap_dm_timer_set_pwm(lirc_rx51->pwm_timer, 0, 1,
  58. OMAP_TIMER_TRIGGER_OVERFLOW_AND_COMPARE);
  59. }
  60. static void lirc_rx51_off(struct lirc_rx51 *lirc_rx51)
  61. {
  62. omap_dm_timer_set_pwm(lirc_rx51->pwm_timer, 0, 1,
  63. OMAP_TIMER_TRIGGER_NONE);
  64. }
  65. static int init_timing_params(struct lirc_rx51 *lirc_rx51)
  66. {
  67. u32 load, match;
  68. load = -(lirc_rx51->fclk_khz * 1000 / lirc_rx51->freq);
  69. match = -(lirc_rx51->duty_cycle * -load / 100);
  70. omap_dm_timer_set_load(lirc_rx51->pwm_timer, 1, load);
  71. omap_dm_timer_set_match(lirc_rx51->pwm_timer, 1, match);
  72. omap_dm_timer_write_counter(lirc_rx51->pwm_timer, TIMER_MAX_VALUE - 2);
  73. omap_dm_timer_start(lirc_rx51->pwm_timer);
  74. omap_dm_timer_set_int_enable(lirc_rx51->pulse_timer, 0);
  75. omap_dm_timer_start(lirc_rx51->pulse_timer);
  76. lirc_rx51->match = 0;
  77. return 0;
  78. }
  79. #define tics_after(a, b) ((long)(b) - (long)(a) < 0)
  80. static int pulse_timer_set_timeout(struct lirc_rx51 *lirc_rx51, int usec)
  81. {
  82. int counter;
  83. BUG_ON(usec < 0);
  84. if (lirc_rx51->match == 0)
  85. counter = omap_dm_timer_read_counter(lirc_rx51->pulse_timer);
  86. else
  87. counter = lirc_rx51->match;
  88. counter += (u32)(lirc_rx51->fclk_khz * usec / (1000));
  89. omap_dm_timer_set_match(lirc_rx51->pulse_timer, 1, counter);
  90. omap_dm_timer_set_int_enable(lirc_rx51->pulse_timer,
  91. OMAP_TIMER_INT_MATCH);
  92. if (tics_after(omap_dm_timer_read_counter(lirc_rx51->pulse_timer),
  93. counter)) {
  94. return 1;
  95. }
  96. return 0;
  97. }
  98. static irqreturn_t lirc_rx51_interrupt_handler(int irq, void *ptr)
  99. {
  100. unsigned int retval;
  101. struct lirc_rx51 *lirc_rx51 = ptr;
  102. retval = omap_dm_timer_read_status(lirc_rx51->pulse_timer);
  103. if (!retval)
  104. return IRQ_NONE;
  105. if (retval & ~OMAP_TIMER_INT_MATCH)
  106. dev_err_ratelimited(lirc_rx51->dev,
  107. ": Unexpected interrupt source: %x\n", retval);
  108. omap_dm_timer_write_status(lirc_rx51->pulse_timer,
  109. OMAP_TIMER_INT_MATCH |
  110. OMAP_TIMER_INT_OVERFLOW |
  111. OMAP_TIMER_INT_CAPTURE);
  112. if (lirc_rx51->wbuf_index < 0) {
  113. dev_err_ratelimited(lirc_rx51->dev,
  114. ": BUG wbuf_index has value of %i\n",
  115. lirc_rx51->wbuf_index);
  116. goto end;
  117. }
  118. /*
  119. * If we happen to hit an odd latency spike, loop through the
  120. * pulses until we catch up.
  121. */
  122. do {
  123. if (lirc_rx51->wbuf_index >= WBUF_LEN)
  124. goto end;
  125. if (lirc_rx51->wbuf[lirc_rx51->wbuf_index] == -1)
  126. goto end;
  127. if (lirc_rx51->wbuf_index % 2)
  128. lirc_rx51_off(lirc_rx51);
  129. else
  130. lirc_rx51_on(lirc_rx51);
  131. retval = pulse_timer_set_timeout(lirc_rx51,
  132. lirc_rx51->wbuf[lirc_rx51->wbuf_index]);
  133. lirc_rx51->wbuf_index++;
  134. } while (retval);
  135. return IRQ_HANDLED;
  136. end:
  137. /* Stop TX here */
  138. lirc_rx51_off(lirc_rx51);
  139. lirc_rx51->wbuf_index = -1;
  140. omap_dm_timer_stop(lirc_rx51->pwm_timer);
  141. omap_dm_timer_stop(lirc_rx51->pulse_timer);
  142. omap_dm_timer_set_int_enable(lirc_rx51->pulse_timer, 0);
  143. wake_up_interruptible(&lirc_rx51->wqueue);
  144. return IRQ_HANDLED;
  145. }
  146. static int lirc_rx51_init_port(struct lirc_rx51 *lirc_rx51)
  147. {
  148. struct clk *clk_fclk;
  149. int retval, pwm_timer = lirc_rx51->pwm_timer_num;
  150. lirc_rx51->pwm_timer = omap_dm_timer_request_specific(pwm_timer);
  151. if (lirc_rx51->pwm_timer == NULL) {
  152. dev_err(lirc_rx51->dev, ": Error requesting GPT%d timer\n",
  153. pwm_timer);
  154. return -EBUSY;
  155. }
  156. lirc_rx51->pulse_timer = omap_dm_timer_request();
  157. if (lirc_rx51->pulse_timer == NULL) {
  158. dev_err(lirc_rx51->dev, ": Error requesting pulse timer\n");
  159. retval = -EBUSY;
  160. goto err1;
  161. }
  162. omap_dm_timer_set_source(lirc_rx51->pwm_timer, OMAP_TIMER_SRC_SYS_CLK);
  163. omap_dm_timer_set_source(lirc_rx51->pulse_timer,
  164. OMAP_TIMER_SRC_SYS_CLK);
  165. omap_dm_timer_enable(lirc_rx51->pwm_timer);
  166. omap_dm_timer_enable(lirc_rx51->pulse_timer);
  167. lirc_rx51->irq_num = omap_dm_timer_get_irq(lirc_rx51->pulse_timer);
  168. retval = request_irq(lirc_rx51->irq_num, lirc_rx51_interrupt_handler,
  169. IRQF_DISABLED | IRQF_SHARED,
  170. "lirc_pulse_timer", lirc_rx51);
  171. if (retval) {
  172. dev_err(lirc_rx51->dev, ": Failed to request interrupt line\n");
  173. goto err2;
  174. }
  175. clk_fclk = omap_dm_timer_get_fclk(lirc_rx51->pwm_timer);
  176. lirc_rx51->fclk_khz = clk_fclk->rate / 1000;
  177. return 0;
  178. err2:
  179. omap_dm_timer_free(lirc_rx51->pulse_timer);
  180. err1:
  181. omap_dm_timer_free(lirc_rx51->pwm_timer);
  182. return retval;
  183. }
  184. static int lirc_rx51_free_port(struct lirc_rx51 *lirc_rx51)
  185. {
  186. omap_dm_timer_set_int_enable(lirc_rx51->pulse_timer, 0);
  187. free_irq(lirc_rx51->irq_num, lirc_rx51);
  188. lirc_rx51_off(lirc_rx51);
  189. omap_dm_timer_disable(lirc_rx51->pwm_timer);
  190. omap_dm_timer_disable(lirc_rx51->pulse_timer);
  191. omap_dm_timer_free(lirc_rx51->pwm_timer);
  192. omap_dm_timer_free(lirc_rx51->pulse_timer);
  193. lirc_rx51->wbuf_index = -1;
  194. return 0;
  195. }
  196. static ssize_t lirc_rx51_write(struct file *file, const char *buf,
  197. size_t n, loff_t *ppos)
  198. {
  199. int count, i;
  200. struct lirc_rx51 *lirc_rx51 = file->private_data;
  201. if (n % sizeof(int))
  202. return -EINVAL;
  203. count = n / sizeof(int);
  204. if ((count > WBUF_LEN) || (count % 2 == 0))
  205. return -EINVAL;
  206. /* Wait any pending transfers to finish */
  207. wait_event_interruptible(lirc_rx51->wqueue, lirc_rx51->wbuf_index < 0);
  208. if (copy_from_user(lirc_rx51->wbuf, buf, n))
  209. return -EFAULT;
  210. /* Sanity check the input pulses */
  211. for (i = 0; i < count; i++)
  212. if (lirc_rx51->wbuf[i] < 0)
  213. return -EINVAL;
  214. init_timing_params(lirc_rx51);
  215. if (count < WBUF_LEN)
  216. lirc_rx51->wbuf[count] = -1; /* Insert termination mark */
  217. /*
  218. * Adjust latency requirements so the device doesn't go in too
  219. * deep sleep states
  220. */
  221. lirc_rx51->pdata->set_max_mpu_wakeup_lat(lirc_rx51->dev, 50);
  222. lirc_rx51_on(lirc_rx51);
  223. lirc_rx51->wbuf_index = 1;
  224. pulse_timer_set_timeout(lirc_rx51, lirc_rx51->wbuf[0]);
  225. /*
  226. * Don't return back to the userspace until the transfer has
  227. * finished
  228. */
  229. wait_event_interruptible(lirc_rx51->wqueue, lirc_rx51->wbuf_index < 0);
  230. /* We can sleep again */
  231. lirc_rx51->pdata->set_max_mpu_wakeup_lat(lirc_rx51->dev, -1);
  232. return n;
  233. }
  234. static long lirc_rx51_ioctl(struct file *filep,
  235. unsigned int cmd, unsigned long arg)
  236. {
  237. int result;
  238. unsigned long value;
  239. unsigned int ivalue;
  240. struct lirc_rx51 *lirc_rx51 = filep->private_data;
  241. switch (cmd) {
  242. case LIRC_GET_SEND_MODE:
  243. result = put_user(LIRC_MODE_PULSE, (unsigned long *)arg);
  244. if (result)
  245. return result;
  246. break;
  247. case LIRC_SET_SEND_MODE:
  248. result = get_user(value, (unsigned long *)arg);
  249. if (result)
  250. return result;
  251. /* only LIRC_MODE_PULSE supported */
  252. if (value != LIRC_MODE_PULSE)
  253. return -ENOSYS;
  254. break;
  255. case LIRC_GET_REC_MODE:
  256. result = put_user(0, (unsigned long *) arg);
  257. if (result)
  258. return result;
  259. break;
  260. case LIRC_GET_LENGTH:
  261. return -ENOSYS;
  262. break;
  263. case LIRC_SET_SEND_DUTY_CYCLE:
  264. result = get_user(ivalue, (unsigned int *) arg);
  265. if (result)
  266. return result;
  267. if (ivalue <= 0 || ivalue > 100) {
  268. dev_err(lirc_rx51->dev, ": invalid duty cycle %d\n",
  269. ivalue);
  270. return -EINVAL;
  271. }
  272. lirc_rx51->duty_cycle = ivalue;
  273. break;
  274. case LIRC_SET_SEND_CARRIER:
  275. result = get_user(ivalue, (unsigned int *) arg);
  276. if (result)
  277. return result;
  278. if (ivalue > 500000 || ivalue < 20000) {
  279. dev_err(lirc_rx51->dev, ": invalid carrier freq %d\n",
  280. ivalue);
  281. return -EINVAL;
  282. }
  283. lirc_rx51->freq = ivalue;
  284. break;
  285. case LIRC_GET_FEATURES:
  286. result = put_user(LIRC_RX51_DRIVER_FEATURES,
  287. (unsigned long *) arg);
  288. if (result)
  289. return result;
  290. break;
  291. default:
  292. return -ENOIOCTLCMD;
  293. }
  294. return 0;
  295. }
  296. static int lirc_rx51_open(struct inode *inode, struct file *file)
  297. {
  298. struct lirc_rx51 *lirc_rx51 = lirc_get_pdata(file);
  299. BUG_ON(!lirc_rx51);
  300. file->private_data = lirc_rx51;
  301. if (test_and_set_bit(1, &lirc_rx51->device_is_open))
  302. return -EBUSY;
  303. return lirc_rx51_init_port(lirc_rx51);
  304. }
  305. static int lirc_rx51_release(struct inode *inode, struct file *file)
  306. {
  307. struct lirc_rx51 *lirc_rx51 = file->private_data;
  308. lirc_rx51_free_port(lirc_rx51);
  309. clear_bit(1, &lirc_rx51->device_is_open);
  310. return 0;
  311. }
  312. static struct lirc_rx51 lirc_rx51 = {
  313. .freq = 38000,
  314. .duty_cycle = 50,
  315. .wbuf_index = -1,
  316. };
  317. static const struct file_operations lirc_fops = {
  318. .owner = THIS_MODULE,
  319. .write = lirc_rx51_write,
  320. .unlocked_ioctl = lirc_rx51_ioctl,
  321. .read = lirc_dev_fop_read,
  322. .poll = lirc_dev_fop_poll,
  323. .open = lirc_rx51_open,
  324. .release = lirc_rx51_release,
  325. };
  326. static struct lirc_driver lirc_rx51_driver = {
  327. .name = DRIVER_NAME,
  328. .minor = -1,
  329. .code_length = 1,
  330. .data = &lirc_rx51,
  331. .fops = &lirc_fops,
  332. .owner = THIS_MODULE,
  333. };
  334. #ifdef CONFIG_PM
  335. static int lirc_rx51_suspend(struct platform_device *dev, pm_message_t state)
  336. {
  337. /*
  338. * In case the device is still open, do not suspend. Normally
  339. * this should not be a problem as lircd only keeps the device
  340. * open only for short periods of time. We also don't want to
  341. * get involved with race conditions that might happen if we
  342. * were in a middle of a transmit. Thus, we defer any suspend
  343. * actions until transmit has completed.
  344. */
  345. if (test_and_set_bit(1, &lirc_rx51.device_is_open))
  346. return -EAGAIN;
  347. clear_bit(1, &lirc_rx51.device_is_open);
  348. return 0;
  349. }
  350. static int lirc_rx51_resume(struct platform_device *dev)
  351. {
  352. return 0;
  353. }
  354. #else
  355. #define lirc_rx51_suspend NULL
  356. #define lirc_rx51_resume NULL
  357. #endif /* CONFIG_PM */
  358. static int __devinit lirc_rx51_probe(struct platform_device *dev)
  359. {
  360. lirc_rx51_driver.features = LIRC_RX51_DRIVER_FEATURES;
  361. lirc_rx51.pdata = dev->dev.platform_data;
  362. lirc_rx51.pwm_timer_num = lirc_rx51.pdata->pwm_timer;
  363. lirc_rx51.dev = &dev->dev;
  364. lirc_rx51_driver.dev = &dev->dev;
  365. lirc_rx51_driver.minor = lirc_register_driver(&lirc_rx51_driver);
  366. init_waitqueue_head(&lirc_rx51.wqueue);
  367. if (lirc_rx51_driver.minor < 0) {
  368. dev_err(lirc_rx51.dev, ": lirc_register_driver failed: %d\n",
  369. lirc_rx51_driver.minor);
  370. return lirc_rx51_driver.minor;
  371. }
  372. dev_info(lirc_rx51.dev, "registration ok, minor: %d, pwm: %d\n",
  373. lirc_rx51_driver.minor, lirc_rx51.pwm_timer_num);
  374. return 0;
  375. }
  376. static int __exit lirc_rx51_remove(struct platform_device *dev)
  377. {
  378. return lirc_unregister_driver(lirc_rx51_driver.minor);
  379. }
  380. struct platform_driver lirc_rx51_platform_driver = {
  381. .probe = lirc_rx51_probe,
  382. .remove = __exit_p(lirc_rx51_remove),
  383. .suspend = lirc_rx51_suspend,
  384. .resume = lirc_rx51_resume,
  385. .driver = {
  386. .name = DRIVER_NAME,
  387. .owner = THIS_MODULE,
  388. },
  389. };
  390. module_platform_driver(lirc_rx51_platform_driver);
  391. MODULE_DESCRIPTION("LIRC TX driver for Nokia RX51");
  392. MODULE_AUTHOR("Nokia Corporation");
  393. MODULE_LICENSE("GPL");