1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * camss-vfe.c 4 * 5 * Qualcomm MSM Camera Subsystem - VFE (Video Front End) Module 6 * 7 * Copyright (c) 2013-2015, The Linux Foundation. All rights reserved. 8 * Copyright (C) 2015-2018 Linaro Ltd. 9 */ 10 #include <linux/clk.h> 11 #include <linux/completion.h> 12 #include <linux/interrupt.h> 13 #include <linux/iommu.h> 14 #include <linux/mutex.h> 15 #include <linux/of.h> 16 #include <linux/platform_device.h> 17 #include <linux/pm_runtime.h> 18 #include <linux/spinlock_types.h> 19 #include <linux/spinlock.h> 20 #include <media/media-entity.h> 21 #include <media/v4l2-device.h> 22 #include <media/v4l2-subdev.h> 23 24 #include "camss-vfe.h" 25 #include "camss.h" 26 27 #define MSM_VFE_NAME "msm_vfe" 28 29 #define vfe_line_array(ptr_line) \ 30 ((const struct vfe_line (*)[]) &(ptr_line[-(ptr_line->id)])) 31 32 #define to_vfe(ptr_line) \ 33 container_of(vfe_line_array(ptr_line), struct vfe_device, line) 34 35 /* VFE reset timeout */ 36 #define VFE_RESET_TIMEOUT_MS 50 37 /* VFE halt timeout */ 38 #define VFE_HALT_TIMEOUT_MS 100 39 /* Max number of frame drop updates per frame */ 40 #define VFE_FRAME_DROP_UPDATES 2 41 /* Frame drop value. VAL + UPDATES - 1 should not exceed 31 */ 42 #define VFE_FRAME_DROP_VAL 30 43 44 #define VFE_NEXT_SOF_MS 500 45 46 #define SCALER_RATIO_MAX 16 47 48 struct vfe_format { 49 u32 code; 50 u8 bpp; 51 }; 52 53 static const struct vfe_format formats_rdi_8x16[] = { 54 { MEDIA_BUS_FMT_UYVY8_2X8, 8 }, 55 { MEDIA_BUS_FMT_VYUY8_2X8, 8 }, 56 { MEDIA_BUS_FMT_YUYV8_2X8, 8 }, 57 { MEDIA_BUS_FMT_YVYU8_2X8, 8 }, 58 { MEDIA_BUS_FMT_SBGGR8_1X8, 8 }, 59 { MEDIA_BUS_FMT_SGBRG8_1X8, 8 }, 60 { MEDIA_BUS_FMT_SGRBG8_1X8, 8 }, 61 { MEDIA_BUS_FMT_SRGGB8_1X8, 8 }, 62 { MEDIA_BUS_FMT_SBGGR10_1X10, 10 }, 63 { MEDIA_BUS_FMT_SGBRG10_1X10, 10 }, 64 { MEDIA_BUS_FMT_SGRBG10_1X10, 10 }, 65 { MEDIA_BUS_FMT_SRGGB10_1X10, 10 }, 66 { MEDIA_BUS_FMT_SBGGR12_1X12, 12 }, 67 { MEDIA_BUS_FMT_SGBRG12_1X12, 12 }, 68 { MEDIA_BUS_FMT_SGRBG12_1X12, 12 }, 69 { MEDIA_BUS_FMT_SRGGB12_1X12, 12 }, 70 { MEDIA_BUS_FMT_Y10_1X10, 10 }, 71 }; 72 73 static const struct vfe_format formats_pix_8x16[] = { 74 { MEDIA_BUS_FMT_UYVY8_2X8, 8 }, 75 { MEDIA_BUS_FMT_VYUY8_2X8, 8 }, 76 { MEDIA_BUS_FMT_YUYV8_2X8, 8 }, 77 { MEDIA_BUS_FMT_YVYU8_2X8, 8 }, 78 }; 79 80 static const struct vfe_format formats_rdi_8x96[] = { 81 { MEDIA_BUS_FMT_UYVY8_2X8, 8 }, 82 { MEDIA_BUS_FMT_VYUY8_2X8, 8 }, 83 { MEDIA_BUS_FMT_YUYV8_2X8, 8 }, 84 { MEDIA_BUS_FMT_YVYU8_2X8, 8 }, 85 { MEDIA_BUS_FMT_SBGGR8_1X8, 8 }, 86 { MEDIA_BUS_FMT_SGBRG8_1X8, 8 }, 87 { MEDIA_BUS_FMT_SGRBG8_1X8, 8 }, 88 { MEDIA_BUS_FMT_SRGGB8_1X8, 8 }, 89 { MEDIA_BUS_FMT_SBGGR10_1X10, 10 }, 90 { MEDIA_BUS_FMT_SGBRG10_1X10, 10 }, 91 { MEDIA_BUS_FMT_SGRBG10_1X10, 10 }, 92 { MEDIA_BUS_FMT_SRGGB10_1X10, 10 }, 93 { MEDIA_BUS_FMT_SBGGR10_2X8_PADHI_LE, 16 }, 94 { MEDIA_BUS_FMT_SBGGR12_1X12, 12 }, 95 { MEDIA_BUS_FMT_SGBRG12_1X12, 12 }, 96 { MEDIA_BUS_FMT_SGRBG12_1X12, 12 }, 97 { MEDIA_BUS_FMT_SRGGB12_1X12, 12 }, 98 { MEDIA_BUS_FMT_SBGGR14_1X14, 14 }, 99 { MEDIA_BUS_FMT_SGBRG14_1X14, 14 }, 100 { MEDIA_BUS_FMT_SGRBG14_1X14, 14 }, 101 { MEDIA_BUS_FMT_SRGGB14_1X14, 14 }, 102 { MEDIA_BUS_FMT_Y10_1X10, 10 }, 103 { MEDIA_BUS_FMT_Y10_2X8_PADHI_LE, 16 }, 104 }; 105 106 static const struct vfe_format formats_pix_8x96[] = { 107 { MEDIA_BUS_FMT_UYVY8_2X8, 8 }, 108 { MEDIA_BUS_FMT_VYUY8_2X8, 8 }, 109 { MEDIA_BUS_FMT_YUYV8_2X8, 8 }, 110 { MEDIA_BUS_FMT_YVYU8_2X8, 8 }, 111 }; 112 113 /* 114 * vfe_get_bpp - map media bus format to bits per pixel 115 * @formats: supported media bus formats array 116 * @nformats: size of @formats array 117 * @code: media bus format code 118 * 119 * Return number of bits per pixel 120 */ 121 static u8 vfe_get_bpp(const struct vfe_format *formats, 122 unsigned int nformats, u32 code) 123 { 124 unsigned int i; 125 126 for (i = 0; i < nformats; i++) 127 if (code == formats[i].code) 128 return formats[i].bpp; 129 130 WARN(1, "Unknown format\n"); 131 132 return formats[0].bpp; 133 } 134 135 static u32 vfe_find_code(u32 *code, unsigned int n_code, 136 unsigned int index, u32 req_code) 137 { 138 int i; 139 140 if (!req_code && (index >= n_code)) 141 return 0; 142 143 for (i = 0; i < n_code; i++) 144 if (req_code) { 145 if (req_code == code[i]) 146 return req_code; 147 } else { 148 if (i == index) 149 return code[i]; 150 } 151 152 return code[0]; 153 } 154 155 static u32 vfe_src_pad_code(struct vfe_line *line, u32 sink_code, 156 unsigned int index, u32 src_req_code) 157 { 158 struct vfe_device *vfe = to_vfe(line); 159 160 if (vfe->camss->version == CAMSS_8x16) 161 switch (sink_code) { 162 case MEDIA_BUS_FMT_YUYV8_2X8: 163 { 164 u32 src_code[] = { 165 MEDIA_BUS_FMT_YUYV8_2X8, 166 MEDIA_BUS_FMT_YUYV8_1_5X8, 167 }; 168 169 return vfe_find_code(src_code, ARRAY_SIZE(src_code), 170 index, src_req_code); 171 } 172 case MEDIA_BUS_FMT_YVYU8_2X8: 173 { 174 u32 src_code[] = { 175 MEDIA_BUS_FMT_YVYU8_2X8, 176 MEDIA_BUS_FMT_YVYU8_1_5X8, 177 }; 178 179 return vfe_find_code(src_code, ARRAY_SIZE(src_code), 180 index, src_req_code); 181 } 182 case MEDIA_BUS_FMT_UYVY8_2X8: 183 { 184 u32 src_code[] = { 185 MEDIA_BUS_FMT_UYVY8_2X8, 186 MEDIA_BUS_FMT_UYVY8_1_5X8, 187 }; 188 189 return vfe_find_code(src_code, ARRAY_SIZE(src_code), 190 index, src_req_code); 191 } 192 case MEDIA_BUS_FMT_VYUY8_2X8: 193 { 194 u32 src_code[] = { 195 MEDIA_BUS_FMT_VYUY8_2X8, 196 MEDIA_BUS_FMT_VYUY8_1_5X8, 197 }; 198 199 return vfe_find_code(src_code, ARRAY_SIZE(src_code), 200 index, src_req_code); 201 } 202 default: 203 if (index > 0) 204 return 0; 205 206 return sink_code; 207 } 208 else if (vfe->camss->version == CAMSS_8x96 || 209 vfe->camss->version == CAMSS_660) 210 switch (sink_code) { 211 case MEDIA_BUS_FMT_YUYV8_2X8: 212 { 213 u32 src_code[] = { 214 MEDIA_BUS_FMT_YUYV8_2X8, 215 MEDIA_BUS_FMT_YVYU8_2X8, 216 MEDIA_BUS_FMT_UYVY8_2X8, 217 MEDIA_BUS_FMT_VYUY8_2X8, 218 MEDIA_BUS_FMT_YUYV8_1_5X8, 219 }; 220 221 return vfe_find_code(src_code, ARRAY_SIZE(src_code), 222 index, src_req_code); 223 } 224 case MEDIA_BUS_FMT_YVYU8_2X8: 225 { 226 u32 src_code[] = { 227 MEDIA_BUS_FMT_YVYU8_2X8, 228 MEDIA_BUS_FMT_YUYV8_2X8, 229 MEDIA_BUS_FMT_UYVY8_2X8, 230 MEDIA_BUS_FMT_VYUY8_2X8, 231 MEDIA_BUS_FMT_YVYU8_1_5X8, 232 }; 233 234 return vfe_find_code(src_code, ARRAY_SIZE(src_code), 235 index, src_req_code); 236 } 237 case MEDIA_BUS_FMT_UYVY8_2X8: 238 { 239 u32 src_code[] = { 240 MEDIA_BUS_FMT_UYVY8_2X8, 241 MEDIA_BUS_FMT_YUYV8_2X8, 242 MEDIA_BUS_FMT_YVYU8_2X8, 243 MEDIA_BUS_FMT_VYUY8_2X8, 244 MEDIA_BUS_FMT_UYVY8_1_5X8, 245 }; 246 247 return vfe_find_code(src_code, ARRAY_SIZE(src_code), 248 index, src_req_code); 249 } 250 case MEDIA_BUS_FMT_VYUY8_2X8: 251 { 252 u32 src_code[] = { 253 MEDIA_BUS_FMT_VYUY8_2X8, 254 MEDIA_BUS_FMT_YUYV8_2X8, 255 MEDIA_BUS_FMT_YVYU8_2X8, 256 MEDIA_BUS_FMT_UYVY8_2X8, 257 MEDIA_BUS_FMT_VYUY8_1_5X8, 258 }; 259 260 return vfe_find_code(src_code, ARRAY_SIZE(src_code), 261 index, src_req_code); 262 } 263 default: 264 if (index > 0) 265 return 0; 266 267 return sink_code; 268 } 269 else 270 return 0; 271 } 272 273 /* 274 * vfe_reset - Trigger reset on VFE module and wait to complete 275 * @vfe: VFE device 276 * 277 * Return 0 on success or a negative error code otherwise 278 */ 279 static int vfe_reset(struct vfe_device *vfe) 280 { 281 unsigned long time; 282 283 reinit_completion(&vfe->reset_complete); 284 285 vfe->ops->global_reset(vfe); 286 287 time = wait_for_completion_timeout(&vfe->reset_complete, 288 msecs_to_jiffies(VFE_RESET_TIMEOUT_MS)); 289 if (!time) { 290 dev_err(vfe->camss->dev, "VFE reset timeout\n"); 291 return -EIO; 292 } 293 294 return 0; 295 } 296 297 /* 298 * vfe_halt - Trigger halt on VFE module and wait to complete 299 * @vfe: VFE device 300 * 301 * Return 0 on success or a negative error code otherwise 302 */ 303 static int vfe_halt(struct vfe_device *vfe) 304 { 305 unsigned long time; 306 307 reinit_completion(&vfe->halt_complete); 308 309 vfe->ops->halt_request(vfe); 310 311 time = wait_for_completion_timeout(&vfe->halt_complete, 312 msecs_to_jiffies(VFE_HALT_TIMEOUT_MS)); 313 if (!time) { 314 dev_err(vfe->camss->dev, "VFE halt timeout\n"); 315 return -EIO; 316 } 317 318 return 0; 319 } 320 321 static void vfe_init_outputs(struct vfe_device *vfe) 322 { 323 int i; 324 325 for (i = 0; i < ARRAY_SIZE(vfe->line); i++) { 326 struct vfe_output *output = &vfe->line[i].output; 327 328 output->state = VFE_OUTPUT_OFF; 329 output->buf[0] = NULL; 330 output->buf[1] = NULL; 331 INIT_LIST_HEAD(&output->pending_bufs); 332 } 333 } 334 335 static void vfe_reset_output_maps(struct vfe_device *vfe) 336 { 337 int i; 338 339 for (i = 0; i < ARRAY_SIZE(vfe->wm_output_map); i++) 340 vfe->wm_output_map[i] = VFE_LINE_NONE; 341 } 342 343 static void vfe_output_init_addrs(struct vfe_device *vfe, 344 struct vfe_output *output, u8 sync) 345 { 346 u32 ping_addr; 347 u32 pong_addr; 348 unsigned int i; 349 350 output->active_buf = 0; 351 352 for (i = 0; i < output->wm_num; i++) { 353 if (output->buf[0]) 354 ping_addr = output->buf[0]->addr[i]; 355 else 356 ping_addr = 0; 357 358 if (output->buf[1]) 359 pong_addr = output->buf[1]->addr[i]; 360 else 361 pong_addr = ping_addr; 362 363 vfe->ops->wm_set_ping_addr(vfe, output->wm_idx[i], ping_addr); 364 vfe->ops->wm_set_pong_addr(vfe, output->wm_idx[i], pong_addr); 365 if (sync) 366 vfe->ops->bus_reload_wm(vfe, output->wm_idx[i]); 367 } 368 } 369 370 static void vfe_output_update_ping_addr(struct vfe_device *vfe, 371 struct vfe_output *output, u8 sync) 372 { 373 u32 addr; 374 unsigned int i; 375 376 for (i = 0; i < output->wm_num; i++) { 377 if (output->buf[0]) 378 addr = output->buf[0]->addr[i]; 379 else 380 addr = 0; 381 382 vfe->ops->wm_set_ping_addr(vfe, output->wm_idx[i], addr); 383 if (sync) 384 vfe->ops->bus_reload_wm(vfe, output->wm_idx[i]); 385 } 386 } 387 388 static void vfe_output_update_pong_addr(struct vfe_device *vfe, 389 struct vfe_output *output, u8 sync) 390 { 391 u32 addr; 392 unsigned int i; 393 394 for (i = 0; i < output->wm_num; i++) { 395 if (output->buf[1]) 396 addr = output->buf[1]->addr[i]; 397 else 398 addr = 0; 399 400 vfe->ops->wm_set_pong_addr(vfe, output->wm_idx[i], addr); 401 if (sync) 402 vfe->ops->bus_reload_wm(vfe, output->wm_idx[i]); 403 } 404 405 } 406 407 static int vfe_reserve_wm(struct vfe_device *vfe, enum vfe_line_id line_id) 408 { 409 int ret = -EBUSY; 410 int i; 411 412 for (i = 0; i < ARRAY_SIZE(vfe->wm_output_map); i++) { 413 if (vfe->wm_output_map[i] == VFE_LINE_NONE) { 414 vfe->wm_output_map[i] = line_id; 415 ret = i; 416 break; 417 } 418 } 419 420 return ret; 421 } 422 423 static int vfe_release_wm(struct vfe_device *vfe, u8 wm) 424 { 425 if (wm >= ARRAY_SIZE(vfe->wm_output_map)) 426 return -EINVAL; 427 428 vfe->wm_output_map[wm] = VFE_LINE_NONE; 429 430 return 0; 431 } 432 433 static void vfe_output_frame_drop(struct vfe_device *vfe, 434 struct vfe_output *output, 435 u32 drop_pattern) 436 { 437 u8 drop_period; 438 unsigned int i; 439 440 /* We need to toggle update period to be valid on next frame */ 441 output->drop_update_idx++; 442 output->drop_update_idx %= VFE_FRAME_DROP_UPDATES; 443 drop_period = VFE_FRAME_DROP_VAL + output->drop_update_idx; 444 445 for (i = 0; i < output->wm_num; i++) { 446 vfe->ops->wm_set_framedrop_period(vfe, output->wm_idx[i], 447 drop_period); 448 vfe->ops->wm_set_framedrop_pattern(vfe, output->wm_idx[i], 449 drop_pattern); 450 } 451 vfe->ops->reg_update(vfe, 452 container_of(output, struct vfe_line, output)->id); 453 } 454 455 static struct camss_buffer *vfe_buf_get_pending(struct vfe_output *output) 456 { 457 struct camss_buffer *buffer = NULL; 458 459 if (!list_empty(&output->pending_bufs)) { 460 buffer = list_first_entry(&output->pending_bufs, 461 struct camss_buffer, 462 queue); 463 list_del(&buffer->queue); 464 } 465 466 return buffer; 467 } 468 469 /* 470 * vfe_buf_add_pending - Add output buffer to list of pending 471 * @output: VFE output 472 * @buffer: Video buffer 473 */ 474 static void vfe_buf_add_pending(struct vfe_output *output, 475 struct camss_buffer *buffer) 476 { 477 INIT_LIST_HEAD(&buffer->queue); 478 list_add_tail(&buffer->queue, &output->pending_bufs); 479 } 480 481 /* 482 * vfe_buf_flush_pending - Flush all pending buffers. 483 * @output: VFE output 484 * @state: vb2 buffer state 485 */ 486 static void vfe_buf_flush_pending(struct vfe_output *output, 487 enum vb2_buffer_state state) 488 { 489 struct camss_buffer *buf; 490 struct camss_buffer *t; 491 492 list_for_each_entry_safe(buf, t, &output->pending_bufs, queue) { 493 vb2_buffer_done(&buf->vb.vb2_buf, state); 494 list_del(&buf->queue); 495 } 496 } 497 498 static void vfe_buf_update_wm_on_next(struct vfe_device *vfe, 499 struct vfe_output *output) 500 { 501 switch (output->state) { 502 case VFE_OUTPUT_CONTINUOUS: 503 vfe_output_frame_drop(vfe, output, 3); 504 break; 505 case VFE_OUTPUT_SINGLE: 506 default: 507 dev_err_ratelimited(vfe->camss->dev, 508 "Next buf in wrong state! %d\n", 509 output->state); 510 break; 511 } 512 } 513 514 static void vfe_buf_update_wm_on_last(struct vfe_device *vfe, 515 struct vfe_output *output) 516 { 517 switch (output->state) { 518 case VFE_OUTPUT_CONTINUOUS: 519 output->state = VFE_OUTPUT_SINGLE; 520 vfe_output_frame_drop(vfe, output, 1); 521 break; 522 case VFE_OUTPUT_SINGLE: 523 output->state = VFE_OUTPUT_STOPPING; 524 vfe_output_frame_drop(vfe, output, 0); 525 break; 526 default: 527 dev_err_ratelimited(vfe->camss->dev, 528 "Last buff in wrong state! %d\n", 529 output->state); 530 break; 531 } 532 } 533 534 static void vfe_buf_update_wm_on_new(struct vfe_device *vfe, 535 struct vfe_output *output, 536 struct camss_buffer *new_buf) 537 { 538 int inactive_idx; 539 540 switch (output->state) { 541 case VFE_OUTPUT_SINGLE: 542 inactive_idx = !output->active_buf; 543 544 if (!output->buf[inactive_idx]) { 545 output->buf[inactive_idx] = new_buf; 546 547 if (inactive_idx) 548 vfe_output_update_pong_addr(vfe, output, 0); 549 else 550 vfe_output_update_ping_addr(vfe, output, 0); 551 552 vfe_output_frame_drop(vfe, output, 3); 553 output->state = VFE_OUTPUT_CONTINUOUS; 554 } else { 555 vfe_buf_add_pending(output, new_buf); 556 dev_err_ratelimited(vfe->camss->dev, 557 "Inactive buffer is busy\n"); 558 } 559 break; 560 561 case VFE_OUTPUT_IDLE: 562 if (!output->buf[0]) { 563 output->buf[0] = new_buf; 564 565 vfe_output_init_addrs(vfe, output, 1); 566 567 vfe_output_frame_drop(vfe, output, 1); 568 output->state = VFE_OUTPUT_SINGLE; 569 } else { 570 vfe_buf_add_pending(output, new_buf); 571 dev_err_ratelimited(vfe->camss->dev, 572 "Output idle with buffer set!\n"); 573 } 574 break; 575 576 case VFE_OUTPUT_CONTINUOUS: 577 default: 578 vfe_buf_add_pending(output, new_buf); 579 break; 580 } 581 } 582 583 static int vfe_get_output(struct vfe_line *line) 584 { 585 struct vfe_device *vfe = to_vfe(line); 586 struct vfe_output *output; 587 struct v4l2_format *f = &line->video_out.active_fmt; 588 unsigned long flags; 589 int i; 590 int wm_idx; 591 592 spin_lock_irqsave(&vfe->output_lock, flags); 593 594 output = &line->output; 595 if (output->state != VFE_OUTPUT_OFF) { 596 dev_err(vfe->camss->dev, "Output is running\n"); 597 goto error; 598 } 599 output->state = VFE_OUTPUT_RESERVED; 600 601 output->active_buf = 0; 602 603 switch (f->fmt.pix_mp.pixelformat) { 604 case V4L2_PIX_FMT_NV12: 605 case V4L2_PIX_FMT_NV21: 606 case V4L2_PIX_FMT_NV16: 607 case V4L2_PIX_FMT_NV61: 608 output->wm_num = 2; 609 break; 610 default: 611 output->wm_num = 1; 612 break; 613 } 614 615 for (i = 0; i < output->wm_num; i++) { 616 wm_idx = vfe_reserve_wm(vfe, line->id); 617 if (wm_idx < 0) { 618 dev_err(vfe->camss->dev, "Can not reserve wm\n"); 619 goto error_get_wm; 620 } 621 output->wm_idx[i] = wm_idx; 622 } 623 624 output->drop_update_idx = 0; 625 626 spin_unlock_irqrestore(&vfe->output_lock, flags); 627 628 return 0; 629 630 error_get_wm: 631 for (i--; i >= 0; i--) 632 vfe_release_wm(vfe, output->wm_idx[i]); 633 output->state = VFE_OUTPUT_OFF; 634 error: 635 spin_unlock_irqrestore(&vfe->output_lock, flags); 636 637 return -EINVAL; 638 } 639 640 static int vfe_put_output(struct vfe_line *line) 641 { 642 struct vfe_device *vfe = to_vfe(line); 643 struct vfe_output *output = &line->output; 644 unsigned long flags; 645 unsigned int i; 646 647 spin_lock_irqsave(&vfe->output_lock, flags); 648 649 for (i = 0; i < output->wm_num; i++) 650 vfe_release_wm(vfe, output->wm_idx[i]); 651 652 output->state = VFE_OUTPUT_OFF; 653 654 spin_unlock_irqrestore(&vfe->output_lock, flags); 655 return 0; 656 } 657 658 static int vfe_enable_output(struct vfe_line *line) 659 { 660 struct vfe_device *vfe = to_vfe(line); 661 struct vfe_output *output = &line->output; 662 const struct vfe_hw_ops *ops = vfe->ops; 663 struct media_entity *sensor; 664 unsigned long flags; 665 unsigned int frame_skip = 0; 666 unsigned int i; 667 u16 ub_size; 668 669 ub_size = ops->get_ub_size(vfe->id); 670 if (!ub_size) 671 return -EINVAL; 672 673 sensor = camss_find_sensor(&line->subdev.entity); 674 if (sensor) { 675 struct v4l2_subdev *subdev = 676 media_entity_to_v4l2_subdev(sensor); 677 678 v4l2_subdev_call(subdev, sensor, g_skip_frames, &frame_skip); 679 /* Max frame skip is 29 frames */ 680 if (frame_skip > VFE_FRAME_DROP_VAL - 1) 681 frame_skip = VFE_FRAME_DROP_VAL - 1; 682 } 683 684 spin_lock_irqsave(&vfe->output_lock, flags); 685 686 ops->reg_update_clear(vfe, line->id); 687 688 if (output->state != VFE_OUTPUT_RESERVED) { 689 dev_err(vfe->camss->dev, "Output is not in reserved state %d\n", 690 output->state); 691 spin_unlock_irqrestore(&vfe->output_lock, flags); 692 return -EINVAL; 693 } 694 output->state = VFE_OUTPUT_IDLE; 695 696 output->buf[0] = vfe_buf_get_pending(output); 697 output->buf[1] = vfe_buf_get_pending(output); 698 699 if (!output->buf[0] && output->buf[1]) { 700 output->buf[0] = output->buf[1]; 701 output->buf[1] = NULL; 702 } 703 704 if (output->buf[0]) 705 output->state = VFE_OUTPUT_SINGLE; 706 707 if (output->buf[1]) 708 output->state = VFE_OUTPUT_CONTINUOUS; 709 710 switch (output->state) { 711 case VFE_OUTPUT_SINGLE: 712 vfe_output_frame_drop(vfe, output, 1 << frame_skip); 713 break; 714 case VFE_OUTPUT_CONTINUOUS: 715 vfe_output_frame_drop(vfe, output, 3 << frame_skip); 716 break; 717 default: 718 vfe_output_frame_drop(vfe, output, 0); 719 break; 720 } 721 722 output->sequence = 0; 723 output->wait_sof = 0; 724 output->wait_reg_update = 0; 725 reinit_completion(&output->sof); 726 reinit_completion(&output->reg_update); 727 728 vfe_output_init_addrs(vfe, output, 0); 729 730 if (line->id != VFE_LINE_PIX) { 731 ops->set_cgc_override(vfe, output->wm_idx[0], 1); 732 ops->enable_irq_wm_line(vfe, output->wm_idx[0], line->id, 1); 733 ops->bus_connect_wm_to_rdi(vfe, output->wm_idx[0], line->id); 734 ops->wm_set_subsample(vfe, output->wm_idx[0]); 735 ops->set_rdi_cid(vfe, line->id, 0); 736 ops->wm_set_ub_cfg(vfe, output->wm_idx[0], 737 (ub_size + 1) * output->wm_idx[0], ub_size); 738 ops->wm_frame_based(vfe, output->wm_idx[0], 1); 739 ops->wm_enable(vfe, output->wm_idx[0], 1); 740 ops->bus_reload_wm(vfe, output->wm_idx[0]); 741 } else { 742 ub_size /= output->wm_num; 743 for (i = 0; i < output->wm_num; i++) { 744 ops->set_cgc_override(vfe, output->wm_idx[i], 1); 745 ops->wm_set_subsample(vfe, output->wm_idx[i]); 746 ops->wm_set_ub_cfg(vfe, output->wm_idx[i], 747 (ub_size + 1) * output->wm_idx[i], 748 ub_size); 749 ops->wm_line_based(vfe, output->wm_idx[i], 750 &line->video_out.active_fmt.fmt.pix_mp, 751 i, 1); 752 ops->wm_enable(vfe, output->wm_idx[i], 1); 753 ops->bus_reload_wm(vfe, output->wm_idx[i]); 754 } 755 ops->enable_irq_pix_line(vfe, 0, line->id, 1); 756 ops->set_module_cfg(vfe, 1); 757 ops->set_camif_cfg(vfe, line); 758 ops->set_realign_cfg(vfe, line, 1); 759 ops->set_xbar_cfg(vfe, output, 1); 760 ops->set_demux_cfg(vfe, line); 761 ops->set_scale_cfg(vfe, line); 762 ops->set_crop_cfg(vfe, line); 763 ops->set_clamp_cfg(vfe); 764 ops->set_camif_cmd(vfe, 1); 765 } 766 767 ops->reg_update(vfe, line->id); 768 769 spin_unlock_irqrestore(&vfe->output_lock, flags); 770 771 return 0; 772 } 773 774 static int vfe_disable_output(struct vfe_line *line) 775 { 776 struct vfe_device *vfe = to_vfe(line); 777 struct vfe_output *output = &line->output; 778 const struct vfe_hw_ops *ops = vfe->ops; 779 unsigned long flags; 780 unsigned long time; 781 unsigned int i; 782 783 spin_lock_irqsave(&vfe->output_lock, flags); 784 785 output->wait_sof = 1; 786 spin_unlock_irqrestore(&vfe->output_lock, flags); 787 788 time = wait_for_completion_timeout(&output->sof, 789 msecs_to_jiffies(VFE_NEXT_SOF_MS)); 790 if (!time) 791 dev_err(vfe->camss->dev, "VFE sof timeout\n"); 792 793 spin_lock_irqsave(&vfe->output_lock, flags); 794 for (i = 0; i < output->wm_num; i++) 795 ops->wm_enable(vfe, output->wm_idx[i], 0); 796 797 ops->reg_update(vfe, line->id); 798 output->wait_reg_update = 1; 799 spin_unlock_irqrestore(&vfe->output_lock, flags); 800 801 time = wait_for_completion_timeout(&output->reg_update, 802 msecs_to_jiffies(VFE_NEXT_SOF_MS)); 803 if (!time) 804 dev_err(vfe->camss->dev, "VFE reg update timeout\n"); 805 806 spin_lock_irqsave(&vfe->output_lock, flags); 807 808 if (line->id != VFE_LINE_PIX) { 809 ops->wm_frame_based(vfe, output->wm_idx[0], 0); 810 ops->bus_disconnect_wm_from_rdi(vfe, output->wm_idx[0], 811 line->id); 812 ops->enable_irq_wm_line(vfe, output->wm_idx[0], line->id, 0); 813 ops->set_cgc_override(vfe, output->wm_idx[0], 0); 814 spin_unlock_irqrestore(&vfe->output_lock, flags); 815 } else { 816 for (i = 0; i < output->wm_num; i++) { 817 ops->wm_line_based(vfe, output->wm_idx[i], NULL, i, 0); 818 ops->set_cgc_override(vfe, output->wm_idx[i], 0); 819 } 820 821 ops->enable_irq_pix_line(vfe, 0, line->id, 0); 822 ops->set_module_cfg(vfe, 0); 823 ops->set_realign_cfg(vfe, line, 0); 824 ops->set_xbar_cfg(vfe, output, 0); 825 826 ops->set_camif_cmd(vfe, 0); 827 spin_unlock_irqrestore(&vfe->output_lock, flags); 828 829 ops->camif_wait_for_stop(vfe, vfe->camss->dev); 830 } 831 832 return 0; 833 } 834 835 /* 836 * vfe_enable - Enable streaming on VFE line 837 * @line: VFE line 838 * 839 * Return 0 on success or a negative error code otherwise 840 */ 841 static int vfe_enable(struct vfe_line *line) 842 { 843 struct vfe_device *vfe = to_vfe(line); 844 int ret; 845 846 mutex_lock(&vfe->stream_lock); 847 848 if (!vfe->stream_count) { 849 vfe->ops->enable_irq_common(vfe); 850 851 vfe->ops->bus_enable_wr_if(vfe, 1); 852 853 vfe->ops->set_qos(vfe); 854 855 vfe->ops->set_ds(vfe); 856 } 857 858 vfe->stream_count++; 859 860 mutex_unlock(&vfe->stream_lock); 861 862 ret = vfe_get_output(line); 863 if (ret < 0) 864 goto error_get_output; 865 866 ret = vfe_enable_output(line); 867 if (ret < 0) 868 goto error_enable_output; 869 870 vfe->was_streaming = 1; 871 872 return 0; 873 874 875 error_enable_output: 876 vfe_put_output(line); 877 878 error_get_output: 879 mutex_lock(&vfe->stream_lock); 880 881 if (vfe->stream_count == 1) 882 vfe->ops->bus_enable_wr_if(vfe, 0); 883 884 vfe->stream_count--; 885 886 mutex_unlock(&vfe->stream_lock); 887 888 return ret; 889 } 890 891 /* 892 * vfe_disable - Disable streaming on VFE line 893 * @line: VFE line 894 * 895 * Return 0 on success or a negative error code otherwise 896 */ 897 static int vfe_disable(struct vfe_line *line) 898 { 899 struct vfe_device *vfe = to_vfe(line); 900 901 vfe_disable_output(line); 902 903 vfe_put_output(line); 904 905 mutex_lock(&vfe->stream_lock); 906 907 if (vfe->stream_count == 1) 908 vfe->ops->bus_enable_wr_if(vfe, 0); 909 910 vfe->stream_count--; 911 912 mutex_unlock(&vfe->stream_lock); 913 914 return 0; 915 } 916 917 /* 918 * vfe_isr_sof - Process start of frame interrupt 919 * @vfe: VFE Device 920 * @line_id: VFE line 921 */ 922 static void vfe_isr_sof(struct vfe_device *vfe, enum vfe_line_id line_id) 923 { 924 struct vfe_output *output; 925 unsigned long flags; 926 927 spin_lock_irqsave(&vfe->output_lock, flags); 928 output = &vfe->line[line_id].output; 929 if (output->wait_sof) { 930 output->wait_sof = 0; 931 complete(&output->sof); 932 } 933 spin_unlock_irqrestore(&vfe->output_lock, flags); 934 } 935 936 /* 937 * vfe_isr_reg_update - Process reg update interrupt 938 * @vfe: VFE Device 939 * @line_id: VFE line 940 */ 941 static void vfe_isr_reg_update(struct vfe_device *vfe, enum vfe_line_id line_id) 942 { 943 struct vfe_output *output; 944 unsigned long flags; 945 946 spin_lock_irqsave(&vfe->output_lock, flags); 947 vfe->ops->reg_update_clear(vfe, line_id); 948 949 output = &vfe->line[line_id].output; 950 951 if (output->wait_reg_update) { 952 output->wait_reg_update = 0; 953 complete(&output->reg_update); 954 spin_unlock_irqrestore(&vfe->output_lock, flags); 955 return; 956 } 957 958 if (output->state == VFE_OUTPUT_STOPPING) { 959 /* Release last buffer when hw is idle */ 960 if (output->last_buffer) { 961 vb2_buffer_done(&output->last_buffer->vb.vb2_buf, 962 VB2_BUF_STATE_DONE); 963 output->last_buffer = NULL; 964 } 965 output->state = VFE_OUTPUT_IDLE; 966 967 /* Buffers received in stopping state are queued in */ 968 /* dma pending queue, start next capture here */ 969 970 output->buf[0] = vfe_buf_get_pending(output); 971 output->buf[1] = vfe_buf_get_pending(output); 972 973 if (!output->buf[0] && output->buf[1]) { 974 output->buf[0] = output->buf[1]; 975 output->buf[1] = NULL; 976 } 977 978 if (output->buf[0]) 979 output->state = VFE_OUTPUT_SINGLE; 980 981 if (output->buf[1]) 982 output->state = VFE_OUTPUT_CONTINUOUS; 983 984 switch (output->state) { 985 case VFE_OUTPUT_SINGLE: 986 vfe_output_frame_drop(vfe, output, 2); 987 break; 988 case VFE_OUTPUT_CONTINUOUS: 989 vfe_output_frame_drop(vfe, output, 3); 990 break; 991 default: 992 vfe_output_frame_drop(vfe, output, 0); 993 break; 994 } 995 996 vfe_output_init_addrs(vfe, output, 1); 997 } 998 999 spin_unlock_irqrestore(&vfe->output_lock, flags); 1000 } 1001 1002 /* 1003 * vfe_isr_wm_done - Process write master done interrupt 1004 * @vfe: VFE Device 1005 * @wm: Write master id 1006 */ 1007 static void vfe_isr_wm_done(struct vfe_device *vfe, u8 wm) 1008 { 1009 struct camss_buffer *ready_buf; 1010 struct vfe_output *output; 1011 dma_addr_t *new_addr; 1012 unsigned long flags; 1013 u32 active_index; 1014 u64 ts = ktime_get_ns(); 1015 unsigned int i; 1016 1017 active_index = vfe->ops->wm_get_ping_pong_status(vfe, wm); 1018 1019 spin_lock_irqsave(&vfe->output_lock, flags); 1020 1021 if (vfe->wm_output_map[wm] == VFE_LINE_NONE) { 1022 dev_err_ratelimited(vfe->camss->dev, 1023 "Received wm done for unmapped index\n"); 1024 goto out_unlock; 1025 } 1026 output = &vfe->line[vfe->wm_output_map[wm]].output; 1027 1028 if (output->active_buf == active_index) { 1029 dev_err_ratelimited(vfe->camss->dev, 1030 "Active buffer mismatch!\n"); 1031 goto out_unlock; 1032 } 1033 output->active_buf = active_index; 1034 1035 ready_buf = output->buf[!active_index]; 1036 if (!ready_buf) { 1037 dev_err_ratelimited(vfe->camss->dev, 1038 "Missing ready buf %d %d!\n", 1039 !active_index, output->state); 1040 goto out_unlock; 1041 } 1042 1043 ready_buf->vb.vb2_buf.timestamp = ts; 1044 ready_buf->vb.sequence = output->sequence++; 1045 1046 /* Get next buffer */ 1047 output->buf[!active_index] = vfe_buf_get_pending(output); 1048 if (!output->buf[!active_index]) { 1049 /* No next buffer - set same address */ 1050 new_addr = ready_buf->addr; 1051 vfe_buf_update_wm_on_last(vfe, output); 1052 } else { 1053 new_addr = output->buf[!active_index]->addr; 1054 vfe_buf_update_wm_on_next(vfe, output); 1055 } 1056 1057 if (active_index) 1058 for (i = 0; i < output->wm_num; i++) 1059 vfe->ops->wm_set_ping_addr(vfe, output->wm_idx[i], 1060 new_addr[i]); 1061 else 1062 for (i = 0; i < output->wm_num; i++) 1063 vfe->ops->wm_set_pong_addr(vfe, output->wm_idx[i], 1064 new_addr[i]); 1065 1066 spin_unlock_irqrestore(&vfe->output_lock, flags); 1067 1068 if (output->state == VFE_OUTPUT_STOPPING) 1069 output->last_buffer = ready_buf; 1070 else 1071 vb2_buffer_done(&ready_buf->vb.vb2_buf, VB2_BUF_STATE_DONE); 1072 1073 return; 1074 1075 out_unlock: 1076 spin_unlock_irqrestore(&vfe->output_lock, flags); 1077 } 1078 1079 /* 1080 * vfe_isr_wm_done - Process composite image done interrupt 1081 * @vfe: VFE Device 1082 * @comp: Composite image id 1083 */ 1084 static void vfe_isr_comp_done(struct vfe_device *vfe, u8 comp) 1085 { 1086 unsigned int i; 1087 1088 for (i = 0; i < ARRAY_SIZE(vfe->wm_output_map); i++) 1089 if (vfe->wm_output_map[i] == VFE_LINE_PIX) { 1090 vfe_isr_wm_done(vfe, i); 1091 break; 1092 } 1093 } 1094 1095 static inline void vfe_isr_reset_ack(struct vfe_device *vfe) 1096 { 1097 complete(&vfe->reset_complete); 1098 } 1099 1100 static inline void vfe_isr_halt_ack(struct vfe_device *vfe) 1101 { 1102 complete(&vfe->halt_complete); 1103 vfe->ops->halt_clear(vfe); 1104 } 1105 1106 /* 1107 * vfe_set_clock_rates - Calculate and set clock rates on VFE module 1108 * @vfe: VFE device 1109 * 1110 * Return 0 on success or a negative error code otherwise 1111 */ 1112 static int vfe_set_clock_rates(struct vfe_device *vfe) 1113 { 1114 struct device *dev = vfe->camss->dev; 1115 u32 pixel_clock[MSM_VFE_LINE_NUM]; 1116 int i, j; 1117 int ret; 1118 1119 for (i = VFE_LINE_RDI0; i <= VFE_LINE_PIX; i++) { 1120 ret = camss_get_pixel_clock(&vfe->line[i].subdev.entity, 1121 &pixel_clock[i]); 1122 if (ret) 1123 pixel_clock[i] = 0; 1124 } 1125 1126 for (i = 0; i < vfe->nclocks; i++) { 1127 struct camss_clock *clock = &vfe->clock[i]; 1128 1129 if (!strcmp(clock->name, "vfe0") || 1130 !strcmp(clock->name, "vfe1")) { 1131 u64 min_rate = 0; 1132 long rate; 1133 1134 for (j = VFE_LINE_RDI0; j <= VFE_LINE_PIX; j++) { 1135 u32 tmp; 1136 u8 bpp; 1137 1138 if (j == VFE_LINE_PIX) { 1139 tmp = pixel_clock[j]; 1140 } else { 1141 struct vfe_line *l = &vfe->line[j]; 1142 1143 bpp = vfe_get_bpp(l->formats, 1144 l->nformats, 1145 l->fmt[MSM_VFE_PAD_SINK].code); 1146 tmp = pixel_clock[j] * bpp / 64; 1147 } 1148 1149 if (min_rate < tmp) 1150 min_rate = tmp; 1151 } 1152 1153 camss_add_clock_margin(&min_rate); 1154 1155 for (j = 0; j < clock->nfreqs; j++) 1156 if (min_rate < clock->freq[j]) 1157 break; 1158 1159 if (j == clock->nfreqs) { 1160 dev_err(dev, 1161 "Pixel clock is too high for VFE"); 1162 return -EINVAL; 1163 } 1164 1165 /* if sensor pixel clock is not available */ 1166 /* set highest possible VFE clock rate */ 1167 if (min_rate == 0) 1168 j = clock->nfreqs - 1; 1169 1170 rate = clk_round_rate(clock->clk, clock->freq[j]); 1171 if (rate < 0) { 1172 dev_err(dev, "clk round rate failed: %ld\n", 1173 rate); 1174 return -EINVAL; 1175 } 1176 1177 ret = clk_set_rate(clock->clk, rate); 1178 if (ret < 0) { 1179 dev_err(dev, "clk set rate failed: %d\n", ret); 1180 return ret; 1181 } 1182 } 1183 } 1184 1185 return 0; 1186 } 1187 1188 /* 1189 * vfe_check_clock_rates - Check current clock rates on VFE module 1190 * @vfe: VFE device 1191 * 1192 * Return 0 if current clock rates are suitable for a new pipeline 1193 * or a negative error code otherwise 1194 */ 1195 static int vfe_check_clock_rates(struct vfe_device *vfe) 1196 { 1197 u32 pixel_clock[MSM_VFE_LINE_NUM]; 1198 int i, j; 1199 int ret; 1200 1201 for (i = VFE_LINE_RDI0; i <= VFE_LINE_PIX; i++) { 1202 ret = camss_get_pixel_clock(&vfe->line[i].subdev.entity, 1203 &pixel_clock[i]); 1204 if (ret) 1205 pixel_clock[i] = 0; 1206 } 1207 1208 for (i = 0; i < vfe->nclocks; i++) { 1209 struct camss_clock *clock = &vfe->clock[i]; 1210 1211 if (!strcmp(clock->name, "vfe0") || 1212 !strcmp(clock->name, "vfe1")) { 1213 u64 min_rate = 0; 1214 unsigned long rate; 1215 1216 for (j = VFE_LINE_RDI0; j <= VFE_LINE_PIX; j++) { 1217 u32 tmp; 1218 u8 bpp; 1219 1220 if (j == VFE_LINE_PIX) { 1221 tmp = pixel_clock[j]; 1222 } else { 1223 struct vfe_line *l = &vfe->line[j]; 1224 1225 bpp = vfe_get_bpp(l->formats, 1226 l->nformats, 1227 l->fmt[MSM_VFE_PAD_SINK].code); 1228 tmp = pixel_clock[j] * bpp / 64; 1229 } 1230 1231 if (min_rate < tmp) 1232 min_rate = tmp; 1233 } 1234 1235 camss_add_clock_margin(&min_rate); 1236 1237 rate = clk_get_rate(clock->clk); 1238 if (rate < min_rate) 1239 return -EBUSY; 1240 } 1241 } 1242 1243 return 0; 1244 } 1245 1246 /* 1247 * vfe_get - Power up and reset VFE module 1248 * @vfe: VFE Device 1249 * 1250 * Return 0 on success or a negative error code otherwise 1251 */ 1252 static int vfe_get(struct vfe_device *vfe) 1253 { 1254 int ret; 1255 1256 mutex_lock(&vfe->power_lock); 1257 1258 if (vfe->power_count == 0) { 1259 ret = camss_pm_domain_on(vfe->camss, vfe->id); 1260 if (ret < 0) 1261 goto error_pm_domain; 1262 1263 ret = pm_runtime_get_sync(vfe->camss->dev); 1264 if (ret < 0) 1265 goto error_pm_runtime_get; 1266 1267 ret = vfe_set_clock_rates(vfe); 1268 if (ret < 0) 1269 goto error_pm_runtime_get; 1270 1271 ret = camss_enable_clocks(vfe->nclocks, vfe->clock, 1272 vfe->camss->dev); 1273 if (ret < 0) 1274 goto error_pm_runtime_get; 1275 1276 ret = vfe_reset(vfe); 1277 if (ret < 0) 1278 goto error_reset; 1279 1280 vfe_reset_output_maps(vfe); 1281 1282 vfe_init_outputs(vfe); 1283 } else { 1284 ret = vfe_check_clock_rates(vfe); 1285 if (ret < 0) 1286 goto error_pm_runtime_get; 1287 } 1288 vfe->power_count++; 1289 1290 mutex_unlock(&vfe->power_lock); 1291 1292 return 0; 1293 1294 error_reset: 1295 camss_disable_clocks(vfe->nclocks, vfe->clock); 1296 1297 error_pm_runtime_get: 1298 pm_runtime_put_sync(vfe->camss->dev); 1299 camss_pm_domain_off(vfe->camss, vfe->id); 1300 1301 error_pm_domain: 1302 mutex_unlock(&vfe->power_lock); 1303 1304 return ret; 1305 } 1306 1307 /* 1308 * vfe_put - Power down VFE module 1309 * @vfe: VFE Device 1310 */ 1311 static void vfe_put(struct vfe_device *vfe) 1312 { 1313 mutex_lock(&vfe->power_lock); 1314 1315 if (vfe->power_count == 0) { 1316 dev_err(vfe->camss->dev, "vfe power off on power_count == 0\n"); 1317 goto exit; 1318 } else if (vfe->power_count == 1) { 1319 if (vfe->was_streaming) { 1320 vfe->was_streaming = 0; 1321 vfe_halt(vfe); 1322 } 1323 camss_disable_clocks(vfe->nclocks, vfe->clock); 1324 pm_runtime_put_sync(vfe->camss->dev); 1325 camss_pm_domain_off(vfe->camss, vfe->id); 1326 } 1327 1328 vfe->power_count--; 1329 1330 exit: 1331 mutex_unlock(&vfe->power_lock); 1332 } 1333 1334 /* 1335 * vfe_queue_buffer - Add empty buffer 1336 * @vid: Video device structure 1337 * @buf: Buffer to be enqueued 1338 * 1339 * Add an empty buffer - depending on the current number of buffers it will be 1340 * put in pending buffer queue or directly given to the hardware to be filled. 1341 * 1342 * Return 0 on success or a negative error code otherwise 1343 */ 1344 static int vfe_queue_buffer(struct camss_video *vid, 1345 struct camss_buffer *buf) 1346 { 1347 struct vfe_line *line = container_of(vid, struct vfe_line, video_out); 1348 struct vfe_device *vfe = to_vfe(line); 1349 struct vfe_output *output; 1350 unsigned long flags; 1351 1352 output = &line->output; 1353 1354 spin_lock_irqsave(&vfe->output_lock, flags); 1355 1356 vfe_buf_update_wm_on_new(vfe, output, buf); 1357 1358 spin_unlock_irqrestore(&vfe->output_lock, flags); 1359 1360 return 0; 1361 } 1362 1363 /* 1364 * vfe_flush_buffers - Return all vb2 buffers 1365 * @vid: Video device structure 1366 * @state: vb2 buffer state of the returned buffers 1367 * 1368 * Return all buffers to vb2. This includes queued pending buffers (still 1369 * unused) and any buffers given to the hardware but again still not used. 1370 * 1371 * Return 0 on success or a negative error code otherwise 1372 */ 1373 static int vfe_flush_buffers(struct camss_video *vid, 1374 enum vb2_buffer_state state) 1375 { 1376 struct vfe_line *line = container_of(vid, struct vfe_line, video_out); 1377 struct vfe_device *vfe = to_vfe(line); 1378 struct vfe_output *output; 1379 unsigned long flags; 1380 1381 output = &line->output; 1382 1383 spin_lock_irqsave(&vfe->output_lock, flags); 1384 1385 vfe_buf_flush_pending(output, state); 1386 1387 if (output->buf[0]) 1388 vb2_buffer_done(&output->buf[0]->vb.vb2_buf, state); 1389 1390 if (output->buf[1]) 1391 vb2_buffer_done(&output->buf[1]->vb.vb2_buf, state); 1392 1393 if (output->last_buffer) { 1394 vb2_buffer_done(&output->last_buffer->vb.vb2_buf, state); 1395 output->last_buffer = NULL; 1396 } 1397 1398 spin_unlock_irqrestore(&vfe->output_lock, flags); 1399 1400 return 0; 1401 } 1402 1403 /* 1404 * vfe_set_power - Power on/off VFE module 1405 * @sd: VFE V4L2 subdevice 1406 * @on: Requested power state 1407 * 1408 * Return 0 on success or a negative error code otherwise 1409 */ 1410 static int vfe_set_power(struct v4l2_subdev *sd, int on) 1411 { 1412 struct vfe_line *line = v4l2_get_subdevdata(sd); 1413 struct vfe_device *vfe = to_vfe(line); 1414 int ret; 1415 1416 if (on) { 1417 ret = vfe_get(vfe); 1418 if (ret < 0) 1419 return ret; 1420 1421 vfe->ops->hw_version_read(vfe, vfe->camss->dev); 1422 } else { 1423 vfe_put(vfe); 1424 } 1425 1426 return 0; 1427 } 1428 1429 /* 1430 * vfe_set_stream - Enable/disable streaming on VFE module 1431 * @sd: VFE V4L2 subdevice 1432 * @enable: Requested streaming state 1433 * 1434 * Main configuration of VFE module is triggered here. 1435 * 1436 * Return 0 on success or a negative error code otherwise 1437 */ 1438 static int vfe_set_stream(struct v4l2_subdev *sd, int enable) 1439 { 1440 struct vfe_line *line = v4l2_get_subdevdata(sd); 1441 struct vfe_device *vfe = to_vfe(line); 1442 int ret; 1443 1444 if (enable) { 1445 ret = vfe_enable(line); 1446 if (ret < 0) 1447 dev_err(vfe->camss->dev, 1448 "Failed to enable vfe outputs\n"); 1449 } else { 1450 ret = vfe_disable(line); 1451 if (ret < 0) 1452 dev_err(vfe->camss->dev, 1453 "Failed to disable vfe outputs\n"); 1454 } 1455 1456 return ret; 1457 } 1458 1459 /* 1460 * __vfe_get_format - Get pointer to format structure 1461 * @line: VFE line 1462 * @cfg: V4L2 subdev pad configuration 1463 * @pad: pad from which format is requested 1464 * @which: TRY or ACTIVE format 1465 * 1466 * Return pointer to TRY or ACTIVE format structure 1467 */ 1468 static struct v4l2_mbus_framefmt * 1469 __vfe_get_format(struct vfe_line *line, 1470 struct v4l2_subdev_pad_config *cfg, 1471 unsigned int pad, 1472 enum v4l2_subdev_format_whence which) 1473 { 1474 if (which == V4L2_SUBDEV_FORMAT_TRY) 1475 return v4l2_subdev_get_try_format(&line->subdev, cfg, pad); 1476 1477 return &line->fmt[pad]; 1478 } 1479 1480 /* 1481 * __vfe_get_compose - Get pointer to compose selection structure 1482 * @line: VFE line 1483 * @cfg: V4L2 subdev pad configuration 1484 * @which: TRY or ACTIVE format 1485 * 1486 * Return pointer to TRY or ACTIVE compose rectangle structure 1487 */ 1488 static struct v4l2_rect * 1489 __vfe_get_compose(struct vfe_line *line, 1490 struct v4l2_subdev_pad_config *cfg, 1491 enum v4l2_subdev_format_whence which) 1492 { 1493 if (which == V4L2_SUBDEV_FORMAT_TRY) 1494 return v4l2_subdev_get_try_compose(&line->subdev, cfg, 1495 MSM_VFE_PAD_SINK); 1496 1497 return &line->compose; 1498 } 1499 1500 /* 1501 * __vfe_get_crop - Get pointer to crop selection structure 1502 * @line: VFE line 1503 * @cfg: V4L2 subdev pad configuration 1504 * @which: TRY or ACTIVE format 1505 * 1506 * Return pointer to TRY or ACTIVE crop rectangle structure 1507 */ 1508 static struct v4l2_rect * 1509 __vfe_get_crop(struct vfe_line *line, 1510 struct v4l2_subdev_pad_config *cfg, 1511 enum v4l2_subdev_format_whence which) 1512 { 1513 if (which == V4L2_SUBDEV_FORMAT_TRY) 1514 return v4l2_subdev_get_try_crop(&line->subdev, cfg, 1515 MSM_VFE_PAD_SRC); 1516 1517 return &line->crop; 1518 } 1519 1520 /* 1521 * vfe_try_format - Handle try format by pad subdev method 1522 * @line: VFE line 1523 * @cfg: V4L2 subdev pad configuration 1524 * @pad: pad on which format is requested 1525 * @fmt: pointer to v4l2 format structure 1526 * @which: wanted subdev format 1527 */ 1528 static void vfe_try_format(struct vfe_line *line, 1529 struct v4l2_subdev_pad_config *cfg, 1530 unsigned int pad, 1531 struct v4l2_mbus_framefmt *fmt, 1532 enum v4l2_subdev_format_whence which) 1533 { 1534 unsigned int i; 1535 u32 code; 1536 1537 switch (pad) { 1538 case MSM_VFE_PAD_SINK: 1539 /* Set format on sink pad */ 1540 1541 for (i = 0; i < line->nformats; i++) 1542 if (fmt->code == line->formats[i].code) 1543 break; 1544 1545 /* If not found, use UYVY as default */ 1546 if (i >= line->nformats) 1547 fmt->code = MEDIA_BUS_FMT_UYVY8_2X8; 1548 1549 fmt->width = clamp_t(u32, fmt->width, 1, 8191); 1550 fmt->height = clamp_t(u32, fmt->height, 1, 8191); 1551 1552 fmt->field = V4L2_FIELD_NONE; 1553 fmt->colorspace = V4L2_COLORSPACE_SRGB; 1554 1555 break; 1556 1557 case MSM_VFE_PAD_SRC: 1558 /* Set and return a format same as sink pad */ 1559 code = fmt->code; 1560 1561 *fmt = *__vfe_get_format(line, cfg, MSM_VFE_PAD_SINK, which); 1562 1563 fmt->code = vfe_src_pad_code(line, fmt->code, 0, code); 1564 1565 if (line->id == VFE_LINE_PIX) { 1566 struct v4l2_rect *rect; 1567 1568 rect = __vfe_get_crop(line, cfg, which); 1569 1570 fmt->width = rect->width; 1571 fmt->height = rect->height; 1572 } 1573 1574 break; 1575 } 1576 1577 fmt->colorspace = V4L2_COLORSPACE_SRGB; 1578 } 1579 1580 /* 1581 * vfe_try_compose - Handle try compose selection by pad subdev method 1582 * @line: VFE line 1583 * @cfg: V4L2 subdev pad configuration 1584 * @rect: pointer to v4l2 rect structure 1585 * @which: wanted subdev format 1586 */ 1587 static void vfe_try_compose(struct vfe_line *line, 1588 struct v4l2_subdev_pad_config *cfg, 1589 struct v4l2_rect *rect, 1590 enum v4l2_subdev_format_whence which) 1591 { 1592 struct v4l2_mbus_framefmt *fmt; 1593 1594 fmt = __vfe_get_format(line, cfg, MSM_VFE_PAD_SINK, which); 1595 1596 if (rect->width > fmt->width) 1597 rect->width = fmt->width; 1598 1599 if (rect->height > fmt->height) 1600 rect->height = fmt->height; 1601 1602 if (fmt->width > rect->width * SCALER_RATIO_MAX) 1603 rect->width = (fmt->width + SCALER_RATIO_MAX - 1) / 1604 SCALER_RATIO_MAX; 1605 1606 rect->width &= ~0x1; 1607 1608 if (fmt->height > rect->height * SCALER_RATIO_MAX) 1609 rect->height = (fmt->height + SCALER_RATIO_MAX - 1) / 1610 SCALER_RATIO_MAX; 1611 1612 if (rect->width < 16) 1613 rect->width = 16; 1614 1615 if (rect->height < 4) 1616 rect->height = 4; 1617 } 1618 1619 /* 1620 * vfe_try_crop - Handle try crop selection by pad subdev method 1621 * @line: VFE line 1622 * @cfg: V4L2 subdev pad configuration 1623 * @rect: pointer to v4l2 rect structure 1624 * @which: wanted subdev format 1625 */ 1626 static void vfe_try_crop(struct vfe_line *line, 1627 struct v4l2_subdev_pad_config *cfg, 1628 struct v4l2_rect *rect, 1629 enum v4l2_subdev_format_whence which) 1630 { 1631 struct v4l2_rect *compose; 1632 1633 compose = __vfe_get_compose(line, cfg, which); 1634 1635 if (rect->width > compose->width) 1636 rect->width = compose->width; 1637 1638 if (rect->width + rect->left > compose->width) 1639 rect->left = compose->width - rect->width; 1640 1641 if (rect->height > compose->height) 1642 rect->height = compose->height; 1643 1644 if (rect->height + rect->top > compose->height) 1645 rect->top = compose->height - rect->height; 1646 1647 /* wm in line based mode writes multiple of 16 horizontally */ 1648 rect->left += (rect->width & 0xf) >> 1; 1649 rect->width &= ~0xf; 1650 1651 if (rect->width < 16) { 1652 rect->left = 0; 1653 rect->width = 16; 1654 } 1655 1656 if (rect->height < 4) { 1657 rect->top = 0; 1658 rect->height = 4; 1659 } 1660 } 1661 1662 /* 1663 * vfe_enum_mbus_code - Handle pixel format enumeration 1664 * @sd: VFE V4L2 subdevice 1665 * @cfg: V4L2 subdev pad configuration 1666 * @code: pointer to v4l2_subdev_mbus_code_enum structure 1667 * 1668 * return -EINVAL or zero on success 1669 */ 1670 static int vfe_enum_mbus_code(struct v4l2_subdev *sd, 1671 struct v4l2_subdev_pad_config *cfg, 1672 struct v4l2_subdev_mbus_code_enum *code) 1673 { 1674 struct vfe_line *line = v4l2_get_subdevdata(sd); 1675 1676 if (code->pad == MSM_VFE_PAD_SINK) { 1677 if (code->index >= line->nformats) 1678 return -EINVAL; 1679 1680 code->code = line->formats[code->index].code; 1681 } else { 1682 struct v4l2_mbus_framefmt *sink_fmt; 1683 1684 sink_fmt = __vfe_get_format(line, cfg, MSM_VFE_PAD_SINK, 1685 code->which); 1686 1687 code->code = vfe_src_pad_code(line, sink_fmt->code, 1688 code->index, 0); 1689 if (!code->code) 1690 return -EINVAL; 1691 } 1692 1693 return 0; 1694 } 1695 1696 /* 1697 * vfe_enum_frame_size - Handle frame size enumeration 1698 * @sd: VFE V4L2 subdevice 1699 * @cfg: V4L2 subdev pad configuration 1700 * @fse: pointer to v4l2_subdev_frame_size_enum structure 1701 * 1702 * Return -EINVAL or zero on success 1703 */ 1704 static int vfe_enum_frame_size(struct v4l2_subdev *sd, 1705 struct v4l2_subdev_pad_config *cfg, 1706 struct v4l2_subdev_frame_size_enum *fse) 1707 { 1708 struct vfe_line *line = v4l2_get_subdevdata(sd); 1709 struct v4l2_mbus_framefmt format; 1710 1711 if (fse->index != 0) 1712 return -EINVAL; 1713 1714 format.code = fse->code; 1715 format.width = 1; 1716 format.height = 1; 1717 vfe_try_format(line, cfg, fse->pad, &format, fse->which); 1718 fse->min_width = format.width; 1719 fse->min_height = format.height; 1720 1721 if (format.code != fse->code) 1722 return -EINVAL; 1723 1724 format.code = fse->code; 1725 format.width = -1; 1726 format.height = -1; 1727 vfe_try_format(line, cfg, fse->pad, &format, fse->which); 1728 fse->max_width = format.width; 1729 fse->max_height = format.height; 1730 1731 return 0; 1732 } 1733 1734 /* 1735 * vfe_get_format - Handle get format by pads subdev method 1736 * @sd: VFE V4L2 subdevice 1737 * @cfg: V4L2 subdev pad configuration 1738 * @fmt: pointer to v4l2 subdev format structure 1739 * 1740 * Return -EINVAL or zero on success 1741 */ 1742 static int vfe_get_format(struct v4l2_subdev *sd, 1743 struct v4l2_subdev_pad_config *cfg, 1744 struct v4l2_subdev_format *fmt) 1745 { 1746 struct vfe_line *line = v4l2_get_subdevdata(sd); 1747 struct v4l2_mbus_framefmt *format; 1748 1749 format = __vfe_get_format(line, cfg, fmt->pad, fmt->which); 1750 if (format == NULL) 1751 return -EINVAL; 1752 1753 fmt->format = *format; 1754 1755 return 0; 1756 } 1757 1758 static int vfe_set_selection(struct v4l2_subdev *sd, 1759 struct v4l2_subdev_pad_config *cfg, 1760 struct v4l2_subdev_selection *sel); 1761 1762 /* 1763 * vfe_set_format - Handle set format by pads subdev method 1764 * @sd: VFE V4L2 subdevice 1765 * @cfg: V4L2 subdev pad configuration 1766 * @fmt: pointer to v4l2 subdev format structure 1767 * 1768 * Return -EINVAL or zero on success 1769 */ 1770 static int vfe_set_format(struct v4l2_subdev *sd, 1771 struct v4l2_subdev_pad_config *cfg, 1772 struct v4l2_subdev_format *fmt) 1773 { 1774 struct vfe_line *line = v4l2_get_subdevdata(sd); 1775 struct v4l2_mbus_framefmt *format; 1776 1777 format = __vfe_get_format(line, cfg, fmt->pad, fmt->which); 1778 if (format == NULL) 1779 return -EINVAL; 1780 1781 vfe_try_format(line, cfg, fmt->pad, &fmt->format, fmt->which); 1782 *format = fmt->format; 1783 1784 if (fmt->pad == MSM_VFE_PAD_SINK) { 1785 struct v4l2_subdev_selection sel = { 0 }; 1786 int ret; 1787 1788 /* Propagate the format from sink to source */ 1789 format = __vfe_get_format(line, cfg, MSM_VFE_PAD_SRC, 1790 fmt->which); 1791 1792 *format = fmt->format; 1793 vfe_try_format(line, cfg, MSM_VFE_PAD_SRC, format, 1794 fmt->which); 1795 1796 if (line->id != VFE_LINE_PIX) 1797 return 0; 1798 1799 /* Reset sink pad compose selection */ 1800 sel.which = fmt->which; 1801 sel.pad = MSM_VFE_PAD_SINK; 1802 sel.target = V4L2_SEL_TGT_COMPOSE; 1803 sel.r.width = fmt->format.width; 1804 sel.r.height = fmt->format.height; 1805 ret = vfe_set_selection(sd, cfg, &sel); 1806 if (ret < 0) 1807 return ret; 1808 } 1809 1810 return 0; 1811 } 1812 1813 /* 1814 * vfe_get_selection - Handle get selection by pads subdev method 1815 * @sd: VFE V4L2 subdevice 1816 * @cfg: V4L2 subdev pad configuration 1817 * @sel: pointer to v4l2 subdev selection structure 1818 * 1819 * Return -EINVAL or zero on success 1820 */ 1821 static int vfe_get_selection(struct v4l2_subdev *sd, 1822 struct v4l2_subdev_pad_config *cfg, 1823 struct v4l2_subdev_selection *sel) 1824 { 1825 struct vfe_line *line = v4l2_get_subdevdata(sd); 1826 struct v4l2_subdev_format fmt = { 0 }; 1827 struct v4l2_rect *rect; 1828 int ret; 1829 1830 if (line->id != VFE_LINE_PIX) 1831 return -EINVAL; 1832 1833 if (sel->pad == MSM_VFE_PAD_SINK) 1834 switch (sel->target) { 1835 case V4L2_SEL_TGT_COMPOSE_BOUNDS: 1836 fmt.pad = sel->pad; 1837 fmt.which = sel->which; 1838 ret = vfe_get_format(sd, cfg, &fmt); 1839 if (ret < 0) 1840 return ret; 1841 1842 sel->r.left = 0; 1843 sel->r.top = 0; 1844 sel->r.width = fmt.format.width; 1845 sel->r.height = fmt.format.height; 1846 break; 1847 case V4L2_SEL_TGT_COMPOSE: 1848 rect = __vfe_get_compose(line, cfg, sel->which); 1849 if (rect == NULL) 1850 return -EINVAL; 1851 1852 sel->r = *rect; 1853 break; 1854 default: 1855 return -EINVAL; 1856 } 1857 else if (sel->pad == MSM_VFE_PAD_SRC) 1858 switch (sel->target) { 1859 case V4L2_SEL_TGT_CROP_BOUNDS: 1860 rect = __vfe_get_compose(line, cfg, sel->which); 1861 if (rect == NULL) 1862 return -EINVAL; 1863 1864 sel->r.left = rect->left; 1865 sel->r.top = rect->top; 1866 sel->r.width = rect->width; 1867 sel->r.height = rect->height; 1868 break; 1869 case V4L2_SEL_TGT_CROP: 1870 rect = __vfe_get_crop(line, cfg, sel->which); 1871 if (rect == NULL) 1872 return -EINVAL; 1873 1874 sel->r = *rect; 1875 break; 1876 default: 1877 return -EINVAL; 1878 } 1879 1880 return 0; 1881 } 1882 1883 /* 1884 * vfe_set_selection - Handle set selection by pads subdev method 1885 * @sd: VFE V4L2 subdevice 1886 * @cfg: V4L2 subdev pad configuration 1887 * @sel: pointer to v4l2 subdev selection structure 1888 * 1889 * Return -EINVAL or zero on success 1890 */ 1891 static int vfe_set_selection(struct v4l2_subdev *sd, 1892 struct v4l2_subdev_pad_config *cfg, 1893 struct v4l2_subdev_selection *sel) 1894 { 1895 struct vfe_line *line = v4l2_get_subdevdata(sd); 1896 struct v4l2_rect *rect; 1897 int ret; 1898 1899 if (line->id != VFE_LINE_PIX) 1900 return -EINVAL; 1901 1902 if (sel->target == V4L2_SEL_TGT_COMPOSE && 1903 sel->pad == MSM_VFE_PAD_SINK) { 1904 struct v4l2_subdev_selection crop = { 0 }; 1905 1906 rect = __vfe_get_compose(line, cfg, sel->which); 1907 if (rect == NULL) 1908 return -EINVAL; 1909 1910 vfe_try_compose(line, cfg, &sel->r, sel->which); 1911 *rect = sel->r; 1912 1913 /* Reset source crop selection */ 1914 crop.which = sel->which; 1915 crop.pad = MSM_VFE_PAD_SRC; 1916 crop.target = V4L2_SEL_TGT_CROP; 1917 crop.r = *rect; 1918 ret = vfe_set_selection(sd, cfg, &crop); 1919 } else if (sel->target == V4L2_SEL_TGT_CROP && 1920 sel->pad == MSM_VFE_PAD_SRC) { 1921 struct v4l2_subdev_format fmt = { 0 }; 1922 1923 rect = __vfe_get_crop(line, cfg, sel->which); 1924 if (rect == NULL) 1925 return -EINVAL; 1926 1927 vfe_try_crop(line, cfg, &sel->r, sel->which); 1928 *rect = sel->r; 1929 1930 /* Reset source pad format width and height */ 1931 fmt.which = sel->which; 1932 fmt.pad = MSM_VFE_PAD_SRC; 1933 ret = vfe_get_format(sd, cfg, &fmt); 1934 if (ret < 0) 1935 return ret; 1936 1937 fmt.format.width = rect->width; 1938 fmt.format.height = rect->height; 1939 ret = vfe_set_format(sd, cfg, &fmt); 1940 } else { 1941 ret = -EINVAL; 1942 } 1943 1944 return ret; 1945 } 1946 1947 /* 1948 * vfe_init_formats - Initialize formats on all pads 1949 * @sd: VFE V4L2 subdevice 1950 * @fh: V4L2 subdev file handle 1951 * 1952 * Initialize all pad formats with default values. 1953 * 1954 * Return 0 on success or a negative error code otherwise 1955 */ 1956 static int vfe_init_formats(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh) 1957 { 1958 struct v4l2_subdev_format format = { 1959 .pad = MSM_VFE_PAD_SINK, 1960 .which = fh ? V4L2_SUBDEV_FORMAT_TRY : 1961 V4L2_SUBDEV_FORMAT_ACTIVE, 1962 .format = { 1963 .code = MEDIA_BUS_FMT_UYVY8_2X8, 1964 .width = 1920, 1965 .height = 1080 1966 } 1967 }; 1968 1969 return vfe_set_format(sd, fh ? fh->pad : NULL, &format); 1970 } 1971 1972 /* 1973 * msm_vfe_subdev_init - Initialize VFE device structure and resources 1974 * @vfe: VFE device 1975 * @res: VFE module resources table 1976 * 1977 * Return 0 on success or a negative error code otherwise 1978 */ 1979 int msm_vfe_subdev_init(struct camss *camss, struct vfe_device *vfe, 1980 const struct resources *res, u8 id) 1981 { 1982 struct device *dev = camss->dev; 1983 struct platform_device *pdev = to_platform_device(dev); 1984 struct resource *r; 1985 int i, j; 1986 int ret; 1987 1988 vfe->isr_ops.reset_ack = vfe_isr_reset_ack; 1989 vfe->isr_ops.halt_ack = vfe_isr_halt_ack; 1990 vfe->isr_ops.reg_update = vfe_isr_reg_update; 1991 vfe->isr_ops.sof = vfe_isr_sof; 1992 vfe->isr_ops.comp_done = vfe_isr_comp_done; 1993 vfe->isr_ops.wm_done = vfe_isr_wm_done; 1994 1995 switch (camss->version) { 1996 case CAMSS_8x16: 1997 vfe->ops = &vfe_ops_4_1; 1998 break; 1999 case CAMSS_8x96: 2000 vfe->ops = &vfe_ops_4_7; 2001 break; 2002 case CAMSS_660: 2003 vfe->ops = &vfe_ops_4_8; 2004 break; 2005 default: 2006 return -EINVAL; 2007 } 2008 2009 /* Memory */ 2010 2011 r = platform_get_resource_byname(pdev, IORESOURCE_MEM, res->reg[0]); 2012 vfe->base = devm_ioremap_resource(dev, r); 2013 if (IS_ERR(vfe->base)) { 2014 dev_err(dev, "could not map memory\n"); 2015 return PTR_ERR(vfe->base); 2016 } 2017 2018 /* Interrupt */ 2019 2020 r = platform_get_resource_byname(pdev, IORESOURCE_IRQ, 2021 res->interrupt[0]); 2022 if (!r) { 2023 dev_err(dev, "missing IRQ\n"); 2024 return -EINVAL; 2025 } 2026 2027 vfe->irq = r->start; 2028 snprintf(vfe->irq_name, sizeof(vfe->irq_name), "%s_%s%d", 2029 dev_name(dev), MSM_VFE_NAME, vfe->id); 2030 ret = devm_request_irq(dev, vfe->irq, vfe->ops->isr, 2031 IRQF_TRIGGER_RISING, vfe->irq_name, vfe); 2032 if (ret < 0) { 2033 dev_err(dev, "request_irq failed: %d\n", ret); 2034 return ret; 2035 } 2036 2037 /* Clocks */ 2038 2039 vfe->nclocks = 0; 2040 while (res->clock[vfe->nclocks]) 2041 vfe->nclocks++; 2042 2043 vfe->clock = devm_kcalloc(dev, vfe->nclocks, sizeof(*vfe->clock), 2044 GFP_KERNEL); 2045 if (!vfe->clock) 2046 return -ENOMEM; 2047 2048 for (i = 0; i < vfe->nclocks; i++) { 2049 struct camss_clock *clock = &vfe->clock[i]; 2050 2051 clock->clk = devm_clk_get(dev, res->clock[i]); 2052 if (IS_ERR(clock->clk)) 2053 return PTR_ERR(clock->clk); 2054 2055 clock->name = res->clock[i]; 2056 2057 clock->nfreqs = 0; 2058 while (res->clock_rate[i][clock->nfreqs]) 2059 clock->nfreqs++; 2060 2061 if (!clock->nfreqs) { 2062 clock->freq = NULL; 2063 continue; 2064 } 2065 2066 clock->freq = devm_kcalloc(dev, 2067 clock->nfreqs, 2068 sizeof(*clock->freq), 2069 GFP_KERNEL); 2070 if (!clock->freq) 2071 return -ENOMEM; 2072 2073 for (j = 0; j < clock->nfreqs; j++) 2074 clock->freq[j] = res->clock_rate[i][j]; 2075 } 2076 2077 mutex_init(&vfe->power_lock); 2078 vfe->power_count = 0; 2079 2080 mutex_init(&vfe->stream_lock); 2081 vfe->stream_count = 0; 2082 2083 spin_lock_init(&vfe->output_lock); 2084 2085 vfe->camss = camss; 2086 vfe->id = id; 2087 vfe->reg_update = 0; 2088 2089 for (i = VFE_LINE_RDI0; i <= VFE_LINE_PIX; i++) { 2090 struct vfe_line *l = &vfe->line[i]; 2091 2092 l->video_out.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; 2093 l->video_out.camss = camss; 2094 l->id = i; 2095 init_completion(&l->output.sof); 2096 init_completion(&l->output.reg_update); 2097 2098 if (camss->version == CAMSS_8x16) { 2099 if (i == VFE_LINE_PIX) { 2100 l->formats = formats_pix_8x16; 2101 l->nformats = ARRAY_SIZE(formats_pix_8x16); 2102 } else { 2103 l->formats = formats_rdi_8x16; 2104 l->nformats = ARRAY_SIZE(formats_rdi_8x16); 2105 } 2106 } else if (camss->version == CAMSS_8x96 || 2107 camss->version == CAMSS_660) { 2108 if (i == VFE_LINE_PIX) { 2109 l->formats = formats_pix_8x96; 2110 l->nformats = ARRAY_SIZE(formats_pix_8x96); 2111 } else { 2112 l->formats = formats_rdi_8x96; 2113 l->nformats = ARRAY_SIZE(formats_rdi_8x96); 2114 } 2115 } else { 2116 return -EINVAL; 2117 } 2118 } 2119 2120 init_completion(&vfe->reset_complete); 2121 init_completion(&vfe->halt_complete); 2122 2123 return 0; 2124 } 2125 2126 /* 2127 * msm_vfe_get_vfe_id - Get VFE HW module id 2128 * @entity: Pointer to VFE media entity structure 2129 * @id: Return CSID HW module id here 2130 */ 2131 void msm_vfe_get_vfe_id(struct media_entity *entity, u8 *id) 2132 { 2133 struct v4l2_subdev *sd; 2134 struct vfe_line *line; 2135 struct vfe_device *vfe; 2136 2137 sd = media_entity_to_v4l2_subdev(entity); 2138 line = v4l2_get_subdevdata(sd); 2139 vfe = to_vfe(line); 2140 2141 *id = vfe->id; 2142 } 2143 2144 /* 2145 * msm_vfe_get_vfe_line_id - Get VFE line id by media entity 2146 * @entity: Pointer to VFE media entity structure 2147 * @id: Return VFE line id here 2148 */ 2149 void msm_vfe_get_vfe_line_id(struct media_entity *entity, enum vfe_line_id *id) 2150 { 2151 struct v4l2_subdev *sd; 2152 struct vfe_line *line; 2153 2154 sd = media_entity_to_v4l2_subdev(entity); 2155 line = v4l2_get_subdevdata(sd); 2156 2157 *id = line->id; 2158 } 2159 2160 /* 2161 * vfe_link_setup - Setup VFE connections 2162 * @entity: Pointer to media entity structure 2163 * @local: Pointer to local pad 2164 * @remote: Pointer to remote pad 2165 * @flags: Link flags 2166 * 2167 * Return 0 on success 2168 */ 2169 static int vfe_link_setup(struct media_entity *entity, 2170 const struct media_pad *local, 2171 const struct media_pad *remote, u32 flags) 2172 { 2173 if (flags & MEDIA_LNK_FL_ENABLED) 2174 if (media_entity_remote_pad(local)) 2175 return -EBUSY; 2176 2177 return 0; 2178 } 2179 2180 static const struct v4l2_subdev_core_ops vfe_core_ops = { 2181 .s_power = vfe_set_power, 2182 }; 2183 2184 static const struct v4l2_subdev_video_ops vfe_video_ops = { 2185 .s_stream = vfe_set_stream, 2186 }; 2187 2188 static const struct v4l2_subdev_pad_ops vfe_pad_ops = { 2189 .enum_mbus_code = vfe_enum_mbus_code, 2190 .enum_frame_size = vfe_enum_frame_size, 2191 .get_fmt = vfe_get_format, 2192 .set_fmt = vfe_set_format, 2193 .get_selection = vfe_get_selection, 2194 .set_selection = vfe_set_selection, 2195 }; 2196 2197 static const struct v4l2_subdev_ops vfe_v4l2_ops = { 2198 .core = &vfe_core_ops, 2199 .video = &vfe_video_ops, 2200 .pad = &vfe_pad_ops, 2201 }; 2202 2203 static const struct v4l2_subdev_internal_ops vfe_v4l2_internal_ops = { 2204 .open = vfe_init_formats, 2205 }; 2206 2207 static const struct media_entity_operations vfe_media_ops = { 2208 .link_setup = vfe_link_setup, 2209 .link_validate = v4l2_subdev_link_validate, 2210 }; 2211 2212 static const struct camss_video_ops camss_vfe_video_ops = { 2213 .queue_buffer = vfe_queue_buffer, 2214 .flush_buffers = vfe_flush_buffers, 2215 }; 2216 2217 /* 2218 * msm_vfe_register_entities - Register subdev node for VFE module 2219 * @vfe: VFE device 2220 * @v4l2_dev: V4L2 device 2221 * 2222 * Initialize and register a subdev node for the VFE module. Then 2223 * call msm_video_register() to register the video device node which 2224 * will be connected to this subdev node. Then actually create the 2225 * media link between them. 2226 * 2227 * Return 0 on success or a negative error code otherwise 2228 */ 2229 int msm_vfe_register_entities(struct vfe_device *vfe, 2230 struct v4l2_device *v4l2_dev) 2231 { 2232 struct device *dev = vfe->camss->dev; 2233 struct v4l2_subdev *sd; 2234 struct media_pad *pads; 2235 struct camss_video *video_out; 2236 int ret; 2237 int i; 2238 2239 for (i = 0; i < ARRAY_SIZE(vfe->line); i++) { 2240 char name[32]; 2241 2242 sd = &vfe->line[i].subdev; 2243 pads = vfe->line[i].pads; 2244 video_out = &vfe->line[i].video_out; 2245 2246 v4l2_subdev_init(sd, &vfe_v4l2_ops); 2247 sd->internal_ops = &vfe_v4l2_internal_ops; 2248 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; 2249 if (i == VFE_LINE_PIX) 2250 snprintf(sd->name, ARRAY_SIZE(sd->name), "%s%d_%s", 2251 MSM_VFE_NAME, vfe->id, "pix"); 2252 else 2253 snprintf(sd->name, ARRAY_SIZE(sd->name), "%s%d_%s%d", 2254 MSM_VFE_NAME, vfe->id, "rdi", i); 2255 2256 v4l2_set_subdevdata(sd, &vfe->line[i]); 2257 2258 ret = vfe_init_formats(sd, NULL); 2259 if (ret < 0) { 2260 dev_err(dev, "Failed to init format: %d\n", ret); 2261 goto error_init; 2262 } 2263 2264 pads[MSM_VFE_PAD_SINK].flags = MEDIA_PAD_FL_SINK; 2265 pads[MSM_VFE_PAD_SRC].flags = MEDIA_PAD_FL_SOURCE; 2266 2267 sd->entity.function = MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER; 2268 sd->entity.ops = &vfe_media_ops; 2269 ret = media_entity_pads_init(&sd->entity, MSM_VFE_PADS_NUM, 2270 pads); 2271 if (ret < 0) { 2272 dev_err(dev, "Failed to init media entity: %d\n", ret); 2273 goto error_init; 2274 } 2275 2276 ret = v4l2_device_register_subdev(v4l2_dev, sd); 2277 if (ret < 0) { 2278 dev_err(dev, "Failed to register subdev: %d\n", ret); 2279 goto error_reg_subdev; 2280 } 2281 2282 video_out->ops = &camss_vfe_video_ops; 2283 video_out->bpl_alignment = 8; 2284 video_out->line_based = 0; 2285 if (i == VFE_LINE_PIX) { 2286 video_out->bpl_alignment = 16; 2287 video_out->line_based = 1; 2288 } 2289 snprintf(name, ARRAY_SIZE(name), "%s%d_%s%d", 2290 MSM_VFE_NAME, vfe->id, "video", i); 2291 ret = msm_video_register(video_out, v4l2_dev, name, 2292 i == VFE_LINE_PIX ? 1 : 0); 2293 if (ret < 0) { 2294 dev_err(dev, "Failed to register video node: %d\n", 2295 ret); 2296 goto error_reg_video; 2297 } 2298 2299 ret = media_create_pad_link( 2300 &sd->entity, MSM_VFE_PAD_SRC, 2301 &video_out->vdev.entity, 0, 2302 MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED); 2303 if (ret < 0) { 2304 dev_err(dev, "Failed to link %s->%s entities: %d\n", 2305 sd->entity.name, video_out->vdev.entity.name, 2306 ret); 2307 goto error_link; 2308 } 2309 } 2310 2311 return 0; 2312 2313 error_link: 2314 msm_video_unregister(video_out); 2315 2316 error_reg_video: 2317 v4l2_device_unregister_subdev(sd); 2318 2319 error_reg_subdev: 2320 media_entity_cleanup(&sd->entity); 2321 2322 error_init: 2323 for (i--; i >= 0; i--) { 2324 sd = &vfe->line[i].subdev; 2325 video_out = &vfe->line[i].video_out; 2326 2327 msm_video_unregister(video_out); 2328 v4l2_device_unregister_subdev(sd); 2329 media_entity_cleanup(&sd->entity); 2330 } 2331 2332 return ret; 2333 } 2334 2335 /* 2336 * msm_vfe_unregister_entities - Unregister VFE module subdev node 2337 * @vfe: VFE device 2338 */ 2339 void msm_vfe_unregister_entities(struct vfe_device *vfe) 2340 { 2341 int i; 2342 2343 mutex_destroy(&vfe->power_lock); 2344 mutex_destroy(&vfe->stream_lock); 2345 2346 for (i = 0; i < ARRAY_SIZE(vfe->line); i++) { 2347 struct v4l2_subdev *sd = &vfe->line[i].subdev; 2348 struct camss_video *video_out = &vfe->line[i].video_out; 2349 2350 msm_video_unregister(video_out); 2351 v4l2_device_unregister_subdev(sd); 2352 media_entity_cleanup(&sd->entity); 2353 } 2354 } 2355