1 From ecc83f926bd3cf3c9e62389494f39a0939949ef3 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 048/454] 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 lirc: Remove restriction on gpio pins that can be used with lirc
11 Compute Module, for example could use different pins
13 lirc_rpi: Add parameter to specify input pin pull
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.
19 Signed-off-by: Julian Scheel <julian@jusst.de>
21 lirc-rpi: Use the higher-level irq control functions
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.
29 lirc-rpi: Correct the interrupt usage
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
35 lirc-rpi: use getnstimeofday instead of read_current_timer
37 read_current_timer isn't guaranteed to return values in
38 microseconds, and indeed it doesn't on a Pi2.
42 lirc-rpi: Add device tree support, and a suitable overlay
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".
48 lirc-rpi: Also support pinctrl-bcm2835 in non-DT mode
50 fix auto-sense in lirc_rpi driver
52 On a Raspberry Pi 2, the lirc_rpi driver might receive spurious
53 interrupts and change it's low-active / high-active setting.
54 When this happens, the IR remote control stops working.
56 This patch disables this auto-detection if the 'sense' parameter
57 was set in the device tree, making the driver robust to such
60 drivers/staging/media/lirc/Kconfig | 6 +
61 drivers/staging/media/lirc/Makefile | 1 +
62 drivers/staging/media/lirc/lirc_rpi.c | 733 ++++++++++++++++++++++++++
63 include/linux/platform_data/bcm2708.h | 23 +
64 4 files changed, 763 insertions(+)
65 create mode 100644 drivers/staging/media/lirc/lirc_rpi.c
66 create mode 100644 include/linux/platform_data/bcm2708.h
68 --- a/drivers/staging/media/lirc/Kconfig
69 +++ b/drivers/staging/media/lirc/Kconfig
70 @@ -12,6 +12,12 @@ menuconfig LIRC_STAGING
75 + tristate "Homebrew GPIO Port Receiver/Transmitter for the RaspberryPi"
78 + Driver for Homebrew GPIO Port Receiver/Transmitter for the RaspberryPi
81 tristate "Zilog/Hauppauge IR Transmitter"
82 depends on LIRC && I2C
83 --- a/drivers/staging/media/lirc/Makefile
84 +++ b/drivers/staging/media/lirc/Makefile
87 # Each configuration option enables a list of files.
89 +obj-$(CONFIG_LIRC_RPI) += lirc_rpi.o
90 obj-$(CONFIG_LIRC_ZILOG) += lirc_zilog.o
92 +++ b/drivers/staging/media/lirc/lirc_rpi.c
97 + * lirc_rpi - Device driver that records pulse- and pause-lengths
98 + * (space-lengths) (just like the lirc_serial driver does)
99 + * between GPIO interrupt events on the Raspberry Pi.
100 + * Lots of code has been taken from the lirc_serial module,
101 + * so I would like say thanks to the authors.
103 + * Copyright (C) 2012 Aron Robert Szabo <aron@reon.hu>,
104 + * Michael Bishop <cleverca22@gmail.com>
105 + * This program is free software; you can redistribute it and/or modify
106 + * it under the terms of the GNU General Public License as published by
107 + * the Free Software Foundation; either version 2 of the License, or
108 + * (at your option) any later version.
110 + * This program is distributed in the hope that it will be useful,
111 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
112 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
113 + * GNU General Public License for more details.
115 + * You should have received a copy of the GNU General Public License
116 + * along with this program; if not, write to the Free Software
117 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
120 +#include <linux/module.h>
121 +#include <linux/errno.h>
122 +#include <linux/interrupt.h>
123 +#include <linux/sched.h>
124 +#include <linux/kernel.h>
125 +#include <linux/time.h>
126 +#include <linux/timex.h>
127 +#include <linux/timekeeping.h>
128 +#include <linux/string.h>
129 +#include <linux/delay.h>
130 +#include <linux/platform_device.h>
131 +#include <linux/irq.h>
132 +#include <linux/spinlock.h>
133 +#include <media/lirc.h>
134 +#include <media/lirc_dev.h>
135 +#include <linux/gpio.h>
136 +#include <linux/of_platform.h>
137 +#include <linux/platform_data/bcm2708.h>
139 +#define LIRC_DRIVER_NAME "lirc_rpi"
140 +#define RBUF_LEN 256
141 +#define LIRC_TRANSMITTER_LATENCY 50
143 +#ifndef MAX_UDELAY_MS
144 +#define MAX_UDELAY_US 5000
146 +#define MAX_UDELAY_US (MAX_UDELAY_MS*1000)
149 +#define dprintk(fmt, args...) \
152 + printk(KERN_DEBUG LIRC_DRIVER_NAME ": " \
156 +/* module parameters */
158 +/* set the default GPIO input pin */
159 +static int gpio_in_pin = 18;
160 +/* set the default pull behaviour for input pin */
161 +static int gpio_in_pull = BCM2708_PULL_DOWN;
162 +/* set the default GPIO output pin */
163 +static int gpio_out_pin = 17;
164 +/* enable debugging messages */
166 +/* -1 = auto, 0 = active high, 1 = active low */
167 +static int sense = -1;
168 +/* use softcarrier by default */
169 +static bool softcarrier = 1;
170 +/* 0 = do not invert output, 1 = invert output */
171 +static bool invert = 0;
173 +struct gpio_chip *gpiochip;
175 +static int auto_sense = 1;
177 +/* forward declarations */
178 +static long send_pulse(unsigned long length);
179 +static void send_space(long length);
180 +static void lirc_rpi_exit(void);
182 +static struct platform_device *lirc_rpi_dev;
183 +static struct timeval lasttv = { 0, 0 };
184 +static struct lirc_buffer rbuf;
185 +static spinlock_t lock;
187 +/* initialized/set in init_timing_params() */
188 +static unsigned int freq = 38000;
189 +static unsigned int duty_cycle = 50;
190 +static unsigned long period;
191 +static unsigned long pulse_width;
192 +static unsigned long space_width;
194 +static void safe_udelay(unsigned long usecs)
196 + while (usecs > MAX_UDELAY_US) {
197 + udelay(MAX_UDELAY_US);
198 + usecs -= MAX_UDELAY_US;
203 +static unsigned long read_current_us(void)
205 + struct timespec now;
206 + getnstimeofday(&now);
207 + return (now.tv_sec * 1000000) + (now.tv_nsec/1000);
210 +static int init_timing_params(unsigned int new_duty_cycle,
211 + unsigned int new_freq)
213 + if (1000 * 1000000L / new_freq * new_duty_cycle / 100 <=
214 + LIRC_TRANSMITTER_LATENCY)
216 + if (1000 * 1000000L / new_freq * (100 - new_duty_cycle) / 100 <=
217 + LIRC_TRANSMITTER_LATENCY)
219 + duty_cycle = new_duty_cycle;
221 + period = 1000 * 1000000L / freq;
222 + pulse_width = period * duty_cycle / 100;
223 + space_width = period - pulse_width;
224 + dprintk("in init_timing_params, freq=%d pulse=%ld, "
225 + "space=%ld\n", freq, pulse_width, space_width);
229 +static long send_pulse_softcarrier(unsigned long length)
232 + unsigned long actual, target;
233 + unsigned long actual_us, initial_us, target_us;
237 + actual = 0; target = 0; flag = 0;
238 + actual_us = read_current_us();
240 + while (actual < length) {
242 + gpiochip->set(gpiochip, gpio_out_pin, invert);
243 + target += space_width;
245 + gpiochip->set(gpiochip, gpio_out_pin, !invert);
246 + target += pulse_width;
248 + initial_us = actual_us;
249 + target_us = actual_us + (target - actual) / 1000;
251 + * Note - we've checked in ioctl that the pulse/space
252 + * widths are big enough so that d is > 0
254 + if ((int)(target_us - actual_us) > 0)
255 + udelay(target_us - actual_us);
256 + actual_us = read_current_us();
257 + actual += (actual_us - initial_us) * 1000;
260 + return (actual-length) / 1000;
263 +static long send_pulse(unsigned long length)
269 + return send_pulse_softcarrier(length);
271 + gpiochip->set(gpiochip, gpio_out_pin, !invert);
272 + safe_udelay(length);
277 +static void send_space(long length)
279 + gpiochip->set(gpiochip, gpio_out_pin, invert);
282 + safe_udelay(length);
285 +static void rbwrite(int l)
287 + if (lirc_buffer_full(&rbuf)) {
288 + /* no new signals will be accepted */
289 + dprintk("Buffer overrun\n");
292 + lirc_buffer_write(&rbuf, (void *)&l);
295 +static void frbwrite(int l)
297 + /* simple noise filter */
298 + static int pulse, space;
299 + static unsigned int ptr;
301 + if (ptr > 0 && (l & PULSE_BIT)) {
302 + pulse += l & PULSE_MASK;
305 + rbwrite(pulse | PULSE_BIT);
311 + if (!(l & PULSE_BIT)) {
321 + if (space > PULSE_MASK)
322 + space = PULSE_MASK;
324 + if (space > PULSE_MASK)
325 + space = PULSE_MASK;
330 + rbwrite(pulse | PULSE_BIT);
338 +static irqreturn_t irq_handler(int i, void *blah, struct pt_regs *regs)
345 + /* use the GPIO signal level */
346 + signal = gpiochip->get(gpiochip, gpio_in_pin);
349 + /* get current time */
350 + do_gettimeofday(&tv);
352 + /* calc time since last interrupt in microseconds */
353 + deltv = tv.tv_sec-lasttv.tv_sec;
354 + if (tv.tv_sec < lasttv.tv_sec ||
355 + (tv.tv_sec == lasttv.tv_sec &&
356 + tv.tv_usec < lasttv.tv_usec)) {
357 + printk(KERN_WARNING LIRC_DRIVER_NAME
358 + ": AIEEEE: your clock just jumped backwards\n");
359 + printk(KERN_WARNING LIRC_DRIVER_NAME
360 + ": %d %d %lx %lx %lx %lx\n", signal, sense,
361 + tv.tv_sec, lasttv.tv_sec,
362 + tv.tv_usec, lasttv.tv_usec);
364 + } else if (deltv > 15) {
365 + data = PULSE_MASK; /* really long time */
366 + if (!(signal^sense)) {
368 + printk(KERN_DEBUG LIRC_DRIVER_NAME
369 + ": AIEEEE: %d %d %lx %lx %lx %lx\n",
370 + signal, sense, tv.tv_sec, lasttv.tv_sec,
371 + tv.tv_usec, lasttv.tv_usec);
373 + * detecting pulse while this
377 + sense = sense ? 0 : 1;
381 + data = (int) (deltv*1000000 +
382 + (tv.tv_usec - lasttv.tv_usec));
384 + frbwrite(signal^sense ? data : (data|PULSE_BIT));
386 + wake_up_interruptible(&rbuf.wait_poll);
389 + return IRQ_HANDLED;
392 +static int is_right_chip(struct gpio_chip *chip, void *data)
394 + dprintk("is_right_chip %s %d\n", chip->label, strcmp(data, chip->label));
396 + if (strcmp(data, chip->label) == 0)
401 +static inline int read_bool_property(const struct device_node *np,
402 + const char *propname,
406 + int err = of_property_read_u32(np, propname, &value);
408 + *out_value = (value != 0);
412 +static void read_pin_settings(struct device_node *node)
418 + of_property_read_u32_index(
426 + err = of_property_read_u32_index(
432 + if (function == 1) /* Output */
433 + gpio_out_pin = pin;
434 + else if (function == 0) /* Input */
440 +static int init_port(void)
442 + int i, nlow, nhigh;
443 + struct device_node *node;
445 + node = lirc_rpi_dev->dev.of_node;
447 + gpiochip = gpiochip_find("bcm2708_gpio", is_right_chip);
450 + * Because of the lack of a setpull function, only support
451 + * pinctrl-bcm2835 if using device tree.
453 + if (!gpiochip && node)
454 + gpiochip = gpiochip_find("pinctrl-bcm2835", is_right_chip);
457 + pr_err(LIRC_DRIVER_NAME ": gpio chip not found!\n");
462 + struct device_node *pins_node;
464 + pins_node = of_parse_phandle(node, "pinctrl-0", 0);
466 + printk(KERN_ERR LIRC_DRIVER_NAME
467 + ": pinctrl settings not found!\n");
471 + read_pin_settings(pins_node);
473 + of_property_read_u32(node, "rpi,sense", &sense);
475 + read_bool_property(node, "rpi,softcarrier", &softcarrier);
477 + read_bool_property(node, "rpi,invert", &invert);
479 + read_bool_property(node, "rpi,debug", &debug);
485 + gpiochip->set(gpiochip, gpio_out_pin, invert);
487 + irq_num = gpiochip->to_irq(gpiochip, gpio_in_pin);
488 + dprintk("to_irq %d\n", irq_num);
490 + /* if pin is high, then this must be an active low receiver. */
492 + /* wait 1/2 sec for the power supply */
496 + * probe 9 times every 0.04s, collect "votes" for
501 + for (i = 0; i < 9; i++) {
502 + if (gpiochip->get(gpiochip, gpio_in_pin))
508 + sense = (nlow >= nhigh ? 1 : 0);
509 + printk(KERN_INFO LIRC_DRIVER_NAME
510 + ": auto-detected active %s receiver on GPIO pin %d\n",
511 + sense ? "low" : "high", gpio_in_pin);
513 + printk(KERN_INFO LIRC_DRIVER_NAME
514 + ": manually using active %s receiver on GPIO pin %d\n",
515 + sense ? "low" : "high", gpio_in_pin);
522 +// called when the character device is opened
523 +static int set_use_inc(void *data)
527 + /* initialize timestamp */
528 + do_gettimeofday(&lasttv);
530 + result = request_irq(irq_num,
531 + (irq_handler_t) irq_handler,
532 + IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING,
533 + LIRC_DRIVER_NAME, (void*) 0);
537 + printk(KERN_ERR LIRC_DRIVER_NAME
538 + ": IRQ %d is busy\n",
542 + printk(KERN_ERR LIRC_DRIVER_NAME
543 + ": Bad irq number or handler\n");
546 + dprintk("Interrupt %d obtained\n",
551 + /* initialize pulse/space widths */
552 + init_timing_params(duty_cycle, freq);
557 +static void set_use_dec(void *data)
559 + /* GPIO Pin Falling/Rising Edge Detect Disable */
560 + irq_set_irq_type(irq_num, 0);
561 + disable_irq(irq_num);
563 + free_irq(irq_num, (void *) 0);
565 + dprintk(KERN_INFO LIRC_DRIVER_NAME
566 + ": freed IRQ %d\n", irq_num);
569 +static ssize_t lirc_write(struct file *file, const char *buf,
570 + size_t n, loff_t *ppos)
573 + unsigned long flags;
577 + count = n / sizeof(int);
578 + if (n % sizeof(int) || count % 2 == 0)
580 + wbuf = memdup_user(buf, n);
582 + return PTR_ERR(wbuf);
583 + spin_lock_irqsave(&lock, flags);
585 + for (i = 0; i < count; i++) {
587 + send_space(wbuf[i] - delta);
589 + delta = send_pulse(wbuf[i]);
591 + gpiochip->set(gpiochip, gpio_out_pin, invert);
593 + spin_unlock_irqrestore(&lock, flags);
598 +static long lirc_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
604 + case LIRC_GET_SEND_MODE:
605 + return -ENOIOCTLCMD;
608 + case LIRC_SET_SEND_MODE:
609 + result = get_user(value, (__u32 *) arg);
612 + /* only LIRC_MODE_PULSE supported */
613 + if (value != LIRC_MODE_PULSE)
617 + case LIRC_GET_LENGTH:
621 + case LIRC_SET_SEND_DUTY_CYCLE:
622 + dprintk("SET_SEND_DUTY_CYCLE\n");
623 + result = get_user(value, (__u32 *) arg);
626 + if (value <= 0 || value > 100)
628 + return init_timing_params(value, freq);
631 + case LIRC_SET_SEND_CARRIER:
632 + dprintk("SET_SEND_CARRIER\n");
633 + result = get_user(value, (__u32 *) arg);
636 + if (value > 500000 || value < 20000)
638 + return init_timing_params(duty_cycle, value);
642 + return lirc_dev_fop_ioctl(filep, cmd, arg);
647 +static const struct file_operations lirc_fops = {
648 + .owner = THIS_MODULE,
649 + .write = lirc_write,
650 + .unlocked_ioctl = lirc_ioctl,
651 + .read = lirc_dev_fop_read,
652 + .poll = lirc_dev_fop_poll,
653 + .open = lirc_dev_fop_open,
654 + .release = lirc_dev_fop_close,
655 + .llseek = no_llseek,
658 +static struct lirc_driver driver = {
659 + .name = LIRC_DRIVER_NAME,
664 + .fops = &lirc_fops,
666 + .owner = THIS_MODULE,
669 +static const struct of_device_id lirc_rpi_of_match[] = {
670 + { .compatible = "rpi,lirc-rpi", },
673 +MODULE_DEVICE_TABLE(of, lirc_rpi_of_match);
675 +static struct platform_driver lirc_rpi_driver = {
677 + .name = LIRC_DRIVER_NAME,
678 + .owner = THIS_MODULE,
679 + .of_match_table = of_match_ptr(lirc_rpi_of_match),
683 +static int __init lirc_rpi_init(void)
685 + struct device_node *node;
688 + /* Init read buffer. */
689 + result = lirc_buffer_init(&rbuf, sizeof(int), RBUF_LEN);
693 + result = platform_driver_register(&lirc_rpi_driver);
695 + printk(KERN_ERR LIRC_DRIVER_NAME
696 + ": lirc register returned %d\n", result);
697 + goto exit_buffer_free;
700 + node = of_find_compatible_node(NULL, NULL,
701 + lirc_rpi_of_match[0].compatible);
705 + lirc_rpi_dev = of_find_device_by_node(node);
706 + WARN_ON(lirc_rpi_dev->dev.of_node != node);
710 + lirc_rpi_dev = platform_device_alloc(LIRC_DRIVER_NAME, 0);
711 + if (!lirc_rpi_dev) {
713 + goto exit_driver_unregister;
716 + result = platform_device_add(lirc_rpi_dev);
718 + goto exit_device_put;
724 + platform_device_put(lirc_rpi_dev);
726 + exit_driver_unregister:
727 + platform_driver_unregister(&lirc_rpi_driver);
730 + lirc_buffer_free(&rbuf);
735 +static void lirc_rpi_exit(void)
738 + if (!lirc_rpi_dev->dev.of_node)
739 + platform_device_unregister(lirc_rpi_dev);
740 + platform_driver_unregister(&lirc_rpi_driver);
741 + lirc_buffer_free(&rbuf);
744 +static int __init lirc_rpi_init_module(void)
748 + result = lirc_rpi_init();
752 + result = init_port();
756 + driver.features = LIRC_CAN_SET_SEND_DUTY_CYCLE |
757 + LIRC_CAN_SET_SEND_CARRIER |
758 + LIRC_CAN_SEND_PULSE |
759 + LIRC_CAN_REC_MODE2;
761 + driver.dev = &lirc_rpi_dev->dev;
762 + driver.minor = lirc_register_driver(&driver);
764 + if (driver.minor < 0) {
765 + printk(KERN_ERR LIRC_DRIVER_NAME
766 + ": device registration failed with %d\n", result);
771 + printk(KERN_INFO LIRC_DRIVER_NAME ": driver registered!\n");
783 +static void __exit lirc_rpi_exit_module(void)
785 + lirc_unregister_driver(driver.minor);
787 + gpio_free(gpio_out_pin);
788 + gpio_free(gpio_in_pin);
792 + printk(KERN_INFO LIRC_DRIVER_NAME ": cleaned up module\n");
795 +module_init(lirc_rpi_init_module);
796 +module_exit(lirc_rpi_exit_module);
798 +MODULE_DESCRIPTION("Infra-red receiver and blaster driver for Raspberry Pi GPIO.");
799 +MODULE_AUTHOR("Aron Robert Szabo <aron@reon.hu>");
800 +MODULE_AUTHOR("Michael Bishop <cleverca22@gmail.com>");
801 +MODULE_LICENSE("GPL");
803 +module_param(gpio_out_pin, int, S_IRUGO);
804 +MODULE_PARM_DESC(gpio_out_pin, "GPIO output/transmitter pin number of the BCM"
805 + " processor. (default 17");
807 +module_param(gpio_in_pin, int, S_IRUGO);
808 +MODULE_PARM_DESC(gpio_in_pin, "GPIO input pin number of the BCM processor."
811 +module_param(gpio_in_pull, int, S_IRUGO);
812 +MODULE_PARM_DESC(gpio_in_pull, "GPIO input pin pull configuration."
813 + " (0 = off, 1 = up, 2 = down, default down)");
815 +module_param(sense, int, S_IRUGO);
816 +MODULE_PARM_DESC(sense, "Override autodetection of IR receiver circuit"
817 + " (0 = active high, 1 = active low )");
819 +module_param(softcarrier, bool, S_IRUGO);
820 +MODULE_PARM_DESC(softcarrier, "Software carrier (0 = off, 1 = on, default on)");
822 +module_param(invert, bool, S_IRUGO);
823 +MODULE_PARM_DESC(invert, "Invert output (0 = off, 1 = on, default off");
825 +module_param(debug, bool, S_IRUGO | S_IWUSR);
826 +MODULE_PARM_DESC(debug, "Enable debugging messages");
828 +++ b/include/linux/platform_data/bcm2708.h
831 + * include/linux/platform_data/bcm2708.h
833 + * This program is free software; you can redistribute it and/or modify
834 + * it under the terms of the GNU General Public License version 2 as
835 + * published by the Free Software Foundation.
837 + * (C) 2014 Julian Scheel <julian@jusst.de>
840 +#ifndef __BCM2708_H_
841 +#define __BCM2708_H_
847 +} bcm2708_gpio_pull_t;
849 +extern int bcm2708_gpio_setpull(struct gpio_chip *gc, unsigned offset,
850 + bcm2708_gpio_pull_t value);