ir-rx51.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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/platform_device.h>
  19. #include <linux/wait.h>
  20. #include <linux/pwm.h>
  21. #include <linux/of.h>
  22. #include <linux/hrtimer.h>
  23. #include <media/rc-core.h>
  24. #include <linux/platform_data/media/ir-rx51.h>
  25. #define WBUF_LEN 256
  26. struct ir_rx51 {
  27. struct rc_dev *rcdev;
  28. struct pwm_device *pwm;
  29. struct hrtimer timer;
  30. struct device *dev;
  31. struct ir_rx51_platform_data *pdata;
  32. wait_queue_head_t wqueue;
  33. unsigned int freq; /* carrier frequency */
  34. unsigned int duty_cycle; /* carrier duty cycle */
  35. int wbuf[WBUF_LEN];
  36. int wbuf_index;
  37. unsigned long device_is_open;
  38. };
  39. static inline void ir_rx51_on(struct ir_rx51 *ir_rx51)
  40. {
  41. pwm_enable(ir_rx51->pwm);
  42. }
  43. static inline void ir_rx51_off(struct ir_rx51 *ir_rx51)
  44. {
  45. pwm_disable(ir_rx51->pwm);
  46. }
  47. static int init_timing_params(struct ir_rx51 *ir_rx51)
  48. {
  49. struct pwm_device *pwm = ir_rx51->pwm;
  50. int duty, period = DIV_ROUND_CLOSEST(NSEC_PER_SEC, ir_rx51->freq);
  51. duty = DIV_ROUND_CLOSEST(ir_rx51->duty_cycle * period, 100);
  52. pwm_config(pwm, duty, period);
  53. return 0;
  54. }
  55. static enum hrtimer_restart ir_rx51_timer_cb(struct hrtimer *timer)
  56. {
  57. struct ir_rx51 *ir_rx51 = container_of(timer, struct ir_rx51, timer);
  58. ktime_t now;
  59. if (ir_rx51->wbuf_index < 0) {
  60. dev_err_ratelimited(ir_rx51->dev,
  61. "BUG wbuf_index has value of %i\n",
  62. ir_rx51->wbuf_index);
  63. goto end;
  64. }
  65. /*
  66. * If we happen to hit an odd latency spike, loop through the
  67. * pulses until we catch up.
  68. */
  69. do {
  70. u64 ns;
  71. if (ir_rx51->wbuf_index >= WBUF_LEN)
  72. goto end;
  73. if (ir_rx51->wbuf[ir_rx51->wbuf_index] == -1)
  74. goto end;
  75. if (ir_rx51->wbuf_index % 2)
  76. ir_rx51_off(ir_rx51);
  77. else
  78. ir_rx51_on(ir_rx51);
  79. ns = US_TO_NS(ir_rx51->wbuf[ir_rx51->wbuf_index]);
  80. hrtimer_add_expires_ns(timer, ns);
  81. ir_rx51->wbuf_index++;
  82. now = timer->base->get_time();
  83. } while (hrtimer_get_expires_tv64(timer) < now);
  84. return HRTIMER_RESTART;
  85. end:
  86. /* Stop TX here */
  87. ir_rx51_off(ir_rx51);
  88. ir_rx51->wbuf_index = -1;
  89. wake_up_interruptible(&ir_rx51->wqueue);
  90. return HRTIMER_NORESTART;
  91. }
  92. static int ir_rx51_tx(struct rc_dev *dev, unsigned int *buffer,
  93. unsigned int count)
  94. {
  95. struct ir_rx51 *ir_rx51 = dev->priv;
  96. if (count > WBUF_LEN)
  97. return -EINVAL;
  98. memcpy(ir_rx51->wbuf, buffer, count * sizeof(unsigned int));
  99. /* Wait any pending transfers to finish */
  100. wait_event_interruptible(ir_rx51->wqueue, ir_rx51->wbuf_index < 0);
  101. init_timing_params(ir_rx51);
  102. if (count < WBUF_LEN)
  103. ir_rx51->wbuf[count] = -1; /* Insert termination mark */
  104. /*
  105. * Adjust latency requirements so the device doesn't go in too
  106. * deep sleep states
  107. */
  108. ir_rx51->pdata->set_max_mpu_wakeup_lat(ir_rx51->dev, 50);
  109. ir_rx51_on(ir_rx51);
  110. ir_rx51->wbuf_index = 1;
  111. hrtimer_start(&ir_rx51->timer,
  112. ns_to_ktime(US_TO_NS(ir_rx51->wbuf[0])),
  113. HRTIMER_MODE_REL);
  114. /*
  115. * Don't return back to the userspace until the transfer has
  116. * finished
  117. */
  118. wait_event_interruptible(ir_rx51->wqueue, ir_rx51->wbuf_index < 0);
  119. /* We can sleep again */
  120. ir_rx51->pdata->set_max_mpu_wakeup_lat(ir_rx51->dev, -1);
  121. return count;
  122. }
  123. static int ir_rx51_open(struct rc_dev *dev)
  124. {
  125. struct ir_rx51 *ir_rx51 = dev->priv;
  126. if (test_and_set_bit(1, &ir_rx51->device_is_open))
  127. return -EBUSY;
  128. ir_rx51->pwm = pwm_get(ir_rx51->dev, NULL);
  129. if (IS_ERR(ir_rx51->pwm)) {
  130. int res = PTR_ERR(ir_rx51->pwm);
  131. dev_err(ir_rx51->dev, "pwm_get failed: %d\n", res);
  132. return res;
  133. }
  134. return 0;
  135. }
  136. static void ir_rx51_release(struct rc_dev *dev)
  137. {
  138. struct ir_rx51 *ir_rx51 = dev->priv;
  139. hrtimer_cancel(&ir_rx51->timer);
  140. ir_rx51_off(ir_rx51);
  141. pwm_put(ir_rx51->pwm);
  142. clear_bit(1, &ir_rx51->device_is_open);
  143. }
  144. static struct ir_rx51 ir_rx51 = {
  145. .duty_cycle = 50,
  146. .wbuf_index = -1,
  147. };
  148. static int ir_rx51_set_duty_cycle(struct rc_dev *dev, u32 duty)
  149. {
  150. struct ir_rx51 *ir_rx51 = dev->priv;
  151. ir_rx51->duty_cycle = duty;
  152. return 0;
  153. }
  154. static int ir_rx51_set_tx_carrier(struct rc_dev *dev, u32 carrier)
  155. {
  156. struct ir_rx51 *ir_rx51 = dev->priv;
  157. if (carrier > 500000 || carrier < 20000)
  158. return -EINVAL;
  159. ir_rx51->freq = carrier;
  160. return 0;
  161. }
  162. #ifdef CONFIG_PM
  163. static int ir_rx51_suspend(struct platform_device *dev, pm_message_t state)
  164. {
  165. /*
  166. * In case the device is still open, do not suspend. Normally
  167. * this should not be a problem as lircd only keeps the device
  168. * open only for short periods of time. We also don't want to
  169. * get involved with race conditions that might happen if we
  170. * were in a middle of a transmit. Thus, we defer any suspend
  171. * actions until transmit has completed.
  172. */
  173. if (test_and_set_bit(1, &ir_rx51.device_is_open))
  174. return -EAGAIN;
  175. clear_bit(1, &ir_rx51.device_is_open);
  176. return 0;
  177. }
  178. static int ir_rx51_resume(struct platform_device *dev)
  179. {
  180. return 0;
  181. }
  182. #else
  183. #define ir_rx51_suspend NULL
  184. #define ir_rx51_resume NULL
  185. #endif /* CONFIG_PM */
  186. static int ir_rx51_probe(struct platform_device *dev)
  187. {
  188. struct pwm_device *pwm;
  189. struct rc_dev *rcdev;
  190. ir_rx51.pdata = dev->dev.platform_data;
  191. if (!ir_rx51.pdata) {
  192. dev_err(&dev->dev, "Platform Data is missing\n");
  193. return -ENXIO;
  194. }
  195. pwm = pwm_get(&dev->dev, NULL);
  196. if (IS_ERR(pwm)) {
  197. int err = PTR_ERR(pwm);
  198. if (err != -EPROBE_DEFER)
  199. dev_err(&dev->dev, "pwm_get failed: %d\n", err);
  200. return err;
  201. }
  202. /* Use default, in case userspace does not set the carrier */
  203. ir_rx51.freq = DIV_ROUND_CLOSEST(pwm_get_period(pwm), NSEC_PER_SEC);
  204. pwm_put(pwm);
  205. hrtimer_init(&ir_rx51.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  206. ir_rx51.timer.function = ir_rx51_timer_cb;
  207. ir_rx51.dev = &dev->dev;
  208. rcdev = devm_rc_allocate_device(&dev->dev, RC_DRIVER_IR_RAW_TX);
  209. if (!rcdev)
  210. return -ENOMEM;
  211. rcdev->priv = &ir_rx51;
  212. rcdev->open = ir_rx51_open;
  213. rcdev->close = ir_rx51_release;
  214. rcdev->tx_ir = ir_rx51_tx;
  215. rcdev->s_tx_duty_cycle = ir_rx51_set_duty_cycle;
  216. rcdev->s_tx_carrier = ir_rx51_set_tx_carrier;
  217. rcdev->driver_name = KBUILD_MODNAME;
  218. ir_rx51.rcdev = rcdev;
  219. return devm_rc_register_device(&dev->dev, ir_rx51.rcdev);
  220. }
  221. static int ir_rx51_remove(struct platform_device *dev)
  222. {
  223. return 0;
  224. }
  225. static const struct of_device_id ir_rx51_match[] = {
  226. {
  227. .compatible = "nokia,n900-ir",
  228. },
  229. {},
  230. };
  231. MODULE_DEVICE_TABLE(of, ir_rx51_match);
  232. static struct platform_driver ir_rx51_platform_driver = {
  233. .probe = ir_rx51_probe,
  234. .remove = ir_rx51_remove,
  235. .suspend = ir_rx51_suspend,
  236. .resume = ir_rx51_resume,
  237. .driver = {
  238. .name = KBUILD_MODNAME,
  239. .of_match_table = of_match_ptr(ir_rx51_match),
  240. },
  241. };
  242. module_platform_driver(ir_rx51_platform_driver);
  243. MODULE_DESCRIPTION("IR TX driver for Nokia RX51");
  244. MODULE_AUTHOR("Nokia Corporation");
  245. MODULE_LICENSE("GPL");