1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * SuperH Video Output Unit (VOU) driver 4 * 5 * Copyright (C) 2010, Guennadi Liakhovetski <g.liakhovetski@gmx.de> 6 */ 7 8 #include <linux/dma-mapping.h> 9 #include <linux/delay.h> 10 #include <linux/errno.h> 11 #include <linux/fs.h> 12 #include <linux/i2c.h> 13 #include <linux/init.h> 14 #include <linux/interrupt.h> 15 #include <linux/kernel.h> 16 #include <linux/platform_device.h> 17 #include <linux/pm_runtime.h> 18 #include <linux/slab.h> 19 #include <linux/videodev2.h> 20 #include <linux/module.h> 21 22 #include <media/drv-intf/sh_vou.h> 23 #include <media/v4l2-common.h> 24 #include <media/v4l2-device.h> 25 #include <media/v4l2-ioctl.h> 26 #include <media/v4l2-mediabus.h> 27 #include <media/videobuf2-v4l2.h> 28 #include <media/videobuf2-dma-contig.h> 29 30 /* Mirror addresses are not available for all registers */ 31 #define VOUER 0 32 #define VOUCR 4 33 #define VOUSTR 8 34 #define VOUVCR 0xc 35 #define VOUISR 0x10 36 #define VOUBCR 0x14 37 #define VOUDPR 0x18 38 #define VOUDSR 0x1c 39 #define VOUVPR 0x20 40 #define VOUIR 0x24 41 #define VOUSRR 0x28 42 #define VOUMSR 0x2c 43 #define VOUHIR 0x30 44 #define VOUDFR 0x34 45 #define VOUAD1R 0x38 46 #define VOUAD2R 0x3c 47 #define VOUAIR 0x40 48 #define VOUSWR 0x44 49 #define VOURCR 0x48 50 #define VOURPR 0x50 51 52 enum sh_vou_status { 53 SH_VOU_IDLE, 54 SH_VOU_INITIALISING, 55 SH_VOU_RUNNING, 56 }; 57 58 #define VOU_MIN_IMAGE_WIDTH 16 59 #define VOU_MAX_IMAGE_WIDTH 720 60 #define VOU_MIN_IMAGE_HEIGHT 16 61 62 struct sh_vou_buffer { 63 struct vb2_v4l2_buffer vb; 64 struct list_head list; 65 }; 66 67 static inline struct 68 sh_vou_buffer *to_sh_vou_buffer(struct vb2_v4l2_buffer *vb2) 69 { 70 return container_of(vb2, struct sh_vou_buffer, vb); 71 } 72 73 struct sh_vou_device { 74 struct v4l2_device v4l2_dev; 75 struct video_device vdev; 76 struct sh_vou_pdata *pdata; 77 spinlock_t lock; 78 void __iomem *base; 79 /* State information */ 80 struct v4l2_pix_format pix; 81 struct v4l2_rect rect; 82 struct list_head buf_list; 83 v4l2_std_id std; 84 int pix_idx; 85 struct vb2_queue queue; 86 struct sh_vou_buffer *active; 87 enum sh_vou_status status; 88 unsigned sequence; 89 struct mutex fop_lock; 90 }; 91 92 /* Register access routines for sides A, B and mirror addresses */ 93 static void sh_vou_reg_a_write(struct sh_vou_device *vou_dev, unsigned int reg, 94 u32 value) 95 { 96 __raw_writel(value, vou_dev->base + reg); 97 } 98 99 static void sh_vou_reg_ab_write(struct sh_vou_device *vou_dev, unsigned int reg, 100 u32 value) 101 { 102 __raw_writel(value, vou_dev->base + reg); 103 __raw_writel(value, vou_dev->base + reg + 0x1000); 104 } 105 106 static void sh_vou_reg_m_write(struct sh_vou_device *vou_dev, unsigned int reg, 107 u32 value) 108 { 109 __raw_writel(value, vou_dev->base + reg + 0x2000); 110 } 111 112 static u32 sh_vou_reg_a_read(struct sh_vou_device *vou_dev, unsigned int reg) 113 { 114 return __raw_readl(vou_dev->base + reg); 115 } 116 117 static void sh_vou_reg_a_set(struct sh_vou_device *vou_dev, unsigned int reg, 118 u32 value, u32 mask) 119 { 120 u32 old = __raw_readl(vou_dev->base + reg); 121 122 value = (value & mask) | (old & ~mask); 123 __raw_writel(value, vou_dev->base + reg); 124 } 125 126 static void sh_vou_reg_b_set(struct sh_vou_device *vou_dev, unsigned int reg, 127 u32 value, u32 mask) 128 { 129 sh_vou_reg_a_set(vou_dev, reg + 0x1000, value, mask); 130 } 131 132 static void sh_vou_reg_ab_set(struct sh_vou_device *vou_dev, unsigned int reg, 133 u32 value, u32 mask) 134 { 135 sh_vou_reg_a_set(vou_dev, reg, value, mask); 136 sh_vou_reg_b_set(vou_dev, reg, value, mask); 137 } 138 139 struct sh_vou_fmt { 140 u32 pfmt; 141 unsigned char bpp; 142 unsigned char bpl; 143 unsigned char rgb; 144 unsigned char yf; 145 unsigned char pkf; 146 }; 147 148 /* Further pixel formats can be added */ 149 static struct sh_vou_fmt vou_fmt[] = { 150 { 151 .pfmt = V4L2_PIX_FMT_NV12, 152 .bpp = 12, 153 .bpl = 1, 154 .yf = 0, 155 .rgb = 0, 156 }, 157 { 158 .pfmt = V4L2_PIX_FMT_NV16, 159 .bpp = 16, 160 .bpl = 1, 161 .yf = 1, 162 .rgb = 0, 163 }, 164 { 165 .pfmt = V4L2_PIX_FMT_RGB24, 166 .bpp = 24, 167 .bpl = 3, 168 .pkf = 2, 169 .rgb = 1, 170 }, 171 { 172 .pfmt = V4L2_PIX_FMT_RGB565, 173 .bpp = 16, 174 .bpl = 2, 175 .pkf = 3, 176 .rgb = 1, 177 }, 178 { 179 .pfmt = V4L2_PIX_FMT_RGB565X, 180 .bpp = 16, 181 .bpl = 2, 182 .pkf = 3, 183 .rgb = 1, 184 }, 185 }; 186 187 static void sh_vou_schedule_next(struct sh_vou_device *vou_dev, 188 struct vb2_v4l2_buffer *vbuf) 189 { 190 dma_addr_t addr1, addr2; 191 192 addr1 = vb2_dma_contig_plane_dma_addr(&vbuf->vb2_buf, 0); 193 switch (vou_dev->pix.pixelformat) { 194 case V4L2_PIX_FMT_NV12: 195 case V4L2_PIX_FMT_NV16: 196 addr2 = addr1 + vou_dev->pix.width * vou_dev->pix.height; 197 break; 198 default: 199 addr2 = 0; 200 } 201 202 sh_vou_reg_m_write(vou_dev, VOUAD1R, addr1); 203 sh_vou_reg_m_write(vou_dev, VOUAD2R, addr2); 204 } 205 206 static void sh_vou_stream_config(struct sh_vou_device *vou_dev) 207 { 208 unsigned int row_coeff; 209 #ifdef __LITTLE_ENDIAN 210 u32 dataswap = 7; 211 #else 212 u32 dataswap = 0; 213 #endif 214 215 switch (vou_dev->pix.pixelformat) { 216 default: 217 case V4L2_PIX_FMT_NV12: 218 case V4L2_PIX_FMT_NV16: 219 row_coeff = 1; 220 break; 221 case V4L2_PIX_FMT_RGB565: 222 dataswap ^= 1; 223 fallthrough; 224 case V4L2_PIX_FMT_RGB565X: 225 row_coeff = 2; 226 break; 227 case V4L2_PIX_FMT_RGB24: 228 row_coeff = 3; 229 break; 230 } 231 232 sh_vou_reg_a_write(vou_dev, VOUSWR, dataswap); 233 sh_vou_reg_ab_write(vou_dev, VOUAIR, vou_dev->pix.width * row_coeff); 234 } 235 236 /* Locking: caller holds fop_lock mutex */ 237 static int sh_vou_queue_setup(struct vb2_queue *vq, 238 unsigned int *nbuffers, unsigned int *nplanes, 239 unsigned int sizes[], struct device *alloc_devs[]) 240 { 241 struct sh_vou_device *vou_dev = vb2_get_drv_priv(vq); 242 struct v4l2_pix_format *pix = &vou_dev->pix; 243 int bytes_per_line = vou_fmt[vou_dev->pix_idx].bpp * pix->width / 8; 244 245 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__); 246 247 if (*nplanes) 248 return sizes[0] < pix->height * bytes_per_line ? -EINVAL : 0; 249 *nplanes = 1; 250 sizes[0] = pix->height * bytes_per_line; 251 return 0; 252 } 253 254 static int sh_vou_buf_prepare(struct vb2_buffer *vb) 255 { 256 struct sh_vou_device *vou_dev = vb2_get_drv_priv(vb->vb2_queue); 257 struct v4l2_pix_format *pix = &vou_dev->pix; 258 unsigned bytes_per_line = vou_fmt[vou_dev->pix_idx].bpp * pix->width / 8; 259 unsigned size = pix->height * bytes_per_line; 260 261 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__); 262 263 if (vb2_plane_size(vb, 0) < size) { 264 /* User buffer too small */ 265 dev_warn(vou_dev->v4l2_dev.dev, "buffer too small (%lu < %u)\n", 266 vb2_plane_size(vb, 0), size); 267 return -EINVAL; 268 } 269 270 vb2_set_plane_payload(vb, 0, size); 271 return 0; 272 } 273 274 /* Locking: caller holds fop_lock mutex and vq->irqlock spinlock */ 275 static void sh_vou_buf_queue(struct vb2_buffer *vb) 276 { 277 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 278 struct sh_vou_device *vou_dev = vb2_get_drv_priv(vb->vb2_queue); 279 struct sh_vou_buffer *shbuf = to_sh_vou_buffer(vbuf); 280 unsigned long flags; 281 282 spin_lock_irqsave(&vou_dev->lock, flags); 283 list_add_tail(&shbuf->list, &vou_dev->buf_list); 284 spin_unlock_irqrestore(&vou_dev->lock, flags); 285 } 286 287 static int sh_vou_start_streaming(struct vb2_queue *vq, unsigned int count) 288 { 289 struct sh_vou_device *vou_dev = vb2_get_drv_priv(vq); 290 struct sh_vou_buffer *buf, *node; 291 int ret; 292 293 vou_dev->sequence = 0; 294 ret = v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, 295 video, s_stream, 1); 296 if (ret < 0 && ret != -ENOIOCTLCMD) { 297 list_for_each_entry_safe(buf, node, &vou_dev->buf_list, list) { 298 vb2_buffer_done(&buf->vb.vb2_buf, 299 VB2_BUF_STATE_QUEUED); 300 list_del(&buf->list); 301 } 302 vou_dev->active = NULL; 303 return ret; 304 } 305 306 buf = list_entry(vou_dev->buf_list.next, struct sh_vou_buffer, list); 307 308 vou_dev->active = buf; 309 310 /* Start from side A: we use mirror addresses, so, set B */ 311 sh_vou_reg_a_write(vou_dev, VOURPR, 1); 312 dev_dbg(vou_dev->v4l2_dev.dev, "%s: first buffer status 0x%x\n", 313 __func__, sh_vou_reg_a_read(vou_dev, VOUSTR)); 314 sh_vou_schedule_next(vou_dev, &buf->vb); 315 316 buf = list_entry(buf->list.next, struct sh_vou_buffer, list); 317 318 /* Second buffer - initialise register side B */ 319 sh_vou_reg_a_write(vou_dev, VOURPR, 0); 320 sh_vou_schedule_next(vou_dev, &buf->vb); 321 322 /* Register side switching with frame VSYNC */ 323 sh_vou_reg_a_write(vou_dev, VOURCR, 5); 324 325 sh_vou_stream_config(vou_dev); 326 /* Enable End-of-Frame (VSYNC) interrupts */ 327 sh_vou_reg_a_write(vou_dev, VOUIR, 0x10004); 328 329 /* Two buffers on the queue - activate the hardware */ 330 vou_dev->status = SH_VOU_RUNNING; 331 sh_vou_reg_a_write(vou_dev, VOUER, 0x107); 332 return 0; 333 } 334 335 static void sh_vou_stop_streaming(struct vb2_queue *vq) 336 { 337 struct sh_vou_device *vou_dev = vb2_get_drv_priv(vq); 338 struct sh_vou_buffer *buf, *node; 339 unsigned long flags; 340 341 v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, 342 video, s_stream, 0); 343 /* disable output */ 344 sh_vou_reg_a_set(vou_dev, VOUER, 0, 1); 345 /* ...but the current frame will complete */ 346 sh_vou_reg_a_set(vou_dev, VOUIR, 0, 0x30000); 347 msleep(50); 348 spin_lock_irqsave(&vou_dev->lock, flags); 349 list_for_each_entry_safe(buf, node, &vou_dev->buf_list, list) { 350 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR); 351 list_del(&buf->list); 352 } 353 vou_dev->active = NULL; 354 spin_unlock_irqrestore(&vou_dev->lock, flags); 355 } 356 357 static const struct vb2_ops sh_vou_qops = { 358 .queue_setup = sh_vou_queue_setup, 359 .buf_prepare = sh_vou_buf_prepare, 360 .buf_queue = sh_vou_buf_queue, 361 .start_streaming = sh_vou_start_streaming, 362 .stop_streaming = sh_vou_stop_streaming, 363 }; 364 365 /* Video IOCTLs */ 366 static int sh_vou_querycap(struct file *file, void *priv, 367 struct v4l2_capability *cap) 368 { 369 struct sh_vou_device *vou_dev = video_drvdata(file); 370 371 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__); 372 373 strscpy(cap->card, "SuperH VOU", sizeof(cap->card)); 374 strscpy(cap->driver, "sh-vou", sizeof(cap->driver)); 375 strscpy(cap->bus_info, "platform:sh-vou", sizeof(cap->bus_info)); 376 return 0; 377 } 378 379 /* Enumerate formats, that the device can accept from the user */ 380 static int sh_vou_enum_fmt_vid_out(struct file *file, void *priv, 381 struct v4l2_fmtdesc *fmt) 382 { 383 struct sh_vou_device *vou_dev = video_drvdata(file); 384 385 if (fmt->index >= ARRAY_SIZE(vou_fmt)) 386 return -EINVAL; 387 388 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__); 389 390 fmt->pixelformat = vou_fmt[fmt->index].pfmt; 391 392 return 0; 393 } 394 395 static int sh_vou_g_fmt_vid_out(struct file *file, void *priv, 396 struct v4l2_format *fmt) 397 { 398 struct sh_vou_device *vou_dev = video_drvdata(file); 399 400 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__); 401 402 fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT; 403 fmt->fmt.pix = vou_dev->pix; 404 405 return 0; 406 } 407 408 static const unsigned char vou_scale_h_num[] = {1, 9, 2, 9, 4}; 409 static const unsigned char vou_scale_h_den[] = {1, 8, 1, 4, 1}; 410 static const unsigned char vou_scale_h_fld[] = {0, 2, 1, 3}; 411 static const unsigned char vou_scale_v_num[] = {1, 2, 4}; 412 static const unsigned char vou_scale_v_den[] = {1, 1, 1}; 413 static const unsigned char vou_scale_v_fld[] = {0, 1}; 414 415 static void sh_vou_configure_geometry(struct sh_vou_device *vou_dev, 416 int pix_idx, int w_idx, int h_idx) 417 { 418 struct sh_vou_fmt *fmt = vou_fmt + pix_idx; 419 unsigned int black_left, black_top, width_max, 420 frame_in_height, frame_out_height, frame_out_top; 421 struct v4l2_rect *rect = &vou_dev->rect; 422 struct v4l2_pix_format *pix = &vou_dev->pix; 423 u32 vouvcr = 0, dsr_h, dsr_v; 424 425 if (vou_dev->std & V4L2_STD_525_60) { 426 width_max = 858; 427 /* height_max = 262; */ 428 } else { 429 width_max = 864; 430 /* height_max = 312; */ 431 } 432 433 frame_in_height = pix->height / 2; 434 frame_out_height = rect->height / 2; 435 frame_out_top = rect->top / 2; 436 437 /* 438 * Cropping scheme: max useful image is 720x480, and the total video 439 * area is 858x525 (NTSC) or 864x625 (PAL). AK8813 / 8814 starts 440 * sampling data beginning with fixed 276th (NTSC) / 288th (PAL) clock, 441 * of which the first 33 / 25 clocks HSYNC must be held active. This 442 * has to be configured in CR[HW]. 1 pixel equals 2 clock periods. 443 * This gives CR[HW] = 16 / 12, VPR[HVP] = 138 / 144, which gives 444 * exactly 858 - 138 = 864 - 144 = 720! We call the out-of-display area, 445 * beyond DSR, specified on the left and top by the VPR register "black 446 * pixels" and out-of-image area (DPR) "background pixels." We fix VPR 447 * at 138 / 144 : 20, because that's the HSYNC timing, that our first 448 * client requires, and that's exactly what leaves us 720 pixels for the 449 * image; we leave VPR[VVP] at default 20 for now, because the client 450 * doesn't seem to have any special requirements for it. Otherwise we 451 * could also set it to max - 240 = 22 / 72. Thus VPR depends only on 452 * the selected standard, and DPR and DSR are selected according to 453 * cropping. Q: how does the client detect the first valid line? Does 454 * HSYNC stay inactive during invalid (black) lines? 455 */ 456 black_left = width_max - VOU_MAX_IMAGE_WIDTH; 457 black_top = 20; 458 459 dsr_h = rect->width + rect->left; 460 dsr_v = frame_out_height + frame_out_top; 461 462 dev_dbg(vou_dev->v4l2_dev.dev, 463 "image %ux%u, black %u:%u, offset %u:%u, display %ux%u\n", 464 pix->width, frame_in_height, black_left, black_top, 465 rect->left, frame_out_top, dsr_h, dsr_v); 466 467 /* VOUISR height - half of a frame height in frame mode */ 468 sh_vou_reg_ab_write(vou_dev, VOUISR, (pix->width << 16) | frame_in_height); 469 sh_vou_reg_ab_write(vou_dev, VOUVPR, (black_left << 16) | black_top); 470 sh_vou_reg_ab_write(vou_dev, VOUDPR, (rect->left << 16) | frame_out_top); 471 sh_vou_reg_ab_write(vou_dev, VOUDSR, (dsr_h << 16) | dsr_v); 472 473 /* 474 * if necessary, we could set VOUHIR to 475 * max(black_left + dsr_h, width_max) here 476 */ 477 478 if (w_idx) 479 vouvcr |= (1 << 15) | (vou_scale_h_fld[w_idx - 1] << 4); 480 if (h_idx) 481 vouvcr |= (1 << 14) | vou_scale_v_fld[h_idx - 1]; 482 483 dev_dbg(vou_dev->v4l2_dev.dev, "0x%08x: scaling 0x%x\n", 484 fmt->pfmt, vouvcr); 485 486 /* To produce a colour bar for testing set bit 23 of VOUVCR */ 487 sh_vou_reg_ab_write(vou_dev, VOUVCR, vouvcr); 488 sh_vou_reg_ab_write(vou_dev, VOUDFR, 489 fmt->pkf | (fmt->yf << 8) | (fmt->rgb << 16)); 490 } 491 492 struct sh_vou_geometry { 493 struct v4l2_rect output; 494 unsigned int in_width; 495 unsigned int in_height; 496 int scale_idx_h; 497 int scale_idx_v; 498 }; 499 500 /* 501 * Find input geometry, that we can use to produce output, closest to the 502 * requested rectangle, using VOU scaling 503 */ 504 static void vou_adjust_input(struct sh_vou_geometry *geo, v4l2_std_id std) 505 { 506 /* The compiler cannot know, that best and idx will indeed be set */ 507 unsigned int best_err = UINT_MAX, best = 0, img_height_max; 508 int i, idx = 0; 509 510 if (std & V4L2_STD_525_60) 511 img_height_max = 480; 512 else 513 img_height_max = 576; 514 515 /* Image width must be a multiple of 4 */ 516 v4l_bound_align_image(&geo->in_width, 517 VOU_MIN_IMAGE_WIDTH, VOU_MAX_IMAGE_WIDTH, 2, 518 &geo->in_height, 519 VOU_MIN_IMAGE_HEIGHT, img_height_max, 1, 0); 520 521 /* Select scales to come as close as possible to the output image */ 522 for (i = ARRAY_SIZE(vou_scale_h_num) - 1; i >= 0; i--) { 523 unsigned int err; 524 unsigned int found = geo->output.width * vou_scale_h_den[i] / 525 vou_scale_h_num[i]; 526 527 if (found > VOU_MAX_IMAGE_WIDTH) 528 /* scales increase */ 529 break; 530 531 err = abs(found - geo->in_width); 532 if (err < best_err) { 533 best_err = err; 534 idx = i; 535 best = found; 536 } 537 if (!err) 538 break; 539 } 540 541 geo->in_width = best; 542 geo->scale_idx_h = idx; 543 544 best_err = UINT_MAX; 545 546 /* This loop can be replaced with one division */ 547 for (i = ARRAY_SIZE(vou_scale_v_num) - 1; i >= 0; i--) { 548 unsigned int err; 549 unsigned int found = geo->output.height * vou_scale_v_den[i] / 550 vou_scale_v_num[i]; 551 552 if (found > img_height_max) 553 /* scales increase */ 554 break; 555 556 err = abs(found - geo->in_height); 557 if (err < best_err) { 558 best_err = err; 559 idx = i; 560 best = found; 561 } 562 if (!err) 563 break; 564 } 565 566 geo->in_height = best; 567 geo->scale_idx_v = idx; 568 } 569 570 /* 571 * Find output geometry, that we can produce, using VOU scaling, closest to 572 * the requested rectangle 573 */ 574 static void vou_adjust_output(struct sh_vou_geometry *geo, v4l2_std_id std) 575 { 576 unsigned int best_err = UINT_MAX, best = geo->in_width, 577 width_max, height_max, img_height_max; 578 int i, idx_h = 0, idx_v = 0; 579 580 if (std & V4L2_STD_525_60) { 581 width_max = 858; 582 height_max = 262 * 2; 583 img_height_max = 480; 584 } else { 585 width_max = 864; 586 height_max = 312 * 2; 587 img_height_max = 576; 588 } 589 590 /* Select scales to come as close as possible to the output image */ 591 for (i = 0; i < ARRAY_SIZE(vou_scale_h_num); i++) { 592 unsigned int err; 593 unsigned int found = geo->in_width * vou_scale_h_num[i] / 594 vou_scale_h_den[i]; 595 596 if (found > VOU_MAX_IMAGE_WIDTH) 597 /* scales increase */ 598 break; 599 600 err = abs(found - geo->output.width); 601 if (err < best_err) { 602 best_err = err; 603 idx_h = i; 604 best = found; 605 } 606 if (!err) 607 break; 608 } 609 610 geo->output.width = best; 611 geo->scale_idx_h = idx_h; 612 if (geo->output.left + best > width_max) 613 geo->output.left = width_max - best; 614 615 pr_debug("%s(): W %u * %u/%u = %u\n", __func__, geo->in_width, 616 vou_scale_h_num[idx_h], vou_scale_h_den[idx_h], best); 617 618 best_err = UINT_MAX; 619 620 /* This loop can be replaced with one division */ 621 for (i = 0; i < ARRAY_SIZE(vou_scale_v_num); i++) { 622 unsigned int err; 623 unsigned int found = geo->in_height * vou_scale_v_num[i] / 624 vou_scale_v_den[i]; 625 626 if (found > img_height_max) 627 /* scales increase */ 628 break; 629 630 err = abs(found - geo->output.height); 631 if (err < best_err) { 632 best_err = err; 633 idx_v = i; 634 best = found; 635 } 636 if (!err) 637 break; 638 } 639 640 geo->output.height = best; 641 geo->scale_idx_v = idx_v; 642 if (geo->output.top + best > height_max) 643 geo->output.top = height_max - best; 644 645 pr_debug("%s(): H %u * %u/%u = %u\n", __func__, geo->in_height, 646 vou_scale_v_num[idx_v], vou_scale_v_den[idx_v], best); 647 } 648 649 static int sh_vou_try_fmt_vid_out(struct file *file, void *priv, 650 struct v4l2_format *fmt) 651 { 652 struct sh_vou_device *vou_dev = video_drvdata(file); 653 struct v4l2_pix_format *pix = &fmt->fmt.pix; 654 unsigned int img_height_max; 655 int pix_idx; 656 657 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__); 658 659 pix->field = V4L2_FIELD_INTERLACED; 660 pix->colorspace = V4L2_COLORSPACE_SMPTE170M; 661 pix->ycbcr_enc = pix->quantization = 0; 662 663 for (pix_idx = 0; pix_idx < ARRAY_SIZE(vou_fmt); pix_idx++) 664 if (vou_fmt[pix_idx].pfmt == pix->pixelformat) 665 break; 666 667 if (pix_idx == ARRAY_SIZE(vou_fmt)) 668 return -EINVAL; 669 670 if (vou_dev->std & V4L2_STD_525_60) 671 img_height_max = 480; 672 else 673 img_height_max = 576; 674 675 v4l_bound_align_image(&pix->width, 676 VOU_MIN_IMAGE_WIDTH, VOU_MAX_IMAGE_WIDTH, 2, 677 &pix->height, 678 VOU_MIN_IMAGE_HEIGHT, img_height_max, 1, 0); 679 pix->bytesperline = pix->width * vou_fmt[pix_idx].bpl; 680 pix->sizeimage = pix->height * ((pix->width * vou_fmt[pix_idx].bpp) >> 3); 681 682 return 0; 683 } 684 685 static int sh_vou_set_fmt_vid_out(struct sh_vou_device *vou_dev, 686 struct v4l2_pix_format *pix) 687 { 688 unsigned int img_height_max; 689 struct sh_vou_geometry geo; 690 struct v4l2_subdev_format format = { 691 .which = V4L2_SUBDEV_FORMAT_ACTIVE, 692 /* Revisit: is this the correct code? */ 693 .format.code = MEDIA_BUS_FMT_YUYV8_2X8, 694 .format.field = V4L2_FIELD_INTERLACED, 695 .format.colorspace = V4L2_COLORSPACE_SMPTE170M, 696 }; 697 struct v4l2_mbus_framefmt *mbfmt = &format.format; 698 int pix_idx; 699 int ret; 700 701 if (vb2_is_busy(&vou_dev->queue)) 702 return -EBUSY; 703 704 for (pix_idx = 0; pix_idx < ARRAY_SIZE(vou_fmt); pix_idx++) 705 if (vou_fmt[pix_idx].pfmt == pix->pixelformat) 706 break; 707 708 geo.in_width = pix->width; 709 geo.in_height = pix->height; 710 geo.output = vou_dev->rect; 711 712 vou_adjust_output(&geo, vou_dev->std); 713 714 mbfmt->width = geo.output.width; 715 mbfmt->height = geo.output.height; 716 ret = v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, pad, 717 set_fmt, NULL, &format); 718 /* Must be implemented, so, don't check for -ENOIOCTLCMD */ 719 if (ret < 0) 720 return ret; 721 722 dev_dbg(vou_dev->v4l2_dev.dev, "%s(): %ux%u -> %ux%u\n", __func__, 723 geo.output.width, geo.output.height, mbfmt->width, mbfmt->height); 724 725 if (vou_dev->std & V4L2_STD_525_60) 726 img_height_max = 480; 727 else 728 img_height_max = 576; 729 730 /* Sanity checks */ 731 if ((unsigned)mbfmt->width > VOU_MAX_IMAGE_WIDTH || 732 (unsigned)mbfmt->height > img_height_max || 733 mbfmt->code != MEDIA_BUS_FMT_YUYV8_2X8) 734 return -EIO; 735 736 if (mbfmt->width != geo.output.width || 737 mbfmt->height != geo.output.height) { 738 geo.output.width = mbfmt->width; 739 geo.output.height = mbfmt->height; 740 741 vou_adjust_input(&geo, vou_dev->std); 742 } 743 744 /* We tried to preserve output rectangle, but it could have changed */ 745 vou_dev->rect = geo.output; 746 pix->width = geo.in_width; 747 pix->height = geo.in_height; 748 749 dev_dbg(vou_dev->v4l2_dev.dev, "%s(): %ux%u\n", __func__, 750 pix->width, pix->height); 751 752 vou_dev->pix_idx = pix_idx; 753 754 vou_dev->pix = *pix; 755 756 sh_vou_configure_geometry(vou_dev, pix_idx, 757 geo.scale_idx_h, geo.scale_idx_v); 758 759 return 0; 760 } 761 762 static int sh_vou_s_fmt_vid_out(struct file *file, void *priv, 763 struct v4l2_format *fmt) 764 { 765 struct sh_vou_device *vou_dev = video_drvdata(file); 766 int ret = sh_vou_try_fmt_vid_out(file, priv, fmt); 767 768 if (ret) 769 return ret; 770 return sh_vou_set_fmt_vid_out(vou_dev, &fmt->fmt.pix); 771 } 772 773 static int sh_vou_enum_output(struct file *file, void *fh, 774 struct v4l2_output *a) 775 { 776 struct sh_vou_device *vou_dev = video_drvdata(file); 777 778 if (a->index) 779 return -EINVAL; 780 strscpy(a->name, "Video Out", sizeof(a->name)); 781 a->type = V4L2_OUTPUT_TYPE_ANALOG; 782 a->std = vou_dev->vdev.tvnorms; 783 return 0; 784 } 785 786 static int sh_vou_g_output(struct file *file, void *fh, unsigned int *i) 787 { 788 *i = 0; 789 return 0; 790 } 791 792 static int sh_vou_s_output(struct file *file, void *fh, unsigned int i) 793 { 794 return i ? -EINVAL : 0; 795 } 796 797 static u32 sh_vou_ntsc_mode(enum sh_vou_bus_fmt bus_fmt) 798 { 799 switch (bus_fmt) { 800 default: 801 pr_warn("%s(): Invalid bus-format code %d, using default 8-bit\n", 802 __func__, bus_fmt); 803 fallthrough; 804 case SH_VOU_BUS_8BIT: 805 return 1; 806 case SH_VOU_BUS_16BIT: 807 return 0; 808 case SH_VOU_BUS_BT656: 809 return 3; 810 } 811 } 812 813 static int sh_vou_s_std(struct file *file, void *priv, v4l2_std_id std_id) 814 { 815 struct sh_vou_device *vou_dev = video_drvdata(file); 816 int ret; 817 818 dev_dbg(vou_dev->v4l2_dev.dev, "%s(): 0x%llx\n", __func__, std_id); 819 820 if (std_id == vou_dev->std) 821 return 0; 822 823 if (vb2_is_busy(&vou_dev->queue)) 824 return -EBUSY; 825 826 ret = v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, video, 827 s_std_output, std_id); 828 /* Shall we continue, if the subdev doesn't support .s_std_output()? */ 829 if (ret < 0 && ret != -ENOIOCTLCMD) 830 return ret; 831 832 vou_dev->rect.top = vou_dev->rect.left = 0; 833 vou_dev->rect.width = VOU_MAX_IMAGE_WIDTH; 834 if (std_id & V4L2_STD_525_60) { 835 sh_vou_reg_ab_set(vou_dev, VOUCR, 836 sh_vou_ntsc_mode(vou_dev->pdata->bus_fmt) << 29, 7 << 29); 837 vou_dev->rect.height = 480; 838 } else { 839 sh_vou_reg_ab_set(vou_dev, VOUCR, 5 << 29, 7 << 29); 840 vou_dev->rect.height = 576; 841 } 842 843 vou_dev->pix.width = vou_dev->rect.width; 844 vou_dev->pix.height = vou_dev->rect.height; 845 vou_dev->pix.bytesperline = 846 vou_dev->pix.width * vou_fmt[vou_dev->pix_idx].bpl; 847 vou_dev->pix.sizeimage = vou_dev->pix.height * 848 ((vou_dev->pix.width * vou_fmt[vou_dev->pix_idx].bpp) >> 3); 849 vou_dev->std = std_id; 850 sh_vou_set_fmt_vid_out(vou_dev, &vou_dev->pix); 851 852 return 0; 853 } 854 855 static int sh_vou_g_std(struct file *file, void *priv, v4l2_std_id *std) 856 { 857 struct sh_vou_device *vou_dev = video_drvdata(file); 858 859 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__); 860 861 *std = vou_dev->std; 862 863 return 0; 864 } 865 866 static int sh_vou_log_status(struct file *file, void *priv) 867 { 868 struct sh_vou_device *vou_dev = video_drvdata(file); 869 870 pr_info("VOUER: 0x%08x\n", sh_vou_reg_a_read(vou_dev, VOUER)); 871 pr_info("VOUCR: 0x%08x\n", sh_vou_reg_a_read(vou_dev, VOUCR)); 872 pr_info("VOUSTR: 0x%08x\n", sh_vou_reg_a_read(vou_dev, VOUSTR)); 873 pr_info("VOUVCR: 0x%08x\n", sh_vou_reg_a_read(vou_dev, VOUVCR)); 874 pr_info("VOUISR: 0x%08x\n", sh_vou_reg_a_read(vou_dev, VOUISR)); 875 pr_info("VOUBCR: 0x%08x\n", sh_vou_reg_a_read(vou_dev, VOUBCR)); 876 pr_info("VOUDPR: 0x%08x\n", sh_vou_reg_a_read(vou_dev, VOUDPR)); 877 pr_info("VOUDSR: 0x%08x\n", sh_vou_reg_a_read(vou_dev, VOUDSR)); 878 pr_info("VOUVPR: 0x%08x\n", sh_vou_reg_a_read(vou_dev, VOUVPR)); 879 pr_info("VOUIR: 0x%08x\n", sh_vou_reg_a_read(vou_dev, VOUIR)); 880 pr_info("VOUSRR: 0x%08x\n", sh_vou_reg_a_read(vou_dev, VOUSRR)); 881 pr_info("VOUMSR: 0x%08x\n", sh_vou_reg_a_read(vou_dev, VOUMSR)); 882 pr_info("VOUHIR: 0x%08x\n", sh_vou_reg_a_read(vou_dev, VOUHIR)); 883 pr_info("VOUDFR: 0x%08x\n", sh_vou_reg_a_read(vou_dev, VOUDFR)); 884 pr_info("VOUAD1R: 0x%08x\n", sh_vou_reg_a_read(vou_dev, VOUAD1R)); 885 pr_info("VOUAD2R: 0x%08x\n", sh_vou_reg_a_read(vou_dev, VOUAD2R)); 886 pr_info("VOUAIR: 0x%08x\n", sh_vou_reg_a_read(vou_dev, VOUAIR)); 887 pr_info("VOUSWR: 0x%08x\n", sh_vou_reg_a_read(vou_dev, VOUSWR)); 888 pr_info("VOURCR: 0x%08x\n", sh_vou_reg_a_read(vou_dev, VOURCR)); 889 pr_info("VOURPR: 0x%08x\n", sh_vou_reg_a_read(vou_dev, VOURPR)); 890 return 0; 891 } 892 893 static int sh_vou_g_selection(struct file *file, void *fh, 894 struct v4l2_selection *sel) 895 { 896 struct sh_vou_device *vou_dev = video_drvdata(file); 897 898 if (sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) 899 return -EINVAL; 900 switch (sel->target) { 901 case V4L2_SEL_TGT_COMPOSE: 902 sel->r = vou_dev->rect; 903 break; 904 case V4L2_SEL_TGT_COMPOSE_DEFAULT: 905 case V4L2_SEL_TGT_COMPOSE_BOUNDS: 906 sel->r.left = 0; 907 sel->r.top = 0; 908 sel->r.width = VOU_MAX_IMAGE_WIDTH; 909 if (vou_dev->std & V4L2_STD_525_60) 910 sel->r.height = 480; 911 else 912 sel->r.height = 576; 913 break; 914 default: 915 return -EINVAL; 916 } 917 return 0; 918 } 919 920 /* Assume a dull encoder, do all the work ourselves. */ 921 static int sh_vou_s_selection(struct file *file, void *fh, 922 struct v4l2_selection *sel) 923 { 924 struct v4l2_rect *rect = &sel->r; 925 struct sh_vou_device *vou_dev = video_drvdata(file); 926 struct v4l2_subdev_selection sd_sel = { 927 .which = V4L2_SUBDEV_FORMAT_ACTIVE, 928 .target = V4L2_SEL_TGT_COMPOSE, 929 }; 930 struct v4l2_pix_format *pix = &vou_dev->pix; 931 struct sh_vou_geometry geo; 932 struct v4l2_subdev_format format = { 933 .which = V4L2_SUBDEV_FORMAT_ACTIVE, 934 /* Revisit: is this the correct code? */ 935 .format.code = MEDIA_BUS_FMT_YUYV8_2X8, 936 .format.field = V4L2_FIELD_INTERLACED, 937 .format.colorspace = V4L2_COLORSPACE_SMPTE170M, 938 }; 939 unsigned int img_height_max; 940 int ret; 941 942 if (sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT || 943 sel->target != V4L2_SEL_TGT_COMPOSE) 944 return -EINVAL; 945 946 if (vb2_is_busy(&vou_dev->queue)) 947 return -EBUSY; 948 949 if (vou_dev->std & V4L2_STD_525_60) 950 img_height_max = 480; 951 else 952 img_height_max = 576; 953 954 v4l_bound_align_image(&rect->width, 955 VOU_MIN_IMAGE_WIDTH, VOU_MAX_IMAGE_WIDTH, 1, 956 &rect->height, 957 VOU_MIN_IMAGE_HEIGHT, img_height_max, 1, 0); 958 959 if (rect->width + rect->left > VOU_MAX_IMAGE_WIDTH) 960 rect->left = VOU_MAX_IMAGE_WIDTH - rect->width; 961 962 if (rect->height + rect->top > img_height_max) 963 rect->top = img_height_max - rect->height; 964 965 geo.output = *rect; 966 geo.in_width = pix->width; 967 geo.in_height = pix->height; 968 969 /* Configure the encoder one-to-one, position at 0, ignore errors */ 970 sd_sel.r.width = geo.output.width; 971 sd_sel.r.height = geo.output.height; 972 /* 973 * We first issue a S_SELECTION, so that the subsequent S_FMT delivers the 974 * final encoder configuration. 975 */ 976 v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, pad, 977 set_selection, NULL, &sd_sel); 978 format.format.width = geo.output.width; 979 format.format.height = geo.output.height; 980 ret = v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, pad, 981 set_fmt, NULL, &format); 982 /* Must be implemented, so, don't check for -ENOIOCTLCMD */ 983 if (ret < 0) 984 return ret; 985 986 /* Sanity checks */ 987 if ((unsigned)format.format.width > VOU_MAX_IMAGE_WIDTH || 988 (unsigned)format.format.height > img_height_max || 989 format.format.code != MEDIA_BUS_FMT_YUYV8_2X8) 990 return -EIO; 991 992 geo.output.width = format.format.width; 993 geo.output.height = format.format.height; 994 995 /* 996 * No down-scaling. According to the API, current call has precedence: 997 * https://linuxtv.org/downloads/v4l-dvb-apis/uapi/v4l/crop.html#cropping-structures 998 */ 999 vou_adjust_input(&geo, vou_dev->std); 1000 1001 /* We tried to preserve output rectangle, but it could have changed */ 1002 vou_dev->rect = geo.output; 1003 pix->width = geo.in_width; 1004 pix->height = geo.in_height; 1005 1006 sh_vou_configure_geometry(vou_dev, vou_dev->pix_idx, 1007 geo.scale_idx_h, geo.scale_idx_v); 1008 1009 return 0; 1010 } 1011 1012 static irqreturn_t sh_vou_isr(int irq, void *dev_id) 1013 { 1014 struct sh_vou_device *vou_dev = dev_id; 1015 static unsigned long j; 1016 struct sh_vou_buffer *vb; 1017 static int cnt; 1018 u32 irq_status = sh_vou_reg_a_read(vou_dev, VOUIR), masked; 1019 u32 vou_status = sh_vou_reg_a_read(vou_dev, VOUSTR); 1020 1021 if (!(irq_status & 0x300)) { 1022 if (printk_timed_ratelimit(&j, 500)) 1023 dev_warn(vou_dev->v4l2_dev.dev, "IRQ status 0x%x!\n", 1024 irq_status); 1025 return IRQ_NONE; 1026 } 1027 1028 spin_lock(&vou_dev->lock); 1029 if (!vou_dev->active || list_empty(&vou_dev->buf_list)) { 1030 if (printk_timed_ratelimit(&j, 500)) 1031 dev_warn(vou_dev->v4l2_dev.dev, 1032 "IRQ without active buffer: %x!\n", irq_status); 1033 /* Just ack: buf_release will disable further interrupts */ 1034 sh_vou_reg_a_set(vou_dev, VOUIR, 0, 0x300); 1035 spin_unlock(&vou_dev->lock); 1036 return IRQ_HANDLED; 1037 } 1038 1039 masked = ~(0x300 & irq_status) & irq_status & 0x30304; 1040 dev_dbg(vou_dev->v4l2_dev.dev, 1041 "IRQ status 0x%x -> 0x%x, VOU status 0x%x, cnt %d\n", 1042 irq_status, masked, vou_status, cnt); 1043 1044 cnt++; 1045 /* side = vou_status & 0x10000; */ 1046 1047 /* Clear only set interrupts */ 1048 sh_vou_reg_a_write(vou_dev, VOUIR, masked); 1049 1050 vb = vou_dev->active; 1051 if (list_is_singular(&vb->list)) { 1052 /* Keep cycling while no next buffer is available */ 1053 sh_vou_schedule_next(vou_dev, &vb->vb); 1054 spin_unlock(&vou_dev->lock); 1055 return IRQ_HANDLED; 1056 } 1057 1058 list_del(&vb->list); 1059 1060 vb->vb.vb2_buf.timestamp = ktime_get_ns(); 1061 vb->vb.sequence = vou_dev->sequence++; 1062 vb->vb.field = V4L2_FIELD_INTERLACED; 1063 vb2_buffer_done(&vb->vb.vb2_buf, VB2_BUF_STATE_DONE); 1064 1065 vou_dev->active = list_entry(vou_dev->buf_list.next, 1066 struct sh_vou_buffer, list); 1067 1068 if (list_is_singular(&vou_dev->buf_list)) { 1069 /* Keep cycling while no next buffer is available */ 1070 sh_vou_schedule_next(vou_dev, &vou_dev->active->vb); 1071 } else { 1072 struct sh_vou_buffer *new = list_entry(vou_dev->active->list.next, 1073 struct sh_vou_buffer, list); 1074 sh_vou_schedule_next(vou_dev, &new->vb); 1075 } 1076 1077 spin_unlock(&vou_dev->lock); 1078 1079 return IRQ_HANDLED; 1080 } 1081 1082 static int sh_vou_hw_init(struct sh_vou_device *vou_dev) 1083 { 1084 struct sh_vou_pdata *pdata = vou_dev->pdata; 1085 u32 voucr = sh_vou_ntsc_mode(pdata->bus_fmt) << 29; 1086 int i = 100; 1087 1088 /* Disable all IRQs */ 1089 sh_vou_reg_a_write(vou_dev, VOUIR, 0); 1090 1091 /* Reset VOU interfaces - registers unaffected */ 1092 sh_vou_reg_a_write(vou_dev, VOUSRR, 0x101); 1093 while (--i && (sh_vou_reg_a_read(vou_dev, VOUSRR) & 0x101)) 1094 udelay(1); 1095 1096 if (!i) 1097 return -ETIMEDOUT; 1098 1099 dev_dbg(vou_dev->v4l2_dev.dev, "Reset took %dus\n", 100 - i); 1100 1101 if (pdata->flags & SH_VOU_PCLK_FALLING) 1102 voucr |= 1 << 28; 1103 if (pdata->flags & SH_VOU_HSYNC_LOW) 1104 voucr |= 1 << 27; 1105 if (pdata->flags & SH_VOU_VSYNC_LOW) 1106 voucr |= 1 << 26; 1107 sh_vou_reg_ab_set(vou_dev, VOUCR, voucr, 0xfc000000); 1108 1109 /* Manual register side switching at first */ 1110 sh_vou_reg_a_write(vou_dev, VOURCR, 4); 1111 /* Default - fixed HSYNC length, can be made configurable is required */ 1112 sh_vou_reg_ab_write(vou_dev, VOUMSR, 0x800000); 1113 1114 sh_vou_set_fmt_vid_out(vou_dev, &vou_dev->pix); 1115 1116 return 0; 1117 } 1118 1119 /* File operations */ 1120 static int sh_vou_open(struct file *file) 1121 { 1122 struct sh_vou_device *vou_dev = video_drvdata(file); 1123 int err; 1124 1125 if (mutex_lock_interruptible(&vou_dev->fop_lock)) 1126 return -ERESTARTSYS; 1127 1128 err = v4l2_fh_open(file); 1129 if (err) 1130 goto done_open; 1131 if (v4l2_fh_is_singular_file(file) && 1132 vou_dev->status == SH_VOU_INITIALISING) { 1133 /* First open */ 1134 err = pm_runtime_resume_and_get(vou_dev->v4l2_dev.dev); 1135 if (err < 0) { 1136 v4l2_fh_release(file); 1137 goto done_open; 1138 } 1139 err = sh_vou_hw_init(vou_dev); 1140 if (err < 0) { 1141 pm_runtime_put(vou_dev->v4l2_dev.dev); 1142 v4l2_fh_release(file); 1143 } else { 1144 vou_dev->status = SH_VOU_IDLE; 1145 } 1146 } 1147 done_open: 1148 mutex_unlock(&vou_dev->fop_lock); 1149 return err; 1150 } 1151 1152 static int sh_vou_release(struct file *file) 1153 { 1154 struct sh_vou_device *vou_dev = video_drvdata(file); 1155 bool is_last; 1156 1157 mutex_lock(&vou_dev->fop_lock); 1158 is_last = v4l2_fh_is_singular_file(file); 1159 _vb2_fop_release(file, NULL); 1160 if (is_last) { 1161 /* Last close */ 1162 vou_dev->status = SH_VOU_INITIALISING; 1163 sh_vou_reg_a_set(vou_dev, VOUER, 0, 0x101); 1164 pm_runtime_put(vou_dev->v4l2_dev.dev); 1165 } 1166 mutex_unlock(&vou_dev->fop_lock); 1167 return 0; 1168 } 1169 1170 /* sh_vou display ioctl operations */ 1171 static const struct v4l2_ioctl_ops sh_vou_ioctl_ops = { 1172 .vidioc_querycap = sh_vou_querycap, 1173 .vidioc_enum_fmt_vid_out = sh_vou_enum_fmt_vid_out, 1174 .vidioc_g_fmt_vid_out = sh_vou_g_fmt_vid_out, 1175 .vidioc_s_fmt_vid_out = sh_vou_s_fmt_vid_out, 1176 .vidioc_try_fmt_vid_out = sh_vou_try_fmt_vid_out, 1177 .vidioc_reqbufs = vb2_ioctl_reqbufs, 1178 .vidioc_create_bufs = vb2_ioctl_create_bufs, 1179 .vidioc_querybuf = vb2_ioctl_querybuf, 1180 .vidioc_qbuf = vb2_ioctl_qbuf, 1181 .vidioc_dqbuf = vb2_ioctl_dqbuf, 1182 .vidioc_prepare_buf = vb2_ioctl_prepare_buf, 1183 .vidioc_streamon = vb2_ioctl_streamon, 1184 .vidioc_streamoff = vb2_ioctl_streamoff, 1185 .vidioc_expbuf = vb2_ioctl_expbuf, 1186 .vidioc_g_output = sh_vou_g_output, 1187 .vidioc_s_output = sh_vou_s_output, 1188 .vidioc_enum_output = sh_vou_enum_output, 1189 .vidioc_s_std = sh_vou_s_std, 1190 .vidioc_g_std = sh_vou_g_std, 1191 .vidioc_g_selection = sh_vou_g_selection, 1192 .vidioc_s_selection = sh_vou_s_selection, 1193 .vidioc_log_status = sh_vou_log_status, 1194 }; 1195 1196 static const struct v4l2_file_operations sh_vou_fops = { 1197 .owner = THIS_MODULE, 1198 .open = sh_vou_open, 1199 .release = sh_vou_release, 1200 .unlocked_ioctl = video_ioctl2, 1201 .mmap = vb2_fop_mmap, 1202 .poll = vb2_fop_poll, 1203 .write = vb2_fop_write, 1204 }; 1205 1206 static const struct video_device sh_vou_video_template = { 1207 .name = "sh_vou", 1208 .fops = &sh_vou_fops, 1209 .ioctl_ops = &sh_vou_ioctl_ops, 1210 .tvnorms = V4L2_STD_525_60, /* PAL only supported in 8-bit non-bt656 mode */ 1211 .vfl_dir = VFL_DIR_TX, 1212 .device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_READWRITE | 1213 V4L2_CAP_STREAMING, 1214 }; 1215 1216 static int sh_vou_probe(struct platform_device *pdev) 1217 { 1218 struct sh_vou_pdata *vou_pdata = pdev->dev.platform_data; 1219 struct v4l2_rect *rect; 1220 struct v4l2_pix_format *pix; 1221 struct i2c_adapter *i2c_adap; 1222 struct video_device *vdev; 1223 struct sh_vou_device *vou_dev; 1224 struct v4l2_subdev *subdev; 1225 struct vb2_queue *q; 1226 int irq, ret; 1227 1228 if (!vou_pdata) { 1229 dev_err(&pdev->dev, "Insufficient VOU platform information.\n"); 1230 return -ENODEV; 1231 } 1232 1233 irq = platform_get_irq(pdev, 0); 1234 if (irq < 0) 1235 return irq; 1236 1237 vou_dev = devm_kzalloc(&pdev->dev, sizeof(*vou_dev), GFP_KERNEL); 1238 if (!vou_dev) 1239 return -ENOMEM; 1240 1241 INIT_LIST_HEAD(&vou_dev->buf_list); 1242 spin_lock_init(&vou_dev->lock); 1243 mutex_init(&vou_dev->fop_lock); 1244 vou_dev->pdata = vou_pdata; 1245 vou_dev->status = SH_VOU_INITIALISING; 1246 vou_dev->pix_idx = 1; 1247 1248 rect = &vou_dev->rect; 1249 pix = &vou_dev->pix; 1250 1251 /* Fill in defaults */ 1252 vou_dev->std = V4L2_STD_NTSC_M; 1253 rect->left = 0; 1254 rect->top = 0; 1255 rect->width = VOU_MAX_IMAGE_WIDTH; 1256 rect->height = 480; 1257 pix->width = VOU_MAX_IMAGE_WIDTH; 1258 pix->height = 480; 1259 pix->pixelformat = V4L2_PIX_FMT_NV16; 1260 pix->field = V4L2_FIELD_INTERLACED; 1261 pix->bytesperline = VOU_MAX_IMAGE_WIDTH; 1262 pix->sizeimage = VOU_MAX_IMAGE_WIDTH * 2 * 480; 1263 pix->colorspace = V4L2_COLORSPACE_SMPTE170M; 1264 1265 vou_dev->base = devm_platform_ioremap_resource(pdev, 0); 1266 if (IS_ERR(vou_dev->base)) 1267 return PTR_ERR(vou_dev->base); 1268 1269 ret = devm_request_irq(&pdev->dev, irq, sh_vou_isr, 0, "vou", vou_dev); 1270 if (ret < 0) 1271 return ret; 1272 1273 ret = v4l2_device_register(&pdev->dev, &vou_dev->v4l2_dev); 1274 if (ret < 0) { 1275 dev_err(&pdev->dev, "Error registering v4l2 device\n"); 1276 return ret; 1277 } 1278 1279 vdev = &vou_dev->vdev; 1280 *vdev = sh_vou_video_template; 1281 if (vou_pdata->bus_fmt == SH_VOU_BUS_8BIT) 1282 vdev->tvnorms |= V4L2_STD_PAL; 1283 vdev->v4l2_dev = &vou_dev->v4l2_dev; 1284 vdev->release = video_device_release_empty; 1285 vdev->lock = &vou_dev->fop_lock; 1286 1287 video_set_drvdata(vdev, vou_dev); 1288 1289 /* Initialize the vb2 queue */ 1290 q = &vou_dev->queue; 1291 q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT; 1292 q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_WRITE; 1293 q->drv_priv = vou_dev; 1294 q->buf_struct_size = sizeof(struct sh_vou_buffer); 1295 q->ops = &sh_vou_qops; 1296 q->mem_ops = &vb2_dma_contig_memops; 1297 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 1298 q->min_queued_buffers = 2; 1299 q->lock = &vou_dev->fop_lock; 1300 q->dev = &pdev->dev; 1301 ret = vb2_queue_init(q); 1302 if (ret) 1303 goto ei2cgadap; 1304 1305 vdev->queue = q; 1306 INIT_LIST_HEAD(&vou_dev->buf_list); 1307 1308 pm_runtime_enable(&pdev->dev); 1309 pm_runtime_resume(&pdev->dev); 1310 1311 i2c_adap = i2c_get_adapter(vou_pdata->i2c_adap); 1312 if (!i2c_adap) { 1313 ret = -ENODEV; 1314 goto ei2cgadap; 1315 } 1316 1317 ret = sh_vou_hw_init(vou_dev); 1318 if (ret < 0) 1319 goto ereset; 1320 1321 subdev = v4l2_i2c_new_subdev_board(&vou_dev->v4l2_dev, i2c_adap, 1322 vou_pdata->board_info, NULL); 1323 if (!subdev) { 1324 ret = -ENOMEM; 1325 goto ei2cnd; 1326 } 1327 1328 ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1); 1329 if (ret < 0) 1330 goto evregdev; 1331 1332 return 0; 1333 1334 evregdev: 1335 ei2cnd: 1336 ereset: 1337 i2c_put_adapter(i2c_adap); 1338 ei2cgadap: 1339 pm_runtime_disable(&pdev->dev); 1340 v4l2_device_unregister(&vou_dev->v4l2_dev); 1341 return ret; 1342 } 1343 1344 static void sh_vou_remove(struct platform_device *pdev) 1345 { 1346 struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev); 1347 struct sh_vou_device *vou_dev = container_of(v4l2_dev, 1348 struct sh_vou_device, v4l2_dev); 1349 struct v4l2_subdev *sd = list_entry(v4l2_dev->subdevs.next, 1350 struct v4l2_subdev, list); 1351 struct i2c_client *client = v4l2_get_subdevdata(sd); 1352 1353 pm_runtime_disable(&pdev->dev); 1354 video_unregister_device(&vou_dev->vdev); 1355 i2c_put_adapter(client->adapter); 1356 v4l2_device_unregister(&vou_dev->v4l2_dev); 1357 } 1358 1359 static struct platform_driver sh_vou = { 1360 .remove = sh_vou_remove, 1361 .driver = { 1362 .name = "sh-vou", 1363 }, 1364 }; 1365 1366 module_platform_driver_probe(sh_vou, sh_vou_probe); 1367 1368 MODULE_DESCRIPTION("SuperH VOU driver"); 1369 MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>"); 1370 MODULE_LICENSE("GPL v2"); 1371 MODULE_VERSION("0.1.0"); 1372 MODULE_ALIAS("platform:sh-vou"); 1373