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