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
6 This patch adds driver for sunxi IR controller.
7 It is based on Alexsey Shestacov's work based on the original driver
10 Signed-off-by: Alexander Bersenev <bay@hackerdom.ru>
11 Signed-off-by: Alexsey Shestacov <wingrime@linux-sunxi.org>
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
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
25 If you're not sure, select N here.
28 + tristate "SUNXI IR remote control"
30 + depends on ARCH_SUNXI
32 + Say Y if you want to use sunXi internal IR Controller
34 + To compile this driver as a module, choose M here: the module will
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
49 index 0000000..5971b69
51 +++ b/drivers/media/rc/sunxi-cir.c
54 + * Driver for Allwinner sunXi IR controller
56 + * Copyright (C) 2014 Alexsey Shestacov <wingrime@linux-sunxi.org>
57 + * Copyright (C) 2014 Alexander Bersenev <bay@hackerdom.ru>
59 + * Based on sun5i-ir.c:
60 + * Copyright (C) 2007-2012 Daniel Wang
61 + * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
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.
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.
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>
80 +#define SUNXI_IR_DEV "sunxi-ir"
84 +#define SUNXI_IR_CTL_REG 0x00
86 +#define REG_CTL_GEN BIT(0)
87 +/* RX block enable */
88 +#define REG_CTL_RXEN BIT(1)
90 +#define REG_CTL_MD (BIT(4) | BIT(5))
93 +#define SUNXI_IR_RXCTL_REG 0x10
94 +/* Pulse Polarity Invert flag */
95 +#define REG_RXCTL_RPPI BIT(2)
98 +#define SUNXI_IR_RXFIFO_REG 0x20
100 +/* Rx Interrupt Enable */
101 +#define SUNXI_IR_RXINT_REG 0x2C
102 +/* Rx FIFO Overflow */
103 +#define REG_RXINT_ROI_EN BIT(0)
105 +#define REG_RXINT_RPEI_EN BIT(1)
106 +/* Rx FIFO Data Available */
107 +#define REG_RXINT_RAI_EN BIT(4)
109 +/* Rx FIFO available byte level */
110 +#define REG_RXINT_RAL(val) (((val) << 8) & (GENMASK(11, 8)))
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
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)))
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
144 + spinlock_t ir_lock;
146 + void __iomem *base;
149 + struct clk *apb_clk;
150 + const char *map_name;
153 +static irqreturn_t sunxi_ir_irq(int irqno, void *dev_id)
155 + unsigned long status;
157 + unsigned int cnt, rc;
158 + struct sunxi_ir *ir = dev_id;
159 + DEFINE_IR_RAW_EVENT(rawir);
161 + spin_lock(&ir->ir_lock);
163 + status = readl(ir->base + SUNXI_IR_RXSTA_REG);
165 + /* clean all pending statuses */
166 + writel(status | REG_RXSTA_CLEARALL, ir->base + SUNXI_IR_RXSTA_REG);
168 + if (status & REG_RXINT_RAI_EN) {
169 + /* How many messages in fifo */
170 + rc = REG_RXSTA_GET_AC(status);
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);
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);
190 + spin_unlock(&ir->ir_lock);
192 + return IRQ_HANDLED;
195 +static int sunxi_ir_probe(struct platform_device *pdev)
198 + unsigned long tmp = 0;
200 + struct device *dev = &pdev->dev;
201 + struct device_node *dn = dev->of_node;
202 + struct resource *res;
203 + struct sunxi_ir *ir;
205 + ir = devm_kzalloc(dev, sizeof(struct sunxi_ir), GFP_KERNEL);
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);
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);
221 + ret = clk_set_rate(ir->clk, SUNXI_IR_BASE_CLK);
223 + dev_err(dev, "set ir base clock failed!\n");
227 + if (clk_prepare_enable(ir->apb_clk)) {
228 + dev_err(dev, "try to enable apb_ir_clk failed\n");
232 + if (clk_prepare_enable(ir->clk)) {
233 + dev_err(dev, "try to enable ir_clk failed\n");
235 + goto exit_clkdisable_apb_clk;
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;
247 + ir->rc = rc_allocate_device();
249 + dev_err(dev, "failed to allocate device\n");
251 + goto exit_clkdisable_clk;
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;
270 + ret = rc_register_device(ir->rc);
272 + dev_err(dev, "failed to register rc device\n");
273 + goto exit_free_dev;
276 + platform_set_drvdata(pdev, ir);
279 + ir->irq = platform_get_irq(pdev, 0);
281 + dev_err(dev, "no irq resource\n");
283 + goto exit_free_dev;
286 + ret = devm_request_irq(dev, ir->irq, sunxi_ir_irq, 0, SUNXI_IR_DEV, ir);
288 + dev_err(dev, "failed request irq\n");
289 + goto exit_free_dev;
292 + /* Enable CIR Mode */
293 + writel(REG_CTL_MD, ir->base+SUNXI_IR_CTL_REG);
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);
299 + /* Invert Input Signal */
300 + writel(REG_RXCTL_RPPI, ir->base + SUNXI_IR_RXCTL_REG);
302 + /* Clear All Rx Interrupt Status */
303 + writel(REG_RXSTA_CLEARALL, ir->base + SUNXI_IR_RXSTA_REG);
306 + * Enable IRQ on overflow, packet end, FIFO available with trigger
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);
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);
317 + dev_info(dev, "initialized sunXi IR driver\n");
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);
330 +static int sunxi_ir_remove(struct platform_device *pdev)
332 + unsigned long flags;
333 + struct sunxi_ir *ir = platform_get_drvdata(pdev);
335 + clk_disable_unprepare(ir->clk);
336 + clk_disable_unprepare(ir->apb_clk);
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);
344 + writel(0, ir->base + SUNXI_IR_CTL_REG);
345 + spin_unlock_irqrestore(&ir->ir_lock, flags);
347 + rc_unregister_device(ir->rc);
351 +static const struct of_device_id sunxi_ir_match[] = {
352 + { .compatible = "allwinner,sun7i-a20-ir", },
356 +static struct platform_driver sunxi_ir_driver = {
357 + .probe = sunxi_ir_probe,
358 + .remove = sunxi_ir_remove,
360 + .name = SUNXI_IR_DEV,
361 + .owner = THIS_MODULE,
362 + .of_match_table = sunxi_ir_match,
366 +module_platform_driver(sunxi_ir_driver);
368 +MODULE_DESCRIPTION("Allwinner sunXi IR controller driver");
369 +MODULE_AUTHOR("Alexsey Shestacov <wingrime@linux-sunxi.org>");
370 +MODULE_LICENSE("GPL");