[media] media: ti-vpe: Increasing max buffer height and width
[linux-block.git] / drivers / media / platform / ti-vpe / vpe.c
CommitLineData
45719127
AT
1/*
2 * TI VPE mem2mem driver, based on the virtual v4l2-mem2mem example driver
3 *
4 * Copyright (c) 2013 Texas Instruments Inc.
5 * David Griego, <dagriego@biglakesoftware.com>
6 * Dale Farnsworth, <dale@farnsworth.org>
7 * Archit Taneja, <archit@ti.com>
8 *
9 * Copyright (c) 2009-2010 Samsung Electronics Co., Ltd.
10 * Pawel Osciak, <pawel@osciak.com>
11 * Marek Szyprowski, <m.szyprowski@samsung.com>
12 *
13 * Based on the virtual v4l2-mem2mem example device
14 *
15 * This program is free software; you can redistribute it and/or modify it
16 * under the terms of the GNU General Public License version 2 as published by
17 * the Free Software Foundation
18 */
19
20#include <linux/delay.h>
21#include <linux/dma-mapping.h>
22#include <linux/err.h>
23#include <linux/fs.h>
24#include <linux/interrupt.h>
25#include <linux/io.h>
26#include <linux/ioctl.h>
27#include <linux/module.h>
47e604c5 28#include <linux/of.h>
45719127
AT
29#include <linux/platform_device.h>
30#include <linux/pm_runtime.h>
31#include <linux/sched.h>
32#include <linux/slab.h>
33#include <linux/videodev2.h>
a51cd8f5 34#include <linux/log2.h>
f3334180 35#include <linux/sizes.h>
45719127
AT
36
37#include <media/v4l2-common.h>
38#include <media/v4l2-ctrls.h>
39#include <media/v4l2-device.h>
40#include <media/v4l2-event.h>
41#include <media/v4l2-ioctl.h>
42#include <media/v4l2-mem2mem.h>
c139990e 43#include <media/videobuf2-v4l2.h>
45719127
AT
44#include <media/videobuf2-dma-contig.h>
45
46#include "vpdma.h"
47#include "vpe_regs.h"
44687b2e 48#include "sc.h"
6948082d 49#include "csc.h"
45719127
AT
50
51#define VPE_MODULE_NAME "vpe"
52
53/* minimum and maximum frame sizes */
ce392fd7
AT
54#define MIN_W 32
55#define MIN_H 32
8028bfed
HB
56#define MAX_W 2048
57#define MAX_H 1184
45719127
AT
58
59/* required alignments */
60#define S_ALIGN 0 /* multiple of 1 */
61#define H_ALIGN 1 /* multiple of 2 */
45719127
AT
62
63/* flags that indicate a format can be used for capture/output */
64#define VPE_FMT_TYPE_CAPTURE (1 << 0)
65#define VPE_FMT_TYPE_OUTPUT (1 << 1)
66
67/* used as plane indices */
68#define VPE_MAX_PLANES 2
69#define VPE_LUMA 0
70#define VPE_CHROMA 1
71
72/* per m2m context info */
585e6f01
AT
73#define VPE_MAX_SRC_BUFS 3 /* need 3 src fields to de-interlace */
74
45719127
AT
75#define VPE_DEF_BUFS_PER_JOB 1 /* default one buffer per batch job */
76
77/*
47e604c5 78 * each VPE context can need up to 3 config descriptors, 7 input descriptors,
45719127
AT
79 * 3 output descriptors, and 10 control descriptors
80 */
81#define VPE_DESC_LIST_SIZE (10 * VPDMA_DTD_DESC_SIZE + \
82 13 * VPDMA_CFD_CTD_DESC_SIZE)
83
84#define vpe_dbg(vpedev, fmt, arg...) \
85 dev_dbg((vpedev)->v4l2_dev.dev, fmt, ##arg)
86#define vpe_err(vpedev, fmt, arg...) \
87 dev_err((vpedev)->v4l2_dev.dev, fmt, ##arg)
88
89struct vpe_us_coeffs {
90 unsigned short anchor_fid0_c0;
91 unsigned short anchor_fid0_c1;
92 unsigned short anchor_fid0_c2;
93 unsigned short anchor_fid0_c3;
94 unsigned short interp_fid0_c0;
95 unsigned short interp_fid0_c1;
96 unsigned short interp_fid0_c2;
97 unsigned short interp_fid0_c3;
98 unsigned short anchor_fid1_c0;
99 unsigned short anchor_fid1_c1;
100 unsigned short anchor_fid1_c2;
101 unsigned short anchor_fid1_c3;
102 unsigned short interp_fid1_c0;
103 unsigned short interp_fid1_c1;
104 unsigned short interp_fid1_c2;
105 unsigned short interp_fid1_c3;
106};
107
108/*
109 * Default upsampler coefficients
110 */
111static const struct vpe_us_coeffs us_coeffs[] = {
112 {
113 /* Coefficients for progressive input */
114 0x00C8, 0x0348, 0x0018, 0x3FD8, 0x3FB8, 0x0378, 0x00E8, 0x3FE8,
115 0x00C8, 0x0348, 0x0018, 0x3FD8, 0x3FB8, 0x0378, 0x00E8, 0x3FE8,
116 },
585e6f01
AT
117 {
118 /* Coefficients for Top Field Interlaced input */
119 0x0051, 0x03D5, 0x3FE3, 0x3FF7, 0x3FB5, 0x02E9, 0x018F, 0x3FD3,
120 /* Coefficients for Bottom Field Interlaced input */
121 0x016B, 0x0247, 0x00B1, 0x3F9D, 0x3FCF, 0x03DB, 0x005D, 0x3FF9,
122 },
123};
124
125/*
126 * the following registers are for configuring some of the parameters of the
127 * motion and edge detection blocks inside DEI, these generally remain the same,
128 * these could be passed later via userspace if some one needs to tweak these.
129 */
130struct vpe_dei_regs {
131 unsigned long mdt_spacial_freq_thr_reg; /* VPE_DEI_REG2 */
132 unsigned long edi_config_reg; /* VPE_DEI_REG3 */
133 unsigned long edi_lut_reg0; /* VPE_DEI_REG4 */
134 unsigned long edi_lut_reg1; /* VPE_DEI_REG5 */
135 unsigned long edi_lut_reg2; /* VPE_DEI_REG6 */
136 unsigned long edi_lut_reg3; /* VPE_DEI_REG7 */
137};
138
139/*
140 * default expert DEI register values, unlikely to be modified.
141 */
142static const struct vpe_dei_regs dei_regs = {
1148f6fd 143 .mdt_spacial_freq_thr_reg = 0x020C0804u,
15f632e6 144 .edi_config_reg = 0x0118100Cu,
1148f6fd
JL
145 .edi_lut_reg0 = 0x08040200u,
146 .edi_lut_reg1 = 0x1010100Cu,
147 .edi_lut_reg2 = 0x10101010u,
148 .edi_lut_reg3 = 0x10101010u,
45719127
AT
149};
150
151/*
152 * The port_data structure contains per-port data.
153 */
154struct vpe_port_data {
155 enum vpdma_channel channel; /* VPDMA channel */
585e6f01 156 u8 vb_index; /* input frame f, f-1, f-2 index */
45719127
AT
157 u8 vb_part; /* plane index for co-panar formats */
158};
159
160/*
161 * Define indices into the port_data tables
162 */
163#define VPE_PORT_LUMA1_IN 0
164#define VPE_PORT_CHROMA1_IN 1
585e6f01
AT
165#define VPE_PORT_LUMA2_IN 2
166#define VPE_PORT_CHROMA2_IN 3
167#define VPE_PORT_LUMA3_IN 4
168#define VPE_PORT_CHROMA3_IN 5
169#define VPE_PORT_MV_IN 6
170#define VPE_PORT_MV_OUT 7
45719127
AT
171#define VPE_PORT_LUMA_OUT 8
172#define VPE_PORT_CHROMA_OUT 9
173#define VPE_PORT_RGB_OUT 10
174
175static const struct vpe_port_data port_data[11] = {
176 [VPE_PORT_LUMA1_IN] = {
177 .channel = VPE_CHAN_LUMA1_IN,
585e6f01 178 .vb_index = 0,
45719127
AT
179 .vb_part = VPE_LUMA,
180 },
181 [VPE_PORT_CHROMA1_IN] = {
182 .channel = VPE_CHAN_CHROMA1_IN,
585e6f01
AT
183 .vb_index = 0,
184 .vb_part = VPE_CHROMA,
185 },
186 [VPE_PORT_LUMA2_IN] = {
187 .channel = VPE_CHAN_LUMA2_IN,
188 .vb_index = 1,
189 .vb_part = VPE_LUMA,
190 },
191 [VPE_PORT_CHROMA2_IN] = {
192 .channel = VPE_CHAN_CHROMA2_IN,
193 .vb_index = 1,
194 .vb_part = VPE_CHROMA,
195 },
196 [VPE_PORT_LUMA3_IN] = {
197 .channel = VPE_CHAN_LUMA3_IN,
198 .vb_index = 2,
199 .vb_part = VPE_LUMA,
200 },
201 [VPE_PORT_CHROMA3_IN] = {
202 .channel = VPE_CHAN_CHROMA3_IN,
203 .vb_index = 2,
45719127
AT
204 .vb_part = VPE_CHROMA,
205 },
585e6f01
AT
206 [VPE_PORT_MV_IN] = {
207 .channel = VPE_CHAN_MV_IN,
208 },
209 [VPE_PORT_MV_OUT] = {
210 .channel = VPE_CHAN_MV_OUT,
211 },
45719127
AT
212 [VPE_PORT_LUMA_OUT] = {
213 .channel = VPE_CHAN_LUMA_OUT,
214 .vb_part = VPE_LUMA,
215 },
216 [VPE_PORT_CHROMA_OUT] = {
217 .channel = VPE_CHAN_CHROMA_OUT,
218 .vb_part = VPE_CHROMA,
219 },
220 [VPE_PORT_RGB_OUT] = {
221 .channel = VPE_CHAN_RGB_OUT,
222 .vb_part = VPE_LUMA,
223 },
224};
225
226
227/* driver info for each of the supported video formats */
228struct vpe_fmt {
229 char *name; /* human-readable name */
230 u32 fourcc; /* standard format identifier */
231 u8 types; /* CAPTURE and/or OUTPUT */
232 u8 coplanar; /* set for unpacked Luma and Chroma */
233 /* vpdma format info for each plane */
234 struct vpdma_data_format const *vpdma_fmt[VPE_MAX_PLANES];
235};
236
237static struct vpe_fmt vpe_formats[] = {
238 {
239 .name = "YUV 422 co-planar",
240 .fourcc = V4L2_PIX_FMT_NV16,
241 .types = VPE_FMT_TYPE_CAPTURE | VPE_FMT_TYPE_OUTPUT,
242 .coplanar = 1,
243 .vpdma_fmt = { &vpdma_yuv_fmts[VPDMA_DATA_FMT_Y444],
244 &vpdma_yuv_fmts[VPDMA_DATA_FMT_C444],
245 },
246 },
247 {
248 .name = "YUV 420 co-planar",
249 .fourcc = V4L2_PIX_FMT_NV12,
250 .types = VPE_FMT_TYPE_CAPTURE | VPE_FMT_TYPE_OUTPUT,
251 .coplanar = 1,
252 .vpdma_fmt = { &vpdma_yuv_fmts[VPDMA_DATA_FMT_Y420],
253 &vpdma_yuv_fmts[VPDMA_DATA_FMT_C420],
254 },
255 },
256 {
257 .name = "YUYV 422 packed",
258 .fourcc = V4L2_PIX_FMT_YUYV,
259 .types = VPE_FMT_TYPE_CAPTURE | VPE_FMT_TYPE_OUTPUT,
260 .coplanar = 0,
261 .vpdma_fmt = { &vpdma_yuv_fmts[VPDMA_DATA_FMT_YC422],
262 },
263 },
264 {
265 .name = "UYVY 422 packed",
266 .fourcc = V4L2_PIX_FMT_UYVY,
267 .types = VPE_FMT_TYPE_CAPTURE | VPE_FMT_TYPE_OUTPUT,
268 .coplanar = 0,
269 .vpdma_fmt = { &vpdma_yuv_fmts[VPDMA_DATA_FMT_CY422],
270 },
271 },
30496799
AT
272 {
273 .name = "RGB888 packed",
274 .fourcc = V4L2_PIX_FMT_RGB24,
275 .types = VPE_FMT_TYPE_CAPTURE,
276 .coplanar = 0,
277 .vpdma_fmt = { &vpdma_rgb_fmts[VPDMA_DATA_FMT_RGB24],
278 },
279 },
280 {
281 .name = "ARGB32",
282 .fourcc = V4L2_PIX_FMT_RGB32,
283 .types = VPE_FMT_TYPE_CAPTURE,
284 .coplanar = 0,
285 .vpdma_fmt = { &vpdma_rgb_fmts[VPDMA_DATA_FMT_ARGB32],
286 },
287 },
288 {
289 .name = "BGR888 packed",
290 .fourcc = V4L2_PIX_FMT_BGR24,
291 .types = VPE_FMT_TYPE_CAPTURE,
292 .coplanar = 0,
293 .vpdma_fmt = { &vpdma_rgb_fmts[VPDMA_DATA_FMT_BGR24],
294 },
295 },
296 {
297 .name = "ABGR32",
298 .fourcc = V4L2_PIX_FMT_BGR32,
299 .types = VPE_FMT_TYPE_CAPTURE,
300 .coplanar = 0,
301 .vpdma_fmt = { &vpdma_rgb_fmts[VPDMA_DATA_FMT_ABGR32],
302 },
303 },
45719127
AT
304};
305
306/*
307 * per-queue, driver-specific private data.
308 * there is one source queue and one destination queue for each m2m context.
309 */
310struct vpe_q_data {
311 unsigned int width; /* frame width */
312 unsigned int height; /* frame height */
313 unsigned int bytesperline[VPE_MAX_PLANES]; /* bytes per line in memory */
314 enum v4l2_colorspace colorspace;
585e6f01 315 enum v4l2_field field; /* supported field value */
45719127
AT
316 unsigned int flags;
317 unsigned int sizeimage[VPE_MAX_PLANES]; /* image size in memory */
318 struct v4l2_rect c_rect; /* crop/compose rectangle */
319 struct vpe_fmt *fmt; /* format info */
320};
321
322/* vpe_q_data flag bits */
823f4208
ND
323#define Q_DATA_FRAME_1D BIT(0)
324#define Q_DATA_MODE_TILED BIT(1)
325#define Q_DATA_INTERLACED_ALTERNATE BIT(2)
326#define Q_DATA_INTERLACED_SEQ_TB BIT(3)
327
328#define Q_IS_INTERLACED (Q_DATA_INTERLACED_ALTERNATE | \
329 Q_DATA_INTERLACED_SEQ_TB)
45719127
AT
330
331enum {
332 Q_DATA_SRC = 0,
333 Q_DATA_DST = 1,
334};
335
336/* find our format description corresponding to the passed v4l2_format */
337static struct vpe_fmt *find_format(struct v4l2_format *f)
338{
339 struct vpe_fmt *fmt;
340 unsigned int k;
341
342 for (k = 0; k < ARRAY_SIZE(vpe_formats); k++) {
343 fmt = &vpe_formats[k];
344 if (fmt->fourcc == f->fmt.pix.pixelformat)
345 return fmt;
346 }
347
348 return NULL;
349}
350
351/*
352 * there is one vpe_dev structure in the driver, it is shared by
353 * all instances.
354 */
355struct vpe_dev {
356 struct v4l2_device v4l2_dev;
357 struct video_device vfd;
358 struct v4l2_m2m_dev *m2m_dev;
359
360 atomic_t num_instances; /* count of driver instances */
361 dma_addr_t loaded_mmrs; /* shadow mmrs in device */
362 struct mutex dev_mutex;
363 spinlock_t lock;
364
365 int irq;
366 void __iomem *base;
44687b2e 367 struct resource *res;
45719127 368
45719127 369 struct vpdma_data *vpdma; /* vpdma data handle */
44687b2e 370 struct sc_data *sc; /* scaler data handle */
6948082d 371 struct csc_data *csc; /* csc data handle */
45719127
AT
372};
373
374/*
375 * There is one vpe_ctx structure for each m2m context.
376 */
377struct vpe_ctx {
378 struct v4l2_fh fh;
379 struct vpe_dev *dev;
45719127
AT
380 struct v4l2_ctrl_handler hdl;
381
585e6f01 382 unsigned int field; /* current field */
45719127
AT
383 unsigned int sequence; /* current frame/field seq */
384 unsigned int aborting; /* abort after next irq */
385
386 unsigned int bufs_per_job; /* input buffers per batch */
387 unsigned int bufs_completed; /* bufs done in this batch */
388
389 struct vpe_q_data q_data[2]; /* src & dst queue data */
2d700715
JS
390 struct vb2_v4l2_buffer *src_vbs[VPE_MAX_SRC_BUFS];
391 struct vb2_v4l2_buffer *dst_vb;
45719127 392
585e6f01
AT
393 dma_addr_t mv_buf_dma[2]; /* dma addrs of motion vector in/out bufs */
394 void *mv_buf[2]; /* virtual addrs of motion vector bufs */
395 size_t mv_buf_size; /* current motion vector buffer size */
45719127 396 struct vpdma_buf mmr_adb; /* shadow reg addr/data block */
773f0657
AT
397 struct vpdma_buf sc_coeff_h; /* h coeff buffer */
398 struct vpdma_buf sc_coeff_v; /* v coeff buffer */
45719127
AT
399 struct vpdma_desc_list desc_list; /* DMA descriptor list */
400
585e6f01 401 bool deinterlacing; /* using de-interlacer */
45719127 402 bool load_mmrs; /* have new shadow reg values */
585e6f01
AT
403
404 unsigned int src_mv_buf_selector;
45719127
AT
405};
406
407
408/*
409 * M2M devices get 2 queues.
410 * Return the queue given the type.
411 */
412static struct vpe_q_data *get_q_data(struct vpe_ctx *ctx,
413 enum v4l2_buf_type type)
414{
415 switch (type) {
416 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
2ef114f6 417 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
45719127
AT
418 return &ctx->q_data[Q_DATA_SRC];
419 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
2ef114f6 420 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
45719127
AT
421 return &ctx->q_data[Q_DATA_DST];
422 default:
423 BUG();
424 }
425 return NULL;
426}
427
428static u32 read_reg(struct vpe_dev *dev, int offset)
429{
430 return ioread32(dev->base + offset);
431}
432
433static void write_reg(struct vpe_dev *dev, int offset, u32 value)
434{
435 iowrite32(value, dev->base + offset);
436}
437
438/* register field read/write helpers */
439static int get_field(u32 value, u32 mask, int shift)
440{
441 return (value & (mask << shift)) >> shift;
442}
443
444static int read_field_reg(struct vpe_dev *dev, int offset, u32 mask, int shift)
445{
446 return get_field(read_reg(dev, offset), mask, shift);
447}
448
449static void write_field(u32 *valp, u32 field, u32 mask, int shift)
450{
451 u32 val = *valp;
452
453 val &= ~(mask << shift);
454 val |= (field & mask) << shift;
455 *valp = val;
456}
457
458static void write_field_reg(struct vpe_dev *dev, int offset, u32 field,
459 u32 mask, int shift)
460{
461 u32 val = read_reg(dev, offset);
462
463 write_field(&val, field, mask, shift);
464
465 write_reg(dev, offset, val);
466}
467
468/*
469 * DMA address/data block for the shadow registers
470 */
471struct vpe_mmr_adb {
472 struct vpdma_adb_hdr out_fmt_hdr;
473 u32 out_fmt_reg[1];
474 u32 out_fmt_pad[3];
475 struct vpdma_adb_hdr us1_hdr;
476 u32 us1_regs[8];
477 struct vpdma_adb_hdr us2_hdr;
478 u32 us2_regs[8];
479 struct vpdma_adb_hdr us3_hdr;
480 u32 us3_regs[8];
481 struct vpdma_adb_hdr dei_hdr;
585e6f01 482 u32 dei_regs[8];
bbee8b39
AT
483 struct vpdma_adb_hdr sc_hdr0;
484 u32 sc_regs0[7];
485 u32 sc_pad0[1];
486 struct vpdma_adb_hdr sc_hdr8;
487 u32 sc_regs8[6];
488 u32 sc_pad8[2];
489 struct vpdma_adb_hdr sc_hdr17;
490 u32 sc_regs17[9];
491 u32 sc_pad17[3];
45719127
AT
492 struct vpdma_adb_hdr csc_hdr;
493 u32 csc_regs[6];
494 u32 csc_pad[2];
495};
496
44687b2e
AT
497#define GET_OFFSET_TOP(ctx, obj, reg) \
498 ((obj)->res->start - ctx->dev->res->start + reg)
499
45719127
AT
500#define VPE_SET_MMR_ADB_HDR(ctx, hdr, regs, offset_a) \
501 VPDMA_SET_MMR_ADB_HDR(ctx->mmr_adb, vpe_mmr_adb, hdr, regs, offset_a)
502/*
503 * Set the headers for all of the address/data block structures.
504 */
505static void init_adb_hdrs(struct vpe_ctx *ctx)
506{
507 VPE_SET_MMR_ADB_HDR(ctx, out_fmt_hdr, out_fmt_reg, VPE_CLK_FORMAT_SELECT);
508 VPE_SET_MMR_ADB_HDR(ctx, us1_hdr, us1_regs, VPE_US1_R0);
509 VPE_SET_MMR_ADB_HDR(ctx, us2_hdr, us2_regs, VPE_US2_R0);
510 VPE_SET_MMR_ADB_HDR(ctx, us3_hdr, us3_regs, VPE_US3_R0);
511 VPE_SET_MMR_ADB_HDR(ctx, dei_hdr, dei_regs, VPE_DEI_FRAME_SIZE);
bbee8b39 512 VPE_SET_MMR_ADB_HDR(ctx, sc_hdr0, sc_regs0,
44687b2e 513 GET_OFFSET_TOP(ctx, ctx->dev->sc, CFG_SC0));
bbee8b39
AT
514 VPE_SET_MMR_ADB_HDR(ctx, sc_hdr8, sc_regs8,
515 GET_OFFSET_TOP(ctx, ctx->dev->sc, CFG_SC8));
516 VPE_SET_MMR_ADB_HDR(ctx, sc_hdr17, sc_regs17,
517 GET_OFFSET_TOP(ctx, ctx->dev->sc, CFG_SC17));
6948082d
AT
518 VPE_SET_MMR_ADB_HDR(ctx, csc_hdr, csc_regs,
519 GET_OFFSET_TOP(ctx, ctx->dev->csc, CSC_CSC00));
45719127
AT
520};
521
585e6f01
AT
522/*
523 * Allocate or re-allocate the motion vector DMA buffers
524 * There are two buffers, one for input and one for output.
525 * However, the roles are reversed after each field is processed.
526 * In other words, after each field is processed, the previous
527 * output (dst) MV buffer becomes the new input (src) MV buffer.
528 */
529static int realloc_mv_buffers(struct vpe_ctx *ctx, size_t size)
530{
531 struct device *dev = ctx->dev->v4l2_dev.dev;
532
533 if (ctx->mv_buf_size == size)
534 return 0;
535
536 if (ctx->mv_buf[0])
537 dma_free_coherent(dev, ctx->mv_buf_size, ctx->mv_buf[0],
538 ctx->mv_buf_dma[0]);
539
540 if (ctx->mv_buf[1])
541 dma_free_coherent(dev, ctx->mv_buf_size, ctx->mv_buf[1],
542 ctx->mv_buf_dma[1]);
543
544 if (size == 0)
545 return 0;
546
547 ctx->mv_buf[0] = dma_alloc_coherent(dev, size, &ctx->mv_buf_dma[0],
548 GFP_KERNEL);
549 if (!ctx->mv_buf[0]) {
550 vpe_err(ctx->dev, "failed to allocate motion vector buffer\n");
551 return -ENOMEM;
552 }
553
554 ctx->mv_buf[1] = dma_alloc_coherent(dev, size, &ctx->mv_buf_dma[1],
555 GFP_KERNEL);
556 if (!ctx->mv_buf[1]) {
557 vpe_err(ctx->dev, "failed to allocate motion vector buffer\n");
558 dma_free_coherent(dev, size, ctx->mv_buf[0],
559 ctx->mv_buf_dma[0]);
560
561 return -ENOMEM;
562 }
563
564 ctx->mv_buf_size = size;
565 ctx->src_mv_buf_selector = 0;
566
567 return 0;
568}
569
570static void free_mv_buffers(struct vpe_ctx *ctx)
571{
572 realloc_mv_buffers(ctx, 0);
573}
574
575/*
576 * While de-interlacing, we keep the two most recent input buffers
577 * around. This function frees those two buffers when we have
578 * finished processing the current stream.
579 */
580static void free_vbs(struct vpe_ctx *ctx)
581{
582 struct vpe_dev *dev = ctx->dev;
583 unsigned long flags;
584
585 if (ctx->src_vbs[2] == NULL)
586 return;
587
588 spin_lock_irqsave(&dev->lock, flags);
589 if (ctx->src_vbs[2]) {
590 v4l2_m2m_buf_done(ctx->src_vbs[2], VB2_BUF_STATE_DONE);
591 v4l2_m2m_buf_done(ctx->src_vbs[1], VB2_BUF_STATE_DONE);
592 }
593 spin_unlock_irqrestore(&dev->lock, flags);
594}
595
45719127
AT
596/*
597 * Enable or disable the VPE clocks
598 */
599static void vpe_set_clock_enable(struct vpe_dev *dev, bool on)
600{
601 u32 val = 0;
602
603 if (on)
604 val = VPE_DATA_PATH_CLK_ENABLE | VPE_VPEDMA_CLK_ENABLE;
605 write_reg(dev, VPE_CLK_ENABLE, val);
606}
607
608static void vpe_top_reset(struct vpe_dev *dev)
609{
610
611 write_field_reg(dev, VPE_CLK_RESET, 1, VPE_DATA_PATH_CLK_RESET_MASK,
612 VPE_DATA_PATH_CLK_RESET_SHIFT);
613
614 usleep_range(100, 150);
615
616 write_field_reg(dev, VPE_CLK_RESET, 0, VPE_DATA_PATH_CLK_RESET_MASK,
617 VPE_DATA_PATH_CLK_RESET_SHIFT);
618}
619
620static void vpe_top_vpdma_reset(struct vpe_dev *dev)
621{
622 write_field_reg(dev, VPE_CLK_RESET, 1, VPE_VPDMA_CLK_RESET_MASK,
623 VPE_VPDMA_CLK_RESET_SHIFT);
624
625 usleep_range(100, 150);
626
627 write_field_reg(dev, VPE_CLK_RESET, 0, VPE_VPDMA_CLK_RESET_MASK,
628 VPE_VPDMA_CLK_RESET_SHIFT);
629}
630
631/*
632 * Load the correct of upsampler coefficients into the shadow MMRs
633 */
634static void set_us_coefficients(struct vpe_ctx *ctx)
635{
636 struct vpe_mmr_adb *mmr_adb = ctx->mmr_adb.addr;
585e6f01 637 struct vpe_q_data *s_q_data = &ctx->q_data[Q_DATA_SRC];
45719127
AT
638 u32 *us1_reg = &mmr_adb->us1_regs[0];
639 u32 *us2_reg = &mmr_adb->us2_regs[0];
640 u32 *us3_reg = &mmr_adb->us3_regs[0];
641 const unsigned short *cp, *end_cp;
642
643 cp = &us_coeffs[0].anchor_fid0_c0;
644
823f4208 645 if (s_q_data->flags & Q_IS_INTERLACED) /* interlaced */
585e6f01
AT
646 cp += sizeof(us_coeffs[0]) / sizeof(*cp);
647
45719127
AT
648 end_cp = cp + sizeof(us_coeffs[0]) / sizeof(*cp);
649
650 while (cp < end_cp) {
651 write_field(us1_reg, *cp++, VPE_US_C0_MASK, VPE_US_C0_SHIFT);
652 write_field(us1_reg, *cp++, VPE_US_C1_MASK, VPE_US_C1_SHIFT);
653 *us2_reg++ = *us1_reg;
654 *us3_reg++ = *us1_reg++;
655 }
656 ctx->load_mmrs = true;
657}
658
659/*
660 * Set the upsampler config mode and the VPDMA line mode in the shadow MMRs.
661 */
662static void set_cfg_and_line_modes(struct vpe_ctx *ctx)
663{
664 struct vpe_fmt *fmt = ctx->q_data[Q_DATA_SRC].fmt;
665 struct vpe_mmr_adb *mmr_adb = ctx->mmr_adb.addr;
666 u32 *us1_reg0 = &mmr_adb->us1_regs[0];
667 u32 *us2_reg0 = &mmr_adb->us2_regs[0];
668 u32 *us3_reg0 = &mmr_adb->us3_regs[0];
669 int line_mode = 1;
670 int cfg_mode = 1;
671
672 /*
673 * Cfg Mode 0: YUV420 source, enable upsampler, DEI is de-interlacing.
674 * Cfg Mode 1: YUV422 source, disable upsampler, DEI is de-interlacing.
675 */
676
677 if (fmt->fourcc == V4L2_PIX_FMT_NV12) {
678 cfg_mode = 0;
679 line_mode = 0; /* double lines to line buffer */
680 }
681
682 write_field(us1_reg0, cfg_mode, VPE_US_MODE_MASK, VPE_US_MODE_SHIFT);
683 write_field(us2_reg0, cfg_mode, VPE_US_MODE_MASK, VPE_US_MODE_SHIFT);
684 write_field(us3_reg0, cfg_mode, VPE_US_MODE_MASK, VPE_US_MODE_SHIFT);
685
686 /* regs for now */
687 vpdma_set_line_mode(ctx->dev->vpdma, line_mode, VPE_CHAN_CHROMA1_IN);
585e6f01
AT
688 vpdma_set_line_mode(ctx->dev->vpdma, line_mode, VPE_CHAN_CHROMA2_IN);
689 vpdma_set_line_mode(ctx->dev->vpdma, line_mode, VPE_CHAN_CHROMA3_IN);
45719127
AT
690
691 /* frame start for input luma */
692 vpdma_set_frame_start_event(ctx->dev->vpdma, VPDMA_FSEVENT_CHANNEL_ACTIVE,
693 VPE_CHAN_LUMA1_IN);
585e6f01
AT
694 vpdma_set_frame_start_event(ctx->dev->vpdma, VPDMA_FSEVENT_CHANNEL_ACTIVE,
695 VPE_CHAN_LUMA2_IN);
696 vpdma_set_frame_start_event(ctx->dev->vpdma, VPDMA_FSEVENT_CHANNEL_ACTIVE,
697 VPE_CHAN_LUMA3_IN);
45719127
AT
698
699 /* frame start for input chroma */
700 vpdma_set_frame_start_event(ctx->dev->vpdma, VPDMA_FSEVENT_CHANNEL_ACTIVE,
701 VPE_CHAN_CHROMA1_IN);
585e6f01
AT
702 vpdma_set_frame_start_event(ctx->dev->vpdma, VPDMA_FSEVENT_CHANNEL_ACTIVE,
703 VPE_CHAN_CHROMA2_IN);
704 vpdma_set_frame_start_event(ctx->dev->vpdma, VPDMA_FSEVENT_CHANNEL_ACTIVE,
705 VPE_CHAN_CHROMA3_IN);
706
707 /* frame start for MV in client */
708 vpdma_set_frame_start_event(ctx->dev->vpdma, VPDMA_FSEVENT_CHANNEL_ACTIVE,
709 VPE_CHAN_MV_IN);
45719127
AT
710
711 ctx->load_mmrs = true;
712}
713
714/*
715 * Set the shadow registers that are modified when the source
716 * format changes.
717 */
718static void set_src_registers(struct vpe_ctx *ctx)
719{
720 set_us_coefficients(ctx);
721}
722
723/*
724 * Set the shadow registers that are modified when the destination
725 * format changes.
726 */
727static void set_dst_registers(struct vpe_ctx *ctx)
728{
729 struct vpe_mmr_adb *mmr_adb = ctx->mmr_adb.addr;
30496799 730 enum v4l2_colorspace clrspc = ctx->q_data[Q_DATA_DST].colorspace;
45719127
AT
731 struct vpe_fmt *fmt = ctx->q_data[Q_DATA_DST].fmt;
732 u32 val = 0;
733
30496799
AT
734 if (clrspc == V4L2_COLORSPACE_SRGB)
735 val |= VPE_RGB_OUT_SELECT;
45719127
AT
736 else if (fmt->fourcc == V4L2_PIX_FMT_NV16)
737 val |= VPE_COLOR_SEPARATE_422;
738
30496799
AT
739 /*
740 * the source of CHR_DS and CSC is always the scaler, irrespective of
741 * whether it's used or not
742 */
743 val |= VPE_DS_SRC_DEI_SCALER | VPE_CSC_SRC_DEI_SCALER;
45719127
AT
744
745 if (fmt->fourcc != V4L2_PIX_FMT_NV12)
746 val |= VPE_DS_BYPASS;
747
748 mmr_adb->out_fmt_reg[0] = val;
749
750 ctx->load_mmrs = true;
751}
752
753/*
754 * Set the de-interlacer shadow register values
755 */
585e6f01 756static void set_dei_regs(struct vpe_ctx *ctx)
45719127
AT
757{
758 struct vpe_mmr_adb *mmr_adb = ctx->mmr_adb.addr;
759 struct vpe_q_data *s_q_data = &ctx->q_data[Q_DATA_SRC];
760 unsigned int src_h = s_q_data->c_rect.height;
761 unsigned int src_w = s_q_data->c_rect.width;
762 u32 *dei_mmr0 = &mmr_adb->dei_regs[0];
585e6f01 763 bool deinterlace = true;
45719127
AT
764 u32 val = 0;
765
766 /*
767 * according to TRM, we should set DEI in progressive bypass mode when
768 * the input content is progressive, however, DEI is bypassed correctly
769 * for both progressive and interlace content in interlace bypass mode.
770 * It has been recommended not to use progressive bypass mode.
771 */
823f4208 772 if (!(s_q_data->flags & Q_IS_INTERLACED) || !ctx->deinterlacing) {
585e6f01
AT
773 deinterlace = false;
774 val = VPE_DEI_INTERLACE_BYPASS;
775 }
776
777 src_h = deinterlace ? src_h * 2 : src_h;
45719127
AT
778
779 val |= (src_h << VPE_DEI_HEIGHT_SHIFT) |
780 (src_w << VPE_DEI_WIDTH_SHIFT) |
781 VPE_DEI_FIELD_FLUSH;
782
783 *dei_mmr0 = val;
784
785 ctx->load_mmrs = true;
786}
787
585e6f01
AT
788static void set_dei_shadow_registers(struct vpe_ctx *ctx)
789{
790 struct vpe_mmr_adb *mmr_adb = ctx->mmr_adb.addr;
791 u32 *dei_mmr = &mmr_adb->dei_regs[0];
792 const struct vpe_dei_regs *cur = &dei_regs;
793
794 dei_mmr[2] = cur->mdt_spacial_freq_thr_reg;
795 dei_mmr[3] = cur->edi_config_reg;
796 dei_mmr[4] = cur->edi_lut_reg0;
797 dei_mmr[5] = cur->edi_lut_reg1;
798 dei_mmr[6] = cur->edi_lut_reg2;
799 dei_mmr[7] = cur->edi_lut_reg3;
800
801 ctx->load_mmrs = true;
802}
803
15f632e6
AT
804static void config_edi_input_mode(struct vpe_ctx *ctx, int mode)
805{
806 struct vpe_mmr_adb *mmr_adb = ctx->mmr_adb.addr;
807 u32 *edi_config_reg = &mmr_adb->dei_regs[3];
808
809 if (mode & 0x2)
810 write_field(edi_config_reg, 1, 1, 2); /* EDI_ENABLE_3D */
811
812 if (mode & 0x3)
813 write_field(edi_config_reg, 1, 1, 3); /* EDI_CHROMA_3D */
814
815 write_field(edi_config_reg, mode, VPE_EDI_INP_MODE_MASK,
816 VPE_EDI_INP_MODE_SHIFT);
817
818 ctx->load_mmrs = true;
819}
820
45719127
AT
821/*
822 * Set the shadow registers whose values are modified when either the
823 * source or destination format is changed.
824 */
825static int set_srcdst_params(struct vpe_ctx *ctx)
826{
585e6f01
AT
827 struct vpe_q_data *s_q_data = &ctx->q_data[Q_DATA_SRC];
828 struct vpe_q_data *d_q_data = &ctx->q_data[Q_DATA_DST];
44687b2e 829 struct vpe_mmr_adb *mmr_adb = ctx->mmr_adb.addr;
773f0657
AT
830 unsigned int src_w = s_q_data->c_rect.width;
831 unsigned int src_h = s_q_data->c_rect.height;
832 unsigned int dst_w = d_q_data->c_rect.width;
833 unsigned int dst_h = d_q_data->c_rect.height;
585e6f01
AT
834 size_t mv_buf_size;
835 int ret;
836
45719127 837 ctx->sequence = 0;
585e6f01
AT
838 ctx->field = V4L2_FIELD_TOP;
839
823f4208
ND
840 if ((s_q_data->flags & Q_IS_INTERLACED) &&
841 !(d_q_data->flags & Q_IS_INTERLACED)) {
a51cd8f5 842 int bytes_per_line;
585e6f01
AT
843 const struct vpdma_data_format *mv =
844 &vpdma_misc_fmts[VPDMA_DATA_FMT_MV];
845
a51cd8f5
AT
846 /*
847 * we make sure that the source image has a 16 byte aligned
848 * stride, we need to do the same for the motion vector buffer
849 * by aligning it's stride to the next 16 byte boundry. this
850 * extra space will not be used by the de-interlacer, but will
851 * ensure that vpdma operates correctly
852 */
853 bytes_per_line = ALIGN((s_q_data->width * mv->depth) >> 3,
854 VPDMA_STRIDE_ALIGN);
855 mv_buf_size = bytes_per_line * s_q_data->height;
773f0657 856
68bbbd79 857 ctx->deinterlacing = true;
773f0657 858 src_h <<= 1;
585e6f01 859 } else {
68bbbd79 860 ctx->deinterlacing = false;
585e6f01
AT
861 mv_buf_size = 0;
862 }
863
864 free_vbs(ctx);
865
866 ret = realloc_mv_buffers(ctx, mv_buf_size);
867 if (ret)
868 return ret;
45719127
AT
869
870 set_cfg_and_line_modes(ctx);
585e6f01 871 set_dei_regs(ctx);
6948082d 872
30496799
AT
873 csc_set_coeff(ctx->dev->csc, &mmr_adb->csc_regs[0],
874 s_q_data->colorspace, d_q_data->colorspace);
bbee8b39 875
773f0657
AT
876 sc_set_hs_coeffs(ctx->dev->sc, ctx->sc_coeff_h.addr, src_w, dst_w);
877 sc_set_vs_coeffs(ctx->dev->sc, ctx->sc_coeff_v.addr, src_h, dst_h);
bbee8b39
AT
878
879 sc_config_scaler(ctx->dev->sc, &mmr_adb->sc_regs0[0],
880 &mmr_adb->sc_regs8[0], &mmr_adb->sc_regs17[0],
881 src_w, src_h, dst_w, dst_h);
45719127
AT
882
883 return 0;
884}
885
886/*
887 * Return the vpe_ctx structure for a given struct file
888 */
889static struct vpe_ctx *file2ctx(struct file *file)
890{
891 return container_of(file->private_data, struct vpe_ctx, fh);
892}
893
894/*
895 * mem2mem callbacks
896 */
897
898/**
899 * job_ready() - check whether an instance is ready to be scheduled to run
900 */
901static int job_ready(void *priv)
902{
903 struct vpe_ctx *ctx = priv;
45719127 904
5dc07f20
ND
905 /*
906 * This check is needed as this might be called directly from driver
907 * When called by m2m framework, this will always satisfy, but when
908 * called from vpe_irq, this might fail. (src stream with zero buffers)
909 */
910 if (v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) <= 0 ||
911 v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx) <= 0)
db476163
AT
912 return 0;
913
45719127
AT
914 return 1;
915}
916
917static void job_abort(void *priv)
918{
919 struct vpe_ctx *ctx = priv;
920
921 /* Will cancel the transaction in the next interrupt handler */
922 ctx->aborting = 1;
923}
924
925/*
926 * Lock access to the device
927 */
928static void vpe_lock(void *priv)
929{
930 struct vpe_ctx *ctx = priv;
931 struct vpe_dev *dev = ctx->dev;
932 mutex_lock(&dev->dev_mutex);
933}
934
935static void vpe_unlock(void *priv)
936{
937 struct vpe_ctx *ctx = priv;
938 struct vpe_dev *dev = ctx->dev;
939 mutex_unlock(&dev->dev_mutex);
940}
941
942static void vpe_dump_regs(struct vpe_dev *dev)
943{
944#define DUMPREG(r) vpe_dbg(dev, "%-35s %08x\n", #r, read_reg(dev, VPE_##r))
945
946 vpe_dbg(dev, "VPE Registers:\n");
947
948 DUMPREG(PID);
949 DUMPREG(SYSCONFIG);
950 DUMPREG(INT0_STATUS0_RAW);
951 DUMPREG(INT0_STATUS0);
952 DUMPREG(INT0_ENABLE0);
953 DUMPREG(INT0_STATUS1_RAW);
954 DUMPREG(INT0_STATUS1);
955 DUMPREG(INT0_ENABLE1);
956 DUMPREG(CLK_ENABLE);
957 DUMPREG(CLK_RESET);
958 DUMPREG(CLK_FORMAT_SELECT);
959 DUMPREG(CLK_RANGE_MAP);
960 DUMPREG(US1_R0);
961 DUMPREG(US1_R1);
962 DUMPREG(US1_R2);
963 DUMPREG(US1_R3);
964 DUMPREG(US1_R4);
965 DUMPREG(US1_R5);
966 DUMPREG(US1_R6);
967 DUMPREG(US1_R7);
968 DUMPREG(US2_R0);
969 DUMPREG(US2_R1);
970 DUMPREG(US2_R2);
971 DUMPREG(US2_R3);
972 DUMPREG(US2_R4);
973 DUMPREG(US2_R5);
974 DUMPREG(US2_R6);
975 DUMPREG(US2_R7);
976 DUMPREG(US3_R0);
977 DUMPREG(US3_R1);
978 DUMPREG(US3_R2);
979 DUMPREG(US3_R3);
980 DUMPREG(US3_R4);
981 DUMPREG(US3_R5);
982 DUMPREG(US3_R6);
983 DUMPREG(US3_R7);
984 DUMPREG(DEI_FRAME_SIZE);
985 DUMPREG(MDT_BYPASS);
986 DUMPREG(MDT_SF_THRESHOLD);
987 DUMPREG(EDI_CONFIG);
988 DUMPREG(DEI_EDI_LUT_R0);
989 DUMPREG(DEI_EDI_LUT_R1);
990 DUMPREG(DEI_EDI_LUT_R2);
991 DUMPREG(DEI_EDI_LUT_R3);
992 DUMPREG(DEI_FMD_WINDOW_R0);
993 DUMPREG(DEI_FMD_WINDOW_R1);
994 DUMPREG(DEI_FMD_CONTROL_R0);
995 DUMPREG(DEI_FMD_CONTROL_R1);
996 DUMPREG(DEI_FMD_STATUS_R0);
997 DUMPREG(DEI_FMD_STATUS_R1);
998 DUMPREG(DEI_FMD_STATUS_R2);
45719127 999#undef DUMPREG
44687b2e
AT
1000
1001 sc_dump_regs(dev->sc);
6948082d 1002 csc_dump_regs(dev->csc);
45719127
AT
1003}
1004
1005static void add_out_dtd(struct vpe_ctx *ctx, int port)
1006{
1007 struct vpe_q_data *q_data = &ctx->q_data[Q_DATA_DST];
1008 const struct vpe_port_data *p_data = &port_data[port];
2d700715 1009 struct vb2_buffer *vb = &ctx->dst_vb->vb2_buf;
45719127
AT
1010 struct vpe_fmt *fmt = q_data->fmt;
1011 const struct vpdma_data_format *vpdma_fmt;
585e6f01 1012 int mv_buf_selector = !ctx->src_mv_buf_selector;
45719127
AT
1013 dma_addr_t dma_addr;
1014 u32 flags = 0;
1015
585e6f01
AT
1016 if (port == VPE_PORT_MV_OUT) {
1017 vpdma_fmt = &vpdma_misc_fmts[VPDMA_DATA_FMT_MV];
1018 dma_addr = ctx->mv_buf_dma[mv_buf_selector];
1019 } else {
1020 /* to incorporate interleaved formats */
1021 int plane = fmt->coplanar ? p_data->vb_part : 0;
1022
1023 vpdma_fmt = fmt->vpdma_fmt[plane];
1024 dma_addr = vb2_dma_contig_plane_dma_addr(vb, plane);
1025 if (!dma_addr) {
1026 vpe_err(ctx->dev,
1027 "acquiring output buffer(%d) dma_addr failed\n",
1028 port);
1029 return;
1030 }
45719127
AT
1031 }
1032
1033 if (q_data->flags & Q_DATA_FRAME_1D)
1034 flags |= VPDMA_DATA_FRAME_1D;
1035 if (q_data->flags & Q_DATA_MODE_TILED)
1036 flags |= VPDMA_DATA_MODE_TILED;
1037
928bf2ba
AT
1038 vpdma_add_out_dtd(&ctx->desc_list, q_data->width, &q_data->c_rect,
1039 vpdma_fmt, dma_addr, p_data->channel, flags);
45719127
AT
1040}
1041
1042static void add_in_dtd(struct vpe_ctx *ctx, int port)
1043{
1044 struct vpe_q_data *q_data = &ctx->q_data[Q_DATA_SRC];
1045 const struct vpe_port_data *p_data = &port_data[port];
2d700715
JS
1046 struct vb2_buffer *vb = &ctx->src_vbs[p_data->vb_index]->vb2_buf;
1047 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
45719127
AT
1048 struct vpe_fmt *fmt = q_data->fmt;
1049 const struct vpdma_data_format *vpdma_fmt;
585e6f01 1050 int mv_buf_selector = ctx->src_mv_buf_selector;
2d700715 1051 int field = vbuf->field == V4L2_FIELD_BOTTOM;
928bf2ba 1052 int frame_width, frame_height;
45719127
AT
1053 dma_addr_t dma_addr;
1054 u32 flags = 0;
1055
585e6f01
AT
1056 if (port == VPE_PORT_MV_IN) {
1057 vpdma_fmt = &vpdma_misc_fmts[VPDMA_DATA_FMT_MV];
1058 dma_addr = ctx->mv_buf_dma[mv_buf_selector];
1059 } else {
1060 /* to incorporate interleaved formats */
1061 int plane = fmt->coplanar ? p_data->vb_part : 0;
45719127 1062
585e6f01
AT
1063 vpdma_fmt = fmt->vpdma_fmt[plane];
1064
1065 dma_addr = vb2_dma_contig_plane_dma_addr(vb, plane);
1066 if (!dma_addr) {
1067 vpe_err(ctx->dev,
1068 "acquiring input buffer(%d) dma_addr failed\n",
1069 port);
1070 return;
1071 }
823f4208
ND
1072
1073 if (q_data->flags & Q_DATA_INTERLACED_SEQ_TB) {
1074 /*
1075 * Use top or bottom field from same vb alternately
1076 * f,f-1,f-2 = TBT when seq is even
1077 * f,f-1,f-2 = BTB when seq is odd
1078 */
1079 field = (p_data->vb_index + (ctx->sequence % 2)) % 2;
1080
1081 if (field) {
1082 /*
1083 * bottom field of a SEQ_TB buffer
1084 * Skip the top field data by
1085 */
1086 int height = q_data->height / 2;
1087 int bpp = fmt->fourcc == V4L2_PIX_FMT_NV12 ?
1088 1 : (vpdma_fmt->depth >> 3);
1089 if (plane)
1090 height /= 2;
1091 dma_addr += q_data->width * height * bpp;
1092 }
1093 }
45719127
AT
1094 }
1095
1096 if (q_data->flags & Q_DATA_FRAME_1D)
1097 flags |= VPDMA_DATA_FRAME_1D;
1098 if (q_data->flags & Q_DATA_MODE_TILED)
1099 flags |= VPDMA_DATA_MODE_TILED;
1100
928bf2ba
AT
1101 frame_width = q_data->c_rect.width;
1102 frame_height = q_data->c_rect.height;
1103
1104 if (p_data->vb_part && fmt->fourcc == V4L2_PIX_FMT_NV12)
1105 frame_height /= 2;
1106
1107 vpdma_add_in_dtd(&ctx->desc_list, q_data->width, &q_data->c_rect,
1108 vpdma_fmt, dma_addr, p_data->channel, field, flags, frame_width,
1109 frame_height, 0, 0);
45719127
AT
1110}
1111
1112/*
1113 * Enable the expected IRQ sources
1114 */
1115static void enable_irqs(struct vpe_ctx *ctx)
1116{
1117 write_reg(ctx->dev, VPE_INT0_ENABLE0_SET, VPE_INT0_LIST0_COMPLETE);
585e6f01
AT
1118 write_reg(ctx->dev, VPE_INT0_ENABLE1_SET, VPE_DEI_ERROR_INT |
1119 VPE_DS1_UV_ERROR_INT);
45719127 1120
2f88703a 1121 vpdma_enable_list_complete_irq(ctx->dev->vpdma, 0, 0, true);
45719127
AT
1122}
1123
1124static void disable_irqs(struct vpe_ctx *ctx)
1125{
1126 write_reg(ctx->dev, VPE_INT0_ENABLE0_CLR, 0xffffffff);
1127 write_reg(ctx->dev, VPE_INT0_ENABLE1_CLR, 0xffffffff);
1128
2f88703a 1129 vpdma_enable_list_complete_irq(ctx->dev->vpdma, 0, 0, false);
45719127
AT
1130}
1131
1132/* device_run() - prepares and starts the device
1133 *
1134 * This function is only called when both the source and destination
1135 * buffers are in place.
1136 */
1137static void device_run(void *priv)
1138{
1139 struct vpe_ctx *ctx = priv;
773f0657 1140 struct sc_data *sc = ctx->dev->sc;
45719127 1141 struct vpe_q_data *d_q_data = &ctx->q_data[Q_DATA_DST];
823f4208
ND
1142 struct vpe_q_data *s_q_data = &ctx->q_data[Q_DATA_SRC];
1143
1144 if (ctx->deinterlacing && s_q_data->flags & Q_DATA_INTERLACED_SEQ_TB &&
1145 ctx->sequence % 2 == 0) {
1146 /* When using SEQ_TB buffers, When using it first time,
1147 * No need to remove the buffer as the next field is present
1148 * in the same buffer. (so that job_ready won't fail)
1149 * It will be removed when using bottom field
1150 */
1151 ctx->src_vbs[0] = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
1152 WARN_ON(ctx->src_vbs[0] == NULL);
1153 } else {
1154 ctx->src_vbs[0] = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
1155 WARN_ON(ctx->src_vbs[0] == NULL);
1156 }
45719127 1157
47e604c5 1158 ctx->dst_vb = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
45719127
AT
1159 WARN_ON(ctx->dst_vb == NULL);
1160
15f632e6 1161 if (ctx->deinterlacing) {
5dc07f20
ND
1162
1163 if (ctx->src_vbs[2] == NULL) {
1164 ctx->src_vbs[2] = ctx->src_vbs[0];
1165 WARN_ON(ctx->src_vbs[2] == NULL);
1166 ctx->src_vbs[1] = ctx->src_vbs[0];
1167 WARN_ON(ctx->src_vbs[1] == NULL);
1168 }
1169
15f632e6
AT
1170 /*
1171 * we have output the first 2 frames through line average, we
1172 * now switch to EDI de-interlacer
1173 */
1174 if (ctx->sequence == 2)
1175 config_edi_input_mode(ctx, 0x3); /* EDI (Y + UV) */
1176 }
1177
45719127
AT
1178 /* config descriptors */
1179 if (ctx->dev->loaded_mmrs != ctx->mmr_adb.dma_addr || ctx->load_mmrs) {
1180 vpdma_map_desc_buf(ctx->dev->vpdma, &ctx->mmr_adb);
1181 vpdma_add_cfd_adb(&ctx->desc_list, CFD_MMR_CLIENT, &ctx->mmr_adb);
1182 ctx->dev->loaded_mmrs = ctx->mmr_adb.dma_addr;
1183 ctx->load_mmrs = false;
1184 }
1185
773f0657
AT
1186 if (sc->loaded_coeff_h != ctx->sc_coeff_h.dma_addr ||
1187 sc->load_coeff_h) {
1188 vpdma_map_desc_buf(ctx->dev->vpdma, &ctx->sc_coeff_h);
1189 vpdma_add_cfd_block(&ctx->desc_list, CFD_SC_CLIENT,
1190 &ctx->sc_coeff_h, 0);
1191
1192 sc->loaded_coeff_h = ctx->sc_coeff_h.dma_addr;
1193 sc->load_coeff_h = false;
1194 }
1195
1196 if (sc->loaded_coeff_v != ctx->sc_coeff_v.dma_addr ||
1197 sc->load_coeff_v) {
1198 vpdma_map_desc_buf(ctx->dev->vpdma, &ctx->sc_coeff_v);
1199 vpdma_add_cfd_block(&ctx->desc_list, CFD_SC_CLIENT,
1200 &ctx->sc_coeff_v, SC_COEF_SRAM_SIZE >> 4);
1201
1202 sc->loaded_coeff_v = ctx->sc_coeff_v.dma_addr;
1203 sc->load_coeff_v = false;
1204 }
1205
585e6f01
AT
1206 /* output data descriptors */
1207 if (ctx->deinterlacing)
1208 add_out_dtd(ctx, VPE_PORT_MV_OUT);
1209
30496799
AT
1210 if (d_q_data->colorspace == V4L2_COLORSPACE_SRGB) {
1211 add_out_dtd(ctx, VPE_PORT_RGB_OUT);
1212 } else {
1213 add_out_dtd(ctx, VPE_PORT_LUMA_OUT);
1214 if (d_q_data->fmt->coplanar)
1215 add_out_dtd(ctx, VPE_PORT_CHROMA_OUT);
1216 }
45719127 1217
585e6f01
AT
1218 /* input data descriptors */
1219 if (ctx->deinterlacing) {
1220 add_in_dtd(ctx, VPE_PORT_LUMA3_IN);
1221 add_in_dtd(ctx, VPE_PORT_CHROMA3_IN);
1222
1223 add_in_dtd(ctx, VPE_PORT_LUMA2_IN);
1224 add_in_dtd(ctx, VPE_PORT_CHROMA2_IN);
1225 }
1226
45719127
AT
1227 add_in_dtd(ctx, VPE_PORT_LUMA1_IN);
1228 add_in_dtd(ctx, VPE_PORT_CHROMA1_IN);
1229
585e6f01
AT
1230 if (ctx->deinterlacing)
1231 add_in_dtd(ctx, VPE_PORT_MV_IN);
1232
45719127
AT
1233 /* sync on channel control descriptors for input ports */
1234 vpdma_add_sync_on_channel_ctd(&ctx->desc_list, VPE_CHAN_LUMA1_IN);
1235 vpdma_add_sync_on_channel_ctd(&ctx->desc_list, VPE_CHAN_CHROMA1_IN);
1236
585e6f01
AT
1237 if (ctx->deinterlacing) {
1238 vpdma_add_sync_on_channel_ctd(&ctx->desc_list,
1239 VPE_CHAN_LUMA2_IN);
1240 vpdma_add_sync_on_channel_ctd(&ctx->desc_list,
1241 VPE_CHAN_CHROMA2_IN);
1242
1243 vpdma_add_sync_on_channel_ctd(&ctx->desc_list,
1244 VPE_CHAN_LUMA3_IN);
1245 vpdma_add_sync_on_channel_ctd(&ctx->desc_list,
1246 VPE_CHAN_CHROMA3_IN);
1247
1248 vpdma_add_sync_on_channel_ctd(&ctx->desc_list, VPE_CHAN_MV_IN);
1249 }
1250
45719127 1251 /* sync on channel control descriptors for output ports */
30496799
AT
1252 if (d_q_data->colorspace == V4L2_COLORSPACE_SRGB) {
1253 vpdma_add_sync_on_channel_ctd(&ctx->desc_list,
1254 VPE_CHAN_RGB_OUT);
1255 } else {
1256 vpdma_add_sync_on_channel_ctd(&ctx->desc_list,
1257 VPE_CHAN_LUMA_OUT);
1258 if (d_q_data->fmt->coplanar)
1259 vpdma_add_sync_on_channel_ctd(&ctx->desc_list,
1260 VPE_CHAN_CHROMA_OUT);
1261 }
45719127 1262
585e6f01
AT
1263 if (ctx->deinterlacing)
1264 vpdma_add_sync_on_channel_ctd(&ctx->desc_list, VPE_CHAN_MV_OUT);
1265
45719127
AT
1266 enable_irqs(ctx);
1267
1268 vpdma_map_desc_buf(ctx->dev->vpdma, &ctx->desc_list.buf);
2f88703a 1269 vpdma_submit_descs(ctx->dev->vpdma, &ctx->desc_list, 0);
45719127
AT
1270}
1271
585e6f01
AT
1272static void dei_error(struct vpe_ctx *ctx)
1273{
1274 dev_warn(ctx->dev->v4l2_dev.dev,
1275 "received DEI error interrupt\n");
1276}
1277
45719127
AT
1278static void ds1_uv_error(struct vpe_ctx *ctx)
1279{
1280 dev_warn(ctx->dev->v4l2_dev.dev,
1281 "received downsampler error interrupt\n");
1282}
1283
1284static irqreturn_t vpe_irq(int irq_vpe, void *data)
1285{
1286 struct vpe_dev *dev = (struct vpe_dev *)data;
1287 struct vpe_ctx *ctx;
585e6f01 1288 struct vpe_q_data *d_q_data;
2d700715 1289 struct vb2_v4l2_buffer *s_vb, *d_vb;
45719127
AT
1290 unsigned long flags;
1291 u32 irqst0, irqst1;
1292
1293 irqst0 = read_reg(dev, VPE_INT0_STATUS0);
1294 if (irqst0) {
1295 write_reg(dev, VPE_INT0_STATUS0_CLR, irqst0);
1296 vpe_dbg(dev, "INT0_STATUS0 = 0x%08x\n", irqst0);
1297 }
1298
1299 irqst1 = read_reg(dev, VPE_INT0_STATUS1);
1300 if (irqst1) {
1301 write_reg(dev, VPE_INT0_STATUS1_CLR, irqst1);
1302 vpe_dbg(dev, "INT0_STATUS1 = 0x%08x\n", irqst1);
1303 }
1304
1305 ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev);
1306 if (!ctx) {
1307 vpe_err(dev, "instance released before end of transaction\n");
1308 goto handled;
1309 }
1310
585e6f01
AT
1311 if (irqst1) {
1312 if (irqst1 & VPE_DEI_ERROR_INT) {
1313 irqst1 &= ~VPE_DEI_ERROR_INT;
1314 dei_error(ctx);
1315 }
1316 if (irqst1 & VPE_DS1_UV_ERROR_INT) {
1317 irqst1 &= ~VPE_DS1_UV_ERROR_INT;
1318 ds1_uv_error(ctx);
1319 }
45719127
AT
1320 }
1321
1322 if (irqst0) {
1323 if (irqst0 & VPE_INT0_LIST0_COMPLETE)
2f88703a 1324 vpdma_clear_list_stat(ctx->dev->vpdma, 0);
45719127
AT
1325
1326 irqst0 &= ~(VPE_INT0_LIST0_COMPLETE);
1327 }
1328
1329 if (irqst0 | irqst1) {
637d5ac5 1330 dev_warn(dev->v4l2_dev.dev, "Unexpected interrupt: INT0_STATUS0 = 0x%08x, INT0_STATUS1 = 0x%08x\n",
45719127
AT
1331 irqst0, irqst1);
1332 }
1333
1334 disable_irqs(ctx);
1335
1336 vpdma_unmap_desc_buf(dev->vpdma, &ctx->desc_list.buf);
1337 vpdma_unmap_desc_buf(dev->vpdma, &ctx->mmr_adb);
773f0657
AT
1338 vpdma_unmap_desc_buf(dev->vpdma, &ctx->sc_coeff_h);
1339 vpdma_unmap_desc_buf(dev->vpdma, &ctx->sc_coeff_v);
45719127
AT
1340
1341 vpdma_reset_desc_list(&ctx->desc_list);
1342
585e6f01
AT
1343 /* the previous dst mv buffer becomes the next src mv buffer */
1344 ctx->src_mv_buf_selector = !ctx->src_mv_buf_selector;
1345
45719127
AT
1346 if (ctx->aborting)
1347 goto finished;
1348
585e6f01 1349 s_vb = ctx->src_vbs[0];
45719127 1350 d_vb = ctx->dst_vb;
45719127 1351
2d700715 1352 d_vb->flags = s_vb->flags;
d6dd645e 1353 d_vb->vb2_buf.timestamp = s_vb->vb2_buf.timestamp;
bbe24c67 1354
2d700715
JS
1355 if (s_vb->flags & V4L2_BUF_FLAG_TIMECODE)
1356 d_vb->timecode = s_vb->timecode;
bbe24c67 1357
2d700715 1358 d_vb->sequence = ctx->sequence;
585e6f01
AT
1359
1360 d_q_data = &ctx->q_data[Q_DATA_DST];
823f4208 1361 if (d_q_data->flags & Q_IS_INTERLACED) {
2d700715 1362 d_vb->field = ctx->field;
585e6f01
AT
1363 if (ctx->field == V4L2_FIELD_BOTTOM) {
1364 ctx->sequence++;
1365 ctx->field = V4L2_FIELD_TOP;
1366 } else {
1367 WARN_ON(ctx->field != V4L2_FIELD_TOP);
1368 ctx->field = V4L2_FIELD_BOTTOM;
1369 }
1370 } else {
2d700715 1371 d_vb->field = V4L2_FIELD_NONE;
585e6f01
AT
1372 ctx->sequence++;
1373 }
45719127 1374
823f4208
ND
1375 if (ctx->deinterlacing) {
1376 /*
1377 * Allow source buffer to be dequeued only if it won't be used
1378 * in the next iteration. All vbs are initialized to first
1379 * buffer and we are shifting buffers every iteration, for the
1380 * first two iterations, no buffer will be dequeued.
1381 * This ensures that driver will keep (n-2)th (n-1)th and (n)th
1382 * field when deinterlacing is enabled
1383 */
1384 if (ctx->src_vbs[2] != ctx->src_vbs[1])
1385 s_vb = ctx->src_vbs[2];
1386 else
1387 s_vb = NULL;
1388 }
45719127
AT
1389
1390 spin_lock_irqsave(&dev->lock, flags);
823f4208
ND
1391
1392 if (s_vb)
1393 v4l2_m2m_buf_done(s_vb, VB2_BUF_STATE_DONE);
1394
45719127 1395 v4l2_m2m_buf_done(d_vb, VB2_BUF_STATE_DONE);
823f4208 1396
45719127
AT
1397 spin_unlock_irqrestore(&dev->lock, flags);
1398
585e6f01
AT
1399 if (ctx->deinterlacing) {
1400 ctx->src_vbs[2] = ctx->src_vbs[1];
1401 ctx->src_vbs[1] = ctx->src_vbs[0];
1402 }
1403
45719127 1404 ctx->bufs_completed++;
5dc07f20 1405 if (ctx->bufs_completed < ctx->bufs_per_job && job_ready(ctx)) {
45719127
AT
1406 device_run(ctx);
1407 goto handled;
1408 }
1409
1410finished:
1411 vpe_dbg(ctx->dev, "finishing transaction\n");
1412 ctx->bufs_completed = 0;
47e604c5 1413 v4l2_m2m_job_finish(dev->m2m_dev, ctx->fh.m2m_ctx);
45719127
AT
1414handled:
1415 return IRQ_HANDLED;
1416}
1417
1418/*
1419 * video ioctls
1420 */
1421static int vpe_querycap(struct file *file, void *priv,
1422 struct v4l2_capability *cap)
1423{
1424 strncpy(cap->driver, VPE_MODULE_NAME, sizeof(cap->driver) - 1);
1425 strncpy(cap->card, VPE_MODULE_NAME, sizeof(cap->card) - 1);
b20902b9
AT
1426 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
1427 VPE_MODULE_NAME);
fca27a98 1428 cap->device_caps = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING;
45719127
AT
1429 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
1430 return 0;
1431}
1432
1433static int __enum_fmt(struct v4l2_fmtdesc *f, u32 type)
1434{
1435 int i, index;
1436 struct vpe_fmt *fmt = NULL;
1437
1438 index = 0;
1439 for (i = 0; i < ARRAY_SIZE(vpe_formats); ++i) {
1440 if (vpe_formats[i].types & type) {
1441 if (index == f->index) {
1442 fmt = &vpe_formats[i];
1443 break;
1444 }
1445 index++;
1446 }
1447 }
1448
1449 if (!fmt)
1450 return -EINVAL;
1451
1452 strncpy(f->description, fmt->name, sizeof(f->description) - 1);
1453 f->pixelformat = fmt->fourcc;
1454 return 0;
1455}
1456
1457static int vpe_enum_fmt(struct file *file, void *priv,
1458 struct v4l2_fmtdesc *f)
1459{
1460 if (V4L2_TYPE_IS_OUTPUT(f->type))
1461 return __enum_fmt(f, VPE_FMT_TYPE_OUTPUT);
1462
1463 return __enum_fmt(f, VPE_FMT_TYPE_CAPTURE);
1464}
1465
1466static int vpe_g_fmt(struct file *file, void *priv, struct v4l2_format *f)
1467{
1468 struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
1469 struct vpe_ctx *ctx = file2ctx(file);
1470 struct vb2_queue *vq;
1471 struct vpe_q_data *q_data;
1472 int i;
1473
47e604c5 1474 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
45719127
AT
1475 if (!vq)
1476 return -EINVAL;
1477
1478 q_data = get_q_data(ctx, f->type);
1479
1480 pix->width = q_data->width;
1481 pix->height = q_data->height;
1482 pix->pixelformat = q_data->fmt->fourcc;
585e6f01 1483 pix->field = q_data->field;
45719127
AT
1484
1485 if (V4L2_TYPE_IS_OUTPUT(f->type)) {
1486 pix->colorspace = q_data->colorspace;
1487 } else {
1488 struct vpe_q_data *s_q_data;
1489
1490 /* get colorspace from the source queue */
1491 s_q_data = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
1492
1493 pix->colorspace = s_q_data->colorspace;
1494 }
1495
1496 pix->num_planes = q_data->fmt->coplanar ? 2 : 1;
1497
1498 for (i = 0; i < pix->num_planes; i++) {
1499 pix->plane_fmt[i].bytesperline = q_data->bytesperline[i];
1500 pix->plane_fmt[i].sizeimage = q_data->sizeimage[i];
1501 }
1502
1503 return 0;
1504}
1505
1506static int __vpe_try_fmt(struct vpe_ctx *ctx, struct v4l2_format *f,
1507 struct vpe_fmt *fmt, int type)
1508{
1509 struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
1510 struct v4l2_plane_pix_format *plane_fmt;
a51cd8f5 1511 unsigned int w_align;
823f4208 1512 int i, depth, depth_bytes, height;
45719127
AT
1513
1514 if (!fmt || !(fmt->types & type)) {
1515 vpe_err(ctx->dev, "Fourcc format (0x%08x) invalid.\n",
1516 pix->pixelformat);
1517 return -EINVAL;
1518 }
1519
823f4208
ND
1520 if (pix->field != V4L2_FIELD_NONE && pix->field != V4L2_FIELD_ALTERNATE
1521 && pix->field != V4L2_FIELD_SEQ_TB)
585e6f01 1522 pix->field = V4L2_FIELD_NONE;
45719127 1523
a51cd8f5
AT
1524 depth = fmt->vpdma_fmt[VPE_LUMA]->depth;
1525
1526 /*
1527 * the line stride should 16 byte aligned for VPDMA to work, based on
1528 * the bytes per pixel, figure out how much the width should be aligned
1529 * to make sure line stride is 16 byte aligned
1530 */
1531 depth_bytes = depth >> 3;
1532
1533 if (depth_bytes == 3)
1534 /*
1535 * if bpp is 3(as in some RGB formats), the pixel width doesn't
1536 * really help in ensuring line stride is 16 byte aligned
1537 */
1538 w_align = 4;
1539 else
1540 /*
1541 * for the remainder bpp(4, 2 and 1), the pixel width alignment
1542 * can ensure a line stride alignment of 16 bytes. For example,
1543 * if bpp is 2, then the line stride can be 16 byte aligned if
1544 * the width is 8 byte aligned
1545 */
1546 w_align = order_base_2(VPDMA_DESC_ALIGN / depth_bytes);
1547
1548 v4l_bound_align_image(&pix->width, MIN_W, MAX_W, w_align,
45719127
AT
1549 &pix->height, MIN_H, MAX_H, H_ALIGN,
1550 S_ALIGN);
1551
1552 pix->num_planes = fmt->coplanar ? 2 : 1;
1553 pix->pixelformat = fmt->fourcc;
1554
823f4208
ND
1555 /*
1556 * For the actual image parameters, we need to consider the field
1557 * height of the image for SEQ_TB buffers.
1558 */
1559 if (pix->field == V4L2_FIELD_SEQ_TB)
1560 height = pix->height / 2;
1561 else
1562 height = pix->height;
1563
30496799
AT
1564 if (!pix->colorspace) {
1565 if (fmt->fourcc == V4L2_PIX_FMT_RGB24 ||
1566 fmt->fourcc == V4L2_PIX_FMT_BGR24 ||
1567 fmt->fourcc == V4L2_PIX_FMT_RGB32 ||
1568 fmt->fourcc == V4L2_PIX_FMT_BGR32) {
1569 pix->colorspace = V4L2_COLORSPACE_SRGB;
1570 } else {
823f4208 1571 if (height > 1280) /* HD */
30496799
AT
1572 pix->colorspace = V4L2_COLORSPACE_REC709;
1573 else /* SD */
1574 pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
1575 }
45719127
AT
1576 }
1577
92851f1c 1578 memset(pix->reserved, 0, sizeof(pix->reserved));
45719127 1579 for (i = 0; i < pix->num_planes; i++) {
45719127
AT
1580 plane_fmt = &pix->plane_fmt[i];
1581 depth = fmt->vpdma_fmt[i]->depth;
1582
1583 if (i == VPE_LUMA)
a51cd8f5 1584 plane_fmt->bytesperline = (pix->width * depth) >> 3;
45719127
AT
1585 else
1586 plane_fmt->bytesperline = pix->width;
1587
1588 plane_fmt->sizeimage =
1589 (pix->height * pix->width * depth) >> 3;
92851f1c
AT
1590
1591 memset(plane_fmt->reserved, 0, sizeof(plane_fmt->reserved));
45719127
AT
1592 }
1593
1594 return 0;
1595}
1596
1597static int vpe_try_fmt(struct file *file, void *priv, struct v4l2_format *f)
1598{
1599 struct vpe_ctx *ctx = file2ctx(file);
1600 struct vpe_fmt *fmt = find_format(f);
1601
1602 if (V4L2_TYPE_IS_OUTPUT(f->type))
1603 return __vpe_try_fmt(ctx, f, fmt, VPE_FMT_TYPE_OUTPUT);
1604 else
1605 return __vpe_try_fmt(ctx, f, fmt, VPE_FMT_TYPE_CAPTURE);
1606}
1607
1608static int __vpe_s_fmt(struct vpe_ctx *ctx, struct v4l2_format *f)
1609{
1610 struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
1611 struct v4l2_plane_pix_format *plane_fmt;
1612 struct vpe_q_data *q_data;
1613 struct vb2_queue *vq;
1614 int i;
1615
47e604c5 1616 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
45719127
AT
1617 if (!vq)
1618 return -EINVAL;
1619
1620 if (vb2_is_busy(vq)) {
1621 vpe_err(ctx->dev, "queue busy\n");
1622 return -EBUSY;
1623 }
1624
1625 q_data = get_q_data(ctx, f->type);
1626 if (!q_data)
1627 return -EINVAL;
1628
1629 q_data->fmt = find_format(f);
1630 q_data->width = pix->width;
1631 q_data->height = pix->height;
1632 q_data->colorspace = pix->colorspace;
585e6f01 1633 q_data->field = pix->field;
45719127
AT
1634
1635 for (i = 0; i < pix->num_planes; i++) {
1636 plane_fmt = &pix->plane_fmt[i];
1637
1638 q_data->bytesperline[i] = plane_fmt->bytesperline;
1639 q_data->sizeimage[i] = plane_fmt->sizeimage;
1640 }
1641
1642 q_data->c_rect.left = 0;
1643 q_data->c_rect.top = 0;
1644 q_data->c_rect.width = q_data->width;
1645 q_data->c_rect.height = q_data->height;
1646
585e6f01 1647 if (q_data->field == V4L2_FIELD_ALTERNATE)
823f4208
ND
1648 q_data->flags |= Q_DATA_INTERLACED_ALTERNATE;
1649 else if (q_data->field == V4L2_FIELD_SEQ_TB)
1650 q_data->flags |= Q_DATA_INTERLACED_SEQ_TB;
585e6f01 1651 else
823f4208
ND
1652 q_data->flags &= ~Q_IS_INTERLACED;
1653
1654 /* the crop height is halved for the case of SEQ_TB buffers */
1655 if (q_data->flags & Q_DATA_INTERLACED_SEQ_TB)
1656 q_data->c_rect.height /= 2;
585e6f01 1657
45719127
AT
1658 vpe_dbg(ctx->dev, "Setting format for type %d, wxh: %dx%d, fmt: %d bpl_y %d",
1659 f->type, q_data->width, q_data->height, q_data->fmt->fourcc,
1660 q_data->bytesperline[VPE_LUMA]);
1661 if (q_data->fmt->coplanar)
1662 vpe_dbg(ctx->dev, " bpl_uv %d\n",
1663 q_data->bytesperline[VPE_CHROMA]);
1664
1665 return 0;
1666}
1667
1668static int vpe_s_fmt(struct file *file, void *priv, struct v4l2_format *f)
1669{
1670 int ret;
1671 struct vpe_ctx *ctx = file2ctx(file);
1672
1673 ret = vpe_try_fmt(file, priv, f);
1674 if (ret)
1675 return ret;
1676
1677 ret = __vpe_s_fmt(ctx, f);
1678 if (ret)
1679 return ret;
1680
1681 if (V4L2_TYPE_IS_OUTPUT(f->type))
1682 set_src_registers(ctx);
1683 else
1684 set_dst_registers(ctx);
1685
1686 return set_srcdst_params(ctx);
1687}
1688
2ef114f6
AT
1689static int __vpe_try_selection(struct vpe_ctx *ctx, struct v4l2_selection *s)
1690{
1691 struct vpe_q_data *q_data;
823f4208 1692 int height;
2ef114f6
AT
1693
1694 if ((s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
1695 (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT))
1696 return -EINVAL;
1697
1698 q_data = get_q_data(ctx, s->type);
1699 if (!q_data)
1700 return -EINVAL;
1701
1702 switch (s->target) {
1703 case V4L2_SEL_TGT_COMPOSE:
1704 /*
1705 * COMPOSE target is only valid for capture buffer type, return
1706 * error for output buffer type
1707 */
1708 if (s->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1709 return -EINVAL;
1710 break;
1711 case V4L2_SEL_TGT_CROP:
1712 /*
1713 * CROP target is only valid for output buffer type, return
1714 * error for capture buffer type
1715 */
1716 if (s->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1717 return -EINVAL;
1718 break;
1719 /*
1720 * bound and default crop/compose targets are invalid targets to
1721 * try/set
1722 */
1723 default:
1724 return -EINVAL;
1725 }
1726
823f4208
ND
1727 /*
1728 * For SEQ_TB buffers, crop height should be less than the height of
1729 * the field height, not the buffer height
1730 */
1731 if (q_data->flags & Q_DATA_INTERLACED_SEQ_TB)
1732 height = q_data->height / 2;
1733 else
1734 height = q_data->height;
1735
2ef114f6
AT
1736 if (s->r.top < 0 || s->r.left < 0) {
1737 vpe_err(ctx->dev, "negative values for top and left\n");
1738 s->r.top = s->r.left = 0;
1739 }
1740
1741 v4l_bound_align_image(&s->r.width, MIN_W, q_data->width, 1,
823f4208 1742 &s->r.height, MIN_H, height, H_ALIGN, S_ALIGN);
2ef114f6
AT
1743
1744 /* adjust left/top if cropping rectangle is out of bounds */
1745 if (s->r.left + s->r.width > q_data->width)
1746 s->r.left = q_data->width - s->r.width;
1747 if (s->r.top + s->r.height > q_data->height)
1748 s->r.top = q_data->height - s->r.height;
1749
1750 return 0;
1751}
1752
1753static int vpe_g_selection(struct file *file, void *fh,
1754 struct v4l2_selection *s)
1755{
1756 struct vpe_ctx *ctx = file2ctx(file);
1757 struct vpe_q_data *q_data;
1758 bool use_c_rect = false;
1759
1760 if ((s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
1761 (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT))
1762 return -EINVAL;
1763
1764 q_data = get_q_data(ctx, s->type);
1765 if (!q_data)
1766 return -EINVAL;
1767
1768 switch (s->target) {
1769 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
1770 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1771 if (s->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1772 return -EINVAL;
1773 break;
1774 case V4L2_SEL_TGT_CROP_BOUNDS:
1775 case V4L2_SEL_TGT_CROP_DEFAULT:
1776 if (s->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1777 return -EINVAL;
1778 break;
1779 case V4L2_SEL_TGT_COMPOSE:
1780 if (s->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1781 return -EINVAL;
1782 use_c_rect = true;
1783 break;
1784 case V4L2_SEL_TGT_CROP:
1785 if (s->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1786 return -EINVAL;
1787 use_c_rect = true;
1788 break;
1789 default:
1790 return -EINVAL;
1791 }
1792
1793 if (use_c_rect) {
1794 /*
1795 * for CROP/COMPOSE target type, return c_rect params from the
1796 * respective buffer type
1797 */
1798 s->r = q_data->c_rect;
1799 } else {
1800 /*
1801 * for DEFAULT/BOUNDS target type, return width and height from
1802 * S_FMT of the respective buffer type
1803 */
1804 s->r.left = 0;
1805 s->r.top = 0;
1806 s->r.width = q_data->width;
1807 s->r.height = q_data->height;
1808 }
1809
1810 return 0;
1811}
1812
1813
1814static int vpe_s_selection(struct file *file, void *fh,
1815 struct v4l2_selection *s)
1816{
1817 struct vpe_ctx *ctx = file2ctx(file);
1818 struct vpe_q_data *q_data;
1819 struct v4l2_selection sel = *s;
1820 int ret;
1821
1822 ret = __vpe_try_selection(ctx, &sel);
1823 if (ret)
1824 return ret;
1825
1826 q_data = get_q_data(ctx, sel.type);
1827 if (!q_data)
1828 return -EINVAL;
1829
1830 if ((q_data->c_rect.left == sel.r.left) &&
1831 (q_data->c_rect.top == sel.r.top) &&
1832 (q_data->c_rect.width == sel.r.width) &&
1833 (q_data->c_rect.height == sel.r.height)) {
1834 vpe_dbg(ctx->dev,
1835 "requested crop/compose values are already set\n");
1836 return 0;
1837 }
1838
1839 q_data->c_rect = sel.r;
1840
1841 return set_srcdst_params(ctx);
1842}
1843
45719127
AT
1844/*
1845 * defines number of buffers/frames a context can process with VPE before
1846 * switching to a different context. default value is 1 buffer per context
1847 */
1848#define V4L2_CID_VPE_BUFS_PER_JOB (V4L2_CID_USER_TI_VPE_BASE + 0)
1849
1850static int vpe_s_ctrl(struct v4l2_ctrl *ctrl)
1851{
1852 struct vpe_ctx *ctx =
1853 container_of(ctrl->handler, struct vpe_ctx, hdl);
1854
1855 switch (ctrl->id) {
1856 case V4L2_CID_VPE_BUFS_PER_JOB:
1857 ctx->bufs_per_job = ctrl->val;
1858 break;
1859
1860 default:
1861 vpe_err(ctx->dev, "Invalid control\n");
1862 return -EINVAL;
1863 }
1864
1865 return 0;
1866}
1867
1868static const struct v4l2_ctrl_ops vpe_ctrl_ops = {
1869 .s_ctrl = vpe_s_ctrl,
1870};
1871
1872static const struct v4l2_ioctl_ops vpe_ioctl_ops = {
47e604c5 1873 .vidioc_querycap = vpe_querycap,
45719127 1874
47e604c5 1875 .vidioc_enum_fmt_vid_cap_mplane = vpe_enum_fmt,
45719127
AT
1876 .vidioc_g_fmt_vid_cap_mplane = vpe_g_fmt,
1877 .vidioc_try_fmt_vid_cap_mplane = vpe_try_fmt,
1878 .vidioc_s_fmt_vid_cap_mplane = vpe_s_fmt,
1879
47e604c5 1880 .vidioc_enum_fmt_vid_out_mplane = vpe_enum_fmt,
45719127
AT
1881 .vidioc_g_fmt_vid_out_mplane = vpe_g_fmt,
1882 .vidioc_try_fmt_vid_out_mplane = vpe_try_fmt,
1883 .vidioc_s_fmt_vid_out_mplane = vpe_s_fmt,
1884
2ef114f6
AT
1885 .vidioc_g_selection = vpe_g_selection,
1886 .vidioc_s_selection = vpe_s_selection,
1887
47e604c5
PL
1888 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
1889 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
1890 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
1891 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
1892 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
1893 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
45719127 1894
47e604c5
PL
1895 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1896 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
45719127
AT
1897};
1898
1899/*
1900 * Queue operations
1901 */
1902static int vpe_queue_setup(struct vb2_queue *vq,
45719127 1903 unsigned int *nbuffers, unsigned int *nplanes,
36c0f8b3 1904 unsigned int sizes[], struct device *alloc_devs[])
45719127
AT
1905{
1906 int i;
1907 struct vpe_ctx *ctx = vb2_get_drv_priv(vq);
1908 struct vpe_q_data *q_data;
1909
1910 q_data = get_q_data(ctx, vq->type);
1911
1912 *nplanes = q_data->fmt->coplanar ? 2 : 1;
1913
dce57314 1914 for (i = 0; i < *nplanes; i++)
45719127 1915 sizes[i] = q_data->sizeimage[i];
45719127
AT
1916
1917 vpe_dbg(ctx->dev, "get %d buffer(s) of size %d", *nbuffers,
1918 sizes[VPE_LUMA]);
1919 if (q_data->fmt->coplanar)
1920 vpe_dbg(ctx->dev, " and %d\n", sizes[VPE_CHROMA]);
1921
1922 return 0;
1923}
1924
1925static int vpe_buf_prepare(struct vb2_buffer *vb)
1926{
2d700715 1927 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
45719127
AT
1928 struct vpe_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1929 struct vpe_q_data *q_data;
1930 int i, num_planes;
1931
1932 vpe_dbg(ctx->dev, "type: %d\n", vb->vb2_queue->type);
1933
1934 q_data = get_q_data(ctx, vb->vb2_queue->type);
1935 num_planes = q_data->fmt->coplanar ? 2 : 1;
1936
5269fef7 1937 if (vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
823f4208 1938 if (!(q_data->flags & Q_IS_INTERLACED)) {
2d700715 1939 vbuf->field = V4L2_FIELD_NONE;
5269fef7 1940 } else {
2d700715 1941 if (vbuf->field != V4L2_FIELD_TOP &&
823f4208
ND
1942 vbuf->field != V4L2_FIELD_BOTTOM &&
1943 vbuf->field != V4L2_FIELD_SEQ_TB)
5269fef7
AT
1944 return -EINVAL;
1945 }
1946 }
1947
45719127
AT
1948 for (i = 0; i < num_planes; i++) {
1949 if (vb2_plane_size(vb, i) < q_data->sizeimage[i]) {
1950 vpe_err(ctx->dev,
1951 "data will not fit into plane (%lu < %lu)\n",
1952 vb2_plane_size(vb, i),
1953 (long) q_data->sizeimage[i]);
1954 return -EINVAL;
1955 }
1956 }
1957
1958 for (i = 0; i < num_planes; i++)
1959 vb2_set_plane_payload(vb, i, q_data->sizeimage[i]);
1960
1961 return 0;
1962}
1963
1964static void vpe_buf_queue(struct vb2_buffer *vb)
1965{
2d700715 1966 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
45719127 1967 struct vpe_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
47e604c5 1968
2d700715 1969 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
47e604c5
PL
1970}
1971
1972static int vpe_start_streaming(struct vb2_queue *q, unsigned int count)
1973{
15f632e6
AT
1974 struct vpe_ctx *ctx = vb2_get_drv_priv(q);
1975
1976 if (ctx->deinterlacing)
1977 config_edi_input_mode(ctx, 0x0);
47e604c5
PL
1978
1979 return 0;
1980}
1981
1982static void vpe_stop_streaming(struct vb2_queue *q)
1983{
1984 struct vpe_ctx *ctx = vb2_get_drv_priv(q);
1985
1986 vpe_dump_regs(ctx->dev);
1987 vpdma_dump_regs(ctx->dev->vpdma);
45719127
AT
1988}
1989
b7b361f0 1990static const struct vb2_ops vpe_qops = {
45719127
AT
1991 .queue_setup = vpe_queue_setup,
1992 .buf_prepare = vpe_buf_prepare,
1993 .buf_queue = vpe_buf_queue,
3d0aed38
PL
1994 .wait_prepare = vb2_ops_wait_prepare,
1995 .wait_finish = vb2_ops_wait_finish,
47e604c5
PL
1996 .start_streaming = vpe_start_streaming,
1997 .stop_streaming = vpe_stop_streaming,
45719127
AT
1998};
1999
2000static int queue_init(void *priv, struct vb2_queue *src_vq,
2001 struct vb2_queue *dst_vq)
2002{
2003 struct vpe_ctx *ctx = priv;
3d0aed38 2004 struct vpe_dev *dev = ctx->dev;
45719127
AT
2005 int ret;
2006
2007 memset(src_vq, 0, sizeof(*src_vq));
2008 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
668f91d4 2009 src_vq->io_modes = VB2_MMAP | VB2_DMABUF;
45719127
AT
2010 src_vq->drv_priv = ctx;
2011 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
2012 src_vq->ops = &vpe_qops;
2013 src_vq->mem_ops = &vb2_dma_contig_memops;
ade48681 2014 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
3d0aed38 2015 src_vq->lock = &dev->dev_mutex;
dce57314 2016 src_vq->dev = dev->v4l2_dev.dev;
45719127
AT
2017
2018 ret = vb2_queue_init(src_vq);
2019 if (ret)
2020 return ret;
2021
2022 memset(dst_vq, 0, sizeof(*dst_vq));
2023 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
668f91d4 2024 dst_vq->io_modes = VB2_MMAP | VB2_DMABUF;
45719127
AT
2025 dst_vq->drv_priv = ctx;
2026 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
2027 dst_vq->ops = &vpe_qops;
2028 dst_vq->mem_ops = &vb2_dma_contig_memops;
ade48681 2029 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
3d0aed38 2030 dst_vq->lock = &dev->dev_mutex;
dce57314 2031 dst_vq->dev = dev->v4l2_dev.dev;
45719127
AT
2032
2033 return vb2_queue_init(dst_vq);
2034}
2035
2036static const struct v4l2_ctrl_config vpe_bufs_per_job = {
2037 .ops = &vpe_ctrl_ops,
2038 .id = V4L2_CID_VPE_BUFS_PER_JOB,
2039 .name = "Buffers Per Transaction",
2040 .type = V4L2_CTRL_TYPE_INTEGER,
2041 .def = VPE_DEF_BUFS_PER_JOB,
2042 .min = 1,
2043 .max = VIDEO_MAX_FRAME,
2044 .step = 1,
2045};
2046
2047/*
2048 * File operations
2049 */
2050static int vpe_open(struct file *file)
2051{
2052 struct vpe_dev *dev = video_drvdata(file);
45719127
AT
2053 struct vpe_q_data *s_q_data;
2054 struct v4l2_ctrl_handler *hdl;
47e604c5 2055 struct vpe_ctx *ctx;
45719127
AT
2056 int ret;
2057
2058 vpe_dbg(dev, "vpe_open\n");
2059
45719127
AT
2060 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
2061 if (!ctx)
2062 return -ENOMEM;
2063
2064 ctx->dev = dev;
2065
2066 if (mutex_lock_interruptible(&dev->dev_mutex)) {
2067 ret = -ERESTARTSYS;
2068 goto free_ctx;
2069 }
2070
2071 ret = vpdma_create_desc_list(&ctx->desc_list, VPE_DESC_LIST_SIZE,
2072 VPDMA_LIST_TYPE_NORMAL);
2073 if (ret != 0)
2074 goto unlock;
2075
2076 ret = vpdma_alloc_desc_buf(&ctx->mmr_adb, sizeof(struct vpe_mmr_adb));
2077 if (ret != 0)
2078 goto free_desc_list;
2079
773f0657
AT
2080 ret = vpdma_alloc_desc_buf(&ctx->sc_coeff_h, SC_COEF_SRAM_SIZE);
2081 if (ret != 0)
2082 goto free_mmr_adb;
2083
2084 ret = vpdma_alloc_desc_buf(&ctx->sc_coeff_v, SC_COEF_SRAM_SIZE);
2085 if (ret != 0)
2086 goto free_sc_h;
2087
45719127
AT
2088 init_adb_hdrs(ctx);
2089
2090 v4l2_fh_init(&ctx->fh, video_devdata(file));
2091 file->private_data = &ctx->fh;
2092
2093 hdl = &ctx->hdl;
2094 v4l2_ctrl_handler_init(hdl, 1);
2095 v4l2_ctrl_new_custom(hdl, &vpe_bufs_per_job, NULL);
2096 if (hdl->error) {
2097 ret = hdl->error;
2098 goto exit_fh;
2099 }
2100 ctx->fh.ctrl_handler = hdl;
2101 v4l2_ctrl_handler_setup(hdl);
2102
2103 s_q_data = &ctx->q_data[Q_DATA_SRC];
2104 s_q_data->fmt = &vpe_formats[2];
2105 s_q_data->width = 1920;
2106 s_q_data->height = 1080;
67fb87ee 2107 s_q_data->bytesperline[VPE_LUMA] = (s_q_data->width *
45719127 2108 s_q_data->fmt->vpdma_fmt[VPE_LUMA]->depth) >> 3;
67fb87ee
AT
2109 s_q_data->sizeimage[VPE_LUMA] = (s_q_data->bytesperline[VPE_LUMA] *
2110 s_q_data->height);
2111 s_q_data->colorspace = V4L2_COLORSPACE_REC709;
585e6f01 2112 s_q_data->field = V4L2_FIELD_NONE;
45719127
AT
2113 s_q_data->c_rect.left = 0;
2114 s_q_data->c_rect.top = 0;
2115 s_q_data->c_rect.width = s_q_data->width;
2116 s_q_data->c_rect.height = s_q_data->height;
2117 s_q_data->flags = 0;
2118
2119 ctx->q_data[Q_DATA_DST] = *s_q_data;
2120
585e6f01 2121 set_dei_shadow_registers(ctx);
45719127
AT
2122 set_src_registers(ctx);
2123 set_dst_registers(ctx);
2124 ret = set_srcdst_params(ctx);
2125 if (ret)
2126 goto exit_fh;
2127
47e604c5 2128 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx, &queue_init);
45719127 2129
47e604c5
PL
2130 if (IS_ERR(ctx->fh.m2m_ctx)) {
2131 ret = PTR_ERR(ctx->fh.m2m_ctx);
45719127
AT
2132 goto exit_fh;
2133 }
2134
2135 v4l2_fh_add(&ctx->fh);
2136
2137 /*
2138 * for now, just report the creation of the first instance, we can later
2139 * optimize the driver to enable or disable clocks when the first
2140 * instance is created or the last instance released
2141 */
2142 if (atomic_inc_return(&dev->num_instances) == 1)
2143 vpe_dbg(dev, "first instance created\n");
2144
2145 ctx->bufs_per_job = VPE_DEF_BUFS_PER_JOB;
2146
2147 ctx->load_mmrs = true;
2148
2149 vpe_dbg(dev, "created instance %p, m2m_ctx: %p\n",
47e604c5 2150 ctx, ctx->fh.m2m_ctx);
45719127
AT
2151
2152 mutex_unlock(&dev->dev_mutex);
2153
2154 return 0;
2155exit_fh:
2156 v4l2_ctrl_handler_free(hdl);
2157 v4l2_fh_exit(&ctx->fh);
773f0657
AT
2158 vpdma_free_desc_buf(&ctx->sc_coeff_v);
2159free_sc_h:
2160 vpdma_free_desc_buf(&ctx->sc_coeff_h);
2161free_mmr_adb:
45719127
AT
2162 vpdma_free_desc_buf(&ctx->mmr_adb);
2163free_desc_list:
2164 vpdma_free_desc_list(&ctx->desc_list);
2165unlock:
2166 mutex_unlock(&dev->dev_mutex);
2167free_ctx:
2168 kfree(ctx);
2169 return ret;
2170}
2171
2172static int vpe_release(struct file *file)
2173{
2174 struct vpe_dev *dev = video_drvdata(file);
2175 struct vpe_ctx *ctx = file2ctx(file);
2176
2177 vpe_dbg(dev, "releasing instance %p\n", ctx);
2178
2179 mutex_lock(&dev->dev_mutex);
585e6f01
AT
2180 free_vbs(ctx);
2181 free_mv_buffers(ctx);
45719127
AT
2182 vpdma_free_desc_list(&ctx->desc_list);
2183 vpdma_free_desc_buf(&ctx->mmr_adb);
2184
2185 v4l2_fh_del(&ctx->fh);
2186 v4l2_fh_exit(&ctx->fh);
2187 v4l2_ctrl_handler_free(&ctx->hdl);
47e604c5 2188 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
45719127
AT
2189
2190 kfree(ctx);
2191
2192 /*
2193 * for now, just report the release of the last instance, we can later
2194 * optimize the driver to enable or disable clocks when the first
2195 * instance is created or the last instance released
2196 */
2197 if (atomic_dec_return(&dev->num_instances) == 0)
2198 vpe_dbg(dev, "last instance released\n");
2199
2200 mutex_unlock(&dev->dev_mutex);
2201
2202 return 0;
2203}
2204
45719127
AT
2205static const struct v4l2_file_operations vpe_fops = {
2206 .owner = THIS_MODULE,
2207 .open = vpe_open,
2208 .release = vpe_release,
47e604c5 2209 .poll = v4l2_m2m_fop_poll,
45719127 2210 .unlocked_ioctl = video_ioctl2,
47e604c5 2211 .mmap = v4l2_m2m_fop_mmap,
45719127
AT
2212};
2213
2214static struct video_device vpe_videodev = {
2215 .name = VPE_MODULE_NAME,
2216 .fops = &vpe_fops,
2217 .ioctl_ops = &vpe_ioctl_ops,
2218 .minor = -1,
772a7b7a 2219 .release = video_device_release_empty,
45719127
AT
2220 .vfl_dir = VFL_DIR_M2M,
2221};
2222
2223static struct v4l2_m2m_ops m2m_ops = {
2224 .device_run = device_run,
2225 .job_ready = job_ready,
2226 .job_abort = job_abort,
2227 .lock = vpe_lock,
2228 .unlock = vpe_unlock,
2229};
2230
2231static int vpe_runtime_get(struct platform_device *pdev)
2232{
2233 int r;
2234
2235 dev_dbg(&pdev->dev, "vpe_runtime_get\n");
2236
2237 r = pm_runtime_get_sync(&pdev->dev);
2238 WARN_ON(r < 0);
2239 return r < 0 ? r : 0;
2240}
2241
2242static void vpe_runtime_put(struct platform_device *pdev)
2243{
2244
2245 int r;
2246
2247 dev_dbg(&pdev->dev, "vpe_runtime_put\n");
2248
2249 r = pm_runtime_put_sync(&pdev->dev);
2250 WARN_ON(r < 0 && r != -ENOSYS);
2251}
2252
b2c9472f
AT
2253static void vpe_fw_cb(struct platform_device *pdev)
2254{
2255 struct vpe_dev *dev = platform_get_drvdata(pdev);
2256 struct video_device *vfd;
2257 int ret;
2258
2259 vfd = &dev->vfd;
2260 *vfd = vpe_videodev;
2261 vfd->lock = &dev->dev_mutex;
2262 vfd->v4l2_dev = &dev->v4l2_dev;
2263
2264 ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
2265 if (ret) {
2266 vpe_err(dev, "Failed to register video device\n");
2267
2268 vpe_set_clock_enable(dev, 0);
2269 vpe_runtime_put(pdev);
2270 pm_runtime_disable(&pdev->dev);
2271 v4l2_m2m_release(dev->m2m_dev);
b2c9472f
AT
2272 v4l2_device_unregister(&dev->v4l2_dev);
2273
2274 return;
2275 }
2276
2277 video_set_drvdata(vfd, dev);
2278 snprintf(vfd->name, sizeof(vfd->name), "%s", vpe_videodev.name);
2279 dev_info(dev->v4l2_dev.dev, "Device registered as /dev/video%d\n",
2280 vfd->num);
2281}
2282
45719127
AT
2283static int vpe_probe(struct platform_device *pdev)
2284{
2285 struct vpe_dev *dev;
45719127
AT
2286 int ret, irq, func;
2287
2288 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
b68231a1
WY
2289 if (!dev)
2290 return -ENOMEM;
45719127
AT
2291
2292 spin_lock_init(&dev->lock);
2293
2294 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
2295 if (ret)
2296 return ret;
2297
2298 atomic_set(&dev->num_instances, 0);
2299 mutex_init(&dev->dev_mutex);
2300
44687b2e
AT
2301 dev->res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
2302 "vpe_top");
45719127
AT
2303 /*
2304 * HACK: we get resource info from device tree in the form of a list of
2305 * VPE sub blocks, the driver currently uses only the base of vpe_top
2306 * for register access, the driver should be changed later to access
2307 * registers based on the sub block base addresses
2308 */
44687b2e 2309 dev->base = devm_ioremap(&pdev->dev, dev->res->start, SZ_32K);
b68231a1
WY
2310 if (!dev->base) {
2311 ret = -ENOMEM;
45719127
AT
2312 goto v4l2_dev_unreg;
2313 }
2314
2315 irq = platform_get_irq(pdev, 0);
2316 ret = devm_request_irq(&pdev->dev, irq, vpe_irq, 0, VPE_MODULE_NAME,
2317 dev);
2318 if (ret)
2319 goto v4l2_dev_unreg;
2320
2321 platform_set_drvdata(pdev, dev);
2322
45719127
AT
2323 dev->m2m_dev = v4l2_m2m_init(&m2m_ops);
2324 if (IS_ERR(dev->m2m_dev)) {
2325 vpe_err(dev, "Failed to init mem2mem device\n");
2326 ret = PTR_ERR(dev->m2m_dev);
dce57314 2327 goto v4l2_dev_unreg;
45719127
AT
2328 }
2329
2330 pm_runtime_enable(&pdev->dev);
2331
2332 ret = vpe_runtime_get(pdev);
2333 if (ret)
2334 goto rel_m2m;
2335
2336 /* Perform clk enable followed by reset */
2337 vpe_set_clock_enable(dev, 1);
2338
2339 vpe_top_reset(dev);
2340
2341 func = read_field_reg(dev, VPE_PID, VPE_PID_FUNC_MASK,
2342 VPE_PID_FUNC_SHIFT);
2343 vpe_dbg(dev, "VPE PID function %x\n", func);
2344
2345 vpe_top_vpdma_reset(dev);
2346
44687b2e
AT
2347 dev->sc = sc_create(pdev);
2348 if (IS_ERR(dev->sc)) {
2349 ret = PTR_ERR(dev->sc);
2350 goto runtime_put;
2351 }
2352
6948082d
AT
2353 dev->csc = csc_create(pdev);
2354 if (IS_ERR(dev->csc)) {
2355 ret = PTR_ERR(dev->csc);
2356 goto runtime_put;
2357 }
2358
b2c9472f 2359 dev->vpdma = vpdma_create(pdev, vpe_fw_cb);
6676cafe
WY
2360 if (IS_ERR(dev->vpdma)) {
2361 ret = PTR_ERR(dev->vpdma);
45719127 2362 goto runtime_put;
6676cafe 2363 }
45719127 2364
45719127
AT
2365 return 0;
2366
2367runtime_put:
2368 vpe_runtime_put(pdev);
2369rel_m2m:
2370 pm_runtime_disable(&pdev->dev);
2371 v4l2_m2m_release(dev->m2m_dev);
45719127
AT
2372v4l2_dev_unreg:
2373 v4l2_device_unregister(&dev->v4l2_dev);
2374
2375 return ret;
2376}
2377
2378static int vpe_remove(struct platform_device *pdev)
2379{
1eb96047 2380 struct vpe_dev *dev = platform_get_drvdata(pdev);
45719127
AT
2381
2382 v4l2_info(&dev->v4l2_dev, "Removing " VPE_MODULE_NAME);
2383
2384 v4l2_m2m_release(dev->m2m_dev);
2385 video_unregister_device(&dev->vfd);
2386 v4l2_device_unregister(&dev->v4l2_dev);
45719127
AT
2387
2388 vpe_set_clock_enable(dev, 0);
2389 vpe_runtime_put(pdev);
2390 pm_runtime_disable(&pdev->dev);
2391
2392 return 0;
2393}
2394
2395#if defined(CONFIG_OF)
2396static const struct of_device_id vpe_of_match[] = {
2397 {
2398 .compatible = "ti,vpe",
2399 },
2400 {},
2401};
45719127
AT
2402#endif
2403
2404static struct platform_driver vpe_pdrv = {
2405 .probe = vpe_probe,
2406 .remove = vpe_remove,
2407 .driver = {
2408 .name = VPE_MODULE_NAME,
47e604c5 2409 .of_match_table = of_match_ptr(vpe_of_match),
45719127
AT
2410 },
2411};
2412
903cbb83 2413module_platform_driver(vpe_pdrv);
45719127
AT
2414
2415MODULE_DESCRIPTION("TI VPE driver");
2416MODULE_AUTHOR("Dale Farnsworth, <dale@farnsworth.org>");
2417MODULE_LICENSE("GPL");