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