1 From b7496d2df33f324ee6854c600944b50a011d95e0 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 042/174] lirc: added support for RaspberryPi GPIO
7 drivers/staging/media/lirc/Kconfig | 6 +
8 drivers/staging/media/lirc/Makefile | 1 +
9 drivers/staging/media/lirc/lirc_rpi.c | 687 ++++++++++++++++++++++++++++++++++
10 3 files changed, 694 insertions(+)
11 create mode 100644 drivers/staging/media/lirc/lirc_rpi.c
13 --- a/drivers/staging/media/lirc/Kconfig
14 +++ b/drivers/staging/media/lirc/Kconfig
15 @@ -38,6 +38,12 @@ config LIRC_PARALLEL
17 Driver for Homebrew Parallel Port Receivers
20 + tristate "Homebrew GPIO Port Receiver/Transmitter for the RaspberryPi"
23 + Driver for Homebrew GPIO Port Receiver/Transmitter for the RaspberryPi
26 tristate "Sasem USB IR Remote"
27 depends on LIRC && USB
28 --- a/drivers/staging/media/lirc/Makefile
29 +++ b/drivers/staging/media/lirc/Makefile
30 @@ -7,6 +7,7 @@ obj-$(CONFIG_LIRC_BT829) += lirc_bt829.o
31 obj-$(CONFIG_LIRC_IGORPLUGUSB) += lirc_igorplugusb.o
32 obj-$(CONFIG_LIRC_IMON) += lirc_imon.o
33 obj-$(CONFIG_LIRC_PARALLEL) += lirc_parallel.o
34 +obj-$(CONFIG_LIRC_RPI) += lirc_rpi.o
35 obj-$(CONFIG_LIRC_SASEM) += lirc_sasem.o
36 obj-$(CONFIG_LIRC_SERIAL) += lirc_serial.o
37 obj-$(CONFIG_LIRC_SIR) += lirc_sir.o
39 +++ b/drivers/staging/media/lirc/lirc_rpi.c
44 + * lirc_rpi - Device driver that records pulse- and pause-lengths
45 + * (space-lengths) (just like the lirc_serial driver does)
46 + * between GPIO interrupt events on the Raspberry Pi.
47 + * Lots of code has been taken from the lirc_serial module,
48 + * so I would like say thanks to the authors.
50 + * Copyright (C) 2012 Aron Robert Szabo <aron@reon.hu>,
51 + * Michael Bishop <cleverca22@gmail.com>
52 + * This program is free software; you can redistribute it and/or modify
53 + * it under the terms of the GNU General Public License as published by
54 + * the Free Software Foundation; either version 2 of the License, or
55 + * (at your option) any later version.
57 + * This program is distributed in the hope that it will be useful,
58 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
59 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
60 + * GNU General Public License for more details.
62 + * You should have received a copy of the GNU General Public License
63 + * along with this program; if not, write to the Free Software
64 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
67 +#include <linux/module.h>
68 +#include <linux/errno.h>
69 +#include <linux/interrupt.h>
70 +#include <linux/sched.h>
71 +#include <linux/kernel.h>
72 +#include <linux/time.h>
73 +#include <linux/string.h>
74 +#include <linux/delay.h>
75 +#include <linux/platform_device.h>
76 +#include <linux/irq.h>
77 +#include <linux/spinlock.h>
78 +#include <media/lirc.h>
79 +#include <media/lirc_dev.h>
80 +#include <linux/gpio.h>
82 +#define LIRC_DRIVER_NAME "lirc_rpi"
84 +#define LIRC_TRANSMITTER_LATENCY 256
86 +#ifndef MAX_UDELAY_MS
87 +#define MAX_UDELAY_US 5000
89 +#define MAX_UDELAY_US (MAX_UDELAY_MS*1000)
92 +#define dprintk(fmt, args...) \
95 + printk(KERN_DEBUG LIRC_DRIVER_NAME ": " \
99 +/* module parameters */
101 +/* set the default GPIO input pin */
102 +static int gpio_in_pin = 18;
103 +/* set the default GPIO output pin */
104 +static int gpio_out_pin = 17;
105 +/* enable debugging messages */
107 +/* -1 = auto, 0 = active high, 1 = active low */
108 +static int sense = -1;
109 +/* use softcarrier by default */
110 +static int softcarrier = 1;
112 +struct gpio_chip *gpiochip;
113 +struct irq_chip *irqchip;
114 +struct irq_data *irqdata;
116 +/* forward declarations */
117 +static long send_pulse(unsigned long length);
118 +static void send_space(long length);
119 +static void lirc_rpi_exit(void);
121 +int valid_gpio_pins[] = { 0, 1, 4, 8, 7, 9, 10, 11, 14, 15, 17, 18, 21, 22, 23,
124 +static struct platform_device *lirc_rpi_dev;
125 +static struct timeval lasttv = { 0, 0 };
126 +static struct lirc_buffer rbuf;
127 +static spinlock_t lock;
129 +/* initialized/set in init_timing_params() */
130 +static unsigned int freq = 38000;
131 +static unsigned int duty_cycle = 50;
132 +static unsigned long period;
133 +static unsigned long pulse_width;
134 +static unsigned long space_width;
136 +static void safe_udelay(unsigned long usecs)
138 + while (usecs > MAX_UDELAY_US) {
139 + udelay(MAX_UDELAY_US);
140 + usecs -= MAX_UDELAY_US;
145 +static int init_timing_params(unsigned int new_duty_cycle,
146 + unsigned int new_freq)
149 + * period, pulse/space width are kept with 8 binary places -
150 + * IE multiplied by 256.
152 + if (256 * 1000000L / new_freq * new_duty_cycle / 100 <=
153 + LIRC_TRANSMITTER_LATENCY)
155 + if (256 * 1000000L / new_freq * (100 - new_duty_cycle) / 100 <=
156 + LIRC_TRANSMITTER_LATENCY)
158 + duty_cycle = new_duty_cycle;
160 + period = 256 * 1000000L / freq;
161 + pulse_width = period * duty_cycle / 100;
162 + space_width = period - pulse_width;
163 + dprintk("in init_timing_params, freq=%d pulse=%ld, "
164 + "space=%ld\n", freq, pulse_width, space_width);
168 +static long send_pulse_softcarrier(unsigned long length)
171 + unsigned long actual, target, d;
175 + actual = 0; target = 0; flag = 0;
176 + while (actual < length) {
178 + gpiochip->set(gpiochip, gpio_out_pin, 0);
179 + target += space_width;
181 + gpiochip->set(gpiochip, gpio_out_pin, 1);
182 + target += pulse_width;
184 + d = (target - actual -
185 + LIRC_TRANSMITTER_LATENCY + 128) >> 8;
187 + * Note - we've checked in ioctl that the pulse/space
188 + * widths are big enough so that d is > 0
191 + actual += (d << 8) + LIRC_TRANSMITTER_LATENCY;
194 + return (actual-length) >> 8;
197 +static long send_pulse(unsigned long length)
203 + return send_pulse_softcarrier(length);
205 + gpiochip->set(gpiochip, gpio_out_pin, 1);
206 + safe_udelay(length);
211 +static void send_space(long length)
213 + gpiochip->set(gpiochip, gpio_out_pin, 0);
216 + safe_udelay(length);
219 +static void rbwrite(int l)
221 + if (lirc_buffer_full(&rbuf)) {
222 + /* no new signals will be accepted */
223 + dprintk("Buffer overrun\n");
226 + lirc_buffer_write(&rbuf, (void *)&l);
229 +static void frbwrite(int l)
231 + /* simple noise filter */
232 + static int pulse, space;
233 + static unsigned int ptr;
235 + if (ptr > 0 && (l & PULSE_BIT)) {
236 + pulse += l & PULSE_MASK;
239 + rbwrite(pulse | PULSE_BIT);
245 + if (!(l & PULSE_BIT)) {
255 + if (space > PULSE_MASK)
256 + space = PULSE_MASK;
258 + if (space > PULSE_MASK)
259 + space = PULSE_MASK;
264 + rbwrite(pulse | PULSE_BIT);
272 +static irqreturn_t irq_handler(int i, void *blah, struct pt_regs *regs)
279 + /* use the GPIO signal level */
280 + signal = gpiochip->get(gpiochip, gpio_in_pin);
282 + /* unmask the irq */
283 + irqchip->irq_unmask(irqdata);
286 + /* get current time */
287 + do_gettimeofday(&tv);
289 + /* calc time since last interrupt in microseconds */
290 + deltv = tv.tv_sec-lasttv.tv_sec;
291 + if (tv.tv_sec < lasttv.tv_sec ||
292 + (tv.tv_sec == lasttv.tv_sec &&
293 + tv.tv_usec < lasttv.tv_usec)) {
294 + printk(KERN_WARNING LIRC_DRIVER_NAME
295 + ": AIEEEE: your clock just jumped backwards\n");
296 + printk(KERN_WARNING LIRC_DRIVER_NAME
297 + ": %d %d %lx %lx %lx %lx\n", signal, sense,
298 + tv.tv_sec, lasttv.tv_sec,
299 + tv.tv_usec, lasttv.tv_usec);
301 + } else if (deltv > 15) {
302 + data = PULSE_MASK; /* really long time */
303 + if (!(signal^sense)) {
305 + printk(KERN_WARNING LIRC_DRIVER_NAME
306 + ": AIEEEE: %d %d %lx %lx %lx %lx\n",
307 + signal, sense, tv.tv_sec, lasttv.tv_sec,
308 + tv.tv_usec, lasttv.tv_usec);
310 + * detecting pulse while this
313 + sense = sense ? 0 : 1;
316 + data = (int) (deltv*1000000 +
317 + (tv.tv_usec - lasttv.tv_usec));
319 + frbwrite(signal^sense ? data : (data|PULSE_BIT));
321 + wake_up_interruptible(&rbuf.wait_poll);
324 + return IRQ_HANDLED;
327 +static int is_right_chip(struct gpio_chip *chip, void *data)
329 + dprintk("is_right_chip %s %d\n", chip->label, strcmp(data, chip->label));
331 + if (strcmp(data, chip->label) == 0)
336 +static int init_port(void)
338 + int i, nlow, nhigh, ret, irq;
340 + gpiochip = gpiochip_find("bcm2708_gpio", is_right_chip);
345 + if (gpio_request(gpio_out_pin, LIRC_DRIVER_NAME " ir/out")) {
346 + printk(KERN_ALERT LIRC_DRIVER_NAME
347 + ": cant claim gpio pin %d\n", gpio_out_pin);
349 + goto exit_init_port;
352 + if (gpio_request(gpio_in_pin, LIRC_DRIVER_NAME " ir/in")) {
353 + printk(KERN_ALERT LIRC_DRIVER_NAME
354 + ": cant claim gpio pin %d\n", gpio_in_pin);
356 + goto exit_gpio_free_out_pin;
359 + gpiochip->direction_input(gpiochip, gpio_in_pin);
360 + gpiochip->direction_output(gpiochip, gpio_out_pin, 1);
361 + gpiochip->set(gpiochip, gpio_out_pin, 0);
363 + irq = gpiochip->to_irq(gpiochip, gpio_in_pin);
364 + dprintk("to_irq %d\n", irq);
365 + irqdata = irq_get_irq_data(irq);
367 + if (irqdata && irqdata->chip) {
368 + irqchip = irqdata->chip;
371 + goto exit_gpio_free_in_pin;
374 + /* if pin is high, then this must be an active low receiver. */
376 + /* wait 1/2 sec for the power supply */
380 + * probe 9 times every 0.04s, collect "votes" for
385 + for (i = 0; i < 9; i++) {
386 + if (gpiochip->get(gpiochip, gpio_in_pin))
392 + sense = (nlow >= nhigh ? 1 : 0);
393 + printk(KERN_INFO LIRC_DRIVER_NAME
394 + ": auto-detected active %s receiver on GPIO pin %d\n",
395 + sense ? "low" : "high", gpio_in_pin);
397 + printk(KERN_INFO LIRC_DRIVER_NAME
398 + ": manually using active %s receiver on GPIO pin %d\n",
399 + sense ? "low" : "high", gpio_in_pin);
404 + exit_gpio_free_in_pin:
405 + gpio_free(gpio_in_pin);
407 + exit_gpio_free_out_pin:
408 + gpio_free(gpio_out_pin);
414 +// called when the character device is opened
415 +static int set_use_inc(void *data)
418 + unsigned long flags;
420 + /* initialize timestamp */
421 + do_gettimeofday(&lasttv);
423 + result = request_irq(gpiochip->to_irq(gpiochip, gpio_in_pin),
424 + (irq_handler_t) irq_handler, 0,
425 + LIRC_DRIVER_NAME, (void*) 0);
429 + printk(KERN_ERR LIRC_DRIVER_NAME
430 + ": IRQ %d is busy\n",
431 + gpiochip->to_irq(gpiochip, gpio_in_pin));
434 + printk(KERN_ERR LIRC_DRIVER_NAME
435 + ": Bad irq number or handler\n");
438 + dprintk("Interrupt %d obtained\n",
439 + gpiochip->to_irq(gpiochip, gpio_in_pin));
443 + /* initialize pulse/space widths */
444 + init_timing_params(duty_cycle, freq);
446 + spin_lock_irqsave(&lock, flags);
448 + /* GPIO Pin Falling/Rising Edge Detect Enable */
449 + irqchip->irq_set_type(irqdata,
450 + IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING);
452 + /* unmask the irq */
453 + irqchip->irq_unmask(irqdata);
455 + spin_unlock_irqrestore(&lock, flags);
460 +static void set_use_dec(void *data)
462 + unsigned long flags;
464 + spin_lock_irqsave(&lock, flags);
466 + /* GPIO Pin Falling/Rising Edge Detect Disable */
467 + irqchip->irq_set_type(irqdata, 0);
468 + irqchip->irq_mask(irqdata);
470 + spin_unlock_irqrestore(&lock, flags);
472 + free_irq(gpiochip->to_irq(gpiochip, gpio_in_pin), (void *) 0);
474 + dprintk(KERN_INFO LIRC_DRIVER_NAME
475 + ": freed IRQ %d\n", gpiochip->to_irq(gpiochip, gpio_in_pin));
478 +static ssize_t lirc_write(struct file *file, const char *buf,
479 + size_t n, loff_t *ppos)
482 + unsigned long flags;
486 + count = n / sizeof(int);
487 + if (n % sizeof(int) || count % 2 == 0)
489 + wbuf = memdup_user(buf, n);
491 + return PTR_ERR(wbuf);
492 + spin_lock_irqsave(&lock, flags);
494 + for (i = 0; i < count; i++) {
496 + send_space(wbuf[i] - delta);
498 + delta = send_pulse(wbuf[i]);
500 + gpiochip->set(gpiochip, gpio_out_pin, 0);
502 + spin_unlock_irqrestore(&lock, flags);
507 +static long lirc_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
513 + case LIRC_GET_SEND_MODE:
514 + return -ENOIOCTLCMD;
517 + case LIRC_SET_SEND_MODE:
518 + result = get_user(value, (__u32 *) arg);
521 + /* only LIRC_MODE_PULSE supported */
522 + if (value != LIRC_MODE_PULSE)
526 + case LIRC_GET_LENGTH:
530 + case LIRC_SET_SEND_DUTY_CYCLE:
531 + dprintk("SET_SEND_DUTY_CYCLE\n");
532 + result = get_user(value, (__u32 *) arg);
535 + if (value <= 0 || value > 100)
537 + return init_timing_params(value, freq);
540 + case LIRC_SET_SEND_CARRIER:
541 + dprintk("SET_SEND_CARRIER\n");
542 + result = get_user(value, (__u32 *) arg);
545 + if (value > 500000 || value < 20000)
547 + return init_timing_params(duty_cycle, value);
551 + return lirc_dev_fop_ioctl(filep, cmd, arg);
556 +static const struct file_operations lirc_fops = {
557 + .owner = THIS_MODULE,
558 + .write = lirc_write,
559 + .unlocked_ioctl = lirc_ioctl,
560 + .read = lirc_dev_fop_read,
561 + .poll = lirc_dev_fop_poll,
562 + .open = lirc_dev_fop_open,
563 + .release = lirc_dev_fop_close,
564 + .llseek = no_llseek,
567 +static struct lirc_driver driver = {
568 + .name = LIRC_DRIVER_NAME,
573 + .add_to_buf = NULL,
575 + .set_use_inc = set_use_inc,
576 + .set_use_dec = set_use_dec,
577 + .fops = &lirc_fops,
579 + .owner = THIS_MODULE,
582 +static struct platform_driver lirc_rpi_driver = {
584 + .name = LIRC_DRIVER_NAME,
585 + .owner = THIS_MODULE,
589 +static int __init lirc_rpi_init(void)
593 + /* Init read buffer. */
594 + result = lirc_buffer_init(&rbuf, sizeof(int), RBUF_LEN);
598 + result = platform_driver_register(&lirc_rpi_driver);
600 + printk(KERN_ERR LIRC_DRIVER_NAME
601 + ": lirc register returned %d\n", result);
602 + goto exit_buffer_free;
605 + lirc_rpi_dev = platform_device_alloc(LIRC_DRIVER_NAME, 0);
606 + if (!lirc_rpi_dev) {
608 + goto exit_driver_unregister;
611 + result = platform_device_add(lirc_rpi_dev);
613 + goto exit_device_put;
618 + platform_device_put(lirc_rpi_dev);
620 + exit_driver_unregister:
621 + platform_driver_unregister(&lirc_rpi_driver);
624 + lirc_buffer_free(&rbuf);
629 +static void lirc_rpi_exit(void)
631 + gpio_free(gpio_out_pin);
632 + gpio_free(gpio_in_pin);
633 + platform_device_unregister(lirc_rpi_dev);
634 + platform_driver_unregister(&lirc_rpi_driver);
635 + lirc_buffer_free(&rbuf);
638 +static int __init lirc_rpi_init_module(void)
642 + result = lirc_rpi_init();
646 + /* check if the module received valid gpio pin numbers */
648 + if (gpio_in_pin != gpio_out_pin) {
649 + for(i = 0; (i < ARRAY_SIZE(valid_gpio_pins)) && (result != 2); i++) {
650 + if (gpio_in_pin == valid_gpio_pins[i] ||
651 + gpio_out_pin == valid_gpio_pins[i]) {
659 + printk(KERN_ERR LIRC_DRIVER_NAME
660 + ": invalid GPIO pin(s) specified!\n");
664 + driver.features = LIRC_CAN_SET_SEND_DUTY_CYCLE |
665 + LIRC_CAN_SET_SEND_CARRIER |
666 + LIRC_CAN_SEND_PULSE |
667 + LIRC_CAN_REC_MODE2;
669 + driver.dev = &lirc_rpi_dev->dev;
670 + driver.minor = lirc_register_driver(&driver);
672 + if (driver.minor < 0) {
673 + printk(KERN_ERR LIRC_DRIVER_NAME
674 + ": device registration failed with %d\n", result);
679 + printk(KERN_INFO LIRC_DRIVER_NAME ": driver registered!\n");
681 + result = init_port();
693 +static void __exit lirc_rpi_exit_module(void)
697 + lirc_unregister_driver(driver.minor);
698 + printk(KERN_INFO LIRC_DRIVER_NAME ": cleaned up module\n");
701 +module_init(lirc_rpi_init_module);
702 +module_exit(lirc_rpi_exit_module);
704 +MODULE_DESCRIPTION("Infra-red receiver and blaster driver for Raspberry Pi GPIO.");
705 +MODULE_AUTHOR("Aron Robert Szabo <aron@reon.hu>");
706 +MODULE_AUTHOR("Michael Bishop <cleverca22@gmail.com>");
707 +MODULE_LICENSE("GPL");
709 +module_param(gpio_out_pin, int, S_IRUGO);
710 +MODULE_PARM_DESC(gpio_out_pin, "GPIO output/transmitter pin number of the BCM"
711 + " processor. Valid pin numbers are: 0, 1, 4, 8, 7, 9, 10, 11,"
712 + " 14, 15, 17, 18, 21, 22, 23, 24, 25, default 17");
714 +module_param(gpio_in_pin, int, S_IRUGO);
715 +MODULE_PARM_DESC(gpio_in_pin, "GPIO input pin number of the BCM processor."
716 + " Valid pin numbers are: 0, 1, 4, 8, 7, 9, 10, 11, 14, 15,"
717 + " 17, 18, 21, 22, 23, 24, 25, default 18");
719 +module_param(sense, bool, S_IRUGO);
720 +MODULE_PARM_DESC(sense, "Override autodetection of IR receiver circuit"
721 + " (0 = active high, 1 = active low )");
723 +module_param(softcarrier, bool, S_IRUGO);
724 +MODULE_PARM_DESC(softcarrier, "Software carrier (0 = off, 1 = on, default on)");
726 +module_param(debug, bool, S_IRUGO | S_IWUSR);
727 +MODULE_PARM_DESC(debug, "Enable debugging messages");