blob: 7eb3f4f1ddcd670d501432528a2472dc0775d55f [file] [log] [blame]
Beniamino Galvani12ddbad2014-11-18 17:22:34 -03001/*
2 * Driver for Amlogic Meson IR remote receiver
3 *
4 * Copyright (C) 2014 Beniamino Galvani <b.galvani@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * version 2 as published by the Free Software Foundation.
9 *
10 * You should have received a copy of the GNU General Public License
11 * along with this program. If not, see <http://www.gnu.org/licenses/>.
12 */
13
14#include <linux/device.h>
15#include <linux/err.h>
16#include <linux/interrupt.h>
17#include <linux/io.h>
18#include <linux/module.h>
19#include <linux/of_platform.h>
20#include <linux/platform_device.h>
21#include <linux/spinlock.h>
22
23#include <media/rc-core.h>
24
25#define DRIVER_NAME "meson-ir"
26
Neil Armstrong6edf27e2016-08-20 11:54:22 +020027/* valid on all Meson platforms */
Beniamino Galvani12ddbad2014-11-18 17:22:34 -030028#define IR_DEC_LDR_ACTIVE 0x00
29#define IR_DEC_LDR_IDLE 0x04
30#define IR_DEC_LDR_REPEAT 0x08
31#define IR_DEC_BIT_0 0x0c
32#define IR_DEC_REG0 0x10
33#define IR_DEC_FRAME 0x14
34#define IR_DEC_STATUS 0x18
35#define IR_DEC_REG1 0x1c
Neil Armstrong6edf27e2016-08-20 11:54:22 +020036/* only available on Meson 8b and newer */
37#define IR_DEC_REG2 0x20
Beniamino Galvani12ddbad2014-11-18 17:22:34 -030038
39#define REG0_RATE_MASK (BIT(11) - 1)
40
Neil Armstrong6edf27e2016-08-20 11:54:22 +020041#define DECODE_MODE_NEC 0x0
42#define DECODE_MODE_RAW 0x2
43
44/* Meson 6b uses REG1 to configure the mode */
45#define REG1_MODE_MASK GENMASK(8, 7)
46#define REG1_MODE_SHIFT 7
47
48/* Meson 8b / GXBB use REG2 to configure the mode */
49#define REG2_MODE_MASK GENMASK(3, 0)
50#define REG2_MODE_SHIFT 0
Beniamino Galvani12ddbad2014-11-18 17:22:34 -030051
52#define REG1_TIME_IV_SHIFT 16
53#define REG1_TIME_IV_MASK ((BIT(13) - 1) << REG1_TIME_IV_SHIFT)
54
55#define REG1_IRQSEL_MASK (BIT(2) | BIT(3))
56#define REG1_IRQSEL_NEC_MODE (0 << 2)
57#define REG1_IRQSEL_RISE_FALL (1 << 2)
58#define REG1_IRQSEL_FALL (2 << 2)
59#define REG1_IRQSEL_RISE (3 << 2)
60
61#define REG1_RESET BIT(0)
62#define REG1_ENABLE BIT(15)
63
64#define STATUS_IR_DEC_IN BIT(8)
65
66#define MESON_TRATE 10 /* us */
67
68struct meson_ir {
69 void __iomem *reg;
70 struct rc_dev *rc;
71 int irq;
72 spinlock_t lock;
73};
74
75static void meson_ir_set_mask(struct meson_ir *ir, unsigned int reg,
76 u32 mask, u32 value)
77{
78 u32 data;
79
80 data = readl(ir->reg + reg);
81 data &= ~mask;
82 data |= (value & mask);
83 writel(data, ir->reg + reg);
84}
85
86static irqreturn_t meson_ir_irq(int irqno, void *dev_id)
87{
88 struct meson_ir *ir = dev_id;
89 u32 duration;
90 DEFINE_IR_RAW_EVENT(rawir);
91
92 spin_lock(&ir->lock);
93
94 duration = readl(ir->reg + IR_DEC_REG1);
95 duration = (duration & REG1_TIME_IV_MASK) >> REG1_TIME_IV_SHIFT;
96 rawir.duration = US_TO_NS(duration * MESON_TRATE);
97
98 rawir.pulse = !!(readl(ir->reg + IR_DEC_STATUS) & STATUS_IR_DEC_IN);
99
100 ir_raw_event_store_with_filter(ir->rc, &rawir);
101 ir_raw_event_handle(ir->rc);
102
103 spin_unlock(&ir->lock);
104
105 return IRQ_HANDLED;
106}
107
108static int meson_ir_probe(struct platform_device *pdev)
109{
110 struct device *dev = &pdev->dev;
111 struct device_node *node = dev->of_node;
112 struct resource *res;
113 const char *map_name;
114 struct meson_ir *ir;
115 int ret;
116
117 ir = devm_kzalloc(dev, sizeof(struct meson_ir), GFP_KERNEL);
118 if (!ir)
119 return -ENOMEM;
120
121 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
122 ir->reg = devm_ioremap_resource(dev, res);
123 if (IS_ERR(ir->reg)) {
124 dev_err(dev, "failed to map registers\n");
125 return PTR_ERR(ir->reg);
126 }
127
128 ir->irq = platform_get_irq(pdev, 0);
129 if (ir->irq < 0) {
130 dev_err(dev, "no irq resource\n");
131 return ir->irq;
132 }
133
134 ir->rc = rc_allocate_device();
135 if (!ir->rc) {
136 dev_err(dev, "failed to allocate rc device\n");
137 return -ENOMEM;
138 }
139
140 ir->rc->priv = ir;
141 ir->rc->input_name = DRIVER_NAME;
142 ir->rc->input_phys = DRIVER_NAME "/input0";
143 ir->rc->input_id.bustype = BUS_HOST;
144 map_name = of_get_property(node, "linux,rc-map-name", NULL);
145 ir->rc->map_name = map_name ? map_name : RC_MAP_EMPTY;
146 ir->rc->dev.parent = dev;
147 ir->rc->driver_type = RC_DRIVER_IR_RAW;
148 ir->rc->allowed_protocols = RC_BIT_ALL;
149 ir->rc->rx_resolution = US_TO_NS(MESON_TRATE);
150 ir->rc->timeout = MS_TO_NS(200);
151 ir->rc->driver_name = DRIVER_NAME;
152
153 spin_lock_init(&ir->lock);
154 platform_set_drvdata(pdev, ir);
155
156 ret = rc_register_device(ir->rc);
157 if (ret) {
158 dev_err(dev, "failed to register rc device\n");
159 goto out_free;
160 }
161
162 ret = devm_request_irq(dev, ir->irq, meson_ir_irq, 0, "ir-meson", ir);
163 if (ret) {
164 dev_err(dev, "failed to request irq\n");
165 goto out_unreg;
166 }
167
168 /* Reset the decoder */
169 meson_ir_set_mask(ir, IR_DEC_REG1, REG1_RESET, REG1_RESET);
170 meson_ir_set_mask(ir, IR_DEC_REG1, REG1_RESET, 0);
Neil Armstrong6edf27e2016-08-20 11:54:22 +0200171
172 /* Set general operation mode (= raw/software decoding) */
173 if (of_device_is_compatible(node, "amlogic,meson6-ir"))
174 meson_ir_set_mask(ir, IR_DEC_REG1, REG1_MODE_MASK,
175 DECODE_MODE_RAW << REG1_MODE_SHIFT);
176 else
177 meson_ir_set_mask(ir, IR_DEC_REG2, REG2_MODE_MASK,
178 DECODE_MODE_RAW << REG2_MODE_SHIFT);
179
Beniamino Galvani12ddbad2014-11-18 17:22:34 -0300180 /* Set rate */
181 meson_ir_set_mask(ir, IR_DEC_REG0, REG0_RATE_MASK, MESON_TRATE - 1);
182 /* IRQ on rising and falling edges */
183 meson_ir_set_mask(ir, IR_DEC_REG1, REG1_IRQSEL_MASK,
184 REG1_IRQSEL_RISE_FALL);
185 /* Enable the decoder */
186 meson_ir_set_mask(ir, IR_DEC_REG1, REG1_ENABLE, REG1_ENABLE);
187
188 dev_info(dev, "receiver initialized\n");
189
190 return 0;
191out_unreg:
192 rc_unregister_device(ir->rc);
193 ir->rc = NULL;
194out_free:
195 rc_free_device(ir->rc);
196
197 return ret;
198}
199
200static int meson_ir_remove(struct platform_device *pdev)
201{
202 struct meson_ir *ir = platform_get_drvdata(pdev);
203 unsigned long flags;
204
205 /* Disable the decoder */
206 spin_lock_irqsave(&ir->lock, flags);
207 meson_ir_set_mask(ir, IR_DEC_REG1, REG1_ENABLE, 0);
208 spin_unlock_irqrestore(&ir->lock, flags);
209
210 rc_unregister_device(ir->rc);
211
212 return 0;
213}
214
215static const struct of_device_id meson_ir_match[] = {
216 { .compatible = "amlogic,meson6-ir" },
Neil Armstrong6edf27e2016-08-20 11:54:22 +0200217 { .compatible = "amlogic,meson8b-ir" },
218 { .compatible = "amlogic,meson-gxbb-ir" },
Beniamino Galvani12ddbad2014-11-18 17:22:34 -0300219 { },
220};
Javier Martinez Canillas5bb50fe2016-10-17 13:44:10 -0200221MODULE_DEVICE_TABLE(of, meson_ir_match);
Beniamino Galvani12ddbad2014-11-18 17:22:34 -0300222
223static struct platform_driver meson_ir_driver = {
224 .probe = meson_ir_probe,
225 .remove = meson_ir_remove,
226 .driver = {
227 .name = DRIVER_NAME,
228 .of_match_table = meson_ir_match,
229 },
230};
231
232module_platform_driver(meson_ir_driver);
233
234MODULE_DESCRIPTION("Amlogic Meson IR remote receiver driver");
235MODULE_AUTHOR("Beniamino Galvani <b.galvani@gmail.com>");
236MODULE_LICENSE("GPL v2");