1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * ImgTec IR Decoder found in PowerDown Controller.
5 * Copyright 2010-2014 Imagination Technologies Ltd.
7 * This contains core img-ir code for setting up the driver. The two interfaces
8 * (raw and hardware decode) are handled separately.
11 #include <linux/clk.h>
12 #include <linux/init.h>
13 #include <linux/interrupt.h>
15 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/slab.h>
18 #include <linux/spinlock.h>
21 static irqreturn_t
img_ir_isr(int irq
, void *dev_id
)
23 struct img_ir_priv
*priv
= dev_id
;
26 spin_lock(&priv
->lock
);
27 /* we have to clear irqs before reading */
28 irq_status
= img_ir_read(priv
, IMG_IR_IRQ_STATUS
);
29 img_ir_write(priv
, IMG_IR_IRQ_CLEAR
, irq_status
);
31 /* don't handle valid data irqs if we're only interested in matches */
32 irq_status
&= img_ir_read(priv
, IMG_IR_IRQ_ENABLE
);
34 /* hand off edge interrupts to raw decode handler */
35 if (irq_status
& IMG_IR_IRQ_EDGE
&& img_ir_raw_enabled(&priv
->raw
))
36 img_ir_isr_raw(priv
, irq_status
);
38 /* hand off hardware match interrupts to hardware decode handler */
39 if (irq_status
& (IMG_IR_IRQ_DATA_MATCH
|
40 IMG_IR_IRQ_DATA_VALID
|
41 IMG_IR_IRQ_DATA2_VALID
) &&
42 img_ir_hw_enabled(&priv
->hw
))
43 img_ir_isr_hw(priv
, irq_status
);
45 spin_unlock(&priv
->lock
);
49 static void img_ir_setup(struct img_ir_priv
*priv
)
51 /* start off with interrupts disabled */
52 img_ir_write(priv
, IMG_IR_IRQ_ENABLE
, 0);
54 img_ir_setup_raw(priv
);
55 img_ir_setup_hw(priv
);
57 if (!IS_ERR(priv
->clk
))
58 clk_prepare_enable(priv
->clk
);
61 static void img_ir_ident(struct img_ir_priv
*priv
)
63 u32 core_rev
= img_ir_read(priv
, IMG_IR_CORE_REV
);
66 "IMG IR Decoder (%d.%d.%d.%d) probed successfully\n",
67 (core_rev
& IMG_IR_DESIGNER
) >> IMG_IR_DESIGNER_SHIFT
,
68 (core_rev
& IMG_IR_MAJOR_REV
) >> IMG_IR_MAJOR_REV_SHIFT
,
69 (core_rev
& IMG_IR_MINOR_REV
) >> IMG_IR_MINOR_REV_SHIFT
,
70 (core_rev
& IMG_IR_MAINT_REV
) >> IMG_IR_MAINT_REV_SHIFT
);
71 dev_info(priv
->dev
, "Modes:%s%s\n",
72 img_ir_hw_enabled(&priv
->hw
) ? " hardware" : "",
73 img_ir_raw_enabled(&priv
->raw
) ? " raw" : "");
76 static int img_ir_probe(struct platform_device
*pdev
)
78 struct img_ir_priv
*priv
;
79 struct resource
*res_regs
;
80 int irq
, error
, error2
;
82 /* Get resources from platform device */
83 irq
= platform_get_irq(pdev
, 0);
85 dev_err(&pdev
->dev
, "cannot find IRQ resource\n");
89 /* Private driver data */
90 priv
= devm_kzalloc(&pdev
->dev
, sizeof(*priv
), GFP_KERNEL
);
94 platform_set_drvdata(pdev
, priv
);
95 priv
->dev
= &pdev
->dev
;
96 spin_lock_init(&priv
->lock
);
98 /* Ioremap the registers */
99 res_regs
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
100 priv
->reg_base
= devm_ioremap_resource(&pdev
->dev
, res_regs
);
101 if (IS_ERR(priv
->reg_base
))
102 return PTR_ERR(priv
->reg_base
);
105 priv
->clk
= devm_clk_get(&pdev
->dev
, "core");
106 if (IS_ERR(priv
->clk
))
107 dev_warn(&pdev
->dev
, "cannot get core clock resource\n");
110 priv
->sys_clk
= devm_clk_get(&pdev
->dev
, "sys");
111 if (IS_ERR(priv
->sys_clk
))
112 dev_warn(&pdev
->dev
, "cannot get sys clock resource\n");
114 * Enabling the system clock before the register interface is
115 * accessed. ISR shouldn't get called with Sys Clock disabled,
116 * hence exiting probe with an error.
118 if (!IS_ERR(priv
->sys_clk
)) {
119 error
= clk_prepare_enable(priv
->sys_clk
);
121 dev_err(&pdev
->dev
, "cannot enable sys clock\n");
126 /* Set up raw & hw decoder */
127 error
= img_ir_probe_raw(priv
);
128 error2
= img_ir_probe_hw(priv
);
129 if (error
&& error2
) {
130 if (error
== -ENODEV
)
137 error
= request_irq(priv
->irq
, img_ir_isr
, 0, "img-ir", priv
);
139 dev_err(&pdev
->dev
, "cannot register IRQ %u\n",
151 img_ir_remove_hw(priv
);
152 img_ir_remove_raw(priv
);
154 if (!IS_ERR(priv
->sys_clk
))
155 clk_disable_unprepare(priv
->sys_clk
);
159 static int img_ir_remove(struct platform_device
*pdev
)
161 struct img_ir_priv
*priv
= platform_get_drvdata(pdev
);
163 free_irq(priv
->irq
, priv
);
164 img_ir_remove_hw(priv
);
165 img_ir_remove_raw(priv
);
167 if (!IS_ERR(priv
->clk
))
168 clk_disable_unprepare(priv
->clk
);
169 if (!IS_ERR(priv
->sys_clk
))
170 clk_disable_unprepare(priv
->sys_clk
);
174 static SIMPLE_DEV_PM_OPS(img_ir_pmops
, img_ir_suspend
, img_ir_resume
);
176 static const struct of_device_id img_ir_match
[] = {
177 { .compatible
= "img,ir-rev1" },
180 MODULE_DEVICE_TABLE(of
, img_ir_match
);
182 static struct platform_driver img_ir_driver
= {
185 .of_match_table
= img_ir_match
,
188 .probe
= img_ir_probe
,
189 .remove
= img_ir_remove
,
192 module_platform_driver(img_ir_driver
);
194 MODULE_AUTHOR("Imagination Technologies Ltd.");
195 MODULE_DESCRIPTION("ImgTec IR");
196 MODULE_LICENSE("GPL");