brcm2708: add linux 4.1 support
[openwrt.git] / target / linux / brcm2708 / patches-4.1 / 0016-lirc-added-support-for-RaspberryPi-GPIO.patch
1 From e074d656e4011eae32577e4d20d7ce6dfa15f6e8 Mon Sep 17 00:00:00 2001
2 From: Aron Szabo <aron@aron.ws>
3 Date: Sat, 16 Jun 2012 12:15:55 +0200
4 Subject: [PATCH 016/121] lirc: added support for RaspberryPi GPIO
5
6 lirc_rpi: Use read_current_timer to determine transmitter delay. Thanks to jjmz and others
7 See: https://github.com/raspberrypi/linux/issues/525
8
9 lirc: Remove restriction on gpio pins that can be used with lirc
10
11 Compute Module, for example could use different pins
12
13 lirc_rpi: Add parameter to specify input pin pull
14
15 Depending on the connected IR circuitry it might be desirable to change the
16 gpios internal pull from it pull-down default behaviour. Add a module
17 parameter to allow the user to set it explicitly.
18
19 Signed-off-by: Julian Scheel <julian@jusst.de>
20
21 lirc-rpi: Use the higher-level irq control functions
22
23 This module used to access the irq_chip methods of the
24 gpio controller directly, rather than going through the
25 standard enable_irq/irq_set_irq_type functions. This
26 caused problems on pinctrl-bcm2835 which only implements
27 the irq_enable/disable methods and not irq_unmask/mask.
28
29 lirc-rpi: Correct the interrupt usage
30
31 1) Correct the use of enable_irq (i.e. don't call it so often)
32 2) Correct the shutdown sequence.
33 3) Avoid a bcm2708_gpio driver quirk by setting the irq flags earlier
34
35 lirc-rpi: use getnstimeofday instead of read_current_timer
36
37 read_current_timer isn't guaranteed to return values in
38 microseconds, and indeed it doesn't on a Pi2.
39
40 Issue: linux#827
41
42 lirc-rpi: Add device tree support, and a suitable overlay
43
44 The overlay supports DT parameters that match the old module
45 parameters, except that gpio_in_pull should be set using the
46 strings "up", "down" or "off".
47
48 lirc-rpi: Also support pinctrl-bcm2835 in non-DT mode
49 ---
50  drivers/staging/media/lirc/Kconfig    |   6 +
51  drivers/staging/media/lirc/Makefile   |   1 +
52  drivers/staging/media/lirc/lirc_rpi.c | 765 ++++++++++++++++++++++++++++++++++
53  3 files changed, 772 insertions(+)
54  create mode 100644 drivers/staging/media/lirc/lirc_rpi.c
55
56 --- a/drivers/staging/media/lirc/Kconfig
57 +++ b/drivers/staging/media/lirc/Kconfig
58 @@ -32,6 +32,12 @@ config LIRC_PARALLEL
59         help
60           Driver for Homebrew Parallel Port Receivers
61  
62 +config LIRC_RPI
63 +       tristate "Homebrew GPIO Port Receiver/Transmitter for the RaspberryPi"
64 +       depends on LIRC
65 +       help
66 +         Driver for Homebrew GPIO Port Receiver/Transmitter for the RaspberryPi
67 +
68  config LIRC_SASEM
69         tristate "Sasem USB IR Remote"
70         depends on LIRC && USB
71 --- a/drivers/staging/media/lirc/Makefile
72 +++ b/drivers/staging/media/lirc/Makefile
73 @@ -6,6 +6,7 @@
74  obj-$(CONFIG_LIRC_BT829)       += lirc_bt829.o
75  obj-$(CONFIG_LIRC_IMON)                += lirc_imon.o
76  obj-$(CONFIG_LIRC_PARALLEL)    += lirc_parallel.o
77 +obj-$(CONFIG_LIRC_RPI)         += lirc_rpi.o
78  obj-$(CONFIG_LIRC_SASEM)       += lirc_sasem.o
79  obj-$(CONFIG_LIRC_SERIAL)      += lirc_serial.o
80  obj-$(CONFIG_LIRC_SIR)         += lirc_sir.o
81 --- /dev/null
82 +++ b/drivers/staging/media/lirc/lirc_rpi.c
83 @@ -0,0 +1,765 @@
84 +/*
85 + * lirc_rpi.c
86 + *
87 + * lirc_rpi - Device driver that records pulse- and pause-lengths
88 + *           (space-lengths) (just like the lirc_serial driver does)
89 + *           between GPIO interrupt events on the Raspberry Pi.
90 + *           Lots of code has been taken from the lirc_serial module,
91 + *           so I would like say thanks to the authors.
92 + *
93 + * Copyright (C) 2012 Aron Robert Szabo <aron@reon.hu>,
94 + *                   Michael Bishop <cleverca22@gmail.com>
95 + *  This program is free software; you can redistribute it and/or modify
96 + *  it under the terms of the GNU General Public License as published by
97 + *  the Free Software Foundation; either version 2 of the License, or
98 + *  (at your option) any later version.
99 + *
100 + *  This program is distributed in the hope that it will be useful,
101 + *  but WITHOUT ANY WARRANTY; without even the implied warranty of
102 + *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
103 + *  GNU General Public License for more details.
104 + *
105 + *  You should have received a copy of the GNU General Public License
106 + *  along with this program; if not, write to the Free Software
107 + *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
108 + */
109 +
110 +#include <linux/module.h>
111 +#include <linux/errno.h>
112 +#include <linux/interrupt.h>
113 +#include <linux/sched.h>
114 +#include <linux/kernel.h>
115 +#include <linux/time.h>
116 +#include <linux/timex.h>
117 +#include <linux/timekeeping.h>
118 +#include <linux/string.h>
119 +#include <linux/delay.h>
120 +#include <linux/platform_device.h>
121 +#include <linux/irq.h>
122 +#include <linux/spinlock.h>
123 +#include <media/lirc.h>
124 +#include <media/lirc_dev.h>
125 +#include <mach/gpio.h>
126 +#include <linux/gpio.h>
127 +#include <linux/of_platform.h>
128 +
129 +#include <linux/platform_data/bcm2708.h>
130 +
131 +#define LIRC_DRIVER_NAME "lirc_rpi"
132 +#define RBUF_LEN 256
133 +#define LIRC_TRANSMITTER_LATENCY 50
134 +
135 +#ifndef MAX_UDELAY_MS
136 +#define MAX_UDELAY_US 5000
137 +#else
138 +#define MAX_UDELAY_US (MAX_UDELAY_MS*1000)
139 +#endif
140 +
141 +#define dprintk(fmt, args...)                                  \
142 +       do {                                                    \
143 +               if (debug)                                      \
144 +                       printk(KERN_DEBUG LIRC_DRIVER_NAME ": " \
145 +                              fmt, ## args);                   \
146 +       } while (0)
147 +
148 +/* module parameters */
149 +
150 +/* set the default GPIO input pin */
151 +static int gpio_in_pin = 18;
152 +/* set the default pull behaviour for input pin */
153 +static int gpio_in_pull = BCM2708_PULL_DOWN;
154 +/* set the default GPIO output pin */
155 +static int gpio_out_pin = 17;
156 +/* enable debugging messages */
157 +static bool debug;
158 +/* -1 = auto, 0 = active high, 1 = active low */
159 +static int sense = -1;
160 +/* use softcarrier by default */
161 +static bool softcarrier = 1;
162 +/* 0 = do not invert output, 1 = invert output */
163 +static bool invert = 0;
164 +
165 +struct gpio_chip *gpiochip;
166 +static int irq_num;
167 +
168 +/* forward declarations */
169 +static long send_pulse(unsigned long length);
170 +static void send_space(long length);
171 +static void lirc_rpi_exit(void);
172 +
173 +static struct platform_device *lirc_rpi_dev;
174 +static struct timeval lasttv = { 0, 0 };
175 +static struct lirc_buffer rbuf;
176 +static spinlock_t lock;
177 +
178 +/* initialized/set in init_timing_params() */
179 +static unsigned int freq = 38000;
180 +static unsigned int duty_cycle = 50;
181 +static unsigned long period;
182 +static unsigned long pulse_width;
183 +static unsigned long space_width;
184 +
185 +static void safe_udelay(unsigned long usecs)
186 +{
187 +       while (usecs > MAX_UDELAY_US) {
188 +               udelay(MAX_UDELAY_US);
189 +               usecs -= MAX_UDELAY_US;
190 +       }
191 +       udelay(usecs);
192 +}
193 +
194 +static unsigned long read_current_us(void)
195 +{
196 +       struct timespec now;
197 +       getnstimeofday(&now);
198 +       return (now.tv_sec * 1000000) + (now.tv_nsec/1000);
199 +}
200 +
201 +static int init_timing_params(unsigned int new_duty_cycle,
202 +       unsigned int new_freq)
203 +{
204 +       if (1000 * 1000000L / new_freq * new_duty_cycle / 100 <=
205 +           LIRC_TRANSMITTER_LATENCY)
206 +               return -EINVAL;
207 +       if (1000 * 1000000L / new_freq * (100 - new_duty_cycle) / 100 <=
208 +           LIRC_TRANSMITTER_LATENCY)
209 +               return -EINVAL;
210 +       duty_cycle = new_duty_cycle;
211 +       freq = new_freq;
212 +       period = 1000 * 1000000L / freq;
213 +       pulse_width = period * duty_cycle / 100;
214 +       space_width = period - pulse_width;
215 +       dprintk("in init_timing_params, freq=%d pulse=%ld, "
216 +               "space=%ld\n", freq, pulse_width, space_width);
217 +       return 0;
218 +}
219 +
220 +static long send_pulse_softcarrier(unsigned long length)
221 +{
222 +       int flag;
223 +       unsigned long actual, target;
224 +       unsigned long actual_us, initial_us, target_us;
225 +
226 +       length *= 1000;
227 +
228 +       actual = 0; target = 0; flag = 0;
229 +       actual_us = read_current_us();
230 +
231 +       while (actual < length) {
232 +               if (flag) {
233 +                       gpiochip->set(gpiochip, gpio_out_pin, invert);
234 +                       target += space_width;
235 +               } else {
236 +                       gpiochip->set(gpiochip, gpio_out_pin, !invert);
237 +                       target += pulse_width;
238 +               }
239 +               initial_us = actual_us;
240 +               target_us = actual_us + (target - actual) / 1000;
241 +               /*
242 +                * Note - we've checked in ioctl that the pulse/space
243 +                * widths are big enough so that d is > 0
244 +                */
245 +               if  ((int)(target_us - actual_us) > 0)
246 +                       udelay(target_us - actual_us);
247 +               actual_us = read_current_us();
248 +               actual += (actual_us - initial_us) * 1000;
249 +               flag = !flag;
250 +       }
251 +       return (actual-length) / 1000;
252 +}
253 +
254 +static long send_pulse(unsigned long length)
255 +{
256 +       if (length <= 0)
257 +               return 0;
258 +
259 +       if (softcarrier) {
260 +               return send_pulse_softcarrier(length);
261 +       } else {
262 +               gpiochip->set(gpiochip, gpio_out_pin, !invert);
263 +               safe_udelay(length);
264 +               return 0;
265 +       }
266 +}
267 +
268 +static void send_space(long length)
269 +{
270 +       gpiochip->set(gpiochip, gpio_out_pin, invert);
271 +       if (length <= 0)
272 +               return;
273 +       safe_udelay(length);
274 +}
275 +
276 +static void rbwrite(int l)
277 +{
278 +       if (lirc_buffer_full(&rbuf)) {
279 +               /* no new signals will be accepted */
280 +               dprintk("Buffer overrun\n");
281 +               return;
282 +       }
283 +       lirc_buffer_write(&rbuf, (void *)&l);
284 +}
285 +
286 +static void frbwrite(int l)
287 +{
288 +       /* simple noise filter */
289 +       static int pulse, space;
290 +       static unsigned int ptr;
291 +
292 +       if (ptr > 0 && (l & PULSE_BIT)) {
293 +               pulse += l & PULSE_MASK;
294 +               if (pulse > 250) {
295 +                       rbwrite(space);
296 +                       rbwrite(pulse | PULSE_BIT);
297 +                       ptr = 0;
298 +                       pulse = 0;
299 +               }
300 +               return;
301 +       }
302 +       if (!(l & PULSE_BIT)) {
303 +               if (ptr == 0) {
304 +                       if (l > 20000) {
305 +                               space = l;
306 +                               ptr++;
307 +                               return;
308 +                       }
309 +               } else {
310 +                       if (l > 20000) {
311 +                               space += pulse;
312 +                               if (space > PULSE_MASK)
313 +                                       space = PULSE_MASK;
314 +                               space += l;
315 +                               if (space > PULSE_MASK)
316 +                                       space = PULSE_MASK;
317 +                               pulse = 0;
318 +                               return;
319 +                       }
320 +                       rbwrite(space);
321 +                       rbwrite(pulse | PULSE_BIT);
322 +                       ptr = 0;
323 +                       pulse = 0;
324 +               }
325 +       }
326 +       rbwrite(l);
327 +}
328 +
329 +static irqreturn_t irq_handler(int i, void *blah, struct pt_regs *regs)
330 +{
331 +       struct timeval tv;
332 +       long deltv;
333 +       int data;
334 +       int signal;
335 +
336 +       /* use the GPIO signal level */
337 +       signal = gpiochip->get(gpiochip, gpio_in_pin);
338 +
339 +       if (sense != -1) {
340 +               /* get current time */
341 +               do_gettimeofday(&tv);
342 +
343 +               /* calc time since last interrupt in microseconds */
344 +               deltv = tv.tv_sec-lasttv.tv_sec;
345 +               if (tv.tv_sec < lasttv.tv_sec ||
346 +                   (tv.tv_sec == lasttv.tv_sec &&
347 +                    tv.tv_usec < lasttv.tv_usec)) {
348 +                       printk(KERN_WARNING LIRC_DRIVER_NAME
349 +                              ": AIEEEE: your clock just jumped backwards\n");
350 +                       printk(KERN_WARNING LIRC_DRIVER_NAME
351 +                              ": %d %d %lx %lx %lx %lx\n", signal, sense,
352 +                              tv.tv_sec, lasttv.tv_sec,
353 +                              tv.tv_usec, lasttv.tv_usec);
354 +                       data = PULSE_MASK;
355 +               } else if (deltv > 15) {
356 +                       data = PULSE_MASK; /* really long time */
357 +                       if (!(signal^sense)) {
358 +                               /* sanity check */
359 +                               printk(KERN_WARNING LIRC_DRIVER_NAME
360 +                                      ": AIEEEE: %d %d %lx %lx %lx %lx\n",
361 +                                      signal, sense, tv.tv_sec, lasttv.tv_sec,
362 +                                      tv.tv_usec, lasttv.tv_usec);
363 +                               /*
364 +                                * detecting pulse while this
365 +                                * MUST be a space!
366 +                                */
367 +                               sense = sense ? 0 : 1;
368 +                       }
369 +               } else {
370 +                       data = (int) (deltv*1000000 +
371 +                                     (tv.tv_usec - lasttv.tv_usec));
372 +               }
373 +               frbwrite(signal^sense ? data : (data|PULSE_BIT));
374 +               lasttv = tv;
375 +               wake_up_interruptible(&rbuf.wait_poll);
376 +       }
377 +
378 +       return IRQ_HANDLED;
379 +}
380 +
381 +static int is_right_chip(struct gpio_chip *chip, void *data)
382 +{
383 +       dprintk("is_right_chip %s %d\n", chip->label, strcmp(data, chip->label));
384 +
385 +       if (strcmp(data, chip->label) == 0)
386 +               return 1;
387 +       return 0;
388 +}
389 +
390 +static inline int read_bool_property(const struct device_node *np,
391 +                                    const char *propname,
392 +                                    bool *out_value)
393 +{
394 +       u32 value = 0;
395 +       int err = of_property_read_u32(np, propname, &value);
396 +       if (err == 0)
397 +               *out_value = (value != 0);
398 +       return err;
399 +}
400 +
401 +static void read_pin_settings(struct device_node *node)
402 +{
403 +       u32 pin;
404 +       int index;
405 +
406 +       for (index = 0;
407 +            of_property_read_u32_index(
408 +                    node,
409 +                    "brcm,pins",
410 +                    index,
411 +                    &pin) == 0;
412 +            index++) {
413 +               u32 function;
414 +               int err;
415 +               err = of_property_read_u32_index(
416 +                       node,
417 +                       "brcm,function",
418 +                       index,
419 +                       &function);
420 +               if (err == 0) {
421 +                       if (function == 1) /* Output */
422 +                               gpio_out_pin = pin;
423 +                       else if (function == 0) /* Input */
424 +                               gpio_in_pin = pin;
425 +               }
426 +       }
427 +}
428 +
429 +static int init_port(void)
430 +{
431 +       int i, nlow, nhigh, ret;
432 +       struct device_node *node;
433 +
434 +       node = lirc_rpi_dev->dev.of_node;
435 +
436 +       gpiochip = gpiochip_find("bcm2708_gpio", is_right_chip);
437 +
438 +       /*
439 +        * Because of the lack of a setpull function, only support
440 +        * pinctrl-bcm2835 if using device tree.
441 +       */
442 +       if (!gpiochip && node)
443 +               gpiochip = gpiochip_find("pinctrl-bcm2835", is_right_chip);
444 +
445 +       if (!gpiochip) {
446 +               pr_err(LIRC_DRIVER_NAME ": gpio chip not found!\n");
447 +               return -ENODEV;
448 +       }
449 +
450 +       if (node) {
451 +               struct device_node *pins_node;
452 +
453 +               pins_node = of_parse_phandle(node, "pinctrl-0", 0);
454 +               if (!pins_node) {
455 +                       printk(KERN_ERR LIRC_DRIVER_NAME
456 +                              ": pinctrl settings not found!\n");
457 +                       ret = -EINVAL;
458 +                       goto exit_init_port;
459 +               }
460 +
461 +               read_pin_settings(pins_node);
462 +
463 +               of_property_read_u32(node, "rpi,sense", &sense);
464 +
465 +               read_bool_property(node, "rpi,softcarrier", &softcarrier);
466 +
467 +               read_bool_property(node, "rpi,invert", &invert);
468 +
469 +               read_bool_property(node, "rpi,debug", &debug);
470 +
471 +       }
472 +       else
473 +       {
474 +               if (gpio_in_pin >= BCM2708_NR_GPIOS ||
475 +                   gpio_out_pin >= BCM2708_NR_GPIOS) {
476 +                       ret = -EINVAL;
477 +                       printk(KERN_ERR LIRC_DRIVER_NAME
478 +                              ": invalid GPIO pin(s) specified!\n");
479 +                       goto exit_init_port;
480 +               }
481 +
482 +               if (gpio_request(gpio_out_pin, LIRC_DRIVER_NAME " ir/out")) {
483 +                       printk(KERN_ALERT LIRC_DRIVER_NAME
484 +                              ": cant claim gpio pin %d\n", gpio_out_pin);
485 +                       ret = -ENODEV;
486 +                       goto exit_init_port;
487 +               }
488 +
489 +               if (gpio_request(gpio_in_pin, LIRC_DRIVER_NAME " ir/in")) {
490 +                       printk(KERN_ALERT LIRC_DRIVER_NAME
491 +                              ": cant claim gpio pin %d\n", gpio_in_pin);
492 +                       ret = -ENODEV;
493 +                       goto exit_gpio_free_out_pin;
494 +               }
495 +
496 +               bcm2708_gpio_setpull(gpiochip, gpio_in_pin, gpio_in_pull);
497 +               gpiochip->direction_input(gpiochip, gpio_in_pin);
498 +               gpiochip->direction_output(gpiochip, gpio_out_pin, 1);
499 +       }
500 +
501 +       gpiochip->set(gpiochip, gpio_out_pin, invert);
502 +
503 +       irq_num = gpiochip->to_irq(gpiochip, gpio_in_pin);
504 +       dprintk("to_irq %d\n", irq_num);
505 +
506 +       /* if pin is high, then this must be an active low receiver. */
507 +       if (sense == -1) {
508 +               /* wait 1/2 sec for the power supply */
509 +               msleep(500);
510 +
511 +               /*
512 +                * probe 9 times every 0.04s, collect "votes" for
513 +                * active high/low
514 +                */
515 +               nlow = 0;
516 +               nhigh = 0;
517 +               for (i = 0; i < 9; i++) {
518 +                       if (gpiochip->get(gpiochip, gpio_in_pin))
519 +                               nlow++;
520 +                       else
521 +                               nhigh++;
522 +                       msleep(40);
523 +               }
524 +               sense = (nlow >= nhigh ? 1 : 0);
525 +               printk(KERN_INFO LIRC_DRIVER_NAME
526 +                      ": auto-detected active %s receiver on GPIO pin %d\n",
527 +                      sense ? "low" : "high", gpio_in_pin);
528 +       } else {
529 +               printk(KERN_INFO LIRC_DRIVER_NAME
530 +                      ": manually using active %s receiver on GPIO pin %d\n",
531 +                      sense ? "low" : "high", gpio_in_pin);
532 +       }
533 +
534 +       return 0;
535 +
536 +       exit_gpio_free_out_pin:
537 +       gpio_free(gpio_out_pin);
538 +
539 +       exit_init_port:
540 +       return ret;
541 +}
542 +
543 +// called when the character device is opened
544 +static int set_use_inc(void *data)
545 +{
546 +       int result;
547 +
548 +       /* initialize timestamp */
549 +       do_gettimeofday(&lasttv);
550 +
551 +       result = request_irq(irq_num,
552 +                            (irq_handler_t) irq_handler,
553 +                            IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING,
554 +                            LIRC_DRIVER_NAME, (void*) 0);
555 +
556 +       switch (result) {
557 +       case -EBUSY:
558 +               printk(KERN_ERR LIRC_DRIVER_NAME
559 +                      ": IRQ %d is busy\n",
560 +                      irq_num);
561 +               return -EBUSY;
562 +       case -EINVAL:
563 +               printk(KERN_ERR LIRC_DRIVER_NAME
564 +                      ": Bad irq number or handler\n");
565 +               return -EINVAL;
566 +       default:
567 +               dprintk("Interrupt %d obtained\n",
568 +                       irq_num);
569 +               break;
570 +       };
571 +
572 +       /* initialize pulse/space widths */
573 +       init_timing_params(duty_cycle, freq);
574 +
575 +       return 0;
576 +}
577 +
578 +static void set_use_dec(void *data)
579 +{
580 +       /* GPIO Pin Falling/Rising Edge Detect Disable */
581 +       irq_set_irq_type(irq_num, 0);
582 +       disable_irq(irq_num);
583 +
584 +       free_irq(irq_num, (void *) 0);
585 +
586 +       dprintk(KERN_INFO LIRC_DRIVER_NAME
587 +               ": freed IRQ %d\n", irq_num);
588 +}
589 +
590 +static ssize_t lirc_write(struct file *file, const char *buf,
591 +       size_t n, loff_t *ppos)
592 +{
593 +       int i, count;
594 +       unsigned long flags;
595 +       long delta = 0;
596 +       int *wbuf;
597 +
598 +       count = n / sizeof(int);
599 +       if (n % sizeof(int) || count % 2 == 0)
600 +               return -EINVAL;
601 +       wbuf = memdup_user(buf, n);
602 +       if (IS_ERR(wbuf))
603 +               return PTR_ERR(wbuf);
604 +       spin_lock_irqsave(&lock, flags);
605 +
606 +       for (i = 0; i < count; i++) {
607 +               if (i%2)
608 +                       send_space(wbuf[i] - delta);
609 +               else
610 +                       delta = send_pulse(wbuf[i]);
611 +       }
612 +       gpiochip->set(gpiochip, gpio_out_pin, invert);
613 +
614 +       spin_unlock_irqrestore(&lock, flags);
615 +       kfree(wbuf);
616 +       return n;
617 +}
618 +
619 +static long lirc_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
620 +{
621 +       int result;
622 +       __u32 value;
623 +
624 +       switch (cmd) {
625 +       case LIRC_GET_SEND_MODE:
626 +               return -ENOIOCTLCMD;
627 +               break;
628 +
629 +       case LIRC_SET_SEND_MODE:
630 +               result = get_user(value, (__u32 *) arg);
631 +               if (result)
632 +                       return result;
633 +               /* only LIRC_MODE_PULSE supported */
634 +               if (value != LIRC_MODE_PULSE)
635 +                       return -ENOSYS;
636 +               break;
637 +
638 +       case LIRC_GET_LENGTH:
639 +               return -ENOSYS;
640 +               break;
641 +
642 +       case LIRC_SET_SEND_DUTY_CYCLE:
643 +               dprintk("SET_SEND_DUTY_CYCLE\n");
644 +               result = get_user(value, (__u32 *) arg);
645 +               if (result)
646 +                       return result;
647 +               if (value <= 0 || value > 100)
648 +                       return -EINVAL;
649 +               return init_timing_params(value, freq);
650 +               break;
651 +
652 +       case LIRC_SET_SEND_CARRIER:
653 +               dprintk("SET_SEND_CARRIER\n");
654 +               result = get_user(value, (__u32 *) arg);
655 +               if (result)
656 +                       return result;
657 +               if (value > 500000 || value < 20000)
658 +                       return -EINVAL;
659 +               return init_timing_params(duty_cycle, value);
660 +               break;
661 +
662 +       default:
663 +               return lirc_dev_fop_ioctl(filep, cmd, arg);
664 +       }
665 +       return 0;
666 +}
667 +
668 +static const struct file_operations lirc_fops = {
669 +       .owner          = THIS_MODULE,
670 +       .write          = lirc_write,
671 +       .unlocked_ioctl = lirc_ioctl,
672 +       .read           = lirc_dev_fop_read,
673 +       .poll           = lirc_dev_fop_poll,
674 +       .open           = lirc_dev_fop_open,
675 +       .release        = lirc_dev_fop_close,
676 +       .llseek         = no_llseek,
677 +};
678 +
679 +static struct lirc_driver driver = {
680 +       .name           = LIRC_DRIVER_NAME,
681 +       .minor          = -1,
682 +       .code_length    = 1,
683 +       .sample_rate    = 0,
684 +       .data           = NULL,
685 +       .add_to_buf     = NULL,
686 +       .rbuf           = &rbuf,
687 +       .set_use_inc    = set_use_inc,
688 +       .set_use_dec    = set_use_dec,
689 +       .fops           = &lirc_fops,
690 +       .dev            = NULL,
691 +       .owner          = THIS_MODULE,
692 +};
693 +
694 +static const struct of_device_id lirc_rpi_of_match[] = {
695 +       { .compatible = "rpi,lirc-rpi", },
696 +       {},
697 +};
698 +MODULE_DEVICE_TABLE(of, lirc_rpi_of_match);
699 +
700 +static struct platform_driver lirc_rpi_driver = {
701 +       .driver = {
702 +               .name   = LIRC_DRIVER_NAME,
703 +               .owner  = THIS_MODULE,
704 +               .of_match_table = of_match_ptr(lirc_rpi_of_match),
705 +       },
706 +};
707 +
708 +static int __init lirc_rpi_init(void)
709 +{
710 +       struct device_node *node;
711 +       int result;
712 +
713 +       /* Init read buffer. */
714 +       result = lirc_buffer_init(&rbuf, sizeof(int), RBUF_LEN);
715 +       if (result < 0)
716 +               return -ENOMEM;
717 +
718 +       result = platform_driver_register(&lirc_rpi_driver);
719 +       if (result) {
720 +               printk(KERN_ERR LIRC_DRIVER_NAME
721 +                      ": lirc register returned %d\n", result);
722 +               goto exit_buffer_free;
723 +       }
724 +
725 +       node = of_find_compatible_node(NULL, NULL,
726 +                                      lirc_rpi_of_match[0].compatible);
727 +
728 +       if (node) {
729 +               /* DT-enabled */
730 +               lirc_rpi_dev = of_find_device_by_node(node);
731 +               WARN_ON(lirc_rpi_dev->dev.of_node != node);
732 +               of_node_put(node);
733 +       }
734 +       else {
735 +               lirc_rpi_dev = platform_device_alloc(LIRC_DRIVER_NAME, 0);
736 +               if (!lirc_rpi_dev) {
737 +                       result = -ENOMEM;
738 +                       goto exit_driver_unregister;
739 +               }
740 +
741 +               result = platform_device_add(lirc_rpi_dev);
742 +               if (result)
743 +                       goto exit_device_put;
744 +       }
745 +
746 +       return 0;
747 +
748 +       exit_device_put:
749 +       platform_device_put(lirc_rpi_dev);
750 +
751 +       exit_driver_unregister:
752 +       platform_driver_unregister(&lirc_rpi_driver);
753 +
754 +       exit_buffer_free:
755 +       lirc_buffer_free(&rbuf);
756 +
757 +       return result;
758 +}
759 +
760 +static void lirc_rpi_exit(void)
761 +{
762 +       if (!lirc_rpi_dev->dev.of_node)
763 +               platform_device_unregister(lirc_rpi_dev);
764 +       platform_driver_unregister(&lirc_rpi_driver);
765 +       lirc_buffer_free(&rbuf);
766 +}
767 +
768 +static int __init lirc_rpi_init_module(void)
769 +{
770 +       int result;
771 +
772 +       result = lirc_rpi_init();
773 +       if (result)
774 +               return result;
775 +
776 +       result = init_port();
777 +       if (result < 0)
778 +               goto exit_rpi;
779 +
780 +       driver.features = LIRC_CAN_SET_SEND_DUTY_CYCLE |
781 +                         LIRC_CAN_SET_SEND_CARRIER |
782 +                         LIRC_CAN_SEND_PULSE |
783 +                         LIRC_CAN_REC_MODE2;
784 +
785 +       driver.dev = &lirc_rpi_dev->dev;
786 +       driver.minor = lirc_register_driver(&driver);
787 +
788 +       if (driver.minor < 0) {
789 +               printk(KERN_ERR LIRC_DRIVER_NAME
790 +                      ": device registration failed with %d\n", result);
791 +               result = -EIO;
792 +               goto exit_rpi;
793 +       }
794 +
795 +       printk(KERN_INFO LIRC_DRIVER_NAME ": driver registered!\n");
796 +
797 +       return 0;
798 +
799 +       exit_rpi:
800 +       lirc_rpi_exit();
801 +
802 +       return result;
803 +}
804 +
805 +static void __exit lirc_rpi_exit_module(void)
806 +{
807 +       lirc_unregister_driver(driver.minor);
808 +
809 +       gpio_free(gpio_out_pin);
810 +       gpio_free(gpio_in_pin);
811 +
812 +       lirc_rpi_exit();
813 +
814 +       printk(KERN_INFO LIRC_DRIVER_NAME ": cleaned up module\n");
815 +}
816 +
817 +module_init(lirc_rpi_init_module);
818 +module_exit(lirc_rpi_exit_module);
819 +
820 +MODULE_DESCRIPTION("Infra-red receiver and blaster driver for Raspberry Pi GPIO.");
821 +MODULE_AUTHOR("Aron Robert Szabo <aron@reon.hu>");
822 +MODULE_AUTHOR("Michael Bishop <cleverca22@gmail.com>");
823 +MODULE_LICENSE("GPL");
824 +
825 +module_param(gpio_out_pin, int, S_IRUGO);
826 +MODULE_PARM_DESC(gpio_out_pin, "GPIO output/transmitter pin number of the BCM"
827 +                " processor. (default 17");
828 +
829 +module_param(gpio_in_pin, int, S_IRUGO);
830 +MODULE_PARM_DESC(gpio_in_pin, "GPIO input pin number of the BCM processor."
831 +                " (default 18");
832 +
833 +module_param(gpio_in_pull, int, S_IRUGO);
834 +MODULE_PARM_DESC(gpio_in_pull, "GPIO input pin pull configuration."
835 +                " (0 = off, 1 = up, 2 = down, default down)");
836 +
837 +module_param(sense, int, S_IRUGO);
838 +MODULE_PARM_DESC(sense, "Override autodetection of IR receiver circuit"
839 +                " (0 = active high, 1 = active low )");
840 +
841 +module_param(softcarrier, bool, S_IRUGO);
842 +MODULE_PARM_DESC(softcarrier, "Software carrier (0 = off, 1 = on, default on)");
843 +
844 +module_param(invert, bool, S_IRUGO);
845 +MODULE_PARM_DESC(invert, "Invert output (0 = off, 1 = on, default off");
846 +
847 +module_param(debug, bool, S_IRUGO | S_IWUSR);
848 +MODULE_PARM_DESC(debug, "Enable debugging messages");