ir-rx51: use PWM framework instead of OMAP dmtimer
[deliverable/linux.git] / drivers / media / rc / ir-rx51.c
1 /*
2 * Copyright (C) 2008 Nokia Corporation
3 *
4 * Based on lirc_serial.c
5 *
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.
10 *
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.
15 */
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>
24
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>
29
30 #define LIRC_RX51_DRIVER_FEATURES (LIRC_CAN_SET_SEND_DUTY_CYCLE | \
31 LIRC_CAN_SET_SEND_CARRIER | \
32 LIRC_CAN_SEND_PULSE)
33
34 #define DRIVER_NAME "lirc_rx51"
35
36 #define WBUF_LEN 256
37
38 #define TIMER_MAX_VALUE 0xffffffff
39
40 struct lirc_rx51 {
41 struct pwm_device *pwm;
42 pwm_omap_dmtimer *pulse_timer;
43 struct pwm_omap_dmtimer_pdata *dmtimer;
44 struct device *dev;
45 struct lirc_rx51_platform_data *pdata;
46 wait_queue_head_t wqueue;
47
48 unsigned long fclk_khz;
49 unsigned int freq; /* carrier frequency */
50 unsigned int duty_cycle; /* carrier duty cycle */
51 unsigned int irq_num;
52 unsigned int match;
53 int wbuf[WBUF_LEN];
54 int wbuf_index;
55 unsigned long device_is_open;
56 };
57
58 static void lirc_rx51_on(struct lirc_rx51 *lirc_rx51)
59 {
60 pwm_enable(lirc_rx51->pwm);
61 }
62
63 static void lirc_rx51_off(struct lirc_rx51 *lirc_rx51)
64 {
65 pwm_disable(lirc_rx51->pwm);
66 }
67
68 static int init_timing_params(struct lirc_rx51 *lirc_rx51)
69 {
70 struct pwm_device *pwm = lirc_rx51->pwm;
71 int duty, period = DIV_ROUND_CLOSEST(NSEC_PER_SEC, lirc_rx51->freq);
72
73 duty = DIV_ROUND_CLOSEST(lirc_rx51->duty_cycle * period, 100);
74 lirc_rx51->dmtimer->set_int_enable(lirc_rx51->pulse_timer, 0);
75
76 pwm_config(pwm, duty, period);
77
78 lirc_rx51->dmtimer->start(lirc_rx51->pulse_timer);
79
80 lirc_rx51->match = 0;
81
82 return 0;
83 }
84
85 #define tics_after(a, b) ((long)(b) - (long)(a) < 0)
86
87 static int pulse_timer_set_timeout(struct lirc_rx51 *lirc_rx51, int usec)
88 {
89 int counter;
90
91 BUG_ON(usec < 0);
92
93 if (lirc_rx51->match == 0)
94 counter = lirc_rx51->dmtimer->read_counter(lirc_rx51->pulse_timer);
95 else
96 counter = lirc_rx51->match;
97
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),
103 counter)) {
104 return 1;
105 }
106 return 0;
107 }
108
109 static irqreturn_t lirc_rx51_interrupt_handler(int irq, void *ptr)
110 {
111 unsigned int retval;
112 struct lirc_rx51 *lirc_rx51 = ptr;
113
114 retval = lirc_rx51->dmtimer->read_status(lirc_rx51->pulse_timer);
115 if (!retval)
116 return IRQ_NONE;
117
118 if (retval & ~PWM_OMAP_DMTIMER_INT_MATCH)
119 dev_err_ratelimited(lirc_rx51->dev,
120 ": Unexpected interrupt source: %x\n", retval);
121
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);
130 goto end;
131 }
132
133 /*
134 * If we happen to hit an odd latency spike, loop through the
135 * pulses until we catch up.
136 */
137 do {
138 if (lirc_rx51->wbuf_index >= WBUF_LEN)
139 goto end;
140 if (lirc_rx51->wbuf[lirc_rx51->wbuf_index] == -1)
141 goto end;
142
143 if (lirc_rx51->wbuf_index % 2)
144 lirc_rx51_off(lirc_rx51);
145 else
146 lirc_rx51_on(lirc_rx51);
147
148 retval = pulse_timer_set_timeout(lirc_rx51,
149 lirc_rx51->wbuf[lirc_rx51->wbuf_index]);
150 lirc_rx51->wbuf_index++;
151
152 } while (retval);
153
154 return IRQ_HANDLED;
155 end:
156 /* Stop TX here */
157 lirc_rx51_off(lirc_rx51);
158 lirc_rx51->wbuf_index = -1;
159
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);
163
164 return IRQ_HANDLED;
165 }
166
167 static int lirc_rx51_init_port(struct lirc_rx51 *lirc_rx51)
168 {
169 struct clk *clk_fclk;
170 int retval;
171
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);
176 return retval;
177 }
178
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");
182 retval = -EBUSY;
183 goto err1;
184 }
185
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);
189 lirc_rx51->irq_num =
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);
193 if (retval) {
194 dev_err(lirc_rx51->dev, ": Failed to request interrupt line\n");
195 goto err2;
196 }
197
198 clk_fclk = lirc_rx51->dmtimer->get_fclk(lirc_rx51->pulse_timer);
199 lirc_rx51->fclk_khz = clk_get_rate(clk_fclk) / 1000;
200
201 return 0;
202
203 err2:
204 lirc_rx51->dmtimer->free(lirc_rx51->pulse_timer);
205 err1:
206 pwm_put(lirc_rx51->pwm);
207
208 return retval;
209 }
210
211 static int lirc_rx51_free_port(struct lirc_rx51 *lirc_rx51)
212 {
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);
220
221 return 0;
222 }
223
224 static ssize_t lirc_rx51_write(struct file *file, const char *buf,
225 size_t n, loff_t *ppos)
226 {
227 int count, i;
228 struct lirc_rx51 *lirc_rx51 = file->private_data;
229
230 if (n % sizeof(int))
231 return -EINVAL;
232
233 count = n / sizeof(int);
234 if ((count > WBUF_LEN) || (count % 2 == 0))
235 return -EINVAL;
236
237 /* Wait any pending transfers to finish */
238 wait_event_interruptible(lirc_rx51->wqueue, lirc_rx51->wbuf_index < 0);
239
240 if (copy_from_user(lirc_rx51->wbuf, buf, n))
241 return -EFAULT;
242
243 /* Sanity check the input pulses */
244 for (i = 0; i < count; i++)
245 if (lirc_rx51->wbuf[i] < 0)
246 return -EINVAL;
247
248 init_timing_params(lirc_rx51);
249 if (count < WBUF_LEN)
250 lirc_rx51->wbuf[count] = -1; /* Insert termination mark */
251
252 /*
253 * Adjust latency requirements so the device doesn't go in too
254 * deep sleep states
255 */
256 lirc_rx51->pdata->set_max_mpu_wakeup_lat(lirc_rx51->dev, 50);
257
258 lirc_rx51_on(lirc_rx51);
259 lirc_rx51->wbuf_index = 1;
260 pulse_timer_set_timeout(lirc_rx51, lirc_rx51->wbuf[0]);
261
262 /*
263 * Don't return back to the userspace until the transfer has
264 * finished
265 */
266 wait_event_interruptible(lirc_rx51->wqueue, lirc_rx51->wbuf_index < 0);
267
268 /* We can sleep again */
269 lirc_rx51->pdata->set_max_mpu_wakeup_lat(lirc_rx51->dev, -1);
270
271 return n;
272 }
273
274 static long lirc_rx51_ioctl(struct file *filep,
275 unsigned int cmd, unsigned long arg)
276 {
277 int result;
278 unsigned long value;
279 unsigned int ivalue;
280 struct lirc_rx51 *lirc_rx51 = filep->private_data;
281
282 switch (cmd) {
283 case LIRC_GET_SEND_MODE:
284 result = put_user(LIRC_MODE_PULSE, (unsigned long *)arg);
285 if (result)
286 return result;
287 break;
288
289 case LIRC_SET_SEND_MODE:
290 result = get_user(value, (unsigned long *)arg);
291 if (result)
292 return result;
293
294 /* only LIRC_MODE_PULSE supported */
295 if (value != LIRC_MODE_PULSE)
296 return -ENOSYS;
297 break;
298
299 case LIRC_GET_REC_MODE:
300 result = put_user(0, (unsigned long *) arg);
301 if (result)
302 return result;
303 break;
304
305 case LIRC_GET_LENGTH:
306 return -ENOSYS;
307 break;
308
309 case LIRC_SET_SEND_DUTY_CYCLE:
310 result = get_user(ivalue, (unsigned int *) arg);
311 if (result)
312 return result;
313
314 if (ivalue <= 0 || ivalue > 100) {
315 dev_err(lirc_rx51->dev, ": invalid duty cycle %d\n",
316 ivalue);
317 return -EINVAL;
318 }
319
320 lirc_rx51->duty_cycle = ivalue;
321 break;
322
323 case LIRC_SET_SEND_CARRIER:
324 result = get_user(ivalue, (unsigned int *) arg);
325 if (result)
326 return result;
327
328 if (ivalue > 500000 || ivalue < 20000) {
329 dev_err(lirc_rx51->dev, ": invalid carrier freq %d\n",
330 ivalue);
331 return -EINVAL;
332 }
333
334 lirc_rx51->freq = ivalue;
335 break;
336
337 case LIRC_GET_FEATURES:
338 result = put_user(LIRC_RX51_DRIVER_FEATURES,
339 (unsigned long *) arg);
340 if (result)
341 return result;
342 break;
343
344 default:
345 return -ENOIOCTLCMD;
346 }
347
348 return 0;
349 }
350
351 static int lirc_rx51_open(struct inode *inode, struct file *file)
352 {
353 struct lirc_rx51 *lirc_rx51 = lirc_get_pdata(file);
354 BUG_ON(!lirc_rx51);
355
356 file->private_data = lirc_rx51;
357
358 if (test_and_set_bit(1, &lirc_rx51->device_is_open))
359 return -EBUSY;
360
361 return lirc_rx51_init_port(lirc_rx51);
362 }
363
364 static int lirc_rx51_release(struct inode *inode, struct file *file)
365 {
366 struct lirc_rx51 *lirc_rx51 = file->private_data;
367
368 lirc_rx51_free_port(lirc_rx51);
369
370 clear_bit(1, &lirc_rx51->device_is_open);
371
372 return 0;
373 }
374
375 static struct lirc_rx51 lirc_rx51 = {
376 .duty_cycle = 50,
377 .wbuf_index = -1,
378 };
379
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,
388 };
389
390 static struct lirc_driver lirc_rx51_driver = {
391 .name = DRIVER_NAME,
392 .minor = -1,
393 .code_length = 1,
394 .data = &lirc_rx51,
395 .fops = &lirc_fops,
396 .owner = THIS_MODULE,
397 };
398
399 #ifdef CONFIG_PM
400
401 static int lirc_rx51_suspend(struct platform_device *dev, pm_message_t state)
402 {
403 /*
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.
410 */
411 if (test_and_set_bit(1, &lirc_rx51.device_is_open))
412 return -EAGAIN;
413
414 clear_bit(1, &lirc_rx51.device_is_open);
415
416 return 0;
417 }
418
419 static int lirc_rx51_resume(struct platform_device *dev)
420 {
421 return 0;
422 }
423
424 #else
425
426 #define lirc_rx51_suspend NULL
427 #define lirc_rx51_resume NULL
428
429 #endif /* CONFIG_PM */
430
431 static int lirc_rx51_probe(struct platform_device *dev)
432 {
433 struct pwm_device *pwm;
434
435 lirc_rx51_driver.features = LIRC_RX51_DRIVER_FEATURES;
436 lirc_rx51.pdata = dev->dev.platform_data;
437
438 if (!lirc_rx51.pdata) {
439 dev_err(&dev->dev, "Platform Data is missing\n");
440 return -ENXIO;
441 }
442
443 if (!lirc_rx51.pdata->dmtimer) {
444 dev_err(&dev->dev, "no dmtimer?\n");
445 return -ENODEV;
446 }
447
448 pwm = pwm_get(&dev->dev, NULL);
449 if (IS_ERR(pwm)) {
450 int err = PTR_ERR(pwm);
451
452 if (err != -EPROBE_DEFER)
453 dev_err(&dev->dev, "pwm_get failed: %d\n", err);
454 return err;
455 }
456
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);
459 pwm_put(pwm);
460
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);
466
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;
471 }
472
473 return 0;
474 }
475
476 static int lirc_rx51_remove(struct platform_device *dev)
477 {
478 return lirc_unregister_driver(lirc_rx51_driver.minor);
479 }
480
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,
486 .driver = {
487 .name = DRIVER_NAME,
488 .owner = THIS_MODULE,
489 },
490 };
491 module_platform_driver(lirc_rx51_platform_driver);
492
493 MODULE_DESCRIPTION("LIRC TX driver for Nokia RX51");
494 MODULE_AUTHOR("Nokia Corporation");
495 MODULE_LICENSE("GPL");
This page took 0.062402 seconds and 6 git commands to generate.