1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2008 Nokia Corporation
5 * Based on lirc_serial.c
8 #include <linux/module.h>
9 #include <linux/platform_device.h>
10 #include <linux/wait.h>
11 #include <linux/pwm.h>
13 #include <linux/hrtimer.h>
15 #include <media/rc-core.h>
21 struct pwm_device *pwm;
22 struct pwm_state state;
25 wait_queue_head_t wqueue;
27 unsigned int freq; /* carrier frequency */
28 unsigned int duty_cycle; /* carrier duty cycle */
31 unsigned long device_is_open;
34 static inline void ir_rx51_on(struct ir_rx51 *ir_rx51)
36 ir_rx51->state.enabled = true;
37 pwm_apply_state(ir_rx51->pwm, &ir_rx51->state);
40 static inline void ir_rx51_off(struct ir_rx51 *ir_rx51)
42 ir_rx51->state.enabled = false;
43 pwm_apply_state(ir_rx51->pwm, &ir_rx51->state);
46 static int init_timing_params(struct ir_rx51 *ir_rx51)
48 ir_rx51->state.period = DIV_ROUND_CLOSEST(NSEC_PER_SEC, ir_rx51->freq);
49 pwm_set_relative_duty_cycle(&ir_rx51->state, ir_rx51->duty_cycle, 100);
54 static enum hrtimer_restart ir_rx51_timer_cb(struct hrtimer *timer)
56 struct ir_rx51 *ir_rx51 = container_of(timer, struct ir_rx51, timer);
59 if (ir_rx51->wbuf_index < 0) {
60 dev_err_ratelimited(ir_rx51->dev,
61 "BUG wbuf_index has value of %i\n",
67 * If we happen to hit an odd latency spike, loop through the
68 * pulses until we catch up.
73 if (ir_rx51->wbuf_index >= WBUF_LEN)
75 if (ir_rx51->wbuf[ir_rx51->wbuf_index] == -1)
78 if (ir_rx51->wbuf_index % 2)
83 ns = US_TO_NS(ir_rx51->wbuf[ir_rx51->wbuf_index]);
84 hrtimer_add_expires_ns(timer, ns);
86 ir_rx51->wbuf_index++;
88 now = timer->base->get_time();
90 } while (hrtimer_get_expires_tv64(timer) < now);
92 return HRTIMER_RESTART;
96 ir_rx51->wbuf_index = -1;
98 wake_up_interruptible(&ir_rx51->wqueue);
100 return HRTIMER_NORESTART;
103 static int ir_rx51_tx(struct rc_dev *dev, unsigned int *buffer,
106 struct ir_rx51 *ir_rx51 = dev->priv;
108 if (count > WBUF_LEN)
111 memcpy(ir_rx51->wbuf, buffer, count * sizeof(unsigned int));
113 /* Wait any pending transfers to finish */
114 wait_event_interruptible(ir_rx51->wqueue, ir_rx51->wbuf_index < 0);
116 init_timing_params(ir_rx51);
117 if (count < WBUF_LEN)
118 ir_rx51->wbuf[count] = -1; /* Insert termination mark */
121 * REVISIT: Adjust latency requirements so the device doesn't go in too
122 * deep sleep states with pm_qos_add_request().
126 ir_rx51->wbuf_index = 1;
127 hrtimer_start(&ir_rx51->timer,
128 ns_to_ktime(US_TO_NS(ir_rx51->wbuf[0])),
131 * Don't return back to the userspace until the transfer has
134 wait_event_interruptible(ir_rx51->wqueue, ir_rx51->wbuf_index < 0);
136 /* REVISIT: Remove pm_qos constraint, we can sleep again */
141 static int ir_rx51_open(struct rc_dev *dev)
143 struct ir_rx51 *ir_rx51 = dev->priv;
145 if (test_and_set_bit(1, &ir_rx51->device_is_open))
148 ir_rx51->pwm = pwm_get(ir_rx51->dev, NULL);
149 if (IS_ERR(ir_rx51->pwm)) {
150 int res = PTR_ERR(ir_rx51->pwm);
152 dev_err(ir_rx51->dev, "pwm_get failed: %d\n", res);
159 static void ir_rx51_release(struct rc_dev *dev)
161 struct ir_rx51 *ir_rx51 = dev->priv;
163 hrtimer_cancel(&ir_rx51->timer);
164 ir_rx51_off(ir_rx51);
165 pwm_put(ir_rx51->pwm);
167 clear_bit(1, &ir_rx51->device_is_open);
170 static struct ir_rx51 ir_rx51 = {
175 static int ir_rx51_set_duty_cycle(struct rc_dev *dev, u32 duty)
177 struct ir_rx51 *ir_rx51 = dev->priv;
179 ir_rx51->duty_cycle = duty;
184 static int ir_rx51_set_tx_carrier(struct rc_dev *dev, u32 carrier)
186 struct ir_rx51 *ir_rx51 = dev->priv;
188 if (carrier > 500000 || carrier < 20000)
191 ir_rx51->freq = carrier;
198 static int ir_rx51_suspend(struct platform_device *dev, pm_message_t state)
201 * In case the device is still open, do not suspend. Normally
202 * this should not be a problem as lircd only keeps the device
203 * open only for short periods of time. We also don't want to
204 * get involved with race conditions that might happen if we
205 * were in a middle of a transmit. Thus, we defer any suspend
206 * actions until transmit has completed.
208 if (test_and_set_bit(1, &ir_rx51.device_is_open))
211 clear_bit(1, &ir_rx51.device_is_open);
216 static int ir_rx51_resume(struct platform_device *dev)
223 #define ir_rx51_suspend NULL
224 #define ir_rx51_resume NULL
226 #endif /* CONFIG_PM */
228 static int ir_rx51_probe(struct platform_device *dev)
230 struct pwm_device *pwm;
231 struct rc_dev *rcdev;
233 pwm = pwm_get(&dev->dev, NULL);
235 return dev_err_probe(&dev->dev, PTR_ERR(pwm), "pwm_get failed\n");
237 /* Use default, in case userspace does not set the carrier */
238 ir_rx51.freq = DIV_ROUND_CLOSEST_ULL(pwm_get_period(pwm), NSEC_PER_SEC);
239 pwm_init_state(pwm, &ir_rx51.state);
242 hrtimer_init(&ir_rx51.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
243 ir_rx51.timer.function = ir_rx51_timer_cb;
245 ir_rx51.dev = &dev->dev;
247 rcdev = devm_rc_allocate_device(&dev->dev, RC_DRIVER_IR_RAW_TX);
251 rcdev->priv = &ir_rx51;
252 rcdev->open = ir_rx51_open;
253 rcdev->close = ir_rx51_release;
254 rcdev->tx_ir = ir_rx51_tx;
255 rcdev->s_tx_duty_cycle = ir_rx51_set_duty_cycle;
256 rcdev->s_tx_carrier = ir_rx51_set_tx_carrier;
257 rcdev->driver_name = KBUILD_MODNAME;
259 ir_rx51.rcdev = rcdev;
261 return devm_rc_register_device(&dev->dev, ir_rx51.rcdev);
264 static const struct of_device_id ir_rx51_match[] = {
266 .compatible = "nokia,n900-ir",
270 MODULE_DEVICE_TABLE(of, ir_rx51_match);
272 static struct platform_driver ir_rx51_platform_driver = {
273 .probe = ir_rx51_probe,
274 .suspend = ir_rx51_suspend,
275 .resume = ir_rx51_resume,
277 .name = KBUILD_MODNAME,
278 .of_match_table = of_match_ptr(ir_rx51_match),
281 module_platform_driver(ir_rx51_platform_driver);
283 MODULE_DESCRIPTION("IR TX driver for Nokia RX51");
284 MODULE_AUTHOR("Nokia Corporation");
285 MODULE_LICENSE("GPL");