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