Commit | Line | Data |
---|---|---|
c332e847 TK |
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 | * 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 | |
19 | * | |
20 | */ | |
21 | ||
4406d52a | 22 | #include <linux/clk.h> |
c332e847 TK |
23 | #include <linux/module.h> |
24 | #include <linux/interrupt.h> | |
25 | #include <linux/uaccess.h> | |
26 | #include <linux/platform_device.h> | |
27 | #include <linux/sched.h> | |
28 | #include <linux/wait.h> | |
29 | ||
c332e847 TK |
30 | #include <media/lirc.h> |
31 | #include <media/lirc_dev.h> | |
4406d52a | 32 | #include <linux/platform_data/pwm_omap_dmtimer.h> |
eb4b0ec7 | 33 | #include <linux/platform_data/media/ir-rx51.h> |
c332e847 TK |
34 | |
35 | #define LIRC_RX51_DRIVER_FEATURES (LIRC_CAN_SET_SEND_DUTY_CYCLE | \ | |
36 | LIRC_CAN_SET_SEND_CARRIER | \ | |
37 | LIRC_CAN_SEND_PULSE) | |
38 | ||
39 | #define DRIVER_NAME "lirc_rx51" | |
40 | ||
41 | #define WBUF_LEN 256 | |
42 | ||
43 | #define TIMER_MAX_VALUE 0xffffffff | |
44 | ||
45 | struct lirc_rx51 { | |
4406d52a ID |
46 | pwm_omap_dmtimer *pwm_timer; |
47 | pwm_omap_dmtimer *pulse_timer; | |
48 | struct pwm_omap_dmtimer_pdata *dmtimer; | |
c332e847 TK |
49 | struct device *dev; |
50 | struct lirc_rx51_platform_data *pdata; | |
51 | wait_queue_head_t wqueue; | |
52 | ||
53 | unsigned long fclk_khz; | |
54 | unsigned int freq; /* carrier frequency */ | |
55 | unsigned int duty_cycle; /* carrier duty cycle */ | |
56 | unsigned int irq_num; | |
57 | unsigned int match; | |
58 | int wbuf[WBUF_LEN]; | |
59 | int wbuf_index; | |
60 | unsigned long device_is_open; | |
362b29ba | 61 | int pwm_timer_num; |
c332e847 TK |
62 | }; |
63 | ||
64 | static void lirc_rx51_on(struct lirc_rx51 *lirc_rx51) | |
65 | { | |
4406d52a ID |
66 | lirc_rx51->dmtimer->set_pwm(lirc_rx51->pwm_timer, 0, 1, |
67 | PWM_OMAP_DMTIMER_TRIGGER_OVERFLOW_AND_COMPARE); | |
c332e847 TK |
68 | } |
69 | ||
70 | static void lirc_rx51_off(struct lirc_rx51 *lirc_rx51) | |
71 | { | |
4406d52a ID |
72 | lirc_rx51->dmtimer->set_pwm(lirc_rx51->pwm_timer, 0, 1, |
73 | PWM_OMAP_DMTIMER_TRIGGER_NONE); | |
c332e847 TK |
74 | } |
75 | ||
76 | static int init_timing_params(struct lirc_rx51 *lirc_rx51) | |
77 | { | |
78 | u32 load, match; | |
79 | ||
80 | load = -(lirc_rx51->fclk_khz * 1000 / lirc_rx51->freq); | |
81 | match = -(lirc_rx51->duty_cycle * -load / 100); | |
4406d52a ID |
82 | lirc_rx51->dmtimer->set_load(lirc_rx51->pwm_timer, 1, load); |
83 | lirc_rx51->dmtimer->set_match(lirc_rx51->pwm_timer, 1, match); | |
84 | lirc_rx51->dmtimer->write_counter(lirc_rx51->pwm_timer, TIMER_MAX_VALUE - 2); | |
85 | lirc_rx51->dmtimer->start(lirc_rx51->pwm_timer); | |
86 | lirc_rx51->dmtimer->set_int_enable(lirc_rx51->pulse_timer, 0); | |
87 | lirc_rx51->dmtimer->start(lirc_rx51->pulse_timer); | |
c332e847 TK |
88 | |
89 | lirc_rx51->match = 0; | |
90 | ||
91 | return 0; | |
92 | } | |
93 | ||
94 | #define tics_after(a, b) ((long)(b) - (long)(a) < 0) | |
95 | ||
96 | static int pulse_timer_set_timeout(struct lirc_rx51 *lirc_rx51, int usec) | |
97 | { | |
98 | int counter; | |
99 | ||
100 | BUG_ON(usec < 0); | |
101 | ||
102 | if (lirc_rx51->match == 0) | |
4406d52a | 103 | counter = lirc_rx51->dmtimer->read_counter(lirc_rx51->pulse_timer); |
c332e847 TK |
104 | else |
105 | counter = lirc_rx51->match; | |
106 | ||
107 | counter += (u32)(lirc_rx51->fclk_khz * usec / (1000)); | |
4406d52a ID |
108 | lirc_rx51->dmtimer->set_match(lirc_rx51->pulse_timer, 1, counter); |
109 | lirc_rx51->dmtimer->set_int_enable(lirc_rx51->pulse_timer, | |
110 | PWM_OMAP_DMTIMER_INT_MATCH); | |
111 | if (tics_after(lirc_rx51->dmtimer->read_counter(lirc_rx51->pulse_timer), | |
c332e847 TK |
112 | counter)) { |
113 | return 1; | |
114 | } | |
115 | return 0; | |
116 | } | |
117 | ||
118 | static irqreturn_t lirc_rx51_interrupt_handler(int irq, void *ptr) | |
119 | { | |
120 | unsigned int retval; | |
121 | struct lirc_rx51 *lirc_rx51 = ptr; | |
122 | ||
4406d52a | 123 | retval = lirc_rx51->dmtimer->read_status(lirc_rx51->pulse_timer); |
c332e847 TK |
124 | if (!retval) |
125 | return IRQ_NONE; | |
126 | ||
4406d52a | 127 | if (retval & ~PWM_OMAP_DMTIMER_INT_MATCH) |
c332e847 TK |
128 | dev_err_ratelimited(lirc_rx51->dev, |
129 | ": Unexpected interrupt source: %x\n", retval); | |
130 | ||
4406d52a ID |
131 | lirc_rx51->dmtimer->write_status(lirc_rx51->pulse_timer, |
132 | PWM_OMAP_DMTIMER_INT_MATCH | | |
133 | PWM_OMAP_DMTIMER_INT_OVERFLOW | | |
134 | PWM_OMAP_DMTIMER_INT_CAPTURE); | |
c332e847 TK |
135 | if (lirc_rx51->wbuf_index < 0) { |
136 | dev_err_ratelimited(lirc_rx51->dev, | |
137 | ": BUG wbuf_index has value of %i\n", | |
138 | lirc_rx51->wbuf_index); | |
139 | goto end; | |
140 | } | |
141 | ||
142 | /* | |
143 | * If we happen to hit an odd latency spike, loop through the | |
144 | * pulses until we catch up. | |
145 | */ | |
146 | do { | |
147 | if (lirc_rx51->wbuf_index >= WBUF_LEN) | |
148 | goto end; | |
149 | if (lirc_rx51->wbuf[lirc_rx51->wbuf_index] == -1) | |
150 | goto end; | |
151 | ||
152 | if (lirc_rx51->wbuf_index % 2) | |
153 | lirc_rx51_off(lirc_rx51); | |
154 | else | |
155 | lirc_rx51_on(lirc_rx51); | |
156 | ||
157 | retval = pulse_timer_set_timeout(lirc_rx51, | |
158 | lirc_rx51->wbuf[lirc_rx51->wbuf_index]); | |
159 | lirc_rx51->wbuf_index++; | |
160 | ||
161 | } while (retval); | |
162 | ||
163 | return IRQ_HANDLED; | |
164 | end: | |
165 | /* Stop TX here */ | |
166 | lirc_rx51_off(lirc_rx51); | |
167 | lirc_rx51->wbuf_index = -1; | |
4406d52a ID |
168 | lirc_rx51->dmtimer->stop(lirc_rx51->pwm_timer); |
169 | lirc_rx51->dmtimer->stop(lirc_rx51->pulse_timer); | |
170 | lirc_rx51->dmtimer->set_int_enable(lirc_rx51->pulse_timer, 0); | |
c332e847 TK |
171 | wake_up_interruptible(&lirc_rx51->wqueue); |
172 | ||
173 | return IRQ_HANDLED; | |
174 | } | |
175 | ||
176 | static int lirc_rx51_init_port(struct lirc_rx51 *lirc_rx51) | |
177 | { | |
178 | struct clk *clk_fclk; | |
179 | int retval, pwm_timer = lirc_rx51->pwm_timer_num; | |
180 | ||
4406d52a | 181 | lirc_rx51->pwm_timer = lirc_rx51->dmtimer->request_specific(pwm_timer); |
c332e847 TK |
182 | if (lirc_rx51->pwm_timer == NULL) { |
183 | dev_err(lirc_rx51->dev, ": Error requesting GPT%d timer\n", | |
184 | pwm_timer); | |
185 | return -EBUSY; | |
186 | } | |
187 | ||
4406d52a | 188 | lirc_rx51->pulse_timer = lirc_rx51->dmtimer->request(); |
c332e847 TK |
189 | if (lirc_rx51->pulse_timer == NULL) { |
190 | dev_err(lirc_rx51->dev, ": Error requesting pulse timer\n"); | |
191 | retval = -EBUSY; | |
192 | goto err1; | |
193 | } | |
194 | ||
4406d52a ID |
195 | lirc_rx51->dmtimer->set_source(lirc_rx51->pwm_timer, |
196 | PWM_OMAP_DMTIMER_SRC_SYS_CLK); | |
197 | lirc_rx51->dmtimer->set_source(lirc_rx51->pulse_timer, | |
198 | PWM_OMAP_DMTIMER_SRC_SYS_CLK); | |
c332e847 | 199 | |
4406d52a ID |
200 | lirc_rx51->dmtimer->enable(lirc_rx51->pwm_timer); |
201 | lirc_rx51->dmtimer->enable(lirc_rx51->pulse_timer); | |
c332e847 | 202 | |
4406d52a | 203 | lirc_rx51->irq_num = lirc_rx51->dmtimer->get_irq(lirc_rx51->pulse_timer); |
c332e847 | 204 | retval = request_irq(lirc_rx51->irq_num, lirc_rx51_interrupt_handler, |
a232728d | 205 | IRQF_SHARED, "lirc_pulse_timer", lirc_rx51); |
c332e847 TK |
206 | if (retval) { |
207 | dev_err(lirc_rx51->dev, ": Failed to request interrupt line\n"); | |
208 | goto err2; | |
209 | } | |
210 | ||
4406d52a ID |
211 | clk_fclk = lirc_rx51->dmtimer->get_fclk(lirc_rx51->pwm_timer); |
212 | lirc_rx51->fclk_khz = clk_get_rate(clk_fclk) / 1000; | |
c332e847 TK |
213 | |
214 | return 0; | |
215 | ||
216 | err2: | |
4406d52a | 217 | lirc_rx51->dmtimer->free(lirc_rx51->pulse_timer); |
c332e847 | 218 | err1: |
4406d52a | 219 | lirc_rx51->dmtimer->free(lirc_rx51->pwm_timer); |
c332e847 TK |
220 | |
221 | return retval; | |
222 | } | |
223 | ||
224 | static int lirc_rx51_free_port(struct lirc_rx51 *lirc_rx51) | |
225 | { | |
4406d52a | 226 | lirc_rx51->dmtimer->set_int_enable(lirc_rx51->pulse_timer, 0); |
c332e847 TK |
227 | free_irq(lirc_rx51->irq_num, lirc_rx51); |
228 | lirc_rx51_off(lirc_rx51); | |
4406d52a ID |
229 | lirc_rx51->dmtimer->disable(lirc_rx51->pwm_timer); |
230 | lirc_rx51->dmtimer->disable(lirc_rx51->pulse_timer); | |
231 | lirc_rx51->dmtimer->free(lirc_rx51->pwm_timer); | |
232 | lirc_rx51->dmtimer->free(lirc_rx51->pulse_timer); | |
c332e847 TK |
233 | lirc_rx51->wbuf_index = -1; |
234 | ||
235 | return 0; | |
236 | } | |
237 | ||
238 | static ssize_t lirc_rx51_write(struct file *file, const char *buf, | |
239 | size_t n, loff_t *ppos) | |
240 | { | |
241 | int count, i; | |
242 | struct lirc_rx51 *lirc_rx51 = file->private_data; | |
243 | ||
244 | if (n % sizeof(int)) | |
245 | return -EINVAL; | |
246 | ||
247 | count = n / sizeof(int); | |
248 | if ((count > WBUF_LEN) || (count % 2 == 0)) | |
249 | return -EINVAL; | |
250 | ||
251 | /* Wait any pending transfers to finish */ | |
252 | wait_event_interruptible(lirc_rx51->wqueue, lirc_rx51->wbuf_index < 0); | |
253 | ||
254 | if (copy_from_user(lirc_rx51->wbuf, buf, n)) | |
255 | return -EFAULT; | |
256 | ||
257 | /* Sanity check the input pulses */ | |
258 | for (i = 0; i < count; i++) | |
259 | if (lirc_rx51->wbuf[i] < 0) | |
260 | return -EINVAL; | |
261 | ||
262 | init_timing_params(lirc_rx51); | |
263 | if (count < WBUF_LEN) | |
264 | lirc_rx51->wbuf[count] = -1; /* Insert termination mark */ | |
265 | ||
266 | /* | |
267 | * Adjust latency requirements so the device doesn't go in too | |
268 | * deep sleep states | |
269 | */ | |
270 | lirc_rx51->pdata->set_max_mpu_wakeup_lat(lirc_rx51->dev, 50); | |
271 | ||
272 | lirc_rx51_on(lirc_rx51); | |
273 | lirc_rx51->wbuf_index = 1; | |
274 | pulse_timer_set_timeout(lirc_rx51, lirc_rx51->wbuf[0]); | |
275 | ||
276 | /* | |
277 | * Don't return back to the userspace until the transfer has | |
278 | * finished | |
279 | */ | |
280 | wait_event_interruptible(lirc_rx51->wqueue, lirc_rx51->wbuf_index < 0); | |
281 | ||
282 | /* We can sleep again */ | |
283 | lirc_rx51->pdata->set_max_mpu_wakeup_lat(lirc_rx51->dev, -1); | |
284 | ||
285 | return n; | |
286 | } | |
287 | ||
288 | static long lirc_rx51_ioctl(struct file *filep, | |
289 | unsigned int cmd, unsigned long arg) | |
290 | { | |
291 | int result; | |
292 | unsigned long value; | |
293 | unsigned int ivalue; | |
294 | struct lirc_rx51 *lirc_rx51 = filep->private_data; | |
295 | ||
296 | switch (cmd) { | |
297 | case LIRC_GET_SEND_MODE: | |
298 | result = put_user(LIRC_MODE_PULSE, (unsigned long *)arg); | |
299 | if (result) | |
300 | return result; | |
301 | break; | |
302 | ||
303 | case LIRC_SET_SEND_MODE: | |
304 | result = get_user(value, (unsigned long *)arg); | |
305 | if (result) | |
306 | return result; | |
307 | ||
308 | /* only LIRC_MODE_PULSE supported */ | |
309 | if (value != LIRC_MODE_PULSE) | |
310 | return -ENOSYS; | |
311 | break; | |
312 | ||
313 | case LIRC_GET_REC_MODE: | |
314 | result = put_user(0, (unsigned long *) arg); | |
315 | if (result) | |
316 | return result; | |
317 | break; | |
318 | ||
319 | case LIRC_GET_LENGTH: | |
320 | return -ENOSYS; | |
321 | break; | |
322 | ||
323 | case LIRC_SET_SEND_DUTY_CYCLE: | |
324 | result = get_user(ivalue, (unsigned int *) arg); | |
325 | if (result) | |
326 | return result; | |
327 | ||
328 | if (ivalue <= 0 || ivalue > 100) { | |
329 | dev_err(lirc_rx51->dev, ": invalid duty cycle %d\n", | |
330 | ivalue); | |
331 | return -EINVAL; | |
332 | } | |
333 | ||
334 | lirc_rx51->duty_cycle = ivalue; | |
335 | break; | |
336 | ||
337 | case LIRC_SET_SEND_CARRIER: | |
338 | result = get_user(ivalue, (unsigned int *) arg); | |
339 | if (result) | |
340 | return result; | |
341 | ||
342 | if (ivalue > 500000 || ivalue < 20000) { | |
343 | dev_err(lirc_rx51->dev, ": invalid carrier freq %d\n", | |
344 | ivalue); | |
345 | return -EINVAL; | |
346 | } | |
347 | ||
348 | lirc_rx51->freq = ivalue; | |
349 | break; | |
350 | ||
351 | case LIRC_GET_FEATURES: | |
352 | result = put_user(LIRC_RX51_DRIVER_FEATURES, | |
353 | (unsigned long *) arg); | |
354 | if (result) | |
355 | return result; | |
356 | break; | |
357 | ||
358 | default: | |
359 | return -ENOIOCTLCMD; | |
360 | } | |
361 | ||
362 | return 0; | |
363 | } | |
364 | ||
365 | static int lirc_rx51_open(struct inode *inode, struct file *file) | |
366 | { | |
367 | struct lirc_rx51 *lirc_rx51 = lirc_get_pdata(file); | |
368 | BUG_ON(!lirc_rx51); | |
369 | ||
370 | file->private_data = lirc_rx51; | |
371 | ||
372 | if (test_and_set_bit(1, &lirc_rx51->device_is_open)) | |
373 | return -EBUSY; | |
374 | ||
375 | return lirc_rx51_init_port(lirc_rx51); | |
376 | } | |
377 | ||
378 | static int lirc_rx51_release(struct inode *inode, struct file *file) | |
379 | { | |
380 | struct lirc_rx51 *lirc_rx51 = file->private_data; | |
381 | ||
382 | lirc_rx51_free_port(lirc_rx51); | |
383 | ||
384 | clear_bit(1, &lirc_rx51->device_is_open); | |
385 | ||
386 | return 0; | |
387 | } | |
388 | ||
389 | static struct lirc_rx51 lirc_rx51 = { | |
390 | .freq = 38000, | |
391 | .duty_cycle = 50, | |
392 | .wbuf_index = -1, | |
393 | }; | |
394 | ||
395 | static const struct file_operations lirc_fops = { | |
396 | .owner = THIS_MODULE, | |
397 | .write = lirc_rx51_write, | |
398 | .unlocked_ioctl = lirc_rx51_ioctl, | |
399 | .read = lirc_dev_fop_read, | |
400 | .poll = lirc_dev_fop_poll, | |
401 | .open = lirc_rx51_open, | |
402 | .release = lirc_rx51_release, | |
403 | }; | |
404 | ||
405 | static struct lirc_driver lirc_rx51_driver = { | |
406 | .name = DRIVER_NAME, | |
407 | .minor = -1, | |
408 | .code_length = 1, | |
409 | .data = &lirc_rx51, | |
410 | .fops = &lirc_fops, | |
411 | .owner = THIS_MODULE, | |
412 | }; | |
413 | ||
414 | #ifdef CONFIG_PM | |
415 | ||
416 | static int lirc_rx51_suspend(struct platform_device *dev, pm_message_t state) | |
417 | { | |
418 | /* | |
419 | * In case the device is still open, do not suspend. Normally | |
420 | * this should not be a problem as lircd only keeps the device | |
421 | * open only for short periods of time. We also don't want to | |
422 | * get involved with race conditions that might happen if we | |
423 | * were in a middle of a transmit. Thus, we defer any suspend | |
424 | * actions until transmit has completed. | |
425 | */ | |
426 | if (test_and_set_bit(1, &lirc_rx51.device_is_open)) | |
427 | return -EAGAIN; | |
428 | ||
429 | clear_bit(1, &lirc_rx51.device_is_open); | |
430 | ||
431 | return 0; | |
432 | } | |
433 | ||
434 | static int lirc_rx51_resume(struct platform_device *dev) | |
435 | { | |
436 | return 0; | |
437 | } | |
438 | ||
439 | #else | |
440 | ||
441 | #define lirc_rx51_suspend NULL | |
442 | #define lirc_rx51_resume NULL | |
443 | ||
444 | #endif /* CONFIG_PM */ | |
445 | ||
4c62e976 | 446 | static int lirc_rx51_probe(struct platform_device *dev) |
c332e847 TK |
447 | { |
448 | lirc_rx51_driver.features = LIRC_RX51_DRIVER_FEATURES; | |
449 | lirc_rx51.pdata = dev->dev.platform_data; | |
4406d52a ID |
450 | if (!lirc_rx51.pdata->dmtimer) { |
451 | dev_err(&dev->dev, "no dmtimer?\n"); | |
452 | return -ENODEV; | |
453 | } | |
454 | ||
c332e847 | 455 | lirc_rx51.pwm_timer_num = lirc_rx51.pdata->pwm_timer; |
4406d52a | 456 | lirc_rx51.dmtimer = lirc_rx51.pdata->dmtimer; |
c332e847 TK |
457 | lirc_rx51.dev = &dev->dev; |
458 | lirc_rx51_driver.dev = &dev->dev; | |
459 | lirc_rx51_driver.minor = lirc_register_driver(&lirc_rx51_driver); | |
460 | init_waitqueue_head(&lirc_rx51.wqueue); | |
461 | ||
462 | if (lirc_rx51_driver.minor < 0) { | |
463 | dev_err(lirc_rx51.dev, ": lirc_register_driver failed: %d\n", | |
464 | lirc_rx51_driver.minor); | |
465 | return lirc_rx51_driver.minor; | |
466 | } | |
467 | dev_info(lirc_rx51.dev, "registration ok, minor: %d, pwm: %d\n", | |
468 | lirc_rx51_driver.minor, lirc_rx51.pwm_timer_num); | |
469 | ||
470 | return 0; | |
471 | } | |
472 | ||
bf306900 | 473 | static int lirc_rx51_remove(struct platform_device *dev) |
c332e847 TK |
474 | { |
475 | return lirc_unregister_driver(lirc_rx51_driver.minor); | |
476 | } | |
477 | ||
478 | struct platform_driver lirc_rx51_platform_driver = { | |
479 | .probe = lirc_rx51_probe, | |
bf306900 | 480 | .remove = lirc_rx51_remove, |
c332e847 TK |
481 | .suspend = lirc_rx51_suspend, |
482 | .resume = lirc_rx51_resume, | |
c332e847 TK |
483 | .driver = { |
484 | .name = DRIVER_NAME, | |
485 | .owner = THIS_MODULE, | |
486 | }, | |
487 | }; | |
304ce75d | 488 | module_platform_driver(lirc_rx51_platform_driver); |
c332e847 TK |
489 | |
490 | MODULE_DESCRIPTION("LIRC TX driver for Nokia RX51"); | |
491 | MODULE_AUTHOR("Nokia Corporation"); | |
492 | MODULE_LICENSE("GPL"); |