blob: 1c9e57c278e9fef6597f018df92d0bfc05701626 [file] [log] [blame]
Archit Taneja45719122013-10-16 02:36:47 -03001/*
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/platform_device.h>
29#include <linux/pm_runtime.h>
30#include <linux/sched.h>
31#include <linux/slab.h>
32#include <linux/videodev2.h>
Archit Tanejaa51cd8f2013-12-03 08:51:13 -030033#include <linux/log2.h>
Archit Taneja45719122013-10-16 02:36:47 -030034
35#include <media/v4l2-common.h>
36#include <media/v4l2-ctrls.h>
37#include <media/v4l2-device.h>
38#include <media/v4l2-event.h>
39#include <media/v4l2-ioctl.h>
40#include <media/v4l2-mem2mem.h>
41#include <media/videobuf2-core.h>
42#include <media/videobuf2-dma-contig.h>
43
44#include "vpdma.h"
45#include "vpe_regs.h"
Archit Taneja44687b22013-12-12 05:35:57 -030046#include "sc.h"
Archit Taneja69480822013-12-12 05:36:01 -030047#include "csc.h"
Archit Taneja45719122013-10-16 02:36:47 -030048
49#define VPE_MODULE_NAME "vpe"
50
51/* minimum and maximum frame sizes */
Archit Tanejace392fd2014-02-12 04:04:22 -030052#define MIN_W 32
53#define MIN_H 32
Archit Taneja45719122013-10-16 02:36:47 -030054#define MAX_W 1920
55#define MAX_H 1080
56
57/* required alignments */
58#define S_ALIGN 0 /* multiple of 1 */
59#define H_ALIGN 1 /* multiple of 2 */
Archit Taneja45719122013-10-16 02:36:47 -030060
61/* flags that indicate a format can be used for capture/output */
62#define VPE_FMT_TYPE_CAPTURE (1 << 0)
63#define VPE_FMT_TYPE_OUTPUT (1 << 1)
64
65/* used as plane indices */
66#define VPE_MAX_PLANES 2
67#define VPE_LUMA 0
68#define VPE_CHROMA 1
69
70/* per m2m context info */
Archit Taneja585e6f02013-10-16 02:36:48 -030071#define VPE_MAX_SRC_BUFS 3 /* need 3 src fields to de-interlace */
72
Archit Taneja45719122013-10-16 02:36:47 -030073#define VPE_DEF_BUFS_PER_JOB 1 /* default one buffer per batch job */
74
75/*
76 * each VPE context can need up to 3 config desciptors, 7 input descriptors,
77 * 3 output descriptors, and 10 control descriptors
78 */
79#define VPE_DESC_LIST_SIZE (10 * VPDMA_DTD_DESC_SIZE + \
80 13 * VPDMA_CFD_CTD_DESC_SIZE)
81
82#define vpe_dbg(vpedev, fmt, arg...) \
83 dev_dbg((vpedev)->v4l2_dev.dev, fmt, ##arg)
84#define vpe_err(vpedev, fmt, arg...) \
85 dev_err((vpedev)->v4l2_dev.dev, fmt, ##arg)
86
87struct vpe_us_coeffs {
88 unsigned short anchor_fid0_c0;
89 unsigned short anchor_fid0_c1;
90 unsigned short anchor_fid0_c2;
91 unsigned short anchor_fid0_c3;
92 unsigned short interp_fid0_c0;
93 unsigned short interp_fid0_c1;
94 unsigned short interp_fid0_c2;
95 unsigned short interp_fid0_c3;
96 unsigned short anchor_fid1_c0;
97 unsigned short anchor_fid1_c1;
98 unsigned short anchor_fid1_c2;
99 unsigned short anchor_fid1_c3;
100 unsigned short interp_fid1_c0;
101 unsigned short interp_fid1_c1;
102 unsigned short interp_fid1_c2;
103 unsigned short interp_fid1_c3;
104};
105
106/*
107 * Default upsampler coefficients
108 */
109static const struct vpe_us_coeffs us_coeffs[] = {
110 {
111 /* Coefficients for progressive input */
112 0x00C8, 0x0348, 0x0018, 0x3FD8, 0x3FB8, 0x0378, 0x00E8, 0x3FE8,
113 0x00C8, 0x0348, 0x0018, 0x3FD8, 0x3FB8, 0x0378, 0x00E8, 0x3FE8,
114 },
Archit Taneja585e6f02013-10-16 02:36:48 -0300115 {
116 /* Coefficients for Top Field Interlaced input */
117 0x0051, 0x03D5, 0x3FE3, 0x3FF7, 0x3FB5, 0x02E9, 0x018F, 0x3FD3,
118 /* Coefficients for Bottom Field Interlaced input */
119 0x016B, 0x0247, 0x00B1, 0x3F9D, 0x3FCF, 0x03DB, 0x005D, 0x3FF9,
120 },
121};
122
123/*
124 * the following registers are for configuring some of the parameters of the
125 * motion and edge detection blocks inside DEI, these generally remain the same,
126 * these could be passed later via userspace if some one needs to tweak these.
127 */
128struct vpe_dei_regs {
129 unsigned long mdt_spacial_freq_thr_reg; /* VPE_DEI_REG2 */
130 unsigned long edi_config_reg; /* VPE_DEI_REG3 */
131 unsigned long edi_lut_reg0; /* VPE_DEI_REG4 */
132 unsigned long edi_lut_reg1; /* VPE_DEI_REG5 */
133 unsigned long edi_lut_reg2; /* VPE_DEI_REG6 */
134 unsigned long edi_lut_reg3; /* VPE_DEI_REG7 */
135};
136
137/*
138 * default expert DEI register values, unlikely to be modified.
139 */
140static const struct vpe_dei_regs dei_regs = {
141 0x020C0804u,
142 0x0118100Fu,
143 0x08040200u,
144 0x1010100Cu,
145 0x10101010u,
146 0x10101010u,
Archit Taneja45719122013-10-16 02:36:47 -0300147};
148
149/*
150 * The port_data structure contains per-port data.
151 */
152struct vpe_port_data {
153 enum vpdma_channel channel; /* VPDMA channel */
Archit Taneja585e6f02013-10-16 02:36:48 -0300154 u8 vb_index; /* input frame f, f-1, f-2 index */
Archit Taneja45719122013-10-16 02:36:47 -0300155 u8 vb_part; /* plane index for co-panar formats */
156};
157
158/*
159 * Define indices into the port_data tables
160 */
161#define VPE_PORT_LUMA1_IN 0
162#define VPE_PORT_CHROMA1_IN 1
Archit Taneja585e6f02013-10-16 02:36:48 -0300163#define VPE_PORT_LUMA2_IN 2
164#define VPE_PORT_CHROMA2_IN 3
165#define VPE_PORT_LUMA3_IN 4
166#define VPE_PORT_CHROMA3_IN 5
167#define VPE_PORT_MV_IN 6
168#define VPE_PORT_MV_OUT 7
Archit Taneja45719122013-10-16 02:36:47 -0300169#define VPE_PORT_LUMA_OUT 8
170#define VPE_PORT_CHROMA_OUT 9
171#define VPE_PORT_RGB_OUT 10
172
173static const struct vpe_port_data port_data[11] = {
174 [VPE_PORT_LUMA1_IN] = {
175 .channel = VPE_CHAN_LUMA1_IN,
Archit Taneja585e6f02013-10-16 02:36:48 -0300176 .vb_index = 0,
Archit Taneja45719122013-10-16 02:36:47 -0300177 .vb_part = VPE_LUMA,
178 },
179 [VPE_PORT_CHROMA1_IN] = {
180 .channel = VPE_CHAN_CHROMA1_IN,
Archit Taneja585e6f02013-10-16 02:36:48 -0300181 .vb_index = 0,
Archit Taneja45719122013-10-16 02:36:47 -0300182 .vb_part = VPE_CHROMA,
183 },
Archit Taneja585e6f02013-10-16 02:36:48 -0300184 [VPE_PORT_LUMA2_IN] = {
185 .channel = VPE_CHAN_LUMA2_IN,
186 .vb_index = 1,
187 .vb_part = VPE_LUMA,
188 },
189 [VPE_PORT_CHROMA2_IN] = {
190 .channel = VPE_CHAN_CHROMA2_IN,
191 .vb_index = 1,
192 .vb_part = VPE_CHROMA,
193 },
194 [VPE_PORT_LUMA3_IN] = {
195 .channel = VPE_CHAN_LUMA3_IN,
196 .vb_index = 2,
197 .vb_part = VPE_LUMA,
198 },
199 [VPE_PORT_CHROMA3_IN] = {
200 .channel = VPE_CHAN_CHROMA3_IN,
201 .vb_index = 2,
202 .vb_part = VPE_CHROMA,
203 },
204 [VPE_PORT_MV_IN] = {
205 .channel = VPE_CHAN_MV_IN,
206 },
207 [VPE_PORT_MV_OUT] = {
208 .channel = VPE_CHAN_MV_OUT,
209 },
Archit Taneja45719122013-10-16 02:36:47 -0300210 [VPE_PORT_LUMA_OUT] = {
211 .channel = VPE_CHAN_LUMA_OUT,
212 .vb_part = VPE_LUMA,
213 },
214 [VPE_PORT_CHROMA_OUT] = {
215 .channel = VPE_CHAN_CHROMA_OUT,
216 .vb_part = VPE_CHROMA,
217 },
218 [VPE_PORT_RGB_OUT] = {
219 .channel = VPE_CHAN_RGB_OUT,
220 .vb_part = VPE_LUMA,
221 },
222};
223
224
225/* driver info for each of the supported video formats */
226struct vpe_fmt {
227 char *name; /* human-readable name */
228 u32 fourcc; /* standard format identifier */
229 u8 types; /* CAPTURE and/or OUTPUT */
230 u8 coplanar; /* set for unpacked Luma and Chroma */
231 /* vpdma format info for each plane */
232 struct vpdma_data_format const *vpdma_fmt[VPE_MAX_PLANES];
233};
234
235static struct vpe_fmt vpe_formats[] = {
236 {
237 .name = "YUV 422 co-planar",
238 .fourcc = V4L2_PIX_FMT_NV16,
239 .types = VPE_FMT_TYPE_CAPTURE | VPE_FMT_TYPE_OUTPUT,
240 .coplanar = 1,
241 .vpdma_fmt = { &vpdma_yuv_fmts[VPDMA_DATA_FMT_Y444],
242 &vpdma_yuv_fmts[VPDMA_DATA_FMT_C444],
243 },
244 },
245 {
246 .name = "YUV 420 co-planar",
247 .fourcc = V4L2_PIX_FMT_NV12,
248 .types = VPE_FMT_TYPE_CAPTURE | VPE_FMT_TYPE_OUTPUT,
249 .coplanar = 1,
250 .vpdma_fmt = { &vpdma_yuv_fmts[VPDMA_DATA_FMT_Y420],
251 &vpdma_yuv_fmts[VPDMA_DATA_FMT_C420],
252 },
253 },
254 {
255 .name = "YUYV 422 packed",
256 .fourcc = V4L2_PIX_FMT_YUYV,
257 .types = VPE_FMT_TYPE_CAPTURE | VPE_FMT_TYPE_OUTPUT,
258 .coplanar = 0,
259 .vpdma_fmt = { &vpdma_yuv_fmts[VPDMA_DATA_FMT_YC422],
260 },
261 },
262 {
263 .name = "UYVY 422 packed",
264 .fourcc = V4L2_PIX_FMT_UYVY,
265 .types = VPE_FMT_TYPE_CAPTURE | VPE_FMT_TYPE_OUTPUT,
266 .coplanar = 0,
267 .vpdma_fmt = { &vpdma_yuv_fmts[VPDMA_DATA_FMT_CY422],
268 },
269 },
Archit Taneja30496792013-12-12 05:36:03 -0300270 {
271 .name = "RGB888 packed",
272 .fourcc = V4L2_PIX_FMT_RGB24,
273 .types = VPE_FMT_TYPE_CAPTURE,
274 .coplanar = 0,
275 .vpdma_fmt = { &vpdma_rgb_fmts[VPDMA_DATA_FMT_RGB24],
276 },
277 },
278 {
279 .name = "ARGB32",
280 .fourcc = V4L2_PIX_FMT_RGB32,
281 .types = VPE_FMT_TYPE_CAPTURE,
282 .coplanar = 0,
283 .vpdma_fmt = { &vpdma_rgb_fmts[VPDMA_DATA_FMT_ARGB32],
284 },
285 },
286 {
287 .name = "BGR888 packed",
288 .fourcc = V4L2_PIX_FMT_BGR24,
289 .types = VPE_FMT_TYPE_CAPTURE,
290 .coplanar = 0,
291 .vpdma_fmt = { &vpdma_rgb_fmts[VPDMA_DATA_FMT_BGR24],
292 },
293 },
294 {
295 .name = "ABGR32",
296 .fourcc = V4L2_PIX_FMT_BGR32,
297 .types = VPE_FMT_TYPE_CAPTURE,
298 .coplanar = 0,
299 .vpdma_fmt = { &vpdma_rgb_fmts[VPDMA_DATA_FMT_ABGR32],
300 },
301 },
Archit Taneja45719122013-10-16 02:36:47 -0300302};
303
304/*
305 * per-queue, driver-specific private data.
306 * there is one source queue and one destination queue for each m2m context.
307 */
308struct vpe_q_data {
309 unsigned int width; /* frame width */
310 unsigned int height; /* frame height */
311 unsigned int bytesperline[VPE_MAX_PLANES]; /* bytes per line in memory */
312 enum v4l2_colorspace colorspace;
Archit Taneja585e6f02013-10-16 02:36:48 -0300313 enum v4l2_field field; /* supported field value */
Archit Taneja45719122013-10-16 02:36:47 -0300314 unsigned int flags;
315 unsigned int sizeimage[VPE_MAX_PLANES]; /* image size in memory */
316 struct v4l2_rect c_rect; /* crop/compose rectangle */
317 struct vpe_fmt *fmt; /* format info */
318};
319
320/* vpe_q_data flag bits */
321#define Q_DATA_FRAME_1D (1 << 0)
322#define Q_DATA_MODE_TILED (1 << 1)
Archit Taneja585e6f02013-10-16 02:36:48 -0300323#define Q_DATA_INTERLACED (1 << 2)
Archit Taneja45719122013-10-16 02:36:47 -0300324
325enum {
326 Q_DATA_SRC = 0,
327 Q_DATA_DST = 1,
328};
329
330/* find our format description corresponding to the passed v4l2_format */
331static struct vpe_fmt *find_format(struct v4l2_format *f)
332{
333 struct vpe_fmt *fmt;
334 unsigned int k;
335
336 for (k = 0; k < ARRAY_SIZE(vpe_formats); k++) {
337 fmt = &vpe_formats[k];
338 if (fmt->fourcc == f->fmt.pix.pixelformat)
339 return fmt;
340 }
341
342 return NULL;
343}
344
345/*
346 * there is one vpe_dev structure in the driver, it is shared by
347 * all instances.
348 */
349struct vpe_dev {
350 struct v4l2_device v4l2_dev;
351 struct video_device vfd;
352 struct v4l2_m2m_dev *m2m_dev;
353
354 atomic_t num_instances; /* count of driver instances */
355 dma_addr_t loaded_mmrs; /* shadow mmrs in device */
356 struct mutex dev_mutex;
357 spinlock_t lock;
358
359 int irq;
360 void __iomem *base;
Archit Taneja44687b22013-12-12 05:35:57 -0300361 struct resource *res;
Archit Taneja45719122013-10-16 02:36:47 -0300362
363 struct vb2_alloc_ctx *alloc_ctx;
364 struct vpdma_data *vpdma; /* vpdma data handle */
Archit Taneja44687b22013-12-12 05:35:57 -0300365 struct sc_data *sc; /* scaler data handle */
Archit Taneja69480822013-12-12 05:36:01 -0300366 struct csc_data *csc; /* csc data handle */
Archit Taneja45719122013-10-16 02:36:47 -0300367};
368
369/*
370 * There is one vpe_ctx structure for each m2m context.
371 */
372struct vpe_ctx {
373 struct v4l2_fh fh;
374 struct vpe_dev *dev;
375 struct v4l2_m2m_ctx *m2m_ctx;
376 struct v4l2_ctrl_handler hdl;
377
Archit Taneja585e6f02013-10-16 02:36:48 -0300378 unsigned int field; /* current field */
Archit Taneja45719122013-10-16 02:36:47 -0300379 unsigned int sequence; /* current frame/field seq */
380 unsigned int aborting; /* abort after next irq */
381
382 unsigned int bufs_per_job; /* input buffers per batch */
383 unsigned int bufs_completed; /* bufs done in this batch */
384
385 struct vpe_q_data q_data[2]; /* src & dst queue data */
Archit Taneja585e6f02013-10-16 02:36:48 -0300386 struct vb2_buffer *src_vbs[VPE_MAX_SRC_BUFS];
Archit Taneja45719122013-10-16 02:36:47 -0300387 struct vb2_buffer *dst_vb;
388
Archit Taneja585e6f02013-10-16 02:36:48 -0300389 dma_addr_t mv_buf_dma[2]; /* dma addrs of motion vector in/out bufs */
390 void *mv_buf[2]; /* virtual addrs of motion vector bufs */
391 size_t mv_buf_size; /* current motion vector buffer size */
Archit Taneja45719122013-10-16 02:36:47 -0300392 struct vpdma_buf mmr_adb; /* shadow reg addr/data block */
Archit Taneja773f0652013-12-12 05:35:59 -0300393 struct vpdma_buf sc_coeff_h; /* h coeff buffer */
394 struct vpdma_buf sc_coeff_v; /* v coeff buffer */
Archit Taneja45719122013-10-16 02:36:47 -0300395 struct vpdma_desc_list desc_list; /* DMA descriptor list */
396
Archit Taneja585e6f02013-10-16 02:36:48 -0300397 bool deinterlacing; /* using de-interlacer */
Archit Taneja45719122013-10-16 02:36:47 -0300398 bool load_mmrs; /* have new shadow reg values */
Archit Taneja585e6f02013-10-16 02:36:48 -0300399
400 unsigned int src_mv_buf_selector;
Archit Taneja45719122013-10-16 02:36:47 -0300401};
402
403
404/*
405 * M2M devices get 2 queues.
406 * Return the queue given the type.
407 */
408static struct vpe_q_data *get_q_data(struct vpe_ctx *ctx,
409 enum v4l2_buf_type type)
410{
411 switch (type) {
412 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
413 return &ctx->q_data[Q_DATA_SRC];
414 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
415 return &ctx->q_data[Q_DATA_DST];
416 default:
417 BUG();
418 }
419 return NULL;
420}
421
422static u32 read_reg(struct vpe_dev *dev, int offset)
423{
424 return ioread32(dev->base + offset);
425}
426
427static void write_reg(struct vpe_dev *dev, int offset, u32 value)
428{
429 iowrite32(value, dev->base + offset);
430}
431
432/* register field read/write helpers */
433static int get_field(u32 value, u32 mask, int shift)
434{
435 return (value & (mask << shift)) >> shift;
436}
437
438static int read_field_reg(struct vpe_dev *dev, int offset, u32 mask, int shift)
439{
440 return get_field(read_reg(dev, offset), mask, shift);
441}
442
443static void write_field(u32 *valp, u32 field, u32 mask, int shift)
444{
445 u32 val = *valp;
446
447 val &= ~(mask << shift);
448 val |= (field & mask) << shift;
449 *valp = val;
450}
451
452static void write_field_reg(struct vpe_dev *dev, int offset, u32 field,
453 u32 mask, int shift)
454{
455 u32 val = read_reg(dev, offset);
456
457 write_field(&val, field, mask, shift);
458
459 write_reg(dev, offset, val);
460}
461
462/*
463 * DMA address/data block for the shadow registers
464 */
465struct vpe_mmr_adb {
466 struct vpdma_adb_hdr out_fmt_hdr;
467 u32 out_fmt_reg[1];
468 u32 out_fmt_pad[3];
469 struct vpdma_adb_hdr us1_hdr;
470 u32 us1_regs[8];
471 struct vpdma_adb_hdr us2_hdr;
472 u32 us2_regs[8];
473 struct vpdma_adb_hdr us3_hdr;
474 u32 us3_regs[8];
475 struct vpdma_adb_hdr dei_hdr;
Archit Taneja585e6f02013-10-16 02:36:48 -0300476 u32 dei_regs[8];
Archit Tanejabbee8b32013-12-12 05:36:00 -0300477 struct vpdma_adb_hdr sc_hdr0;
478 u32 sc_regs0[7];
479 u32 sc_pad0[1];
480 struct vpdma_adb_hdr sc_hdr8;
481 u32 sc_regs8[6];
482 u32 sc_pad8[2];
483 struct vpdma_adb_hdr sc_hdr17;
484 u32 sc_regs17[9];
485 u32 sc_pad17[3];
Archit Taneja45719122013-10-16 02:36:47 -0300486 struct vpdma_adb_hdr csc_hdr;
487 u32 csc_regs[6];
488 u32 csc_pad[2];
489};
490
Archit Taneja44687b22013-12-12 05:35:57 -0300491#define GET_OFFSET_TOP(ctx, obj, reg) \
492 ((obj)->res->start - ctx->dev->res->start + reg)
493
Archit Taneja45719122013-10-16 02:36:47 -0300494#define VPE_SET_MMR_ADB_HDR(ctx, hdr, regs, offset_a) \
495 VPDMA_SET_MMR_ADB_HDR(ctx->mmr_adb, vpe_mmr_adb, hdr, regs, offset_a)
496/*
497 * Set the headers for all of the address/data block structures.
498 */
499static void init_adb_hdrs(struct vpe_ctx *ctx)
500{
501 VPE_SET_MMR_ADB_HDR(ctx, out_fmt_hdr, out_fmt_reg, VPE_CLK_FORMAT_SELECT);
502 VPE_SET_MMR_ADB_HDR(ctx, us1_hdr, us1_regs, VPE_US1_R0);
503 VPE_SET_MMR_ADB_HDR(ctx, us2_hdr, us2_regs, VPE_US2_R0);
504 VPE_SET_MMR_ADB_HDR(ctx, us3_hdr, us3_regs, VPE_US3_R0);
505 VPE_SET_MMR_ADB_HDR(ctx, dei_hdr, dei_regs, VPE_DEI_FRAME_SIZE);
Archit Tanejabbee8b32013-12-12 05:36:00 -0300506 VPE_SET_MMR_ADB_HDR(ctx, sc_hdr0, sc_regs0,
Archit Taneja44687b22013-12-12 05:35:57 -0300507 GET_OFFSET_TOP(ctx, ctx->dev->sc, CFG_SC0));
Archit Tanejabbee8b32013-12-12 05:36:00 -0300508 VPE_SET_MMR_ADB_HDR(ctx, sc_hdr8, sc_regs8,
509 GET_OFFSET_TOP(ctx, ctx->dev->sc, CFG_SC8));
510 VPE_SET_MMR_ADB_HDR(ctx, sc_hdr17, sc_regs17,
511 GET_OFFSET_TOP(ctx, ctx->dev->sc, CFG_SC17));
Archit Taneja69480822013-12-12 05:36:01 -0300512 VPE_SET_MMR_ADB_HDR(ctx, csc_hdr, csc_regs,
513 GET_OFFSET_TOP(ctx, ctx->dev->csc, CSC_CSC00));
Archit Taneja45719122013-10-16 02:36:47 -0300514};
515
516/*
Archit Taneja585e6f02013-10-16 02:36:48 -0300517 * Allocate or re-allocate the motion vector DMA buffers
518 * There are two buffers, one for input and one for output.
519 * However, the roles are reversed after each field is processed.
520 * In other words, after each field is processed, the previous
521 * output (dst) MV buffer becomes the new input (src) MV buffer.
522 */
523static int realloc_mv_buffers(struct vpe_ctx *ctx, size_t size)
524{
525 struct device *dev = ctx->dev->v4l2_dev.dev;
526
527 if (ctx->mv_buf_size == size)
528 return 0;
529
530 if (ctx->mv_buf[0])
531 dma_free_coherent(dev, ctx->mv_buf_size, ctx->mv_buf[0],
532 ctx->mv_buf_dma[0]);
533
534 if (ctx->mv_buf[1])
535 dma_free_coherent(dev, ctx->mv_buf_size, ctx->mv_buf[1],
536 ctx->mv_buf_dma[1]);
537
538 if (size == 0)
539 return 0;
540
541 ctx->mv_buf[0] = dma_alloc_coherent(dev, size, &ctx->mv_buf_dma[0],
542 GFP_KERNEL);
543 if (!ctx->mv_buf[0]) {
544 vpe_err(ctx->dev, "failed to allocate motion vector buffer\n");
545 return -ENOMEM;
546 }
547
548 ctx->mv_buf[1] = dma_alloc_coherent(dev, size, &ctx->mv_buf_dma[1],
549 GFP_KERNEL);
550 if (!ctx->mv_buf[1]) {
551 vpe_err(ctx->dev, "failed to allocate motion vector buffer\n");
552 dma_free_coherent(dev, size, ctx->mv_buf[0],
553 ctx->mv_buf_dma[0]);
554
555 return -ENOMEM;
556 }
557
558 ctx->mv_buf_size = size;
559 ctx->src_mv_buf_selector = 0;
560
561 return 0;
562}
563
564static void free_mv_buffers(struct vpe_ctx *ctx)
565{
566 realloc_mv_buffers(ctx, 0);
567}
568
569/*
570 * While de-interlacing, we keep the two most recent input buffers
571 * around. This function frees those two buffers when we have
572 * finished processing the current stream.
573 */
574static void free_vbs(struct vpe_ctx *ctx)
575{
576 struct vpe_dev *dev = ctx->dev;
577 unsigned long flags;
578
579 if (ctx->src_vbs[2] == NULL)
580 return;
581
582 spin_lock_irqsave(&dev->lock, flags);
583 if (ctx->src_vbs[2]) {
584 v4l2_m2m_buf_done(ctx->src_vbs[2], VB2_BUF_STATE_DONE);
585 v4l2_m2m_buf_done(ctx->src_vbs[1], VB2_BUF_STATE_DONE);
586 }
587 spin_unlock_irqrestore(&dev->lock, flags);
588}
589
590/*
Archit Taneja45719122013-10-16 02:36:47 -0300591 * Enable or disable the VPE clocks
592 */
593static void vpe_set_clock_enable(struct vpe_dev *dev, bool on)
594{
595 u32 val = 0;
596
597 if (on)
598 val = VPE_DATA_PATH_CLK_ENABLE | VPE_VPEDMA_CLK_ENABLE;
599 write_reg(dev, VPE_CLK_ENABLE, val);
600}
601
602static void vpe_top_reset(struct vpe_dev *dev)
603{
604
605 write_field_reg(dev, VPE_CLK_RESET, 1, VPE_DATA_PATH_CLK_RESET_MASK,
606 VPE_DATA_PATH_CLK_RESET_SHIFT);
607
608 usleep_range(100, 150);
609
610 write_field_reg(dev, VPE_CLK_RESET, 0, VPE_DATA_PATH_CLK_RESET_MASK,
611 VPE_DATA_PATH_CLK_RESET_SHIFT);
612}
613
614static void vpe_top_vpdma_reset(struct vpe_dev *dev)
615{
616 write_field_reg(dev, VPE_CLK_RESET, 1, VPE_VPDMA_CLK_RESET_MASK,
617 VPE_VPDMA_CLK_RESET_SHIFT);
618
619 usleep_range(100, 150);
620
621 write_field_reg(dev, VPE_CLK_RESET, 0, VPE_VPDMA_CLK_RESET_MASK,
622 VPE_VPDMA_CLK_RESET_SHIFT);
623}
624
625/*
626 * Load the correct of upsampler coefficients into the shadow MMRs
627 */
628static void set_us_coefficients(struct vpe_ctx *ctx)
629{
630 struct vpe_mmr_adb *mmr_adb = ctx->mmr_adb.addr;
Archit Taneja585e6f02013-10-16 02:36:48 -0300631 struct vpe_q_data *s_q_data = &ctx->q_data[Q_DATA_SRC];
Archit Taneja45719122013-10-16 02:36:47 -0300632 u32 *us1_reg = &mmr_adb->us1_regs[0];
633 u32 *us2_reg = &mmr_adb->us2_regs[0];
634 u32 *us3_reg = &mmr_adb->us3_regs[0];
635 const unsigned short *cp, *end_cp;
636
637 cp = &us_coeffs[0].anchor_fid0_c0;
638
Archit Taneja585e6f02013-10-16 02:36:48 -0300639 if (s_q_data->flags & Q_DATA_INTERLACED) /* interlaced */
640 cp += sizeof(us_coeffs[0]) / sizeof(*cp);
641
Archit Taneja45719122013-10-16 02:36:47 -0300642 end_cp = cp + sizeof(us_coeffs[0]) / sizeof(*cp);
643
644 while (cp < end_cp) {
645 write_field(us1_reg, *cp++, VPE_US_C0_MASK, VPE_US_C0_SHIFT);
646 write_field(us1_reg, *cp++, VPE_US_C1_MASK, VPE_US_C1_SHIFT);
647 *us2_reg++ = *us1_reg;
648 *us3_reg++ = *us1_reg++;
649 }
650 ctx->load_mmrs = true;
651}
652
653/*
654 * Set the upsampler config mode and the VPDMA line mode in the shadow MMRs.
655 */
656static void set_cfg_and_line_modes(struct vpe_ctx *ctx)
657{
658 struct vpe_fmt *fmt = ctx->q_data[Q_DATA_SRC].fmt;
659 struct vpe_mmr_adb *mmr_adb = ctx->mmr_adb.addr;
660 u32 *us1_reg0 = &mmr_adb->us1_regs[0];
661 u32 *us2_reg0 = &mmr_adb->us2_regs[0];
662 u32 *us3_reg0 = &mmr_adb->us3_regs[0];
663 int line_mode = 1;
664 int cfg_mode = 1;
665
666 /*
667 * Cfg Mode 0: YUV420 source, enable upsampler, DEI is de-interlacing.
668 * Cfg Mode 1: YUV422 source, disable upsampler, DEI is de-interlacing.
669 */
670
671 if (fmt->fourcc == V4L2_PIX_FMT_NV12) {
672 cfg_mode = 0;
673 line_mode = 0; /* double lines to line buffer */
674 }
675
676 write_field(us1_reg0, cfg_mode, VPE_US_MODE_MASK, VPE_US_MODE_SHIFT);
677 write_field(us2_reg0, cfg_mode, VPE_US_MODE_MASK, VPE_US_MODE_SHIFT);
678 write_field(us3_reg0, cfg_mode, VPE_US_MODE_MASK, VPE_US_MODE_SHIFT);
679
680 /* regs for now */
681 vpdma_set_line_mode(ctx->dev->vpdma, line_mode, VPE_CHAN_CHROMA1_IN);
Archit Taneja585e6f02013-10-16 02:36:48 -0300682 vpdma_set_line_mode(ctx->dev->vpdma, line_mode, VPE_CHAN_CHROMA2_IN);
683 vpdma_set_line_mode(ctx->dev->vpdma, line_mode, VPE_CHAN_CHROMA3_IN);
Archit Taneja45719122013-10-16 02:36:47 -0300684
685 /* frame start for input luma */
686 vpdma_set_frame_start_event(ctx->dev->vpdma, VPDMA_FSEVENT_CHANNEL_ACTIVE,
687 VPE_CHAN_LUMA1_IN);
Archit Taneja585e6f02013-10-16 02:36:48 -0300688 vpdma_set_frame_start_event(ctx->dev->vpdma, VPDMA_FSEVENT_CHANNEL_ACTIVE,
689 VPE_CHAN_LUMA2_IN);
690 vpdma_set_frame_start_event(ctx->dev->vpdma, VPDMA_FSEVENT_CHANNEL_ACTIVE,
691 VPE_CHAN_LUMA3_IN);
Archit Taneja45719122013-10-16 02:36:47 -0300692
693 /* frame start for input chroma */
694 vpdma_set_frame_start_event(ctx->dev->vpdma, VPDMA_FSEVENT_CHANNEL_ACTIVE,
695 VPE_CHAN_CHROMA1_IN);
Archit Taneja585e6f02013-10-16 02:36:48 -0300696 vpdma_set_frame_start_event(ctx->dev->vpdma, VPDMA_FSEVENT_CHANNEL_ACTIVE,
697 VPE_CHAN_CHROMA2_IN);
698 vpdma_set_frame_start_event(ctx->dev->vpdma, VPDMA_FSEVENT_CHANNEL_ACTIVE,
699 VPE_CHAN_CHROMA3_IN);
700
701 /* frame start for MV in client */
702 vpdma_set_frame_start_event(ctx->dev->vpdma, VPDMA_FSEVENT_CHANNEL_ACTIVE,
703 VPE_CHAN_MV_IN);
Archit Taneja45719122013-10-16 02:36:47 -0300704
705 ctx->load_mmrs = true;
706}
707
708/*
709 * Set the shadow registers that are modified when the source
710 * format changes.
711 */
712static void set_src_registers(struct vpe_ctx *ctx)
713{
714 set_us_coefficients(ctx);
715}
716
717/*
718 * Set the shadow registers that are modified when the destination
719 * format changes.
720 */
721static void set_dst_registers(struct vpe_ctx *ctx)
722{
723 struct vpe_mmr_adb *mmr_adb = ctx->mmr_adb.addr;
Archit Taneja30496792013-12-12 05:36:03 -0300724 enum v4l2_colorspace clrspc = ctx->q_data[Q_DATA_DST].colorspace;
Archit Taneja45719122013-10-16 02:36:47 -0300725 struct vpe_fmt *fmt = ctx->q_data[Q_DATA_DST].fmt;
726 u32 val = 0;
727
Archit Taneja30496792013-12-12 05:36:03 -0300728 if (clrspc == V4L2_COLORSPACE_SRGB)
729 val |= VPE_RGB_OUT_SELECT;
Archit Taneja45719122013-10-16 02:36:47 -0300730 else if (fmt->fourcc == V4L2_PIX_FMT_NV16)
731 val |= VPE_COLOR_SEPARATE_422;
732
Archit Taneja30496792013-12-12 05:36:03 -0300733 /*
734 * the source of CHR_DS and CSC is always the scaler, irrespective of
735 * whether it's used or not
736 */
737 val |= VPE_DS_SRC_DEI_SCALER | VPE_CSC_SRC_DEI_SCALER;
Archit Taneja45719122013-10-16 02:36:47 -0300738
739 if (fmt->fourcc != V4L2_PIX_FMT_NV12)
740 val |= VPE_DS_BYPASS;
741
742 mmr_adb->out_fmt_reg[0] = val;
743
744 ctx->load_mmrs = true;
745}
746
747/*
748 * Set the de-interlacer shadow register values
749 */
Archit Taneja585e6f02013-10-16 02:36:48 -0300750static void set_dei_regs(struct vpe_ctx *ctx)
Archit Taneja45719122013-10-16 02:36:47 -0300751{
752 struct vpe_mmr_adb *mmr_adb = ctx->mmr_adb.addr;
753 struct vpe_q_data *s_q_data = &ctx->q_data[Q_DATA_SRC];
754 unsigned int src_h = s_q_data->c_rect.height;
755 unsigned int src_w = s_q_data->c_rect.width;
756 u32 *dei_mmr0 = &mmr_adb->dei_regs[0];
Archit Taneja585e6f02013-10-16 02:36:48 -0300757 bool deinterlace = true;
Archit Taneja45719122013-10-16 02:36:47 -0300758 u32 val = 0;
759
760 /*
761 * according to TRM, we should set DEI in progressive bypass mode when
762 * the input content is progressive, however, DEI is bypassed correctly
763 * for both progressive and interlace content in interlace bypass mode.
764 * It has been recommended not to use progressive bypass mode.
765 */
Archit Taneja585e6f02013-10-16 02:36:48 -0300766 if ((!ctx->deinterlacing && (s_q_data->flags & Q_DATA_INTERLACED)) ||
767 !(s_q_data->flags & Q_DATA_INTERLACED)) {
768 deinterlace = false;
769 val = VPE_DEI_INTERLACE_BYPASS;
770 }
771
772 src_h = deinterlace ? src_h * 2 : src_h;
Archit Taneja45719122013-10-16 02:36:47 -0300773
774 val |= (src_h << VPE_DEI_HEIGHT_SHIFT) |
775 (src_w << VPE_DEI_WIDTH_SHIFT) |
776 VPE_DEI_FIELD_FLUSH;
777
778 *dei_mmr0 = val;
779
780 ctx->load_mmrs = true;
781}
782
Archit Taneja585e6f02013-10-16 02:36:48 -0300783static void set_dei_shadow_registers(struct vpe_ctx *ctx)
784{
785 struct vpe_mmr_adb *mmr_adb = ctx->mmr_adb.addr;
786 u32 *dei_mmr = &mmr_adb->dei_regs[0];
787 const struct vpe_dei_regs *cur = &dei_regs;
788
789 dei_mmr[2] = cur->mdt_spacial_freq_thr_reg;
790 dei_mmr[3] = cur->edi_config_reg;
791 dei_mmr[4] = cur->edi_lut_reg0;
792 dei_mmr[5] = cur->edi_lut_reg1;
793 dei_mmr[6] = cur->edi_lut_reg2;
794 dei_mmr[7] = cur->edi_lut_reg3;
795
796 ctx->load_mmrs = true;
797}
798
Archit Taneja45719122013-10-16 02:36:47 -0300799/*
800 * Set the shadow registers whose values are modified when either the
801 * source or destination format is changed.
802 */
803static int set_srcdst_params(struct vpe_ctx *ctx)
804{
Archit Taneja585e6f02013-10-16 02:36:48 -0300805 struct vpe_q_data *s_q_data = &ctx->q_data[Q_DATA_SRC];
806 struct vpe_q_data *d_q_data = &ctx->q_data[Q_DATA_DST];
Archit Taneja44687b22013-12-12 05:35:57 -0300807 struct vpe_mmr_adb *mmr_adb = ctx->mmr_adb.addr;
Archit Taneja773f0652013-12-12 05:35:59 -0300808 unsigned int src_w = s_q_data->c_rect.width;
809 unsigned int src_h = s_q_data->c_rect.height;
810 unsigned int dst_w = d_q_data->c_rect.width;
811 unsigned int dst_h = d_q_data->c_rect.height;
Archit Taneja585e6f02013-10-16 02:36:48 -0300812 size_t mv_buf_size;
813 int ret;
814
Archit Taneja45719122013-10-16 02:36:47 -0300815 ctx->sequence = 0;
Archit Taneja585e6f02013-10-16 02:36:48 -0300816 ctx->field = V4L2_FIELD_TOP;
817
818 if ((s_q_data->flags & Q_DATA_INTERLACED) &&
819 !(d_q_data->flags & Q_DATA_INTERLACED)) {
Archit Tanejaa51cd8f2013-12-03 08:51:13 -0300820 int bytes_per_line;
Archit Taneja585e6f02013-10-16 02:36:48 -0300821 const struct vpdma_data_format *mv =
822 &vpdma_misc_fmts[VPDMA_DATA_FMT_MV];
823
Archit Tanejaa51cd8f2013-12-03 08:51:13 -0300824 /*
825 * we make sure that the source image has a 16 byte aligned
826 * stride, we need to do the same for the motion vector buffer
827 * by aligning it's stride to the next 16 byte boundry. this
828 * extra space will not be used by the de-interlacer, but will
829 * ensure that vpdma operates correctly
830 */
831 bytes_per_line = ALIGN((s_q_data->width * mv->depth) >> 3,
832 VPDMA_STRIDE_ALIGN);
833 mv_buf_size = bytes_per_line * s_q_data->height;
Archit Taneja773f0652013-12-12 05:35:59 -0300834
835 ctx->deinterlacing = 1;
836 src_h <<= 1;
Archit Taneja585e6f02013-10-16 02:36:48 -0300837 } else {
838 ctx->deinterlacing = 0;
839 mv_buf_size = 0;
840 }
841
842 free_vbs(ctx);
843
844 ret = realloc_mv_buffers(ctx, mv_buf_size);
845 if (ret)
846 return ret;
Archit Taneja45719122013-10-16 02:36:47 -0300847
848 set_cfg_and_line_modes(ctx);
Archit Taneja585e6f02013-10-16 02:36:48 -0300849 set_dei_regs(ctx);
Archit Taneja69480822013-12-12 05:36:01 -0300850
Archit Taneja30496792013-12-12 05:36:03 -0300851 csc_set_coeff(ctx->dev->csc, &mmr_adb->csc_regs[0],
852 s_q_data->colorspace, d_q_data->colorspace);
Archit Tanejabbee8b32013-12-12 05:36:00 -0300853
Archit Taneja773f0652013-12-12 05:35:59 -0300854 sc_set_hs_coeffs(ctx->dev->sc, ctx->sc_coeff_h.addr, src_w, dst_w);
855 sc_set_vs_coeffs(ctx->dev->sc, ctx->sc_coeff_v.addr, src_h, dst_h);
Archit Tanejabbee8b32013-12-12 05:36:00 -0300856
857 sc_config_scaler(ctx->dev->sc, &mmr_adb->sc_regs0[0],
858 &mmr_adb->sc_regs8[0], &mmr_adb->sc_regs17[0],
859 src_w, src_h, dst_w, dst_h);
Archit Taneja45719122013-10-16 02:36:47 -0300860
861 return 0;
862}
863
864/*
865 * Return the vpe_ctx structure for a given struct file
866 */
867static struct vpe_ctx *file2ctx(struct file *file)
868{
869 return container_of(file->private_data, struct vpe_ctx, fh);
870}
871
872/*
873 * mem2mem callbacks
874 */
875
876/**
877 * job_ready() - check whether an instance is ready to be scheduled to run
878 */
879static int job_ready(void *priv)
880{
881 struct vpe_ctx *ctx = priv;
882 int needed = ctx->bufs_per_job;
883
Archit Taneja585e6f02013-10-16 02:36:48 -0300884 if (ctx->deinterlacing && ctx->src_vbs[2] == NULL)
885 needed += 2; /* need additional two most recent fields */
886
Archit Taneja45719122013-10-16 02:36:47 -0300887 if (v4l2_m2m_num_src_bufs_ready(ctx->m2m_ctx) < needed)
888 return 0;
889
Archit Tanejadb476162014-01-15 08:31:51 -0300890 if (v4l2_m2m_num_dst_bufs_ready(ctx->m2m_ctx) < needed)
891 return 0;
892
Archit Taneja45719122013-10-16 02:36:47 -0300893 return 1;
894}
895
896static void job_abort(void *priv)
897{
898 struct vpe_ctx *ctx = priv;
899
900 /* Will cancel the transaction in the next interrupt handler */
901 ctx->aborting = 1;
902}
903
904/*
905 * Lock access to the device
906 */
907static void vpe_lock(void *priv)
908{
909 struct vpe_ctx *ctx = priv;
910 struct vpe_dev *dev = ctx->dev;
911 mutex_lock(&dev->dev_mutex);
912}
913
914static void vpe_unlock(void *priv)
915{
916 struct vpe_ctx *ctx = priv;
917 struct vpe_dev *dev = ctx->dev;
918 mutex_unlock(&dev->dev_mutex);
919}
920
921static void vpe_dump_regs(struct vpe_dev *dev)
922{
923#define DUMPREG(r) vpe_dbg(dev, "%-35s %08x\n", #r, read_reg(dev, VPE_##r))
924
925 vpe_dbg(dev, "VPE Registers:\n");
926
927 DUMPREG(PID);
928 DUMPREG(SYSCONFIG);
929 DUMPREG(INT0_STATUS0_RAW);
930 DUMPREG(INT0_STATUS0);
931 DUMPREG(INT0_ENABLE0);
932 DUMPREG(INT0_STATUS1_RAW);
933 DUMPREG(INT0_STATUS1);
934 DUMPREG(INT0_ENABLE1);
935 DUMPREG(CLK_ENABLE);
936 DUMPREG(CLK_RESET);
937 DUMPREG(CLK_FORMAT_SELECT);
938 DUMPREG(CLK_RANGE_MAP);
939 DUMPREG(US1_R0);
940 DUMPREG(US1_R1);
941 DUMPREG(US1_R2);
942 DUMPREG(US1_R3);
943 DUMPREG(US1_R4);
944 DUMPREG(US1_R5);
945 DUMPREG(US1_R6);
946 DUMPREG(US1_R7);
947 DUMPREG(US2_R0);
948 DUMPREG(US2_R1);
949 DUMPREG(US2_R2);
950 DUMPREG(US2_R3);
951 DUMPREG(US2_R4);
952 DUMPREG(US2_R5);
953 DUMPREG(US2_R6);
954 DUMPREG(US2_R7);
955 DUMPREG(US3_R0);
956 DUMPREG(US3_R1);
957 DUMPREG(US3_R2);
958 DUMPREG(US3_R3);
959 DUMPREG(US3_R4);
960 DUMPREG(US3_R5);
961 DUMPREG(US3_R6);
962 DUMPREG(US3_R7);
963 DUMPREG(DEI_FRAME_SIZE);
964 DUMPREG(MDT_BYPASS);
965 DUMPREG(MDT_SF_THRESHOLD);
966 DUMPREG(EDI_CONFIG);
967 DUMPREG(DEI_EDI_LUT_R0);
968 DUMPREG(DEI_EDI_LUT_R1);
969 DUMPREG(DEI_EDI_LUT_R2);
970 DUMPREG(DEI_EDI_LUT_R3);
971 DUMPREG(DEI_FMD_WINDOW_R0);
972 DUMPREG(DEI_FMD_WINDOW_R1);
973 DUMPREG(DEI_FMD_CONTROL_R0);
974 DUMPREG(DEI_FMD_CONTROL_R1);
975 DUMPREG(DEI_FMD_STATUS_R0);
976 DUMPREG(DEI_FMD_STATUS_R1);
977 DUMPREG(DEI_FMD_STATUS_R2);
Archit Taneja45719122013-10-16 02:36:47 -0300978#undef DUMPREG
Archit Taneja44687b22013-12-12 05:35:57 -0300979
980 sc_dump_regs(dev->sc);
Archit Taneja69480822013-12-12 05:36:01 -0300981 csc_dump_regs(dev->csc);
Archit Taneja45719122013-10-16 02:36:47 -0300982}
983
984static void add_out_dtd(struct vpe_ctx *ctx, int port)
985{
986 struct vpe_q_data *q_data = &ctx->q_data[Q_DATA_DST];
987 const struct vpe_port_data *p_data = &port_data[port];
988 struct vb2_buffer *vb = ctx->dst_vb;
Archit Taneja45719122013-10-16 02:36:47 -0300989 struct vpe_fmt *fmt = q_data->fmt;
990 const struct vpdma_data_format *vpdma_fmt;
Archit Taneja585e6f02013-10-16 02:36:48 -0300991 int mv_buf_selector = !ctx->src_mv_buf_selector;
Archit Taneja45719122013-10-16 02:36:47 -0300992 dma_addr_t dma_addr;
993 u32 flags = 0;
994
Archit Taneja585e6f02013-10-16 02:36:48 -0300995 if (port == VPE_PORT_MV_OUT) {
996 vpdma_fmt = &vpdma_misc_fmts[VPDMA_DATA_FMT_MV];
997 dma_addr = ctx->mv_buf_dma[mv_buf_selector];
998 } else {
999 /* to incorporate interleaved formats */
1000 int plane = fmt->coplanar ? p_data->vb_part : 0;
1001
1002 vpdma_fmt = fmt->vpdma_fmt[plane];
1003 dma_addr = vb2_dma_contig_plane_dma_addr(vb, plane);
1004 if (!dma_addr) {
1005 vpe_err(ctx->dev,
1006 "acquiring output buffer(%d) dma_addr failed\n",
1007 port);
1008 return;
1009 }
Archit Taneja45719122013-10-16 02:36:47 -03001010 }
1011
1012 if (q_data->flags & Q_DATA_FRAME_1D)
1013 flags |= VPDMA_DATA_FRAME_1D;
1014 if (q_data->flags & Q_DATA_MODE_TILED)
1015 flags |= VPDMA_DATA_MODE_TILED;
1016
Archit Taneja928bf2b2014-03-13 08:44:08 -03001017 vpdma_add_out_dtd(&ctx->desc_list, q_data->width, &q_data->c_rect,
1018 vpdma_fmt, dma_addr, p_data->channel, flags);
Archit Taneja45719122013-10-16 02:36:47 -03001019}
1020
1021static void add_in_dtd(struct vpe_ctx *ctx, int port)
1022{
1023 struct vpe_q_data *q_data = &ctx->q_data[Q_DATA_SRC];
1024 const struct vpe_port_data *p_data = &port_data[port];
Archit Taneja585e6f02013-10-16 02:36:48 -03001025 struct vb2_buffer *vb = ctx->src_vbs[p_data->vb_index];
Archit Taneja45719122013-10-16 02:36:47 -03001026 struct vpe_fmt *fmt = q_data->fmt;
1027 const struct vpdma_data_format *vpdma_fmt;
Archit Taneja585e6f02013-10-16 02:36:48 -03001028 int mv_buf_selector = ctx->src_mv_buf_selector;
1029 int field = vb->v4l2_buf.field == V4L2_FIELD_BOTTOM;
Archit Taneja928bf2b2014-03-13 08:44:08 -03001030 int frame_width, frame_height;
Archit Taneja45719122013-10-16 02:36:47 -03001031 dma_addr_t dma_addr;
1032 u32 flags = 0;
1033
Archit Taneja585e6f02013-10-16 02:36:48 -03001034 if (port == VPE_PORT_MV_IN) {
1035 vpdma_fmt = &vpdma_misc_fmts[VPDMA_DATA_FMT_MV];
1036 dma_addr = ctx->mv_buf_dma[mv_buf_selector];
1037 } else {
1038 /* to incorporate interleaved formats */
1039 int plane = fmt->coplanar ? p_data->vb_part : 0;
Archit Taneja45719122013-10-16 02:36:47 -03001040
Archit Taneja585e6f02013-10-16 02:36:48 -03001041 vpdma_fmt = fmt->vpdma_fmt[plane];
1042
1043 dma_addr = vb2_dma_contig_plane_dma_addr(vb, plane);
1044 if (!dma_addr) {
1045 vpe_err(ctx->dev,
1046 "acquiring input buffer(%d) dma_addr failed\n",
1047 port);
1048 return;
1049 }
Archit Taneja45719122013-10-16 02:36:47 -03001050 }
1051
1052 if (q_data->flags & Q_DATA_FRAME_1D)
1053 flags |= VPDMA_DATA_FRAME_1D;
1054 if (q_data->flags & Q_DATA_MODE_TILED)
1055 flags |= VPDMA_DATA_MODE_TILED;
1056
Archit Taneja928bf2b2014-03-13 08:44:08 -03001057 frame_width = q_data->c_rect.width;
1058 frame_height = q_data->c_rect.height;
1059
1060 if (p_data->vb_part && fmt->fourcc == V4L2_PIX_FMT_NV12)
1061 frame_height /= 2;
1062
1063 vpdma_add_in_dtd(&ctx->desc_list, q_data->width, &q_data->c_rect,
1064 vpdma_fmt, dma_addr, p_data->channel, field, flags, frame_width,
1065 frame_height, 0, 0);
Archit Taneja45719122013-10-16 02:36:47 -03001066}
1067
1068/*
1069 * Enable the expected IRQ sources
1070 */
1071static void enable_irqs(struct vpe_ctx *ctx)
1072{
1073 write_reg(ctx->dev, VPE_INT0_ENABLE0_SET, VPE_INT0_LIST0_COMPLETE);
Archit Taneja585e6f02013-10-16 02:36:48 -03001074 write_reg(ctx->dev, VPE_INT0_ENABLE1_SET, VPE_DEI_ERROR_INT |
1075 VPE_DS1_UV_ERROR_INT);
Archit Taneja45719122013-10-16 02:36:47 -03001076
1077 vpdma_enable_list_complete_irq(ctx->dev->vpdma, 0, true);
1078}
1079
1080static void disable_irqs(struct vpe_ctx *ctx)
1081{
1082 write_reg(ctx->dev, VPE_INT0_ENABLE0_CLR, 0xffffffff);
1083 write_reg(ctx->dev, VPE_INT0_ENABLE1_CLR, 0xffffffff);
1084
1085 vpdma_enable_list_complete_irq(ctx->dev->vpdma, 0, false);
1086}
1087
1088/* device_run() - prepares and starts the device
1089 *
1090 * This function is only called when both the source and destination
1091 * buffers are in place.
1092 */
1093static void device_run(void *priv)
1094{
1095 struct vpe_ctx *ctx = priv;
Archit Taneja773f0652013-12-12 05:35:59 -03001096 struct sc_data *sc = ctx->dev->sc;
Archit Taneja45719122013-10-16 02:36:47 -03001097 struct vpe_q_data *d_q_data = &ctx->q_data[Q_DATA_DST];
1098
Archit Taneja585e6f02013-10-16 02:36:48 -03001099 if (ctx->deinterlacing && ctx->src_vbs[2] == NULL) {
1100 ctx->src_vbs[2] = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
1101 WARN_ON(ctx->src_vbs[2] == NULL);
1102 ctx->src_vbs[1] = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
1103 WARN_ON(ctx->src_vbs[1] == NULL);
1104 }
1105
1106 ctx->src_vbs[0] = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
1107 WARN_ON(ctx->src_vbs[0] == NULL);
Archit Taneja45719122013-10-16 02:36:47 -03001108 ctx->dst_vb = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
1109 WARN_ON(ctx->dst_vb == NULL);
1110
1111 /* config descriptors */
1112 if (ctx->dev->loaded_mmrs != ctx->mmr_adb.dma_addr || ctx->load_mmrs) {
1113 vpdma_map_desc_buf(ctx->dev->vpdma, &ctx->mmr_adb);
1114 vpdma_add_cfd_adb(&ctx->desc_list, CFD_MMR_CLIENT, &ctx->mmr_adb);
1115 ctx->dev->loaded_mmrs = ctx->mmr_adb.dma_addr;
1116 ctx->load_mmrs = false;
1117 }
1118
Archit Taneja773f0652013-12-12 05:35:59 -03001119 if (sc->loaded_coeff_h != ctx->sc_coeff_h.dma_addr ||
1120 sc->load_coeff_h) {
1121 vpdma_map_desc_buf(ctx->dev->vpdma, &ctx->sc_coeff_h);
1122 vpdma_add_cfd_block(&ctx->desc_list, CFD_SC_CLIENT,
1123 &ctx->sc_coeff_h, 0);
1124
1125 sc->loaded_coeff_h = ctx->sc_coeff_h.dma_addr;
1126 sc->load_coeff_h = false;
1127 }
1128
1129 if (sc->loaded_coeff_v != ctx->sc_coeff_v.dma_addr ||
1130 sc->load_coeff_v) {
1131 vpdma_map_desc_buf(ctx->dev->vpdma, &ctx->sc_coeff_v);
1132 vpdma_add_cfd_block(&ctx->desc_list, CFD_SC_CLIENT,
1133 &ctx->sc_coeff_v, SC_COEF_SRAM_SIZE >> 4);
1134
1135 sc->loaded_coeff_v = ctx->sc_coeff_v.dma_addr;
1136 sc->load_coeff_v = false;
1137 }
1138
Archit Taneja585e6f02013-10-16 02:36:48 -03001139 /* output data descriptors */
1140 if (ctx->deinterlacing)
1141 add_out_dtd(ctx, VPE_PORT_MV_OUT);
1142
Archit Taneja30496792013-12-12 05:36:03 -03001143 if (d_q_data->colorspace == V4L2_COLORSPACE_SRGB) {
1144 add_out_dtd(ctx, VPE_PORT_RGB_OUT);
1145 } else {
1146 add_out_dtd(ctx, VPE_PORT_LUMA_OUT);
1147 if (d_q_data->fmt->coplanar)
1148 add_out_dtd(ctx, VPE_PORT_CHROMA_OUT);
1149 }
Archit Taneja45719122013-10-16 02:36:47 -03001150
Archit Taneja585e6f02013-10-16 02:36:48 -03001151 /* input data descriptors */
1152 if (ctx->deinterlacing) {
1153 add_in_dtd(ctx, VPE_PORT_LUMA3_IN);
1154 add_in_dtd(ctx, VPE_PORT_CHROMA3_IN);
1155
1156 add_in_dtd(ctx, VPE_PORT_LUMA2_IN);
1157 add_in_dtd(ctx, VPE_PORT_CHROMA2_IN);
1158 }
1159
Archit Taneja45719122013-10-16 02:36:47 -03001160 add_in_dtd(ctx, VPE_PORT_LUMA1_IN);
1161 add_in_dtd(ctx, VPE_PORT_CHROMA1_IN);
1162
Archit Taneja585e6f02013-10-16 02:36:48 -03001163 if (ctx->deinterlacing)
1164 add_in_dtd(ctx, VPE_PORT_MV_IN);
1165
Archit Taneja45719122013-10-16 02:36:47 -03001166 /* sync on channel control descriptors for input ports */
1167 vpdma_add_sync_on_channel_ctd(&ctx->desc_list, VPE_CHAN_LUMA1_IN);
1168 vpdma_add_sync_on_channel_ctd(&ctx->desc_list, VPE_CHAN_CHROMA1_IN);
1169
Archit Taneja585e6f02013-10-16 02:36:48 -03001170 if (ctx->deinterlacing) {
1171 vpdma_add_sync_on_channel_ctd(&ctx->desc_list,
1172 VPE_CHAN_LUMA2_IN);
1173 vpdma_add_sync_on_channel_ctd(&ctx->desc_list,
1174 VPE_CHAN_CHROMA2_IN);
1175
1176 vpdma_add_sync_on_channel_ctd(&ctx->desc_list,
1177 VPE_CHAN_LUMA3_IN);
1178 vpdma_add_sync_on_channel_ctd(&ctx->desc_list,
1179 VPE_CHAN_CHROMA3_IN);
1180
1181 vpdma_add_sync_on_channel_ctd(&ctx->desc_list, VPE_CHAN_MV_IN);
1182 }
1183
Archit Taneja45719122013-10-16 02:36:47 -03001184 /* sync on channel control descriptors for output ports */
Archit Taneja30496792013-12-12 05:36:03 -03001185 if (d_q_data->colorspace == V4L2_COLORSPACE_SRGB) {
1186 vpdma_add_sync_on_channel_ctd(&ctx->desc_list,
1187 VPE_CHAN_RGB_OUT);
1188 } else {
1189 vpdma_add_sync_on_channel_ctd(&ctx->desc_list,
1190 VPE_CHAN_LUMA_OUT);
1191 if (d_q_data->fmt->coplanar)
1192 vpdma_add_sync_on_channel_ctd(&ctx->desc_list,
1193 VPE_CHAN_CHROMA_OUT);
1194 }
Archit Taneja45719122013-10-16 02:36:47 -03001195
Archit Taneja585e6f02013-10-16 02:36:48 -03001196 if (ctx->deinterlacing)
1197 vpdma_add_sync_on_channel_ctd(&ctx->desc_list, VPE_CHAN_MV_OUT);
1198
Archit Taneja45719122013-10-16 02:36:47 -03001199 enable_irqs(ctx);
1200
1201 vpdma_map_desc_buf(ctx->dev->vpdma, &ctx->desc_list.buf);
1202 vpdma_submit_descs(ctx->dev->vpdma, &ctx->desc_list);
1203}
1204
Archit Taneja585e6f02013-10-16 02:36:48 -03001205static void dei_error(struct vpe_ctx *ctx)
1206{
1207 dev_warn(ctx->dev->v4l2_dev.dev,
1208 "received DEI error interrupt\n");
1209}
1210
Archit Taneja45719122013-10-16 02:36:47 -03001211static void ds1_uv_error(struct vpe_ctx *ctx)
1212{
1213 dev_warn(ctx->dev->v4l2_dev.dev,
1214 "received downsampler error interrupt\n");
1215}
1216
1217static irqreturn_t vpe_irq(int irq_vpe, void *data)
1218{
1219 struct vpe_dev *dev = (struct vpe_dev *)data;
1220 struct vpe_ctx *ctx;
Archit Taneja585e6f02013-10-16 02:36:48 -03001221 struct vpe_q_data *d_q_data;
Archit Taneja45719122013-10-16 02:36:47 -03001222 struct vb2_buffer *s_vb, *d_vb;
1223 struct v4l2_buffer *s_buf, *d_buf;
1224 unsigned long flags;
1225 u32 irqst0, irqst1;
1226
1227 irqst0 = read_reg(dev, VPE_INT0_STATUS0);
1228 if (irqst0) {
1229 write_reg(dev, VPE_INT0_STATUS0_CLR, irqst0);
1230 vpe_dbg(dev, "INT0_STATUS0 = 0x%08x\n", irqst0);
1231 }
1232
1233 irqst1 = read_reg(dev, VPE_INT0_STATUS1);
1234 if (irqst1) {
1235 write_reg(dev, VPE_INT0_STATUS1_CLR, irqst1);
1236 vpe_dbg(dev, "INT0_STATUS1 = 0x%08x\n", irqst1);
1237 }
1238
1239 ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev);
1240 if (!ctx) {
1241 vpe_err(dev, "instance released before end of transaction\n");
1242 goto handled;
1243 }
1244
Archit Taneja585e6f02013-10-16 02:36:48 -03001245 if (irqst1) {
1246 if (irqst1 & VPE_DEI_ERROR_INT) {
1247 irqst1 &= ~VPE_DEI_ERROR_INT;
1248 dei_error(ctx);
1249 }
1250 if (irqst1 & VPE_DS1_UV_ERROR_INT) {
1251 irqst1 &= ~VPE_DS1_UV_ERROR_INT;
1252 ds1_uv_error(ctx);
1253 }
Archit Taneja45719122013-10-16 02:36:47 -03001254 }
1255
1256 if (irqst0) {
1257 if (irqst0 & VPE_INT0_LIST0_COMPLETE)
1258 vpdma_clear_list_stat(ctx->dev->vpdma);
1259
1260 irqst0 &= ~(VPE_INT0_LIST0_COMPLETE);
1261 }
1262
1263 if (irqst0 | irqst1) {
1264 dev_warn(dev->v4l2_dev.dev, "Unexpected interrupt: "
1265 "INT0_STATUS0 = 0x%08x, INT0_STATUS1 = 0x%08x\n",
1266 irqst0, irqst1);
1267 }
1268
1269 disable_irqs(ctx);
1270
1271 vpdma_unmap_desc_buf(dev->vpdma, &ctx->desc_list.buf);
1272 vpdma_unmap_desc_buf(dev->vpdma, &ctx->mmr_adb);
Archit Taneja773f0652013-12-12 05:35:59 -03001273 vpdma_unmap_desc_buf(dev->vpdma, &ctx->sc_coeff_h);
1274 vpdma_unmap_desc_buf(dev->vpdma, &ctx->sc_coeff_v);
Archit Taneja45719122013-10-16 02:36:47 -03001275
1276 vpdma_reset_desc_list(&ctx->desc_list);
1277
Archit Taneja585e6f02013-10-16 02:36:48 -03001278 /* the previous dst mv buffer becomes the next src mv buffer */
1279 ctx->src_mv_buf_selector = !ctx->src_mv_buf_selector;
1280
Archit Taneja45719122013-10-16 02:36:47 -03001281 if (ctx->aborting)
1282 goto finished;
1283
Archit Taneja585e6f02013-10-16 02:36:48 -03001284 s_vb = ctx->src_vbs[0];
Archit Taneja45719122013-10-16 02:36:47 -03001285 d_vb = ctx->dst_vb;
1286 s_buf = &s_vb->v4l2_buf;
1287 d_buf = &d_vb->v4l2_buf;
1288
Archit Tanejabbe24c62014-03-11 04:47:52 -03001289 d_buf->flags = s_buf->flags;
1290
Archit Taneja45719122013-10-16 02:36:47 -03001291 d_buf->timestamp = s_buf->timestamp;
Archit Tanejabbe24c62014-03-11 04:47:52 -03001292 if (s_buf->flags & V4L2_BUF_FLAG_TIMECODE)
Archit Taneja45719122013-10-16 02:36:47 -03001293 d_buf->timecode = s_buf->timecode;
Archit Tanejabbe24c62014-03-11 04:47:52 -03001294
Archit Taneja45719122013-10-16 02:36:47 -03001295 d_buf->sequence = ctx->sequence;
1296
Archit Taneja585e6f02013-10-16 02:36:48 -03001297 d_q_data = &ctx->q_data[Q_DATA_DST];
1298 if (d_q_data->flags & Q_DATA_INTERLACED) {
Archit Taneja5269fef2014-03-10 03:24:01 -03001299 d_buf->field = ctx->field;
Archit Taneja585e6f02013-10-16 02:36:48 -03001300 if (ctx->field == V4L2_FIELD_BOTTOM) {
1301 ctx->sequence++;
1302 ctx->field = V4L2_FIELD_TOP;
1303 } else {
1304 WARN_ON(ctx->field != V4L2_FIELD_TOP);
1305 ctx->field = V4L2_FIELD_BOTTOM;
1306 }
1307 } else {
Archit Taneja5269fef2014-03-10 03:24:01 -03001308 d_buf->field = V4L2_FIELD_NONE;
Archit Taneja585e6f02013-10-16 02:36:48 -03001309 ctx->sequence++;
1310 }
1311
1312 if (ctx->deinterlacing)
1313 s_vb = ctx->src_vbs[2];
Archit Taneja45719122013-10-16 02:36:47 -03001314
1315 spin_lock_irqsave(&dev->lock, flags);
1316 v4l2_m2m_buf_done(s_vb, VB2_BUF_STATE_DONE);
1317 v4l2_m2m_buf_done(d_vb, VB2_BUF_STATE_DONE);
1318 spin_unlock_irqrestore(&dev->lock, flags);
1319
Archit Taneja585e6f02013-10-16 02:36:48 -03001320 if (ctx->deinterlacing) {
1321 ctx->src_vbs[2] = ctx->src_vbs[1];
1322 ctx->src_vbs[1] = ctx->src_vbs[0];
1323 }
1324
Archit Taneja45719122013-10-16 02:36:47 -03001325 ctx->bufs_completed++;
1326 if (ctx->bufs_completed < ctx->bufs_per_job) {
1327 device_run(ctx);
1328 goto handled;
1329 }
1330
1331finished:
1332 vpe_dbg(ctx->dev, "finishing transaction\n");
1333 ctx->bufs_completed = 0;
1334 v4l2_m2m_job_finish(dev->m2m_dev, ctx->m2m_ctx);
1335handled:
1336 return IRQ_HANDLED;
1337}
1338
1339/*
1340 * video ioctls
1341 */
1342static int vpe_querycap(struct file *file, void *priv,
1343 struct v4l2_capability *cap)
1344{
1345 strncpy(cap->driver, VPE_MODULE_NAME, sizeof(cap->driver) - 1);
1346 strncpy(cap->card, VPE_MODULE_NAME, sizeof(cap->card) - 1);
Archit Tanejab20902b2014-03-06 07:07:47 -03001347 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
1348 VPE_MODULE_NAME);
Archit Tanejafca27a92014-03-05 09:52:38 -03001349 cap->device_caps = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING;
Archit Taneja45719122013-10-16 02:36:47 -03001350 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
1351 return 0;
1352}
1353
1354static int __enum_fmt(struct v4l2_fmtdesc *f, u32 type)
1355{
1356 int i, index;
1357 struct vpe_fmt *fmt = NULL;
1358
1359 index = 0;
1360 for (i = 0; i < ARRAY_SIZE(vpe_formats); ++i) {
1361 if (vpe_formats[i].types & type) {
1362 if (index == f->index) {
1363 fmt = &vpe_formats[i];
1364 break;
1365 }
1366 index++;
1367 }
1368 }
1369
1370 if (!fmt)
1371 return -EINVAL;
1372
1373 strncpy(f->description, fmt->name, sizeof(f->description) - 1);
1374 f->pixelformat = fmt->fourcc;
1375 return 0;
1376}
1377
1378static int vpe_enum_fmt(struct file *file, void *priv,
1379 struct v4l2_fmtdesc *f)
1380{
1381 if (V4L2_TYPE_IS_OUTPUT(f->type))
1382 return __enum_fmt(f, VPE_FMT_TYPE_OUTPUT);
1383
1384 return __enum_fmt(f, VPE_FMT_TYPE_CAPTURE);
1385}
1386
1387static int vpe_g_fmt(struct file *file, void *priv, struct v4l2_format *f)
1388{
1389 struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
1390 struct vpe_ctx *ctx = file2ctx(file);
1391 struct vb2_queue *vq;
1392 struct vpe_q_data *q_data;
1393 int i;
1394
1395 vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
1396 if (!vq)
1397 return -EINVAL;
1398
1399 q_data = get_q_data(ctx, f->type);
1400
1401 pix->width = q_data->width;
1402 pix->height = q_data->height;
1403 pix->pixelformat = q_data->fmt->fourcc;
Archit Taneja585e6f02013-10-16 02:36:48 -03001404 pix->field = q_data->field;
Archit Taneja45719122013-10-16 02:36:47 -03001405
1406 if (V4L2_TYPE_IS_OUTPUT(f->type)) {
1407 pix->colorspace = q_data->colorspace;
1408 } else {
1409 struct vpe_q_data *s_q_data;
1410
1411 /* get colorspace from the source queue */
1412 s_q_data = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
1413
1414 pix->colorspace = s_q_data->colorspace;
1415 }
1416
1417 pix->num_planes = q_data->fmt->coplanar ? 2 : 1;
1418
1419 for (i = 0; i < pix->num_planes; i++) {
1420 pix->plane_fmt[i].bytesperline = q_data->bytesperline[i];
1421 pix->plane_fmt[i].sizeimage = q_data->sizeimage[i];
1422 }
1423
1424 return 0;
1425}
1426
1427static int __vpe_try_fmt(struct vpe_ctx *ctx, struct v4l2_format *f,
1428 struct vpe_fmt *fmt, int type)
1429{
1430 struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
1431 struct v4l2_plane_pix_format *plane_fmt;
Archit Tanejaa51cd8f2013-12-03 08:51:13 -03001432 unsigned int w_align;
1433 int i, depth, depth_bytes;
Archit Taneja45719122013-10-16 02:36:47 -03001434
1435 if (!fmt || !(fmt->types & type)) {
1436 vpe_err(ctx->dev, "Fourcc format (0x%08x) invalid.\n",
1437 pix->pixelformat);
1438 return -EINVAL;
1439 }
1440
Archit Taneja585e6f02013-10-16 02:36:48 -03001441 if (pix->field != V4L2_FIELD_NONE && pix->field != V4L2_FIELD_ALTERNATE)
1442 pix->field = V4L2_FIELD_NONE;
Archit Taneja45719122013-10-16 02:36:47 -03001443
Archit Tanejaa51cd8f2013-12-03 08:51:13 -03001444 depth = fmt->vpdma_fmt[VPE_LUMA]->depth;
1445
1446 /*
1447 * the line stride should 16 byte aligned for VPDMA to work, based on
1448 * the bytes per pixel, figure out how much the width should be aligned
1449 * to make sure line stride is 16 byte aligned
1450 */
1451 depth_bytes = depth >> 3;
1452
1453 if (depth_bytes == 3)
1454 /*
1455 * if bpp is 3(as in some RGB formats), the pixel width doesn't
1456 * really help in ensuring line stride is 16 byte aligned
1457 */
1458 w_align = 4;
1459 else
1460 /*
1461 * for the remainder bpp(4, 2 and 1), the pixel width alignment
1462 * can ensure a line stride alignment of 16 bytes. For example,
1463 * if bpp is 2, then the line stride can be 16 byte aligned if
1464 * the width is 8 byte aligned
1465 */
1466 w_align = order_base_2(VPDMA_DESC_ALIGN / depth_bytes);
1467
1468 v4l_bound_align_image(&pix->width, MIN_W, MAX_W, w_align,
Archit Taneja45719122013-10-16 02:36:47 -03001469 &pix->height, MIN_H, MAX_H, H_ALIGN,
1470 S_ALIGN);
1471
1472 pix->num_planes = fmt->coplanar ? 2 : 1;
1473 pix->pixelformat = fmt->fourcc;
1474
Archit Taneja30496792013-12-12 05:36:03 -03001475 if (!pix->colorspace) {
1476 if (fmt->fourcc == V4L2_PIX_FMT_RGB24 ||
1477 fmt->fourcc == V4L2_PIX_FMT_BGR24 ||
1478 fmt->fourcc == V4L2_PIX_FMT_RGB32 ||
1479 fmt->fourcc == V4L2_PIX_FMT_BGR32) {
1480 pix->colorspace = V4L2_COLORSPACE_SRGB;
1481 } else {
1482 if (pix->height > 1280) /* HD */
1483 pix->colorspace = V4L2_COLORSPACE_REC709;
1484 else /* SD */
1485 pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
1486 }
Archit Taneja45719122013-10-16 02:36:47 -03001487 }
1488
Archit Taneja92851f12014-03-10 04:19:02 -03001489 memset(pix->reserved, 0, sizeof(pix->reserved));
Archit Taneja45719122013-10-16 02:36:47 -03001490 for (i = 0; i < pix->num_planes; i++) {
Archit Taneja45719122013-10-16 02:36:47 -03001491 plane_fmt = &pix->plane_fmt[i];
1492 depth = fmt->vpdma_fmt[i]->depth;
1493
1494 if (i == VPE_LUMA)
Archit Tanejaa51cd8f2013-12-03 08:51:13 -03001495 plane_fmt->bytesperline = (pix->width * depth) >> 3;
Archit Taneja45719122013-10-16 02:36:47 -03001496 else
1497 plane_fmt->bytesperline = pix->width;
1498
1499 plane_fmt->sizeimage =
1500 (pix->height * pix->width * depth) >> 3;
Archit Taneja92851f12014-03-10 04:19:02 -03001501
1502 memset(plane_fmt->reserved, 0, sizeof(plane_fmt->reserved));
Archit Taneja45719122013-10-16 02:36:47 -03001503 }
1504
1505 return 0;
1506}
1507
1508static int vpe_try_fmt(struct file *file, void *priv, struct v4l2_format *f)
1509{
1510 struct vpe_ctx *ctx = file2ctx(file);
1511 struct vpe_fmt *fmt = find_format(f);
1512
1513 if (V4L2_TYPE_IS_OUTPUT(f->type))
1514 return __vpe_try_fmt(ctx, f, fmt, VPE_FMT_TYPE_OUTPUT);
1515 else
1516 return __vpe_try_fmt(ctx, f, fmt, VPE_FMT_TYPE_CAPTURE);
1517}
1518
1519static int __vpe_s_fmt(struct vpe_ctx *ctx, struct v4l2_format *f)
1520{
1521 struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
1522 struct v4l2_plane_pix_format *plane_fmt;
1523 struct vpe_q_data *q_data;
1524 struct vb2_queue *vq;
1525 int i;
1526
1527 vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
1528 if (!vq)
1529 return -EINVAL;
1530
1531 if (vb2_is_busy(vq)) {
1532 vpe_err(ctx->dev, "queue busy\n");
1533 return -EBUSY;
1534 }
1535
1536 q_data = get_q_data(ctx, f->type);
1537 if (!q_data)
1538 return -EINVAL;
1539
1540 q_data->fmt = find_format(f);
1541 q_data->width = pix->width;
1542 q_data->height = pix->height;
1543 q_data->colorspace = pix->colorspace;
Archit Taneja585e6f02013-10-16 02:36:48 -03001544 q_data->field = pix->field;
Archit Taneja45719122013-10-16 02:36:47 -03001545
1546 for (i = 0; i < pix->num_planes; i++) {
1547 plane_fmt = &pix->plane_fmt[i];
1548
1549 q_data->bytesperline[i] = plane_fmt->bytesperline;
1550 q_data->sizeimage[i] = plane_fmt->sizeimage;
1551 }
1552
1553 q_data->c_rect.left = 0;
1554 q_data->c_rect.top = 0;
1555 q_data->c_rect.width = q_data->width;
1556 q_data->c_rect.height = q_data->height;
1557
Archit Taneja585e6f02013-10-16 02:36:48 -03001558 if (q_data->field == V4L2_FIELD_ALTERNATE)
1559 q_data->flags |= Q_DATA_INTERLACED;
1560 else
1561 q_data->flags &= ~Q_DATA_INTERLACED;
1562
Archit Taneja45719122013-10-16 02:36:47 -03001563 vpe_dbg(ctx->dev, "Setting format for type %d, wxh: %dx%d, fmt: %d bpl_y %d",
1564 f->type, q_data->width, q_data->height, q_data->fmt->fourcc,
1565 q_data->bytesperline[VPE_LUMA]);
1566 if (q_data->fmt->coplanar)
1567 vpe_dbg(ctx->dev, " bpl_uv %d\n",
1568 q_data->bytesperline[VPE_CHROMA]);
1569
1570 return 0;
1571}
1572
1573static int vpe_s_fmt(struct file *file, void *priv, struct v4l2_format *f)
1574{
1575 int ret;
1576 struct vpe_ctx *ctx = file2ctx(file);
1577
1578 ret = vpe_try_fmt(file, priv, f);
1579 if (ret)
1580 return ret;
1581
1582 ret = __vpe_s_fmt(ctx, f);
1583 if (ret)
1584 return ret;
1585
1586 if (V4L2_TYPE_IS_OUTPUT(f->type))
1587 set_src_registers(ctx);
1588 else
1589 set_dst_registers(ctx);
1590
1591 return set_srcdst_params(ctx);
1592}
1593
1594static int vpe_reqbufs(struct file *file, void *priv,
1595 struct v4l2_requestbuffers *reqbufs)
1596{
1597 struct vpe_ctx *ctx = file2ctx(file);
1598
1599 return v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
1600}
1601
1602static int vpe_querybuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1603{
1604 struct vpe_ctx *ctx = file2ctx(file);
1605
1606 return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf);
1607}
1608
1609static int vpe_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1610{
1611 struct vpe_ctx *ctx = file2ctx(file);
1612
1613 return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
1614}
1615
1616static int vpe_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1617{
1618 struct vpe_ctx *ctx = file2ctx(file);
1619
1620 return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
1621}
1622
1623static int vpe_streamon(struct file *file, void *priv, enum v4l2_buf_type type)
1624{
1625 struct vpe_ctx *ctx = file2ctx(file);
1626
1627 return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
1628}
1629
1630static int vpe_streamoff(struct file *file, void *priv, enum v4l2_buf_type type)
1631{
1632 struct vpe_ctx *ctx = file2ctx(file);
1633
1634 vpe_dump_regs(ctx->dev);
1635 vpdma_dump_regs(ctx->dev->vpdma);
1636
1637 return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
1638}
1639
1640/*
1641 * defines number of buffers/frames a context can process with VPE before
1642 * switching to a different context. default value is 1 buffer per context
1643 */
1644#define V4L2_CID_VPE_BUFS_PER_JOB (V4L2_CID_USER_TI_VPE_BASE + 0)
1645
1646static int vpe_s_ctrl(struct v4l2_ctrl *ctrl)
1647{
1648 struct vpe_ctx *ctx =
1649 container_of(ctrl->handler, struct vpe_ctx, hdl);
1650
1651 switch (ctrl->id) {
1652 case V4L2_CID_VPE_BUFS_PER_JOB:
1653 ctx->bufs_per_job = ctrl->val;
1654 break;
1655
1656 default:
1657 vpe_err(ctx->dev, "Invalid control\n");
1658 return -EINVAL;
1659 }
1660
1661 return 0;
1662}
1663
1664static const struct v4l2_ctrl_ops vpe_ctrl_ops = {
1665 .s_ctrl = vpe_s_ctrl,
1666};
1667
1668static const struct v4l2_ioctl_ops vpe_ioctl_ops = {
1669 .vidioc_querycap = vpe_querycap,
1670
1671 .vidioc_enum_fmt_vid_cap_mplane = vpe_enum_fmt,
1672 .vidioc_g_fmt_vid_cap_mplane = vpe_g_fmt,
1673 .vidioc_try_fmt_vid_cap_mplane = vpe_try_fmt,
1674 .vidioc_s_fmt_vid_cap_mplane = vpe_s_fmt,
1675
1676 .vidioc_enum_fmt_vid_out_mplane = vpe_enum_fmt,
1677 .vidioc_g_fmt_vid_out_mplane = vpe_g_fmt,
1678 .vidioc_try_fmt_vid_out_mplane = vpe_try_fmt,
1679 .vidioc_s_fmt_vid_out_mplane = vpe_s_fmt,
1680
1681 .vidioc_reqbufs = vpe_reqbufs,
1682 .vidioc_querybuf = vpe_querybuf,
1683
1684 .vidioc_qbuf = vpe_qbuf,
1685 .vidioc_dqbuf = vpe_dqbuf,
1686
1687 .vidioc_streamon = vpe_streamon,
1688 .vidioc_streamoff = vpe_streamoff,
1689 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1690 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1691};
1692
1693/*
1694 * Queue operations
1695 */
1696static int vpe_queue_setup(struct vb2_queue *vq,
1697 const struct v4l2_format *fmt,
1698 unsigned int *nbuffers, unsigned int *nplanes,
1699 unsigned int sizes[], void *alloc_ctxs[])
1700{
1701 int i;
1702 struct vpe_ctx *ctx = vb2_get_drv_priv(vq);
1703 struct vpe_q_data *q_data;
1704
1705 q_data = get_q_data(ctx, vq->type);
1706
1707 *nplanes = q_data->fmt->coplanar ? 2 : 1;
1708
1709 for (i = 0; i < *nplanes; i++) {
1710 sizes[i] = q_data->sizeimage[i];
1711 alloc_ctxs[i] = ctx->dev->alloc_ctx;
1712 }
1713
1714 vpe_dbg(ctx->dev, "get %d buffer(s) of size %d", *nbuffers,
1715 sizes[VPE_LUMA]);
1716 if (q_data->fmt->coplanar)
1717 vpe_dbg(ctx->dev, " and %d\n", sizes[VPE_CHROMA]);
1718
1719 return 0;
1720}
1721
1722static int vpe_buf_prepare(struct vb2_buffer *vb)
1723{
1724 struct vpe_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1725 struct vpe_q_data *q_data;
1726 int i, num_planes;
1727
1728 vpe_dbg(ctx->dev, "type: %d\n", vb->vb2_queue->type);
1729
1730 q_data = get_q_data(ctx, vb->vb2_queue->type);
1731 num_planes = q_data->fmt->coplanar ? 2 : 1;
1732
Archit Taneja5269fef2014-03-10 03:24:01 -03001733 if (vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1734 if (!(q_data->flags & Q_DATA_INTERLACED)) {
1735 vb->v4l2_buf.field = V4L2_FIELD_NONE;
1736 } else {
1737 if (vb->v4l2_buf.field != V4L2_FIELD_TOP &&
1738 vb->v4l2_buf.field != V4L2_FIELD_BOTTOM)
1739 return -EINVAL;
1740 }
1741 }
1742
Archit Taneja45719122013-10-16 02:36:47 -03001743 for (i = 0; i < num_planes; i++) {
1744 if (vb2_plane_size(vb, i) < q_data->sizeimage[i]) {
1745 vpe_err(ctx->dev,
1746 "data will not fit into plane (%lu < %lu)\n",
1747 vb2_plane_size(vb, i),
1748 (long) q_data->sizeimage[i]);
1749 return -EINVAL;
1750 }
1751 }
1752
1753 for (i = 0; i < num_planes; i++)
1754 vb2_set_plane_payload(vb, i, q_data->sizeimage[i]);
1755
1756 return 0;
1757}
1758
1759static void vpe_buf_queue(struct vb2_buffer *vb)
1760{
1761 struct vpe_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1762 v4l2_m2m_buf_queue(ctx->m2m_ctx, vb);
1763}
1764
1765static void vpe_wait_prepare(struct vb2_queue *q)
1766{
1767 struct vpe_ctx *ctx = vb2_get_drv_priv(q);
1768 vpe_unlock(ctx);
1769}
1770
1771static void vpe_wait_finish(struct vb2_queue *q)
1772{
1773 struct vpe_ctx *ctx = vb2_get_drv_priv(q);
1774 vpe_lock(ctx);
1775}
1776
1777static struct vb2_ops vpe_qops = {
1778 .queue_setup = vpe_queue_setup,
1779 .buf_prepare = vpe_buf_prepare,
1780 .buf_queue = vpe_buf_queue,
1781 .wait_prepare = vpe_wait_prepare,
1782 .wait_finish = vpe_wait_finish,
1783};
1784
1785static int queue_init(void *priv, struct vb2_queue *src_vq,
1786 struct vb2_queue *dst_vq)
1787{
1788 struct vpe_ctx *ctx = priv;
1789 int ret;
1790
1791 memset(src_vq, 0, sizeof(*src_vq));
1792 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
Archit Taneja668f91d2014-03-13 08:44:06 -03001793 src_vq->io_modes = VB2_MMAP | VB2_DMABUF;
Archit Taneja45719122013-10-16 02:36:47 -03001794 src_vq->drv_priv = ctx;
1795 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1796 src_vq->ops = &vpe_qops;
1797 src_vq->mem_ops = &vb2_dma_contig_memops;
Sakari Ailusade48682014-02-25 19:12:19 -03001798 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Archit Taneja45719122013-10-16 02:36:47 -03001799
1800 ret = vb2_queue_init(src_vq);
1801 if (ret)
1802 return ret;
1803
1804 memset(dst_vq, 0, sizeof(*dst_vq));
1805 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
Archit Taneja668f91d2014-03-13 08:44:06 -03001806 dst_vq->io_modes = VB2_MMAP | VB2_DMABUF;
Archit Taneja45719122013-10-16 02:36:47 -03001807 dst_vq->drv_priv = ctx;
1808 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1809 dst_vq->ops = &vpe_qops;
1810 dst_vq->mem_ops = &vb2_dma_contig_memops;
Sakari Ailusade48682014-02-25 19:12:19 -03001811 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Archit Taneja45719122013-10-16 02:36:47 -03001812
1813 return vb2_queue_init(dst_vq);
1814}
1815
1816static const struct v4l2_ctrl_config vpe_bufs_per_job = {
1817 .ops = &vpe_ctrl_ops,
1818 .id = V4L2_CID_VPE_BUFS_PER_JOB,
1819 .name = "Buffers Per Transaction",
1820 .type = V4L2_CTRL_TYPE_INTEGER,
1821 .def = VPE_DEF_BUFS_PER_JOB,
1822 .min = 1,
1823 .max = VIDEO_MAX_FRAME,
1824 .step = 1,
1825};
1826
1827/*
1828 * File operations
1829 */
1830static int vpe_open(struct file *file)
1831{
1832 struct vpe_dev *dev = video_drvdata(file);
1833 struct vpe_ctx *ctx = NULL;
1834 struct vpe_q_data *s_q_data;
1835 struct v4l2_ctrl_handler *hdl;
1836 int ret;
1837
1838 vpe_dbg(dev, "vpe_open\n");
1839
Archit Taneja45719122013-10-16 02:36:47 -03001840 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1841 if (!ctx)
1842 return -ENOMEM;
1843
1844 ctx->dev = dev;
1845
1846 if (mutex_lock_interruptible(&dev->dev_mutex)) {
1847 ret = -ERESTARTSYS;
1848 goto free_ctx;
1849 }
1850
1851 ret = vpdma_create_desc_list(&ctx->desc_list, VPE_DESC_LIST_SIZE,
1852 VPDMA_LIST_TYPE_NORMAL);
1853 if (ret != 0)
1854 goto unlock;
1855
1856 ret = vpdma_alloc_desc_buf(&ctx->mmr_adb, sizeof(struct vpe_mmr_adb));
1857 if (ret != 0)
1858 goto free_desc_list;
1859
Archit Taneja773f0652013-12-12 05:35:59 -03001860 ret = vpdma_alloc_desc_buf(&ctx->sc_coeff_h, SC_COEF_SRAM_SIZE);
1861 if (ret != 0)
1862 goto free_mmr_adb;
1863
1864 ret = vpdma_alloc_desc_buf(&ctx->sc_coeff_v, SC_COEF_SRAM_SIZE);
1865 if (ret != 0)
1866 goto free_sc_h;
1867
Archit Taneja45719122013-10-16 02:36:47 -03001868 init_adb_hdrs(ctx);
1869
1870 v4l2_fh_init(&ctx->fh, video_devdata(file));
1871 file->private_data = &ctx->fh;
1872
1873 hdl = &ctx->hdl;
1874 v4l2_ctrl_handler_init(hdl, 1);
1875 v4l2_ctrl_new_custom(hdl, &vpe_bufs_per_job, NULL);
1876 if (hdl->error) {
1877 ret = hdl->error;
1878 goto exit_fh;
1879 }
1880 ctx->fh.ctrl_handler = hdl;
1881 v4l2_ctrl_handler_setup(hdl);
1882
1883 s_q_data = &ctx->q_data[Q_DATA_SRC];
1884 s_q_data->fmt = &vpe_formats[2];
1885 s_q_data->width = 1920;
1886 s_q_data->height = 1080;
Archit Taneja67fb87e2014-03-10 03:57:40 -03001887 s_q_data->bytesperline[VPE_LUMA] = (s_q_data->width *
Archit Taneja45719122013-10-16 02:36:47 -03001888 s_q_data->fmt->vpdma_fmt[VPE_LUMA]->depth) >> 3;
Archit Taneja67fb87e2014-03-10 03:57:40 -03001889 s_q_data->sizeimage[VPE_LUMA] = (s_q_data->bytesperline[VPE_LUMA] *
1890 s_q_data->height);
1891 s_q_data->colorspace = V4L2_COLORSPACE_REC709;
Archit Taneja585e6f02013-10-16 02:36:48 -03001892 s_q_data->field = V4L2_FIELD_NONE;
Archit Taneja45719122013-10-16 02:36:47 -03001893 s_q_data->c_rect.left = 0;
1894 s_q_data->c_rect.top = 0;
1895 s_q_data->c_rect.width = s_q_data->width;
1896 s_q_data->c_rect.height = s_q_data->height;
1897 s_q_data->flags = 0;
1898
1899 ctx->q_data[Q_DATA_DST] = *s_q_data;
1900
Archit Taneja585e6f02013-10-16 02:36:48 -03001901 set_dei_shadow_registers(ctx);
Archit Taneja45719122013-10-16 02:36:47 -03001902 set_src_registers(ctx);
1903 set_dst_registers(ctx);
1904 ret = set_srcdst_params(ctx);
1905 if (ret)
1906 goto exit_fh;
1907
1908 ctx->m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx, &queue_init);
1909
1910 if (IS_ERR(ctx->m2m_ctx)) {
1911 ret = PTR_ERR(ctx->m2m_ctx);
1912 goto exit_fh;
1913 }
1914
1915 v4l2_fh_add(&ctx->fh);
1916
1917 /*
1918 * for now, just report the creation of the first instance, we can later
1919 * optimize the driver to enable or disable clocks when the first
1920 * instance is created or the last instance released
1921 */
1922 if (atomic_inc_return(&dev->num_instances) == 1)
1923 vpe_dbg(dev, "first instance created\n");
1924
1925 ctx->bufs_per_job = VPE_DEF_BUFS_PER_JOB;
1926
1927 ctx->load_mmrs = true;
1928
1929 vpe_dbg(dev, "created instance %p, m2m_ctx: %p\n",
1930 ctx, ctx->m2m_ctx);
1931
1932 mutex_unlock(&dev->dev_mutex);
1933
1934 return 0;
1935exit_fh:
1936 v4l2_ctrl_handler_free(hdl);
1937 v4l2_fh_exit(&ctx->fh);
Archit Taneja773f0652013-12-12 05:35:59 -03001938 vpdma_free_desc_buf(&ctx->sc_coeff_v);
1939free_sc_h:
1940 vpdma_free_desc_buf(&ctx->sc_coeff_h);
1941free_mmr_adb:
Archit Taneja45719122013-10-16 02:36:47 -03001942 vpdma_free_desc_buf(&ctx->mmr_adb);
1943free_desc_list:
1944 vpdma_free_desc_list(&ctx->desc_list);
1945unlock:
1946 mutex_unlock(&dev->dev_mutex);
1947free_ctx:
1948 kfree(ctx);
1949 return ret;
1950}
1951
1952static int vpe_release(struct file *file)
1953{
1954 struct vpe_dev *dev = video_drvdata(file);
1955 struct vpe_ctx *ctx = file2ctx(file);
1956
1957 vpe_dbg(dev, "releasing instance %p\n", ctx);
1958
1959 mutex_lock(&dev->dev_mutex);
Archit Taneja585e6f02013-10-16 02:36:48 -03001960 free_vbs(ctx);
1961 free_mv_buffers(ctx);
Archit Taneja45719122013-10-16 02:36:47 -03001962 vpdma_free_desc_list(&ctx->desc_list);
1963 vpdma_free_desc_buf(&ctx->mmr_adb);
1964
1965 v4l2_fh_del(&ctx->fh);
1966 v4l2_fh_exit(&ctx->fh);
1967 v4l2_ctrl_handler_free(&ctx->hdl);
1968 v4l2_m2m_ctx_release(ctx->m2m_ctx);
1969
1970 kfree(ctx);
1971
1972 /*
1973 * for now, just report the release of the last instance, we can later
1974 * optimize the driver to enable or disable clocks when the first
1975 * instance is created or the last instance released
1976 */
1977 if (atomic_dec_return(&dev->num_instances) == 0)
1978 vpe_dbg(dev, "last instance released\n");
1979
1980 mutex_unlock(&dev->dev_mutex);
1981
1982 return 0;
1983}
1984
1985static unsigned int vpe_poll(struct file *file,
1986 struct poll_table_struct *wait)
1987{
1988 struct vpe_ctx *ctx = file2ctx(file);
1989 struct vpe_dev *dev = ctx->dev;
1990 int ret;
1991
1992 mutex_lock(&dev->dev_mutex);
1993 ret = v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
1994 mutex_unlock(&dev->dev_mutex);
1995 return ret;
1996}
1997
1998static int vpe_mmap(struct file *file, struct vm_area_struct *vma)
1999{
2000 struct vpe_ctx *ctx = file2ctx(file);
2001 struct vpe_dev *dev = ctx->dev;
2002 int ret;
2003
2004 if (mutex_lock_interruptible(&dev->dev_mutex))
2005 return -ERESTARTSYS;
2006 ret = v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
2007 mutex_unlock(&dev->dev_mutex);
2008 return ret;
2009}
2010
2011static const struct v4l2_file_operations vpe_fops = {
2012 .owner = THIS_MODULE,
2013 .open = vpe_open,
2014 .release = vpe_release,
2015 .poll = vpe_poll,
2016 .unlocked_ioctl = video_ioctl2,
2017 .mmap = vpe_mmap,
2018};
2019
2020static struct video_device vpe_videodev = {
2021 .name = VPE_MODULE_NAME,
2022 .fops = &vpe_fops,
2023 .ioctl_ops = &vpe_ioctl_ops,
2024 .minor = -1,
Archit Taneja772a7b72014-02-18 10:24:07 -03002025 .release = video_device_release_empty,
Archit Taneja45719122013-10-16 02:36:47 -03002026 .vfl_dir = VFL_DIR_M2M,
2027};
2028
2029static struct v4l2_m2m_ops m2m_ops = {
2030 .device_run = device_run,
2031 .job_ready = job_ready,
2032 .job_abort = job_abort,
2033 .lock = vpe_lock,
2034 .unlock = vpe_unlock,
2035};
2036
2037static int vpe_runtime_get(struct platform_device *pdev)
2038{
2039 int r;
2040
2041 dev_dbg(&pdev->dev, "vpe_runtime_get\n");
2042
2043 r = pm_runtime_get_sync(&pdev->dev);
2044 WARN_ON(r < 0);
2045 return r < 0 ? r : 0;
2046}
2047
2048static void vpe_runtime_put(struct platform_device *pdev)
2049{
2050
2051 int r;
2052
2053 dev_dbg(&pdev->dev, "vpe_runtime_put\n");
2054
2055 r = pm_runtime_put_sync(&pdev->dev);
2056 WARN_ON(r < 0 && r != -ENOSYS);
2057}
2058
Archit Tanejab2c94722014-03-13 08:44:04 -03002059static void vpe_fw_cb(struct platform_device *pdev)
2060{
2061 struct vpe_dev *dev = platform_get_drvdata(pdev);
2062 struct video_device *vfd;
2063 int ret;
2064
2065 vfd = &dev->vfd;
2066 *vfd = vpe_videodev;
2067 vfd->lock = &dev->dev_mutex;
2068 vfd->v4l2_dev = &dev->v4l2_dev;
2069
2070 ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
2071 if (ret) {
2072 vpe_err(dev, "Failed to register video device\n");
2073
2074 vpe_set_clock_enable(dev, 0);
2075 vpe_runtime_put(pdev);
2076 pm_runtime_disable(&pdev->dev);
2077 v4l2_m2m_release(dev->m2m_dev);
2078 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
2079 v4l2_device_unregister(&dev->v4l2_dev);
2080
2081 return;
2082 }
2083
2084 video_set_drvdata(vfd, dev);
2085 snprintf(vfd->name, sizeof(vfd->name), "%s", vpe_videodev.name);
2086 dev_info(dev->v4l2_dev.dev, "Device registered as /dev/video%d\n",
2087 vfd->num);
2088}
2089
Archit Taneja45719122013-10-16 02:36:47 -03002090static int vpe_probe(struct platform_device *pdev)
2091{
2092 struct vpe_dev *dev;
Archit Taneja45719122013-10-16 02:36:47 -03002093 int ret, irq, func;
2094
2095 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
Wei Yongjunb68231a2013-10-30 00:15:13 -03002096 if (!dev)
2097 return -ENOMEM;
Archit Taneja45719122013-10-16 02:36:47 -03002098
2099 spin_lock_init(&dev->lock);
2100
2101 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
2102 if (ret)
2103 return ret;
2104
2105 atomic_set(&dev->num_instances, 0);
2106 mutex_init(&dev->dev_mutex);
2107
Archit Taneja44687b22013-12-12 05:35:57 -03002108 dev->res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
2109 "vpe_top");
Archit Taneja45719122013-10-16 02:36:47 -03002110 /*
2111 * HACK: we get resource info from device tree in the form of a list of
2112 * VPE sub blocks, the driver currently uses only the base of vpe_top
2113 * for register access, the driver should be changed later to access
2114 * registers based on the sub block base addresses
2115 */
Archit Taneja44687b22013-12-12 05:35:57 -03002116 dev->base = devm_ioremap(&pdev->dev, dev->res->start, SZ_32K);
Wei Yongjunb68231a2013-10-30 00:15:13 -03002117 if (!dev->base) {
2118 ret = -ENOMEM;
Archit Taneja45719122013-10-16 02:36:47 -03002119 goto v4l2_dev_unreg;
2120 }
2121
2122 irq = platform_get_irq(pdev, 0);
2123 ret = devm_request_irq(&pdev->dev, irq, vpe_irq, 0, VPE_MODULE_NAME,
2124 dev);
2125 if (ret)
2126 goto v4l2_dev_unreg;
2127
2128 platform_set_drvdata(pdev, dev);
2129
2130 dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
2131 if (IS_ERR(dev->alloc_ctx)) {
2132 vpe_err(dev, "Failed to alloc vb2 context\n");
2133 ret = PTR_ERR(dev->alloc_ctx);
2134 goto v4l2_dev_unreg;
2135 }
2136
2137 dev->m2m_dev = v4l2_m2m_init(&m2m_ops);
2138 if (IS_ERR(dev->m2m_dev)) {
2139 vpe_err(dev, "Failed to init mem2mem device\n");
2140 ret = PTR_ERR(dev->m2m_dev);
2141 goto rel_ctx;
2142 }
2143
2144 pm_runtime_enable(&pdev->dev);
2145
2146 ret = vpe_runtime_get(pdev);
2147 if (ret)
2148 goto rel_m2m;
2149
2150 /* Perform clk enable followed by reset */
2151 vpe_set_clock_enable(dev, 1);
2152
2153 vpe_top_reset(dev);
2154
2155 func = read_field_reg(dev, VPE_PID, VPE_PID_FUNC_MASK,
2156 VPE_PID_FUNC_SHIFT);
2157 vpe_dbg(dev, "VPE PID function %x\n", func);
2158
2159 vpe_top_vpdma_reset(dev);
2160
Archit Taneja44687b22013-12-12 05:35:57 -03002161 dev->sc = sc_create(pdev);
2162 if (IS_ERR(dev->sc)) {
2163 ret = PTR_ERR(dev->sc);
2164 goto runtime_put;
2165 }
2166
Archit Taneja69480822013-12-12 05:36:01 -03002167 dev->csc = csc_create(pdev);
2168 if (IS_ERR(dev->csc)) {
2169 ret = PTR_ERR(dev->csc);
2170 goto runtime_put;
2171 }
2172
Archit Tanejab2c94722014-03-13 08:44:04 -03002173 dev->vpdma = vpdma_create(pdev, vpe_fw_cb);
Wei Yongjun6676caf2013-10-30 00:10:45 -03002174 if (IS_ERR(dev->vpdma)) {
2175 ret = PTR_ERR(dev->vpdma);
Archit Taneja45719122013-10-16 02:36:47 -03002176 goto runtime_put;
Wei Yongjun6676caf2013-10-30 00:10:45 -03002177 }
Archit Taneja45719122013-10-16 02:36:47 -03002178
Archit Taneja45719122013-10-16 02:36:47 -03002179 return 0;
2180
2181runtime_put:
2182 vpe_runtime_put(pdev);
2183rel_m2m:
2184 pm_runtime_disable(&pdev->dev);
2185 v4l2_m2m_release(dev->m2m_dev);
2186rel_ctx:
2187 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
2188v4l2_dev_unreg:
2189 v4l2_device_unregister(&dev->v4l2_dev);
2190
2191 return ret;
2192}
2193
2194static int vpe_remove(struct platform_device *pdev)
2195{
2196 struct vpe_dev *dev =
2197 (struct vpe_dev *) platform_get_drvdata(pdev);
2198
2199 v4l2_info(&dev->v4l2_dev, "Removing " VPE_MODULE_NAME);
2200
2201 v4l2_m2m_release(dev->m2m_dev);
2202 video_unregister_device(&dev->vfd);
2203 v4l2_device_unregister(&dev->v4l2_dev);
2204 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
2205
2206 vpe_set_clock_enable(dev, 0);
2207 vpe_runtime_put(pdev);
2208 pm_runtime_disable(&pdev->dev);
2209
2210 return 0;
2211}
2212
2213#if defined(CONFIG_OF)
2214static const struct of_device_id vpe_of_match[] = {
2215 {
2216 .compatible = "ti,vpe",
2217 },
2218 {},
2219};
2220#else
2221#define vpe_of_match NULL
2222#endif
2223
2224static struct platform_driver vpe_pdrv = {
2225 .probe = vpe_probe,
2226 .remove = vpe_remove,
2227 .driver = {
2228 .name = VPE_MODULE_NAME,
2229 .owner = THIS_MODULE,
2230 .of_match_table = vpe_of_match,
2231 },
2232};
2233
Wei Yongjun903cbb82013-10-30 00:09:44 -03002234module_platform_driver(vpe_pdrv);
Archit Taneja45719122013-10-16 02:36:47 -03002235
2236MODULE_DESCRIPTION("TI VPE driver");
2237MODULE_AUTHOR("Dale Farnsworth, <dale@farnsworth.org>");
2238MODULE_LICENSE("GPL");