mfd: wm8350-i2c: Make sure the i2c regmap functions are compiled
[linux/fpc-iii.git] / drivers / media / platform / marvell-ccic / mmp-driver.c
blobb5a19af5c587103c1c134b8b5fd0e19a13dc8853
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.
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.
9 */
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>
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"};
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;
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);
89 return cam;
92 mutex_unlock(&mmpcam_devices_lock);
93 return NULL;
100 * Power-related registers; this almost certainly belongs
101 * somewhere else.
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)
112 unsigned int i;
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)
122 int i;
124 for (i = NR_MCAM_CLK - 1; i >= 0; i--) {
125 if (!IS_ERR(mcam->clk[i]))
126 clk_disable_unprepare(mcam->clk[i]);
131 * Power control.
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);
137 mdelay(1);
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);
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);
168 mcam_clk_enable(mcam);
170 return 0;
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)) {
190 if (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)
200 unsigned long val;
201 struct mmp_camera *cam = mcam_to_cam(mcam);
203 if (mcam->ccic_id) {
205 * Using CCIC2
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 {
212 * Using CCIC1
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
255 * HS transistions.
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
262 * and D-TERM-EN.
264 switch (pdata->dphy3_algo) {
265 case DPHY3_ALGO_PXA910:
267 * Calculate CSI2_DPHY3 algo for PXA910
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:
275 * Calculate CSI2_DPHY3 algo for PXA2128
277 pdata->dphy[0] =
278 (((2 + (pdata->lane_clk * 110) / 1000) & 0xff) << 8)
279 | (1 + pdata->lane_clk * 35 / 1000);
280 break;
281 default:
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))
292 return;
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
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.
308 pdata->dphy[2] =
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)
330 unsigned int i;
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]);
337 mcam->clk[i] = NULL;
341 static void mcam_init_clk(struct mcam_camera *mcam)
343 unsigned int i;
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",
353 mcam_clks[i]);
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;
364 int ret;
366 pdata = pdev->dev.platform_data;
367 if (!pdata)
368 return -ENODEV;
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);
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);
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");
418 return -ENODEV;
421 * Sensor GPIO pins.
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;
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;
438 gpio_direction_output(pdata->sensor_reset_gpio, 0);
440 mcam_init_clk(mcam);
443 * Power the device up and hand it off to the core.
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;
452 * Finally, set up our IRQ now that the core is ready to
453 * deal with it.
455 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
456 if (res == NULL) {
457 ret = -ENODEV;
458 goto out_unregister;
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;
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;
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 free_irq(cam->irq, mcam);
485 mccic_shutdown(mcam);
486 mmpcam_power_down(mcam);
487 pdata = cam->pdev->dev.platform_data;
488 gpio_free(pdata->sensor_reset_gpio);
489 gpio_free(pdata->sensor_power_gpio);
490 mcam_deinit_clk(mcam);
491 iounmap(cam->power_regs);
492 iounmap(mcam->regs);
493 kfree(cam);
494 return 0;
497 static int mmpcam_platform_remove(struct platform_device *pdev)
499 struct mmp_camera *cam = mmpcam_find_device(pdev);
501 if (cam == NULL)
502 return -ENODEV;
503 return mmpcam_remove(cam);
507 * Suspend/resume support.
509 #ifdef CONFIG_PM
511 static int mmpcam_suspend(struct platform_device *pdev, pm_message_t state)
513 struct mmp_camera *cam = mmpcam_find_device(pdev);
515 if (state.event != PM_EVENT_SUSPEND)
516 return 0;
517 mccic_suspend(&cam->mcam);
518 return 0;
521 static int mmpcam_resume(struct platform_device *pdev)
523 struct mmp_camera *cam = mmpcam_find_device(pdev);
526 * Power up unconditionally just in case the core tries to
527 * touch a register even if nothing was active before; trust
528 * me, it's better this way.
530 mmpcam_power_up_ctlr(cam);
531 return mccic_resume(&cam->mcam);
534 #endif
537 static struct platform_driver mmpcam_driver = {
538 .probe = mmpcam_probe,
539 .remove = mmpcam_platform_remove,
540 #ifdef CONFIG_PM
541 .suspend = mmpcam_suspend,
542 .resume = mmpcam_resume,
543 #endif
544 .driver = {
545 .name = "mmp-camera",
546 .owner = THIS_MODULE
551 static int __init mmpcam_init_module(void)
553 mutex_init(&mmpcam_devices_lock);
554 return platform_driver_register(&mmpcam_driver);
557 static void __exit mmpcam_exit_module(void)
559 platform_driver_unregister(&mmpcam_driver);
561 * platform_driver_unregister() should have emptied the list
563 if (!list_empty(&mmpcam_devices))
564 printk(KERN_ERR "mmp_camera leaving devices behind\n");
567 module_init(mmpcam_init_module);
568 module_exit(mmpcam_exit_module);