blob: fa80a4a914d4bb1eee8041d4ae01382627ae6344 [file] [log] [blame]
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001/*
2 * camera image capture (abstract) bus driver
3 *
4 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
5 *
6 * This driver provides an interface between platform-specific camera
7 * busses and camera devices. It should be used if the camera is
8 * connected not over a "proper" bus like PCI or USB, but over a
9 * special bus, like, for example, the Quick Capture interface on PXA270
10 * SoCs. Later it should also be used for i.MX31 SoCs from Freescale.
11 * It can handle multiple cameras and / or multiple busses, which can
12 * be used, e.g., in stereo-vision applications.
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License version 2 as
16 * published by the Free Software Foundation.
17 */
18
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -030019#include <linux/device.h>
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -030020#include <linux/err.h>
Guennadi Liakhovetski0fd327b2009-05-07 13:25:32 -030021#include <linux/i2c.h>
22#include <linux/init.h>
23#include <linux/list.h>
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -030024#include <linux/mutex.h>
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -030025#include <linux/module.h>
Guennadi Liakhovetski0fd327b2009-05-07 13:25:32 -030026#include <linux/platform_device.h>
Alberto Panizzo96e442c2010-12-02 07:43:37 -030027#include <linux/regulator/consumer.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Mauro Carvalho Chehab4f9fb5e2010-05-18 00:46:09 -030029#include <linux/pm_runtime.h>
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -030030#include <linux/vmalloc.h>
31
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -030032#include <media/soc_camera.h>
Guennadi Liakhovetski0fd327b2009-05-07 13:25:32 -030033#include <media/v4l2-common.h>
Guennadi Liakhovetski0fd327b2009-05-07 13:25:32 -030034#include <media/v4l2-ioctl.h>
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -030035#include <media/v4l2-dev.h>
Guennadi Liakhovetski0fd327b2009-05-07 13:25:32 -030036#include <media/videobuf-core.h>
Guennadi Liakhovetski592c2ab2011-01-29 12:44:51 -030037#include <media/videobuf2-core.h>
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -030038#include <media/soc_mediabus.h>
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -030039
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -030040/* Default to VGA resolution */
41#define DEFAULT_WIDTH 640
42#define DEFAULT_HEIGHT 480
43
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -030044static LIST_HEAD(hosts);
45static LIST_HEAD(devices);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -030046static DEFINE_MUTEX(list_lock); /* Protects the list of hosts */
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -030047
Alberto Panizzo96e442c2010-12-02 07:43:37 -030048static int soc_camera_power_set(struct soc_camera_device *icd,
49 struct soc_camera_link *icl,
50 int power_on)
51{
52 int ret;
53
54 if (power_on) {
55 ret = regulator_bulk_enable(icl->num_regulators,
56 icl->regulators);
57 if (ret < 0) {
58 dev_err(&icd->dev, "Cannot enable regulators\n");
59 return ret;
60 }
61
62 if (icl->power)
63 ret = icl->power(icd->pdev, power_on);
64 if (ret < 0) {
65 dev_err(&icd->dev,
66 "Platform failed to power-on the camera.\n");
67
68 regulator_bulk_disable(icl->num_regulators,
69 icl->regulators);
70 return ret;
71 }
72 } else {
73 ret = 0;
74 if (icl->power)
75 ret = icl->power(icd->pdev, 0);
76 if (ret < 0) {
77 dev_err(&icd->dev,
78 "Platform failed to power-off the camera.\n");
79 return ret;
80 }
81
82 ret = regulator_bulk_disable(icl->num_regulators,
83 icl->regulators);
84 if (ret < 0) {
85 dev_err(&icd->dev, "Cannot disable regulators\n");
86 return ret;
87 }
88 }
89
90 return 0;
91}
92
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -030093const struct soc_camera_format_xlate *soc_camera_xlate_by_fourcc(
94 struct soc_camera_device *icd, unsigned int fourcc)
95{
96 unsigned int i;
97
98 for (i = 0; i < icd->num_user_formats; i++)
99 if (icd->user_formats[i].host_fmt->fourcc == fourcc)
100 return icd->user_formats + i;
101 return NULL;
102}
103EXPORT_SYMBOL(soc_camera_xlate_by_fourcc);
104
Guennadi Liakhovetskibd73b362008-12-23 05:54:45 -0300105/**
106 * soc_camera_apply_sensor_flags() - apply platform SOCAM_SENSOR_INVERT_* flags
107 * @icl: camera platform parameters
108 * @flags: flags to be inverted according to platform configuration
109 * @return: resulting flags
110 */
111unsigned long soc_camera_apply_sensor_flags(struct soc_camera_link *icl,
112 unsigned long flags)
113{
114 unsigned long f;
115
116 /* If only one of the two polarities is supported, switch to the opposite */
117 if (icl->flags & SOCAM_SENSOR_INVERT_HSYNC) {
118 f = flags & (SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW);
119 if (f == SOCAM_HSYNC_ACTIVE_HIGH || f == SOCAM_HSYNC_ACTIVE_LOW)
120 flags ^= SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW;
121 }
122
123 if (icl->flags & SOCAM_SENSOR_INVERT_VSYNC) {
124 f = flags & (SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW);
125 if (f == SOCAM_VSYNC_ACTIVE_HIGH || f == SOCAM_VSYNC_ACTIVE_LOW)
126 flags ^= SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW;
127 }
128
129 if (icl->flags & SOCAM_SENSOR_INVERT_PCLK) {
130 f = flags & (SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING);
131 if (f == SOCAM_PCLK_SAMPLE_RISING || f == SOCAM_PCLK_SAMPLE_FALLING)
132 flags ^= SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING;
133 }
134
135 return flags;
136}
137EXPORT_SYMBOL(soc_camera_apply_sensor_flags);
138
Hans Verkuil72937892008-06-01 10:35:18 -0300139static int soc_camera_try_fmt_vid_cap(struct file *file, void *priv,
Guennadi Liakhovetskiabe4c472008-12-01 09:44:56 -0300140 struct v4l2_format *f)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300141{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300142 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -0300143 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300144
145 WARN_ON(priv != file->private_data);
146
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300147 /* limit format to hardware capabilities */
Guennadi Liakhovetski06daa1a2008-12-18 12:52:08 -0300148 return ici->ops->try_fmt(icd, f);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300149}
150
151static int soc_camera_enum_input(struct file *file, void *priv,
152 struct v4l2_input *inp)
153{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300154 struct soc_camera_device *icd = file->private_data;
Kuninori Morimoto34d359d2008-12-18 12:47:46 -0300155 int ret = 0;
156
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300157 if (inp->index != 0)
158 return -EINVAL;
159
Kuninori Morimoto34d359d2008-12-18 12:47:46 -0300160 if (icd->ops->enum_input)
161 ret = icd->ops->enum_input(icd, inp);
162 else {
163 /* default is camera */
164 inp->type = V4L2_INPUT_TYPE_CAMERA;
165 inp->std = V4L2_STD_UNKNOWN;
166 strcpy(inp->name, "Camera");
167 }
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300168
Kuninori Morimoto34d359d2008-12-18 12:47:46 -0300169 return ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300170}
171
172static int soc_camera_g_input(struct file *file, void *priv, unsigned int *i)
173{
174 *i = 0;
175
176 return 0;
177}
178
179static int soc_camera_s_input(struct file *file, void *priv, unsigned int i)
180{
181 if (i > 0)
182 return -EINVAL;
183
184 return 0;
185}
186
187static int soc_camera_s_std(struct file *file, void *priv, v4l2_std_id *a)
188{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300189 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300190 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Kuninori Morimoto513791a2008-12-18 12:46:45 -0300191
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300192 return v4l2_subdev_call(sd, core, s_std, *a);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300193}
194
Qing Xued5b65d2011-01-20 05:19:40 -0300195static int soc_camera_enum_fsizes(struct file *file, void *fh,
196 struct v4l2_frmsizeenum *fsize)
197{
198 struct soc_camera_device *icd = file->private_data;
199 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
200
201 return ici->ops->enum_fsizes(icd, fsize);
202}
203
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300204static int soc_camera_reqbufs(struct file *file, void *priv,
205 struct v4l2_requestbuffers *p)
206{
207 int ret;
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300208 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -0300209 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300210
211 WARN_ON(priv != file->private_data);
212
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300213 if (icd->streamer && icd->streamer != file)
214 return -EBUSY;
215
Guennadi Liakhovetski592c2ab2011-01-29 12:44:51 -0300216 if (ici->ops->init_videobuf) {
217 ret = videobuf_reqbufs(&icd->vb_vidq, p);
218 if (ret < 0)
219 return ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300220
Guennadi Liakhovetski592c2ab2011-01-29 12:44:51 -0300221 ret = ici->ops->reqbufs(icd, p);
222 } else {
223 ret = vb2_reqbufs(&icd->vb2_vidq, p);
224 }
225
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300226 if (!ret && !icd->streamer)
227 icd->streamer = file;
228
229 return ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300230}
231
232static int soc_camera_querybuf(struct file *file, void *priv,
233 struct v4l2_buffer *p)
234{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300235 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetski592c2ab2011-01-29 12:44:51 -0300236 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300237
238 WARN_ON(priv != file->private_data);
239
Guennadi Liakhovetski592c2ab2011-01-29 12:44:51 -0300240 if (ici->ops->init_videobuf)
241 return videobuf_querybuf(&icd->vb_vidq, p);
242 else
243 return vb2_querybuf(&icd->vb2_vidq, p);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300244}
245
246static int soc_camera_qbuf(struct file *file, void *priv,
247 struct v4l2_buffer *p)
248{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300249 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetski592c2ab2011-01-29 12:44:51 -0300250 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300251
252 WARN_ON(priv != file->private_data);
253
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300254 if (icd->streamer != file)
255 return -EBUSY;
256
Guennadi Liakhovetski592c2ab2011-01-29 12:44:51 -0300257 if (ici->ops->init_videobuf)
258 return videobuf_qbuf(&icd->vb_vidq, p);
259 else
260 return vb2_qbuf(&icd->vb2_vidq, p);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300261}
262
263static int soc_camera_dqbuf(struct file *file, void *priv,
264 struct v4l2_buffer *p)
265{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300266 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetski592c2ab2011-01-29 12:44:51 -0300267 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300268
269 WARN_ON(priv != file->private_data);
270
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300271 if (icd->streamer != file)
272 return -EBUSY;
273
Guennadi Liakhovetski592c2ab2011-01-29 12:44:51 -0300274 if (ici->ops->init_videobuf)
275 return videobuf_dqbuf(&icd->vb_vidq, p, file->f_flags & O_NONBLOCK);
276 else
277 return vb2_dqbuf(&icd->vb2_vidq, p, file->f_flags & O_NONBLOCK);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300278}
279
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300280/* Always entered with .video_lock held */
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300281static int soc_camera_init_user_formats(struct soc_camera_device *icd)
282{
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300283 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300284 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Hans Verkuil3805f202010-05-08 17:55:00 -0300285 unsigned int i, fmts = 0, raw_fmts = 0;
286 int ret;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300287 enum v4l2_mbus_pixelcode code;
288
289 while (!v4l2_subdev_call(sd, video, enum_mbus_fmt, raw_fmts, &code))
290 raw_fmts++;
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300291
292 if (!ici->ops->get_formats)
293 /*
294 * Fallback mode - the host will have to serve all
295 * sensor-provided formats one-to-one to the user
296 */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300297 fmts = raw_fmts;
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300298 else
299 /*
300 * First pass - only count formats this host-sensor
301 * configuration can provide
302 */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300303 for (i = 0; i < raw_fmts; i++) {
Guennadi Liakhovetskifa489842009-08-25 11:46:43 -0300304 ret = ici->ops->get_formats(icd, i, NULL);
305 if (ret < 0)
306 return ret;
307 fmts += ret;
308 }
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300309
310 if (!fmts)
311 return -ENXIO;
312
313 icd->user_formats =
314 vmalloc(fmts * sizeof(struct soc_camera_format_xlate));
315 if (!icd->user_formats)
316 return -ENOMEM;
317
318 icd->num_user_formats = fmts;
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300319
320 dev_dbg(&icd->dev, "Found %d supported formats.\n", fmts);
321
322 /* Second pass - actually fill data formats */
Stefan Herbrechtsmeier14df2cc2009-06-24 10:31:25 -0300323 fmts = 0;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300324 for (i = 0; i < raw_fmts; i++)
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300325 if (!ici->ops->get_formats) {
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300326 v4l2_subdev_call(sd, video, enum_mbus_fmt, i, &code);
327 icd->user_formats[i].host_fmt =
328 soc_mbus_get_fmtdesc(code);
329 icd->user_formats[i].code = code;
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300330 } else {
Guennadi Liakhovetskifa489842009-08-25 11:46:43 -0300331 ret = ici->ops->get_formats(icd, i,
332 &icd->user_formats[fmts]);
333 if (ret < 0)
334 goto egfmt;
335 fmts += ret;
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300336 }
337
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300338 icd->current_fmt = &icd->user_formats[0];
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300339
340 return 0;
Guennadi Liakhovetskifa489842009-08-25 11:46:43 -0300341
342egfmt:
343 icd->num_user_formats = 0;
344 vfree(icd->user_formats);
345 return ret;
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300346}
347
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300348/* Always entered with .video_lock held */
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300349static void soc_camera_free_user_formats(struct soc_camera_device *icd)
350{
Guennadi Liakhovetskifa489842009-08-25 11:46:43 -0300351 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
352
353 if (ici->ops->put_formats)
354 ici->ops->put_formats(icd);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300355 icd->current_fmt = NULL;
Guennadi Liakhovetskifa489842009-08-25 11:46:43 -0300356 icd->num_user_formats = 0;
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300357 vfree(icd->user_formats);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300358 icd->user_formats = NULL;
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300359}
360
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300361#define pixfmtstr(x) (x) & 0xff, ((x) >> 8) & 0xff, ((x) >> 16) & 0xff, \
362 ((x) >> 24) & 0xff
363
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300364/* Called with .vb_lock held, or from the first open(2), see comment there */
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300365static int soc_camera_set_fmt(struct soc_camera_device *icd,
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300366 struct v4l2_format *f)
367{
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300368 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
369 struct v4l2_pix_format *pix = &f->fmt.pix;
370 int ret;
371
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300372 dev_dbg(&icd->dev, "S_FMT(%c%c%c%c, %ux%u)\n",
373 pixfmtstr(pix->pixelformat), pix->width, pix->height);
374
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300375 /* We always call try_fmt() before set_fmt() or set_crop() */
376 ret = ici->ops->try_fmt(icd, f);
377 if (ret < 0)
378 return ret;
379
380 ret = ici->ops->set_fmt(icd, f);
381 if (ret < 0) {
382 return ret;
383 } else if (!icd->current_fmt ||
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300384 icd->current_fmt->host_fmt->fourcc != pix->pixelformat) {
Guennadi Liakhovetski2aa58db2009-08-25 11:47:00 -0300385 dev_err(&icd->dev,
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300386 "Host driver hasn't set up current format correctly!\n");
387 return -EINVAL;
388 }
389
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300390 icd->user_width = pix->width;
391 icd->user_height = pix->height;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300392 icd->colorspace = pix->colorspace;
Guennadi Liakhovetski592c2ab2011-01-29 12:44:51 -0300393 icd->field = pix->field;
394 if (ici->ops->init_videobuf)
395 icd->vb_vidq.field = pix->field;
Guennadi Liakhovetski025c18a2009-03-13 06:08:20 -0300396
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300397 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
398 dev_warn(&icd->dev, "Attention! Wrong buf-type %d\n",
399 f->type);
400
401 dev_dbg(&icd->dev, "set width: %d height: %d\n",
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300402 icd->user_width, icd->user_height);
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300403
404 /* set physical bus parameters */
405 return ici->ops->set_bus_param(icd, pix->pixelformat);
406}
407
Hans Verkuilbec43662008-12-30 06:58:20 -0300408static int soc_camera_open(struct file *file)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300409{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300410 struct video_device *vdev = video_devdata(file);
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300411 struct soc_camera_device *icd = container_of(vdev->parent,
412 struct soc_camera_device,
413 dev);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300414 struct soc_camera_link *icl = to_soc_camera_link(icd);
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300415 struct soc_camera_host *ici;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300416 int ret;
417
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300418 if (!icd->ops)
419 /* No device driver attached */
420 return -ENODEV;
421
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300422 ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300423
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300424 if (!try_module_get(ici->ops->owner)) {
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300425 dev_err(&icd->dev, "Couldn't lock capture bus driver.\n");
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300426 return -EINVAL;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300427 }
428
Guennadi Liakhovetski1a0063a2008-04-04 13:46:34 -0300429 icd->use_count++;
430
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300431 /* Now we really have to activate the camera */
432 if (icd->use_count == 1) {
Guennadi Liakhovetski025c18a2009-03-13 06:08:20 -0300433 /* Restore parameters before the last close() per V4L2 API */
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300434 struct v4l2_format f = {
435 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
436 .fmt.pix = {
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300437 .width = icd->user_width,
438 .height = icd->user_height,
Guennadi Liakhovetski025c18a2009-03-13 06:08:20 -0300439 .field = icd->field,
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300440 .colorspace = icd->colorspace,
441 .pixelformat =
442 icd->current_fmt->host_fmt->fourcc,
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300443 },
444 };
445
Alberto Panizzo96e442c2010-12-02 07:43:37 -0300446 ret = soc_camera_power_set(icd, icl, 1);
447 if (ret < 0)
448 goto epower;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300449
450 /* The camera could have been already on, try to reset */
451 if (icl->reset)
452 icl->reset(icd->pdev);
453
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300454 ret = ici->ops->add(icd);
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300455 if (ret < 0) {
456 dev_err(&icd->dev, "Couldn't activate the camera: %d\n", ret);
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300457 goto eiciadd;
458 }
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300459
Mauro Carvalho Chehab4f9fb5e2010-05-18 00:46:09 -0300460 pm_runtime_enable(&icd->vdev->dev);
461 ret = pm_runtime_resume(&icd->vdev->dev);
462 if (ret < 0 && ret != -ENOSYS)
463 goto eresume;
464
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300465 /*
466 * Try to configure with default parameters. Notice: this is the
467 * very first open, so, we cannot race against other calls,
468 * apart from someone else calling open() simultaneously, but
469 * .video_lock is protecting us against it.
470 */
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300471 ret = soc_camera_set_fmt(icd, &f);
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300472 if (ret < 0)
473 goto esfmt;
Guennadi Liakhovetski24d8c022010-12-25 18:29:52 -0300474
Guennadi Liakhovetski592c2ab2011-01-29 12:44:51 -0300475 if (ici->ops->init_videobuf) {
476 ici->ops->init_videobuf(&icd->vb_vidq, icd);
477 } else {
478 ret = ici->ops->init_videobuf2(&icd->vb2_vidq, icd);
479 if (ret < 0)
480 goto einitvb;
481 }
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300482 }
483
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300484 file->private_data = icd;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300485 dev_dbg(&icd->dev, "camera device open\n");
486
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300487 return 0;
488
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300489 /*
Mauro Carvalho Chehab4f9fb5e2010-05-18 00:46:09 -0300490 * First four errors are entered with the .video_lock held
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300491 * and use_count == 1
492 */
Guennadi Liakhovetski592c2ab2011-01-29 12:44:51 -0300493einitvb:
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300494esfmt:
Mauro Carvalho Chehab4f9fb5e2010-05-18 00:46:09 -0300495 pm_runtime_disable(&icd->vdev->dev);
496eresume:
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300497 ici->ops->remove(icd);
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300498eiciadd:
Alberto Panizzo96e442c2010-12-02 07:43:37 -0300499 soc_camera_power_set(icd, icl, 0);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300500epower:
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300501 icd->use_count--;
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300502 module_put(ici->ops->owner);
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300503
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300504 return ret;
505}
506
Hans Verkuilbec43662008-12-30 06:58:20 -0300507static int soc_camera_close(struct file *file)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300508{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300509 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300510 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300511
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300512 icd->use_count--;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300513 if (!icd->use_count) {
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300514 struct soc_camera_link *icl = to_soc_camera_link(icd);
515
Mauro Carvalho Chehab4f9fb5e2010-05-18 00:46:09 -0300516 pm_runtime_suspend(&icd->vdev->dev);
517 pm_runtime_disable(&icd->vdev->dev);
518
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300519 ici->ops->remove(icd);
Guennadi Liakhovetski592c2ab2011-01-29 12:44:51 -0300520 if (ici->ops->init_videobuf2)
521 vb2_queue_release(&icd->vb2_vidq);
Mauro Carvalho Chehab4f9fb5e2010-05-18 00:46:09 -0300522
Alberto Panizzo96e442c2010-12-02 07:43:37 -0300523 soc_camera_power_set(icd, icl, 0);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300524 }
Guennadi Liakhovetski025c18a2009-03-13 06:08:20 -0300525
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300526 if (icd->streamer == file)
527 icd->streamer = NULL;
528
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300529 module_put(ici->ops->owner);
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300530
Guennadi Liakhovetski2aa58db2009-08-25 11:47:00 -0300531 dev_dbg(&icd->dev, "camera device close\n");
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300532
533 return 0;
534}
535
Andrew Mortonaba360d2008-04-22 14:45:59 -0300536static ssize_t soc_camera_read(struct file *file, char __user *buf,
Guennadi Liakhovetskiabe4c472008-12-01 09:44:56 -0300537 size_t count, loff_t *ppos)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300538{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300539 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300540 int err = -EINVAL;
541
Guennadi Liakhovetski2aa58db2009-08-25 11:47:00 -0300542 dev_err(&icd->dev, "camera device read not implemented\n");
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300543
544 return err;
545}
546
547static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma)
548{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300549 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetski592c2ab2011-01-29 12:44:51 -0300550 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300551 int err;
552
553 dev_dbg(&icd->dev, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
554
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300555 if (icd->streamer != file)
556 return -EBUSY;
557
Guennadi Liakhovetski592c2ab2011-01-29 12:44:51 -0300558 if (ici->ops->init_videobuf)
559 err = videobuf_mmap_mapper(&icd->vb_vidq, vma);
560 else
561 err = vb2_mmap(&icd->vb2_vidq, vma);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300562
563 dev_dbg(&icd->dev, "vma start=0x%08lx, size=%ld, ret=%d\n",
564 (unsigned long)vma->vm_start,
565 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start,
566 err);
567
568 return err;
569}
570
571static unsigned int soc_camera_poll(struct file *file, poll_table *pt)
572{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300573 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -0300574 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300575
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300576 if (icd->streamer != file)
577 return -EBUSY;
578
Guennadi Liakhovetski592c2ab2011-01-29 12:44:51 -0300579 if (ici->ops->init_videobuf && list_empty(&icd->vb_vidq.stream)) {
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300580 dev_err(&icd->dev, "Trying to poll with no queued buffers!\n");
581 return POLLERR;
582 }
583
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300584 return ici->ops->poll(file, pt);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300585}
586
Guennadi Liakhovetski592c2ab2011-01-29 12:44:51 -0300587void soc_camera_lock(struct vb2_queue *vq)
588{
589 struct soc_camera_device *icd = vb2_get_drv_priv(vq);
590 mutex_lock(&icd->video_lock);
591}
592EXPORT_SYMBOL(soc_camera_lock);
593
594void soc_camera_unlock(struct vb2_queue *vq)
595{
596 struct soc_camera_device *icd = vb2_get_drv_priv(vq);
597 mutex_unlock(&icd->video_lock);
598}
599EXPORT_SYMBOL(soc_camera_unlock);
600
Hans Verkuilbec43662008-12-30 06:58:20 -0300601static struct v4l2_file_operations soc_camera_fops = {
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300602 .owner = THIS_MODULE,
603 .open = soc_camera_open,
604 .release = soc_camera_close,
Guennadi Liakhovetskib6a633c2010-12-25 17:40:26 -0300605 .unlocked_ioctl = video_ioctl2,
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300606 .read = soc_camera_read,
607 .mmap = soc_camera_mmap,
608 .poll = soc_camera_poll,
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300609};
610
Hans Verkuil72937892008-06-01 10:35:18 -0300611static int soc_camera_s_fmt_vid_cap(struct file *file, void *priv,
Guennadi Liakhovetskiabe4c472008-12-01 09:44:56 -0300612 struct v4l2_format *f)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300613{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300614 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300615 int ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300616
617 WARN_ON(priv != file->private_data);
618
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300619 if (icd->streamer && icd->streamer != file)
620 return -EBUSY;
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300621
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300622 if (icd->vb_vidq.bufs[0]) {
Guennadi Liakhovetskib897a912009-08-25 11:46:53 -0300623 dev_err(&icd->dev, "S_FMT denied: queue initialised\n");
Guennadi Liakhovetskib6a633c2010-12-25 17:40:26 -0300624 return -EBUSY;
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300625 }
626
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300627 ret = soc_camera_set_fmt(icd, f);
628
629 if (!ret && !icd->streamer)
630 icd->streamer = file;
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300631
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300632 return ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300633}
634
Hans Verkuil72937892008-06-01 10:35:18 -0300635static int soc_camera_enum_fmt_vid_cap(struct file *file, void *priv,
Guennadi Liakhovetskiabe4c472008-12-01 09:44:56 -0300636 struct v4l2_fmtdesc *f)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300637{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300638 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300639 const struct soc_mbus_pixelfmt *format;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300640
641 WARN_ON(priv != file->private_data);
642
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300643 if (f->index >= icd->num_user_formats)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300644 return -EINVAL;
645
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300646 format = icd->user_formats[f->index].host_fmt;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300647
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300648 if (format->name)
649 strlcpy(f->description, format->name, sizeof(f->description));
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300650 f->pixelformat = format->fourcc;
651 return 0;
652}
653
Hans Verkuil72937892008-06-01 10:35:18 -0300654static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv,
Guennadi Liakhovetskiabe4c472008-12-01 09:44:56 -0300655 struct v4l2_format *f)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300656{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300657 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -0300658 struct v4l2_pix_format *pix = &f->fmt.pix;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300659
660 WARN_ON(priv != file->private_data);
661
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300662 pix->width = icd->user_width;
663 pix->height = icd->user_height;
Guennadi Liakhovetski592c2ab2011-01-29 12:44:51 -0300664 pix->field = icd->field;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300665 pix->pixelformat = icd->current_fmt->host_fmt->fourcc;
666 pix->bytesperline = soc_mbus_bytes_per_line(pix->width,
667 icd->current_fmt->host_fmt);
668 pix->colorspace = icd->colorspace;
669 if (pix->bytesperline < 0)
670 return pix->bytesperline;
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -0300671 pix->sizeimage = pix->height * pix->bytesperline;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300672 dev_dbg(&icd->dev, "current_fmt->fourcc: 0x%08x\n",
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300673 icd->current_fmt->host_fmt->fourcc);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300674 return 0;
675}
676
677static int soc_camera_querycap(struct file *file, void *priv,
678 struct v4l2_capability *cap)
679{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300680 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -0300681 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300682
683 WARN_ON(priv != file->private_data);
684
685 strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver));
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300686 return ici->ops->querycap(ici, cap);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300687}
688
689static int soc_camera_streamon(struct file *file, void *priv,
690 enum v4l2_buf_type i)
691{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300692 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetski592c2ab2011-01-29 12:44:51 -0300693 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300694 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300695 int ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300696
697 WARN_ON(priv != file->private_data);
698
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300699 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
700 return -EINVAL;
701
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300702 if (icd->streamer != file)
703 return -EBUSY;
704
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300705 /* This calls buf_queue from host driver's videobuf_queue_ops */
Guennadi Liakhovetski592c2ab2011-01-29 12:44:51 -0300706 if (ici->ops->init_videobuf)
707 ret = videobuf_streamon(&icd->vb_vidq);
708 else
709 ret = vb2_streamon(&icd->vb2_vidq, i);
710
Anatolij Gustschin7fdbd852011-01-31 09:19:32 -0300711 if (!ret)
712 v4l2_subdev_call(sd, video, s_stream, 1);
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300713
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300714 return ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300715}
716
717static int soc_camera_streamoff(struct file *file, void *priv,
718 enum v4l2_buf_type i)
719{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300720 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300721 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski592c2ab2011-01-29 12:44:51 -0300722 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300723
724 WARN_ON(priv != file->private_data);
725
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300726 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
727 return -EINVAL;
728
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300729 if (icd->streamer != file)
730 return -EBUSY;
731
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300732 /*
733 * This calls buf_release from host driver's videobuf_queue_ops for all
734 * remaining buffers. When the last buffer is freed, stop capture
735 */
Guennadi Liakhovetski592c2ab2011-01-29 12:44:51 -0300736 if (ici->ops->init_videobuf)
737 videobuf_streamoff(&icd->vb_vidq);
738 else
739 vb2_streamoff(&icd->vb2_vidq, i);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300740
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300741 v4l2_subdev_call(sd, video, s_stream, 0);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300742
743 return 0;
744}
745
746static int soc_camera_queryctrl(struct file *file, void *priv,
747 struct v4l2_queryctrl *qc)
748{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300749 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetski2840d242009-08-25 11:44:15 -0300750 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300751 int i;
752
753 WARN_ON(priv != file->private_data);
754
755 if (!qc->id)
756 return -EINVAL;
757
Guennadi Liakhovetski2840d242009-08-25 11:44:15 -0300758 /* First check host controls */
759 for (i = 0; i < ici->ops->num_controls; i++)
760 if (qc->id == ici->ops->controls[i].id) {
761 memcpy(qc, &(ici->ops->controls[i]),
762 sizeof(*qc));
763 return 0;
764 }
765
766 /* Then device controls */
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300767 for (i = 0; i < icd->ops->num_controls; i++)
768 if (qc->id == icd->ops->controls[i].id) {
769 memcpy(qc, &(icd->ops->controls[i]),
770 sizeof(*qc));
771 return 0;
772 }
773
774 return -EINVAL;
775}
776
777static int soc_camera_g_ctrl(struct file *file, void *priv,
778 struct v4l2_control *ctrl)
779{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300780 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300781 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300782 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski2840d242009-08-25 11:44:15 -0300783 int ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300784
785 WARN_ON(priv != file->private_data);
786
Guennadi Liakhovetski2840d242009-08-25 11:44:15 -0300787 if (ici->ops->get_ctrl) {
788 ret = ici->ops->get_ctrl(icd, ctrl);
789 if (ret != -ENOIOCTLCMD)
790 return ret;
791 }
792
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300793 return v4l2_subdev_call(sd, core, g_ctrl, ctrl);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300794}
795
796static int soc_camera_s_ctrl(struct file *file, void *priv,
797 struct v4l2_control *ctrl)
798{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300799 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300800 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300801 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski2840d242009-08-25 11:44:15 -0300802 int ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300803
804 WARN_ON(priv != file->private_data);
805
Guennadi Liakhovetski2840d242009-08-25 11:44:15 -0300806 if (ici->ops->set_ctrl) {
807 ret = ici->ops->set_ctrl(icd, ctrl);
808 if (ret != -ENOIOCTLCMD)
809 return ret;
810 }
811
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300812 return v4l2_subdev_call(sd, core, s_ctrl, ctrl);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300813}
814
815static int soc_camera_cropcap(struct file *file, void *fh,
816 struct v4l2_cropcap *a)
817{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300818 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300819 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300820
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300821 return ici->ops->cropcap(icd, a);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300822}
823
824static int soc_camera_g_crop(struct file *file, void *fh,
825 struct v4l2_crop *a)
826{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300827 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300828 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
829 int ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300830
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300831 ret = ici->ops->get_crop(icd, a);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300832
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300833 return ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300834}
835
Guennadi Liakhovetski68a54f02009-08-25 11:46:52 -0300836/*
837 * According to the V4L2 API, drivers shall not update the struct v4l2_crop
838 * argument with the actual geometry, instead, the user shall use G_CROP to
Guennadi Liakhovetskiab56d5e2010-03-23 11:31:46 -0300839 * retrieve it.
Guennadi Liakhovetski68a54f02009-08-25 11:46:52 -0300840 */
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300841static int soc_camera_s_crop(struct file *file, void *fh,
842 struct v4l2_crop *a)
843{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300844 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -0300845 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300846 struct v4l2_rect *rect = &a->c;
847 struct v4l2_crop current_crop;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300848 int ret;
849
850 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
851 return -EINVAL;
852
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300853 dev_dbg(&icd->dev, "S_CROP(%ux%u@%u:%u)\n",
854 rect->width, rect->height, rect->left, rect->top);
855
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300856 /* If get_crop fails, we'll let host and / or client drivers decide */
857 ret = ici->ops->get_crop(icd, &current_crop);
858
Guennadi Liakhovetskib897a912009-08-25 11:46:53 -0300859 /* Prohibit window size change with initialised buffers */
Guennadi Liakhovetski103754a2010-08-05 19:23:44 -0300860 if (ret < 0) {
861 dev_err(&icd->dev,
862 "S_CROP denied: getting current crop failed\n");
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300863 } else if (icd->vb_vidq.bufs[0] &&
Guennadi Liakhovetski103754a2010-08-05 19:23:44 -0300864 (a->c.width != current_crop.c.width ||
865 a->c.height != current_crop.c.height)) {
Guennadi Liakhovetskib897a912009-08-25 11:46:53 -0300866 dev_err(&icd->dev,
867 "S_CROP denied: queue initialised and sizes differ\n");
868 ret = -EBUSY;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300869 } else {
870 ret = ici->ops->set_crop(icd, a);
Guennadi Liakhovetskib897a912009-08-25 11:46:53 -0300871 }
872
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300873 return ret;
874}
875
Guennadi Liakhovetskic9f6ef62010-02-09 18:00:30 +0100876static int soc_camera_g_parm(struct file *file, void *fh,
877 struct v4l2_streamparm *a)
878{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300879 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetskic9f6ef62010-02-09 18:00:30 +0100880 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
881
882 if (ici->ops->get_parm)
883 return ici->ops->get_parm(icd, a);
884
885 return -ENOIOCTLCMD;
886}
887
888static int soc_camera_s_parm(struct file *file, void *fh,
889 struct v4l2_streamparm *a)
890{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300891 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetskic9f6ef62010-02-09 18:00:30 +0100892 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
893
894 if (ici->ops->set_parm)
895 return ici->ops->set_parm(icd, a);
896
897 return -ENOIOCTLCMD;
898}
899
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300900static int soc_camera_g_chip_ident(struct file *file, void *fh,
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300901 struct v4l2_dbg_chip_ident *id)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300902{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300903 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300904 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300905
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300906 return v4l2_subdev_call(sd, core, g_chip_ident, id);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300907}
908
909#ifdef CONFIG_VIDEO_ADV_DEBUG
910static int soc_camera_g_register(struct file *file, void *fh,
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300911 struct v4l2_dbg_register *reg)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300912{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300913 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300914 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300915
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300916 return v4l2_subdev_call(sd, core, g_register, reg);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300917}
918
919static int soc_camera_s_register(struct file *file, void *fh,
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300920 struct v4l2_dbg_register *reg)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300921{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300922 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300923 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300924
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300925 return v4l2_subdev_call(sd, core, s_register, reg);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300926}
927#endif
928
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300929/* So far this function cannot fail */
930static void scan_add_host(struct soc_camera_host *ici)
931{
932 struct soc_camera_device *icd;
933
934 mutex_lock(&list_lock);
935
936 list_for_each_entry(icd, &devices, list) {
937 if (icd->iface == ici->nr) {
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300938 int ret;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300939 icd->dev.parent = ici->v4l2_dev.dev;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300940 dev_set_name(&icd->dev, "%u-%u", icd->iface,
941 icd->devnum);
942 ret = device_register(&icd->dev);
943 if (ret < 0) {
944 icd->dev.parent = NULL;
945 dev_err(&icd->dev,
946 "Cannot register device: %d\n", ret);
947 }
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300948 }
949 }
950
951 mutex_unlock(&list_lock);
952}
953
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300954#ifdef CONFIG_I2C_BOARDINFO
955static int soc_camera_init_i2c(struct soc_camera_device *icd,
956 struct soc_camera_link *icl)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300957{
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300958 struct i2c_client *client;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300959 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300960 struct i2c_adapter *adap = i2c_get_adapter(icl->i2c_adapter_id);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300961 struct v4l2_subdev *subdev;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300962
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300963 if (!adap) {
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300964 dev_err(&icd->dev, "Cannot get I2C adapter #%d. No driver?\n",
965 icl->i2c_adapter_id);
966 goto ei2cga;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300967 }
968
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300969 icl->board_info->platform_data = icd;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300970
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300971 subdev = v4l2_i2c_new_subdev_board(&ici->v4l2_dev, adap,
Laurent Pinchart9a1f8b32010-09-24 10:16:44 -0300972 icl->board_info, NULL);
Magnus Dammd74f8412010-02-09 17:40:40 +0900973 if (!subdev)
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300974 goto ei2cnd;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300975
Laurent Pinchartc4ce6d12010-07-30 17:24:54 -0300976 client = v4l2_get_subdevdata(subdev);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300977
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300978 /* Use to_i2c_client(dev) to recover the i2c client */
979 dev_set_drvdata(&icd->dev, &client->dev);
980
981 return 0;
982ei2cnd:
983 i2c_put_adapter(adap);
984ei2cga:
Magnus Dammd74f8412010-02-09 17:40:40 +0900985 return -ENODEV;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300986}
987
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300988static void soc_camera_free_i2c(struct soc_camera_device *icd)
989{
990 struct i2c_client *client =
991 to_i2c_client(to_soc_camera_control(icd));
992 dev_set_drvdata(&icd->dev, NULL);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300993 v4l2_device_unregister_subdev(i2c_get_clientdata(client));
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300994 i2c_unregister_device(client);
995 i2c_put_adapter(client->adapter);
996}
997#else
998#define soc_camera_init_i2c(icd, icl) (-ENODEV)
999#define soc_camera_free_i2c(icd) do {} while (0)
1000#endif
1001
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001002static int soc_camera_video_start(struct soc_camera_device *icd);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001003static int video_dev_create(struct soc_camera_device *icd);
1004/* Called during host-driver probe */
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001005static int soc_camera_probe(struct device *dev)
1006{
1007 struct soc_camera_device *icd = to_soc_camera_dev(dev);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001008 struct soc_camera_host *ici = to_soc_camera_host(dev->parent);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001009 struct soc_camera_link *icl = to_soc_camera_link(icd);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001010 struct device *control = NULL;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001011 struct v4l2_subdev *sd;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001012 struct v4l2_mbus_framefmt mf;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001013 int ret;
1014
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001015 dev_info(dev, "Probing %s\n", dev_name(dev));
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -03001016
Alberto Panizzo96e442c2010-12-02 07:43:37 -03001017 ret = regulator_bulk_get(icd->pdev, icl->num_regulators,
1018 icl->regulators);
1019 if (ret < 0)
1020 goto ereg;
1021
1022 ret = soc_camera_power_set(icd, icl, 1);
1023 if (ret < 0)
1024 goto epower;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001025
1026 /* The camera could have been already on, try to reset */
1027 if (icl->reset)
1028 icl->reset(icd->pdev);
1029
1030 ret = ici->ops->add(icd);
1031 if (ret < 0)
1032 goto eadd;
1033
1034 /* Must have icd->vdev before registering the device */
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001035 ret = video_dev_create(icd);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001036 if (ret < 0)
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001037 goto evdc;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001038
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001039 /* Non-i2c cameras, e.g., soc_camera_platform, have no board_info */
1040 if (icl->board_info) {
1041 ret = soc_camera_init_i2c(icd, icl);
1042 if (ret < 0)
1043 goto eadddev;
1044 } else if (!icl->add_device || !icl->del_device) {
1045 ret = -EINVAL;
1046 goto eadddev;
1047 } else {
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001048 if (icl->module_name)
1049 ret = request_module(icl->module_name);
1050
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001051 ret = icl->add_device(icl, &icd->dev);
1052 if (ret < 0)
1053 goto eadddev;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001054
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -03001055 /*
1056 * FIXME: this is racy, have to use driver-binding notification,
1057 * when it is available
1058 */
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001059 control = to_soc_camera_control(icd);
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -03001060 if (!control || !control->driver || !dev_get_drvdata(control) ||
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001061 !try_module_get(control->driver->owner)) {
1062 icl->del_device(icl);
1063 goto enodrv;
1064 }
Guennadi Liakhovetski025c18a2009-03-13 06:08:20 -03001065 }
1066
Guennadi Liakhovetskifa489842009-08-25 11:46:43 -03001067 /* At this point client .probe() should have run already */
1068 ret = soc_camera_init_user_formats(icd);
1069 if (ret < 0)
1070 goto eiufmt;
1071
Guennadi Liakhovetskifa489842009-08-25 11:46:43 -03001072 icd->field = V4L2_FIELD_ANY;
1073
Guennadi Liakhovetskib6a633c2010-12-25 17:40:26 -03001074 icd->vdev->lock = &icd->video_lock;
1075
1076 /*
1077 * ..._video_start() will create a device node, video_register_device()
1078 * itself is protected against concurrent open() calls, but we also have
1079 * to protect our data.
1080 */
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001081 mutex_lock(&icd->video_lock);
1082
1083 ret = soc_camera_video_start(icd);
1084 if (ret < 0)
1085 goto evidstart;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001086
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001087 /* Try to improve our guess of a reasonable window format */
1088 sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001089 if (!v4l2_subdev_call(sd, video, g_mbus_fmt, &mf)) {
1090 icd->user_width = mf.width;
1091 icd->user_height = mf.height;
1092 icd->colorspace = mf.colorspace;
1093 icd->field = mf.field;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001094 }
1095
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001096 /* Do we have to sysfs_remove_link() before device_unregister()? */
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001097 if (sysfs_create_link(&icd->dev.kobj, &to_soc_camera_control(icd)->kobj,
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001098 "control"))
1099 dev_warn(&icd->dev, "Failed creating the control symlink\n");
1100
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001101 ici->ops->remove(icd);
1102
Alberto Panizzo96e442c2010-12-02 07:43:37 -03001103 soc_camera_power_set(icd, icl, 0);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001104
1105 mutex_unlock(&icd->video_lock);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001106
1107 return 0;
1108
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001109evidstart:
1110 mutex_unlock(&icd->video_lock);
Guennadi Liakhovetskifa489842009-08-25 11:46:43 -03001111 soc_camera_free_user_formats(icd);
1112eiufmt:
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001113 if (icl->board_info) {
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001114 soc_camera_free_i2c(icd);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001115 } else {
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001116 icl->del_device(icl);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001117 module_put(control->driver->owner);
1118 }
1119enodrv:
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001120eadddev:
1121 video_device_release(icd->vdev);
1122evdc:
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001123 ici->ops->remove(icd);
1124eadd:
Alberto Panizzo96e442c2010-12-02 07:43:37 -03001125 soc_camera_power_set(icd, icl, 0);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001126epower:
Alberto Panizzo96e442c2010-12-02 07:43:37 -03001127 regulator_bulk_free(icl->num_regulators, icl->regulators);
1128ereg:
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001129 return ret;
1130}
1131
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -03001132/*
1133 * This is called on device_unregister, which only means we have to disconnect
1134 * from the host, but not remove ourselves from the device list
1135 */
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001136static int soc_camera_remove(struct device *dev)
1137{
1138 struct soc_camera_device *icd = to_soc_camera_dev(dev);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001139 struct soc_camera_link *icl = to_soc_camera_link(icd);
1140 struct video_device *vdev = icd->vdev;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001141
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001142 BUG_ON(!dev->parent);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001143
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001144 if (vdev) {
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001145 video_unregister_device(vdev);
1146 icd->vdev = NULL;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001147 }
1148
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001149 if (icl->board_info) {
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001150 soc_camera_free_i2c(icd);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001151 } else {
1152 struct device_driver *drv = to_soc_camera_control(icd) ?
1153 to_soc_camera_control(icd)->driver : NULL;
1154 if (drv) {
1155 icl->del_device(icl);
1156 module_put(drv->owner);
1157 }
1158 }
Guennadi Liakhovetskifa489842009-08-25 11:46:43 -03001159 soc_camera_free_user_formats(icd);
Guennadi Liakhovetski025c18a2009-03-13 06:08:20 -03001160
Alberto Panizzo96e442c2010-12-02 07:43:37 -03001161 regulator_bulk_free(icl->num_regulators, icl->regulators);
1162
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001163 return 0;
1164}
1165
Robert Jarzmik2e521062008-08-01 20:14:50 -03001166static int soc_camera_suspend(struct device *dev, pm_message_t state)
1167{
1168 struct soc_camera_device *icd = to_soc_camera_dev(dev);
1169 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1170 int ret = 0;
1171
1172 if (ici->ops->suspend)
1173 ret = ici->ops->suspend(icd, state);
1174
1175 return ret;
1176}
1177
1178static int soc_camera_resume(struct device *dev)
1179{
1180 struct soc_camera_device *icd = to_soc_camera_dev(dev);
1181 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1182 int ret = 0;
1183
1184 if (ici->ops->resume)
1185 ret = ici->ops->resume(icd);
1186
1187 return ret;
1188}
1189
Guennadi Liakhovetski52d268a2010-07-26 11:37:13 -03001190struct bus_type soc_camera_bus_type = {
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001191 .name = "soc-camera",
1192 .probe = soc_camera_probe,
1193 .remove = soc_camera_remove,
Robert Jarzmik2e521062008-08-01 20:14:50 -03001194 .suspend = soc_camera_suspend,
1195 .resume = soc_camera_resume,
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001196};
Guennadi Liakhovetski52d268a2010-07-26 11:37:13 -03001197EXPORT_SYMBOL_GPL(soc_camera_bus_type);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001198
1199static struct device_driver ic_drv = {
1200 .name = "camera",
1201 .bus = &soc_camera_bus_type,
1202 .owner = THIS_MODULE,
1203};
1204
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001205static void dummy_release(struct device *dev)
1206{
1207}
1208
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001209static int default_cropcap(struct soc_camera_device *icd,
1210 struct v4l2_cropcap *a)
1211{
1212 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1213 return v4l2_subdev_call(sd, video, cropcap, a);
1214}
1215
1216static int default_g_crop(struct soc_camera_device *icd, struct v4l2_crop *a)
1217{
1218 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1219 return v4l2_subdev_call(sd, video, g_crop, a);
1220}
1221
1222static int default_s_crop(struct soc_camera_device *icd, struct v4l2_crop *a)
1223{
1224 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1225 return v4l2_subdev_call(sd, video, s_crop, a);
1226}
1227
Janusz Krzysztofik06e17822010-09-10 22:26:16 -03001228static int default_g_parm(struct soc_camera_device *icd,
1229 struct v4l2_streamparm *parm)
1230{
1231 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1232 return v4l2_subdev_call(sd, video, g_parm, parm);
1233}
1234
1235static int default_s_parm(struct soc_camera_device *icd,
1236 struct v4l2_streamparm *parm)
1237{
1238 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1239 return v4l2_subdev_call(sd, video, s_parm, parm);
1240}
1241
Qing Xued5b65d2011-01-20 05:19:40 -03001242static int default_enum_fsizes(struct soc_camera_device *icd,
1243 struct v4l2_frmsizeenum *fsize)
1244{
1245 int ret;
1246 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1247 const struct soc_camera_format_xlate *xlate;
1248 __u32 pixfmt = fsize->pixel_format;
1249 struct v4l2_frmsizeenum fsize_mbus = *fsize;
1250
1251 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1252 if (!xlate)
1253 return -EINVAL;
1254 /* map xlate-code to pixel_format, sensor only handle xlate-code*/
1255 fsize_mbus.pixel_format = xlate->code;
1256
1257 ret = v4l2_subdev_call(sd, video, enum_mbus_fsizes, &fsize_mbus);
1258 if (ret < 0)
1259 return ret;
1260
1261 *fsize = fsize_mbus;
1262 fsize->pixel_format = pixfmt;
1263
1264 return 0;
1265}
1266
Guennadi Liakhovetski64ff9ba2009-11-09 16:12:37 -03001267static void soc_camera_device_init(struct device *dev, void *pdata)
1268{
1269 dev->platform_data = pdata;
1270 dev->bus = &soc_camera_bus_type;
1271 dev->release = dummy_release;
1272}
1273
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -03001274int soc_camera_host_register(struct soc_camera_host *ici)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001275{
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001276 struct soc_camera_host *ix;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001277 int ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001278
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -03001279 if (!ici || !ici->ops ||
1280 !ici->ops->try_fmt ||
1281 !ici->ops->set_fmt ||
1282 !ici->ops->set_bus_param ||
1283 !ici->ops->querycap ||
Guennadi Liakhovetski592c2ab2011-01-29 12:44:51 -03001284 ((!ici->ops->init_videobuf ||
1285 !ici->ops->reqbufs) &&
1286 !ici->ops->init_videobuf2) ||
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -03001287 !ici->ops->add ||
1288 !ici->ops->remove ||
Guennadi Liakhovetskieff505f2009-04-24 12:55:48 -03001289 !ici->ops->poll ||
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001290 !ici->v4l2_dev.dev)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001291 return -EINVAL;
1292
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001293 if (!ici->ops->set_crop)
1294 ici->ops->set_crop = default_s_crop;
1295 if (!ici->ops->get_crop)
1296 ici->ops->get_crop = default_g_crop;
1297 if (!ici->ops->cropcap)
1298 ici->ops->cropcap = default_cropcap;
Janusz Krzysztofik06e17822010-09-10 22:26:16 -03001299 if (!ici->ops->set_parm)
1300 ici->ops->set_parm = default_s_parm;
1301 if (!ici->ops->get_parm)
1302 ici->ops->get_parm = default_g_parm;
Qing Xued5b65d2011-01-20 05:19:40 -03001303 if (!ici->ops->enum_fsizes)
1304 ici->ops->enum_fsizes = default_enum_fsizes;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001305
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001306 mutex_lock(&list_lock);
1307 list_for_each_entry(ix, &hosts, list) {
1308 if (ix->nr == ici->nr) {
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001309 ret = -EBUSY;
1310 goto edevreg;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001311 }
1312 }
1313
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001314 ret = v4l2_device_register(ici->v4l2_dev.dev, &ici->v4l2_dev);
1315 if (ret < 0)
1316 goto edevreg;
Guennadi Liakhovetskieff505f2009-04-24 12:55:48 -03001317
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001318 list_add_tail(&ici->list, &hosts);
1319 mutex_unlock(&list_lock);
1320
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001321 scan_add_host(ici);
1322
1323 return 0;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001324
1325edevreg:
1326 mutex_unlock(&list_lock);
1327 return ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001328}
1329EXPORT_SYMBOL(soc_camera_host_register);
1330
1331/* Unregister all clients! */
1332void soc_camera_host_unregister(struct soc_camera_host *ici)
1333{
1334 struct soc_camera_device *icd;
1335
1336 mutex_lock(&list_lock);
1337
1338 list_del(&ici->list);
1339
1340 list_for_each_entry(icd, &devices, list) {
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001341 if (icd->iface == ici->nr) {
Guennadi Liakhovetski64ff9ba2009-11-09 16:12:37 -03001342 void *pdata = icd->dev.platform_data;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001343 /* The bus->remove will be called */
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001344 device_unregister(&icd->dev);
Guennadi Liakhovetski76823b72009-10-05 12:54:54 -03001345 /*
1346 * Not before device_unregister(), .remove
1347 * needs parent to call ici->ops->remove().
1348 * If the host module is loaded again, device_register()
1349 * would complain "already initialised," since 2.6.32
1350 * this is also needed to prevent use-after-free of the
1351 * device private data.
1352 */
1353 memset(&icd->dev, 0, sizeof(icd->dev));
Guennadi Liakhovetski64ff9ba2009-11-09 16:12:37 -03001354 soc_camera_device_init(&icd->dev, pdata);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001355 }
1356 }
1357
1358 mutex_unlock(&list_lock);
1359
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001360 v4l2_device_unregister(&ici->v4l2_dev);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001361}
1362EXPORT_SYMBOL(soc_camera_host_unregister);
1363
1364/* Image capture device */
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001365static int soc_camera_device_register(struct soc_camera_device *icd)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001366{
1367 struct soc_camera_device *ix;
1368 int num = -1, i;
1369
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001370 for (i = 0; i < 256 && num < 0; i++) {
1371 num = i;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001372 /* Check if this index is available on this interface */
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001373 list_for_each_entry(ix, &devices, list) {
1374 if (ix->iface == icd->iface && ix->devnum == i) {
1375 num = -1;
1376 break;
1377 }
1378 }
1379 }
1380
1381 if (num < 0)
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -03001382 /*
1383 * ok, we have 256 cameras on this host...
1384 * man, stay reasonable...
1385 */
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001386 return -ENOMEM;
1387
Guennadi Liakhovetski64ff9ba2009-11-09 16:12:37 -03001388 icd->devnum = num;
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -03001389 icd->use_count = 0;
1390 icd->host_priv = NULL;
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -03001391 mutex_init(&icd->video_lock);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001392
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001393 list_add_tail(&icd->list, &devices);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001394
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001395 return 0;
1396}
1397
1398static void soc_camera_device_unregister(struct soc_camera_device *icd)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001399{
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001400 list_del(&icd->list);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001401}
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001402
Hans Verkuila3998102008-07-21 02:57:38 -03001403static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
1404 .vidioc_querycap = soc_camera_querycap,
1405 .vidioc_g_fmt_vid_cap = soc_camera_g_fmt_vid_cap,
1406 .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
1407 .vidioc_s_fmt_vid_cap = soc_camera_s_fmt_vid_cap,
1408 .vidioc_enum_input = soc_camera_enum_input,
1409 .vidioc_g_input = soc_camera_g_input,
1410 .vidioc_s_input = soc_camera_s_input,
1411 .vidioc_s_std = soc_camera_s_std,
Qing Xued5b65d2011-01-20 05:19:40 -03001412 .vidioc_enum_framesizes = soc_camera_enum_fsizes,
Hans Verkuila3998102008-07-21 02:57:38 -03001413 .vidioc_reqbufs = soc_camera_reqbufs,
1414 .vidioc_try_fmt_vid_cap = soc_camera_try_fmt_vid_cap,
1415 .vidioc_querybuf = soc_camera_querybuf,
1416 .vidioc_qbuf = soc_camera_qbuf,
1417 .vidioc_dqbuf = soc_camera_dqbuf,
1418 .vidioc_streamon = soc_camera_streamon,
1419 .vidioc_streamoff = soc_camera_streamoff,
1420 .vidioc_queryctrl = soc_camera_queryctrl,
1421 .vidioc_g_ctrl = soc_camera_g_ctrl,
1422 .vidioc_s_ctrl = soc_camera_s_ctrl,
1423 .vidioc_cropcap = soc_camera_cropcap,
1424 .vidioc_g_crop = soc_camera_g_crop,
1425 .vidioc_s_crop = soc_camera_s_crop,
Guennadi Liakhovetskic9f6ef62010-02-09 18:00:30 +01001426 .vidioc_g_parm = soc_camera_g_parm,
1427 .vidioc_s_parm = soc_camera_s_parm,
Hans Verkuila3998102008-07-21 02:57:38 -03001428 .vidioc_g_chip_ident = soc_camera_g_chip_ident,
1429#ifdef CONFIG_VIDEO_ADV_DEBUG
1430 .vidioc_g_register = soc_camera_g_register,
1431 .vidioc_s_register = soc_camera_s_register,
1432#endif
1433};
1434
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001435static int video_dev_create(struct soc_camera_device *icd)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001436{
1437 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001438 struct video_device *vdev = video_device_alloc();
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001439
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001440 if (!vdev)
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001441 return -ENOMEM;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001442
1443 strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -03001444
Hans Verkuil5e85e732008-07-20 06:31:39 -03001445 vdev->parent = &icd->dev;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001446 vdev->current_norm = V4L2_STD_UNKNOWN;
1447 vdev->fops = &soc_camera_fops;
Hans Verkuila3998102008-07-21 02:57:38 -03001448 vdev->ioctl_ops = &soc_camera_ioctl_ops;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001449 vdev->release = video_device_release;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001450 vdev->tvnorms = V4L2_STD_UNKNOWN;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001451
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001452 icd->vdev = vdev;
1453
1454 return 0;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001455}
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001456
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001457/*
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001458 * Called from soc_camera_probe() above (with .video_lock held???)
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001459 */
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001460static int soc_camera_video_start(struct soc_camera_device *icd)
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001461{
Mauro Carvalho Chehab4f9fb5e2010-05-18 00:46:09 -03001462 struct device_type *type = icd->vdev->dev.type;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001463 int ret;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001464
1465 if (!icd->dev.parent)
1466 return -ENODEV;
1467
1468 if (!icd->ops ||
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001469 !icd->ops->query_bus_param ||
1470 !icd->ops->set_bus_param)
1471 return -EINVAL;
1472
Laurent Pinchart46b21092009-12-10 11:51:38 -02001473 ret = video_register_device(icd->vdev, VFL_TYPE_GRABBER, -1);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001474 if (ret < 0) {
1475 dev_err(&icd->dev, "video_register_device failed: %d\n", ret);
1476 return ret;
1477 }
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001478
Mauro Carvalho Chehab4f9fb5e2010-05-18 00:46:09 -03001479 /* Restore device type, possibly set by the subdevice driver */
1480 icd->vdev->dev.type = type;
1481
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001482 return 0;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001483}
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001484
Guennadi Liakhovetskic41deba2009-08-25 11:06:21 -03001485static int __devinit soc_camera_pdrv_probe(struct platform_device *pdev)
1486{
1487 struct soc_camera_link *icl = pdev->dev.platform_data;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001488 struct soc_camera_device *icd;
1489 int ret;
Guennadi Liakhovetskic41deba2009-08-25 11:06:21 -03001490
1491 if (!icl)
1492 return -EINVAL;
1493
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001494 icd = kzalloc(sizeof(*icd), GFP_KERNEL);
1495 if (!icd)
1496 return -ENOMEM;
Guennadi Liakhovetskic41deba2009-08-25 11:06:21 -03001497
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001498 icd->iface = icl->bus_id;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001499 icd->pdev = &pdev->dev;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001500 platform_set_drvdata(pdev, icd);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001501
1502 ret = soc_camera_device_register(icd);
1503 if (ret < 0)
1504 goto escdevreg;
1505
Guennadi Liakhovetski64ff9ba2009-11-09 16:12:37 -03001506 soc_camera_device_init(&icd->dev, icl);
1507
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001508 icd->user_width = DEFAULT_WIDTH;
1509 icd->user_height = DEFAULT_HEIGHT;
1510
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001511 return 0;
1512
1513escdevreg:
1514 kfree(icd);
1515
1516 return ret;
Guennadi Liakhovetskic41deba2009-08-25 11:06:21 -03001517}
1518
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -03001519/*
1520 * Only called on rmmod for each platform device, since they are not
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001521 * hot-pluggable. Now we know, that all our users - hosts and devices have
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -03001522 * been unloaded already
1523 */
Guennadi Liakhovetskic41deba2009-08-25 11:06:21 -03001524static int __devexit soc_camera_pdrv_remove(struct platform_device *pdev)
1525{
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001526 struct soc_camera_device *icd = platform_get_drvdata(pdev);
Guennadi Liakhovetskic41deba2009-08-25 11:06:21 -03001527
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001528 if (!icd)
1529 return -EINVAL;
1530
1531 soc_camera_device_unregister(icd);
1532
1533 kfree(icd);
1534
Guennadi Liakhovetski0fd327b2009-05-07 13:25:32 -03001535 return 0;
1536}
1537
1538static struct platform_driver __refdata soc_camera_pdrv = {
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001539 .remove = __devexit_p(soc_camera_pdrv_remove),
1540 .driver = {
1541 .name = "soc-camera-pdrv",
1542 .owner = THIS_MODULE,
Guennadi Liakhovetski0fd327b2009-05-07 13:25:32 -03001543 },
1544};
1545
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001546static int __init soc_camera_init(void)
1547{
1548 int ret = bus_register(&soc_camera_bus_type);
1549 if (ret)
1550 return ret;
1551 ret = driver_register(&ic_drv);
1552 if (ret)
1553 goto edrvr;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001554
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001555 ret = platform_driver_probe(&soc_camera_pdrv, soc_camera_pdrv_probe);
Guennadi Liakhovetski0fd327b2009-05-07 13:25:32 -03001556 if (ret)
1557 goto epdr;
1558
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001559 return 0;
1560
Guennadi Liakhovetski0fd327b2009-05-07 13:25:32 -03001561epdr:
1562 driver_unregister(&ic_drv);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001563edrvr:
1564 bus_unregister(&soc_camera_bus_type);
1565 return ret;
1566}
1567
1568static void __exit soc_camera_exit(void)
1569{
Guennadi Liakhovetski0fd327b2009-05-07 13:25:32 -03001570 platform_driver_unregister(&soc_camera_pdrv);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001571 driver_unregister(&ic_drv);
1572 bus_unregister(&soc_camera_bus_type);
1573}
1574
1575module_init(soc_camera_init);
1576module_exit(soc_camera_exit);
1577
1578MODULE_DESCRIPTION("Image capture bus driver");
1579MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
1580MODULE_LICENSE("GPL");
Guennadi Liakhovetski0fd327b2009-05-07 13:25:32 -03001581MODULE_ALIAS("platform:soc-camera-pdrv");