Merge branch 'scripts'
[linux-flexiantxendom0-3.2.10.git] / drivers / media / video / marvell-ccic / mmp-driver.c
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.
4  *
5  * Copyright 2011 Jonathan Corbet <corbet@lwn.net>
6  *
7  * This file may be distributed under the terms of the GNU General
8  * Public License, version 2.
9  */
10
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/v4l2-chip-ident.h>
22 #include <media/mmp-camera.h>
23 #include <linux/device.h>
24 #include <linux/platform_device.h>
25 #include <linux/gpio.h>
26 #include <linux/io.h>
27 #include <linux/delay.h>
28 #include <linux/list.h>
29 #include <linux/pm.h>
30
31 #include "mcam-core.h"
32
33 MODULE_ALIAS("platform:mmp-camera");
34 MODULE_AUTHOR("Jonathan Corbet <corbet@lwn.net>");
35 MODULE_LICENSE("GPL");
36
37 struct mmp_camera {
38         void *power_regs;
39         struct platform_device *pdev;
40         struct mcam_camera mcam;
41         struct list_head devlist;
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 /*
107  * Power control.
108  */
109 static void mmpcam_power_up_ctlr(struct mmp_camera *cam)
110 {
111         iowrite32(0x3f, cam->power_regs + REG_CCIC_DCGCR);
112         iowrite32(0x3805b, cam->power_regs + REG_CCIC_CRCR);
113         mdelay(1);
114 }
115
116 static void mmpcam_power_up(struct mcam_camera *mcam)
117 {
118         struct mmp_camera *cam = mcam_to_cam(mcam);
119         struct mmp_camera_platform_data *pdata;
120 /*
121  * Turn on power and clocks to the controller.
122  */
123         mmpcam_power_up_ctlr(cam);
124 /*
125  * Provide power to the sensor.
126  */
127         mcam_reg_write(mcam, REG_CLKCTRL, 0x60000002);
128         pdata = cam->pdev->dev.platform_data;
129         gpio_set_value(pdata->sensor_power_gpio, 1);
130         mdelay(5);
131         mcam_reg_clear_bit(mcam, REG_CTRL1, 0x10000000);
132         gpio_set_value(pdata->sensor_reset_gpio, 0); /* reset is active low */
133         mdelay(5);
134         gpio_set_value(pdata->sensor_reset_gpio, 1); /* reset is active low */
135         mdelay(5);
136 }
137
138 static void mmpcam_power_down(struct mcam_camera *mcam)
139 {
140         struct mmp_camera *cam = mcam_to_cam(mcam);
141         struct mmp_camera_platform_data *pdata;
142 /*
143  * Turn off clocks and set reset lines
144  */
145         iowrite32(0, cam->power_regs + REG_CCIC_DCGCR);
146         iowrite32(0, cam->power_regs + REG_CCIC_CRCR);
147 /*
148  * Shut down the sensor.
149  */
150         pdata = cam->pdev->dev.platform_data;
151         gpio_set_value(pdata->sensor_power_gpio, 0);
152         gpio_set_value(pdata->sensor_reset_gpio, 0);
153 }
154
155
156 static irqreturn_t mmpcam_irq(int irq, void *data)
157 {
158         struct mcam_camera *mcam = data;
159         unsigned int irqs, handled;
160
161         spin_lock(&mcam->dev_lock);
162         irqs = mcam_reg_read(mcam, REG_IRQSTAT);
163         handled = mccic_irq(mcam, irqs);
164         spin_unlock(&mcam->dev_lock);
165         return IRQ_RETVAL(handled);
166 }
167
168
169 static int mmpcam_probe(struct platform_device *pdev)
170 {
171         struct mmp_camera *cam;
172         struct mcam_camera *mcam;
173         struct resource *res;
174         struct mmp_camera_platform_data *pdata;
175         int ret;
176
177         cam = kzalloc(sizeof(*cam), GFP_KERNEL);
178         if (cam == NULL)
179                 return -ENOMEM;
180         cam->pdev = pdev;
181         INIT_LIST_HEAD(&cam->devlist);
182
183         mcam = &cam->mcam;
184         mcam->platform = MHP_Armada610;
185         mcam->plat_power_up = mmpcam_power_up;
186         mcam->plat_power_down = mmpcam_power_down;
187         mcam->dev = &pdev->dev;
188         mcam->use_smbus = 0;
189         mcam->chip_id = V4L2_IDENT_ARMADA610;
190         mcam->buffer_mode = B_DMA_sg;
191         spin_lock_init(&mcam->dev_lock);
192         /*
193          * Get our I/O memory.
194          */
195         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
196         if (res == NULL) {
197                 dev_err(&pdev->dev, "no iomem resource!\n");
198                 ret = -ENODEV;
199                 goto out_free;
200         }
201         mcam->regs = ioremap(res->start, resource_size(res));
202         if (mcam->regs == NULL) {
203                 dev_err(&pdev->dev, "MMIO ioremap fail\n");
204                 ret = -ENODEV;
205                 goto out_free;
206         }
207         /*
208          * Power/clock memory is elsewhere; get it too.  Perhaps this
209          * should really be managed outside of this driver?
210          */
211         res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
212         if (res == NULL) {
213                 dev_err(&pdev->dev, "no power resource!\n");
214                 ret = -ENODEV;
215                 goto out_unmap1;
216         }
217         cam->power_regs = ioremap(res->start, resource_size(res));
218         if (cam->power_regs == NULL) {
219                 dev_err(&pdev->dev, "power MMIO ioremap fail\n");
220                 ret = -ENODEV;
221                 goto out_unmap1;
222         }
223         /*
224          * Find the i2c adapter.  This assumes, of course, that the
225          * i2c bus is already up and functioning.
226          */
227         pdata = pdev->dev.platform_data;
228         mcam->i2c_adapter = platform_get_drvdata(pdata->i2c_device);
229         if (mcam->i2c_adapter == NULL) {
230                 ret = -ENODEV;
231                 dev_err(&pdev->dev, "No i2c adapter\n");
232                 goto out_unmap2;
233         }
234         /*
235          * Sensor GPIO pins.
236          */
237         ret = gpio_request(pdata->sensor_power_gpio, "cam-power");
238         if (ret) {
239                 dev_err(&pdev->dev, "Can't get sensor power gpio %d",
240                                 pdata->sensor_power_gpio);
241                 goto out_unmap2;
242         }
243         gpio_direction_output(pdata->sensor_power_gpio, 0);
244         ret = gpio_request(pdata->sensor_reset_gpio, "cam-reset");
245         if (ret) {
246                 dev_err(&pdev->dev, "Can't get sensor reset gpio %d",
247                                 pdata->sensor_reset_gpio);
248                 goto out_gpio;
249         }
250         gpio_direction_output(pdata->sensor_reset_gpio, 0);
251         /*
252          * Power the device up and hand it off to the core.
253          */
254         mmpcam_power_up(mcam);
255         ret = mccic_register(mcam);
256         if (ret)
257                 goto out_gpio2;
258         /*
259          * Finally, set up our IRQ now that the core is ready to
260          * deal with it.
261          */
262         res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
263         if (res == NULL) {
264                 ret = -ENODEV;
265                 goto out_unregister;
266         }
267         cam->irq = res->start;
268         ret = request_irq(cam->irq, mmpcam_irq, IRQF_SHARED,
269                         "mmp-camera", mcam);
270         if (ret == 0) {
271                 mmpcam_add_device(cam);
272                 return 0;
273         }
274
275 out_unregister:
276         mccic_shutdown(mcam);
277 out_gpio2:
278         mmpcam_power_down(mcam);
279         gpio_free(pdata->sensor_reset_gpio);
280 out_gpio:
281         gpio_free(pdata->sensor_power_gpio);
282 out_unmap2:
283         iounmap(cam->power_regs);
284 out_unmap1:
285         iounmap(mcam->regs);
286 out_free:
287         kfree(cam);
288         return ret;
289 }
290
291
292 static int mmpcam_remove(struct mmp_camera *cam)
293 {
294         struct mcam_camera *mcam = &cam->mcam;
295         struct mmp_camera_platform_data *pdata;
296
297         mmpcam_remove_device(cam);
298         free_irq(cam->irq, mcam);
299         mccic_shutdown(mcam);
300         mmpcam_power_down(mcam);
301         pdata = cam->pdev->dev.platform_data;
302         gpio_free(pdata->sensor_reset_gpio);
303         gpio_free(pdata->sensor_power_gpio);
304         iounmap(cam->power_regs);
305         iounmap(mcam->regs);
306         kfree(cam);
307         return 0;
308 }
309
310 static int mmpcam_platform_remove(struct platform_device *pdev)
311 {
312         struct mmp_camera *cam = mmpcam_find_device(pdev);
313
314         if (cam == NULL)
315                 return -ENODEV;
316         return mmpcam_remove(cam);
317 }
318
319 /*
320  * Suspend/resume support.
321  */
322 #ifdef CONFIG_PM
323
324 static int mmpcam_suspend(struct platform_device *pdev, pm_message_t state)
325 {
326         struct mmp_camera *cam = mmpcam_find_device(pdev);
327
328         if (state.event != PM_EVENT_SUSPEND)
329                 return 0;
330         mccic_suspend(&cam->mcam);
331         return 0;
332 }
333
334 static int mmpcam_resume(struct platform_device *pdev)
335 {
336         struct mmp_camera *cam = mmpcam_find_device(pdev);
337
338         /*
339          * Power up unconditionally just in case the core tries to
340          * touch a register even if nothing was active before; trust
341          * me, it's better this way.
342          */
343         mmpcam_power_up_ctlr(cam);
344         return mccic_resume(&cam->mcam);
345 }
346
347 #endif
348
349
350 static struct platform_driver mmpcam_driver = {
351         .probe          = mmpcam_probe,
352         .remove         = mmpcam_platform_remove,
353 #ifdef CONFIG_PM
354         .suspend        = mmpcam_suspend,
355         .resume         = mmpcam_resume,
356 #endif
357         .driver = {
358                 .name   = "mmp-camera",
359                 .owner  = THIS_MODULE
360         }
361 };
362
363
364 static int __init mmpcam_init_module(void)
365 {
366         mutex_init(&mmpcam_devices_lock);
367         return platform_driver_register(&mmpcam_driver);
368 }
369
370 static void __exit mmpcam_exit_module(void)
371 {
372         platform_driver_unregister(&mmpcam_driver);
373         /*
374          * platform_driver_unregister() should have emptied the list
375          */
376         if (!list_empty(&mmpcam_devices))
377                 printk(KERN_ERR "mmp_camera leaving devices behind\n");
378 }
379
380 module_init(mmpcam_init_module);
381 module_exit(mmpcam_exit_module);