blob: f6d790af5a2dd489ece195413fe660efdbba7e31 [file] [log] [blame]
Javier Martin186b2502012-07-26 05:53:35 -03001/*
2 * Coda multi-standard codec IP
3 *
4 * Copyright (C) 2012 Vista Silicon S.L.
5 * Javier Martin, <javier.martin@vista-silicon.com>
6 * Xavier Duret
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 */
13
14#include <linux/clk.h>
15#include <linux/delay.h>
16#include <linux/firmware.h>
Philipp Zabel657eee72013-04-29 16:17:14 -070017#include <linux/genalloc.h>
Javier Martin186b2502012-07-26 05:53:35 -030018#include <linux/interrupt.h>
19#include <linux/io.h>
20#include <linux/irq.h>
21#include <linux/module.h>
22#include <linux/of_device.h>
23#include <linux/platform_device.h>
24#include <linux/slab.h>
25#include <linux/videodev2.h>
26#include <linux/of.h>
Philipp Zabel657eee72013-04-29 16:17:14 -070027#include <linux/platform_data/coda.h>
Javier Martin186b2502012-07-26 05:53:35 -030028
29#include <media/v4l2-ctrls.h>
30#include <media/v4l2-device.h>
31#include <media/v4l2-ioctl.h>
32#include <media/v4l2-mem2mem.h>
33#include <media/videobuf2-core.h>
34#include <media/videobuf2-dma-contig.h>
35
36#include "coda.h"
37
38#define CODA_NAME "coda"
39
40#define CODA_MAX_INSTANCES 4
41
42#define CODA_FMO_BUF_SIZE 32
43#define CODADX6_WORK_BUF_SIZE (288 * 1024 + CODA_FMO_BUF_SIZE * 8 * 1024)
44#define CODA7_WORK_BUF_SIZE (512 * 1024 + CODA_FMO_BUF_SIZE * 8 * 1024)
45#define CODA_PARA_BUF_SIZE (10 * 1024)
46#define CODA_ISRAM_SIZE (2048 * 2)
Philipp Zabel657eee72013-04-29 16:17:14 -070047#define CODADX6_IRAM_SIZE 0xb000
Philipp Zabel10436672012-07-02 09:03:55 -030048#define CODA7_IRAM_SIZE 0x14000 /* 81920 bytes */
Javier Martin186b2502012-07-26 05:53:35 -030049
Philipp Zabelec25f682012-07-20 08:54:29 -030050#define CODA_MAX_FRAMEBUFFERS 2
Javier Martin186b2502012-07-26 05:53:35 -030051
Philipp Zabelb96904e2013-05-23 10:42:58 -030052#define MAX_W 8192
53#define MAX_H 8192
54#define CODA_MAX_FRAME_SIZE 0x100000
Javier Martin186b2502012-07-26 05:53:35 -030055#define FMO_SLICE_SAVE_BUF_SIZE (32)
56#define CODA_DEFAULT_GAMMA 4096
57
58#define MIN_W 176
59#define MIN_H 144
Javier Martin186b2502012-07-26 05:53:35 -030060
61#define S_ALIGN 1 /* multiple of 2 */
62#define W_ALIGN 1 /* multiple of 2 */
63#define H_ALIGN 1 /* multiple of 2 */
64
65#define fh_to_ctx(__fh) container_of(__fh, struct coda_ctx, fh)
66
67static int coda_debug;
Philipp Zabelc4eb1bfc2013-05-21 04:24:16 -030068module_param(coda_debug, int, 0644);
Javier Martin186b2502012-07-26 05:53:35 -030069MODULE_PARM_DESC(coda_debug, "Debug level (0-1)");
70
71enum {
72 V4L2_M2M_SRC = 0,
73 V4L2_M2M_DST = 1,
74};
75
Javier Martin186b2502012-07-26 05:53:35 -030076enum coda_inst_type {
77 CODA_INST_ENCODER,
78 CODA_INST_DECODER,
79};
80
81enum coda_product {
82 CODA_DX6 = 0xf001,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -030083 CODA_7541 = 0xf012,
Javier Martin186b2502012-07-26 05:53:35 -030084};
85
86struct coda_fmt {
87 char *name;
88 u32 fourcc;
Philipp Zabelb96904e2013-05-23 10:42:58 -030089};
90
91struct coda_codec {
92 u32 mode;
93 u32 src_fourcc;
94 u32 dst_fourcc;
95 u32 max_w;
96 u32 max_h;
Javier Martin186b2502012-07-26 05:53:35 -030097};
98
99struct coda_devtype {
100 char *firmware;
101 enum coda_product product;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300102 struct coda_codec *codecs;
103 unsigned int num_codecs;
Javier Martin186b2502012-07-26 05:53:35 -0300104 size_t workbuf_size;
105};
106
107/* Per-queue, driver-specific private data */
108struct coda_q_data {
109 unsigned int width;
110 unsigned int height;
111 unsigned int sizeimage;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300112 unsigned int fourcc;
Javier Martin186b2502012-07-26 05:53:35 -0300113};
114
115struct coda_aux_buf {
116 void *vaddr;
117 dma_addr_t paddr;
118 u32 size;
119};
120
121struct coda_dev {
122 struct v4l2_device v4l2_dev;
123 struct video_device vfd;
124 struct platform_device *plat_dev;
Emil Goodec06d8752012-08-14 17:44:42 -0300125 const struct coda_devtype *devtype;
Javier Martin186b2502012-07-26 05:53:35 -0300126
127 void __iomem *regs_base;
128 struct clk *clk_per;
129 struct clk *clk_ahb;
130
131 struct coda_aux_buf codebuf;
132 struct coda_aux_buf workbuf;
Philipp Zabel657eee72013-04-29 16:17:14 -0700133 struct gen_pool *iram_pool;
134 long unsigned int iram_vaddr;
Philipp Zabel10436672012-07-02 09:03:55 -0300135 long unsigned int iram_paddr;
Philipp Zabel657eee72013-04-29 16:17:14 -0700136 unsigned long iram_size;
Javier Martin186b2502012-07-26 05:53:35 -0300137
138 spinlock_t irqlock;
139 struct mutex dev_mutex;
Philipp Zabelfcb62822013-05-23 10:43:00 -0300140 struct mutex coda_mutex;
Javier Martin186b2502012-07-26 05:53:35 -0300141 struct v4l2_m2m_dev *m2m_dev;
142 struct vb2_alloc_ctx *alloc_ctx;
Philipp Zabele11f3e62012-07-25 09:16:58 -0300143 struct list_head instances;
144 unsigned long instance_mask;
Philipp Zabel2fb57f02012-07-25 09:22:07 -0300145 struct delayed_work timeout;
Javier Martin186b2502012-07-26 05:53:35 -0300146};
147
148struct coda_params {
Philipp Zabel8f35c7b2012-07-09 04:25:52 -0300149 u8 rot_mode;
Javier Martin186b2502012-07-26 05:53:35 -0300150 u8 h264_intra_qp;
151 u8 h264_inter_qp;
152 u8 mpeg4_intra_qp;
153 u8 mpeg4_inter_qp;
154 u8 gop_size;
155 int codec_mode;
156 enum v4l2_mpeg_video_multi_slice_mode slice_mode;
157 u32 framerate;
158 u16 bitrate;
Philipp Zabelc566c782012-08-08 11:59:38 -0300159 u32 slice_max_bits;
Javier Martin186b2502012-07-26 05:53:35 -0300160 u32 slice_max_mb;
161};
162
Philipp Zabelc2d22512013-06-21 03:55:28 -0300163struct coda_iram_info {
164 u32 axi_sram_use;
165 phys_addr_t buf_bit_use;
166 phys_addr_t buf_ip_ac_dc_use;
167 phys_addr_t buf_dbk_y_use;
168 phys_addr_t buf_dbk_c_use;
169 phys_addr_t buf_ovl_use;
170 phys_addr_t buf_btp_use;
171 phys_addr_t search_ram_paddr;
172 int search_ram_size;
173};
174
Javier Martin186b2502012-07-26 05:53:35 -0300175struct coda_ctx {
176 struct coda_dev *dev;
Philipp Zabele11f3e62012-07-25 09:16:58 -0300177 struct list_head list;
Javier Martin186b2502012-07-26 05:53:35 -0300178 int aborting;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300179 int streamon_out;
180 int streamon_cap;
Javier Martin186b2502012-07-26 05:53:35 -0300181 u32 isequence;
182 struct coda_q_data q_data[2];
183 enum coda_inst_type inst_type;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300184 struct coda_codec *codec;
Javier Martin186b2502012-07-26 05:53:35 -0300185 enum v4l2_colorspace colorspace;
186 struct coda_params params;
187 struct v4l2_m2m_ctx *m2m_ctx;
188 struct v4l2_ctrl_handler ctrls;
189 struct v4l2_fh fh;
Javier Martin186b2502012-07-26 05:53:35 -0300190 int gopcounter;
191 char vpu_header[3][64];
192 int vpu_header_size[3];
193 struct coda_aux_buf parabuf;
Philipp Zabelec25f682012-07-20 08:54:29 -0300194 struct coda_aux_buf internal_frames[CODA_MAX_FRAMEBUFFERS];
195 int num_internal_frames;
Javier Martin186b2502012-07-26 05:53:35 -0300196 int idx;
Philipp Zabelc2d22512013-06-21 03:55:28 -0300197 struct coda_iram_info iram_info;
Javier Martin186b2502012-07-26 05:53:35 -0300198};
199
Javier Martin832fbb52012-10-29 08:34:59 -0300200static const u8 coda_filler_nal[14] = { 0x00, 0x00, 0x00, 0x01, 0x0c, 0xff,
201 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80 };
202static const u8 coda_filler_size[8] = { 0, 7, 14, 13, 12, 11, 10, 9 };
Javier Martin3f3f5c72012-10-29 05:20:29 -0300203
Javier Martin186b2502012-07-26 05:53:35 -0300204static inline void coda_write(struct coda_dev *dev, u32 data, u32 reg)
205{
206 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
207 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
208 writel(data, dev->regs_base + reg);
209}
210
211static inline unsigned int coda_read(struct coda_dev *dev, u32 reg)
212{
213 u32 data;
214 data = readl(dev->regs_base + reg);
215 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
216 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
217 return data;
218}
219
220static inline unsigned long coda_isbusy(struct coda_dev *dev)
221{
222 return coda_read(dev, CODA_REG_BIT_BUSY);
223}
224
225static inline int coda_is_initialized(struct coda_dev *dev)
226{
227 return (coda_read(dev, CODA_REG_BIT_CUR_PC) != 0);
228}
229
230static int coda_wait_timeout(struct coda_dev *dev)
231{
232 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
233
234 while (coda_isbusy(dev)) {
235 if (time_after(jiffies, timeout))
236 return -ETIMEDOUT;
237 }
238 return 0;
239}
240
241static void coda_command_async(struct coda_ctx *ctx, int cmd)
242{
243 struct coda_dev *dev = ctx->dev;
244 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
245
246 coda_write(dev, ctx->idx, CODA_REG_BIT_RUN_INDEX);
247 coda_write(dev, ctx->params.codec_mode, CODA_REG_BIT_RUN_COD_STD);
248 coda_write(dev, cmd, CODA_REG_BIT_RUN_COMMAND);
249}
250
251static int coda_command_sync(struct coda_ctx *ctx, int cmd)
252{
253 struct coda_dev *dev = ctx->dev;
254
255 coda_command_async(ctx, cmd);
256 return coda_wait_timeout(dev);
257}
258
259static struct coda_q_data *get_q_data(struct coda_ctx *ctx,
260 enum v4l2_buf_type type)
261{
262 switch (type) {
263 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
264 return &(ctx->q_data[V4L2_M2M_SRC]);
265 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
266 return &(ctx->q_data[V4L2_M2M_DST]);
267 default:
268 BUG();
269 }
270 return NULL;
271}
272
273/*
Philipp Zabelb96904e2013-05-23 10:42:58 -0300274 * Array of all formats supported by any version of Coda:
Javier Martin186b2502012-07-26 05:53:35 -0300275 */
Philipp Zabelb96904e2013-05-23 10:42:58 -0300276static struct coda_fmt coda_formats[] = {
Javier Martin186b2502012-07-26 05:53:35 -0300277 {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300278 .name = "YUV 4:2:0 Planar, YCbCr",
Javier Martin186b2502012-07-26 05:53:35 -0300279 .fourcc = V4L2_PIX_FMT_YUV420,
Philipp Zabelb96904e2013-05-23 10:42:58 -0300280 },
281 {
282 .name = "YUV 4:2:0 Planar, YCrCb",
283 .fourcc = V4L2_PIX_FMT_YVU420,
Javier Martin186b2502012-07-26 05:53:35 -0300284 },
285 {
286 .name = "H264 Encoded Stream",
287 .fourcc = V4L2_PIX_FMT_H264,
Javier Martin186b2502012-07-26 05:53:35 -0300288 },
289 {
290 .name = "MPEG4 Encoded Stream",
291 .fourcc = V4L2_PIX_FMT_MPEG4,
Javier Martin186b2502012-07-26 05:53:35 -0300292 },
293};
294
Philipp Zabelb96904e2013-05-23 10:42:58 -0300295#define CODA_CODEC(mode, src_fourcc, dst_fourcc, max_w, max_h) \
296 { mode, src_fourcc, dst_fourcc, max_w, max_h }
297
298/*
299 * Arrays of codecs supported by each given version of Coda:
300 * i.MX27 -> codadx6
301 * i.MX5x -> coda7
302 * i.MX6 -> coda960
303 * Use V4L2_PIX_FMT_YUV420 as placeholder for all supported YUV 4:2:0 variants
304 */
305static struct coda_codec codadx6_codecs[] = {
306 CODA_CODEC(CODADX6_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 720, 576),
307 CODA_CODEC(CODADX6_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 720, 576),
Philipp Zabeldf1e74c2012-07-02 06:07:10 -0300308};
309
Philipp Zabelb96904e2013-05-23 10:42:58 -0300310static struct coda_codec coda7_codecs[] = {
311 CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1280, 720),
312 CODA_CODEC(CODA7_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1280, 720),
313};
314
315static bool coda_format_is_yuv(u32 fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300316{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300317 switch (fourcc) {
318 case V4L2_PIX_FMT_YUV420:
319 case V4L2_PIX_FMT_YVU420:
320 return true;
321 default:
322 return false;
323 }
324}
Javier Martin186b2502012-07-26 05:53:35 -0300325
Philipp Zabelb96904e2013-05-23 10:42:58 -0300326/*
327 * Normalize all supported YUV 4:2:0 formats to the value used in the codec
328 * tables.
329 */
330static u32 coda_format_normalize_yuv(u32 fourcc)
331{
332 return coda_format_is_yuv(fourcc) ? V4L2_PIX_FMT_YUV420 : fourcc;
333}
334
335static struct coda_codec *coda_find_codec(struct coda_dev *dev, int src_fourcc,
336 int dst_fourcc)
337{
338 struct coda_codec *codecs = dev->devtype->codecs;
339 int num_codecs = dev->devtype->num_codecs;
340 int k;
341
342 src_fourcc = coda_format_normalize_yuv(src_fourcc);
343 dst_fourcc = coda_format_normalize_yuv(dst_fourcc);
344 if (src_fourcc == dst_fourcc)
345 return NULL;
346
347 for (k = 0; k < num_codecs; k++) {
348 if (codecs[k].src_fourcc == src_fourcc &&
349 codecs[k].dst_fourcc == dst_fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300350 break;
351 }
352
Philipp Zabelb96904e2013-05-23 10:42:58 -0300353 if (k == num_codecs)
Javier Martin186b2502012-07-26 05:53:35 -0300354 return NULL;
355
Philipp Zabelb96904e2013-05-23 10:42:58 -0300356 return &codecs[k];
Javier Martin186b2502012-07-26 05:53:35 -0300357}
358
359/*
360 * V4L2 ioctl() operations.
361 */
362static int vidioc_querycap(struct file *file, void *priv,
363 struct v4l2_capability *cap)
364{
365 strlcpy(cap->driver, CODA_NAME, sizeof(cap->driver));
366 strlcpy(cap->card, CODA_NAME, sizeof(cap->card));
Philipp Zabeldad0e1c2013-05-21 04:18:22 -0300367 strlcpy(cap->bus_info, "platform:" CODA_NAME, sizeof(cap->bus_info));
Sylwester Nawrockic3aac8b2012-08-22 18:00:19 -0300368 /*
369 * This is only a mem-to-mem video device. The capture and output
370 * device capability flags are left only for backward compatibility
371 * and are scheduled for removal.
372 */
373 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT |
374 V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
Javier Martin186b2502012-07-26 05:53:35 -0300375 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
376
377 return 0;
378}
379
380static int enum_fmt(void *priv, struct v4l2_fmtdesc *f,
Philipp Zabelb96904e2013-05-23 10:42:58 -0300381 enum v4l2_buf_type type)
Javier Martin186b2502012-07-26 05:53:35 -0300382{
383 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300384 struct coda_codec *codecs = ctx->dev->devtype->codecs;
385 struct coda_fmt *formats = coda_formats;
Javier Martin186b2502012-07-26 05:53:35 -0300386 struct coda_fmt *fmt;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300387 int num_codecs = ctx->dev->devtype->num_codecs;
388 int num_formats = ARRAY_SIZE(coda_formats);
389 int i, k, num = 0;
Javier Martin186b2502012-07-26 05:53:35 -0300390
391 for (i = 0; i < num_formats; i++) {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300392 /* Both uncompressed formats are always supported */
393 if (coda_format_is_yuv(formats[i].fourcc)) {
394 if (num == f->index)
395 break;
396 ++num;
397 continue;
398 }
399 /* Compressed formats may be supported, check the codec list */
400 for (k = 0; k < num_codecs; k++) {
401 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
402 formats[i].fourcc == codecs[k].dst_fourcc)
403 break;
404 if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
405 formats[i].fourcc == codecs[k].src_fourcc)
406 break;
407 }
408 if (k < num_codecs) {
Javier Martin186b2502012-07-26 05:53:35 -0300409 if (num == f->index)
410 break;
411 ++num;
412 }
413 }
414
415 if (i < num_formats) {
416 fmt = &formats[i];
417 strlcpy(f->description, fmt->name, sizeof(f->description));
418 f->pixelformat = fmt->fourcc;
419 return 0;
420 }
421
422 /* Format not found */
423 return -EINVAL;
424}
425
426static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
427 struct v4l2_fmtdesc *f)
428{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300429 return enum_fmt(priv, f, V4L2_BUF_TYPE_VIDEO_CAPTURE);
Javier Martin186b2502012-07-26 05:53:35 -0300430}
431
432static int vidioc_enum_fmt_vid_out(struct file *file, void *priv,
433 struct v4l2_fmtdesc *f)
434{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300435 return enum_fmt(priv, f, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Javier Martin186b2502012-07-26 05:53:35 -0300436}
437
438static int vidioc_g_fmt(struct file *file, void *priv, struct v4l2_format *f)
439{
440 struct vb2_queue *vq;
441 struct coda_q_data *q_data;
442 struct coda_ctx *ctx = fh_to_ctx(priv);
443
444 vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
445 if (!vq)
446 return -EINVAL;
447
448 q_data = get_q_data(ctx, f->type);
449
450 f->fmt.pix.field = V4L2_FIELD_NONE;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300451 f->fmt.pix.pixelformat = q_data->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -0300452 f->fmt.pix.width = q_data->width;
453 f->fmt.pix.height = q_data->height;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300454 if (coda_format_is_yuv(f->fmt.pix.pixelformat))
Javier Martin186b2502012-07-26 05:53:35 -0300455 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 2);
456 else /* encoded formats h.264/mpeg4 */
457 f->fmt.pix.bytesperline = 0;
458
459 f->fmt.pix.sizeimage = q_data->sizeimage;
460 f->fmt.pix.colorspace = ctx->colorspace;
461
462 return 0;
463}
464
Philipp Zabelb96904e2013-05-23 10:42:58 -0300465static int vidioc_try_fmt(struct coda_codec *codec, struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300466{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300467 unsigned int max_w, max_h;
Javier Martin186b2502012-07-26 05:53:35 -0300468 enum v4l2_field field;
469
470 field = f->fmt.pix.field;
471 if (field == V4L2_FIELD_ANY)
472 field = V4L2_FIELD_NONE;
473 else if (V4L2_FIELD_NONE != field)
474 return -EINVAL;
475
476 /* V4L2 specification suggests the driver corrects the format struct
477 * if any of the dimensions is unsupported */
478 f->fmt.pix.field = field;
479
Philipp Zabelb96904e2013-05-23 10:42:58 -0300480 if (codec) {
481 max_w = codec->max_w;
482 max_h = codec->max_h;
483 } else {
484 max_w = MAX_W;
485 max_h = MAX_H;
486 }
487 v4l_bound_align_image(&f->fmt.pix.width, MIN_W, max_w,
488 W_ALIGN, &f->fmt.pix.height,
489 MIN_H, max_h, H_ALIGN, S_ALIGN);
490
491 if (coda_format_is_yuv(f->fmt.pix.pixelformat)) {
Philipp Zabel47cf0c62013-05-23 10:42:54 -0300492 /* Frame stride must be multiple of 8 */
493 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 8);
494 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
Philipp Zabel451d43a2012-07-26 09:18:27 -0300495 f->fmt.pix.height * 3 / 2;
Javier Martin186b2502012-07-26 05:53:35 -0300496 } else { /*encoded formats h.264/mpeg4 */
497 f->fmt.pix.bytesperline = 0;
498 f->fmt.pix.sizeimage = CODA_MAX_FRAME_SIZE;
499 }
500
501 return 0;
502}
503
504static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
505 struct v4l2_format *f)
506{
Javier Martin186b2502012-07-26 05:53:35 -0300507 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300508 struct coda_codec *codec = NULL;
Javier Martin186b2502012-07-26 05:53:35 -0300509
Philipp Zabelb96904e2013-05-23 10:42:58 -0300510 /* Determine codec by the encoded format */
511 codec = coda_find_codec(ctx->dev, V4L2_PIX_FMT_YUV420,
512 f->fmt.pix.pixelformat);
Javier Martin186b2502012-07-26 05:53:35 -0300513
514 f->fmt.pix.colorspace = ctx->colorspace;
515
Philipp Zabelb96904e2013-05-23 10:42:58 -0300516 return vidioc_try_fmt(codec, f);
Javier Martin186b2502012-07-26 05:53:35 -0300517}
518
519static int vidioc_try_fmt_vid_out(struct file *file, void *priv,
520 struct v4l2_format *f)
521{
522 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300523 struct coda_codec *codec;
Javier Martin186b2502012-07-26 05:53:35 -0300524
Philipp Zabelb96904e2013-05-23 10:42:58 -0300525 /* Determine codec by encoded format, returns NULL if raw or invalid */
526 codec = coda_find_codec(ctx->dev, f->fmt.pix.pixelformat,
527 V4L2_PIX_FMT_YUV420);
Javier Martin186b2502012-07-26 05:53:35 -0300528
529 if (!f->fmt.pix.colorspace)
530 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
531
Philipp Zabelb96904e2013-05-23 10:42:58 -0300532 return vidioc_try_fmt(codec, f);
Javier Martin186b2502012-07-26 05:53:35 -0300533}
534
535static int vidioc_s_fmt(struct coda_ctx *ctx, struct v4l2_format *f)
536{
537 struct coda_q_data *q_data;
538 struct vb2_queue *vq;
Javier Martin186b2502012-07-26 05:53:35 -0300539
540 vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
541 if (!vq)
542 return -EINVAL;
543
544 q_data = get_q_data(ctx, f->type);
545 if (!q_data)
546 return -EINVAL;
547
548 if (vb2_is_busy(vq)) {
549 v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
550 return -EBUSY;
551 }
552
Philipp Zabelb96904e2013-05-23 10:42:58 -0300553 q_data->fourcc = f->fmt.pix.pixelformat;
Javier Martin186b2502012-07-26 05:53:35 -0300554 q_data->width = f->fmt.pix.width;
555 q_data->height = f->fmt.pix.height;
Philipp Zabel451d43a2012-07-26 09:18:27 -0300556 q_data->sizeimage = f->fmt.pix.sizeimage;
Javier Martin186b2502012-07-26 05:53:35 -0300557
558 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
559 "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
Philipp Zabelb96904e2013-05-23 10:42:58 -0300560 f->type, q_data->width, q_data->height, q_data->fourcc);
Javier Martin186b2502012-07-26 05:53:35 -0300561
562 return 0;
563}
564
565static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
566 struct v4l2_format *f)
567{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300568 struct coda_ctx *ctx = fh_to_ctx(priv);
Javier Martin186b2502012-07-26 05:53:35 -0300569 int ret;
570
571 ret = vidioc_try_fmt_vid_cap(file, priv, f);
572 if (ret)
573 return ret;
574
Philipp Zabelb96904e2013-05-23 10:42:58 -0300575 return vidioc_s_fmt(ctx, f);
Javier Martin186b2502012-07-26 05:53:35 -0300576}
577
578static int vidioc_s_fmt_vid_out(struct file *file, void *priv,
579 struct v4l2_format *f)
580{
581 struct coda_ctx *ctx = fh_to_ctx(priv);
582 int ret;
583
584 ret = vidioc_try_fmt_vid_out(file, priv, f);
585 if (ret)
586 return ret;
587
Richard Zhao8d621472012-08-21 02:47:42 -0300588 ret = vidioc_s_fmt(ctx, f);
Javier Martin186b2502012-07-26 05:53:35 -0300589 if (ret)
590 ctx->colorspace = f->fmt.pix.colorspace;
591
592 return ret;
593}
594
595static int vidioc_reqbufs(struct file *file, void *priv,
596 struct v4l2_requestbuffers *reqbufs)
597{
598 struct coda_ctx *ctx = fh_to_ctx(priv);
599
600 return v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
601}
602
603static int vidioc_querybuf(struct file *file, void *priv,
604 struct v4l2_buffer *buf)
605{
606 struct coda_ctx *ctx = fh_to_ctx(priv);
607
608 return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf);
609}
610
611static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
612{
613 struct coda_ctx *ctx = fh_to_ctx(priv);
614
615 return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
616}
617
Philipp Zabel419869c2013-05-23 07:06:30 -0300618static int vidioc_expbuf(struct file *file, void *priv,
619 struct v4l2_exportbuffer *eb)
620{
621 struct coda_ctx *ctx = fh_to_ctx(priv);
622
623 return v4l2_m2m_expbuf(file, ctx->m2m_ctx, eb);
624}
625
Javier Martin186b2502012-07-26 05:53:35 -0300626static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
627{
628 struct coda_ctx *ctx = fh_to_ctx(priv);
629
630 return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
631}
632
Philipp Zabel8fdf94a2013-05-21 04:16:29 -0300633static int vidioc_create_bufs(struct file *file, void *priv,
634 struct v4l2_create_buffers *create)
635{
636 struct coda_ctx *ctx = fh_to_ctx(priv);
637
638 return v4l2_m2m_create_bufs(file, ctx->m2m_ctx, create);
639}
640
Javier Martin186b2502012-07-26 05:53:35 -0300641static int vidioc_streamon(struct file *file, void *priv,
642 enum v4l2_buf_type type)
643{
644 struct coda_ctx *ctx = fh_to_ctx(priv);
645
646 return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
647}
648
649static int vidioc_streamoff(struct file *file, void *priv,
650 enum v4l2_buf_type type)
651{
652 struct coda_ctx *ctx = fh_to_ctx(priv);
653
654 return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
655}
656
657static const struct v4l2_ioctl_ops coda_ioctl_ops = {
658 .vidioc_querycap = vidioc_querycap,
659
660 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
661 .vidioc_g_fmt_vid_cap = vidioc_g_fmt,
662 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
663 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
664
665 .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
666 .vidioc_g_fmt_vid_out = vidioc_g_fmt,
667 .vidioc_try_fmt_vid_out = vidioc_try_fmt_vid_out,
668 .vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out,
669
670 .vidioc_reqbufs = vidioc_reqbufs,
671 .vidioc_querybuf = vidioc_querybuf,
672
673 .vidioc_qbuf = vidioc_qbuf,
Philipp Zabel419869c2013-05-23 07:06:30 -0300674 .vidioc_expbuf = vidioc_expbuf,
Javier Martin186b2502012-07-26 05:53:35 -0300675 .vidioc_dqbuf = vidioc_dqbuf,
Philipp Zabel8fdf94a2013-05-21 04:16:29 -0300676 .vidioc_create_bufs = vidioc_create_bufs,
Javier Martin186b2502012-07-26 05:53:35 -0300677
678 .vidioc_streamon = vidioc_streamon,
679 .vidioc_streamoff = vidioc_streamoff,
680};
681
682/*
683 * Mem-to-mem operations.
684 */
685static void coda_device_run(void *m2m_priv)
686{
687 struct coda_ctx *ctx = m2m_priv;
688 struct coda_q_data *q_data_src, *q_data_dst;
689 struct vb2_buffer *src_buf, *dst_buf;
690 struct coda_dev *dev = ctx->dev;
691 int force_ipicture;
692 int quant_param = 0;
693 u32 picture_y, picture_cb, picture_cr;
694 u32 pic_stream_buffer_addr, pic_stream_buffer_size;
695 u32 dst_fourcc;
696
Philipp Zabelfcb62822013-05-23 10:43:00 -0300697 mutex_lock(&dev->coda_mutex);
698
Javier Martin186b2502012-07-26 05:53:35 -0300699 src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
700 dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
701 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
702 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300703 dst_fourcc = q_data_dst->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -0300704
705 src_buf->v4l2_buf.sequence = ctx->isequence;
706 dst_buf->v4l2_buf.sequence = ctx->isequence;
707 ctx->isequence++;
708
709 /*
710 * Workaround coda firmware BUG that only marks the first
711 * frame as IDR. This is a problem for some decoders that can't
712 * recover when a frame is lost.
713 */
714 if (src_buf->v4l2_buf.sequence % ctx->params.gop_size) {
715 src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
716 src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
717 } else {
718 src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
719 src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
720 }
721
722 /*
723 * Copy headers at the beginning of the first frame for H.264 only.
724 * In MPEG4 they are already copied by the coda.
725 */
726 if (src_buf->v4l2_buf.sequence == 0) {
727 pic_stream_buffer_addr =
728 vb2_dma_contig_plane_dma_addr(dst_buf, 0) +
729 ctx->vpu_header_size[0] +
730 ctx->vpu_header_size[1] +
731 ctx->vpu_header_size[2];
732 pic_stream_buffer_size = CODA_MAX_FRAME_SIZE -
733 ctx->vpu_header_size[0] -
734 ctx->vpu_header_size[1] -
735 ctx->vpu_header_size[2];
736 memcpy(vb2_plane_vaddr(dst_buf, 0),
737 &ctx->vpu_header[0][0], ctx->vpu_header_size[0]);
738 memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0],
739 &ctx->vpu_header[1][0], ctx->vpu_header_size[1]);
740 memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0] +
741 ctx->vpu_header_size[1], &ctx->vpu_header[2][0],
742 ctx->vpu_header_size[2]);
743 } else {
744 pic_stream_buffer_addr =
745 vb2_dma_contig_plane_dma_addr(dst_buf, 0);
746 pic_stream_buffer_size = CODA_MAX_FRAME_SIZE;
747 }
748
749 if (src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) {
750 force_ipicture = 1;
751 switch (dst_fourcc) {
752 case V4L2_PIX_FMT_H264:
753 quant_param = ctx->params.h264_intra_qp;
754 break;
755 case V4L2_PIX_FMT_MPEG4:
756 quant_param = ctx->params.mpeg4_intra_qp;
757 break;
758 default:
759 v4l2_warn(&ctx->dev->v4l2_dev,
760 "cannot set intra qp, fmt not supported\n");
761 break;
762 }
763 } else {
764 force_ipicture = 0;
765 switch (dst_fourcc) {
766 case V4L2_PIX_FMT_H264:
767 quant_param = ctx->params.h264_inter_qp;
768 break;
769 case V4L2_PIX_FMT_MPEG4:
770 quant_param = ctx->params.mpeg4_inter_qp;
771 break;
772 default:
773 v4l2_warn(&ctx->dev->v4l2_dev,
774 "cannot set inter qp, fmt not supported\n");
775 break;
776 }
777 }
778
779 /* submit */
Philipp Zabel8f35c7b2012-07-09 04:25:52 -0300780 coda_write(dev, CODA_ROT_MIR_ENABLE | ctx->params.rot_mode, CODA_CMD_ENC_PIC_ROT_MODE);
Javier Martin186b2502012-07-26 05:53:35 -0300781 coda_write(dev, quant_param, CODA_CMD_ENC_PIC_QS);
782
783
784 picture_y = vb2_dma_contig_plane_dma_addr(src_buf, 0);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300785 switch (q_data_src->fourcc) {
786 case V4L2_PIX_FMT_YVU420:
787 /* Switch Cb and Cr for YVU420 format */
788 picture_cr = picture_y + q_data_src->width * q_data_src->height;
789 picture_cb = picture_cr + q_data_src->width / 2 *
790 q_data_src->height / 2;
791 break;
792 case V4L2_PIX_FMT_YUV420:
793 default:
794 picture_cb = picture_y + q_data_src->width * q_data_src->height;
795 picture_cr = picture_cb + q_data_src->width / 2 *
796 q_data_src->height / 2;
797 break;
798 }
Javier Martin186b2502012-07-26 05:53:35 -0300799
800 coda_write(dev, picture_y, CODA_CMD_ENC_PIC_SRC_ADDR_Y);
801 coda_write(dev, picture_cb, CODA_CMD_ENC_PIC_SRC_ADDR_CB);
802 coda_write(dev, picture_cr, CODA_CMD_ENC_PIC_SRC_ADDR_CR);
803 coda_write(dev, force_ipicture << 1 & 0x2,
804 CODA_CMD_ENC_PIC_OPTION);
805
806 coda_write(dev, pic_stream_buffer_addr, CODA_CMD_ENC_PIC_BB_START);
807 coda_write(dev, pic_stream_buffer_size / 1024,
808 CODA_CMD_ENC_PIC_BB_SIZE);
Philipp Zabel10436672012-07-02 09:03:55 -0300809
810 if (dev->devtype->product == CODA_7541) {
811 coda_write(dev, CODA7_USE_BIT_ENABLE | CODA7_USE_HOST_BIT_ENABLE |
812 CODA7_USE_ME_ENABLE | CODA7_USE_HOST_ME_ENABLE,
813 CODA7_REG_BIT_AXI_SRAM_USE);
814 }
815
Philipp Zabelc2d22512013-06-21 03:55:28 -0300816 if (dev->devtype->product != CODA_DX6)
817 coda_write(dev, ctx->iram_info.axi_sram_use,
818 CODA7_REG_BIT_AXI_SRAM_USE);
819
Philipp Zabel2fb57f02012-07-25 09:22:07 -0300820 /* 1 second timeout in case CODA locks up */
821 schedule_delayed_work(&dev->timeout, HZ);
822
Javier Martin186b2502012-07-26 05:53:35 -0300823 coda_command_async(ctx, CODA_COMMAND_PIC_RUN);
824}
825
826static int coda_job_ready(void *m2m_priv)
827{
828 struct coda_ctx *ctx = m2m_priv;
829
830 /*
831 * For both 'P' and 'key' frame cases 1 picture
832 * and 1 frame are needed.
833 */
834 if (!v4l2_m2m_num_src_bufs_ready(ctx->m2m_ctx) ||
835 !v4l2_m2m_num_dst_bufs_ready(ctx->m2m_ctx)) {
836 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
837 "not ready: not enough video buffers.\n");
838 return 0;
839 }
840
Philipp Zabel3e748262013-05-23 10:43:01 -0300841 if (ctx->aborting) {
842 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
843 "not ready: aborting\n");
844 return 0;
845 }
846
Javier Martin186b2502012-07-26 05:53:35 -0300847 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
848 "job ready\n");
849 return 1;
850}
851
852static void coda_job_abort(void *priv)
853{
854 struct coda_ctx *ctx = priv;
Javier Martin186b2502012-07-26 05:53:35 -0300855
856 ctx->aborting = 1;
857
858 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
859 "Aborting task\n");
Javier Martin186b2502012-07-26 05:53:35 -0300860}
861
862static void coda_lock(void *m2m_priv)
863{
864 struct coda_ctx *ctx = m2m_priv;
865 struct coda_dev *pcdev = ctx->dev;
866 mutex_lock(&pcdev->dev_mutex);
867}
868
869static void coda_unlock(void *m2m_priv)
870{
871 struct coda_ctx *ctx = m2m_priv;
872 struct coda_dev *pcdev = ctx->dev;
873 mutex_unlock(&pcdev->dev_mutex);
874}
875
876static struct v4l2_m2m_ops coda_m2m_ops = {
877 .device_run = coda_device_run,
878 .job_ready = coda_job_ready,
879 .job_abort = coda_job_abort,
880 .lock = coda_lock,
881 .unlock = coda_unlock,
882};
883
884static void set_default_params(struct coda_ctx *ctx)
885{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300886 int max_w;
887 int max_h;
888
889 ctx->codec = &ctx->dev->devtype->codecs[0];
890 max_w = ctx->codec->max_w;
891 max_h = ctx->codec->max_h;
Javier Martin186b2502012-07-26 05:53:35 -0300892
893 ctx->params.codec_mode = CODA_MODE_INVALID;
894 ctx->colorspace = V4L2_COLORSPACE_REC709;
895 ctx->params.framerate = 30;
Javier Martin186b2502012-07-26 05:53:35 -0300896 ctx->aborting = 0;
897
898 /* Default formats for output and input queues */
Philipp Zabelb96904e2013-05-23 10:42:58 -0300899 ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->codec->src_fourcc;
900 ctx->q_data[V4L2_M2M_DST].fourcc = ctx->codec->dst_fourcc;
901 ctx->q_data[V4L2_M2M_SRC].width = max_w;
902 ctx->q_data[V4L2_M2M_SRC].height = max_h;
903 ctx->q_data[V4L2_M2M_SRC].sizeimage = (max_w * max_h * 3) / 2;
904 ctx->q_data[V4L2_M2M_DST].width = max_w;
905 ctx->q_data[V4L2_M2M_DST].height = max_h;
Javier Martin186b2502012-07-26 05:53:35 -0300906 ctx->q_data[V4L2_M2M_DST].sizeimage = CODA_MAX_FRAME_SIZE;
907}
908
909/*
910 * Queue operations
911 */
912static int coda_queue_setup(struct vb2_queue *vq,
913 const struct v4l2_format *fmt,
914 unsigned int *nbuffers, unsigned int *nplanes,
915 unsigned int sizes[], void *alloc_ctxs[])
916{
917 struct coda_ctx *ctx = vb2_get_drv_priv(vq);
Philipp Zabele34db062012-08-29 08:22:00 -0300918 struct coda_q_data *q_data;
Javier Martin186b2502012-07-26 05:53:35 -0300919 unsigned int size;
920
Philipp Zabele34db062012-08-29 08:22:00 -0300921 q_data = get_q_data(ctx, vq->type);
922 size = q_data->sizeimage;
Javier Martin186b2502012-07-26 05:53:35 -0300923
924 *nplanes = 1;
925 sizes[0] = size;
926
927 alloc_ctxs[0] = ctx->dev->alloc_ctx;
928
929 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
930 "get %d buffer(s) of size %d each.\n", *nbuffers, size);
931
932 return 0;
933}
934
935static int coda_buf_prepare(struct vb2_buffer *vb)
936{
937 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
938 struct coda_q_data *q_data;
939
940 q_data = get_q_data(ctx, vb->vb2_queue->type);
941
942 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
943 v4l2_warn(&ctx->dev->v4l2_dev,
944 "%s data will not fit into plane (%lu < %lu)\n",
945 __func__, vb2_plane_size(vb, 0),
946 (long)q_data->sizeimage);
947 return -EINVAL;
948 }
949
Javier Martin186b2502012-07-26 05:53:35 -0300950 return 0;
951}
952
953static void coda_buf_queue(struct vb2_buffer *vb)
954{
955 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
956 v4l2_m2m_buf_queue(ctx->m2m_ctx, vb);
957}
958
959static void coda_wait_prepare(struct vb2_queue *q)
960{
961 struct coda_ctx *ctx = vb2_get_drv_priv(q);
962 coda_unlock(ctx);
963}
964
965static void coda_wait_finish(struct vb2_queue *q)
966{
967 struct coda_ctx *ctx = vb2_get_drv_priv(q);
968 coda_lock(ctx);
969}
970
Philipp Zabelec25f682012-07-20 08:54:29 -0300971static void coda_free_framebuffers(struct coda_ctx *ctx)
972{
973 int i;
974
975 for (i = 0; i < CODA_MAX_FRAMEBUFFERS; i++) {
976 if (ctx->internal_frames[i].vaddr) {
977 dma_free_coherent(&ctx->dev->plat_dev->dev,
978 ctx->internal_frames[i].size,
979 ctx->internal_frames[i].vaddr,
980 ctx->internal_frames[i].paddr);
981 ctx->internal_frames[i].vaddr = NULL;
982 }
983 }
984}
985
Philipp Zabel86eda902013-05-23 10:42:57 -0300986static void coda_parabuf_write(struct coda_ctx *ctx, int index, u32 value)
987{
988 struct coda_dev *dev = ctx->dev;
989 u32 *p = ctx->parabuf.vaddr;
990
991 if (dev->devtype->product == CODA_DX6)
992 p[index] = value;
993 else
994 p[index ^ 1] = value;
995}
996
Philipp Zabelec25f682012-07-20 08:54:29 -0300997static int coda_alloc_framebuffers(struct coda_ctx *ctx, struct coda_q_data *q_data, u32 fourcc)
998{
999 struct coda_dev *dev = ctx->dev;
1000
1001 int height = q_data->height;
Philipp Zabel86eda902013-05-23 10:42:57 -03001002 dma_addr_t paddr;
1003 int ysize;
Philipp Zabelec25f682012-07-20 08:54:29 -03001004 int i;
1005
Philipp Zabel86eda902013-05-23 10:42:57 -03001006 ysize = round_up(q_data->width, 8) * height;
1007
Philipp Zabelec25f682012-07-20 08:54:29 -03001008 /* Allocate frame buffers */
1009 ctx->num_internal_frames = CODA_MAX_FRAMEBUFFERS;
1010 for (i = 0; i < ctx->num_internal_frames; i++) {
1011 ctx->internal_frames[i].size = q_data->sizeimage;
1012 if (fourcc == V4L2_PIX_FMT_H264 && dev->devtype->product != CODA_DX6)
Philipp Zabel86eda902013-05-23 10:42:57 -03001013 ctx->internal_frames[i].size += ysize/4;
Philipp Zabelec25f682012-07-20 08:54:29 -03001014 ctx->internal_frames[i].vaddr = dma_alloc_coherent(
1015 &dev->plat_dev->dev, ctx->internal_frames[i].size,
1016 &ctx->internal_frames[i].paddr, GFP_KERNEL);
1017 if (!ctx->internal_frames[i].vaddr) {
1018 coda_free_framebuffers(ctx);
1019 return -ENOMEM;
1020 }
1021 }
1022
1023 /* Register frame buffers in the parameter buffer */
Philipp Zabel86eda902013-05-23 10:42:57 -03001024 for (i = 0; i < ctx->num_internal_frames; i++) {
1025 paddr = ctx->internal_frames[i].paddr;
1026 coda_parabuf_write(ctx, i * 3 + 0, paddr); /* Y */
1027 coda_parabuf_write(ctx, i * 3 + 1, paddr + ysize); /* Cb */
1028 coda_parabuf_write(ctx, i * 3 + 2, paddr + ysize + ysize/4); /* Cr */
Philipp Zabelec25f682012-07-20 08:54:29 -03001029
Philipp Zabel86eda902013-05-23 10:42:57 -03001030 if (dev->devtype->product != CODA_DX6 && fourcc == V4L2_PIX_FMT_H264)
1031 coda_parabuf_write(ctx, 96 + i, ctx->internal_frames[i].paddr + ysize + ysize/4 + ysize/4);
Philipp Zabelec25f682012-07-20 08:54:29 -03001032 }
1033
1034 return 0;
1035}
1036
Javier Martin3f3f5c72012-10-29 05:20:29 -03001037static int coda_h264_padding(int size, char *p)
1038{
Javier Martin3f3f5c72012-10-29 05:20:29 -03001039 int nal_size;
1040 int diff;
1041
Javier Martin832fbb52012-10-29 08:34:59 -03001042 diff = size - (size & ~0x7);
Javier Martin3f3f5c72012-10-29 05:20:29 -03001043 if (diff == 0)
1044 return 0;
1045
Javier Martin832fbb52012-10-29 08:34:59 -03001046 nal_size = coda_filler_size[diff];
Javier Martin3f3f5c72012-10-29 05:20:29 -03001047 memcpy(p, coda_filler_nal, nal_size);
1048
1049 /* Add rbsp stop bit and trailing at the end */
1050 *(p + nal_size - 1) = 0x80;
1051
1052 return nal_size;
1053}
1054
Philipp Zabelc2d22512013-06-21 03:55:28 -03001055static void coda_setup_iram(struct coda_ctx *ctx)
1056{
1057 struct coda_iram_info *iram_info = &ctx->iram_info;
1058 struct coda_dev *dev = ctx->dev;
1059 int ipacdc_size;
1060 int bitram_size;
1061 int dbk_size;
1062 int mb_width;
1063 int me_size;
1064 int size;
1065
1066 memset(iram_info, 0, sizeof(*iram_info));
1067 size = dev->iram_size;
1068
1069 if (dev->devtype->product == CODA_DX6)
1070 return;
1071
1072 if (ctx->inst_type == CODA_INST_ENCODER) {
1073 struct coda_q_data *q_data_src;
1074
1075 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1076 mb_width = DIV_ROUND_UP(q_data_src->width, 16);
1077
1078 /* Prioritize in case IRAM is too small for everything */
1079 me_size = round_up(round_up(q_data_src->width, 16) * 36 + 2048,
1080 1024);
1081 iram_info->search_ram_size = me_size;
1082 if (size >= iram_info->search_ram_size) {
1083 if (dev->devtype->product == CODA_7541)
1084 iram_info->axi_sram_use |= CODA7_USE_HOST_ME_ENABLE;
1085 iram_info->search_ram_paddr = dev->iram_paddr;
1086 size -= iram_info->search_ram_size;
1087 } else {
1088 pr_err("IRAM is smaller than the search ram size\n");
1089 goto out;
1090 }
1091
1092 /* Only H.264BP and H.263P3 are considered */
1093 dbk_size = round_up(128 * mb_width, 1024);
1094 if (size >= dbk_size) {
1095 iram_info->axi_sram_use |= CODA7_USE_HOST_DBK_ENABLE;
1096 iram_info->buf_dbk_y_use = dev->iram_paddr +
1097 iram_info->search_ram_size;
1098 iram_info->buf_dbk_c_use = iram_info->buf_dbk_y_use +
1099 dbk_size / 2;
1100 size -= dbk_size;
1101 } else {
1102 goto out;
1103 }
1104
1105 bitram_size = round_up(128 * mb_width, 1024);
1106 if (size >= bitram_size) {
1107 iram_info->axi_sram_use |= CODA7_USE_HOST_BIT_ENABLE;
1108 iram_info->buf_bit_use = iram_info->buf_dbk_c_use +
1109 dbk_size / 2;
1110 size -= bitram_size;
1111 } else {
1112 goto out;
1113 }
1114
1115 ipacdc_size = round_up(128 * mb_width, 1024);
1116 if (size >= ipacdc_size) {
1117 iram_info->axi_sram_use |= CODA7_USE_HOST_IP_ENABLE;
1118 iram_info->buf_ip_ac_dc_use = iram_info->buf_bit_use +
1119 bitram_size;
1120 size -= ipacdc_size;
1121 }
1122
1123 /* OVL disabled for encoder */
1124 }
1125
1126out:
1127 switch (dev->devtype->product) {
1128 case CODA_DX6:
1129 break;
1130 case CODA_7541:
1131 /* i.MX53 uses secondary AXI for IRAM access */
1132 if (iram_info->axi_sram_use & CODA7_USE_HOST_BIT_ENABLE)
1133 iram_info->axi_sram_use |= CODA7_USE_BIT_ENABLE;
1134 if (iram_info->axi_sram_use & CODA7_USE_HOST_IP_ENABLE)
1135 iram_info->axi_sram_use |= CODA7_USE_IP_ENABLE;
1136 if (iram_info->axi_sram_use & CODA7_USE_HOST_DBK_ENABLE)
1137 iram_info->axi_sram_use |= CODA7_USE_DBK_ENABLE;
1138 if (iram_info->axi_sram_use & CODA7_USE_HOST_OVL_ENABLE)
1139 iram_info->axi_sram_use |= CODA7_USE_OVL_ENABLE;
1140 if (iram_info->axi_sram_use & CODA7_USE_HOST_ME_ENABLE)
1141 iram_info->axi_sram_use |= CODA7_USE_ME_ENABLE;
1142 }
1143
1144 if (!(iram_info->axi_sram_use & CODA7_USE_HOST_IP_ENABLE))
1145 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1146 "IRAM smaller than needed\n");
1147
1148 if (dev->devtype->product == CODA_7541) {
1149 /* TODO - Enabling these causes picture errors on CODA7541 */
1150 if (ctx->inst_type == CODA_INST_ENCODER) {
1151 iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE |
1152 CODA7_USE_HOST_DBK_ENABLE |
1153 CODA7_USE_IP_ENABLE |
1154 CODA7_USE_DBK_ENABLE);
1155 }
1156 }
1157}
1158
Philipp Zabeld35167a2013-05-23 10:42:59 -03001159static int coda_encode_header(struct coda_ctx *ctx, struct vb2_buffer *buf,
1160 int header_code, u8 *header, int *size)
1161{
1162 struct coda_dev *dev = ctx->dev;
1163 int ret;
1164
1165 coda_write(dev, vb2_dma_contig_plane_dma_addr(buf, 0),
1166 CODA_CMD_ENC_HEADER_BB_START);
1167 coda_write(dev, vb2_plane_size(buf, 0), CODA_CMD_ENC_HEADER_BB_SIZE);
1168 coda_write(dev, header_code, CODA_CMD_ENC_HEADER_CODE);
1169 ret = coda_command_sync(ctx, CODA_COMMAND_ENCODE_HEADER);
1170 if (ret < 0) {
1171 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_ENCODE_HEADER timeout\n");
1172 return ret;
1173 }
1174 *size = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->idx)) -
1175 coda_read(dev, CODA_CMD_ENC_HEADER_BB_START);
1176 memcpy(header, vb2_plane_vaddr(buf, 0), *size);
1177
1178 return 0;
1179}
1180
Javier Martin186b2502012-07-26 05:53:35 -03001181static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
1182{
1183 struct coda_ctx *ctx = vb2_get_drv_priv(q);
1184 struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
1185 u32 bitstream_buf, bitstream_size;
1186 struct coda_dev *dev = ctx->dev;
1187 struct coda_q_data *q_data_src, *q_data_dst;
Javier Martin186b2502012-07-26 05:53:35 -03001188 struct vb2_buffer *buf;
Philipp Zabelec25f682012-07-20 08:54:29 -03001189 u32 dst_fourcc;
Javier Martin186b2502012-07-26 05:53:35 -03001190 u32 value;
Philipp Zabeld35167a2013-05-23 10:42:59 -03001191 int ret = 0;
Javier Martin186b2502012-07-26 05:53:35 -03001192
1193 if (count < 1)
1194 return -EINVAL;
1195
1196 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
Philipp Zabelb96904e2013-05-23 10:42:58 -03001197 ctx->streamon_out = 1;
Javier Martin186b2502012-07-26 05:53:35 -03001198 else
Philipp Zabelb96904e2013-05-23 10:42:58 -03001199 ctx->streamon_cap = 1;
Javier Martin186b2502012-07-26 05:53:35 -03001200
Philipp Zabelb96904e2013-05-23 10:42:58 -03001201 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1202 if (ctx->streamon_out) {
1203 if (coda_format_is_yuv(q_data_src->fourcc))
1204 ctx->inst_type = CODA_INST_ENCODER;
1205 else
1206 ctx->inst_type = CODA_INST_DECODER;
1207 }
Javier Martin186b2502012-07-26 05:53:35 -03001208
Philipp Zabelb96904e2013-05-23 10:42:58 -03001209 /* Don't start the coda unless both queues are on */
1210 if (!(ctx->streamon_out & ctx->streamon_cap))
1211 return 0;
Javier Martin186b2502012-07-26 05:53:35 -03001212
Philipp Zabelb96904e2013-05-23 10:42:58 -03001213 ctx->gopcounter = ctx->params.gop_size - 1;
Javier Martin186b2502012-07-26 05:53:35 -03001214 buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
1215 bitstream_buf = vb2_dma_contig_plane_dma_addr(buf, 0);
1216 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1217 bitstream_size = q_data_dst->sizeimage;
Philipp Zabelb96904e2013-05-23 10:42:58 -03001218 dst_fourcc = q_data_dst->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -03001219
Philipp Zabelb96904e2013-05-23 10:42:58 -03001220 ctx->codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
1221 q_data_dst->fourcc);
1222 if (!ctx->codec) {
Javier Martin186b2502012-07-26 05:53:35 -03001223 v4l2_err(v4l2_dev, "couldn't tell instance type.\n");
1224 return -EINVAL;
1225 }
1226
1227 if (!coda_is_initialized(dev)) {
1228 v4l2_err(v4l2_dev, "coda is not initialized.\n");
1229 return -EFAULT;
1230 }
Philipp Zabelfcb62822013-05-23 10:43:00 -03001231
1232 mutex_lock(&dev->coda_mutex);
1233
Javier Martin186b2502012-07-26 05:53:35 -03001234 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
1235 coda_write(dev, bitstream_buf, CODA_REG_BIT_RD_PTR(ctx->idx));
1236 coda_write(dev, bitstream_buf, CODA_REG_BIT_WR_PTR(ctx->idx));
1237 switch (dev->devtype->product) {
1238 case CODA_DX6:
1239 coda_write(dev, CODADX6_STREAM_BUF_DYNALLOC_EN |
1240 CODADX6_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
1241 break;
1242 default:
1243 coda_write(dev, CODA7_STREAM_BUF_DYNALLOC_EN |
1244 CODA7_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
1245 }
1246
Philipp Zabel10436672012-07-02 09:03:55 -03001247 if (dev->devtype->product == CODA_DX6) {
1248 /* Configure the coda */
1249 coda_write(dev, dev->iram_paddr, CODADX6_REG_BIT_SEARCH_RAM_BASE_ADDR);
1250 }
Javier Martin186b2502012-07-26 05:53:35 -03001251
1252 /* Could set rotation here if needed */
1253 switch (dev->devtype->product) {
1254 case CODA_DX6:
1255 value = (q_data_src->width & CODADX6_PICWIDTH_MASK) << CODADX6_PICWIDTH_OFFSET;
Philipp Zabelb96904e2013-05-23 10:42:58 -03001256 value |= (q_data_src->height & CODADX6_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03001257 break;
1258 default:
1259 value = (q_data_src->width & CODA7_PICWIDTH_MASK) << CODA7_PICWIDTH_OFFSET;
Philipp Zabelb96904e2013-05-23 10:42:58 -03001260 value |= (q_data_src->height & CODA7_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03001261 }
Javier Martin186b2502012-07-26 05:53:35 -03001262 coda_write(dev, value, CODA_CMD_ENC_SEQ_SRC_SIZE);
1263 coda_write(dev, ctx->params.framerate,
1264 CODA_CMD_ENC_SEQ_SRC_F_RATE);
1265
Philipp Zabelb96904e2013-05-23 10:42:58 -03001266 ctx->params.codec_mode = ctx->codec->mode;
Javier Martin186b2502012-07-26 05:53:35 -03001267 switch (dst_fourcc) {
1268 case V4L2_PIX_FMT_MPEG4:
Javier Martin186b2502012-07-26 05:53:35 -03001269 coda_write(dev, CODA_STD_MPEG4, CODA_CMD_ENC_SEQ_COD_STD);
1270 coda_write(dev, 0, CODA_CMD_ENC_SEQ_MP4_PARA);
1271 break;
1272 case V4L2_PIX_FMT_H264:
Javier Martin186b2502012-07-26 05:53:35 -03001273 coda_write(dev, CODA_STD_H264, CODA_CMD_ENC_SEQ_COD_STD);
1274 coda_write(dev, 0, CODA_CMD_ENC_SEQ_264_PARA);
1275 break;
1276 default:
1277 v4l2_err(v4l2_dev,
1278 "dst format (0x%08x) invalid.\n", dst_fourcc);
Philipp Zabelfcb62822013-05-23 10:43:00 -03001279 ret = -EINVAL;
1280 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03001281 }
1282
Philipp Zabelc566c782012-08-08 11:59:38 -03001283 switch (ctx->params.slice_mode) {
1284 case V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE:
1285 value = 0;
1286 break;
1287 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_MB:
1288 value = (ctx->params.slice_max_mb & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
1289 value |= (1 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03001290 value |= 1 & CODA_SLICING_MODE_MASK;
Philipp Zabelc566c782012-08-08 11:59:38 -03001291 break;
1292 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES:
1293 value = (ctx->params.slice_max_bits & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
1294 value |= (0 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
1295 value |= 1 & CODA_SLICING_MODE_MASK;
1296 break;
1297 }
Javier Martin186b2502012-07-26 05:53:35 -03001298 coda_write(dev, value, CODA_CMD_ENC_SEQ_SLICE_MODE);
Philipp Zabelc566c782012-08-08 11:59:38 -03001299 value = ctx->params.gop_size & CODA_GOP_SIZE_MASK;
Javier Martin186b2502012-07-26 05:53:35 -03001300 coda_write(dev, value, CODA_CMD_ENC_SEQ_GOP_SIZE);
1301
1302 if (ctx->params.bitrate) {
1303 /* Rate control enabled */
1304 value = (ctx->params.bitrate & CODA_RATECONTROL_BITRATE_MASK) << CODA_RATECONTROL_BITRATE_OFFSET;
1305 value |= 1 & CODA_RATECONTROL_ENABLE_MASK;
1306 } else {
1307 value = 0;
1308 }
1309 coda_write(dev, value, CODA_CMD_ENC_SEQ_RC_PARA);
1310
1311 coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_BUF_SIZE);
1312 coda_write(dev, 0, CODA_CMD_ENC_SEQ_INTRA_REFRESH);
1313
1314 coda_write(dev, bitstream_buf, CODA_CMD_ENC_SEQ_BB_START);
1315 coda_write(dev, bitstream_size / 1024, CODA_CMD_ENC_SEQ_BB_SIZE);
1316
1317 /* set default gamma */
1318 value = (CODA_DEFAULT_GAMMA & CODA_GAMMA_MASK) << CODA_GAMMA_OFFSET;
1319 coda_write(dev, value, CODA_CMD_ENC_SEQ_RC_GAMMA);
1320
Philipp Zabelfb1fcf12013-05-23 10:42:53 -03001321 if (CODA_DEFAULT_GAMMA > 0) {
1322 if (dev->devtype->product == CODA_DX6)
1323 value = 1 << CODADX6_OPTION_GAMMA_OFFSET;
1324 else
1325 value = 1 << CODA7_OPTION_GAMMA_OFFSET;
1326 } else {
1327 value = 0;
1328 }
Javier Martin186b2502012-07-26 05:53:35 -03001329 coda_write(dev, value, CODA_CMD_ENC_SEQ_OPTION);
1330
Philipp Zabelc2d22512013-06-21 03:55:28 -03001331 coda_setup_iram(ctx);
1332
Javier Martin186b2502012-07-26 05:53:35 -03001333 if (dst_fourcc == V4L2_PIX_FMT_H264) {
1334 value = (FMO_SLICE_SAVE_BUF_SIZE << 7);
1335 value |= (0 & CODA_FMOPARAM_TYPE_MASK) << CODA_FMOPARAM_TYPE_OFFSET;
1336 value |= 0 & CODA_FMOPARAM_SLICENUM_MASK;
Philipp Zabel10436672012-07-02 09:03:55 -03001337 if (dev->devtype->product == CODA_DX6) {
1338 coda_write(dev, value, CODADX6_CMD_ENC_SEQ_FMO);
1339 } else {
Philipp Zabelc2d22512013-06-21 03:55:28 -03001340 coda_write(dev, ctx->iram_info.search_ram_paddr,
1341 CODA7_CMD_ENC_SEQ_SEARCH_BASE);
1342 coda_write(dev, ctx->iram_info.search_ram_size,
1343 CODA7_CMD_ENC_SEQ_SEARCH_SIZE);
Philipp Zabel10436672012-07-02 09:03:55 -03001344 }
Javier Martin186b2502012-07-26 05:53:35 -03001345 }
1346
Philipp Zabelfcb62822013-05-23 10:43:00 -03001347 ret = coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT);
1348 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03001349 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
Philipp Zabelfcb62822013-05-23 10:43:00 -03001350 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03001351 }
1352
Philipp Zabelfcb62822013-05-23 10:43:00 -03001353 if (coda_read(dev, CODA_RET_ENC_SEQ_SUCCESS) == 0) {
1354 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT failed\n");
1355 ret = -EFAULT;
1356 goto out;
1357 }
Javier Martin186b2502012-07-26 05:53:35 -03001358
Philipp Zabelec25f682012-07-20 08:54:29 -03001359 ret = coda_alloc_framebuffers(ctx, q_data_src, dst_fourcc);
Philipp Zabelfcb62822013-05-23 10:43:00 -03001360 if (ret < 0) {
1361 v4l2_err(v4l2_dev, "failed to allocate framebuffers\n");
1362 goto out;
1363 }
Javier Martin186b2502012-07-26 05:53:35 -03001364
Philipp Zabelec25f682012-07-20 08:54:29 -03001365 coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM);
Philipp Zabel10436672012-07-02 09:03:55 -03001366 coda_write(dev, round_up(q_data_src->width, 8), CODA_CMD_SET_FRAME_BUF_STRIDE);
1367 if (dev->devtype->product != CODA_DX6) {
Philipp Zabelc2d22512013-06-21 03:55:28 -03001368 coda_write(dev, ctx->iram_info.buf_bit_use,
1369 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
1370 coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use,
1371 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
1372 coda_write(dev, ctx->iram_info.buf_dbk_y_use,
1373 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
1374 coda_write(dev, ctx->iram_info.buf_dbk_c_use,
1375 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
1376 coda_write(dev, ctx->iram_info.buf_ovl_use,
1377 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
Philipp Zabel10436672012-07-02 09:03:55 -03001378 }
Philipp Zabelfcb62822013-05-23 10:43:00 -03001379 ret = coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF);
1380 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03001381 v4l2_err(v4l2_dev, "CODA_COMMAND_SET_FRAME_BUF timeout\n");
Philipp Zabelfcb62822013-05-23 10:43:00 -03001382 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03001383 }
1384
1385 /* Save stream headers */
1386 buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
1387 switch (dst_fourcc) {
1388 case V4L2_PIX_FMT_H264:
1389 /*
1390 * Get SPS in the first frame and copy it to an
1391 * intermediate buffer.
1392 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03001393 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_SPS,
1394 &ctx->vpu_header[0][0],
1395 &ctx->vpu_header_size[0]);
1396 if (ret < 0)
1397 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03001398
1399 /*
1400 * Get PPS in the first frame and copy it to an
1401 * intermediate buffer.
1402 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03001403 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_PPS,
1404 &ctx->vpu_header[1][0],
1405 &ctx->vpu_header_size[1]);
1406 if (ret < 0)
1407 goto out;
1408
Javier Martin3f3f5c72012-10-29 05:20:29 -03001409 /*
1410 * Length of H.264 headers is variable and thus it might not be
1411 * aligned for the coda to append the encoded frame. In that is
1412 * the case a filler NAL must be added to header 2.
1413 */
1414 ctx->vpu_header_size[2] = coda_h264_padding(
1415 (ctx->vpu_header_size[0] +
1416 ctx->vpu_header_size[1]),
1417 ctx->vpu_header[2]);
Javier Martin186b2502012-07-26 05:53:35 -03001418 break;
1419 case V4L2_PIX_FMT_MPEG4:
1420 /*
1421 * Get VOS in the first frame and copy it to an
1422 * intermediate buffer
1423 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03001424 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOS,
1425 &ctx->vpu_header[0][0],
1426 &ctx->vpu_header_size[0]);
1427 if (ret < 0)
1428 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03001429
Philipp Zabeld35167a2013-05-23 10:42:59 -03001430 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VIS,
1431 &ctx->vpu_header[1][0],
1432 &ctx->vpu_header_size[1]);
1433 if (ret < 0)
1434 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03001435
Philipp Zabeld35167a2013-05-23 10:42:59 -03001436 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOL,
1437 &ctx->vpu_header[2][0],
1438 &ctx->vpu_header_size[2]);
1439 if (ret < 0)
1440 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03001441 break;
1442 default:
1443 /* No more formats need to save headers at the moment */
1444 break;
1445 }
1446
Philipp Zabeld35167a2013-05-23 10:42:59 -03001447out:
Philipp Zabelfcb62822013-05-23 10:43:00 -03001448 mutex_unlock(&dev->coda_mutex);
Philipp Zabeld35167a2013-05-23 10:42:59 -03001449 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03001450}
1451
1452static int coda_stop_streaming(struct vb2_queue *q)
1453{
1454 struct coda_ctx *ctx = vb2_get_drv_priv(q);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03001455 struct coda_dev *dev = ctx->dev;
Javier Martin186b2502012-07-26 05:53:35 -03001456
1457 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1458 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1459 "%s: output\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001460 ctx->streamon_out = 0;
Javier Martin186b2502012-07-26 05:53:35 -03001461 } else {
1462 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1463 "%s: capture\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001464 ctx->streamon_cap = 0;
Javier Martin186b2502012-07-26 05:53:35 -03001465 }
1466
Philipp Zabel62bed142012-07-25 10:40:39 -03001467 /* Don't stop the coda unless both queues are off */
Philipp Zabelb96904e2013-05-23 10:42:58 -03001468 if (ctx->streamon_out || ctx->streamon_cap)
Philipp Zabel62bed142012-07-25 10:40:39 -03001469 return 0;
Philipp Zabel2fb57f02012-07-25 09:22:07 -03001470
Philipp Zabel62bed142012-07-25 10:40:39 -03001471 cancel_delayed_work(&dev->timeout);
1472
Philipp Zabelfcb62822013-05-23 10:43:00 -03001473 mutex_lock(&dev->coda_mutex);
Philipp Zabel62bed142012-07-25 10:40:39 -03001474 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1475 "%s: sent command 'SEQ_END' to coda\n", __func__);
1476 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_END)) {
1477 v4l2_err(&dev->v4l2_dev,
1478 "CODA_COMMAND_SEQ_END failed\n");
1479 return -ETIMEDOUT;
1480 }
Philipp Zabelfcb62822013-05-23 10:43:00 -03001481 mutex_unlock(&dev->coda_mutex);
Philipp Zabel62bed142012-07-25 10:40:39 -03001482
1483 coda_free_framebuffers(ctx);
1484
Javier Martin186b2502012-07-26 05:53:35 -03001485 return 0;
1486}
1487
1488static struct vb2_ops coda_qops = {
1489 .queue_setup = coda_queue_setup,
1490 .buf_prepare = coda_buf_prepare,
1491 .buf_queue = coda_buf_queue,
1492 .wait_prepare = coda_wait_prepare,
1493 .wait_finish = coda_wait_finish,
1494 .start_streaming = coda_start_streaming,
1495 .stop_streaming = coda_stop_streaming,
1496};
1497
1498static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
1499{
1500 struct coda_ctx *ctx =
1501 container_of(ctrl->handler, struct coda_ctx, ctrls);
1502
1503 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1504 "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val);
1505
1506 switch (ctrl->id) {
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03001507 case V4L2_CID_HFLIP:
1508 if (ctrl->val)
1509 ctx->params.rot_mode |= CODA_MIR_HOR;
1510 else
1511 ctx->params.rot_mode &= ~CODA_MIR_HOR;
1512 break;
1513 case V4L2_CID_VFLIP:
1514 if (ctrl->val)
1515 ctx->params.rot_mode |= CODA_MIR_VER;
1516 else
1517 ctx->params.rot_mode &= ~CODA_MIR_VER;
1518 break;
Javier Martin186b2502012-07-26 05:53:35 -03001519 case V4L2_CID_MPEG_VIDEO_BITRATE:
1520 ctx->params.bitrate = ctrl->val / 1000;
1521 break;
1522 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
1523 ctx->params.gop_size = ctrl->val;
1524 break;
1525 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
1526 ctx->params.h264_intra_qp = ctrl->val;
1527 break;
1528 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
1529 ctx->params.h264_inter_qp = ctrl->val;
1530 break;
1531 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
1532 ctx->params.mpeg4_intra_qp = ctrl->val;
1533 break;
1534 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
1535 ctx->params.mpeg4_inter_qp = ctrl->val;
1536 break;
1537 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
1538 ctx->params.slice_mode = ctrl->val;
1539 break;
1540 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
1541 ctx->params.slice_max_mb = ctrl->val;
1542 break;
Philipp Zabelc566c782012-08-08 11:59:38 -03001543 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
1544 ctx->params.slice_max_bits = ctrl->val * 8;
1545 break;
Javier Martin186b2502012-07-26 05:53:35 -03001546 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
1547 break;
1548 default:
1549 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1550 "Invalid control, id=%d, val=%d\n",
1551 ctrl->id, ctrl->val);
1552 return -EINVAL;
1553 }
1554
1555 return 0;
1556}
1557
1558static struct v4l2_ctrl_ops coda_ctrl_ops = {
1559 .s_ctrl = coda_s_ctrl,
1560};
1561
1562static int coda_ctrls_setup(struct coda_ctx *ctx)
1563{
1564 v4l2_ctrl_handler_init(&ctx->ctrls, 9);
1565
1566 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03001567 V4L2_CID_HFLIP, 0, 1, 1, 0);
1568 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1569 V4L2_CID_VFLIP, 0, 1, 1, 0);
1570 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Javier Martin186b2502012-07-26 05:53:35 -03001571 V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1, 0);
1572 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1573 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 60, 1, 16);
1574 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1575 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 1, 51, 1, 25);
1576 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1577 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 1, 51, 1, 25);
1578 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1579 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
1580 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1581 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
1582 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1583 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
Philipp Zabelc566c782012-08-08 11:59:38 -03001584 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0,
1585 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
Javier Martin186b2502012-07-26 05:53:35 -03001586 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1587 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
Philipp Zabelc566c782012-08-08 11:59:38 -03001588 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1589 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1, 500);
Javier Martin186b2502012-07-26 05:53:35 -03001590 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1591 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
1592 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
1593 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
1594 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
1595
1596 if (ctx->ctrls.error) {
1597 v4l2_err(&ctx->dev->v4l2_dev, "control initialization error (%d)",
1598 ctx->ctrls.error);
1599 return -EINVAL;
1600 }
1601
1602 return v4l2_ctrl_handler_setup(&ctx->ctrls);
1603}
1604
1605static int coda_queue_init(void *priv, struct vb2_queue *src_vq,
1606 struct vb2_queue *dst_vq)
1607{
1608 struct coda_ctx *ctx = priv;
1609 int ret;
1610
Javier Martin186b2502012-07-26 05:53:35 -03001611 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
Philipp Zabel419869c2013-05-23 07:06:30 -03001612 src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
Javier Martin186b2502012-07-26 05:53:35 -03001613 src_vq->drv_priv = ctx;
1614 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1615 src_vq->ops = &coda_qops;
1616 src_vq->mem_ops = &vb2_dma_contig_memops;
Kamil Debskiccd571c2013-04-24 10:38:24 -03001617 src_vq->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Javier Martin186b2502012-07-26 05:53:35 -03001618
1619 ret = vb2_queue_init(src_vq);
1620 if (ret)
1621 return ret;
1622
Javier Martin186b2502012-07-26 05:53:35 -03001623 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Philipp Zabel419869c2013-05-23 07:06:30 -03001624 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
Javier Martin186b2502012-07-26 05:53:35 -03001625 dst_vq->drv_priv = ctx;
1626 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1627 dst_vq->ops = &coda_qops;
1628 dst_vq->mem_ops = &vb2_dma_contig_memops;
Kamil Debskiccd571c2013-04-24 10:38:24 -03001629 dst_vq->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Javier Martin186b2502012-07-26 05:53:35 -03001630
1631 return vb2_queue_init(dst_vq);
1632}
1633
Philipp Zabele11f3e62012-07-25 09:16:58 -03001634static int coda_next_free_instance(struct coda_dev *dev)
1635{
1636 return ffz(dev->instance_mask);
1637}
1638
Javier Martin186b2502012-07-26 05:53:35 -03001639static int coda_open(struct file *file)
1640{
1641 struct coda_dev *dev = video_drvdata(file);
1642 struct coda_ctx *ctx = NULL;
1643 int ret = 0;
Philipp Zabele11f3e62012-07-25 09:16:58 -03001644 int idx;
Javier Martin186b2502012-07-26 05:53:35 -03001645
Philipp Zabele11f3e62012-07-25 09:16:58 -03001646 idx = coda_next_free_instance(dev);
1647 if (idx >= CODA_MAX_INSTANCES)
Javier Martin186b2502012-07-26 05:53:35 -03001648 return -EBUSY;
Philipp Zabele11f3e62012-07-25 09:16:58 -03001649 set_bit(idx, &dev->instance_mask);
Javier Martin186b2502012-07-26 05:53:35 -03001650
1651 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
1652 if (!ctx)
1653 return -ENOMEM;
1654
1655 v4l2_fh_init(&ctx->fh, video_devdata(file));
1656 file->private_data = &ctx->fh;
1657 v4l2_fh_add(&ctx->fh);
1658 ctx->dev = dev;
Philipp Zabele11f3e62012-07-25 09:16:58 -03001659 ctx->idx = idx;
Javier Martin186b2502012-07-26 05:53:35 -03001660
1661 set_default_params(ctx);
1662 ctx->m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
1663 &coda_queue_init);
1664 if (IS_ERR(ctx->m2m_ctx)) {
Philipp Zabel72720ff2013-05-21 10:31:25 -03001665 ret = PTR_ERR(ctx->m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001666
1667 v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
1668 __func__, ret);
1669 goto err;
1670 }
1671 ret = coda_ctrls_setup(ctx);
1672 if (ret) {
1673 v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
1674 goto err;
1675 }
1676
1677 ctx->fh.ctrl_handler = &ctx->ctrls;
1678
1679 ctx->parabuf.vaddr = dma_alloc_coherent(&dev->plat_dev->dev,
1680 CODA_PARA_BUF_SIZE, &ctx->parabuf.paddr, GFP_KERNEL);
1681 if (!ctx->parabuf.vaddr) {
1682 v4l2_err(&dev->v4l2_dev, "failed to allocate parabuf");
1683 ret = -ENOMEM;
1684 goto err;
1685 }
1686
1687 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03001688 list_add(&ctx->list, &dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03001689 coda_unlock(ctx);
1690
1691 clk_prepare_enable(dev->clk_per);
1692 clk_prepare_enable(dev->clk_ahb);
1693
1694 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Created instance %d (%p)\n",
1695 ctx->idx, ctx);
1696
1697 return 0;
1698
1699err:
1700 v4l2_fh_del(&ctx->fh);
1701 v4l2_fh_exit(&ctx->fh);
1702 kfree(ctx);
1703 return ret;
1704}
1705
1706static int coda_release(struct file *file)
1707{
1708 struct coda_dev *dev = video_drvdata(file);
1709 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
1710
1711 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Releasing instance %p\n",
1712 ctx);
1713
1714 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03001715 list_del(&ctx->list);
Javier Martin186b2502012-07-26 05:53:35 -03001716 coda_unlock(ctx);
1717
1718 dma_free_coherent(&dev->plat_dev->dev, CODA_PARA_BUF_SIZE,
1719 ctx->parabuf.vaddr, ctx->parabuf.paddr);
1720 v4l2_m2m_ctx_release(ctx->m2m_ctx);
1721 v4l2_ctrl_handler_free(&ctx->ctrls);
1722 clk_disable_unprepare(dev->clk_per);
1723 clk_disable_unprepare(dev->clk_ahb);
1724 v4l2_fh_del(&ctx->fh);
1725 v4l2_fh_exit(&ctx->fh);
Philipp Zabele11f3e62012-07-25 09:16:58 -03001726 clear_bit(ctx->idx, &dev->instance_mask);
Javier Martin186b2502012-07-26 05:53:35 -03001727 kfree(ctx);
1728
1729 return 0;
1730}
1731
1732static unsigned int coda_poll(struct file *file,
1733 struct poll_table_struct *wait)
1734{
1735 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
1736 int ret;
1737
1738 coda_lock(ctx);
1739 ret = v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
1740 coda_unlock(ctx);
1741 return ret;
1742}
1743
1744static int coda_mmap(struct file *file, struct vm_area_struct *vma)
1745{
1746 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
1747
1748 return v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
1749}
1750
1751static const struct v4l2_file_operations coda_fops = {
1752 .owner = THIS_MODULE,
1753 .open = coda_open,
1754 .release = coda_release,
1755 .poll = coda_poll,
1756 .unlocked_ioctl = video_ioctl2,
1757 .mmap = coda_mmap,
1758};
1759
1760static irqreturn_t coda_irq_handler(int irq, void *data)
1761{
Philipp Zabelec25f682012-07-20 08:54:29 -03001762 struct vb2_buffer *src_buf, *dst_buf;
Javier Martin186b2502012-07-26 05:53:35 -03001763 struct coda_dev *dev = data;
1764 u32 wr_ptr, start_ptr;
1765 struct coda_ctx *ctx;
1766
Fabio Estevam2249b452012-10-09 23:33:29 -03001767 cancel_delayed_work(&dev->timeout);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03001768
Javier Martin186b2502012-07-26 05:53:35 -03001769 /* read status register to attend the IRQ */
1770 coda_read(dev, CODA_REG_BIT_INT_STATUS);
1771 coda_write(dev, CODA_REG_BIT_INT_CLEAR_SET,
1772 CODA_REG_BIT_INT_CLEAR);
1773
1774 ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev);
1775 if (ctx == NULL) {
1776 v4l2_err(&dev->v4l2_dev, "Instance released before the end of transaction\n");
Philipp Zabelfcb62822013-05-23 10:43:00 -03001777 mutex_unlock(&dev->coda_mutex);
Javier Martin186b2502012-07-26 05:53:35 -03001778 return IRQ_HANDLED;
1779 }
1780
1781 if (ctx->aborting) {
1782 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1783 "task has been aborted\n");
Philipp Zabelfcb62822013-05-23 10:43:00 -03001784 mutex_unlock(&dev->coda_mutex);
Javier Martin186b2502012-07-26 05:53:35 -03001785 return IRQ_HANDLED;
1786 }
1787
1788 if (coda_isbusy(ctx->dev)) {
1789 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1790 "coda is still busy!!!!\n");
1791 return IRQ_NONE;
1792 }
1793
Philipp Zabelec25f682012-07-20 08:54:29 -03001794 src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
1795 dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001796
1797 /* Get results from the coda */
1798 coda_read(dev, CODA_RET_ENC_PIC_TYPE);
1799 start_ptr = coda_read(dev, CODA_CMD_ENC_PIC_BB_START);
1800 wr_ptr = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->idx));
1801 /* Calculate bytesused field */
1802 if (dst_buf->v4l2_buf.sequence == 0) {
Philipp Zabel366108f2013-06-21 03:55:27 -03001803 vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr +
1804 ctx->vpu_header_size[0] +
1805 ctx->vpu_header_size[1] +
1806 ctx->vpu_header_size[2]);
Javier Martin186b2502012-07-26 05:53:35 -03001807 } else {
Philipp Zabel366108f2013-06-21 03:55:27 -03001808 vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr);
Javier Martin186b2502012-07-26 05:53:35 -03001809 }
1810
1811 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, "frame size = %u\n",
1812 wr_ptr - start_ptr);
1813
1814 coda_read(dev, CODA_RET_ENC_PIC_SLICE_NUM);
1815 coda_read(dev, CODA_RET_ENC_PIC_FLAG);
1816
1817 if (src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) {
1818 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
1819 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
1820 } else {
1821 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
1822 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
1823 }
1824
Kamil Debskiccd571c2013-04-24 10:38:24 -03001825 dst_buf->v4l2_buf.timestamp = src_buf->v4l2_buf.timestamp;
1826 dst_buf->v4l2_buf.timecode = src_buf->v4l2_buf.timecode;
1827
Philipp Zabelec25f682012-07-20 08:54:29 -03001828 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
Javier Martin186b2502012-07-26 05:53:35 -03001829 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
1830
1831 ctx->gopcounter--;
1832 if (ctx->gopcounter < 0)
1833 ctx->gopcounter = ctx->params.gop_size - 1;
1834
1835 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1836 "job finished: encoding frame (%d) (%s)\n",
1837 dst_buf->v4l2_buf.sequence,
1838 (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ?
1839 "KEYFRAME" : "PFRAME");
1840
Philipp Zabelfcb62822013-05-23 10:43:00 -03001841 mutex_unlock(&dev->coda_mutex);
1842
Javier Martin186b2502012-07-26 05:53:35 -03001843 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->m2m_ctx);
1844
1845 return IRQ_HANDLED;
1846}
1847
Philipp Zabel2fb57f02012-07-25 09:22:07 -03001848static void coda_timeout(struct work_struct *work)
1849{
1850 struct coda_ctx *ctx;
1851 struct coda_dev *dev = container_of(to_delayed_work(work),
1852 struct coda_dev, timeout);
1853
Philipp Zabel39cc0292013-05-21 10:32:42 -03001854 dev_err(&dev->plat_dev->dev, "CODA PIC_RUN timeout, stopping all streams\n");
Philipp Zabel2fb57f02012-07-25 09:22:07 -03001855
1856 mutex_lock(&dev->dev_mutex);
1857 list_for_each_entry(ctx, &dev->instances, list) {
1858 v4l2_m2m_streamoff(NULL, ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1859 v4l2_m2m_streamoff(NULL, ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1860 }
1861 mutex_unlock(&dev->dev_mutex);
Philipp Zabelfcb62822013-05-23 10:43:00 -03001862
1863 mutex_unlock(&dev->coda_mutex);
1864 ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev);
1865 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->m2m_ctx);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03001866}
1867
Javier Martin186b2502012-07-26 05:53:35 -03001868static u32 coda_supported_firmwares[] = {
1869 CODA_FIRMWARE_VERNUM(CODA_DX6, 2, 2, 5),
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03001870 CODA_FIRMWARE_VERNUM(CODA_7541, 13, 4, 29),
Javier Martin186b2502012-07-26 05:53:35 -03001871};
1872
1873static bool coda_firmware_supported(u32 vernum)
1874{
1875 int i;
1876
1877 for (i = 0; i < ARRAY_SIZE(coda_supported_firmwares); i++)
1878 if (vernum == coda_supported_firmwares[i])
1879 return true;
1880 return false;
1881}
1882
1883static char *coda_product_name(int product)
1884{
1885 static char buf[9];
1886
1887 switch (product) {
1888 case CODA_DX6:
1889 return "CodaDx6";
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03001890 case CODA_7541:
1891 return "CODA7541";
Javier Martin186b2502012-07-26 05:53:35 -03001892 default:
1893 snprintf(buf, sizeof(buf), "(0x%04x)", product);
1894 return buf;
1895 }
1896}
1897
Philipp Zabel87048bb2012-07-02 07:03:43 -03001898static int coda_hw_init(struct coda_dev *dev)
Javier Martin186b2502012-07-26 05:53:35 -03001899{
1900 u16 product, major, minor, release;
1901 u32 data;
1902 u16 *p;
1903 int i;
1904
1905 clk_prepare_enable(dev->clk_per);
1906 clk_prepare_enable(dev->clk_ahb);
1907
Javier Martin186b2502012-07-26 05:53:35 -03001908 /*
1909 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
Philipp Zabel87048bb2012-07-02 07:03:43 -03001910 * The 16-bit chars in the code buffer are in memory access
1911 * order, re-sort them to CODA order for register download.
Javier Martin186b2502012-07-26 05:53:35 -03001912 * Data in this SRAM survives a reboot.
1913 */
Philipp Zabel87048bb2012-07-02 07:03:43 -03001914 p = (u16 *)dev->codebuf.vaddr;
1915 if (dev->devtype->product == CODA_DX6) {
1916 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
1917 data = CODA_DOWN_ADDRESS_SET(i) |
1918 CODA_DOWN_DATA_SET(p[i ^ 1]);
1919 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
1920 }
1921 } else {
1922 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
1923 data = CODA_DOWN_ADDRESS_SET(i) |
1924 CODA_DOWN_DATA_SET(p[round_down(i, 4) +
1925 3 - (i % 4)]);
1926 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
1927 }
Javier Martin186b2502012-07-26 05:53:35 -03001928 }
Javier Martin186b2502012-07-26 05:53:35 -03001929
Philipp Zabel9acf7692013-05-23 10:42:56 -03001930 /* Clear registers */
1931 for (i = 0; i < 64; i++)
1932 coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
1933
Javier Martin186b2502012-07-26 05:53:35 -03001934 /* Tell the BIT where to find everything it needs */
1935 coda_write(dev, dev->workbuf.paddr,
1936 CODA_REG_BIT_WORK_BUF_ADDR);
1937 coda_write(dev, dev->codebuf.paddr,
1938 CODA_REG_BIT_CODE_BUF_ADDR);
1939 coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
1940
1941 /* Set default values */
1942 switch (dev->devtype->product) {
1943 case CODA_DX6:
1944 coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
1945 break;
1946 default:
1947 coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
1948 }
1949 coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
Philipp Zabel10436672012-07-02 09:03:55 -03001950
1951 if (dev->devtype->product != CODA_DX6)
1952 coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
1953
Javier Martin186b2502012-07-26 05:53:35 -03001954 coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
1955 CODA_REG_BIT_INT_ENABLE);
1956
1957 /* Reset VPU and start processor */
1958 data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
1959 data |= CODA_REG_RESET_ENABLE;
1960 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
1961 udelay(10);
1962 data &= ~CODA_REG_RESET_ENABLE;
1963 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
1964 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
1965
1966 /* Load firmware */
1967 coda_write(dev, 0, CODA_CMD_FIRMWARE_VERNUM);
1968 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
1969 coda_write(dev, 0, CODA_REG_BIT_RUN_INDEX);
1970 coda_write(dev, 0, CODA_REG_BIT_RUN_COD_STD);
1971 coda_write(dev, CODA_COMMAND_FIRMWARE_GET, CODA_REG_BIT_RUN_COMMAND);
1972 if (coda_wait_timeout(dev)) {
1973 clk_disable_unprepare(dev->clk_per);
1974 clk_disable_unprepare(dev->clk_ahb);
1975 v4l2_err(&dev->v4l2_dev, "firmware get command error\n");
1976 return -EIO;
1977 }
1978
1979 /* Check we are compatible with the loaded firmware */
1980 data = coda_read(dev, CODA_CMD_FIRMWARE_VERNUM);
1981 product = CODA_FIRMWARE_PRODUCT(data);
1982 major = CODA_FIRMWARE_MAJOR(data);
1983 minor = CODA_FIRMWARE_MINOR(data);
1984 release = CODA_FIRMWARE_RELEASE(data);
1985
1986 clk_disable_unprepare(dev->clk_per);
1987 clk_disable_unprepare(dev->clk_ahb);
1988
1989 if (product != dev->devtype->product) {
1990 v4l2_err(&dev->v4l2_dev, "Wrong firmware. Hw: %s, Fw: %s,"
1991 " Version: %u.%u.%u\n",
1992 coda_product_name(dev->devtype->product),
1993 coda_product_name(product), major, minor, release);
1994 return -EINVAL;
1995 }
1996
1997 v4l2_info(&dev->v4l2_dev, "Initialized %s.\n",
1998 coda_product_name(product));
1999
2000 if (coda_firmware_supported(data)) {
2001 v4l2_info(&dev->v4l2_dev, "Firmware version: %u.%u.%u\n",
2002 major, minor, release);
2003 } else {
2004 v4l2_warn(&dev->v4l2_dev, "Unsupported firmware version: "
2005 "%u.%u.%u\n", major, minor, release);
2006 }
2007
2008 return 0;
2009}
2010
2011static void coda_fw_callback(const struct firmware *fw, void *context)
2012{
2013 struct coda_dev *dev = context;
2014 struct platform_device *pdev = dev->plat_dev;
2015 int ret;
2016
2017 if (!fw) {
2018 v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
2019 return;
2020 }
2021
2022 /* allocate auxiliary per-device code buffer for the BIT processor */
2023 dev->codebuf.size = fw->size;
2024 dev->codebuf.vaddr = dma_alloc_coherent(&pdev->dev, fw->size,
2025 &dev->codebuf.paddr,
2026 GFP_KERNEL);
2027 if (!dev->codebuf.vaddr) {
2028 dev_err(&pdev->dev, "failed to allocate code buffer\n");
2029 return;
2030 }
2031
Philipp Zabel87048bb2012-07-02 07:03:43 -03002032 /* Copy the whole firmware image to the code buffer */
2033 memcpy(dev->codebuf.vaddr, fw->data, fw->size);
2034 release_firmware(fw);
2035
2036 ret = coda_hw_init(dev);
Javier Martin186b2502012-07-26 05:53:35 -03002037 if (ret) {
2038 v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
2039 return;
2040 }
2041
2042 dev->vfd.fops = &coda_fops,
2043 dev->vfd.ioctl_ops = &coda_ioctl_ops;
2044 dev->vfd.release = video_device_release_empty,
2045 dev->vfd.lock = &dev->dev_mutex;
2046 dev->vfd.v4l2_dev = &dev->v4l2_dev;
Hans Verkuil954f3402012-09-05 06:05:50 -03002047 dev->vfd.vfl_dir = VFL_DIR_M2M;
Javier Martin186b2502012-07-26 05:53:35 -03002048 snprintf(dev->vfd.name, sizeof(dev->vfd.name), "%s", CODA_NAME);
2049 video_set_drvdata(&dev->vfd, dev);
2050
2051 dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
2052 if (IS_ERR(dev->alloc_ctx)) {
2053 v4l2_err(&dev->v4l2_dev, "Failed to alloc vb2 context\n");
2054 return;
2055 }
2056
2057 dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
2058 if (IS_ERR(dev->m2m_dev)) {
2059 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
2060 goto rel_ctx;
2061 }
2062
2063 ret = video_register_device(&dev->vfd, VFL_TYPE_GRABBER, 0);
2064 if (ret) {
2065 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
2066 goto rel_m2m;
2067 }
2068 v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video%d\n",
2069 dev->vfd.num);
2070
2071 return;
2072
2073rel_m2m:
2074 v4l2_m2m_release(dev->m2m_dev);
2075rel_ctx:
2076 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
2077}
2078
2079static int coda_firmware_request(struct coda_dev *dev)
2080{
2081 char *fw = dev->devtype->firmware;
2082
2083 dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw,
2084 coda_product_name(dev->devtype->product));
2085
2086 return request_firmware_nowait(THIS_MODULE, true,
2087 fw, &dev->plat_dev->dev, GFP_KERNEL, dev, coda_fw_callback);
2088}
2089
2090enum coda_platform {
2091 CODA_IMX27,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03002092 CODA_IMX53,
Javier Martin186b2502012-07-26 05:53:35 -03002093};
2094
Emil Goodec06d8752012-08-14 17:44:42 -03002095static const struct coda_devtype coda_devdata[] = {
Javier Martin186b2502012-07-26 05:53:35 -03002096 [CODA_IMX27] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -03002097 .firmware = "v4l-codadx6-imx27.bin",
2098 .product = CODA_DX6,
2099 .codecs = codadx6_codecs,
2100 .num_codecs = ARRAY_SIZE(codadx6_codecs),
Javier Martin186b2502012-07-26 05:53:35 -03002101 },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03002102 [CODA_IMX53] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -03002103 .firmware = "v4l-coda7541-imx53.bin",
2104 .product = CODA_7541,
2105 .codecs = coda7_codecs,
2106 .num_codecs = ARRAY_SIZE(coda7_codecs),
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03002107 },
Javier Martin186b2502012-07-26 05:53:35 -03002108};
2109
2110static struct platform_device_id coda_platform_ids[] = {
2111 { .name = "coda-imx27", .driver_data = CODA_IMX27 },
Fabio Estevam4e44cd02012-10-10 00:07:38 -03002112 { .name = "coda-imx53", .driver_data = CODA_IMX53 },
Javier Martin186b2502012-07-26 05:53:35 -03002113 { /* sentinel */ }
2114};
2115MODULE_DEVICE_TABLE(platform, coda_platform_ids);
2116
2117#ifdef CONFIG_OF
2118static const struct of_device_id coda_dt_ids[] = {
2119 { .compatible = "fsl,imx27-vpu", .data = &coda_platform_ids[CODA_IMX27] },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03002120 { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
Javier Martin186b2502012-07-26 05:53:35 -03002121 { /* sentinel */ }
2122};
2123MODULE_DEVICE_TABLE(of, coda_dt_ids);
2124#endif
2125
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08002126static int coda_probe(struct platform_device *pdev)
Javier Martin186b2502012-07-26 05:53:35 -03002127{
2128 const struct of_device_id *of_id =
2129 of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
2130 const struct platform_device_id *pdev_id;
Philipp Zabel657eee72013-04-29 16:17:14 -07002131 struct coda_platform_data *pdata = pdev->dev.platform_data;
2132 struct device_node *np = pdev->dev.of_node;
2133 struct gen_pool *pool;
Javier Martin186b2502012-07-26 05:53:35 -03002134 struct coda_dev *dev;
2135 struct resource *res;
2136 int ret, irq;
2137
2138 dev = devm_kzalloc(&pdev->dev, sizeof *dev, GFP_KERNEL);
2139 if (!dev) {
2140 dev_err(&pdev->dev, "Not enough memory for %s\n",
2141 CODA_NAME);
2142 return -ENOMEM;
2143 }
2144
2145 spin_lock_init(&dev->irqlock);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002146 INIT_LIST_HEAD(&dev->instances);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03002147 INIT_DELAYED_WORK(&dev->timeout, coda_timeout);
Javier Martin186b2502012-07-26 05:53:35 -03002148
2149 dev->plat_dev = pdev;
2150 dev->clk_per = devm_clk_get(&pdev->dev, "per");
2151 if (IS_ERR(dev->clk_per)) {
2152 dev_err(&pdev->dev, "Could not get per clock\n");
2153 return PTR_ERR(dev->clk_per);
2154 }
2155
2156 dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
2157 if (IS_ERR(dev->clk_ahb)) {
2158 dev_err(&pdev->dev, "Could not get ahb clock\n");
2159 return PTR_ERR(dev->clk_ahb);
2160 }
2161
2162 /* Get memory for physical registers */
2163 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2164 if (res == NULL) {
2165 dev_err(&pdev->dev, "failed to get memory region resource\n");
2166 return -ENOENT;
2167 }
2168
Philipp Zabel611cbbf2013-05-21 04:19:27 -03002169 dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
2170 if (IS_ERR(dev->regs_base))
2171 return PTR_ERR(dev->regs_base);
Javier Martin186b2502012-07-26 05:53:35 -03002172
2173 /* IRQ */
2174 irq = platform_get_irq(pdev, 0);
2175 if (irq < 0) {
2176 dev_err(&pdev->dev, "failed to get irq resource\n");
2177 return -ENOENT;
2178 }
2179
2180 if (devm_request_irq(&pdev->dev, irq, coda_irq_handler,
2181 0, CODA_NAME, dev) < 0) {
2182 dev_err(&pdev->dev, "failed to request irq\n");
2183 return -ENOENT;
2184 }
2185
Philipp Zabel657eee72013-04-29 16:17:14 -07002186 /* Get IRAM pool from device tree or platform data */
2187 pool = of_get_named_gen_pool(np, "iram", 0);
2188 if (!pool && pdata)
2189 pool = dev_get_gen_pool(pdata->iram_dev);
2190 if (!pool) {
2191 dev_err(&pdev->dev, "iram pool not available\n");
2192 return -ENOMEM;
2193 }
2194 dev->iram_pool = pool;
2195
Javier Martin186b2502012-07-26 05:53:35 -03002196 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
2197 if (ret)
2198 return ret;
2199
2200 mutex_init(&dev->dev_mutex);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002201 mutex_init(&dev->coda_mutex);
Javier Martin186b2502012-07-26 05:53:35 -03002202
2203 pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
2204
2205 if (of_id) {
2206 dev->devtype = of_id->data;
2207 } else if (pdev_id) {
2208 dev->devtype = &coda_devdata[pdev_id->driver_data];
2209 } else {
2210 v4l2_device_unregister(&dev->v4l2_dev);
2211 return -EINVAL;
2212 }
2213
2214 /* allocate auxiliary per-device buffers for the BIT processor */
2215 switch (dev->devtype->product) {
2216 case CODA_DX6:
2217 dev->workbuf.size = CODADX6_WORK_BUF_SIZE;
2218 break;
2219 default:
2220 dev->workbuf.size = CODA7_WORK_BUF_SIZE;
2221 }
2222 dev->workbuf.vaddr = dma_alloc_coherent(&pdev->dev, dev->workbuf.size,
2223 &dev->workbuf.paddr,
2224 GFP_KERNEL);
2225 if (!dev->workbuf.vaddr) {
2226 dev_err(&pdev->dev, "failed to allocate work buffer\n");
2227 v4l2_device_unregister(&dev->v4l2_dev);
2228 return -ENOMEM;
2229 }
2230
Philipp Zabel657eee72013-04-29 16:17:14 -07002231 if (dev->devtype->product == CODA_DX6)
2232 dev->iram_size = CODADX6_IRAM_SIZE;
2233 else
2234 dev->iram_size = CODA7_IRAM_SIZE;
2235 dev->iram_vaddr = gen_pool_alloc(dev->iram_pool, dev->iram_size);
2236 if (!dev->iram_vaddr) {
2237 dev_err(&pdev->dev, "unable to alloc iram\n");
2238 return -ENOMEM;
Philipp Zabel10436672012-07-02 09:03:55 -03002239 }
Philipp Zabel657eee72013-04-29 16:17:14 -07002240 dev->iram_paddr = gen_pool_virt_to_phys(dev->iram_pool,
2241 dev->iram_vaddr);
Philipp Zabel10436672012-07-02 09:03:55 -03002242
Javier Martin186b2502012-07-26 05:53:35 -03002243 platform_set_drvdata(pdev, dev);
2244
2245 return coda_firmware_request(dev);
2246}
2247
2248static int coda_remove(struct platform_device *pdev)
2249{
2250 struct coda_dev *dev = platform_get_drvdata(pdev);
2251
2252 video_unregister_device(&dev->vfd);
2253 if (dev->m2m_dev)
2254 v4l2_m2m_release(dev->m2m_dev);
2255 if (dev->alloc_ctx)
2256 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
2257 v4l2_device_unregister(&dev->v4l2_dev);
Philipp Zabel657eee72013-04-29 16:17:14 -07002258 if (dev->iram_vaddr)
2259 gen_pool_free(dev->iram_pool, dev->iram_vaddr, dev->iram_size);
Javier Martin186b2502012-07-26 05:53:35 -03002260 if (dev->codebuf.vaddr)
2261 dma_free_coherent(&pdev->dev, dev->codebuf.size,
2262 &dev->codebuf.vaddr, dev->codebuf.paddr);
2263 if (dev->workbuf.vaddr)
2264 dma_free_coherent(&pdev->dev, dev->workbuf.size, &dev->workbuf.vaddr,
2265 dev->workbuf.paddr);
2266 return 0;
2267}
2268
2269static struct platform_driver coda_driver = {
2270 .probe = coda_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08002271 .remove = coda_remove,
Javier Martin186b2502012-07-26 05:53:35 -03002272 .driver = {
2273 .name = CODA_NAME,
2274 .owner = THIS_MODULE,
2275 .of_match_table = of_match_ptr(coda_dt_ids),
2276 },
2277 .id_table = coda_platform_ids,
2278};
2279
2280module_platform_driver(coda_driver);
2281
2282MODULE_LICENSE("GPL");
2283MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
2284MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");