blob: 398864370267f33d12a7fe56a5feaa5ce6c916fa [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 Liakhovetskiaee5c2f2011-03-28 13:28:28 -030044#define is_streaming(ici, icd) \
45 (((ici)->ops->init_videobuf) ? \
46 (icd)->vb_vidq.streaming : \
47 vb2_is_streaming(&(icd)->vb2_vidq))
48
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -030049static LIST_HEAD(hosts);
50static LIST_HEAD(devices);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -030051static DEFINE_MUTEX(list_lock); /* Protects the list of hosts */
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -030052
Alberto Panizzo96e442c2010-12-02 07:43:37 -030053static int soc_camera_power_set(struct soc_camera_device *icd,
54 struct soc_camera_link *icl,
55 int power_on)
56{
57 int ret;
58
59 if (power_on) {
60 ret = regulator_bulk_enable(icl->num_regulators,
61 icl->regulators);
62 if (ret < 0) {
63 dev_err(&icd->dev, "Cannot enable regulators\n");
64 return ret;
65 }
66
67 if (icl->power)
68 ret = icl->power(icd->pdev, power_on);
69 if (ret < 0) {
70 dev_err(&icd->dev,
71 "Platform failed to power-on the camera.\n");
72
73 regulator_bulk_disable(icl->num_regulators,
74 icl->regulators);
75 return ret;
76 }
77 } else {
78 ret = 0;
79 if (icl->power)
80 ret = icl->power(icd->pdev, 0);
81 if (ret < 0) {
82 dev_err(&icd->dev,
83 "Platform failed to power-off the camera.\n");
84 return ret;
85 }
86
87 ret = regulator_bulk_disable(icl->num_regulators,
88 icl->regulators);
89 if (ret < 0) {
90 dev_err(&icd->dev, "Cannot disable regulators\n");
91 return ret;
92 }
93 }
94
95 return 0;
96}
97
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -030098const struct soc_camera_format_xlate *soc_camera_xlate_by_fourcc(
99 struct soc_camera_device *icd, unsigned int fourcc)
100{
101 unsigned int i;
102
103 for (i = 0; i < icd->num_user_formats; i++)
104 if (icd->user_formats[i].host_fmt->fourcc == fourcc)
105 return icd->user_formats + i;
106 return NULL;
107}
108EXPORT_SYMBOL(soc_camera_xlate_by_fourcc);
109
Guennadi Liakhovetskibd73b362008-12-23 05:54:45 -0300110/**
111 * soc_camera_apply_sensor_flags() - apply platform SOCAM_SENSOR_INVERT_* flags
112 * @icl: camera platform parameters
113 * @flags: flags to be inverted according to platform configuration
114 * @return: resulting flags
115 */
116unsigned long soc_camera_apply_sensor_flags(struct soc_camera_link *icl,
117 unsigned long flags)
118{
119 unsigned long f;
120
121 /* If only one of the two polarities is supported, switch to the opposite */
122 if (icl->flags & SOCAM_SENSOR_INVERT_HSYNC) {
123 f = flags & (SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW);
124 if (f == SOCAM_HSYNC_ACTIVE_HIGH || f == SOCAM_HSYNC_ACTIVE_LOW)
125 flags ^= SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW;
126 }
127
128 if (icl->flags & SOCAM_SENSOR_INVERT_VSYNC) {
129 f = flags & (SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW);
130 if (f == SOCAM_VSYNC_ACTIVE_HIGH || f == SOCAM_VSYNC_ACTIVE_LOW)
131 flags ^= SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW;
132 }
133
134 if (icl->flags & SOCAM_SENSOR_INVERT_PCLK) {
135 f = flags & (SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING);
136 if (f == SOCAM_PCLK_SAMPLE_RISING || f == SOCAM_PCLK_SAMPLE_FALLING)
137 flags ^= SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING;
138 }
139
140 return flags;
141}
142EXPORT_SYMBOL(soc_camera_apply_sensor_flags);
143
Sergio Aguirredca6b6d2011-04-11 13:14:33 -0300144#define pixfmtstr(x) (x) & 0xff, ((x) >> 8) & 0xff, ((x) >> 16) & 0xff, \
145 ((x) >> 24) & 0xff
146
147static int soc_camera_try_fmt(struct soc_camera_device *icd,
148 struct v4l2_format *f)
149{
150 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
151 struct v4l2_pix_format *pix = &f->fmt.pix;
152 int ret;
153
154 dev_dbg(&icd->dev, "TRY_FMT(%c%c%c%c, %ux%u)\n",
155 pixfmtstr(pix->pixelformat), pix->width, pix->height);
156
157 pix->bytesperline = 0;
158 pix->sizeimage = 0;
159
160 ret = ici->ops->try_fmt(icd, f);
161 if (ret < 0)
162 return ret;
163
164 if (!pix->sizeimage) {
165 if (!pix->bytesperline) {
166 const struct soc_camera_format_xlate *xlate;
167
168 xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
169 if (!xlate)
170 return -EINVAL;
171
172 ret = soc_mbus_bytes_per_line(pix->width,
173 xlate->host_fmt);
174 if (ret > 0)
175 pix->bytesperline = ret;
176 }
177 if (pix->bytesperline)
178 pix->sizeimage = pix->bytesperline * pix->height;
179 }
180
181 return 0;
182}
183
Hans Verkuil72937892008-06-01 10:35:18 -0300184static int soc_camera_try_fmt_vid_cap(struct file *file, void *priv,
Guennadi Liakhovetskiabe4c472008-12-01 09:44:56 -0300185 struct v4l2_format *f)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300186{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300187 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300188
189 WARN_ON(priv != file->private_data);
190
Guennadi Liakhovetskid366d4a2011-03-21 06:33:25 -0300191 /* Only single-plane capture is supported so far */
192 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
193 return -EINVAL;
194
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300195 /* limit format to hardware capabilities */
Sergio Aguirredca6b6d2011-04-11 13:14:33 -0300196 return soc_camera_try_fmt(icd, f);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300197}
198
199static int soc_camera_enum_input(struct file *file, void *priv,
200 struct v4l2_input *inp)
201{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300202 struct soc_camera_device *icd = file->private_data;
Kuninori Morimoto34d359d2008-12-18 12:47:46 -0300203 int ret = 0;
204
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300205 if (inp->index != 0)
206 return -EINVAL;
207
Kuninori Morimoto34d359d2008-12-18 12:47:46 -0300208 if (icd->ops->enum_input)
209 ret = icd->ops->enum_input(icd, inp);
210 else {
211 /* default is camera */
212 inp->type = V4L2_INPUT_TYPE_CAMERA;
213 inp->std = V4L2_STD_UNKNOWN;
214 strcpy(inp->name, "Camera");
215 }
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300216
Kuninori Morimoto34d359d2008-12-18 12:47:46 -0300217 return ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300218}
219
220static int soc_camera_g_input(struct file *file, void *priv, unsigned int *i)
221{
222 *i = 0;
223
224 return 0;
225}
226
227static int soc_camera_s_input(struct file *file, void *priv, unsigned int i)
228{
229 if (i > 0)
230 return -EINVAL;
231
232 return 0;
233}
234
235static int soc_camera_s_std(struct file *file, void *priv, v4l2_std_id *a)
236{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300237 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300238 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Kuninori Morimoto513791a2008-12-18 12:46:45 -0300239
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300240 return v4l2_subdev_call(sd, core, s_std, *a);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300241}
242
Qing Xued5b65d2011-01-20 05:19:40 -0300243static int soc_camera_enum_fsizes(struct file *file, void *fh,
244 struct v4l2_frmsizeenum *fsize)
245{
246 struct soc_camera_device *icd = file->private_data;
247 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
248
249 return ici->ops->enum_fsizes(icd, fsize);
250}
251
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300252static int soc_camera_reqbufs(struct file *file, void *priv,
253 struct v4l2_requestbuffers *p)
254{
255 int ret;
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300256 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -0300257 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300258
259 WARN_ON(priv != file->private_data);
260
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300261 if (icd->streamer && icd->streamer != file)
262 return -EBUSY;
263
Guennadi Liakhovetski592c2ab2011-01-29 12:44:51 -0300264 if (ici->ops->init_videobuf) {
265 ret = videobuf_reqbufs(&icd->vb_vidq, p);
266 if (ret < 0)
267 return ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300268
Guennadi Liakhovetski592c2ab2011-01-29 12:44:51 -0300269 ret = ici->ops->reqbufs(icd, p);
270 } else {
271 ret = vb2_reqbufs(&icd->vb2_vidq, p);
272 }
273
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300274 if (!ret && !icd->streamer)
275 icd->streamer = file;
276
277 return ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300278}
279
280static int soc_camera_querybuf(struct file *file, void *priv,
281 struct v4l2_buffer *p)
282{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300283 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetski592c2ab2011-01-29 12:44:51 -0300284 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300285
286 WARN_ON(priv != file->private_data);
287
Guennadi Liakhovetski592c2ab2011-01-29 12:44:51 -0300288 if (ici->ops->init_videobuf)
289 return videobuf_querybuf(&icd->vb_vidq, p);
290 else
291 return vb2_querybuf(&icd->vb2_vidq, p);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300292}
293
294static int soc_camera_qbuf(struct file *file, void *priv,
295 struct v4l2_buffer *p)
296{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300297 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetski592c2ab2011-01-29 12:44:51 -0300298 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300299
300 WARN_ON(priv != file->private_data);
301
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300302 if (icd->streamer != file)
303 return -EBUSY;
304
Guennadi Liakhovetski592c2ab2011-01-29 12:44:51 -0300305 if (ici->ops->init_videobuf)
306 return videobuf_qbuf(&icd->vb_vidq, p);
307 else
308 return vb2_qbuf(&icd->vb2_vidq, p);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300309}
310
311static int soc_camera_dqbuf(struct file *file, void *priv,
312 struct v4l2_buffer *p)
313{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300314 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetski592c2ab2011-01-29 12:44:51 -0300315 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300316
317 WARN_ON(priv != file->private_data);
318
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300319 if (icd->streamer != file)
320 return -EBUSY;
321
Guennadi Liakhovetski592c2ab2011-01-29 12:44:51 -0300322 if (ici->ops->init_videobuf)
323 return videobuf_dqbuf(&icd->vb_vidq, p, file->f_flags & O_NONBLOCK);
324 else
325 return vb2_dqbuf(&icd->vb2_vidq, p, file->f_flags & O_NONBLOCK);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300326}
327
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300328/* Always entered with .video_lock held */
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300329static int soc_camera_init_user_formats(struct soc_camera_device *icd)
330{
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300331 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300332 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Hans Verkuil3805f202010-05-08 17:55:00 -0300333 unsigned int i, fmts = 0, raw_fmts = 0;
334 int ret;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300335 enum v4l2_mbus_pixelcode code;
336
337 while (!v4l2_subdev_call(sd, video, enum_mbus_fmt, raw_fmts, &code))
338 raw_fmts++;
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300339
340 if (!ici->ops->get_formats)
341 /*
342 * Fallback mode - the host will have to serve all
343 * sensor-provided formats one-to-one to the user
344 */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300345 fmts = raw_fmts;
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300346 else
347 /*
348 * First pass - only count formats this host-sensor
349 * configuration can provide
350 */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300351 for (i = 0; i < raw_fmts; i++) {
Guennadi Liakhovetskifa489842009-08-25 11:46:43 -0300352 ret = ici->ops->get_formats(icd, i, NULL);
353 if (ret < 0)
354 return ret;
355 fmts += ret;
356 }
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300357
358 if (!fmts)
359 return -ENXIO;
360
361 icd->user_formats =
362 vmalloc(fmts * sizeof(struct soc_camera_format_xlate));
363 if (!icd->user_formats)
364 return -ENOMEM;
365
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300366 dev_dbg(&icd->dev, "Found %d supported formats.\n", fmts);
367
368 /* Second pass - actually fill data formats */
Stefan Herbrechtsmeier14df2cc2009-06-24 10:31:25 -0300369 fmts = 0;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300370 for (i = 0; i < raw_fmts; i++)
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300371 if (!ici->ops->get_formats) {
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300372 v4l2_subdev_call(sd, video, enum_mbus_fmt, i, &code);
Guennadi Liakhovetskid2dcad42011-05-18 06:49:54 -0300373 icd->user_formats[fmts].host_fmt =
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300374 soc_mbus_get_fmtdesc(code);
Guennadi Liakhovetskid2dcad42011-05-18 06:49:54 -0300375 if (icd->user_formats[fmts].host_fmt)
376 icd->user_formats[fmts++].code = code;
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300377 } else {
Guennadi Liakhovetskifa489842009-08-25 11:46:43 -0300378 ret = ici->ops->get_formats(icd, i,
379 &icd->user_formats[fmts]);
380 if (ret < 0)
381 goto egfmt;
382 fmts += ret;
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300383 }
384
Guennadi Liakhovetskid2dcad42011-05-18 06:49:54 -0300385 icd->num_user_formats = fmts;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300386 icd->current_fmt = &icd->user_formats[0];
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300387
388 return 0;
Guennadi Liakhovetskifa489842009-08-25 11:46:43 -0300389
390egfmt:
Guennadi Liakhovetskifa489842009-08-25 11:46:43 -0300391 vfree(icd->user_formats);
392 return ret;
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300393}
394
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300395/* Always entered with .video_lock held */
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300396static void soc_camera_free_user_formats(struct soc_camera_device *icd)
397{
Guennadi Liakhovetskifa489842009-08-25 11:46:43 -0300398 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
399
400 if (ici->ops->put_formats)
401 ici->ops->put_formats(icd);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300402 icd->current_fmt = NULL;
Guennadi Liakhovetskifa489842009-08-25 11:46:43 -0300403 icd->num_user_formats = 0;
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300404 vfree(icd->user_formats);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300405 icd->user_formats = NULL;
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300406}
407
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300408/* Called with .vb_lock held, or from the first open(2), see comment there */
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300409static int soc_camera_set_fmt(struct soc_camera_device *icd,
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300410 struct v4l2_format *f)
411{
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300412 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
413 struct v4l2_pix_format *pix = &f->fmt.pix;
414 int ret;
415
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300416 dev_dbg(&icd->dev, "S_FMT(%c%c%c%c, %ux%u)\n",
417 pixfmtstr(pix->pixelformat), pix->width, pix->height);
418
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300419 /* We always call try_fmt() before set_fmt() or set_crop() */
Sergio Aguirredca6b6d2011-04-11 13:14:33 -0300420 ret = soc_camera_try_fmt(icd, f);
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300421 if (ret < 0)
422 return ret;
423
424 ret = ici->ops->set_fmt(icd, f);
425 if (ret < 0) {
426 return ret;
427 } else if (!icd->current_fmt ||
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300428 icd->current_fmt->host_fmt->fourcc != pix->pixelformat) {
Guennadi Liakhovetski2aa58db2009-08-25 11:47:00 -0300429 dev_err(&icd->dev,
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300430 "Host driver hasn't set up current format correctly!\n");
431 return -EINVAL;
432 }
433
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300434 icd->user_width = pix->width;
435 icd->user_height = pix->height;
Sergio Aguirre0e4c1802011-03-07 21:49:48 -0300436 icd->bytesperline = pix->bytesperline;
437 icd->sizeimage = pix->sizeimage;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300438 icd->colorspace = pix->colorspace;
Guennadi Liakhovetski592c2ab2011-01-29 12:44:51 -0300439 icd->field = pix->field;
440 if (ici->ops->init_videobuf)
441 icd->vb_vidq.field = pix->field;
Guennadi Liakhovetski025c18a2009-03-13 06:08:20 -0300442
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300443 dev_dbg(&icd->dev, "set width: %d height: %d\n",
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300444 icd->user_width, icd->user_height);
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300445
446 /* set physical bus parameters */
447 return ici->ops->set_bus_param(icd, pix->pixelformat);
448}
449
Hans Verkuilbec43662008-12-30 06:58:20 -0300450static int soc_camera_open(struct file *file)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300451{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300452 struct video_device *vdev = video_devdata(file);
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300453 struct soc_camera_device *icd = container_of(vdev->parent,
454 struct soc_camera_device,
455 dev);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300456 struct soc_camera_link *icl = to_soc_camera_link(icd);
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300457 struct soc_camera_host *ici;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300458 int ret;
459
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300460 if (!icd->ops)
461 /* No device driver attached */
462 return -ENODEV;
463
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300464 ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300465
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300466 if (!try_module_get(ici->ops->owner)) {
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300467 dev_err(&icd->dev, "Couldn't lock capture bus driver.\n");
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300468 return -EINVAL;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300469 }
470
Guennadi Liakhovetski1a0063a2008-04-04 13:46:34 -0300471 icd->use_count++;
472
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300473 /* Now we really have to activate the camera */
474 if (icd->use_count == 1) {
Guennadi Liakhovetski025c18a2009-03-13 06:08:20 -0300475 /* Restore parameters before the last close() per V4L2 API */
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300476 struct v4l2_format f = {
477 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
478 .fmt.pix = {
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300479 .width = icd->user_width,
480 .height = icd->user_height,
Guennadi Liakhovetski025c18a2009-03-13 06:08:20 -0300481 .field = icd->field,
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300482 .colorspace = icd->colorspace,
483 .pixelformat =
484 icd->current_fmt->host_fmt->fourcc,
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300485 },
486 };
487
Alberto Panizzo96e442c2010-12-02 07:43:37 -0300488 ret = soc_camera_power_set(icd, icl, 1);
489 if (ret < 0)
490 goto epower;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300491
492 /* The camera could have been already on, try to reset */
493 if (icl->reset)
494 icl->reset(icd->pdev);
495
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300496 ret = ici->ops->add(icd);
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300497 if (ret < 0) {
498 dev_err(&icd->dev, "Couldn't activate the camera: %d\n", ret);
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300499 goto eiciadd;
500 }
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300501
Mauro Carvalho Chehab4f9fb5e2010-05-18 00:46:09 -0300502 pm_runtime_enable(&icd->vdev->dev);
503 ret = pm_runtime_resume(&icd->vdev->dev);
504 if (ret < 0 && ret != -ENOSYS)
505 goto eresume;
506
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300507 /*
508 * Try to configure with default parameters. Notice: this is the
509 * very first open, so, we cannot race against other calls,
510 * apart from someone else calling open() simultaneously, but
511 * .video_lock is protecting us against it.
512 */
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300513 ret = soc_camera_set_fmt(icd, &f);
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300514 if (ret < 0)
515 goto esfmt;
Guennadi Liakhovetski24d8c022010-12-25 18:29:52 -0300516
Guennadi Liakhovetski592c2ab2011-01-29 12:44:51 -0300517 if (ici->ops->init_videobuf) {
518 ici->ops->init_videobuf(&icd->vb_vidq, icd);
519 } else {
520 ret = ici->ops->init_videobuf2(&icd->vb2_vidq, icd);
521 if (ret < 0)
522 goto einitvb;
523 }
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300524 }
525
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300526 file->private_data = icd;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300527 dev_dbg(&icd->dev, "camera device open\n");
528
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300529 return 0;
530
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300531 /*
Mauro Carvalho Chehab4f9fb5e2010-05-18 00:46:09 -0300532 * First four errors are entered with the .video_lock held
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300533 * and use_count == 1
534 */
Guennadi Liakhovetski592c2ab2011-01-29 12:44:51 -0300535einitvb:
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300536esfmt:
Mauro Carvalho Chehab4f9fb5e2010-05-18 00:46:09 -0300537 pm_runtime_disable(&icd->vdev->dev);
538eresume:
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300539 ici->ops->remove(icd);
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300540eiciadd:
Alberto Panizzo96e442c2010-12-02 07:43:37 -0300541 soc_camera_power_set(icd, icl, 0);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300542epower:
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300543 icd->use_count--;
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300544 module_put(ici->ops->owner);
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300545
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300546 return ret;
547}
548
Hans Verkuilbec43662008-12-30 06:58:20 -0300549static int soc_camera_close(struct file *file)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300550{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300551 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300552 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300553
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300554 icd->use_count--;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300555 if (!icd->use_count) {
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300556 struct soc_camera_link *icl = to_soc_camera_link(icd);
557
Mauro Carvalho Chehab4f9fb5e2010-05-18 00:46:09 -0300558 pm_runtime_suspend(&icd->vdev->dev);
559 pm_runtime_disable(&icd->vdev->dev);
560
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300561 ici->ops->remove(icd);
Guennadi Liakhovetski592c2ab2011-01-29 12:44:51 -0300562 if (ici->ops->init_videobuf2)
563 vb2_queue_release(&icd->vb2_vidq);
Mauro Carvalho Chehab4f9fb5e2010-05-18 00:46:09 -0300564
Alberto Panizzo96e442c2010-12-02 07:43:37 -0300565 soc_camera_power_set(icd, icl, 0);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300566 }
Guennadi Liakhovetski025c18a2009-03-13 06:08:20 -0300567
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300568 if (icd->streamer == file)
569 icd->streamer = NULL;
570
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300571 module_put(ici->ops->owner);
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300572
Guennadi Liakhovetski2aa58db2009-08-25 11:47:00 -0300573 dev_dbg(&icd->dev, "camera device close\n");
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300574
575 return 0;
576}
577
Andrew Mortonaba360d2008-04-22 14:45:59 -0300578static ssize_t soc_camera_read(struct file *file, char __user *buf,
Guennadi Liakhovetskiabe4c472008-12-01 09:44:56 -0300579 size_t count, loff_t *ppos)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300580{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300581 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300582 int err = -EINVAL;
583
Guennadi Liakhovetski2aa58db2009-08-25 11:47:00 -0300584 dev_err(&icd->dev, "camera device read not implemented\n");
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300585
586 return err;
587}
588
589static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma)
590{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300591 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetski592c2ab2011-01-29 12:44:51 -0300592 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300593 int err;
594
595 dev_dbg(&icd->dev, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
596
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300597 if (icd->streamer != file)
598 return -EBUSY;
599
Guennadi Liakhovetski592c2ab2011-01-29 12:44:51 -0300600 if (ici->ops->init_videobuf)
601 err = videobuf_mmap_mapper(&icd->vb_vidq, vma);
602 else
603 err = vb2_mmap(&icd->vb2_vidq, vma);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300604
605 dev_dbg(&icd->dev, "vma start=0x%08lx, size=%ld, ret=%d\n",
606 (unsigned long)vma->vm_start,
607 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start,
608 err);
609
610 return err;
611}
612
613static unsigned int soc_camera_poll(struct file *file, poll_table *pt)
614{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300615 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -0300616 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300617
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300618 if (icd->streamer != file)
619 return -EBUSY;
620
Guennadi Liakhovetski592c2ab2011-01-29 12:44:51 -0300621 if (ici->ops->init_videobuf && list_empty(&icd->vb_vidq.stream)) {
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300622 dev_err(&icd->dev, "Trying to poll with no queued buffers!\n");
623 return POLLERR;
624 }
625
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300626 return ici->ops->poll(file, pt);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300627}
628
Guennadi Liakhovetski592c2ab2011-01-29 12:44:51 -0300629void soc_camera_lock(struct vb2_queue *vq)
630{
631 struct soc_camera_device *icd = vb2_get_drv_priv(vq);
632 mutex_lock(&icd->video_lock);
633}
634EXPORT_SYMBOL(soc_camera_lock);
635
636void soc_camera_unlock(struct vb2_queue *vq)
637{
638 struct soc_camera_device *icd = vb2_get_drv_priv(vq);
639 mutex_unlock(&icd->video_lock);
640}
641EXPORT_SYMBOL(soc_camera_unlock);
642
Hans Verkuilbec43662008-12-30 06:58:20 -0300643static struct v4l2_file_operations soc_camera_fops = {
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300644 .owner = THIS_MODULE,
645 .open = soc_camera_open,
646 .release = soc_camera_close,
Guennadi Liakhovetskib6a633c2010-12-25 17:40:26 -0300647 .unlocked_ioctl = video_ioctl2,
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300648 .read = soc_camera_read,
649 .mmap = soc_camera_mmap,
650 .poll = soc_camera_poll,
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300651};
652
Hans Verkuil72937892008-06-01 10:35:18 -0300653static int soc_camera_s_fmt_vid_cap(struct file *file, void *priv,
Guennadi Liakhovetskiabe4c472008-12-01 09:44:56 -0300654 struct v4l2_format *f)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300655{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300656 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300657 int ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300658
659 WARN_ON(priv != file->private_data);
660
Guennadi Liakhovetskid366d4a2011-03-21 06:33:25 -0300661 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
662 dev_warn(&icd->dev, "Wrong buf-type %d\n", f->type);
663 return -EINVAL;
664 }
665
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300666 if (icd->streamer && icd->streamer != file)
667 return -EBUSY;
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300668
Guennadi Liakhovetskiaee5c2f2011-03-28 13:28:28 -0300669 if (is_streaming(to_soc_camera_host(icd->dev.parent), icd)) {
Guennadi Liakhovetskib897a912009-08-25 11:46:53 -0300670 dev_err(&icd->dev, "S_FMT denied: queue initialised\n");
Guennadi Liakhovetskib6a633c2010-12-25 17:40:26 -0300671 return -EBUSY;
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300672 }
673
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300674 ret = soc_camera_set_fmt(icd, f);
675
676 if (!ret && !icd->streamer)
677 icd->streamer = file;
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300678
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300679 return ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300680}
681
Hans Verkuil72937892008-06-01 10:35:18 -0300682static int soc_camera_enum_fmt_vid_cap(struct file *file, void *priv,
Guennadi Liakhovetskiabe4c472008-12-01 09:44:56 -0300683 struct v4l2_fmtdesc *f)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300684{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300685 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300686 const struct soc_mbus_pixelfmt *format;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300687
688 WARN_ON(priv != file->private_data);
689
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300690 if (f->index >= icd->num_user_formats)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300691 return -EINVAL;
692
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300693 format = icd->user_formats[f->index].host_fmt;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300694
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300695 if (format->name)
696 strlcpy(f->description, format->name, sizeof(f->description));
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300697 f->pixelformat = format->fourcc;
698 return 0;
699}
700
Hans Verkuil72937892008-06-01 10:35:18 -0300701static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv,
Guennadi Liakhovetskiabe4c472008-12-01 09:44:56 -0300702 struct v4l2_format *f)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300703{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300704 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -0300705 struct v4l2_pix_format *pix = &f->fmt.pix;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300706
707 WARN_ON(priv != file->private_data);
708
Guennadi Liakhovetskid366d4a2011-03-21 06:33:25 -0300709 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
710 return -EINVAL;
711
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300712 pix->width = icd->user_width;
713 pix->height = icd->user_height;
Sergio Aguirre0e4c1802011-03-07 21:49:48 -0300714 pix->bytesperline = icd->bytesperline;
715 pix->sizeimage = icd->sizeimage;
Guennadi Liakhovetski592c2ab2011-01-29 12:44:51 -0300716 pix->field = icd->field;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300717 pix->pixelformat = icd->current_fmt->host_fmt->fourcc;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300718 pix->colorspace = icd->colorspace;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300719 dev_dbg(&icd->dev, "current_fmt->fourcc: 0x%08x\n",
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300720 icd->current_fmt->host_fmt->fourcc);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300721 return 0;
722}
723
724static int soc_camera_querycap(struct file *file, void *priv,
725 struct v4l2_capability *cap)
726{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300727 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -0300728 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300729
730 WARN_ON(priv != file->private_data);
731
732 strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver));
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300733 return ici->ops->querycap(ici, cap);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300734}
735
736static int soc_camera_streamon(struct file *file, void *priv,
737 enum v4l2_buf_type i)
738{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300739 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetski592c2ab2011-01-29 12:44:51 -0300740 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300741 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300742 int ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300743
744 WARN_ON(priv != file->private_data);
745
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300746 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
747 return -EINVAL;
748
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300749 if (icd->streamer != file)
750 return -EBUSY;
751
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300752 /* This calls buf_queue from host driver's videobuf_queue_ops */
Guennadi Liakhovetski592c2ab2011-01-29 12:44:51 -0300753 if (ici->ops->init_videobuf)
754 ret = videobuf_streamon(&icd->vb_vidq);
755 else
756 ret = vb2_streamon(&icd->vb2_vidq, i);
757
Anatolij Gustschin7fdbd852011-01-31 09:19:32 -0300758 if (!ret)
759 v4l2_subdev_call(sd, video, s_stream, 1);
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300760
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300761 return ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300762}
763
764static int soc_camera_streamoff(struct file *file, void *priv,
765 enum v4l2_buf_type i)
766{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300767 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300768 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski592c2ab2011-01-29 12:44:51 -0300769 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300770
771 WARN_ON(priv != file->private_data);
772
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300773 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
774 return -EINVAL;
775
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300776 if (icd->streamer != file)
777 return -EBUSY;
778
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300779 /*
780 * This calls buf_release from host driver's videobuf_queue_ops for all
781 * remaining buffers. When the last buffer is freed, stop capture
782 */
Guennadi Liakhovetski592c2ab2011-01-29 12:44:51 -0300783 if (ici->ops->init_videobuf)
784 videobuf_streamoff(&icd->vb_vidq);
785 else
786 vb2_streamoff(&icd->vb2_vidq, i);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300787
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300788 v4l2_subdev_call(sd, video, s_stream, 0);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300789
790 return 0;
791}
792
793static int soc_camera_queryctrl(struct file *file, void *priv,
794 struct v4l2_queryctrl *qc)
795{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300796 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetski2840d242009-08-25 11:44:15 -0300797 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300798 int i;
799
800 WARN_ON(priv != file->private_data);
801
802 if (!qc->id)
803 return -EINVAL;
804
Guennadi Liakhovetski2840d242009-08-25 11:44:15 -0300805 /* First check host controls */
806 for (i = 0; i < ici->ops->num_controls; i++)
807 if (qc->id == ici->ops->controls[i].id) {
808 memcpy(qc, &(ici->ops->controls[i]),
809 sizeof(*qc));
810 return 0;
811 }
812
813 /* Then device controls */
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300814 for (i = 0; i < icd->ops->num_controls; i++)
815 if (qc->id == icd->ops->controls[i].id) {
816 memcpy(qc, &(icd->ops->controls[i]),
817 sizeof(*qc));
818 return 0;
819 }
820
821 return -EINVAL;
822}
823
824static int soc_camera_g_ctrl(struct file *file, void *priv,
825 struct v4l2_control *ctrl)
826{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300827 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300828 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300829 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski2840d242009-08-25 11:44:15 -0300830 int ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300831
832 WARN_ON(priv != file->private_data);
833
Guennadi Liakhovetski2840d242009-08-25 11:44:15 -0300834 if (ici->ops->get_ctrl) {
835 ret = ici->ops->get_ctrl(icd, ctrl);
836 if (ret != -ENOIOCTLCMD)
837 return ret;
838 }
839
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300840 return v4l2_subdev_call(sd, core, g_ctrl, ctrl);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300841}
842
843static int soc_camera_s_ctrl(struct file *file, void *priv,
844 struct v4l2_control *ctrl)
845{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300846 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300847 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300848 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski2840d242009-08-25 11:44:15 -0300849 int ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300850
851 WARN_ON(priv != file->private_data);
852
Guennadi Liakhovetski2840d242009-08-25 11:44:15 -0300853 if (ici->ops->set_ctrl) {
854 ret = ici->ops->set_ctrl(icd, ctrl);
855 if (ret != -ENOIOCTLCMD)
856 return ret;
857 }
858
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300859 return v4l2_subdev_call(sd, core, s_ctrl, ctrl);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300860}
861
862static int soc_camera_cropcap(struct file *file, void *fh,
863 struct v4l2_cropcap *a)
864{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300865 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300866 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300867
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300868 return ici->ops->cropcap(icd, a);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300869}
870
871static int soc_camera_g_crop(struct file *file, void *fh,
872 struct v4l2_crop *a)
873{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300874 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300875 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
876 int ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300877
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300878 ret = ici->ops->get_crop(icd, a);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300879
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300880 return ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300881}
882
Guennadi Liakhovetski68a54f02009-08-25 11:46:52 -0300883/*
884 * According to the V4L2 API, drivers shall not update the struct v4l2_crop
885 * argument with the actual geometry, instead, the user shall use G_CROP to
Guennadi Liakhovetskiab56d5e2010-03-23 11:31:46 -0300886 * retrieve it.
Guennadi Liakhovetski68a54f02009-08-25 11:46:52 -0300887 */
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300888static int soc_camera_s_crop(struct file *file, void *fh,
889 struct v4l2_crop *a)
890{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300891 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -0300892 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300893 struct v4l2_rect *rect = &a->c;
894 struct v4l2_crop current_crop;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300895 int ret;
896
897 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
898 return -EINVAL;
899
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300900 dev_dbg(&icd->dev, "S_CROP(%ux%u@%u:%u)\n",
901 rect->width, rect->height, rect->left, rect->top);
902
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300903 /* If get_crop fails, we'll let host and / or client drivers decide */
904 ret = ici->ops->get_crop(icd, &current_crop);
905
Guennadi Liakhovetskib897a912009-08-25 11:46:53 -0300906 /* Prohibit window size change with initialised buffers */
Guennadi Liakhovetski103754a2010-08-05 19:23:44 -0300907 if (ret < 0) {
908 dev_err(&icd->dev,
909 "S_CROP denied: getting current crop failed\n");
Guennadi Liakhovetskiaee5c2f2011-03-28 13:28:28 -0300910 } else if ((a->c.width == current_crop.c.width &&
911 a->c.height == current_crop.c.height) ||
912 !is_streaming(ici, icd)) {
913 /* same size or not streaming - use .set_crop() */
914 ret = ici->ops->set_crop(icd, a);
915 } else if (ici->ops->set_livecrop) {
916 ret = ici->ops->set_livecrop(icd, a);
917 } else {
Guennadi Liakhovetskib897a912009-08-25 11:46:53 -0300918 dev_err(&icd->dev,
919 "S_CROP denied: queue initialised and sizes differ\n");
920 ret = -EBUSY;
Guennadi Liakhovetskib897a912009-08-25 11:46:53 -0300921 }
922
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300923 return ret;
924}
925
Guennadi Liakhovetskic9f6ef62010-02-09 18:00:30 +0100926static int soc_camera_g_parm(struct file *file, void *fh,
927 struct v4l2_streamparm *a)
928{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300929 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetskic9f6ef62010-02-09 18:00:30 +0100930 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
931
932 if (ici->ops->get_parm)
933 return ici->ops->get_parm(icd, a);
934
935 return -ENOIOCTLCMD;
936}
937
938static int soc_camera_s_parm(struct file *file, void *fh,
939 struct v4l2_streamparm *a)
940{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300941 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetskic9f6ef62010-02-09 18:00:30 +0100942 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
943
944 if (ici->ops->set_parm)
945 return ici->ops->set_parm(icd, a);
946
947 return -ENOIOCTLCMD;
948}
949
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300950static int soc_camera_g_chip_ident(struct file *file, void *fh,
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300951 struct v4l2_dbg_chip_ident *id)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300952{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300953 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300954 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300955
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300956 return v4l2_subdev_call(sd, core, g_chip_ident, id);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300957}
958
959#ifdef CONFIG_VIDEO_ADV_DEBUG
960static int soc_camera_g_register(struct file *file, void *fh,
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300961 struct v4l2_dbg_register *reg)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300962{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300963 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300964 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300965
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300966 return v4l2_subdev_call(sd, core, g_register, reg);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300967}
968
969static int soc_camera_s_register(struct file *file, void *fh,
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300970 struct v4l2_dbg_register *reg)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300971{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300972 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300973 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300974
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300975 return v4l2_subdev_call(sd, core, s_register, reg);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300976}
977#endif
978
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300979/* So far this function cannot fail */
980static void scan_add_host(struct soc_camera_host *ici)
981{
982 struct soc_camera_device *icd;
983
984 mutex_lock(&list_lock);
985
986 list_for_each_entry(icd, &devices, list) {
987 if (icd->iface == ici->nr) {
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300988 int ret;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300989 icd->dev.parent = ici->v4l2_dev.dev;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300990 dev_set_name(&icd->dev, "%u-%u", icd->iface,
991 icd->devnum);
992 ret = device_register(&icd->dev);
993 if (ret < 0) {
994 icd->dev.parent = NULL;
995 dev_err(&icd->dev,
996 "Cannot register device: %d\n", ret);
997 }
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300998 }
999 }
1000
1001 mutex_unlock(&list_lock);
1002}
1003
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001004#ifdef CONFIG_I2C_BOARDINFO
1005static int soc_camera_init_i2c(struct soc_camera_device *icd,
1006 struct soc_camera_link *icl)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001007{
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001008 struct i2c_client *client;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001009 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001010 struct i2c_adapter *adap = i2c_get_adapter(icl->i2c_adapter_id);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001011 struct v4l2_subdev *subdev;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001012
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001013 if (!adap) {
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001014 dev_err(&icd->dev, "Cannot get I2C adapter #%d. No driver?\n",
1015 icl->i2c_adapter_id);
1016 goto ei2cga;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001017 }
1018
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001019 icl->board_info->platform_data = icd;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001020
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001021 subdev = v4l2_i2c_new_subdev_board(&ici->v4l2_dev, adap,
Laurent Pinchart9a1f8b32010-09-24 10:16:44 -03001022 icl->board_info, NULL);
Magnus Dammd74f8412010-02-09 17:40:40 +09001023 if (!subdev)
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001024 goto ei2cnd;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001025
Laurent Pinchartc4ce6d12010-07-30 17:24:54 -03001026 client = v4l2_get_subdevdata(subdev);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001027
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001028 /* Use to_i2c_client(dev) to recover the i2c client */
1029 dev_set_drvdata(&icd->dev, &client->dev);
1030
1031 return 0;
1032ei2cnd:
1033 i2c_put_adapter(adap);
1034ei2cga:
Magnus Dammd74f8412010-02-09 17:40:40 +09001035 return -ENODEV;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001036}
1037
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001038static void soc_camera_free_i2c(struct soc_camera_device *icd)
1039{
1040 struct i2c_client *client =
1041 to_i2c_client(to_soc_camera_control(icd));
Guennadi Liakhovetskic8dd7072011-03-23 05:26:11 -03001042 struct i2c_adapter *adap = client->adapter;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001043 dev_set_drvdata(&icd->dev, NULL);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001044 v4l2_device_unregister_subdev(i2c_get_clientdata(client));
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001045 i2c_unregister_device(client);
Guennadi Liakhovetskic8dd7072011-03-23 05:26:11 -03001046 i2c_put_adapter(adap);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001047}
1048#else
1049#define soc_camera_init_i2c(icd, icl) (-ENODEV)
1050#define soc_camera_free_i2c(icd) do {} while (0)
1051#endif
1052
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001053static int soc_camera_video_start(struct soc_camera_device *icd);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001054static int video_dev_create(struct soc_camera_device *icd);
1055/* Called during host-driver probe */
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001056static int soc_camera_probe(struct device *dev)
1057{
1058 struct soc_camera_device *icd = to_soc_camera_dev(dev);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001059 struct soc_camera_host *ici = to_soc_camera_host(dev->parent);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001060 struct soc_camera_link *icl = to_soc_camera_link(icd);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001061 struct device *control = NULL;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001062 struct v4l2_subdev *sd;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001063 struct v4l2_mbus_framefmt mf;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001064 int ret;
1065
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001066 dev_info(dev, "Probing %s\n", dev_name(dev));
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -03001067
Alberto Panizzo96e442c2010-12-02 07:43:37 -03001068 ret = regulator_bulk_get(icd->pdev, icl->num_regulators,
1069 icl->regulators);
1070 if (ret < 0)
1071 goto ereg;
1072
1073 ret = soc_camera_power_set(icd, icl, 1);
1074 if (ret < 0)
1075 goto epower;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001076
1077 /* The camera could have been already on, try to reset */
1078 if (icl->reset)
1079 icl->reset(icd->pdev);
1080
1081 ret = ici->ops->add(icd);
1082 if (ret < 0)
1083 goto eadd;
1084
1085 /* Must have icd->vdev before registering the device */
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001086 ret = video_dev_create(icd);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001087 if (ret < 0)
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001088 goto evdc;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001089
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001090 /* Non-i2c cameras, e.g., soc_camera_platform, have no board_info */
1091 if (icl->board_info) {
1092 ret = soc_camera_init_i2c(icd, icl);
1093 if (ret < 0)
1094 goto eadddev;
1095 } else if (!icl->add_device || !icl->del_device) {
1096 ret = -EINVAL;
1097 goto eadddev;
1098 } else {
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001099 if (icl->module_name)
1100 ret = request_module(icl->module_name);
1101
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001102 ret = icl->add_device(icl, &icd->dev);
1103 if (ret < 0)
1104 goto eadddev;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001105
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -03001106 /*
1107 * FIXME: this is racy, have to use driver-binding notification,
1108 * when it is available
1109 */
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001110 control = to_soc_camera_control(icd);
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -03001111 if (!control || !control->driver || !dev_get_drvdata(control) ||
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001112 !try_module_get(control->driver->owner)) {
1113 icl->del_device(icl);
1114 goto enodrv;
1115 }
Guennadi Liakhovetski025c18a2009-03-13 06:08:20 -03001116 }
1117
Guennadi Liakhovetski24105eb2011-03-23 05:41:28 -03001118 sd = soc_camera_to_subdev(icd);
1119 sd->grp_id = (long)icd;
1120
Guennadi Liakhovetskifa489842009-08-25 11:46:43 -03001121 /* At this point client .probe() should have run already */
1122 ret = soc_camera_init_user_formats(icd);
1123 if (ret < 0)
1124 goto eiufmt;
1125
Guennadi Liakhovetskifa489842009-08-25 11:46:43 -03001126 icd->field = V4L2_FIELD_ANY;
1127
Guennadi Liakhovetskib6a633c2010-12-25 17:40:26 -03001128 icd->vdev->lock = &icd->video_lock;
1129
1130 /*
1131 * ..._video_start() will create a device node, video_register_device()
1132 * itself is protected against concurrent open() calls, but we also have
1133 * to protect our data.
1134 */
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001135 mutex_lock(&icd->video_lock);
1136
1137 ret = soc_camera_video_start(icd);
1138 if (ret < 0)
1139 goto evidstart;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001140
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001141 /* Try to improve our guess of a reasonable window format */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001142 if (!v4l2_subdev_call(sd, video, g_mbus_fmt, &mf)) {
1143 icd->user_width = mf.width;
1144 icd->user_height = mf.height;
1145 icd->colorspace = mf.colorspace;
1146 icd->field = mf.field;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001147 }
1148
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001149 /* Do we have to sysfs_remove_link() before device_unregister()? */
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001150 if (sysfs_create_link(&icd->dev.kobj, &to_soc_camera_control(icd)->kobj,
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001151 "control"))
1152 dev_warn(&icd->dev, "Failed creating the control symlink\n");
1153
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001154 ici->ops->remove(icd);
1155
Alberto Panizzo96e442c2010-12-02 07:43:37 -03001156 soc_camera_power_set(icd, icl, 0);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001157
1158 mutex_unlock(&icd->video_lock);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001159
1160 return 0;
1161
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001162evidstart:
1163 mutex_unlock(&icd->video_lock);
Guennadi Liakhovetskifa489842009-08-25 11:46:43 -03001164 soc_camera_free_user_formats(icd);
1165eiufmt:
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001166 if (icl->board_info) {
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001167 soc_camera_free_i2c(icd);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001168 } else {
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001169 icl->del_device(icl);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001170 module_put(control->driver->owner);
1171 }
1172enodrv:
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001173eadddev:
1174 video_device_release(icd->vdev);
1175evdc:
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001176 ici->ops->remove(icd);
1177eadd:
Alberto Panizzo96e442c2010-12-02 07:43:37 -03001178 soc_camera_power_set(icd, icl, 0);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001179epower:
Alberto Panizzo96e442c2010-12-02 07:43:37 -03001180 regulator_bulk_free(icl->num_regulators, icl->regulators);
1181ereg:
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001182 return ret;
1183}
1184
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -03001185/*
1186 * This is called on device_unregister, which only means we have to disconnect
1187 * from the host, but not remove ourselves from the device list
1188 */
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001189static int soc_camera_remove(struct device *dev)
1190{
1191 struct soc_camera_device *icd = to_soc_camera_dev(dev);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001192 struct soc_camera_link *icl = to_soc_camera_link(icd);
1193 struct video_device *vdev = icd->vdev;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001194
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001195 BUG_ON(!dev->parent);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001196
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001197 if (vdev) {
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001198 video_unregister_device(vdev);
1199 icd->vdev = NULL;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001200 }
1201
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001202 if (icl->board_info) {
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001203 soc_camera_free_i2c(icd);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001204 } else {
1205 struct device_driver *drv = to_soc_camera_control(icd) ?
1206 to_soc_camera_control(icd)->driver : NULL;
1207 if (drv) {
1208 icl->del_device(icl);
1209 module_put(drv->owner);
1210 }
1211 }
Guennadi Liakhovetskifa489842009-08-25 11:46:43 -03001212 soc_camera_free_user_formats(icd);
Guennadi Liakhovetski025c18a2009-03-13 06:08:20 -03001213
Alberto Panizzo96e442c2010-12-02 07:43:37 -03001214 regulator_bulk_free(icl->num_regulators, icl->regulators);
1215
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001216 return 0;
1217}
1218
Robert Jarzmik2e521062008-08-01 20:14:50 -03001219static int soc_camera_suspend(struct device *dev, pm_message_t state)
1220{
1221 struct soc_camera_device *icd = to_soc_camera_dev(dev);
1222 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1223 int ret = 0;
1224
1225 if (ici->ops->suspend)
1226 ret = ici->ops->suspend(icd, state);
1227
1228 return ret;
1229}
1230
1231static int soc_camera_resume(struct device *dev)
1232{
1233 struct soc_camera_device *icd = to_soc_camera_dev(dev);
1234 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1235 int ret = 0;
1236
1237 if (ici->ops->resume)
1238 ret = ici->ops->resume(icd);
1239
1240 return ret;
1241}
1242
Guennadi Liakhovetski52d268a2010-07-26 11:37:13 -03001243struct bus_type soc_camera_bus_type = {
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001244 .name = "soc-camera",
1245 .probe = soc_camera_probe,
1246 .remove = soc_camera_remove,
Robert Jarzmik2e521062008-08-01 20:14:50 -03001247 .suspend = soc_camera_suspend,
1248 .resume = soc_camera_resume,
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001249};
Guennadi Liakhovetski52d268a2010-07-26 11:37:13 -03001250EXPORT_SYMBOL_GPL(soc_camera_bus_type);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001251
1252static struct device_driver ic_drv = {
1253 .name = "camera",
1254 .bus = &soc_camera_bus_type,
1255 .owner = THIS_MODULE,
1256};
1257
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001258static void dummy_release(struct device *dev)
1259{
1260}
1261
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001262static int default_cropcap(struct soc_camera_device *icd,
1263 struct v4l2_cropcap *a)
1264{
1265 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1266 return v4l2_subdev_call(sd, video, cropcap, a);
1267}
1268
1269static int default_g_crop(struct soc_camera_device *icd, struct v4l2_crop *a)
1270{
1271 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1272 return v4l2_subdev_call(sd, video, g_crop, a);
1273}
1274
1275static int default_s_crop(struct soc_camera_device *icd, struct v4l2_crop *a)
1276{
1277 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1278 return v4l2_subdev_call(sd, video, s_crop, a);
1279}
1280
Janusz Krzysztofik06e17822010-09-10 22:26:16 -03001281static int default_g_parm(struct soc_camera_device *icd,
1282 struct v4l2_streamparm *parm)
1283{
1284 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1285 return v4l2_subdev_call(sd, video, g_parm, parm);
1286}
1287
1288static int default_s_parm(struct soc_camera_device *icd,
1289 struct v4l2_streamparm *parm)
1290{
1291 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1292 return v4l2_subdev_call(sd, video, s_parm, parm);
1293}
1294
Qing Xued5b65d2011-01-20 05:19:40 -03001295static int default_enum_fsizes(struct soc_camera_device *icd,
1296 struct v4l2_frmsizeenum *fsize)
1297{
1298 int ret;
1299 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1300 const struct soc_camera_format_xlate *xlate;
1301 __u32 pixfmt = fsize->pixel_format;
1302 struct v4l2_frmsizeenum fsize_mbus = *fsize;
1303
1304 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1305 if (!xlate)
1306 return -EINVAL;
1307 /* map xlate-code to pixel_format, sensor only handle xlate-code*/
1308 fsize_mbus.pixel_format = xlate->code;
1309
1310 ret = v4l2_subdev_call(sd, video, enum_mbus_fsizes, &fsize_mbus);
1311 if (ret < 0)
1312 return ret;
1313
1314 *fsize = fsize_mbus;
1315 fsize->pixel_format = pixfmt;
1316
1317 return 0;
1318}
1319
Guennadi Liakhovetski64ff9ba2009-11-09 16:12:37 -03001320static void soc_camera_device_init(struct device *dev, void *pdata)
1321{
1322 dev->platform_data = pdata;
1323 dev->bus = &soc_camera_bus_type;
1324 dev->release = dummy_release;
1325}
1326
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -03001327int soc_camera_host_register(struct soc_camera_host *ici)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001328{
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001329 struct soc_camera_host *ix;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001330 int ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001331
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -03001332 if (!ici || !ici->ops ||
1333 !ici->ops->try_fmt ||
1334 !ici->ops->set_fmt ||
1335 !ici->ops->set_bus_param ||
1336 !ici->ops->querycap ||
Guennadi Liakhovetski592c2ab2011-01-29 12:44:51 -03001337 ((!ici->ops->init_videobuf ||
1338 !ici->ops->reqbufs) &&
1339 !ici->ops->init_videobuf2) ||
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -03001340 !ici->ops->add ||
1341 !ici->ops->remove ||
Guennadi Liakhovetskieff505f2009-04-24 12:55:48 -03001342 !ici->ops->poll ||
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001343 !ici->v4l2_dev.dev)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001344 return -EINVAL;
1345
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001346 if (!ici->ops->set_crop)
1347 ici->ops->set_crop = default_s_crop;
1348 if (!ici->ops->get_crop)
1349 ici->ops->get_crop = default_g_crop;
1350 if (!ici->ops->cropcap)
1351 ici->ops->cropcap = default_cropcap;
Janusz Krzysztofik06e17822010-09-10 22:26:16 -03001352 if (!ici->ops->set_parm)
1353 ici->ops->set_parm = default_s_parm;
1354 if (!ici->ops->get_parm)
1355 ici->ops->get_parm = default_g_parm;
Qing Xued5b65d2011-01-20 05:19:40 -03001356 if (!ici->ops->enum_fsizes)
1357 ici->ops->enum_fsizes = default_enum_fsizes;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001358
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001359 mutex_lock(&list_lock);
1360 list_for_each_entry(ix, &hosts, list) {
1361 if (ix->nr == ici->nr) {
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001362 ret = -EBUSY;
1363 goto edevreg;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001364 }
1365 }
1366
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001367 ret = v4l2_device_register(ici->v4l2_dev.dev, &ici->v4l2_dev);
1368 if (ret < 0)
1369 goto edevreg;
Guennadi Liakhovetskieff505f2009-04-24 12:55:48 -03001370
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001371 list_add_tail(&ici->list, &hosts);
1372 mutex_unlock(&list_lock);
1373
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001374 scan_add_host(ici);
1375
1376 return 0;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001377
1378edevreg:
1379 mutex_unlock(&list_lock);
1380 return ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001381}
1382EXPORT_SYMBOL(soc_camera_host_register);
1383
1384/* Unregister all clients! */
1385void soc_camera_host_unregister(struct soc_camera_host *ici)
1386{
1387 struct soc_camera_device *icd;
1388
1389 mutex_lock(&list_lock);
1390
1391 list_del(&ici->list);
1392
1393 list_for_each_entry(icd, &devices, list) {
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001394 if (icd->iface == ici->nr) {
Guennadi Liakhovetski64ff9ba2009-11-09 16:12:37 -03001395 void *pdata = icd->dev.platform_data;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001396 /* The bus->remove will be called */
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001397 device_unregister(&icd->dev);
Guennadi Liakhovetski76823b72009-10-05 12:54:54 -03001398 /*
1399 * Not before device_unregister(), .remove
1400 * needs parent to call ici->ops->remove().
1401 * If the host module is loaded again, device_register()
1402 * would complain "already initialised," since 2.6.32
1403 * this is also needed to prevent use-after-free of the
1404 * device private data.
1405 */
1406 memset(&icd->dev, 0, sizeof(icd->dev));
Guennadi Liakhovetski64ff9ba2009-11-09 16:12:37 -03001407 soc_camera_device_init(&icd->dev, pdata);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001408 }
1409 }
1410
1411 mutex_unlock(&list_lock);
1412
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001413 v4l2_device_unregister(&ici->v4l2_dev);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001414}
1415EXPORT_SYMBOL(soc_camera_host_unregister);
1416
1417/* Image capture device */
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001418static int soc_camera_device_register(struct soc_camera_device *icd)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001419{
1420 struct soc_camera_device *ix;
1421 int num = -1, i;
1422
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001423 for (i = 0; i < 256 && num < 0; i++) {
1424 num = i;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001425 /* Check if this index is available on this interface */
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001426 list_for_each_entry(ix, &devices, list) {
1427 if (ix->iface == icd->iface && ix->devnum == i) {
1428 num = -1;
1429 break;
1430 }
1431 }
1432 }
1433
1434 if (num < 0)
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -03001435 /*
1436 * ok, we have 256 cameras on this host...
1437 * man, stay reasonable...
1438 */
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001439 return -ENOMEM;
1440
Guennadi Liakhovetski64ff9ba2009-11-09 16:12:37 -03001441 icd->devnum = num;
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -03001442 icd->use_count = 0;
1443 icd->host_priv = NULL;
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -03001444 mutex_init(&icd->video_lock);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001445
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001446 list_add_tail(&icd->list, &devices);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001447
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001448 return 0;
1449}
1450
1451static void soc_camera_device_unregister(struct soc_camera_device *icd)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001452{
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001453 list_del(&icd->list);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001454}
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001455
Hans Verkuila3998102008-07-21 02:57:38 -03001456static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
1457 .vidioc_querycap = soc_camera_querycap,
1458 .vidioc_g_fmt_vid_cap = soc_camera_g_fmt_vid_cap,
1459 .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
1460 .vidioc_s_fmt_vid_cap = soc_camera_s_fmt_vid_cap,
1461 .vidioc_enum_input = soc_camera_enum_input,
1462 .vidioc_g_input = soc_camera_g_input,
1463 .vidioc_s_input = soc_camera_s_input,
1464 .vidioc_s_std = soc_camera_s_std,
Qing Xued5b65d2011-01-20 05:19:40 -03001465 .vidioc_enum_framesizes = soc_camera_enum_fsizes,
Hans Verkuila3998102008-07-21 02:57:38 -03001466 .vidioc_reqbufs = soc_camera_reqbufs,
1467 .vidioc_try_fmt_vid_cap = soc_camera_try_fmt_vid_cap,
1468 .vidioc_querybuf = soc_camera_querybuf,
1469 .vidioc_qbuf = soc_camera_qbuf,
1470 .vidioc_dqbuf = soc_camera_dqbuf,
1471 .vidioc_streamon = soc_camera_streamon,
1472 .vidioc_streamoff = soc_camera_streamoff,
1473 .vidioc_queryctrl = soc_camera_queryctrl,
1474 .vidioc_g_ctrl = soc_camera_g_ctrl,
1475 .vidioc_s_ctrl = soc_camera_s_ctrl,
1476 .vidioc_cropcap = soc_camera_cropcap,
1477 .vidioc_g_crop = soc_camera_g_crop,
1478 .vidioc_s_crop = soc_camera_s_crop,
Guennadi Liakhovetskic9f6ef62010-02-09 18:00:30 +01001479 .vidioc_g_parm = soc_camera_g_parm,
1480 .vidioc_s_parm = soc_camera_s_parm,
Hans Verkuila3998102008-07-21 02:57:38 -03001481 .vidioc_g_chip_ident = soc_camera_g_chip_ident,
1482#ifdef CONFIG_VIDEO_ADV_DEBUG
1483 .vidioc_g_register = soc_camera_g_register,
1484 .vidioc_s_register = soc_camera_s_register,
1485#endif
1486};
1487
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001488static int video_dev_create(struct soc_camera_device *icd)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001489{
1490 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001491 struct video_device *vdev = video_device_alloc();
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001492
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001493 if (!vdev)
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001494 return -ENOMEM;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001495
1496 strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -03001497
Hans Verkuil5e85e732008-07-20 06:31:39 -03001498 vdev->parent = &icd->dev;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001499 vdev->current_norm = V4L2_STD_UNKNOWN;
1500 vdev->fops = &soc_camera_fops;
Hans Verkuila3998102008-07-21 02:57:38 -03001501 vdev->ioctl_ops = &soc_camera_ioctl_ops;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001502 vdev->release = video_device_release;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001503 vdev->tvnorms = V4L2_STD_UNKNOWN;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001504
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001505 icd->vdev = vdev;
1506
1507 return 0;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001508}
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001509
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001510/*
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001511 * Called from soc_camera_probe() above (with .video_lock held???)
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001512 */
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001513static int soc_camera_video_start(struct soc_camera_device *icd)
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001514{
Mauro Carvalho Chehab4f9fb5e2010-05-18 00:46:09 -03001515 struct device_type *type = icd->vdev->dev.type;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001516 int ret;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001517
1518 if (!icd->dev.parent)
1519 return -ENODEV;
1520
1521 if (!icd->ops ||
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001522 !icd->ops->query_bus_param ||
1523 !icd->ops->set_bus_param)
1524 return -EINVAL;
1525
Laurent Pinchart46b21092009-12-10 11:51:38 -02001526 ret = video_register_device(icd->vdev, VFL_TYPE_GRABBER, -1);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001527 if (ret < 0) {
1528 dev_err(&icd->dev, "video_register_device failed: %d\n", ret);
1529 return ret;
1530 }
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001531
Mauro Carvalho Chehab4f9fb5e2010-05-18 00:46:09 -03001532 /* Restore device type, possibly set by the subdevice driver */
1533 icd->vdev->dev.type = type;
1534
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001535 return 0;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001536}
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001537
Guennadi Liakhovetskic41deba2009-08-25 11:06:21 -03001538static int __devinit soc_camera_pdrv_probe(struct platform_device *pdev)
1539{
1540 struct soc_camera_link *icl = pdev->dev.platform_data;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001541 struct soc_camera_device *icd;
1542 int ret;
Guennadi Liakhovetskic41deba2009-08-25 11:06:21 -03001543
1544 if (!icl)
1545 return -EINVAL;
1546
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001547 icd = kzalloc(sizeof(*icd), GFP_KERNEL);
1548 if (!icd)
1549 return -ENOMEM;
Guennadi Liakhovetskic41deba2009-08-25 11:06:21 -03001550
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001551 icd->iface = icl->bus_id;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001552 icd->pdev = &pdev->dev;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001553 platform_set_drvdata(pdev, icd);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001554
1555 ret = soc_camera_device_register(icd);
1556 if (ret < 0)
1557 goto escdevreg;
1558
Guennadi Liakhovetski64ff9ba2009-11-09 16:12:37 -03001559 soc_camera_device_init(&icd->dev, icl);
1560
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001561 icd->user_width = DEFAULT_WIDTH;
1562 icd->user_height = DEFAULT_HEIGHT;
1563
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001564 return 0;
1565
1566escdevreg:
1567 kfree(icd);
1568
1569 return ret;
Guennadi Liakhovetskic41deba2009-08-25 11:06:21 -03001570}
1571
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -03001572/*
1573 * Only called on rmmod for each platform device, since they are not
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001574 * hot-pluggable. Now we know, that all our users - hosts and devices have
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -03001575 * been unloaded already
1576 */
Guennadi Liakhovetskic41deba2009-08-25 11:06:21 -03001577static int __devexit soc_camera_pdrv_remove(struct platform_device *pdev)
1578{
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001579 struct soc_camera_device *icd = platform_get_drvdata(pdev);
Guennadi Liakhovetskic41deba2009-08-25 11:06:21 -03001580
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001581 if (!icd)
1582 return -EINVAL;
1583
1584 soc_camera_device_unregister(icd);
1585
1586 kfree(icd);
1587
Guennadi Liakhovetski0fd327b2009-05-07 13:25:32 -03001588 return 0;
1589}
1590
1591static struct platform_driver __refdata soc_camera_pdrv = {
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001592 .remove = __devexit_p(soc_camera_pdrv_remove),
1593 .driver = {
1594 .name = "soc-camera-pdrv",
1595 .owner = THIS_MODULE,
Guennadi Liakhovetski0fd327b2009-05-07 13:25:32 -03001596 },
1597};
1598
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001599static int __init soc_camera_init(void)
1600{
1601 int ret = bus_register(&soc_camera_bus_type);
1602 if (ret)
1603 return ret;
1604 ret = driver_register(&ic_drv);
1605 if (ret)
1606 goto edrvr;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001607
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001608 ret = platform_driver_probe(&soc_camera_pdrv, soc_camera_pdrv_probe);
Guennadi Liakhovetski0fd327b2009-05-07 13:25:32 -03001609 if (ret)
1610 goto epdr;
1611
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001612 return 0;
1613
Guennadi Liakhovetski0fd327b2009-05-07 13:25:32 -03001614epdr:
1615 driver_unregister(&ic_drv);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001616edrvr:
1617 bus_unregister(&soc_camera_bus_type);
1618 return ret;
1619}
1620
1621static void __exit soc_camera_exit(void)
1622{
Guennadi Liakhovetski0fd327b2009-05-07 13:25:32 -03001623 platform_driver_unregister(&soc_camera_pdrv);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001624 driver_unregister(&ic_drv);
1625 bus_unregister(&soc_camera_bus_type);
1626}
1627
1628module_init(soc_camera_init);
1629module_exit(soc_camera_exit);
1630
1631MODULE_DESCRIPTION("Image capture bus driver");
1632MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
1633MODULE_LICENSE("GPL");
Guennadi Liakhovetski0fd327b2009-05-07 13:25:32 -03001634MODULE_ALIAS("platform:soc-camera-pdrv");