1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for Mediatek IR Receiver Controller
9 #include <linux/interrupt.h>
10 #include <linux/module.h>
11 #include <linux/of_platform.h>
12 #include <linux/reset.h>
13 #include <media/rc-core.h>
15 #define MTK_IR_DEV KBUILD_MODNAME
17 /* Register to enable PWM and IR */
18 #define MTK_CONFIG_HIGH_REG 0x0c
20 /* Bit to enable IR pulse width detection */
21 #define MTK_PWM_EN BIT(13)
24 * Register to setting ok count whose unit based on hardware sampling period
25 * indicating IR receiving completion and then making IRQ fires
27 #define MTK_OK_COUNT(x) (((x) & GENMASK(23, 16)) << 16)
29 /* Bit to enable IR hardware function */
30 #define MTK_IR_EN BIT(0)
32 /* Bit to restart IR receiving */
33 #define MTK_IRCLR BIT(0)
35 /* Fields containing pulse width data */
36 #define MTK_WIDTH_MASK (GENMASK(7, 0))
38 /* Bit to enable interrupt */
39 #define MTK_IRINT_EN BIT(0)
41 /* Bit to clear interrupt status */
42 #define MTK_IRINT_CLR BIT(0)
44 /* Maximum count of samples */
45 #define MTK_MAX_SAMPLES 0xff
46 /* Indicate the end of IR message */
47 #define MTK_IR_END(v, p) ((v) == MTK_MAX_SAMPLES && (p) == 0)
48 /* Number of registers to record the pulse width */
49 #define MTK_CHKDATA_SZ 17
50 /* Sample period in ns */
51 #define MTK_IR_SAMPLE 46000
54 /* Register to setting software sampling period */
56 /* Register to setting hardware sampling period */
61 /* Register to clear state of state machine */
63 /* Register containing pulse width data */
65 /* Register to enable IR interrupt */
67 /* Register to ack IR interrupt */
71 static const u32 mt7623_regs[] = {
72 [MTK_IRCLR_REG] = 0x20,
73 [MTK_CHKDATA_REG] = 0x88,
74 [MTK_IRINT_EN_REG] = 0xcc,
75 [MTK_IRINT_CLR_REG] = 0xd0,
78 static const u32 mt7622_regs[] = {
79 [MTK_IRCLR_REG] = 0x18,
80 [MTK_CHKDATA_REG] = 0x30,
81 [MTK_IRINT_EN_REG] = 0x1c,
82 [MTK_IRINT_CLR_REG] = 0x20,
85 struct mtk_field_type {
92 * struct mtk_ir_data - This is the structure holding all differences among
94 * @regs: The pointer to the array holding registers offset
95 * @fields: The pointer to the array holding fields location
96 * @div: The internal divisor for the based reference clock
97 * @ok_count: The count indicating the completion of IR data
98 * receiving when count is reached
99 * @hw_period: The value indicating the hardware sampling period
103 const struct mtk_field_type *fields;
109 static const struct mtk_field_type mt7623_fields[] = {
110 [MTK_CHK_PERIOD] = {0x10, 8, GENMASK(20, 8)},
111 [MTK_HW_PERIOD] = {0x10, 0, GENMASK(7, 0)},
114 static const struct mtk_field_type mt7622_fields[] = {
115 [MTK_CHK_PERIOD] = {0x24, 0, GENMASK(24, 0)},
116 [MTK_HW_PERIOD] = {0x10, 0, GENMASK(24, 0)},
120 * struct mtk_ir - This is the main datasructure for holding the state
122 * @dev: The device pointer
123 * @rc: The rc instrance
124 * @base: The mapped register i/o base
125 * @irq: The IRQ that we are using
126 * @clk: The clock that IR internal is using
127 * @bus: The clock that software decoder is using
128 * @data: Holding specific data for vaious platform
137 const struct mtk_ir_data *data;
140 static inline u32 mtk_chkdata_reg(struct mtk_ir *ir, u32 i)
142 return ir->data->regs[MTK_CHKDATA_REG] + 4 * i;
145 static inline u32 mtk_chk_period(struct mtk_ir *ir)
149 /* Period of raw software sampling in ns */
150 val = DIV_ROUND_CLOSEST(1000000000ul,
151 clk_get_rate(ir->bus) / ir->data->div);
154 * Period for software decoder used in the
155 * unit of raw software sampling
157 val = DIV_ROUND_CLOSEST(MTK_IR_SAMPLE, val);
159 dev_dbg(ir->dev, "@pwm clk = \t%lu\n",
160 clk_get_rate(ir->bus) / ir->data->div);
161 dev_dbg(ir->dev, "@chkperiod = %08x\n", val);
166 static void mtk_w32_mask(struct mtk_ir *ir, u32 val, u32 mask, unsigned int reg)
170 tmp = __raw_readl(ir->base + reg);
171 tmp = (tmp & ~mask) | val;
172 __raw_writel(tmp, ir->base + reg);
175 static void mtk_w32(struct mtk_ir *ir, u32 val, unsigned int reg)
177 __raw_writel(val, ir->base + reg);
180 static u32 mtk_r32(struct mtk_ir *ir, unsigned int reg)
182 return __raw_readl(ir->base + reg);
185 static inline void mtk_irq_disable(struct mtk_ir *ir, u32 mask)
189 val = mtk_r32(ir, ir->data->regs[MTK_IRINT_EN_REG]);
190 mtk_w32(ir, val & ~mask, ir->data->regs[MTK_IRINT_EN_REG]);
193 static inline void mtk_irq_enable(struct mtk_ir *ir, u32 mask)
197 val = mtk_r32(ir, ir->data->regs[MTK_IRINT_EN_REG]);
198 mtk_w32(ir, val | mask, ir->data->regs[MTK_IRINT_EN_REG]);
201 static irqreturn_t mtk_ir_irq(int irqno, void *dev_id)
203 struct mtk_ir *ir = dev_id;
206 struct ir_raw_event rawir = {};
209 * Reset decoder state machine explicitly is required
210 * because 1) the longest duration for space MTK IR hardware
211 * could record is not safely long. e.g 12ms if rx resolution
212 * is 46us by default. There is still the risk to satisfying
213 * every decoder to reset themselves through long enough
214 * trailing spaces and 2) the IRQ handler guarantees that
215 * start of IR message is always contained in and starting
216 * from register mtk_chkdata_reg(ir, i).
218 ir_raw_event_reset(ir->rc);
220 /* First message must be pulse */
223 /* Handle all pulse and space IR controller captures */
224 for (i = 0 ; i < MTK_CHKDATA_SZ ; i++) {
225 val = mtk_r32(ir, mtk_chkdata_reg(ir, i));
226 dev_dbg(ir->dev, "@reg%d=0x%08x\n", i, val);
228 for (j = 0 ; j < 4 ; j++) {
229 wid = (val & (MTK_WIDTH_MASK << j * 8)) >> j * 8;
230 rawir.pulse = !rawir.pulse;
231 rawir.duration = wid * (MTK_IR_SAMPLE + 1);
232 ir_raw_event_store_with_filter(ir->rc, &rawir);
237 * The maximum number of edges the IR controller can
238 * hold is MTK_CHKDATA_SZ * 4. So if received IR messages
239 * is over the limit, the last incomplete IR message would
240 * be appended trailing space and still would be sent into
241 * ir-rc-raw to decode. That helps it is possible that it
242 * has enough information to decode a scancode even if the
243 * trailing end of the message is missing.
245 if (!MTK_IR_END(wid, rawir.pulse)) {
247 rawir.duration = MTK_MAX_SAMPLES * (MTK_IR_SAMPLE + 1);
248 ir_raw_event_store_with_filter(ir->rc, &rawir);
251 ir_raw_event_handle(ir->rc);
254 * Restart controller for the next receive that would
255 * clear up all CHKDATA registers
257 mtk_w32_mask(ir, 0x1, MTK_IRCLR, ir->data->regs[MTK_IRCLR_REG]);
259 /* Clear interrupt status */
260 mtk_w32_mask(ir, 0x1, MTK_IRINT_CLR,
261 ir->data->regs[MTK_IRINT_CLR_REG]);
266 static const struct mtk_ir_data mt7623_data = {
268 .fields = mt7623_fields,
274 static const struct mtk_ir_data mt7622_data = {
276 .fields = mt7622_fields,
282 static const struct of_device_id mtk_ir_match[] = {
283 { .compatible = "mediatek,mt7623-cir", .data = &mt7623_data},
284 { .compatible = "mediatek,mt7622-cir", .data = &mt7622_data},
287 MODULE_DEVICE_TABLE(of, mtk_ir_match);
289 static int mtk_ir_probe(struct platform_device *pdev)
291 struct device *dev = &pdev->dev;
292 struct device_node *dn = dev->of_node;
293 struct resource *res;
297 const char *map_name;
299 ir = devm_kzalloc(dev, sizeof(struct mtk_ir), GFP_KERNEL);
304 ir->data = of_device_get_match_data(dev);
306 ir->clk = devm_clk_get(dev, "clk");
307 if (IS_ERR(ir->clk)) {
308 dev_err(dev, "failed to get a ir clock.\n");
309 return PTR_ERR(ir->clk);
312 ir->bus = devm_clk_get(dev, "bus");
313 if (IS_ERR(ir->bus)) {
315 * For compatibility with older device trees try unnamed
316 * ir->bus uses the same clock as ir->clock.
321 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
322 ir->base = devm_ioremap_resource(dev, res);
323 if (IS_ERR(ir->base))
324 return PTR_ERR(ir->base);
326 ir->rc = devm_rc_allocate_device(dev, RC_DRIVER_IR_RAW);
328 dev_err(dev, "failed to allocate device\n");
333 ir->rc->device_name = MTK_IR_DEV;
334 ir->rc->input_phys = MTK_IR_DEV "/input0";
335 ir->rc->input_id.bustype = BUS_HOST;
336 ir->rc->input_id.vendor = 0x0001;
337 ir->rc->input_id.product = 0x0001;
338 ir->rc->input_id.version = 0x0001;
339 map_name = of_get_property(dn, "linux,rc-map-name", NULL);
340 ir->rc->map_name = map_name ?: RC_MAP_EMPTY;
341 ir->rc->dev.parent = dev;
342 ir->rc->driver_name = MTK_IR_DEV;
343 ir->rc->allowed_protocols = RC_PROTO_BIT_ALL;
344 ir->rc->rx_resolution = MTK_IR_SAMPLE;
345 ir->rc->timeout = MTK_MAX_SAMPLES * (MTK_IR_SAMPLE + 1);
347 ret = devm_rc_register_device(dev, ir->rc);
349 dev_err(dev, "failed to register rc device\n");
353 platform_set_drvdata(pdev, ir);
355 ir->irq = platform_get_irq(pdev, 0);
357 dev_err(dev, "no irq resource\n");
361 if (clk_prepare_enable(ir->clk)) {
362 dev_err(dev, "try to enable ir_clk failed\n");
366 if (clk_prepare_enable(ir->bus)) {
367 dev_err(dev, "try to enable ir_clk failed\n");
369 goto exit_clkdisable_clk;
373 * Enable interrupt after proper hardware
374 * setup and IRQ handler registration
376 mtk_irq_disable(ir, MTK_IRINT_EN);
378 ret = devm_request_irq(dev, ir->irq, mtk_ir_irq, 0, MTK_IR_DEV, ir);
380 dev_err(dev, "failed request irq\n");
381 goto exit_clkdisable_bus;
385 * Setup software sample period as the reference of software decoder
387 val = (mtk_chk_period(ir) << ir->data->fields[MTK_CHK_PERIOD].offset) &
388 ir->data->fields[MTK_CHK_PERIOD].mask;
389 mtk_w32_mask(ir, val, ir->data->fields[MTK_CHK_PERIOD].mask,
390 ir->data->fields[MTK_CHK_PERIOD].reg);
393 * Setup hardware sampling period used to setup the proper timeout for
394 * indicating end of IR receiving completion
396 val = (ir->data->hw_period << ir->data->fields[MTK_HW_PERIOD].offset) &
397 ir->data->fields[MTK_HW_PERIOD].mask;
398 mtk_w32_mask(ir, val, ir->data->fields[MTK_HW_PERIOD].mask,
399 ir->data->fields[MTK_HW_PERIOD].reg);
401 /* Enable IR and PWM */
402 val = mtk_r32(ir, MTK_CONFIG_HIGH_REG);
403 val |= MTK_OK_COUNT(ir->data->ok_count) | MTK_PWM_EN | MTK_IR_EN;
404 mtk_w32(ir, val, MTK_CONFIG_HIGH_REG);
406 mtk_irq_enable(ir, MTK_IRINT_EN);
408 dev_info(dev, "Initialized MT7623 IR driver, sample period = %dus\n",
409 DIV_ROUND_CLOSEST(MTK_IR_SAMPLE, 1000));
414 clk_disable_unprepare(ir->bus);
416 clk_disable_unprepare(ir->clk);
421 static int mtk_ir_remove(struct platform_device *pdev)
423 struct mtk_ir *ir = platform_get_drvdata(pdev);
426 * Avoid contention between remove handler and
427 * IRQ handler so that disabling IR interrupt and
428 * waiting for pending IRQ handler to complete
430 mtk_irq_disable(ir, MTK_IRINT_EN);
431 synchronize_irq(ir->irq);
433 clk_disable_unprepare(ir->bus);
434 clk_disable_unprepare(ir->clk);
439 static struct platform_driver mtk_ir_driver = {
440 .probe = mtk_ir_probe,
441 .remove = mtk_ir_remove,
444 .of_match_table = mtk_ir_match,
448 module_platform_driver(mtk_ir_driver);
450 MODULE_DESCRIPTION("Mediatek IR Receiver Controller Driver");
452 MODULE_LICENSE("GPL");