ir-rx51.c 6.5 KB

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