4 * serial_ir - Device driver that records pulse- and pause-lengths
5 * (space-lengths) between DDCD event on a serial port.
7 * Copyright (C) 1996,97 Ralph Metzler <rjkm@thp.uni-koeln.de>
8 * Copyright (C) 1998 Trent Piepho <xyzzy@u.washington.edu>
9 * Copyright (C) 1998 Ben Pfaff <blp@gnu.org>
10 * Copyright (C) 1999 Christoph Bartelmus <lirc@bartelmus.de>
11 * Copyright (C) 2007 Andrei Tanas <andrei@tanas.ca> (suspend/resume support)
12 * Copyright (C) 2016 Sean Young <sean@mess.org> (port to rc-core)
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
24 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
26 #include <linux/module.h>
27 #include <linux/errno.h>
28 #include <linux/interrupt.h>
29 #include <linux/kernel.h>
30 #include <linux/serial_reg.h>
31 #include <linux/types.h>
32 #include <linux/delay.h>
33 #include <linux/platform_device.h>
34 #include <linux/spinlock.h>
35 #include <media/rc-core.h>
39 int signal_pin_change;
42 unsigned set_send_carrier:1;
43 unsigned set_duty_cycle:1;
44 void (*send_pulse)(unsigned int length, ktime_t edge);
45 void (*send_space)(void);
51 #define IR_IRDEO_REMOTE 2
55 /* module parameters */
61 static bool softcarrier = true;
62 static bool share_irq;
63 static int sense = -1; /* -1 = auto, 0 = active high, 1 = active low */
64 static bool txsense; /* 0 = active high, 1 = active low */
66 /* forward declarations */
67 static void send_pulse_irdeo(unsigned int length, ktime_t edge);
68 static void send_space_irdeo(void);
69 #ifdef CONFIG_IR_SERIAL_TRANSMITTER
70 static void send_pulse_homebrew(unsigned int length, ktime_t edge);
71 static void send_space_homebrew(void);
74 static struct serial_ir_hw hardware[] = {
76 .lock = __SPIN_LOCK_UNLOCKED(hardware[IR_HOMEBREW].lock),
77 .signal_pin = UART_MSR_DCD,
78 .signal_pin_change = UART_MSR_DDCD,
79 .on = (UART_MCR_RTS | UART_MCR_OUT2 | UART_MCR_DTR),
80 .off = (UART_MCR_RTS | UART_MCR_OUT2),
81 #ifdef CONFIG_IR_SERIAL_TRANSMITTER
82 .send_pulse = send_pulse_homebrew,
83 .send_space = send_space_homebrew,
84 .set_send_carrier = true,
85 .set_duty_cycle = true,
90 .lock = __SPIN_LOCK_UNLOCKED(hardware[IR_IRDEO].lock),
91 .signal_pin = UART_MSR_DSR,
92 .signal_pin_change = UART_MSR_DDSR,
94 .off = (UART_MCR_RTS | UART_MCR_DTR | UART_MCR_OUT2),
95 .send_pulse = send_pulse_irdeo,
96 .send_space = send_space_irdeo,
97 .set_duty_cycle = true,
100 [IR_IRDEO_REMOTE] = {
101 .lock = __SPIN_LOCK_UNLOCKED(hardware[IR_IRDEO_REMOTE].lock),
102 .signal_pin = UART_MSR_DSR,
103 .signal_pin_change = UART_MSR_DDSR,
104 .on = (UART_MCR_RTS | UART_MCR_DTR | UART_MCR_OUT2),
105 .off = (UART_MCR_RTS | UART_MCR_DTR | UART_MCR_OUT2),
106 .send_pulse = send_pulse_irdeo,
107 .send_space = send_space_irdeo,
108 .set_duty_cycle = true,
112 .lock = __SPIN_LOCK_UNLOCKED(hardware[IR_ANIMAX].lock),
113 .signal_pin = UART_MSR_DCD,
114 .signal_pin_change = UART_MSR_DDCD,
116 .off = (UART_MCR_RTS | UART_MCR_DTR | UART_MCR_OUT2),
120 .lock = __SPIN_LOCK_UNLOCKED(hardware[IR_IGOR].lock),
121 .signal_pin = UART_MSR_DSR,
122 .signal_pin_change = UART_MSR_DDSR,
123 .on = (UART_MCR_RTS | UART_MCR_OUT2 | UART_MCR_DTR),
124 .off = (UART_MCR_RTS | UART_MCR_OUT2),
125 #ifdef CONFIG_IR_SERIAL_TRANSMITTER
126 .send_pulse = send_pulse_homebrew,
127 .send_space = send_space_homebrew,
128 .set_send_carrier = true,
129 .set_duty_cycle = true,
134 #define RS_ISR_PASS_LIMIT 256
138 struct rc_dev *rcdev;
139 struct platform_device *pdev;
140 struct timer_list timeout_timer;
143 unsigned int duty_cycle;
145 unsigned int pulse_width, space_width;
148 static struct serial_ir serial_ir;
150 /* fetch serial input packet (1 byte) from register offset */
151 static u8 sinp(int offset)
154 /* the register is memory-mapped */
157 return inb(io + offset);
160 /* write serial output packet (1 byte) of value to register offset */
161 static void soutp(int offset, u8 value)
164 /* the register is memory-mapped */
167 outb(value, io + offset);
173 soutp(UART_MCR, hardware[type].off);
175 soutp(UART_MCR, hardware[type].on);
178 static void off(void)
181 soutp(UART_MCR, hardware[type].on);
183 soutp(UART_MCR, hardware[type].off);
186 static void init_timing_params(unsigned int new_duty_cycle,
187 unsigned int new_freq)
189 serial_ir.duty_cycle = new_duty_cycle;
190 serial_ir.freq = new_freq;
192 serial_ir.pulse_width = DIV_ROUND_CLOSEST(
193 new_duty_cycle * NSEC_PER_SEC, new_freq * 100l);
194 serial_ir.space_width = DIV_ROUND_CLOSEST(
195 (100l - new_duty_cycle) * NSEC_PER_SEC, new_freq * 100l);
198 static void send_pulse_irdeo(unsigned int length, ktime_t target)
202 unsigned char output;
203 unsigned char chunk, shifted;
205 /* how many bits have to be sent ? */
206 rawbits = length * 1152 / 10000;
207 if (serial_ir.duty_cycle > 50)
211 for (i = 0, output = 0x7f; rawbits > 0; rawbits -= 3) {
212 shifted = chunk << (i * 3);
214 output &= (~shifted);
217 soutp(UART_TX, output);
218 while (!(sinp(UART_LSR) & UART_LSR_THRE))
225 soutp(UART_TX, output);
226 while (!(sinp(UART_LSR) & UART_LSR_TEMT))
231 static void send_space_irdeo(void)
235 #ifdef CONFIG_IR_SERIAL_TRANSMITTER
236 static void send_pulse_homebrew_softcarrier(unsigned int length, ktime_t edge)
238 ktime_t now, target = ktime_add_us(edge, length);
240 * delta should never exceed 4 seconds and on m68k
241 * ndelay(s64) does not compile; so use s32 rather than s64.
247 if (ktime_compare(now, target) >= 0)
250 edge = ktime_add_ns(edge, serial_ir.pulse_width);
251 delta = ktime_to_ns(ktime_sub(edge, now));
256 if (ktime_compare(now, target) >= 0)
258 edge = ktime_add_ns(edge, serial_ir.space_width);
259 delta = ktime_to_ns(ktime_sub(edge, now));
265 static void send_pulse_homebrew(unsigned int length, ktime_t edge)
268 send_pulse_homebrew_softcarrier(length, edge);
273 static void send_space_homebrew(void)
279 static void frbwrite(unsigned int l, bool is_pulse)
281 /* simple noise filter */
282 static unsigned int ptr, pulse, space;
283 DEFINE_IR_RAW_EVENT(ev);
285 if (ptr > 0 && is_pulse) {
287 if (pulse > 250000) {
290 ir_raw_event_store_with_filter(serial_ir.rcdev, &ev);
293 ir_raw_event_store_with_filter(serial_ir.rcdev, &ev);
309 if (space > IR_MAX_DURATION)
310 space = IR_MAX_DURATION;
312 if (space > IR_MAX_DURATION)
313 space = IR_MAX_DURATION;
320 ir_raw_event_store_with_filter(serial_ir.rcdev, &ev);
323 ir_raw_event_store_with_filter(serial_ir.rcdev, &ev);
331 ir_raw_event_store_with_filter(serial_ir.rcdev, &ev);
334 static irqreturn_t serial_ir_irq_handler(int i, void *blah)
341 static int last_dcd = -1;
343 if ((sinp(UART_IIR) & UART_IIR_NO_INT)) {
344 /* not our interrupt */
351 status = sinp(UART_MSR);
352 if (counter > RS_ISR_PASS_LIMIT) {
353 dev_err(&serial_ir.pdev->dev, "Trapped in interrupt");
356 if ((status & hardware[type].signal_pin_change) &&
358 /* get current time */
362 * The driver needs to know if your receiver is
363 * active high or active low, or the space/pulse
364 * sense could be inverted.
367 /* calc time since last interrupt in nanoseconds */
368 dcd = (status & hardware[type].signal_pin) ? 1 : 0;
370 if (dcd == last_dcd) {
371 dev_err(&serial_ir.pdev->dev,
372 "ignoring spike: %d %d %lldns %lldns\n",
373 dcd, sense, ktime_to_ns(kt),
374 ktime_to_ns(serial_ir.lastkt));
378 delkt = ktime_sub(kt, serial_ir.lastkt);
379 if (ktime_compare(delkt, ktime_set(15, 0)) > 0) {
380 data = IR_MAX_DURATION; /* really long time */
381 if (!(dcd ^ sense)) {
383 dev_err(&serial_ir.pdev->dev,
384 "dcd unexpected: %d %d %lldns %lldns\n",
385 dcd, sense, ktime_to_ns(kt),
386 ktime_to_ns(serial_ir.lastkt));
388 * detecting pulse while this
391 sense = sense ? 0 : 1;
394 data = ktime_to_ns(delkt);
396 frbwrite(data, !(dcd ^ sense));
397 serial_ir.lastkt = kt;
400 } while (!(sinp(UART_IIR) & UART_IIR_NO_INT)); /* still pending ? */
402 mod_timer(&serial_ir.timeout_timer,
403 jiffies + nsecs_to_jiffies(serial_ir.rcdev->timeout));
405 ir_raw_event_handle(serial_ir.rcdev);
410 static int hardware_init_port(void)
412 u8 scratch, scratch2, scratch3;
415 * This is a simple port existence test, borrowed from the autoconfig
416 * function in drivers/tty/serial/8250/8250_port.c
418 scratch = sinp(UART_IER);
423 scratch2 = sinp(UART_IER) & 0x0f;
424 soutp(UART_IER, 0x0f);
428 scratch3 = sinp(UART_IER) & 0x0f;
429 soutp(UART_IER, scratch);
430 if (scratch2 != 0 || scratch3 != 0x0f) {
431 /* we fail, there's nothing here */
432 pr_err("port existence test failed, cannot continue\n");
437 soutp(UART_LCR, sinp(UART_LCR) & (~UART_LCR_DLAB));
439 /* First of all, disable all interrupts */
440 soutp(UART_IER, sinp(UART_IER) &
441 (~(UART_IER_MSI | UART_IER_RLSI | UART_IER_THRI | UART_IER_RDI)));
443 /* Clear registers. */
449 /* Set line for power source */
452 /* Clear registers again to be sure. */
460 case IR_IRDEO_REMOTE:
461 /* setup port to 7N1 @ 115200 Baud */
462 /* 7N1+start = 9 bits at 115200 ~ 3 bits at 38kHz */
465 soutp(UART_LCR, sinp(UART_LCR) | UART_LCR_DLAB);
466 /* Set divisor to 1 => 115200 Baud */
469 /* Set DLAB 0 + 7N1 */
470 soutp(UART_LCR, UART_LCR_WLEN7);
471 /* THR interrupt already disabled at this point */
480 static void serial_ir_timeout(unsigned long arg)
482 DEFINE_IR_RAW_EVENT(ev);
485 ev.duration = serial_ir.rcdev->timeout;
486 ir_raw_event_store_with_filter(serial_ir.rcdev, &ev);
487 ir_raw_event_handle(serial_ir.rcdev);
490 static int serial_ir_probe(struct platform_device *dev)
492 int i, nlow, nhigh, result;
494 result = devm_request_irq(&dev->dev, irq, serial_ir_irq_handler,
495 share_irq ? IRQF_SHARED : 0,
496 KBUILD_MODNAME, &hardware);
498 if (result == -EBUSY)
499 dev_err(&dev->dev, "IRQ %d busy\n", irq);
500 else if (result == -EINVAL)
501 dev_err(&dev->dev, "Bad irq number or handler\n");
505 /* Reserve io region. */
507 (devm_request_mem_region(&dev->dev, iommap, 8 << ioshift,
508 KBUILD_MODNAME) == NULL)) ||
509 (!iommap && (devm_request_region(&dev->dev, io, 8,
510 KBUILD_MODNAME) == NULL))) {
511 dev_err(&dev->dev, "port %04x already in use\n", io);
512 dev_warn(&dev->dev, "use 'setserial /dev/ttySX uart none'\n");
514 "or compile the serial port driver as module and\n");
515 dev_warn(&dev->dev, "make sure this module is loaded first\n");
519 setup_timer(&serial_ir.timeout_timer, serial_ir_timeout,
520 (unsigned long)&serial_ir);
522 result = hardware_init_port();
526 /* Initialize pulse/space widths */
527 init_timing_params(50, 38000);
529 /* If pin is high, then this must be an active low receiver. */
531 /* wait 1/2 sec for the power supply */
535 * probe 9 times every 0.04s, collect "votes" for
540 for (i = 0; i < 9; i++) {
541 if (sinp(UART_MSR) & hardware[type].signal_pin)
547 sense = nlow >= nhigh ? 1 : 0;
548 dev_info(&dev->dev, "auto-detected active %s receiver\n",
549 sense ? "low" : "high");
551 dev_info(&dev->dev, "Manually using active %s receiver\n",
552 sense ? "low" : "high");
554 dev_dbg(&dev->dev, "Interrupt %d, port %04x obtained\n", irq, io);
558 static int serial_ir_open(struct rc_dev *rcdev)
562 /* initialize timestamp */
563 serial_ir.lastkt = ktime_get();
565 spin_lock_irqsave(&hardware[type].lock, flags);
568 soutp(UART_LCR, sinp(UART_LCR) & (~UART_LCR_DLAB));
570 soutp(UART_IER, sinp(UART_IER) | UART_IER_MSI);
572 spin_unlock_irqrestore(&hardware[type].lock, flags);
577 static void serial_ir_close(struct rc_dev *rcdev)
581 spin_lock_irqsave(&hardware[type].lock, flags);
584 soutp(UART_LCR, sinp(UART_LCR) & (~UART_LCR_DLAB));
586 /* First of all, disable all interrupts */
587 soutp(UART_IER, sinp(UART_IER) &
588 (~(UART_IER_MSI | UART_IER_RLSI | UART_IER_THRI | UART_IER_RDI)));
589 spin_unlock_irqrestore(&hardware[type].lock, flags);
592 static int serial_ir_tx(struct rc_dev *dev, unsigned int *txbuf,
600 spin_lock_irqsave(&hardware[type].lock, flags);
601 if (type == IR_IRDEO) {
607 for (i = 0; i < count; i++) {
609 hardware[type].send_space();
611 hardware[type].send_pulse(txbuf[i], edge);
613 edge = ktime_add_us(edge, txbuf[i]);
614 delta = ktime_us_delta(edge, ktime_get());
616 spin_unlock_irqrestore(&hardware[type].lock, flags);
617 usleep_range(delta - 25, delta + 25);
618 spin_lock_irqsave(&hardware[type].lock, flags);
619 } else if (delta > 0) {
624 spin_unlock_irqrestore(&hardware[type].lock, flags);
628 static int serial_ir_tx_duty_cycle(struct rc_dev *dev, u32 cycle)
630 init_timing_params(cycle, serial_ir.freq);
634 static int serial_ir_tx_carrier(struct rc_dev *dev, u32 carrier)
636 if (carrier > 500000 || carrier < 20000)
639 init_timing_params(serial_ir.duty_cycle, carrier);
643 static int serial_ir_suspend(struct platform_device *dev,
647 soutp(UART_LCR, sinp(UART_LCR) & (~UART_LCR_DLAB));
649 /* Disable all interrupts */
650 soutp(UART_IER, sinp(UART_IER) &
651 (~(UART_IER_MSI | UART_IER_RLSI | UART_IER_THRI | UART_IER_RDI)));
653 /* Clear registers. */
662 static int serial_ir_resume(struct platform_device *dev)
667 result = hardware_init_port();
671 spin_lock_irqsave(&hardware[type].lock, flags);
672 /* Enable Interrupt */
673 serial_ir.lastkt = ktime_get();
674 soutp(UART_IER, sinp(UART_IER) | UART_IER_MSI);
677 spin_unlock_irqrestore(&hardware[type].lock, flags);
682 static struct platform_driver serial_ir_driver = {
683 .probe = serial_ir_probe,
684 .suspend = serial_ir_suspend,
685 .resume = serial_ir_resume,
691 static int __init serial_ir_init(void)
695 result = platform_driver_register(&serial_ir_driver);
699 serial_ir.pdev = platform_device_alloc("serial_ir", 0);
700 if (!serial_ir.pdev) {
702 goto exit_driver_unregister;
705 result = platform_device_add(serial_ir.pdev);
707 goto exit_device_put;
712 platform_device_put(serial_ir.pdev);
713 exit_driver_unregister:
714 platform_driver_unregister(&serial_ir_driver);
718 static void serial_ir_exit(void)
720 platform_device_unregister(serial_ir.pdev);
721 platform_driver_unregister(&serial_ir_driver);
724 static int __init serial_ir_init_module(void)
726 struct rc_dev *rcdev;
732 case IR_IRDEO_REMOTE:
735 /* if nothing specified, use ttyS0/com1 and irq 4 */
736 io = io ? io : 0x3f8;
746 hardware[type].set_send_carrier = false;
747 hardware[type].set_duty_cycle = false;
752 /* make sure sense is either -1, 0, or 1 */
756 result = serial_ir_init();
760 rcdev = devm_rc_allocate_device(&serial_ir.pdev->dev, RC_DRIVER_IR_RAW);
766 if (hardware[type].send_pulse && hardware[type].send_space)
767 rcdev->tx_ir = serial_ir_tx;
768 if (hardware[type].set_send_carrier)
769 rcdev->s_tx_carrier = serial_ir_tx_carrier;
770 if (hardware[type].set_duty_cycle)
771 rcdev->s_tx_duty_cycle = serial_ir_tx_duty_cycle;
775 rcdev->input_name = "Serial IR type home-brew";
778 rcdev->input_name = "Serial IR type IRdeo";
780 case IR_IRDEO_REMOTE:
781 rcdev->input_name = "Serial IR type IRdeo remote";
784 rcdev->input_name = "Serial IR type AnimaX";
787 rcdev->input_name = "Serial IR type IgorPlug";
791 rcdev->input_phys = KBUILD_MODNAME "/input0";
792 rcdev->input_id.bustype = BUS_HOST;
793 rcdev->input_id.vendor = 0x0001;
794 rcdev->input_id.product = 0x0001;
795 rcdev->input_id.version = 0x0100;
796 rcdev->open = serial_ir_open;
797 rcdev->close = serial_ir_close;
798 rcdev->dev.parent = &serial_ir.pdev->dev;
799 rcdev->allowed_protocols = RC_BIT_ALL_IR_DECODER;
800 rcdev->driver_name = KBUILD_MODNAME;
801 rcdev->map_name = RC_MAP_RC6_MCE;
802 rcdev->min_timeout = 1;
803 rcdev->timeout = IR_DEFAULT_TIMEOUT;
804 rcdev->max_timeout = 10 * IR_DEFAULT_TIMEOUT;
805 rcdev->rx_resolution = 250000;
807 serial_ir.rcdev = rcdev;
809 result = rc_register_device(rcdev);
818 static void __exit serial_ir_exit_module(void)
820 del_timer_sync(&serial_ir.timeout_timer);
821 rc_unregister_device(serial_ir.rcdev);
825 module_init(serial_ir_init_module);
826 module_exit(serial_ir_exit_module);
828 MODULE_DESCRIPTION("Infra-red receiver driver for serial ports.");
829 MODULE_AUTHOR("Ralph Metzler, Trent Piepho, Ben Pfaff, Christoph Bartelmus, Andrei Tanas");
830 MODULE_LICENSE("GPL");
832 module_param(type, int, 0444);
833 MODULE_PARM_DESC(type, "Hardware type (0 = home-brew, 1 = IRdeo, 2 = IRdeo Remote, 3 = AnimaX, 4 = IgorPlug");
835 module_param(io, int, 0444);
836 MODULE_PARM_DESC(io, "I/O address base (0x3f8 or 0x2f8)");
838 /* some architectures (e.g. intel xscale) have memory mapped registers */
839 module_param(iommap, bool, 0444);
840 MODULE_PARM_DESC(iommap, "physical base for memory mapped I/O (0 = no memory mapped io)");
843 * some architectures (e.g. intel xscale) align the 8bit serial registers
844 * on 32bit word boundaries.
845 * See linux-kernel/drivers/tty/serial/8250/8250.c serial_in()/out()
847 module_param(ioshift, int, 0444);
848 MODULE_PARM_DESC(ioshift, "shift I/O register offset (0 = no shift)");
850 module_param(irq, int, 0444);
851 MODULE_PARM_DESC(irq, "Interrupt (4 or 3)");
853 module_param(share_irq, bool, 0444);
854 MODULE_PARM_DESC(share_irq, "Share interrupts (0 = off, 1 = on)");
856 module_param(sense, int, 0444);
857 MODULE_PARM_DESC(sense, "Override autodetection of IR receiver circuit (0 = active high, 1 = active low )");
859 #ifdef CONFIG_IR_SERIAL_TRANSMITTER
860 module_param(txsense, bool, 0444);
861 MODULE_PARM_DESC(txsense, "Sense of transmitter circuit (0 = active high, 1 = active low )");
864 module_param(softcarrier, bool, 0444);
865 MODULE_PARM_DESC(softcarrier, "Software carrier (0 = off, 1 = on, default on)");