blob: f2032939fd4b728d0d7465d03da5ec587087c52f [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Mauro Carvalho Chehab4f9fb5e2010-05-18 00:46:09 -030028#include <linux/pm_runtime.h>
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -030029#include <linux/vmalloc.h>
30
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -030031#include <media/soc_camera.h>
Guennadi Liakhovetski0fd327b2009-05-07 13:25:32 -030032#include <media/v4l2-common.h>
Guennadi Liakhovetski0fd327b2009-05-07 13:25:32 -030033#include <media/v4l2-ioctl.h>
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -030034#include <media/v4l2-dev.h>
Guennadi Liakhovetski0fd327b2009-05-07 13:25:32 -030035#include <media/videobuf-core.h>
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -030036#include <media/soc_mediabus.h>
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -030037
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -030038/* Default to VGA resolution */
39#define DEFAULT_WIDTH 640
40#define DEFAULT_HEIGHT 480
41
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -030042static LIST_HEAD(hosts);
43static LIST_HEAD(devices);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -030044static DEFINE_MUTEX(list_lock); /* Protects the list of hosts */
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -030045
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -030046const struct soc_camera_format_xlate *soc_camera_xlate_by_fourcc(
47 struct soc_camera_device *icd, unsigned int fourcc)
48{
49 unsigned int i;
50
51 for (i = 0; i < icd->num_user_formats; i++)
52 if (icd->user_formats[i].host_fmt->fourcc == fourcc)
53 return icd->user_formats + i;
54 return NULL;
55}
56EXPORT_SYMBOL(soc_camera_xlate_by_fourcc);
57
Guennadi Liakhovetskibd73b362008-12-23 05:54:45 -030058/**
59 * soc_camera_apply_sensor_flags() - apply platform SOCAM_SENSOR_INVERT_* flags
60 * @icl: camera platform parameters
61 * @flags: flags to be inverted according to platform configuration
62 * @return: resulting flags
63 */
64unsigned long soc_camera_apply_sensor_flags(struct soc_camera_link *icl,
65 unsigned long flags)
66{
67 unsigned long f;
68
69 /* If only one of the two polarities is supported, switch to the opposite */
70 if (icl->flags & SOCAM_SENSOR_INVERT_HSYNC) {
71 f = flags & (SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW);
72 if (f == SOCAM_HSYNC_ACTIVE_HIGH || f == SOCAM_HSYNC_ACTIVE_LOW)
73 flags ^= SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW;
74 }
75
76 if (icl->flags & SOCAM_SENSOR_INVERT_VSYNC) {
77 f = flags & (SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW);
78 if (f == SOCAM_VSYNC_ACTIVE_HIGH || f == SOCAM_VSYNC_ACTIVE_LOW)
79 flags ^= SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW;
80 }
81
82 if (icl->flags & SOCAM_SENSOR_INVERT_PCLK) {
83 f = flags & (SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING);
84 if (f == SOCAM_PCLK_SAMPLE_RISING || f == SOCAM_PCLK_SAMPLE_FALLING)
85 flags ^= SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING;
86 }
87
88 return flags;
89}
90EXPORT_SYMBOL(soc_camera_apply_sensor_flags);
91
Hans Verkuil72937892008-06-01 10:35:18 -030092static int soc_camera_try_fmt_vid_cap(struct file *file, void *priv,
Guennadi Liakhovetskiabe4c472008-12-01 09:44:56 -030093 struct v4l2_format *f)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -030094{
95 struct soc_camera_file *icf = file->private_data;
96 struct soc_camera_device *icd = icf->icd;
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -030097 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -030098
99 WARN_ON(priv != file->private_data);
100
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300101 /* limit format to hardware capabilities */
Guennadi Liakhovetski06daa1a2008-12-18 12:52:08 -0300102 return ici->ops->try_fmt(icd, f);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300103}
104
105static int soc_camera_enum_input(struct file *file, void *priv,
106 struct v4l2_input *inp)
107{
Kuninori Morimoto34d359d2008-12-18 12:47:46 -0300108 struct soc_camera_file *icf = file->private_data;
109 struct soc_camera_device *icd = icf->icd;
110 int ret = 0;
111
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300112 if (inp->index != 0)
113 return -EINVAL;
114
Kuninori Morimoto34d359d2008-12-18 12:47:46 -0300115 if (icd->ops->enum_input)
116 ret = icd->ops->enum_input(icd, inp);
117 else {
118 /* default is camera */
119 inp->type = V4L2_INPUT_TYPE_CAMERA;
120 inp->std = V4L2_STD_UNKNOWN;
121 strcpy(inp->name, "Camera");
122 }
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300123
Kuninori Morimoto34d359d2008-12-18 12:47:46 -0300124 return ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300125}
126
127static int soc_camera_g_input(struct file *file, void *priv, unsigned int *i)
128{
129 *i = 0;
130
131 return 0;
132}
133
134static int soc_camera_s_input(struct file *file, void *priv, unsigned int i)
135{
136 if (i > 0)
137 return -EINVAL;
138
139 return 0;
140}
141
142static int soc_camera_s_std(struct file *file, void *priv, v4l2_std_id *a)
143{
Kuninori Morimoto513791a2008-12-18 12:46:45 -0300144 struct soc_camera_file *icf = file->private_data;
145 struct soc_camera_device *icd = icf->icd;
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300146 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Kuninori Morimoto513791a2008-12-18 12:46:45 -0300147
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300148 return v4l2_subdev_call(sd, core, s_std, *a);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300149}
150
151static int soc_camera_reqbufs(struct file *file, void *priv,
152 struct v4l2_requestbuffers *p)
153{
154 int ret;
155 struct soc_camera_file *icf = file->private_data;
156 struct soc_camera_device *icd = icf->icd;
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -0300157 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300158
159 WARN_ON(priv != file->private_data);
160
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300161 ret = videobuf_reqbufs(&icf->vb_vidq, p);
162 if (ret < 0)
163 return ret;
164
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300165 return ici->ops->reqbufs(icf, p);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300166}
167
168static int soc_camera_querybuf(struct file *file, void *priv,
169 struct v4l2_buffer *p)
170{
171 struct soc_camera_file *icf = file->private_data;
172
173 WARN_ON(priv != file->private_data);
174
175 return videobuf_querybuf(&icf->vb_vidq, p);
176}
177
178static int soc_camera_qbuf(struct file *file, void *priv,
179 struct v4l2_buffer *p)
180{
181 struct soc_camera_file *icf = file->private_data;
182
183 WARN_ON(priv != file->private_data);
184
185 return videobuf_qbuf(&icf->vb_vidq, p);
186}
187
188static int soc_camera_dqbuf(struct file *file, void *priv,
189 struct v4l2_buffer *p)
190{
191 struct soc_camera_file *icf = file->private_data;
192
193 WARN_ON(priv != file->private_data);
194
195 return videobuf_dqbuf(&icf->vb_vidq, p, file->f_flags & O_NONBLOCK);
196}
197
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300198/* Always entered with .video_lock held */
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300199static int soc_camera_init_user_formats(struct soc_camera_device *icd)
200{
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300201 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300202 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Hans Verkuil3805f202010-05-08 17:55:00 -0300203 unsigned int i, fmts = 0, raw_fmts = 0;
204 int ret;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300205 enum v4l2_mbus_pixelcode code;
206
207 while (!v4l2_subdev_call(sd, video, enum_mbus_fmt, raw_fmts, &code))
208 raw_fmts++;
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300209
210 if (!ici->ops->get_formats)
211 /*
212 * Fallback mode - the host will have to serve all
213 * sensor-provided formats one-to-one to the user
214 */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300215 fmts = raw_fmts;
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300216 else
217 /*
218 * First pass - only count formats this host-sensor
219 * configuration can provide
220 */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300221 for (i = 0; i < raw_fmts; i++) {
Guennadi Liakhovetskifa489842009-08-25 11:46:43 -0300222 ret = ici->ops->get_formats(icd, i, NULL);
223 if (ret < 0)
224 return ret;
225 fmts += ret;
226 }
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300227
228 if (!fmts)
229 return -ENXIO;
230
231 icd->user_formats =
232 vmalloc(fmts * sizeof(struct soc_camera_format_xlate));
233 if (!icd->user_formats)
234 return -ENOMEM;
235
236 icd->num_user_formats = fmts;
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300237
238 dev_dbg(&icd->dev, "Found %d supported formats.\n", fmts);
239
240 /* Second pass - actually fill data formats */
Stefan Herbrechtsmeier14df2cc2009-06-24 10:31:25 -0300241 fmts = 0;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300242 for (i = 0; i < raw_fmts; i++)
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300243 if (!ici->ops->get_formats) {
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300244 v4l2_subdev_call(sd, video, enum_mbus_fmt, i, &code);
245 icd->user_formats[i].host_fmt =
246 soc_mbus_get_fmtdesc(code);
247 icd->user_formats[i].code = code;
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300248 } else {
Guennadi Liakhovetskifa489842009-08-25 11:46:43 -0300249 ret = ici->ops->get_formats(icd, i,
250 &icd->user_formats[fmts]);
251 if (ret < 0)
252 goto egfmt;
253 fmts += ret;
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300254 }
255
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300256 icd->current_fmt = &icd->user_formats[0];
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300257
258 return 0;
Guennadi Liakhovetskifa489842009-08-25 11:46:43 -0300259
260egfmt:
261 icd->num_user_formats = 0;
262 vfree(icd->user_formats);
263 return ret;
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300264}
265
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300266/* Always entered with .video_lock held */
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300267static void soc_camera_free_user_formats(struct soc_camera_device *icd)
268{
Guennadi Liakhovetskifa489842009-08-25 11:46:43 -0300269 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
270
271 if (ici->ops->put_formats)
272 ici->ops->put_formats(icd);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300273 icd->current_fmt = NULL;
Guennadi Liakhovetskifa489842009-08-25 11:46:43 -0300274 icd->num_user_formats = 0;
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300275 vfree(icd->user_formats);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300276 icd->user_formats = NULL;
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300277}
278
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300279#define pixfmtstr(x) (x) & 0xff, ((x) >> 8) & 0xff, ((x) >> 16) & 0xff, \
280 ((x) >> 24) & 0xff
281
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300282/* Called with .vb_lock held, or from the first open(2), see comment there */
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300283static int soc_camera_set_fmt(struct soc_camera_file *icf,
284 struct v4l2_format *f)
285{
286 struct soc_camera_device *icd = icf->icd;
287 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
288 struct v4l2_pix_format *pix = &f->fmt.pix;
289 int ret;
290
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300291 dev_dbg(&icd->dev, "S_FMT(%c%c%c%c, %ux%u)\n",
292 pixfmtstr(pix->pixelformat), pix->width, pix->height);
293
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300294 /* We always call try_fmt() before set_fmt() or set_crop() */
295 ret = ici->ops->try_fmt(icd, f);
296 if (ret < 0)
297 return ret;
298
299 ret = ici->ops->set_fmt(icd, f);
300 if (ret < 0) {
301 return ret;
302 } else if (!icd->current_fmt ||
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300303 icd->current_fmt->host_fmt->fourcc != pix->pixelformat) {
Guennadi Liakhovetski2aa58db2009-08-25 11:47:00 -0300304 dev_err(&icd->dev,
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300305 "Host driver hasn't set up current format correctly!\n");
306 return -EINVAL;
307 }
308
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300309 icd->user_width = pix->width;
310 icd->user_height = pix->height;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300311 icd->colorspace = pix->colorspace;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300312 icf->vb_vidq.field =
313 icd->field = pix->field;
Guennadi Liakhovetski025c18a2009-03-13 06:08:20 -0300314
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300315 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
316 dev_warn(&icd->dev, "Attention! Wrong buf-type %d\n",
317 f->type);
318
319 dev_dbg(&icd->dev, "set width: %d height: %d\n",
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300320 icd->user_width, icd->user_height);
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300321
322 /* set physical bus parameters */
323 return ici->ops->set_bus_param(icd, pix->pixelformat);
324}
325
Hans Verkuilbec43662008-12-30 06:58:20 -0300326static int soc_camera_open(struct file *file)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300327{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300328 struct video_device *vdev = video_devdata(file);
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300329 struct soc_camera_device *icd = container_of(vdev->parent,
330 struct soc_camera_device,
331 dev);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300332 struct soc_camera_link *icl = to_soc_camera_link(icd);
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300333 struct soc_camera_host *ici;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300334 struct soc_camera_file *icf;
335 int ret;
336
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300337 if (!icd->ops)
338 /* No device driver attached */
339 return -ENODEV;
340
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300341 ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300342
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300343 icf = vmalloc(sizeof(*icf));
344 if (!icf)
345 return -ENOMEM;
346
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300347 if (!try_module_get(ici->ops->owner)) {
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300348 dev_err(&icd->dev, "Couldn't lock capture bus driver.\n");
349 ret = -EINVAL;
350 goto emgi;
351 }
352
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300353 /*
354 * Protect against icd->ops->remove() until we module_get() both
355 * drivers.
356 */
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300357 mutex_lock(&icd->video_lock);
358
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300359 icf->icd = icd;
Guennadi Liakhovetski1a0063a2008-04-04 13:46:34 -0300360 icd->use_count++;
361
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300362 /* Now we really have to activate the camera */
363 if (icd->use_count == 1) {
Guennadi Liakhovetski025c18a2009-03-13 06:08:20 -0300364 /* Restore parameters before the last close() per V4L2 API */
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300365 struct v4l2_format f = {
366 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
367 .fmt.pix = {
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300368 .width = icd->user_width,
369 .height = icd->user_height,
Guennadi Liakhovetski025c18a2009-03-13 06:08:20 -0300370 .field = icd->field,
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300371 .colorspace = icd->colorspace,
372 .pixelformat =
373 icd->current_fmt->host_fmt->fourcc,
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300374 },
375 };
376
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300377 if (icl->power) {
378 ret = icl->power(icd->pdev, 1);
379 if (ret < 0)
380 goto epower;
381 }
382
383 /* The camera could have been already on, try to reset */
384 if (icl->reset)
385 icl->reset(icd->pdev);
386
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300387 ret = ici->ops->add(icd);
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300388 if (ret < 0) {
389 dev_err(&icd->dev, "Couldn't activate the camera: %d\n", ret);
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300390 goto eiciadd;
391 }
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300392
Mauro Carvalho Chehab4f9fb5e2010-05-18 00:46:09 -0300393 pm_runtime_enable(&icd->vdev->dev);
394 ret = pm_runtime_resume(&icd->vdev->dev);
395 if (ret < 0 && ret != -ENOSYS)
396 goto eresume;
397
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300398 /*
399 * Try to configure with default parameters. Notice: this is the
400 * very first open, so, we cannot race against other calls,
401 * apart from someone else calling open() simultaneously, but
402 * .video_lock is protecting us against it.
403 */
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300404 ret = soc_camera_set_fmt(icf, &f);
405 if (ret < 0)
406 goto esfmt;
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300407 }
408
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300409 file->private_data = icf;
410 dev_dbg(&icd->dev, "camera device open\n");
411
Magnus Damma034d1b2008-07-11 20:59:34 -0300412 ici->ops->init_videobuf(&icf->vb_vidq, icd);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300413
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300414 mutex_unlock(&icd->video_lock);
415
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300416 return 0;
417
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300418 /*
Mauro Carvalho Chehab4f9fb5e2010-05-18 00:46:09 -0300419 * First four errors are entered with the .video_lock held
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300420 * and use_count == 1
421 */
422esfmt:
Mauro Carvalho Chehab4f9fb5e2010-05-18 00:46:09 -0300423 pm_runtime_disable(&icd->vdev->dev);
424eresume:
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300425 ici->ops->remove(icd);
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300426eiciadd:
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300427 if (icl->power)
428 icl->power(icd->pdev, 0);
429epower:
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300430 icd->use_count--;
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300431 mutex_unlock(&icd->video_lock);
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300432 module_put(ici->ops->owner);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300433emgi:
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300434 vfree(icf);
435 return ret;
436}
437
Hans Verkuilbec43662008-12-30 06:58:20 -0300438static int soc_camera_close(struct file *file)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300439{
440 struct soc_camera_file *icf = file->private_data;
441 struct soc_camera_device *icd = icf->icd;
442 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300443
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300444 mutex_lock(&icd->video_lock);
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300445 icd->use_count--;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300446 if (!icd->use_count) {
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300447 struct soc_camera_link *icl = to_soc_camera_link(icd);
448
Mauro Carvalho Chehab4f9fb5e2010-05-18 00:46:09 -0300449 pm_runtime_suspend(&icd->vdev->dev);
450 pm_runtime_disable(&icd->vdev->dev);
451
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300452 ici->ops->remove(icd);
Mauro Carvalho Chehab4f9fb5e2010-05-18 00:46:09 -0300453
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300454 if (icl->power)
455 icl->power(icd->pdev, 0);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300456 }
Guennadi Liakhovetski025c18a2009-03-13 06:08:20 -0300457
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300458 mutex_unlock(&icd->video_lock);
459
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300460 module_put(ici->ops->owner);
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300461
Guennadi Liakhovetski1a0063a2008-04-04 13:46:34 -0300462 vfree(icf);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300463
Guennadi Liakhovetski2aa58db2009-08-25 11:47:00 -0300464 dev_dbg(&icd->dev, "camera device close\n");
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300465
466 return 0;
467}
468
Andrew Mortonaba360d2008-04-22 14:45:59 -0300469static ssize_t soc_camera_read(struct file *file, char __user *buf,
Guennadi Liakhovetskiabe4c472008-12-01 09:44:56 -0300470 size_t count, loff_t *ppos)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300471{
472 struct soc_camera_file *icf = file->private_data;
473 struct soc_camera_device *icd = icf->icd;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300474 int err = -EINVAL;
475
Guennadi Liakhovetski2aa58db2009-08-25 11:47:00 -0300476 dev_err(&icd->dev, "camera device read not implemented\n");
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300477
478 return err;
479}
480
481static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma)
482{
483 struct soc_camera_file *icf = file->private_data;
484 struct soc_camera_device *icd = icf->icd;
485 int err;
486
487 dev_dbg(&icd->dev, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
488
489 err = videobuf_mmap_mapper(&icf->vb_vidq, vma);
490
491 dev_dbg(&icd->dev, "vma start=0x%08lx, size=%ld, ret=%d\n",
492 (unsigned long)vma->vm_start,
493 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start,
494 err);
495
496 return err;
497}
498
499static unsigned int soc_camera_poll(struct file *file, poll_table *pt)
500{
501 struct soc_camera_file *icf = file->private_data;
502 struct soc_camera_device *icd = icf->icd;
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -0300503 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300504
505 if (list_empty(&icf->vb_vidq.stream)) {
506 dev_err(&icd->dev, "Trying to poll with no queued buffers!\n");
507 return POLLERR;
508 }
509
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300510 return ici->ops->poll(file, pt);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300511}
512
Hans Verkuilbec43662008-12-30 06:58:20 -0300513static struct v4l2_file_operations soc_camera_fops = {
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300514 .owner = THIS_MODULE,
515 .open = soc_camera_open,
516 .release = soc_camera_close,
517 .ioctl = video_ioctl2,
518 .read = soc_camera_read,
519 .mmap = soc_camera_mmap,
520 .poll = soc_camera_poll,
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300521};
522
Hans Verkuil72937892008-06-01 10:35:18 -0300523static int soc_camera_s_fmt_vid_cap(struct file *file, void *priv,
Guennadi Liakhovetskiabe4c472008-12-01 09:44:56 -0300524 struct v4l2_format *f)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300525{
526 struct soc_camera_file *icf = file->private_data;
527 struct soc_camera_device *icd = icf->icd;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300528 int ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300529
530 WARN_ON(priv != file->private_data);
531
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300532 mutex_lock(&icf->vb_vidq.vb_lock);
533
Guennadi Liakhovetskib897a912009-08-25 11:46:53 -0300534 if (icf->vb_vidq.bufs[0]) {
535 dev_err(&icd->dev, "S_FMT denied: queue initialised\n");
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300536 ret = -EBUSY;
537 goto unlock;
538 }
539
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300540 ret = soc_camera_set_fmt(icf, f);
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300541
542unlock:
543 mutex_unlock(&icf->vb_vidq.vb_lock);
544
545 return ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300546}
547
Hans Verkuil72937892008-06-01 10:35:18 -0300548static int soc_camera_enum_fmt_vid_cap(struct file *file, void *priv,
Guennadi Liakhovetskiabe4c472008-12-01 09:44:56 -0300549 struct v4l2_fmtdesc *f)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300550{
551 struct soc_camera_file *icf = file->private_data;
552 struct soc_camera_device *icd = icf->icd;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300553 const struct soc_mbus_pixelfmt *format;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300554
555 WARN_ON(priv != file->private_data);
556
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300557 if (f->index >= icd->num_user_formats)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300558 return -EINVAL;
559
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300560 format = icd->user_formats[f->index].host_fmt;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300561
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300562 if (format->name)
563 strlcpy(f->description, format->name, sizeof(f->description));
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300564 f->pixelformat = format->fourcc;
565 return 0;
566}
567
Hans Verkuil72937892008-06-01 10:35:18 -0300568static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv,
Guennadi Liakhovetskiabe4c472008-12-01 09:44:56 -0300569 struct v4l2_format *f)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300570{
571 struct soc_camera_file *icf = file->private_data;
572 struct soc_camera_device *icd = icf->icd;
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -0300573 struct v4l2_pix_format *pix = &f->fmt.pix;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300574
575 WARN_ON(priv != file->private_data);
576
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300577 pix->width = icd->user_width;
578 pix->height = icd->user_height;
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -0300579 pix->field = icf->vb_vidq.field;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300580 pix->pixelformat = icd->current_fmt->host_fmt->fourcc;
581 pix->bytesperline = soc_mbus_bytes_per_line(pix->width,
582 icd->current_fmt->host_fmt);
583 pix->colorspace = icd->colorspace;
584 if (pix->bytesperline < 0)
585 return pix->bytesperline;
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -0300586 pix->sizeimage = pix->height * pix->bytesperline;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300587 dev_dbg(&icd->dev, "current_fmt->fourcc: 0x%08x\n",
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300588 icd->current_fmt->host_fmt->fourcc);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300589 return 0;
590}
591
592static int soc_camera_querycap(struct file *file, void *priv,
593 struct v4l2_capability *cap)
594{
595 struct soc_camera_file *icf = file->private_data;
596 struct soc_camera_device *icd = icf->icd;
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -0300597 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300598
599 WARN_ON(priv != file->private_data);
600
601 strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver));
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300602 return ici->ops->querycap(ici, cap);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300603}
604
605static int soc_camera_streamon(struct file *file, void *priv,
606 enum v4l2_buf_type i)
607{
608 struct soc_camera_file *icf = file->private_data;
609 struct soc_camera_device *icd = icf->icd;
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300610 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300611 int ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300612
613 WARN_ON(priv != file->private_data);
614
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300615 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
616 return -EINVAL;
617
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300618 mutex_lock(&icd->video_lock);
619
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300620 v4l2_subdev_call(sd, video, s_stream, 1);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300621
622 /* This calls buf_queue from host driver's videobuf_queue_ops */
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300623 ret = videobuf_streamon(&icf->vb_vidq);
624
625 mutex_unlock(&icd->video_lock);
626
627 return ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300628}
629
630static int soc_camera_streamoff(struct file *file, void *priv,
631 enum v4l2_buf_type i)
632{
633 struct soc_camera_file *icf = file->private_data;
634 struct soc_camera_device *icd = icf->icd;
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300635 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300636
637 WARN_ON(priv != file->private_data);
638
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300639 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
640 return -EINVAL;
641
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300642 mutex_lock(&icd->video_lock);
643
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300644 /*
645 * This calls buf_release from host driver's videobuf_queue_ops for all
646 * remaining buffers. When the last buffer is freed, stop capture
647 */
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300648 videobuf_streamoff(&icf->vb_vidq);
649
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300650 v4l2_subdev_call(sd, video, s_stream, 0);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300651
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300652 mutex_unlock(&icd->video_lock);
653
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300654 return 0;
655}
656
657static int soc_camera_queryctrl(struct file *file, void *priv,
658 struct v4l2_queryctrl *qc)
659{
660 struct soc_camera_file *icf = file->private_data;
661 struct soc_camera_device *icd = icf->icd;
Guennadi Liakhovetski2840d242009-08-25 11:44:15 -0300662 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300663 int i;
664
665 WARN_ON(priv != file->private_data);
666
667 if (!qc->id)
668 return -EINVAL;
669
Guennadi Liakhovetski2840d242009-08-25 11:44:15 -0300670 /* First check host controls */
671 for (i = 0; i < ici->ops->num_controls; i++)
672 if (qc->id == ici->ops->controls[i].id) {
673 memcpy(qc, &(ici->ops->controls[i]),
674 sizeof(*qc));
675 return 0;
676 }
677
678 /* Then device controls */
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300679 for (i = 0; i < icd->ops->num_controls; i++)
680 if (qc->id == icd->ops->controls[i].id) {
681 memcpy(qc, &(icd->ops->controls[i]),
682 sizeof(*qc));
683 return 0;
684 }
685
686 return -EINVAL;
687}
688
689static int soc_camera_g_ctrl(struct file *file, void *priv,
690 struct v4l2_control *ctrl)
691{
692 struct soc_camera_file *icf = file->private_data;
693 struct soc_camera_device *icd = icf->icd;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300694 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300695 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski2840d242009-08-25 11:44:15 -0300696 int ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300697
698 WARN_ON(priv != file->private_data);
699
Guennadi Liakhovetski2840d242009-08-25 11:44:15 -0300700 if (ici->ops->get_ctrl) {
701 ret = ici->ops->get_ctrl(icd, ctrl);
702 if (ret != -ENOIOCTLCMD)
703 return ret;
704 }
705
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300706 return v4l2_subdev_call(sd, core, g_ctrl, ctrl);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300707}
708
709static int soc_camera_s_ctrl(struct file *file, void *priv,
710 struct v4l2_control *ctrl)
711{
712 struct soc_camera_file *icf = file->private_data;
713 struct soc_camera_device *icd = icf->icd;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300714 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300715 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski2840d242009-08-25 11:44:15 -0300716 int ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300717
718 WARN_ON(priv != file->private_data);
719
Guennadi Liakhovetski2840d242009-08-25 11:44:15 -0300720 if (ici->ops->set_ctrl) {
721 ret = ici->ops->set_ctrl(icd, ctrl);
722 if (ret != -ENOIOCTLCMD)
723 return ret;
724 }
725
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300726 return v4l2_subdev_call(sd, core, s_ctrl, ctrl);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300727}
728
729static int soc_camera_cropcap(struct file *file, void *fh,
730 struct v4l2_cropcap *a)
731{
732 struct soc_camera_file *icf = file->private_data;
733 struct soc_camera_device *icd = icf->icd;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300734 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300735
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300736 return ici->ops->cropcap(icd, a);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300737}
738
739static int soc_camera_g_crop(struct file *file, void *fh,
740 struct v4l2_crop *a)
741{
742 struct soc_camera_file *icf = file->private_data;
743 struct soc_camera_device *icd = icf->icd;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300744 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
745 int ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300746
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300747 mutex_lock(&icf->vb_vidq.vb_lock);
748 ret = ici->ops->get_crop(icd, a);
749 mutex_unlock(&icf->vb_vidq.vb_lock);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300750
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300751 return ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300752}
753
Guennadi Liakhovetski68a54f02009-08-25 11:46:52 -0300754/*
755 * According to the V4L2 API, drivers shall not update the struct v4l2_crop
756 * argument with the actual geometry, instead, the user shall use G_CROP to
Guennadi Liakhovetskiab56d5e2010-03-23 11:31:46 -0300757 * retrieve it.
Guennadi Liakhovetski68a54f02009-08-25 11:46:52 -0300758 */
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300759static int soc_camera_s_crop(struct file *file, void *fh,
760 struct v4l2_crop *a)
761{
762 struct soc_camera_file *icf = file->private_data;
763 struct soc_camera_device *icd = icf->icd;
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -0300764 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300765 struct v4l2_rect *rect = &a->c;
766 struct v4l2_crop current_crop;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300767 int ret;
768
769 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
770 return -EINVAL;
771
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300772 dev_dbg(&icd->dev, "S_CROP(%ux%u@%u:%u)\n",
773 rect->width, rect->height, rect->left, rect->top);
774
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300775 /* Cropping is allowed during a running capture, guard consistency */
776 mutex_lock(&icf->vb_vidq.vb_lock);
777
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300778 /* If get_crop fails, we'll let host and / or client drivers decide */
779 ret = ici->ops->get_crop(icd, &current_crop);
780
Guennadi Liakhovetskib897a912009-08-25 11:46:53 -0300781 /* Prohibit window size change with initialised buffers */
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300782 if (icf->vb_vidq.bufs[0] && !ret &&
783 (a->c.width != current_crop.c.width ||
784 a->c.height != current_crop.c.height)) {
Guennadi Liakhovetskib897a912009-08-25 11:46:53 -0300785 dev_err(&icd->dev,
786 "S_CROP denied: queue initialised and sizes differ\n");
787 ret = -EBUSY;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300788 } else {
789 ret = ici->ops->set_crop(icd, a);
Guennadi Liakhovetskib897a912009-08-25 11:46:53 -0300790 }
791
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300792 mutex_unlock(&icf->vb_vidq.vb_lock);
793
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300794 return ret;
795}
796
Guennadi Liakhovetskic9f6ef62010-02-09 18:00:30 +0100797static int soc_camera_g_parm(struct file *file, void *fh,
798 struct v4l2_streamparm *a)
799{
800 struct soc_camera_file *icf = file->private_data;
801 struct soc_camera_device *icd = icf->icd;
802 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
803
804 if (ici->ops->get_parm)
805 return ici->ops->get_parm(icd, a);
806
807 return -ENOIOCTLCMD;
808}
809
810static int soc_camera_s_parm(struct file *file, void *fh,
811 struct v4l2_streamparm *a)
812{
813 struct soc_camera_file *icf = file->private_data;
814 struct soc_camera_device *icd = icf->icd;
815 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
816
817 if (ici->ops->set_parm)
818 return ici->ops->set_parm(icd, a);
819
820 return -ENOIOCTLCMD;
821}
822
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300823static int soc_camera_g_chip_ident(struct file *file, void *fh,
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300824 struct v4l2_dbg_chip_ident *id)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300825{
826 struct soc_camera_file *icf = file->private_data;
827 struct soc_camera_device *icd = icf->icd;
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300828 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300829
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300830 return v4l2_subdev_call(sd, core, g_chip_ident, id);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300831}
832
833#ifdef CONFIG_VIDEO_ADV_DEBUG
834static int soc_camera_g_register(struct file *file, void *fh,
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300835 struct v4l2_dbg_register *reg)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300836{
837 struct soc_camera_file *icf = file->private_data;
838 struct soc_camera_device *icd = icf->icd;
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300839 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300840
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300841 return v4l2_subdev_call(sd, core, g_register, reg);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300842}
843
844static int soc_camera_s_register(struct file *file, void *fh,
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300845 struct v4l2_dbg_register *reg)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300846{
847 struct soc_camera_file *icf = file->private_data;
848 struct soc_camera_device *icd = icf->icd;
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300849 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300850
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300851 return v4l2_subdev_call(sd, core, s_register, reg);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300852}
853#endif
854
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300855/* So far this function cannot fail */
856static void scan_add_host(struct soc_camera_host *ici)
857{
858 struct soc_camera_device *icd;
859
860 mutex_lock(&list_lock);
861
862 list_for_each_entry(icd, &devices, list) {
863 if (icd->iface == ici->nr) {
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300864 int ret;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300865 icd->dev.parent = ici->v4l2_dev.dev;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300866 dev_set_name(&icd->dev, "%u-%u", icd->iface,
867 icd->devnum);
868 ret = device_register(&icd->dev);
869 if (ret < 0) {
870 icd->dev.parent = NULL;
871 dev_err(&icd->dev,
872 "Cannot register device: %d\n", ret);
873 }
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300874 }
875 }
876
877 mutex_unlock(&list_lock);
878}
879
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300880#ifdef CONFIG_I2C_BOARDINFO
881static int soc_camera_init_i2c(struct soc_camera_device *icd,
882 struct soc_camera_link *icl)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300883{
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300884 struct i2c_client *client;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300885 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300886 struct i2c_adapter *adap = i2c_get_adapter(icl->i2c_adapter_id);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300887 struct v4l2_subdev *subdev;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300888
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300889 if (!adap) {
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300890 dev_err(&icd->dev, "Cannot get I2C adapter #%d. No driver?\n",
891 icl->i2c_adapter_id);
892 goto ei2cga;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300893 }
894
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300895 icl->board_info->platform_data = icd;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300896
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300897 subdev = v4l2_i2c_new_subdev_board(&ici->v4l2_dev, adap,
898 icl->module_name, icl->board_info, NULL);
Magnus Dammd74f8412010-02-09 17:40:40 +0900899 if (!subdev)
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300900 goto ei2cnd;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300901
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300902 client = subdev->priv;
903
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300904 /* Use to_i2c_client(dev) to recover the i2c client */
905 dev_set_drvdata(&icd->dev, &client->dev);
906
907 return 0;
908ei2cnd:
909 i2c_put_adapter(adap);
910ei2cga:
Magnus Dammd74f8412010-02-09 17:40:40 +0900911 return -ENODEV;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300912}
913
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300914static void soc_camera_free_i2c(struct soc_camera_device *icd)
915{
916 struct i2c_client *client =
917 to_i2c_client(to_soc_camera_control(icd));
918 dev_set_drvdata(&icd->dev, NULL);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300919 v4l2_device_unregister_subdev(i2c_get_clientdata(client));
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300920 i2c_unregister_device(client);
921 i2c_put_adapter(client->adapter);
922}
923#else
924#define soc_camera_init_i2c(icd, icl) (-ENODEV)
925#define soc_camera_free_i2c(icd) do {} while (0)
926#endif
927
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300928static int soc_camera_video_start(struct soc_camera_device *icd);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300929static int video_dev_create(struct soc_camera_device *icd);
930/* Called during host-driver probe */
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300931static int soc_camera_probe(struct device *dev)
932{
933 struct soc_camera_device *icd = to_soc_camera_dev(dev);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300934 struct soc_camera_host *ici = to_soc_camera_host(dev->parent);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300935 struct soc_camera_link *icl = to_soc_camera_link(icd);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300936 struct device *control = NULL;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300937 struct v4l2_subdev *sd;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300938 struct v4l2_mbus_framefmt mf;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300939 int ret;
940
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300941 dev_info(dev, "Probing %s\n", dev_name(dev));
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300942
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300943 if (icl->power) {
944 ret = icl->power(icd->pdev, 1);
945 if (ret < 0) {
946 dev_err(dev,
947 "Platform failed to power-on the camera.\n");
948 goto epower;
949 }
950 }
951
952 /* The camera could have been already on, try to reset */
953 if (icl->reset)
954 icl->reset(icd->pdev);
955
956 ret = ici->ops->add(icd);
957 if (ret < 0)
958 goto eadd;
959
960 /* Must have icd->vdev before registering the device */
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300961 ret = video_dev_create(icd);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300962 if (ret < 0)
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300963 goto evdc;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300964
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300965 /* Non-i2c cameras, e.g., soc_camera_platform, have no board_info */
966 if (icl->board_info) {
967 ret = soc_camera_init_i2c(icd, icl);
968 if (ret < 0)
969 goto eadddev;
970 } else if (!icl->add_device || !icl->del_device) {
971 ret = -EINVAL;
972 goto eadddev;
973 } else {
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300974 if (icl->module_name)
975 ret = request_module(icl->module_name);
976
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300977 ret = icl->add_device(icl, &icd->dev);
978 if (ret < 0)
979 goto eadddev;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300980
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300981 /*
982 * FIXME: this is racy, have to use driver-binding notification,
983 * when it is available
984 */
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300985 control = to_soc_camera_control(icd);
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -0300986 if (!control || !control->driver || !dev_get_drvdata(control) ||
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300987 !try_module_get(control->driver->owner)) {
988 icl->del_device(icl);
989 goto enodrv;
990 }
Guennadi Liakhovetski025c18a2009-03-13 06:08:20 -0300991 }
992
Guennadi Liakhovetskifa489842009-08-25 11:46:43 -0300993 /* At this point client .probe() should have run already */
994 ret = soc_camera_init_user_formats(icd);
995 if (ret < 0)
996 goto eiufmt;
997
Guennadi Liakhovetskifa489842009-08-25 11:46:43 -0300998 icd->field = V4L2_FIELD_ANY;
999
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001000 /* ..._video_start() will create a device node, so we have to protect */
1001 mutex_lock(&icd->video_lock);
1002
1003 ret = soc_camera_video_start(icd);
1004 if (ret < 0)
1005 goto evidstart;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001006
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001007 /* Try to improve our guess of a reasonable window format */
1008 sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001009 if (!v4l2_subdev_call(sd, video, g_mbus_fmt, &mf)) {
1010 icd->user_width = mf.width;
1011 icd->user_height = mf.height;
1012 icd->colorspace = mf.colorspace;
1013 icd->field = mf.field;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001014 }
1015
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001016 /* Do we have to sysfs_remove_link() before device_unregister()? */
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001017 if (sysfs_create_link(&icd->dev.kobj, &to_soc_camera_control(icd)->kobj,
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001018 "control"))
1019 dev_warn(&icd->dev, "Failed creating the control symlink\n");
1020
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001021 ici->ops->remove(icd);
1022
1023 if (icl->power)
1024 icl->power(icd->pdev, 0);
1025
1026 mutex_unlock(&icd->video_lock);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001027
1028 return 0;
1029
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001030evidstart:
1031 mutex_unlock(&icd->video_lock);
Guennadi Liakhovetskifa489842009-08-25 11:46:43 -03001032 soc_camera_free_user_formats(icd);
1033eiufmt:
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001034 if (icl->board_info) {
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001035 soc_camera_free_i2c(icd);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001036 } else {
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001037 icl->del_device(icl);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001038 module_put(control->driver->owner);
1039 }
1040enodrv:
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001041eadddev:
1042 video_device_release(icd->vdev);
1043evdc:
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001044 ici->ops->remove(icd);
1045eadd:
1046 if (icl->power)
1047 icl->power(icd->pdev, 0);
1048epower:
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001049 return ret;
1050}
1051
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -03001052/*
1053 * This is called on device_unregister, which only means we have to disconnect
1054 * from the host, but not remove ourselves from the device list
1055 */
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001056static int soc_camera_remove(struct device *dev)
1057{
1058 struct soc_camera_device *icd = to_soc_camera_dev(dev);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001059 struct soc_camera_link *icl = to_soc_camera_link(icd);
1060 struct video_device *vdev = icd->vdev;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001061
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001062 BUG_ON(!dev->parent);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001063
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001064 if (vdev) {
1065 mutex_lock(&icd->video_lock);
1066 video_unregister_device(vdev);
1067 icd->vdev = NULL;
1068 mutex_unlock(&icd->video_lock);
1069 }
1070
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001071 if (icl->board_info) {
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001072 soc_camera_free_i2c(icd);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001073 } else {
1074 struct device_driver *drv = to_soc_camera_control(icd) ?
1075 to_soc_camera_control(icd)->driver : NULL;
1076 if (drv) {
1077 icl->del_device(icl);
1078 module_put(drv->owner);
1079 }
1080 }
Guennadi Liakhovetskifa489842009-08-25 11:46:43 -03001081 soc_camera_free_user_formats(icd);
Guennadi Liakhovetski025c18a2009-03-13 06:08:20 -03001082
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001083 return 0;
1084}
1085
Robert Jarzmik2e521062008-08-01 20:14:50 -03001086static int soc_camera_suspend(struct device *dev, pm_message_t state)
1087{
1088 struct soc_camera_device *icd = to_soc_camera_dev(dev);
1089 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1090 int ret = 0;
1091
1092 if (ici->ops->suspend)
1093 ret = ici->ops->suspend(icd, state);
1094
1095 return ret;
1096}
1097
1098static int soc_camera_resume(struct device *dev)
1099{
1100 struct soc_camera_device *icd = to_soc_camera_dev(dev);
1101 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1102 int ret = 0;
1103
1104 if (ici->ops->resume)
1105 ret = ici->ops->resume(icd);
1106
1107 return ret;
1108}
1109
Guennadi Liakhovetski52d268a2010-07-26 11:37:13 -03001110struct bus_type soc_camera_bus_type = {
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001111 .name = "soc-camera",
1112 .probe = soc_camera_probe,
1113 .remove = soc_camera_remove,
Robert Jarzmik2e521062008-08-01 20:14:50 -03001114 .suspend = soc_camera_suspend,
1115 .resume = soc_camera_resume,
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001116};
Guennadi Liakhovetski52d268a2010-07-26 11:37:13 -03001117EXPORT_SYMBOL_GPL(soc_camera_bus_type);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001118
1119static struct device_driver ic_drv = {
1120 .name = "camera",
1121 .bus = &soc_camera_bus_type,
1122 .owner = THIS_MODULE,
1123};
1124
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001125static void dummy_release(struct device *dev)
1126{
1127}
1128
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001129static int default_cropcap(struct soc_camera_device *icd,
1130 struct v4l2_cropcap *a)
1131{
1132 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1133 return v4l2_subdev_call(sd, video, cropcap, a);
1134}
1135
1136static int default_g_crop(struct soc_camera_device *icd, struct v4l2_crop *a)
1137{
1138 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1139 return v4l2_subdev_call(sd, video, g_crop, a);
1140}
1141
1142static int default_s_crop(struct soc_camera_device *icd, struct v4l2_crop *a)
1143{
1144 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1145 return v4l2_subdev_call(sd, video, s_crop, a);
1146}
1147
Guennadi Liakhovetski64ff9ba2009-11-09 16:12:37 -03001148static void soc_camera_device_init(struct device *dev, void *pdata)
1149{
1150 dev->platform_data = pdata;
1151 dev->bus = &soc_camera_bus_type;
1152 dev->release = dummy_release;
1153}
1154
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -03001155int soc_camera_host_register(struct soc_camera_host *ici)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001156{
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001157 struct soc_camera_host *ix;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001158 int ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001159
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -03001160 if (!ici || !ici->ops ||
1161 !ici->ops->try_fmt ||
1162 !ici->ops->set_fmt ||
1163 !ici->ops->set_bus_param ||
1164 !ici->ops->querycap ||
1165 !ici->ops->init_videobuf ||
1166 !ici->ops->reqbufs ||
1167 !ici->ops->add ||
1168 !ici->ops->remove ||
Guennadi Liakhovetskieff505f2009-04-24 12:55:48 -03001169 !ici->ops->poll ||
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001170 !ici->v4l2_dev.dev)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001171 return -EINVAL;
1172
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001173 if (!ici->ops->set_crop)
1174 ici->ops->set_crop = default_s_crop;
1175 if (!ici->ops->get_crop)
1176 ici->ops->get_crop = default_g_crop;
1177 if (!ici->ops->cropcap)
1178 ici->ops->cropcap = default_cropcap;
1179
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001180 mutex_lock(&list_lock);
1181 list_for_each_entry(ix, &hosts, list) {
1182 if (ix->nr == ici->nr) {
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001183 ret = -EBUSY;
1184 goto edevreg;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001185 }
1186 }
1187
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001188 ret = v4l2_device_register(ici->v4l2_dev.dev, &ici->v4l2_dev);
1189 if (ret < 0)
1190 goto edevreg;
Guennadi Liakhovetskieff505f2009-04-24 12:55:48 -03001191
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001192 list_add_tail(&ici->list, &hosts);
1193 mutex_unlock(&list_lock);
1194
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001195 scan_add_host(ici);
1196
1197 return 0;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001198
1199edevreg:
1200 mutex_unlock(&list_lock);
1201 return ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001202}
1203EXPORT_SYMBOL(soc_camera_host_register);
1204
1205/* Unregister all clients! */
1206void soc_camera_host_unregister(struct soc_camera_host *ici)
1207{
1208 struct soc_camera_device *icd;
1209
1210 mutex_lock(&list_lock);
1211
1212 list_del(&ici->list);
1213
1214 list_for_each_entry(icd, &devices, list) {
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001215 if (icd->iface == ici->nr) {
Guennadi Liakhovetski64ff9ba2009-11-09 16:12:37 -03001216 void *pdata = icd->dev.platform_data;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001217 /* The bus->remove will be called */
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001218 device_unregister(&icd->dev);
Guennadi Liakhovetski76823b72009-10-05 12:54:54 -03001219 /*
1220 * Not before device_unregister(), .remove
1221 * needs parent to call ici->ops->remove().
1222 * If the host module is loaded again, device_register()
1223 * would complain "already initialised," since 2.6.32
1224 * this is also needed to prevent use-after-free of the
1225 * device private data.
1226 */
1227 memset(&icd->dev, 0, sizeof(icd->dev));
Guennadi Liakhovetski64ff9ba2009-11-09 16:12:37 -03001228 soc_camera_device_init(&icd->dev, pdata);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001229 }
1230 }
1231
1232 mutex_unlock(&list_lock);
1233
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001234 v4l2_device_unregister(&ici->v4l2_dev);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001235}
1236EXPORT_SYMBOL(soc_camera_host_unregister);
1237
1238/* Image capture device */
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001239static int soc_camera_device_register(struct soc_camera_device *icd)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001240{
1241 struct soc_camera_device *ix;
1242 int num = -1, i;
1243
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001244 for (i = 0; i < 256 && num < 0; i++) {
1245 num = i;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001246 /* Check if this index is available on this interface */
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001247 list_for_each_entry(ix, &devices, list) {
1248 if (ix->iface == icd->iface && ix->devnum == i) {
1249 num = -1;
1250 break;
1251 }
1252 }
1253 }
1254
1255 if (num < 0)
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -03001256 /*
1257 * ok, we have 256 cameras on this host...
1258 * man, stay reasonable...
1259 */
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001260 return -ENOMEM;
1261
Guennadi Liakhovetski64ff9ba2009-11-09 16:12:37 -03001262 icd->devnum = num;
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -03001263 icd->use_count = 0;
1264 icd->host_priv = NULL;
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -03001265 mutex_init(&icd->video_lock);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001266
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001267 list_add_tail(&icd->list, &devices);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001268
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001269 return 0;
1270}
1271
1272static void soc_camera_device_unregister(struct soc_camera_device *icd)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001273{
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001274 list_del(&icd->list);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001275}
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001276
Hans Verkuila3998102008-07-21 02:57:38 -03001277static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
1278 .vidioc_querycap = soc_camera_querycap,
1279 .vidioc_g_fmt_vid_cap = soc_camera_g_fmt_vid_cap,
1280 .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
1281 .vidioc_s_fmt_vid_cap = soc_camera_s_fmt_vid_cap,
1282 .vidioc_enum_input = soc_camera_enum_input,
1283 .vidioc_g_input = soc_camera_g_input,
1284 .vidioc_s_input = soc_camera_s_input,
1285 .vidioc_s_std = soc_camera_s_std,
1286 .vidioc_reqbufs = soc_camera_reqbufs,
1287 .vidioc_try_fmt_vid_cap = soc_camera_try_fmt_vid_cap,
1288 .vidioc_querybuf = soc_camera_querybuf,
1289 .vidioc_qbuf = soc_camera_qbuf,
1290 .vidioc_dqbuf = soc_camera_dqbuf,
1291 .vidioc_streamon = soc_camera_streamon,
1292 .vidioc_streamoff = soc_camera_streamoff,
1293 .vidioc_queryctrl = soc_camera_queryctrl,
1294 .vidioc_g_ctrl = soc_camera_g_ctrl,
1295 .vidioc_s_ctrl = soc_camera_s_ctrl,
1296 .vidioc_cropcap = soc_camera_cropcap,
1297 .vidioc_g_crop = soc_camera_g_crop,
1298 .vidioc_s_crop = soc_camera_s_crop,
Guennadi Liakhovetskic9f6ef62010-02-09 18:00:30 +01001299 .vidioc_g_parm = soc_camera_g_parm,
1300 .vidioc_s_parm = soc_camera_s_parm,
Hans Verkuila3998102008-07-21 02:57:38 -03001301 .vidioc_g_chip_ident = soc_camera_g_chip_ident,
1302#ifdef CONFIG_VIDEO_ADV_DEBUG
1303 .vidioc_g_register = soc_camera_g_register,
1304 .vidioc_s_register = soc_camera_s_register,
1305#endif
1306};
1307
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001308static int video_dev_create(struct soc_camera_device *icd)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001309{
1310 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001311 struct video_device *vdev = video_device_alloc();
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001312
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001313 if (!vdev)
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001314 return -ENOMEM;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001315
1316 strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -03001317
Hans Verkuil5e85e732008-07-20 06:31:39 -03001318 vdev->parent = &icd->dev;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001319 vdev->current_norm = V4L2_STD_UNKNOWN;
1320 vdev->fops = &soc_camera_fops;
Hans Verkuila3998102008-07-21 02:57:38 -03001321 vdev->ioctl_ops = &soc_camera_ioctl_ops;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001322 vdev->release = video_device_release;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001323 vdev->tvnorms = V4L2_STD_UNKNOWN;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001324
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001325 icd->vdev = vdev;
1326
1327 return 0;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001328}
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001329
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001330/*
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001331 * Called from soc_camera_probe() above (with .video_lock held???)
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001332 */
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001333static int soc_camera_video_start(struct soc_camera_device *icd)
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001334{
Mauro Carvalho Chehab4f9fb5e2010-05-18 00:46:09 -03001335 struct device_type *type = icd->vdev->dev.type;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001336 int ret;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001337
1338 if (!icd->dev.parent)
1339 return -ENODEV;
1340
1341 if (!icd->ops ||
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001342 !icd->ops->query_bus_param ||
1343 !icd->ops->set_bus_param)
1344 return -EINVAL;
1345
Laurent Pinchart46b21092009-12-10 11:51:38 -02001346 ret = video_register_device(icd->vdev, VFL_TYPE_GRABBER, -1);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001347 if (ret < 0) {
1348 dev_err(&icd->dev, "video_register_device failed: %d\n", ret);
1349 return ret;
1350 }
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001351
Mauro Carvalho Chehab4f9fb5e2010-05-18 00:46:09 -03001352 /* Restore device type, possibly set by the subdevice driver */
1353 icd->vdev->dev.type = type;
1354
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001355 return 0;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001356}
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001357
Guennadi Liakhovetskic41deba2009-08-25 11:06:21 -03001358static int __devinit soc_camera_pdrv_probe(struct platform_device *pdev)
1359{
1360 struct soc_camera_link *icl = pdev->dev.platform_data;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001361 struct soc_camera_device *icd;
1362 int ret;
Guennadi Liakhovetskic41deba2009-08-25 11:06:21 -03001363
1364 if (!icl)
1365 return -EINVAL;
1366
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001367 icd = kzalloc(sizeof(*icd), GFP_KERNEL);
1368 if (!icd)
1369 return -ENOMEM;
Guennadi Liakhovetskic41deba2009-08-25 11:06:21 -03001370
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001371 icd->iface = icl->bus_id;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001372 icd->pdev = &pdev->dev;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001373 platform_set_drvdata(pdev, icd);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001374
1375 ret = soc_camera_device_register(icd);
1376 if (ret < 0)
1377 goto escdevreg;
1378
Guennadi Liakhovetski64ff9ba2009-11-09 16:12:37 -03001379 soc_camera_device_init(&icd->dev, icl);
1380
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001381 icd->user_width = DEFAULT_WIDTH;
1382 icd->user_height = DEFAULT_HEIGHT;
1383
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001384 return 0;
1385
1386escdevreg:
1387 kfree(icd);
1388
1389 return ret;
Guennadi Liakhovetskic41deba2009-08-25 11:06:21 -03001390}
1391
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -03001392/*
1393 * Only called on rmmod for each platform device, since they are not
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001394 * hot-pluggable. Now we know, that all our users - hosts and devices have
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -03001395 * been unloaded already
1396 */
Guennadi Liakhovetskic41deba2009-08-25 11:06:21 -03001397static int __devexit soc_camera_pdrv_remove(struct platform_device *pdev)
1398{
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001399 struct soc_camera_device *icd = platform_get_drvdata(pdev);
Guennadi Liakhovetskic41deba2009-08-25 11:06:21 -03001400
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001401 if (!icd)
1402 return -EINVAL;
1403
1404 soc_camera_device_unregister(icd);
1405
1406 kfree(icd);
1407
Guennadi Liakhovetski0fd327b2009-05-07 13:25:32 -03001408 return 0;
1409}
1410
1411static struct platform_driver __refdata soc_camera_pdrv = {
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001412 .remove = __devexit_p(soc_camera_pdrv_remove),
1413 .driver = {
1414 .name = "soc-camera-pdrv",
1415 .owner = THIS_MODULE,
Guennadi Liakhovetski0fd327b2009-05-07 13:25:32 -03001416 },
1417};
1418
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001419static int __init soc_camera_init(void)
1420{
1421 int ret = bus_register(&soc_camera_bus_type);
1422 if (ret)
1423 return ret;
1424 ret = driver_register(&ic_drv);
1425 if (ret)
1426 goto edrvr;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001427
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001428 ret = platform_driver_probe(&soc_camera_pdrv, soc_camera_pdrv_probe);
Guennadi Liakhovetski0fd327b2009-05-07 13:25:32 -03001429 if (ret)
1430 goto epdr;
1431
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001432 return 0;
1433
Guennadi Liakhovetski0fd327b2009-05-07 13:25:32 -03001434epdr:
1435 driver_unregister(&ic_drv);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001436edrvr:
1437 bus_unregister(&soc_camera_bus_type);
1438 return ret;
1439}
1440
1441static void __exit soc_camera_exit(void)
1442{
Guennadi Liakhovetski0fd327b2009-05-07 13:25:32 -03001443 platform_driver_unregister(&soc_camera_pdrv);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001444 driver_unregister(&ic_drv);
1445 bus_unregister(&soc_camera_bus_type);
1446}
1447
1448module_init(soc_camera_init);
1449module_exit(soc_camera_exit);
1450
1451MODULE_DESCRIPTION("Image capture bus driver");
1452MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
1453MODULE_LICENSE("GPL");
Guennadi Liakhovetski0fd327b2009-05-07 13:25:32 -03001454MODULE_ALIAS("platform:soc-camera-pdrv");