blob: 8575b713320ed16cc104d6805ef5dce338b1eca3 [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>
Philipp Zabel9082a7c2013-06-21 03:55:31 -030021#include <linux/kfifo.h>
Javier Martin186b2502012-07-26 05:53:35 -030022#include <linux/module.h>
23#include <linux/of_device.h>
24#include <linux/platform_device.h>
25#include <linux/slab.h>
26#include <linux/videodev2.h>
27#include <linux/of.h>
Philipp Zabel657eee72013-04-29 16:17:14 -070028#include <linux/platform_data/coda.h>
Javier Martin186b2502012-07-26 05:53:35 -030029
30#include <media/v4l2-ctrls.h>
31#include <media/v4l2-device.h>
32#include <media/v4l2-ioctl.h>
33#include <media/v4l2-mem2mem.h>
34#include <media/videobuf2-core.h>
35#include <media/videobuf2-dma-contig.h>
36
37#include "coda.h"
38
39#define CODA_NAME "coda"
40
41#define CODA_MAX_INSTANCES 4
42
43#define CODA_FMO_BUF_SIZE 32
44#define CODADX6_WORK_BUF_SIZE (288 * 1024 + CODA_FMO_BUF_SIZE * 8 * 1024)
Philipp Zabel5677e3b2013-06-21 03:55:30 -030045#define CODA7_WORK_BUF_SIZE (128 * 1024)
46#define CODA7_TEMP_BUF_SIZE (304 * 1024)
Javier Martin186b2502012-07-26 05:53:35 -030047#define CODA_PARA_BUF_SIZE (10 * 1024)
48#define CODA_ISRAM_SIZE (2048 * 2)
Philipp Zabel657eee72013-04-29 16:17:14 -070049#define CODADX6_IRAM_SIZE 0xb000
Philipp Zabel10436672012-07-02 09:03:55 -030050#define CODA7_IRAM_SIZE 0x14000 /* 81920 bytes */
Javier Martin186b2502012-07-26 05:53:35 -030051
Philipp Zabelec25f682012-07-20 08:54:29 -030052#define CODA_MAX_FRAMEBUFFERS 2
Javier Martin186b2502012-07-26 05:53:35 -030053
Philipp Zabelb96904e2013-05-23 10:42:58 -030054#define MAX_W 8192
55#define MAX_H 8192
56#define CODA_MAX_FRAME_SIZE 0x100000
Javier Martin186b2502012-07-26 05:53:35 -030057#define FMO_SLICE_SAVE_BUF_SIZE (32)
58#define CODA_DEFAULT_GAMMA 4096
59
60#define MIN_W 176
61#define MIN_H 144
Javier Martin186b2502012-07-26 05:53:35 -030062
63#define S_ALIGN 1 /* multiple of 2 */
64#define W_ALIGN 1 /* multiple of 2 */
65#define H_ALIGN 1 /* multiple of 2 */
66
67#define fh_to_ctx(__fh) container_of(__fh, struct coda_ctx, fh)
68
69static int coda_debug;
Philipp Zabelc4eb1bfc2013-05-21 04:24:16 -030070module_param(coda_debug, int, 0644);
Javier Martin186b2502012-07-26 05:53:35 -030071MODULE_PARM_DESC(coda_debug, "Debug level (0-1)");
72
73enum {
74 V4L2_M2M_SRC = 0,
75 V4L2_M2M_DST = 1,
76};
77
Javier Martin186b2502012-07-26 05:53:35 -030078enum coda_inst_type {
79 CODA_INST_ENCODER,
80 CODA_INST_DECODER,
81};
82
83enum coda_product {
84 CODA_DX6 = 0xf001,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -030085 CODA_7541 = 0xf012,
Javier Martin186b2502012-07-26 05:53:35 -030086};
87
88struct coda_fmt {
89 char *name;
90 u32 fourcc;
Philipp Zabelb96904e2013-05-23 10:42:58 -030091};
92
93struct coda_codec {
94 u32 mode;
95 u32 src_fourcc;
96 u32 dst_fourcc;
97 u32 max_w;
98 u32 max_h;
Javier Martin186b2502012-07-26 05:53:35 -030099};
100
101struct coda_devtype {
102 char *firmware;
103 enum coda_product product;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300104 struct coda_codec *codecs;
105 unsigned int num_codecs;
Javier Martin186b2502012-07-26 05:53:35 -0300106 size_t workbuf_size;
107};
108
109/* Per-queue, driver-specific private data */
110struct coda_q_data {
111 unsigned int width;
112 unsigned int height;
113 unsigned int sizeimage;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300114 unsigned int fourcc;
Javier Martin186b2502012-07-26 05:53:35 -0300115};
116
117struct coda_aux_buf {
118 void *vaddr;
119 dma_addr_t paddr;
120 u32 size;
121};
122
123struct coda_dev {
124 struct v4l2_device v4l2_dev;
125 struct video_device vfd;
126 struct platform_device *plat_dev;
Emil Goodec06d8752012-08-14 17:44:42 -0300127 const struct coda_devtype *devtype;
Javier Martin186b2502012-07-26 05:53:35 -0300128
129 void __iomem *regs_base;
130 struct clk *clk_per;
131 struct clk *clk_ahb;
132
133 struct coda_aux_buf codebuf;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300134 struct coda_aux_buf tempbuf;
Javier Martin186b2502012-07-26 05:53:35 -0300135 struct coda_aux_buf workbuf;
Philipp Zabel657eee72013-04-29 16:17:14 -0700136 struct gen_pool *iram_pool;
137 long unsigned int iram_vaddr;
Philipp Zabel10436672012-07-02 09:03:55 -0300138 long unsigned int iram_paddr;
Philipp Zabel657eee72013-04-29 16:17:14 -0700139 unsigned long iram_size;
Javier Martin186b2502012-07-26 05:53:35 -0300140
141 spinlock_t irqlock;
142 struct mutex dev_mutex;
Philipp Zabelfcb62822013-05-23 10:43:00 -0300143 struct mutex coda_mutex;
Javier Martin186b2502012-07-26 05:53:35 -0300144 struct v4l2_m2m_dev *m2m_dev;
145 struct vb2_alloc_ctx *alloc_ctx;
Philipp Zabele11f3e62012-07-25 09:16:58 -0300146 struct list_head instances;
147 unsigned long instance_mask;
Philipp Zabel2fb57f02012-07-25 09:22:07 -0300148 struct delayed_work timeout;
Javier Martin186b2502012-07-26 05:53:35 -0300149};
150
151struct coda_params {
Philipp Zabel8f35c7b2012-07-09 04:25:52 -0300152 u8 rot_mode;
Javier Martin186b2502012-07-26 05:53:35 -0300153 u8 h264_intra_qp;
154 u8 h264_inter_qp;
155 u8 mpeg4_intra_qp;
156 u8 mpeg4_inter_qp;
157 u8 gop_size;
158 int codec_mode;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300159 int codec_mode_aux;
Javier Martin186b2502012-07-26 05:53:35 -0300160 enum v4l2_mpeg_video_multi_slice_mode slice_mode;
161 u32 framerate;
162 u16 bitrate;
Philipp Zabelc566c782012-08-08 11:59:38 -0300163 u32 slice_max_bits;
Javier Martin186b2502012-07-26 05:53:35 -0300164 u32 slice_max_mb;
165};
166
Philipp Zabelc2d22512013-06-21 03:55:28 -0300167struct coda_iram_info {
168 u32 axi_sram_use;
169 phys_addr_t buf_bit_use;
170 phys_addr_t buf_ip_ac_dc_use;
171 phys_addr_t buf_dbk_y_use;
172 phys_addr_t buf_dbk_c_use;
173 phys_addr_t buf_ovl_use;
174 phys_addr_t buf_btp_use;
175 phys_addr_t search_ram_paddr;
176 int search_ram_size;
177};
178
Javier Martin186b2502012-07-26 05:53:35 -0300179struct coda_ctx {
180 struct coda_dev *dev;
Philipp Zabele11f3e62012-07-25 09:16:58 -0300181 struct list_head list;
Javier Martin186b2502012-07-26 05:53:35 -0300182 int aborting;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300183 int streamon_out;
184 int streamon_cap;
Javier Martin186b2502012-07-26 05:53:35 -0300185 u32 isequence;
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300186 u32 qsequence;
Javier Martin186b2502012-07-26 05:53:35 -0300187 struct coda_q_data q_data[2];
188 enum coda_inst_type inst_type;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300189 struct coda_codec *codec;
Javier Martin186b2502012-07-26 05:53:35 -0300190 enum v4l2_colorspace colorspace;
191 struct coda_params params;
192 struct v4l2_m2m_ctx *m2m_ctx;
193 struct v4l2_ctrl_handler ctrls;
194 struct v4l2_fh fh;
Javier Martin186b2502012-07-26 05:53:35 -0300195 int gopcounter;
196 char vpu_header[3][64];
197 int vpu_header_size[3];
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300198 struct kfifo bitstream_fifo;
199 struct mutex bitstream_mutex;
200 struct coda_aux_buf bitstream;
Javier Martin186b2502012-07-26 05:53:35 -0300201 struct coda_aux_buf parabuf;
Philipp Zabelec25f682012-07-20 08:54:29 -0300202 struct coda_aux_buf internal_frames[CODA_MAX_FRAMEBUFFERS];
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300203 struct coda_aux_buf workbuf;
Philipp Zabelec25f682012-07-20 08:54:29 -0300204 int num_internal_frames;
Javier Martin186b2502012-07-26 05:53:35 -0300205 int idx;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300206 int reg_idx;
Philipp Zabelc2d22512013-06-21 03:55:28 -0300207 struct coda_iram_info iram_info;
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300208 u32 bit_stream_param;
Javier Martin186b2502012-07-26 05:53:35 -0300209};
210
Javier Martin832fbb52012-10-29 08:34:59 -0300211static const u8 coda_filler_nal[14] = { 0x00, 0x00, 0x00, 0x01, 0x0c, 0xff,
212 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80 };
213static const u8 coda_filler_size[8] = { 0, 7, 14, 13, 12, 11, 10, 9 };
Javier Martin3f3f5c72012-10-29 05:20:29 -0300214
Javier Martin186b2502012-07-26 05:53:35 -0300215static inline void coda_write(struct coda_dev *dev, u32 data, u32 reg)
216{
217 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
218 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
219 writel(data, dev->regs_base + reg);
220}
221
222static inline unsigned int coda_read(struct coda_dev *dev, u32 reg)
223{
224 u32 data;
225 data = readl(dev->regs_base + reg);
226 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
227 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
228 return data;
229}
230
231static inline unsigned long coda_isbusy(struct coda_dev *dev)
232{
233 return coda_read(dev, CODA_REG_BIT_BUSY);
234}
235
236static inline int coda_is_initialized(struct coda_dev *dev)
237{
238 return (coda_read(dev, CODA_REG_BIT_CUR_PC) != 0);
239}
240
241static int coda_wait_timeout(struct coda_dev *dev)
242{
243 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
244
245 while (coda_isbusy(dev)) {
246 if (time_after(jiffies, timeout))
247 return -ETIMEDOUT;
248 }
249 return 0;
250}
251
252static void coda_command_async(struct coda_ctx *ctx, int cmd)
253{
254 struct coda_dev *dev = ctx->dev;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300255
256 if (dev->devtype->product == CODA_7541) {
257 /* Restore context related registers to CODA */
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300258 coda_write(dev, ctx->bit_stream_param,
259 CODA_REG_BIT_BIT_STREAM_PARAM);
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300260 coda_write(dev, ctx->workbuf.paddr, CODA_REG_BIT_WORK_BUF_ADDR);
261 }
262
Javier Martin186b2502012-07-26 05:53:35 -0300263 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
264
265 coda_write(dev, ctx->idx, CODA_REG_BIT_RUN_INDEX);
266 coda_write(dev, ctx->params.codec_mode, CODA_REG_BIT_RUN_COD_STD);
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300267 coda_write(dev, ctx->params.codec_mode_aux, CODA7_REG_BIT_RUN_AUX_STD);
268
Javier Martin186b2502012-07-26 05:53:35 -0300269 coda_write(dev, cmd, CODA_REG_BIT_RUN_COMMAND);
270}
271
272static int coda_command_sync(struct coda_ctx *ctx, int cmd)
273{
274 struct coda_dev *dev = ctx->dev;
275
276 coda_command_async(ctx, cmd);
277 return coda_wait_timeout(dev);
278}
279
280static struct coda_q_data *get_q_data(struct coda_ctx *ctx,
281 enum v4l2_buf_type type)
282{
283 switch (type) {
284 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
285 return &(ctx->q_data[V4L2_M2M_SRC]);
286 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
287 return &(ctx->q_data[V4L2_M2M_DST]);
288 default:
289 BUG();
290 }
291 return NULL;
292}
293
294/*
Philipp Zabelb96904e2013-05-23 10:42:58 -0300295 * Array of all formats supported by any version of Coda:
Javier Martin186b2502012-07-26 05:53:35 -0300296 */
Philipp Zabelb96904e2013-05-23 10:42:58 -0300297static struct coda_fmt coda_formats[] = {
Javier Martin186b2502012-07-26 05:53:35 -0300298 {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300299 .name = "YUV 4:2:0 Planar, YCbCr",
Javier Martin186b2502012-07-26 05:53:35 -0300300 .fourcc = V4L2_PIX_FMT_YUV420,
Philipp Zabelb96904e2013-05-23 10:42:58 -0300301 },
302 {
303 .name = "YUV 4:2:0 Planar, YCrCb",
304 .fourcc = V4L2_PIX_FMT_YVU420,
Javier Martin186b2502012-07-26 05:53:35 -0300305 },
306 {
307 .name = "H264 Encoded Stream",
308 .fourcc = V4L2_PIX_FMT_H264,
Javier Martin186b2502012-07-26 05:53:35 -0300309 },
310 {
311 .name = "MPEG4 Encoded Stream",
312 .fourcc = V4L2_PIX_FMT_MPEG4,
Javier Martin186b2502012-07-26 05:53:35 -0300313 },
314};
315
Philipp Zabelb96904e2013-05-23 10:42:58 -0300316#define CODA_CODEC(mode, src_fourcc, dst_fourcc, max_w, max_h) \
317 { mode, src_fourcc, dst_fourcc, max_w, max_h }
318
319/*
320 * Arrays of codecs supported by each given version of Coda:
321 * i.MX27 -> codadx6
322 * i.MX5x -> coda7
323 * i.MX6 -> coda960
324 * Use V4L2_PIX_FMT_YUV420 as placeholder for all supported YUV 4:2:0 variants
325 */
326static struct coda_codec codadx6_codecs[] = {
327 CODA_CODEC(CODADX6_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 720, 576),
328 CODA_CODEC(CODADX6_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 720, 576),
Philipp Zabeldf1e74c2012-07-02 06:07:10 -0300329};
330
Philipp Zabelb96904e2013-05-23 10:42:58 -0300331static struct coda_codec coda7_codecs[] = {
332 CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1280, 720),
333 CODA_CODEC(CODA7_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1280, 720),
334};
335
336static bool coda_format_is_yuv(u32 fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300337{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300338 switch (fourcc) {
339 case V4L2_PIX_FMT_YUV420:
340 case V4L2_PIX_FMT_YVU420:
341 return true;
342 default:
343 return false;
344 }
345}
Javier Martin186b2502012-07-26 05:53:35 -0300346
Philipp Zabelb96904e2013-05-23 10:42:58 -0300347/*
348 * Normalize all supported YUV 4:2:0 formats to the value used in the codec
349 * tables.
350 */
351static u32 coda_format_normalize_yuv(u32 fourcc)
352{
353 return coda_format_is_yuv(fourcc) ? V4L2_PIX_FMT_YUV420 : fourcc;
354}
355
356static struct coda_codec *coda_find_codec(struct coda_dev *dev, int src_fourcc,
357 int dst_fourcc)
358{
359 struct coda_codec *codecs = dev->devtype->codecs;
360 int num_codecs = dev->devtype->num_codecs;
361 int k;
362
363 src_fourcc = coda_format_normalize_yuv(src_fourcc);
364 dst_fourcc = coda_format_normalize_yuv(dst_fourcc);
365 if (src_fourcc == dst_fourcc)
366 return NULL;
367
368 for (k = 0; k < num_codecs; k++) {
369 if (codecs[k].src_fourcc == src_fourcc &&
370 codecs[k].dst_fourcc == dst_fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300371 break;
372 }
373
Philipp Zabelb96904e2013-05-23 10:42:58 -0300374 if (k == num_codecs)
Javier Martin186b2502012-07-26 05:53:35 -0300375 return NULL;
376
Philipp Zabelb96904e2013-05-23 10:42:58 -0300377 return &codecs[k];
Javier Martin186b2502012-07-26 05:53:35 -0300378}
379
380/*
381 * V4L2 ioctl() operations.
382 */
383static int vidioc_querycap(struct file *file, void *priv,
384 struct v4l2_capability *cap)
385{
386 strlcpy(cap->driver, CODA_NAME, sizeof(cap->driver));
387 strlcpy(cap->card, CODA_NAME, sizeof(cap->card));
Philipp Zabeldad0e1c2013-05-21 04:18:22 -0300388 strlcpy(cap->bus_info, "platform:" CODA_NAME, sizeof(cap->bus_info));
Sylwester Nawrockic3aac8b2012-08-22 18:00:19 -0300389 /*
390 * This is only a mem-to-mem video device. The capture and output
391 * device capability flags are left only for backward compatibility
392 * and are scheduled for removal.
393 */
394 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT |
395 V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
Javier Martin186b2502012-07-26 05:53:35 -0300396 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
397
398 return 0;
399}
400
401static int enum_fmt(void *priv, struct v4l2_fmtdesc *f,
Philipp Zabelb96904e2013-05-23 10:42:58 -0300402 enum v4l2_buf_type type)
Javier Martin186b2502012-07-26 05:53:35 -0300403{
404 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300405 struct coda_codec *codecs = ctx->dev->devtype->codecs;
406 struct coda_fmt *formats = coda_formats;
Javier Martin186b2502012-07-26 05:53:35 -0300407 struct coda_fmt *fmt;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300408 int num_codecs = ctx->dev->devtype->num_codecs;
409 int num_formats = ARRAY_SIZE(coda_formats);
410 int i, k, num = 0;
Javier Martin186b2502012-07-26 05:53:35 -0300411
412 for (i = 0; i < num_formats; i++) {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300413 /* Both uncompressed formats are always supported */
414 if (coda_format_is_yuv(formats[i].fourcc)) {
415 if (num == f->index)
416 break;
417 ++num;
418 continue;
419 }
420 /* Compressed formats may be supported, check the codec list */
421 for (k = 0; k < num_codecs; k++) {
422 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
423 formats[i].fourcc == codecs[k].dst_fourcc)
424 break;
425 if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
426 formats[i].fourcc == codecs[k].src_fourcc)
427 break;
428 }
429 if (k < num_codecs) {
Javier Martin186b2502012-07-26 05:53:35 -0300430 if (num == f->index)
431 break;
432 ++num;
433 }
434 }
435
436 if (i < num_formats) {
437 fmt = &formats[i];
438 strlcpy(f->description, fmt->name, sizeof(f->description));
439 f->pixelformat = fmt->fourcc;
440 return 0;
441 }
442
443 /* Format not found */
444 return -EINVAL;
445}
446
447static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
448 struct v4l2_fmtdesc *f)
449{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300450 return enum_fmt(priv, f, V4L2_BUF_TYPE_VIDEO_CAPTURE);
Javier Martin186b2502012-07-26 05:53:35 -0300451}
452
453static int vidioc_enum_fmt_vid_out(struct file *file, void *priv,
454 struct v4l2_fmtdesc *f)
455{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300456 return enum_fmt(priv, f, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Javier Martin186b2502012-07-26 05:53:35 -0300457}
458
459static int vidioc_g_fmt(struct file *file, void *priv, struct v4l2_format *f)
460{
461 struct vb2_queue *vq;
462 struct coda_q_data *q_data;
463 struct coda_ctx *ctx = fh_to_ctx(priv);
464
465 vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
466 if (!vq)
467 return -EINVAL;
468
469 q_data = get_q_data(ctx, f->type);
470
471 f->fmt.pix.field = V4L2_FIELD_NONE;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300472 f->fmt.pix.pixelformat = q_data->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -0300473 f->fmt.pix.width = q_data->width;
474 f->fmt.pix.height = q_data->height;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300475 if (coda_format_is_yuv(f->fmt.pix.pixelformat))
Javier Martin186b2502012-07-26 05:53:35 -0300476 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 2);
477 else /* encoded formats h.264/mpeg4 */
478 f->fmt.pix.bytesperline = 0;
479
480 f->fmt.pix.sizeimage = q_data->sizeimage;
481 f->fmt.pix.colorspace = ctx->colorspace;
482
483 return 0;
484}
485
Philipp Zabelb96904e2013-05-23 10:42:58 -0300486static int vidioc_try_fmt(struct coda_codec *codec, struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300487{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300488 unsigned int max_w, max_h;
Javier Martin186b2502012-07-26 05:53:35 -0300489 enum v4l2_field field;
490
491 field = f->fmt.pix.field;
492 if (field == V4L2_FIELD_ANY)
493 field = V4L2_FIELD_NONE;
494 else if (V4L2_FIELD_NONE != field)
495 return -EINVAL;
496
497 /* V4L2 specification suggests the driver corrects the format struct
498 * if any of the dimensions is unsupported */
499 f->fmt.pix.field = field;
500
Philipp Zabelb96904e2013-05-23 10:42:58 -0300501 if (codec) {
502 max_w = codec->max_w;
503 max_h = codec->max_h;
504 } else {
505 max_w = MAX_W;
506 max_h = MAX_H;
507 }
508 v4l_bound_align_image(&f->fmt.pix.width, MIN_W, max_w,
509 W_ALIGN, &f->fmt.pix.height,
510 MIN_H, max_h, H_ALIGN, S_ALIGN);
511
512 if (coda_format_is_yuv(f->fmt.pix.pixelformat)) {
Philipp Zabel47cf0c62013-05-23 10:42:54 -0300513 /* Frame stride must be multiple of 8 */
514 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 8);
515 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
Philipp Zabel451d43a2012-07-26 09:18:27 -0300516 f->fmt.pix.height * 3 / 2;
Javier Martin186b2502012-07-26 05:53:35 -0300517 } else { /*encoded formats h.264/mpeg4 */
518 f->fmt.pix.bytesperline = 0;
519 f->fmt.pix.sizeimage = CODA_MAX_FRAME_SIZE;
520 }
521
522 return 0;
523}
524
525static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
526 struct v4l2_format *f)
527{
Javier Martin186b2502012-07-26 05:53:35 -0300528 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300529 struct coda_codec *codec = NULL;
Javier Martin186b2502012-07-26 05:53:35 -0300530
Philipp Zabelb96904e2013-05-23 10:42:58 -0300531 /* Determine codec by the encoded format */
532 codec = coda_find_codec(ctx->dev, V4L2_PIX_FMT_YUV420,
533 f->fmt.pix.pixelformat);
Javier Martin186b2502012-07-26 05:53:35 -0300534
535 f->fmt.pix.colorspace = ctx->colorspace;
536
Philipp Zabelb96904e2013-05-23 10:42:58 -0300537 return vidioc_try_fmt(codec, f);
Javier Martin186b2502012-07-26 05:53:35 -0300538}
539
540static int vidioc_try_fmt_vid_out(struct file *file, void *priv,
541 struct v4l2_format *f)
542{
543 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300544 struct coda_codec *codec;
Javier Martin186b2502012-07-26 05:53:35 -0300545
Philipp Zabelb96904e2013-05-23 10:42:58 -0300546 /* Determine codec by encoded format, returns NULL if raw or invalid */
547 codec = coda_find_codec(ctx->dev, f->fmt.pix.pixelformat,
548 V4L2_PIX_FMT_YUV420);
Javier Martin186b2502012-07-26 05:53:35 -0300549
550 if (!f->fmt.pix.colorspace)
551 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
552
Philipp Zabelb96904e2013-05-23 10:42:58 -0300553 return vidioc_try_fmt(codec, f);
Javier Martin186b2502012-07-26 05:53:35 -0300554}
555
556static int vidioc_s_fmt(struct coda_ctx *ctx, struct v4l2_format *f)
557{
558 struct coda_q_data *q_data;
559 struct vb2_queue *vq;
Javier Martin186b2502012-07-26 05:53:35 -0300560
561 vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
562 if (!vq)
563 return -EINVAL;
564
565 q_data = get_q_data(ctx, f->type);
566 if (!q_data)
567 return -EINVAL;
568
569 if (vb2_is_busy(vq)) {
570 v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
571 return -EBUSY;
572 }
573
Philipp Zabelb96904e2013-05-23 10:42:58 -0300574 q_data->fourcc = f->fmt.pix.pixelformat;
Javier Martin186b2502012-07-26 05:53:35 -0300575 q_data->width = f->fmt.pix.width;
576 q_data->height = f->fmt.pix.height;
Philipp Zabel451d43a2012-07-26 09:18:27 -0300577 q_data->sizeimage = f->fmt.pix.sizeimage;
Javier Martin186b2502012-07-26 05:53:35 -0300578
579 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
580 "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
Philipp Zabelb96904e2013-05-23 10:42:58 -0300581 f->type, q_data->width, q_data->height, q_data->fourcc);
Javier Martin186b2502012-07-26 05:53:35 -0300582
583 return 0;
584}
585
586static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
587 struct v4l2_format *f)
588{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300589 struct coda_ctx *ctx = fh_to_ctx(priv);
Javier Martin186b2502012-07-26 05:53:35 -0300590 int ret;
591
592 ret = vidioc_try_fmt_vid_cap(file, priv, f);
593 if (ret)
594 return ret;
595
Philipp Zabelb96904e2013-05-23 10:42:58 -0300596 return vidioc_s_fmt(ctx, f);
Javier Martin186b2502012-07-26 05:53:35 -0300597}
598
599static int vidioc_s_fmt_vid_out(struct file *file, void *priv,
600 struct v4l2_format *f)
601{
602 struct coda_ctx *ctx = fh_to_ctx(priv);
603 int ret;
604
605 ret = vidioc_try_fmt_vid_out(file, priv, f);
606 if (ret)
607 return ret;
608
Richard Zhao8d621472012-08-21 02:47:42 -0300609 ret = vidioc_s_fmt(ctx, f);
Javier Martin186b2502012-07-26 05:53:35 -0300610 if (ret)
611 ctx->colorspace = f->fmt.pix.colorspace;
612
613 return ret;
614}
615
616static int vidioc_reqbufs(struct file *file, void *priv,
617 struct v4l2_requestbuffers *reqbufs)
618{
619 struct coda_ctx *ctx = fh_to_ctx(priv);
620
621 return v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
622}
623
624static int vidioc_querybuf(struct file *file, void *priv,
625 struct v4l2_buffer *buf)
626{
627 struct coda_ctx *ctx = fh_to_ctx(priv);
628
629 return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf);
630}
631
632static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
633{
634 struct coda_ctx *ctx = fh_to_ctx(priv);
635
636 return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
637}
638
Philipp Zabel419869c2013-05-23 07:06:30 -0300639static int vidioc_expbuf(struct file *file, void *priv,
640 struct v4l2_exportbuffer *eb)
641{
642 struct coda_ctx *ctx = fh_to_ctx(priv);
643
644 return v4l2_m2m_expbuf(file, ctx->m2m_ctx, eb);
645}
646
Javier Martin186b2502012-07-26 05:53:35 -0300647static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
648{
649 struct coda_ctx *ctx = fh_to_ctx(priv);
650
651 return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
652}
653
Philipp Zabel8fdf94a2013-05-21 04:16:29 -0300654static int vidioc_create_bufs(struct file *file, void *priv,
655 struct v4l2_create_buffers *create)
656{
657 struct coda_ctx *ctx = fh_to_ctx(priv);
658
659 return v4l2_m2m_create_bufs(file, ctx->m2m_ctx, create);
660}
661
Javier Martin186b2502012-07-26 05:53:35 -0300662static int vidioc_streamon(struct file *file, void *priv,
663 enum v4l2_buf_type type)
664{
665 struct coda_ctx *ctx = fh_to_ctx(priv);
666
667 return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
668}
669
670static int vidioc_streamoff(struct file *file, void *priv,
671 enum v4l2_buf_type type)
672{
673 struct coda_ctx *ctx = fh_to_ctx(priv);
674
675 return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
676}
677
678static const struct v4l2_ioctl_ops coda_ioctl_ops = {
679 .vidioc_querycap = vidioc_querycap,
680
681 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
682 .vidioc_g_fmt_vid_cap = vidioc_g_fmt,
683 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
684 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
685
686 .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
687 .vidioc_g_fmt_vid_out = vidioc_g_fmt,
688 .vidioc_try_fmt_vid_out = vidioc_try_fmt_vid_out,
689 .vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out,
690
691 .vidioc_reqbufs = vidioc_reqbufs,
692 .vidioc_querybuf = vidioc_querybuf,
693
694 .vidioc_qbuf = vidioc_qbuf,
Philipp Zabel419869c2013-05-23 07:06:30 -0300695 .vidioc_expbuf = vidioc_expbuf,
Javier Martin186b2502012-07-26 05:53:35 -0300696 .vidioc_dqbuf = vidioc_dqbuf,
Philipp Zabel8fdf94a2013-05-21 04:16:29 -0300697 .vidioc_create_bufs = vidioc_create_bufs,
Javier Martin186b2502012-07-26 05:53:35 -0300698
699 .vidioc_streamon = vidioc_streamon,
700 .vidioc_streamoff = vidioc_streamoff,
701};
702
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300703static inline int coda_get_bitstream_payload(struct coda_ctx *ctx)
704{
705 return kfifo_len(&ctx->bitstream_fifo);
706}
707
708static void coda_kfifo_sync_from_device(struct coda_ctx *ctx)
709{
710 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
711 struct coda_dev *dev = ctx->dev;
712 u32 rd_ptr;
713
714 rd_ptr = coda_read(dev, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
715 kfifo->out = (kfifo->in & ~kfifo->mask) |
716 (rd_ptr - ctx->bitstream.paddr);
717 if (kfifo->out > kfifo->in)
718 kfifo->out -= kfifo->mask + 1;
719}
720
721static void coda_kfifo_sync_to_device_full(struct coda_ctx *ctx)
722{
723 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
724 struct coda_dev *dev = ctx->dev;
725 u32 rd_ptr, wr_ptr;
726
727 rd_ptr = ctx->bitstream.paddr + (kfifo->out & kfifo->mask);
728 coda_write(dev, rd_ptr, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
729 wr_ptr = ctx->bitstream.paddr + (kfifo->in & kfifo->mask);
730 coda_write(dev, wr_ptr, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
731}
732
733static void coda_kfifo_sync_to_device_write(struct coda_ctx *ctx)
734{
735 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
736 struct coda_dev *dev = ctx->dev;
737 u32 wr_ptr;
738
739 wr_ptr = ctx->bitstream.paddr + (kfifo->in & kfifo->mask);
740 coda_write(dev, wr_ptr, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
741}
742
743static int coda_bitstream_queue(struct coda_ctx *ctx, struct vb2_buffer *src_buf)
744{
745 u32 src_size = vb2_get_plane_payload(src_buf, 0);
746 u32 n;
747
748 n = kfifo_in(&ctx->bitstream_fifo, vb2_plane_vaddr(src_buf, 0), src_size);
749 if (n < src_size)
750 return -ENOSPC;
751
752 dma_sync_single_for_device(&ctx->dev->plat_dev->dev, ctx->bitstream.paddr,
753 ctx->bitstream.size, DMA_TO_DEVICE);
754
755 ctx->qsequence++;
756
757 return 0;
758}
759
760static bool coda_bitstream_try_queue(struct coda_ctx *ctx,
761 struct vb2_buffer *src_buf)
762{
763 int ret;
764
765 if (coda_get_bitstream_payload(ctx) +
766 vb2_get_plane_payload(src_buf, 0) + 512 >= ctx->bitstream.size)
767 return false;
768
769 if (vb2_plane_vaddr(src_buf, 0) == NULL) {
770 v4l2_err(&ctx->dev->v4l2_dev, "trying to queue empty buffer\n");
771 return true;
772 }
773
774 ret = coda_bitstream_queue(ctx, src_buf);
775 if (ret < 0) {
776 v4l2_err(&ctx->dev->v4l2_dev, "bitstream buffer overflow\n");
777 return false;
778 }
779 /* Sync read pointer to device */
780 if (ctx == v4l2_m2m_get_curr_priv(ctx->dev->m2m_dev))
781 coda_kfifo_sync_to_device_write(ctx);
782
783 return true;
784}
785
786static void coda_fill_bitstream(struct coda_ctx *ctx)
787{
788 struct vb2_buffer *src_buf;
789
790 while (v4l2_m2m_num_src_bufs_ready(ctx->m2m_ctx) > 0) {
791 src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
792
793 if (coda_bitstream_try_queue(ctx, src_buf)) {
794 src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
795 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
796 } else {
797 break;
798 }
799 }
800}
801
Javier Martin186b2502012-07-26 05:53:35 -0300802/*
803 * Mem-to-mem operations.
804 */
805static void coda_device_run(void *m2m_priv)
806{
807 struct coda_ctx *ctx = m2m_priv;
808 struct coda_q_data *q_data_src, *q_data_dst;
809 struct vb2_buffer *src_buf, *dst_buf;
810 struct coda_dev *dev = ctx->dev;
811 int force_ipicture;
812 int quant_param = 0;
813 u32 picture_y, picture_cb, picture_cr;
814 u32 pic_stream_buffer_addr, pic_stream_buffer_size;
815 u32 dst_fourcc;
816
Philipp Zabelfcb62822013-05-23 10:43:00 -0300817 mutex_lock(&dev->coda_mutex);
818
Javier Martin186b2502012-07-26 05:53:35 -0300819 src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
820 dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
821 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
822 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300823 dst_fourcc = q_data_dst->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -0300824
825 src_buf->v4l2_buf.sequence = ctx->isequence;
826 dst_buf->v4l2_buf.sequence = ctx->isequence;
827 ctx->isequence++;
828
829 /*
830 * Workaround coda firmware BUG that only marks the first
831 * frame as IDR. This is a problem for some decoders that can't
832 * recover when a frame is lost.
833 */
834 if (src_buf->v4l2_buf.sequence % ctx->params.gop_size) {
835 src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
836 src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
837 } else {
838 src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
839 src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
840 }
841
842 /*
843 * Copy headers at the beginning of the first frame for H.264 only.
844 * In MPEG4 they are already copied by the coda.
845 */
846 if (src_buf->v4l2_buf.sequence == 0) {
847 pic_stream_buffer_addr =
848 vb2_dma_contig_plane_dma_addr(dst_buf, 0) +
849 ctx->vpu_header_size[0] +
850 ctx->vpu_header_size[1] +
851 ctx->vpu_header_size[2];
852 pic_stream_buffer_size = CODA_MAX_FRAME_SIZE -
853 ctx->vpu_header_size[0] -
854 ctx->vpu_header_size[1] -
855 ctx->vpu_header_size[2];
856 memcpy(vb2_plane_vaddr(dst_buf, 0),
857 &ctx->vpu_header[0][0], ctx->vpu_header_size[0]);
858 memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0],
859 &ctx->vpu_header[1][0], ctx->vpu_header_size[1]);
860 memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0] +
861 ctx->vpu_header_size[1], &ctx->vpu_header[2][0],
862 ctx->vpu_header_size[2]);
863 } else {
864 pic_stream_buffer_addr =
865 vb2_dma_contig_plane_dma_addr(dst_buf, 0);
866 pic_stream_buffer_size = CODA_MAX_FRAME_SIZE;
867 }
868
869 if (src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) {
870 force_ipicture = 1;
871 switch (dst_fourcc) {
872 case V4L2_PIX_FMT_H264:
873 quant_param = ctx->params.h264_intra_qp;
874 break;
875 case V4L2_PIX_FMT_MPEG4:
876 quant_param = ctx->params.mpeg4_intra_qp;
877 break;
878 default:
879 v4l2_warn(&ctx->dev->v4l2_dev,
880 "cannot set intra qp, fmt not supported\n");
881 break;
882 }
883 } else {
884 force_ipicture = 0;
885 switch (dst_fourcc) {
886 case V4L2_PIX_FMT_H264:
887 quant_param = ctx->params.h264_inter_qp;
888 break;
889 case V4L2_PIX_FMT_MPEG4:
890 quant_param = ctx->params.mpeg4_inter_qp;
891 break;
892 default:
893 v4l2_warn(&ctx->dev->v4l2_dev,
894 "cannot set inter qp, fmt not supported\n");
895 break;
896 }
897 }
898
899 /* submit */
Philipp Zabel8f35c7b2012-07-09 04:25:52 -0300900 coda_write(dev, CODA_ROT_MIR_ENABLE | ctx->params.rot_mode, CODA_CMD_ENC_PIC_ROT_MODE);
Javier Martin186b2502012-07-26 05:53:35 -0300901 coda_write(dev, quant_param, CODA_CMD_ENC_PIC_QS);
902
903
904 picture_y = vb2_dma_contig_plane_dma_addr(src_buf, 0);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300905 switch (q_data_src->fourcc) {
906 case V4L2_PIX_FMT_YVU420:
907 /* Switch Cb and Cr for YVU420 format */
908 picture_cr = picture_y + q_data_src->width * q_data_src->height;
909 picture_cb = picture_cr + q_data_src->width / 2 *
910 q_data_src->height / 2;
911 break;
912 case V4L2_PIX_FMT_YUV420:
913 default:
914 picture_cb = picture_y + q_data_src->width * q_data_src->height;
915 picture_cr = picture_cb + q_data_src->width / 2 *
916 q_data_src->height / 2;
917 break;
918 }
Javier Martin186b2502012-07-26 05:53:35 -0300919
920 coda_write(dev, picture_y, CODA_CMD_ENC_PIC_SRC_ADDR_Y);
921 coda_write(dev, picture_cb, CODA_CMD_ENC_PIC_SRC_ADDR_CB);
922 coda_write(dev, picture_cr, CODA_CMD_ENC_PIC_SRC_ADDR_CR);
923 coda_write(dev, force_ipicture << 1 & 0x2,
924 CODA_CMD_ENC_PIC_OPTION);
925
926 coda_write(dev, pic_stream_buffer_addr, CODA_CMD_ENC_PIC_BB_START);
927 coda_write(dev, pic_stream_buffer_size / 1024,
928 CODA_CMD_ENC_PIC_BB_SIZE);
Philipp Zabel10436672012-07-02 09:03:55 -0300929
930 if (dev->devtype->product == CODA_7541) {
931 coda_write(dev, CODA7_USE_BIT_ENABLE | CODA7_USE_HOST_BIT_ENABLE |
932 CODA7_USE_ME_ENABLE | CODA7_USE_HOST_ME_ENABLE,
933 CODA7_REG_BIT_AXI_SRAM_USE);
934 }
935
Philipp Zabelc2d22512013-06-21 03:55:28 -0300936 if (dev->devtype->product != CODA_DX6)
937 coda_write(dev, ctx->iram_info.axi_sram_use,
938 CODA7_REG_BIT_AXI_SRAM_USE);
939
Philipp Zabel2fb57f02012-07-25 09:22:07 -0300940 /* 1 second timeout in case CODA locks up */
941 schedule_delayed_work(&dev->timeout, HZ);
942
Javier Martin186b2502012-07-26 05:53:35 -0300943 coda_command_async(ctx, CODA_COMMAND_PIC_RUN);
944}
945
946static int coda_job_ready(void *m2m_priv)
947{
948 struct coda_ctx *ctx = m2m_priv;
949
950 /*
951 * For both 'P' and 'key' frame cases 1 picture
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300952 * and 1 frame are needed. In the decoder case,
953 * the compressed frame can be in the bitstream.
Javier Martin186b2502012-07-26 05:53:35 -0300954 */
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300955 if (!v4l2_m2m_num_src_bufs_ready(ctx->m2m_ctx) &&
956 ctx->inst_type != CODA_INST_DECODER) {
Javier Martin186b2502012-07-26 05:53:35 -0300957 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
958 "not ready: not enough video buffers.\n");
959 return 0;
960 }
961
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300962 if (!v4l2_m2m_num_dst_bufs_ready(ctx->m2m_ctx)) {
963 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
964 "not ready: not enough video capture buffers.\n");
965 return 0;
966 }
967
Philipp Zabel3e748262013-05-23 10:43:01 -0300968 if (ctx->aborting) {
969 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
970 "not ready: aborting\n");
971 return 0;
972 }
973
Javier Martin186b2502012-07-26 05:53:35 -0300974 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
975 "job ready\n");
976 return 1;
977}
978
979static void coda_job_abort(void *priv)
980{
981 struct coda_ctx *ctx = priv;
Javier Martin186b2502012-07-26 05:53:35 -0300982
983 ctx->aborting = 1;
984
985 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
986 "Aborting task\n");
Javier Martin186b2502012-07-26 05:53:35 -0300987}
988
989static void coda_lock(void *m2m_priv)
990{
991 struct coda_ctx *ctx = m2m_priv;
992 struct coda_dev *pcdev = ctx->dev;
993 mutex_lock(&pcdev->dev_mutex);
994}
995
996static void coda_unlock(void *m2m_priv)
997{
998 struct coda_ctx *ctx = m2m_priv;
999 struct coda_dev *pcdev = ctx->dev;
1000 mutex_unlock(&pcdev->dev_mutex);
1001}
1002
1003static struct v4l2_m2m_ops coda_m2m_ops = {
1004 .device_run = coda_device_run,
1005 .job_ready = coda_job_ready,
1006 .job_abort = coda_job_abort,
1007 .lock = coda_lock,
1008 .unlock = coda_unlock,
1009};
1010
1011static void set_default_params(struct coda_ctx *ctx)
1012{
Philipp Zabelb96904e2013-05-23 10:42:58 -03001013 int max_w;
1014 int max_h;
1015
1016 ctx->codec = &ctx->dev->devtype->codecs[0];
1017 max_w = ctx->codec->max_w;
1018 max_h = ctx->codec->max_h;
Javier Martin186b2502012-07-26 05:53:35 -03001019
1020 ctx->params.codec_mode = CODA_MODE_INVALID;
1021 ctx->colorspace = V4L2_COLORSPACE_REC709;
1022 ctx->params.framerate = 30;
Javier Martin186b2502012-07-26 05:53:35 -03001023 ctx->aborting = 0;
1024
1025 /* Default formats for output and input queues */
Philipp Zabelb96904e2013-05-23 10:42:58 -03001026 ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->codec->src_fourcc;
1027 ctx->q_data[V4L2_M2M_DST].fourcc = ctx->codec->dst_fourcc;
1028 ctx->q_data[V4L2_M2M_SRC].width = max_w;
1029 ctx->q_data[V4L2_M2M_SRC].height = max_h;
1030 ctx->q_data[V4L2_M2M_SRC].sizeimage = (max_w * max_h * 3) / 2;
1031 ctx->q_data[V4L2_M2M_DST].width = max_w;
1032 ctx->q_data[V4L2_M2M_DST].height = max_h;
Javier Martin186b2502012-07-26 05:53:35 -03001033 ctx->q_data[V4L2_M2M_DST].sizeimage = CODA_MAX_FRAME_SIZE;
1034}
1035
1036/*
1037 * Queue operations
1038 */
1039static int coda_queue_setup(struct vb2_queue *vq,
1040 const struct v4l2_format *fmt,
1041 unsigned int *nbuffers, unsigned int *nplanes,
1042 unsigned int sizes[], void *alloc_ctxs[])
1043{
1044 struct coda_ctx *ctx = vb2_get_drv_priv(vq);
Philipp Zabele34db062012-08-29 08:22:00 -03001045 struct coda_q_data *q_data;
Javier Martin186b2502012-07-26 05:53:35 -03001046 unsigned int size;
1047
Philipp Zabele34db062012-08-29 08:22:00 -03001048 q_data = get_q_data(ctx, vq->type);
1049 size = q_data->sizeimage;
Javier Martin186b2502012-07-26 05:53:35 -03001050
1051 *nplanes = 1;
1052 sizes[0] = size;
1053
1054 alloc_ctxs[0] = ctx->dev->alloc_ctx;
1055
1056 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1057 "get %d buffer(s) of size %d each.\n", *nbuffers, size);
1058
1059 return 0;
1060}
1061
1062static int coda_buf_prepare(struct vb2_buffer *vb)
1063{
1064 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1065 struct coda_q_data *q_data;
1066
1067 q_data = get_q_data(ctx, vb->vb2_queue->type);
1068
1069 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
1070 v4l2_warn(&ctx->dev->v4l2_dev,
1071 "%s data will not fit into plane (%lu < %lu)\n",
1072 __func__, vb2_plane_size(vb, 0),
1073 (long)q_data->sizeimage);
1074 return -EINVAL;
1075 }
1076
Javier Martin186b2502012-07-26 05:53:35 -03001077 return 0;
1078}
1079
1080static void coda_buf_queue(struct vb2_buffer *vb)
1081{
1082 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1083 v4l2_m2m_buf_queue(ctx->m2m_ctx, vb);
1084}
1085
1086static void coda_wait_prepare(struct vb2_queue *q)
1087{
1088 struct coda_ctx *ctx = vb2_get_drv_priv(q);
1089 coda_unlock(ctx);
1090}
1091
1092static void coda_wait_finish(struct vb2_queue *q)
1093{
1094 struct coda_ctx *ctx = vb2_get_drv_priv(q);
1095 coda_lock(ctx);
1096}
1097
Philipp Zabel86eda902013-05-23 10:42:57 -03001098static void coda_parabuf_write(struct coda_ctx *ctx, int index, u32 value)
1099{
1100 struct coda_dev *dev = ctx->dev;
1101 u32 *p = ctx->parabuf.vaddr;
1102
1103 if (dev->devtype->product == CODA_DX6)
1104 p[index] = value;
1105 else
1106 p[index ^ 1] = value;
1107}
1108
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001109static int coda_alloc_aux_buf(struct coda_dev *dev,
1110 struct coda_aux_buf *buf, size_t size)
1111{
1112 buf->vaddr = dma_alloc_coherent(&dev->plat_dev->dev, size, &buf->paddr,
1113 GFP_KERNEL);
1114 if (!buf->vaddr)
1115 return -ENOMEM;
1116
1117 buf->size = size;
1118
1119 return 0;
1120}
1121
1122static inline int coda_alloc_context_buf(struct coda_ctx *ctx,
1123 struct coda_aux_buf *buf, size_t size)
1124{
1125 return coda_alloc_aux_buf(ctx->dev, buf, size);
1126}
1127
1128static void coda_free_aux_buf(struct coda_dev *dev,
1129 struct coda_aux_buf *buf)
1130{
1131 if (buf->vaddr) {
1132 dma_free_coherent(&dev->plat_dev->dev, buf->size,
1133 buf->vaddr, buf->paddr);
1134 buf->vaddr = NULL;
1135 buf->size = 0;
1136 }
1137}
1138
1139static void coda_free_framebuffers(struct coda_ctx *ctx)
1140{
1141 int i;
1142
1143 for (i = 0; i < CODA_MAX_FRAMEBUFFERS; i++)
1144 coda_free_aux_buf(ctx->dev, &ctx->internal_frames[i]);
1145}
1146
Philipp Zabelec25f682012-07-20 08:54:29 -03001147static int coda_alloc_framebuffers(struct coda_ctx *ctx, struct coda_q_data *q_data, u32 fourcc)
1148{
1149 struct coda_dev *dev = ctx->dev;
Philipp Zabelec25f682012-07-20 08:54:29 -03001150 int height = q_data->height;
Philipp Zabel86eda902013-05-23 10:42:57 -03001151 dma_addr_t paddr;
1152 int ysize;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001153 int ret;
Philipp Zabelec25f682012-07-20 08:54:29 -03001154 int i;
1155
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001156 if (ctx->codec && ctx->codec->src_fourcc == V4L2_PIX_FMT_H264)
1157 height = round_up(height, 16);
Philipp Zabel86eda902013-05-23 10:42:57 -03001158 ysize = round_up(q_data->width, 8) * height;
1159
Philipp Zabelec25f682012-07-20 08:54:29 -03001160 /* Allocate frame buffers */
Philipp Zabelec25f682012-07-20 08:54:29 -03001161 for (i = 0; i < ctx->num_internal_frames; i++) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001162 size_t size;
1163
1164 size = q_data->sizeimage;
1165 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 &&
1166 dev->devtype->product != CODA_DX6)
Philipp Zabel86eda902013-05-23 10:42:57 -03001167 ctx->internal_frames[i].size += ysize/4;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001168 ret = coda_alloc_context_buf(ctx, &ctx->internal_frames[i], size);
1169 if (ret < 0) {
Philipp Zabelec25f682012-07-20 08:54:29 -03001170 coda_free_framebuffers(ctx);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001171 return ret;
Philipp Zabelec25f682012-07-20 08:54:29 -03001172 }
1173 }
1174
1175 /* Register frame buffers in the parameter buffer */
Philipp Zabel86eda902013-05-23 10:42:57 -03001176 for (i = 0; i < ctx->num_internal_frames; i++) {
1177 paddr = ctx->internal_frames[i].paddr;
1178 coda_parabuf_write(ctx, i * 3 + 0, paddr); /* Y */
1179 coda_parabuf_write(ctx, i * 3 + 1, paddr + ysize); /* Cb */
1180 coda_parabuf_write(ctx, i * 3 + 2, paddr + ysize + ysize/4); /* Cr */
Philipp Zabelec25f682012-07-20 08:54:29 -03001181
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001182 /* mvcol buffer for h.264 */
1183 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 &&
1184 dev->devtype->product != CODA_DX6)
1185 coda_parabuf_write(ctx, 96 + i,
1186 ctx->internal_frames[i].paddr +
1187 ysize + ysize/4 + ysize/4);
Philipp Zabelec25f682012-07-20 08:54:29 -03001188 }
1189
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001190 /* mvcol buffer for mpeg4 */
1191 if ((dev->devtype->product != CODA_DX6) &&
1192 (ctx->codec->src_fourcc == V4L2_PIX_FMT_MPEG4))
1193 coda_parabuf_write(ctx, 97, ctx->internal_frames[i].paddr +
1194 ysize + ysize/4 + ysize/4);
1195
Philipp Zabelec25f682012-07-20 08:54:29 -03001196 return 0;
1197}
1198
Javier Martin3f3f5c72012-10-29 05:20:29 -03001199static int coda_h264_padding(int size, char *p)
1200{
Javier Martin3f3f5c72012-10-29 05:20:29 -03001201 int nal_size;
1202 int diff;
1203
Javier Martin832fbb52012-10-29 08:34:59 -03001204 diff = size - (size & ~0x7);
Javier Martin3f3f5c72012-10-29 05:20:29 -03001205 if (diff == 0)
1206 return 0;
1207
Javier Martin832fbb52012-10-29 08:34:59 -03001208 nal_size = coda_filler_size[diff];
Javier Martin3f3f5c72012-10-29 05:20:29 -03001209 memcpy(p, coda_filler_nal, nal_size);
1210
1211 /* Add rbsp stop bit and trailing at the end */
1212 *(p + nal_size - 1) = 0x80;
1213
1214 return nal_size;
1215}
1216
Philipp Zabelc2d22512013-06-21 03:55:28 -03001217static void coda_setup_iram(struct coda_ctx *ctx)
1218{
1219 struct coda_iram_info *iram_info = &ctx->iram_info;
1220 struct coda_dev *dev = ctx->dev;
1221 int ipacdc_size;
1222 int bitram_size;
1223 int dbk_size;
Philipp Zabel8358e762013-06-21 03:55:32 -03001224 int ovl_size;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001225 int mb_width;
1226 int me_size;
1227 int size;
1228
1229 memset(iram_info, 0, sizeof(*iram_info));
1230 size = dev->iram_size;
1231
1232 if (dev->devtype->product == CODA_DX6)
1233 return;
1234
1235 if (ctx->inst_type == CODA_INST_ENCODER) {
1236 struct coda_q_data *q_data_src;
1237
1238 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1239 mb_width = DIV_ROUND_UP(q_data_src->width, 16);
1240
1241 /* Prioritize in case IRAM is too small for everything */
1242 me_size = round_up(round_up(q_data_src->width, 16) * 36 + 2048,
1243 1024);
1244 iram_info->search_ram_size = me_size;
1245 if (size >= iram_info->search_ram_size) {
1246 if (dev->devtype->product == CODA_7541)
1247 iram_info->axi_sram_use |= CODA7_USE_HOST_ME_ENABLE;
1248 iram_info->search_ram_paddr = dev->iram_paddr;
1249 size -= iram_info->search_ram_size;
1250 } else {
1251 pr_err("IRAM is smaller than the search ram size\n");
1252 goto out;
1253 }
1254
1255 /* Only H.264BP and H.263P3 are considered */
1256 dbk_size = round_up(128 * mb_width, 1024);
1257 if (size >= dbk_size) {
1258 iram_info->axi_sram_use |= CODA7_USE_HOST_DBK_ENABLE;
1259 iram_info->buf_dbk_y_use = dev->iram_paddr +
1260 iram_info->search_ram_size;
1261 iram_info->buf_dbk_c_use = iram_info->buf_dbk_y_use +
1262 dbk_size / 2;
1263 size -= dbk_size;
1264 } else {
1265 goto out;
1266 }
1267
1268 bitram_size = round_up(128 * mb_width, 1024);
1269 if (size >= bitram_size) {
1270 iram_info->axi_sram_use |= CODA7_USE_HOST_BIT_ENABLE;
1271 iram_info->buf_bit_use = iram_info->buf_dbk_c_use +
1272 dbk_size / 2;
1273 size -= bitram_size;
1274 } else {
1275 goto out;
1276 }
1277
1278 ipacdc_size = round_up(128 * mb_width, 1024);
1279 if (size >= ipacdc_size) {
1280 iram_info->axi_sram_use |= CODA7_USE_HOST_IP_ENABLE;
1281 iram_info->buf_ip_ac_dc_use = iram_info->buf_bit_use +
1282 bitram_size;
1283 size -= ipacdc_size;
1284 }
1285
Philipp Zabel8358e762013-06-21 03:55:32 -03001286 /* OVL and BTP disabled for encoder */
1287 } else if (ctx->inst_type == CODA_INST_DECODER) {
1288 struct coda_q_data *q_data_dst;
1289 int mb_height;
1290
1291 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1292 mb_width = DIV_ROUND_UP(q_data_dst->width, 16);
1293 mb_height = DIV_ROUND_UP(q_data_dst->height, 16);
1294
1295 dbk_size = round_up(256 * mb_width, 1024);
1296 if (size >= dbk_size) {
1297 iram_info->axi_sram_use |= CODA7_USE_HOST_DBK_ENABLE;
1298 iram_info->buf_dbk_y_use = dev->iram_paddr;
1299 iram_info->buf_dbk_c_use = dev->iram_paddr +
1300 dbk_size / 2;
1301 size -= dbk_size;
1302 } else {
1303 goto out;
1304 }
1305
1306 bitram_size = round_up(128 * mb_width, 1024);
1307 if (size >= bitram_size) {
1308 iram_info->axi_sram_use |= CODA7_USE_HOST_BIT_ENABLE;
1309 iram_info->buf_bit_use = iram_info->buf_dbk_c_use +
1310 dbk_size / 2;
1311 size -= bitram_size;
1312 } else {
1313 goto out;
1314 }
1315
1316 ipacdc_size = round_up(128 * mb_width, 1024);
1317 if (size >= ipacdc_size) {
1318 iram_info->axi_sram_use |= CODA7_USE_HOST_IP_ENABLE;
1319 iram_info->buf_ip_ac_dc_use = iram_info->buf_bit_use +
1320 bitram_size;
1321 size -= ipacdc_size;
1322 } else {
1323 goto out;
1324 }
1325
1326 ovl_size = round_up(80 * mb_width, 1024);
Philipp Zabelc2d22512013-06-21 03:55:28 -03001327 }
1328
1329out:
1330 switch (dev->devtype->product) {
1331 case CODA_DX6:
1332 break;
1333 case CODA_7541:
1334 /* i.MX53 uses secondary AXI for IRAM access */
1335 if (iram_info->axi_sram_use & CODA7_USE_HOST_BIT_ENABLE)
1336 iram_info->axi_sram_use |= CODA7_USE_BIT_ENABLE;
1337 if (iram_info->axi_sram_use & CODA7_USE_HOST_IP_ENABLE)
1338 iram_info->axi_sram_use |= CODA7_USE_IP_ENABLE;
1339 if (iram_info->axi_sram_use & CODA7_USE_HOST_DBK_ENABLE)
1340 iram_info->axi_sram_use |= CODA7_USE_DBK_ENABLE;
1341 if (iram_info->axi_sram_use & CODA7_USE_HOST_OVL_ENABLE)
1342 iram_info->axi_sram_use |= CODA7_USE_OVL_ENABLE;
1343 if (iram_info->axi_sram_use & CODA7_USE_HOST_ME_ENABLE)
1344 iram_info->axi_sram_use |= CODA7_USE_ME_ENABLE;
1345 }
1346
1347 if (!(iram_info->axi_sram_use & CODA7_USE_HOST_IP_ENABLE))
1348 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1349 "IRAM smaller than needed\n");
1350
1351 if (dev->devtype->product == CODA_7541) {
1352 /* TODO - Enabling these causes picture errors on CODA7541 */
Philipp Zabel8358e762013-06-21 03:55:32 -03001353 if (ctx->inst_type == CODA_INST_DECODER) {
1354 /* fw 1.4.50 */
1355 iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE |
1356 CODA7_USE_IP_ENABLE);
1357 } else {
1358 /* fw 13.4.29 */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001359 iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE |
1360 CODA7_USE_HOST_DBK_ENABLE |
1361 CODA7_USE_IP_ENABLE |
1362 CODA7_USE_DBK_ENABLE);
1363 }
1364 }
1365}
1366
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001367static void coda_free_context_buffers(struct coda_ctx *ctx)
1368{
1369 struct coda_dev *dev = ctx->dev;
1370
1371 if (dev->devtype->product != CODA_DX6)
1372 coda_free_aux_buf(dev, &ctx->workbuf);
1373}
1374
1375static int coda_alloc_context_buffers(struct coda_ctx *ctx,
1376 struct coda_q_data *q_data)
1377{
1378 struct coda_dev *dev = ctx->dev;
1379 size_t size;
1380 int ret;
1381
1382 switch (dev->devtype->product) {
1383 case CODA_7541:
1384 size = CODA7_WORK_BUF_SIZE;
1385 break;
1386 default:
1387 return 0;
1388 }
1389
1390 if (ctx->workbuf.vaddr) {
1391 v4l2_err(&dev->v4l2_dev, "context buffer still allocated\n");
1392 ret = -EBUSY;
1393 return -ENOMEM;
1394 }
1395
1396 ret = coda_alloc_context_buf(ctx, &ctx->workbuf, size);
1397 if (ret < 0) {
1398 v4l2_err(&dev->v4l2_dev, "failed to allocate %d byte context buffer",
1399 ctx->workbuf.size);
1400 goto err;
1401 }
1402
1403 return 0;
1404
1405err:
1406 coda_free_context_buffers(ctx);
1407 return ret;
1408}
1409
Philipp Zabeld35167a2013-05-23 10:42:59 -03001410static int coda_encode_header(struct coda_ctx *ctx, struct vb2_buffer *buf,
1411 int header_code, u8 *header, int *size)
1412{
1413 struct coda_dev *dev = ctx->dev;
1414 int ret;
1415
1416 coda_write(dev, vb2_dma_contig_plane_dma_addr(buf, 0),
1417 CODA_CMD_ENC_HEADER_BB_START);
1418 coda_write(dev, vb2_plane_size(buf, 0), CODA_CMD_ENC_HEADER_BB_SIZE);
1419 coda_write(dev, header_code, CODA_CMD_ENC_HEADER_CODE);
1420 ret = coda_command_sync(ctx, CODA_COMMAND_ENCODE_HEADER);
1421 if (ret < 0) {
1422 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_ENCODE_HEADER timeout\n");
1423 return ret;
1424 }
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001425 *size = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx)) -
Philipp Zabeld35167a2013-05-23 10:42:59 -03001426 coda_read(dev, CODA_CMD_ENC_HEADER_BB_START);
1427 memcpy(header, vb2_plane_vaddr(buf, 0), *size);
1428
1429 return 0;
1430}
1431
Javier Martin186b2502012-07-26 05:53:35 -03001432static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
1433{
1434 struct coda_ctx *ctx = vb2_get_drv_priv(q);
1435 struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
1436 u32 bitstream_buf, bitstream_size;
1437 struct coda_dev *dev = ctx->dev;
1438 struct coda_q_data *q_data_src, *q_data_dst;
Javier Martin186b2502012-07-26 05:53:35 -03001439 struct vb2_buffer *buf;
Philipp Zabelec25f682012-07-20 08:54:29 -03001440 u32 dst_fourcc;
Javier Martin186b2502012-07-26 05:53:35 -03001441 u32 value;
Philipp Zabeld35167a2013-05-23 10:42:59 -03001442 int ret = 0;
Javier Martin186b2502012-07-26 05:53:35 -03001443
1444 if (count < 1)
1445 return -EINVAL;
1446
1447 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
Philipp Zabelb96904e2013-05-23 10:42:58 -03001448 ctx->streamon_out = 1;
Javier Martin186b2502012-07-26 05:53:35 -03001449 else
Philipp Zabelb96904e2013-05-23 10:42:58 -03001450 ctx->streamon_cap = 1;
Javier Martin186b2502012-07-26 05:53:35 -03001451
Philipp Zabelb96904e2013-05-23 10:42:58 -03001452 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1453 if (ctx->streamon_out) {
1454 if (coda_format_is_yuv(q_data_src->fourcc))
1455 ctx->inst_type = CODA_INST_ENCODER;
1456 else
1457 ctx->inst_type = CODA_INST_DECODER;
1458 }
Javier Martin186b2502012-07-26 05:53:35 -03001459
Philipp Zabelb96904e2013-05-23 10:42:58 -03001460 /* Don't start the coda unless both queues are on */
1461 if (!(ctx->streamon_out & ctx->streamon_cap))
1462 return 0;
Javier Martin186b2502012-07-26 05:53:35 -03001463
Philipp Zabelb96904e2013-05-23 10:42:58 -03001464 ctx->gopcounter = ctx->params.gop_size - 1;
Javier Martin186b2502012-07-26 05:53:35 -03001465 buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
1466 bitstream_buf = vb2_dma_contig_plane_dma_addr(buf, 0);
1467 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1468 bitstream_size = q_data_dst->sizeimage;
Philipp Zabelb96904e2013-05-23 10:42:58 -03001469 dst_fourcc = q_data_dst->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -03001470
Philipp Zabelb96904e2013-05-23 10:42:58 -03001471 ctx->codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
1472 q_data_dst->fourcc);
1473 if (!ctx->codec) {
Javier Martin186b2502012-07-26 05:53:35 -03001474 v4l2_err(v4l2_dev, "couldn't tell instance type.\n");
1475 return -EINVAL;
1476 }
1477
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001478 /* Allocate per-instance buffers */
1479 ret = coda_alloc_context_buffers(ctx, q_data_src);
1480 if (ret < 0)
1481 return ret;
1482
Javier Martin186b2502012-07-26 05:53:35 -03001483 if (!coda_is_initialized(dev)) {
1484 v4l2_err(v4l2_dev, "coda is not initialized.\n");
1485 return -EFAULT;
1486 }
Philipp Zabelfcb62822013-05-23 10:43:00 -03001487
1488 mutex_lock(&dev->coda_mutex);
1489
Javier Martin186b2502012-07-26 05:53:35 -03001490 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001491 coda_write(dev, bitstream_buf, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
1492 coda_write(dev, bitstream_buf, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
Javier Martin186b2502012-07-26 05:53:35 -03001493 switch (dev->devtype->product) {
1494 case CODA_DX6:
1495 coda_write(dev, CODADX6_STREAM_BUF_DYNALLOC_EN |
1496 CODADX6_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
1497 break;
1498 default:
1499 coda_write(dev, CODA7_STREAM_BUF_DYNALLOC_EN |
1500 CODA7_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
1501 }
1502
Philipp Zabel10436672012-07-02 09:03:55 -03001503 if (dev->devtype->product == CODA_DX6) {
1504 /* Configure the coda */
1505 coda_write(dev, dev->iram_paddr, CODADX6_REG_BIT_SEARCH_RAM_BASE_ADDR);
1506 }
Javier Martin186b2502012-07-26 05:53:35 -03001507
1508 /* Could set rotation here if needed */
1509 switch (dev->devtype->product) {
1510 case CODA_DX6:
1511 value = (q_data_src->width & CODADX6_PICWIDTH_MASK) << CODADX6_PICWIDTH_OFFSET;
Philipp Zabelb96904e2013-05-23 10:42:58 -03001512 value |= (q_data_src->height & CODADX6_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03001513 break;
1514 default:
1515 value = (q_data_src->width & CODA7_PICWIDTH_MASK) << CODA7_PICWIDTH_OFFSET;
Philipp Zabelb96904e2013-05-23 10:42:58 -03001516 value |= (q_data_src->height & CODA7_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03001517 }
Javier Martin186b2502012-07-26 05:53:35 -03001518 coda_write(dev, value, CODA_CMD_ENC_SEQ_SRC_SIZE);
1519 coda_write(dev, ctx->params.framerate,
1520 CODA_CMD_ENC_SEQ_SRC_F_RATE);
1521
Philipp Zabelb96904e2013-05-23 10:42:58 -03001522 ctx->params.codec_mode = ctx->codec->mode;
Javier Martin186b2502012-07-26 05:53:35 -03001523 switch (dst_fourcc) {
1524 case V4L2_PIX_FMT_MPEG4:
Javier Martin186b2502012-07-26 05:53:35 -03001525 coda_write(dev, CODA_STD_MPEG4, CODA_CMD_ENC_SEQ_COD_STD);
1526 coda_write(dev, 0, CODA_CMD_ENC_SEQ_MP4_PARA);
1527 break;
1528 case V4L2_PIX_FMT_H264:
Javier Martin186b2502012-07-26 05:53:35 -03001529 coda_write(dev, CODA_STD_H264, CODA_CMD_ENC_SEQ_COD_STD);
1530 coda_write(dev, 0, CODA_CMD_ENC_SEQ_264_PARA);
1531 break;
1532 default:
1533 v4l2_err(v4l2_dev,
1534 "dst format (0x%08x) invalid.\n", dst_fourcc);
Philipp Zabelfcb62822013-05-23 10:43:00 -03001535 ret = -EINVAL;
1536 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03001537 }
1538
Philipp Zabelc566c782012-08-08 11:59:38 -03001539 switch (ctx->params.slice_mode) {
1540 case V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE:
1541 value = 0;
1542 break;
1543 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_MB:
1544 value = (ctx->params.slice_max_mb & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
1545 value |= (1 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03001546 value |= 1 & CODA_SLICING_MODE_MASK;
Philipp Zabelc566c782012-08-08 11:59:38 -03001547 break;
1548 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES:
1549 value = (ctx->params.slice_max_bits & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
1550 value |= (0 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
1551 value |= 1 & CODA_SLICING_MODE_MASK;
1552 break;
1553 }
Javier Martin186b2502012-07-26 05:53:35 -03001554 coda_write(dev, value, CODA_CMD_ENC_SEQ_SLICE_MODE);
Philipp Zabelc566c782012-08-08 11:59:38 -03001555 value = ctx->params.gop_size & CODA_GOP_SIZE_MASK;
Javier Martin186b2502012-07-26 05:53:35 -03001556 coda_write(dev, value, CODA_CMD_ENC_SEQ_GOP_SIZE);
1557
1558 if (ctx->params.bitrate) {
1559 /* Rate control enabled */
1560 value = (ctx->params.bitrate & CODA_RATECONTROL_BITRATE_MASK) << CODA_RATECONTROL_BITRATE_OFFSET;
1561 value |= 1 & CODA_RATECONTROL_ENABLE_MASK;
1562 } else {
1563 value = 0;
1564 }
1565 coda_write(dev, value, CODA_CMD_ENC_SEQ_RC_PARA);
1566
1567 coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_BUF_SIZE);
1568 coda_write(dev, 0, CODA_CMD_ENC_SEQ_INTRA_REFRESH);
1569
1570 coda_write(dev, bitstream_buf, CODA_CMD_ENC_SEQ_BB_START);
1571 coda_write(dev, bitstream_size / 1024, CODA_CMD_ENC_SEQ_BB_SIZE);
1572
1573 /* set default gamma */
1574 value = (CODA_DEFAULT_GAMMA & CODA_GAMMA_MASK) << CODA_GAMMA_OFFSET;
1575 coda_write(dev, value, CODA_CMD_ENC_SEQ_RC_GAMMA);
1576
Philipp Zabelfb1fcf12013-05-23 10:42:53 -03001577 if (CODA_DEFAULT_GAMMA > 0) {
1578 if (dev->devtype->product == CODA_DX6)
1579 value = 1 << CODADX6_OPTION_GAMMA_OFFSET;
1580 else
1581 value = 1 << CODA7_OPTION_GAMMA_OFFSET;
1582 } else {
1583 value = 0;
1584 }
Javier Martin186b2502012-07-26 05:53:35 -03001585 coda_write(dev, value, CODA_CMD_ENC_SEQ_OPTION);
1586
Philipp Zabelc2d22512013-06-21 03:55:28 -03001587 coda_setup_iram(ctx);
1588
Javier Martin186b2502012-07-26 05:53:35 -03001589 if (dst_fourcc == V4L2_PIX_FMT_H264) {
1590 value = (FMO_SLICE_SAVE_BUF_SIZE << 7);
1591 value |= (0 & CODA_FMOPARAM_TYPE_MASK) << CODA_FMOPARAM_TYPE_OFFSET;
1592 value |= 0 & CODA_FMOPARAM_SLICENUM_MASK;
Philipp Zabel10436672012-07-02 09:03:55 -03001593 if (dev->devtype->product == CODA_DX6) {
1594 coda_write(dev, value, CODADX6_CMD_ENC_SEQ_FMO);
1595 } else {
Philipp Zabelc2d22512013-06-21 03:55:28 -03001596 coda_write(dev, ctx->iram_info.search_ram_paddr,
1597 CODA7_CMD_ENC_SEQ_SEARCH_BASE);
1598 coda_write(dev, ctx->iram_info.search_ram_size,
1599 CODA7_CMD_ENC_SEQ_SEARCH_SIZE);
Philipp Zabel10436672012-07-02 09:03:55 -03001600 }
Javier Martin186b2502012-07-26 05:53:35 -03001601 }
1602
Philipp Zabelfcb62822013-05-23 10:43:00 -03001603 ret = coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT);
1604 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03001605 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
Philipp Zabelfcb62822013-05-23 10:43:00 -03001606 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03001607 }
1608
Philipp Zabelfcb62822013-05-23 10:43:00 -03001609 if (coda_read(dev, CODA_RET_ENC_SEQ_SUCCESS) == 0) {
1610 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT failed\n");
1611 ret = -EFAULT;
1612 goto out;
1613 }
Javier Martin186b2502012-07-26 05:53:35 -03001614
Philipp Zabel20397492013-06-21 03:55:29 -03001615 ctx->num_internal_frames = 2;
Philipp Zabelec25f682012-07-20 08:54:29 -03001616 ret = coda_alloc_framebuffers(ctx, q_data_src, dst_fourcc);
Philipp Zabelfcb62822013-05-23 10:43:00 -03001617 if (ret < 0) {
1618 v4l2_err(v4l2_dev, "failed to allocate framebuffers\n");
1619 goto out;
1620 }
Javier Martin186b2502012-07-26 05:53:35 -03001621
Philipp Zabelec25f682012-07-20 08:54:29 -03001622 coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM);
Philipp Zabel10436672012-07-02 09:03:55 -03001623 coda_write(dev, round_up(q_data_src->width, 8), CODA_CMD_SET_FRAME_BUF_STRIDE);
1624 if (dev->devtype->product != CODA_DX6) {
Philipp Zabelc2d22512013-06-21 03:55:28 -03001625 coda_write(dev, ctx->iram_info.buf_bit_use,
1626 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
1627 coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use,
1628 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
1629 coda_write(dev, ctx->iram_info.buf_dbk_y_use,
1630 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
1631 coda_write(dev, ctx->iram_info.buf_dbk_c_use,
1632 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
1633 coda_write(dev, ctx->iram_info.buf_ovl_use,
1634 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
Philipp Zabel10436672012-07-02 09:03:55 -03001635 }
Philipp Zabelfcb62822013-05-23 10:43:00 -03001636 ret = coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF);
1637 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03001638 v4l2_err(v4l2_dev, "CODA_COMMAND_SET_FRAME_BUF timeout\n");
Philipp Zabelfcb62822013-05-23 10:43:00 -03001639 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03001640 }
1641
1642 /* Save stream headers */
1643 buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
1644 switch (dst_fourcc) {
1645 case V4L2_PIX_FMT_H264:
1646 /*
1647 * Get SPS in the first frame and copy it to an
1648 * intermediate buffer.
1649 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03001650 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_SPS,
1651 &ctx->vpu_header[0][0],
1652 &ctx->vpu_header_size[0]);
1653 if (ret < 0)
1654 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03001655
1656 /*
1657 * Get PPS in the first frame and copy it to an
1658 * intermediate buffer.
1659 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03001660 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_PPS,
1661 &ctx->vpu_header[1][0],
1662 &ctx->vpu_header_size[1]);
1663 if (ret < 0)
1664 goto out;
1665
Javier Martin3f3f5c72012-10-29 05:20:29 -03001666 /*
1667 * Length of H.264 headers is variable and thus it might not be
1668 * aligned for the coda to append the encoded frame. In that is
1669 * the case a filler NAL must be added to header 2.
1670 */
1671 ctx->vpu_header_size[2] = coda_h264_padding(
1672 (ctx->vpu_header_size[0] +
1673 ctx->vpu_header_size[1]),
1674 ctx->vpu_header[2]);
Javier Martin186b2502012-07-26 05:53:35 -03001675 break;
1676 case V4L2_PIX_FMT_MPEG4:
1677 /*
1678 * Get VOS in the first frame and copy it to an
1679 * intermediate buffer
1680 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03001681 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOS,
1682 &ctx->vpu_header[0][0],
1683 &ctx->vpu_header_size[0]);
1684 if (ret < 0)
1685 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03001686
Philipp Zabeld35167a2013-05-23 10:42:59 -03001687 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VIS,
1688 &ctx->vpu_header[1][0],
1689 &ctx->vpu_header_size[1]);
1690 if (ret < 0)
1691 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03001692
Philipp Zabeld35167a2013-05-23 10:42:59 -03001693 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOL,
1694 &ctx->vpu_header[2][0],
1695 &ctx->vpu_header_size[2]);
1696 if (ret < 0)
1697 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03001698 break;
1699 default:
1700 /* No more formats need to save headers at the moment */
1701 break;
1702 }
1703
Philipp Zabeld35167a2013-05-23 10:42:59 -03001704out:
Philipp Zabelfcb62822013-05-23 10:43:00 -03001705 mutex_unlock(&dev->coda_mutex);
Philipp Zabeld35167a2013-05-23 10:42:59 -03001706 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03001707}
1708
1709static int coda_stop_streaming(struct vb2_queue *q)
1710{
1711 struct coda_ctx *ctx = vb2_get_drv_priv(q);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03001712 struct coda_dev *dev = ctx->dev;
Javier Martin186b2502012-07-26 05:53:35 -03001713
1714 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1715 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1716 "%s: output\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001717 ctx->streamon_out = 0;
Javier Martin186b2502012-07-26 05:53:35 -03001718 } else {
1719 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1720 "%s: capture\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001721 ctx->streamon_cap = 0;
Javier Martin186b2502012-07-26 05:53:35 -03001722 }
1723
Philipp Zabel62bed142012-07-25 10:40:39 -03001724 /* Don't stop the coda unless both queues are off */
Philipp Zabelb96904e2013-05-23 10:42:58 -03001725 if (ctx->streamon_out || ctx->streamon_cap)
Philipp Zabel62bed142012-07-25 10:40:39 -03001726 return 0;
Philipp Zabel2fb57f02012-07-25 09:22:07 -03001727
Philipp Zabel62bed142012-07-25 10:40:39 -03001728 cancel_delayed_work(&dev->timeout);
1729
Philipp Zabelfcb62822013-05-23 10:43:00 -03001730 mutex_lock(&dev->coda_mutex);
Philipp Zabel62bed142012-07-25 10:40:39 -03001731 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1732 "%s: sent command 'SEQ_END' to coda\n", __func__);
1733 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_END)) {
1734 v4l2_err(&dev->v4l2_dev,
1735 "CODA_COMMAND_SEQ_END failed\n");
1736 return -ETIMEDOUT;
1737 }
Philipp Zabelfcb62822013-05-23 10:43:00 -03001738 mutex_unlock(&dev->coda_mutex);
Philipp Zabel62bed142012-07-25 10:40:39 -03001739
1740 coda_free_framebuffers(ctx);
1741
Javier Martin186b2502012-07-26 05:53:35 -03001742 return 0;
1743}
1744
1745static struct vb2_ops coda_qops = {
1746 .queue_setup = coda_queue_setup,
1747 .buf_prepare = coda_buf_prepare,
1748 .buf_queue = coda_buf_queue,
1749 .wait_prepare = coda_wait_prepare,
1750 .wait_finish = coda_wait_finish,
1751 .start_streaming = coda_start_streaming,
1752 .stop_streaming = coda_stop_streaming,
1753};
1754
1755static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
1756{
1757 struct coda_ctx *ctx =
1758 container_of(ctrl->handler, struct coda_ctx, ctrls);
1759
1760 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1761 "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val);
1762
1763 switch (ctrl->id) {
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03001764 case V4L2_CID_HFLIP:
1765 if (ctrl->val)
1766 ctx->params.rot_mode |= CODA_MIR_HOR;
1767 else
1768 ctx->params.rot_mode &= ~CODA_MIR_HOR;
1769 break;
1770 case V4L2_CID_VFLIP:
1771 if (ctrl->val)
1772 ctx->params.rot_mode |= CODA_MIR_VER;
1773 else
1774 ctx->params.rot_mode &= ~CODA_MIR_VER;
1775 break;
Javier Martin186b2502012-07-26 05:53:35 -03001776 case V4L2_CID_MPEG_VIDEO_BITRATE:
1777 ctx->params.bitrate = ctrl->val / 1000;
1778 break;
1779 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
1780 ctx->params.gop_size = ctrl->val;
1781 break;
1782 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
1783 ctx->params.h264_intra_qp = ctrl->val;
1784 break;
1785 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
1786 ctx->params.h264_inter_qp = ctrl->val;
1787 break;
1788 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
1789 ctx->params.mpeg4_intra_qp = ctrl->val;
1790 break;
1791 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
1792 ctx->params.mpeg4_inter_qp = ctrl->val;
1793 break;
1794 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
1795 ctx->params.slice_mode = ctrl->val;
1796 break;
1797 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
1798 ctx->params.slice_max_mb = ctrl->val;
1799 break;
Philipp Zabelc566c782012-08-08 11:59:38 -03001800 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
1801 ctx->params.slice_max_bits = ctrl->val * 8;
1802 break;
Javier Martin186b2502012-07-26 05:53:35 -03001803 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
1804 break;
1805 default:
1806 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1807 "Invalid control, id=%d, val=%d\n",
1808 ctrl->id, ctrl->val);
1809 return -EINVAL;
1810 }
1811
1812 return 0;
1813}
1814
1815static struct v4l2_ctrl_ops coda_ctrl_ops = {
1816 .s_ctrl = coda_s_ctrl,
1817};
1818
1819static int coda_ctrls_setup(struct coda_ctx *ctx)
1820{
1821 v4l2_ctrl_handler_init(&ctx->ctrls, 9);
1822
1823 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03001824 V4L2_CID_HFLIP, 0, 1, 1, 0);
1825 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1826 V4L2_CID_VFLIP, 0, 1, 1, 0);
1827 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Javier Martin186b2502012-07-26 05:53:35 -03001828 V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1, 0);
1829 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1830 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 60, 1, 16);
1831 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1832 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 1, 51, 1, 25);
1833 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1834 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 1, 51, 1, 25);
1835 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1836 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
1837 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1838 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
1839 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1840 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
Philipp Zabelc566c782012-08-08 11:59:38 -03001841 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0,
1842 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
Javier Martin186b2502012-07-26 05:53:35 -03001843 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1844 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
Philipp Zabelc566c782012-08-08 11:59:38 -03001845 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1846 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1, 500);
Javier Martin186b2502012-07-26 05:53:35 -03001847 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1848 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
1849 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
1850 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
1851 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
1852
1853 if (ctx->ctrls.error) {
1854 v4l2_err(&ctx->dev->v4l2_dev, "control initialization error (%d)",
1855 ctx->ctrls.error);
1856 return -EINVAL;
1857 }
1858
1859 return v4l2_ctrl_handler_setup(&ctx->ctrls);
1860}
1861
1862static int coda_queue_init(void *priv, struct vb2_queue *src_vq,
1863 struct vb2_queue *dst_vq)
1864{
1865 struct coda_ctx *ctx = priv;
1866 int ret;
1867
Javier Martin186b2502012-07-26 05:53:35 -03001868 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
Philipp Zabel419869c2013-05-23 07:06:30 -03001869 src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
Javier Martin186b2502012-07-26 05:53:35 -03001870 src_vq->drv_priv = ctx;
1871 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1872 src_vq->ops = &coda_qops;
1873 src_vq->mem_ops = &vb2_dma_contig_memops;
Kamil Debskiccd571c2013-04-24 10:38:24 -03001874 src_vq->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Javier Martin186b2502012-07-26 05:53:35 -03001875
1876 ret = vb2_queue_init(src_vq);
1877 if (ret)
1878 return ret;
1879
Javier Martin186b2502012-07-26 05:53:35 -03001880 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Philipp Zabel419869c2013-05-23 07:06:30 -03001881 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
Javier Martin186b2502012-07-26 05:53:35 -03001882 dst_vq->drv_priv = ctx;
1883 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1884 dst_vq->ops = &coda_qops;
1885 dst_vq->mem_ops = &vb2_dma_contig_memops;
Kamil Debskiccd571c2013-04-24 10:38:24 -03001886 dst_vq->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Javier Martin186b2502012-07-26 05:53:35 -03001887
1888 return vb2_queue_init(dst_vq);
1889}
1890
Philipp Zabele11f3e62012-07-25 09:16:58 -03001891static int coda_next_free_instance(struct coda_dev *dev)
1892{
1893 return ffz(dev->instance_mask);
1894}
1895
Javier Martin186b2502012-07-26 05:53:35 -03001896static int coda_open(struct file *file)
1897{
1898 struct coda_dev *dev = video_drvdata(file);
1899 struct coda_ctx *ctx = NULL;
1900 int ret = 0;
Philipp Zabele11f3e62012-07-25 09:16:58 -03001901 int idx;
Javier Martin186b2502012-07-26 05:53:35 -03001902
Philipp Zabele11f3e62012-07-25 09:16:58 -03001903 idx = coda_next_free_instance(dev);
1904 if (idx >= CODA_MAX_INSTANCES)
Javier Martin186b2502012-07-26 05:53:35 -03001905 return -EBUSY;
Philipp Zabele11f3e62012-07-25 09:16:58 -03001906 set_bit(idx, &dev->instance_mask);
Javier Martin186b2502012-07-26 05:53:35 -03001907
1908 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
1909 if (!ctx)
1910 return -ENOMEM;
1911
1912 v4l2_fh_init(&ctx->fh, video_devdata(file));
1913 file->private_data = &ctx->fh;
1914 v4l2_fh_add(&ctx->fh);
1915 ctx->dev = dev;
Philipp Zabele11f3e62012-07-25 09:16:58 -03001916 ctx->idx = idx;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001917 switch (dev->devtype->product) {
1918 case CODA_7541:
1919 ctx->reg_idx = 0;
1920 break;
1921 default:
1922 ctx->reg_idx = idx;
1923 }
Javier Martin186b2502012-07-26 05:53:35 -03001924 set_default_params(ctx);
1925 ctx->m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
1926 &coda_queue_init);
1927 if (IS_ERR(ctx->m2m_ctx)) {
Philipp Zabel72720ff2013-05-21 10:31:25 -03001928 ret = PTR_ERR(ctx->m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001929
1930 v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
1931 __func__, ret);
1932 goto err;
1933 }
1934 ret = coda_ctrls_setup(ctx);
1935 if (ret) {
1936 v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
1937 goto err;
1938 }
1939
1940 ctx->fh.ctrl_handler = &ctx->ctrls;
1941
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001942 ret = coda_alloc_context_buf(ctx, &ctx->parabuf, CODA_PARA_BUF_SIZE);
1943 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03001944 v4l2_err(&dev->v4l2_dev, "failed to allocate parabuf");
Javier Martin186b2502012-07-26 05:53:35 -03001945 goto err;
1946 }
1947
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001948 ctx->bitstream.size = CODA_MAX_FRAME_SIZE;
1949 ctx->bitstream.vaddr = dma_alloc_writecombine(&dev->plat_dev->dev,
1950 ctx->bitstream.size, &ctx->bitstream.paddr, GFP_KERNEL);
1951 if (!ctx->bitstream.vaddr) {
1952 v4l2_err(&dev->v4l2_dev, "failed to allocate bitstream ringbuffer");
1953 ret = -ENOMEM;
1954 goto err;
1955 }
1956 kfifo_init(&ctx->bitstream_fifo,
1957 ctx->bitstream.vaddr, ctx->bitstream.size);
1958 mutex_init(&ctx->bitstream_mutex);
1959
Javier Martin186b2502012-07-26 05:53:35 -03001960 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03001961 list_add(&ctx->list, &dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03001962 coda_unlock(ctx);
1963
1964 clk_prepare_enable(dev->clk_per);
1965 clk_prepare_enable(dev->clk_ahb);
1966
1967 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Created instance %d (%p)\n",
1968 ctx->idx, ctx);
1969
1970 return 0;
1971
1972err:
1973 v4l2_fh_del(&ctx->fh);
1974 v4l2_fh_exit(&ctx->fh);
1975 kfree(ctx);
1976 return ret;
1977}
1978
1979static int coda_release(struct file *file)
1980{
1981 struct coda_dev *dev = video_drvdata(file);
1982 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
1983
1984 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Releasing instance %p\n",
1985 ctx);
1986
1987 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03001988 list_del(&ctx->list);
Javier Martin186b2502012-07-26 05:53:35 -03001989 coda_unlock(ctx);
1990
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001991 dma_free_writecombine(&dev->plat_dev->dev, ctx->bitstream.size,
1992 ctx->bitstream.vaddr, ctx->bitstream.paddr);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001993 coda_free_context_buffers(ctx);
1994 if (ctx->dev->devtype->product == CODA_DX6)
1995 coda_free_aux_buf(dev, &ctx->workbuf);
1996
1997 coda_free_aux_buf(dev, &ctx->parabuf);
Javier Martin186b2502012-07-26 05:53:35 -03001998 v4l2_ctrl_handler_free(&ctx->ctrls);
1999 clk_disable_unprepare(dev->clk_per);
2000 clk_disable_unprepare(dev->clk_ahb);
2001 v4l2_fh_del(&ctx->fh);
2002 v4l2_fh_exit(&ctx->fh);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002003 clear_bit(ctx->idx, &dev->instance_mask);
Javier Martin186b2502012-07-26 05:53:35 -03002004 kfree(ctx);
2005
2006 return 0;
2007}
2008
2009static unsigned int coda_poll(struct file *file,
2010 struct poll_table_struct *wait)
2011{
2012 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
2013 int ret;
2014
2015 coda_lock(ctx);
2016 ret = v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
2017 coda_unlock(ctx);
2018 return ret;
2019}
2020
2021static int coda_mmap(struct file *file, struct vm_area_struct *vma)
2022{
2023 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
2024
2025 return v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
2026}
2027
2028static const struct v4l2_file_operations coda_fops = {
2029 .owner = THIS_MODULE,
2030 .open = coda_open,
2031 .release = coda_release,
2032 .poll = coda_poll,
2033 .unlocked_ioctl = video_ioctl2,
2034 .mmap = coda_mmap,
2035};
2036
2037static irqreturn_t coda_irq_handler(int irq, void *data)
2038{
Philipp Zabelec25f682012-07-20 08:54:29 -03002039 struct vb2_buffer *src_buf, *dst_buf;
Javier Martin186b2502012-07-26 05:53:35 -03002040 struct coda_dev *dev = data;
2041 u32 wr_ptr, start_ptr;
2042 struct coda_ctx *ctx;
2043
Fabio Estevam2249b452012-10-09 23:33:29 -03002044 cancel_delayed_work(&dev->timeout);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03002045
Javier Martin186b2502012-07-26 05:53:35 -03002046 /* read status register to attend the IRQ */
2047 coda_read(dev, CODA_REG_BIT_INT_STATUS);
2048 coda_write(dev, CODA_REG_BIT_INT_CLEAR_SET,
2049 CODA_REG_BIT_INT_CLEAR);
2050
2051 ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev);
2052 if (ctx == NULL) {
2053 v4l2_err(&dev->v4l2_dev, "Instance released before the end of transaction\n");
Philipp Zabelfcb62822013-05-23 10:43:00 -03002054 mutex_unlock(&dev->coda_mutex);
Javier Martin186b2502012-07-26 05:53:35 -03002055 return IRQ_HANDLED;
2056 }
2057
2058 if (ctx->aborting) {
2059 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
2060 "task has been aborted\n");
Philipp Zabelfcb62822013-05-23 10:43:00 -03002061 mutex_unlock(&dev->coda_mutex);
Javier Martin186b2502012-07-26 05:53:35 -03002062 return IRQ_HANDLED;
2063 }
2064
2065 if (coda_isbusy(ctx->dev)) {
2066 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
2067 "coda is still busy!!!!\n");
2068 return IRQ_NONE;
2069 }
2070
Philipp Zabelec25f682012-07-20 08:54:29 -03002071 src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
2072 dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03002073
2074 /* Get results from the coda */
2075 coda_read(dev, CODA_RET_ENC_PIC_TYPE);
2076 start_ptr = coda_read(dev, CODA_CMD_ENC_PIC_BB_START);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002077 wr_ptr = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
2078
Javier Martin186b2502012-07-26 05:53:35 -03002079 /* Calculate bytesused field */
2080 if (dst_buf->v4l2_buf.sequence == 0) {
Philipp Zabel366108f2013-06-21 03:55:27 -03002081 vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr +
2082 ctx->vpu_header_size[0] +
2083 ctx->vpu_header_size[1] +
2084 ctx->vpu_header_size[2]);
Javier Martin186b2502012-07-26 05:53:35 -03002085 } else {
Philipp Zabel366108f2013-06-21 03:55:27 -03002086 vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr);
Javier Martin186b2502012-07-26 05:53:35 -03002087 }
2088
2089 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, "frame size = %u\n",
2090 wr_ptr - start_ptr);
2091
2092 coda_read(dev, CODA_RET_ENC_PIC_SLICE_NUM);
2093 coda_read(dev, CODA_RET_ENC_PIC_FLAG);
2094
2095 if (src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) {
2096 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
2097 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
2098 } else {
2099 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
2100 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
2101 }
2102
Kamil Debskiccd571c2013-04-24 10:38:24 -03002103 dst_buf->v4l2_buf.timestamp = src_buf->v4l2_buf.timestamp;
2104 dst_buf->v4l2_buf.timecode = src_buf->v4l2_buf.timecode;
2105
Philipp Zabelec25f682012-07-20 08:54:29 -03002106 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
Javier Martin186b2502012-07-26 05:53:35 -03002107 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
2108
2109 ctx->gopcounter--;
2110 if (ctx->gopcounter < 0)
2111 ctx->gopcounter = ctx->params.gop_size - 1;
2112
2113 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
2114 "job finished: encoding frame (%d) (%s)\n",
2115 dst_buf->v4l2_buf.sequence,
2116 (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ?
2117 "KEYFRAME" : "PFRAME");
2118
Philipp Zabelfcb62822013-05-23 10:43:00 -03002119 mutex_unlock(&dev->coda_mutex);
2120
Javier Martin186b2502012-07-26 05:53:35 -03002121 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->m2m_ctx);
2122
2123 return IRQ_HANDLED;
2124}
2125
Philipp Zabel2fb57f02012-07-25 09:22:07 -03002126static void coda_timeout(struct work_struct *work)
2127{
2128 struct coda_ctx *ctx;
2129 struct coda_dev *dev = container_of(to_delayed_work(work),
2130 struct coda_dev, timeout);
2131
Philipp Zabel39cc0292013-05-21 10:32:42 -03002132 dev_err(&dev->plat_dev->dev, "CODA PIC_RUN timeout, stopping all streams\n");
Philipp Zabel2fb57f02012-07-25 09:22:07 -03002133
2134 mutex_lock(&dev->dev_mutex);
2135 list_for_each_entry(ctx, &dev->instances, list) {
2136 v4l2_m2m_streamoff(NULL, ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
2137 v4l2_m2m_streamoff(NULL, ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2138 }
2139 mutex_unlock(&dev->dev_mutex);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002140
2141 mutex_unlock(&dev->coda_mutex);
2142 ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev);
2143 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->m2m_ctx);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03002144}
2145
Javier Martin186b2502012-07-26 05:53:35 -03002146static u32 coda_supported_firmwares[] = {
2147 CODA_FIRMWARE_VERNUM(CODA_DX6, 2, 2, 5),
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002148 CODA_FIRMWARE_VERNUM(CODA_7541, 1, 4, 50),
Javier Martin186b2502012-07-26 05:53:35 -03002149};
2150
2151static bool coda_firmware_supported(u32 vernum)
2152{
2153 int i;
2154
2155 for (i = 0; i < ARRAY_SIZE(coda_supported_firmwares); i++)
2156 if (vernum == coda_supported_firmwares[i])
2157 return true;
2158 return false;
2159}
2160
2161static char *coda_product_name(int product)
2162{
2163 static char buf[9];
2164
2165 switch (product) {
2166 case CODA_DX6:
2167 return "CodaDx6";
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03002168 case CODA_7541:
2169 return "CODA7541";
Javier Martin186b2502012-07-26 05:53:35 -03002170 default:
2171 snprintf(buf, sizeof(buf), "(0x%04x)", product);
2172 return buf;
2173 }
2174}
2175
Philipp Zabel87048bb2012-07-02 07:03:43 -03002176static int coda_hw_init(struct coda_dev *dev)
Javier Martin186b2502012-07-26 05:53:35 -03002177{
2178 u16 product, major, minor, release;
2179 u32 data;
2180 u16 *p;
2181 int i;
2182
2183 clk_prepare_enable(dev->clk_per);
2184 clk_prepare_enable(dev->clk_ahb);
2185
Javier Martin186b2502012-07-26 05:53:35 -03002186 /*
2187 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
Philipp Zabel87048bb2012-07-02 07:03:43 -03002188 * The 16-bit chars in the code buffer are in memory access
2189 * order, re-sort them to CODA order for register download.
Javier Martin186b2502012-07-26 05:53:35 -03002190 * Data in this SRAM survives a reboot.
2191 */
Philipp Zabel87048bb2012-07-02 07:03:43 -03002192 p = (u16 *)dev->codebuf.vaddr;
2193 if (dev->devtype->product == CODA_DX6) {
2194 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
2195 data = CODA_DOWN_ADDRESS_SET(i) |
2196 CODA_DOWN_DATA_SET(p[i ^ 1]);
2197 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
2198 }
2199 } else {
2200 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
2201 data = CODA_DOWN_ADDRESS_SET(i) |
2202 CODA_DOWN_DATA_SET(p[round_down(i, 4) +
2203 3 - (i % 4)]);
2204 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
2205 }
Javier Martin186b2502012-07-26 05:53:35 -03002206 }
Javier Martin186b2502012-07-26 05:53:35 -03002207
Philipp Zabel9acf7692013-05-23 10:42:56 -03002208 /* Clear registers */
2209 for (i = 0; i < 64; i++)
2210 coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
2211
Javier Martin186b2502012-07-26 05:53:35 -03002212 /* Tell the BIT where to find everything it needs */
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002213 if (dev->devtype->product == CODA_7541) {
2214 coda_write(dev, dev->tempbuf.paddr,
2215 CODA_REG_BIT_TEMP_BUF_ADDR);
2216 } else {
2217 coda_write(dev, dev->workbuf.paddr,
2218 CODA_REG_BIT_WORK_BUF_ADDR);
2219 }
Javier Martin186b2502012-07-26 05:53:35 -03002220 coda_write(dev, dev->codebuf.paddr,
2221 CODA_REG_BIT_CODE_BUF_ADDR);
2222 coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
2223
2224 /* Set default values */
2225 switch (dev->devtype->product) {
2226 case CODA_DX6:
2227 coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
2228 break;
2229 default:
2230 coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
2231 }
2232 coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
Philipp Zabel10436672012-07-02 09:03:55 -03002233
2234 if (dev->devtype->product != CODA_DX6)
2235 coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
2236
Javier Martin186b2502012-07-26 05:53:35 -03002237 coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
2238 CODA_REG_BIT_INT_ENABLE);
2239
2240 /* Reset VPU and start processor */
2241 data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
2242 data |= CODA_REG_RESET_ENABLE;
2243 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
2244 udelay(10);
2245 data &= ~CODA_REG_RESET_ENABLE;
2246 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
2247 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
2248
2249 /* Load firmware */
2250 coda_write(dev, 0, CODA_CMD_FIRMWARE_VERNUM);
2251 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
2252 coda_write(dev, 0, CODA_REG_BIT_RUN_INDEX);
2253 coda_write(dev, 0, CODA_REG_BIT_RUN_COD_STD);
2254 coda_write(dev, CODA_COMMAND_FIRMWARE_GET, CODA_REG_BIT_RUN_COMMAND);
2255 if (coda_wait_timeout(dev)) {
2256 clk_disable_unprepare(dev->clk_per);
2257 clk_disable_unprepare(dev->clk_ahb);
2258 v4l2_err(&dev->v4l2_dev, "firmware get command error\n");
2259 return -EIO;
2260 }
2261
2262 /* Check we are compatible with the loaded firmware */
2263 data = coda_read(dev, CODA_CMD_FIRMWARE_VERNUM);
2264 product = CODA_FIRMWARE_PRODUCT(data);
2265 major = CODA_FIRMWARE_MAJOR(data);
2266 minor = CODA_FIRMWARE_MINOR(data);
2267 release = CODA_FIRMWARE_RELEASE(data);
2268
2269 clk_disable_unprepare(dev->clk_per);
2270 clk_disable_unprepare(dev->clk_ahb);
2271
2272 if (product != dev->devtype->product) {
2273 v4l2_err(&dev->v4l2_dev, "Wrong firmware. Hw: %s, Fw: %s,"
2274 " Version: %u.%u.%u\n",
2275 coda_product_name(dev->devtype->product),
2276 coda_product_name(product), major, minor, release);
2277 return -EINVAL;
2278 }
2279
2280 v4l2_info(&dev->v4l2_dev, "Initialized %s.\n",
2281 coda_product_name(product));
2282
2283 if (coda_firmware_supported(data)) {
2284 v4l2_info(&dev->v4l2_dev, "Firmware version: %u.%u.%u\n",
2285 major, minor, release);
2286 } else {
2287 v4l2_warn(&dev->v4l2_dev, "Unsupported firmware version: "
2288 "%u.%u.%u\n", major, minor, release);
2289 }
2290
2291 return 0;
2292}
2293
2294static void coda_fw_callback(const struct firmware *fw, void *context)
2295{
2296 struct coda_dev *dev = context;
2297 struct platform_device *pdev = dev->plat_dev;
2298 int ret;
2299
2300 if (!fw) {
2301 v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
2302 return;
2303 }
2304
2305 /* allocate auxiliary per-device code buffer for the BIT processor */
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002306 ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size);
2307 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002308 dev_err(&pdev->dev, "failed to allocate code buffer\n");
2309 return;
2310 }
2311
Philipp Zabel87048bb2012-07-02 07:03:43 -03002312 /* Copy the whole firmware image to the code buffer */
2313 memcpy(dev->codebuf.vaddr, fw->data, fw->size);
2314 release_firmware(fw);
2315
2316 ret = coda_hw_init(dev);
Javier Martin186b2502012-07-26 05:53:35 -03002317 if (ret) {
2318 v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
2319 return;
2320 }
2321
2322 dev->vfd.fops = &coda_fops,
2323 dev->vfd.ioctl_ops = &coda_ioctl_ops;
2324 dev->vfd.release = video_device_release_empty,
2325 dev->vfd.lock = &dev->dev_mutex;
2326 dev->vfd.v4l2_dev = &dev->v4l2_dev;
Hans Verkuil954f3402012-09-05 06:05:50 -03002327 dev->vfd.vfl_dir = VFL_DIR_M2M;
Javier Martin186b2502012-07-26 05:53:35 -03002328 snprintf(dev->vfd.name, sizeof(dev->vfd.name), "%s", CODA_NAME);
2329 video_set_drvdata(&dev->vfd, dev);
2330
2331 dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
2332 if (IS_ERR(dev->alloc_ctx)) {
2333 v4l2_err(&dev->v4l2_dev, "Failed to alloc vb2 context\n");
2334 return;
2335 }
2336
2337 dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
2338 if (IS_ERR(dev->m2m_dev)) {
2339 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
2340 goto rel_ctx;
2341 }
2342
2343 ret = video_register_device(&dev->vfd, VFL_TYPE_GRABBER, 0);
2344 if (ret) {
2345 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
2346 goto rel_m2m;
2347 }
2348 v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video%d\n",
2349 dev->vfd.num);
2350
2351 return;
2352
2353rel_m2m:
2354 v4l2_m2m_release(dev->m2m_dev);
2355rel_ctx:
2356 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
2357}
2358
2359static int coda_firmware_request(struct coda_dev *dev)
2360{
2361 char *fw = dev->devtype->firmware;
2362
2363 dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw,
2364 coda_product_name(dev->devtype->product));
2365
2366 return request_firmware_nowait(THIS_MODULE, true,
2367 fw, &dev->plat_dev->dev, GFP_KERNEL, dev, coda_fw_callback);
2368}
2369
2370enum coda_platform {
2371 CODA_IMX27,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03002372 CODA_IMX53,
Javier Martin186b2502012-07-26 05:53:35 -03002373};
2374
Emil Goodec06d8752012-08-14 17:44:42 -03002375static const struct coda_devtype coda_devdata[] = {
Javier Martin186b2502012-07-26 05:53:35 -03002376 [CODA_IMX27] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -03002377 .firmware = "v4l-codadx6-imx27.bin",
2378 .product = CODA_DX6,
2379 .codecs = codadx6_codecs,
2380 .num_codecs = ARRAY_SIZE(codadx6_codecs),
Javier Martin186b2502012-07-26 05:53:35 -03002381 },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03002382 [CODA_IMX53] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -03002383 .firmware = "v4l-coda7541-imx53.bin",
2384 .product = CODA_7541,
2385 .codecs = coda7_codecs,
2386 .num_codecs = ARRAY_SIZE(coda7_codecs),
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03002387 },
Javier Martin186b2502012-07-26 05:53:35 -03002388};
2389
2390static struct platform_device_id coda_platform_ids[] = {
2391 { .name = "coda-imx27", .driver_data = CODA_IMX27 },
Fabio Estevam4e44cd02012-10-10 00:07:38 -03002392 { .name = "coda-imx53", .driver_data = CODA_IMX53 },
Javier Martin186b2502012-07-26 05:53:35 -03002393 { /* sentinel */ }
2394};
2395MODULE_DEVICE_TABLE(platform, coda_platform_ids);
2396
2397#ifdef CONFIG_OF
2398static const struct of_device_id coda_dt_ids[] = {
2399 { .compatible = "fsl,imx27-vpu", .data = &coda_platform_ids[CODA_IMX27] },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03002400 { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
Javier Martin186b2502012-07-26 05:53:35 -03002401 { /* sentinel */ }
2402};
2403MODULE_DEVICE_TABLE(of, coda_dt_ids);
2404#endif
2405
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08002406static int coda_probe(struct platform_device *pdev)
Javier Martin186b2502012-07-26 05:53:35 -03002407{
2408 const struct of_device_id *of_id =
2409 of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
2410 const struct platform_device_id *pdev_id;
Philipp Zabel657eee72013-04-29 16:17:14 -07002411 struct coda_platform_data *pdata = pdev->dev.platform_data;
2412 struct device_node *np = pdev->dev.of_node;
2413 struct gen_pool *pool;
Javier Martin186b2502012-07-26 05:53:35 -03002414 struct coda_dev *dev;
2415 struct resource *res;
2416 int ret, irq;
2417
2418 dev = devm_kzalloc(&pdev->dev, sizeof *dev, GFP_KERNEL);
2419 if (!dev) {
2420 dev_err(&pdev->dev, "Not enough memory for %s\n",
2421 CODA_NAME);
2422 return -ENOMEM;
2423 }
2424
2425 spin_lock_init(&dev->irqlock);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002426 INIT_LIST_HEAD(&dev->instances);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03002427 INIT_DELAYED_WORK(&dev->timeout, coda_timeout);
Javier Martin186b2502012-07-26 05:53:35 -03002428
2429 dev->plat_dev = pdev;
2430 dev->clk_per = devm_clk_get(&pdev->dev, "per");
2431 if (IS_ERR(dev->clk_per)) {
2432 dev_err(&pdev->dev, "Could not get per clock\n");
2433 return PTR_ERR(dev->clk_per);
2434 }
2435
2436 dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
2437 if (IS_ERR(dev->clk_ahb)) {
2438 dev_err(&pdev->dev, "Could not get ahb clock\n");
2439 return PTR_ERR(dev->clk_ahb);
2440 }
2441
2442 /* Get memory for physical registers */
2443 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2444 if (res == NULL) {
2445 dev_err(&pdev->dev, "failed to get memory region resource\n");
2446 return -ENOENT;
2447 }
2448
Philipp Zabel611cbbf2013-05-21 04:19:27 -03002449 dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
2450 if (IS_ERR(dev->regs_base))
2451 return PTR_ERR(dev->regs_base);
Javier Martin186b2502012-07-26 05:53:35 -03002452
2453 /* IRQ */
2454 irq = platform_get_irq(pdev, 0);
2455 if (irq < 0) {
2456 dev_err(&pdev->dev, "failed to get irq resource\n");
2457 return -ENOENT;
2458 }
2459
2460 if (devm_request_irq(&pdev->dev, irq, coda_irq_handler,
2461 0, CODA_NAME, dev) < 0) {
2462 dev_err(&pdev->dev, "failed to request irq\n");
2463 return -ENOENT;
2464 }
2465
Philipp Zabel657eee72013-04-29 16:17:14 -07002466 /* Get IRAM pool from device tree or platform data */
2467 pool = of_get_named_gen_pool(np, "iram", 0);
2468 if (!pool && pdata)
2469 pool = dev_get_gen_pool(pdata->iram_dev);
2470 if (!pool) {
2471 dev_err(&pdev->dev, "iram pool not available\n");
2472 return -ENOMEM;
2473 }
2474 dev->iram_pool = pool;
2475
Javier Martin186b2502012-07-26 05:53:35 -03002476 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
2477 if (ret)
2478 return ret;
2479
2480 mutex_init(&dev->dev_mutex);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002481 mutex_init(&dev->coda_mutex);
Javier Martin186b2502012-07-26 05:53:35 -03002482
2483 pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
2484
2485 if (of_id) {
2486 dev->devtype = of_id->data;
2487 } else if (pdev_id) {
2488 dev->devtype = &coda_devdata[pdev_id->driver_data];
2489 } else {
2490 v4l2_device_unregister(&dev->v4l2_dev);
2491 return -EINVAL;
2492 }
2493
2494 /* allocate auxiliary per-device buffers for the BIT processor */
2495 switch (dev->devtype->product) {
2496 case CODA_DX6:
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002497 ret = coda_alloc_aux_buf(dev, &dev->workbuf,
2498 CODADX6_WORK_BUF_SIZE);
2499 if (ret < 0) {
2500 dev_err(&pdev->dev, "failed to allocate work buffer\n");
2501 v4l2_device_unregister(&dev->v4l2_dev);
2502 return ret;
2503 }
Javier Martin186b2502012-07-26 05:53:35 -03002504 break;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002505 case CODA_7541:
2506 dev->tempbuf.size = CODA7_TEMP_BUF_SIZE;
2507 break;
Javier Martin186b2502012-07-26 05:53:35 -03002508 }
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002509 if (dev->tempbuf.size) {
2510 ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
2511 dev->tempbuf.size);
2512 if (ret < 0) {
2513 dev_err(&pdev->dev, "failed to allocate temp buffer\n");
2514 v4l2_device_unregister(&dev->v4l2_dev);
2515 return ret;
2516 }
Javier Martin186b2502012-07-26 05:53:35 -03002517 }
2518
Philipp Zabel657eee72013-04-29 16:17:14 -07002519 if (dev->devtype->product == CODA_DX6)
2520 dev->iram_size = CODADX6_IRAM_SIZE;
2521 else
2522 dev->iram_size = CODA7_IRAM_SIZE;
2523 dev->iram_vaddr = gen_pool_alloc(dev->iram_pool, dev->iram_size);
2524 if (!dev->iram_vaddr) {
2525 dev_err(&pdev->dev, "unable to alloc iram\n");
2526 return -ENOMEM;
Philipp Zabel10436672012-07-02 09:03:55 -03002527 }
Philipp Zabel657eee72013-04-29 16:17:14 -07002528 dev->iram_paddr = gen_pool_virt_to_phys(dev->iram_pool,
2529 dev->iram_vaddr);
Philipp Zabel10436672012-07-02 09:03:55 -03002530
Javier Martin186b2502012-07-26 05:53:35 -03002531 platform_set_drvdata(pdev, dev);
2532
2533 return coda_firmware_request(dev);
2534}
2535
2536static int coda_remove(struct platform_device *pdev)
2537{
2538 struct coda_dev *dev = platform_get_drvdata(pdev);
2539
2540 video_unregister_device(&dev->vfd);
2541 if (dev->m2m_dev)
2542 v4l2_m2m_release(dev->m2m_dev);
2543 if (dev->alloc_ctx)
2544 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
2545 v4l2_device_unregister(&dev->v4l2_dev);
Philipp Zabel657eee72013-04-29 16:17:14 -07002546 if (dev->iram_vaddr)
2547 gen_pool_free(dev->iram_pool, dev->iram_vaddr, dev->iram_size);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002548 coda_free_aux_buf(dev, &dev->codebuf);
2549 coda_free_aux_buf(dev, &dev->tempbuf);
2550 coda_free_aux_buf(dev, &dev->workbuf);
Javier Martin186b2502012-07-26 05:53:35 -03002551 return 0;
2552}
2553
2554static struct platform_driver coda_driver = {
2555 .probe = coda_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08002556 .remove = coda_remove,
Javier Martin186b2502012-07-26 05:53:35 -03002557 .driver = {
2558 .name = CODA_NAME,
2559 .owner = THIS_MODULE,
2560 .of_match_table = of_match_ptr(coda_dt_ids),
2561 },
2562 .id_table = coda_platform_ids,
2563};
2564
2565module_platform_driver(coda_driver);
2566
2567MODULE_LICENSE("GPL");
2568MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
2569MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");