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/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/platform_device.h>
21 #include <linux/gpio.h>
23 #include <linux/delay.h>
24 #include <linux/list.h>
26 #include <linux/clk.h>
28 #include "mcam-core.h"
30 MODULE_ALIAS("platform:mmp-camera");
31 MODULE_AUTHOR("Jonathan Corbet <corbet@lwn.net>");
32 MODULE_LICENSE("GPL");
34 static char *mcam_clks[] = {"axi", "func", "phy"};
37 void __iomem *power_regs;
38 struct platform_device *pdev;
39 struct mcam_camera mcam;
40 struct list_head devlist;
45 static inline struct mmp_camera *mcam_to_cam(struct mcam_camera *mcam)
47 return container_of(mcam, struct mmp_camera, mcam);
51 * A silly little infrastructure so we can keep track of our devices.
52 * Chances are that we will never have more than one of them, but
53 * the Armada 610 *does* have two controllers...
56 static LIST_HEAD(mmpcam_devices);
57 static struct mutex mmpcam_devices_lock;
59 static void mmpcam_add_device(struct mmp_camera *cam)
61 mutex_lock(&mmpcam_devices_lock);
62 list_add(&cam->devlist, &mmpcam_devices);
63 mutex_unlock(&mmpcam_devices_lock);
66 static void mmpcam_remove_device(struct mmp_camera *cam)
68 mutex_lock(&mmpcam_devices_lock);
69 list_del(&cam->devlist);
70 mutex_unlock(&mmpcam_devices_lock);
74 * Platform dev remove passes us a platform_device, and there's
75 * no handy unused drvdata to stash a backpointer in. So just
76 * dig it out of our list.
78 static struct mmp_camera *mmpcam_find_device(struct platform_device *pdev)
80 struct mmp_camera *cam;
82 mutex_lock(&mmpcam_devices_lock);
83 list_for_each_entry(cam, &mmpcam_devices, devlist) {
84 if (cam->pdev == pdev) {
85 mutex_unlock(&mmpcam_devices_lock);
89 mutex_unlock(&mmpcam_devices_lock);
97 * Power-related registers; this almost certainly belongs
100 * ARMADA 610 register manual, sec 7.2.1, p1842.
102 #define CPU_SUBSYS_PMU_BASE 0xd4282800
103 #define REG_CCIC_DCGCR 0x28 /* CCIC dyn clock gate ctrl reg */
104 #define REG_CCIC_CRCR 0x50 /* CCIC clk reset ctrl reg */
106 static void mcam_clk_enable(struct mcam_camera *mcam)
110 for (i = 0; i < NR_MCAM_CLK; i++) {
111 if (!IS_ERR(mcam->clk[i]))
112 clk_prepare_enable(mcam->clk[i]);
116 static void mcam_clk_disable(struct mcam_camera *mcam)
120 for (i = NR_MCAM_CLK - 1; i >= 0; i--) {
121 if (!IS_ERR(mcam->clk[i]))
122 clk_disable_unprepare(mcam->clk[i]);
129 static void mmpcam_power_up_ctlr(struct mmp_camera *cam)
131 iowrite32(0x3f, cam->power_regs + REG_CCIC_DCGCR);
132 iowrite32(0x3805b, cam->power_regs + REG_CCIC_CRCR);
136 static int mmpcam_power_up(struct mcam_camera *mcam)
138 struct mmp_camera *cam = mcam_to_cam(mcam);
139 struct mmp_camera_platform_data *pdata;
142 * Turn on power and clocks to the controller.
144 mmpcam_power_up_ctlr(cam);
145 mcam_clk_enable(mcam);
147 * Provide power to the sensor.
149 mcam_reg_write(mcam, REG_CLKCTRL, 0x60000002);
150 pdata = cam->pdev->dev.platform_data;
151 gpio_set_value(pdata->sensor_power_gpio, 1);
153 mcam_reg_clear_bit(mcam, REG_CTRL1, 0x10000000);
154 gpio_set_value(pdata->sensor_reset_gpio, 0); /* reset is active low */
156 gpio_set_value(pdata->sensor_reset_gpio, 1); /* reset is active low */
162 static void mmpcam_power_down(struct mcam_camera *mcam)
164 struct mmp_camera *cam = mcam_to_cam(mcam);
165 struct mmp_camera_platform_data *pdata;
167 * Turn off clocks and set reset lines
169 iowrite32(0, cam->power_regs + REG_CCIC_DCGCR);
170 iowrite32(0, cam->power_regs + REG_CCIC_CRCR);
172 * Shut down the sensor.
174 pdata = cam->pdev->dev.platform_data;
175 gpio_set_value(pdata->sensor_power_gpio, 0);
176 gpio_set_value(pdata->sensor_reset_gpio, 0);
178 mcam_clk_disable(mcam);
182 * calc the dphy register values
183 * There are three dphy registers being used.
184 * dphy[0] - CSI2_DPHY3
185 * dphy[1] - CSI2_DPHY5
186 * dphy[2] - CSI2_DPHY6
187 * CSI2_DPHY3 and CSI2_DPHY6 can be set with a default value
188 * or be calculated dynamically
190 static void mmpcam_calc_dphy(struct mcam_camera *mcam)
192 struct mmp_camera *cam = mcam_to_cam(mcam);
193 struct mmp_camera_platform_data *pdata = cam->pdev->dev.platform_data;
194 struct device *dev = &cam->pdev->dev;
195 unsigned long tx_clk_esc;
198 * If CSI2_DPHY3 is calculated dynamically,
199 * pdata->lane_clk should be already set
200 * either in the board driver statically
201 * or in the sensor driver dynamically.
204 * dphy[0] - CSI2_DPHY3:
205 * bit 0 ~ bit 7: HS Term Enable.
206 * defines the time that the DPHY
207 * wait before enabling the data
208 * lane termination after detecting
209 * that the sensor has driven the data
210 * lanes to the LP00 bridge state.
211 * The value is calculated by:
212 * (Max T(D_TERM_EN)/Period(DDR)) - 1
213 * bit 8 ~ bit 15: HS_SETTLE
214 * Time interval during which the HS
215 * receiver shall ignore any Data Lane
217 * The value has been calibrated on
218 * different boards. It seems to work well.
220 * More detail please refer
221 * MIPI Alliance Spectification for D-PHY
222 * document for explanation of HS-SETTLE
225 switch (pdata->dphy3_algo) {
226 case DPHY3_ALGO_PXA910:
228 * Calculate CSI2_DPHY3 algo for PXA910
231 (((1 + (pdata->lane_clk * 80) / 1000) & 0xff) << 8)
232 | (1 + pdata->lane_clk * 35 / 1000);
234 case DPHY3_ALGO_PXA2128:
236 * Calculate CSI2_DPHY3 algo for PXA2128
239 (((2 + (pdata->lane_clk * 110) / 1000) & 0xff) << 8)
240 | (1 + pdata->lane_clk * 35 / 1000);
244 * Use default CSI2_DPHY3 value for PXA688/PXA988
246 dev_dbg(dev, "camera: use the default CSI2_DPHY3 value\n");
250 * mipi_clk will never be changed, it is a fixed value on MMP
252 if (IS_ERR(cam->mipi_clk))
255 /* get the escape clk, this is hard coded */
256 clk_prepare_enable(cam->mipi_clk);
257 tx_clk_esc = (clk_get_rate(cam->mipi_clk) / 1000000) / 12;
258 clk_disable_unprepare(cam->mipi_clk);
260 * dphy[2] - CSI2_DPHY6:
261 * bit 0 ~ bit 7: CK Term Enable
262 * Time for the Clock Lane receiver to enable the HS line
263 * termination. The value is calculated similarly with
265 * bit 8 ~ bit 15: CK Settle
266 * Time interval during which the HS receiver shall ignore
267 * any Clock Lane HS transitions.
268 * The value is calibrated on the boards.
271 ((((534 * tx_clk_esc) / 2000 - 1) & 0xff) << 8)
272 | (((38 * tx_clk_esc) / 1000 - 1) & 0xff);
274 dev_dbg(dev, "camera: DPHY sets: dphy3=0x%x, dphy5=0x%x, dphy6=0x%x\n",
275 pdata->dphy[0], pdata->dphy[1], pdata->dphy[2]);
278 static irqreturn_t mmpcam_irq(int irq, void *data)
280 struct mcam_camera *mcam = data;
281 unsigned int irqs, handled;
283 spin_lock(&mcam->dev_lock);
284 irqs = mcam_reg_read(mcam, REG_IRQSTAT);
285 handled = mccic_irq(mcam, irqs);
286 spin_unlock(&mcam->dev_lock);
287 return IRQ_RETVAL(handled);
290 static void mcam_init_clk(struct mcam_camera *mcam)
294 for (i = 0; i < NR_MCAM_CLK; i++) {
295 if (mcam_clks[i] != NULL) {
296 /* Some clks are not necessary on some boards
297 * We still try to run even it fails getting clk
299 mcam->clk[i] = devm_clk_get(mcam->dev, mcam_clks[i]);
300 if (IS_ERR(mcam->clk[i]))
301 dev_warn(mcam->dev, "Could not get clk: %s\n",
307 static int mmpcam_probe(struct platform_device *pdev)
309 struct mmp_camera *cam;
310 struct mcam_camera *mcam;
311 struct resource *res;
312 struct mmp_camera_platform_data *pdata;
315 pdata = pdev->dev.platform_data;
319 cam = devm_kzalloc(&pdev->dev, sizeof(*cam), GFP_KERNEL);
323 INIT_LIST_HEAD(&cam->devlist);
326 mcam->plat_power_up = mmpcam_power_up;
327 mcam->plat_power_down = mmpcam_power_down;
328 mcam->calc_dphy = mmpcam_calc_dphy;
329 mcam->dev = &pdev->dev;
331 mcam->mclk_src = pdata->mclk_src;
332 mcam->mclk_div = pdata->mclk_div;
333 mcam->bus_type = pdata->bus_type;
334 mcam->dphy = pdata->dphy;
335 if (mcam->bus_type == V4L2_MBUS_CSI2_DPHY) {
336 cam->mipi_clk = devm_clk_get(mcam->dev, "mipi");
337 if ((IS_ERR(cam->mipi_clk) && mcam->dphy[2] == 0))
338 return PTR_ERR(cam->mipi_clk);
340 mcam->mipi_enabled = false;
341 mcam->lane = pdata->lane;
342 mcam->chip_id = MCAM_ARMADA610;
343 mcam->buffer_mode = B_DMA_sg;
344 strscpy(mcam->bus_info, "platform:mmp-camera", sizeof(mcam->bus_info));
345 spin_lock_init(&mcam->dev_lock);
347 * Get our I/O memory.
349 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
350 mcam->regs = devm_ioremap_resource(&pdev->dev, res);
351 if (IS_ERR(mcam->regs))
352 return PTR_ERR(mcam->regs);
353 mcam->regs_size = resource_size(res);
355 * Power/clock memory is elsewhere; get it too. Perhaps this
356 * should really be managed outside of this driver?
358 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
359 cam->power_regs = devm_ioremap_resource(&pdev->dev, res);
360 if (IS_ERR(cam->power_regs))
361 return PTR_ERR(cam->power_regs);
363 * Find the i2c adapter. This assumes, of course, that the
364 * i2c bus is already up and functioning.
366 mcam->i2c_adapter = platform_get_drvdata(pdata->i2c_device);
367 if (mcam->i2c_adapter == NULL) {
368 dev_err(&pdev->dev, "No i2c adapter\n");
374 ret = devm_gpio_request(&pdev->dev, pdata->sensor_power_gpio,
377 dev_err(&pdev->dev, "Can't get sensor power gpio %d",
378 pdata->sensor_power_gpio);
381 gpio_direction_output(pdata->sensor_power_gpio, 0);
382 ret = devm_gpio_request(&pdev->dev, pdata->sensor_reset_gpio,
385 dev_err(&pdev->dev, "Can't get sensor reset gpio %d",
386 pdata->sensor_reset_gpio);
389 gpio_direction_output(pdata->sensor_reset_gpio, 0);
394 * Power the device up and hand it off to the core.
396 ret = mmpcam_power_up(mcam);
399 ret = mccic_register(mcam);
403 * Finally, set up our IRQ now that the core is ready to
406 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
411 cam->irq = res->start;
412 ret = devm_request_irq(&pdev->dev, cam->irq, mmpcam_irq, IRQF_SHARED,
415 mmpcam_add_device(cam);
420 mccic_shutdown(mcam);
422 mmpcam_power_down(mcam);
427 static int mmpcam_remove(struct mmp_camera *cam)
429 struct mcam_camera *mcam = &cam->mcam;
431 mmpcam_remove_device(cam);
432 mccic_shutdown(mcam);
433 mmpcam_power_down(mcam);
437 static int mmpcam_platform_remove(struct platform_device *pdev)
439 struct mmp_camera *cam = mmpcam_find_device(pdev);
443 return mmpcam_remove(cam);
447 * Suspend/resume support.
451 static int mmpcam_suspend(struct platform_device *pdev, pm_message_t state)
453 struct mmp_camera *cam = mmpcam_find_device(pdev);
455 if (state.event != PM_EVENT_SUSPEND)
457 mccic_suspend(&cam->mcam);
461 static int mmpcam_resume(struct platform_device *pdev)
463 struct mmp_camera *cam = mmpcam_find_device(pdev);
466 * Power up unconditionally just in case the core tries to
467 * touch a register even if nothing was active before; trust
468 * me, it's better this way.
470 mmpcam_power_up_ctlr(cam);
471 return mccic_resume(&cam->mcam);
477 static struct platform_driver mmpcam_driver = {
478 .probe = mmpcam_probe,
479 .remove = mmpcam_platform_remove,
481 .suspend = mmpcam_suspend,
482 .resume = mmpcam_resume,
485 .name = "mmp-camera",
490 static int __init mmpcam_init_module(void)
492 mutex_init(&mmpcam_devices_lock);
493 return platform_driver_register(&mmpcam_driver);
496 static void __exit mmpcam_exit_module(void)
498 platform_driver_unregister(&mmpcam_driver);
500 * platform_driver_unregister() should have emptied the list
502 if (!list_empty(&mmpcam_devices))
503 printk(KERN_ERR "mmp_camera leaving devices behind\n");
506 module_init(mmpcam_init_module);
507 module_exit(mmpcam_exit_module);