1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * ImgTec IR Raw Decoder found in PowerDown Controller.
5 * Copyright 2010-2014 Imagination Technologies Ltd.
7 * This ties into the input subsystem using the RC-core in raw mode. Raw IR
8 * signal edges are reported and decoded by generic software decoders.
11 #include <linux/spinlock.h>
12 #include <media/rc-core.h>
15 #define ECHO_TIMEOUT_MS 150 /* ms between echos */
17 /* must be called with priv->lock held */
18 static void img_ir_refresh_raw(struct img_ir_priv *priv, u32 irq_status)
20 struct img_ir_priv_raw *raw = &priv->raw;
21 struct rc_dev *rc_dev = priv->raw.rdev;
25 /* find whether both rise and fall was detected */
26 multiple = ((irq_status & IMG_IR_IRQ_EDGE) == IMG_IR_IRQ_EDGE);
28 * If so, we need to see if the level has actually changed.
29 * If it's just noise that we didn't have time to process,
30 * there's no point reporting it.
32 ir_status = img_ir_read(priv, IMG_IR_STATUS) & IMG_IR_IRRXD;
33 if (multiple && ir_status == raw->last_status)
35 raw->last_status = ir_status;
37 /* report the edge to the IR raw decoders */
38 if (ir_status) /* low */
39 ir_raw_event_store_edge(rc_dev, false);
41 ir_raw_event_store_edge(rc_dev, true);
42 ir_raw_event_handle(rc_dev);
45 /* called with priv->lock held */
46 void img_ir_isr_raw(struct img_ir_priv *priv, u32 irq_status)
48 struct img_ir_priv_raw *raw = &priv->raw;
50 /* check not removing */
54 img_ir_refresh_raw(priv, irq_status);
56 /* start / push back the echo timer */
57 mod_timer(&raw->timer, jiffies + msecs_to_jiffies(ECHO_TIMEOUT_MS));
61 * Echo timer callback function.
62 * The raw decoders expect to get a final sample even if there are no edges, in
63 * order to be assured of the final space. If there are no edges for a certain
64 * time we use this timer to emit a final sample to satisfy them.
66 static void img_ir_echo_timer(struct timer_list *t)
68 struct img_ir_priv *priv = from_timer(priv, t, raw.timer);
70 spin_lock_irq(&priv->lock);
72 /* check not removing */
75 * It's safe to pass irq_status=0 since it's only used to check
78 img_ir_refresh_raw(priv, 0);
80 spin_unlock_irq(&priv->lock);
83 void img_ir_setup_raw(struct img_ir_priv *priv)
90 /* clear and enable edge interrupts */
91 spin_lock_irq(&priv->lock);
92 irq_en = img_ir_read(priv, IMG_IR_IRQ_ENABLE);
93 irq_en |= IMG_IR_IRQ_EDGE;
94 img_ir_write(priv, IMG_IR_IRQ_CLEAR, IMG_IR_IRQ_EDGE);
95 img_ir_write(priv, IMG_IR_IRQ_ENABLE, irq_en);
96 spin_unlock_irq(&priv->lock);
99 int img_ir_probe_raw(struct img_ir_priv *priv)
101 struct img_ir_priv_raw *raw = &priv->raw;
105 /* Set up the echo timer */
106 timer_setup(&raw->timer, img_ir_echo_timer, 0);
108 /* Allocate raw decoder */
109 raw->rdev = rdev = rc_allocate_device(RC_DRIVER_IR_RAW);
111 dev_err(priv->dev, "cannot allocate raw input device\n");
115 rdev->map_name = RC_MAP_EMPTY;
116 rdev->device_name = "IMG Infrared Decoder Raw";
118 /* Register raw decoder */
119 error = rc_register_device(rdev);
121 dev_err(priv->dev, "failed to register raw IR input device\n");
122 rc_free_device(rdev);
130 void img_ir_remove_raw(struct img_ir_priv *priv)
132 struct img_ir_priv_raw *raw = &priv->raw;
133 struct rc_dev *rdev = raw->rdev;
139 /* switch off and disable raw (edge) interrupts */
140 spin_lock_irq(&priv->lock);
142 irq_en = img_ir_read(priv, IMG_IR_IRQ_ENABLE);
143 irq_en &= ~IMG_IR_IRQ_EDGE;
144 img_ir_write(priv, IMG_IR_IRQ_ENABLE, irq_en);
145 img_ir_write(priv, IMG_IR_IRQ_CLEAR, IMG_IR_IRQ_EDGE);
146 spin_unlock_irq(&priv->lock);
148 rc_unregister_device(rdev);
150 del_timer_sync(&raw->timer);