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