]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/media/platform/marvell-ccic/mmp-driver.c
Merge tag 'scsi-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi
[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/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>
23 #include <linux/io.h>
24 #include <linux/delay.h>
25 #include <linux/list.h>
26 #include <linux/pm.h>
27 #include <linux/clk.h>
28
29 #include "mcam-core.h"
30
31 MODULE_ALIAS("platform:mmp-camera");
32 MODULE_AUTHOR("Jonathan Corbet <corbet@lwn.net>");
33 MODULE_LICENSE("GPL");
34
35 static char *mcam_clks[] = {"CCICAXICLK", "CCICFUNCLK", "CCICPHYCLK"};
36
37 struct mmp_camera {
38         void __iomem *power_regs;
39         struct platform_device *pdev;
40         struct mcam_camera mcam;
41         struct list_head devlist;
42         struct clk *mipi_clk;
43         int irq;
44 };
45
46 static inline struct mmp_camera *mcam_to_cam(struct mcam_camera *mcam)
47 {
48         return container_of(mcam, struct mmp_camera, mcam);
49 }
50
51 /*
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...
55  */
56
57 static LIST_HEAD(mmpcam_devices);
58 static struct mutex mmpcam_devices_lock;
59
60 static void mmpcam_add_device(struct mmp_camera *cam)
61 {
62         mutex_lock(&mmpcam_devices_lock);
63         list_add(&cam->devlist, &mmpcam_devices);
64         mutex_unlock(&mmpcam_devices_lock);
65 }
66
67 static void mmpcam_remove_device(struct mmp_camera *cam)
68 {
69         mutex_lock(&mmpcam_devices_lock);
70         list_del(&cam->devlist);
71         mutex_unlock(&mmpcam_devices_lock);
72 }
73
74 /*
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.
78  */
79 static struct mmp_camera *mmpcam_find_device(struct platform_device *pdev)
80 {
81         struct mmp_camera *cam;
82
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);
87                         return cam;
88                 }
89         }
90         mutex_unlock(&mmpcam_devices_lock);
91         return NULL;
92 }
93
94
95
96
97 /*
98  * Power-related registers; this almost certainly belongs
99  * somewhere else.
100  *
101  * ARMADA 610 register manual, sec 7.2.1, p1842.
102  */
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     */
107
108 static void mcam_clk_enable(struct mcam_camera *mcam)
109 {
110         unsigned int i;
111
112         for (i = 0; i < NR_MCAM_CLK; i++) {
113                 if (!IS_ERR(mcam->clk[i]))
114                         clk_prepare_enable(mcam->clk[i]);
115         }
116 }
117
118 static void mcam_clk_disable(struct mcam_camera *mcam)
119 {
120         int i;
121
122         for (i = NR_MCAM_CLK - 1; i >= 0; i--) {
123                 if (!IS_ERR(mcam->clk[i]))
124                         clk_disable_unprepare(mcam->clk[i]);
125         }
126 }
127
128 /*
129  * Power control.
130  */
131 static void mmpcam_power_up_ctlr(struct mmp_camera *cam)
132 {
133         iowrite32(0x3f, cam->power_regs + REG_CCIC_DCGCR);
134         iowrite32(0x3805b, cam->power_regs + REG_CCIC_CRCR);
135         mdelay(1);
136 }
137
138 static int mmpcam_power_up(struct mcam_camera *mcam)
139 {
140         struct mmp_camera *cam = mcam_to_cam(mcam);
141         struct mmp_camera_platform_data *pdata;
142
143 /*
144  * Turn on power and clocks to the controller.
145  */
146         mmpcam_power_up_ctlr(cam);
147 /*
148  * Provide power to the sensor.
149  */
150         mcam_reg_write(mcam, REG_CLKCTRL, 0x60000002);
151         pdata = cam->pdev->dev.platform_data;
152         gpio_set_value(pdata->sensor_power_gpio, 1);
153         mdelay(5);
154         mcam_reg_clear_bit(mcam, REG_CTRL1, 0x10000000);
155         gpio_set_value(pdata->sensor_reset_gpio, 0); /* reset is active low */
156         mdelay(5);
157         gpio_set_value(pdata->sensor_reset_gpio, 1); /* reset is active low */
158         mdelay(5);
159
160         mcam_clk_enable(mcam);
161
162         return 0;
163 }
164
165 static void mmpcam_power_down(struct mcam_camera *mcam)
166 {
167         struct mmp_camera *cam = mcam_to_cam(mcam);
168         struct mmp_camera_platform_data *pdata;
169 /*
170  * Turn off clocks and set reset lines
171  */
172         iowrite32(0, cam->power_regs + REG_CCIC_DCGCR);
173         iowrite32(0, cam->power_regs + REG_CCIC_CRCR);
174 /*
175  * Shut down the sensor.
176  */
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);
180
181         mcam_clk_disable(mcam);
182 }
183
184 static void mcam_ctlr_reset(struct mcam_camera *mcam)
185 {
186         unsigned long val;
187         struct mmp_camera *cam = mcam_to_cam(mcam);
188
189         if (mcam->ccic_id) {
190                 /*
191                  * Using CCIC2
192                  */
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);
196         } else {
197                 /*
198                  * Using CCIC1
199                  */
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);
203         }
204 }
205
206 /*
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
214  */
215 static void mmpcam_calc_dphy(struct mcam_camera *mcam)
216 {
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;
221
222         /*
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.
227          */
228         /*
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
241          *   HS transitions.
242          *   The value has been calibrated on
243          *   different boards. It seems to work well.
244          *
245          *  More detail please refer
246          *  MIPI Alliance Spectification for D-PHY
247          *  document for explanation of HS-SETTLE
248          *  and D-TERM-EN.
249          */
250         switch (pdata->dphy3_algo) {
251         case DPHY3_ALGO_PXA910:
252                 /*
253                  * Calculate CSI2_DPHY3 algo for PXA910
254                  */
255                 pdata->dphy[0] =
256                         (((1 + (pdata->lane_clk * 80) / 1000) & 0xff) << 8)
257                         | (1 + pdata->lane_clk * 35 / 1000);
258                 break;
259         case DPHY3_ALGO_PXA2128:
260                 /*
261                  * Calculate CSI2_DPHY3 algo for PXA2128
262                  */
263                 pdata->dphy[0] =
264                         (((2 + (pdata->lane_clk * 110) / 1000) & 0xff) << 8)
265                         | (1 + pdata->lane_clk * 35 / 1000);
266                 break;
267         default:
268                 /*
269                  * Use default CSI2_DPHY3 value for PXA688/PXA988
270                  */
271                 dev_dbg(dev, "camera: use the default CSI2_DPHY3 value\n");
272         }
273
274         /*
275          * mipi_clk will never be changed, it is a fixed value on MMP
276          */
277         if (IS_ERR(cam->mipi_clk))
278                 return;
279
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);
284         /*
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
289          *  HS Term Enable
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.
294          */
295         pdata->dphy[2] =
296                 ((((534 * tx_clk_esc) / 2000 - 1) & 0xff) << 8)
297                 | (((38 * tx_clk_esc) / 1000 - 1) & 0xff);
298
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]);
301 }
302
303 static irqreturn_t mmpcam_irq(int irq, void *data)
304 {
305         struct mcam_camera *mcam = data;
306         unsigned int irqs, handled;
307
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);
313 }
314
315 static void mcam_init_clk(struct mcam_camera *mcam)
316 {
317         unsigned int i;
318
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
323                          */
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",
327                                                 mcam_clks[i]);
328                 }
329         }
330 }
331
332 static int mmpcam_probe(struct platform_device *pdev)
333 {
334         struct mmp_camera *cam;
335         struct mcam_camera *mcam;
336         struct resource *res;
337         struct mmp_camera_platform_data *pdata;
338         int ret;
339
340         pdata = pdev->dev.platform_data;
341         if (!pdata)
342                 return -ENODEV;
343
344         cam = devm_kzalloc(&pdev->dev, sizeof(*cam), GFP_KERNEL);
345         if (cam == NULL)
346                 return -ENOMEM;
347         cam->pdev = pdev;
348         INIT_LIST_HEAD(&cam->devlist);
349
350         mcam = &cam->mcam;
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;
356         mcam->use_smbus = 0;
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);
367         }
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);
374         /*
375          * Get our I/O memory.
376          */
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);
382         /*
383          * Power/clock memory is elsewhere; get it too.  Perhaps this
384          * should really be managed outside of this driver?
385          */
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);
390         /*
391          * Find the i2c adapter.  This assumes, of course, that the
392          * i2c bus is already up and functioning.
393          */
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");
397                 return -ENODEV;
398         }
399         /*
400          * Sensor GPIO pins.
401          */
402         ret = devm_gpio_request(&pdev->dev, pdata->sensor_power_gpio,
403                                                         "cam-power");
404         if (ret) {
405                 dev_err(&pdev->dev, "Can't get sensor power gpio %d",
406                                 pdata->sensor_power_gpio);
407                 return ret;
408         }
409         gpio_direction_output(pdata->sensor_power_gpio, 0);
410         ret = devm_gpio_request(&pdev->dev, pdata->sensor_reset_gpio,
411                                                         "cam-reset");
412         if (ret) {
413                 dev_err(&pdev->dev, "Can't get sensor reset gpio %d",
414                                 pdata->sensor_reset_gpio);
415                 return ret;
416         }
417         gpio_direction_output(pdata->sensor_reset_gpio, 0);
418
419         mcam_init_clk(mcam);
420
421         /*
422          * Power the device up and hand it off to the core.
423          */
424         ret = mmpcam_power_up(mcam);
425         if (ret)
426                 return ret;
427         ret = mccic_register(mcam);
428         if (ret)
429                 goto out_power_down;
430         /*
431          * Finally, set up our IRQ now that the core is ready to
432          * deal with it.
433          */
434         res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
435         if (res == NULL) {
436                 ret = -ENODEV;
437                 goto out_unregister;
438         }
439         cam->irq = res->start;
440         ret = devm_request_irq(&pdev->dev, cam->irq, mmpcam_irq, IRQF_SHARED,
441                                         "mmp-camera", mcam);
442         if (ret == 0) {
443                 mmpcam_add_device(cam);
444                 return 0;
445         }
446
447 out_unregister:
448         mccic_shutdown(mcam);
449 out_power_down:
450         mmpcam_power_down(mcam);
451         return ret;
452 }
453
454
455 static int mmpcam_remove(struct mmp_camera *cam)
456 {
457         struct mcam_camera *mcam = &cam->mcam;
458
459         mmpcam_remove_device(cam);
460         mccic_shutdown(mcam);
461         mmpcam_power_down(mcam);
462         return 0;
463 }
464
465 static int mmpcam_platform_remove(struct platform_device *pdev)
466 {
467         struct mmp_camera *cam = mmpcam_find_device(pdev);
468
469         if (cam == NULL)
470                 return -ENODEV;
471         return mmpcam_remove(cam);
472 }
473
474 /*
475  * Suspend/resume support.
476  */
477 #ifdef CONFIG_PM
478
479 static int mmpcam_suspend(struct platform_device *pdev, pm_message_t state)
480 {
481         struct mmp_camera *cam = mmpcam_find_device(pdev);
482
483         if (state.event != PM_EVENT_SUSPEND)
484                 return 0;
485         mccic_suspend(&cam->mcam);
486         return 0;
487 }
488
489 static int mmpcam_resume(struct platform_device *pdev)
490 {
491         struct mmp_camera *cam = mmpcam_find_device(pdev);
492
493         /*
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.
497          */
498         mmpcam_power_up_ctlr(cam);
499         return mccic_resume(&cam->mcam);
500 }
501
502 #endif
503
504
505 static struct platform_driver mmpcam_driver = {
506         .probe          = mmpcam_probe,
507         .remove         = mmpcam_platform_remove,
508 #ifdef CONFIG_PM
509         .suspend        = mmpcam_suspend,
510         .resume         = mmpcam_resume,
511 #endif
512         .driver = {
513                 .name   = "mmp-camera",
514         }
515 };
516
517
518 static int __init mmpcam_init_module(void)
519 {
520         mutex_init(&mmpcam_devices_lock);
521         return platform_driver_register(&mmpcam_driver);
522 }
523
524 static void __exit mmpcam_exit_module(void)
525 {
526         platform_driver_unregister(&mmpcam_driver);
527         /*
528          * platform_driver_unregister() should have emptied the list
529          */
530         if (!list_empty(&mmpcam_devices))
531                 printk(KERN_ERR "mmp_camera leaving devices behind\n");
532 }
533
534 module_init(mmpcam_init_module);
535 module_exit(mmpcam_exit_module);