1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Samsung EXYNOS FIMC-LITE (camera host interface) driver 4 * 5 * Copyright (C) 2012 - 2013 Samsung Electronics Co., Ltd. 6 * Author: Sylwester Nawrocki <s.nawrocki@samsung.com> 7 */ 8 #define pr_fmt(fmt) "%s:%d " fmt, __func__, __LINE__ 9 10 #include <linux/bug.h> 11 #include <linux/clk.h> 12 #include <linux/device.h> 13 #include <linux/errno.h> 14 #include <linux/interrupt.h> 15 #include <linux/kernel.h> 16 #include <linux/list.h> 17 #include <linux/module.h> 18 #include <linux/of.h> 19 #include <linux/types.h> 20 #include <linux/platform_device.h> 21 #include <linux/pm_runtime.h> 22 #include <linux/slab.h> 23 #include <linux/videodev2.h> 24 25 #include <media/v4l2-device.h> 26 #include <media/v4l2-ioctl.h> 27 #include <media/v4l2-mem2mem.h> 28 #include <media/v4l2-rect.h> 29 #include <media/videobuf2-v4l2.h> 30 #include <media/videobuf2-dma-contig.h> 31 #include <media/drv-intf/exynos-fimc.h> 32 33 #include "common.h" 34 #include "fimc-core.h" 35 #include "fimc-lite.h" 36 #include "fimc-lite-reg.h" 37 38 static int debug; 39 module_param(debug, int, 0644); 40 41 static const struct fimc_fmt fimc_lite_formats[] = { 42 { 43 .fourcc = V4L2_PIX_FMT_YUYV, 44 .colorspace = V4L2_COLORSPACE_JPEG, 45 .depth = { 16 }, 46 .color = FIMC_FMT_YCBYCR422, 47 .memplanes = 1, 48 .mbus_code = MEDIA_BUS_FMT_YUYV8_2X8, 49 .flags = FMT_FLAGS_YUV, 50 }, { 51 .fourcc = V4L2_PIX_FMT_UYVY, 52 .colorspace = V4L2_COLORSPACE_JPEG, 53 .depth = { 16 }, 54 .color = FIMC_FMT_CBYCRY422, 55 .memplanes = 1, 56 .mbus_code = MEDIA_BUS_FMT_UYVY8_2X8, 57 .flags = FMT_FLAGS_YUV, 58 }, { 59 .fourcc = V4L2_PIX_FMT_VYUY, 60 .colorspace = V4L2_COLORSPACE_JPEG, 61 .depth = { 16 }, 62 .color = FIMC_FMT_CRYCBY422, 63 .memplanes = 1, 64 .mbus_code = MEDIA_BUS_FMT_VYUY8_2X8, 65 .flags = FMT_FLAGS_YUV, 66 }, { 67 .fourcc = V4L2_PIX_FMT_YVYU, 68 .colorspace = V4L2_COLORSPACE_JPEG, 69 .depth = { 16 }, 70 .color = FIMC_FMT_YCRYCB422, 71 .memplanes = 1, 72 .mbus_code = MEDIA_BUS_FMT_YVYU8_2X8, 73 .flags = FMT_FLAGS_YUV, 74 }, { 75 .fourcc = V4L2_PIX_FMT_SGRBG8, 76 .colorspace = V4L2_COLORSPACE_SRGB, 77 .depth = { 8 }, 78 .color = FIMC_FMT_RAW8, 79 .memplanes = 1, 80 .mbus_code = MEDIA_BUS_FMT_SGRBG8_1X8, 81 .flags = FMT_FLAGS_RAW_BAYER, 82 }, { 83 .fourcc = V4L2_PIX_FMT_SGRBG10, 84 .colorspace = V4L2_COLORSPACE_SRGB, 85 .depth = { 16 }, 86 .color = FIMC_FMT_RAW10, 87 .memplanes = 1, 88 .mbus_code = MEDIA_BUS_FMT_SGRBG10_1X10, 89 .flags = FMT_FLAGS_RAW_BAYER, 90 }, { 91 .fourcc = V4L2_PIX_FMT_SGRBG12, 92 .colorspace = V4L2_COLORSPACE_SRGB, 93 .depth = { 16 }, 94 .color = FIMC_FMT_RAW12, 95 .memplanes = 1, 96 .mbus_code = MEDIA_BUS_FMT_SGRBG12_1X12, 97 .flags = FMT_FLAGS_RAW_BAYER, 98 }, 99 }; 100 101 /** 102 * fimc_lite_find_format - lookup fimc color format by fourcc or media bus code 103 * @pixelformat: fourcc to match, ignored if null 104 * @mbus_code: media bus code to match, ignored if null 105 * @mask: the color format flags to match 106 * @index: index to the fimc_lite_formats array, ignored if negative 107 */ 108 static const struct fimc_fmt *fimc_lite_find_format(const u32 *pixelformat, 109 const u32 *mbus_code, unsigned int mask, int index) 110 { 111 const struct fimc_fmt *fmt, *def_fmt = NULL; 112 unsigned int i; 113 int id = 0; 114 115 if (index >= (int)ARRAY_SIZE(fimc_lite_formats)) 116 return NULL; 117 118 for (i = 0; i < ARRAY_SIZE(fimc_lite_formats); ++i) { 119 fmt = &fimc_lite_formats[i]; 120 if (mask && !(fmt->flags & mask)) 121 continue; 122 if (pixelformat && fmt->fourcc == *pixelformat) 123 return fmt; 124 if (mbus_code && fmt->mbus_code == *mbus_code) 125 return fmt; 126 if (index == id) 127 def_fmt = fmt; 128 id++; 129 } 130 return def_fmt; 131 } 132 133 static int fimc_lite_hw_init(struct fimc_lite *fimc, bool isp_output) 134 { 135 struct fimc_source_info *si; 136 unsigned long flags; 137 138 if (fimc->sensor == NULL) 139 return -ENXIO; 140 141 if (fimc->inp_frame.fmt == NULL || fimc->out_frame.fmt == NULL) 142 return -EINVAL; 143 144 /* Get sensor configuration data from the sensor subdev */ 145 si = v4l2_get_subdev_hostdata(fimc->sensor); 146 if (!si) 147 return -EINVAL; 148 149 spin_lock_irqsave(&fimc->slock, flags); 150 151 flite_hw_set_camera_bus(fimc, si); 152 flite_hw_set_source_format(fimc, &fimc->inp_frame); 153 flite_hw_set_window_offset(fimc, &fimc->inp_frame); 154 flite_hw_set_dma_buf_mask(fimc, 0); 155 flite_hw_set_output_dma(fimc, &fimc->out_frame, !isp_output); 156 flite_hw_set_interrupt_mask(fimc); 157 flite_hw_set_test_pattern(fimc, fimc->test_pattern->val); 158 159 if (debug > 0) 160 flite_hw_dump_regs(fimc, __func__); 161 162 spin_unlock_irqrestore(&fimc->slock, flags); 163 return 0; 164 } 165 166 /* 167 * Reinitialize the driver so it is ready to start the streaming again. 168 * Set fimc->state to indicate stream off and the hardware shut down state. 169 * If not suspending (@suspend is false), return any buffers to videobuf2. 170 * Otherwise put any owned buffers onto the pending buffers queue, so they 171 * can be re-spun when the device is being resumed. Also perform FIMC 172 * software reset and disable streaming on the whole pipeline if required. 173 */ 174 static int fimc_lite_reinit(struct fimc_lite *fimc, bool suspend) 175 { 176 struct flite_buffer *buf; 177 unsigned long flags; 178 bool streaming; 179 180 spin_lock_irqsave(&fimc->slock, flags); 181 streaming = fimc->state & (1 << ST_SENSOR_STREAM); 182 183 fimc->state &= ~(1 << ST_FLITE_RUN | 1 << ST_FLITE_OFF | 184 1 << ST_FLITE_STREAM | 1 << ST_SENSOR_STREAM); 185 if (suspend) 186 fimc->state |= (1 << ST_FLITE_SUSPENDED); 187 else 188 fimc->state &= ~(1 << ST_FLITE_PENDING | 189 1 << ST_FLITE_SUSPENDED); 190 191 /* Release unused buffers */ 192 while (!suspend && !list_empty(&fimc->pending_buf_q)) { 193 buf = fimc_lite_pending_queue_pop(fimc); 194 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR); 195 } 196 /* If suspending put unused buffers onto pending queue */ 197 while (!list_empty(&fimc->active_buf_q)) { 198 buf = fimc_lite_active_queue_pop(fimc); 199 if (suspend) 200 fimc_lite_pending_queue_add(fimc, buf); 201 else 202 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR); 203 } 204 205 spin_unlock_irqrestore(&fimc->slock, flags); 206 207 flite_hw_reset(fimc); 208 209 if (!streaming) 210 return 0; 211 212 return fimc_pipeline_call(&fimc->ve, set_stream, 0); 213 } 214 215 static int fimc_lite_stop_capture(struct fimc_lite *fimc, bool suspend) 216 { 217 unsigned long flags; 218 219 if (!fimc_lite_active(fimc)) 220 return 0; 221 222 spin_lock_irqsave(&fimc->slock, flags); 223 set_bit(ST_FLITE_OFF, &fimc->state); 224 flite_hw_capture_stop(fimc); 225 spin_unlock_irqrestore(&fimc->slock, flags); 226 227 wait_event_timeout(fimc->irq_queue, 228 !test_bit(ST_FLITE_OFF, &fimc->state), 229 (2*HZ/10)); /* 200 ms */ 230 231 return fimc_lite_reinit(fimc, suspend); 232 } 233 234 /* Must be called with fimc.slock spinlock held. */ 235 static void fimc_lite_config_update(struct fimc_lite *fimc) 236 { 237 flite_hw_set_window_offset(fimc, &fimc->inp_frame); 238 flite_hw_set_dma_window(fimc, &fimc->out_frame); 239 flite_hw_set_test_pattern(fimc, fimc->test_pattern->val); 240 clear_bit(ST_FLITE_CONFIG, &fimc->state); 241 } 242 243 static irqreturn_t flite_irq_handler(int irq, void *priv) 244 { 245 struct fimc_lite *fimc = priv; 246 struct flite_buffer *vbuf; 247 unsigned long flags; 248 u32 intsrc; 249 250 spin_lock_irqsave(&fimc->slock, flags); 251 252 intsrc = flite_hw_get_interrupt_source(fimc); 253 flite_hw_clear_pending_irq(fimc); 254 255 if (test_and_clear_bit(ST_FLITE_OFF, &fimc->state)) { 256 wake_up(&fimc->irq_queue); 257 goto done; 258 } 259 260 if (intsrc & FLITE_REG_CISTATUS_IRQ_SRC_OVERFLOW) { 261 clear_bit(ST_FLITE_RUN, &fimc->state); 262 fimc->events.data_overflow++; 263 } 264 265 if (intsrc & FLITE_REG_CISTATUS_IRQ_SRC_LASTCAPEND) { 266 flite_hw_clear_last_capture_end(fimc); 267 clear_bit(ST_FLITE_STREAM, &fimc->state); 268 wake_up(&fimc->irq_queue); 269 } 270 271 if (atomic_read(&fimc->out_path) != FIMC_IO_DMA) 272 goto done; 273 274 if ((intsrc & FLITE_REG_CISTATUS_IRQ_SRC_FRMSTART) && 275 test_bit(ST_FLITE_RUN, &fimc->state) && 276 !list_empty(&fimc->pending_buf_q)) { 277 vbuf = fimc_lite_pending_queue_pop(fimc); 278 flite_hw_set_dma_buffer(fimc, vbuf); 279 fimc_lite_active_queue_add(fimc, vbuf); 280 } 281 282 if ((intsrc & FLITE_REG_CISTATUS_IRQ_SRC_FRMEND) && 283 test_bit(ST_FLITE_RUN, &fimc->state) && 284 !list_empty(&fimc->active_buf_q)) { 285 vbuf = fimc_lite_active_queue_pop(fimc); 286 vbuf->vb.vb2_buf.timestamp = ktime_get_ns(); 287 vbuf->vb.sequence = fimc->frame_count++; 288 flite_hw_mask_dma_buffer(fimc, vbuf->index); 289 vb2_buffer_done(&vbuf->vb.vb2_buf, VB2_BUF_STATE_DONE); 290 } 291 292 if (test_bit(ST_FLITE_CONFIG, &fimc->state)) 293 fimc_lite_config_update(fimc); 294 295 if (list_empty(&fimc->pending_buf_q)) { 296 flite_hw_capture_stop(fimc); 297 clear_bit(ST_FLITE_STREAM, &fimc->state); 298 } 299 done: 300 set_bit(ST_FLITE_RUN, &fimc->state); 301 spin_unlock_irqrestore(&fimc->slock, flags); 302 return IRQ_HANDLED; 303 } 304 305 static int start_streaming(struct vb2_queue *q, unsigned int count) 306 { 307 struct fimc_lite *fimc = q->drv_priv; 308 unsigned long flags; 309 int ret; 310 311 spin_lock_irqsave(&fimc->slock, flags); 312 313 fimc->buf_index = 0; 314 fimc->frame_count = 0; 315 316 spin_unlock_irqrestore(&fimc->slock, flags); 317 318 ret = fimc_lite_hw_init(fimc, false); 319 if (ret) { 320 fimc_lite_reinit(fimc, false); 321 return ret; 322 } 323 324 set_bit(ST_FLITE_PENDING, &fimc->state); 325 326 if (!list_empty(&fimc->active_buf_q) && 327 !test_and_set_bit(ST_FLITE_STREAM, &fimc->state)) { 328 flite_hw_capture_start(fimc); 329 330 if (!test_and_set_bit(ST_SENSOR_STREAM, &fimc->state)) 331 fimc_pipeline_call(&fimc->ve, set_stream, 1); 332 } 333 if (debug > 0) 334 flite_hw_dump_regs(fimc, __func__); 335 336 return 0; 337 } 338 339 static void stop_streaming(struct vb2_queue *q) 340 { 341 struct fimc_lite *fimc = q->drv_priv; 342 343 if (!fimc_lite_active(fimc)) 344 return; 345 346 fimc_lite_stop_capture(fimc, false); 347 } 348 349 static int queue_setup(struct vb2_queue *vq, 350 unsigned int *num_buffers, unsigned int *num_planes, 351 unsigned int sizes[], struct device *alloc_devs[]) 352 { 353 struct fimc_lite *fimc = vq->drv_priv; 354 struct flite_frame *frame = &fimc->out_frame; 355 const struct fimc_fmt *fmt = frame->fmt; 356 unsigned long wh = frame->f_width * frame->f_height; 357 int i; 358 359 if (fmt == NULL) 360 return -EINVAL; 361 362 if (*num_planes) { 363 if (*num_planes != fmt->memplanes) 364 return -EINVAL; 365 for (i = 0; i < *num_planes; i++) 366 if (sizes[i] < (wh * fmt->depth[i]) / 8) 367 return -EINVAL; 368 return 0; 369 } 370 371 *num_planes = fmt->memplanes; 372 373 for (i = 0; i < fmt->memplanes; i++) 374 sizes[i] = (wh * fmt->depth[i]) / 8; 375 376 return 0; 377 } 378 379 static int buffer_prepare(struct vb2_buffer *vb) 380 { 381 struct vb2_queue *vq = vb->vb2_queue; 382 struct fimc_lite *fimc = vq->drv_priv; 383 int i; 384 385 if (fimc->out_frame.fmt == NULL) 386 return -EINVAL; 387 388 for (i = 0; i < fimc->out_frame.fmt->memplanes; i++) { 389 unsigned long size = fimc->payload[i]; 390 391 if (vb2_plane_size(vb, i) < size) { 392 v4l2_err(&fimc->ve.vdev, 393 "User buffer too small (%ld < %ld)\n", 394 vb2_plane_size(vb, i), size); 395 return -EINVAL; 396 } 397 vb2_set_plane_payload(vb, i, size); 398 } 399 400 return 0; 401 } 402 403 static void buffer_queue(struct vb2_buffer *vb) 404 { 405 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 406 struct flite_buffer *buf 407 = container_of(vbuf, struct flite_buffer, vb); 408 struct fimc_lite *fimc = vb2_get_drv_priv(vb->vb2_queue); 409 unsigned long flags; 410 411 spin_lock_irqsave(&fimc->slock, flags); 412 buf->addr = vb2_dma_contig_plane_dma_addr(vb, 0); 413 414 buf->index = fimc->buf_index++; 415 if (fimc->buf_index >= fimc->reqbufs_count) 416 fimc->buf_index = 0; 417 418 if (!test_bit(ST_FLITE_SUSPENDED, &fimc->state) && 419 !test_bit(ST_FLITE_STREAM, &fimc->state) && 420 list_empty(&fimc->active_buf_q)) { 421 flite_hw_set_dma_buffer(fimc, buf); 422 fimc_lite_active_queue_add(fimc, buf); 423 } else { 424 fimc_lite_pending_queue_add(fimc, buf); 425 } 426 427 if (vb2_is_streaming(&fimc->vb_queue) && 428 !list_empty(&fimc->pending_buf_q) && 429 !test_and_set_bit(ST_FLITE_STREAM, &fimc->state)) { 430 flite_hw_capture_start(fimc); 431 spin_unlock_irqrestore(&fimc->slock, flags); 432 433 if (!test_and_set_bit(ST_SENSOR_STREAM, &fimc->state)) 434 fimc_pipeline_call(&fimc->ve, set_stream, 1); 435 return; 436 } 437 spin_unlock_irqrestore(&fimc->slock, flags); 438 } 439 440 static const struct vb2_ops fimc_lite_qops = { 441 .queue_setup = queue_setup, 442 .buf_prepare = buffer_prepare, 443 .buf_queue = buffer_queue, 444 .wait_prepare = vb2_ops_wait_prepare, 445 .wait_finish = vb2_ops_wait_finish, 446 .start_streaming = start_streaming, 447 .stop_streaming = stop_streaming, 448 }; 449 450 static void fimc_lite_clear_event_counters(struct fimc_lite *fimc) 451 { 452 unsigned long flags; 453 454 spin_lock_irqsave(&fimc->slock, flags); 455 memset(&fimc->events, 0, sizeof(fimc->events)); 456 spin_unlock_irqrestore(&fimc->slock, flags); 457 } 458 459 static int fimc_lite_open(struct file *file) 460 { 461 struct fimc_lite *fimc = video_drvdata(file); 462 struct media_entity *me = &fimc->ve.vdev.entity; 463 int ret; 464 465 mutex_lock(&fimc->lock); 466 if (atomic_read(&fimc->out_path) != FIMC_IO_DMA) { 467 ret = -EBUSY; 468 goto unlock; 469 } 470 471 set_bit(ST_FLITE_IN_USE, &fimc->state); 472 ret = pm_runtime_resume_and_get(&fimc->pdev->dev); 473 if (ret < 0) 474 goto err_in_use; 475 476 ret = v4l2_fh_open(file); 477 if (ret < 0) 478 goto err_pm; 479 480 if (!v4l2_fh_is_singular_file(file) || 481 atomic_read(&fimc->out_path) != FIMC_IO_DMA) 482 goto unlock; 483 484 mutex_lock(&me->graph_obj.mdev->graph_mutex); 485 486 ret = fimc_pipeline_call(&fimc->ve, open, me, true); 487 488 /* Mark video pipeline ending at this video node as in use. */ 489 if (ret == 0) 490 me->use_count++; 491 492 mutex_unlock(&me->graph_obj.mdev->graph_mutex); 493 494 if (!ret) { 495 fimc_lite_clear_event_counters(fimc); 496 goto unlock; 497 } 498 499 v4l2_fh_release(file); 500 err_pm: 501 pm_runtime_put_sync(&fimc->pdev->dev); 502 err_in_use: 503 clear_bit(ST_FLITE_IN_USE, &fimc->state); 504 unlock: 505 mutex_unlock(&fimc->lock); 506 return ret; 507 } 508 509 static int fimc_lite_release(struct file *file) 510 { 511 struct fimc_lite *fimc = video_drvdata(file); 512 struct media_entity *entity = &fimc->ve.vdev.entity; 513 514 mutex_lock(&fimc->lock); 515 516 if (v4l2_fh_is_singular_file(file) && 517 atomic_read(&fimc->out_path) == FIMC_IO_DMA) { 518 if (fimc->streaming) { 519 video_device_pipeline_stop(&fimc->ve.vdev); 520 fimc->streaming = false; 521 } 522 fimc_lite_stop_capture(fimc, false); 523 fimc_pipeline_call(&fimc->ve, close); 524 clear_bit(ST_FLITE_IN_USE, &fimc->state); 525 526 mutex_lock(&entity->graph_obj.mdev->graph_mutex); 527 entity->use_count--; 528 mutex_unlock(&entity->graph_obj.mdev->graph_mutex); 529 } 530 531 _vb2_fop_release(file, NULL); 532 pm_runtime_put(&fimc->pdev->dev); 533 clear_bit(ST_FLITE_SUSPENDED, &fimc->state); 534 535 mutex_unlock(&fimc->lock); 536 return 0; 537 } 538 539 static const struct v4l2_file_operations fimc_lite_fops = { 540 .owner = THIS_MODULE, 541 .open = fimc_lite_open, 542 .release = fimc_lite_release, 543 .poll = vb2_fop_poll, 544 .unlocked_ioctl = video_ioctl2, 545 .mmap = vb2_fop_mmap, 546 }; 547 548 /* 549 * Format and crop negotiation helpers 550 */ 551 552 static const struct fimc_fmt *fimc_lite_subdev_try_fmt(struct fimc_lite *fimc, 553 struct v4l2_subdev_state *sd_state, 554 struct v4l2_subdev_format *format) 555 { 556 struct flite_drvdata *dd = fimc->dd; 557 struct v4l2_mbus_framefmt *mf = &format->format; 558 const struct fimc_fmt *fmt = NULL; 559 560 if (format->pad == FLITE_SD_PAD_SINK) { 561 v4l_bound_align_image(&mf->width, 8, dd->max_width, 562 ffs(dd->out_width_align) - 1, 563 &mf->height, 0, dd->max_height, 0, 0); 564 565 fmt = fimc_lite_find_format(NULL, &mf->code, 0, 0); 566 if (WARN_ON(!fmt)) 567 return NULL; 568 569 mf->colorspace = fmt->colorspace; 570 mf->code = fmt->mbus_code; 571 } else { 572 struct flite_frame *sink = &fimc->inp_frame; 573 struct v4l2_mbus_framefmt *sink_fmt; 574 struct v4l2_rect *rect; 575 576 if (format->which == V4L2_SUBDEV_FORMAT_TRY) { 577 sink_fmt = v4l2_subdev_get_try_format(&fimc->subdev, 578 sd_state, 579 FLITE_SD_PAD_SINK); 580 581 mf->code = sink_fmt->code; 582 mf->colorspace = sink_fmt->colorspace; 583 584 rect = v4l2_subdev_get_try_crop(&fimc->subdev, 585 sd_state, 586 FLITE_SD_PAD_SINK); 587 } else { 588 mf->code = sink->fmt->mbus_code; 589 mf->colorspace = sink->fmt->colorspace; 590 rect = &sink->rect; 591 } 592 593 /* Allow changing format only on sink pad */ 594 mf->width = rect->width; 595 mf->height = rect->height; 596 } 597 598 mf->field = V4L2_FIELD_NONE; 599 600 v4l2_dbg(1, debug, &fimc->subdev, "code: %#x (%d), %dx%d\n", 601 mf->code, mf->colorspace, mf->width, mf->height); 602 603 return fmt; 604 } 605 606 static void fimc_lite_try_crop(struct fimc_lite *fimc, struct v4l2_rect *r) 607 { 608 struct flite_frame *frame = &fimc->inp_frame; 609 610 v4l_bound_align_image(&r->width, 0, frame->f_width, 0, 611 &r->height, 0, frame->f_height, 0, 0); 612 613 /* Adjust left/top if cropping rectangle got out of bounds */ 614 r->left = clamp_t(u32, r->left, 0, frame->f_width - r->width); 615 r->left = round_down(r->left, fimc->dd->win_hor_offs_align); 616 r->top = clamp_t(u32, r->top, 0, frame->f_height - r->height); 617 618 v4l2_dbg(1, debug, &fimc->subdev, "(%d,%d)/%dx%d, sink fmt: %dx%d\n", 619 r->left, r->top, r->width, r->height, 620 frame->f_width, frame->f_height); 621 } 622 623 static void fimc_lite_try_compose(struct fimc_lite *fimc, struct v4l2_rect *r) 624 { 625 struct flite_frame *frame = &fimc->out_frame; 626 struct v4l2_rect *crop_rect = &fimc->inp_frame.rect; 627 628 /* Scaling is not supported so we enforce compose rectangle size 629 same as size of the sink crop rectangle. */ 630 r->width = crop_rect->width; 631 r->height = crop_rect->height; 632 633 /* Adjust left/top if the composing rectangle got out of bounds */ 634 r->left = clamp_t(u32, r->left, 0, frame->f_width - r->width); 635 r->left = round_down(r->left, fimc->dd->out_hor_offs_align); 636 r->top = clamp_t(u32, r->top, 0, fimc->out_frame.f_height - r->height); 637 638 v4l2_dbg(1, debug, &fimc->subdev, "(%d,%d)/%dx%d, source fmt: %dx%d\n", 639 r->left, r->top, r->width, r->height, 640 frame->f_width, frame->f_height); 641 } 642 643 /* 644 * Video node ioctl operations 645 */ 646 static int fimc_lite_querycap(struct file *file, void *priv, 647 struct v4l2_capability *cap) 648 { 649 strscpy(cap->driver, FIMC_LITE_DRV_NAME, sizeof(cap->driver)); 650 strscpy(cap->card, FIMC_LITE_DRV_NAME, sizeof(cap->card)); 651 return 0; 652 } 653 654 static int fimc_lite_enum_fmt(struct file *file, void *priv, 655 struct v4l2_fmtdesc *f) 656 { 657 const struct fimc_fmt *fmt; 658 659 if (f->index >= ARRAY_SIZE(fimc_lite_formats)) 660 return -EINVAL; 661 662 fmt = &fimc_lite_formats[f->index]; 663 f->pixelformat = fmt->fourcc; 664 665 return 0; 666 } 667 668 static int fimc_lite_g_fmt_mplane(struct file *file, void *fh, 669 struct v4l2_format *f) 670 { 671 struct fimc_lite *fimc = video_drvdata(file); 672 struct v4l2_pix_format_mplane *pixm = &f->fmt.pix_mp; 673 struct v4l2_plane_pix_format *plane_fmt = &pixm->plane_fmt[0]; 674 struct flite_frame *frame = &fimc->out_frame; 675 const struct fimc_fmt *fmt = frame->fmt; 676 677 plane_fmt->bytesperline = (frame->f_width * fmt->depth[0]) / 8; 678 plane_fmt->sizeimage = plane_fmt->bytesperline * frame->f_height; 679 680 pixm->num_planes = fmt->memplanes; 681 pixm->pixelformat = fmt->fourcc; 682 pixm->width = frame->f_width; 683 pixm->height = frame->f_height; 684 pixm->field = V4L2_FIELD_NONE; 685 pixm->colorspace = fmt->colorspace; 686 return 0; 687 } 688 689 static int fimc_lite_try_fmt(struct fimc_lite *fimc, 690 struct v4l2_pix_format_mplane *pixm, 691 const struct fimc_fmt **ffmt) 692 { 693 u32 bpl = pixm->plane_fmt[0].bytesperline; 694 struct flite_drvdata *dd = fimc->dd; 695 const struct fimc_fmt *inp_fmt = fimc->inp_frame.fmt; 696 const struct fimc_fmt *fmt; 697 698 if (WARN_ON(inp_fmt == NULL)) 699 return -EINVAL; 700 /* 701 * We allow some flexibility only for YUV formats. In case of raw 702 * raw Bayer the FIMC-LITE's output format must match its camera 703 * interface input format. 704 */ 705 if (inp_fmt->flags & FMT_FLAGS_YUV) 706 fmt = fimc_lite_find_format(&pixm->pixelformat, NULL, 707 inp_fmt->flags, 0); 708 else 709 fmt = inp_fmt; 710 711 if (WARN_ON(fmt == NULL)) 712 return -EINVAL; 713 if (ffmt) 714 *ffmt = fmt; 715 v4l_bound_align_image(&pixm->width, 8, dd->max_width, 716 ffs(dd->out_width_align) - 1, 717 &pixm->height, 0, dd->max_height, 0, 0); 718 719 if ((bpl == 0 || ((bpl * 8) / fmt->depth[0]) < pixm->width)) 720 pixm->plane_fmt[0].bytesperline = (pixm->width * 721 fmt->depth[0]) / 8; 722 723 if (pixm->plane_fmt[0].sizeimage == 0) 724 pixm->plane_fmt[0].sizeimage = (pixm->width * pixm->height * 725 fmt->depth[0]) / 8; 726 pixm->num_planes = fmt->memplanes; 727 pixm->pixelformat = fmt->fourcc; 728 pixm->colorspace = fmt->colorspace; 729 pixm->field = V4L2_FIELD_NONE; 730 return 0; 731 } 732 733 static int fimc_lite_try_fmt_mplane(struct file *file, void *fh, 734 struct v4l2_format *f) 735 { 736 struct fimc_lite *fimc = video_drvdata(file); 737 return fimc_lite_try_fmt(fimc, &f->fmt.pix_mp, NULL); 738 } 739 740 static int fimc_lite_s_fmt_mplane(struct file *file, void *priv, 741 struct v4l2_format *f) 742 { 743 struct v4l2_pix_format_mplane *pixm = &f->fmt.pix_mp; 744 struct fimc_lite *fimc = video_drvdata(file); 745 struct flite_frame *frame = &fimc->out_frame; 746 const struct fimc_fmt *fmt = NULL; 747 int ret; 748 749 if (vb2_is_busy(&fimc->vb_queue)) 750 return -EBUSY; 751 752 ret = fimc_lite_try_fmt(fimc, &f->fmt.pix_mp, &fmt); 753 if (ret < 0) 754 return ret; 755 756 frame->fmt = fmt; 757 fimc->payload[0] = max((pixm->width * pixm->height * fmt->depth[0]) / 8, 758 pixm->plane_fmt[0].sizeimage); 759 frame->f_width = pixm->width; 760 frame->f_height = pixm->height; 761 762 return 0; 763 } 764 765 static int fimc_pipeline_validate(struct fimc_lite *fimc) 766 { 767 struct v4l2_subdev *sd = &fimc->subdev; 768 struct v4l2_subdev_format sink_fmt, src_fmt; 769 struct media_pad *pad; 770 int ret; 771 772 while (1) { 773 /* Retrieve format at the sink pad */ 774 pad = &sd->entity.pads[0]; 775 if (!(pad->flags & MEDIA_PAD_FL_SINK)) 776 break; 777 /* Don't call FIMC subdev operation to avoid nested locking */ 778 if (sd == &fimc->subdev) { 779 struct flite_frame *ff = &fimc->out_frame; 780 sink_fmt.format.width = ff->f_width; 781 sink_fmt.format.height = ff->f_height; 782 sink_fmt.format.code = fimc->inp_frame.fmt->mbus_code; 783 } else { 784 sink_fmt.pad = pad->index; 785 sink_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE; 786 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, 787 &sink_fmt); 788 if (ret < 0 && ret != -ENOIOCTLCMD) 789 return -EPIPE; 790 } 791 /* Retrieve format at the source pad */ 792 pad = media_pad_remote_pad_first(pad); 793 if (!pad || !is_media_entity_v4l2_subdev(pad->entity)) 794 break; 795 796 sd = media_entity_to_v4l2_subdev(pad->entity); 797 src_fmt.pad = pad->index; 798 src_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE; 799 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &src_fmt); 800 if (ret < 0 && ret != -ENOIOCTLCMD) 801 return -EPIPE; 802 803 if (src_fmt.format.width != sink_fmt.format.width || 804 src_fmt.format.height != sink_fmt.format.height || 805 src_fmt.format.code != sink_fmt.format.code) 806 return -EPIPE; 807 } 808 return 0; 809 } 810 811 static int fimc_lite_streamon(struct file *file, void *priv, 812 enum v4l2_buf_type type) 813 { 814 struct fimc_lite *fimc = video_drvdata(file); 815 int ret; 816 817 if (fimc_lite_active(fimc)) 818 return -EBUSY; 819 820 ret = video_device_pipeline_start(&fimc->ve.vdev, &fimc->ve.pipe->mp); 821 if (ret < 0) 822 return ret; 823 824 ret = fimc_pipeline_validate(fimc); 825 if (ret < 0) 826 goto err_p_stop; 827 828 fimc->sensor = fimc_find_remote_sensor(&fimc->subdev.entity); 829 830 ret = vb2_ioctl_streamon(file, priv, type); 831 if (!ret) { 832 fimc->streaming = true; 833 return ret; 834 } 835 836 err_p_stop: 837 video_device_pipeline_stop(&fimc->ve.vdev); 838 return 0; 839 } 840 841 static int fimc_lite_streamoff(struct file *file, void *priv, 842 enum v4l2_buf_type type) 843 { 844 struct fimc_lite *fimc = video_drvdata(file); 845 int ret; 846 847 ret = vb2_ioctl_streamoff(file, priv, type); 848 if (ret < 0) 849 return ret; 850 851 video_device_pipeline_stop(&fimc->ve.vdev); 852 fimc->streaming = false; 853 return 0; 854 } 855 856 static int fimc_lite_reqbufs(struct file *file, void *priv, 857 struct v4l2_requestbuffers *reqbufs) 858 { 859 struct fimc_lite *fimc = video_drvdata(file); 860 int ret; 861 862 reqbufs->count = max_t(u32, FLITE_REQ_BUFS_MIN, reqbufs->count); 863 ret = vb2_ioctl_reqbufs(file, priv, reqbufs); 864 if (!ret) 865 fimc->reqbufs_count = reqbufs->count; 866 867 return ret; 868 } 869 870 static int fimc_lite_g_selection(struct file *file, void *fh, 871 struct v4l2_selection *sel) 872 { 873 struct fimc_lite *fimc = video_drvdata(file); 874 struct flite_frame *f = &fimc->out_frame; 875 876 if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 877 return -EINVAL; 878 879 switch (sel->target) { 880 case V4L2_SEL_TGT_COMPOSE_BOUNDS: 881 case V4L2_SEL_TGT_COMPOSE_DEFAULT: 882 sel->r.left = 0; 883 sel->r.top = 0; 884 sel->r.width = f->f_width; 885 sel->r.height = f->f_height; 886 return 0; 887 888 case V4L2_SEL_TGT_COMPOSE: 889 sel->r = f->rect; 890 return 0; 891 } 892 893 return -EINVAL; 894 } 895 896 static int fimc_lite_s_selection(struct file *file, void *fh, 897 struct v4l2_selection *sel) 898 { 899 struct fimc_lite *fimc = video_drvdata(file); 900 struct flite_frame *f = &fimc->out_frame; 901 struct v4l2_rect rect = sel->r; 902 unsigned long flags; 903 904 if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE || 905 sel->target != V4L2_SEL_TGT_COMPOSE) 906 return -EINVAL; 907 908 fimc_lite_try_compose(fimc, &rect); 909 910 if ((sel->flags & V4L2_SEL_FLAG_LE) && 911 !v4l2_rect_enclosed(&rect, &sel->r)) 912 return -ERANGE; 913 914 if ((sel->flags & V4L2_SEL_FLAG_GE) && 915 !v4l2_rect_enclosed(&sel->r, &rect)) 916 return -ERANGE; 917 918 sel->r = rect; 919 spin_lock_irqsave(&fimc->slock, flags); 920 f->rect = rect; 921 set_bit(ST_FLITE_CONFIG, &fimc->state); 922 spin_unlock_irqrestore(&fimc->slock, flags); 923 924 return 0; 925 } 926 927 static const struct v4l2_ioctl_ops fimc_lite_ioctl_ops = { 928 .vidioc_querycap = fimc_lite_querycap, 929 .vidioc_enum_fmt_vid_cap = fimc_lite_enum_fmt, 930 .vidioc_try_fmt_vid_cap_mplane = fimc_lite_try_fmt_mplane, 931 .vidioc_s_fmt_vid_cap_mplane = fimc_lite_s_fmt_mplane, 932 .vidioc_g_fmt_vid_cap_mplane = fimc_lite_g_fmt_mplane, 933 .vidioc_g_selection = fimc_lite_g_selection, 934 .vidioc_s_selection = fimc_lite_s_selection, 935 .vidioc_reqbufs = fimc_lite_reqbufs, 936 .vidioc_querybuf = vb2_ioctl_querybuf, 937 .vidioc_prepare_buf = vb2_ioctl_prepare_buf, 938 .vidioc_create_bufs = vb2_ioctl_create_bufs, 939 .vidioc_qbuf = vb2_ioctl_qbuf, 940 .vidioc_dqbuf = vb2_ioctl_dqbuf, 941 .vidioc_streamon = fimc_lite_streamon, 942 .vidioc_streamoff = fimc_lite_streamoff, 943 }; 944 945 /* Capture subdev media entity operations */ 946 static int fimc_lite_link_setup(struct media_entity *entity, 947 const struct media_pad *local, 948 const struct media_pad *remote, u32 flags) 949 { 950 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity); 951 struct fimc_lite *fimc = v4l2_get_subdevdata(sd); 952 int ret = 0; 953 954 if (WARN_ON(fimc == NULL)) 955 return 0; 956 957 v4l2_dbg(1, debug, sd, "%s: %s --> %s, flags: 0x%x. source_id: 0x%x\n", 958 __func__, remote->entity->name, local->entity->name, 959 flags, fimc->source_subdev_grp_id); 960 961 switch (local->index) { 962 case FLITE_SD_PAD_SINK: 963 if (flags & MEDIA_LNK_FL_ENABLED) { 964 if (fimc->source_subdev_grp_id == 0) 965 fimc->source_subdev_grp_id = sd->grp_id; 966 else 967 ret = -EBUSY; 968 } else { 969 fimc->source_subdev_grp_id = 0; 970 fimc->sensor = NULL; 971 } 972 break; 973 974 case FLITE_SD_PAD_SOURCE_DMA: 975 if (!(flags & MEDIA_LNK_FL_ENABLED)) 976 atomic_set(&fimc->out_path, FIMC_IO_NONE); 977 else 978 atomic_set(&fimc->out_path, FIMC_IO_DMA); 979 break; 980 981 case FLITE_SD_PAD_SOURCE_ISP: 982 if (!(flags & MEDIA_LNK_FL_ENABLED)) 983 atomic_set(&fimc->out_path, FIMC_IO_NONE); 984 else 985 atomic_set(&fimc->out_path, FIMC_IO_ISP); 986 break; 987 988 default: 989 v4l2_err(sd, "Invalid pad index\n"); 990 ret = -EINVAL; 991 } 992 mb(); 993 994 return ret; 995 } 996 997 static const struct media_entity_operations fimc_lite_subdev_media_ops = { 998 .link_setup = fimc_lite_link_setup, 999 }; 1000 1001 static int fimc_lite_subdev_enum_mbus_code(struct v4l2_subdev *sd, 1002 struct v4l2_subdev_state *sd_state, 1003 struct v4l2_subdev_mbus_code_enum *code) 1004 { 1005 const struct fimc_fmt *fmt; 1006 1007 fmt = fimc_lite_find_format(NULL, NULL, 0, code->index); 1008 if (!fmt) 1009 return -EINVAL; 1010 code->code = fmt->mbus_code; 1011 return 0; 1012 } 1013 1014 static struct v4l2_mbus_framefmt *__fimc_lite_subdev_get_try_fmt( 1015 struct v4l2_subdev *sd, 1016 struct v4l2_subdev_state *sd_state, unsigned int pad) 1017 { 1018 if (pad != FLITE_SD_PAD_SINK) 1019 pad = FLITE_SD_PAD_SOURCE_DMA; 1020 1021 return v4l2_subdev_get_try_format(sd, sd_state, pad); 1022 } 1023 1024 static int fimc_lite_subdev_get_fmt(struct v4l2_subdev *sd, 1025 struct v4l2_subdev_state *sd_state, 1026 struct v4l2_subdev_format *fmt) 1027 { 1028 struct fimc_lite *fimc = v4l2_get_subdevdata(sd); 1029 struct v4l2_mbus_framefmt *mf = &fmt->format; 1030 struct flite_frame *f = &fimc->inp_frame; 1031 1032 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) { 1033 mf = __fimc_lite_subdev_get_try_fmt(sd, sd_state, fmt->pad); 1034 fmt->format = *mf; 1035 return 0; 1036 } 1037 1038 mutex_lock(&fimc->lock); 1039 mf->colorspace = f->fmt->colorspace; 1040 mf->code = f->fmt->mbus_code; 1041 1042 if (fmt->pad == FLITE_SD_PAD_SINK) { 1043 /* full camera input frame size */ 1044 mf->width = f->f_width; 1045 mf->height = f->f_height; 1046 } else { 1047 /* crop size */ 1048 mf->width = f->rect.width; 1049 mf->height = f->rect.height; 1050 } 1051 mutex_unlock(&fimc->lock); 1052 return 0; 1053 } 1054 1055 static int fimc_lite_subdev_set_fmt(struct v4l2_subdev *sd, 1056 struct v4l2_subdev_state *sd_state, 1057 struct v4l2_subdev_format *fmt) 1058 { 1059 struct fimc_lite *fimc = v4l2_get_subdevdata(sd); 1060 struct v4l2_mbus_framefmt *mf = &fmt->format; 1061 struct flite_frame *sink = &fimc->inp_frame; 1062 struct flite_frame *source = &fimc->out_frame; 1063 const struct fimc_fmt *ffmt; 1064 1065 v4l2_dbg(1, debug, sd, "pad%d: code: 0x%x, %dx%d\n", 1066 fmt->pad, mf->code, mf->width, mf->height); 1067 1068 mutex_lock(&fimc->lock); 1069 1070 if ((atomic_read(&fimc->out_path) == FIMC_IO_ISP && 1071 media_entity_is_streaming(&sd->entity)) || 1072 (atomic_read(&fimc->out_path) == FIMC_IO_DMA && 1073 vb2_is_busy(&fimc->vb_queue))) { 1074 mutex_unlock(&fimc->lock); 1075 return -EBUSY; 1076 } 1077 1078 ffmt = fimc_lite_subdev_try_fmt(fimc, sd_state, fmt); 1079 1080 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) { 1081 struct v4l2_mbus_framefmt *src_fmt; 1082 1083 mf = __fimc_lite_subdev_get_try_fmt(sd, sd_state, fmt->pad); 1084 *mf = fmt->format; 1085 1086 if (fmt->pad == FLITE_SD_PAD_SINK) { 1087 unsigned int pad = FLITE_SD_PAD_SOURCE_DMA; 1088 src_fmt = __fimc_lite_subdev_get_try_fmt(sd, sd_state, 1089 pad); 1090 *src_fmt = *mf; 1091 } 1092 1093 mutex_unlock(&fimc->lock); 1094 return 0; 1095 } 1096 1097 if (fmt->pad == FLITE_SD_PAD_SINK) { 1098 sink->f_width = mf->width; 1099 sink->f_height = mf->height; 1100 sink->fmt = ffmt; 1101 /* Set sink crop rectangle */ 1102 sink->rect.width = mf->width; 1103 sink->rect.height = mf->height; 1104 sink->rect.left = 0; 1105 sink->rect.top = 0; 1106 /* Reset source format and crop rectangle */ 1107 source->rect = sink->rect; 1108 source->f_width = mf->width; 1109 source->f_height = mf->height; 1110 } 1111 1112 mutex_unlock(&fimc->lock); 1113 return 0; 1114 } 1115 1116 static int fimc_lite_subdev_get_selection(struct v4l2_subdev *sd, 1117 struct v4l2_subdev_state *sd_state, 1118 struct v4l2_subdev_selection *sel) 1119 { 1120 struct fimc_lite *fimc = v4l2_get_subdevdata(sd); 1121 struct flite_frame *f = &fimc->inp_frame; 1122 1123 if ((sel->target != V4L2_SEL_TGT_CROP && 1124 sel->target != V4L2_SEL_TGT_CROP_BOUNDS) || 1125 sel->pad != FLITE_SD_PAD_SINK) 1126 return -EINVAL; 1127 1128 if (sel->which == V4L2_SUBDEV_FORMAT_TRY) { 1129 sel->r = *v4l2_subdev_get_try_crop(sd, sd_state, sel->pad); 1130 return 0; 1131 } 1132 1133 mutex_lock(&fimc->lock); 1134 if (sel->target == V4L2_SEL_TGT_CROP) { 1135 sel->r = f->rect; 1136 } else { 1137 sel->r.left = 0; 1138 sel->r.top = 0; 1139 sel->r.width = f->f_width; 1140 sel->r.height = f->f_height; 1141 } 1142 mutex_unlock(&fimc->lock); 1143 1144 v4l2_dbg(1, debug, sd, "%s: (%d,%d) %dx%d, f_w: %d, f_h: %d\n", 1145 __func__, f->rect.left, f->rect.top, f->rect.width, 1146 f->rect.height, f->f_width, f->f_height); 1147 1148 return 0; 1149 } 1150 1151 static int fimc_lite_subdev_set_selection(struct v4l2_subdev *sd, 1152 struct v4l2_subdev_state *sd_state, 1153 struct v4l2_subdev_selection *sel) 1154 { 1155 struct fimc_lite *fimc = v4l2_get_subdevdata(sd); 1156 struct flite_frame *f = &fimc->inp_frame; 1157 int ret = 0; 1158 1159 if (sel->target != V4L2_SEL_TGT_CROP || sel->pad != FLITE_SD_PAD_SINK) 1160 return -EINVAL; 1161 1162 mutex_lock(&fimc->lock); 1163 fimc_lite_try_crop(fimc, &sel->r); 1164 1165 if (sel->which == V4L2_SUBDEV_FORMAT_TRY) { 1166 *v4l2_subdev_get_try_crop(sd, sd_state, sel->pad) = sel->r; 1167 } else { 1168 unsigned long flags; 1169 spin_lock_irqsave(&fimc->slock, flags); 1170 f->rect = sel->r; 1171 /* Same crop rectangle on the source pad */ 1172 fimc->out_frame.rect = sel->r; 1173 set_bit(ST_FLITE_CONFIG, &fimc->state); 1174 spin_unlock_irqrestore(&fimc->slock, flags); 1175 } 1176 mutex_unlock(&fimc->lock); 1177 1178 v4l2_dbg(1, debug, sd, "%s: (%d,%d) %dx%d, f_w: %d, f_h: %d\n", 1179 __func__, f->rect.left, f->rect.top, f->rect.width, 1180 f->rect.height, f->f_width, f->f_height); 1181 1182 return ret; 1183 } 1184 1185 static int fimc_lite_subdev_s_stream(struct v4l2_subdev *sd, int on) 1186 { 1187 struct fimc_lite *fimc = v4l2_get_subdevdata(sd); 1188 unsigned long flags; 1189 int ret; 1190 1191 /* 1192 * Find sensor subdev linked to FIMC-LITE directly or through 1193 * MIPI-CSIS. This is required for configuration where FIMC-LITE 1194 * is used as a subdev only and feeds data internally to FIMC-IS. 1195 * The pipeline links are protected through entity.pipe so there is no 1196 * need to take the media graph mutex here. 1197 */ 1198 fimc->sensor = fimc_find_remote_sensor(&sd->entity); 1199 1200 if (atomic_read(&fimc->out_path) != FIMC_IO_ISP) 1201 return -ENOIOCTLCMD; 1202 1203 mutex_lock(&fimc->lock); 1204 if (on) { 1205 flite_hw_reset(fimc); 1206 ret = fimc_lite_hw_init(fimc, true); 1207 if (!ret) { 1208 spin_lock_irqsave(&fimc->slock, flags); 1209 flite_hw_capture_start(fimc); 1210 spin_unlock_irqrestore(&fimc->slock, flags); 1211 } 1212 } else { 1213 set_bit(ST_FLITE_OFF, &fimc->state); 1214 1215 spin_lock_irqsave(&fimc->slock, flags); 1216 flite_hw_capture_stop(fimc); 1217 spin_unlock_irqrestore(&fimc->slock, flags); 1218 1219 ret = wait_event_timeout(fimc->irq_queue, 1220 !test_bit(ST_FLITE_OFF, &fimc->state), 1221 msecs_to_jiffies(200)); 1222 if (ret == 0) 1223 v4l2_err(sd, "s_stream(0) timeout\n"); 1224 clear_bit(ST_FLITE_RUN, &fimc->state); 1225 } 1226 1227 mutex_unlock(&fimc->lock); 1228 return ret; 1229 } 1230 1231 static int fimc_lite_log_status(struct v4l2_subdev *sd) 1232 { 1233 struct fimc_lite *fimc = v4l2_get_subdevdata(sd); 1234 1235 flite_hw_dump_regs(fimc, __func__); 1236 return 0; 1237 } 1238 1239 static int fimc_lite_subdev_registered(struct v4l2_subdev *sd) 1240 { 1241 struct fimc_lite *fimc = v4l2_get_subdevdata(sd); 1242 struct vb2_queue *q = &fimc->vb_queue; 1243 struct video_device *vfd = &fimc->ve.vdev; 1244 int ret; 1245 1246 memset(vfd, 0, sizeof(*vfd)); 1247 atomic_set(&fimc->out_path, FIMC_IO_DMA); 1248 1249 snprintf(vfd->name, sizeof(vfd->name), "fimc-lite.%d.capture", 1250 fimc->index); 1251 1252 vfd->fops = &fimc_lite_fops; 1253 vfd->ioctl_ops = &fimc_lite_ioctl_ops; 1254 vfd->v4l2_dev = sd->v4l2_dev; 1255 vfd->minor = -1; 1256 vfd->release = video_device_release_empty; 1257 vfd->queue = q; 1258 vfd->device_caps = V4L2_CAP_VIDEO_CAPTURE_MPLANE | V4L2_CAP_STREAMING; 1259 fimc->reqbufs_count = 0; 1260 1261 INIT_LIST_HEAD(&fimc->pending_buf_q); 1262 INIT_LIST_HEAD(&fimc->active_buf_q); 1263 1264 memset(q, 0, sizeof(*q)); 1265 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; 1266 q->io_modes = VB2_MMAP | VB2_USERPTR; 1267 q->ops = &fimc_lite_qops; 1268 q->mem_ops = &vb2_dma_contig_memops; 1269 q->buf_struct_size = sizeof(struct flite_buffer); 1270 q->drv_priv = fimc; 1271 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 1272 q->lock = &fimc->lock; 1273 q->dev = &fimc->pdev->dev; 1274 1275 ret = vb2_queue_init(q); 1276 if (ret < 0) 1277 return ret; 1278 1279 fimc->vd_pad.flags = MEDIA_PAD_FL_SINK; 1280 ret = media_entity_pads_init(&vfd->entity, 1, &fimc->vd_pad); 1281 if (ret < 0) 1282 return ret; 1283 1284 video_set_drvdata(vfd, fimc); 1285 fimc->ve.pipe = v4l2_get_subdev_hostdata(sd); 1286 1287 ret = video_register_device(vfd, VFL_TYPE_VIDEO, -1); 1288 if (ret < 0) { 1289 media_entity_cleanup(&vfd->entity); 1290 fimc->ve.pipe = NULL; 1291 return ret; 1292 } 1293 1294 v4l2_info(sd->v4l2_dev, "Registered %s as /dev/%s\n", 1295 vfd->name, video_device_node_name(vfd)); 1296 return 0; 1297 } 1298 1299 static void fimc_lite_subdev_unregistered(struct v4l2_subdev *sd) 1300 { 1301 struct fimc_lite *fimc = v4l2_get_subdevdata(sd); 1302 1303 if (fimc == NULL) 1304 return; 1305 1306 mutex_lock(&fimc->lock); 1307 1308 if (video_is_registered(&fimc->ve.vdev)) { 1309 video_unregister_device(&fimc->ve.vdev); 1310 media_entity_cleanup(&fimc->ve.vdev.entity); 1311 fimc->ve.pipe = NULL; 1312 } 1313 1314 mutex_unlock(&fimc->lock); 1315 } 1316 1317 static const struct v4l2_subdev_internal_ops fimc_lite_subdev_internal_ops = { 1318 .registered = fimc_lite_subdev_registered, 1319 .unregistered = fimc_lite_subdev_unregistered, 1320 }; 1321 1322 static const struct v4l2_subdev_pad_ops fimc_lite_subdev_pad_ops = { 1323 .enum_mbus_code = fimc_lite_subdev_enum_mbus_code, 1324 .get_selection = fimc_lite_subdev_get_selection, 1325 .set_selection = fimc_lite_subdev_set_selection, 1326 .get_fmt = fimc_lite_subdev_get_fmt, 1327 .set_fmt = fimc_lite_subdev_set_fmt, 1328 }; 1329 1330 static const struct v4l2_subdev_video_ops fimc_lite_subdev_video_ops = { 1331 .s_stream = fimc_lite_subdev_s_stream, 1332 }; 1333 1334 static const struct v4l2_subdev_core_ops fimc_lite_core_ops = { 1335 .log_status = fimc_lite_log_status, 1336 }; 1337 1338 static const struct v4l2_subdev_ops fimc_lite_subdev_ops = { 1339 .core = &fimc_lite_core_ops, 1340 .video = &fimc_lite_subdev_video_ops, 1341 .pad = &fimc_lite_subdev_pad_ops, 1342 }; 1343 1344 static int fimc_lite_s_ctrl(struct v4l2_ctrl *ctrl) 1345 { 1346 struct fimc_lite *fimc = container_of(ctrl->handler, struct fimc_lite, 1347 ctrl_handler); 1348 set_bit(ST_FLITE_CONFIG, &fimc->state); 1349 return 0; 1350 } 1351 1352 static const struct v4l2_ctrl_ops fimc_lite_ctrl_ops = { 1353 .s_ctrl = fimc_lite_s_ctrl, 1354 }; 1355 1356 static const struct v4l2_ctrl_config fimc_lite_ctrl = { 1357 .ops = &fimc_lite_ctrl_ops, 1358 .id = V4L2_CTRL_CLASS_USER | 0x1001, 1359 .type = V4L2_CTRL_TYPE_BOOLEAN, 1360 .name = "Test Pattern 640x480", 1361 .step = 1, 1362 }; 1363 1364 static void fimc_lite_set_default_config(struct fimc_lite *fimc) 1365 { 1366 struct flite_frame *sink = &fimc->inp_frame; 1367 struct flite_frame *source = &fimc->out_frame; 1368 1369 sink->fmt = &fimc_lite_formats[0]; 1370 sink->f_width = FLITE_DEFAULT_WIDTH; 1371 sink->f_height = FLITE_DEFAULT_HEIGHT; 1372 1373 sink->rect.width = FLITE_DEFAULT_WIDTH; 1374 sink->rect.height = FLITE_DEFAULT_HEIGHT; 1375 sink->rect.left = 0; 1376 sink->rect.top = 0; 1377 1378 *source = *sink; 1379 } 1380 1381 static int fimc_lite_create_capture_subdev(struct fimc_lite *fimc) 1382 { 1383 struct v4l2_ctrl_handler *handler = &fimc->ctrl_handler; 1384 struct v4l2_subdev *sd = &fimc->subdev; 1385 int ret; 1386 1387 v4l2_subdev_init(sd, &fimc_lite_subdev_ops); 1388 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; 1389 snprintf(sd->name, sizeof(sd->name), "FIMC-LITE.%d", fimc->index); 1390 1391 fimc->subdev_pads[FLITE_SD_PAD_SINK].flags = MEDIA_PAD_FL_SINK; 1392 fimc->subdev_pads[FLITE_SD_PAD_SOURCE_DMA].flags = MEDIA_PAD_FL_SOURCE; 1393 fimc->subdev_pads[FLITE_SD_PAD_SOURCE_ISP].flags = MEDIA_PAD_FL_SOURCE; 1394 ret = media_entity_pads_init(&sd->entity, FLITE_SD_PADS_NUM, 1395 fimc->subdev_pads); 1396 if (ret) 1397 return ret; 1398 1399 v4l2_ctrl_handler_init(handler, 1); 1400 fimc->test_pattern = v4l2_ctrl_new_custom(handler, &fimc_lite_ctrl, 1401 NULL); 1402 if (handler->error) { 1403 media_entity_cleanup(&sd->entity); 1404 return handler->error; 1405 } 1406 1407 sd->ctrl_handler = handler; 1408 sd->internal_ops = &fimc_lite_subdev_internal_ops; 1409 sd->entity.function = MEDIA_ENT_F_PROC_VIDEO_SCALER; 1410 sd->entity.ops = &fimc_lite_subdev_media_ops; 1411 sd->owner = THIS_MODULE; 1412 v4l2_set_subdevdata(sd, fimc); 1413 1414 return 0; 1415 } 1416 1417 static void fimc_lite_unregister_capture_subdev(struct fimc_lite *fimc) 1418 { 1419 struct v4l2_subdev *sd = &fimc->subdev; 1420 1421 v4l2_device_unregister_subdev(sd); 1422 media_entity_cleanup(&sd->entity); 1423 v4l2_ctrl_handler_free(&fimc->ctrl_handler); 1424 v4l2_set_subdevdata(sd, NULL); 1425 } 1426 1427 static void fimc_lite_clk_put(struct fimc_lite *fimc) 1428 { 1429 if (IS_ERR(fimc->clock)) 1430 return; 1431 1432 clk_put(fimc->clock); 1433 fimc->clock = ERR_PTR(-EINVAL); 1434 } 1435 1436 static int fimc_lite_clk_get(struct fimc_lite *fimc) 1437 { 1438 fimc->clock = clk_get(&fimc->pdev->dev, FLITE_CLK_NAME); 1439 return PTR_ERR_OR_ZERO(fimc->clock); 1440 } 1441 1442 static const struct of_device_id flite_of_match[]; 1443 1444 static int fimc_lite_probe(struct platform_device *pdev) 1445 { 1446 struct flite_drvdata *drv_data = NULL; 1447 struct device *dev = &pdev->dev; 1448 const struct of_device_id *of_id; 1449 struct fimc_lite *fimc; 1450 struct resource *res; 1451 int ret; 1452 int irq; 1453 1454 if (!dev->of_node) 1455 return -ENODEV; 1456 1457 fimc = devm_kzalloc(dev, sizeof(*fimc), GFP_KERNEL); 1458 if (!fimc) 1459 return -ENOMEM; 1460 1461 of_id = of_match_node(flite_of_match, dev->of_node); 1462 if (of_id) 1463 drv_data = (struct flite_drvdata *)of_id->data; 1464 fimc->index = of_alias_get_id(dev->of_node, "fimc-lite"); 1465 1466 if (!drv_data || fimc->index >= drv_data->num_instances || 1467 fimc->index < 0) { 1468 dev_err(dev, "Wrong %pOF node alias\n", dev->of_node); 1469 return -EINVAL; 1470 } 1471 1472 fimc->dd = drv_data; 1473 fimc->pdev = pdev; 1474 1475 init_waitqueue_head(&fimc->irq_queue); 1476 spin_lock_init(&fimc->slock); 1477 mutex_init(&fimc->lock); 1478 1479 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1480 fimc->regs = devm_ioremap_resource(dev, res); 1481 if (IS_ERR(fimc->regs)) 1482 return PTR_ERR(fimc->regs); 1483 1484 irq = platform_get_irq(pdev, 0); 1485 if (irq < 0) 1486 return irq; 1487 1488 ret = fimc_lite_clk_get(fimc); 1489 if (ret) 1490 return ret; 1491 1492 ret = devm_request_irq(dev, irq, flite_irq_handler, 1493 0, dev_name(dev), fimc); 1494 if (ret) { 1495 dev_err(dev, "Failed to install irq (%d)\n", ret); 1496 goto err_clk_put; 1497 } 1498 1499 /* The video node will be created within the subdev's registered() op */ 1500 ret = fimc_lite_create_capture_subdev(fimc); 1501 if (ret) 1502 goto err_clk_put; 1503 1504 platform_set_drvdata(pdev, fimc); 1505 pm_runtime_enable(dev); 1506 1507 if (!pm_runtime_enabled(dev)) { 1508 ret = clk_prepare_enable(fimc->clock); 1509 if (ret < 0) 1510 goto err_sd; 1511 } 1512 1513 vb2_dma_contig_set_max_seg_size(dev, DMA_BIT_MASK(32)); 1514 1515 fimc_lite_set_default_config(fimc); 1516 1517 dev_dbg(dev, "FIMC-LITE.%d registered successfully\n", 1518 fimc->index); 1519 return 0; 1520 1521 err_sd: 1522 fimc_lite_unregister_capture_subdev(fimc); 1523 err_clk_put: 1524 fimc_lite_clk_put(fimc); 1525 return ret; 1526 } 1527 1528 #ifdef CONFIG_PM 1529 static int fimc_lite_runtime_resume(struct device *dev) 1530 { 1531 struct fimc_lite *fimc = dev_get_drvdata(dev); 1532 1533 clk_prepare_enable(fimc->clock); 1534 return 0; 1535 } 1536 1537 static int fimc_lite_runtime_suspend(struct device *dev) 1538 { 1539 struct fimc_lite *fimc = dev_get_drvdata(dev); 1540 1541 clk_disable_unprepare(fimc->clock); 1542 return 0; 1543 } 1544 #endif 1545 1546 #ifdef CONFIG_PM_SLEEP 1547 static int fimc_lite_resume(struct device *dev) 1548 { 1549 struct fimc_lite *fimc = dev_get_drvdata(dev); 1550 struct flite_buffer *buf; 1551 unsigned long flags; 1552 int i; 1553 1554 spin_lock_irqsave(&fimc->slock, flags); 1555 if (!test_and_clear_bit(ST_LPM, &fimc->state) || 1556 !test_bit(ST_FLITE_IN_USE, &fimc->state)) { 1557 spin_unlock_irqrestore(&fimc->slock, flags); 1558 return 0; 1559 } 1560 flite_hw_reset(fimc); 1561 spin_unlock_irqrestore(&fimc->slock, flags); 1562 1563 if (!test_and_clear_bit(ST_FLITE_SUSPENDED, &fimc->state)) 1564 return 0; 1565 1566 INIT_LIST_HEAD(&fimc->active_buf_q); 1567 fimc_pipeline_call(&fimc->ve, open, 1568 &fimc->ve.vdev.entity, false); 1569 fimc_lite_hw_init(fimc, atomic_read(&fimc->out_path) == FIMC_IO_ISP); 1570 clear_bit(ST_FLITE_SUSPENDED, &fimc->state); 1571 1572 for (i = 0; i < fimc->reqbufs_count; i++) { 1573 if (list_empty(&fimc->pending_buf_q)) 1574 break; 1575 buf = fimc_lite_pending_queue_pop(fimc); 1576 buffer_queue(&buf->vb.vb2_buf); 1577 } 1578 return 0; 1579 } 1580 1581 static int fimc_lite_suspend(struct device *dev) 1582 { 1583 struct fimc_lite *fimc = dev_get_drvdata(dev); 1584 bool suspend = test_bit(ST_FLITE_IN_USE, &fimc->state); 1585 int ret; 1586 1587 if (test_and_set_bit(ST_LPM, &fimc->state)) 1588 return 0; 1589 1590 ret = fimc_lite_stop_capture(fimc, suspend); 1591 if (ret < 0 || !fimc_lite_active(fimc)) 1592 return ret; 1593 1594 return fimc_pipeline_call(&fimc->ve, close); 1595 } 1596 #endif /* CONFIG_PM_SLEEP */ 1597 1598 static int fimc_lite_remove(struct platform_device *pdev) 1599 { 1600 struct fimc_lite *fimc = platform_get_drvdata(pdev); 1601 struct device *dev = &pdev->dev; 1602 1603 if (!pm_runtime_enabled(dev)) 1604 clk_disable_unprepare(fimc->clock); 1605 1606 pm_runtime_disable(dev); 1607 pm_runtime_set_suspended(dev); 1608 fimc_lite_unregister_capture_subdev(fimc); 1609 vb2_dma_contig_clear_max_seg_size(dev); 1610 fimc_lite_clk_put(fimc); 1611 1612 dev_info(dev, "Driver unloaded\n"); 1613 return 0; 1614 } 1615 1616 static const struct dev_pm_ops fimc_lite_pm_ops = { 1617 SET_SYSTEM_SLEEP_PM_OPS(fimc_lite_suspend, fimc_lite_resume) 1618 SET_RUNTIME_PM_OPS(fimc_lite_runtime_suspend, fimc_lite_runtime_resume, 1619 NULL) 1620 }; 1621 1622 /* EXYNOS4412 */ 1623 static struct flite_drvdata fimc_lite_drvdata_exynos4 = { 1624 .max_width = 8192, 1625 .max_height = 8192, 1626 .out_width_align = 8, 1627 .win_hor_offs_align = 2, 1628 .out_hor_offs_align = 8, 1629 .max_dma_bufs = 1, 1630 .num_instances = 2, 1631 }; 1632 1633 /* EXYNOS5250 */ 1634 static struct flite_drvdata fimc_lite_drvdata_exynos5 = { 1635 .max_width = 8192, 1636 .max_height = 8192, 1637 .out_width_align = 8, 1638 .win_hor_offs_align = 2, 1639 .out_hor_offs_align = 8, 1640 .max_dma_bufs = 32, 1641 .num_instances = 3, 1642 }; 1643 1644 static const struct of_device_id flite_of_match[] = { 1645 { 1646 .compatible = "samsung,exynos4212-fimc-lite", 1647 .data = &fimc_lite_drvdata_exynos4, 1648 }, 1649 { 1650 .compatible = "samsung,exynos5250-fimc-lite", 1651 .data = &fimc_lite_drvdata_exynos5, 1652 }, 1653 { /* sentinel */ }, 1654 }; 1655 MODULE_DEVICE_TABLE(of, flite_of_match); 1656 1657 static struct platform_driver fimc_lite_driver = { 1658 .probe = fimc_lite_probe, 1659 .remove = fimc_lite_remove, 1660 .driver = { 1661 .of_match_table = flite_of_match, 1662 .name = FIMC_LITE_DRV_NAME, 1663 .pm = &fimc_lite_pm_ops, 1664 } 1665 }; 1666 module_platform_driver(fimc_lite_driver); 1667 MODULE_LICENSE("GPL"); 1668 MODULE_ALIAS("platform:" FIMC_LITE_DRV_NAME); 1669