1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2014 Linaro Ltd.
4 * Copyright (c) 2014 HiSilicon Limited.
8 #include <linux/delay.h>
9 #include <linux/interrupt.h>
10 #include <linux/mfd/syscon.h>
11 #include <linux/module.h>
13 #include <linux/platform_device.h>
14 #include <linux/property.h>
15 #include <linux/regmap.h>
16 #include <media/rc-core.h>
18 #define IR_ENABLE 0x00
19 #define IR_CONFIG 0x04
20 #define CNT_LEADS 0x08
21 #define CNT_LEADE 0x0c
22 #define CNT_SLEADE 0x10
34 #define INTMS_SYMBRCV (BIT(24) | BIT(8))
35 #define INTMS_TIMEOUT (BIT(25) | BIT(9))
36 #define INTMS_OVERFLOW (BIT(26) | BIT(10))
37 #define INT_CLR_OVERFLOW BIT(18)
38 #define INT_CLR_TIMEOUT BIT(17)
39 #define INT_CLR_RCV BIT(16)
40 #define INT_CLR_RCVTIMEOUT (BIT(16) | BIT(17))
42 #define IR_CLK_ENABLE BIT(4)
43 #define IR_CLK_RESET BIT(5)
45 /* IR_ENABLE register bits */
46 #define IR_ENABLE_EN BIT(0)
47 #define IR_ENABLE_EN_EXTRA BIT(8)
49 #define IR_CFG_WIDTH_MASK 0xffff
50 #define IR_CFG_WIDTH_SHIFT 16
51 #define IR_CFG_FORMAT_MASK 0x3
52 #define IR_CFG_FORMAT_SHIFT 14
53 #define IR_CFG_INT_LEVEL_MASK 0x3f
54 #define IR_CFG_INT_LEVEL_SHIFT 8
55 /* only support raw mode */
56 #define IR_CFG_MODE_RAW BIT(7)
57 #define IR_CFG_FREQ_MASK 0x7f
58 #define IR_CFG_FREQ_SHIFT 0
59 #define IR_CFG_INT_THRESHOLD 1
60 /* symbol start from low to high, symbol stream end at high*/
61 #define IR_CFG_SYMBOL_FMT 0
62 #define IR_CFG_SYMBOL_MAXWIDTH 0x3e80
64 #define IR_HIX5HD2_NAME "hix5hd2-ir"
66 /* Need to set extra bit for enabling IR */
67 #define HIX5HD2_FLAG_EXTRA_ENABLE BIT(0)
69 struct hix5hd2_soc_data {
74 static const struct hix5hd2_soc_data hix5hd2_data = {
78 static const struct hix5hd2_soc_data hi3796cv300_data = {
80 .flags = HIX5HD2_FLAG_EXTRA_ENABLE,
83 struct hix5hd2_ir_priv {
88 struct regmap *regmap;
91 const struct hix5hd2_soc_data *socdata;
94 static int hix5hd2_ir_clk_enable(struct hix5hd2_ir_priv *dev, bool on)
96 u32 clk_reg = dev->socdata->clk_reg;
101 regmap_read(dev->regmap, clk_reg, &val);
103 val &= ~IR_CLK_RESET;
104 val |= IR_CLK_ENABLE;
106 val &= ~IR_CLK_ENABLE;
109 regmap_write(dev->regmap, clk_reg, val);
112 ret = clk_prepare_enable(dev->clock);
114 clk_disable_unprepare(dev->clock);
119 static inline void hix5hd2_ir_enable(struct hix5hd2_ir_priv *priv)
121 u32 val = IR_ENABLE_EN;
123 if (priv->socdata->flags & HIX5HD2_FLAG_EXTRA_ENABLE)
124 val |= IR_ENABLE_EN_EXTRA;
126 writel_relaxed(val, priv->base + IR_ENABLE);
129 static int hix5hd2_ir_config(struct hix5hd2_ir_priv *priv)
134 hix5hd2_ir_enable(priv);
136 while (readl_relaxed(priv->base + IR_BUSY)) {
140 dev_err(priv->dev, "IR_BUSY timeout\n");
145 /* Now only support raw mode, with symbol start from low to high */
146 rate = DIV_ROUND_CLOSEST(priv->rate, 1000000);
147 val = IR_CFG_SYMBOL_MAXWIDTH & IR_CFG_WIDTH_MASK << IR_CFG_WIDTH_SHIFT;
148 val |= IR_CFG_SYMBOL_FMT & IR_CFG_FORMAT_MASK << IR_CFG_FORMAT_SHIFT;
149 val |= (IR_CFG_INT_THRESHOLD - 1) & IR_CFG_INT_LEVEL_MASK
150 << IR_CFG_INT_LEVEL_SHIFT;
151 val |= IR_CFG_MODE_RAW;
152 val |= (rate - 1) & IR_CFG_FREQ_MASK << IR_CFG_FREQ_SHIFT;
153 writel_relaxed(val, priv->base + IR_CONFIG);
155 writel_relaxed(0x00, priv->base + IR_INTM);
156 /* write arbitrary value to start */
157 writel_relaxed(0x01, priv->base + IR_START);
161 static int hix5hd2_ir_open(struct rc_dev *rdev)
163 struct hix5hd2_ir_priv *priv = rdev->priv;
166 ret = hix5hd2_ir_clk_enable(priv, true);
170 ret = hix5hd2_ir_config(priv);
172 hix5hd2_ir_clk_enable(priv, false);
178 static void hix5hd2_ir_close(struct rc_dev *rdev)
180 struct hix5hd2_ir_priv *priv = rdev->priv;
182 hix5hd2_ir_clk_enable(priv, false);
185 static irqreturn_t hix5hd2_ir_rx_interrupt(int irq, void *data)
187 u32 symb_num, symb_val, symb_time;
190 struct hix5hd2_ir_priv *priv = data;
192 irq_sr = readl_relaxed(priv->base + IR_INTS);
193 if (irq_sr & INTMS_OVERFLOW) {
195 * we must read IR_DATAL first, then we can clean up
196 * IR_INTS availably since logic would not clear
197 * fifo when overflow, drv do the job
199 ir_raw_event_overflow(priv->rdev);
200 symb_num = readl_relaxed(priv->base + IR_DATAH);
201 for (i = 0; i < symb_num; i++)
202 readl_relaxed(priv->base + IR_DATAL);
204 writel_relaxed(INT_CLR_OVERFLOW, priv->base + IR_INTC);
205 dev_info(priv->dev, "overflow, level=%d\n",
206 IR_CFG_INT_THRESHOLD);
209 if ((irq_sr & INTMS_SYMBRCV) || (irq_sr & INTMS_TIMEOUT)) {
210 struct ir_raw_event ev = {};
212 symb_num = readl_relaxed(priv->base + IR_DATAH);
213 for (i = 0; i < symb_num; i++) {
214 symb_val = readl_relaxed(priv->base + IR_DATAL);
215 data_l = ((symb_val & 0xffff) * 10);
216 data_h = ((symb_val >> 16) & 0xffff) * 10;
217 symb_time = (data_l + data_h) / 10;
219 ev.duration = data_l;
221 ir_raw_event_store(priv->rdev, &ev);
223 if (symb_time < IR_CFG_SYMBOL_MAXWIDTH) {
224 ev.duration = data_h;
226 ir_raw_event_store(priv->rdev, &ev);
228 ir_raw_event_set_idle(priv->rdev, true);
232 if (irq_sr & INTMS_SYMBRCV)
233 writel_relaxed(INT_CLR_RCV, priv->base + IR_INTC);
234 if (irq_sr & INTMS_TIMEOUT)
235 writel_relaxed(INT_CLR_TIMEOUT, priv->base + IR_INTC);
238 /* Empty software fifo */
239 ir_raw_event_handle(priv->rdev);
243 static const struct of_device_id hix5hd2_ir_table[] = {
244 { .compatible = "hisilicon,hix5hd2-ir", &hix5hd2_data, },
245 { .compatible = "hisilicon,hi3796cv300-ir", &hi3796cv300_data, },
248 MODULE_DEVICE_TABLE(of, hix5hd2_ir_table);
250 static int hix5hd2_ir_probe(struct platform_device *pdev)
253 struct device *dev = &pdev->dev;
254 struct hix5hd2_ir_priv *priv;
255 struct device_node *node = pdev->dev.of_node;
256 const char *map_name;
259 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
263 priv->socdata = device_get_match_data(dev);
264 if (!priv->socdata) {
265 dev_err(dev, "Unable to initialize IR data\n");
269 priv->regmap = syscon_regmap_lookup_by_phandle(node,
270 "hisilicon,power-syscon");
271 if (IS_ERR(priv->regmap)) {
272 dev_info(dev, "no power-reg\n");
276 priv->base = devm_platform_ioremap_resource(pdev, 0);
277 if (IS_ERR(priv->base))
278 return PTR_ERR(priv->base);
280 priv->irq = platform_get_irq(pdev, 0);
284 rdev = rc_allocate_device(RC_DRIVER_IR_RAW);
288 priv->clock = devm_clk_get(dev, NULL);
289 if (IS_ERR(priv->clock)) {
290 dev_err(dev, "clock not found\n");
291 ret = PTR_ERR(priv->clock);
294 ret = clk_prepare_enable(priv->clock);
297 priv->rate = clk_get_rate(priv->clock);
299 rdev->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER;
301 rdev->open = hix5hd2_ir_open;
302 rdev->close = hix5hd2_ir_close;
303 rdev->driver_name = IR_HIX5HD2_NAME;
304 map_name = of_get_property(node, "linux,rc-map-name", NULL);
305 rdev->map_name = map_name ?: RC_MAP_EMPTY;
306 rdev->device_name = IR_HIX5HD2_NAME;
307 rdev->input_phys = IR_HIX5HD2_NAME "/input0";
308 rdev->input_id.bustype = BUS_HOST;
309 rdev->input_id.vendor = 0x0001;
310 rdev->input_id.product = 0x0001;
311 rdev->input_id.version = 0x0100;
312 rdev->rx_resolution = 10;
313 rdev->timeout = IR_CFG_SYMBOL_MAXWIDTH * 10;
315 ret = rc_register_device(rdev);
319 if (devm_request_irq(dev, priv->irq, hix5hd2_ir_rx_interrupt,
320 0, pdev->name, priv) < 0) {
321 dev_err(dev, "IRQ %d register failed\n", priv->irq);
328 platform_set_drvdata(pdev, priv);
333 rc_unregister_device(rdev);
336 clk_disable_unprepare(priv->clock);
338 rc_free_device(rdev);
339 dev_err(dev, "Unable to register device (%d)\n", ret);
343 static void hix5hd2_ir_remove(struct platform_device *pdev)
345 struct hix5hd2_ir_priv *priv = platform_get_drvdata(pdev);
347 clk_disable_unprepare(priv->clock);
348 rc_unregister_device(priv->rdev);
351 #ifdef CONFIG_PM_SLEEP
352 static int hix5hd2_ir_suspend(struct device *dev)
354 struct hix5hd2_ir_priv *priv = dev_get_drvdata(dev);
356 clk_disable_unprepare(priv->clock);
357 hix5hd2_ir_clk_enable(priv, false);
362 static int hix5hd2_ir_resume(struct device *dev)
364 struct hix5hd2_ir_priv *priv = dev_get_drvdata(dev);
367 ret = hix5hd2_ir_clk_enable(priv, true);
371 ret = clk_prepare_enable(priv->clock);
373 hix5hd2_ir_clk_enable(priv, false);
377 hix5hd2_ir_enable(priv);
379 writel_relaxed(0x00, priv->base + IR_INTM);
380 writel_relaxed(0xff, priv->base + IR_INTC);
381 writel_relaxed(0x01, priv->base + IR_START);
387 static SIMPLE_DEV_PM_OPS(hix5hd2_ir_pm_ops, hix5hd2_ir_suspend,
390 static struct platform_driver hix5hd2_ir_driver = {
392 .name = IR_HIX5HD2_NAME,
393 .of_match_table = hix5hd2_ir_table,
394 .pm = &hix5hd2_ir_pm_ops,
396 .probe = hix5hd2_ir_probe,
397 .remove_new = hix5hd2_ir_remove,
400 module_platform_driver(hix5hd2_ir_driver);
402 MODULE_DESCRIPTION("IR controller driver for hix5hd2 platforms");
403 MODULE_AUTHOR("Guoxiong Yan <yanguoxiong@huawei.com>");
404 MODULE_LICENSE("GPL v2");
405 MODULE_ALIAS("platform:hix5hd2-ir");