gpio: rcar: Fix runtime PM imbalance on error
[linux/fpc-iii.git] / drivers / media / rc / sir_ir.c
blob80b3a6736dbdbf006f041b071d3c1de8776a3858
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * IR SIR driver, (C) 2000 Milan Pikula <www@fornax.sk>
5 * sir_ir - Device driver for use with SIR (serial infra red)
6 * mode of IrDA on many notebooks.
7 */
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #include <linux/module.h>
12 #include <linux/interrupt.h>
13 #include <linux/kernel.h>
14 #include <linux/serial_reg.h>
15 #include <linux/ktime.h>
16 #include <linux/delay.h>
17 #include <linux/platform_device.h>
19 #include <media/rc-core.h>
21 /* SECTION: Definitions */
22 #define PULSE '['
24 /* 9bit * 1s/115200bit in milli seconds = 78.125ms*/
25 #define TIME_CONST (9000000ul / 115200ul)
27 /* timeout for sequences in jiffies (=5/100s), must be longer than TIME_CONST */
28 #define SIR_TIMEOUT (HZ * 5 / 100)
30 /* onboard sir ports are typically com3 */
31 static int io = 0x3e8;
32 static int irq = 4;
33 static int threshold = 3;
35 static DEFINE_SPINLOCK(timer_lock);
36 static struct timer_list timerlist;
37 /* time of last signal change detected */
38 static ktime_t last;
39 /* time of last UART data ready interrupt */
40 static ktime_t last_intr_time;
41 static int last_value;
42 static struct rc_dev *rcdev;
44 static struct platform_device *sir_ir_dev;
46 static DEFINE_SPINLOCK(hardware_lock);
48 /* SECTION: Prototypes */
50 /* Communication with user-space */
51 static void add_read_queue(int flag, unsigned long val);
52 /* Hardware */
53 static irqreturn_t sir_interrupt(int irq, void *dev_id);
54 static void send_space(unsigned long len);
55 static void send_pulse(unsigned long len);
56 static int init_hardware(void);
57 static void drop_hardware(void);
58 /* Initialisation */
60 static inline unsigned int sinp(int offset)
62 return inb(io + offset);
65 static inline void soutp(int offset, int value)
67 outb(value, io + offset);
70 /* SECTION: Communication with user-space */
71 static int sir_tx_ir(struct rc_dev *dev, unsigned int *tx_buf,
72 unsigned int count)
74 unsigned long flags;
75 int i;
77 local_irq_save(flags);
78 for (i = 0; i < count;) {
79 if (tx_buf[i])
80 send_pulse(tx_buf[i]);
81 i++;
82 if (i >= count)
83 break;
84 if (tx_buf[i])
85 send_space(tx_buf[i]);
86 i++;
88 local_irq_restore(flags);
90 return count;
93 static void add_read_queue(int flag, unsigned long val)
95 struct ir_raw_event ev = {};
97 pr_debug("add flag %d with val %lu\n", flag, val);
100 * statistically, pulses are ~TIME_CONST/2 too long. we could
101 * maybe make this more exact, but this is good enough
103 if (flag) {
104 /* pulse */
105 if (val > TIME_CONST / 2)
106 val -= TIME_CONST / 2;
107 else /* should not ever happen */
108 val = 1;
109 ev.pulse = true;
110 } else {
111 val += TIME_CONST / 2;
113 ev.duration = US_TO_NS(val);
115 ir_raw_event_store_with_filter(rcdev, &ev);
118 /* SECTION: Hardware */
119 static void sir_timeout(struct timer_list *unused)
122 * if last received signal was a pulse, but receiving stopped
123 * within the 9 bit frame, we need to finish this pulse and
124 * simulate a signal change to from pulse to space. Otherwise
125 * upper layers will receive two sequences next time.
128 unsigned long flags;
129 unsigned long pulse_end;
131 /* avoid interference with interrupt */
132 spin_lock_irqsave(&timer_lock, flags);
133 if (last_value) {
134 /* clear unread bits in UART and restart */
135 outb(UART_FCR_CLEAR_RCVR, io + UART_FCR);
136 /* determine 'virtual' pulse end: */
137 pulse_end = min_t(unsigned long,
138 ktime_us_delta(last, last_intr_time),
139 IR_MAX_DURATION);
140 dev_dbg(&sir_ir_dev->dev, "timeout add %d for %lu usec\n",
141 last_value, pulse_end);
142 add_read_queue(last_value, pulse_end);
143 last_value = 0;
144 last = last_intr_time;
146 spin_unlock_irqrestore(&timer_lock, flags);
147 ir_raw_event_handle(rcdev);
150 static irqreturn_t sir_interrupt(int irq, void *dev_id)
152 unsigned char data;
153 ktime_t curr_time;
154 unsigned long delt;
155 unsigned long deltintr;
156 unsigned long flags;
157 int counter = 0;
158 int iir, lsr;
160 while ((iir = inb(io + UART_IIR) & UART_IIR_ID)) {
161 if (++counter > 256) {
162 dev_err(&sir_ir_dev->dev, "Trapped in interrupt");
163 break;
166 switch (iir & UART_IIR_ID) { /* FIXME toto treba preriedit */
167 case UART_IIR_MSI:
168 (void)inb(io + UART_MSR);
169 break;
170 case UART_IIR_RLSI:
171 case UART_IIR_THRI:
172 (void)inb(io + UART_LSR);
173 break;
174 case UART_IIR_RDI:
175 /* avoid interference with timer */
176 spin_lock_irqsave(&timer_lock, flags);
177 do {
178 del_timer(&timerlist);
179 data = inb(io + UART_RX);
180 curr_time = ktime_get();
181 delt = min_t(unsigned long,
182 ktime_us_delta(last, curr_time),
183 IR_MAX_DURATION);
184 deltintr = min_t(unsigned long,
185 ktime_us_delta(last_intr_time,
186 curr_time),
187 IR_MAX_DURATION);
188 dev_dbg(&sir_ir_dev->dev, "t %lu, d %d\n",
189 deltintr, (int)data);
191 * if nothing came in last X cycles,
192 * it was gap
194 if (deltintr > TIME_CONST * threshold) {
195 if (last_value) {
196 dev_dbg(&sir_ir_dev->dev, "GAP\n");
197 /* simulate signal change */
198 add_read_queue(last_value,
199 delt -
200 deltintr);
201 last_value = 0;
202 last = last_intr_time;
203 delt = deltintr;
206 data = 1;
207 if (data ^ last_value) {
209 * deltintr > 2*TIME_CONST, remember?
210 * the other case is timeout
212 add_read_queue(last_value,
213 delt - TIME_CONST);
214 last_value = data;
215 last = curr_time;
216 last = ktime_sub_us(last,
217 TIME_CONST);
219 last_intr_time = curr_time;
220 if (data) {
222 * start timer for end of
223 * sequence detection
225 timerlist.expires = jiffies +
226 SIR_TIMEOUT;
227 add_timer(&timerlist);
230 lsr = inb(io + UART_LSR);
231 } while (lsr & UART_LSR_DR); /* data ready */
232 spin_unlock_irqrestore(&timer_lock, flags);
233 break;
234 default:
235 break;
238 ir_raw_event_handle(rcdev);
239 return IRQ_RETVAL(IRQ_HANDLED);
242 static void send_space(unsigned long len)
244 usleep_range(len, len + 25);
247 static void send_pulse(unsigned long len)
249 long bytes_out = len / TIME_CONST;
251 if (bytes_out == 0)
252 bytes_out++;
254 while (bytes_out--) {
255 outb(PULSE, io + UART_TX);
256 /* FIXME treba seriozne cakanie z char/serial.c */
257 while (!(inb(io + UART_LSR) & UART_LSR_THRE))
262 static int init_hardware(void)
264 u8 scratch, scratch2, scratch3;
265 unsigned long flags;
267 spin_lock_irqsave(&hardware_lock, flags);
270 * This is a simple port existence test, borrowed from the autoconfig
271 * function in drivers/tty/serial/8250/8250_port.c
273 scratch = sinp(UART_IER);
274 soutp(UART_IER, 0);
275 #ifdef __i386__
276 outb(0xff, 0x080);
277 #endif
278 scratch2 = sinp(UART_IER) & 0x0f;
279 soutp(UART_IER, 0x0f);
280 #ifdef __i386__
281 outb(0x00, 0x080);
282 #endif
283 scratch3 = sinp(UART_IER) & 0x0f;
284 soutp(UART_IER, scratch);
285 if (scratch2 != 0 || scratch3 != 0x0f) {
286 /* we fail, there's nothing here */
287 spin_unlock_irqrestore(&hardware_lock, flags);
288 pr_err("port existence test failed, cannot continue\n");
289 return -ENODEV;
292 /* reset UART */
293 outb(0, io + UART_MCR);
294 outb(0, io + UART_IER);
295 /* init UART */
296 /* set DLAB, speed = 115200 */
297 outb(UART_LCR_DLAB | UART_LCR_WLEN7, io + UART_LCR);
298 outb(1, io + UART_DLL); outb(0, io + UART_DLM);
299 /* 7N1+start = 9 bits at 115200 ~ 3 bits at 44000 */
300 outb(UART_LCR_WLEN7, io + UART_LCR);
301 /* FIFO operation */
302 outb(UART_FCR_ENABLE_FIFO, io + UART_FCR);
303 /* interrupts */
304 /* outb(UART_IER_RLSI|UART_IER_RDI|UART_IER_THRI, io + UART_IER); */
305 outb(UART_IER_RDI, io + UART_IER);
306 /* turn on UART */
307 outb(UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2, io + UART_MCR);
308 spin_unlock_irqrestore(&hardware_lock, flags);
310 return 0;
313 static void drop_hardware(void)
315 unsigned long flags;
317 spin_lock_irqsave(&hardware_lock, flags);
319 /* turn off interrupts */
320 outb(0, io + UART_IER);
322 spin_unlock_irqrestore(&hardware_lock, flags);
325 /* SECTION: Initialisation */
326 static int sir_ir_probe(struct platform_device *dev)
328 int retval;
330 rcdev = devm_rc_allocate_device(&sir_ir_dev->dev, RC_DRIVER_IR_RAW);
331 if (!rcdev)
332 return -ENOMEM;
334 rcdev->device_name = "SIR IrDA port";
335 rcdev->input_phys = KBUILD_MODNAME "/input0";
336 rcdev->input_id.bustype = BUS_HOST;
337 rcdev->input_id.vendor = 0x0001;
338 rcdev->input_id.product = 0x0001;
339 rcdev->input_id.version = 0x0100;
340 rcdev->tx_ir = sir_tx_ir;
341 rcdev->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER;
342 rcdev->driver_name = KBUILD_MODNAME;
343 rcdev->map_name = RC_MAP_RC6_MCE;
344 rcdev->timeout = IR_DEFAULT_TIMEOUT;
345 rcdev->dev.parent = &sir_ir_dev->dev;
347 timer_setup(&timerlist, sir_timeout, 0);
349 /* get I/O port access and IRQ line */
350 if (!devm_request_region(&sir_ir_dev->dev, io, 8, KBUILD_MODNAME)) {
351 pr_err("i/o port 0x%.4x already in use.\n", io);
352 return -EBUSY;
354 retval = devm_request_irq(&sir_ir_dev->dev, irq, sir_interrupt, 0,
355 KBUILD_MODNAME, NULL);
356 if (retval < 0) {
357 pr_err("IRQ %d already in use.\n", irq);
358 return retval;
361 retval = init_hardware();
362 if (retval) {
363 del_timer_sync(&timerlist);
364 return retval;
367 pr_info("I/O port 0x%.4x, IRQ %d.\n", io, irq);
369 retval = devm_rc_register_device(&sir_ir_dev->dev, rcdev);
370 if (retval < 0)
371 return retval;
373 return 0;
376 static int sir_ir_remove(struct platform_device *dev)
378 drop_hardware();
379 del_timer_sync(&timerlist);
380 return 0;
383 static struct platform_driver sir_ir_driver = {
384 .probe = sir_ir_probe,
385 .remove = sir_ir_remove,
386 .driver = {
387 .name = "sir_ir",
391 static int __init sir_ir_init(void)
393 int retval;
395 retval = platform_driver_register(&sir_ir_driver);
396 if (retval)
397 return retval;
399 sir_ir_dev = platform_device_alloc("sir_ir", 0);
400 if (!sir_ir_dev) {
401 retval = -ENOMEM;
402 goto pdev_alloc_fail;
405 retval = platform_device_add(sir_ir_dev);
406 if (retval)
407 goto pdev_add_fail;
409 return 0;
411 pdev_add_fail:
412 platform_device_put(sir_ir_dev);
413 pdev_alloc_fail:
414 platform_driver_unregister(&sir_ir_driver);
415 return retval;
418 static void __exit sir_ir_exit(void)
420 platform_device_unregister(sir_ir_dev);
421 platform_driver_unregister(&sir_ir_driver);
424 module_init(sir_ir_init);
425 module_exit(sir_ir_exit);
427 MODULE_DESCRIPTION("Infrared receiver driver for SIR type serial ports");
428 MODULE_AUTHOR("Milan Pikula");
429 MODULE_LICENSE("GPL");
431 module_param_hw(io, int, ioport, 0444);
432 MODULE_PARM_DESC(io, "I/O address base (0x3f8 or 0x2f8)");
434 module_param_hw(irq, int, irq, 0444);
435 MODULE_PARM_DESC(irq, "Interrupt (4 or 3)");
437 module_param(threshold, int, 0444);
438 MODULE_PARM_DESC(threshold, "space detection threshold (3)");