]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/media/platform/marvell-ccic/mmp-driver.c
0634aeca40f213f22e187ea88a41db45881ae347
[linux.git] / drivers / media / platform / marvell-ccic / mmp-driver.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
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.
5  *
6  * Copyright 2011 Jonathan Corbet <corbet@lwn.net>
7  */
8
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>
22 #include <linux/io.h>
23 #include <linux/delay.h>
24 #include <linux/list.h>
25 #include <linux/pm.h>
26 #include <linux/clk.h>
27
28 #include "mcam-core.h"
29
30 MODULE_ALIAS("platform:mmp-camera");
31 MODULE_AUTHOR("Jonathan Corbet <corbet@lwn.net>");
32 MODULE_LICENSE("GPL");
33
34 static char *mcam_clks[] = {"axi", "func", "phy"};
35
36 struct mmp_camera {
37         void __iomem *power_regs;
38         struct platform_device *pdev;
39         struct mcam_camera mcam;
40         struct list_head devlist;
41         struct clk *mipi_clk;
42         int irq;
43 };
44
45 static inline struct mmp_camera *mcam_to_cam(struct mcam_camera *mcam)
46 {
47         return container_of(mcam, struct mmp_camera, mcam);
48 }
49
50 /*
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...
54  */
55
56 static LIST_HEAD(mmpcam_devices);
57 static struct mutex mmpcam_devices_lock;
58
59 static void mmpcam_add_device(struct mmp_camera *cam)
60 {
61         mutex_lock(&mmpcam_devices_lock);
62         list_add(&cam->devlist, &mmpcam_devices);
63         mutex_unlock(&mmpcam_devices_lock);
64 }
65
66 static void mmpcam_remove_device(struct mmp_camera *cam)
67 {
68         mutex_lock(&mmpcam_devices_lock);
69         list_del(&cam->devlist);
70         mutex_unlock(&mmpcam_devices_lock);
71 }
72
73 /*
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.
77  */
78 static struct mmp_camera *mmpcam_find_device(struct platform_device *pdev)
79 {
80         struct mmp_camera *cam;
81
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);
86                         return cam;
87                 }
88         }
89         mutex_unlock(&mmpcam_devices_lock);
90         return NULL;
91 }
92
93
94
95
96 /*
97  * Power-related registers; this almost certainly belongs
98  * somewhere else.
99  *
100  * ARMADA 610 register manual, sec 7.2.1, p1842.
101  */
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      */
105
106 static void mcam_clk_enable(struct mcam_camera *mcam)
107 {
108         unsigned int i;
109
110         for (i = 0; i < NR_MCAM_CLK; i++) {
111                 if (!IS_ERR(mcam->clk[i]))
112                         clk_prepare_enable(mcam->clk[i]);
113         }
114 }
115
116 static void mcam_clk_disable(struct mcam_camera *mcam)
117 {
118         int i;
119
120         for (i = NR_MCAM_CLK - 1; i >= 0; i--) {
121                 if (!IS_ERR(mcam->clk[i]))
122                         clk_disable_unprepare(mcam->clk[i]);
123         }
124 }
125
126 /*
127  * Power control.
128  */
129 static void mmpcam_power_up_ctlr(struct mmp_camera *cam)
130 {
131         iowrite32(0x3f, cam->power_regs + REG_CCIC_DCGCR);
132         iowrite32(0x3805b, cam->power_regs + REG_CCIC_CRCR);
133         mdelay(1);
134 }
135
136 static int mmpcam_power_up(struct mcam_camera *mcam)
137 {
138         struct mmp_camera *cam = mcam_to_cam(mcam);
139         struct mmp_camera_platform_data *pdata;
140
141 /*
142  * Turn on power and clocks to the controller.
143  */
144         mmpcam_power_up_ctlr(cam);
145         mcam_clk_enable(mcam);
146 /*
147  * Provide power to the sensor.
148  */
149         mcam_reg_write(mcam, REG_CLKCTRL, 0x60000002);
150         pdata = cam->pdev->dev.platform_data;
151         gpio_set_value(pdata->sensor_power_gpio, 1);
152         mdelay(5);
153         mcam_reg_clear_bit(mcam, REG_CTRL1, 0x10000000);
154         gpio_set_value(pdata->sensor_reset_gpio, 0); /* reset is active low */
155         mdelay(5);
156         gpio_set_value(pdata->sensor_reset_gpio, 1); /* reset is active low */
157         mdelay(5);
158
159         return 0;
160 }
161
162 static void mmpcam_power_down(struct mcam_camera *mcam)
163 {
164         struct mmp_camera *cam = mcam_to_cam(mcam);
165         struct mmp_camera_platform_data *pdata;
166 /*
167  * Turn off clocks and set reset lines
168  */
169         iowrite32(0, cam->power_regs + REG_CCIC_DCGCR);
170         iowrite32(0, cam->power_regs + REG_CCIC_CRCR);
171 /*
172  * Shut down the sensor.
173  */
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);
177
178         mcam_clk_disable(mcam);
179 }
180
181 /*
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
189  */
190 static void mmpcam_calc_dphy(struct mcam_camera *mcam)
191 {
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;
196
197         /*
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.
202          */
203         /*
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
216          *   HS transitions.
217          *   The value has been calibrated on
218          *   different boards. It seems to work well.
219          *
220          *  More detail please refer
221          *  MIPI Alliance Spectification for D-PHY
222          *  document for explanation of HS-SETTLE
223          *  and D-TERM-EN.
224          */
225         switch (pdata->dphy3_algo) {
226         case DPHY3_ALGO_PXA910:
227                 /*
228                  * Calculate CSI2_DPHY3 algo for PXA910
229                  */
230                 pdata->dphy[0] =
231                         (((1 + (pdata->lane_clk * 80) / 1000) & 0xff) << 8)
232                         | (1 + pdata->lane_clk * 35 / 1000);
233                 break;
234         case DPHY3_ALGO_PXA2128:
235                 /*
236                  * Calculate CSI2_DPHY3 algo for PXA2128
237                  */
238                 pdata->dphy[0] =
239                         (((2 + (pdata->lane_clk * 110) / 1000) & 0xff) << 8)
240                         | (1 + pdata->lane_clk * 35 / 1000);
241                 break;
242         default:
243                 /*
244                  * Use default CSI2_DPHY3 value for PXA688/PXA988
245                  */
246                 dev_dbg(dev, "camera: use the default CSI2_DPHY3 value\n");
247         }
248
249         /*
250          * mipi_clk will never be changed, it is a fixed value on MMP
251          */
252         if (IS_ERR(cam->mipi_clk))
253                 return;
254
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);
259         /*
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
264          *  HS Term Enable
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.
269          */
270         pdata->dphy[2] =
271                 ((((534 * tx_clk_esc) / 2000 - 1) & 0xff) << 8)
272                 | (((38 * tx_clk_esc) / 1000 - 1) & 0xff);
273
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]);
276 }
277
278 static irqreturn_t mmpcam_irq(int irq, void *data)
279 {
280         struct mcam_camera *mcam = data;
281         unsigned int irqs, handled;
282
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);
288 }
289
290 static void mcam_init_clk(struct mcam_camera *mcam)
291 {
292         unsigned int i;
293
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
298                          */
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",
302                                                 mcam_clks[i]);
303                 }
304         }
305 }
306
307 static int mmpcam_probe(struct platform_device *pdev)
308 {
309         struct mmp_camera *cam;
310         struct mcam_camera *mcam;
311         struct resource *res;
312         struct mmp_camera_platform_data *pdata;
313         int ret;
314
315         pdata = pdev->dev.platform_data;
316         if (!pdata)
317                 return -ENODEV;
318
319         cam = devm_kzalloc(&pdev->dev, sizeof(*cam), GFP_KERNEL);
320         if (cam == NULL)
321                 return -ENOMEM;
322         cam->pdev = pdev;
323         INIT_LIST_HEAD(&cam->devlist);
324
325         mcam = &cam->mcam;
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;
330         mcam->use_smbus = 0;
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);
339         }
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);
346         /*
347          * Get our I/O memory.
348          */
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);
354         /*
355          * Power/clock memory is elsewhere; get it too.  Perhaps this
356          * should really be managed outside of this driver?
357          */
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);
362         /*
363          * Find the i2c adapter.  This assumes, of course, that the
364          * i2c bus is already up and functioning.
365          */
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");
369                 return -ENODEV;
370         }
371         /*
372          * Sensor GPIO pins.
373          */
374         ret = devm_gpio_request(&pdev->dev, pdata->sensor_power_gpio,
375                                                         "cam-power");
376         if (ret) {
377                 dev_err(&pdev->dev, "Can't get sensor power gpio %d",
378                                 pdata->sensor_power_gpio);
379                 return ret;
380         }
381         gpio_direction_output(pdata->sensor_power_gpio, 0);
382         ret = devm_gpio_request(&pdev->dev, pdata->sensor_reset_gpio,
383                                                         "cam-reset");
384         if (ret) {
385                 dev_err(&pdev->dev, "Can't get sensor reset gpio %d",
386                                 pdata->sensor_reset_gpio);
387                 return ret;
388         }
389         gpio_direction_output(pdata->sensor_reset_gpio, 0);
390
391         mcam_init_clk(mcam);
392
393         /*
394          * Power the device up and hand it off to the core.
395          */
396         ret = mmpcam_power_up(mcam);
397         if (ret)
398                 return ret;
399         ret = mccic_register(mcam);
400         if (ret)
401                 goto out_power_down;
402         /*
403          * Finally, set up our IRQ now that the core is ready to
404          * deal with it.
405          */
406         res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
407         if (res == NULL) {
408                 ret = -ENODEV;
409                 goto out_unregister;
410         }
411         cam->irq = res->start;
412         ret = devm_request_irq(&pdev->dev, cam->irq, mmpcam_irq, IRQF_SHARED,
413                                         "mmp-camera", mcam);
414         if (ret == 0) {
415                 mmpcam_add_device(cam);
416                 return 0;
417         }
418
419 out_unregister:
420         mccic_shutdown(mcam);
421 out_power_down:
422         mmpcam_power_down(mcam);
423         return ret;
424 }
425
426
427 static int mmpcam_remove(struct mmp_camera *cam)
428 {
429         struct mcam_camera *mcam = &cam->mcam;
430
431         mmpcam_remove_device(cam);
432         mccic_shutdown(mcam);
433         mmpcam_power_down(mcam);
434         return 0;
435 }
436
437 static int mmpcam_platform_remove(struct platform_device *pdev)
438 {
439         struct mmp_camera *cam = mmpcam_find_device(pdev);
440
441         if (cam == NULL)
442                 return -ENODEV;
443         return mmpcam_remove(cam);
444 }
445
446 /*
447  * Suspend/resume support.
448  */
449 #ifdef CONFIG_PM
450
451 static int mmpcam_suspend(struct platform_device *pdev, pm_message_t state)
452 {
453         struct mmp_camera *cam = mmpcam_find_device(pdev);
454
455         if (state.event != PM_EVENT_SUSPEND)
456                 return 0;
457         mccic_suspend(&cam->mcam);
458         return 0;
459 }
460
461 static int mmpcam_resume(struct platform_device *pdev)
462 {
463         struct mmp_camera *cam = mmpcam_find_device(pdev);
464
465         /*
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.
469          */
470         mmpcam_power_up_ctlr(cam);
471         return mccic_resume(&cam->mcam);
472 }
473
474 #endif
475
476
477 static struct platform_driver mmpcam_driver = {
478         .probe          = mmpcam_probe,
479         .remove         = mmpcam_platform_remove,
480 #ifdef CONFIG_PM
481         .suspend        = mmpcam_suspend,
482         .resume         = mmpcam_resume,
483 #endif
484         .driver = {
485                 .name   = "mmp-camera",
486         }
487 };
488
489
490 static int __init mmpcam_init_module(void)
491 {
492         mutex_init(&mmpcam_devices_lock);
493         return platform_driver_register(&mmpcam_driver);
494 }
495
496 static void __exit mmpcam_exit_module(void)
497 {
498         platform_driver_unregister(&mmpcam_driver);
499         /*
500          * platform_driver_unregister() should have emptied the list
501          */
502         if (!list_empty(&mmpcam_devices))
503                 printk(KERN_ERR "mmp_camera leaving devices behind\n");
504 }
505
506 module_init(mmpcam_init_module);
507 module_exit(mmpcam_exit_module);