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 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <linux/module.h>
23 #include <linux/interrupt.h>
24 #include <linux/uaccess.h>
25 #include <linux/platform_device.h>
26 #include <linux/sched.h>
27 #include <linux/wait.h>
29 #include <plat/dmtimer.h>
30 #include <plat/clock.h>
31 #include <plat/omap-pm.h>
33 #include <media/lirc.h>
34 #include <media/lirc_dev.h>
35 #include <media/ir-rx51.h>
37 #define LIRC_RX51_DRIVER_FEATURES (LIRC_CAN_SET_SEND_DUTY_CYCLE | \
38 LIRC_CAN_SET_SEND_CARRIER | \
41 #define DRIVER_NAME "lirc_rx51"
45 #define TIMER_MAX_VALUE 0xffffffff
48 struct omap_dm_timer *pwm_timer;
49 struct omap_dm_timer *pulse_timer;
51 struct lirc_rx51_platform_data *pdata;
52 wait_queue_head_t wqueue;
54 unsigned long fclk_khz;
55 unsigned int freq; /* carrier frequency */
56 unsigned int duty_cycle; /* carrier duty cycle */
61 unsigned long device_is_open;
65 static void lirc_rx51_on(struct lirc_rx51 *lirc_rx51)
67 omap_dm_timer_set_pwm(lirc_rx51->pwm_timer, 0, 1,
68 OMAP_TIMER_TRIGGER_OVERFLOW_AND_COMPARE);
71 static void lirc_rx51_off(struct lirc_rx51 *lirc_rx51)
73 omap_dm_timer_set_pwm(lirc_rx51->pwm_timer, 0, 1,
74 OMAP_TIMER_TRIGGER_NONE);
77 static int init_timing_params(struct lirc_rx51 *lirc_rx51)
81 load = -(lirc_rx51->fclk_khz * 1000 / lirc_rx51->freq);
82 match = -(lirc_rx51->duty_cycle * -load / 100);
83 omap_dm_timer_set_load(lirc_rx51->pwm_timer, 1, load);
84 omap_dm_timer_set_match(lirc_rx51->pwm_timer, 1, match);
85 omap_dm_timer_write_counter(lirc_rx51->pwm_timer, TIMER_MAX_VALUE - 2);
86 omap_dm_timer_start(lirc_rx51->pwm_timer);
87 omap_dm_timer_set_int_enable(lirc_rx51->pulse_timer, 0);
88 omap_dm_timer_start(lirc_rx51->pulse_timer);
95 #define tics_after(a, b) ((long)(b) - (long)(a) < 0)
97 static int pulse_timer_set_timeout(struct lirc_rx51 *lirc_rx51, int usec)
103 if (lirc_rx51->match == 0)
104 counter = omap_dm_timer_read_counter(lirc_rx51->pulse_timer);
106 counter = lirc_rx51->match;
108 counter += (u32)(lirc_rx51->fclk_khz * usec / (1000));
109 omap_dm_timer_set_match(lirc_rx51->pulse_timer, 1, counter);
110 omap_dm_timer_set_int_enable(lirc_rx51->pulse_timer,
111 OMAP_TIMER_INT_MATCH);
112 if (tics_after(omap_dm_timer_read_counter(lirc_rx51->pulse_timer),
119 static irqreturn_t lirc_rx51_interrupt_handler(int irq, void *ptr)
122 struct lirc_rx51 *lirc_rx51 = ptr;
124 retval = omap_dm_timer_read_status(lirc_rx51->pulse_timer);
128 if (retval & ~OMAP_TIMER_INT_MATCH)
129 dev_err_ratelimited(lirc_rx51->dev,
130 ": Unexpected interrupt source: %x\n", retval);
132 omap_dm_timer_write_status(lirc_rx51->pulse_timer,
133 OMAP_TIMER_INT_MATCH |
134 OMAP_TIMER_INT_OVERFLOW |
135 OMAP_TIMER_INT_CAPTURE);
136 if (lirc_rx51->wbuf_index < 0) {
137 dev_err_ratelimited(lirc_rx51->dev,
138 ": BUG wbuf_index has value of %i\n",
139 lirc_rx51->wbuf_index);
144 * If we happen to hit an odd latency spike, loop through the
145 * pulses until we catch up.
148 if (lirc_rx51->wbuf_index >= WBUF_LEN)
150 if (lirc_rx51->wbuf[lirc_rx51->wbuf_index] == -1)
153 if (lirc_rx51->wbuf_index % 2)
154 lirc_rx51_off(lirc_rx51);
156 lirc_rx51_on(lirc_rx51);
158 retval = pulse_timer_set_timeout(lirc_rx51,
159 lirc_rx51->wbuf[lirc_rx51->wbuf_index]);
160 lirc_rx51->wbuf_index++;
167 lirc_rx51_off(lirc_rx51);
168 lirc_rx51->wbuf_index = -1;
169 omap_dm_timer_stop(lirc_rx51->pwm_timer);
170 omap_dm_timer_stop(lirc_rx51->pulse_timer);
171 omap_dm_timer_set_int_enable(lirc_rx51->pulse_timer, 0);
172 wake_up_interruptible(&lirc_rx51->wqueue);
177 static int lirc_rx51_init_port(struct lirc_rx51 *lirc_rx51)
179 struct clk *clk_fclk;
180 int retval, pwm_timer = lirc_rx51->pwm_timer_num;
182 lirc_rx51->pwm_timer = omap_dm_timer_request_specific(pwm_timer);
183 if (lirc_rx51->pwm_timer == NULL) {
184 dev_err(lirc_rx51->dev, ": Error requesting GPT%d timer\n",
189 lirc_rx51->pulse_timer = omap_dm_timer_request();
190 if (lirc_rx51->pulse_timer == NULL) {
191 dev_err(lirc_rx51->dev, ": Error requesting pulse timer\n");
196 omap_dm_timer_set_source(lirc_rx51->pwm_timer, OMAP_TIMER_SRC_SYS_CLK);
197 omap_dm_timer_set_source(lirc_rx51->pulse_timer,
198 OMAP_TIMER_SRC_SYS_CLK);
200 omap_dm_timer_enable(lirc_rx51->pwm_timer);
201 omap_dm_timer_enable(lirc_rx51->pulse_timer);
203 lirc_rx51->irq_num = omap_dm_timer_get_irq(lirc_rx51->pulse_timer);
204 retval = request_irq(lirc_rx51->irq_num, lirc_rx51_interrupt_handler,
205 IRQF_DISABLED | IRQF_SHARED,
206 "lirc_pulse_timer", lirc_rx51);
208 dev_err(lirc_rx51->dev, ": Failed to request interrupt line\n");
212 clk_fclk = omap_dm_timer_get_fclk(lirc_rx51->pwm_timer);
213 lirc_rx51->fclk_khz = clk_fclk->rate / 1000;
218 omap_dm_timer_free(lirc_rx51->pulse_timer);
220 omap_dm_timer_free(lirc_rx51->pwm_timer);
225 static int lirc_rx51_free_port(struct lirc_rx51 *lirc_rx51)
227 omap_dm_timer_set_int_enable(lirc_rx51->pulse_timer, 0);
228 free_irq(lirc_rx51->irq_num, lirc_rx51);
229 lirc_rx51_off(lirc_rx51);
230 omap_dm_timer_disable(lirc_rx51->pwm_timer);
231 omap_dm_timer_disable(lirc_rx51->pulse_timer);
232 omap_dm_timer_free(lirc_rx51->pwm_timer);
233 omap_dm_timer_free(lirc_rx51->pulse_timer);
234 lirc_rx51->wbuf_index = -1;
239 static ssize_t lirc_rx51_write(struct file *file, const char *buf,
240 size_t n, loff_t *ppos)
243 struct lirc_rx51 *lirc_rx51 = file->private_data;
248 count = n / sizeof(int);
249 if ((count > WBUF_LEN) || (count % 2 == 0))
252 /* Wait any pending transfers to finish */
253 wait_event_interruptible(lirc_rx51->wqueue, lirc_rx51->wbuf_index < 0);
255 if (copy_from_user(lirc_rx51->wbuf, buf, n))
258 /* Sanity check the input pulses */
259 for (i = 0; i < count; i++)
260 if (lirc_rx51->wbuf[i] < 0)
263 init_timing_params(lirc_rx51);
264 if (count < WBUF_LEN)
265 lirc_rx51->wbuf[count] = -1; /* Insert termination mark */
268 * Adjust latency requirements so the device doesn't go in too
271 lirc_rx51->pdata->set_max_mpu_wakeup_lat(lirc_rx51->dev, 50);
273 lirc_rx51_on(lirc_rx51);
274 lirc_rx51->wbuf_index = 1;
275 pulse_timer_set_timeout(lirc_rx51, lirc_rx51->wbuf[0]);
278 * Don't return back to the userspace until the transfer has
281 wait_event_interruptible(lirc_rx51->wqueue, lirc_rx51->wbuf_index < 0);
283 /* We can sleep again */
284 lirc_rx51->pdata->set_max_mpu_wakeup_lat(lirc_rx51->dev, -1);
289 static long lirc_rx51_ioctl(struct file *filep,
290 unsigned int cmd, unsigned long arg)
295 struct lirc_rx51 *lirc_rx51 = filep->private_data;
298 case LIRC_GET_SEND_MODE:
299 result = put_user(LIRC_MODE_PULSE, (unsigned long *)arg);
304 case LIRC_SET_SEND_MODE:
305 result = get_user(value, (unsigned long *)arg);
309 /* only LIRC_MODE_PULSE supported */
310 if (value != LIRC_MODE_PULSE)
314 case LIRC_GET_REC_MODE:
315 result = put_user(0, (unsigned long *) arg);
320 case LIRC_GET_LENGTH:
324 case LIRC_SET_SEND_DUTY_CYCLE:
325 result = get_user(ivalue, (unsigned int *) arg);
329 if (ivalue <= 0 || ivalue > 100) {
330 dev_err(lirc_rx51->dev, ": invalid duty cycle %d\n",
335 lirc_rx51->duty_cycle = ivalue;
338 case LIRC_SET_SEND_CARRIER:
339 result = get_user(ivalue, (unsigned int *) arg);
343 if (ivalue > 500000 || ivalue < 20000) {
344 dev_err(lirc_rx51->dev, ": invalid carrier freq %d\n",
349 lirc_rx51->freq = ivalue;
352 case LIRC_GET_FEATURES:
353 result = put_user(LIRC_RX51_DRIVER_FEATURES,
354 (unsigned long *) arg);
366 static int lirc_rx51_open(struct inode *inode, struct file *file)
368 struct lirc_rx51 *lirc_rx51 = lirc_get_pdata(file);
371 file->private_data = lirc_rx51;
373 if (test_and_set_bit(1, &lirc_rx51->device_is_open))
376 return lirc_rx51_init_port(lirc_rx51);
379 static int lirc_rx51_release(struct inode *inode, struct file *file)
381 struct lirc_rx51 *lirc_rx51 = file->private_data;
383 lirc_rx51_free_port(lirc_rx51);
385 clear_bit(1, &lirc_rx51->device_is_open);
390 static struct lirc_rx51 lirc_rx51 = {
396 static const struct file_operations lirc_fops = {
397 .owner = THIS_MODULE,
398 .write = lirc_rx51_write,
399 .unlocked_ioctl = lirc_rx51_ioctl,
400 .read = lirc_dev_fop_read,
401 .poll = lirc_dev_fop_poll,
402 .open = lirc_rx51_open,
403 .release = lirc_rx51_release,
406 static struct lirc_driver lirc_rx51_driver = {
412 .owner = THIS_MODULE,
417 static int lirc_rx51_suspend(struct platform_device *dev, pm_message_t state)
420 * In case the device is still open, do not suspend. Normally
421 * this should not be a problem as lircd only keeps the device
422 * open only for short periods of time. We also don't want to
423 * get involved with race conditions that might happen if we
424 * were in a middle of a transmit. Thus, we defer any suspend
425 * actions until transmit has completed.
427 if (test_and_set_bit(1, &lirc_rx51.device_is_open))
430 clear_bit(1, &lirc_rx51.device_is_open);
435 static int lirc_rx51_resume(struct platform_device *dev)
442 #define lirc_rx51_suspend NULL
443 #define lirc_rx51_resume NULL
445 #endif /* CONFIG_PM */
447 static int __devinit lirc_rx51_probe(struct platform_device *dev)
449 lirc_rx51_driver.features = LIRC_RX51_DRIVER_FEATURES;
450 lirc_rx51.pdata = dev->dev.platform_data;
451 lirc_rx51.pwm_timer_num = lirc_rx51.pdata->pwm_timer;
452 lirc_rx51.dev = &dev->dev;
453 lirc_rx51_driver.dev = &dev->dev;
454 lirc_rx51_driver.minor = lirc_register_driver(&lirc_rx51_driver);
455 init_waitqueue_head(&lirc_rx51.wqueue);
457 if (lirc_rx51_driver.minor < 0) {
458 dev_err(lirc_rx51.dev, ": lirc_register_driver failed: %d\n",
459 lirc_rx51_driver.minor);
460 return lirc_rx51_driver.minor;
462 dev_info(lirc_rx51.dev, "registration ok, minor: %d, pwm: %d\n",
463 lirc_rx51_driver.minor, lirc_rx51.pwm_timer_num);
468 static int __exit lirc_rx51_remove(struct platform_device *dev)
470 return lirc_unregister_driver(lirc_rx51_driver.minor);
473 struct platform_driver lirc_rx51_platform_driver = {
474 .probe = lirc_rx51_probe,
475 .remove = __exit_p(lirc_rx51_remove),
476 .suspend = lirc_rx51_suspend,
477 .resume = lirc_rx51_resume,
480 .owner = THIS_MODULE,
483 module_platform_driver(lirc_rx51_platform_driver);
485 MODULE_DESCRIPTION("LIRC TX driver for Nokia RX51");
486 MODULE_AUTHOR("Nokia Corporation");
487 MODULE_LICENSE("GPL");