1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for Allwinner sunXi IR controller
9 * Copyright (C) 2007-2012 Daniel Wang
10 * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
13 #include <linux/clk.h>
14 #include <linux/interrupt.h>
15 #include <linux/module.h>
16 #include <linux/of_platform.h>
17 #include <linux/reset.h>
18 #include <media/rc-core.h>
20 #define SUNXI_IR_DEV "sunxi-ir"
24 #define SUNXI_IR_CTL_REG 0x00
26 #define REG_CTL_GEN BIT(0)
28 #define REG_CTL_RXEN BIT(1)
30 #define REG_CTL_MD (BIT(4) | BIT(5))
33 #define SUNXI_IR_RXCTL_REG 0x10
34 /* Pulse Polarity Invert flag */
35 #define REG_RXCTL_RPPI BIT(2)
38 #define SUNXI_IR_RXFIFO_REG 0x20
40 /* Rx Interrupt Enable */
41 #define SUNXI_IR_RXINT_REG 0x2C
42 /* Rx FIFO Overflow Interrupt Enable */
43 #define REG_RXINT_ROI_EN BIT(0)
44 /* Rx Packet End Interrupt Enable */
45 #define REG_RXINT_RPEI_EN BIT(1)
46 /* Rx FIFO Data Available Interrupt Enable */
47 #define REG_RXINT_RAI_EN BIT(4)
49 /* Rx FIFO available byte level */
50 #define REG_RXINT_RAL(val) ((val) << 8)
52 /* Rx Interrupt Status */
53 #define SUNXI_IR_RXSTA_REG 0x30
54 /* Rx FIFO Overflow */
55 #define REG_RXSTA_ROI REG_RXINT_ROI_EN
57 #define REG_RXSTA_RPE REG_RXINT_RPEI_EN
58 /* Rx FIFO Data Available */
59 #define REG_RXSTA_RA REG_RXINT_RAI_EN
60 /* RX FIFO Get Available Counter */
61 #define REG_RXSTA_GET_AC(val) (((val) >> 8) & (ir->fifo_size * 2 - 1))
62 /* Clear all interrupt status value */
63 #define REG_RXSTA_CLEARALL 0xff
65 /* IR Sample Config */
66 #define SUNXI_IR_CIR_REG 0x34
67 /* CIR_REG register noise threshold */
68 #define REG_CIR_NTHR(val) (((val) << 2) & (GENMASK(7, 2)))
69 /* CIR_REG register idle threshold */
70 #define REG_CIR_ITHR(val) (((val) << 8) & (GENMASK(15, 8)))
72 /* Required frequency for IR0 or IR1 clock in CIR mode (default) */
73 #define SUNXI_IR_BASE_CLK 8000000
74 /* Noise threshold in samples */
75 #define SUNXI_IR_RXNOISE 1
78 * struct sunxi_ir_quirks - Differences between SoC variants.
80 * @has_reset: SoC needs reset deasserted.
81 * @fifo_size: size of the fifo.
83 struct sunxi_ir_quirks {
95 struct reset_control *rst;
99 static irqreturn_t sunxi_ir_irq(int irqno, void *dev_id)
101 unsigned long status;
103 unsigned int cnt, rc;
104 struct sunxi_ir *ir = dev_id;
105 struct ir_raw_event rawir = {};
107 status = readl(ir->base + SUNXI_IR_RXSTA_REG);
109 /* clean all pending statuses */
110 writel(status | REG_RXSTA_CLEARALL, ir->base + SUNXI_IR_RXSTA_REG);
112 if (status & (REG_RXSTA_RA | REG_RXSTA_RPE)) {
113 /* How many messages in fifo */
114 rc = REG_RXSTA_GET_AC(status);
116 rc = rc > ir->fifo_size ? ir->fifo_size : rc;
117 /* If we have data */
118 for (cnt = 0; cnt < rc; cnt++) {
119 /* for each bit in fifo */
120 dt = readb(ir->base + SUNXI_IR_RXFIFO_REG);
121 rawir.pulse = (dt & 0x80) != 0;
122 rawir.duration = ((dt & 0x7f) + 1) *
123 ir->rc->rx_resolution;
124 ir_raw_event_store_with_filter(ir->rc, &rawir);
128 if (status & REG_RXSTA_ROI) {
129 ir_raw_event_reset(ir->rc);
130 } else if (status & REG_RXSTA_RPE) {
131 ir_raw_event_set_idle(ir->rc, true);
132 ir_raw_event_handle(ir->rc);
134 ir_raw_event_handle(ir->rc);
140 /* Convert idle threshold to usec */
141 static unsigned int sunxi_ithr_to_usec(unsigned int base_clk, unsigned int ithr)
143 return DIV_ROUND_CLOSEST(USEC_PER_SEC * (ithr + 1),
144 base_clk / (128 * 64));
147 /* Convert usec to idle threshold */
148 static unsigned int sunxi_usec_to_ithr(unsigned int base_clk, unsigned int usec)
150 /* make sure we don't end up with a timeout less than requested */
151 return DIV_ROUND_UP((base_clk / (128 * 64)) * usec, USEC_PER_SEC) - 1;
154 static int sunxi_ir_set_timeout(struct rc_dev *rc_dev, unsigned int timeout)
156 struct sunxi_ir *ir = rc_dev->priv;
157 unsigned int base_clk = clk_get_rate(ir->clk);
159 unsigned int ithr = sunxi_usec_to_ithr(base_clk, timeout);
161 dev_dbg(rc_dev->dev.parent, "setting idle threshold to %u\n", ithr);
163 /* Set noise threshold and idle threshold */
164 writel(REG_CIR_NTHR(SUNXI_IR_RXNOISE) | REG_CIR_ITHR(ithr),
165 ir->base + SUNXI_IR_CIR_REG);
167 rc_dev->timeout = sunxi_ithr_to_usec(base_clk, ithr);
172 static int sunxi_ir_hw_init(struct device *dev)
174 struct sunxi_ir *ir = dev_get_drvdata(dev);
178 ret = reset_control_deassert(ir->rst);
182 ret = clk_prepare_enable(ir->apb_clk);
184 dev_err(dev, "failed to enable apb clk\n");
185 goto exit_assert_reset;
188 ret = clk_prepare_enable(ir->clk);
190 dev_err(dev, "failed to enable ir clk\n");
191 goto exit_disable_apb_clk;
194 /* Enable CIR Mode */
195 writel(REG_CTL_MD, ir->base + SUNXI_IR_CTL_REG);
197 /* Set noise threshold and idle threshold */
198 sunxi_ir_set_timeout(ir->rc, ir->rc->timeout);
200 /* Invert Input Signal */
201 writel(REG_RXCTL_RPPI, ir->base + SUNXI_IR_RXCTL_REG);
203 /* Clear All Rx Interrupt Status */
204 writel(REG_RXSTA_CLEARALL, ir->base + SUNXI_IR_RXSTA_REG);
207 * Enable IRQ on overflow, packet end, FIFO available with trigger
210 writel(REG_RXINT_ROI_EN | REG_RXINT_RPEI_EN |
211 REG_RXINT_RAI_EN | REG_RXINT_RAL(ir->fifo_size / 2 - 1),
212 ir->base + SUNXI_IR_RXINT_REG);
214 /* Enable IR Module */
215 tmp = readl(ir->base + SUNXI_IR_CTL_REG);
216 writel(tmp | REG_CTL_GEN | REG_CTL_RXEN, ir->base + SUNXI_IR_CTL_REG);
220 exit_disable_apb_clk:
221 clk_disable_unprepare(ir->apb_clk);
223 reset_control_assert(ir->rst);
228 static void sunxi_ir_hw_exit(struct device *dev)
230 struct sunxi_ir *ir = dev_get_drvdata(dev);
232 clk_disable_unprepare(ir->clk);
233 clk_disable_unprepare(ir->apb_clk);
234 reset_control_assert(ir->rst);
237 static int __maybe_unused sunxi_ir_suspend(struct device *dev)
239 sunxi_ir_hw_exit(dev);
244 static int __maybe_unused sunxi_ir_resume(struct device *dev)
246 return sunxi_ir_hw_init(dev);
249 static SIMPLE_DEV_PM_OPS(sunxi_ir_pm_ops, sunxi_ir_suspend, sunxi_ir_resume);
251 static int sunxi_ir_probe(struct platform_device *pdev)
255 struct device *dev = &pdev->dev;
256 struct device_node *dn = dev->of_node;
257 const struct sunxi_ir_quirks *quirks;
259 u32 b_clk_freq = SUNXI_IR_BASE_CLK;
261 ir = devm_kzalloc(dev, sizeof(struct sunxi_ir), GFP_KERNEL);
265 quirks = of_device_get_match_data(&pdev->dev);
267 dev_err(&pdev->dev, "Failed to determine the quirks to use\n");
271 ir->fifo_size = quirks->fifo_size;
274 ir->apb_clk = devm_clk_get(dev, "apb");
275 if (IS_ERR(ir->apb_clk)) {
276 dev_err(dev, "failed to get a apb clock.\n");
277 return PTR_ERR(ir->apb_clk);
279 ir->clk = devm_clk_get(dev, "ir");
280 if (IS_ERR(ir->clk)) {
281 dev_err(dev, "failed to get a ir clock.\n");
282 return PTR_ERR(ir->clk);
285 /* Base clock frequency (optional) */
286 of_property_read_u32(dn, "clock-frequency", &b_clk_freq);
289 if (quirks->has_reset) {
290 ir->rst = devm_reset_control_get_exclusive(dev, NULL);
292 return PTR_ERR(ir->rst);
295 ret = clk_set_rate(ir->clk, b_clk_freq);
297 dev_err(dev, "set ir base clock failed!\n");
300 dev_dbg(dev, "set base clock frequency to %d Hz.\n", b_clk_freq);
303 ir->base = devm_platform_ioremap_resource(pdev, 0);
304 if (IS_ERR(ir->base)) {
305 return PTR_ERR(ir->base);
308 ir->rc = rc_allocate_device(RC_DRIVER_IR_RAW);
310 dev_err(dev, "failed to allocate device\n");
315 ir->rc->device_name = SUNXI_IR_DEV;
316 ir->rc->input_phys = "sunxi-ir/input0";
317 ir->rc->input_id.bustype = BUS_HOST;
318 ir->rc->input_id.vendor = 0x0001;
319 ir->rc->input_id.product = 0x0001;
320 ir->rc->input_id.version = 0x0100;
321 ir->map_name = of_get_property(dn, "linux,rc-map-name", NULL);
322 ir->rc->map_name = ir->map_name ?: RC_MAP_EMPTY;
323 ir->rc->dev.parent = dev;
324 ir->rc->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER;
325 /* Frequency after IR internal divider with sample period in us */
326 ir->rc->rx_resolution = (USEC_PER_SEC / (b_clk_freq / 64));
327 ir->rc->timeout = IR_DEFAULT_TIMEOUT;
328 ir->rc->min_timeout = sunxi_ithr_to_usec(b_clk_freq, 0);
329 ir->rc->max_timeout = sunxi_ithr_to_usec(b_clk_freq, 255);
330 ir->rc->s_timeout = sunxi_ir_set_timeout;
331 ir->rc->driver_name = SUNXI_IR_DEV;
333 ret = rc_register_device(ir->rc);
335 dev_err(dev, "failed to register rc device\n");
339 platform_set_drvdata(pdev, ir);
342 ir->irq = platform_get_irq(pdev, 0);
348 ret = devm_request_irq(dev, ir->irq, sunxi_ir_irq, 0, SUNXI_IR_DEV, ir);
350 dev_err(dev, "failed request irq\n");
354 ret = sunxi_ir_hw_init(dev);
358 dev_info(dev, "initialized sunXi IR driver\n");
362 rc_free_device(ir->rc);
367 static int sunxi_ir_remove(struct platform_device *pdev)
369 struct sunxi_ir *ir = platform_get_drvdata(pdev);
371 rc_unregister_device(ir->rc);
372 sunxi_ir_hw_exit(&pdev->dev);
377 static void sunxi_ir_shutdown(struct platform_device *pdev)
379 sunxi_ir_hw_exit(&pdev->dev);
382 static const struct sunxi_ir_quirks sun4i_a10_ir_quirks = {
387 static const struct sunxi_ir_quirks sun5i_a13_ir_quirks = {
392 static const struct sunxi_ir_quirks sun6i_a31_ir_quirks = {
397 static const struct of_device_id sunxi_ir_match[] = {
399 .compatible = "allwinner,sun4i-a10-ir",
400 .data = &sun4i_a10_ir_quirks,
403 .compatible = "allwinner,sun5i-a13-ir",
404 .data = &sun5i_a13_ir_quirks,
407 .compatible = "allwinner,sun6i-a31-ir",
408 .data = &sun6i_a31_ir_quirks,
412 MODULE_DEVICE_TABLE(of, sunxi_ir_match);
414 static struct platform_driver sunxi_ir_driver = {
415 .probe = sunxi_ir_probe,
416 .remove = sunxi_ir_remove,
417 .shutdown = sunxi_ir_shutdown,
419 .name = SUNXI_IR_DEV,
420 .of_match_table = sunxi_ir_match,
421 .pm = &sunxi_ir_pm_ops,
425 module_platform_driver(sunxi_ir_driver);
427 MODULE_DESCRIPTION("Allwinner sunXi IR controller driver");
429 MODULE_LICENSE("GPL");