ir-rx51.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. #include <linux/clk.h>
  17. #include <linux/module.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/uaccess.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/sched.h>
  22. #include <linux/wait.h>
  23. #include <linux/pwm.h>
  24. #include <linux/of.h>
  25. #include <linux/hrtimer.h>
  26. #include <media/lirc.h>
  27. #include <media/lirc_dev.h>
  28. #include <linux/platform_data/media/ir-rx51.h>
  29. #define LIRC_RX51_DRIVER_FEATURES (LIRC_CAN_SET_SEND_DUTY_CYCLE | \
  30. LIRC_CAN_SET_SEND_CARRIER | \
  31. LIRC_CAN_SEND_PULSE)
  32. #define DRIVER_NAME "lirc_rx51"
  33. #define WBUF_LEN 256
  34. struct lirc_rx51 {
  35. struct pwm_device *pwm;
  36. struct hrtimer timer;
  37. struct device *dev;
  38. struct lirc_rx51_platform_data *pdata;
  39. wait_queue_head_t wqueue;
  40. unsigned int freq; /* carrier frequency */
  41. unsigned int duty_cycle; /* carrier duty cycle */
  42. int wbuf[WBUF_LEN];
  43. int wbuf_index;
  44. unsigned long device_is_open;
  45. };
  46. static inline void lirc_rx51_on(struct lirc_rx51 *lirc_rx51)
  47. {
  48. pwm_enable(lirc_rx51->pwm);
  49. }
  50. static inline void lirc_rx51_off(struct lirc_rx51 *lirc_rx51)
  51. {
  52. pwm_disable(lirc_rx51->pwm);
  53. }
  54. static int init_timing_params(struct lirc_rx51 *lirc_rx51)
  55. {
  56. struct pwm_device *pwm = lirc_rx51->pwm;
  57. int duty, period = DIV_ROUND_CLOSEST(NSEC_PER_SEC, lirc_rx51->freq);
  58. duty = DIV_ROUND_CLOSEST(lirc_rx51->duty_cycle * period, 100);
  59. pwm_config(pwm, duty, period);
  60. return 0;
  61. }
  62. static enum hrtimer_restart lirc_rx51_timer_cb(struct hrtimer *timer)
  63. {
  64. struct lirc_rx51 *lirc_rx51 =
  65. container_of(timer, struct lirc_rx51, timer);
  66. ktime_t now;
  67. if (lirc_rx51->wbuf_index < 0) {
  68. dev_err_ratelimited(lirc_rx51->dev,
  69. "BUG wbuf_index has value of %i\n",
  70. lirc_rx51->wbuf_index);
  71. goto end;
  72. }
  73. /*
  74. * If we happen to hit an odd latency spike, loop through the
  75. * pulses until we catch up.
  76. */
  77. do {
  78. u64 ns;
  79. if (lirc_rx51->wbuf_index >= WBUF_LEN)
  80. goto end;
  81. if (lirc_rx51->wbuf[lirc_rx51->wbuf_index] == -1)
  82. goto end;
  83. if (lirc_rx51->wbuf_index % 2)
  84. lirc_rx51_off(lirc_rx51);
  85. else
  86. lirc_rx51_on(lirc_rx51);
  87. ns = 1000 * lirc_rx51->wbuf[lirc_rx51->wbuf_index];
  88. hrtimer_add_expires_ns(timer, ns);
  89. lirc_rx51->wbuf_index++;
  90. now = timer->base->get_time();
  91. } while (hrtimer_get_expires_tv64(timer) < now.tv64);
  92. return HRTIMER_RESTART;
  93. end:
  94. /* Stop TX here */
  95. lirc_rx51_off(lirc_rx51);
  96. lirc_rx51->wbuf_index = -1;
  97. wake_up_interruptible(&lirc_rx51->wqueue);
  98. return HRTIMER_NORESTART;
  99. }
  100. static ssize_t lirc_rx51_write(struct file *file, const char *buf,
  101. size_t n, loff_t *ppos)
  102. {
  103. int count, i;
  104. struct lirc_rx51 *lirc_rx51 = file->private_data;
  105. if (n % sizeof(int))
  106. return -EINVAL;
  107. count = n / sizeof(int);
  108. if ((count > WBUF_LEN) || (count % 2 == 0))
  109. return -EINVAL;
  110. /* Wait any pending transfers to finish */
  111. wait_event_interruptible(lirc_rx51->wqueue, lirc_rx51->wbuf_index < 0);
  112. if (copy_from_user(lirc_rx51->wbuf, buf, n))
  113. return -EFAULT;
  114. /* Sanity check the input pulses */
  115. for (i = 0; i < count; i++)
  116. if (lirc_rx51->wbuf[i] < 0)
  117. return -EINVAL;
  118. init_timing_params(lirc_rx51);
  119. if (count < WBUF_LEN)
  120. lirc_rx51->wbuf[count] = -1; /* Insert termination mark */
  121. /*
  122. * Adjust latency requirements so the device doesn't go in too
  123. * deep sleep states
  124. */
  125. lirc_rx51->pdata->set_max_mpu_wakeup_lat(lirc_rx51->dev, 50);
  126. lirc_rx51_on(lirc_rx51);
  127. lirc_rx51->wbuf_index = 1;
  128. hrtimer_start(&lirc_rx51->timer,
  129. ns_to_ktime(1000 * lirc_rx51->wbuf[0]),
  130. HRTIMER_MODE_REL);
  131. /*
  132. * Don't return back to the userspace until the transfer has
  133. * finished
  134. */
  135. wait_event_interruptible(lirc_rx51->wqueue, lirc_rx51->wbuf_index < 0);
  136. /* We can sleep again */
  137. lirc_rx51->pdata->set_max_mpu_wakeup_lat(lirc_rx51->dev, -1);
  138. return n;
  139. }
  140. static long lirc_rx51_ioctl(struct file *filep,
  141. unsigned int cmd, unsigned long arg)
  142. {
  143. int result;
  144. unsigned long value;
  145. unsigned int ivalue;
  146. struct lirc_rx51 *lirc_rx51 = filep->private_data;
  147. switch (cmd) {
  148. case LIRC_GET_SEND_MODE:
  149. result = put_user(LIRC_MODE_PULSE, (unsigned long *)arg);
  150. if (result)
  151. return result;
  152. break;
  153. case LIRC_SET_SEND_MODE:
  154. result = get_user(value, (unsigned long *)arg);
  155. if (result)
  156. return result;
  157. /* only LIRC_MODE_PULSE supported */
  158. if (value != LIRC_MODE_PULSE)
  159. return -ENOSYS;
  160. break;
  161. case LIRC_GET_REC_MODE:
  162. result = put_user(0, (unsigned long *) arg);
  163. if (result)
  164. return result;
  165. break;
  166. case LIRC_GET_LENGTH:
  167. return -ENOSYS;
  168. break;
  169. case LIRC_SET_SEND_DUTY_CYCLE:
  170. result = get_user(ivalue, (unsigned int *) arg);
  171. if (result)
  172. return result;
  173. if (ivalue <= 0 || ivalue > 100) {
  174. dev_err(lirc_rx51->dev, ": invalid duty cycle %d\n",
  175. ivalue);
  176. return -EINVAL;
  177. }
  178. lirc_rx51->duty_cycle = ivalue;
  179. break;
  180. case LIRC_SET_SEND_CARRIER:
  181. result = get_user(ivalue, (unsigned int *) arg);
  182. if (result)
  183. return result;
  184. if (ivalue > 500000 || ivalue < 20000) {
  185. dev_err(lirc_rx51->dev, ": invalid carrier freq %d\n",
  186. ivalue);
  187. return -EINVAL;
  188. }
  189. lirc_rx51->freq = ivalue;
  190. break;
  191. case LIRC_GET_FEATURES:
  192. result = put_user(LIRC_RX51_DRIVER_FEATURES,
  193. (unsigned long *) arg);
  194. if (result)
  195. return result;
  196. break;
  197. default:
  198. return -ENOIOCTLCMD;
  199. }
  200. return 0;
  201. }
  202. static int lirc_rx51_open(struct inode *inode, struct file *file)
  203. {
  204. struct lirc_rx51 *lirc_rx51 = lirc_get_pdata(file);
  205. BUG_ON(!lirc_rx51);
  206. file->private_data = lirc_rx51;
  207. if (test_and_set_bit(1, &lirc_rx51->device_is_open))
  208. return -EBUSY;
  209. lirc_rx51->pwm = pwm_get(lirc_rx51->dev, NULL);
  210. if (IS_ERR(lirc_rx51->pwm)) {
  211. int res = PTR_ERR(lirc_rx51->pwm);
  212. dev_err(lirc_rx51->dev, "pwm_get failed: %d\n", res);
  213. return res;
  214. }
  215. return 0;
  216. }
  217. static int lirc_rx51_release(struct inode *inode, struct file *file)
  218. {
  219. struct lirc_rx51 *lirc_rx51 = file->private_data;
  220. hrtimer_cancel(&lirc_rx51->timer);
  221. lirc_rx51_off(lirc_rx51);
  222. pwm_put(lirc_rx51->pwm);
  223. clear_bit(1, &lirc_rx51->device_is_open);
  224. return 0;
  225. }
  226. static struct lirc_rx51 lirc_rx51 = {
  227. .duty_cycle = 50,
  228. .wbuf_index = -1,
  229. };
  230. static const struct file_operations lirc_fops = {
  231. .owner = THIS_MODULE,
  232. .write = lirc_rx51_write,
  233. .unlocked_ioctl = lirc_rx51_ioctl,
  234. .read = lirc_dev_fop_read,
  235. .poll = lirc_dev_fop_poll,
  236. .open = lirc_rx51_open,
  237. .release = lirc_rx51_release,
  238. };
  239. static struct lirc_driver lirc_rx51_driver = {
  240. .name = DRIVER_NAME,
  241. .minor = -1,
  242. .code_length = 1,
  243. .data = &lirc_rx51,
  244. .fops = &lirc_fops,
  245. .owner = THIS_MODULE,
  246. };
  247. #ifdef CONFIG_PM
  248. static int lirc_rx51_suspend(struct platform_device *dev, pm_message_t state)
  249. {
  250. /*
  251. * In case the device is still open, do not suspend. Normally
  252. * this should not be a problem as lircd only keeps the device
  253. * open only for short periods of time. We also don't want to
  254. * get involved with race conditions that might happen if we
  255. * were in a middle of a transmit. Thus, we defer any suspend
  256. * actions until transmit has completed.
  257. */
  258. if (test_and_set_bit(1, &lirc_rx51.device_is_open))
  259. return -EAGAIN;
  260. clear_bit(1, &lirc_rx51.device_is_open);
  261. return 0;
  262. }
  263. static int lirc_rx51_resume(struct platform_device *dev)
  264. {
  265. return 0;
  266. }
  267. #else
  268. #define lirc_rx51_suspend NULL
  269. #define lirc_rx51_resume NULL
  270. #endif /* CONFIG_PM */
  271. static int lirc_rx51_probe(struct platform_device *dev)
  272. {
  273. struct pwm_device *pwm;
  274. lirc_rx51_driver.features = LIRC_RX51_DRIVER_FEATURES;
  275. lirc_rx51.pdata = dev->dev.platform_data;
  276. if (!lirc_rx51.pdata) {
  277. dev_err(&dev->dev, "Platform Data is missing\n");
  278. return -ENXIO;
  279. }
  280. pwm = pwm_get(&dev->dev, NULL);
  281. if (IS_ERR(pwm)) {
  282. int err = PTR_ERR(pwm);
  283. if (err != -EPROBE_DEFER)
  284. dev_err(&dev->dev, "pwm_get failed: %d\n", err);
  285. return err;
  286. }
  287. /* Use default, in case userspace does not set the carrier */
  288. lirc_rx51.freq = DIV_ROUND_CLOSEST(pwm_get_period(pwm), NSEC_PER_SEC);
  289. pwm_put(pwm);
  290. hrtimer_init(&lirc_rx51.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  291. lirc_rx51.timer.function = lirc_rx51_timer_cb;
  292. lirc_rx51.dev = &dev->dev;
  293. lirc_rx51_driver.dev = &dev->dev;
  294. lirc_rx51_driver.minor = lirc_register_driver(&lirc_rx51_driver);
  295. init_waitqueue_head(&lirc_rx51.wqueue);
  296. if (lirc_rx51_driver.minor < 0) {
  297. dev_err(lirc_rx51.dev, ": lirc_register_driver failed: %d\n",
  298. lirc_rx51_driver.minor);
  299. return lirc_rx51_driver.minor;
  300. }
  301. return 0;
  302. }
  303. static int lirc_rx51_remove(struct platform_device *dev)
  304. {
  305. return lirc_unregister_driver(lirc_rx51_driver.minor);
  306. }
  307. static const struct of_device_id lirc_rx51_match[] = {
  308. {
  309. .compatible = "nokia,n900-ir",
  310. },
  311. {},
  312. };
  313. MODULE_DEVICE_TABLE(of, lirc_rx51_match);
  314. struct platform_driver lirc_rx51_platform_driver = {
  315. .probe = lirc_rx51_probe,
  316. .remove = lirc_rx51_remove,
  317. .suspend = lirc_rx51_suspend,
  318. .resume = lirc_rx51_resume,
  319. .driver = {
  320. .name = DRIVER_NAME,
  321. .of_match_table = of_match_ptr(lirc_rx51_match),
  322. },
  323. };
  324. module_platform_driver(lirc_rx51_platform_driver);
  325. MODULE_DESCRIPTION("LIRC TX driver for Nokia RX51");
  326. MODULE_AUTHOR("Nokia Corporation");
  327. MODULE_LICENSE("GPL");