Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs
[pandora-kernel.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
30 #include "mcam-core.h"
31
32 MODULE_ALIAS("platform:mmp-camera");
33 MODULE_AUTHOR("Jonathan Corbet <corbet@lwn.net>");
34 MODULE_LICENSE("GPL");
35
36 struct mmp_camera {
37         void *power_regs;
38         struct platform_device *pdev;
39         struct mcam_camera mcam;
40         struct list_head devlist;
41         int irq;
42 };
43
44 static inline struct mmp_camera *mcam_to_cam(struct mcam_camera *mcam)
45 {
46         return container_of(mcam, struct mmp_camera, mcam);
47 }
48
49 /*
50  * A silly little infrastructure so we can keep track of our devices.
51  * Chances are that we will never have more than one of them, but
52  * the Armada 610 *does* have two controllers...
53  */
54
55 static LIST_HEAD(mmpcam_devices);
56 static struct mutex mmpcam_devices_lock;
57
58 static void mmpcam_add_device(struct mmp_camera *cam)
59 {
60         mutex_lock(&mmpcam_devices_lock);
61         list_add(&cam->devlist, &mmpcam_devices);
62         mutex_unlock(&mmpcam_devices_lock);
63 }
64
65 static void mmpcam_remove_device(struct mmp_camera *cam)
66 {
67         mutex_lock(&mmpcam_devices_lock);
68         list_del(&cam->devlist);
69         mutex_unlock(&mmpcam_devices_lock);
70 }
71
72 /*
73  * Platform dev remove passes us a platform_device, and there's
74  * no handy unused drvdata to stash a backpointer in.  So just
75  * dig it out of our list.
76  */
77 static struct mmp_camera *mmpcam_find_device(struct platform_device *pdev)
78 {
79         struct mmp_camera *cam;
80
81         mutex_lock(&mmpcam_devices_lock);
82         list_for_each_entry(cam, &mmpcam_devices, devlist) {
83                 if (cam->pdev == pdev) {
84                         mutex_unlock(&mmpcam_devices_lock);
85                         return cam;
86                 }
87         }
88         mutex_unlock(&mmpcam_devices_lock);
89         return NULL;
90 }
91
92
93
94
95 /*
96  * Power-related registers; this almost certainly belongs
97  * somewhere else.
98  *
99  * ARMADA 610 register manual, sec 7.2.1, p1842.
100  */
101 #define CPU_SUBSYS_PMU_BASE     0xd4282800
102 #define REG_CCIC_DCGCR          0x28    /* CCIC dyn clock gate ctrl reg */
103 #define REG_CCIC_CRCR           0x50    /* CCIC clk reset ctrl reg      */
104
105 /*
106  * Power control.
107  */
108 static void mmpcam_power_up(struct mcam_camera *mcam)
109 {
110         struct mmp_camera *cam = mcam_to_cam(mcam);
111         struct mmp_camera_platform_data *pdata;
112 /*
113  * Turn on power and clocks to the controller.
114  */
115         iowrite32(0x3f, cam->power_regs + REG_CCIC_DCGCR);
116         iowrite32(0x3805b, cam->power_regs + REG_CCIC_CRCR);
117         mdelay(1);
118 /*
119  * Provide power to the sensor.
120  */
121         mcam_reg_write(mcam, REG_CLKCTRL, 0x60000002);
122         pdata = cam->pdev->dev.platform_data;
123         gpio_set_value(pdata->sensor_power_gpio, 1);
124         mdelay(5);
125         mcam_reg_clear_bit(mcam, REG_CTRL1, 0x10000000);
126         gpio_set_value(pdata->sensor_reset_gpio, 0); /* reset is active low */
127         mdelay(5);
128         gpio_set_value(pdata->sensor_reset_gpio, 1); /* reset is active low */
129         mdelay(5);
130 }
131
132 static void mmpcam_power_down(struct mcam_camera *mcam)
133 {
134         struct mmp_camera *cam = mcam_to_cam(mcam);
135         struct mmp_camera_platform_data *pdata;
136 /*
137  * Turn off clocks and set reset lines
138  */
139         iowrite32(0, cam->power_regs + REG_CCIC_DCGCR);
140         iowrite32(0, cam->power_regs + REG_CCIC_CRCR);
141 /*
142  * Shut down the sensor.
143  */
144         pdata = cam->pdev->dev.platform_data;
145         gpio_set_value(pdata->sensor_power_gpio, 0);
146         gpio_set_value(pdata->sensor_reset_gpio, 0);
147 }
148
149
150 static irqreturn_t mmpcam_irq(int irq, void *data)
151 {
152         struct mcam_camera *mcam = data;
153         unsigned int irqs, handled;
154
155         spin_lock(&mcam->dev_lock);
156         irqs = mcam_reg_read(mcam, REG_IRQSTAT);
157         handled = mccic_irq(mcam, irqs);
158         spin_unlock(&mcam->dev_lock);
159         return IRQ_RETVAL(handled);
160 }
161
162
163 static int mmpcam_probe(struct platform_device *pdev)
164 {
165         struct mmp_camera *cam;
166         struct mcam_camera *mcam;
167         struct resource *res;
168         struct mmp_camera_platform_data *pdata;
169         int ret;
170
171         cam = kzalloc(sizeof(*cam), GFP_KERNEL);
172         if (cam == NULL)
173                 return -ENOMEM;
174         cam->pdev = pdev;
175         INIT_LIST_HEAD(&cam->devlist);
176
177         mcam = &cam->mcam;
178         mcam->platform = MHP_Armada610;
179         mcam->plat_power_up = mmpcam_power_up;
180         mcam->plat_power_down = mmpcam_power_down;
181         mcam->dev = &pdev->dev;
182         mcam->use_smbus = 0;
183         mcam->chip_id = V4L2_IDENT_ARMADA610;
184         mcam->buffer_mode = B_DMA_sg;
185         spin_lock_init(&mcam->dev_lock);
186         /*
187          * Get our I/O memory.
188          */
189         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
190         if (res == NULL) {
191                 dev_err(&pdev->dev, "no iomem resource!\n");
192                 ret = -ENODEV;
193                 goto out_free;
194         }
195         mcam->regs = ioremap(res->start, resource_size(res));
196         if (mcam->regs == NULL) {
197                 dev_err(&pdev->dev, "MMIO ioremap fail\n");
198                 ret = -ENODEV;
199                 goto out_free;
200         }
201         /*
202          * Power/clock memory is elsewhere; get it too.  Perhaps this
203          * should really be managed outside of this driver?
204          */
205         res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
206         if (res == NULL) {
207                 dev_err(&pdev->dev, "no power resource!\n");
208                 ret = -ENODEV;
209                 goto out_unmap1;
210         }
211         cam->power_regs = ioremap(res->start, resource_size(res));
212         if (cam->power_regs == NULL) {
213                 dev_err(&pdev->dev, "power MMIO ioremap fail\n");
214                 ret = -ENODEV;
215                 goto out_unmap1;
216         }
217         /*
218          * Find the i2c adapter.  This assumes, of course, that the
219          * i2c bus is already up and functioning.
220          */
221         pdata = pdev->dev.platform_data;
222         mcam->i2c_adapter = platform_get_drvdata(pdata->i2c_device);
223         if (mcam->i2c_adapter == NULL) {
224                 ret = -ENODEV;
225                 dev_err(&pdev->dev, "No i2c adapter\n");
226                 goto out_unmap2;
227         }
228         /*
229          * Sensor GPIO pins.
230          */
231         ret = gpio_request(pdata->sensor_power_gpio, "cam-power");
232         if (ret) {
233                 dev_err(&pdev->dev, "Can't get sensor power gpio %d",
234                                 pdata->sensor_power_gpio);
235                 goto out_unmap2;
236         }
237         gpio_direction_output(pdata->sensor_power_gpio, 0);
238         ret = gpio_request(pdata->sensor_reset_gpio, "cam-reset");
239         if (ret) {
240                 dev_err(&pdev->dev, "Can't get sensor reset gpio %d",
241                                 pdata->sensor_reset_gpio);
242                 goto out_gpio;
243         }
244         gpio_direction_output(pdata->sensor_reset_gpio, 0);
245         /*
246          * Power the device up and hand it off to the core.
247          */
248         mmpcam_power_up(mcam);
249         ret = mccic_register(mcam);
250         if (ret)
251                 goto out_gpio2;
252         /*
253          * Finally, set up our IRQ now that the core is ready to
254          * deal with it.
255          */
256         res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
257         if (res == NULL) {
258                 ret = -ENODEV;
259                 goto out_unregister;
260         }
261         cam->irq = res->start;
262         ret = request_irq(cam->irq, mmpcam_irq, IRQF_SHARED,
263                         "mmp-camera", mcam);
264         if (ret == 0) {
265                 mmpcam_add_device(cam);
266                 return 0;
267         }
268
269 out_unregister:
270         mccic_shutdown(mcam);
271 out_gpio2:
272         mmpcam_power_down(mcam);
273         gpio_free(pdata->sensor_reset_gpio);
274 out_gpio:
275         gpio_free(pdata->sensor_power_gpio);
276 out_unmap2:
277         iounmap(cam->power_regs);
278 out_unmap1:
279         iounmap(mcam->regs);
280 out_free:
281         kfree(cam);
282         return ret;
283 }
284
285
286 static int mmpcam_remove(struct mmp_camera *cam)
287 {
288         struct mcam_camera *mcam = &cam->mcam;
289         struct mmp_camera_platform_data *pdata;
290
291         mmpcam_remove_device(cam);
292         free_irq(cam->irq, mcam);
293         mccic_shutdown(mcam);
294         mmpcam_power_down(mcam);
295         pdata = cam->pdev->dev.platform_data;
296         gpio_free(pdata->sensor_reset_gpio);
297         gpio_free(pdata->sensor_power_gpio);
298         iounmap(cam->power_regs);
299         iounmap(mcam->regs);
300         kfree(cam);
301         return 0;
302 }
303
304 static int mmpcam_platform_remove(struct platform_device *pdev)
305 {
306         struct mmp_camera *cam = mmpcam_find_device(pdev);
307
308         if (cam == NULL)
309                 return -ENODEV;
310         return mmpcam_remove(cam);
311 }
312
313
314 static struct platform_driver mmpcam_driver = {
315         .probe          = mmpcam_probe,
316         .remove         = mmpcam_platform_remove,
317         .driver = {
318                 .name   = "mmp-camera",
319                 .owner  = THIS_MODULE
320         }
321 };
322
323
324 static int __init mmpcam_init_module(void)
325 {
326         mutex_init(&mmpcam_devices_lock);
327         return platform_driver_register(&mmpcam_driver);
328 }
329
330 static void __exit mmpcam_exit_module(void)
331 {
332         platform_driver_unregister(&mmpcam_driver);
333         /*
334          * platform_driver_unregister() should have emptied the list
335          */
336         if (!list_empty(&mmpcam_devices))
337                 printk(KERN_ERR "mmp_camera leaving devices behind\n");
338 }
339
340 module_init(mmpcam_init_module);
341 module_exit(mmpcam_exit_module);