2 * Copyright (c) 2014 Linaro Ltd.
3 * Copyright (c) 2014 Hisilicon Limited.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
10 #include <linux/clk.h>
11 #include <linux/delay.h>
12 #include <linux/interrupt.h>
13 #include <linux/mfd/syscon.h>
14 #include <linux/module.h>
15 #include <linux/of_device.h>
16 #include <linux/regmap.h>
17 #include <media/rc-core.h>
19 #define IR_ENABLE 0x00
20 #define IR_CONFIG 0x04
21 #define CNT_LEADS 0x08
22 #define CNT_LEADE 0x0c
23 #define CNT_SLEADE 0x10
35 #define INTMS_SYMBRCV (BIT(24) | BIT(8))
36 #define INTMS_TIMEOUT (BIT(25) | BIT(9))
37 #define INTMS_OVERFLOW (BIT(26) | BIT(10))
38 #define INT_CLR_OVERFLOW BIT(18)
39 #define INT_CLR_TIMEOUT BIT(17)
40 #define INT_CLR_RCV BIT(16)
41 #define INT_CLR_RCVTIMEOUT (BIT(16) | BIT(17))
44 #define IR_CLK_ENABLE BIT(4)
45 #define IR_CLK_RESET BIT(5)
47 #define IR_CFG_WIDTH_MASK 0xffff
48 #define IR_CFG_WIDTH_SHIFT 16
49 #define IR_CFG_FORMAT_MASK 0x3
50 #define IR_CFG_FORMAT_SHIFT 14
51 #define IR_CFG_INT_LEVEL_MASK 0x3f
52 #define IR_CFG_INT_LEVEL_SHIFT 8
53 /* only support raw mode */
54 #define IR_CFG_MODE_RAW BIT(7)
55 #define IR_CFG_FREQ_MASK 0x7f
56 #define IR_CFG_FREQ_SHIFT 0
57 #define IR_CFG_INT_THRESHOLD 1
58 /* symbol start from low to high, symbol stream end at high*/
59 #define IR_CFG_SYMBOL_FMT 0
60 #define IR_CFG_SYMBOL_MAXWIDTH 0x3e80
62 #define IR_HIX5HD2_NAME "hix5hd2-ir"
64 struct hix5hd2_ir_priv
{
69 struct regmap
*regmap
;
74 static int hix5hd2_ir_enable(struct hix5hd2_ir_priv
*dev
, bool on
)
80 regmap_read(dev
->regmap
, IR_CLK
, &val
);
85 val
&= ~IR_CLK_ENABLE
;
88 regmap_write(dev
->regmap
, IR_CLK
, val
);
91 ret
= clk_prepare_enable(dev
->clock
);
93 clk_disable_unprepare(dev
->clock
);
98 static int hix5hd2_ir_config(struct hix5hd2_ir_priv
*priv
)
103 writel_relaxed(0x01, priv
->base
+ IR_ENABLE
);
104 while (readl_relaxed(priv
->base
+ IR_BUSY
)) {
108 dev_err(priv
->dev
, "IR_BUSY timeout\n");
113 /* Now only support raw mode, with symbol start from low to high */
114 rate
= DIV_ROUND_CLOSEST(priv
->rate
, 1000000);
115 val
= IR_CFG_SYMBOL_MAXWIDTH
& IR_CFG_WIDTH_MASK
<< IR_CFG_WIDTH_SHIFT
;
116 val
|= IR_CFG_SYMBOL_FMT
& IR_CFG_FORMAT_MASK
<< IR_CFG_FORMAT_SHIFT
;
117 val
|= (IR_CFG_INT_THRESHOLD
- 1) & IR_CFG_INT_LEVEL_MASK
118 << IR_CFG_INT_LEVEL_SHIFT
;
119 val
|= IR_CFG_MODE_RAW
;
120 val
|= (rate
- 1) & IR_CFG_FREQ_MASK
<< IR_CFG_FREQ_SHIFT
;
121 writel_relaxed(val
, priv
->base
+ IR_CONFIG
);
123 writel_relaxed(0x00, priv
->base
+ IR_INTM
);
124 /* write arbitrary value to start */
125 writel_relaxed(0x01, priv
->base
+ IR_START
);
129 static int hix5hd2_ir_open(struct rc_dev
*rdev
)
131 struct hix5hd2_ir_priv
*priv
= rdev
->priv
;
134 ret
= hix5hd2_ir_enable(priv
, true);
138 ret
= hix5hd2_ir_config(priv
);
140 hix5hd2_ir_enable(priv
, false);
146 static void hix5hd2_ir_close(struct rc_dev
*rdev
)
148 struct hix5hd2_ir_priv
*priv
= rdev
->priv
;
150 hix5hd2_ir_enable(priv
, false);
153 static irqreturn_t
hix5hd2_ir_rx_interrupt(int irq
, void *data
)
155 u32 symb_num
, symb_val
, symb_time
;
158 struct hix5hd2_ir_priv
*priv
= data
;
160 irq_sr
= readl_relaxed(priv
->base
+ IR_INTS
);
161 if (irq_sr
& INTMS_OVERFLOW
) {
163 * we must read IR_DATAL first, then we can clean up
164 * IR_INTS availably since logic would not clear
165 * fifo when overflow, drv do the job
167 ir_raw_event_reset(priv
->rdev
);
168 symb_num
= readl_relaxed(priv
->base
+ IR_DATAH
);
169 for (i
= 0; i
< symb_num
; i
++)
170 readl_relaxed(priv
->base
+ IR_DATAL
);
172 writel_relaxed(INT_CLR_OVERFLOW
, priv
->base
+ IR_INTC
);
173 dev_info(priv
->dev
, "overflow, level=%d\n",
174 IR_CFG_INT_THRESHOLD
);
177 if ((irq_sr
& INTMS_SYMBRCV
) || (irq_sr
& INTMS_TIMEOUT
)) {
178 struct ir_raw_event ev
= {};
180 symb_num
= readl_relaxed(priv
->base
+ IR_DATAH
);
181 for (i
= 0; i
< symb_num
; i
++) {
182 symb_val
= readl_relaxed(priv
->base
+ IR_DATAL
);
183 data_l
= ((symb_val
& 0xffff) * 10);
184 data_h
= ((symb_val
>> 16) & 0xffff) * 10;
185 symb_time
= (data_l
+ data_h
) / 10;
187 ev
.duration
= US_TO_NS(data_l
);
189 ir_raw_event_store(priv
->rdev
, &ev
);
191 if (symb_time
< IR_CFG_SYMBOL_MAXWIDTH
) {
192 ev
.duration
= US_TO_NS(data_h
);
194 ir_raw_event_store(priv
->rdev
, &ev
);
196 ir_raw_event_set_idle(priv
->rdev
, true);
200 if (irq_sr
& INTMS_SYMBRCV
)
201 writel_relaxed(INT_CLR_RCV
, priv
->base
+ IR_INTC
);
202 if (irq_sr
& INTMS_TIMEOUT
)
203 writel_relaxed(INT_CLR_TIMEOUT
, priv
->base
+ IR_INTC
);
206 /* Empty software fifo */
207 ir_raw_event_handle(priv
->rdev
);
211 static int hix5hd2_ir_probe(struct platform_device
*pdev
)
214 struct device
*dev
= &pdev
->dev
;
215 struct resource
*res
;
216 struct hix5hd2_ir_priv
*priv
;
217 struct device_node
*node
= pdev
->dev
.of_node
;
218 const char *map_name
;
221 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
225 priv
->regmap
= syscon_regmap_lookup_by_phandle(node
,
226 "hisilicon,power-syscon");
227 if (IS_ERR(priv
->regmap
)) {
228 dev_info(dev
, "no power-reg\n");
232 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
233 priv
->base
= devm_ioremap_resource(dev
, res
);
234 if (IS_ERR(priv
->base
))
235 return PTR_ERR(priv
->base
);
237 priv
->irq
= platform_get_irq(pdev
, 0);
239 dev_err(dev
, "irq can not get\n");
243 rdev
= rc_allocate_device(RC_DRIVER_IR_RAW
);
247 priv
->clock
= devm_clk_get(dev
, NULL
);
248 if (IS_ERR(priv
->clock
)) {
249 dev_err(dev
, "clock not found\n");
250 ret
= PTR_ERR(priv
->clock
);
253 ret
= clk_prepare_enable(priv
->clock
);
256 priv
->rate
= clk_get_rate(priv
->clock
);
258 rdev
->allowed_protocols
= RC_PROTO_BIT_ALL_IR_DECODER
;
260 rdev
->open
= hix5hd2_ir_open
;
261 rdev
->close
= hix5hd2_ir_close
;
262 rdev
->driver_name
= IR_HIX5HD2_NAME
;
263 map_name
= of_get_property(node
, "linux,rc-map-name", NULL
);
264 rdev
->map_name
= map_name
?: RC_MAP_EMPTY
;
265 rdev
->device_name
= IR_HIX5HD2_NAME
;
266 rdev
->input_phys
= IR_HIX5HD2_NAME
"/input0";
267 rdev
->input_id
.bustype
= BUS_HOST
;
268 rdev
->input_id
.vendor
= 0x0001;
269 rdev
->input_id
.product
= 0x0001;
270 rdev
->input_id
.version
= 0x0100;
271 rdev
->rx_resolution
= US_TO_NS(10);
272 rdev
->timeout
= US_TO_NS(IR_CFG_SYMBOL_MAXWIDTH
* 10);
274 ret
= rc_register_device(rdev
);
278 if (devm_request_irq(dev
, priv
->irq
, hix5hd2_ir_rx_interrupt
,
279 0, pdev
->name
, priv
) < 0) {
280 dev_err(dev
, "IRQ %d register failed\n", priv
->irq
);
287 platform_set_drvdata(pdev
, priv
);
292 rc_unregister_device(rdev
);
295 clk_disable_unprepare(priv
->clock
);
297 rc_free_device(rdev
);
298 dev_err(dev
, "Unable to register device (%d)\n", ret
);
302 static int hix5hd2_ir_remove(struct platform_device
*pdev
)
304 struct hix5hd2_ir_priv
*priv
= platform_get_drvdata(pdev
);
306 clk_disable_unprepare(priv
->clock
);
307 rc_unregister_device(priv
->rdev
);
311 #ifdef CONFIG_PM_SLEEP
312 static int hix5hd2_ir_suspend(struct device
*dev
)
314 struct hix5hd2_ir_priv
*priv
= dev_get_drvdata(dev
);
316 clk_disable_unprepare(priv
->clock
);
317 hix5hd2_ir_enable(priv
, false);
322 static int hix5hd2_ir_resume(struct device
*dev
)
324 struct hix5hd2_ir_priv
*priv
= dev_get_drvdata(dev
);
327 ret
= hix5hd2_ir_enable(priv
, true);
331 ret
= clk_prepare_enable(priv
->clock
);
333 hix5hd2_ir_enable(priv
, false);
337 writel_relaxed(0x01, priv
->base
+ IR_ENABLE
);
338 writel_relaxed(0x00, priv
->base
+ IR_INTM
);
339 writel_relaxed(0xff, priv
->base
+ IR_INTC
);
340 writel_relaxed(0x01, priv
->base
+ IR_START
);
346 static SIMPLE_DEV_PM_OPS(hix5hd2_ir_pm_ops
, hix5hd2_ir_suspend
,
349 static const struct of_device_id hix5hd2_ir_table
[] = {
350 { .compatible
= "hisilicon,hix5hd2-ir", },
353 MODULE_DEVICE_TABLE(of
, hix5hd2_ir_table
);
355 static struct platform_driver hix5hd2_ir_driver
= {
357 .name
= IR_HIX5HD2_NAME
,
358 .of_match_table
= hix5hd2_ir_table
,
359 .pm
= &hix5hd2_ir_pm_ops
,
361 .probe
= hix5hd2_ir_probe
,
362 .remove
= hix5hd2_ir_remove
,
365 module_platform_driver(hix5hd2_ir_driver
);
367 MODULE_DESCRIPTION("IR controller driver for hix5hd2 platforms");
368 MODULE_AUTHOR("Guoxiong Yan <yanguoxiong@huawei.com>");
369 MODULE_LICENSE("GPL v2");
370 MODULE_ALIAS("platform:hix5hd2-ir");