1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * serial_ir - Device driver that records pulse- and pause-lengths
6 * (space-lengths) between DDCD event on a serial port.
8 * Copyright (C) 1996,97 Ralph Metzler <rjkm@thp.uni-koeln.de>
9 * Copyright (C) 1998 Trent Piepho <xyzzy@u.washington.edu>
10 * Copyright (C) 1998 Ben Pfaff <blp@gnu.org>
11 * Copyright (C) 1999 Christoph Bartelmus <lirc@bartelmus.de>
12 * Copyright (C) 2007 Andrei Tanas <andrei@tanas.ca> (suspend/resume support)
13 * Copyright (C) 2016 Sean Young <sean@mess.org> (port to rc-core)
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 #include <linux/module.h>
19 #include <linux/errno.h>
20 #include <linux/interrupt.h>
21 #include <linux/kernel.h>
22 #include <linux/serial_reg.h>
23 #include <linux/types.h>
24 #include <linux/delay.h>
25 #include <linux/platform_device.h>
26 #include <linux/spinlock.h>
27 #include <media/rc-core.h>
31 int signal_pin_change
;
34 unsigned set_send_carrier
:1;
35 unsigned set_duty_cycle
:1;
36 void (*send_pulse
)(unsigned int length
, ktime_t edge
);
37 void (*send_space
)(void);
43 #define IR_IRDEO_REMOTE 2
47 /* module parameters */
53 static bool softcarrier
= true;
54 static bool share_irq
;
55 static int sense
= -1; /* -1 = auto, 0 = active high, 1 = active low */
56 static bool txsense
; /* 0 = active high, 1 = active low */
58 /* forward declarations */
59 static void send_pulse_irdeo(unsigned int length
, ktime_t edge
);
60 static void send_space_irdeo(void);
61 #ifdef CONFIG_IR_SERIAL_TRANSMITTER
62 static void send_pulse_homebrew(unsigned int length
, ktime_t edge
);
63 static void send_space_homebrew(void);
66 static struct serial_ir_hw hardware
[] = {
68 .lock
= __SPIN_LOCK_UNLOCKED(hardware
[IR_HOMEBREW
].lock
),
69 .signal_pin
= UART_MSR_DCD
,
70 .signal_pin_change
= UART_MSR_DDCD
,
71 .on
= (UART_MCR_RTS
| UART_MCR_OUT2
| UART_MCR_DTR
),
72 .off
= (UART_MCR_RTS
| UART_MCR_OUT2
),
73 #ifdef CONFIG_IR_SERIAL_TRANSMITTER
74 .send_pulse
= send_pulse_homebrew
,
75 .send_space
= send_space_homebrew
,
76 .set_send_carrier
= true,
77 .set_duty_cycle
= true,
82 .lock
= __SPIN_LOCK_UNLOCKED(hardware
[IR_IRDEO
].lock
),
83 .signal_pin
= UART_MSR_DSR
,
84 .signal_pin_change
= UART_MSR_DDSR
,
86 .off
= (UART_MCR_RTS
| UART_MCR_DTR
| UART_MCR_OUT2
),
87 .send_pulse
= send_pulse_irdeo
,
88 .send_space
= send_space_irdeo
,
89 .set_duty_cycle
= true,
93 .lock
= __SPIN_LOCK_UNLOCKED(hardware
[IR_IRDEO_REMOTE
].lock
),
94 .signal_pin
= UART_MSR_DSR
,
95 .signal_pin_change
= UART_MSR_DDSR
,
96 .on
= (UART_MCR_RTS
| UART_MCR_DTR
| UART_MCR_OUT2
),
97 .off
= (UART_MCR_RTS
| UART_MCR_DTR
| UART_MCR_OUT2
),
98 .send_pulse
= send_pulse_irdeo
,
99 .send_space
= send_space_irdeo
,
100 .set_duty_cycle
= true,
104 .lock
= __SPIN_LOCK_UNLOCKED(hardware
[IR_ANIMAX
].lock
),
105 .signal_pin
= UART_MSR_DCD
,
106 .signal_pin_change
= UART_MSR_DDCD
,
108 .off
= (UART_MCR_RTS
| UART_MCR_DTR
| UART_MCR_OUT2
),
112 .lock
= __SPIN_LOCK_UNLOCKED(hardware
[IR_IGOR
].lock
),
113 .signal_pin
= UART_MSR_DSR
,
114 .signal_pin_change
= UART_MSR_DDSR
,
115 .on
= (UART_MCR_RTS
| UART_MCR_OUT2
| UART_MCR_DTR
),
116 .off
= (UART_MCR_RTS
| UART_MCR_OUT2
),
117 #ifdef CONFIG_IR_SERIAL_TRANSMITTER
118 .send_pulse
= send_pulse_homebrew
,
119 .send_space
= send_space_homebrew
,
120 .set_send_carrier
= true,
121 .set_duty_cycle
= true,
126 #define RS_ISR_PASS_LIMIT 256
130 struct rc_dev
*rcdev
;
131 struct platform_device
*pdev
;
132 struct timer_list timeout_timer
;
134 unsigned int carrier
;
135 unsigned int duty_cycle
;
138 static struct serial_ir serial_ir
;
140 /* fetch serial input packet (1 byte) from register offset */
141 static u8
sinp(int offset
)
144 /* the register is memory-mapped */
147 return inb(io
+ offset
);
150 /* write serial output packet (1 byte) of value to register offset */
151 static void soutp(int offset
, u8 value
)
154 /* the register is memory-mapped */
157 outb(value
, io
+ offset
);
163 soutp(UART_MCR
, hardware
[type
].off
);
165 soutp(UART_MCR
, hardware
[type
].on
);
168 static void off(void)
171 soutp(UART_MCR
, hardware
[type
].on
);
173 soutp(UART_MCR
, hardware
[type
].off
);
176 static void send_pulse_irdeo(unsigned int length
, ktime_t target
)
180 unsigned char output
;
181 unsigned char chunk
, shifted
;
183 /* how many bits have to be sent ? */
184 rawbits
= length
* 1152 / 10000;
185 if (serial_ir
.duty_cycle
> 50)
189 for (i
= 0, output
= 0x7f; rawbits
> 0; rawbits
-= 3) {
190 shifted
= chunk
<< (i
* 3);
192 output
&= (~shifted
);
195 soutp(UART_TX
, output
);
196 while (!(sinp(UART_LSR
) & UART_LSR_THRE
))
203 soutp(UART_TX
, output
);
204 while (!(sinp(UART_LSR
) & UART_LSR_TEMT
))
209 static void send_space_irdeo(void)
213 #ifdef CONFIG_IR_SERIAL_TRANSMITTER
214 static void send_pulse_homebrew_softcarrier(unsigned int length
, ktime_t edge
)
216 ktime_t now
, target
= ktime_add_us(edge
, length
);
218 * delta should never exceed 4 seconds and on m68k
219 * ndelay(s64) does not compile; so use s32 rather than s64.
222 unsigned int pulse
, space
;
224 /* Ensure the dividend fits into 32 bit */
225 pulse
= DIV_ROUND_CLOSEST(serial_ir
.duty_cycle
* (NSEC_PER_SEC
/ 100),
227 space
= DIV_ROUND_CLOSEST((100 - serial_ir
.duty_cycle
) *
228 (NSEC_PER_SEC
/ 100), serial_ir
.carrier
);
232 if (ktime_compare(now
, target
) >= 0)
235 edge
= ktime_add_ns(edge
, pulse
);
236 delta
= ktime_to_ns(ktime_sub(edge
, now
));
241 if (ktime_compare(now
, target
) >= 0)
243 edge
= ktime_add_ns(edge
, space
);
244 delta
= ktime_to_ns(ktime_sub(edge
, now
));
250 static void send_pulse_homebrew(unsigned int length
, ktime_t edge
)
253 send_pulse_homebrew_softcarrier(length
, edge
);
258 static void send_space_homebrew(void)
264 static void frbwrite(unsigned int l
, bool is_pulse
)
266 /* simple noise filter */
267 static unsigned int ptr
, pulse
, space
;
268 struct ir_raw_event ev
= {};
270 if (ptr
> 0 && is_pulse
) {
275 ir_raw_event_store_with_filter(serial_ir
.rcdev
, &ev
);
278 ir_raw_event_store_with_filter(serial_ir
.rcdev
, &ev
);
294 if (space
> IR_MAX_DURATION
)
295 space
= IR_MAX_DURATION
;
297 if (space
> IR_MAX_DURATION
)
298 space
= IR_MAX_DURATION
;
305 ir_raw_event_store_with_filter(serial_ir
.rcdev
, &ev
);
308 ir_raw_event_store_with_filter(serial_ir
.rcdev
, &ev
);
316 ir_raw_event_store_with_filter(serial_ir
.rcdev
, &ev
);
319 static irqreturn_t
serial_ir_irq_handler(int i
, void *blah
)
326 static int last_dcd
= -1;
328 if ((sinp(UART_IIR
) & UART_IIR_NO_INT
)) {
329 /* not our interrupt */
336 status
= sinp(UART_MSR
);
337 if (counter
> RS_ISR_PASS_LIMIT
) {
338 dev_err(&serial_ir
.pdev
->dev
, "Trapped in interrupt");
341 if ((status
& hardware
[type
].signal_pin_change
) &&
343 /* get current time */
347 * The driver needs to know if your receiver is
348 * active high or active low, or the space/pulse
349 * sense could be inverted.
352 /* calc time since last interrupt in nanoseconds */
353 dcd
= (status
& hardware
[type
].signal_pin
) ? 1 : 0;
355 if (dcd
== last_dcd
) {
356 dev_dbg(&serial_ir
.pdev
->dev
,
357 "ignoring spike: %d %d %lldns %lldns\n",
358 dcd
, sense
, ktime_to_ns(kt
),
359 ktime_to_ns(serial_ir
.lastkt
));
363 delkt
= ktime_sub(kt
, serial_ir
.lastkt
);
364 if (ktime_compare(delkt
, ktime_set(15, 0)) > 0) {
365 data
= IR_MAX_DURATION
; /* really long time */
366 if (!(dcd
^ sense
)) {
368 dev_err(&serial_ir
.pdev
->dev
,
369 "dcd unexpected: %d %d %lldns %lldns\n",
370 dcd
, sense
, ktime_to_ns(kt
),
371 ktime_to_ns(serial_ir
.lastkt
));
373 * detecting pulse while this
376 sense
= sense
? 0 : 1;
379 data
= ktime_to_us(delkt
);
381 frbwrite(data
, !(dcd
^ sense
));
382 serial_ir
.lastkt
= kt
;
385 } while (!(sinp(UART_IIR
) & UART_IIR_NO_INT
)); /* still pending ? */
387 mod_timer(&serial_ir
.timeout_timer
,
388 jiffies
+ nsecs_to_jiffies(serial_ir
.rcdev
->timeout
));
390 ir_raw_event_handle(serial_ir
.rcdev
);
395 static int hardware_init_port(void)
397 u8 scratch
, scratch2
, scratch3
;
400 * This is a simple port existence test, borrowed from the autoconfig
401 * function in drivers/tty/serial/8250/8250_port.c
403 scratch
= sinp(UART_IER
);
408 scratch2
= sinp(UART_IER
) & 0x0f;
409 soutp(UART_IER
, 0x0f);
413 scratch3
= sinp(UART_IER
) & 0x0f;
414 soutp(UART_IER
, scratch
);
415 if (scratch2
!= 0 || scratch3
!= 0x0f) {
416 /* we fail, there's nothing here */
417 pr_err("port existence test failed, cannot continue\n");
422 soutp(UART_LCR
, sinp(UART_LCR
) & (~UART_LCR_DLAB
));
424 /* First of all, disable all interrupts */
425 soutp(UART_IER
, sinp(UART_IER
) &
426 (~(UART_IER_MSI
| UART_IER_RLSI
| UART_IER_THRI
| UART_IER_RDI
)));
428 /* Clear registers. */
434 /* Set line for power source */
437 /* Clear registers again to be sure. */
445 case IR_IRDEO_REMOTE
:
446 /* setup port to 7N1 @ 115200 Baud */
447 /* 7N1+start = 9 bits at 115200 ~ 3 bits at 38kHz */
450 soutp(UART_LCR
, sinp(UART_LCR
) | UART_LCR_DLAB
);
451 /* Set divisor to 1 => 115200 Baud */
454 /* Set DLAB 0 + 7N1 */
455 soutp(UART_LCR
, UART_LCR_WLEN7
);
456 /* THR interrupt already disabled at this point */
465 static void serial_ir_timeout(struct timer_list
*unused
)
467 struct ir_raw_event ev
= {
469 .duration
= serial_ir
.rcdev
->timeout
471 ir_raw_event_store_with_filter(serial_ir
.rcdev
, &ev
);
472 ir_raw_event_handle(serial_ir
.rcdev
);
475 /* Needed by serial_ir_probe() */
476 static int serial_ir_tx(struct rc_dev
*dev
, unsigned int *txbuf
,
478 static int serial_ir_tx_duty_cycle(struct rc_dev
*dev
, u32 cycle
);
479 static int serial_ir_tx_carrier(struct rc_dev
*dev
, u32 carrier
);
480 static int serial_ir_open(struct rc_dev
*rcdev
);
481 static void serial_ir_close(struct rc_dev
*rcdev
);
483 static int serial_ir_probe(struct platform_device
*dev
)
485 struct rc_dev
*rcdev
;
486 int i
, nlow
, nhigh
, result
;
488 rcdev
= devm_rc_allocate_device(&dev
->dev
, RC_DRIVER_IR_RAW
);
492 if (hardware
[type
].send_pulse
&& hardware
[type
].send_space
)
493 rcdev
->tx_ir
= serial_ir_tx
;
494 if (hardware
[type
].set_send_carrier
)
495 rcdev
->s_tx_carrier
= serial_ir_tx_carrier
;
496 if (hardware
[type
].set_duty_cycle
)
497 rcdev
->s_tx_duty_cycle
= serial_ir_tx_duty_cycle
;
501 rcdev
->device_name
= "Serial IR type home-brew";
504 rcdev
->device_name
= "Serial IR type IRdeo";
506 case IR_IRDEO_REMOTE
:
507 rcdev
->device_name
= "Serial IR type IRdeo remote";
510 rcdev
->device_name
= "Serial IR type AnimaX";
513 rcdev
->device_name
= "Serial IR type IgorPlug";
517 rcdev
->input_phys
= KBUILD_MODNAME
"/input0";
518 rcdev
->input_id
.bustype
= BUS_HOST
;
519 rcdev
->input_id
.vendor
= 0x0001;
520 rcdev
->input_id
.product
= 0x0001;
521 rcdev
->input_id
.version
= 0x0100;
522 rcdev
->open
= serial_ir_open
;
523 rcdev
->close
= serial_ir_close
;
524 rcdev
->dev
.parent
= &serial_ir
.pdev
->dev
;
525 rcdev
->allowed_protocols
= RC_PROTO_BIT_ALL_IR_DECODER
;
526 rcdev
->driver_name
= KBUILD_MODNAME
;
527 rcdev
->map_name
= RC_MAP_RC6_MCE
;
528 rcdev
->min_timeout
= 1;
529 rcdev
->timeout
= IR_DEFAULT_TIMEOUT
;
530 rcdev
->max_timeout
= 10 * IR_DEFAULT_TIMEOUT
;
531 rcdev
->rx_resolution
= 250;
533 serial_ir
.rcdev
= rcdev
;
535 timer_setup(&serial_ir
.timeout_timer
, serial_ir_timeout
, 0);
537 result
= devm_request_irq(&dev
->dev
, irq
, serial_ir_irq_handler
,
538 share_irq
? IRQF_SHARED
: 0,
539 KBUILD_MODNAME
, &hardware
);
541 if (result
== -EBUSY
)
542 dev_err(&dev
->dev
, "IRQ %d busy\n", irq
);
543 else if (result
== -EINVAL
)
544 dev_err(&dev
->dev
, "Bad irq number or handler\n");
548 /* Reserve io region. */
550 (devm_request_mem_region(&dev
->dev
, iommap
, 8UL << ioshift
,
551 KBUILD_MODNAME
) == NULL
)) ||
552 (!iommap
&& (devm_request_region(&dev
->dev
, io
, 8,
553 KBUILD_MODNAME
) == NULL
))) {
554 dev_err(&dev
->dev
, "port %04x already in use\n", io
);
555 dev_warn(&dev
->dev
, "use 'setserial /dev/ttySX uart none'\n");
557 "or compile the serial port driver as module and\n");
558 dev_warn(&dev
->dev
, "make sure this module is loaded first\n");
562 result
= hardware_init_port();
566 /* Initialize pulse/space widths */
567 serial_ir
.duty_cycle
= 50;
568 serial_ir
.carrier
= 38000;
570 /* If pin is high, then this must be an active low receiver. */
572 /* wait 1/2 sec for the power supply */
576 * probe 9 times every 0.04s, collect "votes" for
581 for (i
= 0; i
< 9; i
++) {
582 if (sinp(UART_MSR
) & hardware
[type
].signal_pin
)
588 sense
= nlow
>= nhigh
? 1 : 0;
589 dev_info(&dev
->dev
, "auto-detected active %s receiver\n",
590 sense
? "low" : "high");
592 dev_info(&dev
->dev
, "Manually using active %s receiver\n",
593 sense
? "low" : "high");
595 dev_dbg(&dev
->dev
, "Interrupt %d, port %04x obtained\n", irq
, io
);
597 return devm_rc_register_device(&dev
->dev
, rcdev
);
600 static int serial_ir_open(struct rc_dev
*rcdev
)
604 /* initialize timestamp */
605 serial_ir
.lastkt
= ktime_get();
607 spin_lock_irqsave(&hardware
[type
].lock
, flags
);
610 soutp(UART_LCR
, sinp(UART_LCR
) & (~UART_LCR_DLAB
));
612 soutp(UART_IER
, sinp(UART_IER
) | UART_IER_MSI
);
614 spin_unlock_irqrestore(&hardware
[type
].lock
, flags
);
619 static void serial_ir_close(struct rc_dev
*rcdev
)
623 spin_lock_irqsave(&hardware
[type
].lock
, flags
);
626 soutp(UART_LCR
, sinp(UART_LCR
) & (~UART_LCR_DLAB
));
628 /* First of all, disable all interrupts */
629 soutp(UART_IER
, sinp(UART_IER
) &
630 (~(UART_IER_MSI
| UART_IER_RLSI
| UART_IER_THRI
| UART_IER_RDI
)));
631 spin_unlock_irqrestore(&hardware
[type
].lock
, flags
);
634 static int serial_ir_tx(struct rc_dev
*dev
, unsigned int *txbuf
,
642 spin_lock_irqsave(&hardware
[type
].lock
, flags
);
643 if (type
== IR_IRDEO
) {
649 for (i
= 0; i
< count
; i
++) {
651 hardware
[type
].send_space();
653 hardware
[type
].send_pulse(txbuf
[i
], edge
);
655 edge
= ktime_add_us(edge
, txbuf
[i
]);
656 delta
= ktime_us_delta(edge
, ktime_get());
658 spin_unlock_irqrestore(&hardware
[type
].lock
, flags
);
659 usleep_range(delta
- 25, delta
+ 25);
660 spin_lock_irqsave(&hardware
[type
].lock
, flags
);
661 } else if (delta
> 0) {
666 spin_unlock_irqrestore(&hardware
[type
].lock
, flags
);
670 static int serial_ir_tx_duty_cycle(struct rc_dev
*dev
, u32 cycle
)
672 serial_ir
.duty_cycle
= cycle
;
676 static int serial_ir_tx_carrier(struct rc_dev
*dev
, u32 carrier
)
678 if (carrier
> 500000 || carrier
< 20000)
681 serial_ir
.carrier
= carrier
;
685 static int serial_ir_suspend(struct platform_device
*dev
,
689 soutp(UART_LCR
, sinp(UART_LCR
) & (~UART_LCR_DLAB
));
691 /* Disable all interrupts */
692 soutp(UART_IER
, sinp(UART_IER
) &
693 (~(UART_IER_MSI
| UART_IER_RLSI
| UART_IER_THRI
| UART_IER_RDI
)));
695 /* Clear registers. */
704 static int serial_ir_resume(struct platform_device
*dev
)
709 result
= hardware_init_port();
713 spin_lock_irqsave(&hardware
[type
].lock
, flags
);
714 /* Enable Interrupt */
715 serial_ir
.lastkt
= ktime_get();
716 soutp(UART_IER
, sinp(UART_IER
) | UART_IER_MSI
);
719 spin_unlock_irqrestore(&hardware
[type
].lock
, flags
);
724 static struct platform_driver serial_ir_driver
= {
725 .probe
= serial_ir_probe
,
726 .suspend
= serial_ir_suspend
,
727 .resume
= serial_ir_resume
,
733 static int __init
serial_ir_init(void)
737 result
= platform_driver_register(&serial_ir_driver
);
741 serial_ir
.pdev
= platform_device_alloc("serial_ir", 0);
742 if (!serial_ir
.pdev
) {
744 goto exit_driver_unregister
;
747 result
= platform_device_add(serial_ir
.pdev
);
749 goto exit_device_put
;
754 platform_device_put(serial_ir
.pdev
);
755 exit_driver_unregister
:
756 platform_driver_unregister(&serial_ir_driver
);
760 static void serial_ir_exit(void)
762 platform_device_unregister(serial_ir
.pdev
);
763 platform_driver_unregister(&serial_ir_driver
);
766 static int __init
serial_ir_init_module(void)
771 case IR_IRDEO_REMOTE
:
774 /* if nothing specified, use ttyS0/com1 and irq 4 */
775 io
= io
? io
: 0x3f8;
785 hardware
[type
].set_send_carrier
= false;
786 hardware
[type
].set_duty_cycle
= false;
791 /* make sure sense is either -1, 0, or 1 */
795 return serial_ir_init();
798 static void __exit
serial_ir_exit_module(void)
800 del_timer_sync(&serial_ir
.timeout_timer
);
804 module_init(serial_ir_init_module
);
805 module_exit(serial_ir_exit_module
);
807 MODULE_DESCRIPTION("Infra-red receiver driver for serial ports.");
808 MODULE_AUTHOR("Ralph Metzler, Trent Piepho, Ben Pfaff, Christoph Bartelmus, Andrei Tanas");
809 MODULE_LICENSE("GPL");
811 module_param(type
, int, 0444);
812 MODULE_PARM_DESC(type
, "Hardware type (0 = home-brew, 1 = IRdeo, 2 = IRdeo Remote, 3 = AnimaX, 4 = IgorPlug");
814 module_param_hw(io
, int, ioport
, 0444);
815 MODULE_PARM_DESC(io
, "I/O address base (0x3f8 or 0x2f8)");
817 /* some architectures (e.g. intel xscale) have memory mapped registers */
818 module_param_hw(iommap
, ulong
, other
, 0444);
819 MODULE_PARM_DESC(iommap
, "physical base for memory mapped I/O (0 = no memory mapped io)");
822 * some architectures (e.g. intel xscale) align the 8bit serial registers
823 * on 32bit word boundaries.
824 * See linux-kernel/drivers/tty/serial/8250/8250.c serial_in()/out()
826 module_param_hw(ioshift
, int, other
, 0444);
827 MODULE_PARM_DESC(ioshift
, "shift I/O register offset (0 = no shift)");
829 module_param_hw(irq
, int, irq
, 0444);
830 MODULE_PARM_DESC(irq
, "Interrupt (4 or 3)");
832 module_param_hw(share_irq
, bool, other
, 0444);
833 MODULE_PARM_DESC(share_irq
, "Share interrupts (0 = off, 1 = on)");
835 module_param(sense
, int, 0444);
836 MODULE_PARM_DESC(sense
, "Override autodetection of IR receiver circuit (0 = active high, 1 = active low )");
838 #ifdef CONFIG_IR_SERIAL_TRANSMITTER
839 module_param(txsense
, bool, 0444);
840 MODULE_PARM_DESC(txsense
, "Sense of transmitter circuit (0 = active high, 1 = active low )");
843 module_param(softcarrier
, bool, 0444);
844 MODULE_PARM_DESC(softcarrier
, "Software carrier (0 = off, 1 = on, default on)");