Sylwester Nawrocki | babde1c | 2012-08-22 20:09:18 -0300 | [diff] [blame] | 1 | /* |
| 2 | * s3c24xx/s3c64xx SoC series Camera Interface (CAMIF) driver |
| 3 | * |
| 4 | * Copyright (C) 2012 Sylwester Nawrocki <sylvester.nawrocki@gmail.com> |
| 5 | * Copyright (C) 2012 Tomasz Figa <tomasz.figa@gmail.com> |
| 6 | * |
| 7 | * Based on drivers/media/platform/s5p-fimc, |
| 8 | * Copyright (C) 2010 - 2012 Samsung Electronics Co., Ltd. |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License version 2 as |
| 12 | * published by the Free Software Foundation. |
| 13 | */ |
| 14 | #define pr_fmt(fmt) "%s:%d " fmt, __func__, __LINE__ |
| 15 | |
| 16 | #include <linux/bug.h> |
| 17 | #include <linux/clk.h> |
| 18 | #include <linux/device.h> |
| 19 | #include <linux/errno.h> |
| 20 | #include <linux/i2c.h> |
| 21 | #include <linux/interrupt.h> |
| 22 | #include <linux/io.h> |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/list.h> |
| 25 | #include <linux/module.h> |
| 26 | #include <linux/platform_device.h> |
| 27 | #include <linux/pm_runtime.h> |
| 28 | #include <linux/ratelimit.h> |
| 29 | #include <linux/slab.h> |
| 30 | #include <linux/types.h> |
| 31 | #include <linux/videodev2.h> |
| 32 | |
| 33 | #include <media/media-device.h> |
| 34 | #include <media/v4l2-ctrls.h> |
| 35 | #include <media/v4l2-event.h> |
| 36 | #include <media/v4l2-ioctl.h> |
| 37 | #include <media/videobuf2-core.h> |
| 38 | #include <media/videobuf2-dma-contig.h> |
| 39 | |
| 40 | #include "camif-core.h" |
| 41 | #include "camif-regs.h" |
| 42 | |
| 43 | static int debug; |
| 44 | module_param(debug, int, 0644); |
| 45 | |
| 46 | /* Locking: called with vp->camif->slock spinlock held */ |
| 47 | static void camif_cfg_video_path(struct camif_vp *vp) |
| 48 | { |
| 49 | WARN_ON(s3c_camif_get_scaler_config(vp, &vp->scaler)); |
| 50 | camif_hw_set_scaler(vp); |
| 51 | camif_hw_set_flip(vp); |
| 52 | camif_hw_set_target_format(vp); |
| 53 | camif_hw_set_output_dma(vp); |
| 54 | } |
| 55 | |
| 56 | static void camif_prepare_dma_offset(struct camif_vp *vp) |
| 57 | { |
| 58 | struct camif_frame *f = &vp->out_frame; |
| 59 | |
| 60 | f->dma_offset.initial = f->rect.top * f->f_width + f->rect.left; |
| 61 | f->dma_offset.line = f->f_width - (f->rect.left + f->rect.width); |
| 62 | |
| 63 | pr_debug("dma_offset: initial: %d, line: %d\n", |
| 64 | f->dma_offset.initial, f->dma_offset.line); |
| 65 | } |
| 66 | |
| 67 | /* Locking: called with camif->slock spinlock held */ |
| 68 | static int s3c_camif_hw_init(struct camif_dev *camif, struct camif_vp *vp) |
| 69 | { |
| 70 | const struct s3c_camif_variant *variant = camif->variant; |
| 71 | |
| 72 | if (camif->sensor.sd == NULL || vp->out_fmt == NULL) |
| 73 | return -EINVAL; |
| 74 | |
| 75 | if (variant->ip_revision == S3C244X_CAMIF_IP_REV) |
| 76 | camif_hw_clear_fifo_overflow(vp); |
| 77 | camif_hw_set_camera_bus(camif); |
| 78 | camif_hw_set_source_format(camif); |
| 79 | camif_hw_set_camera_crop(camif); |
| 80 | camif_hw_set_test_pattern(camif, camif->test_pattern); |
| 81 | if (variant->has_img_effect) |
| 82 | camif_hw_set_effect(camif, camif->colorfx, |
| 83 | camif->colorfx_cb, camif->colorfx_cr); |
| 84 | if (variant->ip_revision == S3C6410_CAMIF_IP_REV) |
| 85 | camif_hw_set_input_path(vp); |
| 86 | camif_cfg_video_path(vp); |
| 87 | vp->state &= ~ST_VP_CONFIG; |
| 88 | |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | /* |
| 93 | * Initialize the video path, only up from the scaler stage. The camera |
| 94 | * input interface set up is skipped. This is useful to enable one of the |
| 95 | * video paths when the other is already running. |
| 96 | * Locking: called with camif->slock spinlock held. |
| 97 | */ |
| 98 | static int s3c_camif_hw_vp_init(struct camif_dev *camif, struct camif_vp *vp) |
| 99 | { |
| 100 | unsigned int ip_rev = camif->variant->ip_revision; |
| 101 | |
| 102 | if (vp->out_fmt == NULL) |
| 103 | return -EINVAL; |
| 104 | |
| 105 | camif_prepare_dma_offset(vp); |
| 106 | if (ip_rev == S3C244X_CAMIF_IP_REV) |
| 107 | camif_hw_clear_fifo_overflow(vp); |
| 108 | camif_cfg_video_path(vp); |
| 109 | vp->state &= ~ST_VP_CONFIG; |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | static int sensor_set_power(struct camif_dev *camif, int on) |
| 114 | { |
| 115 | struct cam_sensor *sensor = &camif->sensor; |
| 116 | int err = 0; |
| 117 | |
| 118 | if (!on == camif->sensor.power_count) |
| 119 | err = v4l2_subdev_call(sensor->sd, core, s_power, on); |
| 120 | if (!err) |
| 121 | sensor->power_count += on ? 1 : -1; |
| 122 | |
| 123 | pr_debug("on: %d, power_count: %d, err: %d\n", |
| 124 | on, sensor->power_count, err); |
| 125 | |
| 126 | return err; |
| 127 | } |
| 128 | |
| 129 | static int sensor_set_streaming(struct camif_dev *camif, int on) |
| 130 | { |
| 131 | struct cam_sensor *sensor = &camif->sensor; |
| 132 | int err = 0; |
| 133 | |
| 134 | if (!on == camif->sensor.stream_count) |
| 135 | err = v4l2_subdev_call(sensor->sd, video, s_stream, on); |
| 136 | if (!err) |
| 137 | sensor->stream_count += on ? 1 : -1; |
| 138 | |
| 139 | pr_debug("on: %d, stream_count: %d, err: %d\n", |
| 140 | on, sensor->stream_count, err); |
| 141 | |
| 142 | return err; |
| 143 | } |
| 144 | |
| 145 | /* |
| 146 | * Reinitialize the driver so it is ready to start streaming again. |
| 147 | * Return any buffers to vb2, perform CAMIF software reset and |
| 148 | * turn off streaming at the data pipeline (sensor) if required. |
| 149 | */ |
| 150 | static int camif_reinitialize(struct camif_vp *vp) |
| 151 | { |
| 152 | struct camif_dev *camif = vp->camif; |
| 153 | struct camif_buffer *buf; |
| 154 | unsigned long flags; |
| 155 | bool streaming; |
| 156 | |
| 157 | spin_lock_irqsave(&camif->slock, flags); |
| 158 | streaming = vp->state & ST_VP_SENSOR_STREAMING; |
| 159 | |
| 160 | vp->state &= ~(ST_VP_PENDING | ST_VP_RUNNING | ST_VP_OFF | |
| 161 | ST_VP_ABORTING | ST_VP_STREAMING | |
| 162 | ST_VP_SENSOR_STREAMING | ST_VP_LASTIRQ); |
| 163 | |
| 164 | /* Release unused buffers */ |
| 165 | while (!list_empty(&vp->pending_buf_q)) { |
| 166 | buf = camif_pending_queue_pop(vp); |
| 167 | vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR); |
| 168 | } |
| 169 | |
| 170 | while (!list_empty(&vp->active_buf_q)) { |
| 171 | buf = camif_active_queue_pop(vp); |
| 172 | vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR); |
| 173 | } |
| 174 | |
| 175 | spin_unlock_irqrestore(&camif->slock, flags); |
| 176 | |
| 177 | if (!streaming) |
| 178 | return 0; |
| 179 | |
| 180 | return sensor_set_streaming(camif, 0); |
| 181 | } |
| 182 | |
| 183 | static bool s3c_vp_active(struct camif_vp *vp) |
| 184 | { |
| 185 | struct camif_dev *camif = vp->camif; |
| 186 | unsigned long flags; |
| 187 | bool ret; |
| 188 | |
| 189 | spin_lock_irqsave(&camif->slock, flags); |
| 190 | ret = (vp->state & ST_VP_RUNNING) || (vp->state & ST_VP_PENDING); |
| 191 | spin_unlock_irqrestore(&camif->slock, flags); |
| 192 | |
| 193 | return ret; |
| 194 | } |
| 195 | |
| 196 | static bool camif_is_streaming(struct camif_dev *camif) |
| 197 | { |
| 198 | unsigned long flags; |
| 199 | bool status; |
| 200 | |
| 201 | spin_lock_irqsave(&camif->slock, flags); |
| 202 | status = camif->stream_count > 0; |
| 203 | spin_unlock_irqrestore(&camif->slock, flags); |
| 204 | |
| 205 | return status; |
| 206 | } |
| 207 | |
| 208 | static int camif_stop_capture(struct camif_vp *vp) |
| 209 | { |
| 210 | struct camif_dev *camif = vp->camif; |
| 211 | unsigned long flags; |
| 212 | int ret; |
| 213 | |
| 214 | if (!s3c_vp_active(vp)) |
| 215 | return 0; |
| 216 | |
| 217 | spin_lock_irqsave(&camif->slock, flags); |
| 218 | vp->state &= ~(ST_VP_OFF | ST_VP_LASTIRQ); |
| 219 | vp->state |= ST_VP_ABORTING; |
| 220 | spin_unlock_irqrestore(&camif->slock, flags); |
| 221 | |
| 222 | ret = wait_event_timeout(vp->irq_queue, |
| 223 | !(vp->state & ST_VP_ABORTING), |
| 224 | msecs_to_jiffies(CAMIF_STOP_TIMEOUT)); |
| 225 | |
| 226 | spin_lock_irqsave(&camif->slock, flags); |
| 227 | |
| 228 | if (ret == 0 && !(vp->state & ST_VP_OFF)) { |
| 229 | /* Timed out, forcibly stop capture */ |
| 230 | vp->state &= ~(ST_VP_OFF | ST_VP_ABORTING | |
| 231 | ST_VP_LASTIRQ); |
| 232 | |
| 233 | camif_hw_disable_capture(vp); |
| 234 | camif_hw_enable_scaler(vp, false); |
| 235 | } |
| 236 | |
| 237 | spin_unlock_irqrestore(&camif->slock, flags); |
| 238 | |
| 239 | return camif_reinitialize(vp); |
| 240 | } |
| 241 | |
| 242 | static int camif_prepare_addr(struct camif_vp *vp, struct vb2_buffer *vb, |
| 243 | struct camif_addr *paddr) |
| 244 | { |
| 245 | struct camif_frame *frame = &vp->out_frame; |
| 246 | u32 pix_size; |
| 247 | |
| 248 | if (vb == NULL || frame == NULL) |
| 249 | return -EINVAL; |
| 250 | |
| 251 | pix_size = frame->rect.width * frame->rect.height; |
| 252 | |
| 253 | pr_debug("colplanes: %d, pix_size: %u\n", |
| 254 | vp->out_fmt->colplanes, pix_size); |
| 255 | |
| 256 | paddr->y = vb2_dma_contig_plane_dma_addr(vb, 0); |
| 257 | |
| 258 | switch (vp->out_fmt->colplanes) { |
| 259 | case 1: |
| 260 | paddr->cb = 0; |
| 261 | paddr->cr = 0; |
| 262 | break; |
| 263 | case 2: |
| 264 | /* decompose Y into Y/Cb */ |
| 265 | paddr->cb = (u32)(paddr->y + pix_size); |
| 266 | paddr->cr = 0; |
| 267 | break; |
| 268 | case 3: |
| 269 | paddr->cb = (u32)(paddr->y + pix_size); |
| 270 | /* decompose Y into Y/Cb/Cr */ |
| 271 | if (vp->out_fmt->color == IMG_FMT_YCBCR422P) |
| 272 | paddr->cr = (u32)(paddr->cb + (pix_size >> 1)); |
| 273 | else /* 420 */ |
| 274 | paddr->cr = (u32)(paddr->cb + (pix_size >> 2)); |
| 275 | |
| 276 | if (vp->out_fmt->color == IMG_FMT_YCRCB420) |
| 277 | swap(paddr->cb, paddr->cr); |
| 278 | break; |
| 279 | default: |
| 280 | return -EINVAL; |
| 281 | } |
| 282 | |
Mauro Carvalho Chehab | c0a566f | 2014-09-24 19:15:06 -0300 | [diff] [blame] | 283 | pr_debug("DMA address: y: %pad cb: %pad cr: %pad\n", |
| 284 | &paddr->y, &paddr->cb, &paddr->cr); |
Sylwester Nawrocki | babde1c | 2012-08-22 20:09:18 -0300 | [diff] [blame] | 285 | |
| 286 | return 0; |
| 287 | } |
| 288 | |
| 289 | irqreturn_t s3c_camif_irq_handler(int irq, void *priv) |
| 290 | { |
| 291 | struct camif_vp *vp = priv; |
| 292 | struct camif_dev *camif = vp->camif; |
| 293 | unsigned int ip_rev = camif->variant->ip_revision; |
| 294 | unsigned int status; |
| 295 | |
| 296 | spin_lock(&camif->slock); |
| 297 | |
| 298 | if (ip_rev == S3C6410_CAMIF_IP_REV) |
| 299 | camif_hw_clear_pending_irq(vp); |
| 300 | |
| 301 | status = camif_hw_get_status(vp); |
| 302 | |
| 303 | if (ip_rev == S3C244X_CAMIF_IP_REV && (status & CISTATUS_OVF_MASK)) { |
| 304 | camif_hw_clear_fifo_overflow(vp); |
| 305 | goto unlock; |
| 306 | } |
| 307 | |
| 308 | if (vp->state & ST_VP_ABORTING) { |
| 309 | if (vp->state & ST_VP_OFF) { |
| 310 | /* Last IRQ */ |
| 311 | vp->state &= ~(ST_VP_OFF | ST_VP_ABORTING | |
| 312 | ST_VP_LASTIRQ); |
| 313 | wake_up(&vp->irq_queue); |
| 314 | goto unlock; |
| 315 | } else if (vp->state & ST_VP_LASTIRQ) { |
| 316 | camif_hw_disable_capture(vp); |
| 317 | camif_hw_enable_scaler(vp, false); |
| 318 | camif_hw_set_lastirq(vp, false); |
| 319 | vp->state |= ST_VP_OFF; |
| 320 | } else { |
| 321 | /* Disable capture, enable last IRQ */ |
| 322 | camif_hw_set_lastirq(vp, true); |
| 323 | vp->state |= ST_VP_LASTIRQ; |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | if (!list_empty(&vp->pending_buf_q) && (vp->state & ST_VP_RUNNING) && |
| 328 | !list_empty(&vp->active_buf_q)) { |
| 329 | unsigned int index; |
| 330 | struct camif_buffer *vbuf; |
| 331 | struct timeval *tv; |
| 332 | struct timespec ts; |
| 333 | /* |
| 334 | * Get previous DMA write buffer index: |
| 335 | * 0 => DMA buffer 0, 2; |
| 336 | * 1 => DMA buffer 1, 3. |
| 337 | */ |
| 338 | index = (CISTATUS_FRAMECNT(status) + 2) & 1; |
| 339 | |
| 340 | ktime_get_ts(&ts); |
| 341 | vbuf = camif_active_queue_peek(vp, index); |
| 342 | |
| 343 | if (!WARN_ON(vbuf == NULL)) { |
| 344 | /* Dequeue a filled buffer */ |
| 345 | tv = &vbuf->vb.v4l2_buf.timestamp; |
| 346 | tv->tv_sec = ts.tv_sec; |
| 347 | tv->tv_usec = ts.tv_nsec / NSEC_PER_USEC; |
| 348 | vbuf->vb.v4l2_buf.sequence = vp->frame_sequence++; |
| 349 | vb2_buffer_done(&vbuf->vb, VB2_BUF_STATE_DONE); |
| 350 | |
| 351 | /* Set up an empty buffer at the DMA engine */ |
| 352 | vbuf = camif_pending_queue_pop(vp); |
| 353 | vbuf->index = index; |
| 354 | camif_hw_set_output_addr(vp, &vbuf->paddr, index); |
| 355 | camif_hw_set_output_addr(vp, &vbuf->paddr, index + 2); |
| 356 | |
| 357 | /* Scheduled in H/W, add to the queue */ |
| 358 | camif_active_queue_add(vp, vbuf); |
| 359 | } |
| 360 | } else if (!(vp->state & ST_VP_ABORTING) && |
| 361 | (vp->state & ST_VP_PENDING)) { |
| 362 | vp->state |= ST_VP_RUNNING; |
| 363 | } |
| 364 | |
| 365 | if (vp->state & ST_VP_CONFIG) { |
| 366 | camif_prepare_dma_offset(vp); |
| 367 | camif_hw_set_camera_crop(camif); |
| 368 | camif_hw_set_scaler(vp); |
| 369 | camif_hw_set_flip(vp); |
| 370 | camif_hw_set_test_pattern(camif, camif->test_pattern); |
| 371 | if (camif->variant->has_img_effect) |
| 372 | camif_hw_set_effect(camif, camif->colorfx, |
| 373 | camif->colorfx_cb, camif->colorfx_cr); |
| 374 | vp->state &= ~ST_VP_CONFIG; |
| 375 | } |
| 376 | unlock: |
| 377 | spin_unlock(&camif->slock); |
| 378 | return IRQ_HANDLED; |
| 379 | } |
| 380 | |
| 381 | static int start_streaming(struct vb2_queue *vq, unsigned int count) |
| 382 | { |
| 383 | struct camif_vp *vp = vb2_get_drv_priv(vq); |
| 384 | struct camif_dev *camif = vp->camif; |
| 385 | unsigned long flags; |
| 386 | int ret; |
| 387 | |
| 388 | /* |
| 389 | * We assume the codec capture path is always activated |
| 390 | * first, before the preview path starts streaming. |
| 391 | * This is required to avoid internal FIFO overflow and |
| 392 | * a need for CAMIF software reset. |
| 393 | */ |
| 394 | spin_lock_irqsave(&camif->slock, flags); |
| 395 | |
| 396 | if (camif->stream_count == 0) { |
| 397 | camif_hw_reset(camif); |
| 398 | ret = s3c_camif_hw_init(camif, vp); |
| 399 | } else { |
| 400 | ret = s3c_camif_hw_vp_init(camif, vp); |
| 401 | } |
| 402 | spin_unlock_irqrestore(&camif->slock, flags); |
| 403 | |
| 404 | if (ret < 0) { |
| 405 | camif_reinitialize(vp); |
| 406 | return ret; |
| 407 | } |
| 408 | |
| 409 | spin_lock_irqsave(&camif->slock, flags); |
| 410 | vp->frame_sequence = 0; |
| 411 | vp->state |= ST_VP_PENDING; |
| 412 | |
| 413 | if (!list_empty(&vp->pending_buf_q) && |
| 414 | (!(vp->state & ST_VP_STREAMING) || |
| 415 | !(vp->state & ST_VP_SENSOR_STREAMING))) { |
| 416 | |
| 417 | camif_hw_enable_scaler(vp, vp->scaler.enable); |
| 418 | camif_hw_enable_capture(vp); |
| 419 | vp->state |= ST_VP_STREAMING; |
| 420 | |
| 421 | if (!(vp->state & ST_VP_SENSOR_STREAMING)) { |
| 422 | vp->state |= ST_VP_SENSOR_STREAMING; |
| 423 | spin_unlock_irqrestore(&camif->slock, flags); |
| 424 | ret = sensor_set_streaming(camif, 1); |
| 425 | if (ret) |
| 426 | v4l2_err(&vp->vdev, "Sensor s_stream failed\n"); |
| 427 | if (debug) |
| 428 | camif_hw_dump_regs(camif, __func__); |
| 429 | |
| 430 | return ret; |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | spin_unlock_irqrestore(&camif->slock, flags); |
| 435 | return 0; |
| 436 | } |
| 437 | |
Hans Verkuil | e37559b | 2014-04-17 02:47:21 -0300 | [diff] [blame] | 438 | static void stop_streaming(struct vb2_queue *vq) |
Sylwester Nawrocki | babde1c | 2012-08-22 20:09:18 -0300 | [diff] [blame] | 439 | { |
| 440 | struct camif_vp *vp = vb2_get_drv_priv(vq); |
Hans Verkuil | e37559b | 2014-04-17 02:47:21 -0300 | [diff] [blame] | 441 | camif_stop_capture(vp); |
Sylwester Nawrocki | babde1c | 2012-08-22 20:09:18 -0300 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *pfmt, |
| 445 | unsigned int *num_buffers, unsigned int *num_planes, |
| 446 | unsigned int sizes[], void *allocators[]) |
| 447 | { |
| 448 | const struct v4l2_pix_format *pix = NULL; |
| 449 | struct camif_vp *vp = vb2_get_drv_priv(vq); |
| 450 | struct camif_dev *camif = vp->camif; |
| 451 | struct camif_frame *frame = &vp->out_frame; |
| 452 | const struct camif_fmt *fmt = vp->out_fmt; |
| 453 | unsigned int size; |
| 454 | |
| 455 | if (pfmt) { |
| 456 | pix = &pfmt->fmt.pix; |
| 457 | fmt = s3c_camif_find_format(vp, &pix->pixelformat, -1); |
| 458 | size = (pix->width * pix->height * fmt->depth) / 8; |
| 459 | } else { |
| 460 | size = (frame->f_width * frame->f_height * fmt->depth) / 8; |
| 461 | } |
| 462 | |
| 463 | if (fmt == NULL) |
| 464 | return -EINVAL; |
| 465 | *num_planes = 1; |
| 466 | |
| 467 | if (pix) |
| 468 | sizes[0] = max(size, pix->sizeimage); |
| 469 | else |
| 470 | sizes[0] = size; |
| 471 | allocators[0] = camif->alloc_ctx; |
| 472 | |
| 473 | pr_debug("size: %u\n", sizes[0]); |
| 474 | return 0; |
| 475 | } |
| 476 | |
| 477 | static int buffer_prepare(struct vb2_buffer *vb) |
| 478 | { |
| 479 | struct camif_vp *vp = vb2_get_drv_priv(vb->vb2_queue); |
| 480 | |
| 481 | if (vp->out_fmt == NULL) |
| 482 | return -EINVAL; |
| 483 | |
| 484 | if (vb2_plane_size(vb, 0) < vp->payload) { |
| 485 | v4l2_err(&vp->vdev, "buffer too small: %lu, required: %u\n", |
| 486 | vb2_plane_size(vb, 0), vp->payload); |
| 487 | return -EINVAL; |
| 488 | } |
| 489 | vb2_set_plane_payload(vb, 0, vp->payload); |
| 490 | |
| 491 | return 0; |
| 492 | } |
| 493 | |
| 494 | static void buffer_queue(struct vb2_buffer *vb) |
| 495 | { |
| 496 | struct camif_buffer *buf = container_of(vb, struct camif_buffer, vb); |
| 497 | struct camif_vp *vp = vb2_get_drv_priv(vb->vb2_queue); |
| 498 | struct camif_dev *camif = vp->camif; |
| 499 | unsigned long flags; |
| 500 | |
| 501 | spin_lock_irqsave(&camif->slock, flags); |
| 502 | WARN_ON(camif_prepare_addr(vp, &buf->vb, &buf->paddr)); |
| 503 | |
| 504 | if (!(vp->state & ST_VP_STREAMING) && vp->active_buffers < 2) { |
| 505 | /* Schedule an empty buffer in H/W */ |
| 506 | buf->index = vp->buf_index; |
| 507 | |
| 508 | camif_hw_set_output_addr(vp, &buf->paddr, buf->index); |
| 509 | camif_hw_set_output_addr(vp, &buf->paddr, buf->index + 2); |
| 510 | |
| 511 | camif_active_queue_add(vp, buf); |
| 512 | vp->buf_index = !vp->buf_index; |
| 513 | } else { |
| 514 | camif_pending_queue_add(vp, buf); |
| 515 | } |
| 516 | |
| 517 | if (vb2_is_streaming(&vp->vb_queue) && !list_empty(&vp->pending_buf_q) |
| 518 | && !(vp->state & ST_VP_STREAMING)) { |
| 519 | |
| 520 | vp->state |= ST_VP_STREAMING; |
| 521 | camif_hw_enable_scaler(vp, vp->scaler.enable); |
| 522 | camif_hw_enable_capture(vp); |
| 523 | spin_unlock_irqrestore(&camif->slock, flags); |
| 524 | |
| 525 | if (!(vp->state & ST_VP_SENSOR_STREAMING)) { |
| 526 | if (sensor_set_streaming(camif, 1) == 0) |
| 527 | vp->state |= ST_VP_SENSOR_STREAMING; |
| 528 | else |
| 529 | v4l2_err(&vp->vdev, "Sensor s_stream failed\n"); |
| 530 | |
| 531 | if (debug) |
| 532 | camif_hw_dump_regs(camif, __func__); |
| 533 | } |
| 534 | return; |
| 535 | } |
| 536 | spin_unlock_irqrestore(&camif->slock, flags); |
| 537 | } |
| 538 | |
| 539 | static void camif_lock(struct vb2_queue *vq) |
| 540 | { |
| 541 | struct camif_vp *vp = vb2_get_drv_priv(vq); |
| 542 | mutex_lock(&vp->camif->lock); |
| 543 | } |
| 544 | |
| 545 | static void camif_unlock(struct vb2_queue *vq) |
| 546 | { |
| 547 | struct camif_vp *vp = vb2_get_drv_priv(vq); |
| 548 | mutex_unlock(&vp->camif->lock); |
| 549 | } |
| 550 | |
| 551 | static const struct vb2_ops s3c_camif_qops = { |
| 552 | .queue_setup = queue_setup, |
| 553 | .buf_prepare = buffer_prepare, |
| 554 | .buf_queue = buffer_queue, |
| 555 | .wait_prepare = camif_unlock, |
| 556 | .wait_finish = camif_lock, |
| 557 | .start_streaming = start_streaming, |
| 558 | .stop_streaming = stop_streaming, |
| 559 | }; |
| 560 | |
| 561 | static int s3c_camif_open(struct file *file) |
| 562 | { |
| 563 | struct camif_vp *vp = video_drvdata(file); |
| 564 | struct camif_dev *camif = vp->camif; |
| 565 | int ret; |
| 566 | |
| 567 | pr_debug("[vp%d] state: %#x, owner: %p, pid: %d\n", vp->id, |
| 568 | vp->state, vp->owner, task_pid_nr(current)); |
| 569 | |
| 570 | if (mutex_lock_interruptible(&camif->lock)) |
| 571 | return -ERESTARTSYS; |
| 572 | |
| 573 | ret = v4l2_fh_open(file); |
| 574 | if (ret < 0) |
| 575 | goto unlock; |
| 576 | |
| 577 | ret = pm_runtime_get_sync(camif->dev); |
| 578 | if (ret < 0) |
| 579 | goto err_pm; |
| 580 | |
| 581 | ret = sensor_set_power(camif, 1); |
| 582 | if (!ret) |
| 583 | goto unlock; |
| 584 | |
| 585 | pm_runtime_put(camif->dev); |
| 586 | err_pm: |
| 587 | v4l2_fh_release(file); |
| 588 | unlock: |
| 589 | mutex_unlock(&camif->lock); |
| 590 | return ret; |
| 591 | } |
| 592 | |
| 593 | static int s3c_camif_close(struct file *file) |
| 594 | { |
| 595 | struct camif_vp *vp = video_drvdata(file); |
| 596 | struct camif_dev *camif = vp->camif; |
| 597 | int ret; |
| 598 | |
| 599 | pr_debug("[vp%d] state: %#x, owner: %p, pid: %d\n", vp->id, |
| 600 | vp->state, vp->owner, task_pid_nr(current)); |
| 601 | |
| 602 | mutex_lock(&camif->lock); |
| 603 | |
| 604 | if (vp->owner == file->private_data) { |
| 605 | camif_stop_capture(vp); |
| 606 | vb2_queue_release(&vp->vb_queue); |
| 607 | vp->owner = NULL; |
| 608 | } |
| 609 | |
| 610 | sensor_set_power(camif, 0); |
| 611 | |
| 612 | pm_runtime_put(camif->dev); |
| 613 | ret = v4l2_fh_release(file); |
| 614 | |
| 615 | mutex_unlock(&camif->lock); |
| 616 | return ret; |
| 617 | } |
| 618 | |
| 619 | static unsigned int s3c_camif_poll(struct file *file, |
| 620 | struct poll_table_struct *wait) |
| 621 | { |
| 622 | struct camif_vp *vp = video_drvdata(file); |
| 623 | struct camif_dev *camif = vp->camif; |
| 624 | int ret; |
| 625 | |
| 626 | mutex_lock(&camif->lock); |
| 627 | if (vp->owner && vp->owner != file->private_data) |
| 628 | ret = -EBUSY; |
| 629 | else |
| 630 | ret = vb2_poll(&vp->vb_queue, file, wait); |
| 631 | |
| 632 | mutex_unlock(&camif->lock); |
| 633 | return ret; |
| 634 | } |
| 635 | |
| 636 | static int s3c_camif_mmap(struct file *file, struct vm_area_struct *vma) |
| 637 | { |
| 638 | struct camif_vp *vp = video_drvdata(file); |
| 639 | int ret; |
| 640 | |
| 641 | if (vp->owner && vp->owner != file->private_data) |
| 642 | ret = -EBUSY; |
| 643 | else |
| 644 | ret = vb2_mmap(&vp->vb_queue, vma); |
| 645 | |
| 646 | return ret; |
| 647 | } |
| 648 | |
| 649 | static const struct v4l2_file_operations s3c_camif_fops = { |
| 650 | .owner = THIS_MODULE, |
| 651 | .open = s3c_camif_open, |
| 652 | .release = s3c_camif_close, |
| 653 | .poll = s3c_camif_poll, |
| 654 | .unlocked_ioctl = video_ioctl2, |
| 655 | .mmap = s3c_camif_mmap, |
| 656 | }; |
| 657 | |
| 658 | /* |
| 659 | * Video node IOCTLs |
| 660 | */ |
| 661 | |
| 662 | static int s3c_camif_vidioc_querycap(struct file *file, void *priv, |
| 663 | struct v4l2_capability *cap) |
| 664 | { |
| 665 | struct camif_vp *vp = video_drvdata(file); |
| 666 | |
| 667 | strlcpy(cap->driver, S3C_CAMIF_DRIVER_NAME, sizeof(cap->driver)); |
| 668 | strlcpy(cap->card, S3C_CAMIF_DRIVER_NAME, sizeof(cap->card)); |
| 669 | snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s.%d", |
| 670 | dev_name(vp->camif->dev), vp->id); |
| 671 | |
| 672 | cap->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE; |
| 673 | cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS; |
| 674 | |
| 675 | return 0; |
| 676 | } |
| 677 | |
| 678 | static int s3c_camif_vidioc_enum_input(struct file *file, void *priv, |
| 679 | struct v4l2_input *input) |
| 680 | { |
| 681 | struct camif_vp *vp = video_drvdata(file); |
| 682 | struct v4l2_subdev *sensor = vp->camif->sensor.sd; |
| 683 | |
| 684 | if (input->index || sensor == NULL) |
| 685 | return -EINVAL; |
| 686 | |
| 687 | input->type = V4L2_INPUT_TYPE_CAMERA; |
| 688 | strlcpy(input->name, sensor->name, sizeof(input->name)); |
| 689 | return 0; |
| 690 | } |
| 691 | |
| 692 | static int s3c_camif_vidioc_s_input(struct file *file, void *priv, |
| 693 | unsigned int i) |
| 694 | { |
| 695 | return i == 0 ? 0 : -EINVAL; |
| 696 | } |
| 697 | |
| 698 | static int s3c_camif_vidioc_g_input(struct file *file, void *priv, |
| 699 | unsigned int *i) |
| 700 | { |
| 701 | *i = 0; |
| 702 | return 0; |
| 703 | } |
| 704 | |
| 705 | static int s3c_camif_vidioc_enum_fmt(struct file *file, void *priv, |
| 706 | struct v4l2_fmtdesc *f) |
| 707 | { |
| 708 | struct camif_vp *vp = video_drvdata(file); |
| 709 | const struct camif_fmt *fmt; |
| 710 | |
| 711 | fmt = s3c_camif_find_format(vp, NULL, f->index); |
| 712 | if (!fmt) |
| 713 | return -EINVAL; |
| 714 | |
| 715 | strlcpy(f->description, fmt->name, sizeof(f->description)); |
| 716 | f->pixelformat = fmt->fourcc; |
| 717 | |
| 718 | pr_debug("fmt(%d): %s\n", f->index, f->description); |
| 719 | return 0; |
| 720 | } |
| 721 | |
| 722 | static int s3c_camif_vidioc_g_fmt(struct file *file, void *priv, |
| 723 | struct v4l2_format *f) |
| 724 | { |
| 725 | struct camif_vp *vp = video_drvdata(file); |
| 726 | struct v4l2_pix_format *pix = &f->fmt.pix; |
| 727 | struct camif_frame *frame = &vp->out_frame; |
| 728 | const struct camif_fmt *fmt = vp->out_fmt; |
| 729 | |
| 730 | pix->bytesperline = frame->f_width * fmt->ybpp; |
| 731 | pix->sizeimage = vp->payload; |
| 732 | |
| 733 | pix->pixelformat = fmt->fourcc; |
| 734 | pix->width = frame->f_width; |
| 735 | pix->height = frame->f_height; |
| 736 | pix->field = V4L2_FIELD_NONE; |
| 737 | pix->colorspace = V4L2_COLORSPACE_JPEG; |
| 738 | |
| 739 | return 0; |
| 740 | } |
| 741 | |
| 742 | static int __camif_video_try_format(struct camif_vp *vp, |
| 743 | struct v4l2_pix_format *pix, |
| 744 | const struct camif_fmt **ffmt) |
| 745 | { |
| 746 | struct camif_dev *camif = vp->camif; |
| 747 | struct v4l2_rect *crop = &camif->camif_crop; |
| 748 | unsigned int wmin, hmin, sc_hrmax, sc_vrmax; |
| 749 | const struct vp_pix_limits *pix_lim; |
| 750 | const struct camif_fmt *fmt; |
| 751 | |
| 752 | fmt = s3c_camif_find_format(vp, &pix->pixelformat, 0); |
| 753 | |
| 754 | if (WARN_ON(fmt == NULL)) |
| 755 | return -EINVAL; |
| 756 | |
| 757 | if (ffmt) |
| 758 | *ffmt = fmt; |
| 759 | |
| 760 | pix_lim = &camif->variant->vp_pix_limits[vp->id]; |
| 761 | |
| 762 | pr_debug("fmt: %ux%u, crop: %ux%u, bytesperline: %u\n", |
| 763 | pix->width, pix->height, crop->width, crop->height, |
| 764 | pix->bytesperline); |
| 765 | /* |
| 766 | * Calculate minimum width and height according to the configured |
| 767 | * camera input interface crop rectangle and the resizer's capabilities. |
| 768 | */ |
| 769 | sc_hrmax = min(SCALER_MAX_RATIO, 1 << (ffs(crop->width) - 3)); |
| 770 | sc_vrmax = min(SCALER_MAX_RATIO, 1 << (ffs(crop->height) - 1)); |
| 771 | |
| 772 | wmin = max_t(u32, pix_lim->min_out_width, crop->width / sc_hrmax); |
| 773 | wmin = round_up(wmin, pix_lim->out_width_align); |
| 774 | hmin = max_t(u32, 8, crop->height / sc_vrmax); |
| 775 | hmin = round_up(hmin, 8); |
| 776 | |
| 777 | v4l_bound_align_image(&pix->width, wmin, pix_lim->max_sc_out_width, |
| 778 | ffs(pix_lim->out_width_align) - 1, |
| 779 | &pix->height, hmin, pix_lim->max_height, 0, 0); |
| 780 | |
| 781 | pix->bytesperline = pix->width * fmt->ybpp; |
| 782 | pix->sizeimage = (pix->width * pix->height * fmt->depth) / 8; |
| 783 | pix->pixelformat = fmt->fourcc; |
| 784 | pix->colorspace = V4L2_COLORSPACE_JPEG; |
| 785 | pix->field = V4L2_FIELD_NONE; |
| 786 | |
| 787 | pr_debug("%ux%u, wmin: %d, hmin: %d, sc_hrmax: %d, sc_vrmax: %d\n", |
| 788 | pix->width, pix->height, wmin, hmin, sc_hrmax, sc_vrmax); |
| 789 | |
| 790 | return 0; |
| 791 | } |
| 792 | |
| 793 | static int s3c_camif_vidioc_try_fmt(struct file *file, void *priv, |
| 794 | struct v4l2_format *f) |
| 795 | { |
| 796 | struct camif_vp *vp = video_drvdata(file); |
| 797 | return __camif_video_try_format(vp, &f->fmt.pix, NULL); |
| 798 | } |
| 799 | |
| 800 | static int s3c_camif_vidioc_s_fmt(struct file *file, void *priv, |
| 801 | struct v4l2_format *f) |
| 802 | { |
| 803 | struct v4l2_pix_format *pix = &f->fmt.pix; |
| 804 | struct camif_vp *vp = video_drvdata(file); |
| 805 | struct camif_frame *out_frame = &vp->out_frame; |
| 806 | const struct camif_fmt *fmt = NULL; |
| 807 | int ret; |
| 808 | |
| 809 | pr_debug("[vp%d]\n", vp->id); |
| 810 | |
| 811 | if (vb2_is_busy(&vp->vb_queue)) |
| 812 | return -EBUSY; |
| 813 | |
| 814 | ret = __camif_video_try_format(vp, &f->fmt.pix, &fmt); |
| 815 | if (ret < 0) |
| 816 | return ret; |
| 817 | |
| 818 | vp->out_fmt = fmt; |
| 819 | vp->payload = pix->sizeimage; |
| 820 | out_frame->f_width = pix->width; |
| 821 | out_frame->f_height = pix->height; |
| 822 | |
| 823 | /* Reset composition rectangle */ |
| 824 | out_frame->rect.width = pix->width; |
| 825 | out_frame->rect.height = pix->height; |
| 826 | out_frame->rect.left = 0; |
| 827 | out_frame->rect.top = 0; |
| 828 | |
| 829 | if (vp->owner == NULL) |
| 830 | vp->owner = priv; |
| 831 | |
| 832 | pr_debug("%ux%u. payload: %u. fmt: %s. %d %d. sizeimage: %d. bpl: %d\n", |
| 833 | out_frame->f_width, out_frame->f_height, vp->payload, fmt->name, |
| 834 | pix->width * pix->height * fmt->depth, fmt->depth, |
| 835 | pix->sizeimage, pix->bytesperline); |
| 836 | |
| 837 | return 0; |
| 838 | } |
| 839 | |
| 840 | /* Only check pixel formats at the sensor and the camif subdev pads */ |
| 841 | static int camif_pipeline_validate(struct camif_dev *camif) |
| 842 | { |
| 843 | struct v4l2_subdev_format src_fmt; |
| 844 | struct media_pad *pad; |
| 845 | int ret; |
| 846 | |
| 847 | /* Retrieve format at the sensor subdev source pad */ |
Andrzej Hajda | 1bddf1b | 2013-06-03 05:16:13 -0300 | [diff] [blame] | 848 | pad = media_entity_remote_pad(&camif->pads[0]); |
Sylwester Nawrocki | babde1c | 2012-08-22 20:09:18 -0300 | [diff] [blame] | 849 | if (!pad || media_entity_type(pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV) |
| 850 | return -EPIPE; |
| 851 | |
| 852 | src_fmt.pad = pad->index; |
| 853 | src_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE; |
| 854 | ret = v4l2_subdev_call(camif->sensor.sd, pad, get_fmt, NULL, &src_fmt); |
| 855 | if (ret < 0 && ret != -ENOIOCTLCMD) |
| 856 | return -EPIPE; |
| 857 | |
| 858 | if (src_fmt.format.width != camif->mbus_fmt.width || |
| 859 | src_fmt.format.height != camif->mbus_fmt.height || |
| 860 | src_fmt.format.code != camif->mbus_fmt.code) |
| 861 | return -EPIPE; |
| 862 | |
| 863 | return 0; |
| 864 | } |
| 865 | |
| 866 | static int s3c_camif_streamon(struct file *file, void *priv, |
| 867 | enum v4l2_buf_type type) |
| 868 | { |
| 869 | struct camif_vp *vp = video_drvdata(file); |
| 870 | struct camif_dev *camif = vp->camif; |
| 871 | struct media_entity *sensor = &camif->sensor.sd->entity; |
| 872 | int ret; |
| 873 | |
| 874 | pr_debug("[vp%d]\n", vp->id); |
| 875 | |
| 876 | if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 877 | return -EINVAL; |
| 878 | |
| 879 | if (vp->owner && vp->owner != priv) |
| 880 | return -EBUSY; |
| 881 | |
| 882 | if (s3c_vp_active(vp)) |
| 883 | return 0; |
| 884 | |
| 885 | ret = media_entity_pipeline_start(sensor, camif->m_pipeline); |
| 886 | if (ret < 0) |
| 887 | return ret; |
| 888 | |
| 889 | ret = camif_pipeline_validate(camif); |
| 890 | if (ret < 0) { |
| 891 | media_entity_pipeline_stop(sensor); |
| 892 | return ret; |
| 893 | } |
| 894 | |
| 895 | return vb2_streamon(&vp->vb_queue, type); |
| 896 | } |
| 897 | |
| 898 | static int s3c_camif_streamoff(struct file *file, void *priv, |
| 899 | enum v4l2_buf_type type) |
| 900 | { |
| 901 | struct camif_vp *vp = video_drvdata(file); |
| 902 | struct camif_dev *camif = vp->camif; |
| 903 | int ret; |
| 904 | |
| 905 | pr_debug("[vp%d]\n", vp->id); |
| 906 | |
| 907 | if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 908 | return -EINVAL; |
| 909 | |
| 910 | if (vp->owner && vp->owner != priv) |
| 911 | return -EBUSY; |
| 912 | |
| 913 | ret = vb2_streamoff(&vp->vb_queue, type); |
| 914 | if (ret == 0) |
| 915 | media_entity_pipeline_stop(&camif->sensor.sd->entity); |
| 916 | return ret; |
| 917 | } |
| 918 | |
| 919 | static int s3c_camif_reqbufs(struct file *file, void *priv, |
| 920 | struct v4l2_requestbuffers *rb) |
| 921 | { |
| 922 | struct camif_vp *vp = video_drvdata(file); |
| 923 | int ret; |
| 924 | |
| 925 | pr_debug("[vp%d] rb count: %d, owner: %p, priv: %p\n", |
| 926 | vp->id, rb->count, vp->owner, priv); |
| 927 | |
| 928 | if (vp->owner && vp->owner != priv) |
| 929 | return -EBUSY; |
| 930 | |
| 931 | if (rb->count) |
| 932 | rb->count = max_t(u32, CAMIF_REQ_BUFS_MIN, rb->count); |
| 933 | else |
| 934 | vp->owner = NULL; |
| 935 | |
| 936 | ret = vb2_reqbufs(&vp->vb_queue, rb); |
Sylwester Nawrocki | 69b95a3 | 2013-02-07 18:36:12 -0300 | [diff] [blame] | 937 | if (ret < 0) |
| 938 | return ret; |
| 939 | |
| 940 | if (rb->count && rb->count < CAMIF_REQ_BUFS_MIN) { |
| 941 | rb->count = 0; |
| 942 | vb2_reqbufs(&vp->vb_queue, rb); |
| 943 | ret = -ENOMEM; |
Sylwester Nawrocki | babde1c | 2012-08-22 20:09:18 -0300 | [diff] [blame] | 944 | } |
| 945 | |
Sylwester Nawrocki | 69b95a3 | 2013-02-07 18:36:12 -0300 | [diff] [blame] | 946 | vp->reqbufs_count = rb->count; |
| 947 | if (vp->owner == NULL && rb->count > 0) |
| 948 | vp->owner = priv; |
| 949 | |
Sylwester Nawrocki | babde1c | 2012-08-22 20:09:18 -0300 | [diff] [blame] | 950 | return ret; |
| 951 | } |
| 952 | |
| 953 | static int s3c_camif_querybuf(struct file *file, void *priv, |
| 954 | struct v4l2_buffer *buf) |
| 955 | { |
| 956 | struct camif_vp *vp = video_drvdata(file); |
| 957 | return vb2_querybuf(&vp->vb_queue, buf); |
| 958 | } |
| 959 | |
| 960 | static int s3c_camif_qbuf(struct file *file, void *priv, |
| 961 | struct v4l2_buffer *buf) |
| 962 | { |
| 963 | struct camif_vp *vp = video_drvdata(file); |
| 964 | |
| 965 | pr_debug("[vp%d]\n", vp->id); |
| 966 | |
| 967 | if (vp->owner && vp->owner != priv) |
| 968 | return -EBUSY; |
| 969 | |
| 970 | return vb2_qbuf(&vp->vb_queue, buf); |
| 971 | } |
| 972 | |
| 973 | static int s3c_camif_dqbuf(struct file *file, void *priv, |
| 974 | struct v4l2_buffer *buf) |
| 975 | { |
| 976 | struct camif_vp *vp = video_drvdata(file); |
| 977 | |
| 978 | pr_debug("[vp%d] sequence: %d\n", vp->id, vp->frame_sequence); |
| 979 | |
| 980 | if (vp->owner && vp->owner != priv) |
| 981 | return -EBUSY; |
| 982 | |
| 983 | return vb2_dqbuf(&vp->vb_queue, buf, file->f_flags & O_NONBLOCK); |
| 984 | } |
| 985 | |
| 986 | static int s3c_camif_create_bufs(struct file *file, void *priv, |
| 987 | struct v4l2_create_buffers *create) |
| 988 | { |
| 989 | struct camif_vp *vp = video_drvdata(file); |
| 990 | int ret; |
| 991 | |
| 992 | if (vp->owner && vp->owner != priv) |
| 993 | return -EBUSY; |
| 994 | |
| 995 | create->count = max_t(u32, 1, create->count); |
| 996 | ret = vb2_create_bufs(&vp->vb_queue, create); |
| 997 | |
| 998 | if (!ret && vp->owner == NULL) |
| 999 | vp->owner = priv; |
| 1000 | |
| 1001 | return ret; |
| 1002 | } |
| 1003 | |
| 1004 | static int s3c_camif_prepare_buf(struct file *file, void *priv, |
| 1005 | struct v4l2_buffer *b) |
| 1006 | { |
| 1007 | struct camif_vp *vp = video_drvdata(file); |
| 1008 | return vb2_prepare_buf(&vp->vb_queue, b); |
| 1009 | } |
| 1010 | |
| 1011 | static int s3c_camif_g_selection(struct file *file, void *priv, |
| 1012 | struct v4l2_selection *sel) |
| 1013 | { |
| 1014 | struct camif_vp *vp = video_drvdata(file); |
| 1015 | |
| 1016 | if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 1017 | return -EINVAL; |
| 1018 | |
| 1019 | switch (sel->target) { |
| 1020 | case V4L2_SEL_TGT_COMPOSE_BOUNDS: |
| 1021 | case V4L2_SEL_TGT_COMPOSE_DEFAULT: |
| 1022 | sel->r.left = 0; |
| 1023 | sel->r.top = 0; |
| 1024 | sel->r.width = vp->out_frame.f_width; |
| 1025 | sel->r.height = vp->out_frame.f_height; |
| 1026 | return 0; |
| 1027 | |
| 1028 | case V4L2_SEL_TGT_COMPOSE: |
| 1029 | sel->r = vp->out_frame.rect; |
| 1030 | return 0; |
| 1031 | } |
| 1032 | |
| 1033 | return -EINVAL; |
| 1034 | } |
| 1035 | |
| 1036 | static void __camif_try_compose(struct camif_dev *camif, struct camif_vp *vp, |
| 1037 | struct v4l2_rect *r) |
| 1038 | { |
| 1039 | /* s3c244x doesn't support composition */ |
| 1040 | if (camif->variant->ip_revision == S3C244X_CAMIF_IP_REV) { |
| 1041 | *r = vp->out_frame.rect; |
| 1042 | return; |
| 1043 | } |
| 1044 | |
| 1045 | /* TODO: s3c64xx */ |
| 1046 | } |
| 1047 | |
| 1048 | static int s3c_camif_s_selection(struct file *file, void *priv, |
| 1049 | struct v4l2_selection *sel) |
| 1050 | { |
| 1051 | struct camif_vp *vp = video_drvdata(file); |
| 1052 | struct camif_dev *camif = vp->camif; |
| 1053 | struct v4l2_rect rect = sel->r; |
| 1054 | unsigned long flags; |
| 1055 | |
| 1056 | if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE || |
| 1057 | sel->target != V4L2_SEL_TGT_COMPOSE) |
| 1058 | return -EINVAL; |
| 1059 | |
| 1060 | __camif_try_compose(camif, vp, &rect); |
| 1061 | |
| 1062 | sel->r = rect; |
| 1063 | spin_lock_irqsave(&camif->slock, flags); |
| 1064 | vp->out_frame.rect = rect; |
| 1065 | vp->state |= ST_VP_CONFIG; |
| 1066 | spin_unlock_irqrestore(&camif->slock, flags); |
| 1067 | |
| 1068 | pr_debug("type: %#x, target: %#x, flags: %#x, (%d,%d)/%dx%d\n", |
| 1069 | sel->type, sel->target, sel->flags, |
| 1070 | sel->r.left, sel->r.top, sel->r.width, sel->r.height); |
| 1071 | |
| 1072 | return 0; |
| 1073 | } |
| 1074 | |
| 1075 | static const struct v4l2_ioctl_ops s3c_camif_ioctl_ops = { |
| 1076 | .vidioc_querycap = s3c_camif_vidioc_querycap, |
| 1077 | .vidioc_enum_input = s3c_camif_vidioc_enum_input, |
| 1078 | .vidioc_g_input = s3c_camif_vidioc_g_input, |
| 1079 | .vidioc_s_input = s3c_camif_vidioc_s_input, |
| 1080 | .vidioc_enum_fmt_vid_cap = s3c_camif_vidioc_enum_fmt, |
| 1081 | .vidioc_try_fmt_vid_cap = s3c_camif_vidioc_try_fmt, |
| 1082 | .vidioc_s_fmt_vid_cap = s3c_camif_vidioc_s_fmt, |
| 1083 | .vidioc_g_fmt_vid_cap = s3c_camif_vidioc_g_fmt, |
| 1084 | .vidioc_g_selection = s3c_camif_g_selection, |
| 1085 | .vidioc_s_selection = s3c_camif_s_selection, |
| 1086 | .vidioc_reqbufs = s3c_camif_reqbufs, |
| 1087 | .vidioc_querybuf = s3c_camif_querybuf, |
| 1088 | .vidioc_prepare_buf = s3c_camif_prepare_buf, |
| 1089 | .vidioc_create_bufs = s3c_camif_create_bufs, |
| 1090 | .vidioc_qbuf = s3c_camif_qbuf, |
| 1091 | .vidioc_dqbuf = s3c_camif_dqbuf, |
| 1092 | .vidioc_streamon = s3c_camif_streamon, |
| 1093 | .vidioc_streamoff = s3c_camif_streamoff, |
| 1094 | .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, |
| 1095 | .vidioc_unsubscribe_event = v4l2_event_unsubscribe, |
| 1096 | .vidioc_log_status = v4l2_ctrl_log_status, |
| 1097 | }; |
| 1098 | |
| 1099 | /* |
| 1100 | * Video node controls |
| 1101 | */ |
| 1102 | static int s3c_camif_video_s_ctrl(struct v4l2_ctrl *ctrl) |
| 1103 | { |
| 1104 | struct camif_vp *vp = ctrl->priv; |
| 1105 | struct camif_dev *camif = vp->camif; |
| 1106 | unsigned long flags; |
| 1107 | |
| 1108 | pr_debug("[vp%d] ctrl: %s, value: %d\n", vp->id, |
| 1109 | ctrl->name, ctrl->val); |
| 1110 | |
| 1111 | spin_lock_irqsave(&camif->slock, flags); |
| 1112 | |
| 1113 | switch (ctrl->id) { |
| 1114 | case V4L2_CID_HFLIP: |
| 1115 | vp->hflip = ctrl->val; |
| 1116 | break; |
| 1117 | |
| 1118 | case V4L2_CID_VFLIP: |
| 1119 | vp->vflip = ctrl->val; |
| 1120 | break; |
| 1121 | } |
| 1122 | |
| 1123 | vp->state |= ST_VP_CONFIG; |
| 1124 | spin_unlock_irqrestore(&camif->slock, flags); |
| 1125 | return 0; |
| 1126 | } |
| 1127 | |
| 1128 | /* Codec and preview video node control ops */ |
| 1129 | static const struct v4l2_ctrl_ops s3c_camif_video_ctrl_ops = { |
| 1130 | .s_ctrl = s3c_camif_video_s_ctrl, |
| 1131 | }; |
| 1132 | |
| 1133 | int s3c_camif_register_video_node(struct camif_dev *camif, int idx) |
| 1134 | { |
| 1135 | struct camif_vp *vp = &camif->vp[idx]; |
| 1136 | struct vb2_queue *q = &vp->vb_queue; |
| 1137 | struct video_device *vfd = &vp->vdev; |
| 1138 | struct v4l2_ctrl *ctrl; |
| 1139 | int ret; |
| 1140 | |
| 1141 | memset(vfd, 0, sizeof(*vfd)); |
| 1142 | snprintf(vfd->name, sizeof(vfd->name), "camif-%s", |
| 1143 | vp->id == 0 ? "codec" : "preview"); |
| 1144 | |
| 1145 | vfd->fops = &s3c_camif_fops; |
| 1146 | vfd->ioctl_ops = &s3c_camif_ioctl_ops; |
| 1147 | vfd->v4l2_dev = &camif->v4l2_dev; |
| 1148 | vfd->minor = -1; |
| 1149 | vfd->release = video_device_release_empty; |
| 1150 | vfd->lock = &camif->lock; |
| 1151 | vp->reqbufs_count = 0; |
| 1152 | |
| 1153 | INIT_LIST_HEAD(&vp->pending_buf_q); |
| 1154 | INIT_LIST_HEAD(&vp->active_buf_q); |
| 1155 | |
| 1156 | memset(q, 0, sizeof(*q)); |
| 1157 | q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 1158 | q->io_modes = VB2_MMAP | VB2_USERPTR; |
| 1159 | q->ops = &s3c_camif_qops; |
| 1160 | q->mem_ops = &vb2_dma_contig_memops; |
| 1161 | q->buf_struct_size = sizeof(struct camif_buffer); |
| 1162 | q->drv_priv = vp; |
Sakari Ailus | ade4868 | 2014-02-25 19:12:19 -0300 | [diff] [blame] | 1163 | q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; |
Sylwester Nawrocki | babde1c | 2012-08-22 20:09:18 -0300 | [diff] [blame] | 1164 | |
| 1165 | ret = vb2_queue_init(q); |
| 1166 | if (ret) |
| 1167 | goto err_vd_rel; |
| 1168 | |
| 1169 | vp->pad.flags = MEDIA_PAD_FL_SINK; |
| 1170 | ret = media_entity_init(&vfd->entity, 1, &vp->pad, 0); |
| 1171 | if (ret) |
| 1172 | goto err_vd_rel; |
| 1173 | |
| 1174 | video_set_drvdata(vfd, vp); |
Sylwester Nawrocki | babde1c | 2012-08-22 20:09:18 -0300 | [diff] [blame] | 1175 | |
| 1176 | v4l2_ctrl_handler_init(&vp->ctrl_handler, 1); |
| 1177 | ctrl = v4l2_ctrl_new_std(&vp->ctrl_handler, &s3c_camif_video_ctrl_ops, |
| 1178 | V4L2_CID_HFLIP, 0, 1, 1, 0); |
| 1179 | if (ctrl) |
| 1180 | ctrl->priv = vp; |
| 1181 | ctrl = v4l2_ctrl_new_std(&vp->ctrl_handler, &s3c_camif_video_ctrl_ops, |
| 1182 | V4L2_CID_VFLIP, 0, 1, 1, 0); |
| 1183 | if (ctrl) |
| 1184 | ctrl->priv = vp; |
| 1185 | |
| 1186 | ret = vp->ctrl_handler.error; |
| 1187 | if (ret < 0) |
| 1188 | goto err_me_cleanup; |
| 1189 | |
| 1190 | vfd->ctrl_handler = &vp->ctrl_handler; |
| 1191 | |
| 1192 | ret = video_register_device(vfd, VFL_TYPE_GRABBER, -1); |
| 1193 | if (ret) |
| 1194 | goto err_ctrlh_free; |
| 1195 | |
| 1196 | v4l2_info(&camif->v4l2_dev, "registered %s as /dev/%s\n", |
| 1197 | vfd->name, video_device_node_name(vfd)); |
| 1198 | return 0; |
| 1199 | |
| 1200 | err_ctrlh_free: |
| 1201 | v4l2_ctrl_handler_free(&vp->ctrl_handler); |
| 1202 | err_me_cleanup: |
| 1203 | media_entity_cleanup(&vfd->entity); |
| 1204 | err_vd_rel: |
| 1205 | video_device_release(vfd); |
| 1206 | return ret; |
| 1207 | } |
| 1208 | |
| 1209 | void s3c_camif_unregister_video_node(struct camif_dev *camif, int idx) |
| 1210 | { |
| 1211 | struct video_device *vfd = &camif->vp[idx].vdev; |
| 1212 | |
| 1213 | if (video_is_registered(vfd)) { |
| 1214 | video_unregister_device(vfd); |
| 1215 | media_entity_cleanup(&vfd->entity); |
| 1216 | v4l2_ctrl_handler_free(vfd->ctrl_handler); |
| 1217 | } |
| 1218 | } |
| 1219 | |
| 1220 | /* Media bus pixel formats supported at the camif input */ |
| 1221 | static const enum v4l2_mbus_pixelcode camif_mbus_formats[] = { |
| 1222 | V4L2_MBUS_FMT_YUYV8_2X8, |
| 1223 | V4L2_MBUS_FMT_YVYU8_2X8, |
| 1224 | V4L2_MBUS_FMT_UYVY8_2X8, |
| 1225 | V4L2_MBUS_FMT_VYUY8_2X8, |
| 1226 | }; |
| 1227 | |
| 1228 | /* |
| 1229 | * Camera input interface subdev operations |
| 1230 | */ |
| 1231 | |
| 1232 | static int s3c_camif_subdev_enum_mbus_code(struct v4l2_subdev *sd, |
| 1233 | struct v4l2_subdev_fh *fh, |
| 1234 | struct v4l2_subdev_mbus_code_enum *code) |
| 1235 | { |
| 1236 | if (code->index >= ARRAY_SIZE(camif_mbus_formats)) |
| 1237 | return -EINVAL; |
| 1238 | |
| 1239 | code->code = camif_mbus_formats[code->index]; |
| 1240 | return 0; |
| 1241 | } |
| 1242 | |
| 1243 | static int s3c_camif_subdev_get_fmt(struct v4l2_subdev *sd, |
| 1244 | struct v4l2_subdev_fh *fh, |
| 1245 | struct v4l2_subdev_format *fmt) |
| 1246 | { |
| 1247 | struct camif_dev *camif = v4l2_get_subdevdata(sd); |
| 1248 | struct v4l2_mbus_framefmt *mf = &fmt->format; |
| 1249 | |
| 1250 | if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) { |
| 1251 | mf = v4l2_subdev_get_try_format(fh, fmt->pad); |
| 1252 | fmt->format = *mf; |
| 1253 | return 0; |
| 1254 | } |
| 1255 | |
| 1256 | mutex_lock(&camif->lock); |
| 1257 | |
| 1258 | switch (fmt->pad) { |
| 1259 | case CAMIF_SD_PAD_SINK: |
| 1260 | /* full camera input pixel size */ |
| 1261 | *mf = camif->mbus_fmt; |
| 1262 | break; |
| 1263 | |
| 1264 | case CAMIF_SD_PAD_SOURCE_C...CAMIF_SD_PAD_SOURCE_P: |
| 1265 | /* crop rectangle at camera interface input */ |
| 1266 | mf->width = camif->camif_crop.width; |
| 1267 | mf->height = camif->camif_crop.height; |
| 1268 | mf->code = camif->mbus_fmt.code; |
| 1269 | break; |
| 1270 | } |
| 1271 | |
| 1272 | mutex_unlock(&camif->lock); |
Laurent Pinchart | 6c5bba9 | 2014-05-19 12:04:32 -0300 | [diff] [blame] | 1273 | mf->field = V4L2_FIELD_NONE; |
Sylwester Nawrocki | babde1c | 2012-08-22 20:09:18 -0300 | [diff] [blame] | 1274 | mf->colorspace = V4L2_COLORSPACE_JPEG; |
| 1275 | return 0; |
| 1276 | } |
| 1277 | |
| 1278 | static void __camif_subdev_try_format(struct camif_dev *camif, |
| 1279 | struct v4l2_mbus_framefmt *mf, int pad) |
| 1280 | { |
| 1281 | const struct s3c_camif_variant *variant = camif->variant; |
| 1282 | const struct vp_pix_limits *pix_lim; |
| 1283 | int i = ARRAY_SIZE(camif_mbus_formats); |
| 1284 | |
| 1285 | /* FIXME: constraints against codec or preview path ? */ |
| 1286 | pix_lim = &variant->vp_pix_limits[VP_CODEC]; |
| 1287 | |
| 1288 | while (i-- >= 0) |
| 1289 | if (camif_mbus_formats[i] == mf->code) |
| 1290 | break; |
| 1291 | |
| 1292 | mf->code = camif_mbus_formats[i]; |
| 1293 | |
| 1294 | if (pad == CAMIF_SD_PAD_SINK) { |
| 1295 | v4l_bound_align_image(&mf->width, 8, CAMIF_MAX_PIX_WIDTH, |
| 1296 | ffs(pix_lim->out_width_align) - 1, |
| 1297 | &mf->height, 8, CAMIF_MAX_PIX_HEIGHT, 0, |
| 1298 | 0); |
| 1299 | } else { |
| 1300 | struct v4l2_rect *crop = &camif->camif_crop; |
| 1301 | v4l_bound_align_image(&mf->width, 8, crop->width, |
| 1302 | ffs(pix_lim->out_width_align) - 1, |
| 1303 | &mf->height, 8, crop->height, |
| 1304 | 0, 0); |
| 1305 | } |
| 1306 | |
| 1307 | v4l2_dbg(1, debug, &camif->subdev, "%ux%u\n", mf->width, mf->height); |
| 1308 | } |
| 1309 | |
| 1310 | static int s3c_camif_subdev_set_fmt(struct v4l2_subdev *sd, |
| 1311 | struct v4l2_subdev_fh *fh, |
| 1312 | struct v4l2_subdev_format *fmt) |
| 1313 | { |
| 1314 | struct camif_dev *camif = v4l2_get_subdevdata(sd); |
| 1315 | struct v4l2_mbus_framefmt *mf = &fmt->format; |
| 1316 | struct v4l2_rect *crop = &camif->camif_crop; |
| 1317 | int i; |
| 1318 | |
| 1319 | v4l2_dbg(1, debug, sd, "pad%d: code: 0x%x, %ux%u\n", |
| 1320 | fmt->pad, mf->code, mf->width, mf->height); |
| 1321 | |
Laurent Pinchart | 6c5bba9 | 2014-05-19 12:04:32 -0300 | [diff] [blame] | 1322 | mf->field = V4L2_FIELD_NONE; |
Sylwester Nawrocki | babde1c | 2012-08-22 20:09:18 -0300 | [diff] [blame] | 1323 | mf->colorspace = V4L2_COLORSPACE_JPEG; |
| 1324 | mutex_lock(&camif->lock); |
| 1325 | |
| 1326 | /* |
| 1327 | * No pixel format change at the camera input is allowed |
| 1328 | * while streaming. |
| 1329 | */ |
| 1330 | if (vb2_is_busy(&camif->vp[VP_CODEC].vb_queue) || |
| 1331 | vb2_is_busy(&camif->vp[VP_PREVIEW].vb_queue)) { |
| 1332 | mutex_unlock(&camif->lock); |
| 1333 | return -EBUSY; |
| 1334 | } |
| 1335 | |
| 1336 | __camif_subdev_try_format(camif, mf, fmt->pad); |
| 1337 | |
| 1338 | if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) { |
| 1339 | mf = v4l2_subdev_get_try_format(fh, fmt->pad); |
| 1340 | *mf = fmt->format; |
| 1341 | mutex_unlock(&camif->lock); |
| 1342 | return 0; |
| 1343 | } |
| 1344 | |
| 1345 | switch (fmt->pad) { |
| 1346 | case CAMIF_SD_PAD_SINK: |
| 1347 | camif->mbus_fmt = *mf; |
| 1348 | /* Reset sink crop rectangle. */ |
| 1349 | crop->width = mf->width; |
| 1350 | crop->height = mf->height; |
| 1351 | crop->left = 0; |
| 1352 | crop->top = 0; |
| 1353 | /* |
| 1354 | * Reset source format (the camif's crop rectangle) |
| 1355 | * and the video output resolution. |
| 1356 | */ |
| 1357 | for (i = 0; i < CAMIF_VP_NUM; i++) { |
| 1358 | struct camif_frame *frame = &camif->vp[i].out_frame; |
| 1359 | frame->rect = *crop; |
| 1360 | frame->f_width = mf->width; |
| 1361 | frame->f_height = mf->height; |
| 1362 | } |
| 1363 | break; |
| 1364 | |
| 1365 | case CAMIF_SD_PAD_SOURCE_C...CAMIF_SD_PAD_SOURCE_P: |
| 1366 | /* Pixel format can be only changed on the sink pad. */ |
| 1367 | mf->code = camif->mbus_fmt.code; |
| 1368 | mf->width = crop->width; |
| 1369 | mf->height = crop->height; |
| 1370 | break; |
| 1371 | } |
| 1372 | |
| 1373 | mutex_unlock(&camif->lock); |
| 1374 | return 0; |
| 1375 | } |
| 1376 | |
| 1377 | static int s3c_camif_subdev_get_selection(struct v4l2_subdev *sd, |
| 1378 | struct v4l2_subdev_fh *fh, |
| 1379 | struct v4l2_subdev_selection *sel) |
| 1380 | { |
| 1381 | struct camif_dev *camif = v4l2_get_subdevdata(sd); |
| 1382 | struct v4l2_rect *crop = &camif->camif_crop; |
| 1383 | struct v4l2_mbus_framefmt *mf = &camif->mbus_fmt; |
| 1384 | |
| 1385 | if ((sel->target != V4L2_SEL_TGT_CROP && |
| 1386 | sel->target != V4L2_SEL_TGT_CROP_BOUNDS) || |
| 1387 | sel->pad != CAMIF_SD_PAD_SINK) |
| 1388 | return -EINVAL; |
| 1389 | |
| 1390 | if (sel->which == V4L2_SUBDEV_FORMAT_TRY) { |
| 1391 | sel->r = *v4l2_subdev_get_try_crop(fh, sel->pad); |
| 1392 | return 0; |
| 1393 | } |
| 1394 | |
| 1395 | mutex_lock(&camif->lock); |
| 1396 | |
| 1397 | if (sel->target == V4L2_SEL_TGT_CROP) { |
| 1398 | sel->r = *crop; |
| 1399 | } else { /* crop bounds */ |
| 1400 | sel->r.width = mf->width; |
| 1401 | sel->r.height = mf->height; |
| 1402 | sel->r.left = 0; |
| 1403 | sel->r.top = 0; |
| 1404 | } |
| 1405 | |
| 1406 | mutex_unlock(&camif->lock); |
| 1407 | |
| 1408 | v4l2_dbg(1, debug, sd, "%s: crop: (%d,%d) %dx%d, size: %ux%u\n", |
| 1409 | __func__, crop->left, crop->top, crop->width, |
| 1410 | crop->height, mf->width, mf->height); |
| 1411 | |
| 1412 | return 0; |
| 1413 | } |
| 1414 | |
| 1415 | static void __camif_try_crop(struct camif_dev *camif, struct v4l2_rect *r) |
| 1416 | { |
| 1417 | struct v4l2_mbus_framefmt *mf = &camif->mbus_fmt; |
| 1418 | const struct camif_pix_limits *pix_lim = &camif->variant->pix_limits; |
| 1419 | unsigned int left = 2 * r->left; |
| 1420 | unsigned int top = 2 * r->top; |
| 1421 | |
| 1422 | /* |
| 1423 | * Following constraints must be met: |
| 1424 | * - r->width + 2 * r->left = mf->width; |
| 1425 | * - r->height + 2 * r->top = mf->height; |
| 1426 | * - crop rectangle size and position must be aligned |
| 1427 | * to 8 or 2 pixels, depending on SoC version. |
| 1428 | */ |
| 1429 | v4l_bound_align_image(&r->width, 0, mf->width, |
| 1430 | ffs(pix_lim->win_hor_offset_align) - 1, |
| 1431 | &r->height, 0, mf->height, 1, 0); |
| 1432 | |
| 1433 | v4l_bound_align_image(&left, 0, mf->width - r->width, |
| 1434 | ffs(pix_lim->win_hor_offset_align), |
| 1435 | &top, 0, mf->height - r->height, 2, 0); |
| 1436 | |
| 1437 | r->left = left / 2; |
| 1438 | r->top = top / 2; |
| 1439 | r->width = mf->width - left; |
| 1440 | r->height = mf->height - top; |
| 1441 | /* |
| 1442 | * Make sure we either downscale or upscale both the pixel |
| 1443 | * width and height. Just return current crop rectangle if |
| 1444 | * this scaler constraint is not met. |
| 1445 | */ |
| 1446 | if (camif->variant->ip_revision == S3C244X_CAMIF_IP_REV && |
| 1447 | camif_is_streaming(camif)) { |
| 1448 | unsigned int i; |
| 1449 | |
| 1450 | for (i = 0; i < CAMIF_VP_NUM; i++) { |
| 1451 | struct v4l2_rect *or = &camif->vp[i].out_frame.rect; |
| 1452 | if ((or->width > r->width) == (or->height > r->height)) |
| 1453 | continue; |
| 1454 | *r = camif->camif_crop; |
| 1455 | pr_debug("Width/height scaling direction limitation\n"); |
| 1456 | break; |
| 1457 | } |
| 1458 | } |
| 1459 | |
| 1460 | v4l2_dbg(1, debug, &camif->v4l2_dev, "crop: (%d,%d)/%dx%d, fmt: %ux%u\n", |
| 1461 | r->left, r->top, r->width, r->height, mf->width, mf->height); |
| 1462 | } |
| 1463 | |
| 1464 | static int s3c_camif_subdev_set_selection(struct v4l2_subdev *sd, |
| 1465 | struct v4l2_subdev_fh *fh, |
| 1466 | struct v4l2_subdev_selection *sel) |
| 1467 | { |
| 1468 | struct camif_dev *camif = v4l2_get_subdevdata(sd); |
| 1469 | struct v4l2_rect *crop = &camif->camif_crop; |
| 1470 | struct camif_scaler scaler; |
| 1471 | |
| 1472 | if (sel->target != V4L2_SEL_TGT_CROP || sel->pad != CAMIF_SD_PAD_SINK) |
| 1473 | return -EINVAL; |
| 1474 | |
| 1475 | mutex_lock(&camif->lock); |
| 1476 | __camif_try_crop(camif, &sel->r); |
| 1477 | |
| 1478 | if (sel->which == V4L2_SUBDEV_FORMAT_TRY) { |
| 1479 | *v4l2_subdev_get_try_crop(fh, sel->pad) = sel->r; |
| 1480 | } else { |
| 1481 | unsigned long flags; |
| 1482 | unsigned int i; |
| 1483 | |
| 1484 | spin_lock_irqsave(&camif->slock, flags); |
| 1485 | *crop = sel->r; |
| 1486 | |
| 1487 | for (i = 0; i < CAMIF_VP_NUM; i++) { |
| 1488 | struct camif_vp *vp = &camif->vp[i]; |
| 1489 | scaler = vp->scaler; |
| 1490 | if (s3c_camif_get_scaler_config(vp, &scaler)) |
| 1491 | continue; |
| 1492 | vp->scaler = scaler; |
| 1493 | vp->state |= ST_VP_CONFIG; |
| 1494 | } |
| 1495 | |
| 1496 | spin_unlock_irqrestore(&camif->slock, flags); |
| 1497 | } |
| 1498 | mutex_unlock(&camif->lock); |
| 1499 | |
| 1500 | v4l2_dbg(1, debug, sd, "%s: (%d,%d) %dx%d, f_w: %u, f_h: %u\n", |
| 1501 | __func__, crop->left, crop->top, crop->width, crop->height, |
| 1502 | camif->mbus_fmt.width, camif->mbus_fmt.height); |
| 1503 | |
| 1504 | return 0; |
| 1505 | } |
| 1506 | |
| 1507 | static const struct v4l2_subdev_pad_ops s3c_camif_subdev_pad_ops = { |
| 1508 | .enum_mbus_code = s3c_camif_subdev_enum_mbus_code, |
| 1509 | .get_selection = s3c_camif_subdev_get_selection, |
| 1510 | .set_selection = s3c_camif_subdev_set_selection, |
| 1511 | .get_fmt = s3c_camif_subdev_get_fmt, |
| 1512 | .set_fmt = s3c_camif_subdev_set_fmt, |
| 1513 | }; |
| 1514 | |
| 1515 | static struct v4l2_subdev_ops s3c_camif_subdev_ops = { |
| 1516 | .pad = &s3c_camif_subdev_pad_ops, |
| 1517 | }; |
| 1518 | |
| 1519 | static int s3c_camif_subdev_s_ctrl(struct v4l2_ctrl *ctrl) |
| 1520 | { |
| 1521 | struct camif_dev *camif = container_of(ctrl->handler, struct camif_dev, |
| 1522 | ctrl_handler); |
| 1523 | unsigned long flags; |
| 1524 | |
| 1525 | spin_lock_irqsave(&camif->slock, flags); |
| 1526 | |
| 1527 | switch (ctrl->id) { |
| 1528 | case V4L2_CID_COLORFX: |
| 1529 | camif->colorfx = camif->ctrl_colorfx->val; |
| 1530 | /* Set Cb, Cr */ |
| 1531 | switch (ctrl->val) { |
| 1532 | case V4L2_COLORFX_SEPIA: |
| 1533 | camif->colorfx_cb = 115; |
| 1534 | camif->colorfx_cr = 145; |
| 1535 | break; |
| 1536 | case V4L2_COLORFX_SET_CBCR: |
| 1537 | camif->colorfx_cb = camif->ctrl_colorfx_cbcr->val >> 8; |
| 1538 | camif->colorfx_cr = camif->ctrl_colorfx_cbcr->val & 0xff; |
| 1539 | break; |
| 1540 | default: |
| 1541 | /* for V4L2_COLORFX_BW and others */ |
| 1542 | camif->colorfx_cb = 128; |
| 1543 | camif->colorfx_cr = 128; |
| 1544 | } |
| 1545 | break; |
| 1546 | case V4L2_CID_TEST_PATTERN: |
| 1547 | camif->test_pattern = camif->ctrl_test_pattern->val; |
| 1548 | break; |
| 1549 | default: |
| 1550 | WARN_ON(1); |
| 1551 | } |
| 1552 | |
| 1553 | camif->vp[VP_CODEC].state |= ST_VP_CONFIG; |
| 1554 | camif->vp[VP_PREVIEW].state |= ST_VP_CONFIG; |
| 1555 | spin_unlock_irqrestore(&camif->slock, flags); |
| 1556 | |
| 1557 | return 0; |
| 1558 | } |
| 1559 | |
| 1560 | static const struct v4l2_ctrl_ops s3c_camif_subdev_ctrl_ops = { |
| 1561 | .s_ctrl = s3c_camif_subdev_s_ctrl, |
| 1562 | }; |
| 1563 | |
| 1564 | static const char * const s3c_camif_test_pattern_menu[] = { |
| 1565 | "Disabled", |
| 1566 | "Color bars", |
| 1567 | "Horizontal increment", |
| 1568 | "Vertical increment", |
| 1569 | }; |
| 1570 | |
| 1571 | int s3c_camif_create_subdev(struct camif_dev *camif) |
| 1572 | { |
| 1573 | struct v4l2_ctrl_handler *handler = &camif->ctrl_handler; |
| 1574 | struct v4l2_subdev *sd = &camif->subdev; |
| 1575 | int ret; |
| 1576 | |
| 1577 | v4l2_subdev_init(sd, &s3c_camif_subdev_ops); |
| 1578 | sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; |
| 1579 | strlcpy(sd->name, "S3C-CAMIF", sizeof(sd->name)); |
| 1580 | |
| 1581 | camif->pads[CAMIF_SD_PAD_SINK].flags = MEDIA_PAD_FL_SINK; |
| 1582 | camif->pads[CAMIF_SD_PAD_SOURCE_C].flags = MEDIA_PAD_FL_SOURCE; |
| 1583 | camif->pads[CAMIF_SD_PAD_SOURCE_P].flags = MEDIA_PAD_FL_SOURCE; |
| 1584 | |
| 1585 | ret = media_entity_init(&sd->entity, CAMIF_SD_PADS_NUM, |
| 1586 | camif->pads, 0); |
| 1587 | if (ret) |
| 1588 | return ret; |
| 1589 | |
| 1590 | v4l2_ctrl_handler_init(handler, 3); |
| 1591 | camif->ctrl_test_pattern = v4l2_ctrl_new_std_menu_items(handler, |
| 1592 | &s3c_camif_subdev_ctrl_ops, V4L2_CID_TEST_PATTERN, |
| 1593 | ARRAY_SIZE(s3c_camif_test_pattern_menu) - 1, 0, 0, |
| 1594 | s3c_camif_test_pattern_menu); |
| 1595 | |
Hans Verkuil | 7c86965 | 2014-01-20 06:27:09 -0300 | [diff] [blame] | 1596 | if (camif->variant->has_img_effect) { |
| 1597 | camif->ctrl_colorfx = v4l2_ctrl_new_std_menu(handler, |
Sylwester Nawrocki | babde1c | 2012-08-22 20:09:18 -0300 | [diff] [blame] | 1598 | &s3c_camif_subdev_ctrl_ops, |
| 1599 | V4L2_CID_COLORFX, V4L2_COLORFX_SET_CBCR, |
| 1600 | ~0x981f, V4L2_COLORFX_NONE); |
| 1601 | |
Hans Verkuil | 7c86965 | 2014-01-20 06:27:09 -0300 | [diff] [blame] | 1602 | camif->ctrl_colorfx_cbcr = v4l2_ctrl_new_std(handler, |
Sylwester Nawrocki | babde1c | 2012-08-22 20:09:18 -0300 | [diff] [blame] | 1603 | &s3c_camif_subdev_ctrl_ops, |
| 1604 | V4L2_CID_COLORFX_CBCR, 0, 0xffff, 1, 0); |
Hans Verkuil | 7c86965 | 2014-01-20 06:27:09 -0300 | [diff] [blame] | 1605 | } |
| 1606 | |
Sylwester Nawrocki | babde1c | 2012-08-22 20:09:18 -0300 | [diff] [blame] | 1607 | if (handler->error) { |
| 1608 | v4l2_ctrl_handler_free(handler); |
| 1609 | media_entity_cleanup(&sd->entity); |
| 1610 | return handler->error; |
| 1611 | } |
| 1612 | |
Hans Verkuil | 7c86965 | 2014-01-20 06:27:09 -0300 | [diff] [blame] | 1613 | if (camif->variant->has_img_effect) |
| 1614 | v4l2_ctrl_auto_cluster(2, &camif->ctrl_colorfx, |
Sylwester Nawrocki | babde1c | 2012-08-22 20:09:18 -0300 | [diff] [blame] | 1615 | V4L2_COLORFX_SET_CBCR, false); |
Hans Verkuil | 7c86965 | 2014-01-20 06:27:09 -0300 | [diff] [blame] | 1616 | |
Sylwester Nawrocki | babde1c | 2012-08-22 20:09:18 -0300 | [diff] [blame] | 1617 | sd->ctrl_handler = handler; |
| 1618 | v4l2_set_subdevdata(sd, camif); |
| 1619 | |
| 1620 | return 0; |
| 1621 | } |
| 1622 | |
| 1623 | void s3c_camif_unregister_subdev(struct camif_dev *camif) |
| 1624 | { |
| 1625 | struct v4l2_subdev *sd = &camif->subdev; |
| 1626 | |
| 1627 | /* Return if not registered */ |
| 1628 | if (v4l2_get_subdevdata(sd) == NULL) |
| 1629 | return; |
| 1630 | |
| 1631 | v4l2_device_unregister_subdev(sd); |
| 1632 | media_entity_cleanup(&sd->entity); |
| 1633 | v4l2_ctrl_handler_free(&camif->ctrl_handler); |
| 1634 | v4l2_set_subdevdata(sd, NULL); |
| 1635 | } |
| 1636 | |
| 1637 | int s3c_camif_set_defaults(struct camif_dev *camif) |
| 1638 | { |
| 1639 | unsigned int ip_rev = camif->variant->ip_revision; |
| 1640 | int i; |
| 1641 | |
| 1642 | for (i = 0; i < CAMIF_VP_NUM; i++) { |
| 1643 | struct camif_vp *vp = &camif->vp[i]; |
| 1644 | struct camif_frame *f = &vp->out_frame; |
| 1645 | |
| 1646 | vp->camif = camif; |
| 1647 | vp->id = i; |
| 1648 | vp->offset = camif->variant->vp_offset; |
| 1649 | |
| 1650 | if (ip_rev == S3C244X_CAMIF_IP_REV) |
| 1651 | vp->fmt_flags = i ? FMT_FL_S3C24XX_PREVIEW : |
| 1652 | FMT_FL_S3C24XX_CODEC; |
| 1653 | else |
| 1654 | vp->fmt_flags = FMT_FL_S3C64XX; |
| 1655 | |
| 1656 | vp->out_fmt = s3c_camif_find_format(vp, NULL, 0); |
| 1657 | BUG_ON(vp->out_fmt == NULL); |
| 1658 | |
| 1659 | memset(f, 0, sizeof(*f)); |
| 1660 | f->f_width = CAMIF_DEF_WIDTH; |
| 1661 | f->f_height = CAMIF_DEF_HEIGHT; |
| 1662 | f->rect.width = CAMIF_DEF_WIDTH; |
| 1663 | f->rect.height = CAMIF_DEF_HEIGHT; |
| 1664 | |
| 1665 | /* Scaler is always enabled */ |
| 1666 | vp->scaler.enable = 1; |
| 1667 | |
| 1668 | vp->payload = (f->f_width * f->f_height * |
| 1669 | vp->out_fmt->depth) / 8; |
| 1670 | } |
| 1671 | |
| 1672 | memset(&camif->mbus_fmt, 0, sizeof(camif->mbus_fmt)); |
| 1673 | camif->mbus_fmt.width = CAMIF_DEF_WIDTH; |
| 1674 | camif->mbus_fmt.height = CAMIF_DEF_HEIGHT; |
| 1675 | camif->mbus_fmt.code = camif_mbus_formats[0]; |
| 1676 | |
| 1677 | memset(&camif->camif_crop, 0, sizeof(camif->camif_crop)); |
| 1678 | camif->camif_crop.width = CAMIF_DEF_WIDTH; |
| 1679 | camif->camif_crop.height = CAMIF_DEF_HEIGHT; |
| 1680 | |
| 1681 | return 0; |
| 1682 | } |