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