root/drivers/media/platform/exynos4-is/fimc-capture.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. fimc_capture_hw_init
  2. fimc_capture_state_cleanup
  3. fimc_stop_capture
  4. fimc_capture_config_update
  5. fimc_capture_irq_handler
  6. start_streaming
  7. stop_streaming
  8. fimc_capture_suspend
  9. fimc_capture_resume
  10. queue_setup
  11. buffer_prepare
  12. buffer_queue
  13. fimc_capture_open
  14. fimc_capture_release
  15. fimc_capture_try_format
  16. fimc_capture_try_selection
  17. fimc_cap_querycap
  18. fimc_cap_enum_fmt
  19. fimc_pipeline_get_head
  20. fimc_pipeline_try_format
  21. fimc_get_sensor_frame_desc
  22. fimc_cap_g_fmt_mplane
  23. __video_try_or_set_format
  24. fimc_cap_try_fmt_mplane
  25. fimc_capture_mark_jpeg_xfer
  26. __fimc_capture_set_format
  27. fimc_cap_s_fmt_mplane
  28. fimc_cap_enum_input
  29. fimc_cap_s_input
  30. fimc_cap_g_input
  31. fimc_pipeline_validate
  32. fimc_cap_streamon
  33. fimc_cap_streamoff
  34. fimc_cap_reqbufs
  35. fimc_cap_g_selection
  36. enclosed_rectangle
  37. fimc_cap_s_selection
  38. fimc_link_setup
  39. fimc_sensor_notify
  40. fimc_subdev_enum_mbus_code
  41. fimc_subdev_get_fmt
  42. fimc_subdev_set_fmt
  43. fimc_subdev_get_selection
  44. fimc_subdev_set_selection
  45. fimc_capture_set_default_format
  46. fimc_register_capture_device
  47. fimc_capture_subdev_registered
  48. fimc_capture_subdev_unregistered
  49. fimc_initialize_capture_subdev
  50. fimc_unregister_capture_subdev

   1 // SPDX-License-Identifier: GPL-2.0-only
   2 /*
   3  * Samsung S5P/EXYNOS4 SoC series camera interface (camera capture) driver
   4  *
   5  * Copyright (C) 2010 - 2012 Samsung Electronics Co., Ltd.
   6  * Sylwester Nawrocki <s.nawrocki@samsung.com>
   7  */
   8 
   9 #include <linux/module.h>
  10 #include <linux/kernel.h>
  11 #include <linux/types.h>
  12 #include <linux/errno.h>
  13 #include <linux/bug.h>
  14 #include <linux/interrupt.h>
  15 #include <linux/device.h>
  16 #include <linux/pm_runtime.h>
  17 #include <linux/list.h>
  18 #include <linux/slab.h>
  19 
  20 #include <linux/videodev2.h>
  21 #include <media/v4l2-device.h>
  22 #include <media/v4l2-ioctl.h>
  23 #include <media/v4l2-mem2mem.h>
  24 #include <media/videobuf2-v4l2.h>
  25 #include <media/videobuf2-dma-contig.h>
  26 
  27 #include "common.h"
  28 #include "fimc-core.h"
  29 #include "fimc-reg.h"
  30 #include "media-dev.h"
  31 
  32 static int fimc_capture_hw_init(struct fimc_dev *fimc)
  33 {
  34         struct fimc_source_info *si = &fimc->vid_cap.source_config;
  35         struct fimc_ctx *ctx = fimc->vid_cap.ctx;
  36         int ret;
  37         unsigned long flags;
  38 
  39         if (ctx == NULL || ctx->s_frame.fmt == NULL)
  40                 return -EINVAL;
  41 
  42         if (si->fimc_bus_type == FIMC_BUS_TYPE_ISP_WRITEBACK) {
  43                 ret = fimc_hw_camblk_cfg_writeback(fimc);
  44                 if (ret < 0)
  45                         return ret;
  46         }
  47 
  48         spin_lock_irqsave(&fimc->slock, flags);
  49         fimc_prepare_dma_offset(ctx, &ctx->d_frame);
  50         fimc_set_yuv_order(ctx);
  51 
  52         fimc_hw_set_camera_polarity(fimc, si);
  53         fimc_hw_set_camera_type(fimc, si);
  54         fimc_hw_set_camera_source(fimc, si);
  55         fimc_hw_set_camera_offset(fimc, &ctx->s_frame);
  56 
  57         ret = fimc_set_scaler_info(ctx);
  58         if (!ret) {
  59                 fimc_hw_set_input_path(ctx);
  60                 fimc_hw_set_prescaler(ctx);
  61                 fimc_hw_set_mainscaler(ctx);
  62                 fimc_hw_set_target_format(ctx);
  63                 fimc_hw_set_rotation(ctx);
  64                 fimc_hw_set_effect(ctx);
  65                 fimc_hw_set_output_path(ctx);
  66                 fimc_hw_set_out_dma(ctx);
  67                 if (fimc->drv_data->alpha_color)
  68                         fimc_hw_set_rgb_alpha(ctx);
  69                 clear_bit(ST_CAPT_APPLY_CFG, &fimc->state);
  70         }
  71         spin_unlock_irqrestore(&fimc->slock, flags);
  72         return ret;
  73 }
  74 
  75 /*
  76  * Reinitialize the driver so it is ready to start the streaming again.
  77  * Set fimc->state to indicate stream off and the hardware shut down state.
  78  * If not suspending (@suspend is false), return any buffers to videobuf2.
  79  * Otherwise put any owned buffers onto the pending buffers queue, so they
  80  * can be re-spun when the device is being resumed. Also perform FIMC
  81  * software reset and disable streaming on the whole pipeline if required.
  82  */
  83 static int fimc_capture_state_cleanup(struct fimc_dev *fimc, bool suspend)
  84 {
  85         struct fimc_vid_cap *cap = &fimc->vid_cap;
  86         struct fimc_vid_buffer *buf;
  87         unsigned long flags;
  88         bool streaming;
  89 
  90         spin_lock_irqsave(&fimc->slock, flags);
  91         streaming = fimc->state & (1 << ST_CAPT_ISP_STREAM);
  92 
  93         fimc->state &= ~(1 << ST_CAPT_RUN | 1 << ST_CAPT_SHUT |
  94                          1 << ST_CAPT_STREAM | 1 << ST_CAPT_ISP_STREAM);
  95         if (suspend)
  96                 fimc->state |= (1 << ST_CAPT_SUSPENDED);
  97         else
  98                 fimc->state &= ~(1 << ST_CAPT_PEND | 1 << ST_CAPT_SUSPENDED);
  99 
 100         /* Release unused buffers */
 101         while (!suspend && !list_empty(&cap->pending_buf_q)) {
 102                 buf = fimc_pending_queue_pop(cap);
 103                 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
 104         }
 105         /* If suspending put unused buffers onto pending queue */
 106         while (!list_empty(&cap->active_buf_q)) {
 107                 buf = fimc_active_queue_pop(cap);
 108                 if (suspend)
 109                         fimc_pending_queue_add(cap, buf);
 110                 else
 111                         vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
 112         }
 113 
 114         fimc_hw_reset(fimc);
 115         cap->buf_index = 0;
 116 
 117         spin_unlock_irqrestore(&fimc->slock, flags);
 118 
 119         if (streaming)
 120                 return fimc_pipeline_call(&cap->ve, set_stream, 0);
 121         else
 122                 return 0;
 123 }
 124 
 125 static int fimc_stop_capture(struct fimc_dev *fimc, bool suspend)
 126 {
 127         unsigned long flags;
 128 
 129         if (!fimc_capture_active(fimc))
 130                 return 0;
 131 
 132         spin_lock_irqsave(&fimc->slock, flags);
 133         set_bit(ST_CAPT_SHUT, &fimc->state);
 134         fimc_deactivate_capture(fimc);
 135         spin_unlock_irqrestore(&fimc->slock, flags);
 136 
 137         wait_event_timeout(fimc->irq_queue,
 138                            !test_bit(ST_CAPT_SHUT, &fimc->state),
 139                            (2*HZ/10)); /* 200 ms */
 140 
 141         return fimc_capture_state_cleanup(fimc, suspend);
 142 }
 143 
 144 /**
 145  * fimc_capture_config_update - apply the camera interface configuration
 146  * @ctx: FIMC capture context
 147  *
 148  * To be called from within the interrupt handler with fimc.slock
 149  * spinlock held. It updates the camera pixel crop, rotation and
 150  * image flip in H/W.
 151  */
 152 static int fimc_capture_config_update(struct fimc_ctx *ctx)
 153 {
 154         struct fimc_dev *fimc = ctx->fimc_dev;
 155         int ret;
 156 
 157         fimc_hw_set_camera_offset(fimc, &ctx->s_frame);
 158 
 159         ret = fimc_set_scaler_info(ctx);
 160         if (ret)
 161                 return ret;
 162 
 163         fimc_hw_set_prescaler(ctx);
 164         fimc_hw_set_mainscaler(ctx);
 165         fimc_hw_set_target_format(ctx);
 166         fimc_hw_set_rotation(ctx);
 167         fimc_hw_set_effect(ctx);
 168         fimc_prepare_dma_offset(ctx, &ctx->d_frame);
 169         fimc_hw_set_out_dma(ctx);
 170         if (fimc->drv_data->alpha_color)
 171                 fimc_hw_set_rgb_alpha(ctx);
 172 
 173         clear_bit(ST_CAPT_APPLY_CFG, &fimc->state);
 174         return ret;
 175 }
 176 
 177 void fimc_capture_irq_handler(struct fimc_dev *fimc, int deq_buf)
 178 {
 179         struct fimc_vid_cap *cap = &fimc->vid_cap;
 180         struct fimc_pipeline *p = to_fimc_pipeline(cap->ve.pipe);
 181         struct v4l2_subdev *csis = p->subdevs[IDX_CSIS];
 182         struct fimc_frame *f = &cap->ctx->d_frame;
 183         struct fimc_vid_buffer *v_buf;
 184 
 185         if (test_and_clear_bit(ST_CAPT_SHUT, &fimc->state)) {
 186                 wake_up(&fimc->irq_queue);
 187                 goto done;
 188         }
 189 
 190         if (!list_empty(&cap->active_buf_q) &&
 191             test_bit(ST_CAPT_RUN, &fimc->state) && deq_buf) {
 192                 v_buf = fimc_active_queue_pop(cap);
 193 
 194                 v_buf->vb.vb2_buf.timestamp = ktime_get_ns();
 195                 v_buf->vb.sequence = cap->frame_count++;
 196 
 197                 vb2_buffer_done(&v_buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
 198         }
 199 
 200         if (!list_empty(&cap->pending_buf_q)) {
 201 
 202                 v_buf = fimc_pending_queue_pop(cap);
 203                 fimc_hw_set_output_addr(fimc, &v_buf->paddr, cap->buf_index);
 204                 v_buf->index = cap->buf_index;
 205 
 206                 /* Move the buffer to the capture active queue */
 207                 fimc_active_queue_add(cap, v_buf);
 208 
 209                 dbg("next frame: %d, done frame: %d",
 210                     fimc_hw_get_frame_index(fimc), v_buf->index);
 211 
 212                 if (++cap->buf_index >= FIMC_MAX_OUT_BUFS)
 213                         cap->buf_index = 0;
 214         }
 215         /*
 216          * Set up a buffer at MIPI-CSIS if current image format
 217          * requires the frame embedded data capture.
 218          */
 219         if (f->fmt->mdataplanes && !list_empty(&cap->active_buf_q)) {
 220                 unsigned int plane = ffs(f->fmt->mdataplanes) - 1;
 221                 unsigned int size = f->payload[plane];
 222                 s32 index = fimc_hw_get_frame_index(fimc);
 223                 void *vaddr;
 224 
 225                 list_for_each_entry(v_buf, &cap->active_buf_q, list) {
 226                         if (v_buf->index != index)
 227                                 continue;
 228                         vaddr = vb2_plane_vaddr(&v_buf->vb.vb2_buf, plane);
 229                         v4l2_subdev_call(csis, video, s_rx_buffer,
 230                                          vaddr, &size);
 231                         break;
 232                 }
 233         }
 234 
 235         if (cap->active_buf_cnt == 0) {
 236                 if (deq_buf)
 237                         clear_bit(ST_CAPT_RUN, &fimc->state);
 238 
 239                 if (++cap->buf_index >= FIMC_MAX_OUT_BUFS)
 240                         cap->buf_index = 0;
 241         } else {
 242                 set_bit(ST_CAPT_RUN, &fimc->state);
 243         }
 244 
 245         if (test_bit(ST_CAPT_APPLY_CFG, &fimc->state))
 246                 fimc_capture_config_update(cap->ctx);
 247 done:
 248         if (cap->active_buf_cnt == 1) {
 249                 fimc_deactivate_capture(fimc);
 250                 clear_bit(ST_CAPT_STREAM, &fimc->state);
 251         }
 252 
 253         dbg("frame: %d, active_buf_cnt: %d",
 254             fimc_hw_get_frame_index(fimc), cap->active_buf_cnt);
 255 }
 256 
 257 
 258 static int start_streaming(struct vb2_queue *q, unsigned int count)
 259 {
 260         struct fimc_ctx *ctx = q->drv_priv;
 261         struct fimc_dev *fimc = ctx->fimc_dev;
 262         struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
 263         int min_bufs;
 264         int ret;
 265 
 266         vid_cap->frame_count = 0;
 267 
 268         ret = fimc_capture_hw_init(fimc);
 269         if (ret) {
 270                 fimc_capture_state_cleanup(fimc, false);
 271                 return ret;
 272         }
 273 
 274         set_bit(ST_CAPT_PEND, &fimc->state);
 275 
 276         min_bufs = fimc->vid_cap.reqbufs_count > 1 ? 2 : 1;
 277 
 278         if (vid_cap->active_buf_cnt >= min_bufs &&
 279             !test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) {
 280                 fimc_activate_capture(ctx);
 281 
 282                 if (!test_and_set_bit(ST_CAPT_ISP_STREAM, &fimc->state))
 283                         return fimc_pipeline_call(&vid_cap->ve, set_stream, 1);
 284         }
 285 
 286         return 0;
 287 }
 288 
 289 static void stop_streaming(struct vb2_queue *q)
 290 {
 291         struct fimc_ctx *ctx = q->drv_priv;
 292         struct fimc_dev *fimc = ctx->fimc_dev;
 293 
 294         if (!fimc_capture_active(fimc))
 295                 return;
 296 
 297         fimc_stop_capture(fimc, false);
 298 }
 299 
 300 int fimc_capture_suspend(struct fimc_dev *fimc)
 301 {
 302         bool suspend = fimc_capture_busy(fimc);
 303 
 304         int ret = fimc_stop_capture(fimc, suspend);
 305         if (ret)
 306                 return ret;
 307         return fimc_pipeline_call(&fimc->vid_cap.ve, close);
 308 }
 309 
 310 static void buffer_queue(struct vb2_buffer *vb);
 311 
 312 int fimc_capture_resume(struct fimc_dev *fimc)
 313 {
 314         struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
 315         struct exynos_video_entity *ve = &vid_cap->ve;
 316         struct fimc_vid_buffer *buf;
 317         int i;
 318 
 319         if (!test_and_clear_bit(ST_CAPT_SUSPENDED, &fimc->state))
 320                 return 0;
 321 
 322         INIT_LIST_HEAD(&fimc->vid_cap.active_buf_q);
 323         vid_cap->buf_index = 0;
 324         fimc_pipeline_call(ve, open, &ve->vdev.entity, false);
 325         fimc_capture_hw_init(fimc);
 326 
 327         clear_bit(ST_CAPT_SUSPENDED, &fimc->state);
 328 
 329         for (i = 0; i < vid_cap->reqbufs_count; i++) {
 330                 if (list_empty(&vid_cap->pending_buf_q))
 331                         break;
 332                 buf = fimc_pending_queue_pop(vid_cap);
 333                 buffer_queue(&buf->vb.vb2_buf);
 334         }
 335         return 0;
 336 
 337 }
 338 
 339 static int queue_setup(struct vb2_queue *vq,
 340                        unsigned int *num_buffers, unsigned int *num_planes,
 341                        unsigned int sizes[], struct device *alloc_devs[])
 342 {
 343         struct fimc_ctx *ctx = vq->drv_priv;
 344         struct fimc_frame *frame = &ctx->d_frame;
 345         struct fimc_fmt *fmt = frame->fmt;
 346         unsigned long wh = frame->f_width * frame->f_height;
 347         int i;
 348 
 349         if (fmt == NULL)
 350                 return -EINVAL;
 351 
 352         if (*num_planes) {
 353                 if (*num_planes != fmt->memplanes)
 354                         return -EINVAL;
 355                 for (i = 0; i < *num_planes; i++)
 356                         if (sizes[i] < (wh * fmt->depth[i]) / 8)
 357                                 return -EINVAL;
 358                 return 0;
 359         }
 360 
 361         *num_planes = fmt->memplanes;
 362 
 363         for (i = 0; i < fmt->memplanes; i++) {
 364                 unsigned int size = (wh * fmt->depth[i]) / 8;
 365 
 366                 if (fimc_fmt_is_user_defined(fmt->color))
 367                         sizes[i] = frame->payload[i];
 368                 else
 369                         sizes[i] = max_t(u32, size, frame->payload[i]);
 370         }
 371 
 372         return 0;
 373 }
 374 
 375 static int buffer_prepare(struct vb2_buffer *vb)
 376 {
 377         struct vb2_queue *vq = vb->vb2_queue;
 378         struct fimc_ctx *ctx = vq->drv_priv;
 379         int i;
 380 
 381         if (ctx->d_frame.fmt == NULL)
 382                 return -EINVAL;
 383 
 384         for (i = 0; i < ctx->d_frame.fmt->memplanes; i++) {
 385                 unsigned long size = ctx->d_frame.payload[i];
 386 
 387                 if (vb2_plane_size(vb, i) < size) {
 388                         v4l2_err(&ctx->fimc_dev->vid_cap.ve.vdev,
 389                                  "User buffer too small (%ld < %ld)\n",
 390                                  vb2_plane_size(vb, i), size);
 391                         return -EINVAL;
 392                 }
 393                 vb2_set_plane_payload(vb, i, size);
 394         }
 395 
 396         return 0;
 397 }
 398 
 399 static void buffer_queue(struct vb2_buffer *vb)
 400 {
 401         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
 402         struct fimc_vid_buffer *buf
 403                 = container_of(vbuf, struct fimc_vid_buffer, vb);
 404         struct fimc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
 405         struct fimc_dev *fimc = ctx->fimc_dev;
 406         struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
 407         struct exynos_video_entity *ve = &vid_cap->ve;
 408         unsigned long flags;
 409         int min_bufs;
 410 
 411         spin_lock_irqsave(&fimc->slock, flags);
 412         fimc_prepare_addr(ctx, &buf->vb.vb2_buf, &ctx->d_frame, &buf->paddr);
 413 
 414         if (!test_bit(ST_CAPT_SUSPENDED, &fimc->state) &&
 415             !test_bit(ST_CAPT_STREAM, &fimc->state) &&
 416             vid_cap->active_buf_cnt < FIMC_MAX_OUT_BUFS) {
 417                 /* Setup the buffer directly for processing. */
 418                 int buf_id = (vid_cap->reqbufs_count == 1) ? -1 :
 419                                 vid_cap->buf_index;
 420 
 421                 fimc_hw_set_output_addr(fimc, &buf->paddr, buf_id);
 422                 buf->index = vid_cap->buf_index;
 423                 fimc_active_queue_add(vid_cap, buf);
 424 
 425                 if (++vid_cap->buf_index >= FIMC_MAX_OUT_BUFS)
 426                         vid_cap->buf_index = 0;
 427         } else {
 428                 fimc_pending_queue_add(vid_cap, buf);
 429         }
 430 
 431         min_bufs = vid_cap->reqbufs_count > 1 ? 2 : 1;
 432 
 433 
 434         if (vb2_is_streaming(&vid_cap->vbq) &&
 435             vid_cap->active_buf_cnt >= min_bufs &&
 436             !test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) {
 437                 int ret;
 438 
 439                 fimc_activate_capture(ctx);
 440                 spin_unlock_irqrestore(&fimc->slock, flags);
 441 
 442                 if (test_and_set_bit(ST_CAPT_ISP_STREAM, &fimc->state))
 443                         return;
 444 
 445                 ret = fimc_pipeline_call(ve, set_stream, 1);
 446                 if (ret < 0)
 447                         v4l2_err(&ve->vdev, "stream on failed: %d\n", ret);
 448                 return;
 449         }
 450         spin_unlock_irqrestore(&fimc->slock, flags);
 451 }
 452 
 453 static const struct vb2_ops fimc_capture_qops = {
 454         .queue_setup            = queue_setup,
 455         .buf_prepare            = buffer_prepare,
 456         .buf_queue              = buffer_queue,
 457         .wait_prepare           = vb2_ops_wait_prepare,
 458         .wait_finish            = vb2_ops_wait_finish,
 459         .start_streaming        = start_streaming,
 460         .stop_streaming         = stop_streaming,
 461 };
 462 
 463 static int fimc_capture_set_default_format(struct fimc_dev *fimc);
 464 
 465 static int fimc_capture_open(struct file *file)
 466 {
 467         struct fimc_dev *fimc = video_drvdata(file);
 468         struct fimc_vid_cap *vc = &fimc->vid_cap;
 469         struct exynos_video_entity *ve = &vc->ve;
 470         int ret = -EBUSY;
 471 
 472         dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
 473 
 474         mutex_lock(&fimc->lock);
 475 
 476         if (fimc_m2m_active(fimc))
 477                 goto unlock;
 478 
 479         set_bit(ST_CAPT_BUSY, &fimc->state);
 480         ret = pm_runtime_get_sync(&fimc->pdev->dev);
 481         if (ret < 0)
 482                 goto unlock;
 483 
 484         ret = v4l2_fh_open(file);
 485         if (ret) {
 486                 pm_runtime_put_sync(&fimc->pdev->dev);
 487                 goto unlock;
 488         }
 489 
 490         if (v4l2_fh_is_singular_file(file)) {
 491                 fimc_md_graph_lock(ve);
 492 
 493                 ret = fimc_pipeline_call(ve, open, &ve->vdev.entity, true);
 494 
 495                 if (ret == 0 && vc->user_subdev_api && vc->inh_sensor_ctrls) {
 496                         /*
 497                          * Recreate controls of the the video node to drop
 498                          * any controls inherited from the sensor subdev.
 499                          */
 500                         fimc_ctrls_delete(vc->ctx);
 501 
 502                         ret = fimc_ctrls_create(vc->ctx);
 503                         if (ret == 0)
 504                                 vc->inh_sensor_ctrls = false;
 505                 }
 506                 if (ret == 0)
 507                         ve->vdev.entity.use_count++;
 508 
 509                 fimc_md_graph_unlock(ve);
 510 
 511                 if (ret == 0)
 512                         ret = fimc_capture_set_default_format(fimc);
 513 
 514                 if (ret < 0) {
 515                         clear_bit(ST_CAPT_BUSY, &fimc->state);
 516                         pm_runtime_put_sync(&fimc->pdev->dev);
 517                         v4l2_fh_release(file);
 518                 }
 519         }
 520 unlock:
 521         mutex_unlock(&fimc->lock);
 522         return ret;
 523 }
 524 
 525 static int fimc_capture_release(struct file *file)
 526 {
 527         struct fimc_dev *fimc = video_drvdata(file);
 528         struct fimc_vid_cap *vc = &fimc->vid_cap;
 529         bool close = v4l2_fh_is_singular_file(file);
 530         int ret;
 531 
 532         dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
 533 
 534         mutex_lock(&fimc->lock);
 535 
 536         if (close && vc->streaming) {
 537                 media_pipeline_stop(&vc->ve.vdev.entity);
 538                 vc->streaming = false;
 539         }
 540 
 541         ret = _vb2_fop_release(file, NULL);
 542 
 543         if (close) {
 544                 clear_bit(ST_CAPT_BUSY, &fimc->state);
 545                 fimc_pipeline_call(&vc->ve, close);
 546                 clear_bit(ST_CAPT_SUSPENDED, &fimc->state);
 547 
 548                 fimc_md_graph_lock(&vc->ve);
 549                 vc->ve.vdev.entity.use_count--;
 550                 fimc_md_graph_unlock(&vc->ve);
 551         }
 552 
 553         pm_runtime_put_sync(&fimc->pdev->dev);
 554         mutex_unlock(&fimc->lock);
 555 
 556         return ret;
 557 }
 558 
 559 static const struct v4l2_file_operations fimc_capture_fops = {
 560         .owner          = THIS_MODULE,
 561         .open           = fimc_capture_open,
 562         .release        = fimc_capture_release,
 563         .poll           = vb2_fop_poll,
 564         .unlocked_ioctl = video_ioctl2,
 565         .mmap           = vb2_fop_mmap,
 566 };
 567 
 568 /*
 569  * Format and crop negotiation helpers
 570  */
 571 
 572 static struct fimc_fmt *fimc_capture_try_format(struct fimc_ctx *ctx,
 573                                                 u32 *width, u32 *height,
 574                                                 u32 *code, u32 *fourcc, int pad)
 575 {
 576         bool rotation = ctx->rotation == 90 || ctx->rotation == 270;
 577         struct fimc_dev *fimc = ctx->fimc_dev;
 578         const struct fimc_variant *var = fimc->variant;
 579         const struct fimc_pix_limit *pl = var->pix_limit;
 580         struct fimc_frame *dst = &ctx->d_frame;
 581         u32 depth, min_w, max_w, min_h, align_h = 3;
 582         u32 mask = FMT_FLAGS_CAM;
 583         struct fimc_fmt *ffmt;
 584 
 585         /* Conversion from/to JPEG or User Defined format is not supported */
 586         if (code && ctx->s_frame.fmt && pad == FIMC_SD_PAD_SOURCE &&
 587             fimc_fmt_is_user_defined(ctx->s_frame.fmt->color))
 588                 *code = ctx->s_frame.fmt->mbus_code;
 589 
 590         if (fourcc && *fourcc != V4L2_PIX_FMT_JPEG && pad == FIMC_SD_PAD_SOURCE)
 591                 mask |= FMT_FLAGS_M2M;
 592 
 593         if (pad == FIMC_SD_PAD_SINK_FIFO)
 594                 mask = FMT_FLAGS_WRITEBACK;
 595 
 596         ffmt = fimc_find_format(fourcc, code, mask, 0);
 597         if (WARN_ON(!ffmt))
 598                 return NULL;
 599 
 600         if (code)
 601                 *code = ffmt->mbus_code;
 602         if (fourcc)
 603                 *fourcc = ffmt->fourcc;
 604 
 605         if (pad != FIMC_SD_PAD_SOURCE) {
 606                 max_w = fimc_fmt_is_user_defined(ffmt->color) ?
 607                         pl->scaler_dis_w : pl->scaler_en_w;
 608                 /* Apply the camera input interface pixel constraints */
 609                 v4l_bound_align_image(width, max_t(u32, *width, 32), max_w, 4,
 610                                       height, max_t(u32, *height, 32),
 611                                       FIMC_CAMIF_MAX_HEIGHT,
 612                                       fimc_fmt_is_user_defined(ffmt->color) ?
 613                                       3 : 1,
 614                                       0);
 615                 return ffmt;
 616         }
 617         /* Can't scale or crop in transparent (JPEG) transfer mode */
 618         if (fimc_fmt_is_user_defined(ffmt->color)) {
 619                 *width  = ctx->s_frame.f_width;
 620                 *height = ctx->s_frame.f_height;
 621                 return ffmt;
 622         }
 623         /* Apply the scaler and the output DMA constraints */
 624         max_w = rotation ? pl->out_rot_en_w : pl->out_rot_dis_w;
 625         if (ctx->state & FIMC_COMPOSE) {
 626                 min_w = dst->offs_h + dst->width;
 627                 min_h = dst->offs_v + dst->height;
 628         } else {
 629                 min_w = var->min_out_pixsize;
 630                 min_h = var->min_out_pixsize;
 631         }
 632         if (var->min_vsize_align == 1 && !rotation)
 633                 align_h = fimc_fmt_is_rgb(ffmt->color) ? 0 : 1;
 634 
 635         depth = fimc_get_format_depth(ffmt);
 636         v4l_bound_align_image(width, min_w, max_w,
 637                               ffs(var->min_out_pixsize) - 1,
 638                               height, min_h, FIMC_CAMIF_MAX_HEIGHT,
 639                               align_h,
 640                               64/(ALIGN(depth, 8)));
 641 
 642         dbg("pad%d: code: 0x%x, %dx%d. dst fmt: %dx%d",
 643             pad, code ? *code : 0, *width, *height,
 644             dst->f_width, dst->f_height);
 645 
 646         return ffmt;
 647 }
 648 
 649 static void fimc_capture_try_selection(struct fimc_ctx *ctx,
 650                                        struct v4l2_rect *r,
 651                                        int target)
 652 {
 653         bool rotate = ctx->rotation == 90 || ctx->rotation == 270;
 654         struct fimc_dev *fimc = ctx->fimc_dev;
 655         const struct fimc_variant *var = fimc->variant;
 656         const struct fimc_pix_limit *pl = var->pix_limit;
 657         struct fimc_frame *sink = &ctx->s_frame;
 658         u32 max_w, max_h, min_w = 0, min_h = 0, min_sz;
 659         u32 align_sz = 0, align_h = 4;
 660         u32 max_sc_h, max_sc_v;
 661 
 662         /* In JPEG transparent transfer mode cropping is not supported */
 663         if (fimc_fmt_is_user_defined(ctx->d_frame.fmt->color)) {
 664                 r->width  = sink->f_width;
 665                 r->height = sink->f_height;
 666                 r->left   = r->top = 0;
 667                 return;
 668         }
 669         if (target == V4L2_SEL_TGT_COMPOSE) {
 670                 u32 tmp_min_h = ffs(sink->width) - 3;
 671                 u32 tmp_min_v = ffs(sink->height) - 1;
 672 
 673                 if (ctx->rotation != 90 && ctx->rotation != 270)
 674                         align_h = 1;
 675                 max_sc_h = min(SCALER_MAX_HRATIO, 1 << tmp_min_h);
 676                 max_sc_v = min(SCALER_MAX_VRATIO, 1 << tmp_min_v);
 677                 min_sz = var->min_out_pixsize;
 678         } else {
 679                 u32 depth = fimc_get_format_depth(sink->fmt);
 680                 align_sz = 64/ALIGN(depth, 8);
 681                 min_sz = var->min_inp_pixsize;
 682                 min_w = min_h = min_sz;
 683                 max_sc_h = max_sc_v = 1;
 684         }
 685         /*
 686          * For the compose rectangle the following constraints must be met:
 687          * - it must fit in the sink pad format rectangle (f_width/f_height);
 688          * - maximum downscaling ratio is 64;
 689          * - maximum crop size depends if the rotator is used or not;
 690          * - the sink pad format width/height must be 4 multiple of the
 691          *   prescaler ratios determined by sink pad size and source pad crop,
 692          *   the prescaler ratio is returned by fimc_get_scaler_factor().
 693          */
 694         max_w = min_t(u32,
 695                       rotate ? pl->out_rot_en_w : pl->out_rot_dis_w,
 696                       rotate ? sink->f_height : sink->f_width);
 697         max_h = min_t(u32, FIMC_CAMIF_MAX_HEIGHT, sink->f_height);
 698 
 699         if (target == V4L2_SEL_TGT_COMPOSE) {
 700                 min_w = min_t(u32, max_w, sink->f_width / max_sc_h);
 701                 min_h = min_t(u32, max_h, sink->f_height / max_sc_v);
 702                 if (rotate) {
 703                         swap(max_sc_h, max_sc_v);
 704                         swap(min_w, min_h);
 705                 }
 706         }
 707         v4l_bound_align_image(&r->width, min_w, max_w, ffs(min_sz) - 1,
 708                               &r->height, min_h, max_h, align_h,
 709                               align_sz);
 710         /* Adjust left/top if crop/compose rectangle is out of bounds */
 711         r->left = clamp_t(u32, r->left, 0, sink->f_width - r->width);
 712         r->top  = clamp_t(u32, r->top, 0, sink->f_height - r->height);
 713         r->left = round_down(r->left, var->hor_offs_align);
 714 
 715         dbg("target %#x: (%d,%d)/%dx%d, sink fmt: %dx%d",
 716             target, r->left, r->top, r->width, r->height,
 717             sink->f_width, sink->f_height);
 718 }
 719 
 720 /*
 721  * The video node ioctl operations
 722  */
 723 static int fimc_cap_querycap(struct file *file, void *priv,
 724                                         struct v4l2_capability *cap)
 725 {
 726         struct fimc_dev *fimc = video_drvdata(file);
 727 
 728         __fimc_vidioc_querycap(&fimc->pdev->dev, cap);
 729         return 0;
 730 }
 731 
 732 static int fimc_cap_enum_fmt(struct file *file, void *priv,
 733                              struct v4l2_fmtdesc *f)
 734 {
 735         struct fimc_fmt *fmt;
 736 
 737         fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM | FMT_FLAGS_M2M,
 738                                f->index);
 739         if (!fmt)
 740                 return -EINVAL;
 741         f->pixelformat = fmt->fourcc;
 742         return 0;
 743 }
 744 
 745 static struct media_entity *fimc_pipeline_get_head(struct media_entity *me)
 746 {
 747         struct media_pad *pad = &me->pads[0];
 748 
 749         while (!(pad->flags & MEDIA_PAD_FL_SOURCE)) {
 750                 pad = media_entity_remote_pad(pad);
 751                 if (!pad)
 752                         break;
 753                 me = pad->entity;
 754                 pad = &me->pads[0];
 755         }
 756 
 757         return me;
 758 }
 759 
 760 /**
 761  * fimc_pipeline_try_format - negotiate and/or set formats at pipeline
 762  *                            elements
 763  * @ctx: FIMC capture context
 764  * @tfmt: media bus format to try/set on subdevs
 765  * @fmt_id: fimc pixel format id corresponding to returned @tfmt (output)
 766  * @set: true to set format on subdevs, false to try only
 767  */
 768 static int fimc_pipeline_try_format(struct fimc_ctx *ctx,
 769                                     struct v4l2_mbus_framefmt *tfmt,
 770                                     struct fimc_fmt **fmt_id,
 771                                     bool set)
 772 {
 773         struct fimc_dev *fimc = ctx->fimc_dev;
 774         struct fimc_pipeline *p = to_fimc_pipeline(fimc->vid_cap.ve.pipe);
 775         struct v4l2_subdev *sd = p->subdevs[IDX_SENSOR];
 776         struct v4l2_subdev_format sfmt;
 777         struct v4l2_mbus_framefmt *mf = &sfmt.format;
 778         struct media_entity *me;
 779         struct fimc_fmt *ffmt;
 780         struct media_pad *pad;
 781         int ret, i = 1;
 782         u32 fcc;
 783 
 784         if (WARN_ON(!sd || !tfmt))
 785                 return -EINVAL;
 786 
 787         memset(&sfmt, 0, sizeof(sfmt));
 788         sfmt.format = *tfmt;
 789         sfmt.which = set ? V4L2_SUBDEV_FORMAT_ACTIVE : V4L2_SUBDEV_FORMAT_TRY;
 790 
 791         me = fimc_pipeline_get_head(&sd->entity);
 792 
 793         while (1) {
 794                 ffmt = fimc_find_format(NULL, mf->code != 0 ? &mf->code : NULL,
 795                                         FMT_FLAGS_CAM, i++);
 796                 if (ffmt == NULL) {
 797                         /*
 798                          * Notify user-space if common pixel code for
 799                          * host and sensor does not exist.
 800                          */
 801                         return -EINVAL;
 802                 }
 803                 mf->code = tfmt->code = ffmt->mbus_code;
 804 
 805                 /* set format on all pipeline subdevs */
 806                 while (me != &fimc->vid_cap.subdev.entity) {
 807                         sd = media_entity_to_v4l2_subdev(me);
 808 
 809                         sfmt.pad = 0;
 810                         ret = v4l2_subdev_call(sd, pad, set_fmt, NULL, &sfmt);
 811                         if (ret)
 812                                 return ret;
 813 
 814                         if (me->pads[0].flags & MEDIA_PAD_FL_SINK) {
 815                                 sfmt.pad = me->num_pads - 1;
 816                                 mf->code = tfmt->code;
 817                                 ret = v4l2_subdev_call(sd, pad, set_fmt, NULL,
 818                                                                         &sfmt);
 819                                 if (ret)
 820                                         return ret;
 821                         }
 822 
 823                         pad = media_entity_remote_pad(&me->pads[sfmt.pad]);
 824                         if (!pad)
 825                                 return -EINVAL;
 826                         me = pad->entity;
 827                 }
 828 
 829                 if (mf->code != tfmt->code)
 830                         continue;
 831 
 832                 fcc = ffmt->fourcc;
 833                 tfmt->width  = mf->width;
 834                 tfmt->height = mf->height;
 835                 ffmt = fimc_capture_try_format(ctx, &tfmt->width, &tfmt->height,
 836                                         NULL, &fcc, FIMC_SD_PAD_SINK_CAM);
 837                 ffmt = fimc_capture_try_format(ctx, &tfmt->width, &tfmt->height,
 838                                         NULL, &fcc, FIMC_SD_PAD_SOURCE);
 839                 if (ffmt && ffmt->mbus_code)
 840                         mf->code = ffmt->mbus_code;
 841                 if (mf->width != tfmt->width || mf->height != tfmt->height)
 842                         continue;
 843                 tfmt->code = mf->code;
 844                 break;
 845         }
 846 
 847         if (fmt_id && ffmt)
 848                 *fmt_id = ffmt;
 849         *tfmt = *mf;
 850 
 851         return 0;
 852 }
 853 
 854 /**
 855  * fimc_get_sensor_frame_desc - query the sensor for media bus frame parameters
 856  * @sensor: pointer to the sensor subdev
 857  * @plane_fmt: provides plane sizes corresponding to the frame layout entries
 858  * @num_planes: number of planes
 859  * @try: true to set the frame parameters, false to query only
 860  *
 861  * This function is used by this driver only for compressed/blob data formats.
 862  */
 863 static int fimc_get_sensor_frame_desc(struct v4l2_subdev *sensor,
 864                                       struct v4l2_plane_pix_format *plane_fmt,
 865                                       unsigned int num_planes, bool try)
 866 {
 867         struct v4l2_mbus_frame_desc fd;
 868         int i, ret;
 869         int pad;
 870 
 871         for (i = 0; i < num_planes; i++)
 872                 fd.entry[i].length = plane_fmt[i].sizeimage;
 873 
 874         pad = sensor->entity.num_pads - 1;
 875         if (try)
 876                 ret = v4l2_subdev_call(sensor, pad, set_frame_desc, pad, &fd);
 877         else
 878                 ret = v4l2_subdev_call(sensor, pad, get_frame_desc, pad, &fd);
 879 
 880         if (ret < 0)
 881                 return ret;
 882 
 883         if (num_planes != fd.num_entries)
 884                 return -EINVAL;
 885 
 886         for (i = 0; i < num_planes; i++)
 887                 plane_fmt[i].sizeimage = fd.entry[i].length;
 888 
 889         if (fd.entry[0].length > FIMC_MAX_JPEG_BUF_SIZE) {
 890                 v4l2_err(sensor->v4l2_dev,  "Unsupported buffer size: %u\n",
 891                          fd.entry[0].length);
 892 
 893                 return -EINVAL;
 894         }
 895 
 896         return 0;
 897 }
 898 
 899 static int fimc_cap_g_fmt_mplane(struct file *file, void *fh,
 900                                  struct v4l2_format *f)
 901 {
 902         struct fimc_dev *fimc = video_drvdata(file);
 903 
 904         __fimc_get_format(&fimc->vid_cap.ctx->d_frame, f);
 905         return 0;
 906 }
 907 
 908 /*
 909  * Try or set format on the fimc.X.capture video node and additionally
 910  * on the whole pipeline if @try is false.
 911  * Locking: the caller must _not_ hold the graph mutex.
 912  */
 913 static int __video_try_or_set_format(struct fimc_dev *fimc,
 914                                      struct v4l2_format *f, bool try,
 915                                      struct fimc_fmt **inp_fmt,
 916                                      struct fimc_fmt **out_fmt)
 917 {
 918         struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
 919         struct fimc_vid_cap *vc = &fimc->vid_cap;
 920         struct exynos_video_entity *ve = &vc->ve;
 921         struct fimc_ctx *ctx = vc->ctx;
 922         unsigned int width = 0, height = 0;
 923         int ret = 0;
 924 
 925         /* Pre-configure format at the camera input interface, for JPEG only */
 926         if (fimc_jpeg_fourcc(pix->pixelformat)) {
 927                 fimc_capture_try_format(ctx, &pix->width, &pix->height,
 928                                         NULL, &pix->pixelformat,
 929                                         FIMC_SD_PAD_SINK_CAM);
 930                 if (try) {
 931                         width = pix->width;
 932                         height = pix->height;
 933                 } else {
 934                         ctx->s_frame.f_width = pix->width;
 935                         ctx->s_frame.f_height = pix->height;
 936                 }
 937         }
 938 
 939         /* Try the format at the scaler and the DMA output */
 940         *out_fmt = fimc_capture_try_format(ctx, &pix->width, &pix->height,
 941                                           NULL, &pix->pixelformat,
 942                                           FIMC_SD_PAD_SOURCE);
 943         if (*out_fmt == NULL)
 944                 return -EINVAL;
 945 
 946         /* Restore image width/height for JPEG (no resizing supported). */
 947         if (try && fimc_jpeg_fourcc(pix->pixelformat)) {
 948                 pix->width = width;
 949                 pix->height = height;
 950         }
 951 
 952         /* Try to match format at the host and the sensor */
 953         if (!vc->user_subdev_api) {
 954                 struct v4l2_mbus_framefmt mbus_fmt;
 955                 struct v4l2_mbus_framefmt *mf;
 956 
 957                 mf = try ? &mbus_fmt : &fimc->vid_cap.ci_fmt;
 958 
 959                 mf->code = (*out_fmt)->mbus_code;
 960                 mf->width = pix->width;
 961                 mf->height = pix->height;
 962 
 963                 fimc_md_graph_lock(ve);
 964                 ret = fimc_pipeline_try_format(ctx, mf, inp_fmt, try);
 965                 fimc_md_graph_unlock(ve);
 966 
 967                 if (ret < 0)
 968                         return ret;
 969 
 970                 pix->width = mf->width;
 971                 pix->height = mf->height;
 972         }
 973 
 974         fimc_adjust_mplane_format(*out_fmt, pix->width, pix->height, pix);
 975 
 976         if ((*out_fmt)->flags & FMT_FLAGS_COMPRESSED) {
 977                 struct v4l2_subdev *sensor;
 978 
 979                 fimc_md_graph_lock(ve);
 980 
 981                 sensor = __fimc_md_get_subdev(ve->pipe, IDX_SENSOR);
 982                 if (sensor)
 983                         fimc_get_sensor_frame_desc(sensor, pix->plane_fmt,
 984                                                    (*out_fmt)->memplanes, try);
 985                 else
 986                         ret = -EPIPE;
 987 
 988                 fimc_md_graph_unlock(ve);
 989         }
 990 
 991         return ret;
 992 }
 993 
 994 static int fimc_cap_try_fmt_mplane(struct file *file, void *fh,
 995                                    struct v4l2_format *f)
 996 {
 997         struct fimc_dev *fimc = video_drvdata(file);
 998         struct fimc_fmt *out_fmt = NULL, *inp_fmt = NULL;
 999 
1000         return __video_try_or_set_format(fimc, f, true, &inp_fmt, &out_fmt);
1001 }
1002 
1003 static void fimc_capture_mark_jpeg_xfer(struct fimc_ctx *ctx,
1004                                         enum fimc_color_fmt color)
1005 {
1006         bool jpeg = fimc_fmt_is_user_defined(color);
1007 
1008         ctx->scaler.enabled = !jpeg;
1009         fimc_ctrls_activate(ctx, !jpeg);
1010 
1011         if (jpeg)
1012                 set_bit(ST_CAPT_JPEG, &ctx->fimc_dev->state);
1013         else
1014                 clear_bit(ST_CAPT_JPEG, &ctx->fimc_dev->state);
1015 }
1016 
1017 static int __fimc_capture_set_format(struct fimc_dev *fimc,
1018                                      struct v4l2_format *f)
1019 {
1020         struct fimc_vid_cap *vc = &fimc->vid_cap;
1021         struct fimc_ctx *ctx = vc->ctx;
1022         struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
1023         struct fimc_frame *ff = &ctx->d_frame;
1024         struct fimc_fmt *inp_fmt = NULL;
1025         int ret, i;
1026 
1027         if (vb2_is_busy(&fimc->vid_cap.vbq))
1028                 return -EBUSY;
1029 
1030         ret = __video_try_or_set_format(fimc, f, false, &inp_fmt, &ff->fmt);
1031         if (ret < 0)
1032                 return ret;
1033 
1034         /* Update RGB Alpha control state and value range */
1035         fimc_alpha_ctrl_update(ctx);
1036 
1037         for (i = 0; i < ff->fmt->memplanes; i++) {
1038                 ff->bytesperline[i] = pix->plane_fmt[i].bytesperline;
1039                 ff->payload[i] = pix->plane_fmt[i].sizeimage;
1040         }
1041 
1042         set_frame_bounds(ff, pix->width, pix->height);
1043         /* Reset the composition rectangle if not yet configured */
1044         if (!(ctx->state & FIMC_COMPOSE))
1045                 set_frame_crop(ff, 0, 0, pix->width, pix->height);
1046 
1047         fimc_capture_mark_jpeg_xfer(ctx, ff->fmt->color);
1048 
1049         /* Reset cropping and set format at the camera interface input */
1050         if (!vc->user_subdev_api) {
1051                 ctx->s_frame.fmt = inp_fmt;
1052                 set_frame_bounds(&ctx->s_frame, pix->width, pix->height);
1053                 set_frame_crop(&ctx->s_frame, 0, 0, pix->width, pix->height);
1054         }
1055 
1056         return ret;
1057 }
1058 
1059 static int fimc_cap_s_fmt_mplane(struct file *file, void *priv,
1060                                  struct v4l2_format *f)
1061 {
1062         struct fimc_dev *fimc = video_drvdata(file);
1063 
1064         return __fimc_capture_set_format(fimc, f);
1065 }
1066 
1067 static int fimc_cap_enum_input(struct file *file, void *priv,
1068                                struct v4l2_input *i)
1069 {
1070         struct fimc_dev *fimc = video_drvdata(file);
1071         struct exynos_video_entity *ve = &fimc->vid_cap.ve;
1072         struct v4l2_subdev *sd;
1073 
1074         if (i->index != 0)
1075                 return -EINVAL;
1076 
1077         i->type = V4L2_INPUT_TYPE_CAMERA;
1078         fimc_md_graph_lock(ve);
1079         sd = __fimc_md_get_subdev(ve->pipe, IDX_SENSOR);
1080         fimc_md_graph_unlock(ve);
1081 
1082         if (sd)
1083                 strscpy(i->name, sd->name, sizeof(i->name));
1084 
1085         return 0;
1086 }
1087 
1088 static int fimc_cap_s_input(struct file *file, void *priv, unsigned int i)
1089 {
1090         return i == 0 ? i : -EINVAL;
1091 }
1092 
1093 static int fimc_cap_g_input(struct file *file, void *priv, unsigned int *i)
1094 {
1095         *i = 0;
1096         return 0;
1097 }
1098 
1099 /**
1100  * fimc_pipeline_validate - check for formats inconsistencies
1101  *                          between source and sink pad of each link
1102  * @fimc:       the FIMC device this context applies to
1103  *
1104  * Return 0 if all formats match or -EPIPE otherwise.
1105  */
1106 static int fimc_pipeline_validate(struct fimc_dev *fimc)
1107 {
1108         struct v4l2_subdev_format sink_fmt, src_fmt;
1109         struct fimc_vid_cap *vc = &fimc->vid_cap;
1110         struct v4l2_subdev *sd = &vc->subdev;
1111         struct fimc_pipeline *p = to_fimc_pipeline(vc->ve.pipe);
1112         struct media_pad *sink_pad, *src_pad;
1113         int i, ret;
1114 
1115         while (1) {
1116                 /*
1117                  * Find current entity sink pad and any remote sink pad linked
1118                  * to it. We stop if there is no sink pad in current entity or
1119                  * it is not linked to any other remote entity.
1120                  */
1121                 src_pad = NULL;
1122 
1123                 for (i = 0; i < sd->entity.num_pads; i++) {
1124                         struct media_pad *p = &sd->entity.pads[i];
1125 
1126                         if (p->flags & MEDIA_PAD_FL_SINK) {
1127                                 sink_pad = p;
1128                                 src_pad = media_entity_remote_pad(sink_pad);
1129                                 if (src_pad)
1130                                         break;
1131                         }
1132                 }
1133 
1134                 if (!src_pad || !is_media_entity_v4l2_subdev(src_pad->entity))
1135                         break;
1136 
1137                 /* Don't call FIMC subdev operation to avoid nested locking */
1138                 if (sd == &vc->subdev) {
1139                         struct fimc_frame *ff = &vc->ctx->s_frame;
1140                         sink_fmt.format.width = ff->f_width;
1141                         sink_fmt.format.height = ff->f_height;
1142                         sink_fmt.format.code = ff->fmt ? ff->fmt->mbus_code : 0;
1143                 } else {
1144                         sink_fmt.pad = sink_pad->index;
1145                         sink_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1146                         ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &sink_fmt);
1147                         if (ret < 0 && ret != -ENOIOCTLCMD)
1148                                 return -EPIPE;
1149                 }
1150 
1151                 /* Retrieve format at the source pad */
1152                 sd = media_entity_to_v4l2_subdev(src_pad->entity);
1153                 src_fmt.pad = src_pad->index;
1154                 src_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1155                 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &src_fmt);
1156                 if (ret < 0 && ret != -ENOIOCTLCMD)
1157                         return -EPIPE;
1158 
1159                 if (src_fmt.format.width != sink_fmt.format.width ||
1160                     src_fmt.format.height != sink_fmt.format.height ||
1161                     src_fmt.format.code != sink_fmt.format.code)
1162                         return -EPIPE;
1163 
1164                 if (sd == p->subdevs[IDX_SENSOR] &&
1165                     fimc_user_defined_mbus_fmt(src_fmt.format.code)) {
1166                         struct v4l2_plane_pix_format plane_fmt[FIMC_MAX_PLANES];
1167                         struct fimc_frame *frame = &vc->ctx->d_frame;
1168                         unsigned int i;
1169 
1170                         ret = fimc_get_sensor_frame_desc(sd, plane_fmt,
1171                                                          frame->fmt->memplanes,
1172                                                          false);
1173                         if (ret < 0)
1174                                 return -EPIPE;
1175 
1176                         for (i = 0; i < frame->fmt->memplanes; i++)
1177                                 if (frame->payload[i] < plane_fmt[i].sizeimage)
1178                                         return -EPIPE;
1179                 }
1180         }
1181         return 0;
1182 }
1183 
1184 static int fimc_cap_streamon(struct file *file, void *priv,
1185                              enum v4l2_buf_type type)
1186 {
1187         struct fimc_dev *fimc = video_drvdata(file);
1188         struct fimc_vid_cap *vc = &fimc->vid_cap;
1189         struct media_entity *entity = &vc->ve.vdev.entity;
1190         struct fimc_source_info *si = NULL;
1191         struct v4l2_subdev *sd;
1192         int ret;
1193 
1194         if (fimc_capture_active(fimc))
1195                 return -EBUSY;
1196 
1197         ret = media_pipeline_start(entity, &vc->ve.pipe->mp);
1198         if (ret < 0)
1199                 return ret;
1200 
1201         sd = __fimc_md_get_subdev(vc->ve.pipe, IDX_SENSOR);
1202         if (sd)
1203                 si = v4l2_get_subdev_hostdata(sd);
1204 
1205         if (si == NULL) {
1206                 ret = -EPIPE;
1207                 goto err_p_stop;
1208         }
1209         /*
1210          * Save configuration data related to currently attached image
1211          * sensor or other data source, e.g. FIMC-IS.
1212          */
1213         vc->source_config = *si;
1214 
1215         if (vc->input == GRP_ID_FIMC_IS)
1216                 vc->source_config.fimc_bus_type = FIMC_BUS_TYPE_ISP_WRITEBACK;
1217 
1218         if (vc->user_subdev_api) {
1219                 ret = fimc_pipeline_validate(fimc);
1220                 if (ret < 0)
1221                         goto err_p_stop;
1222         }
1223 
1224         ret = vb2_ioctl_streamon(file, priv, type);
1225         if (!ret) {
1226                 vc->streaming = true;
1227                 return ret;
1228         }
1229 
1230 err_p_stop:
1231         media_pipeline_stop(entity);
1232         return ret;
1233 }
1234 
1235 static int fimc_cap_streamoff(struct file *file, void *priv,
1236                             enum v4l2_buf_type type)
1237 {
1238         struct fimc_dev *fimc = video_drvdata(file);
1239         struct fimc_vid_cap *vc = &fimc->vid_cap;
1240         int ret;
1241 
1242         ret = vb2_ioctl_streamoff(file, priv, type);
1243         if (ret < 0)
1244                 return ret;
1245 
1246         media_pipeline_stop(&vc->ve.vdev.entity);
1247         vc->streaming = false;
1248         return 0;
1249 }
1250 
1251 static int fimc_cap_reqbufs(struct file *file, void *priv,
1252                             struct v4l2_requestbuffers *reqbufs)
1253 {
1254         struct fimc_dev *fimc = video_drvdata(file);
1255         int ret;
1256 
1257         ret = vb2_ioctl_reqbufs(file, priv, reqbufs);
1258 
1259         if (!ret)
1260                 fimc->vid_cap.reqbufs_count = reqbufs->count;
1261 
1262         return ret;
1263 }
1264 
1265 static int fimc_cap_g_selection(struct file *file, void *fh,
1266                                 struct v4l2_selection *s)
1267 {
1268         struct fimc_dev *fimc = video_drvdata(file);
1269         struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1270         struct fimc_frame *f = &ctx->s_frame;
1271 
1272         if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1273                 return -EINVAL;
1274 
1275         switch (s->target) {
1276         case V4L2_SEL_TGT_COMPOSE_DEFAULT:
1277         case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1278                 f = &ctx->d_frame;
1279                 /* fall through */
1280         case V4L2_SEL_TGT_CROP_BOUNDS:
1281         case V4L2_SEL_TGT_CROP_DEFAULT:
1282                 s->r.left = 0;
1283                 s->r.top = 0;
1284                 s->r.width = f->o_width;
1285                 s->r.height = f->o_height;
1286                 return 0;
1287 
1288         case V4L2_SEL_TGT_COMPOSE:
1289                 f = &ctx->d_frame;
1290                 /* fall through */
1291         case V4L2_SEL_TGT_CROP:
1292                 s->r.left = f->offs_h;
1293                 s->r.top = f->offs_v;
1294                 s->r.width = f->width;
1295                 s->r.height = f->height;
1296                 return 0;
1297         }
1298 
1299         return -EINVAL;
1300 }
1301 
1302 /* Return 1 if rectangle a is enclosed in rectangle b, or 0 otherwise. */
1303 static int enclosed_rectangle(struct v4l2_rect *a, struct v4l2_rect *b)
1304 {
1305         if (a->left < b->left || a->top < b->top)
1306                 return 0;
1307         if (a->left + a->width > b->left + b->width)
1308                 return 0;
1309         if (a->top + a->height > b->top + b->height)
1310                 return 0;
1311 
1312         return 1;
1313 }
1314 
1315 static int fimc_cap_s_selection(struct file *file, void *fh,
1316                                 struct v4l2_selection *s)
1317 {
1318         struct fimc_dev *fimc = video_drvdata(file);
1319         struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1320         struct v4l2_rect rect = s->r;
1321         struct fimc_frame *f;
1322         unsigned long flags;
1323 
1324         if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1325                 return -EINVAL;
1326 
1327         if (s->target == V4L2_SEL_TGT_COMPOSE)
1328                 f = &ctx->d_frame;
1329         else if (s->target == V4L2_SEL_TGT_CROP)
1330                 f = &ctx->s_frame;
1331         else
1332                 return -EINVAL;
1333 
1334         fimc_capture_try_selection(ctx, &rect, s->target);
1335 
1336         if (s->flags & V4L2_SEL_FLAG_LE &&
1337             !enclosed_rectangle(&rect, &s->r))
1338                 return -ERANGE;
1339 
1340         if (s->flags & V4L2_SEL_FLAG_GE &&
1341             !enclosed_rectangle(&s->r, &rect))
1342                 return -ERANGE;
1343 
1344         s->r = rect;
1345         spin_lock_irqsave(&fimc->slock, flags);
1346         set_frame_crop(f, s->r.left, s->r.top, s->r.width,
1347                        s->r.height);
1348         spin_unlock_irqrestore(&fimc->slock, flags);
1349 
1350         set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
1351         return 0;
1352 }
1353 
1354 static const struct v4l2_ioctl_ops fimc_capture_ioctl_ops = {
1355         .vidioc_querycap                = fimc_cap_querycap,
1356 
1357         .vidioc_enum_fmt_vid_cap        = fimc_cap_enum_fmt,
1358         .vidioc_try_fmt_vid_cap_mplane  = fimc_cap_try_fmt_mplane,
1359         .vidioc_s_fmt_vid_cap_mplane    = fimc_cap_s_fmt_mplane,
1360         .vidioc_g_fmt_vid_cap_mplane    = fimc_cap_g_fmt_mplane,
1361 
1362         .vidioc_reqbufs                 = fimc_cap_reqbufs,
1363         .vidioc_querybuf                = vb2_ioctl_querybuf,
1364         .vidioc_qbuf                    = vb2_ioctl_qbuf,
1365         .vidioc_dqbuf                   = vb2_ioctl_dqbuf,
1366         .vidioc_expbuf                  = vb2_ioctl_expbuf,
1367         .vidioc_prepare_buf             = vb2_ioctl_prepare_buf,
1368         .vidioc_create_bufs             = vb2_ioctl_create_bufs,
1369 
1370         .vidioc_streamon                = fimc_cap_streamon,
1371         .vidioc_streamoff               = fimc_cap_streamoff,
1372 
1373         .vidioc_g_selection             = fimc_cap_g_selection,
1374         .vidioc_s_selection             = fimc_cap_s_selection,
1375 
1376         .vidioc_enum_input              = fimc_cap_enum_input,
1377         .vidioc_s_input                 = fimc_cap_s_input,
1378         .vidioc_g_input                 = fimc_cap_g_input,
1379 };
1380 
1381 /* Capture subdev media entity operations */
1382 static int fimc_link_setup(struct media_entity *entity,
1383                            const struct media_pad *local,
1384                            const struct media_pad *remote, u32 flags)
1385 {
1386         struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
1387         struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1388         struct fimc_vid_cap *vc = &fimc->vid_cap;
1389         struct v4l2_subdev *sensor;
1390 
1391         if (!is_media_entity_v4l2_subdev(remote->entity))
1392                 return -EINVAL;
1393 
1394         if (WARN_ON(fimc == NULL))
1395                 return 0;
1396 
1397         dbg("%s --> %s, flags: 0x%x. input: 0x%x",
1398             local->entity->name, remote->entity->name, flags,
1399             fimc->vid_cap.input);
1400 
1401         if (!(flags & MEDIA_LNK_FL_ENABLED)) {
1402                 fimc->vid_cap.input = 0;
1403                 return 0;
1404         }
1405 
1406         if (vc->input != 0)
1407                 return -EBUSY;
1408 
1409         vc->input = sd->grp_id;
1410 
1411         if (vc->user_subdev_api || vc->inh_sensor_ctrls)
1412                 return 0;
1413 
1414         /* Inherit V4L2 controls from the image sensor subdev. */
1415         sensor = fimc_find_remote_sensor(&vc->subdev.entity);
1416         if (sensor == NULL)
1417                 return 0;
1418 
1419         return v4l2_ctrl_add_handler(&vc->ctx->ctrls.handler,
1420                                      sensor->ctrl_handler, NULL, true);
1421 }
1422 
1423 static const struct media_entity_operations fimc_sd_media_ops = {
1424         .link_setup = fimc_link_setup,
1425 };
1426 
1427 /**
1428  * fimc_sensor_notify - v4l2_device notification from a sensor subdev
1429  * @sd: pointer to a subdev generating the notification
1430  * @notification: the notification type, must be S5P_FIMC_TX_END_NOTIFY
1431  * @arg: pointer to an u32 type integer that stores the frame payload value
1432  *
1433  * The End Of Frame notification sent by sensor subdev in its still capture
1434  * mode. If there is only a single VSYNC generated by the sensor at the
1435  * beginning of a frame transmission, FIMC does not issue the LastIrq
1436  * (end of frame) interrupt. And this notification is used to complete the
1437  * frame capture and returning a buffer to user-space. Subdev drivers should
1438  * call this notification from their last 'End of frame capture' interrupt.
1439  */
1440 void fimc_sensor_notify(struct v4l2_subdev *sd, unsigned int notification,
1441                         void *arg)
1442 {
1443         struct fimc_source_info *si;
1444         struct fimc_vid_buffer *buf;
1445         struct fimc_md *fmd;
1446         struct fimc_dev *fimc;
1447         unsigned long flags;
1448 
1449         if (sd == NULL)
1450                 return;
1451 
1452         si = v4l2_get_subdev_hostdata(sd);
1453         fmd = entity_to_fimc_mdev(&sd->entity);
1454 
1455         spin_lock_irqsave(&fmd->slock, flags);
1456 
1457         fimc = si ? source_to_sensor_info(si)->host : NULL;
1458 
1459         if (fimc && arg && notification == S5P_FIMC_TX_END_NOTIFY &&
1460             test_bit(ST_CAPT_PEND, &fimc->state)) {
1461                 unsigned long irq_flags;
1462                 spin_lock_irqsave(&fimc->slock, irq_flags);
1463                 if (!list_empty(&fimc->vid_cap.active_buf_q)) {
1464                         buf = list_entry(fimc->vid_cap.active_buf_q.next,
1465                                          struct fimc_vid_buffer, list);
1466                         vb2_set_plane_payload(&buf->vb.vb2_buf, 0,
1467                                               *((u32 *)arg));
1468                 }
1469                 fimc_capture_irq_handler(fimc, 1);
1470                 fimc_deactivate_capture(fimc);
1471                 spin_unlock_irqrestore(&fimc->slock, irq_flags);
1472         }
1473         spin_unlock_irqrestore(&fmd->slock, flags);
1474 }
1475 
1476 static int fimc_subdev_enum_mbus_code(struct v4l2_subdev *sd,
1477                                       struct v4l2_subdev_pad_config *cfg,
1478                                       struct v4l2_subdev_mbus_code_enum *code)
1479 {
1480         struct fimc_fmt *fmt;
1481 
1482         fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, code->index);
1483         if (!fmt)
1484                 return -EINVAL;
1485         code->code = fmt->mbus_code;
1486         return 0;
1487 }
1488 
1489 static int fimc_subdev_get_fmt(struct v4l2_subdev *sd,
1490                                struct v4l2_subdev_pad_config *cfg,
1491                                struct v4l2_subdev_format *fmt)
1492 {
1493         struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1494         struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1495         struct fimc_frame *ff = &ctx->s_frame;
1496         struct v4l2_mbus_framefmt *mf;
1497 
1498         if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1499                 mf = v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
1500                 fmt->format = *mf;
1501                 return 0;
1502         }
1503 
1504         mf = &fmt->format;
1505         mutex_lock(&fimc->lock);
1506 
1507         switch (fmt->pad) {
1508         case FIMC_SD_PAD_SOURCE:
1509                 if (!WARN_ON(ff->fmt == NULL))
1510                         mf->code = ff->fmt->mbus_code;
1511                 /* Sink pads crop rectangle size */
1512                 mf->width = ff->width;
1513                 mf->height = ff->height;
1514                 break;
1515         case FIMC_SD_PAD_SINK_FIFO:
1516                 *mf = fimc->vid_cap.wb_fmt;
1517                 break;
1518         case FIMC_SD_PAD_SINK_CAM:
1519         default:
1520                 *mf = fimc->vid_cap.ci_fmt;
1521                 break;
1522         }
1523 
1524         mutex_unlock(&fimc->lock);
1525         mf->colorspace = V4L2_COLORSPACE_JPEG;
1526 
1527         return 0;
1528 }
1529 
1530 static int fimc_subdev_set_fmt(struct v4l2_subdev *sd,
1531                                struct v4l2_subdev_pad_config *cfg,
1532                                struct v4l2_subdev_format *fmt)
1533 {
1534         struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1535         struct v4l2_mbus_framefmt *mf = &fmt->format;
1536         struct fimc_vid_cap *vc = &fimc->vid_cap;
1537         struct fimc_ctx *ctx = vc->ctx;
1538         struct fimc_frame *ff;
1539         struct fimc_fmt *ffmt;
1540 
1541         dbg("pad%d: code: 0x%x, %dx%d",
1542             fmt->pad, mf->code, mf->width, mf->height);
1543 
1544         if (fmt->pad == FIMC_SD_PAD_SOURCE && vb2_is_busy(&vc->vbq))
1545                 return -EBUSY;
1546 
1547         mutex_lock(&fimc->lock);
1548         ffmt = fimc_capture_try_format(ctx, &mf->width, &mf->height,
1549                                        &mf->code, NULL, fmt->pad);
1550         mutex_unlock(&fimc->lock);
1551         mf->colorspace = V4L2_COLORSPACE_JPEG;
1552 
1553         if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1554                 mf = v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
1555                 *mf = fmt->format;
1556                 return 0;
1557         }
1558         /* There must be a bug in the driver if this happens */
1559         if (WARN_ON(ffmt == NULL))
1560                 return -EINVAL;
1561 
1562         /* Update RGB Alpha control state and value range */
1563         fimc_alpha_ctrl_update(ctx);
1564 
1565         fimc_capture_mark_jpeg_xfer(ctx, ffmt->color);
1566         if (fmt->pad == FIMC_SD_PAD_SOURCE) {
1567                 ff = &ctx->d_frame;
1568                 /* Sink pads crop rectangle size */
1569                 mf->width = ctx->s_frame.width;
1570                 mf->height = ctx->s_frame.height;
1571         } else {
1572                 ff = &ctx->s_frame;
1573         }
1574 
1575         mutex_lock(&fimc->lock);
1576         set_frame_bounds(ff, mf->width, mf->height);
1577 
1578         if (fmt->pad == FIMC_SD_PAD_SINK_FIFO)
1579                 vc->wb_fmt = *mf;
1580         else if (fmt->pad == FIMC_SD_PAD_SINK_CAM)
1581                 vc->ci_fmt = *mf;
1582 
1583         ff->fmt = ffmt;
1584 
1585         /* Reset the crop rectangle if required. */
1586         if (!(fmt->pad == FIMC_SD_PAD_SOURCE && (ctx->state & FIMC_COMPOSE)))
1587                 set_frame_crop(ff, 0, 0, mf->width, mf->height);
1588 
1589         if (fmt->pad != FIMC_SD_PAD_SOURCE)
1590                 ctx->state &= ~FIMC_COMPOSE;
1591 
1592         mutex_unlock(&fimc->lock);
1593         return 0;
1594 }
1595 
1596 static int fimc_subdev_get_selection(struct v4l2_subdev *sd,
1597                                      struct v4l2_subdev_pad_config *cfg,
1598                                      struct v4l2_subdev_selection *sel)
1599 {
1600         struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1601         struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1602         struct fimc_frame *f = &ctx->s_frame;
1603         struct v4l2_rect *r = &sel->r;
1604         struct v4l2_rect *try_sel;
1605 
1606         if (sel->pad == FIMC_SD_PAD_SOURCE)
1607                 return -EINVAL;
1608 
1609         mutex_lock(&fimc->lock);
1610 
1611         switch (sel->target) {
1612         case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1613                 f = &ctx->d_frame;
1614                 /* fall through */
1615         case V4L2_SEL_TGT_CROP_BOUNDS:
1616                 r->width = f->o_width;
1617                 r->height = f->o_height;
1618                 r->left = 0;
1619                 r->top = 0;
1620                 mutex_unlock(&fimc->lock);
1621                 return 0;
1622 
1623         case V4L2_SEL_TGT_CROP:
1624                 try_sel = v4l2_subdev_get_try_crop(sd, cfg, sel->pad);
1625                 break;
1626         case V4L2_SEL_TGT_COMPOSE:
1627                 try_sel = v4l2_subdev_get_try_compose(sd, cfg, sel->pad);
1628                 f = &ctx->d_frame;
1629                 break;
1630         default:
1631                 mutex_unlock(&fimc->lock);
1632                 return -EINVAL;
1633         }
1634 
1635         if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
1636                 sel->r = *try_sel;
1637         } else {
1638                 r->left = f->offs_h;
1639                 r->top = f->offs_v;
1640                 r->width = f->width;
1641                 r->height = f->height;
1642         }
1643 
1644         dbg("target %#x: l:%d, t:%d, %dx%d, f_w: %d, f_h: %d",
1645             sel->pad, r->left, r->top, r->width, r->height,
1646             f->f_width, f->f_height);
1647 
1648         mutex_unlock(&fimc->lock);
1649         return 0;
1650 }
1651 
1652 static int fimc_subdev_set_selection(struct v4l2_subdev *sd,
1653                                      struct v4l2_subdev_pad_config *cfg,
1654                                      struct v4l2_subdev_selection *sel)
1655 {
1656         struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1657         struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1658         struct fimc_frame *f = &ctx->s_frame;
1659         struct v4l2_rect *r = &sel->r;
1660         struct v4l2_rect *try_sel;
1661         unsigned long flags;
1662 
1663         if (sel->pad == FIMC_SD_PAD_SOURCE)
1664                 return -EINVAL;
1665 
1666         mutex_lock(&fimc->lock);
1667         fimc_capture_try_selection(ctx, r, V4L2_SEL_TGT_CROP);
1668 
1669         switch (sel->target) {
1670         case V4L2_SEL_TGT_CROP:
1671                 try_sel = v4l2_subdev_get_try_crop(sd, cfg, sel->pad);
1672                 break;
1673         case V4L2_SEL_TGT_COMPOSE:
1674                 try_sel = v4l2_subdev_get_try_compose(sd, cfg, sel->pad);
1675                 f = &ctx->d_frame;
1676                 break;
1677         default:
1678                 mutex_unlock(&fimc->lock);
1679                 return -EINVAL;
1680         }
1681 
1682         if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
1683                 *try_sel = sel->r;
1684         } else {
1685                 spin_lock_irqsave(&fimc->slock, flags);
1686                 set_frame_crop(f, r->left, r->top, r->width, r->height);
1687                 set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
1688                 if (sel->target == V4L2_SEL_TGT_COMPOSE)
1689                         ctx->state |= FIMC_COMPOSE;
1690                 spin_unlock_irqrestore(&fimc->slock, flags);
1691         }
1692 
1693         dbg("target %#x: (%d,%d)/%dx%d", sel->target, r->left, r->top,
1694             r->width, r->height);
1695 
1696         mutex_unlock(&fimc->lock);
1697         return 0;
1698 }
1699 
1700 static const struct v4l2_subdev_pad_ops fimc_subdev_pad_ops = {
1701         .enum_mbus_code = fimc_subdev_enum_mbus_code,
1702         .get_selection = fimc_subdev_get_selection,
1703         .set_selection = fimc_subdev_set_selection,
1704         .get_fmt = fimc_subdev_get_fmt,
1705         .set_fmt = fimc_subdev_set_fmt,
1706 };
1707 
1708 static const struct v4l2_subdev_ops fimc_subdev_ops = {
1709         .pad = &fimc_subdev_pad_ops,
1710 };
1711 
1712 /* Set default format at the sensor and host interface */
1713 static int fimc_capture_set_default_format(struct fimc_dev *fimc)
1714 {
1715         struct v4l2_format fmt = {
1716                 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
1717                 .fmt.pix_mp = {
1718                         .width          = FIMC_DEFAULT_WIDTH,
1719                         .height         = FIMC_DEFAULT_HEIGHT,
1720                         .pixelformat    = V4L2_PIX_FMT_YUYV,
1721                         .field          = V4L2_FIELD_NONE,
1722                         .colorspace     = V4L2_COLORSPACE_JPEG,
1723                 },
1724         };
1725 
1726         return __fimc_capture_set_format(fimc, &fmt);
1727 }
1728 
1729 /* fimc->lock must be already initialized */
1730 static int fimc_register_capture_device(struct fimc_dev *fimc,
1731                                  struct v4l2_device *v4l2_dev)
1732 {
1733         struct video_device *vfd = &fimc->vid_cap.ve.vdev;
1734         struct vb2_queue *q = &fimc->vid_cap.vbq;
1735         struct fimc_ctx *ctx;
1736         struct fimc_vid_cap *vid_cap;
1737         struct fimc_fmt *fmt;
1738         int ret = -ENOMEM;
1739 
1740         ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1741         if (!ctx)
1742                 return -ENOMEM;
1743 
1744         ctx->fimc_dev    = fimc;
1745         ctx->in_path     = FIMC_IO_CAMERA;
1746         ctx->out_path    = FIMC_IO_DMA;
1747         ctx->state       = FIMC_CTX_CAP;
1748         ctx->s_frame.fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, 0);
1749         ctx->d_frame.fmt = ctx->s_frame.fmt;
1750 
1751         memset(vfd, 0, sizeof(*vfd));
1752         snprintf(vfd->name, sizeof(vfd->name), "fimc.%d.capture", fimc->id);
1753 
1754         vfd->fops       = &fimc_capture_fops;
1755         vfd->ioctl_ops  = &fimc_capture_ioctl_ops;
1756         vfd->v4l2_dev   = v4l2_dev;
1757         vfd->minor      = -1;
1758         vfd->release    = video_device_release_empty;
1759         vfd->queue      = q;
1760         vfd->lock       = &fimc->lock;
1761         vfd->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE_MPLANE;
1762 
1763         video_set_drvdata(vfd, fimc);
1764         vid_cap = &fimc->vid_cap;
1765         vid_cap->active_buf_cnt = 0;
1766         vid_cap->reqbufs_count = 0;
1767         vid_cap->ctx = ctx;
1768 
1769         INIT_LIST_HEAD(&vid_cap->pending_buf_q);
1770         INIT_LIST_HEAD(&vid_cap->active_buf_q);
1771 
1772         memset(q, 0, sizeof(*q));
1773         q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1774         q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1775         q->drv_priv = ctx;
1776         q->ops = &fimc_capture_qops;
1777         q->mem_ops = &vb2_dma_contig_memops;
1778         q->buf_struct_size = sizeof(struct fimc_vid_buffer);
1779         q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1780         q->lock = &fimc->lock;
1781         q->dev = &fimc->pdev->dev;
1782 
1783         ret = vb2_queue_init(q);
1784         if (ret)
1785                 goto err_free_ctx;
1786 
1787         /* Default format configuration */
1788         fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, 0);
1789         vid_cap->ci_fmt.width = FIMC_DEFAULT_WIDTH;
1790         vid_cap->ci_fmt.height = FIMC_DEFAULT_HEIGHT;
1791         vid_cap->ci_fmt.code = fmt->mbus_code;
1792 
1793         ctx->s_frame.width = FIMC_DEFAULT_WIDTH;
1794         ctx->s_frame.height = FIMC_DEFAULT_HEIGHT;
1795         ctx->s_frame.fmt = fmt;
1796 
1797         fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_WRITEBACK, 0);
1798         vid_cap->wb_fmt = vid_cap->ci_fmt;
1799         vid_cap->wb_fmt.code = fmt->mbus_code;
1800 
1801         vid_cap->vd_pad.flags = MEDIA_PAD_FL_SINK;
1802         vfd->entity.function = MEDIA_ENT_F_PROC_VIDEO_SCALER;
1803         ret = media_entity_pads_init(&vfd->entity, 1, &vid_cap->vd_pad);
1804         if (ret)
1805                 goto err_free_ctx;
1806 
1807         ret = fimc_ctrls_create(ctx);
1808         if (ret)
1809                 goto err_me_cleanup;
1810 
1811         ret = video_register_device(vfd, VFL_TYPE_GRABBER, -1);
1812         if (ret)
1813                 goto err_ctrl_free;
1814 
1815         v4l2_info(v4l2_dev, "Registered %s as /dev/%s\n",
1816                   vfd->name, video_device_node_name(vfd));
1817 
1818         vfd->ctrl_handler = &ctx->ctrls.handler;
1819         return 0;
1820 
1821 err_ctrl_free:
1822         fimc_ctrls_delete(ctx);
1823 err_me_cleanup:
1824         media_entity_cleanup(&vfd->entity);
1825 err_free_ctx:
1826         kfree(ctx);
1827         return ret;
1828 }
1829 
1830 static int fimc_capture_subdev_registered(struct v4l2_subdev *sd)
1831 {
1832         struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1833         int ret;
1834 
1835         if (fimc == NULL)
1836                 return -ENXIO;
1837 
1838         ret = fimc_register_m2m_device(fimc, sd->v4l2_dev);
1839         if (ret)
1840                 return ret;
1841 
1842         fimc->vid_cap.ve.pipe = v4l2_get_subdev_hostdata(sd);
1843 
1844         ret = fimc_register_capture_device(fimc, sd->v4l2_dev);
1845         if (ret) {
1846                 fimc_unregister_m2m_device(fimc);
1847                 fimc->vid_cap.ve.pipe = NULL;
1848         }
1849 
1850         return ret;
1851 }
1852 
1853 static void fimc_capture_subdev_unregistered(struct v4l2_subdev *sd)
1854 {
1855         struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1856         struct video_device *vdev;
1857 
1858         if (fimc == NULL)
1859                 return;
1860 
1861         mutex_lock(&fimc->lock);
1862 
1863         fimc_unregister_m2m_device(fimc);
1864         vdev = &fimc->vid_cap.ve.vdev;
1865 
1866         if (video_is_registered(vdev)) {
1867                 video_unregister_device(vdev);
1868                 media_entity_cleanup(&vdev->entity);
1869                 fimc_ctrls_delete(fimc->vid_cap.ctx);
1870                 fimc->vid_cap.ve.pipe = NULL;
1871         }
1872         kfree(fimc->vid_cap.ctx);
1873         fimc->vid_cap.ctx = NULL;
1874 
1875         mutex_unlock(&fimc->lock);
1876 }
1877 
1878 static const struct v4l2_subdev_internal_ops fimc_capture_sd_internal_ops = {
1879         .registered = fimc_capture_subdev_registered,
1880         .unregistered = fimc_capture_subdev_unregistered,
1881 };
1882 
1883 int fimc_initialize_capture_subdev(struct fimc_dev *fimc)
1884 {
1885         struct v4l2_subdev *sd = &fimc->vid_cap.subdev;
1886         int ret;
1887 
1888         v4l2_subdev_init(sd, &fimc_subdev_ops);
1889         sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1890         snprintf(sd->name, sizeof(sd->name), "FIMC.%d", fimc->id);
1891 
1892         fimc->vid_cap.sd_pads[FIMC_SD_PAD_SINK_CAM].flags = MEDIA_PAD_FL_SINK;
1893         fimc->vid_cap.sd_pads[FIMC_SD_PAD_SINK_FIFO].flags = MEDIA_PAD_FL_SINK;
1894         fimc->vid_cap.sd_pads[FIMC_SD_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
1895         ret = media_entity_pads_init(&sd->entity, FIMC_SD_PADS_NUM,
1896                                 fimc->vid_cap.sd_pads);
1897         if (ret)
1898                 return ret;
1899 
1900         sd->entity.ops = &fimc_sd_media_ops;
1901         sd->internal_ops = &fimc_capture_sd_internal_ops;
1902         v4l2_set_subdevdata(sd, fimc);
1903         return 0;
1904 }
1905 
1906 void fimc_unregister_capture_subdev(struct fimc_dev *fimc)
1907 {
1908         struct v4l2_subdev *sd = &fimc->vid_cap.subdev;
1909 
1910         v4l2_device_unregister_subdev(sd);
1911         media_entity_cleanup(&sd->entity);
1912         v4l2_set_subdevdata(sd, NULL);
1913 }

/* [<][>][^][v][top][bottom][index][help] */