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