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