dd0599e1fddb708f8d4bd29950cf266cc649c026
[15.05/openwrt.git] / target / linux / sunxi / patches-3.14 / 280-ir-add-driver.patch
1 From 601b6a88cd14e655ccd246fe122cbf496a891cbb Mon Sep 17 00:00:00 2001
2 From: Alexander Bersenev <bay@hackerdom.ru>
3 Date: Mon, 9 Jun 2014 00:08:10 +0600
4 Subject: [PATCH] rc: add sunxi-ir driver
5
6 This patch adds driver for sunxi IR controller.
7 It is based on Alexsey Shestacov's work based on the original driver
8 supplied by Allwinner.
9
10 Signed-off-by: Alexander Bersenev <bay@hackerdom.ru>
11 Signed-off-by: Alexsey Shestacov <wingrime@linux-sunxi.org>
12 ---
13  drivers/media/rc/Kconfig     |  10 ++
14  drivers/media/rc/Makefile    |   1 +
15  drivers/media/rc/sunxi-cir.c | 318 +++++++++++++++++++++++++++++++++++++++++++
16  3 files changed, 329 insertions(+)
17  create mode 100644 drivers/media/rc/sunxi-cir.c
18
19 diff --git a/drivers/media/rc/Kconfig b/drivers/media/rc/Kconfig
20 index 8fbd377..9427fad 100644
21 --- a/drivers/media/rc/Kconfig
22 +++ b/drivers/media/rc/Kconfig
23 @@ -343,4 +343,14 @@ config RC_ST
24  
25          If you're not sure, select N here.
26  
27 +config IR_SUNXI
28 +    tristate "SUNXI IR remote control"
29 +    depends on RC_CORE
30 +    depends on ARCH_SUNXI
31 +    ---help---
32 +      Say Y if you want to use sunXi internal IR Controller
33 +
34 +      To compile this driver as a module, choose M here: the module will
35 +      be called sunxi-ir.
36 +
37  endif #RC_DEVICES
38 diff --git a/drivers/media/rc/Makefile b/drivers/media/rc/Makefile
39 index f8b54ff..9ee9ee7 100644
40 --- a/drivers/media/rc/Makefile
41 +++ b/drivers/media/rc/Makefile
42 @@ -32,3 +32,4 @@ obj-$(CONFIG_IR_GPIO_CIR) += gpio-ir-recv.o
43  obj-$(CONFIG_IR_IGUANA) += iguanair.o
44  obj-$(CONFIG_IR_TTUSBIR) += ttusbir.o
45  obj-$(CONFIG_RC_ST) += st_rc.o
46 +obj-$(CONFIG_IR_SUNXI) += sunxi-cir.o
47 diff --git a/drivers/media/rc/sunxi-cir.c b/drivers/media/rc/sunxi-cir.c
48 new file mode 100644
49 index 0000000..5971b69
50 --- /dev/null
51 +++ b/drivers/media/rc/sunxi-cir.c
52 @@ -0,0 +1,318 @@
53 +/*
54 + * Driver for Allwinner sunXi IR controller
55 + *
56 + * Copyright (C) 2014 Alexsey Shestacov <wingrime@linux-sunxi.org>
57 + * Copyright (C) 2014 Alexander Bersenev <bay@hackerdom.ru>
58 + *
59 + * Based on sun5i-ir.c:
60 + * Copyright (C) 2007-2012 Daniel Wang
61 + * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
62 + *
63 + * This program is free software; you can redistribute it and/or
64 + * modify it under the terms of the GNU General Public License as
65 + * published by the Free Software Foundation; either version 2 of
66 + * the License, or (at your option) any later version.
67 + *
68 + * This program is distributed in the hope that it will be useful,
69 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
70 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
71 + * GNU General Public License for more details.
72 + */
73 +
74 +#include <linux/clk.h>
75 +#include <linux/interrupt.h>
76 +#include <linux/module.h>
77 +#include <linux/of_platform.h>
78 +#include <media/rc-core.h>
79 +
80 +#define SUNXI_IR_DEV "sunxi-ir"
81 +
82 +/* Registers */
83 +/* IR Control */
84 +#define SUNXI_IR_CTL_REG      0x00
85 +/* Global Enable */
86 +#define REG_CTL_GEN                    BIT(0)
87 +/* RX block enable */
88 +#define REG_CTL_RXEN                   BIT(1)
89 +/* CIR mode */
90 +#define REG_CTL_MD                     (BIT(4) | BIT(5))
91 +
92 +/* Rx Config */
93 +#define SUNXI_IR_RXCTL_REG    0x10
94 +/* Pulse Polarity Invert flag */
95 +#define REG_RXCTL_RPPI                 BIT(2)
96 +
97 +/* Rx Data */
98 +#define SUNXI_IR_RXFIFO_REG   0x20
99 +
100 +/* Rx Interrupt Enable */
101 +#define SUNXI_IR_RXINT_REG    0x2C
102 +/* Rx FIFO Overflow */
103 +#define REG_RXINT_ROI_EN               BIT(0)
104 +/* Rx Packet End */
105 +#define REG_RXINT_RPEI_EN              BIT(1)
106 +/* Rx FIFO Data Available */
107 +#define REG_RXINT_RAI_EN               BIT(4)
108 +
109 +/* Rx FIFO available byte level */
110 +#define REG_RXINT_RAL(val)    (((val) << 8) & (GENMASK(11, 8)))
111 +
112 +/* Rx Interrupt Status */
113 +#define SUNXI_IR_RXSTA_REG    0x30
114 +/* RX FIFO Get Available Counter */
115 +#define REG_RXSTA_GET_AC(val) (((val) >> 8) & (GENMASK(5, 0)))
116 +/* Clear all interrupt status value */
117 +#define REG_RXSTA_CLEARALL    0xff
118 +
119 +/* IR Sample Config */
120 +#define SUNXI_IR_CIR_REG      0x34
121 +/* CIR_REG register noise threshold */
122 +#define REG_CIR_NTHR(val)    (((val) << 2) & (GENMASK(7, 2)))
123 +/* CIR_REG register idle threshold */
124 +#define REG_CIR_ITHR(val)    (((val) << 8) & (GENMASK(15, 8)))
125 +
126 +/* Hardware supported fifo size */
127 +#define SUNXI_IR_FIFO_SIZE    16
128 +/* How many messages in FIFO trigger IRQ */
129 +#define TRIGGER_LEVEL         8
130 +/* Required frequency for IR0 or IR1 clock in CIR mode */
131 +#define SUNXI_IR_BASE_CLK     8000000
132 +/* Frequency after IR internal divider  */
133 +#define SUNXI_IR_CLK          (SUNXI_IR_BASE_CLK / 64)
134 +/* Sample period in ns */
135 +#define SUNXI_IR_SAMPLE       (1000000000ul / SUNXI_IR_CLK)
136 +/* Noise threshold in samples  */
137 +#define SUNXI_IR_RXNOISE      1
138 +/* Idle Threshold in samples */
139 +#define SUNXI_IR_RXIDLE       20
140 +/* Time after which device stops sending data in ms */
141 +#define SUNXI_IR_TIMEOUT      120
142 +
143 +struct sunxi_ir {
144 +       spinlock_t      ir_lock;
145 +       struct rc_dev   *rc;
146 +       void __iomem    *base;
147 +       int             irq;
148 +       struct clk      *clk;
149 +       struct clk      *apb_clk;
150 +       const char      *map_name;
151 +};
152 +
153 +static irqreturn_t sunxi_ir_irq(int irqno, void *dev_id)
154 +{
155 +       unsigned long status;
156 +       unsigned char dt;
157 +       unsigned int cnt, rc;
158 +       struct sunxi_ir *ir = dev_id;
159 +       DEFINE_IR_RAW_EVENT(rawir);
160 +
161 +       spin_lock(&ir->ir_lock);
162 +
163 +       status = readl(ir->base + SUNXI_IR_RXSTA_REG);
164 +
165 +       /* clean all pending statuses */
166 +       writel(status | REG_RXSTA_CLEARALL, ir->base + SUNXI_IR_RXSTA_REG);
167 +
168 +       if (status & REG_RXINT_RAI_EN) {
169 +               /* How many messages in fifo */
170 +               rc  = REG_RXSTA_GET_AC(status);
171 +               /* Sanity check */
172 +               rc = rc > SUNXI_IR_FIFO_SIZE ? SUNXI_IR_FIFO_SIZE : rc;
173 +               /* If we have data */
174 +               for (cnt = 0; cnt < rc; cnt++) {
175 +                       /* for each bit in fifo */
176 +                       dt = readb(ir->base + SUNXI_IR_RXFIFO_REG);
177 +                       rawir.pulse = (dt & 0x80) != 0;
178 +                       rawir.duration = ((dt & 0x7f) + 1) * SUNXI_IR_SAMPLE;
179 +                       ir_raw_event_store_with_filter(ir->rc, &rawir);
180 +               }
181 +       }
182 +
183 +       if (status & REG_RXINT_ROI_EN) {
184 +               ir_raw_event_reset(ir->rc);
185 +       } else if (status & REG_RXINT_RPEI_EN) {
186 +               ir_raw_event_set_idle(ir->rc, true);
187 +               ir_raw_event_handle(ir->rc);
188 +       }
189 +
190 +       spin_unlock(&ir->ir_lock);
191 +
192 +       return IRQ_HANDLED;
193 +}
194 +
195 +static int sunxi_ir_probe(struct platform_device *pdev)
196 +{
197 +       int ret = 0;
198 +       unsigned long tmp = 0;
199 +
200 +       struct device *dev = &pdev->dev;
201 +       struct device_node *dn = dev->of_node;
202 +       struct resource *res;
203 +       struct sunxi_ir *ir;
204 +
205 +       ir = devm_kzalloc(dev, sizeof(struct sunxi_ir), GFP_KERNEL);
206 +       if (!ir)
207 +               return -ENOMEM;
208 +
209 +       /* Clock */
210 +       ir->apb_clk = devm_clk_get(dev, "apb");
211 +       if (IS_ERR(ir->apb_clk)) {
212 +               dev_err(dev, "failed to get a apb clock.\n");
213 +               return PTR_ERR(ir->apb_clk);
214 +       }
215 +       ir->clk = devm_clk_get(dev, "ir");
216 +       if (IS_ERR(ir->clk)) {
217 +               dev_err(dev, "failed to get a ir clock.\n");
218 +               return PTR_ERR(ir->clk);
219 +       }
220 +
221 +       ret = clk_set_rate(ir->clk, SUNXI_IR_BASE_CLK);
222 +       if (ret) {
223 +               dev_err(dev, "set ir base clock failed!\n");
224 +               return ret;
225 +       }
226 +
227 +       if (clk_prepare_enable(ir->apb_clk)) {
228 +               dev_err(dev, "try to enable apb_ir_clk failed\n");
229 +               return -EINVAL;
230 +       }
231 +
232 +       if (clk_prepare_enable(ir->clk)) {
233 +               dev_err(dev, "try to enable ir_clk failed\n");
234 +               ret = -EINVAL;
235 +               goto exit_clkdisable_apb_clk;
236 +       }
237 +
238 +       /* IO */
239 +       res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
240 +       ir->base = devm_ioremap_resource(dev, res);
241 +       if (IS_ERR(ir->base)) {
242 +               dev_err(dev, "failed to map registers\n");
243 +               ret = PTR_ERR(ir->base);
244 +               goto exit_clkdisable_clk;
245 +       }
246 +
247 +       ir->rc = rc_allocate_device();
248 +       if (!ir->rc) {
249 +               dev_err(dev, "failed to allocate device\n");
250 +               ret = -ENOMEM;
251 +               goto exit_clkdisable_clk;
252 +       }
253 +
254 +       ir->rc->priv = ir;
255 +       ir->rc->input_name = SUNXI_IR_DEV;
256 +       ir->rc->input_phys = "sunxi-ir/input0";
257 +       ir->rc->input_id.bustype = BUS_HOST;
258 +       ir->rc->input_id.vendor = 0x0001;
259 +       ir->rc->input_id.product = 0x0001;
260 +       ir->rc->input_id.version = 0x0100;
261 +       ir->map_name = of_get_property(dn, "linux,rc-map-name", NULL);
262 +       ir->rc->map_name = ir->map_name ?: RC_MAP_EMPTY;
263 +       ir->rc->dev.parent = dev;
264 +       ir->rc->driver_type = RC_DRIVER_IR_RAW;
265 +       rc_set_allowed_protocols(ir->rc, RC_BIT_ALL);
266 +       ir->rc->rx_resolution = SUNXI_IR_SAMPLE;
267 +       ir->rc->timeout = MS_TO_NS(SUNXI_IR_TIMEOUT);
268 +       ir->rc->driver_name = SUNXI_IR_DEV;
269 +
270 +       ret = rc_register_device(ir->rc);
271 +       if (ret) {
272 +               dev_err(dev, "failed to register rc device\n");
273 +               goto exit_free_dev;
274 +       }
275 +
276 +       platform_set_drvdata(pdev, ir);
277 +
278 +       /* IRQ */
279 +       ir->irq = platform_get_irq(pdev, 0);
280 +       if (ir->irq < 0) {
281 +               dev_err(dev, "no irq resource\n");
282 +               ret = ir->irq;
283 +               goto exit_free_dev;
284 +       }
285 +
286 +       ret = devm_request_irq(dev, ir->irq, sunxi_ir_irq, 0, SUNXI_IR_DEV, ir);
287 +       if (ret) {
288 +               dev_err(dev, "failed request irq\n");
289 +               goto exit_free_dev;
290 +       }
291 +
292 +       /* Enable CIR Mode */
293 +       writel(REG_CTL_MD, ir->base+SUNXI_IR_CTL_REG);
294 +
295 +       /* Set noise threshold and idle threshold */
296 +       writel(REG_CIR_NTHR(SUNXI_IR_RXNOISE)|REG_CIR_ITHR(SUNXI_IR_RXIDLE),
297 +              ir->base + SUNXI_IR_CIR_REG);
298 +
299 +       /* Invert Input Signal */
300 +       writel(REG_RXCTL_RPPI, ir->base + SUNXI_IR_RXCTL_REG);
301 +
302 +       /* Clear All Rx Interrupt Status */
303 +       writel(REG_RXSTA_CLEARALL, ir->base + SUNXI_IR_RXSTA_REG);
304 +
305 +       /*
306 +        * Enable IRQ on overflow, packet end, FIFO available with trigger
307 +        * level
308 +        */
309 +       writel(REG_RXINT_ROI_EN | REG_RXINT_RPEI_EN |
310 +              REG_RXINT_RAI_EN | REG_RXINT_RAL(TRIGGER_LEVEL - 1),
311 +              ir->base + SUNXI_IR_RXINT_REG);
312 +
313 +       /* Enable IR Module */
314 +       tmp = readl(ir->base + SUNXI_IR_CTL_REG);
315 +       writel(tmp | REG_CTL_GEN | REG_CTL_RXEN, ir->base + SUNXI_IR_CTL_REG);
316 +
317 +       dev_info(dev, "initialized sunXi IR driver\n");
318 +       return 0;
319 +
320 +exit_free_dev:
321 +       rc_free_device(ir->rc);
322 +exit_clkdisable_clk:
323 +       clk_disable_unprepare(ir->clk);
324 +exit_clkdisable_apb_clk:
325 +       clk_disable_unprepare(ir->apb_clk);
326 +
327 +       return ret;
328 +}
329 +
330 +static int sunxi_ir_remove(struct platform_device *pdev)
331 +{
332 +       unsigned long flags;
333 +       struct sunxi_ir *ir = platform_get_drvdata(pdev);
334 +
335 +       clk_disable_unprepare(ir->clk);
336 +       clk_disable_unprepare(ir->apb_clk);
337 +
338 +       spin_lock_irqsave(&ir->ir_lock, flags);
339 +       /* disable IR IRQ */
340 +       writel(0, ir->base + SUNXI_IR_RXINT_REG);
341 +       /* clear All Rx Interrupt Status */
342 +       writel(REG_RXSTA_CLEARALL, ir->base + SUNXI_IR_RXSTA_REG);
343 +       /* disable IR */
344 +       writel(0, ir->base + SUNXI_IR_CTL_REG);
345 +       spin_unlock_irqrestore(&ir->ir_lock, flags);
346 +
347 +       rc_unregister_device(ir->rc);
348 +       return 0;
349 +}
350 +
351 +static const struct of_device_id sunxi_ir_match[] = {
352 +       { .compatible = "allwinner,sun7i-a20-ir", },
353 +       {},
354 +};
355 +
356 +static struct platform_driver sunxi_ir_driver = {
357 +       .probe          = sunxi_ir_probe,
358 +       .remove         = sunxi_ir_remove,
359 +       .driver = {
360 +               .name = SUNXI_IR_DEV,
361 +               .owner = THIS_MODULE,
362 +               .of_match_table = sunxi_ir_match,
363 +       },
364 +};
365 +
366 +module_platform_driver(sunxi_ir_driver);
367 +
368 +MODULE_DESCRIPTION("Allwinner sunXi IR controller driver");
369 +MODULE_AUTHOR("Alexsey Shestacov <wingrime@linux-sunxi.org>");
370 +MODULE_LICENSE("GPL");