ASoC: tlv312aic23: unbreak resume
[zen-stable.git] / drivers / media / video / marvell-ccic / mmp-driver.c
blob0d64e2d7474a52eebc06bf1dd84314ba7d32d288
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>
29 #include <linux/pm.h>
31 #include "mcam-core.h"
33 MODULE_ALIAS("platform:mmp-camera");
34 MODULE_AUTHOR("Jonathan Corbet <corbet@lwn.net>");
35 MODULE_LICENSE("GPL");
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;
45 static inline struct mmp_camera *mcam_to_cam(struct mcam_camera *mcam)
47 return container_of(mcam, struct mmp_camera, mcam);
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...
56 static LIST_HEAD(mmpcam_devices);
57 static struct mutex mmpcam_devices_lock;
59 static void mmpcam_add_device(struct mmp_camera *cam)
61 mutex_lock(&mmpcam_devices_lock);
62 list_add(&cam->devlist, &mmpcam_devices);
63 mutex_unlock(&mmpcam_devices_lock);
66 static void mmpcam_remove_device(struct mmp_camera *cam)
68 mutex_lock(&mmpcam_devices_lock);
69 list_del(&cam->devlist);
70 mutex_unlock(&mmpcam_devices_lock);
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.
78 static struct mmp_camera *mmpcam_find_device(struct platform_device *pdev)
80 struct mmp_camera *cam;
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;
89 mutex_unlock(&mmpcam_devices_lock);
90 return NULL;
97 * Power-related registers; this almost certainly belongs
98 * somewhere else.
100 * ARMADA 610 register manual, sec 7.2.1, p1842.
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 */
107 * Power control.
109 static void mmpcam_power_up(struct mcam_camera *mcam)
111 struct mmp_camera *cam = mcam_to_cam(mcam);
112 struct mmp_camera_platform_data *pdata;
114 * Turn on power and clocks to the controller.
116 iowrite32(0x3f, cam->power_regs + REG_CCIC_DCGCR);
117 iowrite32(0x3805b, cam->power_regs + REG_CCIC_CRCR);
118 mdelay(1);
120 * Provide power to the sensor.
122 mcam_reg_write(mcam, REG_CLKCTRL, 0x60000002);
123 pdata = cam->pdev->dev.platform_data;
124 gpio_set_value(pdata->sensor_power_gpio, 1);
125 mdelay(5);
126 mcam_reg_clear_bit(mcam, REG_CTRL1, 0x10000000);
127 gpio_set_value(pdata->sensor_reset_gpio, 0); /* reset is active low */
128 mdelay(5);
129 gpio_set_value(pdata->sensor_reset_gpio, 1); /* reset is active low */
130 mdelay(5);
133 static void mmpcam_power_down(struct mcam_camera *mcam)
135 struct mmp_camera *cam = mcam_to_cam(mcam);
136 struct mmp_camera_platform_data *pdata;
138 * Turn off clocks and set reset lines
140 iowrite32(0, cam->power_regs + REG_CCIC_DCGCR);
141 iowrite32(0, cam->power_regs + REG_CCIC_CRCR);
143 * Shut down the sensor.
145 pdata = cam->pdev->dev.platform_data;
146 gpio_set_value(pdata->sensor_power_gpio, 0);
147 gpio_set_value(pdata->sensor_reset_gpio, 0);
151 static irqreturn_t mmpcam_irq(int irq, void *data)
153 struct mcam_camera *mcam = data;
154 unsigned int irqs, handled;
156 spin_lock(&mcam->dev_lock);
157 irqs = mcam_reg_read(mcam, REG_IRQSTAT);
158 handled = mccic_irq(mcam, irqs);
159 spin_unlock(&mcam->dev_lock);
160 return IRQ_RETVAL(handled);
164 static int mmpcam_probe(struct platform_device *pdev)
166 struct mmp_camera *cam;
167 struct mcam_camera *mcam;
168 struct resource *res;
169 struct mmp_camera_platform_data *pdata;
170 int ret;
172 cam = kzalloc(sizeof(*cam), GFP_KERNEL);
173 if (cam == NULL)
174 return -ENOMEM;
175 cam->pdev = pdev;
176 INIT_LIST_HEAD(&cam->devlist);
178 mcam = &cam->mcam;
179 mcam->platform = MHP_Armada610;
180 mcam->plat_power_up = mmpcam_power_up;
181 mcam->plat_power_down = mmpcam_power_down;
182 mcam->dev = &pdev->dev;
183 mcam->use_smbus = 0;
184 mcam->chip_id = V4L2_IDENT_ARMADA610;
185 mcam->buffer_mode = B_DMA_sg;
186 spin_lock_init(&mcam->dev_lock);
188 * Get our I/O memory.
190 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
191 if (res == NULL) {
192 dev_err(&pdev->dev, "no iomem resource!\n");
193 ret = -ENODEV;
194 goto out_free;
196 mcam->regs = ioremap(res->start, resource_size(res));
197 if (mcam->regs == NULL) {
198 dev_err(&pdev->dev, "MMIO ioremap fail\n");
199 ret = -ENODEV;
200 goto out_free;
203 * Power/clock memory is elsewhere; get it too. Perhaps this
204 * should really be managed outside of this driver?
206 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
207 if (res == NULL) {
208 dev_err(&pdev->dev, "no power resource!\n");
209 ret = -ENODEV;
210 goto out_unmap1;
212 cam->power_regs = ioremap(res->start, resource_size(res));
213 if (cam->power_regs == NULL) {
214 dev_err(&pdev->dev, "power MMIO ioremap fail\n");
215 ret = -ENODEV;
216 goto out_unmap1;
219 * Find the i2c adapter. This assumes, of course, that the
220 * i2c bus is already up and functioning.
222 pdata = pdev->dev.platform_data;
223 mcam->i2c_adapter = platform_get_drvdata(pdata->i2c_device);
224 if (mcam->i2c_adapter == NULL) {
225 ret = -ENODEV;
226 dev_err(&pdev->dev, "No i2c adapter\n");
227 goto out_unmap2;
230 * Sensor GPIO pins.
232 ret = gpio_request(pdata->sensor_power_gpio, "cam-power");
233 if (ret) {
234 dev_err(&pdev->dev, "Can't get sensor power gpio %d",
235 pdata->sensor_power_gpio);
236 goto out_unmap2;
238 gpio_direction_output(pdata->sensor_power_gpio, 0);
239 ret = gpio_request(pdata->sensor_reset_gpio, "cam-reset");
240 if (ret) {
241 dev_err(&pdev->dev, "Can't get sensor reset gpio %d",
242 pdata->sensor_reset_gpio);
243 goto out_gpio;
245 gpio_direction_output(pdata->sensor_reset_gpio, 0);
247 * Power the device up and hand it off to the core.
249 mmpcam_power_up(mcam);
250 ret = mccic_register(mcam);
251 if (ret)
252 goto out_gpio2;
254 * Finally, set up our IRQ now that the core is ready to
255 * deal with it.
257 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
258 if (res == NULL) {
259 ret = -ENODEV;
260 goto out_unregister;
262 cam->irq = res->start;
263 ret = request_irq(cam->irq, mmpcam_irq, IRQF_SHARED,
264 "mmp-camera", mcam);
265 if (ret == 0) {
266 mmpcam_add_device(cam);
267 return 0;
270 out_unregister:
271 mccic_shutdown(mcam);
272 out_gpio2:
273 mmpcam_power_down(mcam);
274 gpio_free(pdata->sensor_reset_gpio);
275 out_gpio:
276 gpio_free(pdata->sensor_power_gpio);
277 out_unmap2:
278 iounmap(cam->power_regs);
279 out_unmap1:
280 iounmap(mcam->regs);
281 out_free:
282 kfree(cam);
283 return ret;
287 static int mmpcam_remove(struct mmp_camera *cam)
289 struct mcam_camera *mcam = &cam->mcam;
290 struct mmp_camera_platform_data *pdata;
292 mmpcam_remove_device(cam);
293 free_irq(cam->irq, mcam);
294 mccic_shutdown(mcam);
295 mmpcam_power_down(mcam);
296 pdata = cam->pdev->dev.platform_data;
297 gpio_free(pdata->sensor_reset_gpio);
298 gpio_free(pdata->sensor_power_gpio);
299 iounmap(cam->power_regs);
300 iounmap(mcam->regs);
301 kfree(cam);
302 return 0;
305 static int mmpcam_platform_remove(struct platform_device *pdev)
307 struct mmp_camera *cam = mmpcam_find_device(pdev);
309 if (cam == NULL)
310 return -ENODEV;
311 return mmpcam_remove(cam);
315 * Suspend/resume support.
317 #ifdef CONFIG_PM
319 static int mmpcam_suspend(struct platform_device *pdev, pm_message_t state)
321 struct mmp_camera *cam = mmpcam_find_device(pdev);
323 if (state.event != PM_EVENT_SUSPEND)
324 return 0;
325 mccic_suspend(&cam->mcam);
326 return 0;
329 static int mmpcam_resume(struct platform_device *pdev)
331 struct mmp_camera *cam = mmpcam_find_device(pdev);
334 * Power up unconditionally just in case the core tries to
335 * touch a register even if nothing was active before; trust
336 * me, it's better this way.
338 mmpcam_power_up(&cam->mcam);
339 return mccic_resume(&cam->mcam);
342 #endif
345 static struct platform_driver mmpcam_driver = {
346 .probe = mmpcam_probe,
347 .remove = mmpcam_platform_remove,
348 #ifdef CONFIG_PM
349 .suspend = mmpcam_suspend,
350 .resume = mmpcam_resume,
351 #endif
352 .driver = {
353 .name = "mmp-camera",
354 .owner = THIS_MODULE
359 static int __init mmpcam_init_module(void)
361 mutex_init(&mmpcam_devices_lock);
362 return platform_driver_register(&mmpcam_driver);
365 static void __exit mmpcam_exit_module(void)
367 platform_driver_unregister(&mmpcam_driver);
369 * platform_driver_unregister() should have emptied the list
371 if (!list_empty(&mmpcam_devices))
372 printk(KERN_ERR "mmp_camera leaving devices behind\n");
375 module_init(mmpcam_init_module);
376 module_exit(mmpcam_exit_module);