2 * Copyright (C) 2008 Nokia Corporation
4 * Based on lirc_serial.c
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.
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.
16 #include <linux/clk.h>
17 #include <linux/module.h>
18 #include <linux/interrupt.h>
19 #include <linux/uaccess.h>
20 #include <linux/platform_device.h>
21 #include <linux/sched.h>
22 #include <linux/wait.h>
23 #include <linux/pwm.h>
26 #include <media/lirc.h>
27 #include <media/lirc_dev.h>
28 #include <linux/platform_data/pwm_omap_dmtimer.h>
29 #include <linux/platform_data/media/ir-rx51.h>
31 #define LIRC_RX51_DRIVER_FEATURES (LIRC_CAN_SET_SEND_DUTY_CYCLE | \
32 LIRC_CAN_SET_SEND_CARRIER | \
35 #define DRIVER_NAME "lirc_rx51"
39 #define TIMER_MAX_VALUE 0xffffffff
42 struct pwm_device
*pwm
;
43 pwm_omap_dmtimer
*pulse_timer
;
44 struct pwm_omap_dmtimer_pdata
*dmtimer
;
46 struct lirc_rx51_platform_data
*pdata
;
47 wait_queue_head_t wqueue
;
49 unsigned long fclk_khz
;
50 unsigned int freq
; /* carrier frequency */
51 unsigned int duty_cycle
; /* carrier duty cycle */
56 unsigned long device_is_open
;
59 static void lirc_rx51_on(struct lirc_rx51
*lirc_rx51
)
61 pwm_enable(lirc_rx51
->pwm
);
64 static void lirc_rx51_off(struct lirc_rx51
*lirc_rx51
)
66 pwm_disable(lirc_rx51
->pwm
);
69 static int init_timing_params(struct lirc_rx51
*lirc_rx51
)
71 struct pwm_device
*pwm
= lirc_rx51
->pwm
;
72 int duty
, period
= DIV_ROUND_CLOSEST(NSEC_PER_SEC
, lirc_rx51
->freq
);
74 duty
= DIV_ROUND_CLOSEST(lirc_rx51
->duty_cycle
* period
, 100);
75 lirc_rx51
->dmtimer
->set_int_enable(lirc_rx51
->pulse_timer
, 0);
77 pwm_config(pwm
, duty
, period
);
79 lirc_rx51
->dmtimer
->start(lirc_rx51
->pulse_timer
);
86 #define tics_after(a, b) ((long)(b) - (long)(a) < 0)
88 static int pulse_timer_set_timeout(struct lirc_rx51
*lirc_rx51
, int usec
)
94 if (lirc_rx51
->match
== 0)
95 counter
= lirc_rx51
->dmtimer
->read_counter(lirc_rx51
->pulse_timer
);
97 counter
= lirc_rx51
->match
;
99 counter
+= (u32
)(lirc_rx51
->fclk_khz
* usec
/ (1000));
100 lirc_rx51
->dmtimer
->set_match(lirc_rx51
->pulse_timer
, 1, counter
);
101 lirc_rx51
->dmtimer
->set_int_enable(lirc_rx51
->pulse_timer
,
102 PWM_OMAP_DMTIMER_INT_MATCH
);
103 if (tics_after(lirc_rx51
->dmtimer
->read_counter(lirc_rx51
->pulse_timer
),
110 static irqreturn_t
lirc_rx51_interrupt_handler(int irq
, void *ptr
)
113 struct lirc_rx51
*lirc_rx51
= ptr
;
115 retval
= lirc_rx51
->dmtimer
->read_status(lirc_rx51
->pulse_timer
);
119 if (retval
& ~PWM_OMAP_DMTIMER_INT_MATCH
)
120 dev_err_ratelimited(lirc_rx51
->dev
,
121 ": Unexpected interrupt source: %x\n", retval
);
123 lirc_rx51
->dmtimer
->write_status(lirc_rx51
->pulse_timer
,
124 PWM_OMAP_DMTIMER_INT_MATCH
|
125 PWM_OMAP_DMTIMER_INT_OVERFLOW
|
126 PWM_OMAP_DMTIMER_INT_CAPTURE
);
127 if (lirc_rx51
->wbuf_index
< 0) {
128 dev_err_ratelimited(lirc_rx51
->dev
,
129 ": BUG wbuf_index has value of %i\n",
130 lirc_rx51
->wbuf_index
);
135 * If we happen to hit an odd latency spike, loop through the
136 * pulses until we catch up.
139 if (lirc_rx51
->wbuf_index
>= WBUF_LEN
)
141 if (lirc_rx51
->wbuf
[lirc_rx51
->wbuf_index
] == -1)
144 if (lirc_rx51
->wbuf_index
% 2)
145 lirc_rx51_off(lirc_rx51
);
147 lirc_rx51_on(lirc_rx51
);
149 retval
= pulse_timer_set_timeout(lirc_rx51
,
150 lirc_rx51
->wbuf
[lirc_rx51
->wbuf_index
]);
151 lirc_rx51
->wbuf_index
++;
158 lirc_rx51_off(lirc_rx51
);
159 lirc_rx51
->wbuf_index
= -1;
161 lirc_rx51
->dmtimer
->stop(lirc_rx51
->pulse_timer
);
162 lirc_rx51
->dmtimer
->set_int_enable(lirc_rx51
->pulse_timer
, 0);
163 wake_up_interruptible(&lirc_rx51
->wqueue
);
168 static int lirc_rx51_init_port(struct lirc_rx51
*lirc_rx51
)
170 struct clk
*clk_fclk
;
173 lirc_rx51
->pwm
= pwm_get(lirc_rx51
->dev
, NULL
);
174 if (IS_ERR(lirc_rx51
->pwm
)) {
175 retval
= PTR_ERR(lirc_rx51
->pwm
);
176 dev_err(lirc_rx51
->dev
, ": pwm_get failed: %d\n", retval
);
180 lirc_rx51
->pulse_timer
= lirc_rx51
->dmtimer
->request();
181 if (lirc_rx51
->pulse_timer
== NULL
) {
182 dev_err(lirc_rx51
->dev
, ": Error requesting pulse timer\n");
187 lirc_rx51
->dmtimer
->set_source(lirc_rx51
->pulse_timer
,
188 PWM_OMAP_DMTIMER_SRC_SYS_CLK
);
189 lirc_rx51
->dmtimer
->enable(lirc_rx51
->pulse_timer
);
191 lirc_rx51
->dmtimer
->get_irq(lirc_rx51
->pulse_timer
);
192 retval
= request_irq(lirc_rx51
->irq_num
, lirc_rx51_interrupt_handler
,
193 IRQF_SHARED
, "lirc_pulse_timer", lirc_rx51
);
195 dev_err(lirc_rx51
->dev
, ": Failed to request interrupt line\n");
199 clk_fclk
= lirc_rx51
->dmtimer
->get_fclk(lirc_rx51
->pulse_timer
);
200 lirc_rx51
->fclk_khz
= clk_get_rate(clk_fclk
) / 1000;
205 lirc_rx51
->dmtimer
->free(lirc_rx51
->pulse_timer
);
207 pwm_put(lirc_rx51
->pwm
);
212 static int lirc_rx51_free_port(struct lirc_rx51
*lirc_rx51
)
214 lirc_rx51
->dmtimer
->set_int_enable(lirc_rx51
->pulse_timer
, 0);
215 free_irq(lirc_rx51
->irq_num
, lirc_rx51
);
216 lirc_rx51_off(lirc_rx51
);
217 lirc_rx51
->dmtimer
->disable(lirc_rx51
->pulse_timer
);
218 lirc_rx51
->dmtimer
->free(lirc_rx51
->pulse_timer
);
219 lirc_rx51
->wbuf_index
= -1;
220 pwm_put(lirc_rx51
->pwm
);
225 static ssize_t
lirc_rx51_write(struct file
*file
, const char *buf
,
226 size_t n
, loff_t
*ppos
)
229 struct lirc_rx51
*lirc_rx51
= file
->private_data
;
234 count
= n
/ sizeof(int);
235 if ((count
> WBUF_LEN
) || (count
% 2 == 0))
238 /* Wait any pending transfers to finish */
239 wait_event_interruptible(lirc_rx51
->wqueue
, lirc_rx51
->wbuf_index
< 0);
241 if (copy_from_user(lirc_rx51
->wbuf
, buf
, n
))
244 /* Sanity check the input pulses */
245 for (i
= 0; i
< count
; i
++)
246 if (lirc_rx51
->wbuf
[i
] < 0)
249 init_timing_params(lirc_rx51
);
250 if (count
< WBUF_LEN
)
251 lirc_rx51
->wbuf
[count
] = -1; /* Insert termination mark */
254 * Adjust latency requirements so the device doesn't go in too
257 lirc_rx51
->pdata
->set_max_mpu_wakeup_lat(lirc_rx51
->dev
, 50);
259 lirc_rx51_on(lirc_rx51
);
260 lirc_rx51
->wbuf_index
= 1;
261 pulse_timer_set_timeout(lirc_rx51
, lirc_rx51
->wbuf
[0]);
264 * Don't return back to the userspace until the transfer has
267 wait_event_interruptible(lirc_rx51
->wqueue
, lirc_rx51
->wbuf_index
< 0);
269 /* We can sleep again */
270 lirc_rx51
->pdata
->set_max_mpu_wakeup_lat(lirc_rx51
->dev
, -1);
275 static long lirc_rx51_ioctl(struct file
*filep
,
276 unsigned int cmd
, unsigned long arg
)
281 struct lirc_rx51
*lirc_rx51
= filep
->private_data
;
284 case LIRC_GET_SEND_MODE
:
285 result
= put_user(LIRC_MODE_PULSE
, (unsigned long *)arg
);
290 case LIRC_SET_SEND_MODE
:
291 result
= get_user(value
, (unsigned long *)arg
);
295 /* only LIRC_MODE_PULSE supported */
296 if (value
!= LIRC_MODE_PULSE
)
300 case LIRC_GET_REC_MODE
:
301 result
= put_user(0, (unsigned long *) arg
);
306 case LIRC_GET_LENGTH
:
310 case LIRC_SET_SEND_DUTY_CYCLE
:
311 result
= get_user(ivalue
, (unsigned int *) arg
);
315 if (ivalue
<= 0 || ivalue
> 100) {
316 dev_err(lirc_rx51
->dev
, ": invalid duty cycle %d\n",
321 lirc_rx51
->duty_cycle
= ivalue
;
324 case LIRC_SET_SEND_CARRIER
:
325 result
= get_user(ivalue
, (unsigned int *) arg
);
329 if (ivalue
> 500000 || ivalue
< 20000) {
330 dev_err(lirc_rx51
->dev
, ": invalid carrier freq %d\n",
335 lirc_rx51
->freq
= ivalue
;
338 case LIRC_GET_FEATURES
:
339 result
= put_user(LIRC_RX51_DRIVER_FEATURES
,
340 (unsigned long *) arg
);
352 static int lirc_rx51_open(struct inode
*inode
, struct file
*file
)
354 struct lirc_rx51
*lirc_rx51
= lirc_get_pdata(file
);
357 file
->private_data
= lirc_rx51
;
359 if (test_and_set_bit(1, &lirc_rx51
->device_is_open
))
362 return lirc_rx51_init_port(lirc_rx51
);
365 static int lirc_rx51_release(struct inode
*inode
, struct file
*file
)
367 struct lirc_rx51
*lirc_rx51
= file
->private_data
;
369 lirc_rx51_free_port(lirc_rx51
);
371 clear_bit(1, &lirc_rx51
->device_is_open
);
376 static struct lirc_rx51 lirc_rx51
= {
381 static const struct file_operations lirc_fops
= {
382 .owner
= THIS_MODULE
,
383 .write
= lirc_rx51_write
,
384 .unlocked_ioctl
= lirc_rx51_ioctl
,
385 .read
= lirc_dev_fop_read
,
386 .poll
= lirc_dev_fop_poll
,
387 .open
= lirc_rx51_open
,
388 .release
= lirc_rx51_release
,
391 static struct lirc_driver lirc_rx51_driver
= {
397 .owner
= THIS_MODULE
,
402 static int lirc_rx51_suspend(struct platform_device
*dev
, pm_message_t state
)
405 * In case the device is still open, do not suspend. Normally
406 * this should not be a problem as lircd only keeps the device
407 * open only for short periods of time. We also don't want to
408 * get involved with race conditions that might happen if we
409 * were in a middle of a transmit. Thus, we defer any suspend
410 * actions until transmit has completed.
412 if (test_and_set_bit(1, &lirc_rx51
.device_is_open
))
415 clear_bit(1, &lirc_rx51
.device_is_open
);
420 static int lirc_rx51_resume(struct platform_device
*dev
)
427 #define lirc_rx51_suspend NULL
428 #define lirc_rx51_resume NULL
430 #endif /* CONFIG_PM */
432 static int lirc_rx51_probe(struct platform_device
*dev
)
434 struct pwm_device
*pwm
;
436 lirc_rx51_driver
.features
= LIRC_RX51_DRIVER_FEATURES
;
437 lirc_rx51
.pdata
= dev
->dev
.platform_data
;
439 if (!lirc_rx51
.pdata
) {
440 dev_err(&dev
->dev
, "Platform Data is missing\n");
444 if (!lirc_rx51
.pdata
->dmtimer
) {
445 dev_err(&dev
->dev
, "no dmtimer?\n");
449 pwm
= pwm_get(&dev
->dev
, NULL
);
451 int err
= PTR_ERR(pwm
);
453 if (err
!= -EPROBE_DEFER
)
454 dev_err(&dev
->dev
, "pwm_get failed: %d\n", err
);
458 /* Use default, in case userspace does not set the carrier */
459 lirc_rx51
.freq
= DIV_ROUND_CLOSEST(pwm_get_period(pwm
), NSEC_PER_SEC
);
462 lirc_rx51
.dmtimer
= lirc_rx51
.pdata
->dmtimer
;
463 lirc_rx51
.dev
= &dev
->dev
;
464 lirc_rx51_driver
.dev
= &dev
->dev
;
465 lirc_rx51_driver
.minor
= lirc_register_driver(&lirc_rx51_driver
);
466 init_waitqueue_head(&lirc_rx51
.wqueue
);
468 if (lirc_rx51_driver
.minor
< 0) {
469 dev_err(lirc_rx51
.dev
, ": lirc_register_driver failed: %d\n",
470 lirc_rx51_driver
.minor
);
471 return lirc_rx51_driver
.minor
;
477 static int lirc_rx51_remove(struct platform_device
*dev
)
479 return lirc_unregister_driver(lirc_rx51_driver
.minor
);
482 static const struct of_device_id lirc_rx51_match
[] = {
484 .compatible
= "nokia,n900-ir",
488 MODULE_DEVICE_TABLE(of
, lirc_rx51_match
);
490 struct platform_driver lirc_rx51_platform_driver
= {
491 .probe
= lirc_rx51_probe
,
492 .remove
= lirc_rx51_remove
,
493 .suspend
= lirc_rx51_suspend
,
494 .resume
= lirc_rx51_resume
,
497 .of_match_table
= of_match_ptr(lirc_rx51_match
),
500 module_platform_driver(lirc_rx51_platform_driver
);
502 MODULE_DESCRIPTION("LIRC TX driver for Nokia RX51");
503 MODULE_AUTHOR("Nokia Corporation");
504 MODULE_LICENSE("GPL");