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
;
24 wait_queue_head_t wqueue
;
26 unsigned int freq
; /* carrier frequency */
27 unsigned int duty_cycle
; /* carrier duty cycle */
30 unsigned long device_is_open
;
33 static inline void ir_rx51_on(struct ir_rx51
*ir_rx51
)
35 pwm_enable(ir_rx51
->pwm
);
38 static inline void ir_rx51_off(struct ir_rx51
*ir_rx51
)
40 pwm_disable(ir_rx51
->pwm
);
43 static int init_timing_params(struct ir_rx51
*ir_rx51
)
45 struct pwm_device
*pwm
= ir_rx51
->pwm
;
46 int duty
, period
= DIV_ROUND_CLOSEST(NSEC_PER_SEC
, ir_rx51
->freq
);
48 duty
= DIV_ROUND_CLOSEST(ir_rx51
->duty_cycle
* period
, 100);
50 pwm_config(pwm
, duty
, period
);
55 static enum hrtimer_restart
ir_rx51_timer_cb(struct hrtimer
*timer
)
57 struct ir_rx51
*ir_rx51
= container_of(timer
, struct ir_rx51
, timer
);
60 if (ir_rx51
->wbuf_index
< 0) {
61 dev_err_ratelimited(ir_rx51
->dev
,
62 "BUG wbuf_index has value of %i\n",
68 * If we happen to hit an odd latency spike, loop through the
69 * pulses until we catch up.
74 if (ir_rx51
->wbuf_index
>= WBUF_LEN
)
76 if (ir_rx51
->wbuf
[ir_rx51
->wbuf_index
] == -1)
79 if (ir_rx51
->wbuf_index
% 2)
84 ns
= US_TO_NS(ir_rx51
->wbuf
[ir_rx51
->wbuf_index
]);
85 hrtimer_add_expires_ns(timer
, ns
);
87 ir_rx51
->wbuf_index
++;
89 now
= timer
->base
->get_time();
91 } while (hrtimer_get_expires_tv64(timer
) < now
);
93 return HRTIMER_RESTART
;
97 ir_rx51
->wbuf_index
= -1;
99 wake_up_interruptible(&ir_rx51
->wqueue
);
101 return HRTIMER_NORESTART
;
104 static int ir_rx51_tx(struct rc_dev
*dev
, unsigned int *buffer
,
107 struct ir_rx51
*ir_rx51
= dev
->priv
;
109 if (count
> WBUF_LEN
)
112 memcpy(ir_rx51
->wbuf
, buffer
, count
* sizeof(unsigned int));
114 /* Wait any pending transfers to finish */
115 wait_event_interruptible(ir_rx51
->wqueue
, ir_rx51
->wbuf_index
< 0);
117 init_timing_params(ir_rx51
);
118 if (count
< WBUF_LEN
)
119 ir_rx51
->wbuf
[count
] = -1; /* Insert termination mark */
122 * REVISIT: Adjust latency requirements so the device doesn't go in too
123 * deep sleep states with pm_qos_add_request().
127 ir_rx51
->wbuf_index
= 1;
128 hrtimer_start(&ir_rx51
->timer
,
129 ns_to_ktime(US_TO_NS(ir_rx51
->wbuf
[0])),
132 * Don't return back to the userspace until the transfer has
135 wait_event_interruptible(ir_rx51
->wqueue
, ir_rx51
->wbuf_index
< 0);
137 /* REVISIT: Remove pm_qos constraint, we can sleep again */
142 static int ir_rx51_open(struct rc_dev
*dev
)
144 struct ir_rx51
*ir_rx51
= dev
->priv
;
146 if (test_and_set_bit(1, &ir_rx51
->device_is_open
))
149 ir_rx51
->pwm
= pwm_get(ir_rx51
->dev
, NULL
);
150 if (IS_ERR(ir_rx51
->pwm
)) {
151 int res
= PTR_ERR(ir_rx51
->pwm
);
153 dev_err(ir_rx51
->dev
, "pwm_get failed: %d\n", res
);
160 static void ir_rx51_release(struct rc_dev
*dev
)
162 struct ir_rx51
*ir_rx51
= dev
->priv
;
164 hrtimer_cancel(&ir_rx51
->timer
);
165 ir_rx51_off(ir_rx51
);
166 pwm_put(ir_rx51
->pwm
);
168 clear_bit(1, &ir_rx51
->device_is_open
);
171 static struct ir_rx51 ir_rx51
= {
176 static int ir_rx51_set_duty_cycle(struct rc_dev
*dev
, u32 duty
)
178 struct ir_rx51
*ir_rx51
= dev
->priv
;
180 ir_rx51
->duty_cycle
= duty
;
185 static int ir_rx51_set_tx_carrier(struct rc_dev
*dev
, u32 carrier
)
187 struct ir_rx51
*ir_rx51
= dev
->priv
;
189 if (carrier
> 500000 || carrier
< 20000)
192 ir_rx51
->freq
= carrier
;
199 static int ir_rx51_suspend(struct platform_device
*dev
, pm_message_t state
)
202 * In case the device is still open, do not suspend. Normally
203 * this should not be a problem as lircd only keeps the device
204 * open only for short periods of time. We also don't want to
205 * get involved with race conditions that might happen if we
206 * were in a middle of a transmit. Thus, we defer any suspend
207 * actions until transmit has completed.
209 if (test_and_set_bit(1, &ir_rx51
.device_is_open
))
212 clear_bit(1, &ir_rx51
.device_is_open
);
217 static int ir_rx51_resume(struct platform_device
*dev
)
224 #define ir_rx51_suspend NULL
225 #define ir_rx51_resume NULL
227 #endif /* CONFIG_PM */
229 static int ir_rx51_probe(struct platform_device
*dev
)
231 struct pwm_device
*pwm
;
232 struct rc_dev
*rcdev
;
234 pwm
= pwm_get(&dev
->dev
, NULL
);
236 int err
= PTR_ERR(pwm
);
238 if (err
!= -EPROBE_DEFER
)
239 dev_err(&dev
->dev
, "pwm_get failed: %d\n", err
);
243 /* Use default, in case userspace does not set the carrier */
244 ir_rx51
.freq
= DIV_ROUND_CLOSEST(pwm_get_period(pwm
), NSEC_PER_SEC
);
247 hrtimer_init(&ir_rx51
.timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
248 ir_rx51
.timer
.function
= ir_rx51_timer_cb
;
250 ir_rx51
.dev
= &dev
->dev
;
252 rcdev
= devm_rc_allocate_device(&dev
->dev
, RC_DRIVER_IR_RAW_TX
);
256 rcdev
->priv
= &ir_rx51
;
257 rcdev
->open
= ir_rx51_open
;
258 rcdev
->close
= ir_rx51_release
;
259 rcdev
->tx_ir
= ir_rx51_tx
;
260 rcdev
->s_tx_duty_cycle
= ir_rx51_set_duty_cycle
;
261 rcdev
->s_tx_carrier
= ir_rx51_set_tx_carrier
;
262 rcdev
->driver_name
= KBUILD_MODNAME
;
264 ir_rx51
.rcdev
= rcdev
;
266 return devm_rc_register_device(&dev
->dev
, ir_rx51
.rcdev
);
269 static int ir_rx51_remove(struct platform_device
*dev
)
274 static const struct of_device_id ir_rx51_match
[] = {
276 .compatible
= "nokia,n900-ir",
280 MODULE_DEVICE_TABLE(of
, ir_rx51_match
);
282 static struct platform_driver ir_rx51_platform_driver
= {
283 .probe
= ir_rx51_probe
,
284 .remove
= ir_rx51_remove
,
285 .suspend
= ir_rx51_suspend
,
286 .resume
= ir_rx51_resume
,
288 .name
= KBUILD_MODNAME
,
289 .of_match_table
= of_match_ptr(ir_rx51_match
),
292 module_platform_driver(ir_rx51_platform_driver
);
294 MODULE_DESCRIPTION("IR TX driver for Nokia RX51");
295 MODULE_AUTHOR("Nokia Corporation");
296 MODULE_LICENSE("GPL");