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