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>
22 #include <linux/kernel.h>
23 #include <linux/serial_reg.h>
24 #include <linux/types.h>
25 #include <linux/delay.h>
26 #include <linux/platform_device.h>
27 #include <linux/spinlock.h>
28 #include <media/rc-core.h>
32 int signal_pin_change
;
35 unsigned set_send_carrier
:1;
36 unsigned set_duty_cycle
:1;
37 void (*send_pulse
)(unsigned int length
, ktime_t edge
);
38 void (*send_space
)(void);
44 #define IR_IRDEO_REMOTE 2
48 /* module parameters */
54 static bool softcarrier
= true;
55 static bool share_irq
;
56 static int sense
= -1; /* -1 = auto, 0 = active high, 1 = active low */
57 static bool txsense
; /* 0 = active high, 1 = active low */
59 /* forward declarations */
60 static void send_pulse_irdeo(unsigned int length
, ktime_t edge
);
61 static void send_space_irdeo(void);
62 #ifdef CONFIG_IR_SERIAL_TRANSMITTER
63 static void send_pulse_homebrew(unsigned int length
, ktime_t edge
);
64 static void send_space_homebrew(void);
67 static struct serial_ir_hw hardware
[] = {
69 .lock
= __SPIN_LOCK_UNLOCKED(hardware
[IR_HOMEBREW
].lock
),
70 .signal_pin
= UART_MSR_DCD
,
71 .signal_pin_change
= UART_MSR_DDCD
,
72 .on
= (UART_MCR_RTS
| UART_MCR_OUT2
| UART_MCR_DTR
),
73 .off
= (UART_MCR_RTS
| UART_MCR_OUT2
),
74 #ifdef CONFIG_IR_SERIAL_TRANSMITTER
75 .send_pulse
= send_pulse_homebrew
,
76 .send_space
= send_space_homebrew
,
77 .set_send_carrier
= true,
78 .set_duty_cycle
= true,
83 .lock
= __SPIN_LOCK_UNLOCKED(hardware
[IR_IRDEO
].lock
),
84 .signal_pin
= UART_MSR_DSR
,
85 .signal_pin_change
= UART_MSR_DDSR
,
87 .off
= (UART_MCR_RTS
| UART_MCR_DTR
| UART_MCR_OUT2
),
88 .send_pulse
= send_pulse_irdeo
,
89 .send_space
= send_space_irdeo
,
90 .set_duty_cycle
= true,
94 .lock
= __SPIN_LOCK_UNLOCKED(hardware
[IR_IRDEO_REMOTE
].lock
),
95 .signal_pin
= UART_MSR_DSR
,
96 .signal_pin_change
= UART_MSR_DDSR
,
97 .on
= (UART_MCR_RTS
| UART_MCR_DTR
| UART_MCR_OUT2
),
98 .off
= (UART_MCR_RTS
| UART_MCR_DTR
| UART_MCR_OUT2
),
99 .send_pulse
= send_pulse_irdeo
,
100 .send_space
= send_space_irdeo
,
101 .set_duty_cycle
= true,
105 .lock
= __SPIN_LOCK_UNLOCKED(hardware
[IR_ANIMAX
].lock
),
106 .signal_pin
= UART_MSR_DCD
,
107 .signal_pin_change
= UART_MSR_DDCD
,
109 .off
= (UART_MCR_RTS
| UART_MCR_DTR
| UART_MCR_OUT2
),
113 .lock
= __SPIN_LOCK_UNLOCKED(hardware
[IR_IGOR
].lock
),
114 .signal_pin
= UART_MSR_DSR
,
115 .signal_pin_change
= UART_MSR_DDSR
,
116 .on
= (UART_MCR_RTS
| UART_MCR_OUT2
| UART_MCR_DTR
),
117 .off
= (UART_MCR_RTS
| UART_MCR_OUT2
),
118 #ifdef CONFIG_IR_SERIAL_TRANSMITTER
119 .send_pulse
= send_pulse_homebrew
,
120 .send_space
= send_space_homebrew
,
121 .set_send_carrier
= true,
122 .set_duty_cycle
= true,
127 #define RS_ISR_PASS_LIMIT 256
131 struct rc_dev
*rcdev
;
132 struct platform_device
*pdev
;
133 struct timer_list timeout_timer
;
135 unsigned int carrier
;
136 unsigned int duty_cycle
;
139 static struct serial_ir serial_ir
;
141 /* fetch serial input packet (1 byte) from register offset */
142 static u8
sinp(int offset
)
145 /* the register is memory-mapped */
148 return inb(io
+ offset
);
151 /* write serial output packet (1 byte) of value to register offset */
152 static void soutp(int offset
, u8 value
)
155 /* the register is memory-mapped */
158 outb(value
, io
+ offset
);
164 soutp(UART_MCR
, hardware
[type
].off
);
166 soutp(UART_MCR
, hardware
[type
].on
);
169 static void off(void)
172 soutp(UART_MCR
, hardware
[type
].on
);
174 soutp(UART_MCR
, hardware
[type
].off
);
177 static void send_pulse_irdeo(unsigned int length
, ktime_t target
)
181 unsigned char output
;
182 unsigned char chunk
, shifted
;
184 /* how many bits have to be sent ? */
185 rawbits
= length
* 1152 / 10000;
186 if (serial_ir
.duty_cycle
> 50)
190 for (i
= 0, output
= 0x7f; rawbits
> 0; rawbits
-= 3) {
191 shifted
= chunk
<< (i
* 3);
193 output
&= (~shifted
);
196 soutp(UART_TX
, output
);
197 while (!(sinp(UART_LSR
) & UART_LSR_THRE
))
204 soutp(UART_TX
, output
);
205 while (!(sinp(UART_LSR
) & UART_LSR_TEMT
))
210 static void send_space_irdeo(void)
214 #ifdef CONFIG_IR_SERIAL_TRANSMITTER
215 static void send_pulse_homebrew_softcarrier(unsigned int length
, ktime_t edge
)
217 ktime_t now
, target
= ktime_add_us(edge
, length
);
219 * delta should never exceed 4 seconds and on m68k
220 * ndelay(s64) does not compile; so use s32 rather than s64.
223 unsigned int pulse
, space
;
225 /* Ensure the dividend fits into 32 bit */
226 pulse
= DIV_ROUND_CLOSEST(serial_ir
.duty_cycle
* (NSEC_PER_SEC
/ 100),
228 space
= DIV_ROUND_CLOSEST((100 - serial_ir
.duty_cycle
) *
229 (NSEC_PER_SEC
/ 100), serial_ir
.carrier
);
233 if (ktime_compare(now
, target
) >= 0)
236 edge
= ktime_add_ns(edge
, pulse
);
237 delta
= ktime_to_ns(ktime_sub(edge
, now
));
242 if (ktime_compare(now
, target
) >= 0)
244 edge
= ktime_add_ns(edge
, space
);
245 delta
= ktime_to_ns(ktime_sub(edge
, now
));
251 static void send_pulse_homebrew(unsigned int length
, ktime_t edge
)
254 send_pulse_homebrew_softcarrier(length
, edge
);
259 static void send_space_homebrew(void)
265 static void frbwrite(unsigned int l
, bool is_pulse
)
267 /* simple noise filter */
268 static unsigned int ptr
, pulse
, space
;
269 struct ir_raw_event ev
= {};
271 if (ptr
> 0 && is_pulse
) {
276 ir_raw_event_store_with_filter(serial_ir
.rcdev
, &ev
);
279 ir_raw_event_store_with_filter(serial_ir
.rcdev
, &ev
);
295 if (space
> IR_MAX_DURATION
)
296 space
= IR_MAX_DURATION
;
298 if (space
> IR_MAX_DURATION
)
299 space
= IR_MAX_DURATION
;
306 ir_raw_event_store_with_filter(serial_ir
.rcdev
, &ev
);
309 ir_raw_event_store_with_filter(serial_ir
.rcdev
, &ev
);
317 ir_raw_event_store_with_filter(serial_ir
.rcdev
, &ev
);
320 static irqreturn_t
serial_ir_irq_handler(int i
, void *blah
)
327 static int last_dcd
= -1;
329 if ((sinp(UART_IIR
) & UART_IIR_NO_INT
)) {
330 /* not our interrupt */
337 status
= sinp(UART_MSR
);
338 if (counter
> RS_ISR_PASS_LIMIT
) {
339 dev_err(&serial_ir
.pdev
->dev
, "Trapped in interrupt");
342 if ((status
& hardware
[type
].signal_pin_change
) &&
344 /* get current time */
348 * The driver needs to know if your receiver is
349 * active high or active low, or the space/pulse
350 * sense could be inverted.
353 /* calc time since last interrupt in nanoseconds */
354 dcd
= (status
& hardware
[type
].signal_pin
) ? 1 : 0;
356 if (dcd
== last_dcd
) {
357 dev_dbg(&serial_ir
.pdev
->dev
,
358 "ignoring spike: %d %d %lldns %lldns\n",
359 dcd
, sense
, ktime_to_ns(kt
),
360 ktime_to_ns(serial_ir
.lastkt
));
364 delkt
= ktime_sub(kt
, serial_ir
.lastkt
);
365 if (ktime_compare(delkt
, ktime_set(15, 0)) > 0) {
366 data
= IR_MAX_DURATION
; /* really long time */
367 if (!(dcd
^ sense
)) {
369 dev_err(&serial_ir
.pdev
->dev
,
370 "dcd unexpected: %d %d %lldns %lldns\n",
371 dcd
, sense
, ktime_to_ns(kt
),
372 ktime_to_ns(serial_ir
.lastkt
));
374 * detecting pulse while this
377 sense
= sense
? 0 : 1;
380 data
= ktime_to_us(delkt
);
382 frbwrite(data
, !(dcd
^ sense
));
383 serial_ir
.lastkt
= kt
;
386 } while (!(sinp(UART_IIR
) & UART_IIR_NO_INT
)); /* still pending ? */
388 mod_timer(&serial_ir
.timeout_timer
,
389 jiffies
+ usecs_to_jiffies(serial_ir
.rcdev
->timeout
));
391 ir_raw_event_handle(serial_ir
.rcdev
);
396 static int hardware_init_port(void)
398 u8 scratch
, scratch2
, scratch3
;
401 * This is a simple port existence test, borrowed from the autoconfig
402 * function in drivers/tty/serial/8250/8250_port.c
404 scratch
= sinp(UART_IER
);
409 scratch2
= sinp(UART_IER
) & 0x0f;
410 soutp(UART_IER
, 0x0f);
414 scratch3
= sinp(UART_IER
) & 0x0f;
415 soutp(UART_IER
, scratch
);
416 if (scratch2
!= 0 || scratch3
!= 0x0f) {
417 /* we fail, there's nothing here */
418 pr_err("port existence test failed, cannot continue\n");
423 soutp(UART_LCR
, sinp(UART_LCR
) & (~UART_LCR_DLAB
));
425 /* First of all, disable all interrupts */
426 soutp(UART_IER
, sinp(UART_IER
) &
427 (~(UART_IER_MSI
| UART_IER_RLSI
| UART_IER_THRI
| UART_IER_RDI
)));
429 /* Clear registers. */
435 /* Set line for power source */
438 /* Clear registers again to be sure. */
446 case IR_IRDEO_REMOTE
:
447 /* setup port to 7N1 @ 115200 Baud */
448 /* 7N1+start = 9 bits at 115200 ~ 3 bits at 38kHz */
451 soutp(UART_LCR
, sinp(UART_LCR
) | UART_LCR_DLAB
);
452 /* Set divisor to 1 => 115200 Baud */
455 /* Set DLAB 0 + 7N1 */
456 soutp(UART_LCR
, UART_LCR_WLEN7
);
457 /* THR interrupt already disabled at this point */
466 static void serial_ir_timeout(struct timer_list
*unused
)
468 struct ir_raw_event ev
= {
470 .duration
= serial_ir
.rcdev
->timeout
472 ir_raw_event_store_with_filter(serial_ir
.rcdev
, &ev
);
473 ir_raw_event_handle(serial_ir
.rcdev
);
476 /* Needed by serial_ir_probe() */
477 static int serial_ir_tx(struct rc_dev
*dev
, unsigned int *txbuf
,
479 static int serial_ir_tx_duty_cycle(struct rc_dev
*dev
, u32 cycle
);
480 static int serial_ir_tx_carrier(struct rc_dev
*dev
, u32 carrier
);
481 static int serial_ir_open(struct rc_dev
*rcdev
);
482 static void serial_ir_close(struct rc_dev
*rcdev
);
484 static int serial_ir_probe(struct platform_device
*dev
)
486 struct rc_dev
*rcdev
;
487 int i
, nlow
, nhigh
, result
;
489 rcdev
= devm_rc_allocate_device(&dev
->dev
, RC_DRIVER_IR_RAW
);
493 if (hardware
[type
].send_pulse
&& hardware
[type
].send_space
)
494 rcdev
->tx_ir
= serial_ir_tx
;
495 if (hardware
[type
].set_send_carrier
)
496 rcdev
->s_tx_carrier
= serial_ir_tx_carrier
;
497 if (hardware
[type
].set_duty_cycle
)
498 rcdev
->s_tx_duty_cycle
= serial_ir_tx_duty_cycle
;
502 rcdev
->device_name
= "Serial IR type home-brew";
505 rcdev
->device_name
= "Serial IR type IRdeo";
507 case IR_IRDEO_REMOTE
:
508 rcdev
->device_name
= "Serial IR type IRdeo remote";
511 rcdev
->device_name
= "Serial IR type AnimaX";
514 rcdev
->device_name
= "Serial IR type IgorPlug";
518 rcdev
->input_phys
= KBUILD_MODNAME
"/input0";
519 rcdev
->input_id
.bustype
= BUS_HOST
;
520 rcdev
->input_id
.vendor
= 0x0001;
521 rcdev
->input_id
.product
= 0x0001;
522 rcdev
->input_id
.version
= 0x0100;
523 rcdev
->open
= serial_ir_open
;
524 rcdev
->close
= serial_ir_close
;
525 rcdev
->dev
.parent
= &serial_ir
.pdev
->dev
;
526 rcdev
->allowed_protocols
= RC_PROTO_BIT_ALL_IR_DECODER
;
527 rcdev
->driver_name
= KBUILD_MODNAME
;
528 rcdev
->map_name
= RC_MAP_RC6_MCE
;
529 rcdev
->min_timeout
= 1;
530 rcdev
->timeout
= IR_DEFAULT_TIMEOUT
;
531 rcdev
->max_timeout
= 10 * IR_DEFAULT_TIMEOUT
;
532 rcdev
->rx_resolution
= 250;
534 serial_ir
.rcdev
= rcdev
;
536 timer_setup(&serial_ir
.timeout_timer
, serial_ir_timeout
, 0);
538 result
= devm_request_irq(&dev
->dev
, irq
, serial_ir_irq_handler
,
539 share_irq
? IRQF_SHARED
: 0,
540 KBUILD_MODNAME
, &hardware
);
542 if (result
== -EBUSY
)
543 dev_err(&dev
->dev
, "IRQ %d busy\n", irq
);
544 else if (result
== -EINVAL
)
545 dev_err(&dev
->dev
, "Bad irq number or handler\n");
549 /* Reserve io region. */
551 (devm_request_mem_region(&dev
->dev
, iommap
, 8UL << ioshift
,
552 KBUILD_MODNAME
) == NULL
)) ||
553 (!iommap
&& (devm_request_region(&dev
->dev
, io
, 8,
554 KBUILD_MODNAME
) == NULL
))) {
555 dev_err(&dev
->dev
, "port %04x already in use\n", io
);
556 dev_warn(&dev
->dev
, "use 'setserial /dev/ttySX uart none'\n");
558 "or compile the serial port driver as module and\n");
559 dev_warn(&dev
->dev
, "make sure this module is loaded first\n");
563 result
= hardware_init_port();
567 /* Initialize pulse/space widths */
568 serial_ir
.duty_cycle
= 50;
569 serial_ir
.carrier
= 38000;
571 /* If pin is high, then this must be an active low receiver. */
573 /* wait 1/2 sec for the power supply */
577 * probe 9 times every 0.04s, collect "votes" for
582 for (i
= 0; i
< 9; i
++) {
583 if (sinp(UART_MSR
) & hardware
[type
].signal_pin
)
589 sense
= nlow
>= nhigh
? 1 : 0;
590 dev_info(&dev
->dev
, "auto-detected active %s receiver\n",
591 sense
? "low" : "high");
593 dev_info(&dev
->dev
, "Manually using active %s receiver\n",
594 sense
? "low" : "high");
596 dev_dbg(&dev
->dev
, "Interrupt %d, port %04x obtained\n", irq
, io
);
598 return devm_rc_register_device(&dev
->dev
, rcdev
);
601 static int serial_ir_open(struct rc_dev
*rcdev
)
605 /* initialize timestamp */
606 serial_ir
.lastkt
= ktime_get();
608 spin_lock_irqsave(&hardware
[type
].lock
, flags
);
611 soutp(UART_LCR
, sinp(UART_LCR
) & (~UART_LCR_DLAB
));
613 soutp(UART_IER
, sinp(UART_IER
) | UART_IER_MSI
);
615 spin_unlock_irqrestore(&hardware
[type
].lock
, flags
);
620 static void serial_ir_close(struct rc_dev
*rcdev
)
624 spin_lock_irqsave(&hardware
[type
].lock
, flags
);
627 soutp(UART_LCR
, sinp(UART_LCR
) & (~UART_LCR_DLAB
));
629 /* First of all, disable all interrupts */
630 soutp(UART_IER
, sinp(UART_IER
) &
631 (~(UART_IER_MSI
| UART_IER_RLSI
| UART_IER_THRI
| UART_IER_RDI
)));
632 spin_unlock_irqrestore(&hardware
[type
].lock
, flags
);
635 static int serial_ir_tx(struct rc_dev
*dev
, unsigned int *txbuf
,
643 spin_lock_irqsave(&hardware
[type
].lock
, flags
);
644 if (type
== IR_IRDEO
) {
650 for (i
= 0; i
< count
; i
++) {
652 hardware
[type
].send_space();
654 hardware
[type
].send_pulse(txbuf
[i
], edge
);
656 edge
= ktime_add_us(edge
, txbuf
[i
]);
657 delta
= ktime_us_delta(edge
, ktime_get());
659 spin_unlock_irqrestore(&hardware
[type
].lock
, flags
);
660 usleep_range(delta
- 25, delta
+ 25);
661 spin_lock_irqsave(&hardware
[type
].lock
, flags
);
662 } else if (delta
> 0) {
667 spin_unlock_irqrestore(&hardware
[type
].lock
, flags
);
671 static int serial_ir_tx_duty_cycle(struct rc_dev
*dev
, u32 cycle
)
673 serial_ir
.duty_cycle
= cycle
;
677 static int serial_ir_tx_carrier(struct rc_dev
*dev
, u32 carrier
)
679 if (carrier
> 500000 || carrier
< 20000)
682 serial_ir
.carrier
= carrier
;
686 static int serial_ir_suspend(struct platform_device
*dev
,
690 soutp(UART_LCR
, sinp(UART_LCR
) & (~UART_LCR_DLAB
));
692 /* Disable all interrupts */
693 soutp(UART_IER
, sinp(UART_IER
) &
694 (~(UART_IER_MSI
| UART_IER_RLSI
| UART_IER_THRI
| UART_IER_RDI
)));
696 /* Clear registers. */
705 static int serial_ir_resume(struct platform_device
*dev
)
710 result
= hardware_init_port();
714 spin_lock_irqsave(&hardware
[type
].lock
, flags
);
715 /* Enable Interrupt */
716 serial_ir
.lastkt
= ktime_get();
717 soutp(UART_IER
, sinp(UART_IER
) | UART_IER_MSI
);
720 spin_unlock_irqrestore(&hardware
[type
].lock
, flags
);
725 static struct platform_driver serial_ir_driver
= {
726 .probe
= serial_ir_probe
,
727 .suspend
= serial_ir_suspend
,
728 .resume
= serial_ir_resume
,
734 static int __init
serial_ir_init(void)
738 result
= platform_driver_register(&serial_ir_driver
);
742 serial_ir
.pdev
= platform_device_alloc("serial_ir", 0);
743 if (!serial_ir
.pdev
) {
745 goto exit_driver_unregister
;
748 result
= platform_device_add(serial_ir
.pdev
);
750 goto exit_device_put
;
755 platform_device_put(serial_ir
.pdev
);
756 exit_driver_unregister
:
757 platform_driver_unregister(&serial_ir_driver
);
761 static void serial_ir_exit(void)
763 platform_device_unregister(serial_ir
.pdev
);
764 platform_driver_unregister(&serial_ir_driver
);
767 static int __init
serial_ir_init_module(void)
772 case IR_IRDEO_REMOTE
:
775 /* if nothing specified, use ttyS0/com1 and irq 4 */
776 io
= io
? io
: 0x3f8;
786 hardware
[type
].set_send_carrier
= false;
787 hardware
[type
].set_duty_cycle
= false;
792 /* make sure sense is either -1, 0, or 1 */
796 return serial_ir_init();
799 static void __exit
serial_ir_exit_module(void)
801 del_timer_sync(&serial_ir
.timeout_timer
);
805 module_init(serial_ir_init_module
);
806 module_exit(serial_ir_exit_module
);
808 MODULE_DESCRIPTION("Infra-red receiver driver for serial ports.");
809 MODULE_AUTHOR("Ralph Metzler, Trent Piepho, Ben Pfaff, Christoph Bartelmus, Andrei Tanas");
810 MODULE_LICENSE("GPL");
812 module_param(type
, int, 0444);
813 MODULE_PARM_DESC(type
, "Hardware type (0 = home-brew, 1 = IRdeo, 2 = IRdeo Remote, 3 = AnimaX, 4 = IgorPlug");
815 module_param_hw(io
, int, ioport
, 0444);
816 MODULE_PARM_DESC(io
, "I/O address base (0x3f8 or 0x2f8)");
818 /* some architectures (e.g. intel xscale) have memory mapped registers */
819 module_param_hw(iommap
, ulong
, other
, 0444);
820 MODULE_PARM_DESC(iommap
, "physical base for memory mapped I/O (0 = no memory mapped io)");
823 * some architectures (e.g. intel xscale) align the 8bit serial registers
824 * on 32bit word boundaries.
825 * See linux-kernel/drivers/tty/serial/8250/8250.c serial_in()/out()
827 module_param_hw(ioshift
, int, other
, 0444);
828 MODULE_PARM_DESC(ioshift
, "shift I/O register offset (0 = no shift)");
830 module_param_hw(irq
, int, irq
, 0444);
831 MODULE_PARM_DESC(irq
, "Interrupt (4 or 3)");
833 module_param_hw(share_irq
, bool, other
, 0444);
834 MODULE_PARM_DESC(share_irq
, "Share interrupts (0 = off, 1 = on)");
836 module_param(sense
, int, 0444);
837 MODULE_PARM_DESC(sense
, "Override autodetection of IR receiver circuit (0 = active high, 1 = active low )");
839 #ifdef CONFIG_IR_SERIAL_TRANSMITTER
840 module_param(txsense
, bool, 0444);
841 MODULE_PARM_DESC(txsense
, "Sense of transmitter circuit (0 = active high, 1 = active low )");
844 module_param(softcarrier
, bool, 0444);
845 MODULE_PARM_DESC(softcarrier
, "Software carrier (0 = off, 1 = on, default on)");