blob: a66811b43710a690c2a5d7ee77967ce40af1a6d4 [file] [log] [blame]
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001/*
2 * camera image capture (abstract) bus driver
3 *
4 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
5 *
6 * This driver provides an interface between platform-specific camera
7 * busses and camera devices. It should be used if the camera is
8 * connected not over a "proper" bus like PCI or USB, but over a
9 * special bus, like, for example, the Quick Capture interface on PXA270
10 * SoCs. Later it should also be used for i.MX31 SoCs from Freescale.
11 * It can handle multiple cameras and / or multiple busses, which can
12 * be used, e.g., in stereo-vision applications.
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License version 2 as
16 * published by the Free Software Foundation.
17 */
18
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -030019#include <linux/device.h>
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -030020#include <linux/err.h>
Guennadi Liakhovetski0fd327b2009-05-07 13:25:32 -030021#include <linux/i2c.h>
22#include <linux/init.h>
23#include <linux/list.h>
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -030024#include <linux/mutex.h>
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -030025#include <linux/module.h>
Guennadi Liakhovetski0fd327b2009-05-07 13:25:32 -030026#include <linux/platform_device.h>
Alberto Panizzo96e442c2010-12-02 07:43:37 -030027#include <linux/regulator/consumer.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Mauro Carvalho Chehab4f9fb5e2010-05-18 00:46:09 -030029#include <linux/pm_runtime.h>
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -030030#include <linux/vmalloc.h>
31
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -030032#include <media/soc_camera.h>
Guennadi Liakhovetski0fd327b2009-05-07 13:25:32 -030033#include <media/v4l2-common.h>
Guennadi Liakhovetski0fd327b2009-05-07 13:25:32 -030034#include <media/v4l2-ioctl.h>
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -030035#include <media/v4l2-dev.h>
Guennadi Liakhovetski0fd327b2009-05-07 13:25:32 -030036#include <media/videobuf-core.h>
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -030037#include <media/soc_mediabus.h>
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -030038
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -030039/* Default to VGA resolution */
40#define DEFAULT_WIDTH 640
41#define DEFAULT_HEIGHT 480
42
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -030043static LIST_HEAD(hosts);
44static LIST_HEAD(devices);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -030045static DEFINE_MUTEX(list_lock); /* Protects the list of hosts */
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -030046
Alberto Panizzo96e442c2010-12-02 07:43:37 -030047static int soc_camera_power_set(struct soc_camera_device *icd,
48 struct soc_camera_link *icl,
49 int power_on)
50{
51 int ret;
52
53 if (power_on) {
54 ret = regulator_bulk_enable(icl->num_regulators,
55 icl->regulators);
56 if (ret < 0) {
57 dev_err(&icd->dev, "Cannot enable regulators\n");
58 return ret;
59 }
60
61 if (icl->power)
62 ret = icl->power(icd->pdev, power_on);
63 if (ret < 0) {
64 dev_err(&icd->dev,
65 "Platform failed to power-on the camera.\n");
66
67 regulator_bulk_disable(icl->num_regulators,
68 icl->regulators);
69 return ret;
70 }
71 } else {
72 ret = 0;
73 if (icl->power)
74 ret = icl->power(icd->pdev, 0);
75 if (ret < 0) {
76 dev_err(&icd->dev,
77 "Platform failed to power-off the camera.\n");
78 return ret;
79 }
80
81 ret = regulator_bulk_disable(icl->num_regulators,
82 icl->regulators);
83 if (ret < 0) {
84 dev_err(&icd->dev, "Cannot disable regulators\n");
85 return ret;
86 }
87 }
88
89 return 0;
90}
91
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -030092const struct soc_camera_format_xlate *soc_camera_xlate_by_fourcc(
93 struct soc_camera_device *icd, unsigned int fourcc)
94{
95 unsigned int i;
96
97 for (i = 0; i < icd->num_user_formats; i++)
98 if (icd->user_formats[i].host_fmt->fourcc == fourcc)
99 return icd->user_formats + i;
100 return NULL;
101}
102EXPORT_SYMBOL(soc_camera_xlate_by_fourcc);
103
Guennadi Liakhovetskibd73b362008-12-23 05:54:45 -0300104/**
105 * soc_camera_apply_sensor_flags() - apply platform SOCAM_SENSOR_INVERT_* flags
106 * @icl: camera platform parameters
107 * @flags: flags to be inverted according to platform configuration
108 * @return: resulting flags
109 */
110unsigned long soc_camera_apply_sensor_flags(struct soc_camera_link *icl,
111 unsigned long flags)
112{
113 unsigned long f;
114
115 /* If only one of the two polarities is supported, switch to the opposite */
116 if (icl->flags & SOCAM_SENSOR_INVERT_HSYNC) {
117 f = flags & (SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW);
118 if (f == SOCAM_HSYNC_ACTIVE_HIGH || f == SOCAM_HSYNC_ACTIVE_LOW)
119 flags ^= SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW;
120 }
121
122 if (icl->flags & SOCAM_SENSOR_INVERT_VSYNC) {
123 f = flags & (SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW);
124 if (f == SOCAM_VSYNC_ACTIVE_HIGH || f == SOCAM_VSYNC_ACTIVE_LOW)
125 flags ^= SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW;
126 }
127
128 if (icl->flags & SOCAM_SENSOR_INVERT_PCLK) {
129 f = flags & (SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING);
130 if (f == SOCAM_PCLK_SAMPLE_RISING || f == SOCAM_PCLK_SAMPLE_FALLING)
131 flags ^= SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING;
132 }
133
134 return flags;
135}
136EXPORT_SYMBOL(soc_camera_apply_sensor_flags);
137
Hans Verkuil72937892008-06-01 10:35:18 -0300138static int soc_camera_try_fmt_vid_cap(struct file *file, void *priv,
Guennadi Liakhovetskiabe4c472008-12-01 09:44:56 -0300139 struct v4l2_format *f)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300140{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300141 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -0300142 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300143
144 WARN_ON(priv != file->private_data);
145
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300146 /* limit format to hardware capabilities */
Guennadi Liakhovetski06daa1a2008-12-18 12:52:08 -0300147 return ici->ops->try_fmt(icd, f);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300148}
149
150static int soc_camera_enum_input(struct file *file, void *priv,
151 struct v4l2_input *inp)
152{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300153 struct soc_camera_device *icd = file->private_data;
Kuninori Morimoto34d359d2008-12-18 12:47:46 -0300154 int ret = 0;
155
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300156 if (inp->index != 0)
157 return -EINVAL;
158
Kuninori Morimoto34d359d2008-12-18 12:47:46 -0300159 if (icd->ops->enum_input)
160 ret = icd->ops->enum_input(icd, inp);
161 else {
162 /* default is camera */
163 inp->type = V4L2_INPUT_TYPE_CAMERA;
164 inp->std = V4L2_STD_UNKNOWN;
165 strcpy(inp->name, "Camera");
166 }
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300167
Kuninori Morimoto34d359d2008-12-18 12:47:46 -0300168 return ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300169}
170
171static int soc_camera_g_input(struct file *file, void *priv, unsigned int *i)
172{
173 *i = 0;
174
175 return 0;
176}
177
178static int soc_camera_s_input(struct file *file, void *priv, unsigned int i)
179{
180 if (i > 0)
181 return -EINVAL;
182
183 return 0;
184}
185
186static int soc_camera_s_std(struct file *file, void *priv, v4l2_std_id *a)
187{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300188 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300189 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Kuninori Morimoto513791a2008-12-18 12:46:45 -0300190
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300191 return v4l2_subdev_call(sd, core, s_std, *a);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300192}
193
194static int soc_camera_reqbufs(struct file *file, void *priv,
195 struct v4l2_requestbuffers *p)
196{
197 int ret;
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300198 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -0300199 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300200
201 WARN_ON(priv != file->private_data);
202
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300203 if (icd->streamer && icd->streamer != file)
204 return -EBUSY;
205
206 ret = videobuf_reqbufs(&icd->vb_vidq, p);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300207 if (ret < 0)
208 return ret;
209
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300210 ret = ici->ops->reqbufs(icd, p);
211 if (!ret && !icd->streamer)
212 icd->streamer = file;
213
214 return ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300215}
216
217static int soc_camera_querybuf(struct file *file, void *priv,
218 struct v4l2_buffer *p)
219{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300220 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300221
222 WARN_ON(priv != file->private_data);
223
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300224 return videobuf_querybuf(&icd->vb_vidq, p);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300225}
226
227static int soc_camera_qbuf(struct file *file, void *priv,
228 struct v4l2_buffer *p)
229{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300230 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300231
232 WARN_ON(priv != file->private_data);
233
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300234 if (icd->streamer != file)
235 return -EBUSY;
236
237 return videobuf_qbuf(&icd->vb_vidq, p);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300238}
239
240static int soc_camera_dqbuf(struct file *file, void *priv,
241 struct v4l2_buffer *p)
242{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300243 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300244
245 WARN_ON(priv != file->private_data);
246
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300247 if (icd->streamer != file)
248 return -EBUSY;
249
250 return videobuf_dqbuf(&icd->vb_vidq, p, file->f_flags & O_NONBLOCK);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300251}
252
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300253/* Always entered with .video_lock held */
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300254static int soc_camera_init_user_formats(struct soc_camera_device *icd)
255{
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300256 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300257 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Hans Verkuil3805f202010-05-08 17:55:00 -0300258 unsigned int i, fmts = 0, raw_fmts = 0;
259 int ret;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300260 enum v4l2_mbus_pixelcode code;
261
262 while (!v4l2_subdev_call(sd, video, enum_mbus_fmt, raw_fmts, &code))
263 raw_fmts++;
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300264
265 if (!ici->ops->get_formats)
266 /*
267 * Fallback mode - the host will have to serve all
268 * sensor-provided formats one-to-one to the user
269 */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300270 fmts = raw_fmts;
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300271 else
272 /*
273 * First pass - only count formats this host-sensor
274 * configuration can provide
275 */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300276 for (i = 0; i < raw_fmts; i++) {
Guennadi Liakhovetskifa489842009-08-25 11:46:43 -0300277 ret = ici->ops->get_formats(icd, i, NULL);
278 if (ret < 0)
279 return ret;
280 fmts += ret;
281 }
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300282
283 if (!fmts)
284 return -ENXIO;
285
286 icd->user_formats =
287 vmalloc(fmts * sizeof(struct soc_camera_format_xlate));
288 if (!icd->user_formats)
289 return -ENOMEM;
290
291 icd->num_user_formats = fmts;
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300292
293 dev_dbg(&icd->dev, "Found %d supported formats.\n", fmts);
294
295 /* Second pass - actually fill data formats */
Stefan Herbrechtsmeier14df2cc2009-06-24 10:31:25 -0300296 fmts = 0;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300297 for (i = 0; i < raw_fmts; i++)
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300298 if (!ici->ops->get_formats) {
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300299 v4l2_subdev_call(sd, video, enum_mbus_fmt, i, &code);
300 icd->user_formats[i].host_fmt =
301 soc_mbus_get_fmtdesc(code);
302 icd->user_formats[i].code = code;
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300303 } else {
Guennadi Liakhovetskifa489842009-08-25 11:46:43 -0300304 ret = ici->ops->get_formats(icd, i,
305 &icd->user_formats[fmts]);
306 if (ret < 0)
307 goto egfmt;
308 fmts += ret;
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300309 }
310
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300311 icd->current_fmt = &icd->user_formats[0];
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300312
313 return 0;
Guennadi Liakhovetskifa489842009-08-25 11:46:43 -0300314
315egfmt:
316 icd->num_user_formats = 0;
317 vfree(icd->user_formats);
318 return ret;
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300319}
320
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300321/* Always entered with .video_lock held */
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300322static void soc_camera_free_user_formats(struct soc_camera_device *icd)
323{
Guennadi Liakhovetskifa489842009-08-25 11:46:43 -0300324 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
325
326 if (ici->ops->put_formats)
327 ici->ops->put_formats(icd);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300328 icd->current_fmt = NULL;
Guennadi Liakhovetskifa489842009-08-25 11:46:43 -0300329 icd->num_user_formats = 0;
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300330 vfree(icd->user_formats);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300331 icd->user_formats = NULL;
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300332}
333
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300334#define pixfmtstr(x) (x) & 0xff, ((x) >> 8) & 0xff, ((x) >> 16) & 0xff, \
335 ((x) >> 24) & 0xff
336
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300337/* Called with .vb_lock held, or from the first open(2), see comment there */
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300338static int soc_camera_set_fmt(struct soc_camera_device *icd,
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300339 struct v4l2_format *f)
340{
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300341 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
342 struct v4l2_pix_format *pix = &f->fmt.pix;
343 int ret;
344
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300345 dev_dbg(&icd->dev, "S_FMT(%c%c%c%c, %ux%u)\n",
346 pixfmtstr(pix->pixelformat), pix->width, pix->height);
347
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300348 /* We always call try_fmt() before set_fmt() or set_crop() */
349 ret = ici->ops->try_fmt(icd, f);
350 if (ret < 0)
351 return ret;
352
353 ret = ici->ops->set_fmt(icd, f);
354 if (ret < 0) {
355 return ret;
356 } else if (!icd->current_fmt ||
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300357 icd->current_fmt->host_fmt->fourcc != pix->pixelformat) {
Guennadi Liakhovetski2aa58db2009-08-25 11:47:00 -0300358 dev_err(&icd->dev,
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300359 "Host driver hasn't set up current format correctly!\n");
360 return -EINVAL;
361 }
362
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300363 icd->user_width = pix->width;
364 icd->user_height = pix->height;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300365 icd->colorspace = pix->colorspace;
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300366 icd->vb_vidq.field =
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300367 icd->field = pix->field;
Guennadi Liakhovetski025c18a2009-03-13 06:08:20 -0300368
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300369 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
370 dev_warn(&icd->dev, "Attention! Wrong buf-type %d\n",
371 f->type);
372
373 dev_dbg(&icd->dev, "set width: %d height: %d\n",
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300374 icd->user_width, icd->user_height);
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300375
376 /* set physical bus parameters */
377 return ici->ops->set_bus_param(icd, pix->pixelformat);
378}
379
Hans Verkuilbec43662008-12-30 06:58:20 -0300380static int soc_camera_open(struct file *file)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300381{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300382 struct video_device *vdev = video_devdata(file);
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300383 struct soc_camera_device *icd = container_of(vdev->parent,
384 struct soc_camera_device,
385 dev);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300386 struct soc_camera_link *icl = to_soc_camera_link(icd);
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300387 struct soc_camera_host *ici;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300388 int ret;
389
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300390 if (!icd->ops)
391 /* No device driver attached */
392 return -ENODEV;
393
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300394 ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300395
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300396 if (!try_module_get(ici->ops->owner)) {
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300397 dev_err(&icd->dev, "Couldn't lock capture bus driver.\n");
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300398 return -EINVAL;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300399 }
400
Guennadi Liakhovetski1a0063a2008-04-04 13:46:34 -0300401 icd->use_count++;
402
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300403 /* Now we really have to activate the camera */
404 if (icd->use_count == 1) {
Guennadi Liakhovetski025c18a2009-03-13 06:08:20 -0300405 /* Restore parameters before the last close() per V4L2 API */
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300406 struct v4l2_format f = {
407 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
408 .fmt.pix = {
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300409 .width = icd->user_width,
410 .height = icd->user_height,
Guennadi Liakhovetski025c18a2009-03-13 06:08:20 -0300411 .field = icd->field,
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300412 .colorspace = icd->colorspace,
413 .pixelformat =
414 icd->current_fmt->host_fmt->fourcc,
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300415 },
416 };
417
Alberto Panizzo96e442c2010-12-02 07:43:37 -0300418 ret = soc_camera_power_set(icd, icl, 1);
419 if (ret < 0)
420 goto epower;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300421
422 /* The camera could have been already on, try to reset */
423 if (icl->reset)
424 icl->reset(icd->pdev);
425
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300426 ret = ici->ops->add(icd);
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300427 if (ret < 0) {
428 dev_err(&icd->dev, "Couldn't activate the camera: %d\n", ret);
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300429 goto eiciadd;
430 }
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300431
Mauro Carvalho Chehab4f9fb5e2010-05-18 00:46:09 -0300432 pm_runtime_enable(&icd->vdev->dev);
433 ret = pm_runtime_resume(&icd->vdev->dev);
434 if (ret < 0 && ret != -ENOSYS)
435 goto eresume;
436
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300437 /*
438 * Try to configure with default parameters. Notice: this is the
439 * very first open, so, we cannot race against other calls,
440 * apart from someone else calling open() simultaneously, but
441 * .video_lock is protecting us against it.
442 */
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300443 ret = soc_camera_set_fmt(icd, &f);
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300444 if (ret < 0)
445 goto esfmt;
Guennadi Liakhovetski24d8c022010-12-25 18:29:52 -0300446
447 ici->ops->init_videobuf(&icd->vb_vidq, icd);
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300448 }
449
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300450 file->private_data = icd;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300451 dev_dbg(&icd->dev, "camera device open\n");
452
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300453 return 0;
454
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300455 /*
Mauro Carvalho Chehab4f9fb5e2010-05-18 00:46:09 -0300456 * First four errors are entered with the .video_lock held
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300457 * and use_count == 1
458 */
459esfmt:
Mauro Carvalho Chehab4f9fb5e2010-05-18 00:46:09 -0300460 pm_runtime_disable(&icd->vdev->dev);
461eresume:
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300462 ici->ops->remove(icd);
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300463eiciadd:
Alberto Panizzo96e442c2010-12-02 07:43:37 -0300464 soc_camera_power_set(icd, icl, 0);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300465epower:
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300466 icd->use_count--;
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300467 module_put(ici->ops->owner);
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300468
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300469 return ret;
470}
471
Hans Verkuilbec43662008-12-30 06:58:20 -0300472static int soc_camera_close(struct file *file)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300473{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300474 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300475 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300476
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300477 icd->use_count--;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300478 if (!icd->use_count) {
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300479 struct soc_camera_link *icl = to_soc_camera_link(icd);
480
Mauro Carvalho Chehab4f9fb5e2010-05-18 00:46:09 -0300481 pm_runtime_suspend(&icd->vdev->dev);
482 pm_runtime_disable(&icd->vdev->dev);
483
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300484 ici->ops->remove(icd);
Mauro Carvalho Chehab4f9fb5e2010-05-18 00:46:09 -0300485
Alberto Panizzo96e442c2010-12-02 07:43:37 -0300486 soc_camera_power_set(icd, icl, 0);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300487 }
Guennadi Liakhovetski025c18a2009-03-13 06:08:20 -0300488
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300489 if (icd->streamer == file)
490 icd->streamer = NULL;
491
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300492 module_put(ici->ops->owner);
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300493
Guennadi Liakhovetski2aa58db2009-08-25 11:47:00 -0300494 dev_dbg(&icd->dev, "camera device close\n");
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300495
496 return 0;
497}
498
Andrew Mortonaba360d2008-04-22 14:45:59 -0300499static ssize_t soc_camera_read(struct file *file, char __user *buf,
Guennadi Liakhovetskiabe4c472008-12-01 09:44:56 -0300500 size_t count, loff_t *ppos)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300501{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300502 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300503 int err = -EINVAL;
504
Guennadi Liakhovetski2aa58db2009-08-25 11:47:00 -0300505 dev_err(&icd->dev, "camera device read not implemented\n");
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300506
507 return err;
508}
509
510static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma)
511{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300512 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300513 int err;
514
515 dev_dbg(&icd->dev, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
516
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300517 if (icd->streamer != file)
518 return -EBUSY;
519
520 err = videobuf_mmap_mapper(&icd->vb_vidq, vma);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300521
522 dev_dbg(&icd->dev, "vma start=0x%08lx, size=%ld, ret=%d\n",
523 (unsigned long)vma->vm_start,
524 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start,
525 err);
526
527 return err;
528}
529
530static unsigned int soc_camera_poll(struct file *file, poll_table *pt)
531{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300532 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -0300533 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300534
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300535 if (icd->streamer != file)
536 return -EBUSY;
537
538 if (list_empty(&icd->vb_vidq.stream)) {
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300539 dev_err(&icd->dev, "Trying to poll with no queued buffers!\n");
540 return POLLERR;
541 }
542
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300543 return ici->ops->poll(file, pt);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300544}
545
Hans Verkuilbec43662008-12-30 06:58:20 -0300546static struct v4l2_file_operations soc_camera_fops = {
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300547 .owner = THIS_MODULE,
548 .open = soc_camera_open,
549 .release = soc_camera_close,
Guennadi Liakhovetskib6a633c2010-12-25 17:40:26 -0300550 .unlocked_ioctl = video_ioctl2,
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300551 .read = soc_camera_read,
552 .mmap = soc_camera_mmap,
553 .poll = soc_camera_poll,
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300554};
555
Hans Verkuil72937892008-06-01 10:35:18 -0300556static int soc_camera_s_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{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300559 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300560 int ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300561
562 WARN_ON(priv != file->private_data);
563
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300564 if (icd->streamer && icd->streamer != file)
565 return -EBUSY;
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300566
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300567 if (icd->vb_vidq.bufs[0]) {
Guennadi Liakhovetskib897a912009-08-25 11:46:53 -0300568 dev_err(&icd->dev, "S_FMT denied: queue initialised\n");
Guennadi Liakhovetskib6a633c2010-12-25 17:40:26 -0300569 return -EBUSY;
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300570 }
571
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300572 ret = soc_camera_set_fmt(icd, f);
573
574 if (!ret && !icd->streamer)
575 icd->streamer = file;
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300576
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300577 return ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300578}
579
Hans Verkuil72937892008-06-01 10:35:18 -0300580static int soc_camera_enum_fmt_vid_cap(struct file *file, void *priv,
Guennadi Liakhovetskiabe4c472008-12-01 09:44:56 -0300581 struct v4l2_fmtdesc *f)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300582{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300583 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300584 const struct soc_mbus_pixelfmt *format;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300585
586 WARN_ON(priv != file->private_data);
587
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300588 if (f->index >= icd->num_user_formats)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300589 return -EINVAL;
590
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300591 format = icd->user_formats[f->index].host_fmt;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300592
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300593 if (format->name)
594 strlcpy(f->description, format->name, sizeof(f->description));
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300595 f->pixelformat = format->fourcc;
596 return 0;
597}
598
Hans Verkuil72937892008-06-01 10:35:18 -0300599static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv,
Guennadi Liakhovetskiabe4c472008-12-01 09:44:56 -0300600 struct v4l2_format *f)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300601{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300602 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -0300603 struct v4l2_pix_format *pix = &f->fmt.pix;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300604
605 WARN_ON(priv != file->private_data);
606
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300607 pix->width = icd->user_width;
608 pix->height = icd->user_height;
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300609 pix->field = icd->vb_vidq.field;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300610 pix->pixelformat = icd->current_fmt->host_fmt->fourcc;
611 pix->bytesperline = soc_mbus_bytes_per_line(pix->width,
612 icd->current_fmt->host_fmt);
613 pix->colorspace = icd->colorspace;
614 if (pix->bytesperline < 0)
615 return pix->bytesperline;
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -0300616 pix->sizeimage = pix->height * pix->bytesperline;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300617 dev_dbg(&icd->dev, "current_fmt->fourcc: 0x%08x\n",
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300618 icd->current_fmt->host_fmt->fourcc);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300619 return 0;
620}
621
622static int soc_camera_querycap(struct file *file, void *priv,
623 struct v4l2_capability *cap)
624{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300625 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -0300626 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300627
628 WARN_ON(priv != file->private_data);
629
630 strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver));
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300631 return ici->ops->querycap(ici, cap);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300632}
633
634static int soc_camera_streamon(struct file *file, void *priv,
635 enum v4l2_buf_type i)
636{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300637 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300638 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300639 int ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300640
641 WARN_ON(priv != file->private_data);
642
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300643 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
644 return -EINVAL;
645
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300646 if (icd->streamer != file)
647 return -EBUSY;
648
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300649 v4l2_subdev_call(sd, video, s_stream, 1);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300650
651 /* This calls buf_queue from host driver's videobuf_queue_ops */
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300652 ret = videobuf_streamon(&icd->vb_vidq);
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300653
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300654 return ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300655}
656
657static int soc_camera_streamoff(struct file *file, void *priv,
658 enum v4l2_buf_type i)
659{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300660 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300661 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300662
663 WARN_ON(priv != file->private_data);
664
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300665 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
666 return -EINVAL;
667
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300668 if (icd->streamer != file)
669 return -EBUSY;
670
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300671 /*
672 * This calls buf_release from host driver's videobuf_queue_ops for all
673 * remaining buffers. When the last buffer is freed, stop capture
674 */
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300675 videobuf_streamoff(&icd->vb_vidq);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300676
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300677 v4l2_subdev_call(sd, video, s_stream, 0);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300678
679 return 0;
680}
681
682static int soc_camera_queryctrl(struct file *file, void *priv,
683 struct v4l2_queryctrl *qc)
684{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300685 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetski2840d242009-08-25 11:44:15 -0300686 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300687 int i;
688
689 WARN_ON(priv != file->private_data);
690
691 if (!qc->id)
692 return -EINVAL;
693
Guennadi Liakhovetski2840d242009-08-25 11:44:15 -0300694 /* First check host controls */
695 for (i = 0; i < ici->ops->num_controls; i++)
696 if (qc->id == ici->ops->controls[i].id) {
697 memcpy(qc, &(ici->ops->controls[i]),
698 sizeof(*qc));
699 return 0;
700 }
701
702 /* Then device controls */
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300703 for (i = 0; i < icd->ops->num_controls; i++)
704 if (qc->id == icd->ops->controls[i].id) {
705 memcpy(qc, &(icd->ops->controls[i]),
706 sizeof(*qc));
707 return 0;
708 }
709
710 return -EINVAL;
711}
712
713static int soc_camera_g_ctrl(struct file *file, void *priv,
714 struct v4l2_control *ctrl)
715{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300716 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300717 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300718 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski2840d242009-08-25 11:44:15 -0300719 int ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300720
721 WARN_ON(priv != file->private_data);
722
Guennadi Liakhovetski2840d242009-08-25 11:44:15 -0300723 if (ici->ops->get_ctrl) {
724 ret = ici->ops->get_ctrl(icd, ctrl);
725 if (ret != -ENOIOCTLCMD)
726 return ret;
727 }
728
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300729 return v4l2_subdev_call(sd, core, g_ctrl, ctrl);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300730}
731
732static int soc_camera_s_ctrl(struct file *file, void *priv,
733 struct v4l2_control *ctrl)
734{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300735 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300736 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300737 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski2840d242009-08-25 11:44:15 -0300738 int ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300739
740 WARN_ON(priv != file->private_data);
741
Guennadi Liakhovetski2840d242009-08-25 11:44:15 -0300742 if (ici->ops->set_ctrl) {
743 ret = ici->ops->set_ctrl(icd, ctrl);
744 if (ret != -ENOIOCTLCMD)
745 return ret;
746 }
747
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300748 return v4l2_subdev_call(sd, core, s_ctrl, ctrl);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300749}
750
751static int soc_camera_cropcap(struct file *file, void *fh,
752 struct v4l2_cropcap *a)
753{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300754 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300755 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300756
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300757 return ici->ops->cropcap(icd, a);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300758}
759
760static int soc_camera_g_crop(struct file *file, void *fh,
761 struct v4l2_crop *a)
762{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300763 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300764 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
765 int ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300766
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300767 ret = ici->ops->get_crop(icd, a);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300768
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300769 return ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300770}
771
Guennadi Liakhovetski68a54f02009-08-25 11:46:52 -0300772/*
773 * According to the V4L2 API, drivers shall not update the struct v4l2_crop
774 * argument with the actual geometry, instead, the user shall use G_CROP to
Guennadi Liakhovetskiab56d5e2010-03-23 11:31:46 -0300775 * retrieve it.
Guennadi Liakhovetski68a54f02009-08-25 11:46:52 -0300776 */
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300777static int soc_camera_s_crop(struct file *file, void *fh,
778 struct v4l2_crop *a)
779{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300780 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -0300781 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300782 struct v4l2_rect *rect = &a->c;
783 struct v4l2_crop current_crop;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300784 int ret;
785
786 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
787 return -EINVAL;
788
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300789 dev_dbg(&icd->dev, "S_CROP(%ux%u@%u:%u)\n",
790 rect->width, rect->height, rect->left, rect->top);
791
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300792 /* If get_crop fails, we'll let host and / or client drivers decide */
793 ret = ici->ops->get_crop(icd, &current_crop);
794
Guennadi Liakhovetskib897a912009-08-25 11:46:53 -0300795 /* Prohibit window size change with initialised buffers */
Guennadi Liakhovetski103754a2010-08-05 19:23:44 -0300796 if (ret < 0) {
797 dev_err(&icd->dev,
798 "S_CROP denied: getting current crop failed\n");
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300799 } else if (icd->vb_vidq.bufs[0] &&
Guennadi Liakhovetski103754a2010-08-05 19:23:44 -0300800 (a->c.width != current_crop.c.width ||
801 a->c.height != current_crop.c.height)) {
Guennadi Liakhovetskib897a912009-08-25 11:46:53 -0300802 dev_err(&icd->dev,
803 "S_CROP denied: queue initialised and sizes differ\n");
804 ret = -EBUSY;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300805 } else {
806 ret = ici->ops->set_crop(icd, a);
Guennadi Liakhovetskib897a912009-08-25 11:46:53 -0300807 }
808
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300809 return ret;
810}
811
Guennadi Liakhovetskic9f6ef62010-02-09 18:00:30 +0100812static int soc_camera_g_parm(struct file *file, void *fh,
813 struct v4l2_streamparm *a)
814{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300815 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetskic9f6ef62010-02-09 18:00:30 +0100816 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
817
818 if (ici->ops->get_parm)
819 return ici->ops->get_parm(icd, a);
820
821 return -ENOIOCTLCMD;
822}
823
824static int soc_camera_s_parm(struct file *file, void *fh,
825 struct v4l2_streamparm *a)
826{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300827 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetskic9f6ef62010-02-09 18:00:30 +0100828 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
829
830 if (ici->ops->set_parm)
831 return ici->ops->set_parm(icd, a);
832
833 return -ENOIOCTLCMD;
834}
835
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300836static int soc_camera_g_chip_ident(struct file *file, void *fh,
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300837 struct v4l2_dbg_chip_ident *id)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300838{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300839 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300840 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300841
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300842 return v4l2_subdev_call(sd, core, g_chip_ident, id);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300843}
844
845#ifdef CONFIG_VIDEO_ADV_DEBUG
846static int soc_camera_g_register(struct file *file, void *fh,
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300847 struct v4l2_dbg_register *reg)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300848{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300849 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300850 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300851
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300852 return v4l2_subdev_call(sd, core, g_register, reg);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300853}
854
855static int soc_camera_s_register(struct file *file, void *fh,
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300856 struct v4l2_dbg_register *reg)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300857{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300858 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300859 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300860
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300861 return v4l2_subdev_call(sd, core, s_register, reg);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300862}
863#endif
864
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300865/* So far this function cannot fail */
866static void scan_add_host(struct soc_camera_host *ici)
867{
868 struct soc_camera_device *icd;
869
870 mutex_lock(&list_lock);
871
872 list_for_each_entry(icd, &devices, list) {
873 if (icd->iface == ici->nr) {
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300874 int ret;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300875 icd->dev.parent = ici->v4l2_dev.dev;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300876 dev_set_name(&icd->dev, "%u-%u", icd->iface,
877 icd->devnum);
878 ret = device_register(&icd->dev);
879 if (ret < 0) {
880 icd->dev.parent = NULL;
881 dev_err(&icd->dev,
882 "Cannot register device: %d\n", ret);
883 }
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300884 }
885 }
886
887 mutex_unlock(&list_lock);
888}
889
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300890#ifdef CONFIG_I2C_BOARDINFO
891static int soc_camera_init_i2c(struct soc_camera_device *icd,
892 struct soc_camera_link *icl)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300893{
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300894 struct i2c_client *client;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300895 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300896 struct i2c_adapter *adap = i2c_get_adapter(icl->i2c_adapter_id);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300897 struct v4l2_subdev *subdev;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300898
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300899 if (!adap) {
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300900 dev_err(&icd->dev, "Cannot get I2C adapter #%d. No driver?\n",
901 icl->i2c_adapter_id);
902 goto ei2cga;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300903 }
904
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300905 icl->board_info->platform_data = icd;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300906
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300907 subdev = v4l2_i2c_new_subdev_board(&ici->v4l2_dev, adap,
Laurent Pinchart9a1f8b32010-09-24 10:16:44 -0300908 icl->board_info, NULL);
Magnus Dammd74f8412010-02-09 17:40:40 +0900909 if (!subdev)
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300910 goto ei2cnd;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300911
Laurent Pinchartc4ce6d12010-07-30 17:24:54 -0300912 client = v4l2_get_subdevdata(subdev);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300913
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300914 /* Use to_i2c_client(dev) to recover the i2c client */
915 dev_set_drvdata(&icd->dev, &client->dev);
916
917 return 0;
918ei2cnd:
919 i2c_put_adapter(adap);
920ei2cga:
Magnus Dammd74f8412010-02-09 17:40:40 +0900921 return -ENODEV;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300922}
923
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300924static void soc_camera_free_i2c(struct soc_camera_device *icd)
925{
926 struct i2c_client *client =
927 to_i2c_client(to_soc_camera_control(icd));
928 dev_set_drvdata(&icd->dev, NULL);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300929 v4l2_device_unregister_subdev(i2c_get_clientdata(client));
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300930 i2c_unregister_device(client);
931 i2c_put_adapter(client->adapter);
932}
933#else
934#define soc_camera_init_i2c(icd, icl) (-ENODEV)
935#define soc_camera_free_i2c(icd) do {} while (0)
936#endif
937
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300938static int soc_camera_video_start(struct soc_camera_device *icd);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300939static int video_dev_create(struct soc_camera_device *icd);
940/* Called during host-driver probe */
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300941static int soc_camera_probe(struct device *dev)
942{
943 struct soc_camera_device *icd = to_soc_camera_dev(dev);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300944 struct soc_camera_host *ici = to_soc_camera_host(dev->parent);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300945 struct soc_camera_link *icl = to_soc_camera_link(icd);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300946 struct device *control = NULL;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300947 struct v4l2_subdev *sd;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300948 struct v4l2_mbus_framefmt mf;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300949 int ret;
950
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300951 dev_info(dev, "Probing %s\n", dev_name(dev));
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300952
Alberto Panizzo96e442c2010-12-02 07:43:37 -0300953 ret = regulator_bulk_get(icd->pdev, icl->num_regulators,
954 icl->regulators);
955 if (ret < 0)
956 goto ereg;
957
958 ret = soc_camera_power_set(icd, icl, 1);
959 if (ret < 0)
960 goto epower;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300961
962 /* The camera could have been already on, try to reset */
963 if (icl->reset)
964 icl->reset(icd->pdev);
965
966 ret = ici->ops->add(icd);
967 if (ret < 0)
968 goto eadd;
969
970 /* Must have icd->vdev before registering the device */
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300971 ret = video_dev_create(icd);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300972 if (ret < 0)
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300973 goto evdc;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300974
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300975 /* Non-i2c cameras, e.g., soc_camera_platform, have no board_info */
976 if (icl->board_info) {
977 ret = soc_camera_init_i2c(icd, icl);
978 if (ret < 0)
979 goto eadddev;
980 } else if (!icl->add_device || !icl->del_device) {
981 ret = -EINVAL;
982 goto eadddev;
983 } else {
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300984 if (icl->module_name)
985 ret = request_module(icl->module_name);
986
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300987 ret = icl->add_device(icl, &icd->dev);
988 if (ret < 0)
989 goto eadddev;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300990
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300991 /*
992 * FIXME: this is racy, have to use driver-binding notification,
993 * when it is available
994 */
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300995 control = to_soc_camera_control(icd);
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -0300996 if (!control || !control->driver || !dev_get_drvdata(control) ||
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300997 !try_module_get(control->driver->owner)) {
998 icl->del_device(icl);
999 goto enodrv;
1000 }
Guennadi Liakhovetski025c18a2009-03-13 06:08:20 -03001001 }
1002
Guennadi Liakhovetskifa489842009-08-25 11:46:43 -03001003 /* At this point client .probe() should have run already */
1004 ret = soc_camera_init_user_formats(icd);
1005 if (ret < 0)
1006 goto eiufmt;
1007
Guennadi Liakhovetskifa489842009-08-25 11:46:43 -03001008 icd->field = V4L2_FIELD_ANY;
1009
Guennadi Liakhovetskib6a633c2010-12-25 17:40:26 -03001010 icd->vdev->lock = &icd->video_lock;
1011
1012 /*
1013 * ..._video_start() will create a device node, video_register_device()
1014 * itself is protected against concurrent open() calls, but we also have
1015 * to protect our data.
1016 */
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001017 mutex_lock(&icd->video_lock);
1018
1019 ret = soc_camera_video_start(icd);
1020 if (ret < 0)
1021 goto evidstart;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001022
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001023 /* Try to improve our guess of a reasonable window format */
1024 sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001025 if (!v4l2_subdev_call(sd, video, g_mbus_fmt, &mf)) {
1026 icd->user_width = mf.width;
1027 icd->user_height = mf.height;
1028 icd->colorspace = mf.colorspace;
1029 icd->field = mf.field;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001030 }
1031
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001032 /* Do we have to sysfs_remove_link() before device_unregister()? */
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001033 if (sysfs_create_link(&icd->dev.kobj, &to_soc_camera_control(icd)->kobj,
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001034 "control"))
1035 dev_warn(&icd->dev, "Failed creating the control symlink\n");
1036
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001037 ici->ops->remove(icd);
1038
Alberto Panizzo96e442c2010-12-02 07:43:37 -03001039 soc_camera_power_set(icd, icl, 0);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001040
1041 mutex_unlock(&icd->video_lock);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001042
1043 return 0;
1044
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001045evidstart:
1046 mutex_unlock(&icd->video_lock);
Guennadi Liakhovetskifa489842009-08-25 11:46:43 -03001047 soc_camera_free_user_formats(icd);
1048eiufmt:
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001049 if (icl->board_info) {
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001050 soc_camera_free_i2c(icd);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001051 } else {
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001052 icl->del_device(icl);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001053 module_put(control->driver->owner);
1054 }
1055enodrv:
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001056eadddev:
1057 video_device_release(icd->vdev);
1058evdc:
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001059 ici->ops->remove(icd);
1060eadd:
Alberto Panizzo96e442c2010-12-02 07:43:37 -03001061 soc_camera_power_set(icd, icl, 0);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001062epower:
Alberto Panizzo96e442c2010-12-02 07:43:37 -03001063 regulator_bulk_free(icl->num_regulators, icl->regulators);
1064ereg:
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001065 return ret;
1066}
1067
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -03001068/*
1069 * This is called on device_unregister, which only means we have to disconnect
1070 * from the host, but not remove ourselves from the device list
1071 */
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001072static int soc_camera_remove(struct device *dev)
1073{
1074 struct soc_camera_device *icd = to_soc_camera_dev(dev);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001075 struct soc_camera_link *icl = to_soc_camera_link(icd);
1076 struct video_device *vdev = icd->vdev;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001077
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001078 BUG_ON(!dev->parent);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001079
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001080 if (vdev) {
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001081 video_unregister_device(vdev);
1082 icd->vdev = NULL;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001083 }
1084
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001085 if (icl->board_info) {
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001086 soc_camera_free_i2c(icd);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001087 } else {
1088 struct device_driver *drv = to_soc_camera_control(icd) ?
1089 to_soc_camera_control(icd)->driver : NULL;
1090 if (drv) {
1091 icl->del_device(icl);
1092 module_put(drv->owner);
1093 }
1094 }
Guennadi Liakhovetskifa489842009-08-25 11:46:43 -03001095 soc_camera_free_user_formats(icd);
Guennadi Liakhovetski025c18a2009-03-13 06:08:20 -03001096
Alberto Panizzo96e442c2010-12-02 07:43:37 -03001097 regulator_bulk_free(icl->num_regulators, icl->regulators);
1098
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001099 return 0;
1100}
1101
Robert Jarzmik2e521062008-08-01 20:14:50 -03001102static int soc_camera_suspend(struct device *dev, pm_message_t state)
1103{
1104 struct soc_camera_device *icd = to_soc_camera_dev(dev);
1105 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1106 int ret = 0;
1107
1108 if (ici->ops->suspend)
1109 ret = ici->ops->suspend(icd, state);
1110
1111 return ret;
1112}
1113
1114static int soc_camera_resume(struct device *dev)
1115{
1116 struct soc_camera_device *icd = to_soc_camera_dev(dev);
1117 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1118 int ret = 0;
1119
1120 if (ici->ops->resume)
1121 ret = ici->ops->resume(icd);
1122
1123 return ret;
1124}
1125
Guennadi Liakhovetski52d268a2010-07-26 11:37:13 -03001126struct bus_type soc_camera_bus_type = {
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001127 .name = "soc-camera",
1128 .probe = soc_camera_probe,
1129 .remove = soc_camera_remove,
Robert Jarzmik2e521062008-08-01 20:14:50 -03001130 .suspend = soc_camera_suspend,
1131 .resume = soc_camera_resume,
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001132};
Guennadi Liakhovetski52d268a2010-07-26 11:37:13 -03001133EXPORT_SYMBOL_GPL(soc_camera_bus_type);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001134
1135static struct device_driver ic_drv = {
1136 .name = "camera",
1137 .bus = &soc_camera_bus_type,
1138 .owner = THIS_MODULE,
1139};
1140
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001141static void dummy_release(struct device *dev)
1142{
1143}
1144
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001145static int default_cropcap(struct soc_camera_device *icd,
1146 struct v4l2_cropcap *a)
1147{
1148 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1149 return v4l2_subdev_call(sd, video, cropcap, a);
1150}
1151
1152static int default_g_crop(struct soc_camera_device *icd, struct v4l2_crop *a)
1153{
1154 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1155 return v4l2_subdev_call(sd, video, g_crop, a);
1156}
1157
1158static int default_s_crop(struct soc_camera_device *icd, struct v4l2_crop *a)
1159{
1160 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1161 return v4l2_subdev_call(sd, video, s_crop, a);
1162}
1163
Janusz Krzysztofik06e17822010-09-10 22:26:16 -03001164static int default_g_parm(struct soc_camera_device *icd,
1165 struct v4l2_streamparm *parm)
1166{
1167 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1168 return v4l2_subdev_call(sd, video, g_parm, parm);
1169}
1170
1171static int default_s_parm(struct soc_camera_device *icd,
1172 struct v4l2_streamparm *parm)
1173{
1174 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1175 return v4l2_subdev_call(sd, video, s_parm, parm);
1176}
1177
Guennadi Liakhovetski64ff9ba2009-11-09 16:12:37 -03001178static void soc_camera_device_init(struct device *dev, void *pdata)
1179{
1180 dev->platform_data = pdata;
1181 dev->bus = &soc_camera_bus_type;
1182 dev->release = dummy_release;
1183}
1184
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -03001185int soc_camera_host_register(struct soc_camera_host *ici)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001186{
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001187 struct soc_camera_host *ix;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001188 int ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001189
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -03001190 if (!ici || !ici->ops ||
1191 !ici->ops->try_fmt ||
1192 !ici->ops->set_fmt ||
1193 !ici->ops->set_bus_param ||
1194 !ici->ops->querycap ||
1195 !ici->ops->init_videobuf ||
1196 !ici->ops->reqbufs ||
1197 !ici->ops->add ||
1198 !ici->ops->remove ||
Guennadi Liakhovetskieff505f2009-04-24 12:55:48 -03001199 !ici->ops->poll ||
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001200 !ici->v4l2_dev.dev)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001201 return -EINVAL;
1202
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001203 if (!ici->ops->set_crop)
1204 ici->ops->set_crop = default_s_crop;
1205 if (!ici->ops->get_crop)
1206 ici->ops->get_crop = default_g_crop;
1207 if (!ici->ops->cropcap)
1208 ici->ops->cropcap = default_cropcap;
Janusz Krzysztofik06e17822010-09-10 22:26:16 -03001209 if (!ici->ops->set_parm)
1210 ici->ops->set_parm = default_s_parm;
1211 if (!ici->ops->get_parm)
1212 ici->ops->get_parm = default_g_parm;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001213
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001214 mutex_lock(&list_lock);
1215 list_for_each_entry(ix, &hosts, list) {
1216 if (ix->nr == ici->nr) {
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001217 ret = -EBUSY;
1218 goto edevreg;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001219 }
1220 }
1221
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001222 ret = v4l2_device_register(ici->v4l2_dev.dev, &ici->v4l2_dev);
1223 if (ret < 0)
1224 goto edevreg;
Guennadi Liakhovetskieff505f2009-04-24 12:55:48 -03001225
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001226 list_add_tail(&ici->list, &hosts);
1227 mutex_unlock(&list_lock);
1228
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001229 scan_add_host(ici);
1230
1231 return 0;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001232
1233edevreg:
1234 mutex_unlock(&list_lock);
1235 return ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001236}
1237EXPORT_SYMBOL(soc_camera_host_register);
1238
1239/* Unregister all clients! */
1240void soc_camera_host_unregister(struct soc_camera_host *ici)
1241{
1242 struct soc_camera_device *icd;
1243
1244 mutex_lock(&list_lock);
1245
1246 list_del(&ici->list);
1247
1248 list_for_each_entry(icd, &devices, list) {
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001249 if (icd->iface == ici->nr) {
Guennadi Liakhovetski64ff9ba2009-11-09 16:12:37 -03001250 void *pdata = icd->dev.platform_data;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001251 /* The bus->remove will be called */
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001252 device_unregister(&icd->dev);
Guennadi Liakhovetski76823b72009-10-05 12:54:54 -03001253 /*
1254 * Not before device_unregister(), .remove
1255 * needs parent to call ici->ops->remove().
1256 * If the host module is loaded again, device_register()
1257 * would complain "already initialised," since 2.6.32
1258 * this is also needed to prevent use-after-free of the
1259 * device private data.
1260 */
1261 memset(&icd->dev, 0, sizeof(icd->dev));
Guennadi Liakhovetski64ff9ba2009-11-09 16:12:37 -03001262 soc_camera_device_init(&icd->dev, pdata);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001263 }
1264 }
1265
1266 mutex_unlock(&list_lock);
1267
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001268 v4l2_device_unregister(&ici->v4l2_dev);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001269}
1270EXPORT_SYMBOL(soc_camera_host_unregister);
1271
1272/* Image capture device */
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001273static int soc_camera_device_register(struct soc_camera_device *icd)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001274{
1275 struct soc_camera_device *ix;
1276 int num = -1, i;
1277
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001278 for (i = 0; i < 256 && num < 0; i++) {
1279 num = i;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001280 /* Check if this index is available on this interface */
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001281 list_for_each_entry(ix, &devices, list) {
1282 if (ix->iface == icd->iface && ix->devnum == i) {
1283 num = -1;
1284 break;
1285 }
1286 }
1287 }
1288
1289 if (num < 0)
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -03001290 /*
1291 * ok, we have 256 cameras on this host...
1292 * man, stay reasonable...
1293 */
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001294 return -ENOMEM;
1295
Guennadi Liakhovetski64ff9ba2009-11-09 16:12:37 -03001296 icd->devnum = num;
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -03001297 icd->use_count = 0;
1298 icd->host_priv = NULL;
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -03001299 mutex_init(&icd->video_lock);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001300
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001301 list_add_tail(&icd->list, &devices);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001302
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001303 return 0;
1304}
1305
1306static void soc_camera_device_unregister(struct soc_camera_device *icd)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001307{
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001308 list_del(&icd->list);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001309}
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001310
Hans Verkuila3998102008-07-21 02:57:38 -03001311static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
1312 .vidioc_querycap = soc_camera_querycap,
1313 .vidioc_g_fmt_vid_cap = soc_camera_g_fmt_vid_cap,
1314 .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
1315 .vidioc_s_fmt_vid_cap = soc_camera_s_fmt_vid_cap,
1316 .vidioc_enum_input = soc_camera_enum_input,
1317 .vidioc_g_input = soc_camera_g_input,
1318 .vidioc_s_input = soc_camera_s_input,
1319 .vidioc_s_std = soc_camera_s_std,
1320 .vidioc_reqbufs = soc_camera_reqbufs,
1321 .vidioc_try_fmt_vid_cap = soc_camera_try_fmt_vid_cap,
1322 .vidioc_querybuf = soc_camera_querybuf,
1323 .vidioc_qbuf = soc_camera_qbuf,
1324 .vidioc_dqbuf = soc_camera_dqbuf,
1325 .vidioc_streamon = soc_camera_streamon,
1326 .vidioc_streamoff = soc_camera_streamoff,
1327 .vidioc_queryctrl = soc_camera_queryctrl,
1328 .vidioc_g_ctrl = soc_camera_g_ctrl,
1329 .vidioc_s_ctrl = soc_camera_s_ctrl,
1330 .vidioc_cropcap = soc_camera_cropcap,
1331 .vidioc_g_crop = soc_camera_g_crop,
1332 .vidioc_s_crop = soc_camera_s_crop,
Guennadi Liakhovetskic9f6ef62010-02-09 18:00:30 +01001333 .vidioc_g_parm = soc_camera_g_parm,
1334 .vidioc_s_parm = soc_camera_s_parm,
Hans Verkuila3998102008-07-21 02:57:38 -03001335 .vidioc_g_chip_ident = soc_camera_g_chip_ident,
1336#ifdef CONFIG_VIDEO_ADV_DEBUG
1337 .vidioc_g_register = soc_camera_g_register,
1338 .vidioc_s_register = soc_camera_s_register,
1339#endif
1340};
1341
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001342static int video_dev_create(struct soc_camera_device *icd)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001343{
1344 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001345 struct video_device *vdev = video_device_alloc();
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001346
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001347 if (!vdev)
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001348 return -ENOMEM;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001349
1350 strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -03001351
Hans Verkuil5e85e732008-07-20 06:31:39 -03001352 vdev->parent = &icd->dev;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001353 vdev->current_norm = V4L2_STD_UNKNOWN;
1354 vdev->fops = &soc_camera_fops;
Hans Verkuila3998102008-07-21 02:57:38 -03001355 vdev->ioctl_ops = &soc_camera_ioctl_ops;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001356 vdev->release = video_device_release;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001357 vdev->tvnorms = V4L2_STD_UNKNOWN;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001358
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001359 icd->vdev = vdev;
1360
1361 return 0;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001362}
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001363
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001364/*
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001365 * Called from soc_camera_probe() above (with .video_lock held???)
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001366 */
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001367static int soc_camera_video_start(struct soc_camera_device *icd)
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001368{
Mauro Carvalho Chehab4f9fb5e2010-05-18 00:46:09 -03001369 struct device_type *type = icd->vdev->dev.type;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001370 int ret;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001371
1372 if (!icd->dev.parent)
1373 return -ENODEV;
1374
1375 if (!icd->ops ||
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001376 !icd->ops->query_bus_param ||
1377 !icd->ops->set_bus_param)
1378 return -EINVAL;
1379
Laurent Pinchart46b21092009-12-10 11:51:38 -02001380 ret = video_register_device(icd->vdev, VFL_TYPE_GRABBER, -1);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001381 if (ret < 0) {
1382 dev_err(&icd->dev, "video_register_device failed: %d\n", ret);
1383 return ret;
1384 }
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001385
Mauro Carvalho Chehab4f9fb5e2010-05-18 00:46:09 -03001386 /* Restore device type, possibly set by the subdevice driver */
1387 icd->vdev->dev.type = type;
1388
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001389 return 0;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001390}
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001391
Guennadi Liakhovetskic41deba2009-08-25 11:06:21 -03001392static int __devinit soc_camera_pdrv_probe(struct platform_device *pdev)
1393{
1394 struct soc_camera_link *icl = pdev->dev.platform_data;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001395 struct soc_camera_device *icd;
1396 int ret;
Guennadi Liakhovetskic41deba2009-08-25 11:06:21 -03001397
1398 if (!icl)
1399 return -EINVAL;
1400
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001401 icd = kzalloc(sizeof(*icd), GFP_KERNEL);
1402 if (!icd)
1403 return -ENOMEM;
Guennadi Liakhovetskic41deba2009-08-25 11:06:21 -03001404
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001405 icd->iface = icl->bus_id;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001406 icd->pdev = &pdev->dev;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001407 platform_set_drvdata(pdev, icd);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001408
1409 ret = soc_camera_device_register(icd);
1410 if (ret < 0)
1411 goto escdevreg;
1412
Guennadi Liakhovetski64ff9ba2009-11-09 16:12:37 -03001413 soc_camera_device_init(&icd->dev, icl);
1414
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001415 icd->user_width = DEFAULT_WIDTH;
1416 icd->user_height = DEFAULT_HEIGHT;
1417
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001418 return 0;
1419
1420escdevreg:
1421 kfree(icd);
1422
1423 return ret;
Guennadi Liakhovetskic41deba2009-08-25 11:06:21 -03001424}
1425
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -03001426/*
1427 * Only called on rmmod for each platform device, since they are not
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001428 * hot-pluggable. Now we know, that all our users - hosts and devices have
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -03001429 * been unloaded already
1430 */
Guennadi Liakhovetskic41deba2009-08-25 11:06:21 -03001431static int __devexit soc_camera_pdrv_remove(struct platform_device *pdev)
1432{
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001433 struct soc_camera_device *icd = platform_get_drvdata(pdev);
Guennadi Liakhovetskic41deba2009-08-25 11:06:21 -03001434
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001435 if (!icd)
1436 return -EINVAL;
1437
1438 soc_camera_device_unregister(icd);
1439
1440 kfree(icd);
1441
Guennadi Liakhovetski0fd327b2009-05-07 13:25:32 -03001442 return 0;
1443}
1444
1445static struct platform_driver __refdata soc_camera_pdrv = {
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001446 .remove = __devexit_p(soc_camera_pdrv_remove),
1447 .driver = {
1448 .name = "soc-camera-pdrv",
1449 .owner = THIS_MODULE,
Guennadi Liakhovetski0fd327b2009-05-07 13:25:32 -03001450 },
1451};
1452
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001453static int __init soc_camera_init(void)
1454{
1455 int ret = bus_register(&soc_camera_bus_type);
1456 if (ret)
1457 return ret;
1458 ret = driver_register(&ic_drv);
1459 if (ret)
1460 goto edrvr;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001461
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001462 ret = platform_driver_probe(&soc_camera_pdrv, soc_camera_pdrv_probe);
Guennadi Liakhovetski0fd327b2009-05-07 13:25:32 -03001463 if (ret)
1464 goto epdr;
1465
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001466 return 0;
1467
Guennadi Liakhovetski0fd327b2009-05-07 13:25:32 -03001468epdr:
1469 driver_unregister(&ic_drv);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001470edrvr:
1471 bus_unregister(&soc_camera_bus_type);
1472 return ret;
1473}
1474
1475static void __exit soc_camera_exit(void)
1476{
Guennadi Liakhovetski0fd327b2009-05-07 13:25:32 -03001477 platform_driver_unregister(&soc_camera_pdrv);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001478 driver_unregister(&ic_drv);
1479 bus_unregister(&soc_camera_bus_type);
1480}
1481
1482module_init(soc_camera_init);
1483module_exit(soc_camera_exit);
1484
1485MODULE_DESCRIPTION("Image capture bus driver");
1486MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
1487MODULE_LICENSE("GPL");
Guennadi Liakhovetski0fd327b2009-05-07 13:25:32 -03001488MODULE_ALIAS("platform:soc-camera-pdrv");