1 diff -urN linux-3.10/drivers/staging/media/lirc/Kconfig linux-rpi-3.10.y/drivers/staging/media/lirc/Kconfig
2 --- linux-3.10/drivers/staging/media/lirc/Kconfig 2013-06-30 23:13:29.000000000 +0100
3 +++ linux-rpi-3.10.y/drivers/staging/media/lirc/Kconfig 2013-07-06 15:25:50.000000000 +0100
6 Driver for Homebrew Parallel Port Receivers
9 + tristate "Homebrew GPIO Port Receiver/Transmitter for the RaspberryPi"
12 + Driver for Homebrew GPIO Port Receiver/Transmitter for the RaspberryPi
15 tristate "Sasem USB IR Remote"
16 depends on LIRC && USB
17 diff -urN linux-3.10/drivers/staging/media/lirc/lirc_rpi.c linux-rpi-3.10.y/drivers/staging/media/lirc/lirc_rpi.c
18 --- linux-3.10/drivers/staging/media/lirc/lirc_rpi.c 1970-01-01 01:00:00.000000000 +0100
19 +++ linux-rpi-3.10.y/drivers/staging/media/lirc/lirc_rpi.c 2013-07-06 15:25:50.000000000 +0100
24 + * lirc_rpi - Device driver that records pulse- and pause-lengths
25 + * (space-lengths) (just like the lirc_serial driver does)
26 + * between GPIO interrupt events on the Raspberry Pi.
27 + * Lots of code has been taken from the lirc_serial module,
28 + * so I would like say thanks to the authors.
30 + * Copyright (C) 2012 Aron Robert Szabo <aron@reon.hu>,
31 + * Michael Bishop <cleverca22@gmail.com>
32 + * This program is free software; you can redistribute it and/or modify
33 + * it under the terms of the GNU General Public License as published by
34 + * the Free Software Foundation; either version 2 of the License, or
35 + * (at your option) any later version.
37 + * This program is distributed in the hope that it will be useful,
38 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
39 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
40 + * GNU General Public License for more details.
42 + * You should have received a copy of the GNU General Public License
43 + * along with this program; if not, write to the Free Software
44 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
47 +#include <linux/module.h>
48 +#include <linux/errno.h>
49 +#include <linux/interrupt.h>
50 +#include <linux/sched.h>
51 +#include <linux/kernel.h>
52 +#include <linux/time.h>
53 +#include <linux/string.h>
54 +#include <linux/delay.h>
55 +#include <linux/platform_device.h>
56 +#include <linux/irq.h>
57 +#include <linux/spinlock.h>
58 +#include <media/lirc.h>
59 +#include <media/lirc_dev.h>
60 +#include <linux/gpio.h>
62 +#define LIRC_DRIVER_NAME "lirc_rpi"
64 +#define LIRC_TRANSMITTER_LATENCY 256
66 +#ifndef MAX_UDELAY_MS
67 +#define MAX_UDELAY_US 5000
69 +#define MAX_UDELAY_US (MAX_UDELAY_MS*1000)
72 +#define dprintk(fmt, args...) \
75 + printk(KERN_DEBUG LIRC_DRIVER_NAME ": " \
79 +/* module parameters */
81 +/* set the default GPIO input pin */
82 +static int gpio_in_pin = 18;
83 +/* set the default GPIO output pin */
84 +static int gpio_out_pin = 17;
85 +/* enable debugging messages */
87 +/* -1 = auto, 0 = active high, 1 = active low */
88 +static int sense = -1;
89 +/* use softcarrier by default */
90 +static int softcarrier = 1;
91 +/* 0 = do not invert output, 1 = invert output */
92 +static int invert = 0;
94 +struct gpio_chip *gpiochip;
95 +struct irq_chip *irqchip;
96 +struct irq_data *irqdata;
98 +/* forward declarations */
99 +static long send_pulse(unsigned long length);
100 +static void send_space(long length);
101 +static void lirc_rpi_exit(void);
103 +int valid_gpio_pins[] = { 0, 1, 4, 8, 7, 9, 10, 11, 14, 15, 17, 18, 21, 22, 23,
106 +static struct platform_device *lirc_rpi_dev;
107 +static struct timeval lasttv = { 0, 0 };
108 +static struct lirc_buffer rbuf;
109 +static spinlock_t lock;
111 +/* initialized/set in init_timing_params() */
112 +static unsigned int freq = 38000;
113 +static unsigned int duty_cycle = 50;
114 +static unsigned long period;
115 +static unsigned long pulse_width;
116 +static unsigned long space_width;
118 +static void safe_udelay(unsigned long usecs)
120 + while (usecs > MAX_UDELAY_US) {
121 + udelay(MAX_UDELAY_US);
122 + usecs -= MAX_UDELAY_US;
127 +static int init_timing_params(unsigned int new_duty_cycle,
128 + unsigned int new_freq)
131 + * period, pulse/space width are kept with 8 binary places -
132 + * IE multiplied by 256.
134 + if (256 * 1000000L / new_freq * new_duty_cycle / 100 <=
135 + LIRC_TRANSMITTER_LATENCY)
137 + if (256 * 1000000L / new_freq * (100 - new_duty_cycle) / 100 <=
138 + LIRC_TRANSMITTER_LATENCY)
140 + duty_cycle = new_duty_cycle;
142 + period = 256 * 1000000L / freq;
143 + pulse_width = period * duty_cycle / 100;
144 + space_width = period - pulse_width;
145 + dprintk("in init_timing_params, freq=%d pulse=%ld, "
146 + "space=%ld\n", freq, pulse_width, space_width);
150 +static long send_pulse_softcarrier(unsigned long length)
153 + unsigned long actual, target, d;
157 + actual = 0; target = 0; flag = 0;
158 + while (actual < length) {
160 + gpiochip->set(gpiochip, gpio_out_pin, invert);
161 + target += space_width;
163 + gpiochip->set(gpiochip, gpio_out_pin, !invert);
164 + target += pulse_width;
166 + d = (target - actual -
167 + LIRC_TRANSMITTER_LATENCY + 128) >> 8;
169 + * Note - we've checked in ioctl that the pulse/space
170 + * widths are big enough so that d is > 0
173 + actual += (d << 8) + LIRC_TRANSMITTER_LATENCY;
176 + return (actual-length) >> 8;
179 +static long send_pulse(unsigned long length)
185 + return send_pulse_softcarrier(length);
187 + gpiochip->set(gpiochip, gpio_out_pin, !invert);
188 + safe_udelay(length);
193 +static void send_space(long length)
195 + gpiochip->set(gpiochip, gpio_out_pin, invert);
198 + safe_udelay(length);
201 +static void rbwrite(int l)
203 + if (lirc_buffer_full(&rbuf)) {
204 + /* no new signals will be accepted */
205 + dprintk("Buffer overrun\n");
208 + lirc_buffer_write(&rbuf, (void *)&l);
211 +static void frbwrite(int l)
213 + /* simple noise filter */
214 + static int pulse, space;
215 + static unsigned int ptr;
217 + if (ptr > 0 && (l & PULSE_BIT)) {
218 + pulse += l & PULSE_MASK;
221 + rbwrite(pulse | PULSE_BIT);
227 + if (!(l & PULSE_BIT)) {
237 + if (space > PULSE_MASK)
238 + space = PULSE_MASK;
240 + if (space > PULSE_MASK)
241 + space = PULSE_MASK;
246 + rbwrite(pulse | PULSE_BIT);
254 +static irqreturn_t irq_handler(int i, void *blah, struct pt_regs *regs)
261 + /* use the GPIO signal level */
262 + signal = gpiochip->get(gpiochip, gpio_in_pin);
264 + /* unmask the irq */
265 + irqchip->irq_unmask(irqdata);
268 + /* get current time */
269 + do_gettimeofday(&tv);
271 + /* calc time since last interrupt in microseconds */
272 + deltv = tv.tv_sec-lasttv.tv_sec;
273 + if (tv.tv_sec < lasttv.tv_sec ||
274 + (tv.tv_sec == lasttv.tv_sec &&
275 + tv.tv_usec < lasttv.tv_usec)) {
276 + printk(KERN_WARNING LIRC_DRIVER_NAME
277 + ": AIEEEE: your clock just jumped backwards\n");
278 + printk(KERN_WARNING LIRC_DRIVER_NAME
279 + ": %d %d %lx %lx %lx %lx\n", signal, sense,
280 + tv.tv_sec, lasttv.tv_sec,
281 + tv.tv_usec, lasttv.tv_usec);
283 + } else if (deltv > 15) {
284 + data = PULSE_MASK; /* really long time */
285 + if (!(signal^sense)) {
287 + printk(KERN_WARNING LIRC_DRIVER_NAME
288 + ": AIEEEE: %d %d %lx %lx %lx %lx\n",
289 + signal, sense, tv.tv_sec, lasttv.tv_sec,
290 + tv.tv_usec, lasttv.tv_usec);
292 + * detecting pulse while this
295 + sense = sense ? 0 : 1;
298 + data = (int) (deltv*1000000 +
299 + (tv.tv_usec - lasttv.tv_usec));
301 + frbwrite(signal^sense ? data : (data|PULSE_BIT));
303 + wake_up_interruptible(&rbuf.wait_poll);
306 + return IRQ_HANDLED;
309 +static int is_right_chip(struct gpio_chip *chip, void *data)
311 + dprintk("is_right_chip %s %d\n", chip->label, strcmp(data, chip->label));
313 + if (strcmp(data, chip->label) == 0)
318 +static int init_port(void)
320 + int i, nlow, nhigh, ret, irq;
322 + gpiochip = gpiochip_find("bcm2708_gpio", is_right_chip);
327 + if (gpio_request(gpio_out_pin, LIRC_DRIVER_NAME " ir/out")) {
328 + printk(KERN_ALERT LIRC_DRIVER_NAME
329 + ": cant claim gpio pin %d\n", gpio_out_pin);
331 + goto exit_init_port;
334 + if (gpio_request(gpio_in_pin, LIRC_DRIVER_NAME " ir/in")) {
335 + printk(KERN_ALERT LIRC_DRIVER_NAME
336 + ": cant claim gpio pin %d\n", gpio_in_pin);
338 + goto exit_gpio_free_out_pin;
341 + gpiochip->direction_input(gpiochip, gpio_in_pin);
342 + gpiochip->direction_output(gpiochip, gpio_out_pin, 1);
343 + gpiochip->set(gpiochip, gpio_out_pin, invert);
345 + irq = gpiochip->to_irq(gpiochip, gpio_in_pin);
346 + dprintk("to_irq %d\n", irq);
347 + irqdata = irq_get_irq_data(irq);
349 + if (irqdata && irqdata->chip) {
350 + irqchip = irqdata->chip;
353 + goto exit_gpio_free_in_pin;
356 + /* if pin is high, then this must be an active low receiver. */
358 + /* wait 1/2 sec for the power supply */
362 + * probe 9 times every 0.04s, collect "votes" for
367 + for (i = 0; i < 9; i++) {
368 + if (gpiochip->get(gpiochip, gpio_in_pin))
374 + sense = (nlow >= nhigh ? 1 : 0);
375 + printk(KERN_INFO LIRC_DRIVER_NAME
376 + ": auto-detected active %s receiver on GPIO pin %d\n",
377 + sense ? "low" : "high", gpio_in_pin);
379 + printk(KERN_INFO LIRC_DRIVER_NAME
380 + ": manually using active %s receiver on GPIO pin %d\n",
381 + sense ? "low" : "high", gpio_in_pin);
386 + exit_gpio_free_in_pin:
387 + gpio_free(gpio_in_pin);
389 + exit_gpio_free_out_pin:
390 + gpio_free(gpio_out_pin);
396 +// called when the character device is opened
397 +static int set_use_inc(void *data)
400 + unsigned long flags;
402 + /* initialize timestamp */
403 + do_gettimeofday(&lasttv);
405 + result = request_irq(gpiochip->to_irq(gpiochip, gpio_in_pin),
406 + (irq_handler_t) irq_handler, 0,
407 + LIRC_DRIVER_NAME, (void*) 0);
411 + printk(KERN_ERR LIRC_DRIVER_NAME
412 + ": IRQ %d is busy\n",
413 + gpiochip->to_irq(gpiochip, gpio_in_pin));
416 + printk(KERN_ERR LIRC_DRIVER_NAME
417 + ": Bad irq number or handler\n");
420 + dprintk("Interrupt %d obtained\n",
421 + gpiochip->to_irq(gpiochip, gpio_in_pin));
425 + /* initialize pulse/space widths */
426 + init_timing_params(duty_cycle, freq);
428 + spin_lock_irqsave(&lock, flags);
430 + /* GPIO Pin Falling/Rising Edge Detect Enable */
431 + irqchip->irq_set_type(irqdata,
432 + IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING);
434 + /* unmask the irq */
435 + irqchip->irq_unmask(irqdata);
437 + spin_unlock_irqrestore(&lock, flags);
442 +static void set_use_dec(void *data)
444 + unsigned long flags;
446 + spin_lock_irqsave(&lock, flags);
448 + /* GPIO Pin Falling/Rising Edge Detect Disable */
449 + irqchip->irq_set_type(irqdata, 0);
450 + irqchip->irq_mask(irqdata);
452 + spin_unlock_irqrestore(&lock, flags);
454 + free_irq(gpiochip->to_irq(gpiochip, gpio_in_pin), (void *) 0);
456 + dprintk(KERN_INFO LIRC_DRIVER_NAME
457 + ": freed IRQ %d\n", gpiochip->to_irq(gpiochip, gpio_in_pin));
460 +static ssize_t lirc_write(struct file *file, const char *buf,
461 + size_t n, loff_t *ppos)
464 + unsigned long flags;
468 + count = n / sizeof(int);
469 + if (n % sizeof(int) || count % 2 == 0)
471 + wbuf = memdup_user(buf, n);
473 + return PTR_ERR(wbuf);
474 + spin_lock_irqsave(&lock, flags);
476 + for (i = 0; i < count; i++) {
478 + send_space(wbuf[i] - delta);
480 + delta = send_pulse(wbuf[i]);
482 + gpiochip->set(gpiochip, gpio_out_pin, invert);
484 + spin_unlock_irqrestore(&lock, flags);
489 +static long lirc_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
495 + case LIRC_GET_SEND_MODE:
496 + return -ENOIOCTLCMD;
499 + case LIRC_SET_SEND_MODE:
500 + result = get_user(value, (__u32 *) arg);
503 + /* only LIRC_MODE_PULSE supported */
504 + if (value != LIRC_MODE_PULSE)
508 + case LIRC_GET_LENGTH:
512 + case LIRC_SET_SEND_DUTY_CYCLE:
513 + dprintk("SET_SEND_DUTY_CYCLE\n");
514 + result = get_user(value, (__u32 *) arg);
517 + if (value <= 0 || value > 100)
519 + return init_timing_params(value, freq);
522 + case LIRC_SET_SEND_CARRIER:
523 + dprintk("SET_SEND_CARRIER\n");
524 + result = get_user(value, (__u32 *) arg);
527 + if (value > 500000 || value < 20000)
529 + return init_timing_params(duty_cycle, value);
533 + return lirc_dev_fop_ioctl(filep, cmd, arg);
538 +static const struct file_operations lirc_fops = {
539 + .owner = THIS_MODULE,
540 + .write = lirc_write,
541 + .unlocked_ioctl = lirc_ioctl,
542 + .read = lirc_dev_fop_read,
543 + .poll = lirc_dev_fop_poll,
544 + .open = lirc_dev_fop_open,
545 + .release = lirc_dev_fop_close,
546 + .llseek = no_llseek,
549 +static struct lirc_driver driver = {
550 + .name = LIRC_DRIVER_NAME,
555 + .add_to_buf = NULL,
557 + .set_use_inc = set_use_inc,
558 + .set_use_dec = set_use_dec,
559 + .fops = &lirc_fops,
561 + .owner = THIS_MODULE,
564 +static struct platform_driver lirc_rpi_driver = {
566 + .name = LIRC_DRIVER_NAME,
567 + .owner = THIS_MODULE,
571 +static int __init lirc_rpi_init(void)
575 + /* Init read buffer. */
576 + result = lirc_buffer_init(&rbuf, sizeof(int), RBUF_LEN);
580 + result = platform_driver_register(&lirc_rpi_driver);
582 + printk(KERN_ERR LIRC_DRIVER_NAME
583 + ": lirc register returned %d\n", result);
584 + goto exit_buffer_free;
587 + lirc_rpi_dev = platform_device_alloc(LIRC_DRIVER_NAME, 0);
588 + if (!lirc_rpi_dev) {
590 + goto exit_driver_unregister;
593 + result = platform_device_add(lirc_rpi_dev);
595 + goto exit_device_put;
600 + platform_device_put(lirc_rpi_dev);
602 + exit_driver_unregister:
603 + platform_driver_unregister(&lirc_rpi_driver);
606 + lirc_buffer_free(&rbuf);
611 +static void lirc_rpi_exit(void)
613 + gpio_free(gpio_out_pin);
614 + gpio_free(gpio_in_pin);
615 + platform_device_unregister(lirc_rpi_dev);
616 + platform_driver_unregister(&lirc_rpi_driver);
617 + lirc_buffer_free(&rbuf);
620 +static int __init lirc_rpi_init_module(void)
624 + result = lirc_rpi_init();
628 + /* check if the module received valid gpio pin numbers */
630 + if (gpio_in_pin != gpio_out_pin) {
631 + for(i = 0; (i < ARRAY_SIZE(valid_gpio_pins)) && (result != 2); i++) {
632 + if (gpio_in_pin == valid_gpio_pins[i] ||
633 + gpio_out_pin == valid_gpio_pins[i]) {
641 + printk(KERN_ERR LIRC_DRIVER_NAME
642 + ": invalid GPIO pin(s) specified!\n");
646 + driver.features = LIRC_CAN_SET_SEND_DUTY_CYCLE |
647 + LIRC_CAN_SET_SEND_CARRIER |
648 + LIRC_CAN_SEND_PULSE |
649 + LIRC_CAN_REC_MODE2;
651 + driver.dev = &lirc_rpi_dev->dev;
652 + driver.minor = lirc_register_driver(&driver);
654 + if (driver.minor < 0) {
655 + printk(KERN_ERR LIRC_DRIVER_NAME
656 + ": device registration failed with %d\n", result);
661 + printk(KERN_INFO LIRC_DRIVER_NAME ": driver registered!\n");
663 + result = init_port();
675 +static void __exit lirc_rpi_exit_module(void)
679 + lirc_unregister_driver(driver.minor);
680 + printk(KERN_INFO LIRC_DRIVER_NAME ": cleaned up module\n");
683 +module_init(lirc_rpi_init_module);
684 +module_exit(lirc_rpi_exit_module);
686 +MODULE_DESCRIPTION("Infra-red receiver and blaster driver for Raspberry Pi GPIO.");
687 +MODULE_AUTHOR("Aron Robert Szabo <aron@reon.hu>");
688 +MODULE_AUTHOR("Michael Bishop <cleverca22@gmail.com>");
689 +MODULE_LICENSE("GPL");
691 +module_param(gpio_out_pin, int, S_IRUGO);
692 +MODULE_PARM_DESC(gpio_out_pin, "GPIO output/transmitter pin number of the BCM"
693 + " processor. Valid pin numbers are: 0, 1, 4, 8, 7, 9, 10, 11,"
694 + " 14, 15, 17, 18, 21, 22, 23, 24, 25, default 17");
696 +module_param(gpio_in_pin, int, S_IRUGO);
697 +MODULE_PARM_DESC(gpio_in_pin, "GPIO input pin number of the BCM processor."
698 + " Valid pin numbers are: 0, 1, 4, 8, 7, 9, 10, 11, 14, 15,"
699 + " 17, 18, 21, 22, 23, 24, 25, default 18");
701 +module_param(sense, bool, S_IRUGO);
702 +MODULE_PARM_DESC(sense, "Override autodetection of IR receiver circuit"
703 + " (0 = active high, 1 = active low )");
705 +module_param(softcarrier, bool, S_IRUGO);
706 +MODULE_PARM_DESC(softcarrier, "Software carrier (0 = off, 1 = on, default on)");
708 +module_param(invert, bool, S_IRUGO);
709 +MODULE_PARM_DESC(invert, "Invert output (0 = off, 1 = on, default off");
711 +module_param(debug, bool, S_IRUGO | S_IWUSR);
712 +MODULE_PARM_DESC(debug, "Enable debugging messages");
713 diff -urN linux-3.10/drivers/staging/media/lirc/Makefile linux-rpi-3.10.y/drivers/staging/media/lirc/Makefile
714 --- linux-3.10/drivers/staging/media/lirc/Makefile 2013-06-30 23:13:29.000000000 +0100
715 +++ linux-rpi-3.10.y/drivers/staging/media/lirc/Makefile 2013-07-06 15:25:50.000000000 +0100
717 obj-$(CONFIG_LIRC_IGORPLUGUSB) += lirc_igorplugusb.o
718 obj-$(CONFIG_LIRC_IMON) += lirc_imon.o
719 obj-$(CONFIG_LIRC_PARALLEL) += lirc_parallel.o
720 +obj-$(CONFIG_LIRC_RPI) += lirc_rpi.o
721 obj-$(CONFIG_LIRC_SASEM) += lirc_sasem.o
722 obj-$(CONFIG_LIRC_SERIAL) += lirc_serial.o
723 obj-$(CONFIG_LIRC_SIR) += lirc_sir.o