root/drivers/media/platform/sh_vou.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. to_sh_vou_buffer
  2. sh_vou_reg_a_write
  3. sh_vou_reg_ab_write
  4. sh_vou_reg_m_write
  5. sh_vou_reg_a_read
  6. sh_vou_reg_a_set
  7. sh_vou_reg_b_set
  8. sh_vou_reg_ab_set
  9. sh_vou_schedule_next
  10. sh_vou_stream_config
  11. sh_vou_queue_setup
  12. sh_vou_buf_prepare
  13. sh_vou_buf_queue
  14. sh_vou_start_streaming
  15. sh_vou_stop_streaming
  16. sh_vou_querycap
  17. sh_vou_enum_fmt_vid_out
  18. sh_vou_g_fmt_vid_out
  19. sh_vou_configure_geometry
  20. vou_adjust_input
  21. vou_adjust_output
  22. sh_vou_try_fmt_vid_out
  23. sh_vou_set_fmt_vid_out
  24. sh_vou_s_fmt_vid_out
  25. sh_vou_enum_output
  26. sh_vou_g_output
  27. sh_vou_s_output
  28. sh_vou_ntsc_mode
  29. sh_vou_s_std
  30. sh_vou_g_std
  31. sh_vou_log_status
  32. sh_vou_g_selection
  33. sh_vou_s_selection
  34. sh_vou_isr
  35. sh_vou_hw_init
  36. sh_vou_open
  37. sh_vou_release
  38. sh_vou_probe
  39. sh_vou_remove

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

/* [<][>][^][v][top][bottom][index][help] */