1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (C) 2009 Texas Instruments Inc 4 * Copyright (C) 2014 Lad, Prabhakar <prabhakar.csengg@gmail.com> 5 * 6 * TODO : add support for VBI & HBI data service 7 * add static buffer allocation 8 */ 9 10 #include <linux/module.h> 11 #include <linux/interrupt.h> 12 #include <linux/of_graph.h> 13 #include <linux/platform_device.h> 14 #include <linux/slab.h> 15 16 #include <media/v4l2-fwnode.h> 17 #include <media/v4l2-ioctl.h> 18 #include <media/i2c/tvp514x.h> 19 #include <media/v4l2-mediabus.h> 20 21 #include <linux/videodev2.h> 22 23 #include "vpif.h" 24 #include "vpif_capture.h" 25 26 MODULE_DESCRIPTION("TI DaVinci VPIF Capture driver"); 27 MODULE_LICENSE("GPL"); 28 MODULE_VERSION(VPIF_CAPTURE_VERSION); 29 30 #define vpif_err(fmt, arg...) v4l2_err(&vpif_obj.v4l2_dev, fmt, ## arg) 31 #define vpif_dbg(level, debug, fmt, arg...) \ 32 v4l2_dbg(level, debug, &vpif_obj.v4l2_dev, fmt, ## arg) 33 34 static int debug = 1; 35 36 module_param(debug, int, 0644); 37 38 MODULE_PARM_DESC(debug, "Debug level 0-1"); 39 40 #define VPIF_DRIVER_NAME "vpif_capture" 41 MODULE_ALIAS("platform:" VPIF_DRIVER_NAME); 42 43 /* global variables */ 44 static struct vpif_device vpif_obj = { {NULL} }; 45 static struct device *vpif_dev; 46 static void vpif_calculate_offsets(struct channel_obj *ch); 47 static void vpif_config_addr(struct channel_obj *ch, int muxmode); 48 49 static u8 channel_first_int[VPIF_NUMBER_OF_OBJECTS][2] = { {1, 1} }; 50 51 /* Is set to 1 in case of SDTV formats, 2 in case of HDTV formats. */ 52 static int ycmux_mode; 53 54 static inline 55 struct vpif_cap_buffer *to_vpif_buffer(struct vb2_v4l2_buffer *vb) 56 { 57 return container_of(vb, struct vpif_cap_buffer, vb); 58 } 59 60 /** 61 * vpif_buffer_prepare : callback function for buffer prepare 62 * @vb: ptr to vb2_buffer 63 * 64 * This is the callback function for buffer prepare when vb2_qbuf() 65 * function is called. The buffer is prepared and user space virtual address 66 * or user address is converted into physical address 67 */ 68 static int vpif_buffer_prepare(struct vb2_buffer *vb) 69 { 70 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 71 struct vb2_queue *q = vb->vb2_queue; 72 struct channel_obj *ch = vb2_get_drv_priv(q); 73 struct common_obj *common; 74 unsigned long addr; 75 76 vpif_dbg(2, debug, "vpif_buffer_prepare\n"); 77 78 common = &ch->common[VPIF_VIDEO_INDEX]; 79 80 vb2_set_plane_payload(vb, 0, common->fmt.fmt.pix.sizeimage); 81 if (vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0)) 82 return -EINVAL; 83 84 vbuf->field = common->fmt.fmt.pix.field; 85 86 addr = vb2_dma_contig_plane_dma_addr(vb, 0); 87 if (!IS_ALIGNED((addr + common->ytop_off), 8) || 88 !IS_ALIGNED((addr + common->ybtm_off), 8) || 89 !IS_ALIGNED((addr + common->ctop_off), 8) || 90 !IS_ALIGNED((addr + common->cbtm_off), 8)) { 91 vpif_dbg(1, debug, "offset is not aligned\n"); 92 return -EINVAL; 93 } 94 95 return 0; 96 } 97 98 /** 99 * vpif_buffer_queue_setup : Callback function for buffer setup. 100 * @vq: vb2_queue ptr 101 * @nbuffers: ptr to number of buffers requested by application 102 * @nplanes: contains number of distinct video planes needed to hold a frame 103 * @sizes: contains the size (in bytes) of each plane. 104 * @alloc_devs: ptr to allocation context 105 * 106 * This callback function is called when reqbuf() is called to adjust 107 * the buffer count and buffer size 108 */ 109 static int vpif_buffer_queue_setup(struct vb2_queue *vq, 110 unsigned int *nbuffers, unsigned int *nplanes, 111 unsigned int sizes[], struct device *alloc_devs[]) 112 { 113 struct channel_obj *ch = vb2_get_drv_priv(vq); 114 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX]; 115 unsigned size = common->fmt.fmt.pix.sizeimage; 116 unsigned int q_num_bufs = vb2_get_num_buffers(vq); 117 118 vpif_dbg(2, debug, "vpif_buffer_setup\n"); 119 120 if (*nplanes) { 121 if (sizes[0] < size) 122 return -EINVAL; 123 size = sizes[0]; 124 } 125 126 if (q_num_bufs + *nbuffers < 3) 127 *nbuffers = 3 - q_num_bufs; 128 129 *nplanes = 1; 130 sizes[0] = size; 131 132 /* Calculate the offset for Y and C data in the buffer */ 133 vpif_calculate_offsets(ch); 134 135 return 0; 136 } 137 138 /** 139 * vpif_buffer_queue : Callback function to add buffer to DMA queue 140 * @vb: ptr to vb2_buffer 141 */ 142 static void vpif_buffer_queue(struct vb2_buffer *vb) 143 { 144 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 145 struct channel_obj *ch = vb2_get_drv_priv(vb->vb2_queue); 146 struct vpif_cap_buffer *buf = to_vpif_buffer(vbuf); 147 struct common_obj *common; 148 unsigned long flags; 149 150 common = &ch->common[VPIF_VIDEO_INDEX]; 151 152 vpif_dbg(2, debug, "vpif_buffer_queue\n"); 153 154 spin_lock_irqsave(&common->irqlock, flags); 155 /* add the buffer to the DMA queue */ 156 list_add_tail(&buf->list, &common->dma_queue); 157 spin_unlock_irqrestore(&common->irqlock, flags); 158 } 159 160 /** 161 * vpif_start_streaming : Starts the DMA engine for streaming 162 * @vq: ptr to vb2_buffer 163 * @count: number of buffers 164 */ 165 static int vpif_start_streaming(struct vb2_queue *vq, unsigned int count) 166 { 167 struct vpif_capture_config *vpif_config_data = 168 vpif_dev->platform_data; 169 struct channel_obj *ch = vb2_get_drv_priv(vq); 170 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX]; 171 struct vpif_params *vpif = &ch->vpifparams; 172 struct vpif_cap_buffer *buf, *tmp; 173 unsigned long addr, flags; 174 int ret; 175 176 /* Initialize field_id */ 177 ch->field_id = 0; 178 179 /* configure 1 or 2 channel mode */ 180 if (vpif_config_data->setup_input_channel_mode) { 181 ret = vpif_config_data-> 182 setup_input_channel_mode(vpif->std_info.ycmux_mode); 183 if (ret < 0) { 184 vpif_dbg(1, debug, "can't set vpif channel mode\n"); 185 goto err; 186 } 187 } 188 189 ret = v4l2_subdev_call(ch->sd, video, s_stream, 1); 190 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) { 191 vpif_dbg(1, debug, "stream on failed in subdev\n"); 192 goto err; 193 } 194 195 /* Call vpif_set_params function to set the parameters and addresses */ 196 ret = vpif_set_video_params(vpif, ch->channel_id); 197 if (ret < 0) { 198 vpif_dbg(1, debug, "can't set video params\n"); 199 goto err; 200 } 201 202 ycmux_mode = ret; 203 vpif_config_addr(ch, ret); 204 205 /* Get the next frame from the buffer queue */ 206 spin_lock_irqsave(&common->irqlock, flags); 207 common->cur_frm = common->next_frm = list_entry(common->dma_queue.next, 208 struct vpif_cap_buffer, list); 209 /* Remove buffer from the buffer queue */ 210 list_del(&common->cur_frm->list); 211 spin_unlock_irqrestore(&common->irqlock, flags); 212 213 addr = vb2_dma_contig_plane_dma_addr(&common->cur_frm->vb.vb2_buf, 0); 214 215 common->set_addr(addr + common->ytop_off, 216 addr + common->ybtm_off, 217 addr + common->ctop_off, 218 addr + common->cbtm_off); 219 220 /** 221 * Set interrupt for both the fields in VPIF Register enable channel in 222 * VPIF register 223 */ 224 channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1; 225 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) { 226 channel0_intr_assert(); 227 channel0_intr_enable(1); 228 enable_channel0(1); 229 } 230 if (VPIF_CHANNEL1_VIDEO == ch->channel_id || 231 ycmux_mode == 2) { 232 channel1_intr_assert(); 233 channel1_intr_enable(1); 234 enable_channel1(1); 235 } 236 237 return 0; 238 239 err: 240 spin_lock_irqsave(&common->irqlock, flags); 241 list_for_each_entry_safe(buf, tmp, &common->dma_queue, list) { 242 list_del(&buf->list); 243 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_QUEUED); 244 } 245 spin_unlock_irqrestore(&common->irqlock, flags); 246 247 return ret; 248 } 249 250 /** 251 * vpif_stop_streaming : Stop the DMA engine 252 * @vq: ptr to vb2_queue 253 * 254 * This callback stops the DMA engine and any remaining buffers 255 * in the DMA queue are released. 256 */ 257 static void vpif_stop_streaming(struct vb2_queue *vq) 258 { 259 struct channel_obj *ch = vb2_get_drv_priv(vq); 260 struct common_obj *common; 261 unsigned long flags; 262 int ret; 263 264 common = &ch->common[VPIF_VIDEO_INDEX]; 265 266 /* Disable channel as per its device type and channel id */ 267 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) { 268 enable_channel0(0); 269 channel0_intr_enable(0); 270 } 271 if (VPIF_CHANNEL1_VIDEO == ch->channel_id || 272 ycmux_mode == 2) { 273 enable_channel1(0); 274 channel1_intr_enable(0); 275 } 276 277 ycmux_mode = 0; 278 279 ret = v4l2_subdev_call(ch->sd, video, s_stream, 0); 280 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) 281 vpif_dbg(1, debug, "stream off failed in subdev\n"); 282 283 /* release all active buffers */ 284 if (common->cur_frm == common->next_frm) { 285 vb2_buffer_done(&common->cur_frm->vb.vb2_buf, 286 VB2_BUF_STATE_ERROR); 287 } else { 288 if (common->cur_frm) 289 vb2_buffer_done(&common->cur_frm->vb.vb2_buf, 290 VB2_BUF_STATE_ERROR); 291 if (common->next_frm) 292 vb2_buffer_done(&common->next_frm->vb.vb2_buf, 293 VB2_BUF_STATE_ERROR); 294 } 295 296 spin_lock_irqsave(&common->irqlock, flags); 297 while (!list_empty(&common->dma_queue)) { 298 common->next_frm = list_entry(common->dma_queue.next, 299 struct vpif_cap_buffer, list); 300 list_del(&common->next_frm->list); 301 vb2_buffer_done(&common->next_frm->vb.vb2_buf, 302 VB2_BUF_STATE_ERROR); 303 } 304 spin_unlock_irqrestore(&common->irqlock, flags); 305 } 306 307 static const struct vb2_ops video_qops = { 308 .queue_setup = vpif_buffer_queue_setup, 309 .buf_prepare = vpif_buffer_prepare, 310 .start_streaming = vpif_start_streaming, 311 .stop_streaming = vpif_stop_streaming, 312 .buf_queue = vpif_buffer_queue, 313 }; 314 315 /** 316 * vpif_process_buffer_complete: process a completed buffer 317 * @common: ptr to common channel object 318 * 319 * This function time stamp the buffer and mark it as DONE. It also 320 * wake up any process waiting on the QUEUE and set the next buffer 321 * as current 322 */ 323 static void vpif_process_buffer_complete(struct common_obj *common) 324 { 325 common->cur_frm->vb.vb2_buf.timestamp = ktime_get_ns(); 326 vb2_buffer_done(&common->cur_frm->vb.vb2_buf, VB2_BUF_STATE_DONE); 327 /* Make curFrm pointing to nextFrm */ 328 common->cur_frm = common->next_frm; 329 } 330 331 /** 332 * vpif_schedule_next_buffer: set next buffer address for capture 333 * @common : ptr to common channel object 334 * 335 * This function will get next buffer from the dma queue and 336 * set the buffer address in the vpif register for capture. 337 * the buffer is marked active 338 */ 339 static void vpif_schedule_next_buffer(struct common_obj *common) 340 { 341 unsigned long addr = 0; 342 343 spin_lock(&common->irqlock); 344 common->next_frm = list_entry(common->dma_queue.next, 345 struct vpif_cap_buffer, list); 346 /* Remove that buffer from the buffer queue */ 347 list_del(&common->next_frm->list); 348 spin_unlock(&common->irqlock); 349 addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb.vb2_buf, 0); 350 351 /* Set top and bottom field addresses in VPIF registers */ 352 common->set_addr(addr + common->ytop_off, 353 addr + common->ybtm_off, 354 addr + common->ctop_off, 355 addr + common->cbtm_off); 356 } 357 358 /** 359 * vpif_channel_isr : ISR handler for vpif capture 360 * @irq: irq number 361 * @dev_id: dev_id ptr 362 * 363 * It changes status of the captured buffer, takes next buffer from the queue 364 * and sets its address in VPIF registers 365 */ 366 static irqreturn_t vpif_channel_isr(int irq, void *dev_id) 367 { 368 struct vpif_device *dev = &vpif_obj; 369 struct common_obj *common; 370 struct channel_obj *ch; 371 int channel_id; 372 int fid = -1, i; 373 374 channel_id = *(int *)(dev_id); 375 if (!vpif_intr_status(channel_id)) 376 return IRQ_NONE; 377 378 ch = dev->dev[channel_id]; 379 380 for (i = 0; i < VPIF_NUMBER_OF_OBJECTS; i++) { 381 common = &ch->common[i]; 382 /* skip If streaming is not started in this channel */ 383 /* Check the field format */ 384 if (1 == ch->vpifparams.std_info.frm_fmt || 385 common->fmt.fmt.pix.field == V4L2_FIELD_NONE) { 386 /* Progressive mode */ 387 spin_lock(&common->irqlock); 388 if (list_empty(&common->dma_queue)) { 389 spin_unlock(&common->irqlock); 390 continue; 391 } 392 spin_unlock(&common->irqlock); 393 394 if (!channel_first_int[i][channel_id]) 395 vpif_process_buffer_complete(common); 396 397 channel_first_int[i][channel_id] = 0; 398 399 vpif_schedule_next_buffer(common); 400 401 402 channel_first_int[i][channel_id] = 0; 403 } else { 404 /** 405 * Interlaced mode. If it is first interrupt, ignore 406 * it 407 */ 408 if (channel_first_int[i][channel_id]) { 409 channel_first_int[i][channel_id] = 0; 410 continue; 411 } 412 if (0 == i) { 413 ch->field_id ^= 1; 414 /* Get field id from VPIF registers */ 415 fid = vpif_channel_getfid(ch->channel_id); 416 if (fid != ch->field_id) { 417 /** 418 * If field id does not match stored 419 * field id, make them in sync 420 */ 421 if (0 == fid) 422 ch->field_id = fid; 423 return IRQ_HANDLED; 424 } 425 } 426 /* device field id and local field id are in sync */ 427 if (0 == fid) { 428 /* this is even field */ 429 if (common->cur_frm == common->next_frm) 430 continue; 431 432 /* mark the current buffer as done */ 433 vpif_process_buffer_complete(common); 434 } else if (1 == fid) { 435 /* odd field */ 436 spin_lock(&common->irqlock); 437 if (list_empty(&common->dma_queue) || 438 (common->cur_frm != common->next_frm)) { 439 spin_unlock(&common->irqlock); 440 continue; 441 } 442 spin_unlock(&common->irqlock); 443 444 vpif_schedule_next_buffer(common); 445 } 446 } 447 } 448 return IRQ_HANDLED; 449 } 450 451 /** 452 * vpif_update_std_info() - update standard related info 453 * @ch: ptr to channel object 454 * 455 * For a given standard selected by application, update values 456 * in the device data structures 457 */ 458 static int vpif_update_std_info(struct channel_obj *ch) 459 { 460 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX]; 461 struct vpif_params *vpifparams = &ch->vpifparams; 462 const struct vpif_channel_config_params *config; 463 struct vpif_channel_config_params *std_info = &vpifparams->std_info; 464 struct video_obj *vid_ch = &ch->video; 465 int index; 466 struct v4l2_pix_format *pixfmt = &common->fmt.fmt.pix; 467 468 vpif_dbg(2, debug, "vpif_update_std_info\n"); 469 470 /* 471 * if called after try_fmt or g_fmt, there will already be a size 472 * so use that by default. 473 */ 474 if (pixfmt->width && pixfmt->height) { 475 if (pixfmt->field == V4L2_FIELD_ANY || 476 pixfmt->field == V4L2_FIELD_NONE) 477 pixfmt->field = V4L2_FIELD_NONE; 478 479 vpifparams->iface.if_type = VPIF_IF_BT656; 480 if (pixfmt->pixelformat == V4L2_PIX_FMT_SGRBG10 || 481 pixfmt->pixelformat == V4L2_PIX_FMT_SBGGR8) 482 vpifparams->iface.if_type = VPIF_IF_RAW_BAYER; 483 484 if (pixfmt->pixelformat == V4L2_PIX_FMT_SGRBG10) 485 vpifparams->params.data_sz = 1; /* 10 bits/pixel. */ 486 487 /* 488 * For raw formats from camera sensors, we don't need 489 * the std_info from table lookup, so nothing else to do here. 490 */ 491 if (vpifparams->iface.if_type == VPIF_IF_RAW_BAYER) { 492 memset(std_info, 0, sizeof(struct vpif_channel_config_params)); 493 vpifparams->std_info.capture_format = 1; /* CCD/raw mode */ 494 return 0; 495 } 496 } 497 498 for (index = 0; index < vpif_ch_params_count; index++) { 499 config = &vpif_ch_params[index]; 500 if (config->hd_sd == 0) { 501 vpif_dbg(2, debug, "SD format\n"); 502 if (config->stdid & vid_ch->stdid) { 503 memcpy(std_info, config, sizeof(*config)); 504 break; 505 } 506 } else { 507 vpif_dbg(2, debug, "HD format\n"); 508 if (!memcmp(&config->dv_timings, &vid_ch->dv_timings, 509 sizeof(vid_ch->dv_timings))) { 510 memcpy(std_info, config, sizeof(*config)); 511 break; 512 } 513 } 514 } 515 516 /* standard not found */ 517 if (index == vpif_ch_params_count) 518 return -EINVAL; 519 520 common->fmt.fmt.pix.width = std_info->width; 521 common->width = std_info->width; 522 common->fmt.fmt.pix.height = std_info->height; 523 common->height = std_info->height; 524 common->fmt.fmt.pix.sizeimage = common->height * common->width * 2; 525 common->fmt.fmt.pix.bytesperline = std_info->width; 526 vpifparams->video_params.hpitch = std_info->width; 527 vpifparams->video_params.storage_mode = std_info->frm_fmt; 528 529 if (vid_ch->stdid) 530 common->fmt.fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; 531 else 532 common->fmt.fmt.pix.colorspace = V4L2_COLORSPACE_REC709; 533 534 if (ch->vpifparams.std_info.frm_fmt) 535 common->fmt.fmt.pix.field = V4L2_FIELD_NONE; 536 else 537 common->fmt.fmt.pix.field = V4L2_FIELD_INTERLACED; 538 539 if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER) 540 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SBGGR8; 541 else 542 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_NV16; 543 544 common->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 545 546 return 0; 547 } 548 549 /** 550 * vpif_calculate_offsets : This function calculates buffers offsets 551 * @ch : ptr to channel object 552 * 553 * This function calculates buffer offsets for Y and C in the top and 554 * bottom field 555 */ 556 static void vpif_calculate_offsets(struct channel_obj *ch) 557 { 558 unsigned int hpitch, sizeimage; 559 struct video_obj *vid_ch = &(ch->video); 560 struct vpif_params *vpifparams = &ch->vpifparams; 561 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX]; 562 enum v4l2_field field = common->fmt.fmt.pix.field; 563 564 vpif_dbg(2, debug, "vpif_calculate_offsets\n"); 565 566 if (V4L2_FIELD_ANY == field) { 567 if (vpifparams->std_info.frm_fmt) 568 vid_ch->buf_field = V4L2_FIELD_NONE; 569 else 570 vid_ch->buf_field = V4L2_FIELD_INTERLACED; 571 } else 572 vid_ch->buf_field = common->fmt.fmt.pix.field; 573 574 sizeimage = common->fmt.fmt.pix.sizeimage; 575 576 hpitch = common->fmt.fmt.pix.bytesperline; 577 578 if ((V4L2_FIELD_NONE == vid_ch->buf_field) || 579 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) { 580 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */ 581 common->ytop_off = 0; 582 common->ybtm_off = hpitch; 583 common->ctop_off = sizeimage / 2; 584 common->cbtm_off = sizeimage / 2 + hpitch; 585 } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) { 586 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */ 587 common->ytop_off = 0; 588 common->ybtm_off = sizeimage / 4; 589 common->ctop_off = sizeimage / 2; 590 common->cbtm_off = common->ctop_off + sizeimage / 4; 591 } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) { 592 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */ 593 common->ybtm_off = 0; 594 common->ytop_off = sizeimage / 4; 595 common->cbtm_off = sizeimage / 2; 596 common->ctop_off = common->cbtm_off + sizeimage / 4; 597 } 598 if ((V4L2_FIELD_NONE == vid_ch->buf_field) || 599 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) 600 vpifparams->video_params.storage_mode = 1; 601 else 602 vpifparams->video_params.storage_mode = 0; 603 604 if (1 == vpifparams->std_info.frm_fmt) 605 vpifparams->video_params.hpitch = 606 common->fmt.fmt.pix.bytesperline; 607 else { 608 if ((field == V4L2_FIELD_ANY) 609 || (field == V4L2_FIELD_INTERLACED)) 610 vpifparams->video_params.hpitch = 611 common->fmt.fmt.pix.bytesperline * 2; 612 else 613 vpifparams->video_params.hpitch = 614 common->fmt.fmt.pix.bytesperline; 615 } 616 617 ch->vpifparams.video_params.stdid = vpifparams->std_info.stdid; 618 } 619 620 /** 621 * vpif_config_addr() - function to configure buffer address in vpif 622 * @ch: channel ptr 623 * @muxmode: channel mux mode 624 */ 625 static void vpif_config_addr(struct channel_obj *ch, int muxmode) 626 { 627 struct common_obj *common; 628 629 vpif_dbg(2, debug, "vpif_config_addr\n"); 630 631 common = &(ch->common[VPIF_VIDEO_INDEX]); 632 633 if (VPIF_CHANNEL1_VIDEO == ch->channel_id) 634 common->set_addr = ch1_set_video_buf_addr; 635 else if (2 == muxmode) 636 common->set_addr = ch0_set_video_buf_addr_yc_nmux; 637 else 638 common->set_addr = ch0_set_video_buf_addr; 639 } 640 641 /** 642 * vpif_input_to_subdev() - Maps input to sub device 643 * @vpif_cfg: global config ptr 644 * @chan_cfg: channel config ptr 645 * @input_index: Given input index from application 646 * 647 * lookup the sub device information for a given input index. 648 * we report all the inputs to application. inputs table also 649 * has sub device name for the each input 650 */ 651 static int vpif_input_to_subdev( 652 struct vpif_capture_config *vpif_cfg, 653 struct vpif_capture_chan_config *chan_cfg, 654 int input_index) 655 { 656 struct vpif_subdev_info *subdev_info; 657 const char *subdev_name; 658 int i; 659 660 vpif_dbg(2, debug, "vpif_input_to_subdev\n"); 661 662 if (!chan_cfg) 663 return -1; 664 if (input_index >= chan_cfg->input_count) 665 return -1; 666 subdev_name = chan_cfg->inputs[input_index].subdev_name; 667 if (!subdev_name) 668 return -1; 669 670 /* loop through the sub device list to get the sub device info */ 671 for (i = 0; i < vpif_cfg->subdev_count; i++) { 672 subdev_info = &vpif_cfg->subdev_info[i]; 673 if (subdev_info && !strcmp(subdev_info->name, subdev_name)) 674 return i; 675 } 676 return -1; 677 } 678 679 /** 680 * vpif_set_input() - Select an input 681 * @vpif_cfg: global config ptr 682 * @ch: channel 683 * @index: Given input index from application 684 * 685 * Select the given input. 686 */ 687 static int vpif_set_input( 688 struct vpif_capture_config *vpif_cfg, 689 struct channel_obj *ch, 690 int index) 691 { 692 struct vpif_capture_chan_config *chan_cfg = 693 &vpif_cfg->chan_config[ch->channel_id]; 694 struct vpif_subdev_info *subdev_info = NULL; 695 struct v4l2_subdev *sd = NULL; 696 u32 input = 0, output = 0; 697 int sd_index; 698 int ret; 699 700 sd_index = vpif_input_to_subdev(vpif_cfg, chan_cfg, index); 701 if (sd_index >= 0) { 702 sd = vpif_obj.sd[sd_index]; 703 subdev_info = &vpif_cfg->subdev_info[sd_index]; 704 } else { 705 /* no subdevice, no input to setup */ 706 return 0; 707 } 708 709 /* first setup input path from sub device to vpif */ 710 if (sd && vpif_cfg->setup_input_path) { 711 ret = vpif_cfg->setup_input_path(ch->channel_id, 712 subdev_info->name); 713 if (ret < 0) { 714 vpif_dbg(1, debug, "couldn't setup input path for the" \ 715 " sub device %s, for input index %d\n", 716 subdev_info->name, index); 717 return ret; 718 } 719 } 720 721 if (sd) { 722 input = chan_cfg->inputs[index].input_route; 723 output = chan_cfg->inputs[index].output_route; 724 ret = v4l2_subdev_call(sd, video, s_routing, 725 input, output, 0); 726 if (ret < 0 && ret != -ENOIOCTLCMD) { 727 vpif_dbg(1, debug, "Failed to set input\n"); 728 return ret; 729 } 730 } 731 ch->input_idx = index; 732 ch->sd = sd; 733 /* copy interface parameters to vpif */ 734 ch->vpifparams.iface = chan_cfg->vpif_if; 735 736 /* update tvnorms from the sub device input info */ 737 ch->video_dev.tvnorms = chan_cfg->inputs[index].input.std; 738 return 0; 739 } 740 741 /** 742 * vpif_querystd() - querystd handler 743 * @file: file ptr 744 * @priv: file handle 745 * @std_id: ptr to std id 746 * 747 * This function is called to detect standard at the selected input 748 */ 749 static int vpif_querystd(struct file *file, void *priv, v4l2_std_id *std_id) 750 { 751 struct video_device *vdev = video_devdata(file); 752 struct channel_obj *ch = video_get_drvdata(vdev); 753 int ret; 754 755 vpif_dbg(2, debug, "vpif_querystd\n"); 756 757 /* Call querystd function of decoder device */ 758 ret = v4l2_subdev_call(ch->sd, video, querystd, std_id); 759 760 if (ret == -ENOIOCTLCMD || ret == -ENODEV) 761 return -ENODATA; 762 if (ret) { 763 vpif_dbg(1, debug, "Failed to query standard for sub devices\n"); 764 return ret; 765 } 766 767 return 0; 768 } 769 770 /** 771 * vpif_g_std() - get STD handler 772 * @file: file ptr 773 * @priv: file handle 774 * @std: ptr to std id 775 */ 776 static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std) 777 { 778 struct vpif_capture_config *config = vpif_dev->platform_data; 779 struct video_device *vdev = video_devdata(file); 780 struct channel_obj *ch = video_get_drvdata(vdev); 781 struct vpif_capture_chan_config *chan_cfg; 782 struct v4l2_input input; 783 784 vpif_dbg(2, debug, "vpif_g_std\n"); 785 786 if (!config->chan_config[ch->channel_id].inputs) 787 return -ENODATA; 788 789 chan_cfg = &config->chan_config[ch->channel_id]; 790 input = chan_cfg->inputs[ch->input_idx].input; 791 if (input.capabilities != V4L2_IN_CAP_STD) 792 return -ENODATA; 793 794 *std = ch->video.stdid; 795 return 0; 796 } 797 798 /** 799 * vpif_s_std() - set STD handler 800 * @file: file ptr 801 * @priv: file handle 802 * @std_id: ptr to std id 803 */ 804 static int vpif_s_std(struct file *file, void *priv, v4l2_std_id std_id) 805 { 806 struct vpif_capture_config *config = vpif_dev->platform_data; 807 struct video_device *vdev = video_devdata(file); 808 struct channel_obj *ch = video_get_drvdata(vdev); 809 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX]; 810 struct vpif_capture_chan_config *chan_cfg; 811 struct v4l2_input input; 812 int ret; 813 814 vpif_dbg(2, debug, "vpif_s_std\n"); 815 816 if (!config->chan_config[ch->channel_id].inputs) 817 return -ENODATA; 818 819 chan_cfg = &config->chan_config[ch->channel_id]; 820 input = chan_cfg->inputs[ch->input_idx].input; 821 if (input.capabilities != V4L2_IN_CAP_STD) 822 return -ENODATA; 823 824 if (vb2_is_busy(&common->buffer_queue)) 825 return -EBUSY; 826 827 /* Call encoder subdevice function to set the standard */ 828 ch->video.stdid = std_id; 829 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings)); 830 831 /* Get the information about the standard */ 832 if (vpif_update_std_info(ch)) { 833 vpif_err("Error getting the standard info\n"); 834 return -EINVAL; 835 } 836 837 /* set standard in the sub device */ 838 ret = v4l2_subdev_call(ch->sd, video, s_std, std_id); 839 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) { 840 vpif_dbg(1, debug, "Failed to set standard for sub devices\n"); 841 return ret; 842 } 843 return 0; 844 } 845 846 /** 847 * vpif_enum_input() - ENUMINPUT handler 848 * @file: file ptr 849 * @priv: file handle 850 * @input: ptr to input structure 851 */ 852 static int vpif_enum_input(struct file *file, void *priv, 853 struct v4l2_input *input) 854 { 855 856 struct vpif_capture_config *config = vpif_dev->platform_data; 857 struct video_device *vdev = video_devdata(file); 858 struct channel_obj *ch = video_get_drvdata(vdev); 859 struct vpif_capture_chan_config *chan_cfg; 860 861 chan_cfg = &config->chan_config[ch->channel_id]; 862 863 if (input->index >= chan_cfg->input_count) 864 return -EINVAL; 865 866 memcpy(input, &chan_cfg->inputs[input->index].input, 867 sizeof(*input)); 868 return 0; 869 } 870 871 /** 872 * vpif_g_input() - Get INPUT handler 873 * @file: file ptr 874 * @priv: file handle 875 * @index: ptr to input index 876 */ 877 static int vpif_g_input(struct file *file, void *priv, unsigned int *index) 878 { 879 struct video_device *vdev = video_devdata(file); 880 struct channel_obj *ch = video_get_drvdata(vdev); 881 882 *index = ch->input_idx; 883 return 0; 884 } 885 886 /** 887 * vpif_s_input() - Set INPUT handler 888 * @file: file ptr 889 * @priv: file handle 890 * @index: input index 891 */ 892 static int vpif_s_input(struct file *file, void *priv, unsigned int index) 893 { 894 struct vpif_capture_config *config = vpif_dev->platform_data; 895 struct video_device *vdev = video_devdata(file); 896 struct channel_obj *ch = video_get_drvdata(vdev); 897 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX]; 898 struct vpif_capture_chan_config *chan_cfg; 899 900 chan_cfg = &config->chan_config[ch->channel_id]; 901 902 if (index >= chan_cfg->input_count) 903 return -EINVAL; 904 905 if (vb2_is_busy(&common->buffer_queue)) 906 return -EBUSY; 907 908 return vpif_set_input(config, ch, index); 909 } 910 911 /** 912 * vpif_enum_fmt_vid_cap() - ENUM_FMT handler 913 * @file: file ptr 914 * @priv: file handle 915 * @fmt: ptr to V4L2 format descriptor 916 */ 917 static int vpif_enum_fmt_vid_cap(struct file *file, void *priv, 918 struct v4l2_fmtdesc *fmt) 919 { 920 struct video_device *vdev = video_devdata(file); 921 struct channel_obj *ch = video_get_drvdata(vdev); 922 923 if (fmt->index != 0) { 924 vpif_dbg(1, debug, "Invalid format index\n"); 925 return -EINVAL; 926 } 927 928 /* Fill in the information about format */ 929 if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER) 930 fmt->pixelformat = V4L2_PIX_FMT_SBGGR8; 931 else 932 fmt->pixelformat = V4L2_PIX_FMT_NV16; 933 return 0; 934 } 935 936 /** 937 * vpif_try_fmt_vid_cap() - TRY_FMT handler 938 * @file: file ptr 939 * @priv: file handle 940 * @fmt: ptr to v4l2 format structure 941 */ 942 static int vpif_try_fmt_vid_cap(struct file *file, void *priv, 943 struct v4l2_format *fmt) 944 { 945 struct video_device *vdev = video_devdata(file); 946 struct channel_obj *ch = video_get_drvdata(vdev); 947 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix; 948 struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]); 949 950 common->fmt = *fmt; 951 vpif_update_std_info(ch); 952 953 pixfmt->field = common->fmt.fmt.pix.field; 954 pixfmt->colorspace = common->fmt.fmt.pix.colorspace; 955 pixfmt->bytesperline = common->fmt.fmt.pix.width; 956 pixfmt->width = common->fmt.fmt.pix.width; 957 pixfmt->height = common->fmt.fmt.pix.height; 958 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height * 2; 959 if (pixfmt->pixelformat == V4L2_PIX_FMT_SGRBG10) { 960 pixfmt->bytesperline = common->fmt.fmt.pix.width * 2; 961 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height; 962 } 963 964 dev_dbg(vpif_dev, "%s: %d x %d; pitch=%d pixelformat=0x%08x, field=%d, size=%d\n", __func__, 965 pixfmt->width, pixfmt->height, 966 pixfmt->bytesperline, pixfmt->pixelformat, 967 pixfmt->field, pixfmt->sizeimage); 968 969 return 0; 970 } 971 972 973 /** 974 * vpif_g_fmt_vid_cap() - Set INPUT handler 975 * @file: file ptr 976 * @priv: file handle 977 * @fmt: ptr to v4l2 format structure 978 */ 979 static int vpif_g_fmt_vid_cap(struct file *file, void *priv, 980 struct v4l2_format *fmt) 981 { 982 struct video_device *vdev = video_devdata(file); 983 struct channel_obj *ch = video_get_drvdata(vdev); 984 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX]; 985 struct v4l2_pix_format *pix_fmt = &fmt->fmt.pix; 986 struct v4l2_subdev_format format = { 987 .which = V4L2_SUBDEV_FORMAT_ACTIVE, 988 }; 989 struct v4l2_mbus_framefmt *mbus_fmt = &format.format; 990 int ret; 991 992 /* Check the validity of the buffer type */ 993 if (common->fmt.type != fmt->type) 994 return -EINVAL; 995 996 /* By default, use currently set fmt */ 997 *fmt = common->fmt; 998 999 /* If subdev has get_fmt, use that to override */ 1000 ret = v4l2_subdev_call(ch->sd, pad, get_fmt, NULL, &format); 1001 if (!ret && mbus_fmt->code) { 1002 v4l2_fill_pix_format(pix_fmt, mbus_fmt); 1003 pix_fmt->bytesperline = pix_fmt->width; 1004 if (mbus_fmt->code == MEDIA_BUS_FMT_SGRBG10_1X10) { 1005 /* e.g. mt9v032 */ 1006 pix_fmt->pixelformat = V4L2_PIX_FMT_SGRBG10; 1007 pix_fmt->bytesperline = pix_fmt->width * 2; 1008 } else if (mbus_fmt->code == MEDIA_BUS_FMT_UYVY8_2X8) { 1009 /* e.g. tvp514x */ 1010 pix_fmt->pixelformat = V4L2_PIX_FMT_NV16; 1011 pix_fmt->bytesperline = pix_fmt->width * 2; 1012 } else { 1013 dev_warn(vpif_dev, "%s: Unhandled media-bus format 0x%x\n", 1014 __func__, mbus_fmt->code); 1015 } 1016 pix_fmt->sizeimage = pix_fmt->bytesperline * pix_fmt->height; 1017 dev_dbg(vpif_dev, "%s: %d x %d; pitch=%d, pixelformat=0x%08x, code=0x%x, field=%d, size=%d\n", __func__, 1018 pix_fmt->width, pix_fmt->height, 1019 pix_fmt->bytesperline, pix_fmt->pixelformat, 1020 mbus_fmt->code, pix_fmt->field, pix_fmt->sizeimage); 1021 1022 common->fmt = *fmt; 1023 vpif_update_std_info(ch); 1024 } 1025 1026 return 0; 1027 } 1028 1029 /** 1030 * vpif_s_fmt_vid_cap() - Set FMT handler 1031 * @file: file ptr 1032 * @priv: file handle 1033 * @fmt: ptr to v4l2 format structure 1034 */ 1035 static int vpif_s_fmt_vid_cap(struct file *file, void *priv, 1036 struct v4l2_format *fmt) 1037 { 1038 struct video_device *vdev = video_devdata(file); 1039 struct channel_obj *ch = video_get_drvdata(vdev); 1040 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX]; 1041 int ret; 1042 1043 vpif_dbg(2, debug, "%s\n", __func__); 1044 1045 if (vb2_is_busy(&common->buffer_queue)) 1046 return -EBUSY; 1047 1048 ret = vpif_try_fmt_vid_cap(file, priv, fmt); 1049 if (ret) 1050 return ret; 1051 1052 /* store the format in the channel object */ 1053 common->fmt = *fmt; 1054 return 0; 1055 } 1056 1057 /** 1058 * vpif_querycap() - QUERYCAP handler 1059 * @file: file ptr 1060 * @priv: file handle 1061 * @cap: ptr to v4l2_capability structure 1062 */ 1063 static int vpif_querycap(struct file *file, void *priv, 1064 struct v4l2_capability *cap) 1065 { 1066 struct vpif_capture_config *config = vpif_dev->platform_data; 1067 1068 strscpy(cap->driver, VPIF_DRIVER_NAME, sizeof(cap->driver)); 1069 strscpy(cap->card, config->card_name, sizeof(cap->card)); 1070 1071 return 0; 1072 } 1073 1074 /** 1075 * vpif_enum_dv_timings() - ENUM_DV_TIMINGS handler 1076 * @file: file ptr 1077 * @priv: file handle 1078 * @timings: input timings 1079 */ 1080 static int 1081 vpif_enum_dv_timings(struct file *file, void *priv, 1082 struct v4l2_enum_dv_timings *timings) 1083 { 1084 struct vpif_capture_config *config = vpif_dev->platform_data; 1085 struct video_device *vdev = video_devdata(file); 1086 struct channel_obj *ch = video_get_drvdata(vdev); 1087 struct vpif_capture_chan_config *chan_cfg; 1088 struct v4l2_input input; 1089 int ret; 1090 1091 if (!config->chan_config[ch->channel_id].inputs) 1092 return -ENODATA; 1093 1094 chan_cfg = &config->chan_config[ch->channel_id]; 1095 input = chan_cfg->inputs[ch->input_idx].input; 1096 if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS) 1097 return -ENODATA; 1098 1099 timings->pad = 0; 1100 1101 ret = v4l2_subdev_call(ch->sd, pad, enum_dv_timings, timings); 1102 if (ret == -ENOIOCTLCMD || ret == -ENODEV) 1103 return -EINVAL; 1104 1105 return ret; 1106 } 1107 1108 /** 1109 * vpif_query_dv_timings() - QUERY_DV_TIMINGS handler 1110 * @file: file ptr 1111 * @priv: file handle 1112 * @timings: input timings 1113 */ 1114 static int 1115 vpif_query_dv_timings(struct file *file, void *priv, 1116 struct v4l2_dv_timings *timings) 1117 { 1118 struct vpif_capture_config *config = vpif_dev->platform_data; 1119 struct video_device *vdev = video_devdata(file); 1120 struct channel_obj *ch = video_get_drvdata(vdev); 1121 struct vpif_capture_chan_config *chan_cfg; 1122 struct v4l2_input input; 1123 int ret; 1124 1125 if (!config->chan_config[ch->channel_id].inputs) 1126 return -ENODATA; 1127 1128 chan_cfg = &config->chan_config[ch->channel_id]; 1129 input = chan_cfg->inputs[ch->input_idx].input; 1130 if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS) 1131 return -ENODATA; 1132 1133 ret = v4l2_subdev_call(ch->sd, pad, query_dv_timings, 0, timings); 1134 if (ret == -ENOIOCTLCMD || ret == -ENODEV) 1135 return -ENODATA; 1136 1137 return ret; 1138 } 1139 1140 /** 1141 * vpif_s_dv_timings() - S_DV_TIMINGS handler 1142 * @file: file ptr 1143 * @priv: file handle 1144 * @timings: digital video timings 1145 */ 1146 static int vpif_s_dv_timings(struct file *file, void *priv, 1147 struct v4l2_dv_timings *timings) 1148 { 1149 struct vpif_capture_config *config = vpif_dev->platform_data; 1150 struct video_device *vdev = video_devdata(file); 1151 struct channel_obj *ch = video_get_drvdata(vdev); 1152 struct vpif_params *vpifparams = &ch->vpifparams; 1153 struct vpif_channel_config_params *std_info = &vpifparams->std_info; 1154 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX]; 1155 struct video_obj *vid_ch = &ch->video; 1156 struct v4l2_bt_timings *bt = &vid_ch->dv_timings.bt; 1157 struct vpif_capture_chan_config *chan_cfg; 1158 struct v4l2_input input; 1159 int ret; 1160 1161 if (!config->chan_config[ch->channel_id].inputs) 1162 return -ENODATA; 1163 1164 chan_cfg = &config->chan_config[ch->channel_id]; 1165 input = chan_cfg->inputs[ch->input_idx].input; 1166 if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS) 1167 return -ENODATA; 1168 1169 if (timings->type != V4L2_DV_BT_656_1120) { 1170 vpif_dbg(2, debug, "Timing type not defined\n"); 1171 return -EINVAL; 1172 } 1173 1174 if (vb2_is_busy(&common->buffer_queue)) 1175 return -EBUSY; 1176 1177 /* Configure subdevice timings, if any */ 1178 ret = v4l2_subdev_call(ch->sd, pad, s_dv_timings, 0, timings); 1179 if (ret == -ENOIOCTLCMD || ret == -ENODEV) 1180 ret = 0; 1181 if (ret < 0) { 1182 vpif_dbg(2, debug, "Error setting custom DV timings\n"); 1183 return ret; 1184 } 1185 1186 if (!(timings->bt.width && timings->bt.height && 1187 (timings->bt.hbackporch || 1188 timings->bt.hfrontporch || 1189 timings->bt.hsync) && 1190 timings->bt.vfrontporch && 1191 (timings->bt.vbackporch || 1192 timings->bt.vsync))) { 1193 vpif_dbg(2, debug, "Timings for width, height, horizontal back porch, horizontal sync, horizontal front porch, vertical back porch, vertical sync and vertical back porch must be defined\n"); 1194 return -EINVAL; 1195 } 1196 1197 vid_ch->dv_timings = *timings; 1198 1199 /* Configure video port timings */ 1200 1201 std_info->eav2sav = V4L2_DV_BT_BLANKING_WIDTH(bt) - 8; 1202 std_info->sav2eav = bt->width; 1203 1204 std_info->l1 = 1; 1205 std_info->l3 = bt->vsync + bt->vbackporch + 1; 1206 1207 std_info->vsize = V4L2_DV_BT_FRAME_HEIGHT(bt); 1208 if (bt->interlaced) { 1209 if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) { 1210 std_info->l5 = std_info->vsize/2 - 1211 (bt->vfrontporch - 1); 1212 std_info->l7 = std_info->vsize/2 + 1; 1213 std_info->l9 = std_info->l7 + bt->il_vsync + 1214 bt->il_vbackporch + 1; 1215 std_info->l11 = std_info->vsize - 1216 (bt->il_vfrontporch - 1); 1217 } else { 1218 vpif_dbg(2, debug, "Required timing values for interlaced BT format missing\n"); 1219 return -EINVAL; 1220 } 1221 } else { 1222 std_info->l5 = std_info->vsize - (bt->vfrontporch - 1); 1223 } 1224 strscpy(std_info->name, "Custom timings BT656/1120", 1225 sizeof(std_info->name)); 1226 std_info->width = bt->width; 1227 std_info->height = bt->height; 1228 std_info->frm_fmt = bt->interlaced ? 0 : 1; 1229 std_info->ycmux_mode = 0; 1230 std_info->capture_format = 0; 1231 std_info->vbi_supported = 0; 1232 std_info->hd_sd = 1; 1233 std_info->stdid = 0; 1234 1235 vid_ch->stdid = 0; 1236 return 0; 1237 } 1238 1239 /** 1240 * vpif_g_dv_timings() - G_DV_TIMINGS handler 1241 * @file: file ptr 1242 * @priv: file handle 1243 * @timings: digital video timings 1244 */ 1245 static int vpif_g_dv_timings(struct file *file, void *priv, 1246 struct v4l2_dv_timings *timings) 1247 { 1248 struct vpif_capture_config *config = vpif_dev->platform_data; 1249 struct video_device *vdev = video_devdata(file); 1250 struct channel_obj *ch = video_get_drvdata(vdev); 1251 struct video_obj *vid_ch = &ch->video; 1252 struct vpif_capture_chan_config *chan_cfg; 1253 struct v4l2_input input; 1254 1255 if (!config->chan_config[ch->channel_id].inputs) 1256 return -ENODATA; 1257 1258 chan_cfg = &config->chan_config[ch->channel_id]; 1259 input = chan_cfg->inputs[ch->input_idx].input; 1260 if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS) 1261 return -ENODATA; 1262 1263 *timings = vid_ch->dv_timings; 1264 1265 return 0; 1266 } 1267 1268 /* 1269 * vpif_log_status() - Status information 1270 * @file: file ptr 1271 * @priv: file handle 1272 * 1273 * Returns zero. 1274 */ 1275 static int vpif_log_status(struct file *filep, void *priv) 1276 { 1277 /* status for sub devices */ 1278 v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status); 1279 1280 return 0; 1281 } 1282 1283 /* vpif capture ioctl operations */ 1284 static const struct v4l2_ioctl_ops vpif_ioctl_ops = { 1285 .vidioc_querycap = vpif_querycap, 1286 .vidioc_enum_fmt_vid_cap = vpif_enum_fmt_vid_cap, 1287 .vidioc_g_fmt_vid_cap = vpif_g_fmt_vid_cap, 1288 .vidioc_s_fmt_vid_cap = vpif_s_fmt_vid_cap, 1289 .vidioc_try_fmt_vid_cap = vpif_try_fmt_vid_cap, 1290 1291 .vidioc_enum_input = vpif_enum_input, 1292 .vidioc_s_input = vpif_s_input, 1293 .vidioc_g_input = vpif_g_input, 1294 1295 .vidioc_reqbufs = vb2_ioctl_reqbufs, 1296 .vidioc_create_bufs = vb2_ioctl_create_bufs, 1297 .vidioc_querybuf = vb2_ioctl_querybuf, 1298 .vidioc_qbuf = vb2_ioctl_qbuf, 1299 .vidioc_dqbuf = vb2_ioctl_dqbuf, 1300 .vidioc_expbuf = vb2_ioctl_expbuf, 1301 .vidioc_streamon = vb2_ioctl_streamon, 1302 .vidioc_streamoff = vb2_ioctl_streamoff, 1303 1304 .vidioc_querystd = vpif_querystd, 1305 .vidioc_s_std = vpif_s_std, 1306 .vidioc_g_std = vpif_g_std, 1307 1308 .vidioc_enum_dv_timings = vpif_enum_dv_timings, 1309 .vidioc_query_dv_timings = vpif_query_dv_timings, 1310 .vidioc_s_dv_timings = vpif_s_dv_timings, 1311 .vidioc_g_dv_timings = vpif_g_dv_timings, 1312 1313 .vidioc_log_status = vpif_log_status, 1314 }; 1315 1316 /* vpif file operations */ 1317 static const struct v4l2_file_operations vpif_fops = { 1318 .owner = THIS_MODULE, 1319 .open = v4l2_fh_open, 1320 .release = vb2_fop_release, 1321 .unlocked_ioctl = video_ioctl2, 1322 .mmap = vb2_fop_mmap, 1323 .poll = vb2_fop_poll 1324 }; 1325 1326 /** 1327 * initialize_vpif() - Initialize vpif data structures 1328 * 1329 * Allocate memory for data structures and initialize them 1330 */ 1331 static int initialize_vpif(void) 1332 { 1333 int err, i, j; 1334 int free_channel_objects_index; 1335 1336 /* Allocate memory for six channel objects */ 1337 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) { 1338 vpif_obj.dev[i] = kzalloc_obj(*vpif_obj.dev[i]); 1339 /* If memory allocation fails, return error */ 1340 if (!vpif_obj.dev[i]) { 1341 free_channel_objects_index = i; 1342 err = -ENOMEM; 1343 goto vpif_init_free_channel_objects; 1344 } 1345 } 1346 return 0; 1347 1348 vpif_init_free_channel_objects: 1349 for (j = 0; j < free_channel_objects_index; j++) 1350 kfree(vpif_obj.dev[j]); 1351 return err; 1352 } 1353 1354 static inline void free_vpif_objs(void) 1355 { 1356 int i; 1357 1358 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) 1359 kfree(vpif_obj.dev[i]); 1360 } 1361 1362 static int vpif_async_bound(struct v4l2_async_notifier *notifier, 1363 struct v4l2_subdev *subdev, 1364 struct v4l2_async_connection *asd) 1365 { 1366 int i; 1367 1368 for (i = 0; i < vpif_obj.config->asd_sizes[0]; i++) { 1369 struct v4l2_async_connection *_asd = vpif_obj.config->asd[i]; 1370 const struct fwnode_handle *fwnode = _asd->match.fwnode; 1371 1372 if (fwnode == subdev->fwnode) { 1373 vpif_obj.sd[i] = subdev; 1374 vpif_obj.config->chan_config->inputs[i].subdev_name = 1375 (char *)to_of_node(subdev->fwnode)->full_name; 1376 vpif_dbg(2, debug, 1377 "%s: setting input %d subdev_name = %s\n", 1378 __func__, i, 1379 vpif_obj.config->chan_config->inputs[i].subdev_name); 1380 return 0; 1381 } 1382 } 1383 1384 for (i = 0; i < vpif_obj.config->subdev_count; i++) 1385 if (!strcmp(vpif_obj.config->subdev_info[i].name, 1386 subdev->name)) { 1387 vpif_obj.sd[i] = subdev; 1388 return 0; 1389 } 1390 1391 return -EINVAL; 1392 } 1393 1394 static int vpif_probe_complete(void) 1395 { 1396 struct common_obj *common; 1397 struct video_device *vdev; 1398 struct channel_obj *ch; 1399 struct vb2_queue *q; 1400 int j, err, k; 1401 1402 for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) { 1403 ch = vpif_obj.dev[j]; 1404 ch->channel_id = j; 1405 common = &(ch->common[VPIF_VIDEO_INDEX]); 1406 spin_lock_init(&common->irqlock); 1407 mutex_init(&common->lock); 1408 1409 /* select input 0 */ 1410 err = vpif_set_input(vpif_obj.config, ch, 0); 1411 if (err) 1412 goto probe_out; 1413 1414 /* set initial format */ 1415 ch->video.stdid = V4L2_STD_525_60; 1416 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings)); 1417 common->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 1418 vpif_update_std_info(ch); 1419 1420 /* Initialize vb2 queue */ 1421 q = &common->buffer_queue; 1422 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 1423 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF; 1424 q->drv_priv = ch; 1425 q->ops = &video_qops; 1426 q->mem_ops = &vb2_dma_contig_memops; 1427 q->buf_struct_size = sizeof(struct vpif_cap_buffer); 1428 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 1429 q->min_queued_buffers = 1; 1430 q->lock = &common->lock; 1431 q->dev = vpif_dev; 1432 1433 err = vb2_queue_init(q); 1434 if (err) { 1435 vpif_err("vpif_capture: vb2_queue_init() failed\n"); 1436 goto probe_out; 1437 } 1438 1439 INIT_LIST_HEAD(&common->dma_queue); 1440 1441 /* Initialize the video_device structure */ 1442 vdev = &ch->video_dev; 1443 strscpy(vdev->name, VPIF_DRIVER_NAME, sizeof(vdev->name)); 1444 vdev->release = video_device_release_empty; 1445 vdev->fops = &vpif_fops; 1446 vdev->ioctl_ops = &vpif_ioctl_ops; 1447 vdev->v4l2_dev = &vpif_obj.v4l2_dev; 1448 vdev->vfl_dir = VFL_DIR_RX; 1449 vdev->queue = q; 1450 vdev->lock = &common->lock; 1451 vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING; 1452 video_set_drvdata(&ch->video_dev, ch); 1453 err = video_register_device(vdev, 1454 VFL_TYPE_VIDEO, (j ? 1 : 0)); 1455 if (err) 1456 goto probe_out; 1457 } 1458 1459 v4l2_info(&vpif_obj.v4l2_dev, "VPIF capture driver initialized\n"); 1460 return 0; 1461 1462 probe_out: 1463 for (k = 0; k < j; k++) { 1464 /* Get the pointer to the channel object */ 1465 ch = vpif_obj.dev[k]; 1466 /* Unregister video device */ 1467 video_unregister_device(&ch->video_dev); 1468 } 1469 1470 return err; 1471 } 1472 1473 static int vpif_async_complete(struct v4l2_async_notifier *notifier) 1474 { 1475 return vpif_probe_complete(); 1476 } 1477 1478 static const struct v4l2_async_notifier_operations vpif_async_ops = { 1479 .bound = vpif_async_bound, 1480 .complete = vpif_async_complete, 1481 }; 1482 1483 static struct vpif_capture_config * 1484 vpif_capture_get_pdata(struct platform_device *pdev, 1485 struct v4l2_device *v4l2_dev) 1486 { 1487 struct device_node *endpoint; 1488 struct device_node *rem = NULL; 1489 struct vpif_capture_config *pdata; 1490 struct vpif_subdev_info *sdinfo; 1491 struct vpif_capture_chan_config *chan; 1492 unsigned int i; 1493 1494 v4l2_async_nf_init(&vpif_obj.notifier, v4l2_dev); 1495 1496 /* 1497 * DT boot: OF node from parent device contains 1498 * video ports & endpoints data. 1499 */ 1500 if (pdev->dev.parent && pdev->dev.parent->of_node) 1501 device_set_of_node_from_dev(&pdev->dev, pdev->dev.parent); 1502 if (!IS_ENABLED(CONFIG_OF) || !pdev->dev.of_node) 1503 return pdev->dev.platform_data; 1504 1505 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); 1506 if (!pdata) 1507 return NULL; 1508 pdata->subdev_info = 1509 devm_kcalloc(&pdev->dev, 1510 VPIF_CAPTURE_NUM_CHANNELS, 1511 sizeof(*pdata->subdev_info), 1512 GFP_KERNEL); 1513 1514 if (!pdata->subdev_info) 1515 return NULL; 1516 1517 i = 0; 1518 for_each_endpoint_of_node(pdev->dev.of_node, endpoint) { 1519 struct v4l2_fwnode_endpoint bus_cfg = { .bus_type = 0 }; 1520 unsigned int flags; 1521 int err; 1522 1523 rem = of_graph_get_remote_port_parent(endpoint); 1524 if (!rem) { 1525 dev_dbg(&pdev->dev, "Remote device at %pOF not found\n", 1526 endpoint); 1527 goto done; 1528 } 1529 1530 sdinfo = &pdata->subdev_info[i]; 1531 chan = &pdata->chan_config[i]; 1532 chan->inputs = devm_kcalloc(&pdev->dev, 1533 VPIF_CAPTURE_NUM_CHANNELS, 1534 sizeof(*chan->inputs), 1535 GFP_KERNEL); 1536 if (!chan->inputs) 1537 goto err_cleanup; 1538 1539 chan->input_count++; 1540 chan->inputs[i].input.type = V4L2_INPUT_TYPE_CAMERA; 1541 chan->inputs[i].input.std = V4L2_STD_ALL; 1542 chan->inputs[i].input.capabilities = V4L2_IN_CAP_STD; 1543 1544 err = v4l2_fwnode_endpoint_parse(of_fwnode_handle(endpoint), 1545 &bus_cfg); 1546 if (err) { 1547 dev_err(&pdev->dev, "Could not parse the endpoint\n"); 1548 of_node_put(rem); 1549 goto done; 1550 } 1551 1552 dev_dbg(&pdev->dev, "Endpoint %pOF, bus_width = %d\n", 1553 endpoint, bus_cfg.bus.parallel.bus_width); 1554 1555 flags = bus_cfg.bus.parallel.flags; 1556 1557 if (flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH) 1558 chan->vpif_if.hd_pol = 1; 1559 1560 if (flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH) 1561 chan->vpif_if.vd_pol = 1; 1562 1563 dev_dbg(&pdev->dev, "Remote device %pOF found\n", rem); 1564 sdinfo->name = rem->full_name; 1565 1566 pdata->asd[i] = v4l2_async_nf_add_fwnode(&vpif_obj.notifier, 1567 of_fwnode_handle(rem), 1568 struct v4l2_async_connection); 1569 if (IS_ERR(pdata->asd[i])) 1570 goto err_cleanup; 1571 1572 of_node_put(rem); 1573 1574 i++; 1575 if (i >= VPIF_CAPTURE_NUM_CHANNELS) 1576 break; 1577 } 1578 1579 done: 1580 of_node_put(endpoint); 1581 pdata->asd_sizes[0] = i; 1582 pdata->subdev_count = i; 1583 pdata->card_name = "DA850/OMAP-L138 Video Capture"; 1584 1585 return pdata; 1586 1587 err_cleanup: 1588 of_node_put(rem); 1589 of_node_put(endpoint); 1590 v4l2_async_nf_cleanup(&vpif_obj.notifier); 1591 1592 return NULL; 1593 } 1594 1595 /** 1596 * vpif_probe : This function probes the vpif capture driver 1597 * @pdev: platform device pointer 1598 * 1599 * This creates device entries by register itself to the V4L2 driver and 1600 * initializes fields of each channel objects 1601 */ 1602 static int vpif_probe(struct platform_device *pdev) 1603 { 1604 struct vpif_subdev_info *subdevdata; 1605 struct i2c_adapter *i2c_adap; 1606 int subdev_count; 1607 int res_idx = 0; 1608 int i, err; 1609 1610 vpif_dev = &pdev->dev; 1611 1612 err = initialize_vpif(); 1613 if (err) { 1614 v4l2_err(vpif_dev->driver, "Error initializing vpif\n"); 1615 return err; 1616 } 1617 1618 err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev); 1619 if (err) { 1620 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n"); 1621 goto vpif_free; 1622 } 1623 1624 do { 1625 int irq; 1626 1627 err = platform_get_irq_optional(pdev, res_idx); 1628 if (err < 0 && err != -ENXIO) 1629 goto vpif_unregister; 1630 if (err > 0) 1631 irq = err; 1632 else 1633 break; 1634 1635 err = devm_request_irq(&pdev->dev, irq, vpif_channel_isr, 1636 IRQF_SHARED, VPIF_DRIVER_NAME, 1637 (void *)(&vpif_obj.dev[res_idx]->channel_id)); 1638 if (err) 1639 goto vpif_unregister; 1640 } while (++res_idx); 1641 1642 pdev->dev.platform_data = 1643 vpif_capture_get_pdata(pdev, &vpif_obj.v4l2_dev); 1644 if (!pdev->dev.platform_data) { 1645 err = -EINVAL; 1646 dev_warn(&pdev->dev, "Missing platform data. Giving up.\n"); 1647 goto vpif_unregister; 1648 } 1649 1650 vpif_obj.config = pdev->dev.platform_data; 1651 1652 subdev_count = vpif_obj.config->subdev_count; 1653 vpif_obj.sd = kzalloc_objs(*vpif_obj.sd, subdev_count); 1654 if (!vpif_obj.sd) { 1655 err = -ENOMEM; 1656 goto probe_subdev_out; 1657 } 1658 1659 if (!vpif_obj.config->asd_sizes[0]) { 1660 int i2c_id = vpif_obj.config->i2c_adapter_id; 1661 1662 i2c_adap = i2c_get_adapter(i2c_id); 1663 WARN_ON(!i2c_adap); 1664 for (i = 0; i < subdev_count; i++) { 1665 subdevdata = &vpif_obj.config->subdev_info[i]; 1666 vpif_obj.sd[i] = 1667 v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev, 1668 i2c_adap, 1669 &subdevdata-> 1670 board_info, 1671 NULL); 1672 1673 if (!vpif_obj.sd[i]) { 1674 vpif_err("Error registering v4l2 subdevice\n"); 1675 err = -ENODEV; 1676 goto probe_subdev_out; 1677 } 1678 v4l2_info(&vpif_obj.v4l2_dev, 1679 "registered sub device %s\n", 1680 subdevdata->name); 1681 } 1682 err = vpif_probe_complete(); 1683 if (err) 1684 goto probe_subdev_out; 1685 } else { 1686 vpif_obj.notifier.ops = &vpif_async_ops; 1687 err = v4l2_async_nf_register(&vpif_obj.notifier); 1688 if (err) { 1689 vpif_err("Error registering async notifier\n"); 1690 err = -EINVAL; 1691 goto probe_subdev_out; 1692 } 1693 } 1694 1695 return 0; 1696 1697 probe_subdev_out: 1698 v4l2_async_nf_cleanup(&vpif_obj.notifier); 1699 /* free sub devices memory */ 1700 kfree(vpif_obj.sd); 1701 vpif_unregister: 1702 v4l2_device_unregister(&vpif_obj.v4l2_dev); 1703 vpif_free: 1704 free_vpif_objs(); 1705 1706 return err; 1707 } 1708 1709 /** 1710 * vpif_remove() - driver remove handler 1711 * @device: ptr to platform device structure 1712 * 1713 * The vidoe device is unregistered 1714 */ 1715 static void vpif_remove(struct platform_device *device) 1716 { 1717 struct channel_obj *ch; 1718 int i; 1719 1720 v4l2_async_nf_unregister(&vpif_obj.notifier); 1721 v4l2_async_nf_cleanup(&vpif_obj.notifier); 1722 v4l2_device_unregister(&vpif_obj.v4l2_dev); 1723 1724 kfree(vpif_obj.sd); 1725 /* un-register device */ 1726 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) { 1727 /* Get the pointer to the channel object */ 1728 ch = vpif_obj.dev[i]; 1729 /* Unregister video device */ 1730 video_unregister_device(&ch->video_dev); 1731 kfree(vpif_obj.dev[i]); 1732 } 1733 } 1734 1735 #ifdef CONFIG_PM_SLEEP 1736 /** 1737 * vpif_suspend: vpif device suspend 1738 * @dev: pointer to &struct device 1739 */ 1740 static int vpif_suspend(struct device *dev) 1741 { 1742 1743 struct common_obj *common; 1744 struct channel_obj *ch; 1745 int i; 1746 1747 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) { 1748 /* Get the pointer to the channel object */ 1749 ch = vpif_obj.dev[i]; 1750 common = &ch->common[VPIF_VIDEO_INDEX]; 1751 1752 if (!vb2_start_streaming_called(&common->buffer_queue)) 1753 continue; 1754 1755 mutex_lock(&common->lock); 1756 /* Disable channel */ 1757 if (ch->channel_id == VPIF_CHANNEL0_VIDEO) { 1758 enable_channel0(0); 1759 channel0_intr_enable(0); 1760 } 1761 if (ch->channel_id == VPIF_CHANNEL1_VIDEO || 1762 ycmux_mode == 2) { 1763 enable_channel1(0); 1764 channel1_intr_enable(0); 1765 } 1766 mutex_unlock(&common->lock); 1767 } 1768 1769 return 0; 1770 } 1771 1772 /* 1773 * vpif_resume: vpif device suspend 1774 */ 1775 static int vpif_resume(struct device *dev) 1776 { 1777 struct common_obj *common; 1778 struct channel_obj *ch; 1779 int i; 1780 1781 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) { 1782 /* Get the pointer to the channel object */ 1783 ch = vpif_obj.dev[i]; 1784 common = &ch->common[VPIF_VIDEO_INDEX]; 1785 1786 if (!vb2_start_streaming_called(&common->buffer_queue)) 1787 continue; 1788 1789 mutex_lock(&common->lock); 1790 /* Enable channel */ 1791 if (ch->channel_id == VPIF_CHANNEL0_VIDEO) { 1792 enable_channel0(1); 1793 channel0_intr_enable(1); 1794 } 1795 if (ch->channel_id == VPIF_CHANNEL1_VIDEO || 1796 ycmux_mode == 2) { 1797 enable_channel1(1); 1798 channel1_intr_enable(1); 1799 } 1800 mutex_unlock(&common->lock); 1801 } 1802 1803 return 0; 1804 } 1805 #endif 1806 1807 static SIMPLE_DEV_PM_OPS(vpif_pm_ops, vpif_suspend, vpif_resume); 1808 1809 static struct platform_driver vpif_driver = { 1810 .driver = { 1811 .name = VPIF_DRIVER_NAME, 1812 .pm = &vpif_pm_ops, 1813 }, 1814 .probe = vpif_probe, 1815 .remove = vpif_remove, 1816 }; 1817 1818 module_platform_driver(vpif_driver); 1819