Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Samsung S5P SoC series camera interface (camera capture) driver |
| 3 | * |
| 4 | * Copyright (c) 2010 Samsung Electronics Co., Ltd |
| 5 | * Author: Sylwester Nawrocki, <s.nawrocki@samsung.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/version.h> |
| 15 | #include <linux/types.h> |
| 16 | #include <linux/errno.h> |
| 17 | #include <linux/bug.h> |
| 18 | #include <linux/interrupt.h> |
| 19 | #include <linux/device.h> |
| 20 | #include <linux/platform_device.h> |
| 21 | #include <linux/list.h> |
| 22 | #include <linux/slab.h> |
| 23 | #include <linux/clk.h> |
| 24 | #include <linux/i2c.h> |
| 25 | |
| 26 | #include <linux/videodev2.h> |
| 27 | #include <media/v4l2-device.h> |
| 28 | #include <media/v4l2-ioctl.h> |
| 29 | #include <media/v4l2-mem2mem.h> |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 30 | #include <media/videobuf2-core.h> |
| 31 | #include <media/videobuf2-dma-contig.h> |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 32 | |
| 33 | #include "fimc-core.h" |
| 34 | |
| 35 | static struct v4l2_subdev *fimc_subdev_register(struct fimc_dev *fimc, |
| 36 | struct s3c_fimc_isp_info *isp_info) |
| 37 | { |
| 38 | struct i2c_adapter *i2c_adap; |
| 39 | struct fimc_vid_cap *vid_cap = &fimc->vid_cap; |
| 40 | struct v4l2_subdev *sd = NULL; |
| 41 | |
| 42 | i2c_adap = i2c_get_adapter(isp_info->i2c_bus_num); |
| 43 | if (!i2c_adap) |
| 44 | return ERR_PTR(-ENOMEM); |
| 45 | |
| 46 | sd = v4l2_i2c_new_subdev_board(&vid_cap->v4l2_dev, i2c_adap, |
Laurent Pinchart | 9a1f8b3 | 2010-09-24 10:16:44 -0300 | [diff] [blame] | 47 | isp_info->board_info, NULL); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 48 | if (!sd) { |
| 49 | v4l2_err(&vid_cap->v4l2_dev, "failed to acquire subdev\n"); |
| 50 | return NULL; |
| 51 | } |
| 52 | |
| 53 | v4l2_info(&vid_cap->v4l2_dev, "subdevice %s registered successfuly\n", |
| 54 | isp_info->board_info->type); |
| 55 | |
| 56 | return sd; |
| 57 | } |
| 58 | |
| 59 | static void fimc_subdev_unregister(struct fimc_dev *fimc) |
| 60 | { |
| 61 | struct fimc_vid_cap *vid_cap = &fimc->vid_cap; |
| 62 | struct i2c_client *client; |
| 63 | |
| 64 | if (vid_cap->input_index < 0) |
| 65 | return; /* Subdevice already released or not registered. */ |
| 66 | |
| 67 | if (vid_cap->sd) { |
| 68 | v4l2_device_unregister_subdev(vid_cap->sd); |
| 69 | client = v4l2_get_subdevdata(vid_cap->sd); |
| 70 | i2c_unregister_device(client); |
| 71 | i2c_put_adapter(client->adapter); |
| 72 | vid_cap->sd = NULL; |
| 73 | } |
| 74 | |
| 75 | vid_cap->input_index = -1; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * fimc_subdev_attach - attach v4l2_subdev to camera host interface |
| 80 | * |
| 81 | * @fimc: FIMC device information |
| 82 | * @index: index to the array of available subdevices, |
| 83 | * -1 for full array search or non negative value |
| 84 | * to select specific subdevice |
| 85 | */ |
| 86 | static int fimc_subdev_attach(struct fimc_dev *fimc, int index) |
| 87 | { |
| 88 | struct fimc_vid_cap *vid_cap = &fimc->vid_cap; |
| 89 | struct s3c_platform_fimc *pdata = fimc->pdata; |
| 90 | struct s3c_fimc_isp_info *isp_info; |
| 91 | struct v4l2_subdev *sd; |
| 92 | int i; |
| 93 | |
| 94 | for (i = 0; i < FIMC_MAX_CAMIF_CLIENTS; ++i) { |
| 95 | isp_info = pdata->isp_info[i]; |
| 96 | |
| 97 | if (!isp_info || (index >= 0 && i != index)) |
| 98 | continue; |
| 99 | |
| 100 | sd = fimc_subdev_register(fimc, isp_info); |
| 101 | if (sd) { |
| 102 | vid_cap->sd = sd; |
| 103 | vid_cap->input_index = i; |
| 104 | |
| 105 | return 0; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | vid_cap->input_index = -1; |
| 110 | vid_cap->sd = NULL; |
| 111 | v4l2_err(&vid_cap->v4l2_dev, "fimc%d: sensor attach failed\n", |
| 112 | fimc->id); |
| 113 | return -ENODEV; |
| 114 | } |
| 115 | |
| 116 | static int fimc_isp_subdev_init(struct fimc_dev *fimc, int index) |
| 117 | { |
| 118 | struct s3c_fimc_isp_info *isp_info; |
| 119 | int ret; |
| 120 | |
| 121 | ret = fimc_subdev_attach(fimc, index); |
| 122 | if (ret) |
| 123 | return ret; |
| 124 | |
| 125 | isp_info = fimc->pdata->isp_info[fimc->vid_cap.input_index]; |
| 126 | ret = fimc_hw_set_camera_polarity(fimc, isp_info); |
| 127 | if (!ret) { |
| 128 | ret = v4l2_subdev_call(fimc->vid_cap.sd, core, |
| 129 | s_power, 1); |
| 130 | if (!ret) |
| 131 | return ret; |
| 132 | } |
| 133 | |
| 134 | fimc_subdev_unregister(fimc); |
| 135 | err("ISP initialization failed: %d", ret); |
| 136 | return ret; |
| 137 | } |
| 138 | |
| 139 | /* |
| 140 | * At least one buffer on the pending_buf_q queue is required. |
| 141 | * Locking: The caller holds fimc->slock spinlock. |
| 142 | */ |
| 143 | int fimc_vid_cap_buf_queue(struct fimc_dev *fimc, |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 144 | struct fimc_vid_buffer *fimc_vb) |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 145 | { |
| 146 | struct fimc_vid_cap *cap = &fimc->vid_cap; |
| 147 | struct fimc_ctx *ctx = cap->ctx; |
| 148 | int ret = 0; |
| 149 | |
| 150 | BUG_ON(!fimc || !fimc_vb); |
| 151 | |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 152 | ret = fimc_prepare_addr(ctx, &fimc_vb->vb, &ctx->d_frame, |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 153 | &fimc_vb->paddr); |
| 154 | if (ret) |
| 155 | return ret; |
| 156 | |
| 157 | if (test_bit(ST_CAPT_STREAM, &fimc->state)) { |
| 158 | fimc_pending_queue_add(cap, fimc_vb); |
| 159 | } else { |
| 160 | /* Setup the buffer directly for processing. */ |
| 161 | int buf_id = (cap->reqbufs_count == 1) ? -1 : cap->buf_index; |
| 162 | fimc_hw_set_output_addr(fimc, &fimc_vb->paddr, buf_id); |
| 163 | |
| 164 | fimc_vb->index = cap->buf_index; |
| 165 | active_queue_add(cap, fimc_vb); |
| 166 | |
| 167 | if (++cap->buf_index >= FIMC_MAX_OUT_BUFS) |
| 168 | cap->buf_index = 0; |
| 169 | } |
| 170 | return ret; |
| 171 | } |
| 172 | |
| 173 | static int fimc_stop_capture(struct fimc_dev *fimc) |
| 174 | { |
| 175 | unsigned long flags; |
| 176 | struct fimc_vid_cap *cap; |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 177 | struct fimc_vid_buffer *buf; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 178 | |
| 179 | cap = &fimc->vid_cap; |
| 180 | |
| 181 | if (!fimc_capture_active(fimc)) |
| 182 | return 0; |
| 183 | |
| 184 | spin_lock_irqsave(&fimc->slock, flags); |
| 185 | set_bit(ST_CAPT_SHUT, &fimc->state); |
| 186 | fimc_deactivate_capture(fimc); |
| 187 | spin_unlock_irqrestore(&fimc->slock, flags); |
| 188 | |
| 189 | wait_event_timeout(fimc->irq_queue, |
| 190 | test_bit(ST_CAPT_SHUT, &fimc->state), |
| 191 | FIMC_SHUTDOWN_TIMEOUT); |
| 192 | |
| 193 | ret = v4l2_subdev_call(cap->sd, video, s_stream, 0); |
| 194 | if (ret) |
| 195 | v4l2_err(&fimc->vid_cap.v4l2_dev, "s_stream(0) failed\n"); |
| 196 | |
| 197 | spin_lock_irqsave(&fimc->slock, flags); |
| 198 | fimc->state &= ~(1 << ST_CAPT_RUN | 1 << ST_CAPT_PEND | |
| 199 | 1 << ST_CAPT_STREAM); |
| 200 | |
| 201 | fimc->vid_cap.active_buf_cnt = 0; |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 202 | |
| 203 | /* Release buffers that were enqueued in the driver by videobuf2. */ |
| 204 | while (!list_empty(&cap->pending_buf_q)) { |
| 205 | buf = pending_queue_pop(cap); |
| 206 | vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR); |
| 207 | } |
| 208 | |
| 209 | while (!list_empty(&cap->active_buf_q)) { |
| 210 | buf = active_queue_pop(cap); |
| 211 | vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR); |
| 212 | } |
| 213 | |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 214 | spin_unlock_irqrestore(&fimc->slock, flags); |
| 215 | |
| 216 | dbg("state: 0x%lx", fimc->state); |
| 217 | return 0; |
| 218 | } |
| 219 | |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 220 | static int start_streaming(struct vb2_queue *q) |
| 221 | { |
| 222 | struct fimc_ctx *ctx = q->drv_priv; |
| 223 | struct fimc_dev *fimc = ctx->fimc_dev; |
| 224 | struct s3c_fimc_isp_info *isp_info; |
| 225 | int ret; |
| 226 | |
| 227 | ret = v4l2_subdev_call(fimc->vid_cap.sd, video, s_stream, 1); |
| 228 | if (ret && ret != -ENOIOCTLCMD) |
| 229 | return ret; |
| 230 | |
| 231 | ret = fimc_prepare_config(ctx, ctx->state); |
| 232 | if (ret) |
| 233 | return ret; |
| 234 | |
| 235 | isp_info = fimc->pdata->isp_info[fimc->vid_cap.input_index]; |
| 236 | fimc_hw_set_camera_type(fimc, isp_info); |
| 237 | fimc_hw_set_camera_source(fimc, isp_info); |
| 238 | fimc_hw_set_camera_offset(fimc, &ctx->s_frame); |
| 239 | |
| 240 | if (ctx->state & FIMC_PARAMS) { |
| 241 | ret = fimc_set_scaler_info(ctx); |
| 242 | if (ret) { |
| 243 | err("Scaler setup error"); |
| 244 | return ret; |
| 245 | } |
| 246 | fimc_hw_set_input_path(ctx); |
| 247 | fimc_hw_set_scaler(ctx); |
| 248 | fimc_hw_set_target_format(ctx); |
| 249 | fimc_hw_set_rotation(ctx); |
| 250 | fimc_hw_set_effect(ctx); |
| 251 | } |
| 252 | |
| 253 | fimc_hw_set_output_path(ctx); |
| 254 | fimc_hw_set_out_dma(ctx); |
| 255 | |
| 256 | INIT_LIST_HEAD(&fimc->vid_cap.pending_buf_q); |
| 257 | INIT_LIST_HEAD(&fimc->vid_cap.active_buf_q); |
| 258 | fimc->vid_cap.active_buf_cnt = 0; |
| 259 | fimc->vid_cap.frame_count = 0; |
| 260 | fimc->vid_cap.buf_index = fimc_hw_get_frame_index(fimc); |
| 261 | |
| 262 | set_bit(ST_CAPT_PEND, &fimc->state); |
| 263 | |
| 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | static int stop_streaming(struct vb2_queue *q) |
| 268 | { |
| 269 | struct fimc_ctx *ctx = q->drv_priv; |
| 270 | struct fimc_dev *fimc = ctx->fimc_dev; |
| 271 | unsigned long flags; |
| 272 | |
| 273 | spin_lock_irqsave(&fimc->slock, flags); |
| 274 | if (!fimc_capture_running(fimc) && !fimc_capture_pending(fimc)) { |
| 275 | spin_unlock_irqrestore(&fimc->slock, flags); |
| 276 | return -EINVAL; |
| 277 | } |
| 278 | spin_unlock_irqrestore(&fimc->slock, flags); |
| 279 | |
| 280 | return fimc_stop_capture(fimc); |
| 281 | } |
| 282 | |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 283 | static unsigned int get_plane_size(struct fimc_frame *fr, unsigned int plane) |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 284 | { |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 285 | if (!fr || plane >= fr->fmt->memplanes) |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 286 | return 0; |
| 287 | |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 288 | dbg("%s: w: %d. h: %d. depth[%d]: %d", |
| 289 | __func__, fr->width, fr->height, plane, fr->fmt->depth[plane]); |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 290 | |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 291 | return fr->f_width * fr->f_height * fr->fmt->depth[plane] / 8; |
| 292 | |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | static int queue_setup(struct vb2_queue *vq, unsigned int *num_buffers, |
| 296 | unsigned int *num_planes, unsigned long sizes[], |
| 297 | void *allocators[]) |
| 298 | { |
| 299 | struct fimc_ctx *ctx = vq->drv_priv; |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 300 | struct fimc_fmt *fmt = ctx->d_frame.fmt; |
| 301 | int i; |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 302 | |
| 303 | if (!fmt) |
| 304 | return -EINVAL; |
| 305 | |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 306 | *num_planes = fmt->memplanes; |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 307 | |
| 308 | dbg("%s, buffer count=%d, plane count=%d", |
| 309 | __func__, *num_buffers, *num_planes); |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 310 | |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 311 | for (i = 0; i < fmt->memplanes; i++) { |
| 312 | sizes[i] = get_plane_size(&ctx->d_frame, i); |
| 313 | dbg("plane: %u, plane_size: %lu", i, sizes[i]); |
| 314 | allocators[i] = ctx->fimc_dev->alloc_ctx; |
| 315 | } |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 316 | |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 317 | return 0; |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | static int buffer_init(struct vb2_buffer *vb) |
| 321 | { |
| 322 | /* TODO: */ |
| 323 | return 0; |
| 324 | } |
| 325 | |
| 326 | static int buffer_prepare(struct vb2_buffer *vb) |
| 327 | { |
| 328 | struct vb2_queue *vq = vb->vb2_queue; |
| 329 | struct fimc_ctx *ctx = vq->drv_priv; |
| 330 | struct v4l2_device *v4l2_dev = &ctx->fimc_dev->m2m.v4l2_dev; |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 331 | int i; |
| 332 | |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 333 | if (!ctx->d_frame.fmt || vq->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) |
| 334 | return -EINVAL; |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 335 | |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 336 | for (i = 0; i < ctx->d_frame.fmt->memplanes; i++) { |
| 337 | unsigned long size = get_plane_size(&ctx->d_frame, i); |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 338 | |
| 339 | if (vb2_plane_size(vb, i) < size) { |
| 340 | v4l2_err(v4l2_dev, "User buffer too small (%ld < %ld)\n", |
| 341 | vb2_plane_size(vb, i), size); |
| 342 | return -EINVAL; |
| 343 | } |
| 344 | |
| 345 | vb2_set_plane_payload(vb, i, size); |
| 346 | } |
| 347 | |
| 348 | return 0; |
| 349 | } |
| 350 | |
| 351 | static void buffer_queue(struct vb2_buffer *vb) |
| 352 | { |
| 353 | struct fimc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); |
| 354 | struct fimc_dev *fimc = ctx->fimc_dev; |
| 355 | struct fimc_vid_buffer *buf |
| 356 | = container_of(vb, struct fimc_vid_buffer, vb); |
| 357 | struct fimc_vid_cap *vid_cap = &fimc->vid_cap; |
| 358 | unsigned long flags; |
| 359 | |
| 360 | spin_lock_irqsave(&fimc->slock, flags); |
| 361 | fimc_vid_cap_buf_queue(fimc, buf); |
| 362 | |
| 363 | dbg("active_buf_cnt: %d", fimc->vid_cap.active_buf_cnt); |
| 364 | |
| 365 | if (vid_cap->active_buf_cnt >= vid_cap->reqbufs_count || |
| 366 | vid_cap->active_buf_cnt >= FIMC_MAX_OUT_BUFS) { |
| 367 | if (!test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) { |
| 368 | fimc_activate_capture(ctx); |
| 369 | dbg(""); |
| 370 | } |
| 371 | } |
| 372 | spin_unlock_irqrestore(&fimc->slock, flags); |
| 373 | } |
| 374 | |
| 375 | static void fimc_lock(struct vb2_queue *vq) |
| 376 | { |
| 377 | struct fimc_ctx *ctx = vb2_get_drv_priv(vq); |
| 378 | mutex_lock(&ctx->fimc_dev->lock); |
| 379 | } |
| 380 | |
| 381 | static void fimc_unlock(struct vb2_queue *vq) |
| 382 | { |
| 383 | struct fimc_ctx *ctx = vb2_get_drv_priv(vq); |
| 384 | mutex_unlock(&ctx->fimc_dev->lock); |
| 385 | } |
| 386 | |
| 387 | static struct vb2_ops fimc_capture_qops = { |
| 388 | .queue_setup = queue_setup, |
| 389 | .buf_prepare = buffer_prepare, |
| 390 | .buf_queue = buffer_queue, |
| 391 | .buf_init = buffer_init, |
| 392 | .wait_prepare = fimc_unlock, |
| 393 | .wait_finish = fimc_lock, |
| 394 | .start_streaming = start_streaming, |
| 395 | .stop_streaming = stop_streaming, |
| 396 | }; |
| 397 | |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 398 | static int fimc_capture_open(struct file *file) |
| 399 | { |
| 400 | struct fimc_dev *fimc = video_drvdata(file); |
| 401 | int ret = 0; |
| 402 | |
| 403 | dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state); |
| 404 | |
| 405 | /* Return if the corresponding video mem2mem node is already opened. */ |
| 406 | if (fimc_m2m_active(fimc)) |
| 407 | return -EBUSY; |
| 408 | |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 409 | if (++fimc->vid_cap.refcnt == 1) { |
| 410 | ret = fimc_isp_subdev_init(fimc, -1); |
| 411 | if (ret) { |
| 412 | fimc->vid_cap.refcnt--; |
Sylwester Nawrocki | 8293ebf | 2010-12-01 10:25:18 -0300 | [diff] [blame^] | 413 | return -EIO; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 414 | } |
| 415 | } |
| 416 | |
| 417 | file->private_data = fimc->vid_cap.ctx; |
| 418 | |
Sylwester Nawrocki | 8293ebf | 2010-12-01 10:25:18 -0300 | [diff] [blame^] | 419 | return 0; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | static int fimc_capture_close(struct file *file) |
| 423 | { |
| 424 | struct fimc_dev *fimc = video_drvdata(file); |
| 425 | |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 426 | dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state); |
| 427 | |
| 428 | if (--fimc->vid_cap.refcnt == 0) { |
| 429 | fimc_stop_capture(fimc); |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 430 | vb2_queue_release(&fimc->vid_cap.vbq); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 431 | |
| 432 | v4l2_err(&fimc->vid_cap.v4l2_dev, "releasing ISP\n"); |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 433 | |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 434 | v4l2_subdev_call(fimc->vid_cap.sd, core, s_power, 0); |
| 435 | fimc_subdev_unregister(fimc); |
| 436 | } |
| 437 | |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 438 | return 0; |
| 439 | } |
| 440 | |
| 441 | static unsigned int fimc_capture_poll(struct file *file, |
| 442 | struct poll_table_struct *wait) |
| 443 | { |
| 444 | struct fimc_ctx *ctx = file->private_data; |
| 445 | struct fimc_dev *fimc = ctx->fimc_dev; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 446 | |
Sylwester Nawrocki | 8293ebf | 2010-12-01 10:25:18 -0300 | [diff] [blame^] | 447 | return vb2_poll(&fimc->vid_cap.vbq, file, wait); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | static int fimc_capture_mmap(struct file *file, struct vm_area_struct *vma) |
| 451 | { |
| 452 | struct fimc_ctx *ctx = file->private_data; |
| 453 | struct fimc_dev *fimc = ctx->fimc_dev; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 454 | |
Sylwester Nawrocki | 8293ebf | 2010-12-01 10:25:18 -0300 | [diff] [blame^] | 455 | return vb2_mmap(&fimc->vid_cap.vbq, vma); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | /* video device file operations */ |
| 459 | static const struct v4l2_file_operations fimc_capture_fops = { |
| 460 | .owner = THIS_MODULE, |
| 461 | .open = fimc_capture_open, |
| 462 | .release = fimc_capture_close, |
| 463 | .poll = fimc_capture_poll, |
| 464 | .unlocked_ioctl = video_ioctl2, |
| 465 | .mmap = fimc_capture_mmap, |
| 466 | }; |
| 467 | |
| 468 | static int fimc_vidioc_querycap_capture(struct file *file, void *priv, |
| 469 | struct v4l2_capability *cap) |
| 470 | { |
| 471 | struct fimc_ctx *ctx = file->private_data; |
| 472 | struct fimc_dev *fimc = ctx->fimc_dev; |
| 473 | |
| 474 | strncpy(cap->driver, fimc->pdev->name, sizeof(cap->driver) - 1); |
| 475 | strncpy(cap->card, fimc->pdev->name, sizeof(cap->card) - 1); |
| 476 | cap->bus_info[0] = 0; |
| 477 | cap->version = KERNEL_VERSION(1, 0, 0); |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 478 | cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE | |
| 479 | V4L2_CAP_VIDEO_CAPTURE_MPLANE; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 480 | |
| 481 | return 0; |
| 482 | } |
| 483 | |
| 484 | /* Synchronize formats of the camera interface input and attached sensor. */ |
| 485 | static int sync_capture_fmt(struct fimc_ctx *ctx) |
| 486 | { |
| 487 | struct fimc_frame *frame = &ctx->s_frame; |
| 488 | struct fimc_dev *fimc = ctx->fimc_dev; |
| 489 | struct v4l2_mbus_framefmt *fmt = &fimc->vid_cap.fmt; |
| 490 | int ret; |
| 491 | |
| 492 | fmt->width = ctx->d_frame.o_width; |
| 493 | fmt->height = ctx->d_frame.o_height; |
| 494 | |
| 495 | ret = v4l2_subdev_call(fimc->vid_cap.sd, video, s_mbus_fmt, fmt); |
| 496 | if (ret == -ENOIOCTLCMD) { |
| 497 | err("s_mbus_fmt failed"); |
| 498 | return ret; |
| 499 | } |
| 500 | dbg("w: %d, h: %d, code= %d", fmt->width, fmt->height, fmt->code); |
| 501 | |
| 502 | frame->fmt = find_mbus_format(fmt, FMT_FLAGS_CAM); |
| 503 | if (!frame->fmt) { |
| 504 | err("fimc source format not found\n"); |
| 505 | return -EINVAL; |
| 506 | } |
| 507 | |
| 508 | frame->f_width = fmt->width; |
| 509 | frame->f_height = fmt->height; |
| 510 | frame->width = fmt->width; |
| 511 | frame->height = fmt->height; |
| 512 | frame->o_width = fmt->width; |
| 513 | frame->o_height = fmt->height; |
| 514 | frame->offs_h = 0; |
| 515 | frame->offs_v = 0; |
| 516 | |
| 517 | return 0; |
| 518 | } |
| 519 | |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 520 | static int fimc_cap_s_fmt_mplane(struct file *file, void *priv, |
| 521 | struct v4l2_format *f) |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 522 | { |
| 523 | struct fimc_ctx *ctx = priv; |
| 524 | struct fimc_dev *fimc = ctx->fimc_dev; |
| 525 | struct fimc_frame *frame; |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 526 | struct v4l2_pix_format_mplane *pix; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 527 | int ret; |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 528 | int i; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 529 | |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 530 | if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 531 | return -EINVAL; |
| 532 | |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 533 | ret = fimc_vidioc_try_fmt_mplane(file, priv, f); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 534 | if (ret) |
| 535 | return ret; |
| 536 | |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 537 | if (vb2_is_streaming(&fimc->vid_cap.vbq) || fimc_capture_active(fimc)) |
| 538 | return -EBUSY; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 539 | |
| 540 | frame = &ctx->d_frame; |
| 541 | |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 542 | pix = &f->fmt.pix_mp; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 543 | frame->fmt = find_format(f, FMT_FLAGS_M2M | FMT_FLAGS_CAM); |
| 544 | if (!frame->fmt) { |
| 545 | err("fimc target format not found\n"); |
Sylwester Nawrocki | 8293ebf | 2010-12-01 10:25:18 -0300 | [diff] [blame^] | 546 | return -EINVAL; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 547 | } |
| 548 | |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 549 | for (i = 0; i < frame->fmt->colplanes; i++) |
| 550 | frame->payload[i] = pix->plane_fmt[i].bytesperline * pix->height; |
| 551 | |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 552 | /* Output DMA frame pixel size and offsets. */ |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 553 | frame->f_width = pix->plane_fmt[0].bytesperline * 8 |
| 554 | / frame->fmt->depth[0]; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 555 | frame->f_height = pix->height; |
| 556 | frame->width = pix->width; |
| 557 | frame->height = pix->height; |
| 558 | frame->o_width = pix->width; |
| 559 | frame->o_height = pix->height; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 560 | frame->offs_h = 0; |
| 561 | frame->offs_v = 0; |
| 562 | |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 563 | ctx->state |= (FIMC_PARAMS | FIMC_DST_FMT); |
| 564 | |
Sylwester Nawrocki | 8293ebf | 2010-12-01 10:25:18 -0300 | [diff] [blame^] | 565 | ret = sync_capture_fmt(ctx); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 566 | return ret; |
| 567 | } |
| 568 | |
| 569 | static int fimc_cap_enum_input(struct file *file, void *priv, |
| 570 | struct v4l2_input *i) |
| 571 | { |
| 572 | struct fimc_ctx *ctx = priv; |
| 573 | struct s3c_platform_fimc *pldata = ctx->fimc_dev->pdata; |
| 574 | struct s3c_fimc_isp_info *isp_info; |
| 575 | |
| 576 | if (i->index >= FIMC_MAX_CAMIF_CLIENTS) |
| 577 | return -EINVAL; |
| 578 | |
| 579 | isp_info = pldata->isp_info[i->index]; |
| 580 | if (isp_info == NULL) |
| 581 | return -EINVAL; |
| 582 | |
| 583 | i->type = V4L2_INPUT_TYPE_CAMERA; |
| 584 | strncpy(i->name, isp_info->board_info->type, 32); |
| 585 | return 0; |
| 586 | } |
| 587 | |
| 588 | static int fimc_cap_s_input(struct file *file, void *priv, |
| 589 | unsigned int i) |
| 590 | { |
| 591 | struct fimc_ctx *ctx = priv; |
| 592 | struct fimc_dev *fimc = ctx->fimc_dev; |
| 593 | struct s3c_platform_fimc *pdata = fimc->pdata; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 594 | |
| 595 | if (fimc_capture_active(ctx->fimc_dev)) |
| 596 | return -EBUSY; |
| 597 | |
Sylwester Nawrocki | 8293ebf | 2010-12-01 10:25:18 -0300 | [diff] [blame^] | 598 | if (i >= FIMC_MAX_CAMIF_CLIENTS || !pdata->isp_info[i]) |
| 599 | return -EINVAL; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 600 | |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 601 | |
| 602 | if (fimc->vid_cap.sd) { |
Sylwester Nawrocki | 8293ebf | 2010-12-01 10:25:18 -0300 | [diff] [blame^] | 603 | int ret = v4l2_subdev_call(fimc->vid_cap.sd, core, s_power, 0); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 604 | if (ret) |
| 605 | err("s_power failed: %d", ret); |
| 606 | } |
| 607 | |
| 608 | /* Release the attached sensor subdevice. */ |
| 609 | fimc_subdev_unregister(fimc); |
| 610 | |
Sylwester Nawrocki | 8293ebf | 2010-12-01 10:25:18 -0300 | [diff] [blame^] | 611 | return fimc_isp_subdev_init(fimc, i); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | static int fimc_cap_g_input(struct file *file, void *priv, |
| 615 | unsigned int *i) |
| 616 | { |
| 617 | struct fimc_ctx *ctx = priv; |
| 618 | struct fimc_vid_cap *cap = &ctx->fimc_dev->vid_cap; |
| 619 | |
| 620 | *i = cap->input_index; |
| 621 | return 0; |
| 622 | } |
| 623 | |
| 624 | static int fimc_cap_streamon(struct file *file, void *priv, |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 625 | enum v4l2_buf_type type) |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 626 | { |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 627 | struct fimc_ctx *ctx = priv; |
| 628 | struct fimc_dev *fimc = ctx->fimc_dev; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 629 | |
| 630 | if (fimc_capture_active(fimc) || !fimc->vid_cap.sd) |
Sylwester Nawrocki | 8293ebf | 2010-12-01 10:25:18 -0300 | [diff] [blame^] | 631 | return -EBUSY; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 632 | |
| 633 | if (!(ctx->state & FIMC_DST_FMT)) { |
| 634 | v4l2_err(&fimc->vid_cap.v4l2_dev, "Format is not set\n"); |
Sylwester Nawrocki | 8293ebf | 2010-12-01 10:25:18 -0300 | [diff] [blame^] | 635 | return -EINVAL; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 636 | } |
| 637 | |
Sylwester Nawrocki | 8293ebf | 2010-12-01 10:25:18 -0300 | [diff] [blame^] | 638 | return vb2_streamon(&fimc->vid_cap.vbq, type); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | static int fimc_cap_streamoff(struct file *file, void *priv, |
Sylwester Nawrocki | 8293ebf | 2010-12-01 10:25:18 -0300 | [diff] [blame^] | 642 | enum v4l2_buf_type type) |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 643 | { |
| 644 | struct fimc_ctx *ctx = priv; |
| 645 | struct fimc_dev *fimc = ctx->fimc_dev; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 646 | |
Sylwester Nawrocki | 8293ebf | 2010-12-01 10:25:18 -0300 | [diff] [blame^] | 647 | return vb2_streamoff(&fimc->vid_cap.vbq, type); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | static int fimc_cap_reqbufs(struct file *file, void *priv, |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 651 | struct v4l2_requestbuffers *reqbufs) |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 652 | { |
| 653 | struct fimc_ctx *ctx = priv; |
Sylwester Nawrocki | 8293ebf | 2010-12-01 10:25:18 -0300 | [diff] [blame^] | 654 | struct fimc_vid_cap *cap = &ctx->fimc_dev->vid_cap; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 655 | int ret; |
| 656 | |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 657 | |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 658 | ret = vb2_reqbufs(&cap->vbq, reqbufs); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 659 | if (!ret) |
| 660 | cap->reqbufs_count = reqbufs->count; |
| 661 | |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 662 | return ret; |
| 663 | } |
| 664 | |
| 665 | static int fimc_cap_querybuf(struct file *file, void *priv, |
| 666 | struct v4l2_buffer *buf) |
| 667 | { |
| 668 | struct fimc_ctx *ctx = priv; |
| 669 | struct fimc_vid_cap *cap = &ctx->fimc_dev->vid_cap; |
| 670 | |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 671 | return vb2_querybuf(&cap->vbq, buf); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | static int fimc_cap_qbuf(struct file *file, void *priv, |
| 675 | struct v4l2_buffer *buf) |
| 676 | { |
| 677 | struct fimc_ctx *ctx = priv; |
Sylwester Nawrocki | 8293ebf | 2010-12-01 10:25:18 -0300 | [diff] [blame^] | 678 | struct fimc_vid_cap *cap = &ctx->fimc_dev->vid_cap; |
| 679 | return vb2_qbuf(&cap->vbq, buf); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | static int fimc_cap_dqbuf(struct file *file, void *priv, |
| 683 | struct v4l2_buffer *buf) |
| 684 | { |
| 685 | struct fimc_ctx *ctx = priv; |
Sylwester Nawrocki | 8293ebf | 2010-12-01 10:25:18 -0300 | [diff] [blame^] | 686 | return vb2_dqbuf(&ctx->fimc_dev->vid_cap.vbq, buf, |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 687 | file->f_flags & O_NONBLOCK); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | static int fimc_cap_s_ctrl(struct file *file, void *priv, |
| 691 | struct v4l2_control *ctrl) |
| 692 | { |
| 693 | struct fimc_ctx *ctx = priv; |
| 694 | int ret = -EINVAL; |
| 695 | |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 696 | /* Allow any controls but 90/270 rotation while streaming */ |
| 697 | if (!fimc_capture_active(ctx->fimc_dev) || |
| 698 | ctrl->id != V4L2_CID_ROTATE || |
| 699 | (ctrl->value != 90 && ctrl->value != 270)) { |
| 700 | ret = check_ctrl_val(ctx, ctrl); |
| 701 | if (!ret) { |
| 702 | ret = fimc_s_ctrl(ctx, ctrl); |
| 703 | if (!ret) |
| 704 | ctx->state |= FIMC_PARAMS; |
| 705 | } |
| 706 | } |
| 707 | if (ret == -EINVAL) |
| 708 | ret = v4l2_subdev_call(ctx->fimc_dev->vid_cap.sd, |
| 709 | core, s_ctrl, ctrl); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 710 | return ret; |
| 711 | } |
| 712 | |
Sylwester Nawrocki | e004e02 | 2010-11-25 11:01:51 -0300 | [diff] [blame] | 713 | static int fimc_cap_cropcap(struct file *file, void *fh, |
| 714 | struct v4l2_cropcap *cr) |
| 715 | { |
| 716 | struct fimc_frame *f; |
| 717 | struct fimc_ctx *ctx = fh; |
Sylwester Nawrocki | e004e02 | 2010-11-25 11:01:51 -0300 | [diff] [blame] | 718 | |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 719 | if (cr->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) |
Sylwester Nawrocki | e004e02 | 2010-11-25 11:01:51 -0300 | [diff] [blame] | 720 | return -EINVAL; |
| 721 | |
Sylwester Nawrocki | e004e02 | 2010-11-25 11:01:51 -0300 | [diff] [blame] | 722 | f = &ctx->s_frame; |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 723 | |
Sylwester Nawrocki | e004e02 | 2010-11-25 11:01:51 -0300 | [diff] [blame] | 724 | cr->bounds.left = 0; |
| 725 | cr->bounds.top = 0; |
| 726 | cr->bounds.width = f->o_width; |
| 727 | cr->bounds.height = f->o_height; |
| 728 | cr->defrect = cr->bounds; |
| 729 | |
Sylwester Nawrocki | e004e02 | 2010-11-25 11:01:51 -0300 | [diff] [blame] | 730 | return 0; |
| 731 | } |
| 732 | |
| 733 | static int fimc_cap_g_crop(struct file *file, void *fh, struct v4l2_crop *cr) |
| 734 | { |
| 735 | struct fimc_frame *f; |
| 736 | struct fimc_ctx *ctx = file->private_data; |
Sylwester Nawrocki | e004e02 | 2010-11-25 11:01:51 -0300 | [diff] [blame] | 737 | |
| 738 | f = &ctx->s_frame; |
Sylwester Nawrocki | 8293ebf | 2010-12-01 10:25:18 -0300 | [diff] [blame^] | 739 | |
Sylwester Nawrocki | e004e02 | 2010-11-25 11:01:51 -0300 | [diff] [blame] | 740 | cr->c.left = f->offs_h; |
| 741 | cr->c.top = f->offs_v; |
| 742 | cr->c.width = f->width; |
| 743 | cr->c.height = f->height; |
| 744 | |
Sylwester Nawrocki | e004e02 | 2010-11-25 11:01:51 -0300 | [diff] [blame] | 745 | return 0; |
| 746 | } |
| 747 | |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 748 | static int fimc_cap_s_crop(struct file *file, void *fh, |
| 749 | struct v4l2_crop *cr) |
| 750 | { |
| 751 | struct fimc_frame *f; |
| 752 | struct fimc_ctx *ctx = file->private_data; |
| 753 | struct fimc_dev *fimc = ctx->fimc_dev; |
| 754 | int ret = -EINVAL; |
| 755 | |
| 756 | if (fimc_capture_active(fimc)) |
| 757 | return -EBUSY; |
| 758 | |
| 759 | ret = fimc_try_crop(ctx, cr); |
| 760 | if (ret) |
| 761 | return ret; |
| 762 | |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 763 | if (!(ctx->state & FIMC_DST_FMT)) { |
| 764 | v4l2_err(&fimc->vid_cap.v4l2_dev, |
| 765 | "Capture color format not set\n"); |
Sylwester Nawrocki | 8293ebf | 2010-12-01 10:25:18 -0300 | [diff] [blame^] | 766 | return -EINVAL; /* TODO: make sure this is the right value */ |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | f = &ctx->s_frame; |
| 770 | /* Check for the pixel scaling ratio when cropping input image. */ |
| 771 | ret = fimc_check_scaler_ratio(&cr->c, &ctx->d_frame); |
| 772 | if (ret) { |
| 773 | v4l2_err(&fimc->vid_cap.v4l2_dev, "Out of the scaler range"); |
Sylwester Nawrocki | 8293ebf | 2010-12-01 10:25:18 -0300 | [diff] [blame^] | 774 | return ret; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 775 | } |
| 776 | |
Sylwester Nawrocki | 8293ebf | 2010-12-01 10:25:18 -0300 | [diff] [blame^] | 777 | f->offs_h = cr->c.left; |
| 778 | f->offs_v = cr->c.top; |
| 779 | f->width = cr->c.width; |
| 780 | f->height = cr->c.height; |
| 781 | |
| 782 | return 0; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 783 | } |
| 784 | |
| 785 | |
| 786 | static const struct v4l2_ioctl_ops fimc_capture_ioctl_ops = { |
| 787 | .vidioc_querycap = fimc_vidioc_querycap_capture, |
| 788 | |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 789 | .vidioc_enum_fmt_vid_cap_mplane = fimc_vidioc_enum_fmt_mplane, |
| 790 | .vidioc_try_fmt_vid_cap_mplane = fimc_vidioc_try_fmt_mplane, |
| 791 | .vidioc_s_fmt_vid_cap_mplane = fimc_cap_s_fmt_mplane, |
| 792 | .vidioc_g_fmt_vid_cap_mplane = fimc_vidioc_g_fmt_mplane, |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 793 | |
| 794 | .vidioc_reqbufs = fimc_cap_reqbufs, |
| 795 | .vidioc_querybuf = fimc_cap_querybuf, |
| 796 | |
| 797 | .vidioc_qbuf = fimc_cap_qbuf, |
| 798 | .vidioc_dqbuf = fimc_cap_dqbuf, |
| 799 | |
| 800 | .vidioc_streamon = fimc_cap_streamon, |
| 801 | .vidioc_streamoff = fimc_cap_streamoff, |
| 802 | |
| 803 | .vidioc_queryctrl = fimc_vidioc_queryctrl, |
| 804 | .vidioc_g_ctrl = fimc_vidioc_g_ctrl, |
| 805 | .vidioc_s_ctrl = fimc_cap_s_ctrl, |
| 806 | |
Sylwester Nawrocki | e004e02 | 2010-11-25 11:01:51 -0300 | [diff] [blame] | 807 | .vidioc_g_crop = fimc_cap_g_crop, |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 808 | .vidioc_s_crop = fimc_cap_s_crop, |
Sylwester Nawrocki | e004e02 | 2010-11-25 11:01:51 -0300 | [diff] [blame] | 809 | .vidioc_cropcap = fimc_cap_cropcap, |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 810 | |
| 811 | .vidioc_enum_input = fimc_cap_enum_input, |
| 812 | .vidioc_s_input = fimc_cap_s_input, |
| 813 | .vidioc_g_input = fimc_cap_g_input, |
| 814 | }; |
| 815 | |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 816 | /* fimc->lock must be already initialized */ |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 817 | int fimc_register_capture_device(struct fimc_dev *fimc) |
| 818 | { |
| 819 | struct v4l2_device *v4l2_dev = &fimc->vid_cap.v4l2_dev; |
| 820 | struct video_device *vfd; |
| 821 | struct fimc_vid_cap *vid_cap; |
| 822 | struct fimc_ctx *ctx; |
| 823 | struct v4l2_format f; |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 824 | struct fimc_frame *fr; |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 825 | struct vb2_queue *q; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 826 | int ret; |
| 827 | |
| 828 | ctx = kzalloc(sizeof *ctx, GFP_KERNEL); |
| 829 | if (!ctx) |
| 830 | return -ENOMEM; |
| 831 | |
| 832 | ctx->fimc_dev = fimc; |
| 833 | ctx->in_path = FIMC_CAMERA; |
| 834 | ctx->out_path = FIMC_DMA; |
| 835 | ctx->state = FIMC_CTX_CAP; |
| 836 | |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 837 | /* Default format of the output frames */ |
| 838 | f.fmt.pix.pixelformat = V4L2_PIX_FMT_RGB32; |
| 839 | fr = &ctx->d_frame; |
| 840 | fr->fmt = find_format(&f, FMT_FLAGS_M2M); |
| 841 | fr->width = fr->f_width = fr->o_width = 640; |
| 842 | fr->height = fr->f_height = fr->o_height = 480; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 843 | |
| 844 | if (!v4l2_dev->name[0]) |
| 845 | snprintf(v4l2_dev->name, sizeof(v4l2_dev->name), |
| 846 | "%s.capture", dev_name(&fimc->pdev->dev)); |
| 847 | |
| 848 | ret = v4l2_device_register(NULL, v4l2_dev); |
| 849 | if (ret) |
| 850 | goto err_info; |
| 851 | |
| 852 | vfd = video_device_alloc(); |
| 853 | if (!vfd) { |
| 854 | v4l2_err(v4l2_dev, "Failed to allocate video device\n"); |
| 855 | goto err_v4l2_reg; |
| 856 | } |
| 857 | |
| 858 | snprintf(vfd->name, sizeof(vfd->name), "%s:cap", |
| 859 | dev_name(&fimc->pdev->dev)); |
| 860 | |
| 861 | vfd->fops = &fimc_capture_fops; |
| 862 | vfd->ioctl_ops = &fimc_capture_ioctl_ops; |
| 863 | vfd->minor = -1; |
| 864 | vfd->release = video_device_release; |
Sylwester Nawrocki | 8293ebf | 2010-12-01 10:25:18 -0300 | [diff] [blame^] | 865 | vfd->lock = &fimc->lock; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 866 | video_set_drvdata(vfd, fimc); |
| 867 | |
| 868 | vid_cap = &fimc->vid_cap; |
| 869 | vid_cap->vfd = vfd; |
| 870 | vid_cap->active_buf_cnt = 0; |
| 871 | vid_cap->reqbufs_count = 0; |
| 872 | vid_cap->refcnt = 0; |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 873 | /* Default color format for image sensor */ |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 874 | vid_cap->fmt.code = V4L2_MBUS_FMT_YUYV8_2X8; |
| 875 | |
| 876 | INIT_LIST_HEAD(&vid_cap->pending_buf_q); |
| 877 | INIT_LIST_HEAD(&vid_cap->active_buf_q); |
| 878 | spin_lock_init(&ctx->slock); |
| 879 | vid_cap->ctx = ctx; |
| 880 | |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 881 | q = &fimc->vid_cap.vbq; |
| 882 | memset(q, 0, sizeof(*q)); |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 883 | q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 884 | q->io_modes = VB2_MMAP | VB2_USERPTR; |
| 885 | q->drv_priv = fimc->vid_cap.ctx; |
| 886 | q->ops = &fimc_capture_qops; |
| 887 | q->mem_ops = &vb2_dma_contig_memops; |
| 888 | q->buf_struct_size = sizeof(struct fimc_vid_buffer); |
| 889 | |
| 890 | vb2_queue_init(q); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 891 | |
| 892 | ret = video_register_device(vfd, VFL_TYPE_GRABBER, -1); |
| 893 | if (ret) { |
| 894 | v4l2_err(v4l2_dev, "Failed to register video device\n"); |
| 895 | goto err_vd_reg; |
| 896 | } |
| 897 | |
| 898 | v4l2_info(v4l2_dev, |
| 899 | "FIMC capture driver registered as /dev/video%d\n", |
| 900 | vfd->num); |
| 901 | |
| 902 | return 0; |
| 903 | |
| 904 | err_vd_reg: |
| 905 | video_device_release(vfd); |
| 906 | err_v4l2_reg: |
| 907 | v4l2_device_unregister(v4l2_dev); |
| 908 | err_info: |
| 909 | dev_err(&fimc->pdev->dev, "failed to install\n"); |
| 910 | return ret; |
| 911 | } |
| 912 | |
| 913 | void fimc_unregister_capture_device(struct fimc_dev *fimc) |
| 914 | { |
| 915 | struct fimc_vid_cap *capture = &fimc->vid_cap; |
| 916 | |
| 917 | if (capture->vfd) |
| 918 | video_unregister_device(capture->vfd); |
| 919 | |
| 920 | kfree(capture->ctx); |
| 921 | } |