1
2
3
4
5
6
7
8
9
10#include <linux/init.h>
11#include <linux/kernel.h>
12#include <linux/module.h>
13#include <linux/interrupt.h>
14#include <linux/spinlock.h>
15#include <linux/slab.h>
16#include <linux/videodev2.h>
17#include <media/v4l2-device.h>
18#include <linux/platform_data/media/mmp-camera.h>
19#include <linux/device.h>
20#include <linux/of.h>
21#include <linux/of_platform.h>
22#include <linux/platform_device.h>
23#include <linux/pm_runtime.h>
24#include <linux/io.h>
25#include <linux/list.h>
26#include <linux/pm.h>
27#include <linux/clk.h>
28
29#include "mcam-core.h"
30
31MODULE_ALIAS("platform:mmp-camera");
32MODULE_AUTHOR("Jonathan Corbet <corbet@lwn.net>");
33MODULE_LICENSE("GPL");
34
35static char *mcam_clks[] = {"axi", "func", "phy"};
36
37struct mmp_camera {
38 struct platform_device *pdev;
39 struct mcam_camera mcam;
40 struct list_head devlist;
41 struct clk *mipi_clk;
42 int irq;
43};
44
45static inline struct mmp_camera *mcam_to_cam(struct mcam_camera *mcam)
46{
47 return container_of(mcam, struct mmp_camera, mcam);
48}
49
50
51
52
53
54
55
56
57
58
59static void mmpcam_calc_dphy(struct mcam_camera *mcam)
60{
61 struct mmp_camera *cam = mcam_to_cam(mcam);
62 struct mmp_camera_platform_data *pdata = cam->pdev->dev.platform_data;
63 struct device *dev = &cam->pdev->dev;
64 unsigned long tx_clk_esc;
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94 switch (pdata->dphy3_algo) {
95 case DPHY3_ALGO_PXA910:
96
97
98
99 pdata->dphy[0] =
100 (((1 + (pdata->lane_clk * 80) / 1000) & 0xff) << 8)
101 | (1 + pdata->lane_clk * 35 / 1000);
102 break;
103 case DPHY3_ALGO_PXA2128:
104
105
106
107 pdata->dphy[0] =
108 (((2 + (pdata->lane_clk * 110) / 1000) & 0xff) << 8)
109 | (1 + pdata->lane_clk * 35 / 1000);
110 break;
111 default:
112
113
114
115 dev_dbg(dev, "camera: use the default CSI2_DPHY3 value\n");
116 }
117
118
119
120
121 if (IS_ERR(cam->mipi_clk))
122 return;
123
124
125 clk_prepare_enable(cam->mipi_clk);
126 tx_clk_esc = (clk_get_rate(cam->mipi_clk) / 1000000) / 12;
127 clk_disable_unprepare(cam->mipi_clk);
128
129
130
131
132
133
134
135
136
137
138
139 pdata->dphy[2] =
140 ((((534 * tx_clk_esc) / 2000 - 1) & 0xff) << 8)
141 | (((38 * tx_clk_esc) / 1000 - 1) & 0xff);
142
143 dev_dbg(dev, "camera: DPHY sets: dphy3=0x%x, dphy5=0x%x, dphy6=0x%x\n",
144 pdata->dphy[0], pdata->dphy[1], pdata->dphy[2]);
145}
146
147static irqreturn_t mmpcam_irq(int irq, void *data)
148{
149 struct mcam_camera *mcam = data;
150 unsigned int irqs, handled;
151
152 spin_lock(&mcam->dev_lock);
153 irqs = mcam_reg_read(mcam, REG_IRQSTAT);
154 handled = mccic_irq(mcam, irqs);
155 spin_unlock(&mcam->dev_lock);
156 return IRQ_RETVAL(handled);
157}
158
159static void mcam_init_clk(struct mcam_camera *mcam)
160{
161 unsigned int i;
162
163 for (i = 0; i < NR_MCAM_CLK; i++) {
164 if (mcam_clks[i] != NULL) {
165
166
167
168 mcam->clk[i] = devm_clk_get(mcam->dev, mcam_clks[i]);
169 if (IS_ERR(mcam->clk[i]))
170 dev_warn(mcam->dev, "Could not get clk: %s\n",
171 mcam_clks[i]);
172 }
173 }
174}
175
176static int mmpcam_probe(struct platform_device *pdev)
177{
178 struct mmp_camera *cam;
179 struct mcam_camera *mcam;
180 struct resource *res;
181 struct fwnode_handle *ep;
182 struct mmp_camera_platform_data *pdata;
183 struct v4l2_async_subdev *asd;
184 int ret;
185
186 cam = devm_kzalloc(&pdev->dev, sizeof(*cam), GFP_KERNEL);
187 if (cam == NULL)
188 return -ENOMEM;
189 platform_set_drvdata(pdev, cam);
190 cam->pdev = pdev;
191 INIT_LIST_HEAD(&cam->devlist);
192
193 mcam = &cam->mcam;
194 mcam->calc_dphy = mmpcam_calc_dphy;
195 mcam->dev = &pdev->dev;
196 pdata = pdev->dev.platform_data;
197 if (pdata) {
198 mcam->mclk_src = pdata->mclk_src;
199 mcam->mclk_div = pdata->mclk_div;
200 mcam->bus_type = pdata->bus_type;
201 mcam->dphy = pdata->dphy;
202 mcam->lane = pdata->lane;
203 } else {
204
205
206
207
208
209
210 mcam->mclk_src = 3;
211 mcam->mclk_div = 2;
212 }
213 if (mcam->bus_type == V4L2_MBUS_CSI2_DPHY) {
214 cam->mipi_clk = devm_clk_get(mcam->dev, "mipi");
215 if ((IS_ERR(cam->mipi_clk) && mcam->dphy[2] == 0))
216 return PTR_ERR(cam->mipi_clk);
217 }
218 mcam->mipi_enabled = false;
219 mcam->chip_id = MCAM_ARMADA610;
220 mcam->buffer_mode = B_DMA_sg;
221 strscpy(mcam->bus_info, "platform:mmp-camera", sizeof(mcam->bus_info));
222 spin_lock_init(&mcam->dev_lock);
223
224
225
226 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
227 mcam->regs = devm_ioremap_resource(&pdev->dev, res);
228 if (IS_ERR(mcam->regs))
229 return PTR_ERR(mcam->regs);
230 mcam->regs_size = resource_size(res);
231
232 mcam_init_clk(mcam);
233
234
235
236
237 ep = fwnode_graph_get_next_endpoint(of_fwnode_handle(pdev->dev.of_node),
238 NULL);
239 if (!ep)
240 return -ENODEV;
241
242 v4l2_async_notifier_init(&mcam->notifier);
243
244 asd = v4l2_async_notifier_add_fwnode_remote_subdev(&mcam->notifier, ep,
245 struct v4l2_async_subdev);
246 fwnode_handle_put(ep);
247 if (IS_ERR(asd)) {
248 ret = PTR_ERR(asd);
249 goto out;
250 }
251
252
253
254
255 ret = mccic_register(mcam);
256 if (ret)
257 return ret;
258
259
260
261
262 ret = of_clk_add_provider(pdev->dev.of_node, of_clk_src_simple_get,
263 mcam->mclk);
264 if (ret) {
265 dev_err(&pdev->dev, "can't add DT clock provider\n");
266 goto out;
267 }
268
269
270
271
272
273 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
274 if (res == NULL) {
275 ret = -ENODEV;
276 goto out;
277 }
278 cam->irq = res->start;
279 ret = devm_request_irq(&pdev->dev, cam->irq, mmpcam_irq, IRQF_SHARED,
280 "mmp-camera", mcam);
281 if (ret)
282 goto out;
283
284 pm_runtime_enable(&pdev->dev);
285 return 0;
286out:
287 mccic_shutdown(mcam);
288
289 return ret;
290}
291
292
293static int mmpcam_remove(struct mmp_camera *cam)
294{
295 struct mcam_camera *mcam = &cam->mcam;
296
297 mccic_shutdown(mcam);
298 pm_runtime_force_suspend(mcam->dev);
299 return 0;
300}
301
302static int mmpcam_platform_remove(struct platform_device *pdev)
303{
304 struct mmp_camera *cam = platform_get_drvdata(pdev);
305
306 if (cam == NULL)
307 return -ENODEV;
308 return mmpcam_remove(cam);
309}
310
311
312
313
314
315static int __maybe_unused mmpcam_runtime_resume(struct device *dev)
316{
317 struct mmp_camera *cam = dev_get_drvdata(dev);
318 struct mcam_camera *mcam = &cam->mcam;
319 unsigned int i;
320
321 for (i = 0; i < NR_MCAM_CLK; i++) {
322 if (!IS_ERR(mcam->clk[i]))
323 clk_prepare_enable(mcam->clk[i]);
324 }
325
326 return 0;
327}
328
329static int __maybe_unused mmpcam_runtime_suspend(struct device *dev)
330{
331 struct mmp_camera *cam = dev_get_drvdata(dev);
332 struct mcam_camera *mcam = &cam->mcam;
333 int i;
334
335 for (i = NR_MCAM_CLK - 1; i >= 0; i--) {
336 if (!IS_ERR(mcam->clk[i]))
337 clk_disable_unprepare(mcam->clk[i]);
338 }
339
340 return 0;
341}
342
343static int __maybe_unused mmpcam_suspend(struct device *dev)
344{
345 struct mmp_camera *cam = dev_get_drvdata(dev);
346
347 if (!pm_runtime_suspended(dev))
348 mccic_suspend(&cam->mcam);
349 return 0;
350}
351
352static int __maybe_unused mmpcam_resume(struct device *dev)
353{
354 struct mmp_camera *cam = dev_get_drvdata(dev);
355
356 if (!pm_runtime_suspended(dev))
357 return mccic_resume(&cam->mcam);
358 return 0;
359}
360
361static const struct dev_pm_ops mmpcam_pm_ops = {
362 SET_RUNTIME_PM_OPS(mmpcam_runtime_suspend, mmpcam_runtime_resume, NULL)
363 SET_SYSTEM_SLEEP_PM_OPS(mmpcam_suspend, mmpcam_resume)
364};
365
366static const struct of_device_id mmpcam_of_match[] = {
367 { .compatible = "marvell,mmp2-ccic", },
368 {},
369};
370MODULE_DEVICE_TABLE(of, mmpcam_of_match);
371
372static struct platform_driver mmpcam_driver = {
373 .probe = mmpcam_probe,
374 .remove = mmpcam_platform_remove,
375 .driver = {
376 .name = "mmp-camera",
377 .of_match_table = of_match_ptr(mmpcam_of_match),
378 .pm = &mmpcam_pm_ops,
379 }
380};
381
382module_platform_driver(mmpcam_driver);
383