Merge remote-tracking branch 'moduleh/module.h-split'
[linux-2.6/next.git] / drivers / media / video / marvell-ccic / mmp-driver.c
blobd6b764541375ef0f0076988e6809375af2f2686f
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.
5 * Copyright 2011 Jonathan Corbet <corbet@lwn.net>
7 * This file may be distributed under the terms of the GNU General
8 * Public License, version 2.
9 */
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>
30 #include "mcam-core.h"
32 MODULE_AUTHOR("Jonathan Corbet <corbet@lwn.net>");
33 MODULE_LICENSE("GPL");
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;
43 static inline struct mmp_camera *mcam_to_cam(struct mcam_camera *mcam)
45 return container_of(mcam, struct mmp_camera, mcam);
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...
54 static LIST_HEAD(mmpcam_devices);
55 static struct mutex mmpcam_devices_lock;
57 static void mmpcam_add_device(struct mmp_camera *cam)
59 mutex_lock(&mmpcam_devices_lock);
60 list_add(&cam->devlist, &mmpcam_devices);
61 mutex_unlock(&mmpcam_devices_lock);
64 static void mmpcam_remove_device(struct mmp_camera *cam)
66 mutex_lock(&mmpcam_devices_lock);
67 list_del(&cam->devlist);
68 mutex_unlock(&mmpcam_devices_lock);
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.
76 static struct mmp_camera *mmpcam_find_device(struct platform_device *pdev)
78 struct mmp_camera *cam;
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;
87 mutex_unlock(&mmpcam_devices_lock);
88 return NULL;
95 * Power-related registers; this almost certainly belongs
96 * somewhere else.
98 * ARMADA 610 register manual, sec 7.2.1, p1842.
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 */
105 * Power control.
107 static void mmpcam_power_up(struct mcam_camera *mcam)
109 struct mmp_camera *cam = mcam_to_cam(mcam);
110 struct mmp_camera_platform_data *pdata;
112 * Turn on power and clocks to the controller.
114 iowrite32(0x3f, cam->power_regs + REG_CCIC_DCGCR);
115 iowrite32(0x3805b, cam->power_regs + REG_CCIC_CRCR);
116 mdelay(1);
118 * Provide power to the sensor.
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);
131 static void mmpcam_power_down(struct mcam_camera *mcam)
133 struct mmp_camera *cam = mcam_to_cam(mcam);
134 struct mmp_camera_platform_data *pdata;
136 * Turn off clocks and set reset lines
138 iowrite32(0, cam->power_regs + REG_CCIC_DCGCR);
139 iowrite32(0, cam->power_regs + REG_CCIC_CRCR);
141 * Shut down the sensor.
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);
149 static irqreturn_t mmpcam_irq(int irq, void *data)
151 struct mcam_camera *mcam = data;
152 unsigned int irqs, handled;
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);
162 static int mmpcam_probe(struct platform_device *pdev)
164 struct mmp_camera *cam;
165 struct mcam_camera *mcam;
166 struct resource *res;
167 struct mmp_camera_platform_data *pdata;
168 int ret;
170 cam = kzalloc(sizeof(*cam), GFP_KERNEL);
171 if (cam == NULL)
172 return -ENOMEM;
173 cam->pdev = pdev;
174 INIT_LIST_HEAD(&cam->devlist);
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);
186 * Get our I/O memory.
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;
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;
201 * Power/clock memory is elsewhere; get it too. Perhaps this
202 * should really be managed outside of this driver?
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;
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;
217 * Find the i2c adapter. This assumes, of course, that the
218 * i2c bus is already up and functioning.
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;
228 * Sensor GPIO pins.
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;
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;
243 gpio_direction_output(pdata->sensor_reset_gpio, 0);
245 * Power the device up and hand it off to the core.
247 mmpcam_power_up(mcam);
248 ret = mccic_register(mcam);
249 if (ret)
250 goto out_gpio2;
252 * Finally, set up our IRQ now that the core is ready to
253 * deal with it.
255 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
256 if (res == NULL) {
257 ret = -ENODEV;
258 goto out_unregister;
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;
268 out_unregister:
269 mccic_shutdown(mcam);
270 out_gpio2:
271 mmpcam_power_down(mcam);
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;
285 static int mmpcam_remove(struct mmp_camera *cam)
287 struct mcam_camera *mcam = &cam->mcam;
288 struct mmp_camera_platform_data *pdata;
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;
303 static int mmpcam_platform_remove(struct platform_device *pdev)
305 struct mmp_camera *cam = mmpcam_find_device(pdev);
307 if (cam == NULL)
308 return -ENODEV;
309 return mmpcam_remove(cam);
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
323 static int __init mmpcam_init_module(void)
325 mutex_init(&mmpcam_devices_lock);
326 return platform_driver_register(&mmpcam_driver);
329 static void __exit mmpcam_exit_module(void)
331 platform_driver_unregister(&mmpcam_driver);
333 * platform_driver_unregister() should have emptied the list
335 if (!list_empty(&mmpcam_devices))
336 printk(KERN_ERR "mmp_camera leaving devices behind\n");
339 module_init(mmpcam_init_module);
340 module_exit(mmpcam_exit_module);