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