ir-rx51.c 12 KB

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