1 From 04439327ad7c2afd72b6fe8c43c922b892dff7c3 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 12/54] lirc: added support for RaspberryPi GPIO
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
9 drivers/staging/media/lirc/Kconfig | 6 +
10 drivers/staging/media/lirc/Makefile | 1 +
11 drivers/staging/media/lirc/lirc_rpi.c | 695 ++++++++++++++++++++++++++++++++++
12 3 files changed, 702 insertions(+)
13 create mode 100644 drivers/staging/media/lirc/lirc_rpi.c
15 --- a/drivers/staging/media/lirc/Kconfig
16 +++ b/drivers/staging/media/lirc/Kconfig
17 @@ -38,6 +38,12 @@ config LIRC_PARALLEL
19 Driver for Homebrew Parallel Port Receivers
22 + tristate "Homebrew GPIO Port Receiver/Transmitter for the RaspberryPi"
25 + Driver for Homebrew GPIO Port Receiver/Transmitter for the RaspberryPi
28 tristate "Sasem USB IR Remote"
29 depends on LIRC && USB
30 --- a/drivers/staging/media/lirc/Makefile
31 +++ b/drivers/staging/media/lirc/Makefile
32 @@ -7,6 +7,7 @@ obj-$(CONFIG_LIRC_BT829) += lirc_bt829.o
33 obj-$(CONFIG_LIRC_IGORPLUGUSB) += lirc_igorplugusb.o
34 obj-$(CONFIG_LIRC_IMON) += lirc_imon.o
35 obj-$(CONFIG_LIRC_PARALLEL) += lirc_parallel.o
36 +obj-$(CONFIG_LIRC_RPI) += lirc_rpi.o
37 obj-$(CONFIG_LIRC_SASEM) += lirc_sasem.o
38 obj-$(CONFIG_LIRC_SERIAL) += lirc_serial.o
39 obj-$(CONFIG_LIRC_SIR) += lirc_sir.o
41 +++ b/drivers/staging/media/lirc/lirc_rpi.c
46 + * lirc_rpi - Device driver that records pulse- and pause-lengths
47 + * (space-lengths) (just like the lirc_serial driver does)
48 + * between GPIO interrupt events on the Raspberry Pi.
49 + * Lots of code has been taken from the lirc_serial module,
50 + * so I would like say thanks to the authors.
52 + * Copyright (C) 2012 Aron Robert Szabo <aron@reon.hu>,
53 + * Michael Bishop <cleverca22@gmail.com>
54 + * This program is free software; you can redistribute it and/or modify
55 + * it under the terms of the GNU General Public License as published by
56 + * the Free Software Foundation; either version 2 of the License, or
57 + * (at your option) any later version.
59 + * This program is distributed in the hope that it will be useful,
60 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
61 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
62 + * GNU General Public License for more details.
64 + * You should have received a copy of the GNU General Public License
65 + * along with this program; if not, write to the Free Software
66 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
69 +#include <linux/module.h>
70 +#include <linux/errno.h>
71 +#include <linux/interrupt.h>
72 +#include <linux/sched.h>
73 +#include <linux/kernel.h>
74 +#include <linux/time.h>
75 +#include <linux/timex.h>
76 +#include <linux/string.h>
77 +#include <linux/delay.h>
78 +#include <linux/platform_device.h>
79 +#include <linux/irq.h>
80 +#include <linux/spinlock.h>
81 +#include <media/lirc.h>
82 +#include <media/lirc_dev.h>
83 +#include <linux/gpio.h>
85 +#define LIRC_DRIVER_NAME "lirc_rpi"
87 +#define LIRC_TRANSMITTER_LATENCY 50
89 +#ifndef MAX_UDELAY_MS
90 +#define MAX_UDELAY_US 5000
92 +#define MAX_UDELAY_US (MAX_UDELAY_MS*1000)
95 +#define dprintk(fmt, args...) \
98 + printk(KERN_DEBUG LIRC_DRIVER_NAME ": " \
102 +/* module parameters */
104 +/* set the default GPIO input pin */
105 +static int gpio_in_pin = 18;
106 +/* set the default GPIO output pin */
107 +static int gpio_out_pin = 17;
108 +/* enable debugging messages */
110 +/* -1 = auto, 0 = active high, 1 = active low */
111 +static int sense = -1;
112 +/* use softcarrier by default */
113 +static bool softcarrier = 1;
114 +/* 0 = do not invert output, 1 = invert output */
115 +static bool invert = 0;
117 +struct gpio_chip *gpiochip;
118 +struct irq_chip *irqchip;
119 +struct irq_data *irqdata;
121 +/* forward declarations */
122 +static long send_pulse(unsigned long length);
123 +static void send_space(long length);
124 +static void lirc_rpi_exit(void);
126 +int valid_gpio_pins[] = { 0, 1, 2, 3, 4, 7, 8, 9, 10, 11, 14, 15, 17, 18, 21,
127 + 22, 23, 24, 25 ,27, 28, 29, 30, 31 };
129 +static struct platform_device *lirc_rpi_dev;
130 +static struct timeval lasttv = { 0, 0 };
131 +static struct lirc_buffer rbuf;
132 +static spinlock_t lock;
134 +/* initialized/set in init_timing_params() */
135 +static unsigned int freq = 38000;
136 +static unsigned int duty_cycle = 50;
137 +static unsigned long period;
138 +static unsigned long pulse_width;
139 +static unsigned long space_width;
141 +static void safe_udelay(unsigned long usecs)
143 + while (usecs > MAX_UDELAY_US) {
144 + udelay(MAX_UDELAY_US);
145 + usecs -= MAX_UDELAY_US;
150 +static int init_timing_params(unsigned int new_duty_cycle,
151 + unsigned int new_freq)
153 + if (1000 * 1000000L / new_freq * new_duty_cycle / 100 <=
154 + LIRC_TRANSMITTER_LATENCY)
156 + if (1000 * 1000000L / new_freq * (100 - new_duty_cycle) / 100 <=
157 + LIRC_TRANSMITTER_LATENCY)
159 + duty_cycle = new_duty_cycle;
161 + period = 1000 * 1000000L / freq;
162 + pulse_width = period * duty_cycle / 100;
163 + space_width = period - pulse_width;
164 + dprintk("in init_timing_params, freq=%d pulse=%ld, "
165 + "space=%ld\n", freq, pulse_width, space_width);
169 +static long send_pulse_softcarrier(unsigned long length)
172 + unsigned long actual, target;
173 + unsigned long actual_us, initial_us, target_us;
177 + actual = 0; target = 0; flag = 0;
178 + read_current_timer(&actual_us);
180 + while (actual < length) {
182 + gpiochip->set(gpiochip, gpio_out_pin, invert);
183 + target += space_width;
185 + gpiochip->set(gpiochip, gpio_out_pin, !invert);
186 + target += pulse_width;
188 + initial_us = actual_us;
189 + target_us = actual_us + (target - actual) / 1000;
191 + * Note - we've checked in ioctl that the pulse/space
192 + * widths are big enough so that d is > 0
194 + if ((int)(target_us - actual_us) > 0)
195 + udelay(target_us - actual_us);
196 + read_current_timer(&actual_us);
197 + actual += (actual_us - initial_us) * 1000;
200 + return (actual-length) / 1000;
203 +static long send_pulse(unsigned long length)
209 + return send_pulse_softcarrier(length);
211 + gpiochip->set(gpiochip, gpio_out_pin, !invert);
212 + safe_udelay(length);
217 +static void send_space(long length)
219 + gpiochip->set(gpiochip, gpio_out_pin, invert);
222 + safe_udelay(length);
225 +static void rbwrite(int l)
227 + if (lirc_buffer_full(&rbuf)) {
228 + /* no new signals will be accepted */
229 + dprintk("Buffer overrun\n");
232 + lirc_buffer_write(&rbuf, (void *)&l);
235 +static void frbwrite(int l)
237 + /* simple noise filter */
238 + static int pulse, space;
239 + static unsigned int ptr;
241 + if (ptr > 0 && (l & PULSE_BIT)) {
242 + pulse += l & PULSE_MASK;
245 + rbwrite(pulse | PULSE_BIT);
251 + if (!(l & PULSE_BIT)) {
261 + if (space > PULSE_MASK)
262 + space = PULSE_MASK;
264 + if (space > PULSE_MASK)
265 + space = PULSE_MASK;
270 + rbwrite(pulse | PULSE_BIT);
278 +static irqreturn_t irq_handler(int i, void *blah, struct pt_regs *regs)
285 + /* use the GPIO signal level */
286 + signal = gpiochip->get(gpiochip, gpio_in_pin);
288 + /* unmask the irq */
289 + irqchip->irq_unmask(irqdata);
292 + /* get current time */
293 + do_gettimeofday(&tv);
295 + /* calc time since last interrupt in microseconds */
296 + deltv = tv.tv_sec-lasttv.tv_sec;
297 + if (tv.tv_sec < lasttv.tv_sec ||
298 + (tv.tv_sec == lasttv.tv_sec &&
299 + tv.tv_usec < lasttv.tv_usec)) {
300 + printk(KERN_WARNING LIRC_DRIVER_NAME
301 + ": AIEEEE: your clock just jumped backwards\n");
302 + printk(KERN_WARNING LIRC_DRIVER_NAME
303 + ": %d %d %lx %lx %lx %lx\n", signal, sense,
304 + tv.tv_sec, lasttv.tv_sec,
305 + tv.tv_usec, lasttv.tv_usec);
307 + } else if (deltv > 15) {
308 + data = PULSE_MASK; /* really long time */
309 + if (!(signal^sense)) {
311 + printk(KERN_WARNING LIRC_DRIVER_NAME
312 + ": AIEEEE: %d %d %lx %lx %lx %lx\n",
313 + signal, sense, tv.tv_sec, lasttv.tv_sec,
314 + tv.tv_usec, lasttv.tv_usec);
316 + * detecting pulse while this
319 + sense = sense ? 0 : 1;
322 + data = (int) (deltv*1000000 +
323 + (tv.tv_usec - lasttv.tv_usec));
325 + frbwrite(signal^sense ? data : (data|PULSE_BIT));
327 + wake_up_interruptible(&rbuf.wait_poll);
330 + return IRQ_HANDLED;
333 +static int is_right_chip(struct gpio_chip *chip, void *data)
335 + dprintk("is_right_chip %s %d\n", chip->label, strcmp(data, chip->label));
337 + if (strcmp(data, chip->label) == 0)
342 +static int init_port(void)
344 + int i, nlow, nhigh, ret, irq;
346 + gpiochip = gpiochip_find("bcm2708_gpio", is_right_chip);
351 + if (gpio_request(gpio_out_pin, LIRC_DRIVER_NAME " ir/out")) {
352 + printk(KERN_ALERT LIRC_DRIVER_NAME
353 + ": cant claim gpio pin %d\n", gpio_out_pin);
355 + goto exit_init_port;
358 + if (gpio_request(gpio_in_pin, LIRC_DRIVER_NAME " ir/in")) {
359 + printk(KERN_ALERT LIRC_DRIVER_NAME
360 + ": cant claim gpio pin %d\n", gpio_in_pin);
362 + goto exit_gpio_free_out_pin;
365 + gpiochip->direction_input(gpiochip, gpio_in_pin);
366 + gpiochip->direction_output(gpiochip, gpio_out_pin, 1);
367 + gpiochip->set(gpiochip, gpio_out_pin, invert);
369 + irq = gpiochip->to_irq(gpiochip, gpio_in_pin);
370 + dprintk("to_irq %d\n", irq);
371 + irqdata = irq_get_irq_data(irq);
373 + if (irqdata && irqdata->chip) {
374 + irqchip = irqdata->chip;
377 + goto exit_gpio_free_in_pin;
380 + /* if pin is high, then this must be an active low receiver. */
382 + /* wait 1/2 sec for the power supply */
386 + * probe 9 times every 0.04s, collect "votes" for
391 + for (i = 0; i < 9; i++) {
392 + if (gpiochip->get(gpiochip, gpio_in_pin))
398 + sense = (nlow >= nhigh ? 1 : 0);
399 + printk(KERN_INFO LIRC_DRIVER_NAME
400 + ": auto-detected active %s receiver on GPIO pin %d\n",
401 + sense ? "low" : "high", gpio_in_pin);
403 + printk(KERN_INFO LIRC_DRIVER_NAME
404 + ": manually using active %s receiver on GPIO pin %d\n",
405 + sense ? "low" : "high", gpio_in_pin);
410 + exit_gpio_free_in_pin:
411 + gpio_free(gpio_in_pin);
413 + exit_gpio_free_out_pin:
414 + gpio_free(gpio_out_pin);
420 +// called when the character device is opened
421 +static int set_use_inc(void *data)
424 + unsigned long flags;
426 + /* initialize timestamp */
427 + do_gettimeofday(&lasttv);
429 + result = request_irq(gpiochip->to_irq(gpiochip, gpio_in_pin),
430 + (irq_handler_t) irq_handler, 0,
431 + LIRC_DRIVER_NAME, (void*) 0);
435 + printk(KERN_ERR LIRC_DRIVER_NAME
436 + ": IRQ %d is busy\n",
437 + gpiochip->to_irq(gpiochip, gpio_in_pin));
440 + printk(KERN_ERR LIRC_DRIVER_NAME
441 + ": Bad irq number or handler\n");
444 + dprintk("Interrupt %d obtained\n",
445 + gpiochip->to_irq(gpiochip, gpio_in_pin));
449 + /* initialize pulse/space widths */
450 + init_timing_params(duty_cycle, freq);
452 + spin_lock_irqsave(&lock, flags);
454 + /* GPIO Pin Falling/Rising Edge Detect Enable */
455 + irqchip->irq_set_type(irqdata,
456 + IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING);
458 + /* unmask the irq */
459 + irqchip->irq_unmask(irqdata);
461 + spin_unlock_irqrestore(&lock, flags);
466 +static void set_use_dec(void *data)
468 + unsigned long flags;
470 + spin_lock_irqsave(&lock, flags);
472 + /* GPIO Pin Falling/Rising Edge Detect Disable */
473 + irqchip->irq_set_type(irqdata, 0);
474 + irqchip->irq_mask(irqdata);
476 + spin_unlock_irqrestore(&lock, flags);
478 + free_irq(gpiochip->to_irq(gpiochip, gpio_in_pin), (void *) 0);
480 + dprintk(KERN_INFO LIRC_DRIVER_NAME
481 + ": freed IRQ %d\n", gpiochip->to_irq(gpiochip, gpio_in_pin));
484 +static ssize_t lirc_write(struct file *file, const char *buf,
485 + size_t n, loff_t *ppos)
488 + unsigned long flags;
492 + count = n / sizeof(int);
493 + if (n % sizeof(int) || count % 2 == 0)
495 + wbuf = memdup_user(buf, n);
497 + return PTR_ERR(wbuf);
498 + spin_lock_irqsave(&lock, flags);
500 + for (i = 0; i < count; i++) {
502 + send_space(wbuf[i] - delta);
504 + delta = send_pulse(wbuf[i]);
506 + gpiochip->set(gpiochip, gpio_out_pin, invert);
508 + spin_unlock_irqrestore(&lock, flags);
513 +static long lirc_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
519 + case LIRC_GET_SEND_MODE:
520 + return -ENOIOCTLCMD;
523 + case LIRC_SET_SEND_MODE:
524 + result = get_user(value, (__u32 *) arg);
527 + /* only LIRC_MODE_PULSE supported */
528 + if (value != LIRC_MODE_PULSE)
532 + case LIRC_GET_LENGTH:
536 + case LIRC_SET_SEND_DUTY_CYCLE:
537 + dprintk("SET_SEND_DUTY_CYCLE\n");
538 + result = get_user(value, (__u32 *) arg);
541 + if (value <= 0 || value > 100)
543 + return init_timing_params(value, freq);
546 + case LIRC_SET_SEND_CARRIER:
547 + dprintk("SET_SEND_CARRIER\n");
548 + result = get_user(value, (__u32 *) arg);
551 + if (value > 500000 || value < 20000)
553 + return init_timing_params(duty_cycle, value);
557 + return lirc_dev_fop_ioctl(filep, cmd, arg);
562 +static const struct file_operations lirc_fops = {
563 + .owner = THIS_MODULE,
564 + .write = lirc_write,
565 + .unlocked_ioctl = lirc_ioctl,
566 + .read = lirc_dev_fop_read,
567 + .poll = lirc_dev_fop_poll,
568 + .open = lirc_dev_fop_open,
569 + .release = lirc_dev_fop_close,
570 + .llseek = no_llseek,
573 +static struct lirc_driver driver = {
574 + .name = LIRC_DRIVER_NAME,
579 + .add_to_buf = NULL,
581 + .set_use_inc = set_use_inc,
582 + .set_use_dec = set_use_dec,
583 + .fops = &lirc_fops,
585 + .owner = THIS_MODULE,
588 +static struct platform_driver lirc_rpi_driver = {
590 + .name = LIRC_DRIVER_NAME,
591 + .owner = THIS_MODULE,
595 +static int __init lirc_rpi_init(void)
599 + /* Init read buffer. */
600 + result = lirc_buffer_init(&rbuf, sizeof(int), RBUF_LEN);
604 + result = platform_driver_register(&lirc_rpi_driver);
606 + printk(KERN_ERR LIRC_DRIVER_NAME
607 + ": lirc register returned %d\n", result);
608 + goto exit_buffer_free;
611 + lirc_rpi_dev = platform_device_alloc(LIRC_DRIVER_NAME, 0);
612 + if (!lirc_rpi_dev) {
614 + goto exit_driver_unregister;
617 + result = platform_device_add(lirc_rpi_dev);
619 + goto exit_device_put;
624 + platform_device_put(lirc_rpi_dev);
626 + exit_driver_unregister:
627 + platform_driver_unregister(&lirc_rpi_driver);
630 + lirc_buffer_free(&rbuf);
635 +static void lirc_rpi_exit(void)
637 + platform_device_unregister(lirc_rpi_dev);
638 + platform_driver_unregister(&lirc_rpi_driver);
639 + lirc_buffer_free(&rbuf);
642 +static int __init lirc_rpi_init_module(void)
646 + result = lirc_rpi_init();
650 + /* check if the module received valid gpio pin numbers */
652 + if (gpio_in_pin != gpio_out_pin) {
653 + for(i = 0; (i < ARRAY_SIZE(valid_gpio_pins)) && (result != 2); i++) {
654 + if (gpio_in_pin == valid_gpio_pins[i] ||
655 + gpio_out_pin == valid_gpio_pins[i]) {
663 + printk(KERN_ERR LIRC_DRIVER_NAME
664 + ": invalid GPIO pin(s) specified!\n");
668 + result = init_port();
672 + driver.features = LIRC_CAN_SET_SEND_DUTY_CYCLE |
673 + LIRC_CAN_SET_SEND_CARRIER |
674 + LIRC_CAN_SEND_PULSE |
675 + LIRC_CAN_REC_MODE2;
677 + driver.dev = &lirc_rpi_dev->dev;
678 + driver.minor = lirc_register_driver(&driver);
680 + if (driver.minor < 0) {
681 + printk(KERN_ERR LIRC_DRIVER_NAME
682 + ": device registration failed with %d\n", result);
687 + printk(KERN_INFO LIRC_DRIVER_NAME ": driver registered!\n");
697 +static void __exit lirc_rpi_exit_module(void)
699 + gpio_free(gpio_out_pin);
700 + gpio_free(gpio_in_pin);
704 + lirc_unregister_driver(driver.minor);
705 + printk(KERN_INFO LIRC_DRIVER_NAME ": cleaned up module\n");
708 +module_init(lirc_rpi_init_module);
709 +module_exit(lirc_rpi_exit_module);
711 +MODULE_DESCRIPTION("Infra-red receiver and blaster driver for Raspberry Pi GPIO.");
712 +MODULE_AUTHOR("Aron Robert Szabo <aron@reon.hu>");
713 +MODULE_AUTHOR("Michael Bishop <cleverca22@gmail.com>");
714 +MODULE_LICENSE("GPL");
716 +module_param(gpio_out_pin, int, S_IRUGO);
717 +MODULE_PARM_DESC(gpio_out_pin, "GPIO output/transmitter pin number of the BCM"
718 + " processor. Valid pin numbers are: 0, 1, 4, 8, 7, 9, 10, 11,"
719 + " 14, 15, 17, 18, 21, 22, 23, 24, 25, default 17");
721 +module_param(gpio_in_pin, int, S_IRUGO);
722 +MODULE_PARM_DESC(gpio_in_pin, "GPIO input pin number of the BCM processor."
723 + " Valid pin numbers are: 0, 1, 4, 8, 7, 9, 10, 11, 14, 15,"
724 + " 17, 18, 21, 22, 23, 24, 25, default 18");
726 +module_param(sense, int, S_IRUGO);
727 +MODULE_PARM_DESC(sense, "Override autodetection of IR receiver circuit"
728 + " (0 = active high, 1 = active low )");
730 +module_param(softcarrier, bool, S_IRUGO);
731 +MODULE_PARM_DESC(softcarrier, "Software carrier (0 = off, 1 = on, default on)");
733 +module_param(invert, bool, S_IRUGO);
734 +MODULE_PARM_DESC(invert, "Invert output (0 = off, 1 = on, default off");
736 +module_param(debug, bool, S_IRUGO | S_IWUSR);
737 +MODULE_PARM_DESC(debug, "Enable debugging messages");