Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1 | /* |
| 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 Zabel | 657eee7 | 2013-04-29 16:17:14 -0700 | [diff] [blame] | 17 | #include <linux/genalloc.h> |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 18 | #include <linux/interrupt.h> |
| 19 | #include <linux/io.h> |
| 20 | #include <linux/irq.h> |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/of_device.h> |
| 23 | #include <linux/platform_device.h> |
| 24 | #include <linux/slab.h> |
| 25 | #include <linux/videodev2.h> |
| 26 | #include <linux/of.h> |
Philipp Zabel | 657eee7 | 2013-04-29 16:17:14 -0700 | [diff] [blame] | 27 | #include <linux/platform_data/coda.h> |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 28 | |
| 29 | #include <media/v4l2-ctrls.h> |
| 30 | #include <media/v4l2-device.h> |
| 31 | #include <media/v4l2-ioctl.h> |
| 32 | #include <media/v4l2-mem2mem.h> |
| 33 | #include <media/videobuf2-core.h> |
| 34 | #include <media/videobuf2-dma-contig.h> |
| 35 | |
| 36 | #include "coda.h" |
| 37 | |
| 38 | #define CODA_NAME "coda" |
| 39 | |
| 40 | #define CODA_MAX_INSTANCES 4 |
| 41 | |
| 42 | #define CODA_FMO_BUF_SIZE 32 |
| 43 | #define CODADX6_WORK_BUF_SIZE (288 * 1024 + CODA_FMO_BUF_SIZE * 8 * 1024) |
| 44 | #define CODA7_WORK_BUF_SIZE (512 * 1024 + CODA_FMO_BUF_SIZE * 8 * 1024) |
| 45 | #define CODA_PARA_BUF_SIZE (10 * 1024) |
| 46 | #define CODA_ISRAM_SIZE (2048 * 2) |
Philipp Zabel | 657eee7 | 2013-04-29 16:17:14 -0700 | [diff] [blame] | 47 | #define CODADX6_IRAM_SIZE 0xb000 |
Philipp Zabel | 1043667 | 2012-07-02 09:03:55 -0300 | [diff] [blame] | 48 | #define CODA7_IRAM_SIZE 0x14000 /* 81920 bytes */ |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 49 | |
Philipp Zabel | ec25f68 | 2012-07-20 08:54:29 -0300 | [diff] [blame] | 50 | #define CODA_MAX_FRAMEBUFFERS 2 |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 51 | |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame^] | 52 | #define MAX_W 8192 |
| 53 | #define MAX_H 8192 |
| 54 | #define CODA_MAX_FRAME_SIZE 0x100000 |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 55 | #define FMO_SLICE_SAVE_BUF_SIZE (32) |
| 56 | #define CODA_DEFAULT_GAMMA 4096 |
| 57 | |
| 58 | #define MIN_W 176 |
| 59 | #define MIN_H 144 |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 60 | |
| 61 | #define S_ALIGN 1 /* multiple of 2 */ |
| 62 | #define W_ALIGN 1 /* multiple of 2 */ |
| 63 | #define H_ALIGN 1 /* multiple of 2 */ |
| 64 | |
| 65 | #define fh_to_ctx(__fh) container_of(__fh, struct coda_ctx, fh) |
| 66 | |
| 67 | static int coda_debug; |
Philipp Zabel | c4eb1bfc | 2013-05-21 04:24:16 -0300 | [diff] [blame] | 68 | module_param(coda_debug, int, 0644); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 69 | MODULE_PARM_DESC(coda_debug, "Debug level (0-1)"); |
| 70 | |
| 71 | enum { |
| 72 | V4L2_M2M_SRC = 0, |
| 73 | V4L2_M2M_DST = 1, |
| 74 | }; |
| 75 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 76 | enum coda_inst_type { |
| 77 | CODA_INST_ENCODER, |
| 78 | CODA_INST_DECODER, |
| 79 | }; |
| 80 | |
| 81 | enum coda_product { |
| 82 | CODA_DX6 = 0xf001, |
Philipp Zabel | df1e74c | 2012-07-02 06:07:10 -0300 | [diff] [blame] | 83 | CODA_7541 = 0xf012, |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | struct coda_fmt { |
| 87 | char *name; |
| 88 | u32 fourcc; |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame^] | 89 | }; |
| 90 | |
| 91 | struct coda_codec { |
| 92 | u32 mode; |
| 93 | u32 src_fourcc; |
| 94 | u32 dst_fourcc; |
| 95 | u32 max_w; |
| 96 | u32 max_h; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | struct coda_devtype { |
| 100 | char *firmware; |
| 101 | enum coda_product product; |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame^] | 102 | struct coda_codec *codecs; |
| 103 | unsigned int num_codecs; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 104 | size_t workbuf_size; |
| 105 | }; |
| 106 | |
| 107 | /* Per-queue, driver-specific private data */ |
| 108 | struct coda_q_data { |
| 109 | unsigned int width; |
| 110 | unsigned int height; |
| 111 | unsigned int sizeimage; |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame^] | 112 | unsigned int fourcc; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | struct coda_aux_buf { |
| 116 | void *vaddr; |
| 117 | dma_addr_t paddr; |
| 118 | u32 size; |
| 119 | }; |
| 120 | |
| 121 | struct coda_dev { |
| 122 | struct v4l2_device v4l2_dev; |
| 123 | struct video_device vfd; |
| 124 | struct platform_device *plat_dev; |
Emil Goode | c06d875 | 2012-08-14 17:44:42 -0300 | [diff] [blame] | 125 | const struct coda_devtype *devtype; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 126 | |
| 127 | void __iomem *regs_base; |
| 128 | struct clk *clk_per; |
| 129 | struct clk *clk_ahb; |
| 130 | |
| 131 | struct coda_aux_buf codebuf; |
| 132 | struct coda_aux_buf workbuf; |
Philipp Zabel | 657eee7 | 2013-04-29 16:17:14 -0700 | [diff] [blame] | 133 | struct gen_pool *iram_pool; |
| 134 | long unsigned int iram_vaddr; |
Philipp Zabel | 1043667 | 2012-07-02 09:03:55 -0300 | [diff] [blame] | 135 | long unsigned int iram_paddr; |
Philipp Zabel | 657eee7 | 2013-04-29 16:17:14 -0700 | [diff] [blame] | 136 | unsigned long iram_size; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 137 | |
| 138 | spinlock_t irqlock; |
| 139 | struct mutex dev_mutex; |
| 140 | struct v4l2_m2m_dev *m2m_dev; |
| 141 | struct vb2_alloc_ctx *alloc_ctx; |
Philipp Zabel | e11f3e6 | 2012-07-25 09:16:58 -0300 | [diff] [blame] | 142 | struct list_head instances; |
| 143 | unsigned long instance_mask; |
Philipp Zabel | 2fb57f0 | 2012-07-25 09:22:07 -0300 | [diff] [blame] | 144 | struct delayed_work timeout; |
Philipp Zabel | 62bed14 | 2012-07-25 10:40:39 -0300 | [diff] [blame] | 145 | struct completion done; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 146 | }; |
| 147 | |
| 148 | struct coda_params { |
Philipp Zabel | 8f35c7b | 2012-07-09 04:25:52 -0300 | [diff] [blame] | 149 | u8 rot_mode; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 150 | u8 h264_intra_qp; |
| 151 | u8 h264_inter_qp; |
| 152 | u8 mpeg4_intra_qp; |
| 153 | u8 mpeg4_inter_qp; |
| 154 | u8 gop_size; |
| 155 | int codec_mode; |
| 156 | enum v4l2_mpeg_video_multi_slice_mode slice_mode; |
| 157 | u32 framerate; |
| 158 | u16 bitrate; |
Philipp Zabel | c566c78 | 2012-08-08 11:59:38 -0300 | [diff] [blame] | 159 | u32 slice_max_bits; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 160 | u32 slice_max_mb; |
| 161 | }; |
| 162 | |
| 163 | struct coda_ctx { |
| 164 | struct coda_dev *dev; |
Philipp Zabel | e11f3e6 | 2012-07-25 09:16:58 -0300 | [diff] [blame] | 165 | struct list_head list; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 166 | int aborting; |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame^] | 167 | int streamon_out; |
| 168 | int streamon_cap; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 169 | u32 isequence; |
| 170 | struct coda_q_data q_data[2]; |
| 171 | enum coda_inst_type inst_type; |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame^] | 172 | struct coda_codec *codec; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 173 | enum v4l2_colorspace colorspace; |
| 174 | struct coda_params params; |
| 175 | struct v4l2_m2m_ctx *m2m_ctx; |
| 176 | struct v4l2_ctrl_handler ctrls; |
| 177 | struct v4l2_fh fh; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 178 | int gopcounter; |
| 179 | char vpu_header[3][64]; |
| 180 | int vpu_header_size[3]; |
| 181 | struct coda_aux_buf parabuf; |
Philipp Zabel | ec25f68 | 2012-07-20 08:54:29 -0300 | [diff] [blame] | 182 | struct coda_aux_buf internal_frames[CODA_MAX_FRAMEBUFFERS]; |
| 183 | int num_internal_frames; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 184 | int idx; |
| 185 | }; |
| 186 | |
Javier Martin | 832fbb5 | 2012-10-29 08:34:59 -0300 | [diff] [blame] | 187 | static const u8 coda_filler_nal[14] = { 0x00, 0x00, 0x00, 0x01, 0x0c, 0xff, |
| 188 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80 }; |
| 189 | static const u8 coda_filler_size[8] = { 0, 7, 14, 13, 12, 11, 10, 9 }; |
Javier Martin | 3f3f5c7 | 2012-10-29 05:20:29 -0300 | [diff] [blame] | 190 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 191 | static inline void coda_write(struct coda_dev *dev, u32 data, u32 reg) |
| 192 | { |
| 193 | v4l2_dbg(1, coda_debug, &dev->v4l2_dev, |
| 194 | "%s: data=0x%x, reg=0x%x\n", __func__, data, reg); |
| 195 | writel(data, dev->regs_base + reg); |
| 196 | } |
| 197 | |
| 198 | static inline unsigned int coda_read(struct coda_dev *dev, u32 reg) |
| 199 | { |
| 200 | u32 data; |
| 201 | data = readl(dev->regs_base + reg); |
| 202 | v4l2_dbg(1, coda_debug, &dev->v4l2_dev, |
| 203 | "%s: data=0x%x, reg=0x%x\n", __func__, data, reg); |
| 204 | return data; |
| 205 | } |
| 206 | |
| 207 | static inline unsigned long coda_isbusy(struct coda_dev *dev) |
| 208 | { |
| 209 | return coda_read(dev, CODA_REG_BIT_BUSY); |
| 210 | } |
| 211 | |
| 212 | static inline int coda_is_initialized(struct coda_dev *dev) |
| 213 | { |
| 214 | return (coda_read(dev, CODA_REG_BIT_CUR_PC) != 0); |
| 215 | } |
| 216 | |
| 217 | static int coda_wait_timeout(struct coda_dev *dev) |
| 218 | { |
| 219 | unsigned long timeout = jiffies + msecs_to_jiffies(1000); |
| 220 | |
| 221 | while (coda_isbusy(dev)) { |
| 222 | if (time_after(jiffies, timeout)) |
| 223 | return -ETIMEDOUT; |
| 224 | } |
| 225 | return 0; |
| 226 | } |
| 227 | |
| 228 | static void coda_command_async(struct coda_ctx *ctx, int cmd) |
| 229 | { |
| 230 | struct coda_dev *dev = ctx->dev; |
| 231 | coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY); |
| 232 | |
| 233 | coda_write(dev, ctx->idx, CODA_REG_BIT_RUN_INDEX); |
| 234 | coda_write(dev, ctx->params.codec_mode, CODA_REG_BIT_RUN_COD_STD); |
| 235 | coda_write(dev, cmd, CODA_REG_BIT_RUN_COMMAND); |
| 236 | } |
| 237 | |
| 238 | static int coda_command_sync(struct coda_ctx *ctx, int cmd) |
| 239 | { |
| 240 | struct coda_dev *dev = ctx->dev; |
| 241 | |
| 242 | coda_command_async(ctx, cmd); |
| 243 | return coda_wait_timeout(dev); |
| 244 | } |
| 245 | |
| 246 | static struct coda_q_data *get_q_data(struct coda_ctx *ctx, |
| 247 | enum v4l2_buf_type type) |
| 248 | { |
| 249 | switch (type) { |
| 250 | case V4L2_BUF_TYPE_VIDEO_OUTPUT: |
| 251 | return &(ctx->q_data[V4L2_M2M_SRC]); |
| 252 | case V4L2_BUF_TYPE_VIDEO_CAPTURE: |
| 253 | return &(ctx->q_data[V4L2_M2M_DST]); |
| 254 | default: |
| 255 | BUG(); |
| 256 | } |
| 257 | return NULL; |
| 258 | } |
| 259 | |
| 260 | /* |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame^] | 261 | * Array of all formats supported by any version of Coda: |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 262 | */ |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame^] | 263 | static struct coda_fmt coda_formats[] = { |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 264 | { |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame^] | 265 | .name = "YUV 4:2:0 Planar, YCbCr", |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 266 | .fourcc = V4L2_PIX_FMT_YUV420, |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame^] | 267 | }, |
| 268 | { |
| 269 | .name = "YUV 4:2:0 Planar, YCrCb", |
| 270 | .fourcc = V4L2_PIX_FMT_YVU420, |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 271 | }, |
| 272 | { |
| 273 | .name = "H264 Encoded Stream", |
| 274 | .fourcc = V4L2_PIX_FMT_H264, |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 275 | }, |
| 276 | { |
| 277 | .name = "MPEG4 Encoded Stream", |
| 278 | .fourcc = V4L2_PIX_FMT_MPEG4, |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 279 | }, |
| 280 | }; |
| 281 | |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame^] | 282 | #define CODA_CODEC(mode, src_fourcc, dst_fourcc, max_w, max_h) \ |
| 283 | { mode, src_fourcc, dst_fourcc, max_w, max_h } |
| 284 | |
| 285 | /* |
| 286 | * Arrays of codecs supported by each given version of Coda: |
| 287 | * i.MX27 -> codadx6 |
| 288 | * i.MX5x -> coda7 |
| 289 | * i.MX6 -> coda960 |
| 290 | * Use V4L2_PIX_FMT_YUV420 as placeholder for all supported YUV 4:2:0 variants |
| 291 | */ |
| 292 | static struct coda_codec codadx6_codecs[] = { |
| 293 | CODA_CODEC(CODADX6_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 720, 576), |
| 294 | CODA_CODEC(CODADX6_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 720, 576), |
Philipp Zabel | df1e74c | 2012-07-02 06:07:10 -0300 | [diff] [blame] | 295 | }; |
| 296 | |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame^] | 297 | static struct coda_codec coda7_codecs[] = { |
| 298 | CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1280, 720), |
| 299 | CODA_CODEC(CODA7_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1280, 720), |
| 300 | }; |
| 301 | |
| 302 | static bool coda_format_is_yuv(u32 fourcc) |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 303 | { |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame^] | 304 | switch (fourcc) { |
| 305 | case V4L2_PIX_FMT_YUV420: |
| 306 | case V4L2_PIX_FMT_YVU420: |
| 307 | return true; |
| 308 | default: |
| 309 | return false; |
| 310 | } |
| 311 | } |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 312 | |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame^] | 313 | /* |
| 314 | * Normalize all supported YUV 4:2:0 formats to the value used in the codec |
| 315 | * tables. |
| 316 | */ |
| 317 | static u32 coda_format_normalize_yuv(u32 fourcc) |
| 318 | { |
| 319 | return coda_format_is_yuv(fourcc) ? V4L2_PIX_FMT_YUV420 : fourcc; |
| 320 | } |
| 321 | |
| 322 | static struct coda_codec *coda_find_codec(struct coda_dev *dev, int src_fourcc, |
| 323 | int dst_fourcc) |
| 324 | { |
| 325 | struct coda_codec *codecs = dev->devtype->codecs; |
| 326 | int num_codecs = dev->devtype->num_codecs; |
| 327 | int k; |
| 328 | |
| 329 | src_fourcc = coda_format_normalize_yuv(src_fourcc); |
| 330 | dst_fourcc = coda_format_normalize_yuv(dst_fourcc); |
| 331 | if (src_fourcc == dst_fourcc) |
| 332 | return NULL; |
| 333 | |
| 334 | for (k = 0; k < num_codecs; k++) { |
| 335 | if (codecs[k].src_fourcc == src_fourcc && |
| 336 | codecs[k].dst_fourcc == dst_fourcc) |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 337 | break; |
| 338 | } |
| 339 | |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame^] | 340 | if (k == num_codecs) |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 341 | return NULL; |
| 342 | |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame^] | 343 | return &codecs[k]; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | /* |
| 347 | * V4L2 ioctl() operations. |
| 348 | */ |
| 349 | static int vidioc_querycap(struct file *file, void *priv, |
| 350 | struct v4l2_capability *cap) |
| 351 | { |
| 352 | strlcpy(cap->driver, CODA_NAME, sizeof(cap->driver)); |
| 353 | strlcpy(cap->card, CODA_NAME, sizeof(cap->card)); |
Philipp Zabel | dad0e1c | 2013-05-21 04:18:22 -0300 | [diff] [blame] | 354 | strlcpy(cap->bus_info, "platform:" CODA_NAME, sizeof(cap->bus_info)); |
Sylwester Nawrocki | c3aac8b | 2012-08-22 18:00:19 -0300 | [diff] [blame] | 355 | /* |
| 356 | * This is only a mem-to-mem video device. The capture and output |
| 357 | * device capability flags are left only for backward compatibility |
| 358 | * and are scheduled for removal. |
| 359 | */ |
| 360 | cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT | |
| 361 | V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 362 | cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS; |
| 363 | |
| 364 | return 0; |
| 365 | } |
| 366 | |
| 367 | static int enum_fmt(void *priv, struct v4l2_fmtdesc *f, |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame^] | 368 | enum v4l2_buf_type type) |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 369 | { |
| 370 | struct coda_ctx *ctx = fh_to_ctx(priv); |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame^] | 371 | struct coda_codec *codecs = ctx->dev->devtype->codecs; |
| 372 | struct coda_fmt *formats = coda_formats; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 373 | struct coda_fmt *fmt; |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame^] | 374 | int num_codecs = ctx->dev->devtype->num_codecs; |
| 375 | int num_formats = ARRAY_SIZE(coda_formats); |
| 376 | int i, k, num = 0; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 377 | |
| 378 | for (i = 0; i < num_formats; i++) { |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame^] | 379 | /* Both uncompressed formats are always supported */ |
| 380 | if (coda_format_is_yuv(formats[i].fourcc)) { |
| 381 | if (num == f->index) |
| 382 | break; |
| 383 | ++num; |
| 384 | continue; |
| 385 | } |
| 386 | /* Compressed formats may be supported, check the codec list */ |
| 387 | for (k = 0; k < num_codecs; k++) { |
| 388 | if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE && |
| 389 | formats[i].fourcc == codecs[k].dst_fourcc) |
| 390 | break; |
| 391 | if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT && |
| 392 | formats[i].fourcc == codecs[k].src_fourcc) |
| 393 | break; |
| 394 | } |
| 395 | if (k < num_codecs) { |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 396 | if (num == f->index) |
| 397 | break; |
| 398 | ++num; |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | if (i < num_formats) { |
| 403 | fmt = &formats[i]; |
| 404 | strlcpy(f->description, fmt->name, sizeof(f->description)); |
| 405 | f->pixelformat = fmt->fourcc; |
| 406 | return 0; |
| 407 | } |
| 408 | |
| 409 | /* Format not found */ |
| 410 | return -EINVAL; |
| 411 | } |
| 412 | |
| 413 | static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv, |
| 414 | struct v4l2_fmtdesc *f) |
| 415 | { |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame^] | 416 | return enum_fmt(priv, f, V4L2_BUF_TYPE_VIDEO_CAPTURE); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | static int vidioc_enum_fmt_vid_out(struct file *file, void *priv, |
| 420 | struct v4l2_fmtdesc *f) |
| 421 | { |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame^] | 422 | return enum_fmt(priv, f, V4L2_BUF_TYPE_VIDEO_OUTPUT); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | static int vidioc_g_fmt(struct file *file, void *priv, struct v4l2_format *f) |
| 426 | { |
| 427 | struct vb2_queue *vq; |
| 428 | struct coda_q_data *q_data; |
| 429 | struct coda_ctx *ctx = fh_to_ctx(priv); |
| 430 | |
| 431 | vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type); |
| 432 | if (!vq) |
| 433 | return -EINVAL; |
| 434 | |
| 435 | q_data = get_q_data(ctx, f->type); |
| 436 | |
| 437 | f->fmt.pix.field = V4L2_FIELD_NONE; |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame^] | 438 | f->fmt.pix.pixelformat = q_data->fourcc; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 439 | f->fmt.pix.width = q_data->width; |
| 440 | f->fmt.pix.height = q_data->height; |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame^] | 441 | if (coda_format_is_yuv(f->fmt.pix.pixelformat)) |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 442 | f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 2); |
| 443 | else /* encoded formats h.264/mpeg4 */ |
| 444 | f->fmt.pix.bytesperline = 0; |
| 445 | |
| 446 | f->fmt.pix.sizeimage = q_data->sizeimage; |
| 447 | f->fmt.pix.colorspace = ctx->colorspace; |
| 448 | |
| 449 | return 0; |
| 450 | } |
| 451 | |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame^] | 452 | static int vidioc_try_fmt(struct coda_codec *codec, struct v4l2_format *f) |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 453 | { |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame^] | 454 | unsigned int max_w, max_h; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 455 | enum v4l2_field field; |
| 456 | |
| 457 | field = f->fmt.pix.field; |
| 458 | if (field == V4L2_FIELD_ANY) |
| 459 | field = V4L2_FIELD_NONE; |
| 460 | else if (V4L2_FIELD_NONE != field) |
| 461 | return -EINVAL; |
| 462 | |
| 463 | /* V4L2 specification suggests the driver corrects the format struct |
| 464 | * if any of the dimensions is unsupported */ |
| 465 | f->fmt.pix.field = field; |
| 466 | |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame^] | 467 | if (codec) { |
| 468 | max_w = codec->max_w; |
| 469 | max_h = codec->max_h; |
| 470 | } else { |
| 471 | max_w = MAX_W; |
| 472 | max_h = MAX_H; |
| 473 | } |
| 474 | v4l_bound_align_image(&f->fmt.pix.width, MIN_W, max_w, |
| 475 | W_ALIGN, &f->fmt.pix.height, |
| 476 | MIN_H, max_h, H_ALIGN, S_ALIGN); |
| 477 | |
| 478 | if (coda_format_is_yuv(f->fmt.pix.pixelformat)) { |
Philipp Zabel | 47cf0c6 | 2013-05-23 10:42:54 -0300 | [diff] [blame] | 479 | /* Frame stride must be multiple of 8 */ |
| 480 | f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 8); |
| 481 | f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * |
Philipp Zabel | 451d43a | 2012-07-26 09:18:27 -0300 | [diff] [blame] | 482 | f->fmt.pix.height * 3 / 2; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 483 | } else { /*encoded formats h.264/mpeg4 */ |
| 484 | f->fmt.pix.bytesperline = 0; |
| 485 | f->fmt.pix.sizeimage = CODA_MAX_FRAME_SIZE; |
| 486 | } |
| 487 | |
| 488 | return 0; |
| 489 | } |
| 490 | |
| 491 | static int vidioc_try_fmt_vid_cap(struct file *file, void *priv, |
| 492 | struct v4l2_format *f) |
| 493 | { |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 494 | struct coda_ctx *ctx = fh_to_ctx(priv); |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame^] | 495 | struct coda_codec *codec = NULL; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 496 | |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame^] | 497 | /* Determine codec by the encoded format */ |
| 498 | codec = coda_find_codec(ctx->dev, V4L2_PIX_FMT_YUV420, |
| 499 | f->fmt.pix.pixelformat); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 500 | |
| 501 | f->fmt.pix.colorspace = ctx->colorspace; |
| 502 | |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame^] | 503 | return vidioc_try_fmt(codec, f); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | static int vidioc_try_fmt_vid_out(struct file *file, void *priv, |
| 507 | struct v4l2_format *f) |
| 508 | { |
| 509 | struct coda_ctx *ctx = fh_to_ctx(priv); |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame^] | 510 | struct coda_codec *codec; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 511 | |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame^] | 512 | /* Determine codec by encoded format, returns NULL if raw or invalid */ |
| 513 | codec = coda_find_codec(ctx->dev, f->fmt.pix.pixelformat, |
| 514 | V4L2_PIX_FMT_YUV420); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 515 | |
| 516 | if (!f->fmt.pix.colorspace) |
| 517 | f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709; |
| 518 | |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame^] | 519 | return vidioc_try_fmt(codec, f); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | static int vidioc_s_fmt(struct coda_ctx *ctx, struct v4l2_format *f) |
| 523 | { |
| 524 | struct coda_q_data *q_data; |
| 525 | struct vb2_queue *vq; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 526 | |
| 527 | vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type); |
| 528 | if (!vq) |
| 529 | return -EINVAL; |
| 530 | |
| 531 | q_data = get_q_data(ctx, f->type); |
| 532 | if (!q_data) |
| 533 | return -EINVAL; |
| 534 | |
| 535 | if (vb2_is_busy(vq)) { |
| 536 | v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__); |
| 537 | return -EBUSY; |
| 538 | } |
| 539 | |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame^] | 540 | q_data->fourcc = f->fmt.pix.pixelformat; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 541 | q_data->width = f->fmt.pix.width; |
| 542 | q_data->height = f->fmt.pix.height; |
Philipp Zabel | 451d43a | 2012-07-26 09:18:27 -0300 | [diff] [blame] | 543 | q_data->sizeimage = f->fmt.pix.sizeimage; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 544 | |
| 545 | v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, |
| 546 | "Setting format for type %d, wxh: %dx%d, fmt: %d\n", |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame^] | 547 | f->type, q_data->width, q_data->height, q_data->fourcc); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 548 | |
| 549 | return 0; |
| 550 | } |
| 551 | |
| 552 | static int vidioc_s_fmt_vid_cap(struct file *file, void *priv, |
| 553 | struct v4l2_format *f) |
| 554 | { |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame^] | 555 | struct coda_ctx *ctx = fh_to_ctx(priv); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 556 | int ret; |
| 557 | |
| 558 | ret = vidioc_try_fmt_vid_cap(file, priv, f); |
| 559 | if (ret) |
| 560 | return ret; |
| 561 | |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame^] | 562 | return vidioc_s_fmt(ctx, f); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | static int vidioc_s_fmt_vid_out(struct file *file, void *priv, |
| 566 | struct v4l2_format *f) |
| 567 | { |
| 568 | struct coda_ctx *ctx = fh_to_ctx(priv); |
| 569 | int ret; |
| 570 | |
| 571 | ret = vidioc_try_fmt_vid_out(file, priv, f); |
| 572 | if (ret) |
| 573 | return ret; |
| 574 | |
Richard Zhao | 8d62147 | 2012-08-21 02:47:42 -0300 | [diff] [blame] | 575 | ret = vidioc_s_fmt(ctx, f); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 576 | if (ret) |
| 577 | ctx->colorspace = f->fmt.pix.colorspace; |
| 578 | |
| 579 | return ret; |
| 580 | } |
| 581 | |
| 582 | static int vidioc_reqbufs(struct file *file, void *priv, |
| 583 | struct v4l2_requestbuffers *reqbufs) |
| 584 | { |
| 585 | struct coda_ctx *ctx = fh_to_ctx(priv); |
| 586 | |
| 587 | return v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs); |
| 588 | } |
| 589 | |
| 590 | static int vidioc_querybuf(struct file *file, void *priv, |
| 591 | struct v4l2_buffer *buf) |
| 592 | { |
| 593 | struct coda_ctx *ctx = fh_to_ctx(priv); |
| 594 | |
| 595 | return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf); |
| 596 | } |
| 597 | |
| 598 | static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf) |
| 599 | { |
| 600 | struct coda_ctx *ctx = fh_to_ctx(priv); |
| 601 | |
| 602 | return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf); |
| 603 | } |
| 604 | |
Philipp Zabel | 419869c | 2013-05-23 07:06:30 -0300 | [diff] [blame] | 605 | static int vidioc_expbuf(struct file *file, void *priv, |
| 606 | struct v4l2_exportbuffer *eb) |
| 607 | { |
| 608 | struct coda_ctx *ctx = fh_to_ctx(priv); |
| 609 | |
| 610 | return v4l2_m2m_expbuf(file, ctx->m2m_ctx, eb); |
| 611 | } |
| 612 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 613 | static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf) |
| 614 | { |
| 615 | struct coda_ctx *ctx = fh_to_ctx(priv); |
| 616 | |
| 617 | return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf); |
| 618 | } |
| 619 | |
| 620 | static int vidioc_streamon(struct file *file, void *priv, |
| 621 | enum v4l2_buf_type type) |
| 622 | { |
| 623 | struct coda_ctx *ctx = fh_to_ctx(priv); |
| 624 | |
| 625 | return v4l2_m2m_streamon(file, ctx->m2m_ctx, type); |
| 626 | } |
| 627 | |
| 628 | static int vidioc_streamoff(struct file *file, void *priv, |
| 629 | enum v4l2_buf_type type) |
| 630 | { |
| 631 | struct coda_ctx *ctx = fh_to_ctx(priv); |
| 632 | |
| 633 | return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type); |
| 634 | } |
| 635 | |
| 636 | static const struct v4l2_ioctl_ops coda_ioctl_ops = { |
| 637 | .vidioc_querycap = vidioc_querycap, |
| 638 | |
| 639 | .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap, |
| 640 | .vidioc_g_fmt_vid_cap = vidioc_g_fmt, |
| 641 | .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap, |
| 642 | .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap, |
| 643 | |
| 644 | .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out, |
| 645 | .vidioc_g_fmt_vid_out = vidioc_g_fmt, |
| 646 | .vidioc_try_fmt_vid_out = vidioc_try_fmt_vid_out, |
| 647 | .vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out, |
| 648 | |
| 649 | .vidioc_reqbufs = vidioc_reqbufs, |
| 650 | .vidioc_querybuf = vidioc_querybuf, |
| 651 | |
| 652 | .vidioc_qbuf = vidioc_qbuf, |
Philipp Zabel | 419869c | 2013-05-23 07:06:30 -0300 | [diff] [blame] | 653 | .vidioc_expbuf = vidioc_expbuf, |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 654 | .vidioc_dqbuf = vidioc_dqbuf, |
| 655 | |
| 656 | .vidioc_streamon = vidioc_streamon, |
| 657 | .vidioc_streamoff = vidioc_streamoff, |
| 658 | }; |
| 659 | |
| 660 | /* |
| 661 | * Mem-to-mem operations. |
| 662 | */ |
| 663 | static void coda_device_run(void *m2m_priv) |
| 664 | { |
| 665 | struct coda_ctx *ctx = m2m_priv; |
| 666 | struct coda_q_data *q_data_src, *q_data_dst; |
| 667 | struct vb2_buffer *src_buf, *dst_buf; |
| 668 | struct coda_dev *dev = ctx->dev; |
| 669 | int force_ipicture; |
| 670 | int quant_param = 0; |
| 671 | u32 picture_y, picture_cb, picture_cr; |
| 672 | u32 pic_stream_buffer_addr, pic_stream_buffer_size; |
| 673 | u32 dst_fourcc; |
| 674 | |
| 675 | src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx); |
| 676 | dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx); |
| 677 | q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT); |
| 678 | q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE); |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame^] | 679 | dst_fourcc = q_data_dst->fourcc; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 680 | |
| 681 | src_buf->v4l2_buf.sequence = ctx->isequence; |
| 682 | dst_buf->v4l2_buf.sequence = ctx->isequence; |
| 683 | ctx->isequence++; |
| 684 | |
| 685 | /* |
| 686 | * Workaround coda firmware BUG that only marks the first |
| 687 | * frame as IDR. This is a problem for some decoders that can't |
| 688 | * recover when a frame is lost. |
| 689 | */ |
| 690 | if (src_buf->v4l2_buf.sequence % ctx->params.gop_size) { |
| 691 | src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME; |
| 692 | src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME; |
| 693 | } else { |
| 694 | src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME; |
| 695 | src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME; |
| 696 | } |
| 697 | |
| 698 | /* |
| 699 | * Copy headers at the beginning of the first frame for H.264 only. |
| 700 | * In MPEG4 they are already copied by the coda. |
| 701 | */ |
| 702 | if (src_buf->v4l2_buf.sequence == 0) { |
| 703 | pic_stream_buffer_addr = |
| 704 | vb2_dma_contig_plane_dma_addr(dst_buf, 0) + |
| 705 | ctx->vpu_header_size[0] + |
| 706 | ctx->vpu_header_size[1] + |
| 707 | ctx->vpu_header_size[2]; |
| 708 | pic_stream_buffer_size = CODA_MAX_FRAME_SIZE - |
| 709 | ctx->vpu_header_size[0] - |
| 710 | ctx->vpu_header_size[1] - |
| 711 | ctx->vpu_header_size[2]; |
| 712 | memcpy(vb2_plane_vaddr(dst_buf, 0), |
| 713 | &ctx->vpu_header[0][0], ctx->vpu_header_size[0]); |
| 714 | memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0], |
| 715 | &ctx->vpu_header[1][0], ctx->vpu_header_size[1]); |
| 716 | memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0] + |
| 717 | ctx->vpu_header_size[1], &ctx->vpu_header[2][0], |
| 718 | ctx->vpu_header_size[2]); |
| 719 | } else { |
| 720 | pic_stream_buffer_addr = |
| 721 | vb2_dma_contig_plane_dma_addr(dst_buf, 0); |
| 722 | pic_stream_buffer_size = CODA_MAX_FRAME_SIZE; |
| 723 | } |
| 724 | |
| 725 | if (src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) { |
| 726 | force_ipicture = 1; |
| 727 | switch (dst_fourcc) { |
| 728 | case V4L2_PIX_FMT_H264: |
| 729 | quant_param = ctx->params.h264_intra_qp; |
| 730 | break; |
| 731 | case V4L2_PIX_FMT_MPEG4: |
| 732 | quant_param = ctx->params.mpeg4_intra_qp; |
| 733 | break; |
| 734 | default: |
| 735 | v4l2_warn(&ctx->dev->v4l2_dev, |
| 736 | "cannot set intra qp, fmt not supported\n"); |
| 737 | break; |
| 738 | } |
| 739 | } else { |
| 740 | force_ipicture = 0; |
| 741 | switch (dst_fourcc) { |
| 742 | case V4L2_PIX_FMT_H264: |
| 743 | quant_param = ctx->params.h264_inter_qp; |
| 744 | break; |
| 745 | case V4L2_PIX_FMT_MPEG4: |
| 746 | quant_param = ctx->params.mpeg4_inter_qp; |
| 747 | break; |
| 748 | default: |
| 749 | v4l2_warn(&ctx->dev->v4l2_dev, |
| 750 | "cannot set inter qp, fmt not supported\n"); |
| 751 | break; |
| 752 | } |
| 753 | } |
| 754 | |
| 755 | /* submit */ |
Philipp Zabel | 8f35c7b | 2012-07-09 04:25:52 -0300 | [diff] [blame] | 756 | coda_write(dev, CODA_ROT_MIR_ENABLE | ctx->params.rot_mode, CODA_CMD_ENC_PIC_ROT_MODE); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 757 | coda_write(dev, quant_param, CODA_CMD_ENC_PIC_QS); |
| 758 | |
| 759 | |
| 760 | picture_y = vb2_dma_contig_plane_dma_addr(src_buf, 0); |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame^] | 761 | switch (q_data_src->fourcc) { |
| 762 | case V4L2_PIX_FMT_YVU420: |
| 763 | /* Switch Cb and Cr for YVU420 format */ |
| 764 | picture_cr = picture_y + q_data_src->width * q_data_src->height; |
| 765 | picture_cb = picture_cr + q_data_src->width / 2 * |
| 766 | q_data_src->height / 2; |
| 767 | break; |
| 768 | case V4L2_PIX_FMT_YUV420: |
| 769 | default: |
| 770 | picture_cb = picture_y + q_data_src->width * q_data_src->height; |
| 771 | picture_cr = picture_cb + q_data_src->width / 2 * |
| 772 | q_data_src->height / 2; |
| 773 | break; |
| 774 | } |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 775 | |
| 776 | coda_write(dev, picture_y, CODA_CMD_ENC_PIC_SRC_ADDR_Y); |
| 777 | coda_write(dev, picture_cb, CODA_CMD_ENC_PIC_SRC_ADDR_CB); |
| 778 | coda_write(dev, picture_cr, CODA_CMD_ENC_PIC_SRC_ADDR_CR); |
| 779 | coda_write(dev, force_ipicture << 1 & 0x2, |
| 780 | CODA_CMD_ENC_PIC_OPTION); |
| 781 | |
| 782 | coda_write(dev, pic_stream_buffer_addr, CODA_CMD_ENC_PIC_BB_START); |
| 783 | coda_write(dev, pic_stream_buffer_size / 1024, |
| 784 | CODA_CMD_ENC_PIC_BB_SIZE); |
Philipp Zabel | 1043667 | 2012-07-02 09:03:55 -0300 | [diff] [blame] | 785 | |
| 786 | if (dev->devtype->product == CODA_7541) { |
| 787 | coda_write(dev, CODA7_USE_BIT_ENABLE | CODA7_USE_HOST_BIT_ENABLE | |
| 788 | CODA7_USE_ME_ENABLE | CODA7_USE_HOST_ME_ENABLE, |
| 789 | CODA7_REG_BIT_AXI_SRAM_USE); |
| 790 | } |
| 791 | |
Philipp Zabel | 2fb57f0 | 2012-07-25 09:22:07 -0300 | [diff] [blame] | 792 | /* 1 second timeout in case CODA locks up */ |
| 793 | schedule_delayed_work(&dev->timeout, HZ); |
| 794 | |
Philipp Zabel | 62bed14 | 2012-07-25 10:40:39 -0300 | [diff] [blame] | 795 | INIT_COMPLETION(dev->done); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 796 | coda_command_async(ctx, CODA_COMMAND_PIC_RUN); |
| 797 | } |
| 798 | |
| 799 | static int coda_job_ready(void *m2m_priv) |
| 800 | { |
| 801 | struct coda_ctx *ctx = m2m_priv; |
| 802 | |
| 803 | /* |
| 804 | * For both 'P' and 'key' frame cases 1 picture |
| 805 | * and 1 frame are needed. |
| 806 | */ |
| 807 | if (!v4l2_m2m_num_src_bufs_ready(ctx->m2m_ctx) || |
| 808 | !v4l2_m2m_num_dst_bufs_ready(ctx->m2m_ctx)) { |
| 809 | v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, |
| 810 | "not ready: not enough video buffers.\n"); |
| 811 | return 0; |
| 812 | } |
| 813 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 814 | v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, |
| 815 | "job ready\n"); |
| 816 | return 1; |
| 817 | } |
| 818 | |
| 819 | static void coda_job_abort(void *priv) |
| 820 | { |
| 821 | struct coda_ctx *ctx = priv; |
| 822 | struct coda_dev *dev = ctx->dev; |
| 823 | |
| 824 | ctx->aborting = 1; |
| 825 | |
| 826 | v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, |
| 827 | "Aborting task\n"); |
| 828 | |
| 829 | v4l2_m2m_job_finish(dev->m2m_dev, ctx->m2m_ctx); |
| 830 | } |
| 831 | |
| 832 | static void coda_lock(void *m2m_priv) |
| 833 | { |
| 834 | struct coda_ctx *ctx = m2m_priv; |
| 835 | struct coda_dev *pcdev = ctx->dev; |
| 836 | mutex_lock(&pcdev->dev_mutex); |
| 837 | } |
| 838 | |
| 839 | static void coda_unlock(void *m2m_priv) |
| 840 | { |
| 841 | struct coda_ctx *ctx = m2m_priv; |
| 842 | struct coda_dev *pcdev = ctx->dev; |
| 843 | mutex_unlock(&pcdev->dev_mutex); |
| 844 | } |
| 845 | |
| 846 | static struct v4l2_m2m_ops coda_m2m_ops = { |
| 847 | .device_run = coda_device_run, |
| 848 | .job_ready = coda_job_ready, |
| 849 | .job_abort = coda_job_abort, |
| 850 | .lock = coda_lock, |
| 851 | .unlock = coda_unlock, |
| 852 | }; |
| 853 | |
| 854 | static void set_default_params(struct coda_ctx *ctx) |
| 855 | { |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame^] | 856 | int max_w; |
| 857 | int max_h; |
| 858 | |
| 859 | ctx->codec = &ctx->dev->devtype->codecs[0]; |
| 860 | max_w = ctx->codec->max_w; |
| 861 | max_h = ctx->codec->max_h; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 862 | |
| 863 | ctx->params.codec_mode = CODA_MODE_INVALID; |
| 864 | ctx->colorspace = V4L2_COLORSPACE_REC709; |
| 865 | ctx->params.framerate = 30; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 866 | ctx->aborting = 0; |
| 867 | |
| 868 | /* Default formats for output and input queues */ |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame^] | 869 | ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->codec->src_fourcc; |
| 870 | ctx->q_data[V4L2_M2M_DST].fourcc = ctx->codec->dst_fourcc; |
| 871 | ctx->q_data[V4L2_M2M_SRC].width = max_w; |
| 872 | ctx->q_data[V4L2_M2M_SRC].height = max_h; |
| 873 | ctx->q_data[V4L2_M2M_SRC].sizeimage = (max_w * max_h * 3) / 2; |
| 874 | ctx->q_data[V4L2_M2M_DST].width = max_w; |
| 875 | ctx->q_data[V4L2_M2M_DST].height = max_h; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 876 | ctx->q_data[V4L2_M2M_DST].sizeimage = CODA_MAX_FRAME_SIZE; |
| 877 | } |
| 878 | |
| 879 | /* |
| 880 | * Queue operations |
| 881 | */ |
| 882 | static int coda_queue_setup(struct vb2_queue *vq, |
| 883 | const struct v4l2_format *fmt, |
| 884 | unsigned int *nbuffers, unsigned int *nplanes, |
| 885 | unsigned int sizes[], void *alloc_ctxs[]) |
| 886 | { |
| 887 | struct coda_ctx *ctx = vb2_get_drv_priv(vq); |
Philipp Zabel | e34db06 | 2012-08-29 08:22:00 -0300 | [diff] [blame] | 888 | struct coda_q_data *q_data; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 889 | unsigned int size; |
| 890 | |
Philipp Zabel | e34db06 | 2012-08-29 08:22:00 -0300 | [diff] [blame] | 891 | q_data = get_q_data(ctx, vq->type); |
| 892 | size = q_data->sizeimage; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 893 | |
| 894 | *nplanes = 1; |
| 895 | sizes[0] = size; |
| 896 | |
| 897 | alloc_ctxs[0] = ctx->dev->alloc_ctx; |
| 898 | |
| 899 | v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, |
| 900 | "get %d buffer(s) of size %d each.\n", *nbuffers, size); |
| 901 | |
| 902 | return 0; |
| 903 | } |
| 904 | |
| 905 | static int coda_buf_prepare(struct vb2_buffer *vb) |
| 906 | { |
| 907 | struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); |
| 908 | struct coda_q_data *q_data; |
| 909 | |
| 910 | q_data = get_q_data(ctx, vb->vb2_queue->type); |
| 911 | |
| 912 | if (vb2_plane_size(vb, 0) < q_data->sizeimage) { |
| 913 | v4l2_warn(&ctx->dev->v4l2_dev, |
| 914 | "%s data will not fit into plane (%lu < %lu)\n", |
| 915 | __func__, vb2_plane_size(vb, 0), |
| 916 | (long)q_data->sizeimage); |
| 917 | return -EINVAL; |
| 918 | } |
| 919 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 920 | return 0; |
| 921 | } |
| 922 | |
| 923 | static void coda_buf_queue(struct vb2_buffer *vb) |
| 924 | { |
| 925 | struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); |
| 926 | v4l2_m2m_buf_queue(ctx->m2m_ctx, vb); |
| 927 | } |
| 928 | |
| 929 | static void coda_wait_prepare(struct vb2_queue *q) |
| 930 | { |
| 931 | struct coda_ctx *ctx = vb2_get_drv_priv(q); |
| 932 | coda_unlock(ctx); |
| 933 | } |
| 934 | |
| 935 | static void coda_wait_finish(struct vb2_queue *q) |
| 936 | { |
| 937 | struct coda_ctx *ctx = vb2_get_drv_priv(q); |
| 938 | coda_lock(ctx); |
| 939 | } |
| 940 | |
Philipp Zabel | ec25f68 | 2012-07-20 08:54:29 -0300 | [diff] [blame] | 941 | static void coda_free_framebuffers(struct coda_ctx *ctx) |
| 942 | { |
| 943 | int i; |
| 944 | |
| 945 | for (i = 0; i < CODA_MAX_FRAMEBUFFERS; i++) { |
| 946 | if (ctx->internal_frames[i].vaddr) { |
| 947 | dma_free_coherent(&ctx->dev->plat_dev->dev, |
| 948 | ctx->internal_frames[i].size, |
| 949 | ctx->internal_frames[i].vaddr, |
| 950 | ctx->internal_frames[i].paddr); |
| 951 | ctx->internal_frames[i].vaddr = NULL; |
| 952 | } |
| 953 | } |
| 954 | } |
| 955 | |
Philipp Zabel | 86eda90 | 2013-05-23 10:42:57 -0300 | [diff] [blame] | 956 | static void coda_parabuf_write(struct coda_ctx *ctx, int index, u32 value) |
| 957 | { |
| 958 | struct coda_dev *dev = ctx->dev; |
| 959 | u32 *p = ctx->parabuf.vaddr; |
| 960 | |
| 961 | if (dev->devtype->product == CODA_DX6) |
| 962 | p[index] = value; |
| 963 | else |
| 964 | p[index ^ 1] = value; |
| 965 | } |
| 966 | |
Philipp Zabel | ec25f68 | 2012-07-20 08:54:29 -0300 | [diff] [blame] | 967 | static int coda_alloc_framebuffers(struct coda_ctx *ctx, struct coda_q_data *q_data, u32 fourcc) |
| 968 | { |
| 969 | struct coda_dev *dev = ctx->dev; |
| 970 | |
| 971 | int height = q_data->height; |
Philipp Zabel | 86eda90 | 2013-05-23 10:42:57 -0300 | [diff] [blame] | 972 | dma_addr_t paddr; |
| 973 | int ysize; |
Philipp Zabel | ec25f68 | 2012-07-20 08:54:29 -0300 | [diff] [blame] | 974 | int i; |
| 975 | |
Philipp Zabel | 86eda90 | 2013-05-23 10:42:57 -0300 | [diff] [blame] | 976 | ysize = round_up(q_data->width, 8) * height; |
| 977 | |
Philipp Zabel | ec25f68 | 2012-07-20 08:54:29 -0300 | [diff] [blame] | 978 | /* Allocate frame buffers */ |
| 979 | ctx->num_internal_frames = CODA_MAX_FRAMEBUFFERS; |
| 980 | for (i = 0; i < ctx->num_internal_frames; i++) { |
| 981 | ctx->internal_frames[i].size = q_data->sizeimage; |
| 982 | if (fourcc == V4L2_PIX_FMT_H264 && dev->devtype->product != CODA_DX6) |
Philipp Zabel | 86eda90 | 2013-05-23 10:42:57 -0300 | [diff] [blame] | 983 | ctx->internal_frames[i].size += ysize/4; |
Philipp Zabel | ec25f68 | 2012-07-20 08:54:29 -0300 | [diff] [blame] | 984 | ctx->internal_frames[i].vaddr = dma_alloc_coherent( |
| 985 | &dev->plat_dev->dev, ctx->internal_frames[i].size, |
| 986 | &ctx->internal_frames[i].paddr, GFP_KERNEL); |
| 987 | if (!ctx->internal_frames[i].vaddr) { |
| 988 | coda_free_framebuffers(ctx); |
| 989 | return -ENOMEM; |
| 990 | } |
| 991 | } |
| 992 | |
| 993 | /* Register frame buffers in the parameter buffer */ |
Philipp Zabel | 86eda90 | 2013-05-23 10:42:57 -0300 | [diff] [blame] | 994 | for (i = 0; i < ctx->num_internal_frames; i++) { |
| 995 | paddr = ctx->internal_frames[i].paddr; |
| 996 | coda_parabuf_write(ctx, i * 3 + 0, paddr); /* Y */ |
| 997 | coda_parabuf_write(ctx, i * 3 + 1, paddr + ysize); /* Cb */ |
| 998 | coda_parabuf_write(ctx, i * 3 + 2, paddr + ysize + ysize/4); /* Cr */ |
Philipp Zabel | ec25f68 | 2012-07-20 08:54:29 -0300 | [diff] [blame] | 999 | |
Philipp Zabel | 86eda90 | 2013-05-23 10:42:57 -0300 | [diff] [blame] | 1000 | if (dev->devtype->product != CODA_DX6 && fourcc == V4L2_PIX_FMT_H264) |
| 1001 | coda_parabuf_write(ctx, 96 + i, ctx->internal_frames[i].paddr + ysize + ysize/4 + ysize/4); |
Philipp Zabel | ec25f68 | 2012-07-20 08:54:29 -0300 | [diff] [blame] | 1002 | } |
| 1003 | |
| 1004 | return 0; |
| 1005 | } |
| 1006 | |
Javier Martin | 3f3f5c7 | 2012-10-29 05:20:29 -0300 | [diff] [blame] | 1007 | static int coda_h264_padding(int size, char *p) |
| 1008 | { |
Javier Martin | 3f3f5c7 | 2012-10-29 05:20:29 -0300 | [diff] [blame] | 1009 | int nal_size; |
| 1010 | int diff; |
| 1011 | |
Javier Martin | 832fbb5 | 2012-10-29 08:34:59 -0300 | [diff] [blame] | 1012 | diff = size - (size & ~0x7); |
Javier Martin | 3f3f5c7 | 2012-10-29 05:20:29 -0300 | [diff] [blame] | 1013 | if (diff == 0) |
| 1014 | return 0; |
| 1015 | |
Javier Martin | 832fbb5 | 2012-10-29 08:34:59 -0300 | [diff] [blame] | 1016 | nal_size = coda_filler_size[diff]; |
Javier Martin | 3f3f5c7 | 2012-10-29 05:20:29 -0300 | [diff] [blame] | 1017 | memcpy(p, coda_filler_nal, nal_size); |
| 1018 | |
| 1019 | /* Add rbsp stop bit and trailing at the end */ |
| 1020 | *(p + nal_size - 1) = 0x80; |
| 1021 | |
| 1022 | return nal_size; |
| 1023 | } |
| 1024 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1025 | static int coda_start_streaming(struct vb2_queue *q, unsigned int count) |
| 1026 | { |
| 1027 | struct coda_ctx *ctx = vb2_get_drv_priv(q); |
| 1028 | struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev; |
| 1029 | u32 bitstream_buf, bitstream_size; |
| 1030 | struct coda_dev *dev = ctx->dev; |
| 1031 | struct coda_q_data *q_data_src, *q_data_dst; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1032 | struct vb2_buffer *buf; |
Philipp Zabel | ec25f68 | 2012-07-20 08:54:29 -0300 | [diff] [blame] | 1033 | u32 dst_fourcc; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1034 | u32 value; |
Philipp Zabel | ec25f68 | 2012-07-20 08:54:29 -0300 | [diff] [blame] | 1035 | int ret; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1036 | |
| 1037 | if (count < 1) |
| 1038 | return -EINVAL; |
| 1039 | |
| 1040 | if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame^] | 1041 | ctx->streamon_out = 1; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1042 | else |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame^] | 1043 | ctx->streamon_cap = 1; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1044 | |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame^] | 1045 | q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT); |
| 1046 | if (ctx->streamon_out) { |
| 1047 | if (coda_format_is_yuv(q_data_src->fourcc)) |
| 1048 | ctx->inst_type = CODA_INST_ENCODER; |
| 1049 | else |
| 1050 | ctx->inst_type = CODA_INST_DECODER; |
| 1051 | } |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1052 | |
Philipp Zabel | 62bed14 | 2012-07-25 10:40:39 -0300 | [diff] [blame] | 1053 | if (coda_isbusy(dev)) |
| 1054 | if (wait_for_completion_interruptible_timeout(&dev->done, HZ) <= 0) |
| 1055 | return -EBUSY; |
| 1056 | |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame^] | 1057 | /* Don't start the coda unless both queues are on */ |
| 1058 | if (!(ctx->streamon_out & ctx->streamon_cap)) |
| 1059 | return 0; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1060 | |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame^] | 1061 | ctx->gopcounter = ctx->params.gop_size - 1; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1062 | buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx); |
| 1063 | bitstream_buf = vb2_dma_contig_plane_dma_addr(buf, 0); |
| 1064 | q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE); |
| 1065 | bitstream_size = q_data_dst->sizeimage; |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame^] | 1066 | dst_fourcc = q_data_dst->fourcc; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1067 | |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame^] | 1068 | ctx->codec = coda_find_codec(ctx->dev, q_data_src->fourcc, |
| 1069 | q_data_dst->fourcc); |
| 1070 | if (!ctx->codec) { |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1071 | v4l2_err(v4l2_dev, "couldn't tell instance type.\n"); |
| 1072 | return -EINVAL; |
| 1073 | } |
| 1074 | |
| 1075 | if (!coda_is_initialized(dev)) { |
| 1076 | v4l2_err(v4l2_dev, "coda is not initialized.\n"); |
| 1077 | return -EFAULT; |
| 1078 | } |
| 1079 | coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR); |
| 1080 | coda_write(dev, bitstream_buf, CODA_REG_BIT_RD_PTR(ctx->idx)); |
| 1081 | coda_write(dev, bitstream_buf, CODA_REG_BIT_WR_PTR(ctx->idx)); |
| 1082 | switch (dev->devtype->product) { |
| 1083 | case CODA_DX6: |
| 1084 | coda_write(dev, CODADX6_STREAM_BUF_DYNALLOC_EN | |
| 1085 | CODADX6_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL); |
| 1086 | break; |
| 1087 | default: |
| 1088 | coda_write(dev, CODA7_STREAM_BUF_DYNALLOC_EN | |
| 1089 | CODA7_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL); |
| 1090 | } |
| 1091 | |
Philipp Zabel | 1043667 | 2012-07-02 09:03:55 -0300 | [diff] [blame] | 1092 | if (dev->devtype->product == CODA_DX6) { |
| 1093 | /* Configure the coda */ |
| 1094 | coda_write(dev, dev->iram_paddr, CODADX6_REG_BIT_SEARCH_RAM_BASE_ADDR); |
| 1095 | } |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1096 | |
| 1097 | /* Could set rotation here if needed */ |
| 1098 | switch (dev->devtype->product) { |
| 1099 | case CODA_DX6: |
| 1100 | value = (q_data_src->width & CODADX6_PICWIDTH_MASK) << CODADX6_PICWIDTH_OFFSET; |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame^] | 1101 | value |= (q_data_src->height & CODADX6_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1102 | break; |
| 1103 | default: |
| 1104 | value = (q_data_src->width & CODA7_PICWIDTH_MASK) << CODA7_PICWIDTH_OFFSET; |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame^] | 1105 | value |= (q_data_src->height & CODA7_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1106 | } |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1107 | coda_write(dev, value, CODA_CMD_ENC_SEQ_SRC_SIZE); |
| 1108 | coda_write(dev, ctx->params.framerate, |
| 1109 | CODA_CMD_ENC_SEQ_SRC_F_RATE); |
| 1110 | |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame^] | 1111 | ctx->params.codec_mode = ctx->codec->mode; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1112 | switch (dst_fourcc) { |
| 1113 | case V4L2_PIX_FMT_MPEG4: |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1114 | coda_write(dev, CODA_STD_MPEG4, CODA_CMD_ENC_SEQ_COD_STD); |
| 1115 | coda_write(dev, 0, CODA_CMD_ENC_SEQ_MP4_PARA); |
| 1116 | break; |
| 1117 | case V4L2_PIX_FMT_H264: |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1118 | coda_write(dev, CODA_STD_H264, CODA_CMD_ENC_SEQ_COD_STD); |
| 1119 | coda_write(dev, 0, CODA_CMD_ENC_SEQ_264_PARA); |
| 1120 | break; |
| 1121 | default: |
| 1122 | v4l2_err(v4l2_dev, |
| 1123 | "dst format (0x%08x) invalid.\n", dst_fourcc); |
| 1124 | return -EINVAL; |
| 1125 | } |
| 1126 | |
Philipp Zabel | c566c78 | 2012-08-08 11:59:38 -0300 | [diff] [blame] | 1127 | switch (ctx->params.slice_mode) { |
| 1128 | case V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE: |
| 1129 | value = 0; |
| 1130 | break; |
| 1131 | case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_MB: |
| 1132 | value = (ctx->params.slice_max_mb & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET; |
| 1133 | value |= (1 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1134 | value |= 1 & CODA_SLICING_MODE_MASK; |
Philipp Zabel | c566c78 | 2012-08-08 11:59:38 -0300 | [diff] [blame] | 1135 | break; |
| 1136 | case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES: |
| 1137 | value = (ctx->params.slice_max_bits & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET; |
| 1138 | value |= (0 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET; |
| 1139 | value |= 1 & CODA_SLICING_MODE_MASK; |
| 1140 | break; |
| 1141 | } |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1142 | coda_write(dev, value, CODA_CMD_ENC_SEQ_SLICE_MODE); |
Philipp Zabel | c566c78 | 2012-08-08 11:59:38 -0300 | [diff] [blame] | 1143 | value = ctx->params.gop_size & CODA_GOP_SIZE_MASK; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1144 | coda_write(dev, value, CODA_CMD_ENC_SEQ_GOP_SIZE); |
| 1145 | |
| 1146 | if (ctx->params.bitrate) { |
| 1147 | /* Rate control enabled */ |
| 1148 | value = (ctx->params.bitrate & CODA_RATECONTROL_BITRATE_MASK) << CODA_RATECONTROL_BITRATE_OFFSET; |
| 1149 | value |= 1 & CODA_RATECONTROL_ENABLE_MASK; |
| 1150 | } else { |
| 1151 | value = 0; |
| 1152 | } |
| 1153 | coda_write(dev, value, CODA_CMD_ENC_SEQ_RC_PARA); |
| 1154 | |
| 1155 | coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_BUF_SIZE); |
| 1156 | coda_write(dev, 0, CODA_CMD_ENC_SEQ_INTRA_REFRESH); |
| 1157 | |
| 1158 | coda_write(dev, bitstream_buf, CODA_CMD_ENC_SEQ_BB_START); |
| 1159 | coda_write(dev, bitstream_size / 1024, CODA_CMD_ENC_SEQ_BB_SIZE); |
| 1160 | |
| 1161 | /* set default gamma */ |
| 1162 | value = (CODA_DEFAULT_GAMMA & CODA_GAMMA_MASK) << CODA_GAMMA_OFFSET; |
| 1163 | coda_write(dev, value, CODA_CMD_ENC_SEQ_RC_GAMMA); |
| 1164 | |
Philipp Zabel | fb1fcf1 | 2013-05-23 10:42:53 -0300 | [diff] [blame] | 1165 | if (CODA_DEFAULT_GAMMA > 0) { |
| 1166 | if (dev->devtype->product == CODA_DX6) |
| 1167 | value = 1 << CODADX6_OPTION_GAMMA_OFFSET; |
| 1168 | else |
| 1169 | value = 1 << CODA7_OPTION_GAMMA_OFFSET; |
| 1170 | } else { |
| 1171 | value = 0; |
| 1172 | } |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1173 | coda_write(dev, value, CODA_CMD_ENC_SEQ_OPTION); |
| 1174 | |
| 1175 | if (dst_fourcc == V4L2_PIX_FMT_H264) { |
| 1176 | value = (FMO_SLICE_SAVE_BUF_SIZE << 7); |
| 1177 | value |= (0 & CODA_FMOPARAM_TYPE_MASK) << CODA_FMOPARAM_TYPE_OFFSET; |
| 1178 | value |= 0 & CODA_FMOPARAM_SLICENUM_MASK; |
Philipp Zabel | 1043667 | 2012-07-02 09:03:55 -0300 | [diff] [blame] | 1179 | if (dev->devtype->product == CODA_DX6) { |
| 1180 | coda_write(dev, value, CODADX6_CMD_ENC_SEQ_FMO); |
| 1181 | } else { |
| 1182 | coda_write(dev, dev->iram_paddr, CODA7_CMD_ENC_SEQ_SEARCH_BASE); |
| 1183 | coda_write(dev, 48 * 1024, CODA7_CMD_ENC_SEQ_SEARCH_SIZE); |
| 1184 | } |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1185 | } |
| 1186 | |
| 1187 | if (coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT)) { |
| 1188 | v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n"); |
| 1189 | return -ETIMEDOUT; |
| 1190 | } |
| 1191 | |
| 1192 | if (coda_read(dev, CODA_RET_ENC_SEQ_SUCCESS) == 0) |
| 1193 | return -EFAULT; |
| 1194 | |
Philipp Zabel | ec25f68 | 2012-07-20 08:54:29 -0300 | [diff] [blame] | 1195 | ret = coda_alloc_framebuffers(ctx, q_data_src, dst_fourcc); |
| 1196 | if (ret < 0) |
| 1197 | return ret; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1198 | |
Philipp Zabel | ec25f68 | 2012-07-20 08:54:29 -0300 | [diff] [blame] | 1199 | coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM); |
Philipp Zabel | 1043667 | 2012-07-02 09:03:55 -0300 | [diff] [blame] | 1200 | coda_write(dev, round_up(q_data_src->width, 8), CODA_CMD_SET_FRAME_BUF_STRIDE); |
| 1201 | if (dev->devtype->product != CODA_DX6) { |
| 1202 | coda_write(dev, round_up(q_data_src->width, 8), CODA7_CMD_SET_FRAME_SOURCE_BUF_STRIDE); |
| 1203 | coda_write(dev, dev->iram_paddr + 48 * 1024, CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR); |
| 1204 | coda_write(dev, dev->iram_paddr + 53 * 1024, CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR); |
| 1205 | coda_write(dev, dev->iram_paddr + 58 * 1024, CODA7_CMD_SET_FRAME_AXI_BIT_ADDR); |
| 1206 | coda_write(dev, dev->iram_paddr + 68 * 1024, CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR); |
| 1207 | coda_write(dev, 0x0, CODA7_CMD_SET_FRAME_AXI_OVL_ADDR); |
| 1208 | } |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1209 | if (coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF)) { |
| 1210 | v4l2_err(v4l2_dev, "CODA_COMMAND_SET_FRAME_BUF timeout\n"); |
| 1211 | return -ETIMEDOUT; |
| 1212 | } |
| 1213 | |
| 1214 | /* Save stream headers */ |
| 1215 | buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx); |
| 1216 | switch (dst_fourcc) { |
| 1217 | case V4L2_PIX_FMT_H264: |
| 1218 | /* |
| 1219 | * Get SPS in the first frame and copy it to an |
| 1220 | * intermediate buffer. |
| 1221 | */ |
| 1222 | coda_write(dev, vb2_dma_contig_plane_dma_addr(buf, 0), CODA_CMD_ENC_HEADER_BB_START); |
| 1223 | coda_write(dev, bitstream_size, CODA_CMD_ENC_HEADER_BB_SIZE); |
| 1224 | coda_write(dev, CODA_HEADER_H264_SPS, CODA_CMD_ENC_HEADER_CODE); |
| 1225 | if (coda_command_sync(ctx, CODA_COMMAND_ENCODE_HEADER)) { |
| 1226 | v4l2_err(v4l2_dev, "CODA_COMMAND_ENCODE_HEADER timeout\n"); |
| 1227 | return -ETIMEDOUT; |
| 1228 | } |
| 1229 | ctx->vpu_header_size[0] = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->idx)) - |
| 1230 | coda_read(dev, CODA_CMD_ENC_HEADER_BB_START); |
| 1231 | memcpy(&ctx->vpu_header[0][0], vb2_plane_vaddr(buf, 0), |
| 1232 | ctx->vpu_header_size[0]); |
| 1233 | |
| 1234 | /* |
| 1235 | * Get PPS in the first frame and copy it to an |
| 1236 | * intermediate buffer. |
| 1237 | */ |
| 1238 | coda_write(dev, vb2_dma_contig_plane_dma_addr(buf, 0), CODA_CMD_ENC_HEADER_BB_START); |
| 1239 | coda_write(dev, bitstream_size, CODA_CMD_ENC_HEADER_BB_SIZE); |
| 1240 | coda_write(dev, CODA_HEADER_H264_PPS, CODA_CMD_ENC_HEADER_CODE); |
| 1241 | if (coda_command_sync(ctx, CODA_COMMAND_ENCODE_HEADER)) { |
| 1242 | v4l2_err(v4l2_dev, "CODA_COMMAND_ENCODE_HEADER timeout\n"); |
| 1243 | return -ETIMEDOUT; |
| 1244 | } |
| 1245 | ctx->vpu_header_size[1] = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->idx)) - |
| 1246 | coda_read(dev, CODA_CMD_ENC_HEADER_BB_START); |
| 1247 | memcpy(&ctx->vpu_header[1][0], vb2_plane_vaddr(buf, 0), |
| 1248 | ctx->vpu_header_size[1]); |
Javier Martin | 3f3f5c7 | 2012-10-29 05:20:29 -0300 | [diff] [blame] | 1249 | /* |
| 1250 | * Length of H.264 headers is variable and thus it might not be |
| 1251 | * aligned for the coda to append the encoded frame. In that is |
| 1252 | * the case a filler NAL must be added to header 2. |
| 1253 | */ |
| 1254 | ctx->vpu_header_size[2] = coda_h264_padding( |
| 1255 | (ctx->vpu_header_size[0] + |
| 1256 | ctx->vpu_header_size[1]), |
| 1257 | ctx->vpu_header[2]); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1258 | break; |
| 1259 | case V4L2_PIX_FMT_MPEG4: |
| 1260 | /* |
| 1261 | * Get VOS in the first frame and copy it to an |
| 1262 | * intermediate buffer |
| 1263 | */ |
| 1264 | coda_write(dev, vb2_dma_contig_plane_dma_addr(buf, 0), CODA_CMD_ENC_HEADER_BB_START); |
| 1265 | coda_write(dev, bitstream_size, CODA_CMD_ENC_HEADER_BB_SIZE); |
| 1266 | coda_write(dev, CODA_HEADER_MP4V_VOS, CODA_CMD_ENC_HEADER_CODE); |
| 1267 | if (coda_command_sync(ctx, CODA_COMMAND_ENCODE_HEADER)) { |
| 1268 | v4l2_err(v4l2_dev, "CODA_COMMAND_ENCODE_HEADER timeout\n"); |
| 1269 | return -ETIMEDOUT; |
| 1270 | } |
| 1271 | ctx->vpu_header_size[0] = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->idx)) - |
| 1272 | coda_read(dev, CODA_CMD_ENC_HEADER_BB_START); |
| 1273 | memcpy(&ctx->vpu_header[0][0], vb2_plane_vaddr(buf, 0), |
| 1274 | ctx->vpu_header_size[0]); |
| 1275 | |
| 1276 | coda_write(dev, vb2_dma_contig_plane_dma_addr(buf, 0), CODA_CMD_ENC_HEADER_BB_START); |
| 1277 | coda_write(dev, bitstream_size, CODA_CMD_ENC_HEADER_BB_SIZE); |
| 1278 | coda_write(dev, CODA_HEADER_MP4V_VIS, CODA_CMD_ENC_HEADER_CODE); |
| 1279 | if (coda_command_sync(ctx, CODA_COMMAND_ENCODE_HEADER)) { |
| 1280 | v4l2_err(v4l2_dev, "CODA_COMMAND_ENCODE_HEADER failed\n"); |
| 1281 | return -ETIMEDOUT; |
| 1282 | } |
| 1283 | ctx->vpu_header_size[1] = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->idx)) - |
| 1284 | coda_read(dev, CODA_CMD_ENC_HEADER_BB_START); |
| 1285 | memcpy(&ctx->vpu_header[1][0], vb2_plane_vaddr(buf, 0), |
| 1286 | ctx->vpu_header_size[1]); |
| 1287 | |
| 1288 | coda_write(dev, vb2_dma_contig_plane_dma_addr(buf, 0), CODA_CMD_ENC_HEADER_BB_START); |
| 1289 | coda_write(dev, bitstream_size, CODA_CMD_ENC_HEADER_BB_SIZE); |
| 1290 | coda_write(dev, CODA_HEADER_MP4V_VOL, CODA_CMD_ENC_HEADER_CODE); |
| 1291 | if (coda_command_sync(ctx, CODA_COMMAND_ENCODE_HEADER)) { |
| 1292 | v4l2_err(v4l2_dev, "CODA_COMMAND_ENCODE_HEADER failed\n"); |
| 1293 | return -ETIMEDOUT; |
| 1294 | } |
| 1295 | ctx->vpu_header_size[2] = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->idx)) - |
| 1296 | coda_read(dev, CODA_CMD_ENC_HEADER_BB_START); |
| 1297 | memcpy(&ctx->vpu_header[2][0], vb2_plane_vaddr(buf, 0), |
| 1298 | ctx->vpu_header_size[2]); |
| 1299 | break; |
| 1300 | default: |
| 1301 | /* No more formats need to save headers at the moment */ |
| 1302 | break; |
| 1303 | } |
| 1304 | |
| 1305 | return 0; |
| 1306 | } |
| 1307 | |
| 1308 | static int coda_stop_streaming(struct vb2_queue *q) |
| 1309 | { |
| 1310 | struct coda_ctx *ctx = vb2_get_drv_priv(q); |
Philipp Zabel | 2fb57f0 | 2012-07-25 09:22:07 -0300 | [diff] [blame] | 1311 | struct coda_dev *dev = ctx->dev; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1312 | |
| 1313 | if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) { |
| 1314 | v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, |
| 1315 | "%s: output\n", __func__); |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame^] | 1316 | ctx->streamon_out = 0; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1317 | } else { |
| 1318 | v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, |
| 1319 | "%s: capture\n", __func__); |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame^] | 1320 | ctx->streamon_cap = 0; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1321 | } |
| 1322 | |
Philipp Zabel | 62bed14 | 2012-07-25 10:40:39 -0300 | [diff] [blame] | 1323 | /* Don't stop the coda unless both queues are off */ |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame^] | 1324 | if (ctx->streamon_out || ctx->streamon_cap) |
Philipp Zabel | 62bed14 | 2012-07-25 10:40:39 -0300 | [diff] [blame] | 1325 | return 0; |
Philipp Zabel | 2fb57f0 | 2012-07-25 09:22:07 -0300 | [diff] [blame] | 1326 | |
Philipp Zabel | 62bed14 | 2012-07-25 10:40:39 -0300 | [diff] [blame] | 1327 | if (coda_isbusy(dev)) { |
| 1328 | if (wait_for_completion_interruptible_timeout(&dev->done, HZ) <= 0) { |
| 1329 | v4l2_warn(&dev->v4l2_dev, |
| 1330 | "%s: timeout, sending SEQ_END anyway\n", __func__); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1331 | } |
| 1332 | } |
| 1333 | |
Philipp Zabel | 62bed14 | 2012-07-25 10:40:39 -0300 | [diff] [blame] | 1334 | cancel_delayed_work(&dev->timeout); |
| 1335 | |
| 1336 | v4l2_dbg(1, coda_debug, &dev->v4l2_dev, |
| 1337 | "%s: sent command 'SEQ_END' to coda\n", __func__); |
| 1338 | if (coda_command_sync(ctx, CODA_COMMAND_SEQ_END)) { |
| 1339 | v4l2_err(&dev->v4l2_dev, |
| 1340 | "CODA_COMMAND_SEQ_END failed\n"); |
| 1341 | return -ETIMEDOUT; |
| 1342 | } |
| 1343 | |
| 1344 | coda_free_framebuffers(ctx); |
| 1345 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1346 | return 0; |
| 1347 | } |
| 1348 | |
| 1349 | static struct vb2_ops coda_qops = { |
| 1350 | .queue_setup = coda_queue_setup, |
| 1351 | .buf_prepare = coda_buf_prepare, |
| 1352 | .buf_queue = coda_buf_queue, |
| 1353 | .wait_prepare = coda_wait_prepare, |
| 1354 | .wait_finish = coda_wait_finish, |
| 1355 | .start_streaming = coda_start_streaming, |
| 1356 | .stop_streaming = coda_stop_streaming, |
| 1357 | }; |
| 1358 | |
| 1359 | static int coda_s_ctrl(struct v4l2_ctrl *ctrl) |
| 1360 | { |
| 1361 | struct coda_ctx *ctx = |
| 1362 | container_of(ctrl->handler, struct coda_ctx, ctrls); |
| 1363 | |
| 1364 | v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, |
| 1365 | "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val); |
| 1366 | |
| 1367 | switch (ctrl->id) { |
Philipp Zabel | 8f35c7b | 2012-07-09 04:25:52 -0300 | [diff] [blame] | 1368 | case V4L2_CID_HFLIP: |
| 1369 | if (ctrl->val) |
| 1370 | ctx->params.rot_mode |= CODA_MIR_HOR; |
| 1371 | else |
| 1372 | ctx->params.rot_mode &= ~CODA_MIR_HOR; |
| 1373 | break; |
| 1374 | case V4L2_CID_VFLIP: |
| 1375 | if (ctrl->val) |
| 1376 | ctx->params.rot_mode |= CODA_MIR_VER; |
| 1377 | else |
| 1378 | ctx->params.rot_mode &= ~CODA_MIR_VER; |
| 1379 | break; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1380 | case V4L2_CID_MPEG_VIDEO_BITRATE: |
| 1381 | ctx->params.bitrate = ctrl->val / 1000; |
| 1382 | break; |
| 1383 | case V4L2_CID_MPEG_VIDEO_GOP_SIZE: |
| 1384 | ctx->params.gop_size = ctrl->val; |
| 1385 | break; |
| 1386 | case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP: |
| 1387 | ctx->params.h264_intra_qp = ctrl->val; |
| 1388 | break; |
| 1389 | case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP: |
| 1390 | ctx->params.h264_inter_qp = ctrl->val; |
| 1391 | break; |
| 1392 | case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP: |
| 1393 | ctx->params.mpeg4_intra_qp = ctrl->val; |
| 1394 | break; |
| 1395 | case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP: |
| 1396 | ctx->params.mpeg4_inter_qp = ctrl->val; |
| 1397 | break; |
| 1398 | case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE: |
| 1399 | ctx->params.slice_mode = ctrl->val; |
| 1400 | break; |
| 1401 | case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB: |
| 1402 | ctx->params.slice_max_mb = ctrl->val; |
| 1403 | break; |
Philipp Zabel | c566c78 | 2012-08-08 11:59:38 -0300 | [diff] [blame] | 1404 | case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES: |
| 1405 | ctx->params.slice_max_bits = ctrl->val * 8; |
| 1406 | break; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1407 | case V4L2_CID_MPEG_VIDEO_HEADER_MODE: |
| 1408 | break; |
| 1409 | default: |
| 1410 | v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, |
| 1411 | "Invalid control, id=%d, val=%d\n", |
| 1412 | ctrl->id, ctrl->val); |
| 1413 | return -EINVAL; |
| 1414 | } |
| 1415 | |
| 1416 | return 0; |
| 1417 | } |
| 1418 | |
| 1419 | static struct v4l2_ctrl_ops coda_ctrl_ops = { |
| 1420 | .s_ctrl = coda_s_ctrl, |
| 1421 | }; |
| 1422 | |
| 1423 | static int coda_ctrls_setup(struct coda_ctx *ctx) |
| 1424 | { |
| 1425 | v4l2_ctrl_handler_init(&ctx->ctrls, 9); |
| 1426 | |
| 1427 | v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops, |
Philipp Zabel | 8f35c7b | 2012-07-09 04:25:52 -0300 | [diff] [blame] | 1428 | V4L2_CID_HFLIP, 0, 1, 1, 0); |
| 1429 | v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops, |
| 1430 | V4L2_CID_VFLIP, 0, 1, 1, 0); |
| 1431 | v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops, |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1432 | V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1, 0); |
| 1433 | v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops, |
| 1434 | V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 60, 1, 16); |
| 1435 | v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops, |
| 1436 | V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 1, 51, 1, 25); |
| 1437 | v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops, |
| 1438 | V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 1, 51, 1, 25); |
| 1439 | v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops, |
| 1440 | V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2); |
| 1441 | v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops, |
| 1442 | V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2); |
| 1443 | v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops, |
| 1444 | V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE, |
Philipp Zabel | c566c78 | 2012-08-08 11:59:38 -0300 | [diff] [blame] | 1445 | V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0, |
| 1446 | V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1447 | v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops, |
| 1448 | V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1); |
Philipp Zabel | c566c78 | 2012-08-08 11:59:38 -0300 | [diff] [blame] | 1449 | v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops, |
| 1450 | V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1, 500); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1451 | v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops, |
| 1452 | V4L2_CID_MPEG_VIDEO_HEADER_MODE, |
| 1453 | V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME, |
| 1454 | (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE), |
| 1455 | V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME); |
| 1456 | |
| 1457 | if (ctx->ctrls.error) { |
| 1458 | v4l2_err(&ctx->dev->v4l2_dev, "control initialization error (%d)", |
| 1459 | ctx->ctrls.error); |
| 1460 | return -EINVAL; |
| 1461 | } |
| 1462 | |
| 1463 | return v4l2_ctrl_handler_setup(&ctx->ctrls); |
| 1464 | } |
| 1465 | |
| 1466 | static int coda_queue_init(void *priv, struct vb2_queue *src_vq, |
| 1467 | struct vb2_queue *dst_vq) |
| 1468 | { |
| 1469 | struct coda_ctx *ctx = priv; |
| 1470 | int ret; |
| 1471 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1472 | src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT; |
Philipp Zabel | 419869c | 2013-05-23 07:06:30 -0300 | [diff] [blame] | 1473 | src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1474 | src_vq->drv_priv = ctx; |
| 1475 | src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer); |
| 1476 | src_vq->ops = &coda_qops; |
| 1477 | src_vq->mem_ops = &vb2_dma_contig_memops; |
Kamil Debski | ccd571c | 2013-04-24 10:38:24 -0300 | [diff] [blame] | 1478 | src_vq->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_COPY; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1479 | |
| 1480 | ret = vb2_queue_init(src_vq); |
| 1481 | if (ret) |
| 1482 | return ret; |
| 1483 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1484 | dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
Philipp Zabel | 419869c | 2013-05-23 07:06:30 -0300 | [diff] [blame] | 1485 | dst_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1486 | dst_vq->drv_priv = ctx; |
| 1487 | dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer); |
| 1488 | dst_vq->ops = &coda_qops; |
| 1489 | dst_vq->mem_ops = &vb2_dma_contig_memops; |
Kamil Debski | ccd571c | 2013-04-24 10:38:24 -0300 | [diff] [blame] | 1490 | dst_vq->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_COPY; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1491 | |
| 1492 | return vb2_queue_init(dst_vq); |
| 1493 | } |
| 1494 | |
Philipp Zabel | e11f3e6 | 2012-07-25 09:16:58 -0300 | [diff] [blame] | 1495 | static int coda_next_free_instance(struct coda_dev *dev) |
| 1496 | { |
| 1497 | return ffz(dev->instance_mask); |
| 1498 | } |
| 1499 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1500 | static int coda_open(struct file *file) |
| 1501 | { |
| 1502 | struct coda_dev *dev = video_drvdata(file); |
| 1503 | struct coda_ctx *ctx = NULL; |
| 1504 | int ret = 0; |
Philipp Zabel | e11f3e6 | 2012-07-25 09:16:58 -0300 | [diff] [blame] | 1505 | int idx; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1506 | |
Philipp Zabel | e11f3e6 | 2012-07-25 09:16:58 -0300 | [diff] [blame] | 1507 | idx = coda_next_free_instance(dev); |
| 1508 | if (idx >= CODA_MAX_INSTANCES) |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1509 | return -EBUSY; |
Philipp Zabel | e11f3e6 | 2012-07-25 09:16:58 -0300 | [diff] [blame] | 1510 | set_bit(idx, &dev->instance_mask); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1511 | |
| 1512 | ctx = kzalloc(sizeof *ctx, GFP_KERNEL); |
| 1513 | if (!ctx) |
| 1514 | return -ENOMEM; |
| 1515 | |
| 1516 | v4l2_fh_init(&ctx->fh, video_devdata(file)); |
| 1517 | file->private_data = &ctx->fh; |
| 1518 | v4l2_fh_add(&ctx->fh); |
| 1519 | ctx->dev = dev; |
Philipp Zabel | e11f3e6 | 2012-07-25 09:16:58 -0300 | [diff] [blame] | 1520 | ctx->idx = idx; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1521 | |
| 1522 | set_default_params(ctx); |
| 1523 | ctx->m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx, |
| 1524 | &coda_queue_init); |
| 1525 | if (IS_ERR(ctx->m2m_ctx)) { |
Philipp Zabel | 72720ff | 2013-05-21 10:31:25 -0300 | [diff] [blame] | 1526 | ret = PTR_ERR(ctx->m2m_ctx); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1527 | |
| 1528 | v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n", |
| 1529 | __func__, ret); |
| 1530 | goto err; |
| 1531 | } |
| 1532 | ret = coda_ctrls_setup(ctx); |
| 1533 | if (ret) { |
| 1534 | v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n"); |
| 1535 | goto err; |
| 1536 | } |
| 1537 | |
| 1538 | ctx->fh.ctrl_handler = &ctx->ctrls; |
| 1539 | |
| 1540 | ctx->parabuf.vaddr = dma_alloc_coherent(&dev->plat_dev->dev, |
| 1541 | CODA_PARA_BUF_SIZE, &ctx->parabuf.paddr, GFP_KERNEL); |
| 1542 | if (!ctx->parabuf.vaddr) { |
| 1543 | v4l2_err(&dev->v4l2_dev, "failed to allocate parabuf"); |
| 1544 | ret = -ENOMEM; |
| 1545 | goto err; |
| 1546 | } |
| 1547 | |
| 1548 | coda_lock(ctx); |
Philipp Zabel | e11f3e6 | 2012-07-25 09:16:58 -0300 | [diff] [blame] | 1549 | list_add(&ctx->list, &dev->instances); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1550 | coda_unlock(ctx); |
| 1551 | |
| 1552 | clk_prepare_enable(dev->clk_per); |
| 1553 | clk_prepare_enable(dev->clk_ahb); |
| 1554 | |
| 1555 | v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Created instance %d (%p)\n", |
| 1556 | ctx->idx, ctx); |
| 1557 | |
| 1558 | return 0; |
| 1559 | |
| 1560 | err: |
| 1561 | v4l2_fh_del(&ctx->fh); |
| 1562 | v4l2_fh_exit(&ctx->fh); |
| 1563 | kfree(ctx); |
| 1564 | return ret; |
| 1565 | } |
| 1566 | |
| 1567 | static int coda_release(struct file *file) |
| 1568 | { |
| 1569 | struct coda_dev *dev = video_drvdata(file); |
| 1570 | struct coda_ctx *ctx = fh_to_ctx(file->private_data); |
| 1571 | |
| 1572 | v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Releasing instance %p\n", |
| 1573 | ctx); |
| 1574 | |
| 1575 | coda_lock(ctx); |
Philipp Zabel | e11f3e6 | 2012-07-25 09:16:58 -0300 | [diff] [blame] | 1576 | list_del(&ctx->list); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1577 | coda_unlock(ctx); |
| 1578 | |
| 1579 | dma_free_coherent(&dev->plat_dev->dev, CODA_PARA_BUF_SIZE, |
| 1580 | ctx->parabuf.vaddr, ctx->parabuf.paddr); |
| 1581 | v4l2_m2m_ctx_release(ctx->m2m_ctx); |
| 1582 | v4l2_ctrl_handler_free(&ctx->ctrls); |
| 1583 | clk_disable_unprepare(dev->clk_per); |
| 1584 | clk_disable_unprepare(dev->clk_ahb); |
| 1585 | v4l2_fh_del(&ctx->fh); |
| 1586 | v4l2_fh_exit(&ctx->fh); |
Philipp Zabel | e11f3e6 | 2012-07-25 09:16:58 -0300 | [diff] [blame] | 1587 | clear_bit(ctx->idx, &dev->instance_mask); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1588 | kfree(ctx); |
| 1589 | |
| 1590 | return 0; |
| 1591 | } |
| 1592 | |
| 1593 | static unsigned int coda_poll(struct file *file, |
| 1594 | struct poll_table_struct *wait) |
| 1595 | { |
| 1596 | struct coda_ctx *ctx = fh_to_ctx(file->private_data); |
| 1597 | int ret; |
| 1598 | |
| 1599 | coda_lock(ctx); |
| 1600 | ret = v4l2_m2m_poll(file, ctx->m2m_ctx, wait); |
| 1601 | coda_unlock(ctx); |
| 1602 | return ret; |
| 1603 | } |
| 1604 | |
| 1605 | static int coda_mmap(struct file *file, struct vm_area_struct *vma) |
| 1606 | { |
| 1607 | struct coda_ctx *ctx = fh_to_ctx(file->private_data); |
| 1608 | |
| 1609 | return v4l2_m2m_mmap(file, ctx->m2m_ctx, vma); |
| 1610 | } |
| 1611 | |
| 1612 | static const struct v4l2_file_operations coda_fops = { |
| 1613 | .owner = THIS_MODULE, |
| 1614 | .open = coda_open, |
| 1615 | .release = coda_release, |
| 1616 | .poll = coda_poll, |
| 1617 | .unlocked_ioctl = video_ioctl2, |
| 1618 | .mmap = coda_mmap, |
| 1619 | }; |
| 1620 | |
| 1621 | static irqreturn_t coda_irq_handler(int irq, void *data) |
| 1622 | { |
Philipp Zabel | ec25f68 | 2012-07-20 08:54:29 -0300 | [diff] [blame] | 1623 | struct vb2_buffer *src_buf, *dst_buf; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1624 | struct coda_dev *dev = data; |
| 1625 | u32 wr_ptr, start_ptr; |
| 1626 | struct coda_ctx *ctx; |
| 1627 | |
Fabio Estevam | 2249b45 | 2012-10-09 23:33:29 -0300 | [diff] [blame] | 1628 | cancel_delayed_work(&dev->timeout); |
Philipp Zabel | 2fb57f0 | 2012-07-25 09:22:07 -0300 | [diff] [blame] | 1629 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1630 | /* read status register to attend the IRQ */ |
| 1631 | coda_read(dev, CODA_REG_BIT_INT_STATUS); |
| 1632 | coda_write(dev, CODA_REG_BIT_INT_CLEAR_SET, |
| 1633 | CODA_REG_BIT_INT_CLEAR); |
| 1634 | |
| 1635 | ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev); |
| 1636 | if (ctx == NULL) { |
| 1637 | v4l2_err(&dev->v4l2_dev, "Instance released before the end of transaction\n"); |
| 1638 | return IRQ_HANDLED; |
| 1639 | } |
| 1640 | |
| 1641 | if (ctx->aborting) { |
| 1642 | v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, |
| 1643 | "task has been aborted\n"); |
| 1644 | return IRQ_HANDLED; |
| 1645 | } |
| 1646 | |
| 1647 | if (coda_isbusy(ctx->dev)) { |
| 1648 | v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, |
| 1649 | "coda is still busy!!!!\n"); |
| 1650 | return IRQ_NONE; |
| 1651 | } |
| 1652 | |
Philipp Zabel | 62bed14 | 2012-07-25 10:40:39 -0300 | [diff] [blame] | 1653 | complete(&dev->done); |
| 1654 | |
Philipp Zabel | ec25f68 | 2012-07-20 08:54:29 -0300 | [diff] [blame] | 1655 | src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx); |
| 1656 | dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1657 | |
| 1658 | /* Get results from the coda */ |
| 1659 | coda_read(dev, CODA_RET_ENC_PIC_TYPE); |
| 1660 | start_ptr = coda_read(dev, CODA_CMD_ENC_PIC_BB_START); |
| 1661 | wr_ptr = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->idx)); |
| 1662 | /* Calculate bytesused field */ |
| 1663 | if (dst_buf->v4l2_buf.sequence == 0) { |
| 1664 | dst_buf->v4l2_planes[0].bytesused = (wr_ptr - start_ptr) + |
| 1665 | ctx->vpu_header_size[0] + |
| 1666 | ctx->vpu_header_size[1] + |
| 1667 | ctx->vpu_header_size[2]; |
| 1668 | } else { |
| 1669 | dst_buf->v4l2_planes[0].bytesused = (wr_ptr - start_ptr); |
| 1670 | } |
| 1671 | |
| 1672 | v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, "frame size = %u\n", |
| 1673 | wr_ptr - start_ptr); |
| 1674 | |
| 1675 | coda_read(dev, CODA_RET_ENC_PIC_SLICE_NUM); |
| 1676 | coda_read(dev, CODA_RET_ENC_PIC_FLAG); |
| 1677 | |
| 1678 | if (src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) { |
| 1679 | dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME; |
| 1680 | dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME; |
| 1681 | } else { |
| 1682 | dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME; |
| 1683 | dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME; |
| 1684 | } |
| 1685 | |
Kamil Debski | ccd571c | 2013-04-24 10:38:24 -0300 | [diff] [blame] | 1686 | dst_buf->v4l2_buf.timestamp = src_buf->v4l2_buf.timestamp; |
| 1687 | dst_buf->v4l2_buf.timecode = src_buf->v4l2_buf.timecode; |
| 1688 | |
Philipp Zabel | ec25f68 | 2012-07-20 08:54:29 -0300 | [diff] [blame] | 1689 | v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1690 | v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE); |
| 1691 | |
| 1692 | ctx->gopcounter--; |
| 1693 | if (ctx->gopcounter < 0) |
| 1694 | ctx->gopcounter = ctx->params.gop_size - 1; |
| 1695 | |
| 1696 | v4l2_dbg(1, coda_debug, &dev->v4l2_dev, |
| 1697 | "job finished: encoding frame (%d) (%s)\n", |
| 1698 | dst_buf->v4l2_buf.sequence, |
| 1699 | (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ? |
| 1700 | "KEYFRAME" : "PFRAME"); |
| 1701 | |
| 1702 | v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->m2m_ctx); |
| 1703 | |
| 1704 | return IRQ_HANDLED; |
| 1705 | } |
| 1706 | |
Philipp Zabel | 2fb57f0 | 2012-07-25 09:22:07 -0300 | [diff] [blame] | 1707 | static void coda_timeout(struct work_struct *work) |
| 1708 | { |
| 1709 | struct coda_ctx *ctx; |
| 1710 | struct coda_dev *dev = container_of(to_delayed_work(work), |
| 1711 | struct coda_dev, timeout); |
| 1712 | |
Philipp Zabel | 62bed14 | 2012-07-25 10:40:39 -0300 | [diff] [blame] | 1713 | if (completion_done(&dev->done)) |
| 1714 | return; |
| 1715 | |
| 1716 | complete(&dev->done); |
| 1717 | |
Philipp Zabel | 39cc029 | 2013-05-21 10:32:42 -0300 | [diff] [blame] | 1718 | dev_err(&dev->plat_dev->dev, "CODA PIC_RUN timeout, stopping all streams\n"); |
Philipp Zabel | 2fb57f0 | 2012-07-25 09:22:07 -0300 | [diff] [blame] | 1719 | |
| 1720 | mutex_lock(&dev->dev_mutex); |
| 1721 | list_for_each_entry(ctx, &dev->instances, list) { |
| 1722 | v4l2_m2m_streamoff(NULL, ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT); |
| 1723 | v4l2_m2m_streamoff(NULL, ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE); |
| 1724 | } |
| 1725 | mutex_unlock(&dev->dev_mutex); |
| 1726 | } |
| 1727 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1728 | static u32 coda_supported_firmwares[] = { |
| 1729 | CODA_FIRMWARE_VERNUM(CODA_DX6, 2, 2, 5), |
Philipp Zabel | df1e74c | 2012-07-02 06:07:10 -0300 | [diff] [blame] | 1730 | CODA_FIRMWARE_VERNUM(CODA_7541, 13, 4, 29), |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1731 | }; |
| 1732 | |
| 1733 | static bool coda_firmware_supported(u32 vernum) |
| 1734 | { |
| 1735 | int i; |
| 1736 | |
| 1737 | for (i = 0; i < ARRAY_SIZE(coda_supported_firmwares); i++) |
| 1738 | if (vernum == coda_supported_firmwares[i]) |
| 1739 | return true; |
| 1740 | return false; |
| 1741 | } |
| 1742 | |
| 1743 | static char *coda_product_name(int product) |
| 1744 | { |
| 1745 | static char buf[9]; |
| 1746 | |
| 1747 | switch (product) { |
| 1748 | case CODA_DX6: |
| 1749 | return "CodaDx6"; |
Philipp Zabel | df1e74c | 2012-07-02 06:07:10 -0300 | [diff] [blame] | 1750 | case CODA_7541: |
| 1751 | return "CODA7541"; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1752 | default: |
| 1753 | snprintf(buf, sizeof(buf), "(0x%04x)", product); |
| 1754 | return buf; |
| 1755 | } |
| 1756 | } |
| 1757 | |
Philipp Zabel | 87048bb | 2012-07-02 07:03:43 -0300 | [diff] [blame] | 1758 | static int coda_hw_init(struct coda_dev *dev) |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1759 | { |
| 1760 | u16 product, major, minor, release; |
| 1761 | u32 data; |
| 1762 | u16 *p; |
| 1763 | int i; |
| 1764 | |
| 1765 | clk_prepare_enable(dev->clk_per); |
| 1766 | clk_prepare_enable(dev->clk_ahb); |
| 1767 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1768 | /* |
| 1769 | * Copy the first CODA_ISRAM_SIZE in the internal SRAM. |
Philipp Zabel | 87048bb | 2012-07-02 07:03:43 -0300 | [diff] [blame] | 1770 | * The 16-bit chars in the code buffer are in memory access |
| 1771 | * order, re-sort them to CODA order for register download. |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1772 | * Data in this SRAM survives a reboot. |
| 1773 | */ |
Philipp Zabel | 87048bb | 2012-07-02 07:03:43 -0300 | [diff] [blame] | 1774 | p = (u16 *)dev->codebuf.vaddr; |
| 1775 | if (dev->devtype->product == CODA_DX6) { |
| 1776 | for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) { |
| 1777 | data = CODA_DOWN_ADDRESS_SET(i) | |
| 1778 | CODA_DOWN_DATA_SET(p[i ^ 1]); |
| 1779 | coda_write(dev, data, CODA_REG_BIT_CODE_DOWN); |
| 1780 | } |
| 1781 | } else { |
| 1782 | for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) { |
| 1783 | data = CODA_DOWN_ADDRESS_SET(i) | |
| 1784 | CODA_DOWN_DATA_SET(p[round_down(i, 4) + |
| 1785 | 3 - (i % 4)]); |
| 1786 | coda_write(dev, data, CODA_REG_BIT_CODE_DOWN); |
| 1787 | } |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1788 | } |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1789 | |
Philipp Zabel | 9acf769 | 2013-05-23 10:42:56 -0300 | [diff] [blame] | 1790 | /* Clear registers */ |
| 1791 | for (i = 0; i < 64; i++) |
| 1792 | coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4); |
| 1793 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1794 | /* Tell the BIT where to find everything it needs */ |
| 1795 | coda_write(dev, dev->workbuf.paddr, |
| 1796 | CODA_REG_BIT_WORK_BUF_ADDR); |
| 1797 | coda_write(dev, dev->codebuf.paddr, |
| 1798 | CODA_REG_BIT_CODE_BUF_ADDR); |
| 1799 | coda_write(dev, 0, CODA_REG_BIT_CODE_RUN); |
| 1800 | |
| 1801 | /* Set default values */ |
| 1802 | switch (dev->devtype->product) { |
| 1803 | case CODA_DX6: |
| 1804 | coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL); |
| 1805 | break; |
| 1806 | default: |
| 1807 | coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL); |
| 1808 | } |
| 1809 | coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL); |
Philipp Zabel | 1043667 | 2012-07-02 09:03:55 -0300 | [diff] [blame] | 1810 | |
| 1811 | if (dev->devtype->product != CODA_DX6) |
| 1812 | coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE); |
| 1813 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1814 | coda_write(dev, CODA_INT_INTERRUPT_ENABLE, |
| 1815 | CODA_REG_BIT_INT_ENABLE); |
| 1816 | |
| 1817 | /* Reset VPU and start processor */ |
| 1818 | data = coda_read(dev, CODA_REG_BIT_CODE_RESET); |
| 1819 | data |= CODA_REG_RESET_ENABLE; |
| 1820 | coda_write(dev, data, CODA_REG_BIT_CODE_RESET); |
| 1821 | udelay(10); |
| 1822 | data &= ~CODA_REG_RESET_ENABLE; |
| 1823 | coda_write(dev, data, CODA_REG_BIT_CODE_RESET); |
| 1824 | coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN); |
| 1825 | |
| 1826 | /* Load firmware */ |
| 1827 | coda_write(dev, 0, CODA_CMD_FIRMWARE_VERNUM); |
| 1828 | coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY); |
| 1829 | coda_write(dev, 0, CODA_REG_BIT_RUN_INDEX); |
| 1830 | coda_write(dev, 0, CODA_REG_BIT_RUN_COD_STD); |
| 1831 | coda_write(dev, CODA_COMMAND_FIRMWARE_GET, CODA_REG_BIT_RUN_COMMAND); |
| 1832 | if (coda_wait_timeout(dev)) { |
| 1833 | clk_disable_unprepare(dev->clk_per); |
| 1834 | clk_disable_unprepare(dev->clk_ahb); |
| 1835 | v4l2_err(&dev->v4l2_dev, "firmware get command error\n"); |
| 1836 | return -EIO; |
| 1837 | } |
| 1838 | |
| 1839 | /* Check we are compatible with the loaded firmware */ |
| 1840 | data = coda_read(dev, CODA_CMD_FIRMWARE_VERNUM); |
| 1841 | product = CODA_FIRMWARE_PRODUCT(data); |
| 1842 | major = CODA_FIRMWARE_MAJOR(data); |
| 1843 | minor = CODA_FIRMWARE_MINOR(data); |
| 1844 | release = CODA_FIRMWARE_RELEASE(data); |
| 1845 | |
| 1846 | clk_disable_unprepare(dev->clk_per); |
| 1847 | clk_disable_unprepare(dev->clk_ahb); |
| 1848 | |
| 1849 | if (product != dev->devtype->product) { |
| 1850 | v4l2_err(&dev->v4l2_dev, "Wrong firmware. Hw: %s, Fw: %s," |
| 1851 | " Version: %u.%u.%u\n", |
| 1852 | coda_product_name(dev->devtype->product), |
| 1853 | coda_product_name(product), major, minor, release); |
| 1854 | return -EINVAL; |
| 1855 | } |
| 1856 | |
| 1857 | v4l2_info(&dev->v4l2_dev, "Initialized %s.\n", |
| 1858 | coda_product_name(product)); |
| 1859 | |
| 1860 | if (coda_firmware_supported(data)) { |
| 1861 | v4l2_info(&dev->v4l2_dev, "Firmware version: %u.%u.%u\n", |
| 1862 | major, minor, release); |
| 1863 | } else { |
| 1864 | v4l2_warn(&dev->v4l2_dev, "Unsupported firmware version: " |
| 1865 | "%u.%u.%u\n", major, minor, release); |
| 1866 | } |
| 1867 | |
| 1868 | return 0; |
| 1869 | } |
| 1870 | |
| 1871 | static void coda_fw_callback(const struct firmware *fw, void *context) |
| 1872 | { |
| 1873 | struct coda_dev *dev = context; |
| 1874 | struct platform_device *pdev = dev->plat_dev; |
| 1875 | int ret; |
| 1876 | |
| 1877 | if (!fw) { |
| 1878 | v4l2_err(&dev->v4l2_dev, "firmware request failed\n"); |
| 1879 | return; |
| 1880 | } |
| 1881 | |
| 1882 | /* allocate auxiliary per-device code buffer for the BIT processor */ |
| 1883 | dev->codebuf.size = fw->size; |
| 1884 | dev->codebuf.vaddr = dma_alloc_coherent(&pdev->dev, fw->size, |
| 1885 | &dev->codebuf.paddr, |
| 1886 | GFP_KERNEL); |
| 1887 | if (!dev->codebuf.vaddr) { |
| 1888 | dev_err(&pdev->dev, "failed to allocate code buffer\n"); |
| 1889 | return; |
| 1890 | } |
| 1891 | |
Philipp Zabel | 87048bb | 2012-07-02 07:03:43 -0300 | [diff] [blame] | 1892 | /* Copy the whole firmware image to the code buffer */ |
| 1893 | memcpy(dev->codebuf.vaddr, fw->data, fw->size); |
| 1894 | release_firmware(fw); |
| 1895 | |
| 1896 | ret = coda_hw_init(dev); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1897 | if (ret) { |
| 1898 | v4l2_err(&dev->v4l2_dev, "HW initialization failed\n"); |
| 1899 | return; |
| 1900 | } |
| 1901 | |
| 1902 | dev->vfd.fops = &coda_fops, |
| 1903 | dev->vfd.ioctl_ops = &coda_ioctl_ops; |
| 1904 | dev->vfd.release = video_device_release_empty, |
| 1905 | dev->vfd.lock = &dev->dev_mutex; |
| 1906 | dev->vfd.v4l2_dev = &dev->v4l2_dev; |
Hans Verkuil | 954f340 | 2012-09-05 06:05:50 -0300 | [diff] [blame] | 1907 | dev->vfd.vfl_dir = VFL_DIR_M2M; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1908 | snprintf(dev->vfd.name, sizeof(dev->vfd.name), "%s", CODA_NAME); |
| 1909 | video_set_drvdata(&dev->vfd, dev); |
| 1910 | |
| 1911 | dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev); |
| 1912 | if (IS_ERR(dev->alloc_ctx)) { |
| 1913 | v4l2_err(&dev->v4l2_dev, "Failed to alloc vb2 context\n"); |
| 1914 | return; |
| 1915 | } |
| 1916 | |
| 1917 | dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops); |
| 1918 | if (IS_ERR(dev->m2m_dev)) { |
| 1919 | v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n"); |
| 1920 | goto rel_ctx; |
| 1921 | } |
| 1922 | |
| 1923 | ret = video_register_device(&dev->vfd, VFL_TYPE_GRABBER, 0); |
| 1924 | if (ret) { |
| 1925 | v4l2_err(&dev->v4l2_dev, "Failed to register video device\n"); |
| 1926 | goto rel_m2m; |
| 1927 | } |
| 1928 | v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video%d\n", |
| 1929 | dev->vfd.num); |
| 1930 | |
| 1931 | return; |
| 1932 | |
| 1933 | rel_m2m: |
| 1934 | v4l2_m2m_release(dev->m2m_dev); |
| 1935 | rel_ctx: |
| 1936 | vb2_dma_contig_cleanup_ctx(dev->alloc_ctx); |
| 1937 | } |
| 1938 | |
| 1939 | static int coda_firmware_request(struct coda_dev *dev) |
| 1940 | { |
| 1941 | char *fw = dev->devtype->firmware; |
| 1942 | |
| 1943 | dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw, |
| 1944 | coda_product_name(dev->devtype->product)); |
| 1945 | |
| 1946 | return request_firmware_nowait(THIS_MODULE, true, |
| 1947 | fw, &dev->plat_dev->dev, GFP_KERNEL, dev, coda_fw_callback); |
| 1948 | } |
| 1949 | |
| 1950 | enum coda_platform { |
| 1951 | CODA_IMX27, |
Philipp Zabel | df1e74c | 2012-07-02 06:07:10 -0300 | [diff] [blame] | 1952 | CODA_IMX53, |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1953 | }; |
| 1954 | |
Emil Goode | c06d875 | 2012-08-14 17:44:42 -0300 | [diff] [blame] | 1955 | static const struct coda_devtype coda_devdata[] = { |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1956 | [CODA_IMX27] = { |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame^] | 1957 | .firmware = "v4l-codadx6-imx27.bin", |
| 1958 | .product = CODA_DX6, |
| 1959 | .codecs = codadx6_codecs, |
| 1960 | .num_codecs = ARRAY_SIZE(codadx6_codecs), |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1961 | }, |
Philipp Zabel | df1e74c | 2012-07-02 06:07:10 -0300 | [diff] [blame] | 1962 | [CODA_IMX53] = { |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame^] | 1963 | .firmware = "v4l-coda7541-imx53.bin", |
| 1964 | .product = CODA_7541, |
| 1965 | .codecs = coda7_codecs, |
| 1966 | .num_codecs = ARRAY_SIZE(coda7_codecs), |
Philipp Zabel | df1e74c | 2012-07-02 06:07:10 -0300 | [diff] [blame] | 1967 | }, |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1968 | }; |
| 1969 | |
| 1970 | static struct platform_device_id coda_platform_ids[] = { |
| 1971 | { .name = "coda-imx27", .driver_data = CODA_IMX27 }, |
Fabio Estevam | 4e44cd0 | 2012-10-10 00:07:38 -0300 | [diff] [blame] | 1972 | { .name = "coda-imx53", .driver_data = CODA_IMX53 }, |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1973 | { /* sentinel */ } |
| 1974 | }; |
| 1975 | MODULE_DEVICE_TABLE(platform, coda_platform_ids); |
| 1976 | |
| 1977 | #ifdef CONFIG_OF |
| 1978 | static const struct of_device_id coda_dt_ids[] = { |
| 1979 | { .compatible = "fsl,imx27-vpu", .data = &coda_platform_ids[CODA_IMX27] }, |
Philipp Zabel | df1e74c | 2012-07-02 06:07:10 -0300 | [diff] [blame] | 1980 | { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] }, |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1981 | { /* sentinel */ } |
| 1982 | }; |
| 1983 | MODULE_DEVICE_TABLE(of, coda_dt_ids); |
| 1984 | #endif |
| 1985 | |
Greg Kroah-Hartman | 4c62e97 | 2012-12-21 13:17:53 -0800 | [diff] [blame] | 1986 | static int coda_probe(struct platform_device *pdev) |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1987 | { |
| 1988 | const struct of_device_id *of_id = |
| 1989 | of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev); |
| 1990 | const struct platform_device_id *pdev_id; |
Philipp Zabel | 657eee7 | 2013-04-29 16:17:14 -0700 | [diff] [blame] | 1991 | struct coda_platform_data *pdata = pdev->dev.platform_data; |
| 1992 | struct device_node *np = pdev->dev.of_node; |
| 1993 | struct gen_pool *pool; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1994 | struct coda_dev *dev; |
| 1995 | struct resource *res; |
| 1996 | int ret, irq; |
| 1997 | |
| 1998 | dev = devm_kzalloc(&pdev->dev, sizeof *dev, GFP_KERNEL); |
| 1999 | if (!dev) { |
| 2000 | dev_err(&pdev->dev, "Not enough memory for %s\n", |
| 2001 | CODA_NAME); |
| 2002 | return -ENOMEM; |
| 2003 | } |
| 2004 | |
| 2005 | spin_lock_init(&dev->irqlock); |
Philipp Zabel | e11f3e6 | 2012-07-25 09:16:58 -0300 | [diff] [blame] | 2006 | INIT_LIST_HEAD(&dev->instances); |
Philipp Zabel | 2fb57f0 | 2012-07-25 09:22:07 -0300 | [diff] [blame] | 2007 | INIT_DELAYED_WORK(&dev->timeout, coda_timeout); |
Philipp Zabel | 62bed14 | 2012-07-25 10:40:39 -0300 | [diff] [blame] | 2008 | init_completion(&dev->done); |
| 2009 | complete(&dev->done); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2010 | |
| 2011 | dev->plat_dev = pdev; |
| 2012 | dev->clk_per = devm_clk_get(&pdev->dev, "per"); |
| 2013 | if (IS_ERR(dev->clk_per)) { |
| 2014 | dev_err(&pdev->dev, "Could not get per clock\n"); |
| 2015 | return PTR_ERR(dev->clk_per); |
| 2016 | } |
| 2017 | |
| 2018 | dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb"); |
| 2019 | if (IS_ERR(dev->clk_ahb)) { |
| 2020 | dev_err(&pdev->dev, "Could not get ahb clock\n"); |
| 2021 | return PTR_ERR(dev->clk_ahb); |
| 2022 | } |
| 2023 | |
| 2024 | /* Get memory for physical registers */ |
| 2025 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 2026 | if (res == NULL) { |
| 2027 | dev_err(&pdev->dev, "failed to get memory region resource\n"); |
| 2028 | return -ENOENT; |
| 2029 | } |
| 2030 | |
Philipp Zabel | 611cbbf | 2013-05-21 04:19:27 -0300 | [diff] [blame] | 2031 | dev->regs_base = devm_ioremap_resource(&pdev->dev, res); |
| 2032 | if (IS_ERR(dev->regs_base)) |
| 2033 | return PTR_ERR(dev->regs_base); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2034 | |
| 2035 | /* IRQ */ |
| 2036 | irq = platform_get_irq(pdev, 0); |
| 2037 | if (irq < 0) { |
| 2038 | dev_err(&pdev->dev, "failed to get irq resource\n"); |
| 2039 | return -ENOENT; |
| 2040 | } |
| 2041 | |
| 2042 | if (devm_request_irq(&pdev->dev, irq, coda_irq_handler, |
| 2043 | 0, CODA_NAME, dev) < 0) { |
| 2044 | dev_err(&pdev->dev, "failed to request irq\n"); |
| 2045 | return -ENOENT; |
| 2046 | } |
| 2047 | |
Philipp Zabel | 657eee7 | 2013-04-29 16:17:14 -0700 | [diff] [blame] | 2048 | /* Get IRAM pool from device tree or platform data */ |
| 2049 | pool = of_get_named_gen_pool(np, "iram", 0); |
| 2050 | if (!pool && pdata) |
| 2051 | pool = dev_get_gen_pool(pdata->iram_dev); |
| 2052 | if (!pool) { |
| 2053 | dev_err(&pdev->dev, "iram pool not available\n"); |
| 2054 | return -ENOMEM; |
| 2055 | } |
| 2056 | dev->iram_pool = pool; |
| 2057 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2058 | ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev); |
| 2059 | if (ret) |
| 2060 | return ret; |
| 2061 | |
| 2062 | mutex_init(&dev->dev_mutex); |
| 2063 | |
| 2064 | pdev_id = of_id ? of_id->data : platform_get_device_id(pdev); |
| 2065 | |
| 2066 | if (of_id) { |
| 2067 | dev->devtype = of_id->data; |
| 2068 | } else if (pdev_id) { |
| 2069 | dev->devtype = &coda_devdata[pdev_id->driver_data]; |
| 2070 | } else { |
| 2071 | v4l2_device_unregister(&dev->v4l2_dev); |
| 2072 | return -EINVAL; |
| 2073 | } |
| 2074 | |
| 2075 | /* allocate auxiliary per-device buffers for the BIT processor */ |
| 2076 | switch (dev->devtype->product) { |
| 2077 | case CODA_DX6: |
| 2078 | dev->workbuf.size = CODADX6_WORK_BUF_SIZE; |
| 2079 | break; |
| 2080 | default: |
| 2081 | dev->workbuf.size = CODA7_WORK_BUF_SIZE; |
| 2082 | } |
| 2083 | dev->workbuf.vaddr = dma_alloc_coherent(&pdev->dev, dev->workbuf.size, |
| 2084 | &dev->workbuf.paddr, |
| 2085 | GFP_KERNEL); |
| 2086 | if (!dev->workbuf.vaddr) { |
| 2087 | dev_err(&pdev->dev, "failed to allocate work buffer\n"); |
| 2088 | v4l2_device_unregister(&dev->v4l2_dev); |
| 2089 | return -ENOMEM; |
| 2090 | } |
| 2091 | |
Philipp Zabel | 657eee7 | 2013-04-29 16:17:14 -0700 | [diff] [blame] | 2092 | if (dev->devtype->product == CODA_DX6) |
| 2093 | dev->iram_size = CODADX6_IRAM_SIZE; |
| 2094 | else |
| 2095 | dev->iram_size = CODA7_IRAM_SIZE; |
| 2096 | dev->iram_vaddr = gen_pool_alloc(dev->iram_pool, dev->iram_size); |
| 2097 | if (!dev->iram_vaddr) { |
| 2098 | dev_err(&pdev->dev, "unable to alloc iram\n"); |
| 2099 | return -ENOMEM; |
Philipp Zabel | 1043667 | 2012-07-02 09:03:55 -0300 | [diff] [blame] | 2100 | } |
Philipp Zabel | 657eee7 | 2013-04-29 16:17:14 -0700 | [diff] [blame] | 2101 | dev->iram_paddr = gen_pool_virt_to_phys(dev->iram_pool, |
| 2102 | dev->iram_vaddr); |
Philipp Zabel | 1043667 | 2012-07-02 09:03:55 -0300 | [diff] [blame] | 2103 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2104 | platform_set_drvdata(pdev, dev); |
| 2105 | |
| 2106 | return coda_firmware_request(dev); |
| 2107 | } |
| 2108 | |
| 2109 | static int coda_remove(struct platform_device *pdev) |
| 2110 | { |
| 2111 | struct coda_dev *dev = platform_get_drvdata(pdev); |
| 2112 | |
| 2113 | video_unregister_device(&dev->vfd); |
| 2114 | if (dev->m2m_dev) |
| 2115 | v4l2_m2m_release(dev->m2m_dev); |
| 2116 | if (dev->alloc_ctx) |
| 2117 | vb2_dma_contig_cleanup_ctx(dev->alloc_ctx); |
| 2118 | v4l2_device_unregister(&dev->v4l2_dev); |
Philipp Zabel | 657eee7 | 2013-04-29 16:17:14 -0700 | [diff] [blame] | 2119 | if (dev->iram_vaddr) |
| 2120 | gen_pool_free(dev->iram_pool, dev->iram_vaddr, dev->iram_size); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2121 | if (dev->codebuf.vaddr) |
| 2122 | dma_free_coherent(&pdev->dev, dev->codebuf.size, |
| 2123 | &dev->codebuf.vaddr, dev->codebuf.paddr); |
| 2124 | if (dev->workbuf.vaddr) |
| 2125 | dma_free_coherent(&pdev->dev, dev->workbuf.size, &dev->workbuf.vaddr, |
| 2126 | dev->workbuf.paddr); |
| 2127 | return 0; |
| 2128 | } |
| 2129 | |
| 2130 | static struct platform_driver coda_driver = { |
| 2131 | .probe = coda_probe, |
Greg Kroah-Hartman | 4c62e97 | 2012-12-21 13:17:53 -0800 | [diff] [blame] | 2132 | .remove = coda_remove, |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2133 | .driver = { |
| 2134 | .name = CODA_NAME, |
| 2135 | .owner = THIS_MODULE, |
| 2136 | .of_match_table = of_match_ptr(coda_dt_ids), |
| 2137 | }, |
| 2138 | .id_table = coda_platform_ids, |
| 2139 | }; |
| 2140 | |
| 2141 | module_platform_driver(coda_driver); |
| 2142 | |
| 2143 | MODULE_LICENSE("GPL"); |
| 2144 | MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>"); |
| 2145 | MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver"); |