xref: /OK3568_Linux_fs/kernel/drivers/media/rc/mtk-cir.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Driver for Mediatek IR Receiver Controller
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 2017 Sean Wang <sean.wang@mediatek.com>
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #include <linux/clk.h>
9*4882a593Smuzhiyun #include <linux/interrupt.h>
10*4882a593Smuzhiyun #include <linux/module.h>
11*4882a593Smuzhiyun #include <linux/of_platform.h>
12*4882a593Smuzhiyun #include <linux/reset.h>
13*4882a593Smuzhiyun #include <media/rc-core.h>
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun #define MTK_IR_DEV KBUILD_MODNAME
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun /* Register to enable PWM and IR */
18*4882a593Smuzhiyun #define MTK_CONFIG_HIGH_REG       0x0c
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun /* Bit to enable IR pulse width detection */
21*4882a593Smuzhiyun #define MTK_PWM_EN		  BIT(13)
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun /*
24*4882a593Smuzhiyun  * Register to setting ok count whose unit based on hardware sampling period
25*4882a593Smuzhiyun  * indicating IR receiving completion and then making IRQ fires
26*4882a593Smuzhiyun  */
27*4882a593Smuzhiyun #define MTK_OK_COUNT(x)		  (((x) & GENMASK(23, 16)) << 16)
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun /* Bit to enable IR hardware function */
30*4882a593Smuzhiyun #define MTK_IR_EN		  BIT(0)
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun /* Bit to restart IR receiving */
33*4882a593Smuzhiyun #define MTK_IRCLR		  BIT(0)
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun /* Fields containing pulse width data */
36*4882a593Smuzhiyun #define MTK_WIDTH_MASK		  (GENMASK(7, 0))
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun /* IR threshold */
39*4882a593Smuzhiyun #define MTK_IRTHD		 0x14
40*4882a593Smuzhiyun #define MTK_DG_CNT_MASK		 (GENMASK(12, 8))
41*4882a593Smuzhiyun #define MTK_DG_CNT(x)		 ((x) << 8)
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun /* Bit to enable interrupt */
44*4882a593Smuzhiyun #define MTK_IRINT_EN		  BIT(0)
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun /* Bit to clear interrupt status */
47*4882a593Smuzhiyun #define MTK_IRINT_CLR		  BIT(0)
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun /* Maximum count of samples */
50*4882a593Smuzhiyun #define MTK_MAX_SAMPLES		  0xff
51*4882a593Smuzhiyun /* Indicate the end of IR message */
52*4882a593Smuzhiyun #define MTK_IR_END(v, p)	  ((v) == MTK_MAX_SAMPLES && (p) == 0)
53*4882a593Smuzhiyun /* Number of registers to record the pulse width */
54*4882a593Smuzhiyun #define MTK_CHKDATA_SZ		  17
55*4882a593Smuzhiyun /* Sample period in us */
56*4882a593Smuzhiyun #define MTK_IR_SAMPLE		  46
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun enum mtk_fields {
59*4882a593Smuzhiyun 	/* Register to setting software sampling period */
60*4882a593Smuzhiyun 	MTK_CHK_PERIOD,
61*4882a593Smuzhiyun 	/* Register to setting hardware sampling period */
62*4882a593Smuzhiyun 	MTK_HW_PERIOD,
63*4882a593Smuzhiyun };
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun enum mtk_regs {
66*4882a593Smuzhiyun 	/* Register to clear state of state machine */
67*4882a593Smuzhiyun 	MTK_IRCLR_REG,
68*4882a593Smuzhiyun 	/* Register containing pulse width data */
69*4882a593Smuzhiyun 	MTK_CHKDATA_REG,
70*4882a593Smuzhiyun 	/* Register to enable IR interrupt */
71*4882a593Smuzhiyun 	MTK_IRINT_EN_REG,
72*4882a593Smuzhiyun 	/* Register to ack IR interrupt */
73*4882a593Smuzhiyun 	MTK_IRINT_CLR_REG
74*4882a593Smuzhiyun };
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun static const u32 mt7623_regs[] = {
77*4882a593Smuzhiyun 	[MTK_IRCLR_REG] =	0x20,
78*4882a593Smuzhiyun 	[MTK_CHKDATA_REG] =	0x88,
79*4882a593Smuzhiyun 	[MTK_IRINT_EN_REG] =	0xcc,
80*4882a593Smuzhiyun 	[MTK_IRINT_CLR_REG] =	0xd0,
81*4882a593Smuzhiyun };
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun static const u32 mt7622_regs[] = {
84*4882a593Smuzhiyun 	[MTK_IRCLR_REG] =	0x18,
85*4882a593Smuzhiyun 	[MTK_CHKDATA_REG] =	0x30,
86*4882a593Smuzhiyun 	[MTK_IRINT_EN_REG] =	0x1c,
87*4882a593Smuzhiyun 	[MTK_IRINT_CLR_REG] =	0x20,
88*4882a593Smuzhiyun };
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun struct mtk_field_type {
91*4882a593Smuzhiyun 	u32 reg;
92*4882a593Smuzhiyun 	u8 offset;
93*4882a593Smuzhiyun 	u32 mask;
94*4882a593Smuzhiyun };
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun /*
97*4882a593Smuzhiyun  * struct mtk_ir_data -	This is the structure holding all differences among
98*4882a593Smuzhiyun 			various hardwares
99*4882a593Smuzhiyun  * @regs:		The pointer to the array holding registers offset
100*4882a593Smuzhiyun  * @fields:		The pointer to the array holding fields location
101*4882a593Smuzhiyun  * @div:		The internal divisor for the based reference clock
102*4882a593Smuzhiyun  * @ok_count:		The count indicating the completion of IR data
103*4882a593Smuzhiyun  *			receiving when count is reached
104*4882a593Smuzhiyun  * @hw_period:		The value indicating the hardware sampling period
105*4882a593Smuzhiyun  */
106*4882a593Smuzhiyun struct mtk_ir_data {
107*4882a593Smuzhiyun 	const u32 *regs;
108*4882a593Smuzhiyun 	const struct mtk_field_type *fields;
109*4882a593Smuzhiyun 	u8 div;
110*4882a593Smuzhiyun 	u8 ok_count;
111*4882a593Smuzhiyun 	u32 hw_period;
112*4882a593Smuzhiyun };
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun static const struct mtk_field_type mt7623_fields[] = {
115*4882a593Smuzhiyun 	[MTK_CHK_PERIOD] = {0x10, 8, GENMASK(20, 8)},
116*4882a593Smuzhiyun 	[MTK_HW_PERIOD] = {0x10, 0, GENMASK(7, 0)},
117*4882a593Smuzhiyun };
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun static const struct mtk_field_type mt7622_fields[] = {
120*4882a593Smuzhiyun 	[MTK_CHK_PERIOD] = {0x24, 0, GENMASK(24, 0)},
121*4882a593Smuzhiyun 	[MTK_HW_PERIOD] = {0x10, 0, GENMASK(24, 0)},
122*4882a593Smuzhiyun };
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun /*
125*4882a593Smuzhiyun  * struct mtk_ir -	This is the main datasructure for holding the state
126*4882a593Smuzhiyun  *			of the driver
127*4882a593Smuzhiyun  * @dev:		The device pointer
128*4882a593Smuzhiyun  * @rc:			The rc instrance
129*4882a593Smuzhiyun  * @base:		The mapped register i/o base
130*4882a593Smuzhiyun  * @irq:		The IRQ that we are using
131*4882a593Smuzhiyun  * @clk:		The clock that IR internal is using
132*4882a593Smuzhiyun  * @bus:		The clock that software decoder is using
133*4882a593Smuzhiyun  * @data:		Holding specific data for vaious platform
134*4882a593Smuzhiyun  */
135*4882a593Smuzhiyun struct mtk_ir {
136*4882a593Smuzhiyun 	struct device	*dev;
137*4882a593Smuzhiyun 	struct rc_dev	*rc;
138*4882a593Smuzhiyun 	void __iomem	*base;
139*4882a593Smuzhiyun 	int		irq;
140*4882a593Smuzhiyun 	struct clk	*clk;
141*4882a593Smuzhiyun 	struct clk	*bus;
142*4882a593Smuzhiyun 	const struct mtk_ir_data *data;
143*4882a593Smuzhiyun };
144*4882a593Smuzhiyun 
mtk_chkdata_reg(struct mtk_ir * ir,u32 i)145*4882a593Smuzhiyun static inline u32 mtk_chkdata_reg(struct mtk_ir *ir, u32 i)
146*4882a593Smuzhiyun {
147*4882a593Smuzhiyun 	return ir->data->regs[MTK_CHKDATA_REG] + 4 * i;
148*4882a593Smuzhiyun }
149*4882a593Smuzhiyun 
mtk_chk_period(struct mtk_ir * ir)150*4882a593Smuzhiyun static inline u32 mtk_chk_period(struct mtk_ir *ir)
151*4882a593Smuzhiyun {
152*4882a593Smuzhiyun 	u32 val;
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun 	/*
155*4882a593Smuzhiyun 	 * Period for software decoder used in the
156*4882a593Smuzhiyun 	 * unit of raw software sampling
157*4882a593Smuzhiyun 	 */
158*4882a593Smuzhiyun 	val = DIV_ROUND_CLOSEST(clk_get_rate(ir->bus),
159*4882a593Smuzhiyun 				USEC_PER_SEC * ir->data->div / MTK_IR_SAMPLE);
160*4882a593Smuzhiyun 
161*4882a593Smuzhiyun 	dev_dbg(ir->dev, "@pwm clk  = \t%lu\n",
162*4882a593Smuzhiyun 		clk_get_rate(ir->bus) / ir->data->div);
163*4882a593Smuzhiyun 	dev_dbg(ir->dev, "@chkperiod = %08x\n", val);
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun 	return val;
166*4882a593Smuzhiyun }
167*4882a593Smuzhiyun 
mtk_w32_mask(struct mtk_ir * ir,u32 val,u32 mask,unsigned int reg)168*4882a593Smuzhiyun static void mtk_w32_mask(struct mtk_ir *ir, u32 val, u32 mask, unsigned int reg)
169*4882a593Smuzhiyun {
170*4882a593Smuzhiyun 	u32 tmp;
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun 	tmp = __raw_readl(ir->base + reg);
173*4882a593Smuzhiyun 	tmp = (tmp & ~mask) | val;
174*4882a593Smuzhiyun 	__raw_writel(tmp, ir->base + reg);
175*4882a593Smuzhiyun }
176*4882a593Smuzhiyun 
mtk_w32(struct mtk_ir * ir,u32 val,unsigned int reg)177*4882a593Smuzhiyun static void mtk_w32(struct mtk_ir *ir, u32 val, unsigned int reg)
178*4882a593Smuzhiyun {
179*4882a593Smuzhiyun 	__raw_writel(val, ir->base + reg);
180*4882a593Smuzhiyun }
181*4882a593Smuzhiyun 
mtk_r32(struct mtk_ir * ir,unsigned int reg)182*4882a593Smuzhiyun static u32 mtk_r32(struct mtk_ir *ir, unsigned int reg)
183*4882a593Smuzhiyun {
184*4882a593Smuzhiyun 	return __raw_readl(ir->base + reg);
185*4882a593Smuzhiyun }
186*4882a593Smuzhiyun 
mtk_irq_disable(struct mtk_ir * ir,u32 mask)187*4882a593Smuzhiyun static inline void mtk_irq_disable(struct mtk_ir *ir, u32 mask)
188*4882a593Smuzhiyun {
189*4882a593Smuzhiyun 	u32 val;
190*4882a593Smuzhiyun 
191*4882a593Smuzhiyun 	val = mtk_r32(ir, ir->data->regs[MTK_IRINT_EN_REG]);
192*4882a593Smuzhiyun 	mtk_w32(ir, val & ~mask, ir->data->regs[MTK_IRINT_EN_REG]);
193*4882a593Smuzhiyun }
194*4882a593Smuzhiyun 
mtk_irq_enable(struct mtk_ir * ir,u32 mask)195*4882a593Smuzhiyun static inline void mtk_irq_enable(struct mtk_ir *ir, u32 mask)
196*4882a593Smuzhiyun {
197*4882a593Smuzhiyun 	u32 val;
198*4882a593Smuzhiyun 
199*4882a593Smuzhiyun 	val = mtk_r32(ir, ir->data->regs[MTK_IRINT_EN_REG]);
200*4882a593Smuzhiyun 	mtk_w32(ir, val | mask, ir->data->regs[MTK_IRINT_EN_REG]);
201*4882a593Smuzhiyun }
202*4882a593Smuzhiyun 
mtk_ir_irq(int irqno,void * dev_id)203*4882a593Smuzhiyun static irqreturn_t mtk_ir_irq(int irqno, void *dev_id)
204*4882a593Smuzhiyun {
205*4882a593Smuzhiyun 	struct mtk_ir *ir = dev_id;
206*4882a593Smuzhiyun 	u8  wid = 0;
207*4882a593Smuzhiyun 	u32 i, j, val;
208*4882a593Smuzhiyun 	struct ir_raw_event rawir = {};
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun 	/*
211*4882a593Smuzhiyun 	 * Reset decoder state machine explicitly is required
212*4882a593Smuzhiyun 	 * because 1) the longest duration for space MTK IR hardware
213*4882a593Smuzhiyun 	 * could record is not safely long. e.g  12ms if rx resolution
214*4882a593Smuzhiyun 	 * is 46us by default. There is still the risk to satisfying
215*4882a593Smuzhiyun 	 * every decoder to reset themselves through long enough
216*4882a593Smuzhiyun 	 * trailing spaces and 2) the IRQ handler guarantees that
217*4882a593Smuzhiyun 	 * start of IR message is always contained in and starting
218*4882a593Smuzhiyun 	 * from register mtk_chkdata_reg(ir, i).
219*4882a593Smuzhiyun 	 */
220*4882a593Smuzhiyun 	ir_raw_event_reset(ir->rc);
221*4882a593Smuzhiyun 
222*4882a593Smuzhiyun 	/* First message must be pulse */
223*4882a593Smuzhiyun 	rawir.pulse = false;
224*4882a593Smuzhiyun 
225*4882a593Smuzhiyun 	/* Handle all pulse and space IR controller captures */
226*4882a593Smuzhiyun 	for (i = 0 ; i < MTK_CHKDATA_SZ ; i++) {
227*4882a593Smuzhiyun 		val = mtk_r32(ir, mtk_chkdata_reg(ir, i));
228*4882a593Smuzhiyun 		dev_dbg(ir->dev, "@reg%d=0x%08x\n", i, val);
229*4882a593Smuzhiyun 
230*4882a593Smuzhiyun 		for (j = 0 ; j < 4 ; j++) {
231*4882a593Smuzhiyun 			wid = (val & (MTK_WIDTH_MASK << j * 8)) >> j * 8;
232*4882a593Smuzhiyun 			rawir.pulse = !rawir.pulse;
233*4882a593Smuzhiyun 			rawir.duration = wid * (MTK_IR_SAMPLE + 1);
234*4882a593Smuzhiyun 			ir_raw_event_store_with_filter(ir->rc, &rawir);
235*4882a593Smuzhiyun 		}
236*4882a593Smuzhiyun 	}
237*4882a593Smuzhiyun 
238*4882a593Smuzhiyun 	/*
239*4882a593Smuzhiyun 	 * The maximum number of edges the IR controller can
240*4882a593Smuzhiyun 	 * hold is MTK_CHKDATA_SZ * 4. So if received IR messages
241*4882a593Smuzhiyun 	 * is over the limit, the last incomplete IR message would
242*4882a593Smuzhiyun 	 * be appended trailing space and still would be sent into
243*4882a593Smuzhiyun 	 * ir-rc-raw to decode. That helps it is possible that it
244*4882a593Smuzhiyun 	 * has enough information to decode a scancode even if the
245*4882a593Smuzhiyun 	 * trailing end of the message is missing.
246*4882a593Smuzhiyun 	 */
247*4882a593Smuzhiyun 	if (!MTK_IR_END(wid, rawir.pulse)) {
248*4882a593Smuzhiyun 		rawir.pulse = false;
249*4882a593Smuzhiyun 		rawir.duration = MTK_MAX_SAMPLES * (MTK_IR_SAMPLE + 1);
250*4882a593Smuzhiyun 		ir_raw_event_store_with_filter(ir->rc, &rawir);
251*4882a593Smuzhiyun 	}
252*4882a593Smuzhiyun 
253*4882a593Smuzhiyun 	ir_raw_event_handle(ir->rc);
254*4882a593Smuzhiyun 
255*4882a593Smuzhiyun 	/*
256*4882a593Smuzhiyun 	 * Restart controller for the next receive that would
257*4882a593Smuzhiyun 	 * clear up all CHKDATA registers
258*4882a593Smuzhiyun 	 */
259*4882a593Smuzhiyun 	mtk_w32_mask(ir, 0x1, MTK_IRCLR, ir->data->regs[MTK_IRCLR_REG]);
260*4882a593Smuzhiyun 
261*4882a593Smuzhiyun 	/* Clear interrupt status */
262*4882a593Smuzhiyun 	mtk_w32_mask(ir, 0x1, MTK_IRINT_CLR,
263*4882a593Smuzhiyun 		     ir->data->regs[MTK_IRINT_CLR_REG]);
264*4882a593Smuzhiyun 
265*4882a593Smuzhiyun 	return IRQ_HANDLED;
266*4882a593Smuzhiyun }
267*4882a593Smuzhiyun 
268*4882a593Smuzhiyun static const struct mtk_ir_data mt7623_data = {
269*4882a593Smuzhiyun 	.regs = mt7623_regs,
270*4882a593Smuzhiyun 	.fields = mt7623_fields,
271*4882a593Smuzhiyun 	.ok_count = 0xf,
272*4882a593Smuzhiyun 	.hw_period = 0xff,
273*4882a593Smuzhiyun 	.div	= 4,
274*4882a593Smuzhiyun };
275*4882a593Smuzhiyun 
276*4882a593Smuzhiyun static const struct mtk_ir_data mt7622_data = {
277*4882a593Smuzhiyun 	.regs = mt7622_regs,
278*4882a593Smuzhiyun 	.fields = mt7622_fields,
279*4882a593Smuzhiyun 	.ok_count = 0xf,
280*4882a593Smuzhiyun 	.hw_period = 0xffff,
281*4882a593Smuzhiyun 	.div	= 32,
282*4882a593Smuzhiyun };
283*4882a593Smuzhiyun 
284*4882a593Smuzhiyun static const struct of_device_id mtk_ir_match[] = {
285*4882a593Smuzhiyun 	{ .compatible = "mediatek,mt7623-cir", .data = &mt7623_data},
286*4882a593Smuzhiyun 	{ .compatible = "mediatek,mt7622-cir", .data = &mt7622_data},
287*4882a593Smuzhiyun 	{},
288*4882a593Smuzhiyun };
289*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, mtk_ir_match);
290*4882a593Smuzhiyun 
mtk_ir_probe(struct platform_device * pdev)291*4882a593Smuzhiyun static int mtk_ir_probe(struct platform_device *pdev)
292*4882a593Smuzhiyun {
293*4882a593Smuzhiyun 	struct device *dev = &pdev->dev;
294*4882a593Smuzhiyun 	struct device_node *dn = dev->of_node;
295*4882a593Smuzhiyun 	struct resource *res;
296*4882a593Smuzhiyun 	struct mtk_ir *ir;
297*4882a593Smuzhiyun 	u32 val;
298*4882a593Smuzhiyun 	int ret = 0;
299*4882a593Smuzhiyun 	const char *map_name;
300*4882a593Smuzhiyun 
301*4882a593Smuzhiyun 	ir = devm_kzalloc(dev, sizeof(struct mtk_ir), GFP_KERNEL);
302*4882a593Smuzhiyun 	if (!ir)
303*4882a593Smuzhiyun 		return -ENOMEM;
304*4882a593Smuzhiyun 
305*4882a593Smuzhiyun 	ir->dev = dev;
306*4882a593Smuzhiyun 	ir->data = of_device_get_match_data(dev);
307*4882a593Smuzhiyun 
308*4882a593Smuzhiyun 	ir->clk = devm_clk_get(dev, "clk");
309*4882a593Smuzhiyun 	if (IS_ERR(ir->clk)) {
310*4882a593Smuzhiyun 		dev_err(dev, "failed to get a ir clock.\n");
311*4882a593Smuzhiyun 		return PTR_ERR(ir->clk);
312*4882a593Smuzhiyun 	}
313*4882a593Smuzhiyun 
314*4882a593Smuzhiyun 	ir->bus = devm_clk_get(dev, "bus");
315*4882a593Smuzhiyun 	if (IS_ERR(ir->bus)) {
316*4882a593Smuzhiyun 		/*
317*4882a593Smuzhiyun 		 * For compatibility with older device trees try unnamed
318*4882a593Smuzhiyun 		 * ir->bus uses the same clock as ir->clock.
319*4882a593Smuzhiyun 		 */
320*4882a593Smuzhiyun 		ir->bus = ir->clk;
321*4882a593Smuzhiyun 	}
322*4882a593Smuzhiyun 
323*4882a593Smuzhiyun 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
324*4882a593Smuzhiyun 	ir->base = devm_ioremap_resource(dev, res);
325*4882a593Smuzhiyun 	if (IS_ERR(ir->base))
326*4882a593Smuzhiyun 		return PTR_ERR(ir->base);
327*4882a593Smuzhiyun 
328*4882a593Smuzhiyun 	ir->rc = devm_rc_allocate_device(dev, RC_DRIVER_IR_RAW);
329*4882a593Smuzhiyun 	if (!ir->rc) {
330*4882a593Smuzhiyun 		dev_err(dev, "failed to allocate device\n");
331*4882a593Smuzhiyun 		return -ENOMEM;
332*4882a593Smuzhiyun 	}
333*4882a593Smuzhiyun 
334*4882a593Smuzhiyun 	ir->rc->priv = ir;
335*4882a593Smuzhiyun 	ir->rc->device_name = MTK_IR_DEV;
336*4882a593Smuzhiyun 	ir->rc->input_phys = MTK_IR_DEV "/input0";
337*4882a593Smuzhiyun 	ir->rc->input_id.bustype = BUS_HOST;
338*4882a593Smuzhiyun 	ir->rc->input_id.vendor = 0x0001;
339*4882a593Smuzhiyun 	ir->rc->input_id.product = 0x0001;
340*4882a593Smuzhiyun 	ir->rc->input_id.version = 0x0001;
341*4882a593Smuzhiyun 	map_name = of_get_property(dn, "linux,rc-map-name", NULL);
342*4882a593Smuzhiyun 	ir->rc->map_name = map_name ?: RC_MAP_EMPTY;
343*4882a593Smuzhiyun 	ir->rc->dev.parent = dev;
344*4882a593Smuzhiyun 	ir->rc->driver_name = MTK_IR_DEV;
345*4882a593Smuzhiyun 	ir->rc->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER;
346*4882a593Smuzhiyun 	ir->rc->rx_resolution = MTK_IR_SAMPLE;
347*4882a593Smuzhiyun 	ir->rc->timeout = MTK_MAX_SAMPLES * (MTK_IR_SAMPLE + 1);
348*4882a593Smuzhiyun 
349*4882a593Smuzhiyun 	ret = devm_rc_register_device(dev, ir->rc);
350*4882a593Smuzhiyun 	if (ret) {
351*4882a593Smuzhiyun 		dev_err(dev, "failed to register rc device\n");
352*4882a593Smuzhiyun 		return ret;
353*4882a593Smuzhiyun 	}
354*4882a593Smuzhiyun 
355*4882a593Smuzhiyun 	platform_set_drvdata(pdev, ir);
356*4882a593Smuzhiyun 
357*4882a593Smuzhiyun 	ir->irq = platform_get_irq(pdev, 0);
358*4882a593Smuzhiyun 	if (ir->irq < 0)
359*4882a593Smuzhiyun 		return -ENODEV;
360*4882a593Smuzhiyun 
361*4882a593Smuzhiyun 	if (clk_prepare_enable(ir->clk)) {
362*4882a593Smuzhiyun 		dev_err(dev, "try to enable ir_clk failed\n");
363*4882a593Smuzhiyun 		return -EINVAL;
364*4882a593Smuzhiyun 	}
365*4882a593Smuzhiyun 
366*4882a593Smuzhiyun 	if (clk_prepare_enable(ir->bus)) {
367*4882a593Smuzhiyun 		dev_err(dev, "try to enable ir_clk failed\n");
368*4882a593Smuzhiyun 		ret = -EINVAL;
369*4882a593Smuzhiyun 		goto exit_clkdisable_clk;
370*4882a593Smuzhiyun 	}
371*4882a593Smuzhiyun 
372*4882a593Smuzhiyun 	/*
373*4882a593Smuzhiyun 	 * Enable interrupt after proper hardware
374*4882a593Smuzhiyun 	 * setup and IRQ handler registration
375*4882a593Smuzhiyun 	 */
376*4882a593Smuzhiyun 	mtk_irq_disable(ir, MTK_IRINT_EN);
377*4882a593Smuzhiyun 
378*4882a593Smuzhiyun 	ret = devm_request_irq(dev, ir->irq, mtk_ir_irq, 0, MTK_IR_DEV, ir);
379*4882a593Smuzhiyun 	if (ret) {
380*4882a593Smuzhiyun 		dev_err(dev, "failed request irq\n");
381*4882a593Smuzhiyun 		goto exit_clkdisable_bus;
382*4882a593Smuzhiyun 	}
383*4882a593Smuzhiyun 
384*4882a593Smuzhiyun 	/*
385*4882a593Smuzhiyun 	 * Setup software sample period as the reference of software decoder
386*4882a593Smuzhiyun 	 */
387*4882a593Smuzhiyun 	val = (mtk_chk_period(ir) << ir->data->fields[MTK_CHK_PERIOD].offset) &
388*4882a593Smuzhiyun 	       ir->data->fields[MTK_CHK_PERIOD].mask;
389*4882a593Smuzhiyun 	mtk_w32_mask(ir, val, ir->data->fields[MTK_CHK_PERIOD].mask,
390*4882a593Smuzhiyun 		     ir->data->fields[MTK_CHK_PERIOD].reg);
391*4882a593Smuzhiyun 
392*4882a593Smuzhiyun 	/*
393*4882a593Smuzhiyun 	 * Setup hardware sampling period used to setup the proper timeout for
394*4882a593Smuzhiyun 	 * indicating end of IR receiving completion
395*4882a593Smuzhiyun 	 */
396*4882a593Smuzhiyun 	val = (ir->data->hw_period << ir->data->fields[MTK_HW_PERIOD].offset) &
397*4882a593Smuzhiyun 	       ir->data->fields[MTK_HW_PERIOD].mask;
398*4882a593Smuzhiyun 	mtk_w32_mask(ir, val, ir->data->fields[MTK_HW_PERIOD].mask,
399*4882a593Smuzhiyun 		     ir->data->fields[MTK_HW_PERIOD].reg);
400*4882a593Smuzhiyun 
401*4882a593Smuzhiyun 	/* Set de-glitch counter */
402*4882a593Smuzhiyun 	mtk_w32_mask(ir, MTK_DG_CNT(1), MTK_DG_CNT_MASK, MTK_IRTHD);
403*4882a593Smuzhiyun 
404*4882a593Smuzhiyun 	/* Enable IR and PWM */
405*4882a593Smuzhiyun 	val = mtk_r32(ir, MTK_CONFIG_HIGH_REG);
406*4882a593Smuzhiyun 	val |= MTK_OK_COUNT(ir->data->ok_count) |  MTK_PWM_EN | MTK_IR_EN;
407*4882a593Smuzhiyun 	mtk_w32(ir, val, MTK_CONFIG_HIGH_REG);
408*4882a593Smuzhiyun 
409*4882a593Smuzhiyun 	mtk_irq_enable(ir, MTK_IRINT_EN);
410*4882a593Smuzhiyun 
411*4882a593Smuzhiyun 	dev_info(dev, "Initialized MT7623 IR driver, sample period = %dus\n",
412*4882a593Smuzhiyun 		 MTK_IR_SAMPLE);
413*4882a593Smuzhiyun 
414*4882a593Smuzhiyun 	return 0;
415*4882a593Smuzhiyun 
416*4882a593Smuzhiyun exit_clkdisable_bus:
417*4882a593Smuzhiyun 	clk_disable_unprepare(ir->bus);
418*4882a593Smuzhiyun exit_clkdisable_clk:
419*4882a593Smuzhiyun 	clk_disable_unprepare(ir->clk);
420*4882a593Smuzhiyun 
421*4882a593Smuzhiyun 	return ret;
422*4882a593Smuzhiyun }
423*4882a593Smuzhiyun 
mtk_ir_remove(struct platform_device * pdev)424*4882a593Smuzhiyun static int mtk_ir_remove(struct platform_device *pdev)
425*4882a593Smuzhiyun {
426*4882a593Smuzhiyun 	struct mtk_ir *ir = platform_get_drvdata(pdev);
427*4882a593Smuzhiyun 
428*4882a593Smuzhiyun 	/*
429*4882a593Smuzhiyun 	 * Avoid contention between remove handler and
430*4882a593Smuzhiyun 	 * IRQ handler so that disabling IR interrupt and
431*4882a593Smuzhiyun 	 * waiting for pending IRQ handler to complete
432*4882a593Smuzhiyun 	 */
433*4882a593Smuzhiyun 	mtk_irq_disable(ir, MTK_IRINT_EN);
434*4882a593Smuzhiyun 	synchronize_irq(ir->irq);
435*4882a593Smuzhiyun 
436*4882a593Smuzhiyun 	clk_disable_unprepare(ir->bus);
437*4882a593Smuzhiyun 	clk_disable_unprepare(ir->clk);
438*4882a593Smuzhiyun 
439*4882a593Smuzhiyun 	return 0;
440*4882a593Smuzhiyun }
441*4882a593Smuzhiyun 
442*4882a593Smuzhiyun static struct platform_driver mtk_ir_driver = {
443*4882a593Smuzhiyun 	.probe          = mtk_ir_probe,
444*4882a593Smuzhiyun 	.remove         = mtk_ir_remove,
445*4882a593Smuzhiyun 	.driver = {
446*4882a593Smuzhiyun 		.name = MTK_IR_DEV,
447*4882a593Smuzhiyun 		.of_match_table = mtk_ir_match,
448*4882a593Smuzhiyun 	},
449*4882a593Smuzhiyun };
450*4882a593Smuzhiyun 
451*4882a593Smuzhiyun module_platform_driver(mtk_ir_driver);
452*4882a593Smuzhiyun 
453*4882a593Smuzhiyun MODULE_DESCRIPTION("Mediatek IR Receiver Controller Driver");
454*4882a593Smuzhiyun MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>");
455*4882a593Smuzhiyun MODULE_LICENSE("GPL");
456