]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/media/platform/marvell-ccic/mmp-driver.c
70cb57f3adc263291c9ac1d977c55862eec92e97
[mirror_ubuntu-bionic-kernel.git] / drivers / media / platform / marvell-ccic / mmp-driver.c
1 /*
2 * Support for the camera device found on Marvell MMP processors; known
3 * to work with the Armada 610 as used in the OLPC 1.75 system.
4 *
5 * Copyright 2011 Jonathan Corbet <corbet@lwn.net>
6 *
7 * This file may be distributed under the terms of the GNU General
8 * Public License, version 2.
9 */
10
11 #include <linux/init.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/i2c.h>
15 #include <linux/i2c-gpio.h>
16 #include <linux/interrupt.h>
17 #include <linux/spinlock.h>
18 #include <linux/slab.h>
19 #include <linux/videodev2.h>
20 #include <media/v4l2-device.h>
21 #include <media/mmp-camera.h>
22 #include <linux/device.h>
23 #include <linux/platform_device.h>
24 #include <linux/gpio.h>
25 #include <linux/io.h>
26 #include <linux/delay.h>
27 #include <linux/list.h>
28 #include <linux/pm.h>
29 #include <linux/clk.h>
30
31 #include "mcam-core.h"
32
33 MODULE_ALIAS("platform:mmp-camera");
34 MODULE_AUTHOR("Jonathan Corbet <corbet@lwn.net>");
35 MODULE_LICENSE("GPL");
36
37 static char *mcam_clks[] = {"CCICAXICLK", "CCICFUNCLK", "CCICPHYCLK"};
38
39 struct mmp_camera {
40 void *power_regs;
41 struct platform_device *pdev;
42 struct mcam_camera mcam;
43 struct list_head devlist;
44 struct clk *mipi_clk;
45 int irq;
46 };
47
48 static inline struct mmp_camera *mcam_to_cam(struct mcam_camera *mcam)
49 {
50 return container_of(mcam, struct mmp_camera, mcam);
51 }
52
53 /*
54 * A silly little infrastructure so we can keep track of our devices.
55 * Chances are that we will never have more than one of them, but
56 * the Armada 610 *does* have two controllers...
57 */
58
59 static LIST_HEAD(mmpcam_devices);
60 static struct mutex mmpcam_devices_lock;
61
62 static void mmpcam_add_device(struct mmp_camera *cam)
63 {
64 mutex_lock(&mmpcam_devices_lock);
65 list_add(&cam->devlist, &mmpcam_devices);
66 mutex_unlock(&mmpcam_devices_lock);
67 }
68
69 static void mmpcam_remove_device(struct mmp_camera *cam)
70 {
71 mutex_lock(&mmpcam_devices_lock);
72 list_del(&cam->devlist);
73 mutex_unlock(&mmpcam_devices_lock);
74 }
75
76 /*
77 * Platform dev remove passes us a platform_device, and there's
78 * no handy unused drvdata to stash a backpointer in. So just
79 * dig it out of our list.
80 */
81 static struct mmp_camera *mmpcam_find_device(struct platform_device *pdev)
82 {
83 struct mmp_camera *cam;
84
85 mutex_lock(&mmpcam_devices_lock);
86 list_for_each_entry(cam, &mmpcam_devices, devlist) {
87 if (cam->pdev == pdev) {
88 mutex_unlock(&mmpcam_devices_lock);
89 return cam;
90 }
91 }
92 mutex_unlock(&mmpcam_devices_lock);
93 return NULL;
94 }
95
96
97
98
99 /*
100 * Power-related registers; this almost certainly belongs
101 * somewhere else.
102 *
103 * ARMADA 610 register manual, sec 7.2.1, p1842.
104 */
105 #define CPU_SUBSYS_PMU_BASE 0xd4282800
106 #define REG_CCIC_DCGCR 0x28 /* CCIC dyn clock gate ctrl reg */
107 #define REG_CCIC_CRCR 0x50 /* CCIC clk reset ctrl reg */
108 #define REG_CCIC2_CRCR 0xf4 /* CCIC2 clk reset ctrl reg */
109
110 static void mcam_clk_enable(struct mcam_camera *mcam)
111 {
112 unsigned int i;
113
114 for (i = 0; i < NR_MCAM_CLK; i++) {
115 if (!IS_ERR(mcam->clk[i]))
116 clk_prepare_enable(mcam->clk[i]);
117 }
118 }
119
120 static void mcam_clk_disable(struct mcam_camera *mcam)
121 {
122 int i;
123
124 for (i = NR_MCAM_CLK - 1; i >= 0; i--) {
125 if (!IS_ERR(mcam->clk[i]))
126 clk_disable_unprepare(mcam->clk[i]);
127 }
128 }
129
130 /*
131 * Power control.
132 */
133 static void mmpcam_power_up_ctlr(struct mmp_camera *cam)
134 {
135 iowrite32(0x3f, cam->power_regs + REG_CCIC_DCGCR);
136 iowrite32(0x3805b, cam->power_regs + REG_CCIC_CRCR);
137 mdelay(1);
138 }
139
140 static int mmpcam_power_up(struct mcam_camera *mcam)
141 {
142 struct mmp_camera *cam = mcam_to_cam(mcam);
143 struct mmp_camera_platform_data *pdata;
144
145 if (mcam->bus_type == V4L2_MBUS_CSI2) {
146 cam->mipi_clk = devm_clk_get(mcam->dev, "mipi");
147 if ((IS_ERR(cam->mipi_clk) && mcam->dphy[2] == 0))
148 return PTR_ERR(cam->mipi_clk);
149 }
150
151 /*
152 * Turn on power and clocks to the controller.
153 */
154 mmpcam_power_up_ctlr(cam);
155 /*
156 * Provide power to the sensor.
157 */
158 mcam_reg_write(mcam, REG_CLKCTRL, 0x60000002);
159 pdata = cam->pdev->dev.platform_data;
160 gpio_set_value(pdata->sensor_power_gpio, 1);
161 mdelay(5);
162 mcam_reg_clear_bit(mcam, REG_CTRL1, 0x10000000);
163 gpio_set_value(pdata->sensor_reset_gpio, 0); /* reset is active low */
164 mdelay(5);
165 gpio_set_value(pdata->sensor_reset_gpio, 1); /* reset is active low */
166 mdelay(5);
167
168 mcam_clk_enable(mcam);
169
170 return 0;
171 }
172
173 static void mmpcam_power_down(struct mcam_camera *mcam)
174 {
175 struct mmp_camera *cam = mcam_to_cam(mcam);
176 struct mmp_camera_platform_data *pdata;
177 /*
178 * Turn off clocks and set reset lines
179 */
180 iowrite32(0, cam->power_regs + REG_CCIC_DCGCR);
181 iowrite32(0, cam->power_regs + REG_CCIC_CRCR);
182 /*
183 * Shut down the sensor.
184 */
185 pdata = cam->pdev->dev.platform_data;
186 gpio_set_value(pdata->sensor_power_gpio, 0);
187 gpio_set_value(pdata->sensor_reset_gpio, 0);
188
189 if (mcam->bus_type == V4L2_MBUS_CSI2 && !IS_ERR(cam->mipi_clk)) {
190 if (cam->mipi_clk)
191 devm_clk_put(mcam->dev, cam->mipi_clk);
192 cam->mipi_clk = NULL;
193 }
194
195 mcam_clk_disable(mcam);
196 }
197
198 void mcam_ctlr_reset(struct mcam_camera *mcam)
199 {
200 unsigned long val;
201 struct mmp_camera *cam = mcam_to_cam(mcam);
202
203 if (mcam->ccic_id) {
204 /*
205 * Using CCIC2
206 */
207 val = ioread32(cam->power_regs + REG_CCIC2_CRCR);
208 iowrite32(val & ~0x2, cam->power_regs + REG_CCIC2_CRCR);
209 iowrite32(val | 0x2, cam->power_regs + REG_CCIC2_CRCR);
210 } else {
211 /*
212 * Using CCIC1
213 */
214 val = ioread32(cam->power_regs + REG_CCIC_CRCR);
215 iowrite32(val & ~0x2, cam->power_regs + REG_CCIC_CRCR);
216 iowrite32(val | 0x2, cam->power_regs + REG_CCIC_CRCR);
217 }
218 }
219
220 /*
221 * calc the dphy register values
222 * There are three dphy registers being used.
223 * dphy[0] - CSI2_DPHY3
224 * dphy[1] - CSI2_DPHY5
225 * dphy[2] - CSI2_DPHY6
226 * CSI2_DPHY3 and CSI2_DPHY6 can be set with a default value
227 * or be calculated dynamically
228 */
229 void mmpcam_calc_dphy(struct mcam_camera *mcam)
230 {
231 struct mmp_camera *cam = mcam_to_cam(mcam);
232 struct mmp_camera_platform_data *pdata = cam->pdev->dev.platform_data;
233 struct device *dev = &cam->pdev->dev;
234 unsigned long tx_clk_esc;
235
236 /*
237 * If CSI2_DPHY3 is calculated dynamically,
238 * pdata->lane_clk should be already set
239 * either in the board driver statically
240 * or in the sensor driver dynamically.
241 */
242 /*
243 * dphy[0] - CSI2_DPHY3:
244 * bit 0 ~ bit 7: HS Term Enable.
245 * defines the time that the DPHY
246 * wait before enabling the data
247 * lane termination after detecting
248 * that the sensor has driven the data
249 * lanes to the LP00 bridge state.
250 * The value is calculated by:
251 * (Max T(D_TERM_EN)/Period(DDR)) - 1
252 * bit 8 ~ bit 15: HS_SETTLE
253 * Time interval during which the HS
254 * receiver shall ignore any Data Lane
255 * HS transistions.
256 * The vaule has been calibrated on
257 * different boards. It seems to work well.
258 *
259 * More detail please refer
260 * MIPI Alliance Spectification for D-PHY
261 * document for explanation of HS-SETTLE
262 * and D-TERM-EN.
263 */
264 switch (pdata->dphy3_algo) {
265 case DPHY3_ALGO_PXA910:
266 /*
267 * Calculate CSI2_DPHY3 algo for PXA910
268 */
269 pdata->dphy[0] =
270 (((1 + (pdata->lane_clk * 80) / 1000) & 0xff) << 8)
271 | (1 + pdata->lane_clk * 35 / 1000);
272 break;
273 case DPHY3_ALGO_PXA2128:
274 /*
275 * Calculate CSI2_DPHY3 algo for PXA2128
276 */
277 pdata->dphy[0] =
278 (((2 + (pdata->lane_clk * 110) / 1000) & 0xff) << 8)
279 | (1 + pdata->lane_clk * 35 / 1000);
280 break;
281 default:
282 /*
283 * Use default CSI2_DPHY3 value for PXA688/PXA988
284 */
285 dev_dbg(dev, "camera: use the default CSI2_DPHY3 value\n");
286 }
287
288 /*
289 * mipi_clk will never be changed, it is a fixed value on MMP
290 */
291 if (IS_ERR(cam->mipi_clk))
292 return;
293
294 /* get the escape clk, this is hard coded */
295 tx_clk_esc = (clk_get_rate(cam->mipi_clk) / 1000000) / 12;
296
297 /*
298 * dphy[2] - CSI2_DPHY6:
299 * bit 0 ~ bit 7: CK Term Enable
300 * Time for the Clock Lane receiver to enable the HS line
301 * termination. The value is calculated similarly with
302 * HS Term Enable
303 * bit 8 ~ bit 15: CK Settle
304 * Time interval during which the HS receiver shall ignore
305 * any Clock Lane HS transitions.
306 * The value is calibrated on the boards.
307 */
308 pdata->dphy[2] =
309 ((((534 * tx_clk_esc) / 2000 - 1) & 0xff) << 8)
310 | (((38 * tx_clk_esc) / 1000 - 1) & 0xff);
311
312 dev_dbg(dev, "camera: DPHY sets: dphy3=0x%x, dphy5=0x%x, dphy6=0x%x\n",
313 pdata->dphy[0], pdata->dphy[1], pdata->dphy[2]);
314 }
315
316 static irqreturn_t mmpcam_irq(int irq, void *data)
317 {
318 struct mcam_camera *mcam = data;
319 unsigned int irqs, handled;
320
321 spin_lock(&mcam->dev_lock);
322 irqs = mcam_reg_read(mcam, REG_IRQSTAT);
323 handled = mccic_irq(mcam, irqs);
324 spin_unlock(&mcam->dev_lock);
325 return IRQ_RETVAL(handled);
326 }
327
328 static void mcam_deinit_clk(struct mcam_camera *mcam)
329 {
330 unsigned int i;
331
332 for (i = 0; i < NR_MCAM_CLK; i++) {
333 if (!IS_ERR(mcam->clk[i])) {
334 if (mcam->clk[i])
335 devm_clk_put(mcam->dev, mcam->clk[i]);
336 }
337 mcam->clk[i] = NULL;
338 }
339 }
340
341 static void mcam_init_clk(struct mcam_camera *mcam)
342 {
343 unsigned int i;
344
345 for (i = 0; i < NR_MCAM_CLK; i++) {
346 if (mcam_clks[i] != NULL) {
347 /* Some clks are not necessary on some boards
348 * We still try to run even it fails getting clk
349 */
350 mcam->clk[i] = devm_clk_get(mcam->dev, mcam_clks[i]);
351 if (IS_ERR(mcam->clk[i]))
352 dev_warn(mcam->dev, "Could not get clk: %s\n",
353 mcam_clks[i]);
354 }
355 }
356 }
357
358 static int mmpcam_probe(struct platform_device *pdev)
359 {
360 struct mmp_camera *cam;
361 struct mcam_camera *mcam;
362 struct resource *res;
363 struct mmp_camera_platform_data *pdata;
364 int ret;
365
366 pdata = pdev->dev.platform_data;
367 if (!pdata)
368 return -ENODEV;
369
370 cam = devm_kzalloc(&pdev->dev, sizeof(*cam), GFP_KERNEL);
371 if (cam == NULL)
372 return -ENOMEM;
373 cam->pdev = pdev;
374 cam->mipi_clk = NULL;
375 INIT_LIST_HEAD(&cam->devlist);
376
377 mcam = &cam->mcam;
378 mcam->plat_power_up = mmpcam_power_up;
379 mcam->plat_power_down = mmpcam_power_down;
380 mcam->ctlr_reset = mcam_ctlr_reset;
381 mcam->calc_dphy = mmpcam_calc_dphy;
382 mcam->dev = &pdev->dev;
383 mcam->use_smbus = 0;
384 mcam->ccic_id = pdev->id;
385 mcam->mclk_min = pdata->mclk_min;
386 mcam->mclk_src = pdata->mclk_src;
387 mcam->mclk_div = pdata->mclk_div;
388 mcam->bus_type = pdata->bus_type;
389 mcam->dphy = pdata->dphy;
390 mcam->mipi_enabled = false;
391 mcam->lane = pdata->lane;
392 mcam->chip_id = MCAM_ARMADA610;
393 mcam->buffer_mode = B_DMA_sg;
394 spin_lock_init(&mcam->dev_lock);
395 /*
396 * Get our I/O memory.
397 */
398 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
399 mcam->regs = devm_ioremap_resource(&pdev->dev, res);
400 if (IS_ERR(mcam->regs))
401 return PTR_ERR(mcam->regs);
402 mcam->regs_size = resource_size(res);
403 /*
404 * Power/clock memory is elsewhere; get it too. Perhaps this
405 * should really be managed outside of this driver?
406 */
407 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
408 cam->power_regs = devm_ioremap_resource(&pdev->dev, res);
409 if (IS_ERR(cam->power_regs))
410 return PTR_ERR(cam->power_regs);
411 /*
412 * Find the i2c adapter. This assumes, of course, that the
413 * i2c bus is already up and functioning.
414 */
415 mcam->i2c_adapter = platform_get_drvdata(pdata->i2c_device);
416 if (mcam->i2c_adapter == NULL) {
417 dev_err(&pdev->dev, "No i2c adapter\n");
418 return -ENODEV;
419 }
420 /*
421 * Sensor GPIO pins.
422 */
423 ret = devm_gpio_request(&pdev->dev, pdata->sensor_power_gpio,
424 "cam-power");
425 if (ret) {
426 dev_err(&pdev->dev, "Can't get sensor power gpio %d",
427 pdata->sensor_power_gpio);
428 return ret;
429 }
430 gpio_direction_output(pdata->sensor_power_gpio, 0);
431 ret = devm_gpio_request(&pdev->dev, pdata->sensor_reset_gpio,
432 "cam-reset");
433 if (ret) {
434 dev_err(&pdev->dev, "Can't get sensor reset gpio %d",
435 pdata->sensor_reset_gpio);
436 return ret;
437 }
438 gpio_direction_output(pdata->sensor_reset_gpio, 0);
439
440 mcam_init_clk(mcam);
441
442 /*
443 * Power the device up and hand it off to the core.
444 */
445 ret = mmpcam_power_up(mcam);
446 if (ret)
447 goto out_deinit_clk;
448 ret = mccic_register(mcam);
449 if (ret)
450 goto out_power_down;
451 /*
452 * Finally, set up our IRQ now that the core is ready to
453 * deal with it.
454 */
455 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
456 if (res == NULL) {
457 ret = -ENODEV;
458 goto out_unregister;
459 }
460 cam->irq = res->start;
461 ret = devm_request_irq(&pdev->dev, cam->irq, mmpcam_irq, IRQF_SHARED,
462 "mmp-camera", mcam);
463 if (ret == 0) {
464 mmpcam_add_device(cam);
465 return 0;
466 }
467
468 out_unregister:
469 mccic_shutdown(mcam);
470 out_power_down:
471 mmpcam_power_down(mcam);
472 out_deinit_clk:
473 mcam_deinit_clk(mcam);
474 return ret;
475 }
476
477
478 static int mmpcam_remove(struct mmp_camera *cam)
479 {
480 struct mcam_camera *mcam = &cam->mcam;
481
482 mmpcam_remove_device(cam);
483 mccic_shutdown(mcam);
484 mmpcam_power_down(mcam);
485 mcam_deinit_clk(mcam);
486 return 0;
487 }
488
489 static int mmpcam_platform_remove(struct platform_device *pdev)
490 {
491 struct mmp_camera *cam = mmpcam_find_device(pdev);
492
493 if (cam == NULL)
494 return -ENODEV;
495 return mmpcam_remove(cam);
496 }
497
498 /*
499 * Suspend/resume support.
500 */
501 #ifdef CONFIG_PM
502
503 static int mmpcam_suspend(struct platform_device *pdev, pm_message_t state)
504 {
505 struct mmp_camera *cam = mmpcam_find_device(pdev);
506
507 if (state.event != PM_EVENT_SUSPEND)
508 return 0;
509 mccic_suspend(&cam->mcam);
510 return 0;
511 }
512
513 static int mmpcam_resume(struct platform_device *pdev)
514 {
515 struct mmp_camera *cam = mmpcam_find_device(pdev);
516
517 /*
518 * Power up unconditionally just in case the core tries to
519 * touch a register even if nothing was active before; trust
520 * me, it's better this way.
521 */
522 mmpcam_power_up_ctlr(cam);
523 return mccic_resume(&cam->mcam);
524 }
525
526 #endif
527
528
529 static struct platform_driver mmpcam_driver = {
530 .probe = mmpcam_probe,
531 .remove = mmpcam_platform_remove,
532 #ifdef CONFIG_PM
533 .suspend = mmpcam_suspend,
534 .resume = mmpcam_resume,
535 #endif
536 .driver = {
537 .name = "mmp-camera",
538 .owner = THIS_MODULE
539 }
540 };
541
542
543 static int __init mmpcam_init_module(void)
544 {
545 mutex_init(&mmpcam_devices_lock);
546 return platform_driver_register(&mmpcam_driver);
547 }
548
549 static void __exit mmpcam_exit_module(void)
550 {
551 platform_driver_unregister(&mmpcam_driver);
552 /*
553 * platform_driver_unregister() should have emptied the list
554 */
555 if (!list_empty(&mmpcam_devices))
556 printk(KERN_ERR "mmp_camera leaving devices behind\n");
557 }
558
559 module_init(mmpcam_init_module);
560 module_exit(mmpcam_exit_module);