Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1 | /* |
| 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 Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 19 | #include <linux/device.h> |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 20 | #include <linux/err.h> |
Guennadi Liakhovetski | 0fd327b | 2009-05-07 13:25:32 -0300 | [diff] [blame] | 21 | #include <linux/i2c.h> |
| 22 | #include <linux/init.h> |
| 23 | #include <linux/list.h> |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 24 | #include <linux/mutex.h> |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 25 | #include <linux/module.h> |
Guennadi Liakhovetski | 0fd327b | 2009-05-07 13:25:32 -0300 | [diff] [blame] | 26 | #include <linux/platform_device.h> |
Alberto Panizzo | 96e442c | 2010-12-02 07:43:37 -0300 | [diff] [blame] | 27 | #include <linux/regulator/consumer.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 28 | #include <linux/slab.h> |
Mauro Carvalho Chehab | 4f9fb5e | 2010-05-18 00:46:09 -0300 | [diff] [blame] | 29 | #include <linux/pm_runtime.h> |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 30 | #include <linux/vmalloc.h> |
| 31 | |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 32 | #include <media/soc_camera.h> |
Guennadi Liakhovetski | 0fd327b | 2009-05-07 13:25:32 -0300 | [diff] [blame] | 33 | #include <media/v4l2-common.h> |
Guennadi Liakhovetski | 0fd327b | 2009-05-07 13:25:32 -0300 | [diff] [blame] | 34 | #include <media/v4l2-ioctl.h> |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 35 | #include <media/v4l2-dev.h> |
Guennadi Liakhovetski | 0fd327b | 2009-05-07 13:25:32 -0300 | [diff] [blame] | 36 | #include <media/videobuf-core.h> |
Guennadi Liakhovetski | 592c2ab | 2011-01-29 12:44:51 -0300 | [diff] [blame] | 37 | #include <media/videobuf2-core.h> |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 38 | #include <media/soc_mediabus.h> |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 39 | |
Guennadi Liakhovetski | df2ed07 | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 40 | /* Default to VGA resolution */ |
| 41 | #define DEFAULT_WIDTH 640 |
| 42 | #define DEFAULT_HEIGHT 480 |
| 43 | |
Guennadi Liakhovetski | aee5c2f | 2011-03-28 13:28:28 -0300 | [diff] [blame] | 44 | #define is_streaming(ici, icd) \ |
| 45 | (((ici)->ops->init_videobuf) ? \ |
| 46 | (icd)->vb_vidq.streaming : \ |
| 47 | vb2_is_streaming(&(icd)->vb2_vidq)) |
| 48 | |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 49 | static LIST_HEAD(hosts); |
| 50 | static LIST_HEAD(devices); |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 51 | static DEFINE_MUTEX(list_lock); /* Protects the list of hosts */ |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 52 | |
Alberto Panizzo | 96e442c | 2010-12-02 07:43:37 -0300 | [diff] [blame] | 53 | static int soc_camera_power_set(struct soc_camera_device *icd, |
| 54 | struct soc_camera_link *icl, |
| 55 | int power_on) |
| 56 | { |
| 57 | int ret; |
| 58 | |
| 59 | if (power_on) { |
| 60 | ret = regulator_bulk_enable(icl->num_regulators, |
| 61 | icl->regulators); |
| 62 | if (ret < 0) { |
| 63 | dev_err(&icd->dev, "Cannot enable regulators\n"); |
| 64 | return ret; |
| 65 | } |
| 66 | |
| 67 | if (icl->power) |
| 68 | ret = icl->power(icd->pdev, power_on); |
| 69 | if (ret < 0) { |
| 70 | dev_err(&icd->dev, |
| 71 | "Platform failed to power-on the camera.\n"); |
| 72 | |
| 73 | regulator_bulk_disable(icl->num_regulators, |
| 74 | icl->regulators); |
| 75 | return ret; |
| 76 | } |
| 77 | } else { |
| 78 | ret = 0; |
| 79 | if (icl->power) |
| 80 | ret = icl->power(icd->pdev, 0); |
| 81 | if (ret < 0) { |
| 82 | dev_err(&icd->dev, |
| 83 | "Platform failed to power-off the camera.\n"); |
| 84 | return ret; |
| 85 | } |
| 86 | |
| 87 | ret = regulator_bulk_disable(icl->num_regulators, |
| 88 | icl->regulators); |
| 89 | if (ret < 0) { |
| 90 | dev_err(&icd->dev, "Cannot disable regulators\n"); |
| 91 | return ret; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | return 0; |
| 96 | } |
| 97 | |
Guennadi Liakhovetski | c2786ad | 2008-12-01 09:45:27 -0300 | [diff] [blame] | 98 | const struct soc_camera_format_xlate *soc_camera_xlate_by_fourcc( |
| 99 | struct soc_camera_device *icd, unsigned int fourcc) |
| 100 | { |
| 101 | unsigned int i; |
| 102 | |
| 103 | for (i = 0; i < icd->num_user_formats; i++) |
| 104 | if (icd->user_formats[i].host_fmt->fourcc == fourcc) |
| 105 | return icd->user_formats + i; |
| 106 | return NULL; |
| 107 | } |
| 108 | EXPORT_SYMBOL(soc_camera_xlate_by_fourcc); |
| 109 | |
Guennadi Liakhovetski | bd73b36 | 2008-12-23 05:54:45 -0300 | [diff] [blame] | 110 | /** |
| 111 | * soc_camera_apply_sensor_flags() - apply platform SOCAM_SENSOR_INVERT_* flags |
| 112 | * @icl: camera platform parameters |
| 113 | * @flags: flags to be inverted according to platform configuration |
| 114 | * @return: resulting flags |
| 115 | */ |
| 116 | unsigned long soc_camera_apply_sensor_flags(struct soc_camera_link *icl, |
| 117 | unsigned long flags) |
| 118 | { |
| 119 | unsigned long f; |
| 120 | |
| 121 | /* If only one of the two polarities is supported, switch to the opposite */ |
| 122 | if (icl->flags & SOCAM_SENSOR_INVERT_HSYNC) { |
| 123 | f = flags & (SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW); |
| 124 | if (f == SOCAM_HSYNC_ACTIVE_HIGH || f == SOCAM_HSYNC_ACTIVE_LOW) |
| 125 | flags ^= SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW; |
| 126 | } |
| 127 | |
| 128 | if (icl->flags & SOCAM_SENSOR_INVERT_VSYNC) { |
| 129 | f = flags & (SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW); |
| 130 | if (f == SOCAM_VSYNC_ACTIVE_HIGH || f == SOCAM_VSYNC_ACTIVE_LOW) |
| 131 | flags ^= SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW; |
| 132 | } |
| 133 | |
| 134 | if (icl->flags & SOCAM_SENSOR_INVERT_PCLK) { |
| 135 | f = flags & (SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING); |
| 136 | if (f == SOCAM_PCLK_SAMPLE_RISING || f == SOCAM_PCLK_SAMPLE_FALLING) |
| 137 | flags ^= SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING; |
| 138 | } |
| 139 | |
| 140 | return flags; |
| 141 | } |
| 142 | EXPORT_SYMBOL(soc_camera_apply_sensor_flags); |
| 143 | |
Sergio Aguirre | dca6b6d | 2011-04-11 13:14:33 -0300 | [diff] [blame] | 144 | #define pixfmtstr(x) (x) & 0xff, ((x) >> 8) & 0xff, ((x) >> 16) & 0xff, \ |
| 145 | ((x) >> 24) & 0xff |
| 146 | |
| 147 | static int soc_camera_try_fmt(struct soc_camera_device *icd, |
| 148 | struct v4l2_format *f) |
| 149 | { |
| 150 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
| 151 | struct v4l2_pix_format *pix = &f->fmt.pix; |
| 152 | int ret; |
| 153 | |
| 154 | dev_dbg(&icd->dev, "TRY_FMT(%c%c%c%c, %ux%u)\n", |
| 155 | pixfmtstr(pix->pixelformat), pix->width, pix->height); |
| 156 | |
| 157 | pix->bytesperline = 0; |
| 158 | pix->sizeimage = 0; |
| 159 | |
| 160 | ret = ici->ops->try_fmt(icd, f); |
| 161 | if (ret < 0) |
| 162 | return ret; |
| 163 | |
| 164 | if (!pix->sizeimage) { |
| 165 | if (!pix->bytesperline) { |
| 166 | const struct soc_camera_format_xlate *xlate; |
| 167 | |
| 168 | xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat); |
| 169 | if (!xlate) |
| 170 | return -EINVAL; |
| 171 | |
| 172 | ret = soc_mbus_bytes_per_line(pix->width, |
| 173 | xlate->host_fmt); |
| 174 | if (ret > 0) |
| 175 | pix->bytesperline = ret; |
| 176 | } |
| 177 | if (pix->bytesperline) |
| 178 | pix->sizeimage = pix->bytesperline * pix->height; |
| 179 | } |
| 180 | |
| 181 | return 0; |
| 182 | } |
| 183 | |
Hans Verkuil | 7293789 | 2008-06-01 10:35:18 -0300 | [diff] [blame] | 184 | static int soc_camera_try_fmt_vid_cap(struct file *file, void *priv, |
Guennadi Liakhovetski | abe4c47 | 2008-12-01 09:44:56 -0300 | [diff] [blame] | 185 | struct v4l2_format *f) |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 186 | { |
Guennadi Liakhovetski | 57bee29 | 2010-08-17 14:29:51 -0300 | [diff] [blame] | 187 | struct soc_camera_device *icd = file->private_data; |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 188 | |
| 189 | WARN_ON(priv != file->private_data); |
| 190 | |
Guennadi Liakhovetski | d366d4a | 2011-03-21 06:33:25 -0300 | [diff] [blame] | 191 | /* Only single-plane capture is supported so far */ |
| 192 | if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 193 | return -EINVAL; |
| 194 | |
Guennadi Liakhovetski | ad5f2e8 | 2008-03-07 21:57:18 -0300 | [diff] [blame] | 195 | /* limit format to hardware capabilities */ |
Sergio Aguirre | dca6b6d | 2011-04-11 13:14:33 -0300 | [diff] [blame] | 196 | return soc_camera_try_fmt(icd, f); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | static int soc_camera_enum_input(struct file *file, void *priv, |
| 200 | struct v4l2_input *inp) |
| 201 | { |
Guennadi Liakhovetski | 57bee29 | 2010-08-17 14:29:51 -0300 | [diff] [blame] | 202 | struct soc_camera_device *icd = file->private_data; |
Kuninori Morimoto | 34d359d | 2008-12-18 12:47:46 -0300 | [diff] [blame] | 203 | int ret = 0; |
| 204 | |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 205 | if (inp->index != 0) |
| 206 | return -EINVAL; |
| 207 | |
Kuninori Morimoto | 34d359d | 2008-12-18 12:47:46 -0300 | [diff] [blame] | 208 | if (icd->ops->enum_input) |
| 209 | ret = icd->ops->enum_input(icd, inp); |
| 210 | else { |
| 211 | /* default is camera */ |
| 212 | inp->type = V4L2_INPUT_TYPE_CAMERA; |
| 213 | inp->std = V4L2_STD_UNKNOWN; |
| 214 | strcpy(inp->name, "Camera"); |
| 215 | } |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 216 | |
Kuninori Morimoto | 34d359d | 2008-12-18 12:47:46 -0300 | [diff] [blame] | 217 | return ret; |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | static int soc_camera_g_input(struct file *file, void *priv, unsigned int *i) |
| 221 | { |
| 222 | *i = 0; |
| 223 | |
| 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | static int soc_camera_s_input(struct file *file, void *priv, unsigned int i) |
| 228 | { |
| 229 | if (i > 0) |
| 230 | return -EINVAL; |
| 231 | |
| 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | static int soc_camera_s_std(struct file *file, void *priv, v4l2_std_id *a) |
| 236 | { |
Guennadi Liakhovetski | 57bee29 | 2010-08-17 14:29:51 -0300 | [diff] [blame] | 237 | struct soc_camera_device *icd = file->private_data; |
Guennadi Liakhovetski | c9c1f1c | 2009-08-25 11:46:59 -0300 | [diff] [blame] | 238 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
Kuninori Morimoto | 513791a | 2008-12-18 12:46:45 -0300 | [diff] [blame] | 239 | |
Guennadi Liakhovetski | c9c1f1c | 2009-08-25 11:46:59 -0300 | [diff] [blame] | 240 | return v4l2_subdev_call(sd, core, s_std, *a); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 241 | } |
| 242 | |
Qing Xu | ed5b65d | 2011-01-20 05:19:40 -0300 | [diff] [blame] | 243 | static int soc_camera_enum_fsizes(struct file *file, void *fh, |
| 244 | struct v4l2_frmsizeenum *fsize) |
| 245 | { |
| 246 | struct soc_camera_device *icd = file->private_data; |
| 247 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
| 248 | |
| 249 | return ici->ops->enum_fsizes(icd, fsize); |
| 250 | } |
| 251 | |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 252 | static int soc_camera_reqbufs(struct file *file, void *priv, |
| 253 | struct v4l2_requestbuffers *p) |
| 254 | { |
| 255 | int ret; |
Guennadi Liakhovetski | 57bee29 | 2010-08-17 14:29:51 -0300 | [diff] [blame] | 256 | struct soc_camera_device *icd = file->private_data; |
Guennadi Liakhovetski | 64f5905 | 2008-12-18 11:51:55 -0300 | [diff] [blame] | 257 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 258 | |
| 259 | WARN_ON(priv != file->private_data); |
| 260 | |
Guennadi Liakhovetski | 57bee29 | 2010-08-17 14:29:51 -0300 | [diff] [blame] | 261 | if (icd->streamer && icd->streamer != file) |
| 262 | return -EBUSY; |
| 263 | |
Guennadi Liakhovetski | 592c2ab | 2011-01-29 12:44:51 -0300 | [diff] [blame] | 264 | if (ici->ops->init_videobuf) { |
| 265 | ret = videobuf_reqbufs(&icd->vb_vidq, p); |
| 266 | if (ret < 0) |
| 267 | return ret; |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 268 | |
Guennadi Liakhovetski | 592c2ab | 2011-01-29 12:44:51 -0300 | [diff] [blame] | 269 | ret = ici->ops->reqbufs(icd, p); |
| 270 | } else { |
| 271 | ret = vb2_reqbufs(&icd->vb2_vidq, p); |
| 272 | } |
| 273 | |
Guennadi Liakhovetski | 57bee29 | 2010-08-17 14:29:51 -0300 | [diff] [blame] | 274 | if (!ret && !icd->streamer) |
| 275 | icd->streamer = file; |
| 276 | |
| 277 | return ret; |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | static int soc_camera_querybuf(struct file *file, void *priv, |
| 281 | struct v4l2_buffer *p) |
| 282 | { |
Guennadi Liakhovetski | 57bee29 | 2010-08-17 14:29:51 -0300 | [diff] [blame] | 283 | struct soc_camera_device *icd = file->private_data; |
Guennadi Liakhovetski | 592c2ab | 2011-01-29 12:44:51 -0300 | [diff] [blame] | 284 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 285 | |
| 286 | WARN_ON(priv != file->private_data); |
| 287 | |
Guennadi Liakhovetski | 592c2ab | 2011-01-29 12:44:51 -0300 | [diff] [blame] | 288 | if (ici->ops->init_videobuf) |
| 289 | return videobuf_querybuf(&icd->vb_vidq, p); |
| 290 | else |
| 291 | return vb2_querybuf(&icd->vb2_vidq, p); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | static int soc_camera_qbuf(struct file *file, void *priv, |
| 295 | struct v4l2_buffer *p) |
| 296 | { |
Guennadi Liakhovetski | 57bee29 | 2010-08-17 14:29:51 -0300 | [diff] [blame] | 297 | struct soc_camera_device *icd = file->private_data; |
Guennadi Liakhovetski | 592c2ab | 2011-01-29 12:44:51 -0300 | [diff] [blame] | 298 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 299 | |
| 300 | WARN_ON(priv != file->private_data); |
| 301 | |
Guennadi Liakhovetski | 57bee29 | 2010-08-17 14:29:51 -0300 | [diff] [blame] | 302 | if (icd->streamer != file) |
| 303 | return -EBUSY; |
| 304 | |
Guennadi Liakhovetski | 592c2ab | 2011-01-29 12:44:51 -0300 | [diff] [blame] | 305 | if (ici->ops->init_videobuf) |
| 306 | return videobuf_qbuf(&icd->vb_vidq, p); |
| 307 | else |
| 308 | return vb2_qbuf(&icd->vb2_vidq, p); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | static int soc_camera_dqbuf(struct file *file, void *priv, |
| 312 | struct v4l2_buffer *p) |
| 313 | { |
Guennadi Liakhovetski | 57bee29 | 2010-08-17 14:29:51 -0300 | [diff] [blame] | 314 | struct soc_camera_device *icd = file->private_data; |
Guennadi Liakhovetski | 592c2ab | 2011-01-29 12:44:51 -0300 | [diff] [blame] | 315 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 316 | |
| 317 | WARN_ON(priv != file->private_data); |
| 318 | |
Guennadi Liakhovetski | 57bee29 | 2010-08-17 14:29:51 -0300 | [diff] [blame] | 319 | if (icd->streamer != file) |
| 320 | return -EBUSY; |
| 321 | |
Guennadi Liakhovetski | 592c2ab | 2011-01-29 12:44:51 -0300 | [diff] [blame] | 322 | if (ici->ops->init_videobuf) |
| 323 | return videobuf_dqbuf(&icd->vb_vidq, p, file->f_flags & O_NONBLOCK); |
| 324 | else |
| 325 | return vb2_dqbuf(&icd->vb2_vidq, p, file->f_flags & O_NONBLOCK); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 326 | } |
| 327 | |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 328 | /* Always entered with .video_lock held */ |
Guennadi Liakhovetski | c2786ad | 2008-12-01 09:45:27 -0300 | [diff] [blame] | 329 | static int soc_camera_init_user_formats(struct soc_camera_device *icd) |
| 330 | { |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 331 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
Guennadi Liakhovetski | c2786ad | 2008-12-01 09:45:27 -0300 | [diff] [blame] | 332 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
Hans Verkuil | 3805f20 | 2010-05-08 17:55:00 -0300 | [diff] [blame] | 333 | unsigned int i, fmts = 0, raw_fmts = 0; |
| 334 | int ret; |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 335 | enum v4l2_mbus_pixelcode code; |
| 336 | |
| 337 | while (!v4l2_subdev_call(sd, video, enum_mbus_fmt, raw_fmts, &code)) |
| 338 | raw_fmts++; |
Guennadi Liakhovetski | c2786ad | 2008-12-01 09:45:27 -0300 | [diff] [blame] | 339 | |
| 340 | if (!ici->ops->get_formats) |
| 341 | /* |
| 342 | * Fallback mode - the host will have to serve all |
| 343 | * sensor-provided formats one-to-one to the user |
| 344 | */ |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 345 | fmts = raw_fmts; |
Guennadi Liakhovetski | c2786ad | 2008-12-01 09:45:27 -0300 | [diff] [blame] | 346 | else |
| 347 | /* |
| 348 | * First pass - only count formats this host-sensor |
| 349 | * configuration can provide |
| 350 | */ |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 351 | for (i = 0; i < raw_fmts; i++) { |
Guennadi Liakhovetski | fa48984 | 2009-08-25 11:46:43 -0300 | [diff] [blame] | 352 | ret = ici->ops->get_formats(icd, i, NULL); |
| 353 | if (ret < 0) |
| 354 | return ret; |
| 355 | fmts += ret; |
| 356 | } |
Guennadi Liakhovetski | c2786ad | 2008-12-01 09:45:27 -0300 | [diff] [blame] | 357 | |
| 358 | if (!fmts) |
| 359 | return -ENXIO; |
| 360 | |
| 361 | icd->user_formats = |
| 362 | vmalloc(fmts * sizeof(struct soc_camera_format_xlate)); |
| 363 | if (!icd->user_formats) |
| 364 | return -ENOMEM; |
| 365 | |
Guennadi Liakhovetski | c2786ad | 2008-12-01 09:45:27 -0300 | [diff] [blame] | 366 | dev_dbg(&icd->dev, "Found %d supported formats.\n", fmts); |
| 367 | |
| 368 | /* Second pass - actually fill data formats */ |
Stefan Herbrechtsmeier | 14df2cc | 2009-06-24 10:31:25 -0300 | [diff] [blame] | 369 | fmts = 0; |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 370 | for (i = 0; i < raw_fmts; i++) |
Guennadi Liakhovetski | c2786ad | 2008-12-01 09:45:27 -0300 | [diff] [blame] | 371 | if (!ici->ops->get_formats) { |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 372 | v4l2_subdev_call(sd, video, enum_mbus_fmt, i, &code); |
Guennadi Liakhovetski | d2dcad4 | 2011-05-18 06:49:54 -0300 | [diff] [blame] | 373 | icd->user_formats[fmts].host_fmt = |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 374 | soc_mbus_get_fmtdesc(code); |
Guennadi Liakhovetski | d2dcad4 | 2011-05-18 06:49:54 -0300 | [diff] [blame] | 375 | if (icd->user_formats[fmts].host_fmt) |
| 376 | icd->user_formats[fmts++].code = code; |
Guennadi Liakhovetski | c2786ad | 2008-12-01 09:45:27 -0300 | [diff] [blame] | 377 | } else { |
Guennadi Liakhovetski | fa48984 | 2009-08-25 11:46:43 -0300 | [diff] [blame] | 378 | ret = ici->ops->get_formats(icd, i, |
| 379 | &icd->user_formats[fmts]); |
| 380 | if (ret < 0) |
| 381 | goto egfmt; |
| 382 | fmts += ret; |
Guennadi Liakhovetski | c2786ad | 2008-12-01 09:45:27 -0300 | [diff] [blame] | 383 | } |
| 384 | |
Guennadi Liakhovetski | d2dcad4 | 2011-05-18 06:49:54 -0300 | [diff] [blame] | 385 | icd->num_user_formats = fmts; |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 386 | icd->current_fmt = &icd->user_formats[0]; |
Guennadi Liakhovetski | c2786ad | 2008-12-01 09:45:27 -0300 | [diff] [blame] | 387 | |
| 388 | return 0; |
Guennadi Liakhovetski | fa48984 | 2009-08-25 11:46:43 -0300 | [diff] [blame] | 389 | |
| 390 | egfmt: |
Guennadi Liakhovetski | fa48984 | 2009-08-25 11:46:43 -0300 | [diff] [blame] | 391 | vfree(icd->user_formats); |
| 392 | return ret; |
Guennadi Liakhovetski | c2786ad | 2008-12-01 09:45:27 -0300 | [diff] [blame] | 393 | } |
| 394 | |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 395 | /* Always entered with .video_lock held */ |
Guennadi Liakhovetski | c2786ad | 2008-12-01 09:45:27 -0300 | [diff] [blame] | 396 | static void soc_camera_free_user_formats(struct soc_camera_device *icd) |
| 397 | { |
Guennadi Liakhovetski | fa48984 | 2009-08-25 11:46:43 -0300 | [diff] [blame] | 398 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
| 399 | |
| 400 | if (ici->ops->put_formats) |
| 401 | ici->ops->put_formats(icd); |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 402 | icd->current_fmt = NULL; |
Guennadi Liakhovetski | fa48984 | 2009-08-25 11:46:43 -0300 | [diff] [blame] | 403 | icd->num_user_formats = 0; |
Guennadi Liakhovetski | c2786ad | 2008-12-01 09:45:27 -0300 | [diff] [blame] | 404 | vfree(icd->user_formats); |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 405 | icd->user_formats = NULL; |
Guennadi Liakhovetski | c2786ad | 2008-12-01 09:45:27 -0300 | [diff] [blame] | 406 | } |
| 407 | |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 408 | /* Called with .vb_lock held, or from the first open(2), see comment there */ |
Guennadi Liakhovetski | 57bee29 | 2010-08-17 14:29:51 -0300 | [diff] [blame] | 409 | static int soc_camera_set_fmt(struct soc_camera_device *icd, |
Guennadi Liakhovetski | df2ed07 | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 410 | struct v4l2_format *f) |
| 411 | { |
Guennadi Liakhovetski | df2ed07 | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 412 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
| 413 | struct v4l2_pix_format *pix = &f->fmt.pix; |
| 414 | int ret; |
| 415 | |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 416 | dev_dbg(&icd->dev, "S_FMT(%c%c%c%c, %ux%u)\n", |
| 417 | pixfmtstr(pix->pixelformat), pix->width, pix->height); |
| 418 | |
Guennadi Liakhovetski | df2ed07 | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 419 | /* We always call try_fmt() before set_fmt() or set_crop() */ |
Sergio Aguirre | dca6b6d | 2011-04-11 13:14:33 -0300 | [diff] [blame] | 420 | ret = soc_camera_try_fmt(icd, f); |
Guennadi Liakhovetski | df2ed07 | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 421 | if (ret < 0) |
| 422 | return ret; |
| 423 | |
| 424 | ret = ici->ops->set_fmt(icd, f); |
| 425 | if (ret < 0) { |
| 426 | return ret; |
| 427 | } else if (!icd->current_fmt || |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 428 | icd->current_fmt->host_fmt->fourcc != pix->pixelformat) { |
Guennadi Liakhovetski | 2aa58db | 2009-08-25 11:47:00 -0300 | [diff] [blame] | 429 | dev_err(&icd->dev, |
Guennadi Liakhovetski | df2ed07 | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 430 | "Host driver hasn't set up current format correctly!\n"); |
| 431 | return -EINVAL; |
| 432 | } |
| 433 | |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 434 | icd->user_width = pix->width; |
| 435 | icd->user_height = pix->height; |
Sergio Aguirre | 0e4c180 | 2011-03-07 21:49:48 -0300 | [diff] [blame] | 436 | icd->bytesperline = pix->bytesperline; |
| 437 | icd->sizeimage = pix->sizeimage; |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 438 | icd->colorspace = pix->colorspace; |
Guennadi Liakhovetski | 592c2ab | 2011-01-29 12:44:51 -0300 | [diff] [blame] | 439 | icd->field = pix->field; |
| 440 | if (ici->ops->init_videobuf) |
| 441 | icd->vb_vidq.field = pix->field; |
Guennadi Liakhovetski | 025c18a | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 442 | |
Guennadi Liakhovetski | df2ed07 | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 443 | dev_dbg(&icd->dev, "set width: %d height: %d\n", |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 444 | icd->user_width, icd->user_height); |
Guennadi Liakhovetski | df2ed07 | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 445 | |
| 446 | /* set physical bus parameters */ |
| 447 | return ici->ops->set_bus_param(icd, pix->pixelformat); |
| 448 | } |
| 449 | |
Hans Verkuil | bec4366 | 2008-12-30 06:58:20 -0300 | [diff] [blame] | 450 | static int soc_camera_open(struct file *file) |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 451 | { |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 452 | struct video_device *vdev = video_devdata(file); |
Guennadi Liakhovetski | 96c7539 | 2009-08-25 11:53:23 -0300 | [diff] [blame] | 453 | struct soc_camera_device *icd = container_of(vdev->parent, |
| 454 | struct soc_camera_device, |
| 455 | dev); |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 456 | struct soc_camera_link *icl = to_soc_camera_link(icd); |
Guennadi Liakhovetski | 9dc4e48 | 2008-04-22 14:45:32 -0300 | [diff] [blame] | 457 | struct soc_camera_host *ici; |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 458 | int ret; |
| 459 | |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 460 | if (!icd->ops) |
| 461 | /* No device driver attached */ |
| 462 | return -ENODEV; |
| 463 | |
Guennadi Liakhovetski | 9dc4e48 | 2008-04-22 14:45:32 -0300 | [diff] [blame] | 464 | ici = to_soc_camera_host(icd->dev.parent); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 465 | |
Guennadi Liakhovetski | b8d9904 | 2008-04-04 13:41:25 -0300 | [diff] [blame] | 466 | if (!try_module_get(ici->ops->owner)) { |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 467 | dev_err(&icd->dev, "Couldn't lock capture bus driver.\n"); |
Guennadi Liakhovetski | 57bee29 | 2010-08-17 14:29:51 -0300 | [diff] [blame] | 468 | return -EINVAL; |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 469 | } |
| 470 | |
Guennadi Liakhovetski | 1a0063a | 2008-04-04 13:46:34 -0300 | [diff] [blame] | 471 | icd->use_count++; |
| 472 | |
Guennadi Liakhovetski | 9dc4e48 | 2008-04-22 14:45:32 -0300 | [diff] [blame] | 473 | /* Now we really have to activate the camera */ |
| 474 | if (icd->use_count == 1) { |
Guennadi Liakhovetski | 025c18a | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 475 | /* Restore parameters before the last close() per V4L2 API */ |
Guennadi Liakhovetski | df2ed07 | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 476 | struct v4l2_format f = { |
| 477 | .type = V4L2_BUF_TYPE_VIDEO_CAPTURE, |
| 478 | .fmt.pix = { |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 479 | .width = icd->user_width, |
| 480 | .height = icd->user_height, |
Guennadi Liakhovetski | 025c18a | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 481 | .field = icd->field, |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 482 | .colorspace = icd->colorspace, |
| 483 | .pixelformat = |
| 484 | icd->current_fmt->host_fmt->fourcc, |
Guennadi Liakhovetski | df2ed07 | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 485 | }, |
| 486 | }; |
| 487 | |
Alberto Panizzo | 96e442c | 2010-12-02 07:43:37 -0300 | [diff] [blame] | 488 | ret = soc_camera_power_set(icd, icl, 1); |
| 489 | if (ret < 0) |
| 490 | goto epower; |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 491 | |
| 492 | /* The camera could have been already on, try to reset */ |
| 493 | if (icl->reset) |
| 494 | icl->reset(icd->pdev); |
| 495 | |
Guennadi Liakhovetski | b8d9904 | 2008-04-04 13:41:25 -0300 | [diff] [blame] | 496 | ret = ici->ops->add(icd); |
Guennadi Liakhovetski | 9dc4e48 | 2008-04-22 14:45:32 -0300 | [diff] [blame] | 497 | if (ret < 0) { |
| 498 | dev_err(&icd->dev, "Couldn't activate the camera: %d\n", ret); |
Guennadi Liakhovetski | 9dc4e48 | 2008-04-22 14:45:32 -0300 | [diff] [blame] | 499 | goto eiciadd; |
| 500 | } |
Guennadi Liakhovetski | df2ed07 | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 501 | |
Mauro Carvalho Chehab | 4f9fb5e | 2010-05-18 00:46:09 -0300 | [diff] [blame] | 502 | pm_runtime_enable(&icd->vdev->dev); |
| 503 | ret = pm_runtime_resume(&icd->vdev->dev); |
| 504 | if (ret < 0 && ret != -ENOSYS) |
| 505 | goto eresume; |
| 506 | |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 507 | /* |
| 508 | * Try to configure with default parameters. Notice: this is the |
| 509 | * very first open, so, we cannot race against other calls, |
| 510 | * apart from someone else calling open() simultaneously, but |
| 511 | * .video_lock is protecting us against it. |
| 512 | */ |
Guennadi Liakhovetski | 57bee29 | 2010-08-17 14:29:51 -0300 | [diff] [blame] | 513 | ret = soc_camera_set_fmt(icd, &f); |
Guennadi Liakhovetski | df2ed07 | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 514 | if (ret < 0) |
| 515 | goto esfmt; |
Guennadi Liakhovetski | 24d8c02 | 2010-12-25 18:29:52 -0300 | [diff] [blame] | 516 | |
Guennadi Liakhovetski | 592c2ab | 2011-01-29 12:44:51 -0300 | [diff] [blame] | 517 | if (ici->ops->init_videobuf) { |
| 518 | ici->ops->init_videobuf(&icd->vb_vidq, icd); |
| 519 | } else { |
| 520 | ret = ici->ops->init_videobuf2(&icd->vb2_vidq, icd); |
| 521 | if (ret < 0) |
| 522 | goto einitvb; |
| 523 | } |
Guennadi Liakhovetski | 9dc4e48 | 2008-04-22 14:45:32 -0300 | [diff] [blame] | 524 | } |
| 525 | |
Guennadi Liakhovetski | 57bee29 | 2010-08-17 14:29:51 -0300 | [diff] [blame] | 526 | file->private_data = icd; |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 527 | dev_dbg(&icd->dev, "camera device open\n"); |
| 528 | |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 529 | return 0; |
| 530 | |
Guennadi Liakhovetski | df2ed07 | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 531 | /* |
Mauro Carvalho Chehab | 4f9fb5e | 2010-05-18 00:46:09 -0300 | [diff] [blame] | 532 | * First four errors are entered with the .video_lock held |
Guennadi Liakhovetski | df2ed07 | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 533 | * and use_count == 1 |
| 534 | */ |
Guennadi Liakhovetski | 592c2ab | 2011-01-29 12:44:51 -0300 | [diff] [blame] | 535 | einitvb: |
Guennadi Liakhovetski | df2ed07 | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 536 | esfmt: |
Mauro Carvalho Chehab | 4f9fb5e | 2010-05-18 00:46:09 -0300 | [diff] [blame] | 537 | pm_runtime_disable(&icd->vdev->dev); |
| 538 | eresume: |
Guennadi Liakhovetski | df2ed07 | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 539 | ici->ops->remove(icd); |
Guennadi Liakhovetski | 9dc4e48 | 2008-04-22 14:45:32 -0300 | [diff] [blame] | 540 | eiciadd: |
Alberto Panizzo | 96e442c | 2010-12-02 07:43:37 -0300 | [diff] [blame] | 541 | soc_camera_power_set(icd, icl, 0); |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 542 | epower: |
Guennadi Liakhovetski | c2786ad | 2008-12-01 09:45:27 -0300 | [diff] [blame] | 543 | icd->use_count--; |
Guennadi Liakhovetski | b8d9904 | 2008-04-04 13:41:25 -0300 | [diff] [blame] | 544 | module_put(ici->ops->owner); |
Guennadi Liakhovetski | 57bee29 | 2010-08-17 14:29:51 -0300 | [diff] [blame] | 545 | |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 546 | return ret; |
| 547 | } |
| 548 | |
Hans Verkuil | bec4366 | 2008-12-30 06:58:20 -0300 | [diff] [blame] | 549 | static int soc_camera_close(struct file *file) |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 550 | { |
Guennadi Liakhovetski | 57bee29 | 2010-08-17 14:29:51 -0300 | [diff] [blame] | 551 | struct soc_camera_device *icd = file->private_data; |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 552 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 553 | |
Guennadi Liakhovetski | 9dc4e48 | 2008-04-22 14:45:32 -0300 | [diff] [blame] | 554 | icd->use_count--; |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 555 | if (!icd->use_count) { |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 556 | struct soc_camera_link *icl = to_soc_camera_link(icd); |
| 557 | |
Mauro Carvalho Chehab | 4f9fb5e | 2010-05-18 00:46:09 -0300 | [diff] [blame] | 558 | pm_runtime_suspend(&icd->vdev->dev); |
| 559 | pm_runtime_disable(&icd->vdev->dev); |
| 560 | |
Guennadi Liakhovetski | b8d9904 | 2008-04-04 13:41:25 -0300 | [diff] [blame] | 561 | ici->ops->remove(icd); |
Guennadi Liakhovetski | 592c2ab | 2011-01-29 12:44:51 -0300 | [diff] [blame] | 562 | if (ici->ops->init_videobuf2) |
| 563 | vb2_queue_release(&icd->vb2_vidq); |
Mauro Carvalho Chehab | 4f9fb5e | 2010-05-18 00:46:09 -0300 | [diff] [blame] | 564 | |
Alberto Panizzo | 96e442c | 2010-12-02 07:43:37 -0300 | [diff] [blame] | 565 | soc_camera_power_set(icd, icl, 0); |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 566 | } |
Guennadi Liakhovetski | 025c18a | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 567 | |
Guennadi Liakhovetski | 57bee29 | 2010-08-17 14:29:51 -0300 | [diff] [blame] | 568 | if (icd->streamer == file) |
| 569 | icd->streamer = NULL; |
| 570 | |
Guennadi Liakhovetski | b8d9904 | 2008-04-04 13:41:25 -0300 | [diff] [blame] | 571 | module_put(ici->ops->owner); |
Guennadi Liakhovetski | 9dc4e48 | 2008-04-22 14:45:32 -0300 | [diff] [blame] | 572 | |
Guennadi Liakhovetski | 2aa58db | 2009-08-25 11:47:00 -0300 | [diff] [blame] | 573 | dev_dbg(&icd->dev, "camera device close\n"); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 574 | |
| 575 | return 0; |
| 576 | } |
| 577 | |
Andrew Morton | aba360d | 2008-04-22 14:45:59 -0300 | [diff] [blame] | 578 | static ssize_t soc_camera_read(struct file *file, char __user *buf, |
Guennadi Liakhovetski | abe4c47 | 2008-12-01 09:44:56 -0300 | [diff] [blame] | 579 | size_t count, loff_t *ppos) |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 580 | { |
Guennadi Liakhovetski | 57bee29 | 2010-08-17 14:29:51 -0300 | [diff] [blame] | 581 | struct soc_camera_device *icd = file->private_data; |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 582 | int err = -EINVAL; |
| 583 | |
Guennadi Liakhovetski | 2aa58db | 2009-08-25 11:47:00 -0300 | [diff] [blame] | 584 | dev_err(&icd->dev, "camera device read not implemented\n"); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 585 | |
| 586 | return err; |
| 587 | } |
| 588 | |
| 589 | static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma) |
| 590 | { |
Guennadi Liakhovetski | 57bee29 | 2010-08-17 14:29:51 -0300 | [diff] [blame] | 591 | struct soc_camera_device *icd = file->private_data; |
Guennadi Liakhovetski | 592c2ab | 2011-01-29 12:44:51 -0300 | [diff] [blame] | 592 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 593 | int err; |
| 594 | |
| 595 | dev_dbg(&icd->dev, "mmap called, vma=0x%08lx\n", (unsigned long)vma); |
| 596 | |
Guennadi Liakhovetski | 57bee29 | 2010-08-17 14:29:51 -0300 | [diff] [blame] | 597 | if (icd->streamer != file) |
| 598 | return -EBUSY; |
| 599 | |
Guennadi Liakhovetski | 592c2ab | 2011-01-29 12:44:51 -0300 | [diff] [blame] | 600 | if (ici->ops->init_videobuf) |
| 601 | err = videobuf_mmap_mapper(&icd->vb_vidq, vma); |
| 602 | else |
| 603 | err = vb2_mmap(&icd->vb2_vidq, vma); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 604 | |
| 605 | dev_dbg(&icd->dev, "vma start=0x%08lx, size=%ld, ret=%d\n", |
| 606 | (unsigned long)vma->vm_start, |
| 607 | (unsigned long)vma->vm_end - (unsigned long)vma->vm_start, |
| 608 | err); |
| 609 | |
| 610 | return err; |
| 611 | } |
| 612 | |
| 613 | static unsigned int soc_camera_poll(struct file *file, poll_table *pt) |
| 614 | { |
Guennadi Liakhovetski | 57bee29 | 2010-08-17 14:29:51 -0300 | [diff] [blame] | 615 | struct soc_camera_device *icd = file->private_data; |
Guennadi Liakhovetski | 64f5905 | 2008-12-18 11:51:55 -0300 | [diff] [blame] | 616 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 617 | |
Guennadi Liakhovetski | 57bee29 | 2010-08-17 14:29:51 -0300 | [diff] [blame] | 618 | if (icd->streamer != file) |
| 619 | return -EBUSY; |
| 620 | |
Guennadi Liakhovetski | 592c2ab | 2011-01-29 12:44:51 -0300 | [diff] [blame] | 621 | if (ici->ops->init_videobuf && list_empty(&icd->vb_vidq.stream)) { |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 622 | dev_err(&icd->dev, "Trying to poll with no queued buffers!\n"); |
| 623 | return POLLERR; |
| 624 | } |
| 625 | |
Guennadi Liakhovetski | b8d9904 | 2008-04-04 13:41:25 -0300 | [diff] [blame] | 626 | return ici->ops->poll(file, pt); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 627 | } |
| 628 | |
Guennadi Liakhovetski | 592c2ab | 2011-01-29 12:44:51 -0300 | [diff] [blame] | 629 | void soc_camera_lock(struct vb2_queue *vq) |
| 630 | { |
| 631 | struct soc_camera_device *icd = vb2_get_drv_priv(vq); |
| 632 | mutex_lock(&icd->video_lock); |
| 633 | } |
| 634 | EXPORT_SYMBOL(soc_camera_lock); |
| 635 | |
| 636 | void soc_camera_unlock(struct vb2_queue *vq) |
| 637 | { |
| 638 | struct soc_camera_device *icd = vb2_get_drv_priv(vq); |
| 639 | mutex_unlock(&icd->video_lock); |
| 640 | } |
| 641 | EXPORT_SYMBOL(soc_camera_unlock); |
| 642 | |
Hans Verkuil | bec4366 | 2008-12-30 06:58:20 -0300 | [diff] [blame] | 643 | static struct v4l2_file_operations soc_camera_fops = { |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 644 | .owner = THIS_MODULE, |
| 645 | .open = soc_camera_open, |
| 646 | .release = soc_camera_close, |
Guennadi Liakhovetski | b6a633c | 2010-12-25 17:40:26 -0300 | [diff] [blame] | 647 | .unlocked_ioctl = video_ioctl2, |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 648 | .read = soc_camera_read, |
| 649 | .mmap = soc_camera_mmap, |
| 650 | .poll = soc_camera_poll, |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 651 | }; |
| 652 | |
Hans Verkuil | 7293789 | 2008-06-01 10:35:18 -0300 | [diff] [blame] | 653 | static int soc_camera_s_fmt_vid_cap(struct file *file, void *priv, |
Guennadi Liakhovetski | abe4c47 | 2008-12-01 09:44:56 -0300 | [diff] [blame] | 654 | struct v4l2_format *f) |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 655 | { |
Guennadi Liakhovetski | 57bee29 | 2010-08-17 14:29:51 -0300 | [diff] [blame] | 656 | struct soc_camera_device *icd = file->private_data; |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 657 | int ret; |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 658 | |
| 659 | WARN_ON(priv != file->private_data); |
| 660 | |
Guennadi Liakhovetski | d366d4a | 2011-03-21 06:33:25 -0300 | [diff] [blame] | 661 | if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) { |
| 662 | dev_warn(&icd->dev, "Wrong buf-type %d\n", f->type); |
| 663 | return -EINVAL; |
| 664 | } |
| 665 | |
Guennadi Liakhovetski | 57bee29 | 2010-08-17 14:29:51 -0300 | [diff] [blame] | 666 | if (icd->streamer && icd->streamer != file) |
| 667 | return -EBUSY; |
Guennadi Liakhovetski | 1c3bb74 | 2008-12-18 12:28:54 -0300 | [diff] [blame] | 668 | |
Guennadi Liakhovetski | aee5c2f | 2011-03-28 13:28:28 -0300 | [diff] [blame] | 669 | if (is_streaming(to_soc_camera_host(icd->dev.parent), icd)) { |
Guennadi Liakhovetski | b897a91 | 2009-08-25 11:46:53 -0300 | [diff] [blame] | 670 | dev_err(&icd->dev, "S_FMT denied: queue initialised\n"); |
Guennadi Liakhovetski | b6a633c | 2010-12-25 17:40:26 -0300 | [diff] [blame] | 671 | return -EBUSY; |
Guennadi Liakhovetski | 1c3bb74 | 2008-12-18 12:28:54 -0300 | [diff] [blame] | 672 | } |
| 673 | |
Guennadi Liakhovetski | 57bee29 | 2010-08-17 14:29:51 -0300 | [diff] [blame] | 674 | ret = soc_camera_set_fmt(icd, f); |
| 675 | |
| 676 | if (!ret && !icd->streamer) |
| 677 | icd->streamer = file; |
Guennadi Liakhovetski | 1c3bb74 | 2008-12-18 12:28:54 -0300 | [diff] [blame] | 678 | |
Guennadi Liakhovetski | 1c3bb74 | 2008-12-18 12:28:54 -0300 | [diff] [blame] | 679 | return ret; |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 680 | } |
| 681 | |
Hans Verkuil | 7293789 | 2008-06-01 10:35:18 -0300 | [diff] [blame] | 682 | static int soc_camera_enum_fmt_vid_cap(struct file *file, void *priv, |
Guennadi Liakhovetski | abe4c47 | 2008-12-01 09:44:56 -0300 | [diff] [blame] | 683 | struct v4l2_fmtdesc *f) |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 684 | { |
Guennadi Liakhovetski | 57bee29 | 2010-08-17 14:29:51 -0300 | [diff] [blame] | 685 | struct soc_camera_device *icd = file->private_data; |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 686 | const struct soc_mbus_pixelfmt *format; |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 687 | |
| 688 | WARN_ON(priv != file->private_data); |
| 689 | |
Guennadi Liakhovetski | c2786ad | 2008-12-01 09:45:27 -0300 | [diff] [blame] | 690 | if (f->index >= icd->num_user_formats) |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 691 | return -EINVAL; |
| 692 | |
Guennadi Liakhovetski | c2786ad | 2008-12-01 09:45:27 -0300 | [diff] [blame] | 693 | format = icd->user_formats[f->index].host_fmt; |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 694 | |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 695 | if (format->name) |
| 696 | strlcpy(f->description, format->name, sizeof(f->description)); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 697 | f->pixelformat = format->fourcc; |
| 698 | return 0; |
| 699 | } |
| 700 | |
Hans Verkuil | 7293789 | 2008-06-01 10:35:18 -0300 | [diff] [blame] | 701 | static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv, |
Guennadi Liakhovetski | abe4c47 | 2008-12-01 09:44:56 -0300 | [diff] [blame] | 702 | struct v4l2_format *f) |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 703 | { |
Guennadi Liakhovetski | 57bee29 | 2010-08-17 14:29:51 -0300 | [diff] [blame] | 704 | struct soc_camera_device *icd = file->private_data; |
Guennadi Liakhovetski | 64f5905 | 2008-12-18 11:51:55 -0300 | [diff] [blame] | 705 | struct v4l2_pix_format *pix = &f->fmt.pix; |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 706 | |
| 707 | WARN_ON(priv != file->private_data); |
| 708 | |
Guennadi Liakhovetski | d366d4a | 2011-03-21 06:33:25 -0300 | [diff] [blame] | 709 | if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 710 | return -EINVAL; |
| 711 | |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 712 | pix->width = icd->user_width; |
| 713 | pix->height = icd->user_height; |
Sergio Aguirre | 0e4c180 | 2011-03-07 21:49:48 -0300 | [diff] [blame] | 714 | pix->bytesperline = icd->bytesperline; |
| 715 | pix->sizeimage = icd->sizeimage; |
Guennadi Liakhovetski | 592c2ab | 2011-01-29 12:44:51 -0300 | [diff] [blame] | 716 | pix->field = icd->field; |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 717 | pix->pixelformat = icd->current_fmt->host_fmt->fourcc; |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 718 | pix->colorspace = icd->colorspace; |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 719 | dev_dbg(&icd->dev, "current_fmt->fourcc: 0x%08x\n", |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 720 | icd->current_fmt->host_fmt->fourcc); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 721 | return 0; |
| 722 | } |
| 723 | |
| 724 | static int soc_camera_querycap(struct file *file, void *priv, |
| 725 | struct v4l2_capability *cap) |
| 726 | { |
Guennadi Liakhovetski | 57bee29 | 2010-08-17 14:29:51 -0300 | [diff] [blame] | 727 | struct soc_camera_device *icd = file->private_data; |
Guennadi Liakhovetski | 64f5905 | 2008-12-18 11:51:55 -0300 | [diff] [blame] | 728 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 729 | |
| 730 | WARN_ON(priv != file->private_data); |
| 731 | |
| 732 | strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver)); |
Guennadi Liakhovetski | b8d9904 | 2008-04-04 13:41:25 -0300 | [diff] [blame] | 733 | return ici->ops->querycap(ici, cap); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | static int soc_camera_streamon(struct file *file, void *priv, |
| 737 | enum v4l2_buf_type i) |
| 738 | { |
Guennadi Liakhovetski | 57bee29 | 2010-08-17 14:29:51 -0300 | [diff] [blame] | 739 | struct soc_camera_device *icd = file->private_data; |
Guennadi Liakhovetski | 592c2ab | 2011-01-29 12:44:51 -0300 | [diff] [blame] | 740 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
Guennadi Liakhovetski | c9c1f1c | 2009-08-25 11:46:59 -0300 | [diff] [blame] | 741 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
Guennadi Liakhovetski | 1c3bb74 | 2008-12-18 12:28:54 -0300 | [diff] [blame] | 742 | int ret; |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 743 | |
| 744 | WARN_ON(priv != file->private_data); |
| 745 | |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 746 | if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 747 | return -EINVAL; |
| 748 | |
Guennadi Liakhovetski | 57bee29 | 2010-08-17 14:29:51 -0300 | [diff] [blame] | 749 | if (icd->streamer != file) |
| 750 | return -EBUSY; |
| 751 | |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 752 | /* This calls buf_queue from host driver's videobuf_queue_ops */ |
Guennadi Liakhovetski | 592c2ab | 2011-01-29 12:44:51 -0300 | [diff] [blame] | 753 | if (ici->ops->init_videobuf) |
| 754 | ret = videobuf_streamon(&icd->vb_vidq); |
| 755 | else |
| 756 | ret = vb2_streamon(&icd->vb2_vidq, i); |
| 757 | |
Anatolij Gustschin | 7fdbd85 | 2011-01-31 09:19:32 -0300 | [diff] [blame] | 758 | if (!ret) |
| 759 | v4l2_subdev_call(sd, video, s_stream, 1); |
Guennadi Liakhovetski | 1c3bb74 | 2008-12-18 12:28:54 -0300 | [diff] [blame] | 760 | |
Guennadi Liakhovetski | 1c3bb74 | 2008-12-18 12:28:54 -0300 | [diff] [blame] | 761 | return ret; |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | static int soc_camera_streamoff(struct file *file, void *priv, |
| 765 | enum v4l2_buf_type i) |
| 766 | { |
Guennadi Liakhovetski | 57bee29 | 2010-08-17 14:29:51 -0300 | [diff] [blame] | 767 | struct soc_camera_device *icd = file->private_data; |
Guennadi Liakhovetski | c9c1f1c | 2009-08-25 11:46:59 -0300 | [diff] [blame] | 768 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
Guennadi Liakhovetski | 592c2ab | 2011-01-29 12:44:51 -0300 | [diff] [blame] | 769 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 770 | |
| 771 | WARN_ON(priv != file->private_data); |
| 772 | |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 773 | if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 774 | return -EINVAL; |
| 775 | |
Guennadi Liakhovetski | 57bee29 | 2010-08-17 14:29:51 -0300 | [diff] [blame] | 776 | if (icd->streamer != file) |
| 777 | return -EBUSY; |
| 778 | |
Guennadi Liakhovetski | 5d28d52 | 2009-12-11 11:15:05 -0300 | [diff] [blame] | 779 | /* |
| 780 | * This calls buf_release from host driver's videobuf_queue_ops for all |
| 781 | * remaining buffers. When the last buffer is freed, stop capture |
| 782 | */ |
Guennadi Liakhovetski | 592c2ab | 2011-01-29 12:44:51 -0300 | [diff] [blame] | 783 | if (ici->ops->init_videobuf) |
| 784 | videobuf_streamoff(&icd->vb_vidq); |
| 785 | else |
| 786 | vb2_streamoff(&icd->vb2_vidq, i); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 787 | |
Guennadi Liakhovetski | c9c1f1c | 2009-08-25 11:46:59 -0300 | [diff] [blame] | 788 | v4l2_subdev_call(sd, video, s_stream, 0); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 789 | |
| 790 | return 0; |
| 791 | } |
| 792 | |
| 793 | static int soc_camera_queryctrl(struct file *file, void *priv, |
| 794 | struct v4l2_queryctrl *qc) |
| 795 | { |
Guennadi Liakhovetski | 57bee29 | 2010-08-17 14:29:51 -0300 | [diff] [blame] | 796 | struct soc_camera_device *icd = file->private_data; |
Guennadi Liakhovetski | 2840d24 | 2009-08-25 11:44:15 -0300 | [diff] [blame] | 797 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 798 | int i; |
| 799 | |
| 800 | WARN_ON(priv != file->private_data); |
| 801 | |
| 802 | if (!qc->id) |
| 803 | return -EINVAL; |
| 804 | |
Guennadi Liakhovetski | 2840d24 | 2009-08-25 11:44:15 -0300 | [diff] [blame] | 805 | /* First check host controls */ |
| 806 | for (i = 0; i < ici->ops->num_controls; i++) |
| 807 | if (qc->id == ici->ops->controls[i].id) { |
| 808 | memcpy(qc, &(ici->ops->controls[i]), |
| 809 | sizeof(*qc)); |
| 810 | return 0; |
| 811 | } |
| 812 | |
| 813 | /* Then device controls */ |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 814 | for (i = 0; i < icd->ops->num_controls; i++) |
| 815 | if (qc->id == icd->ops->controls[i].id) { |
| 816 | memcpy(qc, &(icd->ops->controls[i]), |
| 817 | sizeof(*qc)); |
| 818 | return 0; |
| 819 | } |
| 820 | |
| 821 | return -EINVAL; |
| 822 | } |
| 823 | |
| 824 | static int soc_camera_g_ctrl(struct file *file, void *priv, |
| 825 | struct v4l2_control *ctrl) |
| 826 | { |
Guennadi Liakhovetski | 57bee29 | 2010-08-17 14:29:51 -0300 | [diff] [blame] | 827 | struct soc_camera_device *icd = file->private_data; |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 828 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
Guennadi Liakhovetski | c9c1f1c | 2009-08-25 11:46:59 -0300 | [diff] [blame] | 829 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
Guennadi Liakhovetski | 2840d24 | 2009-08-25 11:44:15 -0300 | [diff] [blame] | 830 | int ret; |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 831 | |
| 832 | WARN_ON(priv != file->private_data); |
| 833 | |
Guennadi Liakhovetski | 2840d24 | 2009-08-25 11:44:15 -0300 | [diff] [blame] | 834 | if (ici->ops->get_ctrl) { |
| 835 | ret = ici->ops->get_ctrl(icd, ctrl); |
| 836 | if (ret != -ENOIOCTLCMD) |
| 837 | return ret; |
| 838 | } |
| 839 | |
Guennadi Liakhovetski | c9c1f1c | 2009-08-25 11:46:59 -0300 | [diff] [blame] | 840 | return v4l2_subdev_call(sd, core, g_ctrl, ctrl); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 841 | } |
| 842 | |
| 843 | static int soc_camera_s_ctrl(struct file *file, void *priv, |
| 844 | struct v4l2_control *ctrl) |
| 845 | { |
Guennadi Liakhovetski | 57bee29 | 2010-08-17 14:29:51 -0300 | [diff] [blame] | 846 | struct soc_camera_device *icd = file->private_data; |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 847 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
Guennadi Liakhovetski | c9c1f1c | 2009-08-25 11:46:59 -0300 | [diff] [blame] | 848 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
Guennadi Liakhovetski | 2840d24 | 2009-08-25 11:44:15 -0300 | [diff] [blame] | 849 | int ret; |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 850 | |
| 851 | WARN_ON(priv != file->private_data); |
| 852 | |
Guennadi Liakhovetski | 2840d24 | 2009-08-25 11:44:15 -0300 | [diff] [blame] | 853 | if (ici->ops->set_ctrl) { |
| 854 | ret = ici->ops->set_ctrl(icd, ctrl); |
| 855 | if (ret != -ENOIOCTLCMD) |
| 856 | return ret; |
| 857 | } |
| 858 | |
Guennadi Liakhovetski | c9c1f1c | 2009-08-25 11:46:59 -0300 | [diff] [blame] | 859 | return v4l2_subdev_call(sd, core, s_ctrl, ctrl); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 860 | } |
| 861 | |
| 862 | static int soc_camera_cropcap(struct file *file, void *fh, |
| 863 | struct v4l2_cropcap *a) |
| 864 | { |
Guennadi Liakhovetski | 57bee29 | 2010-08-17 14:29:51 -0300 | [diff] [blame] | 865 | struct soc_camera_device *icd = file->private_data; |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 866 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 867 | |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 868 | return ici->ops->cropcap(icd, a); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 869 | } |
| 870 | |
| 871 | static int soc_camera_g_crop(struct file *file, void *fh, |
| 872 | struct v4l2_crop *a) |
| 873 | { |
Guennadi Liakhovetski | 57bee29 | 2010-08-17 14:29:51 -0300 | [diff] [blame] | 874 | struct soc_camera_device *icd = file->private_data; |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 875 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
| 876 | int ret; |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 877 | |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 878 | ret = ici->ops->get_crop(icd, a); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 879 | |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 880 | return ret; |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 881 | } |
| 882 | |
Guennadi Liakhovetski | 68a54f0 | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 883 | /* |
| 884 | * According to the V4L2 API, drivers shall not update the struct v4l2_crop |
| 885 | * argument with the actual geometry, instead, the user shall use G_CROP to |
Guennadi Liakhovetski | ab56d5e | 2010-03-23 11:31:46 -0300 | [diff] [blame] | 886 | * retrieve it. |
Guennadi Liakhovetski | 68a54f0 | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 887 | */ |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 888 | static int soc_camera_s_crop(struct file *file, void *fh, |
| 889 | struct v4l2_crop *a) |
| 890 | { |
Guennadi Liakhovetski | 57bee29 | 2010-08-17 14:29:51 -0300 | [diff] [blame] | 891 | struct soc_camera_device *icd = file->private_data; |
Guennadi Liakhovetski | 64f5905 | 2008-12-18 11:51:55 -0300 | [diff] [blame] | 892 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 893 | struct v4l2_rect *rect = &a->c; |
| 894 | struct v4l2_crop current_crop; |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 895 | int ret; |
| 896 | |
| 897 | if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 898 | return -EINVAL; |
| 899 | |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 900 | dev_dbg(&icd->dev, "S_CROP(%ux%u@%u:%u)\n", |
| 901 | rect->width, rect->height, rect->left, rect->top); |
| 902 | |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 903 | /* If get_crop fails, we'll let host and / or client drivers decide */ |
| 904 | ret = ici->ops->get_crop(icd, ¤t_crop); |
| 905 | |
Guennadi Liakhovetski | b897a91 | 2009-08-25 11:46:53 -0300 | [diff] [blame] | 906 | /* Prohibit window size change with initialised buffers */ |
Guennadi Liakhovetski | 103754a | 2010-08-05 19:23:44 -0300 | [diff] [blame] | 907 | if (ret < 0) { |
| 908 | dev_err(&icd->dev, |
| 909 | "S_CROP denied: getting current crop failed\n"); |
Guennadi Liakhovetski | aee5c2f | 2011-03-28 13:28:28 -0300 | [diff] [blame] | 910 | } else if ((a->c.width == current_crop.c.width && |
| 911 | a->c.height == current_crop.c.height) || |
| 912 | !is_streaming(ici, icd)) { |
| 913 | /* same size or not streaming - use .set_crop() */ |
| 914 | ret = ici->ops->set_crop(icd, a); |
| 915 | } else if (ici->ops->set_livecrop) { |
| 916 | ret = ici->ops->set_livecrop(icd, a); |
| 917 | } else { |
Guennadi Liakhovetski | b897a91 | 2009-08-25 11:46:53 -0300 | [diff] [blame] | 918 | dev_err(&icd->dev, |
| 919 | "S_CROP denied: queue initialised and sizes differ\n"); |
| 920 | ret = -EBUSY; |
Guennadi Liakhovetski | b897a91 | 2009-08-25 11:46:53 -0300 | [diff] [blame] | 921 | } |
| 922 | |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 923 | return ret; |
| 924 | } |
| 925 | |
Guennadi Liakhovetski | c9f6ef6 | 2010-02-09 18:00:30 +0100 | [diff] [blame] | 926 | static int soc_camera_g_parm(struct file *file, void *fh, |
| 927 | struct v4l2_streamparm *a) |
| 928 | { |
Guennadi Liakhovetski | 57bee29 | 2010-08-17 14:29:51 -0300 | [diff] [blame] | 929 | struct soc_camera_device *icd = file->private_data; |
Guennadi Liakhovetski | c9f6ef6 | 2010-02-09 18:00:30 +0100 | [diff] [blame] | 930 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
| 931 | |
| 932 | if (ici->ops->get_parm) |
| 933 | return ici->ops->get_parm(icd, a); |
| 934 | |
| 935 | return -ENOIOCTLCMD; |
| 936 | } |
| 937 | |
| 938 | static int soc_camera_s_parm(struct file *file, void *fh, |
| 939 | struct v4l2_streamparm *a) |
| 940 | { |
Guennadi Liakhovetski | 57bee29 | 2010-08-17 14:29:51 -0300 | [diff] [blame] | 941 | struct soc_camera_device *icd = file->private_data; |
Guennadi Liakhovetski | c9f6ef6 | 2010-02-09 18:00:30 +0100 | [diff] [blame] | 942 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
| 943 | |
| 944 | if (ici->ops->set_parm) |
| 945 | return ici->ops->set_parm(icd, a); |
| 946 | |
| 947 | return -ENOIOCTLCMD; |
| 948 | } |
| 949 | |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 950 | static int soc_camera_g_chip_ident(struct file *file, void *fh, |
Hans Verkuil | aecde8b | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 951 | struct v4l2_dbg_chip_ident *id) |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 952 | { |
Guennadi Liakhovetski | 57bee29 | 2010-08-17 14:29:51 -0300 | [diff] [blame] | 953 | struct soc_camera_device *icd = file->private_data; |
Guennadi Liakhovetski | c9c1f1c | 2009-08-25 11:46:59 -0300 | [diff] [blame] | 954 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 955 | |
Guennadi Liakhovetski | c9c1f1c | 2009-08-25 11:46:59 -0300 | [diff] [blame] | 956 | return v4l2_subdev_call(sd, core, g_chip_ident, id); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 957 | } |
| 958 | |
| 959 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
| 960 | static int soc_camera_g_register(struct file *file, void *fh, |
Hans Verkuil | aecde8b | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 961 | struct v4l2_dbg_register *reg) |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 962 | { |
Guennadi Liakhovetski | 57bee29 | 2010-08-17 14:29:51 -0300 | [diff] [blame] | 963 | struct soc_camera_device *icd = file->private_data; |
Guennadi Liakhovetski | c9c1f1c | 2009-08-25 11:46:59 -0300 | [diff] [blame] | 964 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 965 | |
Guennadi Liakhovetski | c9c1f1c | 2009-08-25 11:46:59 -0300 | [diff] [blame] | 966 | return v4l2_subdev_call(sd, core, g_register, reg); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 967 | } |
| 968 | |
| 969 | static int soc_camera_s_register(struct file *file, void *fh, |
Hans Verkuil | aecde8b | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 970 | struct v4l2_dbg_register *reg) |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 971 | { |
Guennadi Liakhovetski | 57bee29 | 2010-08-17 14:29:51 -0300 | [diff] [blame] | 972 | struct soc_camera_device *icd = file->private_data; |
Guennadi Liakhovetski | c9c1f1c | 2009-08-25 11:46:59 -0300 | [diff] [blame] | 973 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 974 | |
Guennadi Liakhovetski | c9c1f1c | 2009-08-25 11:46:59 -0300 | [diff] [blame] | 975 | return v4l2_subdev_call(sd, core, s_register, reg); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 976 | } |
| 977 | #endif |
| 978 | |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 979 | /* So far this function cannot fail */ |
| 980 | static void scan_add_host(struct soc_camera_host *ici) |
| 981 | { |
| 982 | struct soc_camera_device *icd; |
| 983 | |
| 984 | mutex_lock(&list_lock); |
| 985 | |
| 986 | list_for_each_entry(icd, &devices, list) { |
| 987 | if (icd->iface == ici->nr) { |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 988 | int ret; |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 989 | icd->dev.parent = ici->v4l2_dev.dev; |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 990 | dev_set_name(&icd->dev, "%u-%u", icd->iface, |
| 991 | icd->devnum); |
| 992 | ret = device_register(&icd->dev); |
| 993 | if (ret < 0) { |
| 994 | icd->dev.parent = NULL; |
| 995 | dev_err(&icd->dev, |
| 996 | "Cannot register device: %d\n", ret); |
| 997 | } |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 998 | } |
| 999 | } |
| 1000 | |
| 1001 | mutex_unlock(&list_lock); |
| 1002 | } |
| 1003 | |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1004 | #ifdef CONFIG_I2C_BOARDINFO |
| 1005 | static int soc_camera_init_i2c(struct soc_camera_device *icd, |
| 1006 | struct soc_camera_link *icl) |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1007 | { |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1008 | struct i2c_client *client; |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 1009 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1010 | struct i2c_adapter *adap = i2c_get_adapter(icl->i2c_adapter_id); |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 1011 | struct v4l2_subdev *subdev; |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1012 | |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1013 | if (!adap) { |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1014 | dev_err(&icd->dev, "Cannot get I2C adapter #%d. No driver?\n", |
| 1015 | icl->i2c_adapter_id); |
| 1016 | goto ei2cga; |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1017 | } |
| 1018 | |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1019 | icl->board_info->platform_data = icd; |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1020 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 1021 | subdev = v4l2_i2c_new_subdev_board(&ici->v4l2_dev, adap, |
Laurent Pinchart | 9a1f8b3 | 2010-09-24 10:16:44 -0300 | [diff] [blame] | 1022 | icl->board_info, NULL); |
Magnus Damm | d74f841 | 2010-02-09 17:40:40 +0900 | [diff] [blame] | 1023 | if (!subdev) |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1024 | goto ei2cnd; |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1025 | |
Laurent Pinchart | c4ce6d1 | 2010-07-30 17:24:54 -0300 | [diff] [blame] | 1026 | client = v4l2_get_subdevdata(subdev); |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 1027 | |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1028 | /* Use to_i2c_client(dev) to recover the i2c client */ |
| 1029 | dev_set_drvdata(&icd->dev, &client->dev); |
| 1030 | |
| 1031 | return 0; |
| 1032 | ei2cnd: |
| 1033 | i2c_put_adapter(adap); |
| 1034 | ei2cga: |
Magnus Damm | d74f841 | 2010-02-09 17:40:40 +0900 | [diff] [blame] | 1035 | return -ENODEV; |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1036 | } |
| 1037 | |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1038 | static void soc_camera_free_i2c(struct soc_camera_device *icd) |
| 1039 | { |
| 1040 | struct i2c_client *client = |
| 1041 | to_i2c_client(to_soc_camera_control(icd)); |
Guennadi Liakhovetski | c8dd707 | 2011-03-23 05:26:11 -0300 | [diff] [blame] | 1042 | struct i2c_adapter *adap = client->adapter; |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1043 | dev_set_drvdata(&icd->dev, NULL); |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 1044 | v4l2_device_unregister_subdev(i2c_get_clientdata(client)); |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1045 | i2c_unregister_device(client); |
Guennadi Liakhovetski | c8dd707 | 2011-03-23 05:26:11 -0300 | [diff] [blame] | 1046 | i2c_put_adapter(adap); |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1047 | } |
| 1048 | #else |
| 1049 | #define soc_camera_init_i2c(icd, icl) (-ENODEV) |
| 1050 | #define soc_camera_free_i2c(icd) do {} while (0) |
| 1051 | #endif |
| 1052 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 1053 | static int soc_camera_video_start(struct soc_camera_device *icd); |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1054 | static int video_dev_create(struct soc_camera_device *icd); |
| 1055 | /* Called during host-driver probe */ |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1056 | static int soc_camera_probe(struct device *dev) |
| 1057 | { |
| 1058 | struct soc_camera_device *icd = to_soc_camera_dev(dev); |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 1059 | struct soc_camera_host *ici = to_soc_camera_host(dev->parent); |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1060 | struct soc_camera_link *icl = to_soc_camera_link(icd); |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 1061 | struct device *control = NULL; |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 1062 | struct v4l2_subdev *sd; |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 1063 | struct v4l2_mbus_framefmt mf; |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1064 | int ret; |
| 1065 | |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1066 | dev_info(dev, "Probing %s\n", dev_name(dev)); |
Guennadi Liakhovetski | 1c3bb74 | 2008-12-18 12:28:54 -0300 | [diff] [blame] | 1067 | |
Alberto Panizzo | 96e442c | 2010-12-02 07:43:37 -0300 | [diff] [blame] | 1068 | ret = regulator_bulk_get(icd->pdev, icl->num_regulators, |
| 1069 | icl->regulators); |
| 1070 | if (ret < 0) |
| 1071 | goto ereg; |
| 1072 | |
| 1073 | ret = soc_camera_power_set(icd, icl, 1); |
| 1074 | if (ret < 0) |
| 1075 | goto epower; |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 1076 | |
| 1077 | /* The camera could have been already on, try to reset */ |
| 1078 | if (icl->reset) |
| 1079 | icl->reset(icd->pdev); |
| 1080 | |
| 1081 | ret = ici->ops->add(icd); |
| 1082 | if (ret < 0) |
| 1083 | goto eadd; |
| 1084 | |
| 1085 | /* Must have icd->vdev before registering the device */ |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1086 | ret = video_dev_create(icd); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1087 | if (ret < 0) |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1088 | goto evdc; |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1089 | |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1090 | /* Non-i2c cameras, e.g., soc_camera_platform, have no board_info */ |
| 1091 | if (icl->board_info) { |
| 1092 | ret = soc_camera_init_i2c(icd, icl); |
| 1093 | if (ret < 0) |
| 1094 | goto eadddev; |
| 1095 | } else if (!icl->add_device || !icl->del_device) { |
| 1096 | ret = -EINVAL; |
| 1097 | goto eadddev; |
| 1098 | } else { |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 1099 | if (icl->module_name) |
| 1100 | ret = request_module(icl->module_name); |
| 1101 | |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1102 | ret = icl->add_device(icl, &icd->dev); |
| 1103 | if (ret < 0) |
| 1104 | goto eadddev; |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 1105 | |
Guennadi Liakhovetski | 96c7539 | 2009-08-25 11:53:23 -0300 | [diff] [blame] | 1106 | /* |
| 1107 | * FIXME: this is racy, have to use driver-binding notification, |
| 1108 | * when it is available |
| 1109 | */ |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 1110 | control = to_soc_camera_control(icd); |
Guennadi Liakhovetski | 08590b9 | 2009-08-25 11:46:54 -0300 | [diff] [blame] | 1111 | if (!control || !control->driver || !dev_get_drvdata(control) || |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 1112 | !try_module_get(control->driver->owner)) { |
| 1113 | icl->del_device(icl); |
| 1114 | goto enodrv; |
| 1115 | } |
Guennadi Liakhovetski | 025c18a | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 1116 | } |
| 1117 | |
Guennadi Liakhovetski | 24105eb | 2011-03-23 05:41:28 -0300 | [diff] [blame] | 1118 | sd = soc_camera_to_subdev(icd); |
| 1119 | sd->grp_id = (long)icd; |
| 1120 | |
Guennadi Liakhovetski | fa48984 | 2009-08-25 11:46:43 -0300 | [diff] [blame] | 1121 | /* At this point client .probe() should have run already */ |
| 1122 | ret = soc_camera_init_user_formats(icd); |
| 1123 | if (ret < 0) |
| 1124 | goto eiufmt; |
| 1125 | |
Guennadi Liakhovetski | fa48984 | 2009-08-25 11:46:43 -0300 | [diff] [blame] | 1126 | icd->field = V4L2_FIELD_ANY; |
| 1127 | |
Guennadi Liakhovetski | b6a633c | 2010-12-25 17:40:26 -0300 | [diff] [blame] | 1128 | icd->vdev->lock = &icd->video_lock; |
| 1129 | |
| 1130 | /* |
| 1131 | * ..._video_start() will create a device node, video_register_device() |
| 1132 | * itself is protected against concurrent open() calls, but we also have |
| 1133 | * to protect our data. |
| 1134 | */ |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 1135 | mutex_lock(&icd->video_lock); |
| 1136 | |
| 1137 | ret = soc_camera_video_start(icd); |
| 1138 | if (ret < 0) |
| 1139 | goto evidstart; |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1140 | |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 1141 | /* Try to improve our guess of a reasonable window format */ |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 1142 | if (!v4l2_subdev_call(sd, video, g_mbus_fmt, &mf)) { |
| 1143 | icd->user_width = mf.width; |
| 1144 | icd->user_height = mf.height; |
| 1145 | icd->colorspace = mf.colorspace; |
| 1146 | icd->field = mf.field; |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 1147 | } |
| 1148 | |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1149 | /* Do we have to sysfs_remove_link() before device_unregister()? */ |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 1150 | if (sysfs_create_link(&icd->dev.kobj, &to_soc_camera_control(icd)->kobj, |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1151 | "control")) |
| 1152 | dev_warn(&icd->dev, "Failed creating the control symlink\n"); |
| 1153 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 1154 | ici->ops->remove(icd); |
| 1155 | |
Alberto Panizzo | 96e442c | 2010-12-02 07:43:37 -0300 | [diff] [blame] | 1156 | soc_camera_power_set(icd, icl, 0); |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 1157 | |
| 1158 | mutex_unlock(&icd->video_lock); |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1159 | |
| 1160 | return 0; |
| 1161 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 1162 | evidstart: |
| 1163 | mutex_unlock(&icd->video_lock); |
Guennadi Liakhovetski | fa48984 | 2009-08-25 11:46:43 -0300 | [diff] [blame] | 1164 | soc_camera_free_user_formats(icd); |
| 1165 | eiufmt: |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 1166 | if (icl->board_info) { |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1167 | soc_camera_free_i2c(icd); |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 1168 | } else { |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1169 | icl->del_device(icl); |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 1170 | module_put(control->driver->owner); |
| 1171 | } |
| 1172 | enodrv: |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1173 | eadddev: |
| 1174 | video_device_release(icd->vdev); |
| 1175 | evdc: |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 1176 | ici->ops->remove(icd); |
| 1177 | eadd: |
Alberto Panizzo | 96e442c | 2010-12-02 07:43:37 -0300 | [diff] [blame] | 1178 | soc_camera_power_set(icd, icl, 0); |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 1179 | epower: |
Alberto Panizzo | 96e442c | 2010-12-02 07:43:37 -0300 | [diff] [blame] | 1180 | regulator_bulk_free(icl->num_regulators, icl->regulators); |
| 1181 | ereg: |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1182 | return ret; |
| 1183 | } |
| 1184 | |
Guennadi Liakhovetski | 5d28d52 | 2009-12-11 11:15:05 -0300 | [diff] [blame] | 1185 | /* |
| 1186 | * This is called on device_unregister, which only means we have to disconnect |
| 1187 | * from the host, but not remove ourselves from the device list |
| 1188 | */ |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1189 | static int soc_camera_remove(struct device *dev) |
| 1190 | { |
| 1191 | struct soc_camera_device *icd = to_soc_camera_dev(dev); |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1192 | struct soc_camera_link *icl = to_soc_camera_link(icd); |
| 1193 | struct video_device *vdev = icd->vdev; |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1194 | |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1195 | BUG_ON(!dev->parent); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1196 | |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1197 | if (vdev) { |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1198 | video_unregister_device(vdev); |
| 1199 | icd->vdev = NULL; |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1200 | } |
| 1201 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 1202 | if (icl->board_info) { |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1203 | soc_camera_free_i2c(icd); |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 1204 | } else { |
| 1205 | struct device_driver *drv = to_soc_camera_control(icd) ? |
| 1206 | to_soc_camera_control(icd)->driver : NULL; |
| 1207 | if (drv) { |
| 1208 | icl->del_device(icl); |
| 1209 | module_put(drv->owner); |
| 1210 | } |
| 1211 | } |
Guennadi Liakhovetski | fa48984 | 2009-08-25 11:46:43 -0300 | [diff] [blame] | 1212 | soc_camera_free_user_formats(icd); |
Guennadi Liakhovetski | 025c18a | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 1213 | |
Alberto Panizzo | 96e442c | 2010-12-02 07:43:37 -0300 | [diff] [blame] | 1214 | regulator_bulk_free(icl->num_regulators, icl->regulators); |
| 1215 | |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1216 | return 0; |
| 1217 | } |
| 1218 | |
Robert Jarzmik | 2e52106 | 2008-08-01 20:14:50 -0300 | [diff] [blame] | 1219 | static int soc_camera_suspend(struct device *dev, pm_message_t state) |
| 1220 | { |
| 1221 | struct soc_camera_device *icd = to_soc_camera_dev(dev); |
| 1222 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
| 1223 | int ret = 0; |
| 1224 | |
| 1225 | if (ici->ops->suspend) |
| 1226 | ret = ici->ops->suspend(icd, state); |
| 1227 | |
| 1228 | return ret; |
| 1229 | } |
| 1230 | |
| 1231 | static int soc_camera_resume(struct device *dev) |
| 1232 | { |
| 1233 | struct soc_camera_device *icd = to_soc_camera_dev(dev); |
| 1234 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
| 1235 | int ret = 0; |
| 1236 | |
| 1237 | if (ici->ops->resume) |
| 1238 | ret = ici->ops->resume(icd); |
| 1239 | |
| 1240 | return ret; |
| 1241 | } |
| 1242 | |
Guennadi Liakhovetski | 52d268a | 2010-07-26 11:37:13 -0300 | [diff] [blame] | 1243 | struct bus_type soc_camera_bus_type = { |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1244 | .name = "soc-camera", |
| 1245 | .probe = soc_camera_probe, |
| 1246 | .remove = soc_camera_remove, |
Robert Jarzmik | 2e52106 | 2008-08-01 20:14:50 -0300 | [diff] [blame] | 1247 | .suspend = soc_camera_suspend, |
| 1248 | .resume = soc_camera_resume, |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1249 | }; |
Guennadi Liakhovetski | 52d268a | 2010-07-26 11:37:13 -0300 | [diff] [blame] | 1250 | EXPORT_SYMBOL_GPL(soc_camera_bus_type); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1251 | |
| 1252 | static struct device_driver ic_drv = { |
| 1253 | .name = "camera", |
| 1254 | .bus = &soc_camera_bus_type, |
| 1255 | .owner = THIS_MODULE, |
| 1256 | }; |
| 1257 | |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1258 | static void dummy_release(struct device *dev) |
| 1259 | { |
| 1260 | } |
| 1261 | |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 1262 | static int default_cropcap(struct soc_camera_device *icd, |
| 1263 | struct v4l2_cropcap *a) |
| 1264 | { |
| 1265 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
| 1266 | return v4l2_subdev_call(sd, video, cropcap, a); |
| 1267 | } |
| 1268 | |
| 1269 | static int default_g_crop(struct soc_camera_device *icd, struct v4l2_crop *a) |
| 1270 | { |
| 1271 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
| 1272 | return v4l2_subdev_call(sd, video, g_crop, a); |
| 1273 | } |
| 1274 | |
| 1275 | static int default_s_crop(struct soc_camera_device *icd, struct v4l2_crop *a) |
| 1276 | { |
| 1277 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
| 1278 | return v4l2_subdev_call(sd, video, s_crop, a); |
| 1279 | } |
| 1280 | |
Janusz Krzysztofik | 06e1782 | 2010-09-10 22:26:16 -0300 | [diff] [blame] | 1281 | static int default_g_parm(struct soc_camera_device *icd, |
| 1282 | struct v4l2_streamparm *parm) |
| 1283 | { |
| 1284 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
| 1285 | return v4l2_subdev_call(sd, video, g_parm, parm); |
| 1286 | } |
| 1287 | |
| 1288 | static int default_s_parm(struct soc_camera_device *icd, |
| 1289 | struct v4l2_streamparm *parm) |
| 1290 | { |
| 1291 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
| 1292 | return v4l2_subdev_call(sd, video, s_parm, parm); |
| 1293 | } |
| 1294 | |
Qing Xu | ed5b65d | 2011-01-20 05:19:40 -0300 | [diff] [blame] | 1295 | static int default_enum_fsizes(struct soc_camera_device *icd, |
| 1296 | struct v4l2_frmsizeenum *fsize) |
| 1297 | { |
| 1298 | int ret; |
| 1299 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
| 1300 | const struct soc_camera_format_xlate *xlate; |
| 1301 | __u32 pixfmt = fsize->pixel_format; |
| 1302 | struct v4l2_frmsizeenum fsize_mbus = *fsize; |
| 1303 | |
| 1304 | xlate = soc_camera_xlate_by_fourcc(icd, pixfmt); |
| 1305 | if (!xlate) |
| 1306 | return -EINVAL; |
| 1307 | /* map xlate-code to pixel_format, sensor only handle xlate-code*/ |
| 1308 | fsize_mbus.pixel_format = xlate->code; |
| 1309 | |
| 1310 | ret = v4l2_subdev_call(sd, video, enum_mbus_fsizes, &fsize_mbus); |
| 1311 | if (ret < 0) |
| 1312 | return ret; |
| 1313 | |
| 1314 | *fsize = fsize_mbus; |
| 1315 | fsize->pixel_format = pixfmt; |
| 1316 | |
| 1317 | return 0; |
| 1318 | } |
| 1319 | |
Guennadi Liakhovetski | 64ff9ba | 2009-11-09 16:12:37 -0300 | [diff] [blame] | 1320 | static void soc_camera_device_init(struct device *dev, void *pdata) |
| 1321 | { |
| 1322 | dev->platform_data = pdata; |
| 1323 | dev->bus = &soc_camera_bus_type; |
| 1324 | dev->release = dummy_release; |
| 1325 | } |
| 1326 | |
Guennadi Liakhovetski | b8d9904 | 2008-04-04 13:41:25 -0300 | [diff] [blame] | 1327 | int soc_camera_host_register(struct soc_camera_host *ici) |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1328 | { |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1329 | struct soc_camera_host *ix; |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 1330 | int ret; |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1331 | |
Guennadi Liakhovetski | 64f5905 | 2008-12-18 11:51:55 -0300 | [diff] [blame] | 1332 | if (!ici || !ici->ops || |
| 1333 | !ici->ops->try_fmt || |
| 1334 | !ici->ops->set_fmt || |
| 1335 | !ici->ops->set_bus_param || |
| 1336 | !ici->ops->querycap || |
Guennadi Liakhovetski | 592c2ab | 2011-01-29 12:44:51 -0300 | [diff] [blame] | 1337 | ((!ici->ops->init_videobuf || |
| 1338 | !ici->ops->reqbufs) && |
| 1339 | !ici->ops->init_videobuf2) || |
Guennadi Liakhovetski | 64f5905 | 2008-12-18 11:51:55 -0300 | [diff] [blame] | 1340 | !ici->ops->add || |
| 1341 | !ici->ops->remove || |
Guennadi Liakhovetski | eff505f | 2009-04-24 12:55:48 -0300 | [diff] [blame] | 1342 | !ici->ops->poll || |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 1343 | !ici->v4l2_dev.dev) |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1344 | return -EINVAL; |
| 1345 | |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 1346 | if (!ici->ops->set_crop) |
| 1347 | ici->ops->set_crop = default_s_crop; |
| 1348 | if (!ici->ops->get_crop) |
| 1349 | ici->ops->get_crop = default_g_crop; |
| 1350 | if (!ici->ops->cropcap) |
| 1351 | ici->ops->cropcap = default_cropcap; |
Janusz Krzysztofik | 06e1782 | 2010-09-10 22:26:16 -0300 | [diff] [blame] | 1352 | if (!ici->ops->set_parm) |
| 1353 | ici->ops->set_parm = default_s_parm; |
| 1354 | if (!ici->ops->get_parm) |
| 1355 | ici->ops->get_parm = default_g_parm; |
Qing Xu | ed5b65d | 2011-01-20 05:19:40 -0300 | [diff] [blame] | 1356 | if (!ici->ops->enum_fsizes) |
| 1357 | ici->ops->enum_fsizes = default_enum_fsizes; |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 1358 | |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1359 | mutex_lock(&list_lock); |
| 1360 | list_for_each_entry(ix, &hosts, list) { |
| 1361 | if (ix->nr == ici->nr) { |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 1362 | ret = -EBUSY; |
| 1363 | goto edevreg; |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1364 | } |
| 1365 | } |
| 1366 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 1367 | ret = v4l2_device_register(ici->v4l2_dev.dev, &ici->v4l2_dev); |
| 1368 | if (ret < 0) |
| 1369 | goto edevreg; |
Guennadi Liakhovetski | eff505f | 2009-04-24 12:55:48 -0300 | [diff] [blame] | 1370 | |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1371 | list_add_tail(&ici->list, &hosts); |
| 1372 | mutex_unlock(&list_lock); |
| 1373 | |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1374 | scan_add_host(ici); |
| 1375 | |
| 1376 | return 0; |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 1377 | |
| 1378 | edevreg: |
| 1379 | mutex_unlock(&list_lock); |
| 1380 | return ret; |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1381 | } |
| 1382 | EXPORT_SYMBOL(soc_camera_host_register); |
| 1383 | |
| 1384 | /* Unregister all clients! */ |
| 1385 | void soc_camera_host_unregister(struct soc_camera_host *ici) |
| 1386 | { |
| 1387 | struct soc_camera_device *icd; |
| 1388 | |
| 1389 | mutex_lock(&list_lock); |
| 1390 | |
| 1391 | list_del(&ici->list); |
| 1392 | |
| 1393 | list_for_each_entry(icd, &devices, list) { |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 1394 | if (icd->iface == ici->nr) { |
Guennadi Liakhovetski | 64ff9ba | 2009-11-09 16:12:37 -0300 | [diff] [blame] | 1395 | void *pdata = icd->dev.platform_data; |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1396 | /* The bus->remove will be called */ |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1397 | device_unregister(&icd->dev); |
Guennadi Liakhovetski | 76823b7 | 2009-10-05 12:54:54 -0300 | [diff] [blame] | 1398 | /* |
| 1399 | * Not before device_unregister(), .remove |
| 1400 | * needs parent to call ici->ops->remove(). |
| 1401 | * If the host module is loaded again, device_register() |
| 1402 | * would complain "already initialised," since 2.6.32 |
| 1403 | * this is also needed to prevent use-after-free of the |
| 1404 | * device private data. |
| 1405 | */ |
| 1406 | memset(&icd->dev, 0, sizeof(icd->dev)); |
Guennadi Liakhovetski | 64ff9ba | 2009-11-09 16:12:37 -0300 | [diff] [blame] | 1407 | soc_camera_device_init(&icd->dev, pdata); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1408 | } |
| 1409 | } |
| 1410 | |
| 1411 | mutex_unlock(&list_lock); |
| 1412 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 1413 | v4l2_device_unregister(&ici->v4l2_dev); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1414 | } |
| 1415 | EXPORT_SYMBOL(soc_camera_host_unregister); |
| 1416 | |
| 1417 | /* Image capture device */ |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1418 | static int soc_camera_device_register(struct soc_camera_device *icd) |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1419 | { |
| 1420 | struct soc_camera_device *ix; |
| 1421 | int num = -1, i; |
| 1422 | |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1423 | for (i = 0; i < 256 && num < 0; i++) { |
| 1424 | num = i; |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1425 | /* Check if this index is available on this interface */ |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1426 | list_for_each_entry(ix, &devices, list) { |
| 1427 | if (ix->iface == icd->iface && ix->devnum == i) { |
| 1428 | num = -1; |
| 1429 | break; |
| 1430 | } |
| 1431 | } |
| 1432 | } |
| 1433 | |
| 1434 | if (num < 0) |
Guennadi Liakhovetski | 5d28d52 | 2009-12-11 11:15:05 -0300 | [diff] [blame] | 1435 | /* |
| 1436 | * ok, we have 256 cameras on this host... |
| 1437 | * man, stay reasonable... |
| 1438 | */ |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1439 | return -ENOMEM; |
| 1440 | |
Guennadi Liakhovetski | 64ff9ba | 2009-11-09 16:12:37 -0300 | [diff] [blame] | 1441 | icd->devnum = num; |
Guennadi Liakhovetski | 64f5905 | 2008-12-18 11:51:55 -0300 | [diff] [blame] | 1442 | icd->use_count = 0; |
| 1443 | icd->host_priv = NULL; |
Guennadi Liakhovetski | 1c3bb74 | 2008-12-18 12:28:54 -0300 | [diff] [blame] | 1444 | mutex_init(&icd->video_lock); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1445 | |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1446 | list_add_tail(&icd->list, &devices); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1447 | |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1448 | return 0; |
| 1449 | } |
| 1450 | |
| 1451 | static void soc_camera_device_unregister(struct soc_camera_device *icd) |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1452 | { |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1453 | list_del(&icd->list); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1454 | } |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1455 | |
Hans Verkuil | a399810 | 2008-07-21 02:57:38 -0300 | [diff] [blame] | 1456 | static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = { |
| 1457 | .vidioc_querycap = soc_camera_querycap, |
| 1458 | .vidioc_g_fmt_vid_cap = soc_camera_g_fmt_vid_cap, |
| 1459 | .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap, |
| 1460 | .vidioc_s_fmt_vid_cap = soc_camera_s_fmt_vid_cap, |
| 1461 | .vidioc_enum_input = soc_camera_enum_input, |
| 1462 | .vidioc_g_input = soc_camera_g_input, |
| 1463 | .vidioc_s_input = soc_camera_s_input, |
| 1464 | .vidioc_s_std = soc_camera_s_std, |
Qing Xu | ed5b65d | 2011-01-20 05:19:40 -0300 | [diff] [blame] | 1465 | .vidioc_enum_framesizes = soc_camera_enum_fsizes, |
Hans Verkuil | a399810 | 2008-07-21 02:57:38 -0300 | [diff] [blame] | 1466 | .vidioc_reqbufs = soc_camera_reqbufs, |
| 1467 | .vidioc_try_fmt_vid_cap = soc_camera_try_fmt_vid_cap, |
| 1468 | .vidioc_querybuf = soc_camera_querybuf, |
| 1469 | .vidioc_qbuf = soc_camera_qbuf, |
| 1470 | .vidioc_dqbuf = soc_camera_dqbuf, |
| 1471 | .vidioc_streamon = soc_camera_streamon, |
| 1472 | .vidioc_streamoff = soc_camera_streamoff, |
| 1473 | .vidioc_queryctrl = soc_camera_queryctrl, |
| 1474 | .vidioc_g_ctrl = soc_camera_g_ctrl, |
| 1475 | .vidioc_s_ctrl = soc_camera_s_ctrl, |
| 1476 | .vidioc_cropcap = soc_camera_cropcap, |
| 1477 | .vidioc_g_crop = soc_camera_g_crop, |
| 1478 | .vidioc_s_crop = soc_camera_s_crop, |
Guennadi Liakhovetski | c9f6ef6 | 2010-02-09 18:00:30 +0100 | [diff] [blame] | 1479 | .vidioc_g_parm = soc_camera_g_parm, |
| 1480 | .vidioc_s_parm = soc_camera_s_parm, |
Hans Verkuil | a399810 | 2008-07-21 02:57:38 -0300 | [diff] [blame] | 1481 | .vidioc_g_chip_ident = soc_camera_g_chip_ident, |
| 1482 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
| 1483 | .vidioc_g_register = soc_camera_g_register, |
| 1484 | .vidioc_s_register = soc_camera_s_register, |
| 1485 | #endif |
| 1486 | }; |
| 1487 | |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1488 | static int video_dev_create(struct soc_camera_device *icd) |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1489 | { |
| 1490 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1491 | struct video_device *vdev = video_device_alloc(); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1492 | |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1493 | if (!vdev) |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1494 | return -ENOMEM; |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1495 | |
| 1496 | strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name)); |
Guennadi Liakhovetski | 1c3bb74 | 2008-12-18 12:28:54 -0300 | [diff] [blame] | 1497 | |
Hans Verkuil | 5e85e73 | 2008-07-20 06:31:39 -0300 | [diff] [blame] | 1498 | vdev->parent = &icd->dev; |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1499 | vdev->current_norm = V4L2_STD_UNKNOWN; |
| 1500 | vdev->fops = &soc_camera_fops; |
Hans Verkuil | a399810 | 2008-07-21 02:57:38 -0300 | [diff] [blame] | 1501 | vdev->ioctl_ops = &soc_camera_ioctl_ops; |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1502 | vdev->release = video_device_release; |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1503 | vdev->tvnorms = V4L2_STD_UNKNOWN; |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1504 | |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1505 | icd->vdev = vdev; |
| 1506 | |
| 1507 | return 0; |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1508 | } |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1509 | |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1510 | /* |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 1511 | * Called from soc_camera_probe() above (with .video_lock held???) |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1512 | */ |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 1513 | static int soc_camera_video_start(struct soc_camera_device *icd) |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1514 | { |
Mauro Carvalho Chehab | 4f9fb5e | 2010-05-18 00:46:09 -0300 | [diff] [blame] | 1515 | struct device_type *type = icd->vdev->dev.type; |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 1516 | int ret; |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1517 | |
| 1518 | if (!icd->dev.parent) |
| 1519 | return -ENODEV; |
| 1520 | |
| 1521 | if (!icd->ops || |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1522 | !icd->ops->query_bus_param || |
| 1523 | !icd->ops->set_bus_param) |
| 1524 | return -EINVAL; |
| 1525 | |
Laurent Pinchart | 46b2109 | 2009-12-10 11:51:38 -0200 | [diff] [blame] | 1526 | ret = video_register_device(icd->vdev, VFL_TYPE_GRABBER, -1); |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 1527 | if (ret < 0) { |
| 1528 | dev_err(&icd->dev, "video_register_device failed: %d\n", ret); |
| 1529 | return ret; |
| 1530 | } |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1531 | |
Mauro Carvalho Chehab | 4f9fb5e | 2010-05-18 00:46:09 -0300 | [diff] [blame] | 1532 | /* Restore device type, possibly set by the subdevice driver */ |
| 1533 | icd->vdev->dev.type = type; |
| 1534 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 1535 | return 0; |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1536 | } |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1537 | |
Guennadi Liakhovetski | c41deba | 2009-08-25 11:06:21 -0300 | [diff] [blame] | 1538 | static int __devinit soc_camera_pdrv_probe(struct platform_device *pdev) |
| 1539 | { |
| 1540 | struct soc_camera_link *icl = pdev->dev.platform_data; |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1541 | struct soc_camera_device *icd; |
| 1542 | int ret; |
Guennadi Liakhovetski | c41deba | 2009-08-25 11:06:21 -0300 | [diff] [blame] | 1543 | |
| 1544 | if (!icl) |
| 1545 | return -EINVAL; |
| 1546 | |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1547 | icd = kzalloc(sizeof(*icd), GFP_KERNEL); |
| 1548 | if (!icd) |
| 1549 | return -ENOMEM; |
Guennadi Liakhovetski | c41deba | 2009-08-25 11:06:21 -0300 | [diff] [blame] | 1550 | |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1551 | icd->iface = icl->bus_id; |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 1552 | icd->pdev = &pdev->dev; |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1553 | platform_set_drvdata(pdev, icd); |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1554 | |
| 1555 | ret = soc_camera_device_register(icd); |
| 1556 | if (ret < 0) |
| 1557 | goto escdevreg; |
| 1558 | |
Guennadi Liakhovetski | 64ff9ba | 2009-11-09 16:12:37 -0300 | [diff] [blame] | 1559 | soc_camera_device_init(&icd->dev, icl); |
| 1560 | |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 1561 | icd->user_width = DEFAULT_WIDTH; |
| 1562 | icd->user_height = DEFAULT_HEIGHT; |
| 1563 | |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1564 | return 0; |
| 1565 | |
| 1566 | escdevreg: |
| 1567 | kfree(icd); |
| 1568 | |
| 1569 | return ret; |
Guennadi Liakhovetski | c41deba | 2009-08-25 11:06:21 -0300 | [diff] [blame] | 1570 | } |
| 1571 | |
Guennadi Liakhovetski | 5d28d52 | 2009-12-11 11:15:05 -0300 | [diff] [blame] | 1572 | /* |
| 1573 | * Only called on rmmod for each platform device, since they are not |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1574 | * hot-pluggable. Now we know, that all our users - hosts and devices have |
Guennadi Liakhovetski | 5d28d52 | 2009-12-11 11:15:05 -0300 | [diff] [blame] | 1575 | * been unloaded already |
| 1576 | */ |
Guennadi Liakhovetski | c41deba | 2009-08-25 11:06:21 -0300 | [diff] [blame] | 1577 | static int __devexit soc_camera_pdrv_remove(struct platform_device *pdev) |
| 1578 | { |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1579 | struct soc_camera_device *icd = platform_get_drvdata(pdev); |
Guennadi Liakhovetski | c41deba | 2009-08-25 11:06:21 -0300 | [diff] [blame] | 1580 | |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1581 | if (!icd) |
| 1582 | return -EINVAL; |
| 1583 | |
| 1584 | soc_camera_device_unregister(icd); |
| 1585 | |
| 1586 | kfree(icd); |
| 1587 | |
Guennadi Liakhovetski | 0fd327b | 2009-05-07 13:25:32 -0300 | [diff] [blame] | 1588 | return 0; |
| 1589 | } |
| 1590 | |
| 1591 | static struct platform_driver __refdata soc_camera_pdrv = { |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1592 | .remove = __devexit_p(soc_camera_pdrv_remove), |
| 1593 | .driver = { |
| 1594 | .name = "soc-camera-pdrv", |
| 1595 | .owner = THIS_MODULE, |
Guennadi Liakhovetski | 0fd327b | 2009-05-07 13:25:32 -0300 | [diff] [blame] | 1596 | }, |
| 1597 | }; |
| 1598 | |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1599 | static int __init soc_camera_init(void) |
| 1600 | { |
| 1601 | int ret = bus_register(&soc_camera_bus_type); |
| 1602 | if (ret) |
| 1603 | return ret; |
| 1604 | ret = driver_register(&ic_drv); |
| 1605 | if (ret) |
| 1606 | goto edrvr; |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1607 | |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1608 | ret = platform_driver_probe(&soc_camera_pdrv, soc_camera_pdrv_probe); |
Guennadi Liakhovetski | 0fd327b | 2009-05-07 13:25:32 -0300 | [diff] [blame] | 1609 | if (ret) |
| 1610 | goto epdr; |
| 1611 | |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1612 | return 0; |
| 1613 | |
Guennadi Liakhovetski | 0fd327b | 2009-05-07 13:25:32 -0300 | [diff] [blame] | 1614 | epdr: |
| 1615 | driver_unregister(&ic_drv); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1616 | edrvr: |
| 1617 | bus_unregister(&soc_camera_bus_type); |
| 1618 | return ret; |
| 1619 | } |
| 1620 | |
| 1621 | static void __exit soc_camera_exit(void) |
| 1622 | { |
Guennadi Liakhovetski | 0fd327b | 2009-05-07 13:25:32 -0300 | [diff] [blame] | 1623 | platform_driver_unregister(&soc_camera_pdrv); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1624 | driver_unregister(&ic_drv); |
| 1625 | bus_unregister(&soc_camera_bus_type); |
| 1626 | } |
| 1627 | |
| 1628 | module_init(soc_camera_init); |
| 1629 | module_exit(soc_camera_exit); |
| 1630 | |
| 1631 | MODULE_DESCRIPTION("Image capture bus driver"); |
| 1632 | MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>"); |
| 1633 | MODULE_LICENSE("GPL"); |
Guennadi Liakhovetski | 0fd327b | 2009-05-07 13:25:32 -0300 | [diff] [blame] | 1634 | MODULE_ALIAS("platform:soc-camera-pdrv"); |