blob: 2c37e3d182597f4568387725081c1d5dce0287f2 [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 */
Philipp Zabel477c1cf2013-06-21 03:55:33 -0300805static void coda_prepare_encode(struct coda_ctx *ctx)
Javier Martin186b2502012-07-26 05:53:35 -0300806{
Javier Martin186b2502012-07-26 05:53:35 -0300807 struct coda_q_data *q_data_src, *q_data_dst;
808 struct vb2_buffer *src_buf, *dst_buf;
809 struct coda_dev *dev = ctx->dev;
810 int force_ipicture;
811 int quant_param = 0;
812 u32 picture_y, picture_cb, picture_cr;
813 u32 pic_stream_buffer_addr, pic_stream_buffer_size;
814 u32 dst_fourcc;
815
816 src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
817 dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
818 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
819 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300820 dst_fourcc = q_data_dst->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -0300821
822 src_buf->v4l2_buf.sequence = ctx->isequence;
823 dst_buf->v4l2_buf.sequence = ctx->isequence;
824 ctx->isequence++;
825
826 /*
827 * Workaround coda firmware BUG that only marks the first
828 * frame as IDR. This is a problem for some decoders that can't
829 * recover when a frame is lost.
830 */
831 if (src_buf->v4l2_buf.sequence % ctx->params.gop_size) {
832 src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
833 src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
834 } else {
835 src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
836 src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
837 }
838
839 /*
840 * Copy headers at the beginning of the first frame for H.264 only.
841 * In MPEG4 they are already copied by the coda.
842 */
843 if (src_buf->v4l2_buf.sequence == 0) {
844 pic_stream_buffer_addr =
845 vb2_dma_contig_plane_dma_addr(dst_buf, 0) +
846 ctx->vpu_header_size[0] +
847 ctx->vpu_header_size[1] +
848 ctx->vpu_header_size[2];
849 pic_stream_buffer_size = CODA_MAX_FRAME_SIZE -
850 ctx->vpu_header_size[0] -
851 ctx->vpu_header_size[1] -
852 ctx->vpu_header_size[2];
853 memcpy(vb2_plane_vaddr(dst_buf, 0),
854 &ctx->vpu_header[0][0], ctx->vpu_header_size[0]);
855 memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0],
856 &ctx->vpu_header[1][0], ctx->vpu_header_size[1]);
857 memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0] +
858 ctx->vpu_header_size[1], &ctx->vpu_header[2][0],
859 ctx->vpu_header_size[2]);
860 } else {
861 pic_stream_buffer_addr =
862 vb2_dma_contig_plane_dma_addr(dst_buf, 0);
863 pic_stream_buffer_size = CODA_MAX_FRAME_SIZE;
864 }
865
866 if (src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) {
867 force_ipicture = 1;
868 switch (dst_fourcc) {
869 case V4L2_PIX_FMT_H264:
870 quant_param = ctx->params.h264_intra_qp;
871 break;
872 case V4L2_PIX_FMT_MPEG4:
873 quant_param = ctx->params.mpeg4_intra_qp;
874 break;
875 default:
876 v4l2_warn(&ctx->dev->v4l2_dev,
877 "cannot set intra qp, fmt not supported\n");
878 break;
879 }
880 } else {
881 force_ipicture = 0;
882 switch (dst_fourcc) {
883 case V4L2_PIX_FMT_H264:
884 quant_param = ctx->params.h264_inter_qp;
885 break;
886 case V4L2_PIX_FMT_MPEG4:
887 quant_param = ctx->params.mpeg4_inter_qp;
888 break;
889 default:
890 v4l2_warn(&ctx->dev->v4l2_dev,
891 "cannot set inter qp, fmt not supported\n");
892 break;
893 }
894 }
895
896 /* submit */
Philipp Zabel8f35c7b2012-07-09 04:25:52 -0300897 coda_write(dev, CODA_ROT_MIR_ENABLE | ctx->params.rot_mode, CODA_CMD_ENC_PIC_ROT_MODE);
Javier Martin186b2502012-07-26 05:53:35 -0300898 coda_write(dev, quant_param, CODA_CMD_ENC_PIC_QS);
899
900
901 picture_y = vb2_dma_contig_plane_dma_addr(src_buf, 0);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300902 switch (q_data_src->fourcc) {
903 case V4L2_PIX_FMT_YVU420:
904 /* Switch Cb and Cr for YVU420 format */
905 picture_cr = picture_y + q_data_src->width * q_data_src->height;
906 picture_cb = picture_cr + q_data_src->width / 2 *
907 q_data_src->height / 2;
908 break;
909 case V4L2_PIX_FMT_YUV420:
910 default:
911 picture_cb = picture_y + q_data_src->width * q_data_src->height;
912 picture_cr = picture_cb + q_data_src->width / 2 *
913 q_data_src->height / 2;
914 break;
915 }
Javier Martin186b2502012-07-26 05:53:35 -0300916
917 coda_write(dev, picture_y, CODA_CMD_ENC_PIC_SRC_ADDR_Y);
918 coda_write(dev, picture_cb, CODA_CMD_ENC_PIC_SRC_ADDR_CB);
919 coda_write(dev, picture_cr, CODA_CMD_ENC_PIC_SRC_ADDR_CR);
920 coda_write(dev, force_ipicture << 1 & 0x2,
921 CODA_CMD_ENC_PIC_OPTION);
922
923 coda_write(dev, pic_stream_buffer_addr, CODA_CMD_ENC_PIC_BB_START);
924 coda_write(dev, pic_stream_buffer_size / 1024,
925 CODA_CMD_ENC_PIC_BB_SIZE);
Philipp Zabel477c1cf2013-06-21 03:55:33 -0300926}
927
928static void coda_device_run(void *m2m_priv)
929{
930 struct coda_ctx *ctx = m2m_priv;
931 struct coda_dev *dev = ctx->dev;
932
933 mutex_lock(&dev->coda_mutex);
934
935 coda_prepare_encode(ctx);
Philipp Zabel10436672012-07-02 09:03:55 -0300936
937 if (dev->devtype->product == CODA_7541) {
938 coda_write(dev, CODA7_USE_BIT_ENABLE | CODA7_USE_HOST_BIT_ENABLE |
939 CODA7_USE_ME_ENABLE | CODA7_USE_HOST_ME_ENABLE,
940 CODA7_REG_BIT_AXI_SRAM_USE);
941 }
942
Philipp Zabelc2d22512013-06-21 03:55:28 -0300943 if (dev->devtype->product != CODA_DX6)
944 coda_write(dev, ctx->iram_info.axi_sram_use,
945 CODA7_REG_BIT_AXI_SRAM_USE);
946
Philipp Zabel2fb57f02012-07-25 09:22:07 -0300947 /* 1 second timeout in case CODA locks up */
948 schedule_delayed_work(&dev->timeout, HZ);
949
Javier Martin186b2502012-07-26 05:53:35 -0300950 coda_command_async(ctx, CODA_COMMAND_PIC_RUN);
951}
952
953static int coda_job_ready(void *m2m_priv)
954{
955 struct coda_ctx *ctx = m2m_priv;
956
957 /*
958 * For both 'P' and 'key' frame cases 1 picture
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300959 * and 1 frame are needed. In the decoder case,
960 * the compressed frame can be in the bitstream.
Javier Martin186b2502012-07-26 05:53:35 -0300961 */
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300962 if (!v4l2_m2m_num_src_bufs_ready(ctx->m2m_ctx) &&
963 ctx->inst_type != CODA_INST_DECODER) {
Javier Martin186b2502012-07-26 05:53:35 -0300964 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
965 "not ready: not enough video buffers.\n");
966 return 0;
967 }
968
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300969 if (!v4l2_m2m_num_dst_bufs_ready(ctx->m2m_ctx)) {
970 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
971 "not ready: not enough video capture buffers.\n");
972 return 0;
973 }
974
Philipp Zabel3e748262013-05-23 10:43:01 -0300975 if (ctx->aborting) {
976 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
977 "not ready: aborting\n");
978 return 0;
979 }
980
Javier Martin186b2502012-07-26 05:53:35 -0300981 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
982 "job ready\n");
983 return 1;
984}
985
986static void coda_job_abort(void *priv)
987{
988 struct coda_ctx *ctx = priv;
Javier Martin186b2502012-07-26 05:53:35 -0300989
990 ctx->aborting = 1;
991
992 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
993 "Aborting task\n");
Javier Martin186b2502012-07-26 05:53:35 -0300994}
995
996static void coda_lock(void *m2m_priv)
997{
998 struct coda_ctx *ctx = m2m_priv;
999 struct coda_dev *pcdev = ctx->dev;
1000 mutex_lock(&pcdev->dev_mutex);
1001}
1002
1003static void coda_unlock(void *m2m_priv)
1004{
1005 struct coda_ctx *ctx = m2m_priv;
1006 struct coda_dev *pcdev = ctx->dev;
1007 mutex_unlock(&pcdev->dev_mutex);
1008}
1009
1010static struct v4l2_m2m_ops coda_m2m_ops = {
1011 .device_run = coda_device_run,
1012 .job_ready = coda_job_ready,
1013 .job_abort = coda_job_abort,
1014 .lock = coda_lock,
1015 .unlock = coda_unlock,
1016};
1017
1018static void set_default_params(struct coda_ctx *ctx)
1019{
Philipp Zabelb96904e2013-05-23 10:42:58 -03001020 int max_w;
1021 int max_h;
1022
1023 ctx->codec = &ctx->dev->devtype->codecs[0];
1024 max_w = ctx->codec->max_w;
1025 max_h = ctx->codec->max_h;
Javier Martin186b2502012-07-26 05:53:35 -03001026
1027 ctx->params.codec_mode = CODA_MODE_INVALID;
1028 ctx->colorspace = V4L2_COLORSPACE_REC709;
1029 ctx->params.framerate = 30;
Javier Martin186b2502012-07-26 05:53:35 -03001030 ctx->aborting = 0;
1031
1032 /* Default formats for output and input queues */
Philipp Zabelb96904e2013-05-23 10:42:58 -03001033 ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->codec->src_fourcc;
1034 ctx->q_data[V4L2_M2M_DST].fourcc = ctx->codec->dst_fourcc;
1035 ctx->q_data[V4L2_M2M_SRC].width = max_w;
1036 ctx->q_data[V4L2_M2M_SRC].height = max_h;
1037 ctx->q_data[V4L2_M2M_SRC].sizeimage = (max_w * max_h * 3) / 2;
1038 ctx->q_data[V4L2_M2M_DST].width = max_w;
1039 ctx->q_data[V4L2_M2M_DST].height = max_h;
Javier Martin186b2502012-07-26 05:53:35 -03001040 ctx->q_data[V4L2_M2M_DST].sizeimage = CODA_MAX_FRAME_SIZE;
1041}
1042
1043/*
1044 * Queue operations
1045 */
1046static int coda_queue_setup(struct vb2_queue *vq,
1047 const struct v4l2_format *fmt,
1048 unsigned int *nbuffers, unsigned int *nplanes,
1049 unsigned int sizes[], void *alloc_ctxs[])
1050{
1051 struct coda_ctx *ctx = vb2_get_drv_priv(vq);
Philipp Zabele34db062012-08-29 08:22:00 -03001052 struct coda_q_data *q_data;
Javier Martin186b2502012-07-26 05:53:35 -03001053 unsigned int size;
1054
Philipp Zabele34db062012-08-29 08:22:00 -03001055 q_data = get_q_data(ctx, vq->type);
1056 size = q_data->sizeimage;
Javier Martin186b2502012-07-26 05:53:35 -03001057
1058 *nplanes = 1;
1059 sizes[0] = size;
1060
1061 alloc_ctxs[0] = ctx->dev->alloc_ctx;
1062
1063 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1064 "get %d buffer(s) of size %d each.\n", *nbuffers, size);
1065
1066 return 0;
1067}
1068
1069static int coda_buf_prepare(struct vb2_buffer *vb)
1070{
1071 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1072 struct coda_q_data *q_data;
1073
1074 q_data = get_q_data(ctx, vb->vb2_queue->type);
1075
1076 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
1077 v4l2_warn(&ctx->dev->v4l2_dev,
1078 "%s data will not fit into plane (%lu < %lu)\n",
1079 __func__, vb2_plane_size(vb, 0),
1080 (long)q_data->sizeimage);
1081 return -EINVAL;
1082 }
1083
Javier Martin186b2502012-07-26 05:53:35 -03001084 return 0;
1085}
1086
1087static void coda_buf_queue(struct vb2_buffer *vb)
1088{
1089 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1090 v4l2_m2m_buf_queue(ctx->m2m_ctx, vb);
1091}
1092
1093static void coda_wait_prepare(struct vb2_queue *q)
1094{
1095 struct coda_ctx *ctx = vb2_get_drv_priv(q);
1096 coda_unlock(ctx);
1097}
1098
1099static void coda_wait_finish(struct vb2_queue *q)
1100{
1101 struct coda_ctx *ctx = vb2_get_drv_priv(q);
1102 coda_lock(ctx);
1103}
1104
Philipp Zabel86eda902013-05-23 10:42:57 -03001105static void coda_parabuf_write(struct coda_ctx *ctx, int index, u32 value)
1106{
1107 struct coda_dev *dev = ctx->dev;
1108 u32 *p = ctx->parabuf.vaddr;
1109
1110 if (dev->devtype->product == CODA_DX6)
1111 p[index] = value;
1112 else
1113 p[index ^ 1] = value;
1114}
1115
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001116static int coda_alloc_aux_buf(struct coda_dev *dev,
1117 struct coda_aux_buf *buf, size_t size)
1118{
1119 buf->vaddr = dma_alloc_coherent(&dev->plat_dev->dev, size, &buf->paddr,
1120 GFP_KERNEL);
1121 if (!buf->vaddr)
1122 return -ENOMEM;
1123
1124 buf->size = size;
1125
1126 return 0;
1127}
1128
1129static inline int coda_alloc_context_buf(struct coda_ctx *ctx,
1130 struct coda_aux_buf *buf, size_t size)
1131{
1132 return coda_alloc_aux_buf(ctx->dev, buf, size);
1133}
1134
1135static void coda_free_aux_buf(struct coda_dev *dev,
1136 struct coda_aux_buf *buf)
1137{
1138 if (buf->vaddr) {
1139 dma_free_coherent(&dev->plat_dev->dev, buf->size,
1140 buf->vaddr, buf->paddr);
1141 buf->vaddr = NULL;
1142 buf->size = 0;
1143 }
1144}
1145
1146static void coda_free_framebuffers(struct coda_ctx *ctx)
1147{
1148 int i;
1149
1150 for (i = 0; i < CODA_MAX_FRAMEBUFFERS; i++)
1151 coda_free_aux_buf(ctx->dev, &ctx->internal_frames[i]);
1152}
1153
Philipp Zabelec25f682012-07-20 08:54:29 -03001154static int coda_alloc_framebuffers(struct coda_ctx *ctx, struct coda_q_data *q_data, u32 fourcc)
1155{
1156 struct coda_dev *dev = ctx->dev;
Philipp Zabelec25f682012-07-20 08:54:29 -03001157 int height = q_data->height;
Philipp Zabel86eda902013-05-23 10:42:57 -03001158 dma_addr_t paddr;
1159 int ysize;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001160 int ret;
Philipp Zabelec25f682012-07-20 08:54:29 -03001161 int i;
1162
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001163 if (ctx->codec && ctx->codec->src_fourcc == V4L2_PIX_FMT_H264)
1164 height = round_up(height, 16);
Philipp Zabel86eda902013-05-23 10:42:57 -03001165 ysize = round_up(q_data->width, 8) * height;
1166
Philipp Zabelec25f682012-07-20 08:54:29 -03001167 /* Allocate frame buffers */
Philipp Zabelec25f682012-07-20 08:54:29 -03001168 for (i = 0; i < ctx->num_internal_frames; i++) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001169 size_t size;
1170
1171 size = q_data->sizeimage;
1172 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 &&
1173 dev->devtype->product != CODA_DX6)
Philipp Zabel86eda902013-05-23 10:42:57 -03001174 ctx->internal_frames[i].size += ysize/4;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001175 ret = coda_alloc_context_buf(ctx, &ctx->internal_frames[i], size);
1176 if (ret < 0) {
Philipp Zabelec25f682012-07-20 08:54:29 -03001177 coda_free_framebuffers(ctx);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001178 return ret;
Philipp Zabelec25f682012-07-20 08:54:29 -03001179 }
1180 }
1181
1182 /* Register frame buffers in the parameter buffer */
Philipp Zabel86eda902013-05-23 10:42:57 -03001183 for (i = 0; i < ctx->num_internal_frames; i++) {
1184 paddr = ctx->internal_frames[i].paddr;
1185 coda_parabuf_write(ctx, i * 3 + 0, paddr); /* Y */
1186 coda_parabuf_write(ctx, i * 3 + 1, paddr + ysize); /* Cb */
1187 coda_parabuf_write(ctx, i * 3 + 2, paddr + ysize + ysize/4); /* Cr */
Philipp Zabelec25f682012-07-20 08:54:29 -03001188
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001189 /* mvcol buffer for h.264 */
1190 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 &&
1191 dev->devtype->product != CODA_DX6)
1192 coda_parabuf_write(ctx, 96 + i,
1193 ctx->internal_frames[i].paddr +
1194 ysize + ysize/4 + ysize/4);
Philipp Zabelec25f682012-07-20 08:54:29 -03001195 }
1196
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001197 /* mvcol buffer for mpeg4 */
1198 if ((dev->devtype->product != CODA_DX6) &&
1199 (ctx->codec->src_fourcc == V4L2_PIX_FMT_MPEG4))
1200 coda_parabuf_write(ctx, 97, ctx->internal_frames[i].paddr +
1201 ysize + ysize/4 + ysize/4);
1202
Philipp Zabelec25f682012-07-20 08:54:29 -03001203 return 0;
1204}
1205
Javier Martin3f3f5c72012-10-29 05:20:29 -03001206static int coda_h264_padding(int size, char *p)
1207{
Javier Martin3f3f5c72012-10-29 05:20:29 -03001208 int nal_size;
1209 int diff;
1210
Javier Martin832fbb52012-10-29 08:34:59 -03001211 diff = size - (size & ~0x7);
Javier Martin3f3f5c72012-10-29 05:20:29 -03001212 if (diff == 0)
1213 return 0;
1214
Javier Martin832fbb52012-10-29 08:34:59 -03001215 nal_size = coda_filler_size[diff];
Javier Martin3f3f5c72012-10-29 05:20:29 -03001216 memcpy(p, coda_filler_nal, nal_size);
1217
1218 /* Add rbsp stop bit and trailing at the end */
1219 *(p + nal_size - 1) = 0x80;
1220
1221 return nal_size;
1222}
1223
Philipp Zabelc2d22512013-06-21 03:55:28 -03001224static void coda_setup_iram(struct coda_ctx *ctx)
1225{
1226 struct coda_iram_info *iram_info = &ctx->iram_info;
1227 struct coda_dev *dev = ctx->dev;
1228 int ipacdc_size;
1229 int bitram_size;
1230 int dbk_size;
Philipp Zabel8358e762013-06-21 03:55:32 -03001231 int ovl_size;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001232 int mb_width;
1233 int me_size;
1234 int size;
1235
1236 memset(iram_info, 0, sizeof(*iram_info));
1237 size = dev->iram_size;
1238
1239 if (dev->devtype->product == CODA_DX6)
1240 return;
1241
1242 if (ctx->inst_type == CODA_INST_ENCODER) {
1243 struct coda_q_data *q_data_src;
1244
1245 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1246 mb_width = DIV_ROUND_UP(q_data_src->width, 16);
1247
1248 /* Prioritize in case IRAM is too small for everything */
1249 me_size = round_up(round_up(q_data_src->width, 16) * 36 + 2048,
1250 1024);
1251 iram_info->search_ram_size = me_size;
1252 if (size >= iram_info->search_ram_size) {
1253 if (dev->devtype->product == CODA_7541)
1254 iram_info->axi_sram_use |= CODA7_USE_HOST_ME_ENABLE;
1255 iram_info->search_ram_paddr = dev->iram_paddr;
1256 size -= iram_info->search_ram_size;
1257 } else {
1258 pr_err("IRAM is smaller than the search ram size\n");
1259 goto out;
1260 }
1261
1262 /* Only H.264BP and H.263P3 are considered */
1263 dbk_size = round_up(128 * mb_width, 1024);
1264 if (size >= dbk_size) {
1265 iram_info->axi_sram_use |= CODA7_USE_HOST_DBK_ENABLE;
1266 iram_info->buf_dbk_y_use = dev->iram_paddr +
1267 iram_info->search_ram_size;
1268 iram_info->buf_dbk_c_use = iram_info->buf_dbk_y_use +
1269 dbk_size / 2;
1270 size -= dbk_size;
1271 } else {
1272 goto out;
1273 }
1274
1275 bitram_size = round_up(128 * mb_width, 1024);
1276 if (size >= bitram_size) {
1277 iram_info->axi_sram_use |= CODA7_USE_HOST_BIT_ENABLE;
1278 iram_info->buf_bit_use = iram_info->buf_dbk_c_use +
1279 dbk_size / 2;
1280 size -= bitram_size;
1281 } else {
1282 goto out;
1283 }
1284
1285 ipacdc_size = round_up(128 * mb_width, 1024);
1286 if (size >= ipacdc_size) {
1287 iram_info->axi_sram_use |= CODA7_USE_HOST_IP_ENABLE;
1288 iram_info->buf_ip_ac_dc_use = iram_info->buf_bit_use +
1289 bitram_size;
1290 size -= ipacdc_size;
1291 }
1292
Philipp Zabel8358e762013-06-21 03:55:32 -03001293 /* OVL and BTP disabled for encoder */
1294 } else if (ctx->inst_type == CODA_INST_DECODER) {
1295 struct coda_q_data *q_data_dst;
1296 int mb_height;
1297
1298 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1299 mb_width = DIV_ROUND_UP(q_data_dst->width, 16);
1300 mb_height = DIV_ROUND_UP(q_data_dst->height, 16);
1301
1302 dbk_size = round_up(256 * mb_width, 1024);
1303 if (size >= dbk_size) {
1304 iram_info->axi_sram_use |= CODA7_USE_HOST_DBK_ENABLE;
1305 iram_info->buf_dbk_y_use = dev->iram_paddr;
1306 iram_info->buf_dbk_c_use = dev->iram_paddr +
1307 dbk_size / 2;
1308 size -= dbk_size;
1309 } else {
1310 goto out;
1311 }
1312
1313 bitram_size = round_up(128 * mb_width, 1024);
1314 if (size >= bitram_size) {
1315 iram_info->axi_sram_use |= CODA7_USE_HOST_BIT_ENABLE;
1316 iram_info->buf_bit_use = iram_info->buf_dbk_c_use +
1317 dbk_size / 2;
1318 size -= bitram_size;
1319 } else {
1320 goto out;
1321 }
1322
1323 ipacdc_size = round_up(128 * mb_width, 1024);
1324 if (size >= ipacdc_size) {
1325 iram_info->axi_sram_use |= CODA7_USE_HOST_IP_ENABLE;
1326 iram_info->buf_ip_ac_dc_use = iram_info->buf_bit_use +
1327 bitram_size;
1328 size -= ipacdc_size;
1329 } else {
1330 goto out;
1331 }
1332
1333 ovl_size = round_up(80 * mb_width, 1024);
Philipp Zabelc2d22512013-06-21 03:55:28 -03001334 }
1335
1336out:
1337 switch (dev->devtype->product) {
1338 case CODA_DX6:
1339 break;
1340 case CODA_7541:
1341 /* i.MX53 uses secondary AXI for IRAM access */
1342 if (iram_info->axi_sram_use & CODA7_USE_HOST_BIT_ENABLE)
1343 iram_info->axi_sram_use |= CODA7_USE_BIT_ENABLE;
1344 if (iram_info->axi_sram_use & CODA7_USE_HOST_IP_ENABLE)
1345 iram_info->axi_sram_use |= CODA7_USE_IP_ENABLE;
1346 if (iram_info->axi_sram_use & CODA7_USE_HOST_DBK_ENABLE)
1347 iram_info->axi_sram_use |= CODA7_USE_DBK_ENABLE;
1348 if (iram_info->axi_sram_use & CODA7_USE_HOST_OVL_ENABLE)
1349 iram_info->axi_sram_use |= CODA7_USE_OVL_ENABLE;
1350 if (iram_info->axi_sram_use & CODA7_USE_HOST_ME_ENABLE)
1351 iram_info->axi_sram_use |= CODA7_USE_ME_ENABLE;
1352 }
1353
1354 if (!(iram_info->axi_sram_use & CODA7_USE_HOST_IP_ENABLE))
1355 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1356 "IRAM smaller than needed\n");
1357
1358 if (dev->devtype->product == CODA_7541) {
1359 /* TODO - Enabling these causes picture errors on CODA7541 */
Philipp Zabel8358e762013-06-21 03:55:32 -03001360 if (ctx->inst_type == CODA_INST_DECODER) {
1361 /* fw 1.4.50 */
1362 iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE |
1363 CODA7_USE_IP_ENABLE);
1364 } else {
1365 /* fw 13.4.29 */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001366 iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE |
1367 CODA7_USE_HOST_DBK_ENABLE |
1368 CODA7_USE_IP_ENABLE |
1369 CODA7_USE_DBK_ENABLE);
1370 }
1371 }
1372}
1373
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001374static void coda_free_context_buffers(struct coda_ctx *ctx)
1375{
1376 struct coda_dev *dev = ctx->dev;
1377
1378 if (dev->devtype->product != CODA_DX6)
1379 coda_free_aux_buf(dev, &ctx->workbuf);
1380}
1381
1382static int coda_alloc_context_buffers(struct coda_ctx *ctx,
1383 struct coda_q_data *q_data)
1384{
1385 struct coda_dev *dev = ctx->dev;
1386 size_t size;
1387 int ret;
1388
1389 switch (dev->devtype->product) {
1390 case CODA_7541:
1391 size = CODA7_WORK_BUF_SIZE;
1392 break;
1393 default:
1394 return 0;
1395 }
1396
1397 if (ctx->workbuf.vaddr) {
1398 v4l2_err(&dev->v4l2_dev, "context buffer still allocated\n");
1399 ret = -EBUSY;
1400 return -ENOMEM;
1401 }
1402
1403 ret = coda_alloc_context_buf(ctx, &ctx->workbuf, size);
1404 if (ret < 0) {
1405 v4l2_err(&dev->v4l2_dev, "failed to allocate %d byte context buffer",
1406 ctx->workbuf.size);
1407 goto err;
1408 }
1409
1410 return 0;
1411
1412err:
1413 coda_free_context_buffers(ctx);
1414 return ret;
1415}
1416
Philipp Zabeld35167a2013-05-23 10:42:59 -03001417static int coda_encode_header(struct coda_ctx *ctx, struct vb2_buffer *buf,
1418 int header_code, u8 *header, int *size)
1419{
1420 struct coda_dev *dev = ctx->dev;
1421 int ret;
1422
1423 coda_write(dev, vb2_dma_contig_plane_dma_addr(buf, 0),
1424 CODA_CMD_ENC_HEADER_BB_START);
1425 coda_write(dev, vb2_plane_size(buf, 0), CODA_CMD_ENC_HEADER_BB_SIZE);
1426 coda_write(dev, header_code, CODA_CMD_ENC_HEADER_CODE);
1427 ret = coda_command_sync(ctx, CODA_COMMAND_ENCODE_HEADER);
1428 if (ret < 0) {
1429 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_ENCODE_HEADER timeout\n");
1430 return ret;
1431 }
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001432 *size = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx)) -
Philipp Zabeld35167a2013-05-23 10:42:59 -03001433 coda_read(dev, CODA_CMD_ENC_HEADER_BB_START);
1434 memcpy(header, vb2_plane_vaddr(buf, 0), *size);
1435
1436 return 0;
1437}
1438
Javier Martin186b2502012-07-26 05:53:35 -03001439static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
1440{
1441 struct coda_ctx *ctx = vb2_get_drv_priv(q);
1442 struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
1443 u32 bitstream_buf, bitstream_size;
1444 struct coda_dev *dev = ctx->dev;
1445 struct coda_q_data *q_data_src, *q_data_dst;
Javier Martin186b2502012-07-26 05:53:35 -03001446 struct vb2_buffer *buf;
Philipp Zabelec25f682012-07-20 08:54:29 -03001447 u32 dst_fourcc;
Javier Martin186b2502012-07-26 05:53:35 -03001448 u32 value;
Philipp Zabeld35167a2013-05-23 10:42:59 -03001449 int ret = 0;
Javier Martin186b2502012-07-26 05:53:35 -03001450
1451 if (count < 1)
1452 return -EINVAL;
1453
1454 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
Philipp Zabelb96904e2013-05-23 10:42:58 -03001455 ctx->streamon_out = 1;
Javier Martin186b2502012-07-26 05:53:35 -03001456 else
Philipp Zabelb96904e2013-05-23 10:42:58 -03001457 ctx->streamon_cap = 1;
Javier Martin186b2502012-07-26 05:53:35 -03001458
Philipp Zabelb96904e2013-05-23 10:42:58 -03001459 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1460 if (ctx->streamon_out) {
1461 if (coda_format_is_yuv(q_data_src->fourcc))
1462 ctx->inst_type = CODA_INST_ENCODER;
1463 else
1464 ctx->inst_type = CODA_INST_DECODER;
1465 }
Javier Martin186b2502012-07-26 05:53:35 -03001466
Philipp Zabelb96904e2013-05-23 10:42:58 -03001467 /* Don't start the coda unless both queues are on */
1468 if (!(ctx->streamon_out & ctx->streamon_cap))
1469 return 0;
Javier Martin186b2502012-07-26 05:53:35 -03001470
Philipp Zabelb96904e2013-05-23 10:42:58 -03001471 ctx->gopcounter = ctx->params.gop_size - 1;
Javier Martin186b2502012-07-26 05:53:35 -03001472 buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
1473 bitstream_buf = vb2_dma_contig_plane_dma_addr(buf, 0);
1474 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1475 bitstream_size = q_data_dst->sizeimage;
Philipp Zabelb96904e2013-05-23 10:42:58 -03001476 dst_fourcc = q_data_dst->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -03001477
Philipp Zabelb96904e2013-05-23 10:42:58 -03001478 ctx->codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
1479 q_data_dst->fourcc);
1480 if (!ctx->codec) {
Javier Martin186b2502012-07-26 05:53:35 -03001481 v4l2_err(v4l2_dev, "couldn't tell instance type.\n");
1482 return -EINVAL;
1483 }
1484
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001485 /* Allocate per-instance buffers */
1486 ret = coda_alloc_context_buffers(ctx, q_data_src);
1487 if (ret < 0)
1488 return ret;
1489
Javier Martin186b2502012-07-26 05:53:35 -03001490 if (!coda_is_initialized(dev)) {
1491 v4l2_err(v4l2_dev, "coda is not initialized.\n");
1492 return -EFAULT;
1493 }
Philipp Zabelfcb62822013-05-23 10:43:00 -03001494
1495 mutex_lock(&dev->coda_mutex);
1496
Javier Martin186b2502012-07-26 05:53:35 -03001497 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001498 coda_write(dev, bitstream_buf, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
1499 coda_write(dev, bitstream_buf, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
Javier Martin186b2502012-07-26 05:53:35 -03001500 switch (dev->devtype->product) {
1501 case CODA_DX6:
1502 coda_write(dev, CODADX6_STREAM_BUF_DYNALLOC_EN |
1503 CODADX6_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
1504 break;
1505 default:
1506 coda_write(dev, CODA7_STREAM_BUF_DYNALLOC_EN |
1507 CODA7_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
1508 }
1509
Philipp Zabel10436672012-07-02 09:03:55 -03001510 if (dev->devtype->product == CODA_DX6) {
1511 /* Configure the coda */
1512 coda_write(dev, dev->iram_paddr, CODADX6_REG_BIT_SEARCH_RAM_BASE_ADDR);
1513 }
Javier Martin186b2502012-07-26 05:53:35 -03001514
1515 /* Could set rotation here if needed */
1516 switch (dev->devtype->product) {
1517 case CODA_DX6:
1518 value = (q_data_src->width & CODADX6_PICWIDTH_MASK) << CODADX6_PICWIDTH_OFFSET;
Philipp Zabelb96904e2013-05-23 10:42:58 -03001519 value |= (q_data_src->height & CODADX6_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03001520 break;
1521 default:
1522 value = (q_data_src->width & CODA7_PICWIDTH_MASK) << CODA7_PICWIDTH_OFFSET;
Philipp Zabelb96904e2013-05-23 10:42:58 -03001523 value |= (q_data_src->height & CODA7_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03001524 }
Javier Martin186b2502012-07-26 05:53:35 -03001525 coda_write(dev, value, CODA_CMD_ENC_SEQ_SRC_SIZE);
1526 coda_write(dev, ctx->params.framerate,
1527 CODA_CMD_ENC_SEQ_SRC_F_RATE);
1528
Philipp Zabelb96904e2013-05-23 10:42:58 -03001529 ctx->params.codec_mode = ctx->codec->mode;
Javier Martin186b2502012-07-26 05:53:35 -03001530 switch (dst_fourcc) {
1531 case V4L2_PIX_FMT_MPEG4:
Javier Martin186b2502012-07-26 05:53:35 -03001532 coda_write(dev, CODA_STD_MPEG4, CODA_CMD_ENC_SEQ_COD_STD);
1533 coda_write(dev, 0, CODA_CMD_ENC_SEQ_MP4_PARA);
1534 break;
1535 case V4L2_PIX_FMT_H264:
Javier Martin186b2502012-07-26 05:53:35 -03001536 coda_write(dev, CODA_STD_H264, CODA_CMD_ENC_SEQ_COD_STD);
1537 coda_write(dev, 0, CODA_CMD_ENC_SEQ_264_PARA);
1538 break;
1539 default:
1540 v4l2_err(v4l2_dev,
1541 "dst format (0x%08x) invalid.\n", dst_fourcc);
Philipp Zabelfcb62822013-05-23 10:43:00 -03001542 ret = -EINVAL;
1543 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03001544 }
1545
Philipp Zabelc566c782012-08-08 11:59:38 -03001546 switch (ctx->params.slice_mode) {
1547 case V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE:
1548 value = 0;
1549 break;
1550 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_MB:
1551 value = (ctx->params.slice_max_mb & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
1552 value |= (1 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03001553 value |= 1 & CODA_SLICING_MODE_MASK;
Philipp Zabelc566c782012-08-08 11:59:38 -03001554 break;
1555 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES:
1556 value = (ctx->params.slice_max_bits & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
1557 value |= (0 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
1558 value |= 1 & CODA_SLICING_MODE_MASK;
1559 break;
1560 }
Javier Martin186b2502012-07-26 05:53:35 -03001561 coda_write(dev, value, CODA_CMD_ENC_SEQ_SLICE_MODE);
Philipp Zabelc566c782012-08-08 11:59:38 -03001562 value = ctx->params.gop_size & CODA_GOP_SIZE_MASK;
Javier Martin186b2502012-07-26 05:53:35 -03001563 coda_write(dev, value, CODA_CMD_ENC_SEQ_GOP_SIZE);
1564
1565 if (ctx->params.bitrate) {
1566 /* Rate control enabled */
1567 value = (ctx->params.bitrate & CODA_RATECONTROL_BITRATE_MASK) << CODA_RATECONTROL_BITRATE_OFFSET;
1568 value |= 1 & CODA_RATECONTROL_ENABLE_MASK;
1569 } else {
1570 value = 0;
1571 }
1572 coda_write(dev, value, CODA_CMD_ENC_SEQ_RC_PARA);
1573
1574 coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_BUF_SIZE);
1575 coda_write(dev, 0, CODA_CMD_ENC_SEQ_INTRA_REFRESH);
1576
1577 coda_write(dev, bitstream_buf, CODA_CMD_ENC_SEQ_BB_START);
1578 coda_write(dev, bitstream_size / 1024, CODA_CMD_ENC_SEQ_BB_SIZE);
1579
1580 /* set default gamma */
1581 value = (CODA_DEFAULT_GAMMA & CODA_GAMMA_MASK) << CODA_GAMMA_OFFSET;
1582 coda_write(dev, value, CODA_CMD_ENC_SEQ_RC_GAMMA);
1583
Philipp Zabelfb1fcf12013-05-23 10:42:53 -03001584 if (CODA_DEFAULT_GAMMA > 0) {
1585 if (dev->devtype->product == CODA_DX6)
1586 value = 1 << CODADX6_OPTION_GAMMA_OFFSET;
1587 else
1588 value = 1 << CODA7_OPTION_GAMMA_OFFSET;
1589 } else {
1590 value = 0;
1591 }
Javier Martin186b2502012-07-26 05:53:35 -03001592 coda_write(dev, value, CODA_CMD_ENC_SEQ_OPTION);
1593
Philipp Zabelc2d22512013-06-21 03:55:28 -03001594 coda_setup_iram(ctx);
1595
Javier Martin186b2502012-07-26 05:53:35 -03001596 if (dst_fourcc == V4L2_PIX_FMT_H264) {
1597 value = (FMO_SLICE_SAVE_BUF_SIZE << 7);
1598 value |= (0 & CODA_FMOPARAM_TYPE_MASK) << CODA_FMOPARAM_TYPE_OFFSET;
1599 value |= 0 & CODA_FMOPARAM_SLICENUM_MASK;
Philipp Zabel10436672012-07-02 09:03:55 -03001600 if (dev->devtype->product == CODA_DX6) {
1601 coda_write(dev, value, CODADX6_CMD_ENC_SEQ_FMO);
1602 } else {
Philipp Zabelc2d22512013-06-21 03:55:28 -03001603 coda_write(dev, ctx->iram_info.search_ram_paddr,
1604 CODA7_CMD_ENC_SEQ_SEARCH_BASE);
1605 coda_write(dev, ctx->iram_info.search_ram_size,
1606 CODA7_CMD_ENC_SEQ_SEARCH_SIZE);
Philipp Zabel10436672012-07-02 09:03:55 -03001607 }
Javier Martin186b2502012-07-26 05:53:35 -03001608 }
1609
Philipp Zabelfcb62822013-05-23 10:43:00 -03001610 ret = coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT);
1611 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03001612 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
Philipp Zabelfcb62822013-05-23 10:43:00 -03001613 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03001614 }
1615
Philipp Zabelfcb62822013-05-23 10:43:00 -03001616 if (coda_read(dev, CODA_RET_ENC_SEQ_SUCCESS) == 0) {
1617 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT failed\n");
1618 ret = -EFAULT;
1619 goto out;
1620 }
Javier Martin186b2502012-07-26 05:53:35 -03001621
Philipp Zabel20397492013-06-21 03:55:29 -03001622 ctx->num_internal_frames = 2;
Philipp Zabelec25f682012-07-20 08:54:29 -03001623 ret = coda_alloc_framebuffers(ctx, q_data_src, dst_fourcc);
Philipp Zabelfcb62822013-05-23 10:43:00 -03001624 if (ret < 0) {
1625 v4l2_err(v4l2_dev, "failed to allocate framebuffers\n");
1626 goto out;
1627 }
Javier Martin186b2502012-07-26 05:53:35 -03001628
Philipp Zabelec25f682012-07-20 08:54:29 -03001629 coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM);
Philipp Zabel10436672012-07-02 09:03:55 -03001630 coda_write(dev, round_up(q_data_src->width, 8), CODA_CMD_SET_FRAME_BUF_STRIDE);
1631 if (dev->devtype->product != CODA_DX6) {
Philipp Zabelc2d22512013-06-21 03:55:28 -03001632 coda_write(dev, ctx->iram_info.buf_bit_use,
1633 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
1634 coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use,
1635 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
1636 coda_write(dev, ctx->iram_info.buf_dbk_y_use,
1637 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
1638 coda_write(dev, ctx->iram_info.buf_dbk_c_use,
1639 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
1640 coda_write(dev, ctx->iram_info.buf_ovl_use,
1641 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
Philipp Zabel10436672012-07-02 09:03:55 -03001642 }
Philipp Zabelfcb62822013-05-23 10:43:00 -03001643 ret = coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF);
1644 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03001645 v4l2_err(v4l2_dev, "CODA_COMMAND_SET_FRAME_BUF timeout\n");
Philipp Zabelfcb62822013-05-23 10:43:00 -03001646 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03001647 }
1648
1649 /* Save stream headers */
1650 buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
1651 switch (dst_fourcc) {
1652 case V4L2_PIX_FMT_H264:
1653 /*
1654 * Get SPS in the first frame and copy it to an
1655 * intermediate buffer.
1656 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03001657 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_SPS,
1658 &ctx->vpu_header[0][0],
1659 &ctx->vpu_header_size[0]);
1660 if (ret < 0)
1661 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03001662
1663 /*
1664 * Get PPS in the first frame and copy it to an
1665 * intermediate buffer.
1666 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03001667 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_PPS,
1668 &ctx->vpu_header[1][0],
1669 &ctx->vpu_header_size[1]);
1670 if (ret < 0)
1671 goto out;
1672
Javier Martin3f3f5c72012-10-29 05:20:29 -03001673 /*
1674 * Length of H.264 headers is variable and thus it might not be
1675 * aligned for the coda to append the encoded frame. In that is
1676 * the case a filler NAL must be added to header 2.
1677 */
1678 ctx->vpu_header_size[2] = coda_h264_padding(
1679 (ctx->vpu_header_size[0] +
1680 ctx->vpu_header_size[1]),
1681 ctx->vpu_header[2]);
Javier Martin186b2502012-07-26 05:53:35 -03001682 break;
1683 case V4L2_PIX_FMT_MPEG4:
1684 /*
1685 * Get VOS in the first frame and copy it to an
1686 * intermediate buffer
1687 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03001688 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOS,
1689 &ctx->vpu_header[0][0],
1690 &ctx->vpu_header_size[0]);
1691 if (ret < 0)
1692 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03001693
Philipp Zabeld35167a2013-05-23 10:42:59 -03001694 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VIS,
1695 &ctx->vpu_header[1][0],
1696 &ctx->vpu_header_size[1]);
1697 if (ret < 0)
1698 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03001699
Philipp Zabeld35167a2013-05-23 10:42:59 -03001700 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOL,
1701 &ctx->vpu_header[2][0],
1702 &ctx->vpu_header_size[2]);
1703 if (ret < 0)
1704 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03001705 break;
1706 default:
1707 /* No more formats need to save headers at the moment */
1708 break;
1709 }
1710
Philipp Zabeld35167a2013-05-23 10:42:59 -03001711out:
Philipp Zabelfcb62822013-05-23 10:43:00 -03001712 mutex_unlock(&dev->coda_mutex);
Philipp Zabeld35167a2013-05-23 10:42:59 -03001713 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03001714}
1715
1716static int coda_stop_streaming(struct vb2_queue *q)
1717{
1718 struct coda_ctx *ctx = vb2_get_drv_priv(q);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03001719 struct coda_dev *dev = ctx->dev;
Javier Martin186b2502012-07-26 05:53:35 -03001720
1721 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1722 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1723 "%s: output\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001724 ctx->streamon_out = 0;
Javier Martin186b2502012-07-26 05:53:35 -03001725 } else {
1726 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1727 "%s: capture\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001728 ctx->streamon_cap = 0;
Javier Martin186b2502012-07-26 05:53:35 -03001729 }
1730
Philipp Zabel62bed142012-07-25 10:40:39 -03001731 /* Don't stop the coda unless both queues are off */
Philipp Zabelb96904e2013-05-23 10:42:58 -03001732 if (ctx->streamon_out || ctx->streamon_cap)
Philipp Zabel62bed142012-07-25 10:40:39 -03001733 return 0;
Philipp Zabel2fb57f02012-07-25 09:22:07 -03001734
Philipp Zabel62bed142012-07-25 10:40:39 -03001735 cancel_delayed_work(&dev->timeout);
1736
Philipp Zabelfcb62822013-05-23 10:43:00 -03001737 mutex_lock(&dev->coda_mutex);
Philipp Zabel62bed142012-07-25 10:40:39 -03001738 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1739 "%s: sent command 'SEQ_END' to coda\n", __func__);
1740 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_END)) {
1741 v4l2_err(&dev->v4l2_dev,
1742 "CODA_COMMAND_SEQ_END failed\n");
1743 return -ETIMEDOUT;
1744 }
Philipp Zabelfcb62822013-05-23 10:43:00 -03001745 mutex_unlock(&dev->coda_mutex);
Philipp Zabel62bed142012-07-25 10:40:39 -03001746
1747 coda_free_framebuffers(ctx);
1748
Javier Martin186b2502012-07-26 05:53:35 -03001749 return 0;
1750}
1751
1752static struct vb2_ops coda_qops = {
1753 .queue_setup = coda_queue_setup,
1754 .buf_prepare = coda_buf_prepare,
1755 .buf_queue = coda_buf_queue,
1756 .wait_prepare = coda_wait_prepare,
1757 .wait_finish = coda_wait_finish,
1758 .start_streaming = coda_start_streaming,
1759 .stop_streaming = coda_stop_streaming,
1760};
1761
1762static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
1763{
1764 struct coda_ctx *ctx =
1765 container_of(ctrl->handler, struct coda_ctx, ctrls);
1766
1767 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1768 "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val);
1769
1770 switch (ctrl->id) {
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03001771 case V4L2_CID_HFLIP:
1772 if (ctrl->val)
1773 ctx->params.rot_mode |= CODA_MIR_HOR;
1774 else
1775 ctx->params.rot_mode &= ~CODA_MIR_HOR;
1776 break;
1777 case V4L2_CID_VFLIP:
1778 if (ctrl->val)
1779 ctx->params.rot_mode |= CODA_MIR_VER;
1780 else
1781 ctx->params.rot_mode &= ~CODA_MIR_VER;
1782 break;
Javier Martin186b2502012-07-26 05:53:35 -03001783 case V4L2_CID_MPEG_VIDEO_BITRATE:
1784 ctx->params.bitrate = ctrl->val / 1000;
1785 break;
1786 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
1787 ctx->params.gop_size = ctrl->val;
1788 break;
1789 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
1790 ctx->params.h264_intra_qp = ctrl->val;
1791 break;
1792 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
1793 ctx->params.h264_inter_qp = ctrl->val;
1794 break;
1795 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
1796 ctx->params.mpeg4_intra_qp = ctrl->val;
1797 break;
1798 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
1799 ctx->params.mpeg4_inter_qp = ctrl->val;
1800 break;
1801 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
1802 ctx->params.slice_mode = ctrl->val;
1803 break;
1804 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
1805 ctx->params.slice_max_mb = ctrl->val;
1806 break;
Philipp Zabelc566c782012-08-08 11:59:38 -03001807 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
1808 ctx->params.slice_max_bits = ctrl->val * 8;
1809 break;
Javier Martin186b2502012-07-26 05:53:35 -03001810 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
1811 break;
1812 default:
1813 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1814 "Invalid control, id=%d, val=%d\n",
1815 ctrl->id, ctrl->val);
1816 return -EINVAL;
1817 }
1818
1819 return 0;
1820}
1821
1822static struct v4l2_ctrl_ops coda_ctrl_ops = {
1823 .s_ctrl = coda_s_ctrl,
1824};
1825
1826static int coda_ctrls_setup(struct coda_ctx *ctx)
1827{
1828 v4l2_ctrl_handler_init(&ctx->ctrls, 9);
1829
1830 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03001831 V4L2_CID_HFLIP, 0, 1, 1, 0);
1832 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1833 V4L2_CID_VFLIP, 0, 1, 1, 0);
1834 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Javier Martin186b2502012-07-26 05:53:35 -03001835 V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1, 0);
1836 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1837 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 60, 1, 16);
1838 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1839 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 1, 51, 1, 25);
1840 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1841 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 1, 51, 1, 25);
1842 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1843 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
1844 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1845 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
1846 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1847 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
Philipp Zabelc566c782012-08-08 11:59:38 -03001848 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0,
1849 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
Javier Martin186b2502012-07-26 05:53:35 -03001850 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1851 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
Philipp Zabelc566c782012-08-08 11:59:38 -03001852 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1853 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1, 500);
Javier Martin186b2502012-07-26 05:53:35 -03001854 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1855 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
1856 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
1857 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
1858 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
1859
1860 if (ctx->ctrls.error) {
1861 v4l2_err(&ctx->dev->v4l2_dev, "control initialization error (%d)",
1862 ctx->ctrls.error);
1863 return -EINVAL;
1864 }
1865
1866 return v4l2_ctrl_handler_setup(&ctx->ctrls);
1867}
1868
1869static int coda_queue_init(void *priv, struct vb2_queue *src_vq,
1870 struct vb2_queue *dst_vq)
1871{
1872 struct coda_ctx *ctx = priv;
1873 int ret;
1874
Javier Martin186b2502012-07-26 05:53:35 -03001875 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
Philipp Zabel419869c2013-05-23 07:06:30 -03001876 src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
Javier Martin186b2502012-07-26 05:53:35 -03001877 src_vq->drv_priv = ctx;
1878 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1879 src_vq->ops = &coda_qops;
1880 src_vq->mem_ops = &vb2_dma_contig_memops;
Kamil Debskiccd571c2013-04-24 10:38:24 -03001881 src_vq->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Javier Martin186b2502012-07-26 05:53:35 -03001882
1883 ret = vb2_queue_init(src_vq);
1884 if (ret)
1885 return ret;
1886
Javier Martin186b2502012-07-26 05:53:35 -03001887 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Philipp Zabel419869c2013-05-23 07:06:30 -03001888 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
Javier Martin186b2502012-07-26 05:53:35 -03001889 dst_vq->drv_priv = ctx;
1890 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1891 dst_vq->ops = &coda_qops;
1892 dst_vq->mem_ops = &vb2_dma_contig_memops;
Kamil Debskiccd571c2013-04-24 10:38:24 -03001893 dst_vq->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Javier Martin186b2502012-07-26 05:53:35 -03001894
1895 return vb2_queue_init(dst_vq);
1896}
1897
Philipp Zabele11f3e62012-07-25 09:16:58 -03001898static int coda_next_free_instance(struct coda_dev *dev)
1899{
1900 return ffz(dev->instance_mask);
1901}
1902
Javier Martin186b2502012-07-26 05:53:35 -03001903static int coda_open(struct file *file)
1904{
1905 struct coda_dev *dev = video_drvdata(file);
1906 struct coda_ctx *ctx = NULL;
1907 int ret = 0;
Philipp Zabele11f3e62012-07-25 09:16:58 -03001908 int idx;
Javier Martin186b2502012-07-26 05:53:35 -03001909
Philipp Zabele11f3e62012-07-25 09:16:58 -03001910 idx = coda_next_free_instance(dev);
1911 if (idx >= CODA_MAX_INSTANCES)
Javier Martin186b2502012-07-26 05:53:35 -03001912 return -EBUSY;
Philipp Zabele11f3e62012-07-25 09:16:58 -03001913 set_bit(idx, &dev->instance_mask);
Javier Martin186b2502012-07-26 05:53:35 -03001914
1915 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
1916 if (!ctx)
1917 return -ENOMEM;
1918
1919 v4l2_fh_init(&ctx->fh, video_devdata(file));
1920 file->private_data = &ctx->fh;
1921 v4l2_fh_add(&ctx->fh);
1922 ctx->dev = dev;
Philipp Zabele11f3e62012-07-25 09:16:58 -03001923 ctx->idx = idx;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001924 switch (dev->devtype->product) {
1925 case CODA_7541:
1926 ctx->reg_idx = 0;
1927 break;
1928 default:
1929 ctx->reg_idx = idx;
1930 }
Javier Martin186b2502012-07-26 05:53:35 -03001931 set_default_params(ctx);
1932 ctx->m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
1933 &coda_queue_init);
1934 if (IS_ERR(ctx->m2m_ctx)) {
Philipp Zabel72720ff2013-05-21 10:31:25 -03001935 ret = PTR_ERR(ctx->m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001936
1937 v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
1938 __func__, ret);
1939 goto err;
1940 }
1941 ret = coda_ctrls_setup(ctx);
1942 if (ret) {
1943 v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
1944 goto err;
1945 }
1946
1947 ctx->fh.ctrl_handler = &ctx->ctrls;
1948
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001949 ret = coda_alloc_context_buf(ctx, &ctx->parabuf, CODA_PARA_BUF_SIZE);
1950 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03001951 v4l2_err(&dev->v4l2_dev, "failed to allocate parabuf");
Javier Martin186b2502012-07-26 05:53:35 -03001952 goto err;
1953 }
1954
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001955 ctx->bitstream.size = CODA_MAX_FRAME_SIZE;
1956 ctx->bitstream.vaddr = dma_alloc_writecombine(&dev->plat_dev->dev,
1957 ctx->bitstream.size, &ctx->bitstream.paddr, GFP_KERNEL);
1958 if (!ctx->bitstream.vaddr) {
1959 v4l2_err(&dev->v4l2_dev, "failed to allocate bitstream ringbuffer");
1960 ret = -ENOMEM;
1961 goto err;
1962 }
1963 kfifo_init(&ctx->bitstream_fifo,
1964 ctx->bitstream.vaddr, ctx->bitstream.size);
1965 mutex_init(&ctx->bitstream_mutex);
1966
Javier Martin186b2502012-07-26 05:53:35 -03001967 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03001968 list_add(&ctx->list, &dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03001969 coda_unlock(ctx);
1970
1971 clk_prepare_enable(dev->clk_per);
1972 clk_prepare_enable(dev->clk_ahb);
1973
1974 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Created instance %d (%p)\n",
1975 ctx->idx, ctx);
1976
1977 return 0;
1978
1979err:
1980 v4l2_fh_del(&ctx->fh);
1981 v4l2_fh_exit(&ctx->fh);
1982 kfree(ctx);
1983 return ret;
1984}
1985
1986static int coda_release(struct file *file)
1987{
1988 struct coda_dev *dev = video_drvdata(file);
1989 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
1990
1991 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Releasing instance %p\n",
1992 ctx);
1993
1994 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03001995 list_del(&ctx->list);
Javier Martin186b2502012-07-26 05:53:35 -03001996 coda_unlock(ctx);
1997
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001998 dma_free_writecombine(&dev->plat_dev->dev, ctx->bitstream.size,
1999 ctx->bitstream.vaddr, ctx->bitstream.paddr);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002000 coda_free_context_buffers(ctx);
2001 if (ctx->dev->devtype->product == CODA_DX6)
2002 coda_free_aux_buf(dev, &ctx->workbuf);
2003
2004 coda_free_aux_buf(dev, &ctx->parabuf);
Javier Martin186b2502012-07-26 05:53:35 -03002005 v4l2_ctrl_handler_free(&ctx->ctrls);
2006 clk_disable_unprepare(dev->clk_per);
2007 clk_disable_unprepare(dev->clk_ahb);
2008 v4l2_fh_del(&ctx->fh);
2009 v4l2_fh_exit(&ctx->fh);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002010 clear_bit(ctx->idx, &dev->instance_mask);
Javier Martin186b2502012-07-26 05:53:35 -03002011 kfree(ctx);
2012
2013 return 0;
2014}
2015
2016static unsigned int coda_poll(struct file *file,
2017 struct poll_table_struct *wait)
2018{
2019 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
2020 int ret;
2021
2022 coda_lock(ctx);
2023 ret = v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
2024 coda_unlock(ctx);
2025 return ret;
2026}
2027
2028static int coda_mmap(struct file *file, struct vm_area_struct *vma)
2029{
2030 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
2031
2032 return v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
2033}
2034
2035static const struct v4l2_file_operations coda_fops = {
2036 .owner = THIS_MODULE,
2037 .open = coda_open,
2038 .release = coda_release,
2039 .poll = coda_poll,
2040 .unlocked_ioctl = video_ioctl2,
2041 .mmap = coda_mmap,
2042};
2043
Philipp Zabel477c1cf2013-06-21 03:55:33 -03002044static void coda_encode_finish(struct coda_ctx *ctx)
Javier Martin186b2502012-07-26 05:53:35 -03002045{
Philipp Zabelec25f682012-07-20 08:54:29 -03002046 struct vb2_buffer *src_buf, *dst_buf;
Philipp Zabel477c1cf2013-06-21 03:55:33 -03002047 struct coda_dev *dev = ctx->dev;
Javier Martin186b2502012-07-26 05:53:35 -03002048 u32 wr_ptr, start_ptr;
Javier Martin186b2502012-07-26 05:53:35 -03002049
Philipp Zabelec25f682012-07-20 08:54:29 -03002050 src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
2051 dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03002052
2053 /* Get results from the coda */
2054 coda_read(dev, CODA_RET_ENC_PIC_TYPE);
2055 start_ptr = coda_read(dev, CODA_CMD_ENC_PIC_BB_START);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002056 wr_ptr = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
2057
Javier Martin186b2502012-07-26 05:53:35 -03002058 /* Calculate bytesused field */
2059 if (dst_buf->v4l2_buf.sequence == 0) {
Philipp Zabel366108f2013-06-21 03:55:27 -03002060 vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr +
2061 ctx->vpu_header_size[0] +
2062 ctx->vpu_header_size[1] +
2063 ctx->vpu_header_size[2]);
Javier Martin186b2502012-07-26 05:53:35 -03002064 } else {
Philipp Zabel366108f2013-06-21 03:55:27 -03002065 vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr);
Javier Martin186b2502012-07-26 05:53:35 -03002066 }
2067
2068 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, "frame size = %u\n",
2069 wr_ptr - start_ptr);
2070
2071 coda_read(dev, CODA_RET_ENC_PIC_SLICE_NUM);
2072 coda_read(dev, CODA_RET_ENC_PIC_FLAG);
2073
2074 if (src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) {
2075 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
2076 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
2077 } else {
2078 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
2079 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
2080 }
2081
Kamil Debskiccd571c2013-04-24 10:38:24 -03002082 dst_buf->v4l2_buf.timestamp = src_buf->v4l2_buf.timestamp;
2083 dst_buf->v4l2_buf.timecode = src_buf->v4l2_buf.timecode;
2084
Philipp Zabelec25f682012-07-20 08:54:29 -03002085 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
Javier Martin186b2502012-07-26 05:53:35 -03002086 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
2087
2088 ctx->gopcounter--;
2089 if (ctx->gopcounter < 0)
2090 ctx->gopcounter = ctx->params.gop_size - 1;
2091
2092 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
2093 "job finished: encoding frame (%d) (%s)\n",
2094 dst_buf->v4l2_buf.sequence,
2095 (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ?
2096 "KEYFRAME" : "PFRAME");
Philipp Zabel477c1cf2013-06-21 03:55:33 -03002097}
2098
2099static irqreturn_t coda_irq_handler(int irq, void *data)
2100{
2101 struct coda_dev *dev = data;
2102 struct coda_ctx *ctx;
2103
2104 cancel_delayed_work(&dev->timeout);
2105
2106 /* read status register to attend the IRQ */
2107 coda_read(dev, CODA_REG_BIT_INT_STATUS);
2108 coda_write(dev, CODA_REG_BIT_INT_CLEAR_SET,
2109 CODA_REG_BIT_INT_CLEAR);
2110
2111 ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev);
2112 if (ctx == NULL) {
2113 v4l2_err(&dev->v4l2_dev, "Instance released before the end of transaction\n");
2114 mutex_unlock(&dev->coda_mutex);
2115 return IRQ_HANDLED;
2116 }
2117
2118 if (ctx->aborting) {
2119 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
2120 "task has been aborted\n");
2121 mutex_unlock(&dev->coda_mutex);
2122 return IRQ_HANDLED;
2123 }
2124
2125 if (coda_isbusy(ctx->dev)) {
2126 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
2127 "coda is still busy!!!!\n");
2128 return IRQ_NONE;
2129 }
2130
2131 coda_encode_finish(ctx);
Javier Martin186b2502012-07-26 05:53:35 -03002132
Philipp Zabelfcb62822013-05-23 10:43:00 -03002133 mutex_unlock(&dev->coda_mutex);
2134
Javier Martin186b2502012-07-26 05:53:35 -03002135 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->m2m_ctx);
2136
2137 return IRQ_HANDLED;
2138}
2139
Philipp Zabel2fb57f02012-07-25 09:22:07 -03002140static void coda_timeout(struct work_struct *work)
2141{
2142 struct coda_ctx *ctx;
2143 struct coda_dev *dev = container_of(to_delayed_work(work),
2144 struct coda_dev, timeout);
2145
Philipp Zabel39cc0292013-05-21 10:32:42 -03002146 dev_err(&dev->plat_dev->dev, "CODA PIC_RUN timeout, stopping all streams\n");
Philipp Zabel2fb57f02012-07-25 09:22:07 -03002147
2148 mutex_lock(&dev->dev_mutex);
2149 list_for_each_entry(ctx, &dev->instances, list) {
2150 v4l2_m2m_streamoff(NULL, ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
2151 v4l2_m2m_streamoff(NULL, ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2152 }
2153 mutex_unlock(&dev->dev_mutex);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002154
2155 mutex_unlock(&dev->coda_mutex);
2156 ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev);
2157 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->m2m_ctx);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03002158}
2159
Javier Martin186b2502012-07-26 05:53:35 -03002160static u32 coda_supported_firmwares[] = {
2161 CODA_FIRMWARE_VERNUM(CODA_DX6, 2, 2, 5),
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002162 CODA_FIRMWARE_VERNUM(CODA_7541, 1, 4, 50),
Javier Martin186b2502012-07-26 05:53:35 -03002163};
2164
2165static bool coda_firmware_supported(u32 vernum)
2166{
2167 int i;
2168
2169 for (i = 0; i < ARRAY_SIZE(coda_supported_firmwares); i++)
2170 if (vernum == coda_supported_firmwares[i])
2171 return true;
2172 return false;
2173}
2174
2175static char *coda_product_name(int product)
2176{
2177 static char buf[9];
2178
2179 switch (product) {
2180 case CODA_DX6:
2181 return "CodaDx6";
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03002182 case CODA_7541:
2183 return "CODA7541";
Javier Martin186b2502012-07-26 05:53:35 -03002184 default:
2185 snprintf(buf, sizeof(buf), "(0x%04x)", product);
2186 return buf;
2187 }
2188}
2189
Philipp Zabel87048bb2012-07-02 07:03:43 -03002190static int coda_hw_init(struct coda_dev *dev)
Javier Martin186b2502012-07-26 05:53:35 -03002191{
2192 u16 product, major, minor, release;
2193 u32 data;
2194 u16 *p;
2195 int i;
2196
2197 clk_prepare_enable(dev->clk_per);
2198 clk_prepare_enable(dev->clk_ahb);
2199
Javier Martin186b2502012-07-26 05:53:35 -03002200 /*
2201 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
Philipp Zabel87048bb2012-07-02 07:03:43 -03002202 * The 16-bit chars in the code buffer are in memory access
2203 * order, re-sort them to CODA order for register download.
Javier Martin186b2502012-07-26 05:53:35 -03002204 * Data in this SRAM survives a reboot.
2205 */
Philipp Zabel87048bb2012-07-02 07:03:43 -03002206 p = (u16 *)dev->codebuf.vaddr;
2207 if (dev->devtype->product == CODA_DX6) {
2208 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
2209 data = CODA_DOWN_ADDRESS_SET(i) |
2210 CODA_DOWN_DATA_SET(p[i ^ 1]);
2211 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
2212 }
2213 } else {
2214 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
2215 data = CODA_DOWN_ADDRESS_SET(i) |
2216 CODA_DOWN_DATA_SET(p[round_down(i, 4) +
2217 3 - (i % 4)]);
2218 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
2219 }
Javier Martin186b2502012-07-26 05:53:35 -03002220 }
Javier Martin186b2502012-07-26 05:53:35 -03002221
Philipp Zabel9acf7692013-05-23 10:42:56 -03002222 /* Clear registers */
2223 for (i = 0; i < 64; i++)
2224 coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
2225
Javier Martin186b2502012-07-26 05:53:35 -03002226 /* Tell the BIT where to find everything it needs */
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002227 if (dev->devtype->product == CODA_7541) {
2228 coda_write(dev, dev->tempbuf.paddr,
2229 CODA_REG_BIT_TEMP_BUF_ADDR);
2230 } else {
2231 coda_write(dev, dev->workbuf.paddr,
2232 CODA_REG_BIT_WORK_BUF_ADDR);
2233 }
Javier Martin186b2502012-07-26 05:53:35 -03002234 coda_write(dev, dev->codebuf.paddr,
2235 CODA_REG_BIT_CODE_BUF_ADDR);
2236 coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
2237
2238 /* Set default values */
2239 switch (dev->devtype->product) {
2240 case CODA_DX6:
2241 coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
2242 break;
2243 default:
2244 coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
2245 }
2246 coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
Philipp Zabel10436672012-07-02 09:03:55 -03002247
2248 if (dev->devtype->product != CODA_DX6)
2249 coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
2250
Javier Martin186b2502012-07-26 05:53:35 -03002251 coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
2252 CODA_REG_BIT_INT_ENABLE);
2253
2254 /* Reset VPU and start processor */
2255 data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
2256 data |= CODA_REG_RESET_ENABLE;
2257 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
2258 udelay(10);
2259 data &= ~CODA_REG_RESET_ENABLE;
2260 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
2261 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
2262
2263 /* Load firmware */
2264 coda_write(dev, 0, CODA_CMD_FIRMWARE_VERNUM);
2265 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
2266 coda_write(dev, 0, CODA_REG_BIT_RUN_INDEX);
2267 coda_write(dev, 0, CODA_REG_BIT_RUN_COD_STD);
2268 coda_write(dev, CODA_COMMAND_FIRMWARE_GET, CODA_REG_BIT_RUN_COMMAND);
2269 if (coda_wait_timeout(dev)) {
2270 clk_disable_unprepare(dev->clk_per);
2271 clk_disable_unprepare(dev->clk_ahb);
2272 v4l2_err(&dev->v4l2_dev, "firmware get command error\n");
2273 return -EIO;
2274 }
2275
2276 /* Check we are compatible with the loaded firmware */
2277 data = coda_read(dev, CODA_CMD_FIRMWARE_VERNUM);
2278 product = CODA_FIRMWARE_PRODUCT(data);
2279 major = CODA_FIRMWARE_MAJOR(data);
2280 minor = CODA_FIRMWARE_MINOR(data);
2281 release = CODA_FIRMWARE_RELEASE(data);
2282
2283 clk_disable_unprepare(dev->clk_per);
2284 clk_disable_unprepare(dev->clk_ahb);
2285
2286 if (product != dev->devtype->product) {
2287 v4l2_err(&dev->v4l2_dev, "Wrong firmware. Hw: %s, Fw: %s,"
2288 " Version: %u.%u.%u\n",
2289 coda_product_name(dev->devtype->product),
2290 coda_product_name(product), major, minor, release);
2291 return -EINVAL;
2292 }
2293
2294 v4l2_info(&dev->v4l2_dev, "Initialized %s.\n",
2295 coda_product_name(product));
2296
2297 if (coda_firmware_supported(data)) {
2298 v4l2_info(&dev->v4l2_dev, "Firmware version: %u.%u.%u\n",
2299 major, minor, release);
2300 } else {
2301 v4l2_warn(&dev->v4l2_dev, "Unsupported firmware version: "
2302 "%u.%u.%u\n", major, minor, release);
2303 }
2304
2305 return 0;
2306}
2307
2308static void coda_fw_callback(const struct firmware *fw, void *context)
2309{
2310 struct coda_dev *dev = context;
2311 struct platform_device *pdev = dev->plat_dev;
2312 int ret;
2313
2314 if (!fw) {
2315 v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
2316 return;
2317 }
2318
2319 /* allocate auxiliary per-device code buffer for the BIT processor */
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002320 ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size);
2321 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002322 dev_err(&pdev->dev, "failed to allocate code buffer\n");
2323 return;
2324 }
2325
Philipp Zabel87048bb2012-07-02 07:03:43 -03002326 /* Copy the whole firmware image to the code buffer */
2327 memcpy(dev->codebuf.vaddr, fw->data, fw->size);
2328 release_firmware(fw);
2329
2330 ret = coda_hw_init(dev);
Javier Martin186b2502012-07-26 05:53:35 -03002331 if (ret) {
2332 v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
2333 return;
2334 }
2335
2336 dev->vfd.fops = &coda_fops,
2337 dev->vfd.ioctl_ops = &coda_ioctl_ops;
2338 dev->vfd.release = video_device_release_empty,
2339 dev->vfd.lock = &dev->dev_mutex;
2340 dev->vfd.v4l2_dev = &dev->v4l2_dev;
Hans Verkuil954f3402012-09-05 06:05:50 -03002341 dev->vfd.vfl_dir = VFL_DIR_M2M;
Javier Martin186b2502012-07-26 05:53:35 -03002342 snprintf(dev->vfd.name, sizeof(dev->vfd.name), "%s", CODA_NAME);
2343 video_set_drvdata(&dev->vfd, dev);
2344
2345 dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
2346 if (IS_ERR(dev->alloc_ctx)) {
2347 v4l2_err(&dev->v4l2_dev, "Failed to alloc vb2 context\n");
2348 return;
2349 }
2350
2351 dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
2352 if (IS_ERR(dev->m2m_dev)) {
2353 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
2354 goto rel_ctx;
2355 }
2356
2357 ret = video_register_device(&dev->vfd, VFL_TYPE_GRABBER, 0);
2358 if (ret) {
2359 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
2360 goto rel_m2m;
2361 }
2362 v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video%d\n",
2363 dev->vfd.num);
2364
2365 return;
2366
2367rel_m2m:
2368 v4l2_m2m_release(dev->m2m_dev);
2369rel_ctx:
2370 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
2371}
2372
2373static int coda_firmware_request(struct coda_dev *dev)
2374{
2375 char *fw = dev->devtype->firmware;
2376
2377 dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw,
2378 coda_product_name(dev->devtype->product));
2379
2380 return request_firmware_nowait(THIS_MODULE, true,
2381 fw, &dev->plat_dev->dev, GFP_KERNEL, dev, coda_fw_callback);
2382}
2383
2384enum coda_platform {
2385 CODA_IMX27,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03002386 CODA_IMX53,
Javier Martin186b2502012-07-26 05:53:35 -03002387};
2388
Emil Goodec06d8752012-08-14 17:44:42 -03002389static const struct coda_devtype coda_devdata[] = {
Javier Martin186b2502012-07-26 05:53:35 -03002390 [CODA_IMX27] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -03002391 .firmware = "v4l-codadx6-imx27.bin",
2392 .product = CODA_DX6,
2393 .codecs = codadx6_codecs,
2394 .num_codecs = ARRAY_SIZE(codadx6_codecs),
Javier Martin186b2502012-07-26 05:53:35 -03002395 },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03002396 [CODA_IMX53] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -03002397 .firmware = "v4l-coda7541-imx53.bin",
2398 .product = CODA_7541,
2399 .codecs = coda7_codecs,
2400 .num_codecs = ARRAY_SIZE(coda7_codecs),
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03002401 },
Javier Martin186b2502012-07-26 05:53:35 -03002402};
2403
2404static struct platform_device_id coda_platform_ids[] = {
2405 { .name = "coda-imx27", .driver_data = CODA_IMX27 },
Fabio Estevam4e44cd02012-10-10 00:07:38 -03002406 { .name = "coda-imx53", .driver_data = CODA_IMX53 },
Javier Martin186b2502012-07-26 05:53:35 -03002407 { /* sentinel */ }
2408};
2409MODULE_DEVICE_TABLE(platform, coda_platform_ids);
2410
2411#ifdef CONFIG_OF
2412static const struct of_device_id coda_dt_ids[] = {
2413 { .compatible = "fsl,imx27-vpu", .data = &coda_platform_ids[CODA_IMX27] },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03002414 { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
Javier Martin186b2502012-07-26 05:53:35 -03002415 { /* sentinel */ }
2416};
2417MODULE_DEVICE_TABLE(of, coda_dt_ids);
2418#endif
2419
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08002420static int coda_probe(struct platform_device *pdev)
Javier Martin186b2502012-07-26 05:53:35 -03002421{
2422 const struct of_device_id *of_id =
2423 of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
2424 const struct platform_device_id *pdev_id;
Philipp Zabel657eee72013-04-29 16:17:14 -07002425 struct coda_platform_data *pdata = pdev->dev.platform_data;
2426 struct device_node *np = pdev->dev.of_node;
2427 struct gen_pool *pool;
Javier Martin186b2502012-07-26 05:53:35 -03002428 struct coda_dev *dev;
2429 struct resource *res;
2430 int ret, irq;
2431
2432 dev = devm_kzalloc(&pdev->dev, sizeof *dev, GFP_KERNEL);
2433 if (!dev) {
2434 dev_err(&pdev->dev, "Not enough memory for %s\n",
2435 CODA_NAME);
2436 return -ENOMEM;
2437 }
2438
2439 spin_lock_init(&dev->irqlock);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002440 INIT_LIST_HEAD(&dev->instances);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03002441 INIT_DELAYED_WORK(&dev->timeout, coda_timeout);
Javier Martin186b2502012-07-26 05:53:35 -03002442
2443 dev->plat_dev = pdev;
2444 dev->clk_per = devm_clk_get(&pdev->dev, "per");
2445 if (IS_ERR(dev->clk_per)) {
2446 dev_err(&pdev->dev, "Could not get per clock\n");
2447 return PTR_ERR(dev->clk_per);
2448 }
2449
2450 dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
2451 if (IS_ERR(dev->clk_ahb)) {
2452 dev_err(&pdev->dev, "Could not get ahb clock\n");
2453 return PTR_ERR(dev->clk_ahb);
2454 }
2455
2456 /* Get memory for physical registers */
2457 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2458 if (res == NULL) {
2459 dev_err(&pdev->dev, "failed to get memory region resource\n");
2460 return -ENOENT;
2461 }
2462
Philipp Zabel611cbbf2013-05-21 04:19:27 -03002463 dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
2464 if (IS_ERR(dev->regs_base))
2465 return PTR_ERR(dev->regs_base);
Javier Martin186b2502012-07-26 05:53:35 -03002466
2467 /* IRQ */
2468 irq = platform_get_irq(pdev, 0);
2469 if (irq < 0) {
2470 dev_err(&pdev->dev, "failed to get irq resource\n");
2471 return -ENOENT;
2472 }
2473
2474 if (devm_request_irq(&pdev->dev, irq, coda_irq_handler,
2475 0, CODA_NAME, dev) < 0) {
2476 dev_err(&pdev->dev, "failed to request irq\n");
2477 return -ENOENT;
2478 }
2479
Philipp Zabel657eee72013-04-29 16:17:14 -07002480 /* Get IRAM pool from device tree or platform data */
2481 pool = of_get_named_gen_pool(np, "iram", 0);
2482 if (!pool && pdata)
2483 pool = dev_get_gen_pool(pdata->iram_dev);
2484 if (!pool) {
2485 dev_err(&pdev->dev, "iram pool not available\n");
2486 return -ENOMEM;
2487 }
2488 dev->iram_pool = pool;
2489
Javier Martin186b2502012-07-26 05:53:35 -03002490 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
2491 if (ret)
2492 return ret;
2493
2494 mutex_init(&dev->dev_mutex);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002495 mutex_init(&dev->coda_mutex);
Javier Martin186b2502012-07-26 05:53:35 -03002496
2497 pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
2498
2499 if (of_id) {
2500 dev->devtype = of_id->data;
2501 } else if (pdev_id) {
2502 dev->devtype = &coda_devdata[pdev_id->driver_data];
2503 } else {
2504 v4l2_device_unregister(&dev->v4l2_dev);
2505 return -EINVAL;
2506 }
2507
2508 /* allocate auxiliary per-device buffers for the BIT processor */
2509 switch (dev->devtype->product) {
2510 case CODA_DX6:
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002511 ret = coda_alloc_aux_buf(dev, &dev->workbuf,
2512 CODADX6_WORK_BUF_SIZE);
2513 if (ret < 0) {
2514 dev_err(&pdev->dev, "failed to allocate work buffer\n");
2515 v4l2_device_unregister(&dev->v4l2_dev);
2516 return ret;
2517 }
Javier Martin186b2502012-07-26 05:53:35 -03002518 break;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002519 case CODA_7541:
2520 dev->tempbuf.size = CODA7_TEMP_BUF_SIZE;
2521 break;
Javier Martin186b2502012-07-26 05:53:35 -03002522 }
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002523 if (dev->tempbuf.size) {
2524 ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
2525 dev->tempbuf.size);
2526 if (ret < 0) {
2527 dev_err(&pdev->dev, "failed to allocate temp buffer\n");
2528 v4l2_device_unregister(&dev->v4l2_dev);
2529 return ret;
2530 }
Javier Martin186b2502012-07-26 05:53:35 -03002531 }
2532
Philipp Zabel657eee72013-04-29 16:17:14 -07002533 if (dev->devtype->product == CODA_DX6)
2534 dev->iram_size = CODADX6_IRAM_SIZE;
2535 else
2536 dev->iram_size = CODA7_IRAM_SIZE;
2537 dev->iram_vaddr = gen_pool_alloc(dev->iram_pool, dev->iram_size);
2538 if (!dev->iram_vaddr) {
2539 dev_err(&pdev->dev, "unable to alloc iram\n");
2540 return -ENOMEM;
Philipp Zabel10436672012-07-02 09:03:55 -03002541 }
Philipp Zabel657eee72013-04-29 16:17:14 -07002542 dev->iram_paddr = gen_pool_virt_to_phys(dev->iram_pool,
2543 dev->iram_vaddr);
Philipp Zabel10436672012-07-02 09:03:55 -03002544
Javier Martin186b2502012-07-26 05:53:35 -03002545 platform_set_drvdata(pdev, dev);
2546
2547 return coda_firmware_request(dev);
2548}
2549
2550static int coda_remove(struct platform_device *pdev)
2551{
2552 struct coda_dev *dev = platform_get_drvdata(pdev);
2553
2554 video_unregister_device(&dev->vfd);
2555 if (dev->m2m_dev)
2556 v4l2_m2m_release(dev->m2m_dev);
2557 if (dev->alloc_ctx)
2558 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
2559 v4l2_device_unregister(&dev->v4l2_dev);
Philipp Zabel657eee72013-04-29 16:17:14 -07002560 if (dev->iram_vaddr)
2561 gen_pool_free(dev->iram_pool, dev->iram_vaddr, dev->iram_size);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002562 coda_free_aux_buf(dev, &dev->codebuf);
2563 coda_free_aux_buf(dev, &dev->tempbuf);
2564 coda_free_aux_buf(dev, &dev->workbuf);
Javier Martin186b2502012-07-26 05:53:35 -03002565 return 0;
2566}
2567
2568static struct platform_driver coda_driver = {
2569 .probe = coda_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08002570 .remove = coda_remove,
Javier Martin186b2502012-07-26 05:53:35 -03002571 .driver = {
2572 .name = CODA_NAME,
2573 .owner = THIS_MODULE,
2574 .of_match_table = of_match_ptr(coda_dt_ids),
2575 },
2576 .id_table = coda_platform_ids,
2577};
2578
2579module_platform_driver(coda_driver);
2580
2581MODULE_LICENSE("GPL");
2582MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
2583MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");