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.
5 * Copyright 2011 Jonathan Corbet <corbet@lwn.net>
7 * This file may be distributed under the terms of the GNU General
8 * Public License, version 2.
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>
26 #include <linux/delay.h>
27 #include <linux/list.h>
29 #include <linux/clk.h>
31 #include "mcam-core.h"
33 MODULE_ALIAS("platform:mmp-camera");
34 MODULE_AUTHOR("Jonathan Corbet <corbet@lwn.net>");
35 MODULE_LICENSE("GPL");
37 static char *mcam_clks[] = {"CCICAXICLK", "CCICFUNCLK", "CCICPHYCLK"};
41 struct platform_device *pdev;
42 struct mcam_camera mcam;
43 struct list_head devlist;
48 static inline struct mmp_camera *mcam_to_cam(struct mcam_camera *mcam)
50 return container_of(mcam, struct mmp_camera, mcam);
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...
59 static LIST_HEAD(mmpcam_devices);
60 static struct mutex mmpcam_devices_lock;
62 static void mmpcam_add_device(struct mmp_camera *cam)
64 mutex_lock(&mmpcam_devices_lock);
65 list_add(&cam->devlist, &mmpcam_devices);
66 mutex_unlock(&mmpcam_devices_lock);
69 static void mmpcam_remove_device(struct mmp_camera *cam)
71 mutex_lock(&mmpcam_devices_lock);
72 list_del(&cam->devlist);
73 mutex_unlock(&mmpcam_devices_lock);
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.
81 static struct mmp_camera *mmpcam_find_device(struct platform_device *pdev)
83 struct mmp_camera *cam;
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);
92 mutex_unlock(&mmpcam_devices_lock);
100 * Power-related registers; this almost certainly belongs
103 * ARMADA 610 register manual, sec 7.2.1, p1842.
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 */
110 static void mcam_clk_enable(struct mcam_camera *mcam)
114 for (i = 0; i < NR_MCAM_CLK; i++) {
115 if (!IS_ERR(mcam->clk[i]))
116 clk_prepare_enable(mcam->clk[i]);
120 static void mcam_clk_disable(struct mcam_camera *mcam)
124 for (i = NR_MCAM_CLK - 1; i >= 0; i--) {
125 if (!IS_ERR(mcam->clk[i]))
126 clk_disable_unprepare(mcam->clk[i]);
133 static void mmpcam_power_up_ctlr(struct mmp_camera *cam)
135 iowrite32(0x3f, cam->power_regs + REG_CCIC_DCGCR);
136 iowrite32(0x3805b, cam->power_regs + REG_CCIC_CRCR);
140 static int mmpcam_power_up(struct mcam_camera *mcam)
142 struct mmp_camera *cam = mcam_to_cam(mcam);
143 struct mmp_camera_platform_data *pdata;
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);
152 * Turn on power and clocks to the controller.
154 mmpcam_power_up_ctlr(cam);
156 * Provide power to the sensor.
158 mcam_reg_write(mcam, REG_CLKCTRL, 0x60000002);
159 pdata = cam->pdev->dev.platform_data;
160 gpio_set_value(pdata->sensor_power_gpio, 1);
162 mcam_reg_clear_bit(mcam, REG_CTRL1, 0x10000000);
163 gpio_set_value(pdata->sensor_reset_gpio, 0); /* reset is active low */
165 gpio_set_value(pdata->sensor_reset_gpio, 1); /* reset is active low */
168 mcam_clk_enable(mcam);
173 static void mmpcam_power_down(struct mcam_camera *mcam)
175 struct mmp_camera *cam = mcam_to_cam(mcam);
176 struct mmp_camera_platform_data *pdata;
178 * Turn off clocks and set reset lines
180 iowrite32(0, cam->power_regs + REG_CCIC_DCGCR);
181 iowrite32(0, cam->power_regs + REG_CCIC_CRCR);
183 * Shut down the sensor.
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);
189 if (mcam->bus_type == V4L2_MBUS_CSI2 && !IS_ERR(cam->mipi_clk)) {
191 devm_clk_put(mcam->dev, cam->mipi_clk);
192 cam->mipi_clk = NULL;
195 mcam_clk_disable(mcam);
198 void mcam_ctlr_reset(struct mcam_camera *mcam)
201 struct mmp_camera *cam = mcam_to_cam(mcam);
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);
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);
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
229 void mmpcam_calc_dphy(struct mcam_camera *mcam)
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;
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.
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
256 * The vaule has been calibrated on
257 * different boards. It seems to work well.
259 * More detail please refer
260 * MIPI Alliance Spectification for D-PHY
261 * document for explanation of HS-SETTLE
264 switch (pdata->dphy3_algo) {
265 case DPHY3_ALGO_PXA910:
267 * Calculate CSI2_DPHY3 algo for PXA910
270 (((1 + (pdata->lane_clk * 80) / 1000) & 0xff) << 8)
271 | (1 + pdata->lane_clk * 35 / 1000);
273 case DPHY3_ALGO_PXA2128:
275 * Calculate CSI2_DPHY3 algo for PXA2128
278 (((2 + (pdata->lane_clk * 110) / 1000) & 0xff) << 8)
279 | (1 + pdata->lane_clk * 35 / 1000);
283 * Use default CSI2_DPHY3 value for PXA688/PXA988
285 dev_dbg(dev, "camera: use the default CSI2_DPHY3 value\n");
289 * mipi_clk will never be changed, it is a fixed value on MMP
291 if (IS_ERR(cam->mipi_clk))
294 /* get the escape clk, this is hard coded */
295 tx_clk_esc = (clk_get_rate(cam->mipi_clk) / 1000000) / 12;
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
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.
309 ((((534 * tx_clk_esc) / 2000 - 1) & 0xff) << 8)
310 | (((38 * tx_clk_esc) / 1000 - 1) & 0xff);
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]);
316 static irqreturn_t mmpcam_irq(int irq, void *data)
318 struct mcam_camera *mcam = data;
319 unsigned int irqs, handled;
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);
328 static void mcam_deinit_clk(struct mcam_camera *mcam)
332 for (i = 0; i < NR_MCAM_CLK; i++) {
333 if (!IS_ERR(mcam->clk[i])) {
335 devm_clk_put(mcam->dev, mcam->clk[i]);
341 static void mcam_init_clk(struct mcam_camera *mcam)
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
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",
358 static int mmpcam_probe(struct platform_device *pdev)
360 struct mmp_camera *cam;
361 struct mcam_camera *mcam;
362 struct resource *res;
363 struct mmp_camera_platform_data *pdata;
366 pdata = pdev->dev.platform_data;
370 cam = devm_kzalloc(&pdev->dev, sizeof(*cam), GFP_KERNEL);
374 cam->mipi_clk = NULL;
375 INIT_LIST_HEAD(&cam->devlist);
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;
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);
396 * Get our I/O memory.
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);
404 * Power/clock memory is elsewhere; get it too. Perhaps this
405 * should really be managed outside of this driver?
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);
412 * Find the i2c adapter. This assumes, of course, that the
413 * i2c bus is already up and functioning.
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");
423 ret = devm_gpio_request(&pdev->dev, pdata->sensor_power_gpio,
426 dev_err(&pdev->dev, "Can't get sensor power gpio %d",
427 pdata->sensor_power_gpio);
430 gpio_direction_output(pdata->sensor_power_gpio, 0);
431 ret = devm_gpio_request(&pdev->dev, pdata->sensor_reset_gpio,
434 dev_err(&pdev->dev, "Can't get sensor reset gpio %d",
435 pdata->sensor_reset_gpio);
438 gpio_direction_output(pdata->sensor_reset_gpio, 0);
443 * Power the device up and hand it off to the core.
445 ret = mmpcam_power_up(mcam);
448 ret = mccic_register(mcam);
452 * Finally, set up our IRQ now that the core is ready to
455 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
460 cam->irq = res->start;
461 ret = devm_request_irq(&pdev->dev, cam->irq, mmpcam_irq, IRQF_SHARED,
464 mmpcam_add_device(cam);
469 mccic_shutdown(mcam);
471 mmpcam_power_down(mcam);
473 mcam_deinit_clk(mcam);
478 static int mmpcam_remove(struct mmp_camera *cam)
480 struct mcam_camera *mcam = &cam->mcam;
481 struct mmp_camera_platform_data *pdata;
483 mmpcam_remove_device(cam);
484 mccic_shutdown(mcam);
485 mmpcam_power_down(mcam);
486 pdata = cam->pdev->dev.platform_data;
487 gpio_free(pdata->sensor_reset_gpio);
488 gpio_free(pdata->sensor_power_gpio);
489 mcam_deinit_clk(mcam);
490 iounmap(cam->power_regs);
496 static int mmpcam_platform_remove(struct platform_device *pdev)
498 struct mmp_camera *cam = mmpcam_find_device(pdev);
502 return mmpcam_remove(cam);
506 * Suspend/resume support.
510 static int mmpcam_suspend(struct platform_device *pdev, pm_message_t state)
512 struct mmp_camera *cam = mmpcam_find_device(pdev);
514 if (state.event != PM_EVENT_SUSPEND)
516 mccic_suspend(&cam->mcam);
520 static int mmpcam_resume(struct platform_device *pdev)
522 struct mmp_camera *cam = mmpcam_find_device(pdev);
525 * Power up unconditionally just in case the core tries to
526 * touch a register even if nothing was active before; trust
527 * me, it's better this way.
529 mmpcam_power_up_ctlr(cam);
530 return mccic_resume(&cam->mcam);
536 static struct platform_driver mmpcam_driver = {
537 .probe = mmpcam_probe,
538 .remove = mmpcam_platform_remove,
540 .suspend = mmpcam_suspend,
541 .resume = mmpcam_resume,
544 .name = "mmp-camera",
550 static int __init mmpcam_init_module(void)
552 mutex_init(&mmpcam_devices_lock);
553 return platform_driver_register(&mmpcam_driver);
556 static void __exit mmpcam_exit_module(void)
558 platform_driver_unregister(&mmpcam_driver);
560 * platform_driver_unregister() should have emptied the list
562 if (!list_empty(&mmpcam_devices))
563 printk(KERN_ERR "mmp_camera leaving devices behind\n");
566 module_init(mmpcam_init_module);
567 module_exit(mmpcam_exit_module);