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