1
2
3
4
5
6
7
8
9
10
11#include <linux/clk.h>
12#include <linux/init.h>
13#include <linux/interrupt.h>
14#include <linux/io.h>
15#include <linux/module.h>
16#include <linux/platform_device.h>
17#include <linux/slab.h>
18#include <linux/spinlock.h>
19#include "img-ir.h"
20
21static irqreturn_t img_ir_isr(int irq, void *dev_id)
22{
23 struct img_ir_priv *priv = dev_id;
24 u32 irq_status;
25
26 spin_lock(&priv->lock);
27
28 irq_status = img_ir_read(priv, IMG_IR_IRQ_STATUS);
29 img_ir_write(priv, IMG_IR_IRQ_CLEAR, irq_status);
30
31
32 irq_status &= img_ir_read(priv, IMG_IR_IRQ_ENABLE);
33
34
35 if (irq_status & IMG_IR_IRQ_EDGE && img_ir_raw_enabled(&priv->raw))
36 img_ir_isr_raw(priv, irq_status);
37
38
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);
44
45 spin_unlock(&priv->lock);
46 return IRQ_HANDLED;
47}
48
49static void img_ir_setup(struct img_ir_priv *priv)
50{
51
52 img_ir_write(priv, IMG_IR_IRQ_ENABLE, 0);
53
54 img_ir_setup_raw(priv);
55 img_ir_setup_hw(priv);
56
57 if (!IS_ERR(priv->clk))
58 clk_prepare_enable(priv->clk);
59}
60
61static void img_ir_ident(struct img_ir_priv *priv)
62{
63 u32 core_rev = img_ir_read(priv, IMG_IR_CORE_REV);
64
65 dev_info(priv->dev,
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" : "");
74}
75
76static int img_ir_probe(struct platform_device *pdev)
77{
78 struct img_ir_priv *priv;
79 int irq, error, error2;
80
81
82 irq = platform_get_irq(pdev, 0);
83 if (irq < 0)
84 return irq;
85
86
87 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
88 if (!priv)
89 return -ENOMEM;
90
91 platform_set_drvdata(pdev, priv);
92 priv->dev = &pdev->dev;
93 spin_lock_init(&priv->lock);
94
95
96 priv->reg_base = devm_platform_ioremap_resource(pdev, 0);
97 if (IS_ERR(priv->reg_base))
98 return PTR_ERR(priv->reg_base);
99
100
101 priv->clk = devm_clk_get(&pdev->dev, "core");
102 if (IS_ERR(priv->clk))
103 dev_warn(&pdev->dev, "cannot get core clock resource\n");
104
105
106 priv->sys_clk = devm_clk_get(&pdev->dev, "sys");
107 if (IS_ERR(priv->sys_clk))
108 dev_warn(&pdev->dev, "cannot get sys clock resource\n");
109
110
111
112
113
114 if (!IS_ERR(priv->sys_clk)) {
115 error = clk_prepare_enable(priv->sys_clk);
116 if (error) {
117 dev_err(&pdev->dev, "cannot enable sys clock\n");
118 return error;
119 }
120 }
121
122
123 error = img_ir_probe_raw(priv);
124 error2 = img_ir_probe_hw(priv);
125 if (error && error2) {
126 if (error == -ENODEV)
127 error = error2;
128 goto err_probe;
129 }
130
131
132 priv->irq = irq;
133 error = request_irq(priv->irq, img_ir_isr, 0, "img-ir", priv);
134 if (error) {
135 dev_err(&pdev->dev, "cannot register IRQ %u\n",
136 priv->irq);
137 error = -EIO;
138 goto err_irq;
139 }
140
141 img_ir_ident(priv);
142 img_ir_setup(priv);
143
144 return 0;
145
146err_irq:
147 img_ir_remove_hw(priv);
148 img_ir_remove_raw(priv);
149err_probe:
150 if (!IS_ERR(priv->sys_clk))
151 clk_disable_unprepare(priv->sys_clk);
152 return error;
153}
154
155static int img_ir_remove(struct platform_device *pdev)
156{
157 struct img_ir_priv *priv = platform_get_drvdata(pdev);
158
159 free_irq(priv->irq, priv);
160 img_ir_remove_hw(priv);
161 img_ir_remove_raw(priv);
162
163 if (!IS_ERR(priv->clk))
164 clk_disable_unprepare(priv->clk);
165 if (!IS_ERR(priv->sys_clk))
166 clk_disable_unprepare(priv->sys_clk);
167 return 0;
168}
169
170static SIMPLE_DEV_PM_OPS(img_ir_pmops, img_ir_suspend, img_ir_resume);
171
172static const struct of_device_id img_ir_match[] = {
173 { .compatible = "img,ir-rev1" },
174 {}
175};
176MODULE_DEVICE_TABLE(of, img_ir_match);
177
178static struct platform_driver img_ir_driver = {
179 .driver = {
180 .name = "img-ir",
181 .of_match_table = img_ir_match,
182 .pm = &img_ir_pmops,
183 },
184 .probe = img_ir_probe,
185 .remove = img_ir_remove,
186};
187
188module_platform_driver(img_ir_driver);
189
190MODULE_AUTHOR("Imagination Technologies Ltd.");
191MODULE_DESCRIPTION("ImgTec IR");
192MODULE_LICENSE("GPL");
193