1 --- a/drivers/staging/media/lirc/Kconfig
2 +++ b/drivers/staging/media/lirc/Kconfig
3 @@ -38,6 +38,12 @@ config LIRC_PARALLEL
5 Driver for Homebrew Parallel Port Receivers
8 + tristate "Homebrew GPIO Port Receiver/Transmitter for the RaspberryPi"
11 + Driver for Homebrew GPIO Port Receiver/Transmitter for the RaspberryPi
14 tristate "Sasem USB IR Remote"
15 depends on LIRC && USB
17 +++ b/drivers/staging/media/lirc/lirc_rpi.c
22 + * lirc_rpi - Device driver that records pulse- and pause-lengths
23 + * (space-lengths) (just like the lirc_serial driver does)
24 + * between GPIO interrupt events on the Raspberry Pi.
25 + * Lots of code has been taken from the lirc_serial module,
26 + * so I would like say thanks to the authors.
28 + * Copyright (C) 2012 Aron Robert Szabo <aron@reon.hu>,
29 + * Michael Bishop <cleverca22@gmail.com>
30 + * This program is free software; you can redistribute it and/or modify
31 + * it under the terms of the GNU General Public License as published by
32 + * the Free Software Foundation; either version 2 of the License, or
33 + * (at your option) any later version.
35 + * This program is distributed in the hope that it will be useful,
36 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 + * GNU General Public License for more details.
40 + * You should have received a copy of the GNU General Public License
41 + * along with this program; if not, write to the Free Software
42 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
45 +#include <linux/module.h>
46 +#include <linux/errno.h>
47 +#include <linux/interrupt.h>
48 +#include <linux/sched.h>
49 +#include <linux/kernel.h>
50 +#include <linux/time.h>
51 +#include <linux/string.h>
52 +#include <linux/delay.h>
53 +#include <linux/platform_device.h>
54 +#include <linux/irq.h>
55 +#include <linux/spinlock.h>
56 +#include <media/lirc.h>
57 +#include <media/lirc_dev.h>
58 +#include <linux/gpio.h>
60 +#define LIRC_DRIVER_NAME "lirc_rpi"
62 +#define LIRC_TRANSMITTER_LATENCY 256
64 +#ifndef MAX_UDELAY_MS
65 +#define MAX_UDELAY_US 5000
67 +#define MAX_UDELAY_US (MAX_UDELAY_MS*1000)
70 +#define dprintk(fmt, args...) \
73 + printk(KERN_DEBUG LIRC_DRIVER_NAME ": " \
77 +/* module parameters */
79 +/* set the default GPIO input pin */
80 +static int gpio_in_pin = 18;
81 +/* set the default GPIO output pin */
82 +static int gpio_out_pin = 17;
83 +/* enable debugging messages */
85 +/* -1 = auto, 0 = active high, 1 = active low */
86 +static int sense = -1;
87 +/* use softcarrier by default */
88 +static int softcarrier = 1;
89 +/* 0 = do not invert output, 1 = invert output */
90 +static int invert = 0;
92 +struct gpio_chip *gpiochip;
93 +struct irq_chip *irqchip;
94 +struct irq_data *irqdata;
96 +/* forward declarations */
97 +static long send_pulse(unsigned long length);
98 +static void send_space(long length);
99 +static void lirc_rpi_exit(void);
101 +int valid_gpio_pins[] = { 0, 1, 4, 8, 7, 9, 10, 11, 14, 15, 17, 18, 21, 22, 23,
104 +static struct platform_device *lirc_rpi_dev;
105 +static struct timeval lasttv = { 0, 0 };
106 +static struct lirc_buffer rbuf;
107 +static spinlock_t lock;
109 +/* initialized/set in init_timing_params() */
110 +static unsigned int freq = 38000;
111 +static unsigned int duty_cycle = 50;
112 +static unsigned long period;
113 +static unsigned long pulse_width;
114 +static unsigned long space_width;
116 +static void safe_udelay(unsigned long usecs)
118 + while (usecs > MAX_UDELAY_US) {
119 + udelay(MAX_UDELAY_US);
120 + usecs -= MAX_UDELAY_US;
125 +static int init_timing_params(unsigned int new_duty_cycle,
126 + unsigned int new_freq)
129 + * period, pulse/space width are kept with 8 binary places -
130 + * IE multiplied by 256.
132 + if (256 * 1000000L / new_freq * new_duty_cycle / 100 <=
133 + LIRC_TRANSMITTER_LATENCY)
135 + if (256 * 1000000L / new_freq * (100 - new_duty_cycle) / 100 <=
136 + LIRC_TRANSMITTER_LATENCY)
138 + duty_cycle = new_duty_cycle;
140 + period = 256 * 1000000L / freq;
141 + pulse_width = period * duty_cycle / 100;
142 + space_width = period - pulse_width;
143 + dprintk("in init_timing_params, freq=%d pulse=%ld, "
144 + "space=%ld\n", freq, pulse_width, space_width);
148 +static long send_pulse_softcarrier(unsigned long length)
151 + unsigned long actual, target, d;
155 + actual = 0; target = 0; flag = 0;
156 + while (actual < length) {
158 + gpiochip->set(gpiochip, gpio_out_pin, invert);
159 + target += space_width;
161 + gpiochip->set(gpiochip, gpio_out_pin, !invert);
162 + target += pulse_width;
164 + d = (target - actual -
165 + LIRC_TRANSMITTER_LATENCY + 128) >> 8;
167 + * Note - we've checked in ioctl that the pulse/space
168 + * widths are big enough so that d is > 0
171 + actual += (d << 8) + LIRC_TRANSMITTER_LATENCY;
174 + return (actual-length) >> 8;
177 +static long send_pulse(unsigned long length)
183 + return send_pulse_softcarrier(length);
185 + gpiochip->set(gpiochip, gpio_out_pin, !invert);
186 + safe_udelay(length);
191 +static void send_space(long length)
193 + gpiochip->set(gpiochip, gpio_out_pin, invert);
196 + safe_udelay(length);
199 +static void rbwrite(int l)
201 + if (lirc_buffer_full(&rbuf)) {
202 + /* no new signals will be accepted */
203 + dprintk("Buffer overrun\n");
206 + lirc_buffer_write(&rbuf, (void *)&l);
209 +static void frbwrite(int l)
211 + /* simple noise filter */
212 + static int pulse, space;
213 + static unsigned int ptr;
215 + if (ptr > 0 && (l & PULSE_BIT)) {
216 + pulse += l & PULSE_MASK;
219 + rbwrite(pulse | PULSE_BIT);
225 + if (!(l & PULSE_BIT)) {
235 + if (space > PULSE_MASK)
236 + space = PULSE_MASK;
238 + if (space > PULSE_MASK)
239 + space = PULSE_MASK;
244 + rbwrite(pulse | PULSE_BIT);
252 +static irqreturn_t irq_handler(int i, void *blah, struct pt_regs *regs)
259 + /* use the GPIO signal level */
260 + signal = gpiochip->get(gpiochip, gpio_in_pin);
262 + /* unmask the irq */
263 + irqchip->irq_unmask(irqdata);
266 + /* get current time */
267 + do_gettimeofday(&tv);
269 + /* calc time since last interrupt in microseconds */
270 + deltv = tv.tv_sec-lasttv.tv_sec;
271 + if (tv.tv_sec < lasttv.tv_sec ||
272 + (tv.tv_sec == lasttv.tv_sec &&
273 + tv.tv_usec < lasttv.tv_usec)) {
274 + printk(KERN_WARNING LIRC_DRIVER_NAME
275 + ": AIEEEE: your clock just jumped backwards\n");
276 + printk(KERN_WARNING LIRC_DRIVER_NAME
277 + ": %d %d %lx %lx %lx %lx\n", signal, sense,
278 + tv.tv_sec, lasttv.tv_sec,
279 + tv.tv_usec, lasttv.tv_usec);
281 + } else if (deltv > 15) {
282 + data = PULSE_MASK; /* really long time */
283 + if (!(signal^sense)) {
285 + printk(KERN_WARNING LIRC_DRIVER_NAME
286 + ": AIEEEE: %d %d %lx %lx %lx %lx\n",
287 + signal, sense, tv.tv_sec, lasttv.tv_sec,
288 + tv.tv_usec, lasttv.tv_usec);
290 + * detecting pulse while this
293 + sense = sense ? 0 : 1;
296 + data = (int) (deltv*1000000 +
297 + (tv.tv_usec - lasttv.tv_usec));
299 + frbwrite(signal^sense ? data : (data|PULSE_BIT));
301 + wake_up_interruptible(&rbuf.wait_poll);
304 + return IRQ_HANDLED;
307 +static int is_right_chip(struct gpio_chip *chip, void *data)
309 + dprintk("is_right_chip %s %d\n", chip->label, strcmp(data, chip->label));
311 + if (strcmp(data, chip->label) == 0)
316 +static int init_port(void)
318 + int i, nlow, nhigh, ret, irq;
320 + gpiochip = gpiochip_find("bcm2708_gpio", is_right_chip);
325 + if (gpio_request(gpio_out_pin, LIRC_DRIVER_NAME " ir/out")) {
326 + printk(KERN_ALERT LIRC_DRIVER_NAME
327 + ": cant claim gpio pin %d\n", gpio_out_pin);
329 + goto exit_init_port;
332 + if (gpio_request(gpio_in_pin, LIRC_DRIVER_NAME " ir/in")) {
333 + printk(KERN_ALERT LIRC_DRIVER_NAME
334 + ": cant claim gpio pin %d\n", gpio_in_pin);
336 + goto exit_gpio_free_out_pin;
339 + gpiochip->direction_input(gpiochip, gpio_in_pin);
340 + gpiochip->direction_output(gpiochip, gpio_out_pin, 1);
341 + gpiochip->set(gpiochip, gpio_out_pin, invert);
343 + irq = gpiochip->to_irq(gpiochip, gpio_in_pin);
344 + dprintk("to_irq %d\n", irq);
345 + irqdata = irq_get_irq_data(irq);
347 + if (irqdata && irqdata->chip) {
348 + irqchip = irqdata->chip;
351 + goto exit_gpio_free_in_pin;
354 + /* if pin is high, then this must be an active low receiver. */
356 + /* wait 1/2 sec for the power supply */
360 + * probe 9 times every 0.04s, collect "votes" for
365 + for (i = 0; i < 9; i++) {
366 + if (gpiochip->get(gpiochip, gpio_in_pin))
372 + sense = (nlow >= nhigh ? 1 : 0);
373 + printk(KERN_INFO LIRC_DRIVER_NAME
374 + ": auto-detected active %s receiver on GPIO pin %d\n",
375 + sense ? "low" : "high", gpio_in_pin);
377 + printk(KERN_INFO LIRC_DRIVER_NAME
378 + ": manually using active %s receiver on GPIO pin %d\n",
379 + sense ? "low" : "high", gpio_in_pin);
384 + exit_gpio_free_in_pin:
385 + gpio_free(gpio_in_pin);
387 + exit_gpio_free_out_pin:
388 + gpio_free(gpio_out_pin);
394 +// called when the character device is opened
395 +static int set_use_inc(void *data)
398 + unsigned long flags;
400 + /* initialize timestamp */
401 + do_gettimeofday(&lasttv);
403 + result = request_irq(gpiochip->to_irq(gpiochip, gpio_in_pin),
404 + (irq_handler_t) irq_handler, 0,
405 + LIRC_DRIVER_NAME, (void*) 0);
409 + printk(KERN_ERR LIRC_DRIVER_NAME
410 + ": IRQ %d is busy\n",
411 + gpiochip->to_irq(gpiochip, gpio_in_pin));
414 + printk(KERN_ERR LIRC_DRIVER_NAME
415 + ": Bad irq number or handler\n");
418 + dprintk("Interrupt %d obtained\n",
419 + gpiochip->to_irq(gpiochip, gpio_in_pin));
423 + /* initialize pulse/space widths */
424 + init_timing_params(duty_cycle, freq);
426 + spin_lock_irqsave(&lock, flags);
428 + /* GPIO Pin Falling/Rising Edge Detect Enable */
429 + irqchip->irq_set_type(irqdata,
430 + IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING);
432 + /* unmask the irq */
433 + irqchip->irq_unmask(irqdata);
435 + spin_unlock_irqrestore(&lock, flags);
440 +static void set_use_dec(void *data)
442 + unsigned long flags;
444 + spin_lock_irqsave(&lock, flags);
446 + /* GPIO Pin Falling/Rising Edge Detect Disable */
447 + irqchip->irq_set_type(irqdata, 0);
448 + irqchip->irq_mask(irqdata);
450 + spin_unlock_irqrestore(&lock, flags);
452 + free_irq(gpiochip->to_irq(gpiochip, gpio_in_pin), (void *) 0);
454 + dprintk(KERN_INFO LIRC_DRIVER_NAME
455 + ": freed IRQ %d\n", gpiochip->to_irq(gpiochip, gpio_in_pin));
458 +static ssize_t lirc_write(struct file *file, const char *buf,
459 + size_t n, loff_t *ppos)
462 + unsigned long flags;
466 + count = n / sizeof(int);
467 + if (n % sizeof(int) || count % 2 == 0)
469 + wbuf = memdup_user(buf, n);
471 + return PTR_ERR(wbuf);
472 + spin_lock_irqsave(&lock, flags);
474 + for (i = 0; i < count; i++) {
476 + send_space(wbuf[i] - delta);
478 + delta = send_pulse(wbuf[i]);
480 + gpiochip->set(gpiochip, gpio_out_pin, invert);
482 + spin_unlock_irqrestore(&lock, flags);
487 +static long lirc_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
493 + case LIRC_GET_SEND_MODE:
494 + return -ENOIOCTLCMD;
497 + case LIRC_SET_SEND_MODE:
498 + result = get_user(value, (__u32 *) arg);
501 + /* only LIRC_MODE_PULSE supported */
502 + if (value != LIRC_MODE_PULSE)
506 + case LIRC_GET_LENGTH:
510 + case LIRC_SET_SEND_DUTY_CYCLE:
511 + dprintk("SET_SEND_DUTY_CYCLE\n");
512 + result = get_user(value, (__u32 *) arg);
515 + if (value <= 0 || value > 100)
517 + return init_timing_params(value, freq);
520 + case LIRC_SET_SEND_CARRIER:
521 + dprintk("SET_SEND_CARRIER\n");
522 + result = get_user(value, (__u32 *) arg);
525 + if (value > 500000 || value < 20000)
527 + return init_timing_params(duty_cycle, value);
531 + return lirc_dev_fop_ioctl(filep, cmd, arg);
536 +static const struct file_operations lirc_fops = {
537 + .owner = THIS_MODULE,
538 + .write = lirc_write,
539 + .unlocked_ioctl = lirc_ioctl,
540 + .read = lirc_dev_fop_read,
541 + .poll = lirc_dev_fop_poll,
542 + .open = lirc_dev_fop_open,
543 + .release = lirc_dev_fop_close,
544 + .llseek = no_llseek,
547 +static struct lirc_driver driver = {
548 + .name = LIRC_DRIVER_NAME,
553 + .add_to_buf = NULL,
555 + .set_use_inc = set_use_inc,
556 + .set_use_dec = set_use_dec,
557 + .fops = &lirc_fops,
559 + .owner = THIS_MODULE,
562 +static struct platform_driver lirc_rpi_driver = {
564 + .name = LIRC_DRIVER_NAME,
565 + .owner = THIS_MODULE,
569 +static int __init lirc_rpi_init(void)
573 + /* Init read buffer. */
574 + result = lirc_buffer_init(&rbuf, sizeof(int), RBUF_LEN);
578 + result = platform_driver_register(&lirc_rpi_driver);
580 + printk(KERN_ERR LIRC_DRIVER_NAME
581 + ": lirc register returned %d\n", result);
582 + goto exit_buffer_free;
585 + lirc_rpi_dev = platform_device_alloc(LIRC_DRIVER_NAME, 0);
586 + if (!lirc_rpi_dev) {
588 + goto exit_driver_unregister;
591 + result = platform_device_add(lirc_rpi_dev);
593 + goto exit_device_put;
598 + platform_device_put(lirc_rpi_dev);
600 + exit_driver_unregister:
601 + platform_driver_unregister(&lirc_rpi_driver);
604 + lirc_buffer_free(&rbuf);
609 +static void lirc_rpi_exit(void)
611 + gpio_free(gpio_out_pin);
612 + gpio_free(gpio_in_pin);
613 + platform_device_unregister(lirc_rpi_dev);
614 + platform_driver_unregister(&lirc_rpi_driver);
615 + lirc_buffer_free(&rbuf);
618 +static int __init lirc_rpi_init_module(void)
622 + result = lirc_rpi_init();
626 + /* check if the module received valid gpio pin numbers */
628 + if (gpio_in_pin != gpio_out_pin) {
629 + for(i = 0; (i < ARRAY_SIZE(valid_gpio_pins)) && (result != 2); i++) {
630 + if (gpio_in_pin == valid_gpio_pins[i] ||
631 + gpio_out_pin == valid_gpio_pins[i]) {
639 + printk(KERN_ERR LIRC_DRIVER_NAME
640 + ": invalid GPIO pin(s) specified!\n");
644 + driver.features = LIRC_CAN_SET_SEND_DUTY_CYCLE |
645 + LIRC_CAN_SET_SEND_CARRIER |
646 + LIRC_CAN_SEND_PULSE |
647 + LIRC_CAN_REC_MODE2;
649 + driver.dev = &lirc_rpi_dev->dev;
650 + driver.minor = lirc_register_driver(&driver);
652 + if (driver.minor < 0) {
653 + printk(KERN_ERR LIRC_DRIVER_NAME
654 + ": device registration failed with %d\n", result);
659 + printk(KERN_INFO LIRC_DRIVER_NAME ": driver registered!\n");
661 + result = init_port();
673 +static void __exit lirc_rpi_exit_module(void)
677 + lirc_unregister_driver(driver.minor);
678 + printk(KERN_INFO LIRC_DRIVER_NAME ": cleaned up module\n");
681 +module_init(lirc_rpi_init_module);
682 +module_exit(lirc_rpi_exit_module);
684 +MODULE_DESCRIPTION("Infra-red receiver and blaster driver for Raspberry Pi GPIO.");
685 +MODULE_AUTHOR("Aron Robert Szabo <aron@reon.hu>");
686 +MODULE_AUTHOR("Michael Bishop <cleverca22@gmail.com>");
687 +MODULE_LICENSE("GPL");
689 +module_param(gpio_out_pin, int, S_IRUGO);
690 +MODULE_PARM_DESC(gpio_out_pin, "GPIO output/transmitter pin number of the BCM"
691 + " processor. Valid pin numbers are: 0, 1, 4, 8, 7, 9, 10, 11,"
692 + " 14, 15, 17, 18, 21, 22, 23, 24, 25, default 17");
694 +module_param(gpio_in_pin, int, S_IRUGO);
695 +MODULE_PARM_DESC(gpio_in_pin, "GPIO input pin number of the BCM processor."
696 + " Valid pin numbers are: 0, 1, 4, 8, 7, 9, 10, 11, 14, 15,"
697 + " 17, 18, 21, 22, 23, 24, 25, default 18");
699 +module_param(sense, bool, S_IRUGO);
700 +MODULE_PARM_DESC(sense, "Override autodetection of IR receiver circuit"
701 + " (0 = active high, 1 = active low )");
703 +module_param(softcarrier, bool, S_IRUGO);
704 +MODULE_PARM_DESC(softcarrier, "Software carrier (0 = off, 1 = on, default on)");
706 +module_param(invert, bool, S_IRUGO);
707 +MODULE_PARM_DESC(invert, "Invert output (0 = off, 1 = on, default off");
709 +module_param(debug, bool, S_IRUGO | S_IWUSR);
710 +MODULE_PARM_DESC(debug, "Enable debugging messages");
711 --- a/drivers/staging/media/lirc/Makefile
712 +++ b/drivers/staging/media/lirc/Makefile
713 @@ -7,6 +7,7 @@ obj-$(CONFIG_LIRC_BT829) += lirc_bt829.o
714 obj-$(CONFIG_LIRC_IGORPLUGUSB) += lirc_igorplugusb.o
715 obj-$(CONFIG_LIRC_IMON) += lirc_imon.o
716 obj-$(CONFIG_LIRC_PARALLEL) += lirc_parallel.o
717 +obj-$(CONFIG_LIRC_RPI) += lirc_rpi.o
718 obj-$(CONFIG_LIRC_SASEM) += lirc_sasem.o
719 obj-$(CONFIG_LIRC_SERIAL) += lirc_serial.o
720 obj-$(CONFIG_LIRC_SIR) += lirc_sir.o