blob: aa6614b60d6f4ed4485c9d7075f3bf7b523d1b70 [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>
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -030027#include <linux/vmalloc.h>
28
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -030029#include <media/soc_camera.h>
Guennadi Liakhovetski0fd327b2009-05-07 13:25:32 -030030#include <media/v4l2-common.h>
Guennadi Liakhovetski0fd327b2009-05-07 13:25:32 -030031#include <media/v4l2-ioctl.h>
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -030032#include <media/v4l2-dev.h>
Guennadi Liakhovetski0fd327b2009-05-07 13:25:32 -030033#include <media/videobuf-core.h>
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -030034
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -030035/* Default to VGA resolution */
36#define DEFAULT_WIDTH 640
37#define DEFAULT_HEIGHT 480
38
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -030039static LIST_HEAD(hosts);
40static LIST_HEAD(devices);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -030041static DEFINE_MUTEX(list_lock); /* Protects the list of hosts */
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -030042
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -030043const struct soc_camera_data_format *soc_camera_format_by_fourcc(
Guennadi Liakhovetskiabe4c472008-12-01 09:44:56 -030044 struct soc_camera_device *icd, unsigned int fourcc)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -030045{
46 unsigned int i;
47
Guennadi Liakhovetski26f1b942008-03-24 12:18:36 -030048 for (i = 0; i < icd->num_formats; i++)
49 if (icd->formats[i].fourcc == fourcc)
50 return icd->formats + i;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -030051 return NULL;
52}
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -030053EXPORT_SYMBOL(soc_camera_format_by_fourcc);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -030054
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -030055const struct soc_camera_format_xlate *soc_camera_xlate_by_fourcc(
56 struct soc_camera_device *icd, unsigned int fourcc)
57{
58 unsigned int i;
59
60 for (i = 0; i < icd->num_user_formats; i++)
61 if (icd->user_formats[i].host_fmt->fourcc == fourcc)
62 return icd->user_formats + i;
63 return NULL;
64}
65EXPORT_SYMBOL(soc_camera_xlate_by_fourcc);
66
Guennadi Liakhovetskibd73b362008-12-23 05:54:45 -030067/**
68 * soc_camera_apply_sensor_flags() - apply platform SOCAM_SENSOR_INVERT_* flags
69 * @icl: camera platform parameters
70 * @flags: flags to be inverted according to platform configuration
71 * @return: resulting flags
72 */
73unsigned long soc_camera_apply_sensor_flags(struct soc_camera_link *icl,
74 unsigned long flags)
75{
76 unsigned long f;
77
78 /* If only one of the two polarities is supported, switch to the opposite */
79 if (icl->flags & SOCAM_SENSOR_INVERT_HSYNC) {
80 f = flags & (SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW);
81 if (f == SOCAM_HSYNC_ACTIVE_HIGH || f == SOCAM_HSYNC_ACTIVE_LOW)
82 flags ^= SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW;
83 }
84
85 if (icl->flags & SOCAM_SENSOR_INVERT_VSYNC) {
86 f = flags & (SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW);
87 if (f == SOCAM_VSYNC_ACTIVE_HIGH || f == SOCAM_VSYNC_ACTIVE_LOW)
88 flags ^= SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW;
89 }
90
91 if (icl->flags & SOCAM_SENSOR_INVERT_PCLK) {
92 f = flags & (SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING);
93 if (f == SOCAM_PCLK_SAMPLE_RISING || f == SOCAM_PCLK_SAMPLE_FALLING)
94 flags ^= SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING;
95 }
96
97 return flags;
98}
99EXPORT_SYMBOL(soc_camera_apply_sensor_flags);
100
Hans Verkuil72937892008-06-01 10:35:18 -0300101static int soc_camera_try_fmt_vid_cap(struct file *file, void *priv,
Guennadi Liakhovetskiabe4c472008-12-01 09:44:56 -0300102 struct v4l2_format *f)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300103{
104 struct soc_camera_file *icf = file->private_data;
105 struct soc_camera_device *icd = icf->icd;
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -0300106 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300107
108 WARN_ON(priv != file->private_data);
109
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300110 /* limit format to hardware capabilities */
Guennadi Liakhovetski06daa1a2008-12-18 12:52:08 -0300111 return ici->ops->try_fmt(icd, f);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300112}
113
114static int soc_camera_enum_input(struct file *file, void *priv,
115 struct v4l2_input *inp)
116{
Kuninori Morimoto34d359d2008-12-18 12:47:46 -0300117 struct soc_camera_file *icf = file->private_data;
118 struct soc_camera_device *icd = icf->icd;
119 int ret = 0;
120
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300121 if (inp->index != 0)
122 return -EINVAL;
123
Kuninori Morimoto34d359d2008-12-18 12:47:46 -0300124 if (icd->ops->enum_input)
125 ret = icd->ops->enum_input(icd, inp);
126 else {
127 /* default is camera */
128 inp->type = V4L2_INPUT_TYPE_CAMERA;
129 inp->std = V4L2_STD_UNKNOWN;
130 strcpy(inp->name, "Camera");
131 }
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300132
Kuninori Morimoto34d359d2008-12-18 12:47:46 -0300133 return ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300134}
135
136static int soc_camera_g_input(struct file *file, void *priv, unsigned int *i)
137{
138 *i = 0;
139
140 return 0;
141}
142
143static int soc_camera_s_input(struct file *file, void *priv, unsigned int i)
144{
145 if (i > 0)
146 return -EINVAL;
147
148 return 0;
149}
150
151static int soc_camera_s_std(struct file *file, void *priv, v4l2_std_id *a)
152{
Kuninori Morimoto513791a2008-12-18 12:46:45 -0300153 struct soc_camera_file *icf = file->private_data;
154 struct soc_camera_device *icd = icf->icd;
155 int ret = 0;
156
157 if (icd->ops->set_std)
158 ret = icd->ops->set_std(icd, a);
159
160 return ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300161}
162
163static int soc_camera_reqbufs(struct file *file, void *priv,
164 struct v4l2_requestbuffers *p)
165{
166 int ret;
167 struct soc_camera_file *icf = file->private_data;
168 struct soc_camera_device *icd = icf->icd;
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -0300169 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300170
171 WARN_ON(priv != file->private_data);
172
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300173 ret = videobuf_reqbufs(&icf->vb_vidq, p);
174 if (ret < 0)
175 return ret;
176
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300177 return ici->ops->reqbufs(icf, p);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300178}
179
180static int soc_camera_querybuf(struct file *file, void *priv,
181 struct v4l2_buffer *p)
182{
183 struct soc_camera_file *icf = file->private_data;
184
185 WARN_ON(priv != file->private_data);
186
187 return videobuf_querybuf(&icf->vb_vidq, p);
188}
189
190static int soc_camera_qbuf(struct file *file, void *priv,
191 struct v4l2_buffer *p)
192{
193 struct soc_camera_file *icf = file->private_data;
194
195 WARN_ON(priv != file->private_data);
196
197 return videobuf_qbuf(&icf->vb_vidq, p);
198}
199
200static int soc_camera_dqbuf(struct file *file, void *priv,
201 struct v4l2_buffer *p)
202{
203 struct soc_camera_file *icf = file->private_data;
204
205 WARN_ON(priv != file->private_data);
206
207 return videobuf_dqbuf(&icf->vb_vidq, p, file->f_flags & O_NONBLOCK);
208}
209
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300210/* Always entered with .video_lock held */
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300211static int soc_camera_init_user_formats(struct soc_camera_device *icd)
212{
213 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskifa489842009-08-25 11:46:43 -0300214 int i, fmts = 0, ret;
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300215
216 if (!ici->ops->get_formats)
217 /*
218 * Fallback mode - the host will have to serve all
219 * sensor-provided formats one-to-one to the user
220 */
221 fmts = icd->num_formats;
222 else
223 /*
224 * First pass - only count formats this host-sensor
225 * configuration can provide
226 */
Guennadi Liakhovetskifa489842009-08-25 11:46:43 -0300227 for (i = 0; i < icd->num_formats; i++) {
228 ret = ici->ops->get_formats(icd, i, NULL);
229 if (ret < 0)
230 return ret;
231 fmts += ret;
232 }
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300233
234 if (!fmts)
235 return -ENXIO;
236
237 icd->user_formats =
238 vmalloc(fmts * sizeof(struct soc_camera_format_xlate));
239 if (!icd->user_formats)
240 return -ENOMEM;
241
242 icd->num_user_formats = fmts;
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300243
244 dev_dbg(&icd->dev, "Found %d supported formats.\n", fmts);
245
246 /* Second pass - actually fill data formats */
Stefan Herbrechtsmeier14df2cc2009-06-24 10:31:25 -0300247 fmts = 0;
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300248 for (i = 0; i < icd->num_formats; i++)
249 if (!ici->ops->get_formats) {
250 icd->user_formats[i].host_fmt = icd->formats + i;
251 icd->user_formats[i].cam_fmt = icd->formats + i;
252 icd->user_formats[i].buswidth = icd->formats[i].depth;
253 } else {
Guennadi Liakhovetskifa489842009-08-25 11:46:43 -0300254 ret = ici->ops->get_formats(icd, i,
255 &icd->user_formats[fmts]);
256 if (ret < 0)
257 goto egfmt;
258 fmts += ret;
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300259 }
260
261 icd->current_fmt = icd->user_formats[0].host_fmt;
262
263 return 0;
Guennadi Liakhovetskifa489842009-08-25 11:46:43 -0300264
265egfmt:
266 icd->num_user_formats = 0;
267 vfree(icd->user_formats);
268 return ret;
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300269}
270
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300271/* Always entered with .video_lock held */
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300272static void soc_camera_free_user_formats(struct soc_camera_device *icd)
273{
Guennadi Liakhovetskifa489842009-08-25 11:46:43 -0300274 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
275
276 if (ici->ops->put_formats)
277 ici->ops->put_formats(icd);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300278 icd->current_fmt = NULL;
Guennadi Liakhovetskifa489842009-08-25 11:46:43 -0300279 icd->num_user_formats = 0;
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300280 vfree(icd->user_formats);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300281 icd->user_formats = NULL;
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300282}
283
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300284/* Called with .vb_lock held */
285static int soc_camera_set_fmt(struct soc_camera_file *icf,
286 struct v4l2_format *f)
287{
288 struct soc_camera_device *icd = icf->icd;
289 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
290 struct v4l2_pix_format *pix = &f->fmt.pix;
291 int ret;
292
293 /* We always call try_fmt() before set_fmt() or set_crop() */
294 ret = ici->ops->try_fmt(icd, f);
295 if (ret < 0)
296 return ret;
297
298 ret = ici->ops->set_fmt(icd, f);
299 if (ret < 0) {
300 return ret;
301 } else if (!icd->current_fmt ||
302 icd->current_fmt->fourcc != pix->pixelformat) {
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300303 dev_err(ici->v4l2_dev.dev,
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300304 "Host driver hasn't set up current format correctly!\n");
305 return -EINVAL;
306 }
307
Guennadi Liakhovetskia0705b02009-08-25 11:46:17 -0300308 icd->rect_current.width = pix->width;
309 icd->rect_current.height = pix->height;
310 icf->vb_vidq.field =
311 icd->field = pix->field;
Guennadi Liakhovetski025c18a2009-03-13 06:08:20 -0300312
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300313 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
314 dev_warn(&icd->dev, "Attention! Wrong buf-type %d\n",
315 f->type);
316
317 dev_dbg(&icd->dev, "set width: %d height: %d\n",
Guennadi Liakhovetskia0705b02009-08-25 11:46:17 -0300318 icd->rect_current.width, icd->rect_current.height);
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300319
320 /* set physical bus parameters */
321 return ici->ops->set_bus_param(icd, pix->pixelformat);
322}
323
Hans Verkuilbec43662008-12-30 06:58:20 -0300324static int soc_camera_open(struct file *file)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300325{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300326 struct video_device *vdev = video_devdata(file);
327 struct soc_camera_device *icd = container_of(vdev->parent, struct soc_camera_device, dev);
328 struct soc_camera_link *icl = to_soc_camera_link(icd);
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300329 struct soc_camera_host *ici;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300330 struct soc_camera_file *icf;
331 int ret;
332
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300333 if (!icd->ops)
334 /* No device driver attached */
335 return -ENODEV;
336
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300337 ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300338
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300339 icf = vmalloc(sizeof(*icf));
340 if (!icf)
341 return -ENOMEM;
342
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300343 if (!try_module_get(ici->ops->owner)) {
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300344 dev_err(&icd->dev, "Couldn't lock capture bus driver.\n");
345 ret = -EINVAL;
346 goto emgi;
347 }
348
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300349 /* Protect against icd->ops->remove() until we module_get() both drivers. */
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300350 mutex_lock(&icd->video_lock);
351
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300352 icf->icd = icd;
Guennadi Liakhovetski1a0063a2008-04-04 13:46:34 -0300353 icd->use_count++;
354
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300355 /* Now we really have to activate the camera */
356 if (icd->use_count == 1) {
Guennadi Liakhovetski025c18a2009-03-13 06:08:20 -0300357 /* Restore parameters before the last close() per V4L2 API */
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300358 struct v4l2_format f = {
359 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
360 .fmt.pix = {
Guennadi Liakhovetskia0705b02009-08-25 11:46:17 -0300361 .width = icd->rect_current.width,
362 .height = icd->rect_current.height,
Guennadi Liakhovetski025c18a2009-03-13 06:08:20 -0300363 .field = icd->field,
Guennadi Liakhovetskifa489842009-08-25 11:46:43 -0300364 .pixelformat = icd->current_fmt->fourcc,
365 .colorspace = icd->current_fmt->colorspace,
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300366 },
367 };
368
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300369 if (icl->power) {
370 ret = icl->power(icd->pdev, 1);
371 if (ret < 0)
372 goto epower;
373 }
374
375 /* The camera could have been already on, try to reset */
376 if (icl->reset)
377 icl->reset(icd->pdev);
378
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300379 ret = ici->ops->add(icd);
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300380 if (ret < 0) {
381 dev_err(&icd->dev, "Couldn't activate the camera: %d\n", ret);
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300382 goto eiciadd;
383 }
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300384
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300385 if (icd->ops->init) {
386 ret = icd->ops->init(icd);
387 if (ret < 0)
388 goto einit;
389 }
390
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300391 /* Try to configure with default parameters */
392 ret = soc_camera_set_fmt(icf, &f);
393 if (ret < 0)
394 goto esfmt;
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300395 }
396
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300397 file->private_data = icf;
398 dev_dbg(&icd->dev, "camera device open\n");
399
Magnus Damma034d1b2008-07-11 20:59:34 -0300400 ici->ops->init_videobuf(&icf->vb_vidq, icd);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300401
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300402 mutex_unlock(&icd->video_lock);
403
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300404 return 0;
405
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300406 /*
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300407 * First five errors are entered with the .video_lock held
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300408 * and use_count == 1
409 */
410esfmt:
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300411 if (icd->ops->release)
412 icd->ops->release(icd);
413einit:
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300414 ici->ops->remove(icd);
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300415eiciadd:
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300416 if (icl->power)
417 icl->power(icd->pdev, 0);
418epower:
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300419 icd->use_count--;
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300420 mutex_unlock(&icd->video_lock);
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300421 module_put(ici->ops->owner);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300422emgi:
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300423 vfree(icf);
424 return ret;
425}
426
Hans Verkuilbec43662008-12-30 06:58:20 -0300427static int soc_camera_close(struct file *file)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300428{
429 struct soc_camera_file *icf = file->private_data;
430 struct soc_camera_device *icd = icf->icd;
431 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
432 struct video_device *vdev = icd->vdev;
433
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300434 mutex_lock(&icd->video_lock);
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300435 icd->use_count--;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300436 if (!icd->use_count) {
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300437 struct soc_camera_link *icl = to_soc_camera_link(icd);
438
439 if (icd->ops->release)
440 icd->ops->release(icd);
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300441 ici->ops->remove(icd);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300442 if (icl->power)
443 icl->power(icd->pdev, 0);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300444 }
Guennadi Liakhovetski025c18a2009-03-13 06:08:20 -0300445
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300446 mutex_unlock(&icd->video_lock);
447
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300448 module_put(ici->ops->owner);
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300449
Guennadi Liakhovetski1a0063a2008-04-04 13:46:34 -0300450 vfree(icf);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300451
Hans Verkuil5e85e732008-07-20 06:31:39 -0300452 dev_dbg(vdev->parent, "camera device close\n");
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300453
454 return 0;
455}
456
Andrew Mortonaba360d2008-04-22 14:45:59 -0300457static ssize_t soc_camera_read(struct file *file, char __user *buf,
Guennadi Liakhovetskiabe4c472008-12-01 09:44:56 -0300458 size_t count, loff_t *ppos)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300459{
460 struct soc_camera_file *icf = file->private_data;
461 struct soc_camera_device *icd = icf->icd;
462 struct video_device *vdev = icd->vdev;
463 int err = -EINVAL;
464
Hans Verkuil5e85e732008-07-20 06:31:39 -0300465 dev_err(vdev->parent, "camera device read not implemented\n");
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300466
467 return err;
468}
469
470static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma)
471{
472 struct soc_camera_file *icf = file->private_data;
473 struct soc_camera_device *icd = icf->icd;
474 int err;
475
476 dev_dbg(&icd->dev, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
477
478 err = videobuf_mmap_mapper(&icf->vb_vidq, vma);
479
480 dev_dbg(&icd->dev, "vma start=0x%08lx, size=%ld, ret=%d\n",
481 (unsigned long)vma->vm_start,
482 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start,
483 err);
484
485 return err;
486}
487
488static unsigned int soc_camera_poll(struct file *file, poll_table *pt)
489{
490 struct soc_camera_file *icf = file->private_data;
491 struct soc_camera_device *icd = icf->icd;
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -0300492 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300493
494 if (list_empty(&icf->vb_vidq.stream)) {
495 dev_err(&icd->dev, "Trying to poll with no queued buffers!\n");
496 return POLLERR;
497 }
498
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300499 return ici->ops->poll(file, pt);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300500}
501
Hans Verkuilbec43662008-12-30 06:58:20 -0300502static struct v4l2_file_operations soc_camera_fops = {
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300503 .owner = THIS_MODULE,
504 .open = soc_camera_open,
505 .release = soc_camera_close,
506 .ioctl = video_ioctl2,
507 .read = soc_camera_read,
508 .mmap = soc_camera_mmap,
509 .poll = soc_camera_poll,
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300510};
511
Hans Verkuil72937892008-06-01 10:35:18 -0300512static int soc_camera_s_fmt_vid_cap(struct file *file, void *priv,
Guennadi Liakhovetskiabe4c472008-12-01 09:44:56 -0300513 struct v4l2_format *f)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300514{
515 struct soc_camera_file *icf = file->private_data;
516 struct soc_camera_device *icd = icf->icd;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300517 int ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300518
519 WARN_ON(priv != file->private_data);
520
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300521 mutex_lock(&icf->vb_vidq.vb_lock);
522
523 if (videobuf_queue_is_busy(&icf->vb_vidq)) {
524 dev_err(&icd->dev, "S_FMT denied: queue busy\n");
525 ret = -EBUSY;
526 goto unlock;
527 }
528
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300529 ret = soc_camera_set_fmt(icf, f);
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300530
531unlock:
532 mutex_unlock(&icf->vb_vidq.vb_lock);
533
534 return ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300535}
536
Hans Verkuil72937892008-06-01 10:35:18 -0300537static int soc_camera_enum_fmt_vid_cap(struct file *file, void *priv,
Guennadi Liakhovetskiabe4c472008-12-01 09:44:56 -0300538 struct v4l2_fmtdesc *f)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300539{
540 struct soc_camera_file *icf = file->private_data;
541 struct soc_camera_device *icd = icf->icd;
542 const struct soc_camera_data_format *format;
543
544 WARN_ON(priv != file->private_data);
545
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300546 if (f->index >= icd->num_user_formats)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300547 return -EINVAL;
548
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300549 format = icd->user_formats[f->index].host_fmt;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300550
551 strlcpy(f->description, format->name, sizeof(f->description));
552 f->pixelformat = format->fourcc;
553 return 0;
554}
555
Hans Verkuil72937892008-06-01 10:35:18 -0300556static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv,
Guennadi Liakhovetskiabe4c472008-12-01 09:44:56 -0300557 struct v4l2_format *f)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300558{
559 struct soc_camera_file *icf = file->private_data;
560 struct soc_camera_device *icd = icf->icd;
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -0300561 struct v4l2_pix_format *pix = &f->fmt.pix;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300562
563 WARN_ON(priv != file->private_data);
564
Guennadi Liakhovetskia0705b02009-08-25 11:46:17 -0300565 pix->width = icd->rect_current.width;
566 pix->height = icd->rect_current.height;
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -0300567 pix->field = icf->vb_vidq.field;
568 pix->pixelformat = icd->current_fmt->fourcc;
569 pix->bytesperline = pix->width *
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -0300570 DIV_ROUND_UP(icd->current_fmt->depth, 8);
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -0300571 pix->sizeimage = pix->height * pix->bytesperline;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300572 dev_dbg(&icd->dev, "current_fmt->fourcc: 0x%08x\n",
573 icd->current_fmt->fourcc);
574 return 0;
575}
576
577static int soc_camera_querycap(struct file *file, void *priv,
578 struct v4l2_capability *cap)
579{
580 struct soc_camera_file *icf = file->private_data;
581 struct soc_camera_device *icd = icf->icd;
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -0300582 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300583
584 WARN_ON(priv != file->private_data);
585
586 strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver));
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300587 return ici->ops->querycap(ici, cap);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300588}
589
590static int soc_camera_streamon(struct file *file, void *priv,
591 enum v4l2_buf_type i)
592{
593 struct soc_camera_file *icf = file->private_data;
594 struct soc_camera_device *icd = icf->icd;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300595 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300596 int ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300597
598 WARN_ON(priv != file->private_data);
599
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300600 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
601 return -EINVAL;
602
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300603 mutex_lock(&icd->video_lock);
604
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300605 v4l2_device_call_until_err(&ici->v4l2_dev, (__u32)icd, video, s_stream, 1);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300606
607 /* This calls buf_queue from host driver's videobuf_queue_ops */
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300608 ret = videobuf_streamon(&icf->vb_vidq);
609
610 mutex_unlock(&icd->video_lock);
611
612 return ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300613}
614
615static int soc_camera_streamoff(struct file *file, void *priv,
616 enum v4l2_buf_type i)
617{
618 struct soc_camera_file *icf = file->private_data;
619 struct soc_camera_device *icd = icf->icd;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300620 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300621
622 WARN_ON(priv != file->private_data);
623
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300624 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
625 return -EINVAL;
626
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300627 mutex_lock(&icd->video_lock);
628
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300629 /* This calls buf_release from host driver's videobuf_queue_ops for all
630 * remaining buffers. When the last buffer is freed, stop capture */
631 videobuf_streamoff(&icf->vb_vidq);
632
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300633 v4l2_device_call_until_err(&ici->v4l2_dev, (__u32)icd, video, s_stream, 0);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300634
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300635 mutex_unlock(&icd->video_lock);
636
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300637 return 0;
638}
639
640static int soc_camera_queryctrl(struct file *file, void *priv,
641 struct v4l2_queryctrl *qc)
642{
643 struct soc_camera_file *icf = file->private_data;
644 struct soc_camera_device *icd = icf->icd;
Guennadi Liakhovetski2840d242009-08-25 11:44:15 -0300645 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300646 int i;
647
648 WARN_ON(priv != file->private_data);
649
650 if (!qc->id)
651 return -EINVAL;
652
Guennadi Liakhovetski2840d242009-08-25 11:44:15 -0300653 /* First check host controls */
654 for (i = 0; i < ici->ops->num_controls; i++)
655 if (qc->id == ici->ops->controls[i].id) {
656 memcpy(qc, &(ici->ops->controls[i]),
657 sizeof(*qc));
658 return 0;
659 }
660
661 /* Then device controls */
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300662 for (i = 0; i < icd->ops->num_controls; i++)
663 if (qc->id == icd->ops->controls[i].id) {
664 memcpy(qc, &(icd->ops->controls[i]),
665 sizeof(*qc));
666 return 0;
667 }
668
669 return -EINVAL;
670}
671
672static int soc_camera_g_ctrl(struct file *file, void *priv,
673 struct v4l2_control *ctrl)
674{
675 struct soc_camera_file *icf = file->private_data;
676 struct soc_camera_device *icd = icf->icd;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300677 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetski2840d242009-08-25 11:44:15 -0300678 int ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300679
680 WARN_ON(priv != file->private_data);
681
682 switch (ctrl->id) {
683 case V4L2_CID_GAIN:
684 if (icd->gain == (unsigned short)~0)
685 return -EINVAL;
686 ctrl->value = icd->gain;
687 return 0;
688 case V4L2_CID_EXPOSURE:
689 if (icd->exposure == (unsigned short)~0)
690 return -EINVAL;
691 ctrl->value = icd->exposure;
692 return 0;
693 }
694
Guennadi Liakhovetski2840d242009-08-25 11:44:15 -0300695 if (ici->ops->get_ctrl) {
696 ret = ici->ops->get_ctrl(icd, ctrl);
697 if (ret != -ENOIOCTLCMD)
698 return ret;
699 }
700
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300701 return v4l2_device_call_until_err(&ici->v4l2_dev, (__u32)icd, core, g_ctrl, ctrl);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300702}
703
704static int soc_camera_s_ctrl(struct file *file, void *priv,
705 struct v4l2_control *ctrl)
706{
707 struct soc_camera_file *icf = file->private_data;
708 struct soc_camera_device *icd = icf->icd;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300709 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetski2840d242009-08-25 11:44:15 -0300710 int ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300711
712 WARN_ON(priv != file->private_data);
713
Guennadi Liakhovetski2840d242009-08-25 11:44:15 -0300714 if (ici->ops->set_ctrl) {
715 ret = ici->ops->set_ctrl(icd, ctrl);
716 if (ret != -ENOIOCTLCMD)
717 return ret;
718 }
719
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300720 return v4l2_device_call_until_err(&ici->v4l2_dev, (__u32)icd, core, s_ctrl, ctrl);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300721}
722
723static int soc_camera_cropcap(struct file *file, void *fh,
724 struct v4l2_cropcap *a)
725{
726 struct soc_camera_file *icf = file->private_data;
727 struct soc_camera_device *icd = icf->icd;
728
729 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Guennadi Liakhovetskia0705b02009-08-25 11:46:17 -0300730 a->bounds = icd->rect_max;
731 a->defrect.left = icd->rect_max.left;
732 a->defrect.top = icd->rect_max.top;
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300733 a->defrect.width = DEFAULT_WIDTH;
734 a->defrect.height = DEFAULT_HEIGHT;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300735 a->pixelaspect.numerator = 1;
736 a->pixelaspect.denominator = 1;
737
738 return 0;
739}
740
741static int soc_camera_g_crop(struct file *file, void *fh,
742 struct v4l2_crop *a)
743{
744 struct soc_camera_file *icf = file->private_data;
745 struct soc_camera_device *icd = icf->icd;
746
Guennadi Liakhovetskia0705b02009-08-25 11:46:17 -0300747 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
748 a->c = icd->rect_current;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300749
750 return 0;
751}
752
753static int soc_camera_s_crop(struct file *file, void *fh,
754 struct v4l2_crop *a)
755{
756 struct soc_camera_file *icf = file->private_data;
757 struct soc_camera_device *icd = icf->icd;
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -0300758 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300759 int ret;
760
761 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
762 return -EINVAL;
763
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300764 /* Cropping is allowed during a running capture, guard consistency */
765 mutex_lock(&icf->vb_vidq.vb_lock);
766
Guennadi Liakhovetskia0705b02009-08-25 11:46:17 -0300767 if (a->c.width > icd->rect_max.width)
768 a->c.width = icd->rect_max.width;
769
770 if (a->c.width < icd->width_min)
771 a->c.width = icd->width_min;
772
773 if (a->c.height > icd->rect_max.height)
774 a->c.height = icd->rect_max.height;
775
776 if (a->c.height < icd->height_min)
777 a->c.height = icd->height_min;
778
779 if (a->c.width + a->c.left > icd->rect_max.width + icd->rect_max.left)
780 a->c.left = icd->rect_max.width + icd->rect_max.left -
781 a->c.width;
782
783 if (a->c.height + a->c.top > icd->rect_max.height + icd->rect_max.top)
784 a->c.top = icd->rect_max.height + icd->rect_max.top -
785 a->c.height;
786
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300787 ret = ici->ops->set_crop(icd, &a->c);
Guennadi Liakhovetskia0705b02009-08-25 11:46:17 -0300788 if (!ret)
789 icd->rect_current = a->c;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300790
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300791 mutex_unlock(&icf->vb_vidq.vb_lock);
792
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300793 return ret;
794}
795
796static int soc_camera_g_chip_ident(struct file *file, void *fh,
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300797 struct v4l2_dbg_chip_ident *id)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300798{
799 struct soc_camera_file *icf = file->private_data;
800 struct soc_camera_device *icd = icf->icd;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300801 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300802
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300803 return v4l2_device_call_until_err(&ici->v4l2_dev, (__u32)icd, core, g_chip_ident, id);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300804}
805
806#ifdef CONFIG_VIDEO_ADV_DEBUG
807static int soc_camera_g_register(struct file *file, void *fh,
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300808 struct v4l2_dbg_register *reg)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300809{
810 struct soc_camera_file *icf = file->private_data;
811 struct soc_camera_device *icd = icf->icd;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300812 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300813
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300814 return v4l2_device_call_until_err(&ici->v4l2_dev, (__u32)icd, core, g_register, reg);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300815}
816
817static int soc_camera_s_register(struct file *file, void *fh,
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300818 struct v4l2_dbg_register *reg)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300819{
820 struct soc_camera_file *icf = file->private_data;
821 struct soc_camera_device *icd = icf->icd;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300822 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300823
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300824 return v4l2_device_call_until_err(&ici->v4l2_dev, (__u32)icd, core, s_register, reg);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300825}
826#endif
827
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300828/* So far this function cannot fail */
829static void scan_add_host(struct soc_camera_host *ici)
830{
831 struct soc_camera_device *icd;
832
833 mutex_lock(&list_lock);
834
835 list_for_each_entry(icd, &devices, list) {
836 if (icd->iface == ici->nr) {
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300837 int ret;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300838 icd->dev.parent = ici->v4l2_dev.dev;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300839 dev_set_name(&icd->dev, "%u-%u", icd->iface,
840 icd->devnum);
841 ret = device_register(&icd->dev);
842 if (ret < 0) {
843 icd->dev.parent = NULL;
844 dev_err(&icd->dev,
845 "Cannot register device: %d\n", ret);
846 }
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300847 }
848 }
849
850 mutex_unlock(&list_lock);
851}
852
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300853#ifdef CONFIG_I2C_BOARDINFO
854static int soc_camera_init_i2c(struct soc_camera_device *icd,
855 struct soc_camera_link *icl)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300856{
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300857 struct i2c_client *client;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300858 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300859 struct i2c_adapter *adap = i2c_get_adapter(icl->i2c_adapter_id);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300860 struct v4l2_subdev *subdev;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300861 int ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300862
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300863 if (!adap) {
864 ret = -ENODEV;
865 dev_err(&icd->dev, "Cannot get I2C adapter #%d. No driver?\n",
866 icl->i2c_adapter_id);
867 goto ei2cga;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300868 }
869
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300870 icl->board_info->platform_data = icd;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300871
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300872 subdev = v4l2_i2c_new_subdev_board(&ici->v4l2_dev, adap,
873 icl->module_name, icl->board_info, NULL);
874 if (!subdev) {
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300875 ret = -ENOMEM;
876 goto ei2cnd;
877 }
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300878
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300879 subdev->grp_id = (__u32)icd;
880 client = subdev->priv;
881
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300882 /* Use to_i2c_client(dev) to recover the i2c client */
883 dev_set_drvdata(&icd->dev, &client->dev);
884
885 return 0;
886ei2cnd:
887 i2c_put_adapter(adap);
888ei2cga:
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300889 return ret;
890}
891
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300892static void soc_camera_free_i2c(struct soc_camera_device *icd)
893{
894 struct i2c_client *client =
895 to_i2c_client(to_soc_camera_control(icd));
896 dev_set_drvdata(&icd->dev, NULL);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300897 v4l2_device_unregister_subdev(i2c_get_clientdata(client));
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300898 i2c_unregister_device(client);
899 i2c_put_adapter(client->adapter);
900}
901#else
902#define soc_camera_init_i2c(icd, icl) (-ENODEV)
903#define soc_camera_free_i2c(icd) do {} while (0)
904#endif
905
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300906static int soc_camera_video_start(struct soc_camera_device *icd);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300907static int video_dev_create(struct soc_camera_device *icd);
908/* Called during host-driver probe */
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300909static int soc_camera_probe(struct device *dev)
910{
911 struct soc_camera_device *icd = to_soc_camera_dev(dev);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300912 struct soc_camera_host *ici = to_soc_camera_host(dev->parent);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300913 struct soc_camera_link *icl = to_soc_camera_link(icd);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300914 struct device *control = NULL;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300915 int ret;
916
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300917 dev_info(dev, "Probing %s\n", dev_name(dev));
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300918
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300919 if (icl->power) {
920 ret = icl->power(icd->pdev, 1);
921 if (ret < 0) {
922 dev_err(dev,
923 "Platform failed to power-on the camera.\n");
924 goto epower;
925 }
926 }
927
928 /* The camera could have been already on, try to reset */
929 if (icl->reset)
930 icl->reset(icd->pdev);
931
932 ret = ici->ops->add(icd);
933 if (ret < 0)
934 goto eadd;
935
936 /* Must have icd->vdev before registering the device */
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300937 ret = video_dev_create(icd);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300938 if (ret < 0)
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300939 goto evdc;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300940
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300941 /* Non-i2c cameras, e.g., soc_camera_platform, have no board_info */
942 if (icl->board_info) {
943 ret = soc_camera_init_i2c(icd, icl);
944 if (ret < 0)
945 goto eadddev;
946 } else if (!icl->add_device || !icl->del_device) {
947 ret = -EINVAL;
948 goto eadddev;
949 } else {
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300950 if (icl->module_name)
951 ret = request_module(icl->module_name);
952
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300953 ret = icl->add_device(icl, &icd->dev);
954 if (ret < 0)
955 goto eadddev;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300956
957 /* FIXME: this is racy, have to use driver-binding notification */
958 control = to_soc_camera_control(icd);
959 if (!control || !control->driver ||
960 !try_module_get(control->driver->owner)) {
961 icl->del_device(icl);
962 goto enodrv;
963 }
Guennadi Liakhovetski025c18a2009-03-13 06:08:20 -0300964 }
965
Guennadi Liakhovetskifa489842009-08-25 11:46:43 -0300966 /* At this point client .probe() should have run already */
967 ret = soc_camera_init_user_formats(icd);
968 if (ret < 0)
969 goto eiufmt;
970
971 icd->rect_current = icd->rect_max;
972 icd->field = V4L2_FIELD_ANY;
973
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300974 /* ..._video_start() will create a device node, so we have to protect */
975 mutex_lock(&icd->video_lock);
976
977 ret = soc_camera_video_start(icd);
978 if (ret < 0)
979 goto evidstart;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300980
981 /* Do we have to sysfs_remove_link() before device_unregister()? */
982 if (to_soc_camera_control(icd) &&
983 sysfs_create_link(&icd->dev.kobj, &to_soc_camera_control(icd)->kobj,
984 "control"))
985 dev_warn(&icd->dev, "Failed creating the control symlink\n");
986
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300987 ici->ops->remove(icd);
988
989 if (icl->power)
990 icl->power(icd->pdev, 0);
991
992 mutex_unlock(&icd->video_lock);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300993
994 return 0;
995
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300996evidstart:
997 mutex_unlock(&icd->video_lock);
Guennadi Liakhovetskifa489842009-08-25 11:46:43 -0300998 soc_camera_free_user_formats(icd);
999eiufmt:
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001000 if (icl->board_info) {
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001001 soc_camera_free_i2c(icd);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001002 } else {
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001003 icl->del_device(icl);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001004 module_put(control->driver->owner);
1005 }
1006enodrv:
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001007eadddev:
1008 video_device_release(icd->vdev);
1009evdc:
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001010 ici->ops->remove(icd);
1011eadd:
1012 if (icl->power)
1013 icl->power(icd->pdev, 0);
1014epower:
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001015 return ret;
1016}
1017
1018/* This is called on device_unregister, which only means we have to disconnect
1019 * from the host, but not remove ourselves from the device list */
1020static int soc_camera_remove(struct device *dev)
1021{
1022 struct soc_camera_device *icd = to_soc_camera_dev(dev);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001023 struct soc_camera_link *icl = to_soc_camera_link(icd);
1024 struct video_device *vdev = icd->vdev;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001025
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001026 BUG_ON(!dev->parent);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001027
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001028 if (vdev) {
1029 mutex_lock(&icd->video_lock);
1030 video_unregister_device(vdev);
1031 icd->vdev = NULL;
1032 mutex_unlock(&icd->video_lock);
1033 }
1034
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001035 if (icl->board_info) {
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001036 soc_camera_free_i2c(icd);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001037 } else {
1038 struct device_driver *drv = to_soc_camera_control(icd) ?
1039 to_soc_camera_control(icd)->driver : NULL;
1040 if (drv) {
1041 icl->del_device(icl);
1042 module_put(drv->owner);
1043 }
1044 }
Guennadi Liakhovetskifa489842009-08-25 11:46:43 -03001045 soc_camera_free_user_formats(icd);
Guennadi Liakhovetski025c18a2009-03-13 06:08:20 -03001046
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001047 return 0;
1048}
1049
Robert Jarzmik2e521062008-08-01 20:14:50 -03001050static int soc_camera_suspend(struct device *dev, pm_message_t state)
1051{
1052 struct soc_camera_device *icd = to_soc_camera_dev(dev);
1053 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1054 int ret = 0;
1055
1056 if (ici->ops->suspend)
1057 ret = ici->ops->suspend(icd, state);
1058
1059 return ret;
1060}
1061
1062static int soc_camera_resume(struct device *dev)
1063{
1064 struct soc_camera_device *icd = to_soc_camera_dev(dev);
1065 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1066 int ret = 0;
1067
1068 if (ici->ops->resume)
1069 ret = ici->ops->resume(icd);
1070
1071 return ret;
1072}
1073
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001074static struct bus_type soc_camera_bus_type = {
1075 .name = "soc-camera",
1076 .probe = soc_camera_probe,
1077 .remove = soc_camera_remove,
Robert Jarzmik2e521062008-08-01 20:14:50 -03001078 .suspend = soc_camera_suspend,
1079 .resume = soc_camera_resume,
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001080};
1081
1082static struct device_driver ic_drv = {
1083 .name = "camera",
1084 .bus = &soc_camera_bus_type,
1085 .owner = THIS_MODULE,
1086};
1087
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001088static void dummy_release(struct device *dev)
1089{
1090}
1091
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -03001092int soc_camera_host_register(struct soc_camera_host *ici)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001093{
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001094 struct soc_camera_host *ix;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001095 int ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001096
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -03001097 if (!ici || !ici->ops ||
1098 !ici->ops->try_fmt ||
1099 !ici->ops->set_fmt ||
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -03001100 !ici->ops->set_crop ||
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -03001101 !ici->ops->set_bus_param ||
1102 !ici->ops->querycap ||
1103 !ici->ops->init_videobuf ||
1104 !ici->ops->reqbufs ||
1105 !ici->ops->add ||
1106 !ici->ops->remove ||
Guennadi Liakhovetskieff505f2009-04-24 12:55:48 -03001107 !ici->ops->poll ||
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001108 !ici->v4l2_dev.dev)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001109 return -EINVAL;
1110
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001111 mutex_lock(&list_lock);
1112 list_for_each_entry(ix, &hosts, list) {
1113 if (ix->nr == ici->nr) {
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001114 ret = -EBUSY;
1115 goto edevreg;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001116 }
1117 }
1118
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001119 ret = v4l2_device_register(ici->v4l2_dev.dev, &ici->v4l2_dev);
1120 if (ret < 0)
1121 goto edevreg;
Guennadi Liakhovetskieff505f2009-04-24 12:55:48 -03001122
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001123 list_add_tail(&ici->list, &hosts);
1124 mutex_unlock(&list_lock);
1125
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001126 scan_add_host(ici);
1127
1128 return 0;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001129
1130edevreg:
1131 mutex_unlock(&list_lock);
1132 return ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001133}
1134EXPORT_SYMBOL(soc_camera_host_register);
1135
1136/* Unregister all clients! */
1137void soc_camera_host_unregister(struct soc_camera_host *ici)
1138{
1139 struct soc_camera_device *icd;
1140
1141 mutex_lock(&list_lock);
1142
1143 list_del(&ici->list);
1144
1145 list_for_each_entry(icd, &devices, list) {
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001146 if (icd->iface == ici->nr) {
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001147 /* The bus->remove will be called */
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001148 device_unregister(&icd->dev);
1149 /* Not before device_unregister(), .remove
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -03001150 * needs parent to call ici->ops->remove() */
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001151 icd->dev.parent = NULL;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001152
1153 /* If the host module is loaded again, device_register()
1154 * would complain "already initialised" */
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001155 memset(&icd->dev.kobj, 0, sizeof(icd->dev.kobj));
1156 }
1157 }
1158
1159 mutex_unlock(&list_lock);
1160
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001161 v4l2_device_unregister(&ici->v4l2_dev);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001162}
1163EXPORT_SYMBOL(soc_camera_host_unregister);
1164
1165/* Image capture device */
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001166static int soc_camera_device_register(struct soc_camera_device *icd)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001167{
1168 struct soc_camera_device *ix;
1169 int num = -1, i;
1170
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001171 for (i = 0; i < 256 && num < 0; i++) {
1172 num = i;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001173 /* Check if this index is available on this interface */
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001174 list_for_each_entry(ix, &devices, list) {
1175 if (ix->iface == icd->iface && ix->devnum == i) {
1176 num = -1;
1177 break;
1178 }
1179 }
1180 }
1181
1182 if (num < 0)
1183 /* ok, we have 256 cameras on this host...
1184 * man, stay reasonable... */
1185 return -ENOMEM;
1186
1187 icd->devnum = num;
1188 icd->dev.bus = &soc_camera_bus_type;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001189
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -03001190 icd->dev.release = dummy_release;
1191 icd->use_count = 0;
1192 icd->host_priv = NULL;
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -03001193 mutex_init(&icd->video_lock);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001194
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001195 list_add_tail(&icd->list, &devices);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001196
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001197 return 0;
1198}
1199
1200static void soc_camera_device_unregister(struct soc_camera_device *icd)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001201{
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001202 list_del(&icd->list);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001203}
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001204
Hans Verkuila3998102008-07-21 02:57:38 -03001205static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
1206 .vidioc_querycap = soc_camera_querycap,
1207 .vidioc_g_fmt_vid_cap = soc_camera_g_fmt_vid_cap,
1208 .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
1209 .vidioc_s_fmt_vid_cap = soc_camera_s_fmt_vid_cap,
1210 .vidioc_enum_input = soc_camera_enum_input,
1211 .vidioc_g_input = soc_camera_g_input,
1212 .vidioc_s_input = soc_camera_s_input,
1213 .vidioc_s_std = soc_camera_s_std,
1214 .vidioc_reqbufs = soc_camera_reqbufs,
1215 .vidioc_try_fmt_vid_cap = soc_camera_try_fmt_vid_cap,
1216 .vidioc_querybuf = soc_camera_querybuf,
1217 .vidioc_qbuf = soc_camera_qbuf,
1218 .vidioc_dqbuf = soc_camera_dqbuf,
1219 .vidioc_streamon = soc_camera_streamon,
1220 .vidioc_streamoff = soc_camera_streamoff,
1221 .vidioc_queryctrl = soc_camera_queryctrl,
1222 .vidioc_g_ctrl = soc_camera_g_ctrl,
1223 .vidioc_s_ctrl = soc_camera_s_ctrl,
1224 .vidioc_cropcap = soc_camera_cropcap,
1225 .vidioc_g_crop = soc_camera_g_crop,
1226 .vidioc_s_crop = soc_camera_s_crop,
1227 .vidioc_g_chip_ident = soc_camera_g_chip_ident,
1228#ifdef CONFIG_VIDEO_ADV_DEBUG
1229 .vidioc_g_register = soc_camera_g_register,
1230 .vidioc_s_register = soc_camera_s_register,
1231#endif
1232};
1233
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001234static int video_dev_create(struct soc_camera_device *icd)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001235{
1236 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001237 struct video_device *vdev = video_device_alloc();
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001238
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001239 if (!vdev)
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001240 return -ENOMEM;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001241
1242 strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -03001243
Hans Verkuil5e85e732008-07-20 06:31:39 -03001244 vdev->parent = &icd->dev;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001245 vdev->current_norm = V4L2_STD_UNKNOWN;
1246 vdev->fops = &soc_camera_fops;
Hans Verkuila3998102008-07-21 02:57:38 -03001247 vdev->ioctl_ops = &soc_camera_ioctl_ops;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001248 vdev->release = video_device_release;
1249 vdev->minor = -1;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001250 vdev->tvnorms = V4L2_STD_UNKNOWN;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001251
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001252 icd->vdev = vdev;
1253
1254 return 0;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001255}
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001256
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001257/*
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001258 * Called from soc_camera_probe() above (with .video_lock held???)
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001259 */
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001260static int soc_camera_video_start(struct soc_camera_device *icd)
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001261{
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001262 const struct v4l2_queryctrl *qctrl;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001263 int ret;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001264
1265 if (!icd->dev.parent)
1266 return -ENODEV;
1267
1268 if (!icd->ops ||
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001269 !icd->ops->query_bus_param ||
1270 !icd->ops->set_bus_param)
1271 return -EINVAL;
1272
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001273 ret = video_register_device(icd->vdev, VFL_TYPE_GRABBER,
1274 icd->vdev->minor);
1275 if (ret < 0) {
1276 dev_err(&icd->dev, "video_register_device failed: %d\n", ret);
1277 return ret;
1278 }
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001279
1280 qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_GAIN);
1281 icd->gain = qctrl ? qctrl->default_value : (unsigned short)~0;
1282 qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_EXPOSURE);
1283 icd->exposure = qctrl ? qctrl->default_value : (unsigned short)~0;
1284
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001285 return 0;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001286}
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001287
Guennadi Liakhovetskic41deba2009-08-25 11:06:21 -03001288static int __devinit soc_camera_pdrv_probe(struct platform_device *pdev)
1289{
1290 struct soc_camera_link *icl = pdev->dev.platform_data;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001291 struct soc_camera_device *icd;
1292 int ret;
Guennadi Liakhovetskic41deba2009-08-25 11:06:21 -03001293
1294 if (!icl)
1295 return -EINVAL;
1296
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001297 icd = kzalloc(sizeof(*icd), GFP_KERNEL);
1298 if (!icd)
1299 return -ENOMEM;
Guennadi Liakhovetskic41deba2009-08-25 11:06:21 -03001300
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001301 icd->iface = icl->bus_id;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001302 icd->pdev = &pdev->dev;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001303 platform_set_drvdata(pdev, icd);
1304 icd->dev.platform_data = icl;
1305
1306 ret = soc_camera_device_register(icd);
1307 if (ret < 0)
1308 goto escdevreg;
1309
1310 return 0;
1311
1312escdevreg:
1313 kfree(icd);
1314
1315 return ret;
Guennadi Liakhovetskic41deba2009-08-25 11:06:21 -03001316}
1317
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001318/* Only called on rmmod for each platform device, since they are not
1319 * hot-pluggable. Now we know, that all our users - hosts and devices have
1320 * been unloaded already */
Guennadi Liakhovetskic41deba2009-08-25 11:06:21 -03001321static int __devexit soc_camera_pdrv_remove(struct platform_device *pdev)
1322{
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001323 struct soc_camera_device *icd = platform_get_drvdata(pdev);
Guennadi Liakhovetskic41deba2009-08-25 11:06:21 -03001324
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001325 if (!icd)
1326 return -EINVAL;
1327
1328 soc_camera_device_unregister(icd);
1329
1330 kfree(icd);
1331
Guennadi Liakhovetski0fd327b2009-05-07 13:25:32 -03001332 return 0;
1333}
1334
1335static struct platform_driver __refdata soc_camera_pdrv = {
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001336 .remove = __devexit_p(soc_camera_pdrv_remove),
1337 .driver = {
1338 .name = "soc-camera-pdrv",
1339 .owner = THIS_MODULE,
Guennadi Liakhovetski0fd327b2009-05-07 13:25:32 -03001340 },
1341};
1342
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001343static int __init soc_camera_init(void)
1344{
1345 int ret = bus_register(&soc_camera_bus_type);
1346 if (ret)
1347 return ret;
1348 ret = driver_register(&ic_drv);
1349 if (ret)
1350 goto edrvr;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001351
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001352 ret = platform_driver_probe(&soc_camera_pdrv, soc_camera_pdrv_probe);
Guennadi Liakhovetski0fd327b2009-05-07 13:25:32 -03001353 if (ret)
1354 goto epdr;
1355
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001356 return 0;
1357
Guennadi Liakhovetski0fd327b2009-05-07 13:25:32 -03001358epdr:
1359 driver_unregister(&ic_drv);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001360edrvr:
1361 bus_unregister(&soc_camera_bus_type);
1362 return ret;
1363}
1364
1365static void __exit soc_camera_exit(void)
1366{
Guennadi Liakhovetski0fd327b2009-05-07 13:25:32 -03001367 platform_driver_unregister(&soc_camera_pdrv);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001368 driver_unregister(&ic_drv);
1369 bus_unregister(&soc_camera_bus_type);
1370}
1371
1372module_init(soc_camera_init);
1373module_exit(soc_camera_exit);
1374
1375MODULE_DESCRIPTION("Image capture bus driver");
1376MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
1377MODULE_LICENSE("GPL");
Guennadi Liakhovetski0fd327b2009-05-07 13:25:32 -03001378MODULE_ALIAS("platform:soc-camera-pdrv");