[media] rename most media/video pci drivers to media/pci
[linux-2.6/btrfs-unstable.git] / drivers / media / rc / ir-rx51.c
blob9487dd33a89ae664c41932b687332f92dd6c1853
1 /*
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 | \
39 LIRC_CAN_SEND_PULSE)
41 #define DRIVER_NAME "lirc_rx51"
43 #define WBUF_LEN 256
45 #define TIMER_MAX_VALUE 0xffffffff
47 struct lirc_rx51 {
48 struct omap_dm_timer *pwm_timer;
49 struct omap_dm_timer *pulse_timer;
50 struct device *dev;
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 */
57 unsigned int irq_num;
58 unsigned int match;
59 int wbuf[WBUF_LEN];
60 int wbuf_index;
61 unsigned long device_is_open;
62 unsigned int pwm_timer_num;
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)
79 u32 load, match;
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);
90 lirc_rx51->match = 0;
92 return 0;
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)
99 int counter;
101 BUG_ON(usec < 0);
103 if (lirc_rx51->match == 0)
104 counter = omap_dm_timer_read_counter(lirc_rx51->pulse_timer);
105 else
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),
113 counter)) {
114 return 1;
116 return 0;
119 static irqreturn_t lirc_rx51_interrupt_handler(int irq, void *ptr)
121 unsigned int retval;
122 struct lirc_rx51 *lirc_rx51 = ptr;
124 retval = omap_dm_timer_read_status(lirc_rx51->pulse_timer);
125 if (!retval)
126 return IRQ_NONE;
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, 7);
133 if (lirc_rx51->wbuf_index < 0) {
134 dev_err_ratelimited(lirc_rx51->dev,
135 ": BUG wbuf_index has value of %i\n",
136 lirc_rx51->wbuf_index);
137 goto end;
141 * If we happen to hit an odd latency spike, loop through the
142 * pulses until we catch up.
144 do {
145 if (lirc_rx51->wbuf_index >= WBUF_LEN)
146 goto end;
147 if (lirc_rx51->wbuf[lirc_rx51->wbuf_index] == -1)
148 goto end;
150 if (lirc_rx51->wbuf_index % 2)
151 lirc_rx51_off(lirc_rx51);
152 else
153 lirc_rx51_on(lirc_rx51);
155 retval = pulse_timer_set_timeout(lirc_rx51,
156 lirc_rx51->wbuf[lirc_rx51->wbuf_index]);
157 lirc_rx51->wbuf_index++;
159 } while (retval);
161 return IRQ_HANDLED;
162 end:
163 /* Stop TX here */
164 lirc_rx51_off(lirc_rx51);
165 lirc_rx51->wbuf_index = -1;
166 omap_dm_timer_stop(lirc_rx51->pwm_timer);
167 omap_dm_timer_stop(lirc_rx51->pulse_timer);
168 omap_dm_timer_set_int_enable(lirc_rx51->pulse_timer, 0);
169 wake_up_interruptible(&lirc_rx51->wqueue);
171 return IRQ_HANDLED;
174 static int lirc_rx51_init_port(struct lirc_rx51 *lirc_rx51)
176 struct clk *clk_fclk;
177 int retval, pwm_timer = lirc_rx51->pwm_timer_num;
179 lirc_rx51->pwm_timer = omap_dm_timer_request_specific(pwm_timer);
180 if (lirc_rx51->pwm_timer == NULL) {
181 dev_err(lirc_rx51->dev, ": Error requesting GPT%d timer\n",
182 pwm_timer);
183 return -EBUSY;
186 lirc_rx51->pulse_timer = omap_dm_timer_request();
187 if (lirc_rx51->pulse_timer == NULL) {
188 dev_err(lirc_rx51->dev, ": Error requesting pulse timer\n");
189 retval = -EBUSY;
190 goto err1;
193 omap_dm_timer_set_source(lirc_rx51->pwm_timer, OMAP_TIMER_SRC_SYS_CLK);
194 omap_dm_timer_set_source(lirc_rx51->pulse_timer,
195 OMAP_TIMER_SRC_SYS_CLK);
197 omap_dm_timer_enable(lirc_rx51->pwm_timer);
198 omap_dm_timer_enable(lirc_rx51->pulse_timer);
200 lirc_rx51->irq_num = omap_dm_timer_get_irq(lirc_rx51->pulse_timer);
201 retval = request_irq(lirc_rx51->irq_num, lirc_rx51_interrupt_handler,
202 IRQF_DISABLED | IRQF_SHARED,
203 "lirc_pulse_timer", lirc_rx51);
204 if (retval) {
205 dev_err(lirc_rx51->dev, ": Failed to request interrupt line\n");
206 goto err2;
209 clk_fclk = omap_dm_timer_get_fclk(lirc_rx51->pwm_timer);
210 lirc_rx51->fclk_khz = clk_fclk->rate / 1000;
212 return 0;
214 err2:
215 omap_dm_timer_free(lirc_rx51->pulse_timer);
216 err1:
217 omap_dm_timer_free(lirc_rx51->pwm_timer);
219 return retval;
222 static int lirc_rx51_free_port(struct lirc_rx51 *lirc_rx51)
224 omap_dm_timer_set_int_enable(lirc_rx51->pulse_timer, 0);
225 free_irq(lirc_rx51->irq_num, lirc_rx51);
226 lirc_rx51_off(lirc_rx51);
227 omap_dm_timer_disable(lirc_rx51->pwm_timer);
228 omap_dm_timer_disable(lirc_rx51->pulse_timer);
229 omap_dm_timer_free(lirc_rx51->pwm_timer);
230 omap_dm_timer_free(lirc_rx51->pulse_timer);
231 lirc_rx51->wbuf_index = -1;
233 return 0;
236 static ssize_t lirc_rx51_write(struct file *file, const char *buf,
237 size_t n, loff_t *ppos)
239 int count, i;
240 struct lirc_rx51 *lirc_rx51 = file->private_data;
242 if (n % sizeof(int))
243 return -EINVAL;
245 count = n / sizeof(int);
246 if ((count > WBUF_LEN) || (count % 2 == 0))
247 return -EINVAL;
249 /* Wait any pending transfers to finish */
250 wait_event_interruptible(lirc_rx51->wqueue, lirc_rx51->wbuf_index < 0);
252 if (copy_from_user(lirc_rx51->wbuf, buf, n))
253 return -EFAULT;
255 /* Sanity check the input pulses */
256 for (i = 0; i < count; i++)
257 if (lirc_rx51->wbuf[i] < 0)
258 return -EINVAL;
260 init_timing_params(lirc_rx51);
261 if (count < WBUF_LEN)
262 lirc_rx51->wbuf[count] = -1; /* Insert termination mark */
265 * Adjust latency requirements so the device doesn't go in too
266 * deep sleep states
268 lirc_rx51->pdata->set_max_mpu_wakeup_lat(lirc_rx51->dev, 50);
270 lirc_rx51_on(lirc_rx51);
271 lirc_rx51->wbuf_index = 1;
272 pulse_timer_set_timeout(lirc_rx51, lirc_rx51->wbuf[0]);
275 * Don't return back to the userspace until the transfer has
276 * finished
278 wait_event_interruptible(lirc_rx51->wqueue, lirc_rx51->wbuf_index < 0);
280 /* We can sleep again */
281 lirc_rx51->pdata->set_max_mpu_wakeup_lat(lirc_rx51->dev, -1);
283 return n;
286 static long lirc_rx51_ioctl(struct file *filep,
287 unsigned int cmd, unsigned long arg)
289 int result;
290 unsigned long value;
291 unsigned int ivalue;
292 struct lirc_rx51 *lirc_rx51 = filep->private_data;
294 switch (cmd) {
295 case LIRC_GET_SEND_MODE:
296 result = put_user(LIRC_MODE_PULSE, (unsigned long *)arg);
297 if (result)
298 return result;
299 break;
301 case LIRC_SET_SEND_MODE:
302 result = get_user(value, (unsigned long *)arg);
303 if (result)
304 return result;
306 /* only LIRC_MODE_PULSE supported */
307 if (value != LIRC_MODE_PULSE)
308 return -ENOSYS;
309 break;
311 case LIRC_GET_REC_MODE:
312 result = put_user(0, (unsigned long *) arg);
313 if (result)
314 return result;
315 break;
317 case LIRC_GET_LENGTH:
318 return -ENOSYS;
319 break;
321 case LIRC_SET_SEND_DUTY_CYCLE:
322 result = get_user(ivalue, (unsigned int *) arg);
323 if (result)
324 return result;
326 if (ivalue <= 0 || ivalue > 100) {
327 dev_err(lirc_rx51->dev, ": invalid duty cycle %d\n",
328 ivalue);
329 return -EINVAL;
332 lirc_rx51->duty_cycle = ivalue;
333 break;
335 case LIRC_SET_SEND_CARRIER:
336 result = get_user(ivalue, (unsigned int *) arg);
337 if (result)
338 return result;
340 if (ivalue > 500000 || ivalue < 20000) {
341 dev_err(lirc_rx51->dev, ": invalid carrier freq %d\n",
342 ivalue);
343 return -EINVAL;
346 lirc_rx51->freq = ivalue;
347 break;
349 case LIRC_GET_FEATURES:
350 result = put_user(LIRC_RX51_DRIVER_FEATURES,
351 (unsigned long *) arg);
352 if (result)
353 return result;
354 break;
356 default:
357 return -ENOIOCTLCMD;
360 return 0;
363 static int lirc_rx51_open(struct inode *inode, struct file *file)
365 struct lirc_rx51 *lirc_rx51 = lirc_get_pdata(file);
366 BUG_ON(!lirc_rx51);
368 file->private_data = lirc_rx51;
370 if (test_and_set_bit(1, &lirc_rx51->device_is_open))
371 return -EBUSY;
373 return lirc_rx51_init_port(lirc_rx51);
376 static int lirc_rx51_release(struct inode *inode, struct file *file)
378 struct lirc_rx51 *lirc_rx51 = file->private_data;
380 lirc_rx51_free_port(lirc_rx51);
382 clear_bit(1, &lirc_rx51->device_is_open);
384 return 0;
387 static struct lirc_rx51 lirc_rx51 = {
388 .freq = 38000,
389 .duty_cycle = 50,
390 .wbuf_index = -1,
393 static const struct file_operations lirc_fops = {
394 .owner = THIS_MODULE,
395 .write = lirc_rx51_write,
396 .unlocked_ioctl = lirc_rx51_ioctl,
397 .read = lirc_dev_fop_read,
398 .poll = lirc_dev_fop_poll,
399 .open = lirc_rx51_open,
400 .release = lirc_rx51_release,
403 static struct lirc_driver lirc_rx51_driver = {
404 .name = DRIVER_NAME,
405 .minor = -1,
406 .code_length = 1,
407 .data = &lirc_rx51,
408 .fops = &lirc_fops,
409 .owner = THIS_MODULE,
412 #ifdef CONFIG_PM
414 static int lirc_rx51_suspend(struct platform_device *dev, pm_message_t state)
417 * In case the device is still open, do not suspend. Normally
418 * this should not be a problem as lircd only keeps the device
419 * open only for short periods of time. We also don't want to
420 * get involved with race conditions that might happen if we
421 * were in a middle of a transmit. Thus, we defer any suspend
422 * actions until transmit has completed.
424 if (test_and_set_bit(1, &lirc_rx51.device_is_open))
425 return -EAGAIN;
427 clear_bit(1, &lirc_rx51.device_is_open);
429 return 0;
432 static int lirc_rx51_resume(struct platform_device *dev)
434 return 0;
437 #else
439 #define lirc_rx51_suspend NULL
440 #define lirc_rx51_resume NULL
442 #endif /* CONFIG_PM */
444 static int __devinit lirc_rx51_probe(struct platform_device *dev)
446 lirc_rx51_driver.features = LIRC_RX51_DRIVER_FEATURES;
447 lirc_rx51.pdata = dev->dev.platform_data;
448 lirc_rx51.pwm_timer_num = lirc_rx51.pdata->pwm_timer;
449 lirc_rx51.dev = &dev->dev;
450 lirc_rx51_driver.dev = &dev->dev;
451 lirc_rx51_driver.minor = lirc_register_driver(&lirc_rx51_driver);
452 init_waitqueue_head(&lirc_rx51.wqueue);
454 if (lirc_rx51_driver.minor < 0) {
455 dev_err(lirc_rx51.dev, ": lirc_register_driver failed: %d\n",
456 lirc_rx51_driver.minor);
457 return lirc_rx51_driver.minor;
459 dev_info(lirc_rx51.dev, "registration ok, minor: %d, pwm: %d\n",
460 lirc_rx51_driver.minor, lirc_rx51.pwm_timer_num);
462 return 0;
465 static int __exit lirc_rx51_remove(struct platform_device *dev)
467 return lirc_unregister_driver(lirc_rx51_driver.minor);
470 struct platform_driver lirc_rx51_platform_driver = {
471 .probe = lirc_rx51_probe,
472 .remove = __exit_p(lirc_rx51_remove),
473 .suspend = lirc_rx51_suspend,
474 .resume = lirc_rx51_resume,
475 .remove = __exit_p(lirc_rx51_remove),
476 .driver = {
477 .name = DRIVER_NAME,
478 .owner = THIS_MODULE,
482 static int __init lirc_rx51_init(void)
484 return platform_driver_register(&lirc_rx51_platform_driver);
486 module_init(lirc_rx51_init);
488 static void __exit lirc_rx51_exit(void)
490 platform_driver_unregister(&lirc_rx51_platform_driver);
492 module_exit(lirc_rx51_exit);
494 MODULE_DESCRIPTION("LIRC TX driver for Nokia RX51");
495 MODULE_AUTHOR("Nokia Corporation");
496 MODULE_LICENSE("GPL");