1 // SPDX-License-Identifier: GPL-2.0-only
3 * Support for the camera device found on Marvell MMP processors; known
4 * to work with the Armada 610 as used in the OLPC 1.75 system.
6 * Copyright 2011 Jonathan Corbet <corbet@lwn.net>
9 #include <linux/init.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/i2c.h>
13 #include <linux/platform_data/i2c-gpio.h>
14 #include <linux/interrupt.h>
15 #include <linux/spinlock.h>
16 #include <linux/slab.h>
17 #include <linux/videodev2.h>
18 #include <media/v4l2-device.h>
19 #include <linux/platform_data/media/mmp-camera.h>
20 #include <linux/device.h>
21 #include <linux/platform_device.h>
22 #include <linux/gpio.h>
24 #include <linux/delay.h>
25 #include <linux/list.h>
27 #include <linux/clk.h>
29 #include "mcam-core.h"
31 MODULE_ALIAS("platform:mmp-camera");
32 MODULE_AUTHOR("Jonathan Corbet <corbet@lwn.net>");
33 MODULE_LICENSE("GPL");
35 static char *mcam_clks[] = {"CCICAXICLK", "CCICFUNCLK", "CCICPHYCLK"};
38 void __iomem *power_regs;
39 struct platform_device *pdev;
40 struct mcam_camera mcam;
41 struct list_head devlist;
46 static inline struct mmp_camera *mcam_to_cam(struct mcam_camera *mcam)
48 return container_of(mcam, struct mmp_camera, mcam);
52 * A silly little infrastructure so we can keep track of our devices.
53 * Chances are that we will never have more than one of them, but
54 * the Armada 610 *does* have two controllers...
57 static LIST_HEAD(mmpcam_devices);
58 static struct mutex mmpcam_devices_lock;
60 static void mmpcam_add_device(struct mmp_camera *cam)
62 mutex_lock(&mmpcam_devices_lock);
63 list_add(&cam->devlist, &mmpcam_devices);
64 mutex_unlock(&mmpcam_devices_lock);
67 static void mmpcam_remove_device(struct mmp_camera *cam)
69 mutex_lock(&mmpcam_devices_lock);
70 list_del(&cam->devlist);
71 mutex_unlock(&mmpcam_devices_lock);
75 * Platform dev remove passes us a platform_device, and there's
76 * no handy unused drvdata to stash a backpointer in. So just
77 * dig it out of our list.
79 static struct mmp_camera *mmpcam_find_device(struct platform_device *pdev)
81 struct mmp_camera *cam;
83 mutex_lock(&mmpcam_devices_lock);
84 list_for_each_entry(cam, &mmpcam_devices, devlist) {
85 if (cam->pdev == pdev) {
86 mutex_unlock(&mmpcam_devices_lock);
90 mutex_unlock(&mmpcam_devices_lock);
98 * Power-related registers; this almost certainly belongs
101 * ARMADA 610 register manual, sec 7.2.1, p1842.
103 #define CPU_SUBSYS_PMU_BASE 0xd4282800
104 #define REG_CCIC_DCGCR 0x28 /* CCIC dyn clock gate ctrl reg */
105 #define REG_CCIC_CRCR 0x50 /* CCIC clk reset ctrl reg */
106 #define REG_CCIC2_CRCR 0xf4 /* CCIC2 clk reset ctrl reg */
108 static void mcam_clk_enable(struct mcam_camera *mcam)
112 for (i = 0; i < NR_MCAM_CLK; i++) {
113 if (!IS_ERR(mcam->clk[i]))
114 clk_prepare_enable(mcam->clk[i]);
118 static void mcam_clk_disable(struct mcam_camera *mcam)
122 for (i = NR_MCAM_CLK - 1; i >= 0; i--) {
123 if (!IS_ERR(mcam->clk[i]))
124 clk_disable_unprepare(mcam->clk[i]);
131 static void mmpcam_power_up_ctlr(struct mmp_camera *cam)
133 iowrite32(0x3f, cam->power_regs + REG_CCIC_DCGCR);
134 iowrite32(0x3805b, cam->power_regs + REG_CCIC_CRCR);
138 static int mmpcam_power_up(struct mcam_camera *mcam)
140 struct mmp_camera *cam = mcam_to_cam(mcam);
141 struct mmp_camera_platform_data *pdata;
144 * Turn on power and clocks to the controller.
146 mmpcam_power_up_ctlr(cam);
148 * Provide power to the sensor.
150 mcam_reg_write(mcam, REG_CLKCTRL, 0x60000002);
151 pdata = cam->pdev->dev.platform_data;
152 gpio_set_value(pdata->sensor_power_gpio, 1);
154 mcam_reg_clear_bit(mcam, REG_CTRL1, 0x10000000);
155 gpio_set_value(pdata->sensor_reset_gpio, 0); /* reset is active low */
157 gpio_set_value(pdata->sensor_reset_gpio, 1); /* reset is active low */
160 mcam_clk_enable(mcam);
165 static void mmpcam_power_down(struct mcam_camera *mcam)
167 struct mmp_camera *cam = mcam_to_cam(mcam);
168 struct mmp_camera_platform_data *pdata;
170 * Turn off clocks and set reset lines
172 iowrite32(0, cam->power_regs + REG_CCIC_DCGCR);
173 iowrite32(0, cam->power_regs + REG_CCIC_CRCR);
175 * Shut down the sensor.
177 pdata = cam->pdev->dev.platform_data;
178 gpio_set_value(pdata->sensor_power_gpio, 0);
179 gpio_set_value(pdata->sensor_reset_gpio, 0);
181 mcam_clk_disable(mcam);
184 static void mcam_ctlr_reset(struct mcam_camera *mcam)
187 struct mmp_camera *cam = mcam_to_cam(mcam);
193 val = ioread32(cam->power_regs + REG_CCIC2_CRCR);
194 iowrite32(val & ~0x2, cam->power_regs + REG_CCIC2_CRCR);
195 iowrite32(val | 0x2, cam->power_regs + REG_CCIC2_CRCR);
200 val = ioread32(cam->power_regs + REG_CCIC_CRCR);
201 iowrite32(val & ~0x2, cam->power_regs + REG_CCIC_CRCR);
202 iowrite32(val | 0x2, cam->power_regs + REG_CCIC_CRCR);
207 * calc the dphy register values
208 * There are three dphy registers being used.
209 * dphy[0] - CSI2_DPHY3
210 * dphy[1] - CSI2_DPHY5
211 * dphy[2] - CSI2_DPHY6
212 * CSI2_DPHY3 and CSI2_DPHY6 can be set with a default value
213 * or be calculated dynamically
215 static void mmpcam_calc_dphy(struct mcam_camera *mcam)
217 struct mmp_camera *cam = mcam_to_cam(mcam);
218 struct mmp_camera_platform_data *pdata = cam->pdev->dev.platform_data;
219 struct device *dev = &cam->pdev->dev;
220 unsigned long tx_clk_esc;
223 * If CSI2_DPHY3 is calculated dynamically,
224 * pdata->lane_clk should be already set
225 * either in the board driver statically
226 * or in the sensor driver dynamically.
229 * dphy[0] - CSI2_DPHY3:
230 * bit 0 ~ bit 7: HS Term Enable.
231 * defines the time that the DPHY
232 * wait before enabling the data
233 * lane termination after detecting
234 * that the sensor has driven the data
235 * lanes to the LP00 bridge state.
236 * The value is calculated by:
237 * (Max T(D_TERM_EN)/Period(DDR)) - 1
238 * bit 8 ~ bit 15: HS_SETTLE
239 * Time interval during which the HS
240 * receiver shall ignore any Data Lane
242 * The value has been calibrated on
243 * different boards. It seems to work well.
245 * More detail please refer
246 * MIPI Alliance Spectification for D-PHY
247 * document for explanation of HS-SETTLE
250 switch (pdata->dphy3_algo) {
251 case DPHY3_ALGO_PXA910:
253 * Calculate CSI2_DPHY3 algo for PXA910
256 (((1 + (pdata->lane_clk * 80) / 1000) & 0xff) << 8)
257 | (1 + pdata->lane_clk * 35 / 1000);
259 case DPHY3_ALGO_PXA2128:
261 * Calculate CSI2_DPHY3 algo for PXA2128
264 (((2 + (pdata->lane_clk * 110) / 1000) & 0xff) << 8)
265 | (1 + pdata->lane_clk * 35 / 1000);
269 * Use default CSI2_DPHY3 value for PXA688/PXA988
271 dev_dbg(dev, "camera: use the default CSI2_DPHY3 value\n");
275 * mipi_clk will never be changed, it is a fixed value on MMP
277 if (IS_ERR(cam->mipi_clk))
280 /* get the escape clk, this is hard coded */
281 clk_prepare_enable(cam->mipi_clk);
282 tx_clk_esc = (clk_get_rate(cam->mipi_clk) / 1000000) / 12;
283 clk_disable_unprepare(cam->mipi_clk);
285 * dphy[2] - CSI2_DPHY6:
286 * bit 0 ~ bit 7: CK Term Enable
287 * Time for the Clock Lane receiver to enable the HS line
288 * termination. The value is calculated similarly with
290 * bit 8 ~ bit 15: CK Settle
291 * Time interval during which the HS receiver shall ignore
292 * any Clock Lane HS transitions.
293 * The value is calibrated on the boards.
296 ((((534 * tx_clk_esc) / 2000 - 1) & 0xff) << 8)
297 | (((38 * tx_clk_esc) / 1000 - 1) & 0xff);
299 dev_dbg(dev, "camera: DPHY sets: dphy3=0x%x, dphy5=0x%x, dphy6=0x%x\n",
300 pdata->dphy[0], pdata->dphy[1], pdata->dphy[2]);
303 static irqreturn_t mmpcam_irq(int irq, void *data)
305 struct mcam_camera *mcam = data;
306 unsigned int irqs, handled;
308 spin_lock(&mcam->dev_lock);
309 irqs = mcam_reg_read(mcam, REG_IRQSTAT);
310 handled = mccic_irq(mcam, irqs);
311 spin_unlock(&mcam->dev_lock);
312 return IRQ_RETVAL(handled);
315 static void mcam_init_clk(struct mcam_camera *mcam)
319 for (i = 0; i < NR_MCAM_CLK; i++) {
320 if (mcam_clks[i] != NULL) {
321 /* Some clks are not necessary on some boards
322 * We still try to run even it fails getting clk
324 mcam->clk[i] = devm_clk_get(mcam->dev, mcam_clks[i]);
325 if (IS_ERR(mcam->clk[i]))
326 dev_warn(mcam->dev, "Could not get clk: %s\n",
332 static int mmpcam_probe(struct platform_device *pdev)
334 struct mmp_camera *cam;
335 struct mcam_camera *mcam;
336 struct resource *res;
337 struct mmp_camera_platform_data *pdata;
340 pdata = pdev->dev.platform_data;
344 cam = devm_kzalloc(&pdev->dev, sizeof(*cam), GFP_KERNEL);
348 INIT_LIST_HEAD(&cam->devlist);
351 mcam->plat_power_up = mmpcam_power_up;
352 mcam->plat_power_down = mmpcam_power_down;
353 mcam->ctlr_reset = mcam_ctlr_reset;
354 mcam->calc_dphy = mmpcam_calc_dphy;
355 mcam->dev = &pdev->dev;
357 mcam->ccic_id = pdev->id;
358 mcam->mclk_min = pdata->mclk_min;
359 mcam->mclk_src = pdata->mclk_src;
360 mcam->mclk_div = pdata->mclk_div;
361 mcam->bus_type = pdata->bus_type;
362 mcam->dphy = pdata->dphy;
363 if (mcam->bus_type == V4L2_MBUS_CSI2_DPHY) {
364 cam->mipi_clk = devm_clk_get(mcam->dev, "mipi");
365 if ((IS_ERR(cam->mipi_clk) && mcam->dphy[2] == 0))
366 return PTR_ERR(cam->mipi_clk);
368 mcam->mipi_enabled = false;
369 mcam->lane = pdata->lane;
370 mcam->chip_id = MCAM_ARMADA610;
371 mcam->buffer_mode = B_DMA_sg;
372 strscpy(mcam->bus_info, "platform:mmp-camera", sizeof(mcam->bus_info));
373 spin_lock_init(&mcam->dev_lock);
375 * Get our I/O memory.
377 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
378 mcam->regs = devm_ioremap_resource(&pdev->dev, res);
379 if (IS_ERR(mcam->regs))
380 return PTR_ERR(mcam->regs);
381 mcam->regs_size = resource_size(res);
383 * Power/clock memory is elsewhere; get it too. Perhaps this
384 * should really be managed outside of this driver?
386 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
387 cam->power_regs = devm_ioremap_resource(&pdev->dev, res);
388 if (IS_ERR(cam->power_regs))
389 return PTR_ERR(cam->power_regs);
391 * Find the i2c adapter. This assumes, of course, that the
392 * i2c bus is already up and functioning.
394 mcam->i2c_adapter = platform_get_drvdata(pdata->i2c_device);
395 if (mcam->i2c_adapter == NULL) {
396 dev_err(&pdev->dev, "No i2c adapter\n");
402 ret = devm_gpio_request(&pdev->dev, pdata->sensor_power_gpio,
405 dev_err(&pdev->dev, "Can't get sensor power gpio %d",
406 pdata->sensor_power_gpio);
409 gpio_direction_output(pdata->sensor_power_gpio, 0);
410 ret = devm_gpio_request(&pdev->dev, pdata->sensor_reset_gpio,
413 dev_err(&pdev->dev, "Can't get sensor reset gpio %d",
414 pdata->sensor_reset_gpio);
417 gpio_direction_output(pdata->sensor_reset_gpio, 0);
422 * Power the device up and hand it off to the core.
424 ret = mmpcam_power_up(mcam);
427 ret = mccic_register(mcam);
431 * Finally, set up our IRQ now that the core is ready to
434 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
439 cam->irq = res->start;
440 ret = devm_request_irq(&pdev->dev, cam->irq, mmpcam_irq, IRQF_SHARED,
443 mmpcam_add_device(cam);
448 mccic_shutdown(mcam);
450 mmpcam_power_down(mcam);
455 static int mmpcam_remove(struct mmp_camera *cam)
457 struct mcam_camera *mcam = &cam->mcam;
459 mmpcam_remove_device(cam);
460 mccic_shutdown(mcam);
461 mmpcam_power_down(mcam);
465 static int mmpcam_platform_remove(struct platform_device *pdev)
467 struct mmp_camera *cam = mmpcam_find_device(pdev);
471 return mmpcam_remove(cam);
475 * Suspend/resume support.
479 static int mmpcam_suspend(struct platform_device *pdev, pm_message_t state)
481 struct mmp_camera *cam = mmpcam_find_device(pdev);
483 if (state.event != PM_EVENT_SUSPEND)
485 mccic_suspend(&cam->mcam);
489 static int mmpcam_resume(struct platform_device *pdev)
491 struct mmp_camera *cam = mmpcam_find_device(pdev);
494 * Power up unconditionally just in case the core tries to
495 * touch a register even if nothing was active before; trust
496 * me, it's better this way.
498 mmpcam_power_up_ctlr(cam);
499 return mccic_resume(&cam->mcam);
505 static struct platform_driver mmpcam_driver = {
506 .probe = mmpcam_probe,
507 .remove = mmpcam_platform_remove,
509 .suspend = mmpcam_suspend,
510 .resume = mmpcam_resume,
513 .name = "mmp-camera",
518 static int __init mmpcam_init_module(void)
520 mutex_init(&mmpcam_devices_lock);
521 return platform_driver_register(&mmpcam_driver);
524 static void __exit mmpcam_exit_module(void)
526 platform_driver_unregister(&mmpcam_driver);
528 * platform_driver_unregister() should have emptied the list
530 if (!list_empty(&mmpcam_devices))
531 printk(KERN_ERR "mmp_camera leaving devices behind\n");
534 module_init(mmpcam_init_module);
535 module_exit(mmpcam_exit_module);