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