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>
25 #include <media/lirc.h>
26 #include <media/lirc_dev.h>
27 #include <linux/platform_data/pwm_omap_dmtimer.h>
28 #include <linux/platform_data/media/ir-rx51.h>
30 #define LIRC_RX51_DRIVER_FEATURES (LIRC_CAN_SET_SEND_DUTY_CYCLE | \
31 LIRC_CAN_SET_SEND_CARRIER | \
34 #define DRIVER_NAME "lirc_rx51"
38 #define TIMER_MAX_VALUE 0xffffffff
41 struct pwm_device
*pwm
;
42 pwm_omap_dmtimer
*pulse_timer
;
43 struct pwm_omap_dmtimer_pdata
*dmtimer
;
45 struct lirc_rx51_platform_data
*pdata
;
46 wait_queue_head_t wqueue
;
48 unsigned long fclk_khz
;
49 unsigned int freq
; /* carrier frequency */
50 unsigned int duty_cycle
; /* carrier duty cycle */
55 unsigned long device_is_open
;
58 static void lirc_rx51_on(struct lirc_rx51
*lirc_rx51
)
60 pwm_enable(lirc_rx51
->pwm
);
63 static void lirc_rx51_off(struct lirc_rx51
*lirc_rx51
)
65 pwm_disable(lirc_rx51
->pwm
);
68 static int init_timing_params(struct lirc_rx51
*lirc_rx51
)
70 struct pwm_device
*pwm
= lirc_rx51
->pwm
;
71 int duty
, period
= DIV_ROUND_CLOSEST(NSEC_PER_SEC
, lirc_rx51
->freq
);
73 duty
= DIV_ROUND_CLOSEST(lirc_rx51
->duty_cycle
* period
, 100);
74 lirc_rx51
->dmtimer
->set_int_enable(lirc_rx51
->pulse_timer
, 0);
76 pwm_config(pwm
, duty
, period
);
78 lirc_rx51
->dmtimer
->start(lirc_rx51
->pulse_timer
);
85 #define tics_after(a, b) ((long)(b) - (long)(a) < 0)
87 static int pulse_timer_set_timeout(struct lirc_rx51
*lirc_rx51
, int usec
)
93 if (lirc_rx51
->match
== 0)
94 counter
= lirc_rx51
->dmtimer
->read_counter(lirc_rx51
->pulse_timer
);
96 counter
= lirc_rx51
->match
;
98 counter
+= (u32
)(lirc_rx51
->fclk_khz
* usec
/ (1000));
99 lirc_rx51
->dmtimer
->set_match(lirc_rx51
->pulse_timer
, 1, counter
);
100 lirc_rx51
->dmtimer
->set_int_enable(lirc_rx51
->pulse_timer
,
101 PWM_OMAP_DMTIMER_INT_MATCH
);
102 if (tics_after(lirc_rx51
->dmtimer
->read_counter(lirc_rx51
->pulse_timer
),
109 static irqreturn_t
lirc_rx51_interrupt_handler(int irq
, void *ptr
)
112 struct lirc_rx51
*lirc_rx51
= ptr
;
114 retval
= lirc_rx51
->dmtimer
->read_status(lirc_rx51
->pulse_timer
);
118 if (retval
& ~PWM_OMAP_DMTIMER_INT_MATCH
)
119 dev_err_ratelimited(lirc_rx51
->dev
,
120 ": Unexpected interrupt source: %x\n", retval
);
122 lirc_rx51
->dmtimer
->write_status(lirc_rx51
->pulse_timer
,
123 PWM_OMAP_DMTIMER_INT_MATCH
|
124 PWM_OMAP_DMTIMER_INT_OVERFLOW
|
125 PWM_OMAP_DMTIMER_INT_CAPTURE
);
126 if (lirc_rx51
->wbuf_index
< 0) {
127 dev_err_ratelimited(lirc_rx51
->dev
,
128 ": BUG wbuf_index has value of %i\n",
129 lirc_rx51
->wbuf_index
);
134 * If we happen to hit an odd latency spike, loop through the
135 * pulses until we catch up.
138 if (lirc_rx51
->wbuf_index
>= WBUF_LEN
)
140 if (lirc_rx51
->wbuf
[lirc_rx51
->wbuf_index
] == -1)
143 if (lirc_rx51
->wbuf_index
% 2)
144 lirc_rx51_off(lirc_rx51
);
146 lirc_rx51_on(lirc_rx51
);
148 retval
= pulse_timer_set_timeout(lirc_rx51
,
149 lirc_rx51
->wbuf
[lirc_rx51
->wbuf_index
]);
150 lirc_rx51
->wbuf_index
++;
157 lirc_rx51_off(lirc_rx51
);
158 lirc_rx51
->wbuf_index
= -1;
160 lirc_rx51
->dmtimer
->stop(lirc_rx51
->pulse_timer
);
161 lirc_rx51
->dmtimer
->set_int_enable(lirc_rx51
->pulse_timer
, 0);
162 wake_up_interruptible(&lirc_rx51
->wqueue
);
167 static int lirc_rx51_init_port(struct lirc_rx51
*lirc_rx51
)
169 struct clk
*clk_fclk
;
172 lirc_rx51
->pwm
= pwm_get(lirc_rx51
->dev
, NULL
);
173 if (IS_ERR(lirc_rx51
->pwm
)) {
174 retval
= PTR_ERR(lirc_rx51
->pwm
);
175 dev_err(lirc_rx51
->dev
, ": pwm_get failed: %d\n", retval
);
179 lirc_rx51
->pulse_timer
= lirc_rx51
->dmtimer
->request();
180 if (lirc_rx51
->pulse_timer
== NULL
) {
181 dev_err(lirc_rx51
->dev
, ": Error requesting pulse timer\n");
186 lirc_rx51
->dmtimer
->set_source(lirc_rx51
->pulse_timer
,
187 PWM_OMAP_DMTIMER_SRC_SYS_CLK
);
188 lirc_rx51
->dmtimer
->enable(lirc_rx51
->pulse_timer
);
190 lirc_rx51
->dmtimer
->get_irq(lirc_rx51
->pulse_timer
);
191 retval
= request_irq(lirc_rx51
->irq_num
, lirc_rx51_interrupt_handler
,
192 IRQF_SHARED
, "lirc_pulse_timer", lirc_rx51
);
194 dev_err(lirc_rx51
->dev
, ": Failed to request interrupt line\n");
198 clk_fclk
= lirc_rx51
->dmtimer
->get_fclk(lirc_rx51
->pulse_timer
);
199 lirc_rx51
->fclk_khz
= clk_get_rate(clk_fclk
) / 1000;
204 lirc_rx51
->dmtimer
->free(lirc_rx51
->pulse_timer
);
206 pwm_put(lirc_rx51
->pwm
);
211 static int lirc_rx51_free_port(struct lirc_rx51
*lirc_rx51
)
213 lirc_rx51
->dmtimer
->set_int_enable(lirc_rx51
->pulse_timer
, 0);
214 free_irq(lirc_rx51
->irq_num
, lirc_rx51
);
215 lirc_rx51_off(lirc_rx51
);
216 lirc_rx51
->dmtimer
->disable(lirc_rx51
->pulse_timer
);
217 lirc_rx51
->dmtimer
->free(lirc_rx51
->pulse_timer
);
218 lirc_rx51
->wbuf_index
= -1;
219 pwm_put(lirc_rx51
->pwm
);
224 static ssize_t
lirc_rx51_write(struct file
*file
, const char *buf
,
225 size_t n
, loff_t
*ppos
)
228 struct lirc_rx51
*lirc_rx51
= file
->private_data
;
233 count
= n
/ sizeof(int);
234 if ((count
> WBUF_LEN
) || (count
% 2 == 0))
237 /* Wait any pending transfers to finish */
238 wait_event_interruptible(lirc_rx51
->wqueue
, lirc_rx51
->wbuf_index
< 0);
240 if (copy_from_user(lirc_rx51
->wbuf
, buf
, n
))
243 /* Sanity check the input pulses */
244 for (i
= 0; i
< count
; i
++)
245 if (lirc_rx51
->wbuf
[i
] < 0)
248 init_timing_params(lirc_rx51
);
249 if (count
< WBUF_LEN
)
250 lirc_rx51
->wbuf
[count
] = -1; /* Insert termination mark */
253 * Adjust latency requirements so the device doesn't go in too
256 lirc_rx51
->pdata
->set_max_mpu_wakeup_lat(lirc_rx51
->dev
, 50);
258 lirc_rx51_on(lirc_rx51
);
259 lirc_rx51
->wbuf_index
= 1;
260 pulse_timer_set_timeout(lirc_rx51
, lirc_rx51
->wbuf
[0]);
263 * Don't return back to the userspace until the transfer has
266 wait_event_interruptible(lirc_rx51
->wqueue
, lirc_rx51
->wbuf_index
< 0);
268 /* We can sleep again */
269 lirc_rx51
->pdata
->set_max_mpu_wakeup_lat(lirc_rx51
->dev
, -1);
274 static long lirc_rx51_ioctl(struct file
*filep
,
275 unsigned int cmd
, unsigned long arg
)
280 struct lirc_rx51
*lirc_rx51
= filep
->private_data
;
283 case LIRC_GET_SEND_MODE
:
284 result
= put_user(LIRC_MODE_PULSE
, (unsigned long *)arg
);
289 case LIRC_SET_SEND_MODE
:
290 result
= get_user(value
, (unsigned long *)arg
);
294 /* only LIRC_MODE_PULSE supported */
295 if (value
!= LIRC_MODE_PULSE
)
299 case LIRC_GET_REC_MODE
:
300 result
= put_user(0, (unsigned long *) arg
);
305 case LIRC_GET_LENGTH
:
309 case LIRC_SET_SEND_DUTY_CYCLE
:
310 result
= get_user(ivalue
, (unsigned int *) arg
);
314 if (ivalue
<= 0 || ivalue
> 100) {
315 dev_err(lirc_rx51
->dev
, ": invalid duty cycle %d\n",
320 lirc_rx51
->duty_cycle
= ivalue
;
323 case LIRC_SET_SEND_CARRIER
:
324 result
= get_user(ivalue
, (unsigned int *) arg
);
328 if (ivalue
> 500000 || ivalue
< 20000) {
329 dev_err(lirc_rx51
->dev
, ": invalid carrier freq %d\n",
334 lirc_rx51
->freq
= ivalue
;
337 case LIRC_GET_FEATURES
:
338 result
= put_user(LIRC_RX51_DRIVER_FEATURES
,
339 (unsigned long *) arg
);
351 static int lirc_rx51_open(struct inode
*inode
, struct file
*file
)
353 struct lirc_rx51
*lirc_rx51
= lirc_get_pdata(file
);
356 file
->private_data
= lirc_rx51
;
358 if (test_and_set_bit(1, &lirc_rx51
->device_is_open
))
361 return lirc_rx51_init_port(lirc_rx51
);
364 static int lirc_rx51_release(struct inode
*inode
, struct file
*file
)
366 struct lirc_rx51
*lirc_rx51
= file
->private_data
;
368 lirc_rx51_free_port(lirc_rx51
);
370 clear_bit(1, &lirc_rx51
->device_is_open
);
375 static struct lirc_rx51 lirc_rx51
= {
380 static const struct file_operations lirc_fops
= {
381 .owner
= THIS_MODULE
,
382 .write
= lirc_rx51_write
,
383 .unlocked_ioctl
= lirc_rx51_ioctl
,
384 .read
= lirc_dev_fop_read
,
385 .poll
= lirc_dev_fop_poll
,
386 .open
= lirc_rx51_open
,
387 .release
= lirc_rx51_release
,
390 static struct lirc_driver lirc_rx51_driver
= {
396 .owner
= THIS_MODULE
,
401 static int lirc_rx51_suspend(struct platform_device
*dev
, pm_message_t state
)
404 * In case the device is still open, do not suspend. Normally
405 * this should not be a problem as lircd only keeps the device
406 * open only for short periods of time. We also don't want to
407 * get involved with race conditions that might happen if we
408 * were in a middle of a transmit. Thus, we defer any suspend
409 * actions until transmit has completed.
411 if (test_and_set_bit(1, &lirc_rx51
.device_is_open
))
414 clear_bit(1, &lirc_rx51
.device_is_open
);
419 static int lirc_rx51_resume(struct platform_device
*dev
)
426 #define lirc_rx51_suspend NULL
427 #define lirc_rx51_resume NULL
429 #endif /* CONFIG_PM */
431 static int lirc_rx51_probe(struct platform_device
*dev
)
433 struct pwm_device
*pwm
;
435 lirc_rx51_driver
.features
= LIRC_RX51_DRIVER_FEATURES
;
436 lirc_rx51
.pdata
= dev
->dev
.platform_data
;
438 if (!lirc_rx51
.pdata
) {
439 dev_err(&dev
->dev
, "Platform Data is missing\n");
443 if (!lirc_rx51
.pdata
->dmtimer
) {
444 dev_err(&dev
->dev
, "no dmtimer?\n");
448 pwm
= pwm_get(&dev
->dev
, NULL
);
450 int err
= PTR_ERR(pwm
);
452 if (err
!= -EPROBE_DEFER
)
453 dev_err(&dev
->dev
, "pwm_get failed: %d\n", err
);
457 /* Use default, in case userspace does not set the carrier */
458 lirc_rx51
.freq
= DIV_ROUND_CLOSEST(pwm_get_period(pwm
), NSEC_PER_SEC
);
461 lirc_rx51
.dmtimer
= lirc_rx51
.pdata
->dmtimer
;
462 lirc_rx51
.dev
= &dev
->dev
;
463 lirc_rx51_driver
.dev
= &dev
->dev
;
464 lirc_rx51_driver
.minor
= lirc_register_driver(&lirc_rx51_driver
);
465 init_waitqueue_head(&lirc_rx51
.wqueue
);
467 if (lirc_rx51_driver
.minor
< 0) {
468 dev_err(lirc_rx51
.dev
, ": lirc_register_driver failed: %d\n",
469 lirc_rx51_driver
.minor
);
470 return lirc_rx51_driver
.minor
;
476 static int lirc_rx51_remove(struct platform_device
*dev
)
478 return lirc_unregister_driver(lirc_rx51_driver
.minor
);
481 struct platform_driver lirc_rx51_platform_driver
= {
482 .probe
= lirc_rx51_probe
,
483 .remove
= lirc_rx51_remove
,
484 .suspend
= lirc_rx51_suspend
,
485 .resume
= lirc_rx51_resume
,
488 .owner
= THIS_MODULE
,
491 module_platform_driver(lirc_rx51_platform_driver
);
493 MODULE_DESCRIPTION("LIRC TX driver for Nokia RX51");
494 MODULE_AUTHOR("Nokia Corporation");
495 MODULE_LICENSE("GPL");