ir-rx51.c 6.8 KB

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