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> |
Philipp Zabel | 9082a7c | 2013-06-21 03:55:31 -0300 | [diff] [blame] | 21 | #include <linux/kfifo.h> |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 22 | #include <linux/module.h> |
| 23 | #include <linux/of_device.h> |
| 24 | #include <linux/platform_device.h> |
| 25 | #include <linux/slab.h> |
| 26 | #include <linux/videodev2.h> |
| 27 | #include <linux/of.h> |
Philipp Zabel | 657eee7 | 2013-04-29 16:17:14 -0700 | [diff] [blame] | 28 | #include <linux/platform_data/coda.h> |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 29 | |
| 30 | #include <media/v4l2-ctrls.h> |
| 31 | #include <media/v4l2-device.h> |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 32 | #include <media/v4l2-event.h> |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 33 | #include <media/v4l2-ioctl.h> |
| 34 | #include <media/v4l2-mem2mem.h> |
| 35 | #include <media/videobuf2-core.h> |
| 36 | #include <media/videobuf2-dma-contig.h> |
| 37 | |
| 38 | #include "coda.h" |
| 39 | |
| 40 | #define CODA_NAME "coda" |
| 41 | |
Philipp Zabel | 0cddc7e | 2013-09-30 10:34:44 -0300 | [diff] [blame] | 42 | #define CODADX6_MAX_INSTANCES 4 |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 43 | |
| 44 | #define CODA_FMO_BUF_SIZE 32 |
| 45 | #define CODADX6_WORK_BUF_SIZE (288 * 1024 + CODA_FMO_BUF_SIZE * 8 * 1024) |
Philipp Zabel | 5677e3b | 2013-06-21 03:55:30 -0300 | [diff] [blame] | 46 | #define CODA7_WORK_BUF_SIZE (128 * 1024) |
| 47 | #define CODA7_TEMP_BUF_SIZE (304 * 1024) |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 48 | #define CODA_PARA_BUF_SIZE (10 * 1024) |
| 49 | #define CODA_ISRAM_SIZE (2048 * 2) |
Philipp Zabel | 657eee7 | 2013-04-29 16:17:14 -0700 | [diff] [blame] | 50 | #define CODADX6_IRAM_SIZE 0xb000 |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 51 | #define CODA7_IRAM_SIZE 0x14000 |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 52 | |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 53 | #define CODA7_PS_BUF_SIZE 0x28000 |
| 54 | |
| 55 | #define CODA_MAX_FRAMEBUFFERS 8 |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 56 | |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 57 | #define MAX_W 8192 |
| 58 | #define MAX_H 8192 |
| 59 | #define CODA_MAX_FRAME_SIZE 0x100000 |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 60 | #define FMO_SLICE_SAVE_BUF_SIZE (32) |
| 61 | #define CODA_DEFAULT_GAMMA 4096 |
| 62 | |
| 63 | #define MIN_W 176 |
| 64 | #define MIN_H 144 |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 65 | |
| 66 | #define S_ALIGN 1 /* multiple of 2 */ |
| 67 | #define W_ALIGN 1 /* multiple of 2 */ |
| 68 | #define H_ALIGN 1 /* multiple of 2 */ |
| 69 | |
| 70 | #define fh_to_ctx(__fh) container_of(__fh, struct coda_ctx, fh) |
| 71 | |
| 72 | static int coda_debug; |
Philipp Zabel | c4eb1bfc | 2013-05-21 04:24:16 -0300 | [diff] [blame] | 73 | module_param(coda_debug, int, 0644); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 74 | MODULE_PARM_DESC(coda_debug, "Debug level (0-1)"); |
| 75 | |
| 76 | enum { |
| 77 | V4L2_M2M_SRC = 0, |
| 78 | V4L2_M2M_DST = 1, |
| 79 | }; |
| 80 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 81 | enum coda_inst_type { |
| 82 | CODA_INST_ENCODER, |
| 83 | CODA_INST_DECODER, |
| 84 | }; |
| 85 | |
| 86 | enum coda_product { |
| 87 | CODA_DX6 = 0xf001, |
Philipp Zabel | df1e74c | 2012-07-02 06:07:10 -0300 | [diff] [blame] | 88 | CODA_7541 = 0xf012, |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | struct coda_fmt { |
| 92 | char *name; |
| 93 | u32 fourcc; |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | struct coda_codec { |
| 97 | u32 mode; |
| 98 | u32 src_fourcc; |
| 99 | u32 dst_fourcc; |
| 100 | u32 max_w; |
| 101 | u32 max_h; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | struct coda_devtype { |
| 105 | char *firmware; |
| 106 | enum coda_product product; |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 107 | struct coda_codec *codecs; |
| 108 | unsigned int num_codecs; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 109 | size_t workbuf_size; |
| 110 | }; |
| 111 | |
| 112 | /* Per-queue, driver-specific private data */ |
| 113 | struct coda_q_data { |
| 114 | unsigned int width; |
| 115 | unsigned int height; |
| 116 | unsigned int sizeimage; |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 117 | unsigned int fourcc; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | struct coda_aux_buf { |
| 121 | void *vaddr; |
| 122 | dma_addr_t paddr; |
| 123 | u32 size; |
| 124 | }; |
| 125 | |
| 126 | struct coda_dev { |
| 127 | struct v4l2_device v4l2_dev; |
| 128 | struct video_device vfd; |
| 129 | struct platform_device *plat_dev; |
Emil Goode | c06d875 | 2012-08-14 17:44:42 -0300 | [diff] [blame] | 130 | const struct coda_devtype *devtype; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 131 | |
| 132 | void __iomem *regs_base; |
| 133 | struct clk *clk_per; |
| 134 | struct clk *clk_ahb; |
| 135 | |
| 136 | struct coda_aux_buf codebuf; |
Philipp Zabel | 5677e3b | 2013-06-21 03:55:30 -0300 | [diff] [blame] | 137 | struct coda_aux_buf tempbuf; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 138 | struct coda_aux_buf workbuf; |
Philipp Zabel | 657eee7 | 2013-04-29 16:17:14 -0700 | [diff] [blame] | 139 | struct gen_pool *iram_pool; |
| 140 | long unsigned int iram_vaddr; |
Philipp Zabel | 1043667 | 2012-07-02 09:03:55 -0300 | [diff] [blame] | 141 | long unsigned int iram_paddr; |
Philipp Zabel | 657eee7 | 2013-04-29 16:17:14 -0700 | [diff] [blame] | 142 | unsigned long iram_size; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 143 | |
| 144 | spinlock_t irqlock; |
| 145 | struct mutex dev_mutex; |
Philipp Zabel | fcb6282 | 2013-05-23 10:43:00 -0300 | [diff] [blame] | 146 | struct mutex coda_mutex; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 147 | struct v4l2_m2m_dev *m2m_dev; |
| 148 | struct vb2_alloc_ctx *alloc_ctx; |
Philipp Zabel | e11f3e6 | 2012-07-25 09:16:58 -0300 | [diff] [blame] | 149 | struct list_head instances; |
| 150 | unsigned long instance_mask; |
Philipp Zabel | 2fb57f0 | 2012-07-25 09:22:07 -0300 | [diff] [blame] | 151 | struct delayed_work timeout; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 152 | }; |
| 153 | |
| 154 | struct coda_params { |
Philipp Zabel | 8f35c7b | 2012-07-09 04:25:52 -0300 | [diff] [blame] | 155 | u8 rot_mode; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 156 | u8 h264_intra_qp; |
| 157 | u8 h264_inter_qp; |
| 158 | u8 mpeg4_intra_qp; |
| 159 | u8 mpeg4_inter_qp; |
| 160 | u8 gop_size; |
| 161 | int codec_mode; |
Philipp Zabel | 5677e3b | 2013-06-21 03:55:30 -0300 | [diff] [blame] | 162 | int codec_mode_aux; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 163 | enum v4l2_mpeg_video_multi_slice_mode slice_mode; |
| 164 | u32 framerate; |
| 165 | u16 bitrate; |
Philipp Zabel | c566c78 | 2012-08-08 11:59:38 -0300 | [diff] [blame] | 166 | u32 slice_max_bits; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 167 | u32 slice_max_mb; |
| 168 | }; |
| 169 | |
Philipp Zabel | c2d2251 | 2013-06-21 03:55:28 -0300 | [diff] [blame] | 170 | struct coda_iram_info { |
| 171 | u32 axi_sram_use; |
| 172 | phys_addr_t buf_bit_use; |
| 173 | phys_addr_t buf_ip_ac_dc_use; |
| 174 | phys_addr_t buf_dbk_y_use; |
| 175 | phys_addr_t buf_dbk_c_use; |
| 176 | phys_addr_t buf_ovl_use; |
| 177 | phys_addr_t buf_btp_use; |
| 178 | phys_addr_t search_ram_paddr; |
| 179 | int search_ram_size; |
| 180 | }; |
| 181 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 182 | struct coda_ctx { |
| 183 | struct coda_dev *dev; |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 184 | struct mutex buffer_mutex; |
Philipp Zabel | e11f3e6 | 2012-07-25 09:16:58 -0300 | [diff] [blame] | 185 | struct list_head list; |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 186 | struct work_struct skip_run; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 187 | int aborting; |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 188 | int initialized; |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 189 | int streamon_out; |
| 190 | int streamon_cap; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 191 | u32 isequence; |
Philipp Zabel | 9082a7c | 2013-06-21 03:55:31 -0300 | [diff] [blame] | 192 | u32 qsequence; |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 193 | u32 osequence; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 194 | struct coda_q_data q_data[2]; |
| 195 | enum coda_inst_type inst_type; |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 196 | struct coda_codec *codec; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 197 | enum v4l2_colorspace colorspace; |
| 198 | struct coda_params params; |
| 199 | struct v4l2_m2m_ctx *m2m_ctx; |
| 200 | struct v4l2_ctrl_handler ctrls; |
| 201 | struct v4l2_fh fh; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 202 | int gopcounter; |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 203 | int runcounter; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 204 | char vpu_header[3][64]; |
| 205 | int vpu_header_size[3]; |
Philipp Zabel | 9082a7c | 2013-06-21 03:55:31 -0300 | [diff] [blame] | 206 | struct kfifo bitstream_fifo; |
| 207 | struct mutex bitstream_mutex; |
| 208 | struct coda_aux_buf bitstream; |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 209 | bool prescan_failed; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 210 | struct coda_aux_buf parabuf; |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 211 | struct coda_aux_buf psbuf; |
| 212 | struct coda_aux_buf slicebuf; |
Philipp Zabel | ec25f68 | 2012-07-20 08:54:29 -0300 | [diff] [blame] | 213 | struct coda_aux_buf internal_frames[CODA_MAX_FRAMEBUFFERS]; |
Philipp Zabel | 5677e3b | 2013-06-21 03:55:30 -0300 | [diff] [blame] | 214 | struct coda_aux_buf workbuf; |
Philipp Zabel | ec25f68 | 2012-07-20 08:54:29 -0300 | [diff] [blame] | 215 | int num_internal_frames; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 216 | int idx; |
Philipp Zabel | 5677e3b | 2013-06-21 03:55:30 -0300 | [diff] [blame] | 217 | int reg_idx; |
Philipp Zabel | c2d2251 | 2013-06-21 03:55:28 -0300 | [diff] [blame] | 218 | struct coda_iram_info iram_info; |
Philipp Zabel | 9082a7c | 2013-06-21 03:55:31 -0300 | [diff] [blame] | 219 | u32 bit_stream_param; |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 220 | u32 frm_dis_flg; |
| 221 | int display_idx; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 222 | }; |
| 223 | |
Javier Martin | 832fbb5 | 2012-10-29 08:34:59 -0300 | [diff] [blame] | 224 | static const u8 coda_filler_nal[14] = { 0x00, 0x00, 0x00, 0x01, 0x0c, 0xff, |
| 225 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80 }; |
| 226 | 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] | 227 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 228 | static inline void coda_write(struct coda_dev *dev, u32 data, u32 reg) |
| 229 | { |
| 230 | v4l2_dbg(1, coda_debug, &dev->v4l2_dev, |
| 231 | "%s: data=0x%x, reg=0x%x\n", __func__, data, reg); |
| 232 | writel(data, dev->regs_base + reg); |
| 233 | } |
| 234 | |
| 235 | static inline unsigned int coda_read(struct coda_dev *dev, u32 reg) |
| 236 | { |
| 237 | u32 data; |
| 238 | data = readl(dev->regs_base + reg); |
| 239 | v4l2_dbg(1, coda_debug, &dev->v4l2_dev, |
| 240 | "%s: data=0x%x, reg=0x%x\n", __func__, data, reg); |
| 241 | return data; |
| 242 | } |
| 243 | |
| 244 | static inline unsigned long coda_isbusy(struct coda_dev *dev) |
| 245 | { |
| 246 | return coda_read(dev, CODA_REG_BIT_BUSY); |
| 247 | } |
| 248 | |
| 249 | static inline int coda_is_initialized(struct coda_dev *dev) |
| 250 | { |
| 251 | return (coda_read(dev, CODA_REG_BIT_CUR_PC) != 0); |
| 252 | } |
| 253 | |
| 254 | static int coda_wait_timeout(struct coda_dev *dev) |
| 255 | { |
| 256 | unsigned long timeout = jiffies + msecs_to_jiffies(1000); |
| 257 | |
| 258 | while (coda_isbusy(dev)) { |
| 259 | if (time_after(jiffies, timeout)) |
| 260 | return -ETIMEDOUT; |
| 261 | } |
| 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | static void coda_command_async(struct coda_ctx *ctx, int cmd) |
| 266 | { |
| 267 | struct coda_dev *dev = ctx->dev; |
Philipp Zabel | 5677e3b | 2013-06-21 03:55:30 -0300 | [diff] [blame] | 268 | |
| 269 | if (dev->devtype->product == CODA_7541) { |
| 270 | /* Restore context related registers to CODA */ |
Philipp Zabel | 9082a7c | 2013-06-21 03:55:31 -0300 | [diff] [blame] | 271 | coda_write(dev, ctx->bit_stream_param, |
| 272 | CODA_REG_BIT_BIT_STREAM_PARAM); |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 273 | coda_write(dev, ctx->frm_dis_flg, |
| 274 | CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx)); |
Philipp Zabel | 5677e3b | 2013-06-21 03:55:30 -0300 | [diff] [blame] | 275 | coda_write(dev, ctx->workbuf.paddr, CODA_REG_BIT_WORK_BUF_ADDR); |
| 276 | } |
| 277 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 278 | coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY); |
| 279 | |
| 280 | coda_write(dev, ctx->idx, CODA_REG_BIT_RUN_INDEX); |
| 281 | coda_write(dev, ctx->params.codec_mode, CODA_REG_BIT_RUN_COD_STD); |
Philipp Zabel | 5677e3b | 2013-06-21 03:55:30 -0300 | [diff] [blame] | 282 | coda_write(dev, ctx->params.codec_mode_aux, CODA7_REG_BIT_RUN_AUX_STD); |
| 283 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 284 | coda_write(dev, cmd, CODA_REG_BIT_RUN_COMMAND); |
| 285 | } |
| 286 | |
| 287 | static int coda_command_sync(struct coda_ctx *ctx, int cmd) |
| 288 | { |
| 289 | struct coda_dev *dev = ctx->dev; |
| 290 | |
| 291 | coda_command_async(ctx, cmd); |
| 292 | return coda_wait_timeout(dev); |
| 293 | } |
| 294 | |
| 295 | static struct coda_q_data *get_q_data(struct coda_ctx *ctx, |
| 296 | enum v4l2_buf_type type) |
| 297 | { |
| 298 | switch (type) { |
| 299 | case V4L2_BUF_TYPE_VIDEO_OUTPUT: |
| 300 | return &(ctx->q_data[V4L2_M2M_SRC]); |
| 301 | case V4L2_BUF_TYPE_VIDEO_CAPTURE: |
| 302 | return &(ctx->q_data[V4L2_M2M_DST]); |
| 303 | default: |
| 304 | BUG(); |
| 305 | } |
| 306 | return NULL; |
| 307 | } |
| 308 | |
| 309 | /* |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 310 | * Array of all formats supported by any version of Coda: |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 311 | */ |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 312 | static struct coda_fmt coda_formats[] = { |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 313 | { |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 314 | .name = "YUV 4:2:0 Planar, YCbCr", |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 315 | .fourcc = V4L2_PIX_FMT_YUV420, |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 316 | }, |
| 317 | { |
| 318 | .name = "YUV 4:2:0 Planar, YCrCb", |
| 319 | .fourcc = V4L2_PIX_FMT_YVU420, |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 320 | }, |
| 321 | { |
| 322 | .name = "H264 Encoded Stream", |
| 323 | .fourcc = V4L2_PIX_FMT_H264, |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 324 | }, |
| 325 | { |
| 326 | .name = "MPEG4 Encoded Stream", |
| 327 | .fourcc = V4L2_PIX_FMT_MPEG4, |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 328 | }, |
| 329 | }; |
| 330 | |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 331 | #define CODA_CODEC(mode, src_fourcc, dst_fourcc, max_w, max_h) \ |
| 332 | { mode, src_fourcc, dst_fourcc, max_w, max_h } |
| 333 | |
| 334 | /* |
| 335 | * Arrays of codecs supported by each given version of Coda: |
| 336 | * i.MX27 -> codadx6 |
| 337 | * i.MX5x -> coda7 |
| 338 | * i.MX6 -> coda960 |
| 339 | * Use V4L2_PIX_FMT_YUV420 as placeholder for all supported YUV 4:2:0 variants |
| 340 | */ |
| 341 | static struct coda_codec codadx6_codecs[] = { |
| 342 | CODA_CODEC(CODADX6_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 720, 576), |
| 343 | 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] | 344 | }; |
| 345 | |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 346 | static struct coda_codec coda7_codecs[] = { |
| 347 | CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1280, 720), |
| 348 | CODA_CODEC(CODA7_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1280, 720), |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 349 | CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1080), |
| 350 | CODA_CODEC(CODA7_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1080), |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 351 | }; |
| 352 | |
| 353 | static bool coda_format_is_yuv(u32 fourcc) |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 354 | { |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 355 | switch (fourcc) { |
| 356 | case V4L2_PIX_FMT_YUV420: |
| 357 | case V4L2_PIX_FMT_YVU420: |
| 358 | return true; |
| 359 | default: |
| 360 | return false; |
| 361 | } |
| 362 | } |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 363 | |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 364 | /* |
| 365 | * Normalize all supported YUV 4:2:0 formats to the value used in the codec |
| 366 | * tables. |
| 367 | */ |
| 368 | static u32 coda_format_normalize_yuv(u32 fourcc) |
| 369 | { |
| 370 | return coda_format_is_yuv(fourcc) ? V4L2_PIX_FMT_YUV420 : fourcc; |
| 371 | } |
| 372 | |
| 373 | static struct coda_codec *coda_find_codec(struct coda_dev *dev, int src_fourcc, |
| 374 | int dst_fourcc) |
| 375 | { |
| 376 | struct coda_codec *codecs = dev->devtype->codecs; |
| 377 | int num_codecs = dev->devtype->num_codecs; |
| 378 | int k; |
| 379 | |
| 380 | src_fourcc = coda_format_normalize_yuv(src_fourcc); |
| 381 | dst_fourcc = coda_format_normalize_yuv(dst_fourcc); |
| 382 | if (src_fourcc == dst_fourcc) |
| 383 | return NULL; |
| 384 | |
| 385 | for (k = 0; k < num_codecs; k++) { |
| 386 | if (codecs[k].src_fourcc == src_fourcc && |
| 387 | codecs[k].dst_fourcc == dst_fourcc) |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 388 | break; |
| 389 | } |
| 390 | |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 391 | if (k == num_codecs) |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 392 | return NULL; |
| 393 | |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 394 | return &codecs[k]; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | /* |
| 398 | * V4L2 ioctl() operations. |
| 399 | */ |
| 400 | static int vidioc_querycap(struct file *file, void *priv, |
| 401 | struct v4l2_capability *cap) |
| 402 | { |
| 403 | strlcpy(cap->driver, CODA_NAME, sizeof(cap->driver)); |
| 404 | strlcpy(cap->card, CODA_NAME, sizeof(cap->card)); |
Philipp Zabel | dad0e1c | 2013-05-21 04:18:22 -0300 | [diff] [blame] | 405 | strlcpy(cap->bus_info, "platform:" CODA_NAME, sizeof(cap->bus_info)); |
Sylwester Nawrocki | c3aac8b | 2012-08-22 18:00:19 -0300 | [diff] [blame] | 406 | /* |
| 407 | * This is only a mem-to-mem video device. The capture and output |
| 408 | * device capability flags are left only for backward compatibility |
| 409 | * and are scheduled for removal. |
| 410 | */ |
| 411 | cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT | |
| 412 | V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 413 | cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS; |
| 414 | |
| 415 | return 0; |
| 416 | } |
| 417 | |
| 418 | static int enum_fmt(void *priv, struct v4l2_fmtdesc *f, |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 419 | enum v4l2_buf_type type, int src_fourcc) |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 420 | { |
| 421 | struct coda_ctx *ctx = fh_to_ctx(priv); |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 422 | struct coda_codec *codecs = ctx->dev->devtype->codecs; |
| 423 | struct coda_fmt *formats = coda_formats; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 424 | struct coda_fmt *fmt; |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 425 | int num_codecs = ctx->dev->devtype->num_codecs; |
| 426 | int num_formats = ARRAY_SIZE(coda_formats); |
| 427 | int i, k, num = 0; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 428 | |
| 429 | for (i = 0; i < num_formats; i++) { |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 430 | /* Both uncompressed formats are always supported */ |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 431 | if (coda_format_is_yuv(formats[i].fourcc) && |
| 432 | !coda_format_is_yuv(src_fourcc)) { |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 433 | if (num == f->index) |
| 434 | break; |
| 435 | ++num; |
| 436 | continue; |
| 437 | } |
| 438 | /* Compressed formats may be supported, check the codec list */ |
| 439 | for (k = 0; k < num_codecs; k++) { |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 440 | /* if src_fourcc is set, only consider matching codecs */ |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 441 | if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE && |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 442 | formats[i].fourcc == codecs[k].dst_fourcc && |
| 443 | (!src_fourcc || src_fourcc == codecs[k].src_fourcc)) |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 444 | break; |
| 445 | if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT && |
| 446 | formats[i].fourcc == codecs[k].src_fourcc) |
| 447 | break; |
| 448 | } |
| 449 | if (k < num_codecs) { |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 450 | if (num == f->index) |
| 451 | break; |
| 452 | ++num; |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | if (i < num_formats) { |
| 457 | fmt = &formats[i]; |
| 458 | strlcpy(f->description, fmt->name, sizeof(f->description)); |
| 459 | f->pixelformat = fmt->fourcc; |
| 460 | return 0; |
| 461 | } |
| 462 | |
| 463 | /* Format not found */ |
| 464 | return -EINVAL; |
| 465 | } |
| 466 | |
| 467 | static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv, |
| 468 | struct v4l2_fmtdesc *f) |
| 469 | { |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 470 | struct coda_ctx *ctx = fh_to_ctx(priv); |
| 471 | struct vb2_queue *src_vq; |
| 472 | struct coda_q_data *q_data_src; |
| 473 | |
| 474 | /* If the source format is already fixed, only list matching formats */ |
| 475 | src_vq = v4l2_m2m_get_vq(ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT); |
| 476 | if (vb2_is_streaming(src_vq)) { |
| 477 | q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT); |
| 478 | |
| 479 | return enum_fmt(priv, f, V4L2_BUF_TYPE_VIDEO_CAPTURE, |
| 480 | q_data_src->fourcc); |
| 481 | } |
| 482 | |
| 483 | return enum_fmt(priv, f, V4L2_BUF_TYPE_VIDEO_CAPTURE, 0); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | static int vidioc_enum_fmt_vid_out(struct file *file, void *priv, |
| 487 | struct v4l2_fmtdesc *f) |
| 488 | { |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 489 | return enum_fmt(priv, f, V4L2_BUF_TYPE_VIDEO_OUTPUT, 0); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | static int vidioc_g_fmt(struct file *file, void *priv, struct v4l2_format *f) |
| 493 | { |
| 494 | struct vb2_queue *vq; |
| 495 | struct coda_q_data *q_data; |
| 496 | struct coda_ctx *ctx = fh_to_ctx(priv); |
| 497 | |
| 498 | vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type); |
| 499 | if (!vq) |
| 500 | return -EINVAL; |
| 501 | |
| 502 | q_data = get_q_data(ctx, f->type); |
| 503 | |
| 504 | f->fmt.pix.field = V4L2_FIELD_NONE; |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 505 | f->fmt.pix.pixelformat = q_data->fourcc; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 506 | f->fmt.pix.width = q_data->width; |
| 507 | f->fmt.pix.height = q_data->height; |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 508 | if (coda_format_is_yuv(f->fmt.pix.pixelformat)) |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 509 | f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 2); |
| 510 | else /* encoded formats h.264/mpeg4 */ |
| 511 | f->fmt.pix.bytesperline = 0; |
| 512 | |
| 513 | f->fmt.pix.sizeimage = q_data->sizeimage; |
| 514 | f->fmt.pix.colorspace = ctx->colorspace; |
| 515 | |
| 516 | return 0; |
| 517 | } |
| 518 | |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 519 | 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] | 520 | { |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 521 | unsigned int max_w, max_h; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 522 | enum v4l2_field field; |
| 523 | |
| 524 | field = f->fmt.pix.field; |
| 525 | if (field == V4L2_FIELD_ANY) |
| 526 | field = V4L2_FIELD_NONE; |
| 527 | else if (V4L2_FIELD_NONE != field) |
| 528 | return -EINVAL; |
| 529 | |
| 530 | /* V4L2 specification suggests the driver corrects the format struct |
| 531 | * if any of the dimensions is unsupported */ |
| 532 | f->fmt.pix.field = field; |
| 533 | |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 534 | if (codec) { |
| 535 | max_w = codec->max_w; |
| 536 | max_h = codec->max_h; |
| 537 | } else { |
| 538 | max_w = MAX_W; |
| 539 | max_h = MAX_H; |
| 540 | } |
| 541 | v4l_bound_align_image(&f->fmt.pix.width, MIN_W, max_w, |
| 542 | W_ALIGN, &f->fmt.pix.height, |
| 543 | MIN_H, max_h, H_ALIGN, S_ALIGN); |
| 544 | |
| 545 | if (coda_format_is_yuv(f->fmt.pix.pixelformat)) { |
Philipp Zabel | 47cf0c6 | 2013-05-23 10:42:54 -0300 | [diff] [blame] | 546 | /* Frame stride must be multiple of 8 */ |
| 547 | f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 8); |
| 548 | f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * |
Philipp Zabel | 451d43a | 2012-07-26 09:18:27 -0300 | [diff] [blame] | 549 | f->fmt.pix.height * 3 / 2; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 550 | } else { /*encoded formats h.264/mpeg4 */ |
| 551 | f->fmt.pix.bytesperline = 0; |
| 552 | f->fmt.pix.sizeimage = CODA_MAX_FRAME_SIZE; |
| 553 | } |
| 554 | |
| 555 | return 0; |
| 556 | } |
| 557 | |
| 558 | static int vidioc_try_fmt_vid_cap(struct file *file, void *priv, |
| 559 | struct v4l2_format *f) |
| 560 | { |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 561 | struct coda_ctx *ctx = fh_to_ctx(priv); |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 562 | struct coda_codec *codec; |
| 563 | struct vb2_queue *src_vq; |
| 564 | int ret; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 565 | |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 566 | /* |
| 567 | * If the source format is already fixed, try to find a codec that |
| 568 | * converts to the given destination format |
| 569 | */ |
| 570 | src_vq = v4l2_m2m_get_vq(ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT); |
| 571 | if (vb2_is_streaming(src_vq)) { |
| 572 | struct coda_q_data *q_data_src; |
| 573 | |
| 574 | q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT); |
| 575 | codec = coda_find_codec(ctx->dev, q_data_src->fourcc, |
| 576 | f->fmt.pix.pixelformat); |
| 577 | if (!codec) |
| 578 | return -EINVAL; |
| 579 | } else { |
| 580 | /* Otherwise determine codec by encoded format, if possible */ |
| 581 | codec = coda_find_codec(ctx->dev, V4L2_PIX_FMT_YUV420, |
| 582 | f->fmt.pix.pixelformat); |
| 583 | } |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 584 | |
| 585 | f->fmt.pix.colorspace = ctx->colorspace; |
| 586 | |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 587 | ret = vidioc_try_fmt(codec, f); |
| 588 | if (ret < 0) |
| 589 | return ret; |
| 590 | |
| 591 | /* The h.264 decoder only returns complete 16x16 macroblocks */ |
| 592 | if (codec && codec->src_fourcc == V4L2_PIX_FMT_H264) { |
| 593 | f->fmt.pix.width = round_up(f->fmt.pix.width, 16); |
| 594 | f->fmt.pix.height = round_up(f->fmt.pix.height, 16); |
| 595 | f->fmt.pix.bytesperline = f->fmt.pix.width; |
| 596 | f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * |
| 597 | f->fmt.pix.height * 3 / 2; |
| 598 | } |
| 599 | |
| 600 | return 0; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | static int vidioc_try_fmt_vid_out(struct file *file, void *priv, |
| 604 | struct v4l2_format *f) |
| 605 | { |
| 606 | struct coda_ctx *ctx = fh_to_ctx(priv); |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 607 | struct coda_codec *codec; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 608 | |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 609 | /* Determine codec by encoded format, returns NULL if raw or invalid */ |
| 610 | codec = coda_find_codec(ctx->dev, f->fmt.pix.pixelformat, |
| 611 | V4L2_PIX_FMT_YUV420); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 612 | |
| 613 | if (!f->fmt.pix.colorspace) |
| 614 | f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709; |
| 615 | |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 616 | return vidioc_try_fmt(codec, f); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | static int vidioc_s_fmt(struct coda_ctx *ctx, struct v4l2_format *f) |
| 620 | { |
| 621 | struct coda_q_data *q_data; |
| 622 | struct vb2_queue *vq; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 623 | |
| 624 | vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type); |
| 625 | if (!vq) |
| 626 | return -EINVAL; |
| 627 | |
| 628 | q_data = get_q_data(ctx, f->type); |
| 629 | if (!q_data) |
| 630 | return -EINVAL; |
| 631 | |
| 632 | if (vb2_is_busy(vq)) { |
| 633 | v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__); |
| 634 | return -EBUSY; |
| 635 | } |
| 636 | |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 637 | q_data->fourcc = f->fmt.pix.pixelformat; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 638 | q_data->width = f->fmt.pix.width; |
| 639 | q_data->height = f->fmt.pix.height; |
Philipp Zabel | 451d43a | 2012-07-26 09:18:27 -0300 | [diff] [blame] | 640 | q_data->sizeimage = f->fmt.pix.sizeimage; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 641 | |
| 642 | v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, |
| 643 | "Setting format for type %d, wxh: %dx%d, fmt: %d\n", |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 644 | f->type, q_data->width, q_data->height, q_data->fourcc); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 645 | |
| 646 | return 0; |
| 647 | } |
| 648 | |
| 649 | static int vidioc_s_fmt_vid_cap(struct file *file, void *priv, |
| 650 | struct v4l2_format *f) |
| 651 | { |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 652 | struct coda_ctx *ctx = fh_to_ctx(priv); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 653 | int ret; |
| 654 | |
| 655 | ret = vidioc_try_fmt_vid_cap(file, priv, f); |
| 656 | if (ret) |
| 657 | return ret; |
| 658 | |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 659 | return vidioc_s_fmt(ctx, f); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | static int vidioc_s_fmt_vid_out(struct file *file, void *priv, |
| 663 | struct v4l2_format *f) |
| 664 | { |
| 665 | struct coda_ctx *ctx = fh_to_ctx(priv); |
| 666 | int ret; |
| 667 | |
| 668 | ret = vidioc_try_fmt_vid_out(file, priv, f); |
| 669 | if (ret) |
| 670 | return ret; |
| 671 | |
Richard Zhao | 8d62147 | 2012-08-21 02:47:42 -0300 | [diff] [blame] | 672 | ret = vidioc_s_fmt(ctx, f); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 673 | if (ret) |
| 674 | ctx->colorspace = f->fmt.pix.colorspace; |
| 675 | |
| 676 | return ret; |
| 677 | } |
| 678 | |
| 679 | static int vidioc_reqbufs(struct file *file, void *priv, |
| 680 | struct v4l2_requestbuffers *reqbufs) |
| 681 | { |
| 682 | struct coda_ctx *ctx = fh_to_ctx(priv); |
| 683 | |
| 684 | return v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs); |
| 685 | } |
| 686 | |
| 687 | static int vidioc_querybuf(struct file *file, void *priv, |
| 688 | struct v4l2_buffer *buf) |
| 689 | { |
| 690 | struct coda_ctx *ctx = fh_to_ctx(priv); |
| 691 | |
| 692 | return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf); |
| 693 | } |
| 694 | |
| 695 | static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf) |
| 696 | { |
| 697 | struct coda_ctx *ctx = fh_to_ctx(priv); |
| 698 | |
| 699 | return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf); |
| 700 | } |
| 701 | |
Philipp Zabel | 419869c | 2013-05-23 07:06:30 -0300 | [diff] [blame] | 702 | static int vidioc_expbuf(struct file *file, void *priv, |
| 703 | struct v4l2_exportbuffer *eb) |
| 704 | { |
| 705 | struct coda_ctx *ctx = fh_to_ctx(priv); |
| 706 | |
| 707 | return v4l2_m2m_expbuf(file, ctx->m2m_ctx, eb); |
| 708 | } |
| 709 | |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 710 | static bool coda_buf_is_end_of_stream(struct coda_ctx *ctx, |
| 711 | struct v4l2_buffer *buf) |
| 712 | { |
| 713 | struct vb2_queue *src_vq; |
| 714 | |
| 715 | src_vq = v4l2_m2m_get_vq(ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT); |
| 716 | |
| 717 | return ((ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) && |
| 718 | (buf->sequence == (ctx->qsequence - 1))); |
| 719 | } |
| 720 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 721 | static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf) |
| 722 | { |
| 723 | struct coda_ctx *ctx = fh_to_ctx(priv); |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 724 | int ret; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 725 | |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 726 | ret = v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf); |
| 727 | |
| 728 | /* If this is the last capture buffer, emit an end-of-stream event */ |
| 729 | if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && |
| 730 | coda_buf_is_end_of_stream(ctx, buf)) { |
| 731 | const struct v4l2_event eos_event = { |
| 732 | .type = V4L2_EVENT_EOS |
| 733 | }; |
| 734 | |
| 735 | v4l2_event_queue_fh(&ctx->fh, &eos_event); |
| 736 | } |
| 737 | |
| 738 | return ret; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 739 | } |
| 740 | |
Philipp Zabel | 8fdf94a | 2013-05-21 04:16:29 -0300 | [diff] [blame] | 741 | static int vidioc_create_bufs(struct file *file, void *priv, |
| 742 | struct v4l2_create_buffers *create) |
| 743 | { |
| 744 | struct coda_ctx *ctx = fh_to_ctx(priv); |
| 745 | |
| 746 | return v4l2_m2m_create_bufs(file, ctx->m2m_ctx, create); |
| 747 | } |
| 748 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 749 | static int vidioc_streamon(struct file *file, void *priv, |
| 750 | enum v4l2_buf_type type) |
| 751 | { |
| 752 | struct coda_ctx *ctx = fh_to_ctx(priv); |
| 753 | |
| 754 | return v4l2_m2m_streamon(file, ctx->m2m_ctx, type); |
| 755 | } |
| 756 | |
| 757 | static int vidioc_streamoff(struct file *file, void *priv, |
| 758 | enum v4l2_buf_type type) |
| 759 | { |
| 760 | struct coda_ctx *ctx = fh_to_ctx(priv); |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 761 | int ret; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 762 | |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 763 | /* |
| 764 | * This indirectly calls __vb2_queue_cancel, which dequeues all buffers. |
| 765 | * We therefore have to lock it against running hardware in this context, |
| 766 | * which still needs the buffers. |
| 767 | */ |
| 768 | mutex_lock(&ctx->buffer_mutex); |
| 769 | ret = v4l2_m2m_streamoff(file, ctx->m2m_ctx, type); |
| 770 | mutex_unlock(&ctx->buffer_mutex); |
| 771 | |
| 772 | return ret; |
| 773 | } |
| 774 | |
| 775 | static int vidioc_decoder_cmd(struct file *file, void *fh, |
| 776 | struct v4l2_decoder_cmd *dc) |
| 777 | { |
| 778 | struct coda_ctx *ctx = fh_to_ctx(fh); |
| 779 | |
| 780 | if (dc->cmd != V4L2_DEC_CMD_STOP) |
| 781 | return -EINVAL; |
| 782 | |
| 783 | if ((dc->flags & V4L2_DEC_CMD_STOP_TO_BLACK) || |
| 784 | (dc->flags & V4L2_DEC_CMD_STOP_IMMEDIATELY)) |
| 785 | return -EINVAL; |
| 786 | |
| 787 | if (dc->stop.pts != 0) |
| 788 | return -EINVAL; |
| 789 | |
| 790 | if (ctx->inst_type != CODA_INST_DECODER) |
| 791 | return -EINVAL; |
| 792 | |
| 793 | /* Set the strem-end flag on this context */ |
| 794 | ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG; |
| 795 | |
| 796 | return 0; |
| 797 | } |
| 798 | |
| 799 | static int vidioc_subscribe_event(struct v4l2_fh *fh, |
| 800 | const struct v4l2_event_subscription *sub) |
| 801 | { |
| 802 | switch (sub->type) { |
| 803 | case V4L2_EVENT_EOS: |
| 804 | return v4l2_event_subscribe(fh, sub, 0, NULL); |
| 805 | default: |
| 806 | return v4l2_ctrl_subscribe_event(fh, sub); |
| 807 | } |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 808 | } |
| 809 | |
| 810 | static const struct v4l2_ioctl_ops coda_ioctl_ops = { |
| 811 | .vidioc_querycap = vidioc_querycap, |
| 812 | |
| 813 | .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap, |
| 814 | .vidioc_g_fmt_vid_cap = vidioc_g_fmt, |
| 815 | .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap, |
| 816 | .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap, |
| 817 | |
| 818 | .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out, |
| 819 | .vidioc_g_fmt_vid_out = vidioc_g_fmt, |
| 820 | .vidioc_try_fmt_vid_out = vidioc_try_fmt_vid_out, |
| 821 | .vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out, |
| 822 | |
| 823 | .vidioc_reqbufs = vidioc_reqbufs, |
| 824 | .vidioc_querybuf = vidioc_querybuf, |
| 825 | |
| 826 | .vidioc_qbuf = vidioc_qbuf, |
Philipp Zabel | 419869c | 2013-05-23 07:06:30 -0300 | [diff] [blame] | 827 | .vidioc_expbuf = vidioc_expbuf, |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 828 | .vidioc_dqbuf = vidioc_dqbuf, |
Philipp Zabel | 8fdf94a | 2013-05-21 04:16:29 -0300 | [diff] [blame] | 829 | .vidioc_create_bufs = vidioc_create_bufs, |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 830 | |
| 831 | .vidioc_streamon = vidioc_streamon, |
| 832 | .vidioc_streamoff = vidioc_streamoff, |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 833 | |
| 834 | .vidioc_decoder_cmd = vidioc_decoder_cmd, |
| 835 | |
| 836 | .vidioc_subscribe_event = vidioc_subscribe_event, |
| 837 | .vidioc_unsubscribe_event = v4l2_event_unsubscribe, |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 838 | }; |
| 839 | |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 840 | static int coda_start_decoding(struct coda_ctx *ctx); |
| 841 | |
| 842 | static void coda_skip_run(struct work_struct *work) |
| 843 | { |
| 844 | struct coda_ctx *ctx = container_of(work, struct coda_ctx, skip_run); |
| 845 | |
| 846 | v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->m2m_ctx); |
| 847 | } |
| 848 | |
Philipp Zabel | 9082a7c | 2013-06-21 03:55:31 -0300 | [diff] [blame] | 849 | static inline int coda_get_bitstream_payload(struct coda_ctx *ctx) |
| 850 | { |
| 851 | return kfifo_len(&ctx->bitstream_fifo); |
| 852 | } |
| 853 | |
| 854 | static void coda_kfifo_sync_from_device(struct coda_ctx *ctx) |
| 855 | { |
| 856 | struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo; |
| 857 | struct coda_dev *dev = ctx->dev; |
| 858 | u32 rd_ptr; |
| 859 | |
| 860 | rd_ptr = coda_read(dev, CODA_REG_BIT_RD_PTR(ctx->reg_idx)); |
| 861 | kfifo->out = (kfifo->in & ~kfifo->mask) | |
| 862 | (rd_ptr - ctx->bitstream.paddr); |
| 863 | if (kfifo->out > kfifo->in) |
| 864 | kfifo->out -= kfifo->mask + 1; |
| 865 | } |
| 866 | |
| 867 | static void coda_kfifo_sync_to_device_full(struct coda_ctx *ctx) |
| 868 | { |
| 869 | struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo; |
| 870 | struct coda_dev *dev = ctx->dev; |
| 871 | u32 rd_ptr, wr_ptr; |
| 872 | |
| 873 | rd_ptr = ctx->bitstream.paddr + (kfifo->out & kfifo->mask); |
| 874 | coda_write(dev, rd_ptr, CODA_REG_BIT_RD_PTR(ctx->reg_idx)); |
| 875 | wr_ptr = ctx->bitstream.paddr + (kfifo->in & kfifo->mask); |
| 876 | coda_write(dev, wr_ptr, CODA_REG_BIT_WR_PTR(ctx->reg_idx)); |
| 877 | } |
| 878 | |
| 879 | static void coda_kfifo_sync_to_device_write(struct coda_ctx *ctx) |
| 880 | { |
| 881 | struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo; |
| 882 | struct coda_dev *dev = ctx->dev; |
| 883 | u32 wr_ptr; |
| 884 | |
| 885 | wr_ptr = ctx->bitstream.paddr + (kfifo->in & kfifo->mask); |
| 886 | coda_write(dev, wr_ptr, CODA_REG_BIT_WR_PTR(ctx->reg_idx)); |
| 887 | } |
| 888 | |
| 889 | static int coda_bitstream_queue(struct coda_ctx *ctx, struct vb2_buffer *src_buf) |
| 890 | { |
| 891 | u32 src_size = vb2_get_plane_payload(src_buf, 0); |
| 892 | u32 n; |
| 893 | |
| 894 | n = kfifo_in(&ctx->bitstream_fifo, vb2_plane_vaddr(src_buf, 0), src_size); |
| 895 | if (n < src_size) |
| 896 | return -ENOSPC; |
| 897 | |
| 898 | dma_sync_single_for_device(&ctx->dev->plat_dev->dev, ctx->bitstream.paddr, |
| 899 | ctx->bitstream.size, DMA_TO_DEVICE); |
| 900 | |
| 901 | ctx->qsequence++; |
| 902 | |
| 903 | return 0; |
| 904 | } |
| 905 | |
| 906 | static bool coda_bitstream_try_queue(struct coda_ctx *ctx, |
| 907 | struct vb2_buffer *src_buf) |
| 908 | { |
| 909 | int ret; |
| 910 | |
| 911 | if (coda_get_bitstream_payload(ctx) + |
| 912 | vb2_get_plane_payload(src_buf, 0) + 512 >= ctx->bitstream.size) |
| 913 | return false; |
| 914 | |
| 915 | if (vb2_plane_vaddr(src_buf, 0) == NULL) { |
| 916 | v4l2_err(&ctx->dev->v4l2_dev, "trying to queue empty buffer\n"); |
| 917 | return true; |
| 918 | } |
| 919 | |
| 920 | ret = coda_bitstream_queue(ctx, src_buf); |
| 921 | if (ret < 0) { |
| 922 | v4l2_err(&ctx->dev->v4l2_dev, "bitstream buffer overflow\n"); |
| 923 | return false; |
| 924 | } |
| 925 | /* Sync read pointer to device */ |
| 926 | if (ctx == v4l2_m2m_get_curr_priv(ctx->dev->m2m_dev)) |
| 927 | coda_kfifo_sync_to_device_write(ctx); |
| 928 | |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 929 | ctx->prescan_failed = false; |
| 930 | |
Philipp Zabel | 9082a7c | 2013-06-21 03:55:31 -0300 | [diff] [blame] | 931 | return true; |
| 932 | } |
| 933 | |
| 934 | static void coda_fill_bitstream(struct coda_ctx *ctx) |
| 935 | { |
| 936 | struct vb2_buffer *src_buf; |
| 937 | |
| 938 | while (v4l2_m2m_num_src_bufs_ready(ctx->m2m_ctx) > 0) { |
| 939 | src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx); |
| 940 | |
| 941 | if (coda_bitstream_try_queue(ctx, src_buf)) { |
| 942 | src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx); |
| 943 | v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE); |
| 944 | } else { |
| 945 | break; |
| 946 | } |
| 947 | } |
| 948 | } |
| 949 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 950 | /* |
| 951 | * Mem-to-mem operations. |
| 952 | */ |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 953 | static int coda_prepare_decode(struct coda_ctx *ctx) |
| 954 | { |
| 955 | struct vb2_buffer *dst_buf; |
| 956 | struct coda_dev *dev = ctx->dev; |
| 957 | struct coda_q_data *q_data_dst; |
| 958 | u32 stridey, height; |
| 959 | u32 picture_y, picture_cb, picture_cr; |
| 960 | |
| 961 | dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx); |
| 962 | q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE); |
| 963 | |
| 964 | if (ctx->params.rot_mode & CODA_ROT_90) { |
| 965 | stridey = q_data_dst->height; |
| 966 | height = q_data_dst->width; |
| 967 | } else { |
| 968 | stridey = q_data_dst->width; |
| 969 | height = q_data_dst->height; |
| 970 | } |
| 971 | |
| 972 | /* Try to copy source buffer contents into the bitstream ringbuffer */ |
| 973 | mutex_lock(&ctx->bitstream_mutex); |
| 974 | coda_fill_bitstream(ctx); |
| 975 | mutex_unlock(&ctx->bitstream_mutex); |
| 976 | |
| 977 | if (coda_get_bitstream_payload(ctx) < 512 && |
| 978 | (!(ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG))) { |
| 979 | v4l2_dbg(1, coda_debug, &dev->v4l2_dev, |
| 980 | "bitstream payload: %d, skipping\n", |
| 981 | coda_get_bitstream_payload(ctx)); |
| 982 | schedule_work(&ctx->skip_run); |
| 983 | return -EAGAIN; |
| 984 | } |
| 985 | |
| 986 | /* Run coda_start_decoding (again) if not yet initialized */ |
| 987 | if (!ctx->initialized) { |
| 988 | int ret = coda_start_decoding(ctx); |
| 989 | if (ret < 0) { |
| 990 | v4l2_err(&dev->v4l2_dev, "failed to start decoding\n"); |
| 991 | schedule_work(&ctx->skip_run); |
| 992 | return -EAGAIN; |
| 993 | } else { |
| 994 | ctx->initialized = 1; |
| 995 | } |
| 996 | } |
| 997 | |
| 998 | /* Set rotator output */ |
| 999 | picture_y = vb2_dma_contig_plane_dma_addr(dst_buf, 0); |
| 1000 | if (q_data_dst->fourcc == V4L2_PIX_FMT_YVU420) { |
| 1001 | /* Switch Cr and Cb for YVU420 format */ |
| 1002 | picture_cr = picture_y + stridey * height; |
| 1003 | picture_cb = picture_cr + stridey / 2 * height / 2; |
| 1004 | } else { |
| 1005 | picture_cb = picture_y + stridey * height; |
| 1006 | picture_cr = picture_cb + stridey / 2 * height / 2; |
| 1007 | } |
| 1008 | coda_write(dev, picture_y, CODA_CMD_DEC_PIC_ROT_ADDR_Y); |
| 1009 | coda_write(dev, picture_cb, CODA_CMD_DEC_PIC_ROT_ADDR_CB); |
| 1010 | coda_write(dev, picture_cr, CODA_CMD_DEC_PIC_ROT_ADDR_CR); |
| 1011 | coda_write(dev, stridey, CODA_CMD_DEC_PIC_ROT_STRIDE); |
| 1012 | coda_write(dev, CODA_ROT_MIR_ENABLE | ctx->params.rot_mode, |
| 1013 | CODA_CMD_DEC_PIC_ROT_MODE); |
| 1014 | |
| 1015 | switch (dev->devtype->product) { |
| 1016 | case CODA_DX6: |
| 1017 | /* TBD */ |
| 1018 | case CODA_7541: |
| 1019 | coda_write(dev, CODA_PRE_SCAN_EN, CODA_CMD_DEC_PIC_OPTION); |
| 1020 | break; |
| 1021 | } |
| 1022 | |
| 1023 | coda_write(dev, 0, CODA_CMD_DEC_PIC_SKIP_NUM); |
| 1024 | |
| 1025 | coda_write(dev, 0, CODA_CMD_DEC_PIC_BB_START); |
| 1026 | coda_write(dev, 0, CODA_CMD_DEC_PIC_START_BYTE); |
| 1027 | |
| 1028 | return 0; |
| 1029 | } |
| 1030 | |
Philipp Zabel | 477c1cf | 2013-06-21 03:55:33 -0300 | [diff] [blame] | 1031 | static void coda_prepare_encode(struct coda_ctx *ctx) |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1032 | { |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1033 | struct coda_q_data *q_data_src, *q_data_dst; |
| 1034 | struct vb2_buffer *src_buf, *dst_buf; |
| 1035 | struct coda_dev *dev = ctx->dev; |
| 1036 | int force_ipicture; |
| 1037 | int quant_param = 0; |
| 1038 | u32 picture_y, picture_cb, picture_cr; |
| 1039 | u32 pic_stream_buffer_addr, pic_stream_buffer_size; |
| 1040 | u32 dst_fourcc; |
| 1041 | |
| 1042 | src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx); |
| 1043 | dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx); |
| 1044 | q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT); |
| 1045 | q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE); |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 1046 | dst_fourcc = q_data_dst->fourcc; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1047 | |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 1048 | src_buf->v4l2_buf.sequence = ctx->osequence; |
| 1049 | dst_buf->v4l2_buf.sequence = ctx->osequence; |
| 1050 | ctx->osequence++; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1051 | |
| 1052 | /* |
| 1053 | * Workaround coda firmware BUG that only marks the first |
| 1054 | * frame as IDR. This is a problem for some decoders that can't |
| 1055 | * recover when a frame is lost. |
| 1056 | */ |
| 1057 | if (src_buf->v4l2_buf.sequence % ctx->params.gop_size) { |
| 1058 | src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME; |
| 1059 | src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME; |
| 1060 | } else { |
| 1061 | src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME; |
| 1062 | src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME; |
| 1063 | } |
| 1064 | |
| 1065 | /* |
| 1066 | * Copy headers at the beginning of the first frame for H.264 only. |
| 1067 | * In MPEG4 they are already copied by the coda. |
| 1068 | */ |
| 1069 | if (src_buf->v4l2_buf.sequence == 0) { |
| 1070 | pic_stream_buffer_addr = |
| 1071 | vb2_dma_contig_plane_dma_addr(dst_buf, 0) + |
| 1072 | ctx->vpu_header_size[0] + |
| 1073 | ctx->vpu_header_size[1] + |
| 1074 | ctx->vpu_header_size[2]; |
| 1075 | pic_stream_buffer_size = CODA_MAX_FRAME_SIZE - |
| 1076 | ctx->vpu_header_size[0] - |
| 1077 | ctx->vpu_header_size[1] - |
| 1078 | ctx->vpu_header_size[2]; |
| 1079 | memcpy(vb2_plane_vaddr(dst_buf, 0), |
| 1080 | &ctx->vpu_header[0][0], ctx->vpu_header_size[0]); |
| 1081 | memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0], |
| 1082 | &ctx->vpu_header[1][0], ctx->vpu_header_size[1]); |
| 1083 | memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0] + |
| 1084 | ctx->vpu_header_size[1], &ctx->vpu_header[2][0], |
| 1085 | ctx->vpu_header_size[2]); |
| 1086 | } else { |
| 1087 | pic_stream_buffer_addr = |
| 1088 | vb2_dma_contig_plane_dma_addr(dst_buf, 0); |
| 1089 | pic_stream_buffer_size = CODA_MAX_FRAME_SIZE; |
| 1090 | } |
| 1091 | |
| 1092 | if (src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) { |
| 1093 | force_ipicture = 1; |
| 1094 | switch (dst_fourcc) { |
| 1095 | case V4L2_PIX_FMT_H264: |
| 1096 | quant_param = ctx->params.h264_intra_qp; |
| 1097 | break; |
| 1098 | case V4L2_PIX_FMT_MPEG4: |
| 1099 | quant_param = ctx->params.mpeg4_intra_qp; |
| 1100 | break; |
| 1101 | default: |
| 1102 | v4l2_warn(&ctx->dev->v4l2_dev, |
| 1103 | "cannot set intra qp, fmt not supported\n"); |
| 1104 | break; |
| 1105 | } |
| 1106 | } else { |
| 1107 | force_ipicture = 0; |
| 1108 | switch (dst_fourcc) { |
| 1109 | case V4L2_PIX_FMT_H264: |
| 1110 | quant_param = ctx->params.h264_inter_qp; |
| 1111 | break; |
| 1112 | case V4L2_PIX_FMT_MPEG4: |
| 1113 | quant_param = ctx->params.mpeg4_inter_qp; |
| 1114 | break; |
| 1115 | default: |
| 1116 | v4l2_warn(&ctx->dev->v4l2_dev, |
| 1117 | "cannot set inter qp, fmt not supported\n"); |
| 1118 | break; |
| 1119 | } |
| 1120 | } |
| 1121 | |
| 1122 | /* submit */ |
Philipp Zabel | 8f35c7b | 2012-07-09 04:25:52 -0300 | [diff] [blame] | 1123 | 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] | 1124 | coda_write(dev, quant_param, CODA_CMD_ENC_PIC_QS); |
| 1125 | |
| 1126 | |
| 1127 | picture_y = vb2_dma_contig_plane_dma_addr(src_buf, 0); |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 1128 | switch (q_data_src->fourcc) { |
| 1129 | case V4L2_PIX_FMT_YVU420: |
| 1130 | /* Switch Cb and Cr for YVU420 format */ |
| 1131 | picture_cr = picture_y + q_data_src->width * q_data_src->height; |
| 1132 | picture_cb = picture_cr + q_data_src->width / 2 * |
| 1133 | q_data_src->height / 2; |
| 1134 | break; |
| 1135 | case V4L2_PIX_FMT_YUV420: |
| 1136 | default: |
| 1137 | picture_cb = picture_y + q_data_src->width * q_data_src->height; |
| 1138 | picture_cr = picture_cb + q_data_src->width / 2 * |
| 1139 | q_data_src->height / 2; |
| 1140 | break; |
| 1141 | } |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1142 | |
| 1143 | coda_write(dev, picture_y, CODA_CMD_ENC_PIC_SRC_ADDR_Y); |
| 1144 | coda_write(dev, picture_cb, CODA_CMD_ENC_PIC_SRC_ADDR_CB); |
| 1145 | coda_write(dev, picture_cr, CODA_CMD_ENC_PIC_SRC_ADDR_CR); |
| 1146 | coda_write(dev, force_ipicture << 1 & 0x2, |
| 1147 | CODA_CMD_ENC_PIC_OPTION); |
| 1148 | |
| 1149 | coda_write(dev, pic_stream_buffer_addr, CODA_CMD_ENC_PIC_BB_START); |
| 1150 | coda_write(dev, pic_stream_buffer_size / 1024, |
| 1151 | CODA_CMD_ENC_PIC_BB_SIZE); |
Philipp Zabel | 477c1cf | 2013-06-21 03:55:33 -0300 | [diff] [blame] | 1152 | } |
| 1153 | |
| 1154 | static void coda_device_run(void *m2m_priv) |
| 1155 | { |
| 1156 | struct coda_ctx *ctx = m2m_priv; |
| 1157 | struct coda_dev *dev = ctx->dev; |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 1158 | int ret; |
| 1159 | |
| 1160 | mutex_lock(&ctx->buffer_mutex); |
| 1161 | |
| 1162 | /* |
| 1163 | * If streamoff dequeued all buffers before we could get the lock, |
| 1164 | * just bail out immediately. |
| 1165 | */ |
| 1166 | if ((!v4l2_m2m_num_src_bufs_ready(ctx->m2m_ctx) && |
| 1167 | ctx->inst_type != CODA_INST_DECODER) || |
| 1168 | !v4l2_m2m_num_dst_bufs_ready(ctx->m2m_ctx)) { |
| 1169 | v4l2_dbg(1, coda_debug, &dev->v4l2_dev, |
| 1170 | "%d: device_run without buffers\n", ctx->idx); |
| 1171 | mutex_unlock(&ctx->buffer_mutex); |
| 1172 | schedule_work(&ctx->skip_run); |
| 1173 | return; |
| 1174 | } |
Philipp Zabel | 477c1cf | 2013-06-21 03:55:33 -0300 | [diff] [blame] | 1175 | |
| 1176 | mutex_lock(&dev->coda_mutex); |
| 1177 | |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 1178 | if (ctx->inst_type == CODA_INST_DECODER) { |
| 1179 | ret = coda_prepare_decode(ctx); |
| 1180 | if (ret < 0) { |
| 1181 | mutex_unlock(&dev->coda_mutex); |
| 1182 | mutex_unlock(&ctx->buffer_mutex); |
| 1183 | /* job_finish scheduled by prepare_decode */ |
| 1184 | return; |
| 1185 | } |
| 1186 | } else { |
| 1187 | coda_prepare_encode(ctx); |
Philipp Zabel | 1043667 | 2012-07-02 09:03:55 -0300 | [diff] [blame] | 1188 | } |
| 1189 | |
Philipp Zabel | c2d2251 | 2013-06-21 03:55:28 -0300 | [diff] [blame] | 1190 | if (dev->devtype->product != CODA_DX6) |
| 1191 | coda_write(dev, ctx->iram_info.axi_sram_use, |
| 1192 | CODA7_REG_BIT_AXI_SRAM_USE); |
| 1193 | |
Philipp Zabel | 2fb57f0 | 2012-07-25 09:22:07 -0300 | [diff] [blame] | 1194 | /* 1 second timeout in case CODA locks up */ |
| 1195 | schedule_delayed_work(&dev->timeout, HZ); |
| 1196 | |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 1197 | if (ctx->inst_type == CODA_INST_DECODER) |
| 1198 | coda_kfifo_sync_to_device_full(ctx); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1199 | coda_command_async(ctx, CODA_COMMAND_PIC_RUN); |
| 1200 | } |
| 1201 | |
| 1202 | static int coda_job_ready(void *m2m_priv) |
| 1203 | { |
| 1204 | struct coda_ctx *ctx = m2m_priv; |
| 1205 | |
| 1206 | /* |
| 1207 | * For both 'P' and 'key' frame cases 1 picture |
Philipp Zabel | 9082a7c | 2013-06-21 03:55:31 -0300 | [diff] [blame] | 1208 | * and 1 frame are needed. In the decoder case, |
| 1209 | * the compressed frame can be in the bitstream. |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1210 | */ |
Philipp Zabel | 9082a7c | 2013-06-21 03:55:31 -0300 | [diff] [blame] | 1211 | if (!v4l2_m2m_num_src_bufs_ready(ctx->m2m_ctx) && |
| 1212 | ctx->inst_type != CODA_INST_DECODER) { |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1213 | v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, |
| 1214 | "not ready: not enough video buffers.\n"); |
| 1215 | return 0; |
| 1216 | } |
| 1217 | |
Philipp Zabel | 9082a7c | 2013-06-21 03:55:31 -0300 | [diff] [blame] | 1218 | if (!v4l2_m2m_num_dst_bufs_ready(ctx->m2m_ctx)) { |
| 1219 | v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, |
| 1220 | "not ready: not enough video capture buffers.\n"); |
| 1221 | return 0; |
| 1222 | } |
| 1223 | |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 1224 | if (ctx->prescan_failed || |
| 1225 | ((ctx->inst_type == CODA_INST_DECODER) && |
| 1226 | (coda_get_bitstream_payload(ctx) < 512) && |
| 1227 | !(ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG))) { |
| 1228 | v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, |
| 1229 | "%d: not ready: not enough bitstream data.\n", |
| 1230 | ctx->idx); |
| 1231 | return 0; |
| 1232 | } |
| 1233 | |
Philipp Zabel | 3e74826 | 2013-05-23 10:43:01 -0300 | [diff] [blame] | 1234 | if (ctx->aborting) { |
| 1235 | v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, |
| 1236 | "not ready: aborting\n"); |
| 1237 | return 0; |
| 1238 | } |
| 1239 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1240 | v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, |
| 1241 | "job ready\n"); |
| 1242 | return 1; |
| 1243 | } |
| 1244 | |
| 1245 | static void coda_job_abort(void *priv) |
| 1246 | { |
| 1247 | struct coda_ctx *ctx = priv; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1248 | |
| 1249 | ctx->aborting = 1; |
| 1250 | |
| 1251 | v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, |
| 1252 | "Aborting task\n"); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1253 | } |
| 1254 | |
| 1255 | static void coda_lock(void *m2m_priv) |
| 1256 | { |
| 1257 | struct coda_ctx *ctx = m2m_priv; |
| 1258 | struct coda_dev *pcdev = ctx->dev; |
| 1259 | mutex_lock(&pcdev->dev_mutex); |
| 1260 | } |
| 1261 | |
| 1262 | static void coda_unlock(void *m2m_priv) |
| 1263 | { |
| 1264 | struct coda_ctx *ctx = m2m_priv; |
| 1265 | struct coda_dev *pcdev = ctx->dev; |
| 1266 | mutex_unlock(&pcdev->dev_mutex); |
| 1267 | } |
| 1268 | |
| 1269 | static struct v4l2_m2m_ops coda_m2m_ops = { |
| 1270 | .device_run = coda_device_run, |
| 1271 | .job_ready = coda_job_ready, |
| 1272 | .job_abort = coda_job_abort, |
| 1273 | .lock = coda_lock, |
| 1274 | .unlock = coda_unlock, |
| 1275 | }; |
| 1276 | |
| 1277 | static void set_default_params(struct coda_ctx *ctx) |
| 1278 | { |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 1279 | int max_w; |
| 1280 | int max_h; |
| 1281 | |
| 1282 | ctx->codec = &ctx->dev->devtype->codecs[0]; |
| 1283 | max_w = ctx->codec->max_w; |
| 1284 | max_h = ctx->codec->max_h; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1285 | |
| 1286 | ctx->params.codec_mode = CODA_MODE_INVALID; |
| 1287 | ctx->colorspace = V4L2_COLORSPACE_REC709; |
| 1288 | ctx->params.framerate = 30; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1289 | ctx->aborting = 0; |
| 1290 | |
| 1291 | /* Default formats for output and input queues */ |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 1292 | ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->codec->src_fourcc; |
| 1293 | ctx->q_data[V4L2_M2M_DST].fourcc = ctx->codec->dst_fourcc; |
| 1294 | ctx->q_data[V4L2_M2M_SRC].width = max_w; |
| 1295 | ctx->q_data[V4L2_M2M_SRC].height = max_h; |
| 1296 | ctx->q_data[V4L2_M2M_SRC].sizeimage = (max_w * max_h * 3) / 2; |
| 1297 | ctx->q_data[V4L2_M2M_DST].width = max_w; |
| 1298 | ctx->q_data[V4L2_M2M_DST].height = max_h; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1299 | ctx->q_data[V4L2_M2M_DST].sizeimage = CODA_MAX_FRAME_SIZE; |
| 1300 | } |
| 1301 | |
| 1302 | /* |
| 1303 | * Queue operations |
| 1304 | */ |
| 1305 | static int coda_queue_setup(struct vb2_queue *vq, |
| 1306 | const struct v4l2_format *fmt, |
| 1307 | unsigned int *nbuffers, unsigned int *nplanes, |
| 1308 | unsigned int sizes[], void *alloc_ctxs[]) |
| 1309 | { |
| 1310 | struct coda_ctx *ctx = vb2_get_drv_priv(vq); |
Philipp Zabel | e34db06 | 2012-08-29 08:22:00 -0300 | [diff] [blame] | 1311 | struct coda_q_data *q_data; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1312 | unsigned int size; |
| 1313 | |
Philipp Zabel | e34db06 | 2012-08-29 08:22:00 -0300 | [diff] [blame] | 1314 | q_data = get_q_data(ctx, vq->type); |
| 1315 | size = q_data->sizeimage; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1316 | |
| 1317 | *nplanes = 1; |
| 1318 | sizes[0] = size; |
| 1319 | |
| 1320 | alloc_ctxs[0] = ctx->dev->alloc_ctx; |
| 1321 | |
| 1322 | v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, |
| 1323 | "get %d buffer(s) of size %d each.\n", *nbuffers, size); |
| 1324 | |
| 1325 | return 0; |
| 1326 | } |
| 1327 | |
| 1328 | static int coda_buf_prepare(struct vb2_buffer *vb) |
| 1329 | { |
| 1330 | struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); |
| 1331 | struct coda_q_data *q_data; |
| 1332 | |
| 1333 | q_data = get_q_data(ctx, vb->vb2_queue->type); |
| 1334 | |
| 1335 | if (vb2_plane_size(vb, 0) < q_data->sizeimage) { |
| 1336 | v4l2_warn(&ctx->dev->v4l2_dev, |
| 1337 | "%s data will not fit into plane (%lu < %lu)\n", |
| 1338 | __func__, vb2_plane_size(vb, 0), |
| 1339 | (long)q_data->sizeimage); |
| 1340 | return -EINVAL; |
| 1341 | } |
| 1342 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1343 | return 0; |
| 1344 | } |
| 1345 | |
| 1346 | static void coda_buf_queue(struct vb2_buffer *vb) |
| 1347 | { |
| 1348 | struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 1349 | struct coda_q_data *q_data; |
| 1350 | |
| 1351 | q_data = get_q_data(ctx, vb->vb2_queue->type); |
| 1352 | |
| 1353 | /* |
| 1354 | * In the decoder case, immediately try to copy the buffer into the |
| 1355 | * bitstream ringbuffer and mark it as ready to be dequeued. |
| 1356 | */ |
| 1357 | if (q_data->fourcc == V4L2_PIX_FMT_H264 && |
| 1358 | vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) { |
| 1359 | /* |
| 1360 | * For backwards compatiblity, queuing an empty buffer marks |
| 1361 | * the stream end |
| 1362 | */ |
| 1363 | if (vb2_get_plane_payload(vb, 0) == 0) |
| 1364 | ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG; |
| 1365 | mutex_lock(&ctx->bitstream_mutex); |
| 1366 | v4l2_m2m_buf_queue(ctx->m2m_ctx, vb); |
| 1367 | coda_fill_bitstream(ctx); |
| 1368 | mutex_unlock(&ctx->bitstream_mutex); |
| 1369 | } else { |
| 1370 | v4l2_m2m_buf_queue(ctx->m2m_ctx, vb); |
| 1371 | } |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1372 | } |
| 1373 | |
| 1374 | static void coda_wait_prepare(struct vb2_queue *q) |
| 1375 | { |
| 1376 | struct coda_ctx *ctx = vb2_get_drv_priv(q); |
| 1377 | coda_unlock(ctx); |
| 1378 | } |
| 1379 | |
| 1380 | static void coda_wait_finish(struct vb2_queue *q) |
| 1381 | { |
| 1382 | struct coda_ctx *ctx = vb2_get_drv_priv(q); |
| 1383 | coda_lock(ctx); |
| 1384 | } |
| 1385 | |
Philipp Zabel | 86eda90 | 2013-05-23 10:42:57 -0300 | [diff] [blame] | 1386 | static void coda_parabuf_write(struct coda_ctx *ctx, int index, u32 value) |
| 1387 | { |
| 1388 | struct coda_dev *dev = ctx->dev; |
| 1389 | u32 *p = ctx->parabuf.vaddr; |
| 1390 | |
| 1391 | if (dev->devtype->product == CODA_DX6) |
| 1392 | p[index] = value; |
| 1393 | else |
| 1394 | p[index ^ 1] = value; |
| 1395 | } |
| 1396 | |
Philipp Zabel | 5677e3b | 2013-06-21 03:55:30 -0300 | [diff] [blame] | 1397 | static int coda_alloc_aux_buf(struct coda_dev *dev, |
| 1398 | struct coda_aux_buf *buf, size_t size) |
| 1399 | { |
| 1400 | buf->vaddr = dma_alloc_coherent(&dev->plat_dev->dev, size, &buf->paddr, |
| 1401 | GFP_KERNEL); |
| 1402 | if (!buf->vaddr) |
| 1403 | return -ENOMEM; |
| 1404 | |
| 1405 | buf->size = size; |
| 1406 | |
| 1407 | return 0; |
| 1408 | } |
| 1409 | |
| 1410 | static inline int coda_alloc_context_buf(struct coda_ctx *ctx, |
| 1411 | struct coda_aux_buf *buf, size_t size) |
| 1412 | { |
| 1413 | return coda_alloc_aux_buf(ctx->dev, buf, size); |
| 1414 | } |
| 1415 | |
| 1416 | static void coda_free_aux_buf(struct coda_dev *dev, |
| 1417 | struct coda_aux_buf *buf) |
| 1418 | { |
| 1419 | if (buf->vaddr) { |
| 1420 | dma_free_coherent(&dev->plat_dev->dev, buf->size, |
| 1421 | buf->vaddr, buf->paddr); |
| 1422 | buf->vaddr = NULL; |
| 1423 | buf->size = 0; |
| 1424 | } |
| 1425 | } |
| 1426 | |
| 1427 | static void coda_free_framebuffers(struct coda_ctx *ctx) |
| 1428 | { |
| 1429 | int i; |
| 1430 | |
| 1431 | for (i = 0; i < CODA_MAX_FRAMEBUFFERS; i++) |
| 1432 | coda_free_aux_buf(ctx->dev, &ctx->internal_frames[i]); |
| 1433 | } |
| 1434 | |
Philipp Zabel | ec25f68 | 2012-07-20 08:54:29 -0300 | [diff] [blame] | 1435 | static int coda_alloc_framebuffers(struct coda_ctx *ctx, struct coda_q_data *q_data, u32 fourcc) |
| 1436 | { |
| 1437 | struct coda_dev *dev = ctx->dev; |
Philipp Zabel | ec25f68 | 2012-07-20 08:54:29 -0300 | [diff] [blame] | 1438 | int height = q_data->height; |
Philipp Zabel | 86eda90 | 2013-05-23 10:42:57 -0300 | [diff] [blame] | 1439 | dma_addr_t paddr; |
| 1440 | int ysize; |
Philipp Zabel | 5677e3b | 2013-06-21 03:55:30 -0300 | [diff] [blame] | 1441 | int ret; |
Philipp Zabel | ec25f68 | 2012-07-20 08:54:29 -0300 | [diff] [blame] | 1442 | int i; |
| 1443 | |
Philipp Zabel | 5677e3b | 2013-06-21 03:55:30 -0300 | [diff] [blame] | 1444 | if (ctx->codec && ctx->codec->src_fourcc == V4L2_PIX_FMT_H264) |
| 1445 | height = round_up(height, 16); |
Philipp Zabel | 86eda90 | 2013-05-23 10:42:57 -0300 | [diff] [blame] | 1446 | ysize = round_up(q_data->width, 8) * height; |
| 1447 | |
Philipp Zabel | ec25f68 | 2012-07-20 08:54:29 -0300 | [diff] [blame] | 1448 | /* Allocate frame buffers */ |
Philipp Zabel | ec25f68 | 2012-07-20 08:54:29 -0300 | [diff] [blame] | 1449 | for (i = 0; i < ctx->num_internal_frames; i++) { |
Philipp Zabel | 5677e3b | 2013-06-21 03:55:30 -0300 | [diff] [blame] | 1450 | size_t size; |
| 1451 | |
| 1452 | size = q_data->sizeimage; |
| 1453 | if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 && |
| 1454 | dev->devtype->product != CODA_DX6) |
Philipp Zabel | 86eda90 | 2013-05-23 10:42:57 -0300 | [diff] [blame] | 1455 | ctx->internal_frames[i].size += ysize/4; |
Philipp Zabel | 5677e3b | 2013-06-21 03:55:30 -0300 | [diff] [blame] | 1456 | ret = coda_alloc_context_buf(ctx, &ctx->internal_frames[i], size); |
| 1457 | if (ret < 0) { |
Philipp Zabel | ec25f68 | 2012-07-20 08:54:29 -0300 | [diff] [blame] | 1458 | coda_free_framebuffers(ctx); |
Philipp Zabel | 5677e3b | 2013-06-21 03:55:30 -0300 | [diff] [blame] | 1459 | return ret; |
Philipp Zabel | ec25f68 | 2012-07-20 08:54:29 -0300 | [diff] [blame] | 1460 | } |
| 1461 | } |
| 1462 | |
| 1463 | /* Register frame buffers in the parameter buffer */ |
Philipp Zabel | 86eda90 | 2013-05-23 10:42:57 -0300 | [diff] [blame] | 1464 | for (i = 0; i < ctx->num_internal_frames; i++) { |
| 1465 | paddr = ctx->internal_frames[i].paddr; |
| 1466 | coda_parabuf_write(ctx, i * 3 + 0, paddr); /* Y */ |
| 1467 | coda_parabuf_write(ctx, i * 3 + 1, paddr + ysize); /* Cb */ |
| 1468 | coda_parabuf_write(ctx, i * 3 + 2, paddr + ysize + ysize/4); /* Cr */ |
Philipp Zabel | ec25f68 | 2012-07-20 08:54:29 -0300 | [diff] [blame] | 1469 | |
Philipp Zabel | 5677e3b | 2013-06-21 03:55:30 -0300 | [diff] [blame] | 1470 | /* mvcol buffer for h.264 */ |
| 1471 | if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 && |
| 1472 | dev->devtype->product != CODA_DX6) |
| 1473 | coda_parabuf_write(ctx, 96 + i, |
| 1474 | ctx->internal_frames[i].paddr + |
| 1475 | ysize + ysize/4 + ysize/4); |
Philipp Zabel | ec25f68 | 2012-07-20 08:54:29 -0300 | [diff] [blame] | 1476 | } |
| 1477 | |
Philipp Zabel | 5677e3b | 2013-06-21 03:55:30 -0300 | [diff] [blame] | 1478 | /* mvcol buffer for mpeg4 */ |
| 1479 | if ((dev->devtype->product != CODA_DX6) && |
| 1480 | (ctx->codec->src_fourcc == V4L2_PIX_FMT_MPEG4)) |
| 1481 | coda_parabuf_write(ctx, 97, ctx->internal_frames[i].paddr + |
| 1482 | ysize + ysize/4 + ysize/4); |
| 1483 | |
Philipp Zabel | ec25f68 | 2012-07-20 08:54:29 -0300 | [diff] [blame] | 1484 | return 0; |
| 1485 | } |
| 1486 | |
Javier Martin | 3f3f5c7 | 2012-10-29 05:20:29 -0300 | [diff] [blame] | 1487 | static int coda_h264_padding(int size, char *p) |
| 1488 | { |
Javier Martin | 3f3f5c7 | 2012-10-29 05:20:29 -0300 | [diff] [blame] | 1489 | int nal_size; |
| 1490 | int diff; |
| 1491 | |
Javier Martin | 832fbb5 | 2012-10-29 08:34:59 -0300 | [diff] [blame] | 1492 | diff = size - (size & ~0x7); |
Javier Martin | 3f3f5c7 | 2012-10-29 05:20:29 -0300 | [diff] [blame] | 1493 | if (diff == 0) |
| 1494 | return 0; |
| 1495 | |
Javier Martin | 832fbb5 | 2012-10-29 08:34:59 -0300 | [diff] [blame] | 1496 | nal_size = coda_filler_size[diff]; |
Javier Martin | 3f3f5c7 | 2012-10-29 05:20:29 -0300 | [diff] [blame] | 1497 | memcpy(p, coda_filler_nal, nal_size); |
| 1498 | |
| 1499 | /* Add rbsp stop bit and trailing at the end */ |
| 1500 | *(p + nal_size - 1) = 0x80; |
| 1501 | |
| 1502 | return nal_size; |
| 1503 | } |
| 1504 | |
Philipp Zabel | c2d2251 | 2013-06-21 03:55:28 -0300 | [diff] [blame] | 1505 | static void coda_setup_iram(struct coda_ctx *ctx) |
| 1506 | { |
| 1507 | struct coda_iram_info *iram_info = &ctx->iram_info; |
| 1508 | struct coda_dev *dev = ctx->dev; |
| 1509 | int ipacdc_size; |
| 1510 | int bitram_size; |
| 1511 | int dbk_size; |
Philipp Zabel | 8358e76 | 2013-06-21 03:55:32 -0300 | [diff] [blame] | 1512 | int ovl_size; |
Philipp Zabel | c2d2251 | 2013-06-21 03:55:28 -0300 | [diff] [blame] | 1513 | int mb_width; |
| 1514 | int me_size; |
| 1515 | int size; |
| 1516 | |
| 1517 | memset(iram_info, 0, sizeof(*iram_info)); |
| 1518 | size = dev->iram_size; |
| 1519 | |
| 1520 | if (dev->devtype->product == CODA_DX6) |
| 1521 | return; |
| 1522 | |
| 1523 | if (ctx->inst_type == CODA_INST_ENCODER) { |
| 1524 | struct coda_q_data *q_data_src; |
| 1525 | |
| 1526 | q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT); |
| 1527 | mb_width = DIV_ROUND_UP(q_data_src->width, 16); |
| 1528 | |
| 1529 | /* Prioritize in case IRAM is too small for everything */ |
| 1530 | me_size = round_up(round_up(q_data_src->width, 16) * 36 + 2048, |
| 1531 | 1024); |
| 1532 | iram_info->search_ram_size = me_size; |
| 1533 | if (size >= iram_info->search_ram_size) { |
| 1534 | if (dev->devtype->product == CODA_7541) |
| 1535 | iram_info->axi_sram_use |= CODA7_USE_HOST_ME_ENABLE; |
| 1536 | iram_info->search_ram_paddr = dev->iram_paddr; |
| 1537 | size -= iram_info->search_ram_size; |
| 1538 | } else { |
| 1539 | pr_err("IRAM is smaller than the search ram size\n"); |
| 1540 | goto out; |
| 1541 | } |
| 1542 | |
| 1543 | /* Only H.264BP and H.263P3 are considered */ |
| 1544 | dbk_size = round_up(128 * mb_width, 1024); |
| 1545 | if (size >= dbk_size) { |
| 1546 | iram_info->axi_sram_use |= CODA7_USE_HOST_DBK_ENABLE; |
| 1547 | iram_info->buf_dbk_y_use = dev->iram_paddr + |
| 1548 | iram_info->search_ram_size; |
| 1549 | iram_info->buf_dbk_c_use = iram_info->buf_dbk_y_use + |
| 1550 | dbk_size / 2; |
| 1551 | size -= dbk_size; |
| 1552 | } else { |
| 1553 | goto out; |
| 1554 | } |
| 1555 | |
| 1556 | bitram_size = round_up(128 * mb_width, 1024); |
| 1557 | if (size >= bitram_size) { |
| 1558 | iram_info->axi_sram_use |= CODA7_USE_HOST_BIT_ENABLE; |
| 1559 | iram_info->buf_bit_use = iram_info->buf_dbk_c_use + |
| 1560 | dbk_size / 2; |
| 1561 | size -= bitram_size; |
| 1562 | } else { |
| 1563 | goto out; |
| 1564 | } |
| 1565 | |
| 1566 | ipacdc_size = round_up(128 * mb_width, 1024); |
| 1567 | if (size >= ipacdc_size) { |
| 1568 | iram_info->axi_sram_use |= CODA7_USE_HOST_IP_ENABLE; |
| 1569 | iram_info->buf_ip_ac_dc_use = iram_info->buf_bit_use + |
| 1570 | bitram_size; |
| 1571 | size -= ipacdc_size; |
| 1572 | } |
| 1573 | |
Philipp Zabel | 8358e76 | 2013-06-21 03:55:32 -0300 | [diff] [blame] | 1574 | /* OVL and BTP disabled for encoder */ |
| 1575 | } else if (ctx->inst_type == CODA_INST_DECODER) { |
| 1576 | struct coda_q_data *q_data_dst; |
| 1577 | int mb_height; |
| 1578 | |
| 1579 | q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE); |
| 1580 | mb_width = DIV_ROUND_UP(q_data_dst->width, 16); |
| 1581 | mb_height = DIV_ROUND_UP(q_data_dst->height, 16); |
| 1582 | |
| 1583 | dbk_size = round_up(256 * mb_width, 1024); |
| 1584 | if (size >= dbk_size) { |
| 1585 | iram_info->axi_sram_use |= CODA7_USE_HOST_DBK_ENABLE; |
| 1586 | iram_info->buf_dbk_y_use = dev->iram_paddr; |
| 1587 | iram_info->buf_dbk_c_use = dev->iram_paddr + |
| 1588 | dbk_size / 2; |
| 1589 | size -= dbk_size; |
| 1590 | } else { |
| 1591 | goto out; |
| 1592 | } |
| 1593 | |
| 1594 | bitram_size = round_up(128 * mb_width, 1024); |
| 1595 | if (size >= bitram_size) { |
| 1596 | iram_info->axi_sram_use |= CODA7_USE_HOST_BIT_ENABLE; |
| 1597 | iram_info->buf_bit_use = iram_info->buf_dbk_c_use + |
| 1598 | dbk_size / 2; |
| 1599 | size -= bitram_size; |
| 1600 | } else { |
| 1601 | goto out; |
| 1602 | } |
| 1603 | |
| 1604 | ipacdc_size = round_up(128 * mb_width, 1024); |
| 1605 | if (size >= ipacdc_size) { |
| 1606 | iram_info->axi_sram_use |= CODA7_USE_HOST_IP_ENABLE; |
| 1607 | iram_info->buf_ip_ac_dc_use = iram_info->buf_bit_use + |
| 1608 | bitram_size; |
| 1609 | size -= ipacdc_size; |
| 1610 | } else { |
| 1611 | goto out; |
| 1612 | } |
| 1613 | |
| 1614 | ovl_size = round_up(80 * mb_width, 1024); |
Philipp Zabel | c2d2251 | 2013-06-21 03:55:28 -0300 | [diff] [blame] | 1615 | } |
| 1616 | |
| 1617 | out: |
| 1618 | switch (dev->devtype->product) { |
| 1619 | case CODA_DX6: |
| 1620 | break; |
| 1621 | case CODA_7541: |
| 1622 | /* i.MX53 uses secondary AXI for IRAM access */ |
| 1623 | if (iram_info->axi_sram_use & CODA7_USE_HOST_BIT_ENABLE) |
| 1624 | iram_info->axi_sram_use |= CODA7_USE_BIT_ENABLE; |
| 1625 | if (iram_info->axi_sram_use & CODA7_USE_HOST_IP_ENABLE) |
| 1626 | iram_info->axi_sram_use |= CODA7_USE_IP_ENABLE; |
| 1627 | if (iram_info->axi_sram_use & CODA7_USE_HOST_DBK_ENABLE) |
| 1628 | iram_info->axi_sram_use |= CODA7_USE_DBK_ENABLE; |
| 1629 | if (iram_info->axi_sram_use & CODA7_USE_HOST_OVL_ENABLE) |
| 1630 | iram_info->axi_sram_use |= CODA7_USE_OVL_ENABLE; |
| 1631 | if (iram_info->axi_sram_use & CODA7_USE_HOST_ME_ENABLE) |
| 1632 | iram_info->axi_sram_use |= CODA7_USE_ME_ENABLE; |
| 1633 | } |
| 1634 | |
| 1635 | if (!(iram_info->axi_sram_use & CODA7_USE_HOST_IP_ENABLE)) |
| 1636 | v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, |
| 1637 | "IRAM smaller than needed\n"); |
| 1638 | |
| 1639 | if (dev->devtype->product == CODA_7541) { |
| 1640 | /* TODO - Enabling these causes picture errors on CODA7541 */ |
Philipp Zabel | 8358e76 | 2013-06-21 03:55:32 -0300 | [diff] [blame] | 1641 | if (ctx->inst_type == CODA_INST_DECODER) { |
| 1642 | /* fw 1.4.50 */ |
| 1643 | iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE | |
| 1644 | CODA7_USE_IP_ENABLE); |
| 1645 | } else { |
| 1646 | /* fw 13.4.29 */ |
Philipp Zabel | c2d2251 | 2013-06-21 03:55:28 -0300 | [diff] [blame] | 1647 | iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE | |
| 1648 | CODA7_USE_HOST_DBK_ENABLE | |
| 1649 | CODA7_USE_IP_ENABLE | |
| 1650 | CODA7_USE_DBK_ENABLE); |
| 1651 | } |
| 1652 | } |
| 1653 | } |
| 1654 | |
Philipp Zabel | 5677e3b | 2013-06-21 03:55:30 -0300 | [diff] [blame] | 1655 | static void coda_free_context_buffers(struct coda_ctx *ctx) |
| 1656 | { |
| 1657 | struct coda_dev *dev = ctx->dev; |
| 1658 | |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 1659 | coda_free_aux_buf(dev, &ctx->slicebuf); |
| 1660 | coda_free_aux_buf(dev, &ctx->psbuf); |
Philipp Zabel | 5677e3b | 2013-06-21 03:55:30 -0300 | [diff] [blame] | 1661 | if (dev->devtype->product != CODA_DX6) |
| 1662 | coda_free_aux_buf(dev, &ctx->workbuf); |
| 1663 | } |
| 1664 | |
| 1665 | static int coda_alloc_context_buffers(struct coda_ctx *ctx, |
| 1666 | struct coda_q_data *q_data) |
| 1667 | { |
| 1668 | struct coda_dev *dev = ctx->dev; |
| 1669 | size_t size; |
| 1670 | int ret; |
| 1671 | |
| 1672 | switch (dev->devtype->product) { |
| 1673 | case CODA_7541: |
| 1674 | size = CODA7_WORK_BUF_SIZE; |
| 1675 | break; |
| 1676 | default: |
| 1677 | return 0; |
| 1678 | } |
| 1679 | |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 1680 | if (ctx->psbuf.vaddr) { |
| 1681 | v4l2_err(&dev->v4l2_dev, "psmembuf still allocated\n"); |
| 1682 | return -EBUSY; |
| 1683 | } |
| 1684 | if (ctx->slicebuf.vaddr) { |
| 1685 | v4l2_err(&dev->v4l2_dev, "slicebuf still allocated\n"); |
| 1686 | return -EBUSY; |
| 1687 | } |
Philipp Zabel | 5677e3b | 2013-06-21 03:55:30 -0300 | [diff] [blame] | 1688 | if (ctx->workbuf.vaddr) { |
| 1689 | v4l2_err(&dev->v4l2_dev, "context buffer still allocated\n"); |
| 1690 | ret = -EBUSY; |
| 1691 | return -ENOMEM; |
| 1692 | } |
| 1693 | |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 1694 | if (q_data->fourcc == V4L2_PIX_FMT_H264) { |
| 1695 | /* worst case slice size */ |
| 1696 | size = (DIV_ROUND_UP(q_data->width, 16) * |
| 1697 | DIV_ROUND_UP(q_data->height, 16)) * 3200 / 8 + 512; |
| 1698 | ret = coda_alloc_context_buf(ctx, &ctx->slicebuf, size); |
| 1699 | if (ret < 0) { |
| 1700 | v4l2_err(&dev->v4l2_dev, "failed to allocate %d byte slice buffer", |
| 1701 | ctx->slicebuf.size); |
| 1702 | return ret; |
| 1703 | } |
| 1704 | } |
| 1705 | |
| 1706 | if (dev->devtype->product == CODA_7541) { |
| 1707 | ret = coda_alloc_context_buf(ctx, &ctx->psbuf, CODA7_PS_BUF_SIZE); |
| 1708 | if (ret < 0) { |
| 1709 | v4l2_err(&dev->v4l2_dev, "failed to allocate psmem buffer"); |
| 1710 | goto err; |
| 1711 | } |
| 1712 | } |
| 1713 | |
Philipp Zabel | 5677e3b | 2013-06-21 03:55:30 -0300 | [diff] [blame] | 1714 | ret = coda_alloc_context_buf(ctx, &ctx->workbuf, size); |
| 1715 | if (ret < 0) { |
| 1716 | v4l2_err(&dev->v4l2_dev, "failed to allocate %d byte context buffer", |
| 1717 | ctx->workbuf.size); |
| 1718 | goto err; |
| 1719 | } |
| 1720 | |
| 1721 | return 0; |
| 1722 | |
| 1723 | err: |
| 1724 | coda_free_context_buffers(ctx); |
| 1725 | return ret; |
| 1726 | } |
| 1727 | |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 1728 | static int coda_start_decoding(struct coda_ctx *ctx) |
| 1729 | { |
| 1730 | struct coda_q_data *q_data_src, *q_data_dst; |
| 1731 | u32 bitstream_buf, bitstream_size; |
| 1732 | struct coda_dev *dev = ctx->dev; |
| 1733 | int width, height; |
| 1734 | u32 src_fourcc; |
| 1735 | u32 val; |
| 1736 | int ret; |
| 1737 | |
| 1738 | /* Start decoding */ |
| 1739 | q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT); |
| 1740 | q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE); |
| 1741 | bitstream_buf = ctx->bitstream.paddr; |
| 1742 | bitstream_size = ctx->bitstream.size; |
| 1743 | src_fourcc = q_data_src->fourcc; |
| 1744 | |
| 1745 | coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR); |
| 1746 | |
| 1747 | /* Update coda bitstream read and write pointers from kfifo */ |
| 1748 | coda_kfifo_sync_to_device_full(ctx); |
| 1749 | |
| 1750 | ctx->display_idx = -1; |
| 1751 | ctx->frm_dis_flg = 0; |
| 1752 | coda_write(dev, 0, CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx)); |
| 1753 | |
| 1754 | coda_write(dev, CODA_BIT_DEC_SEQ_INIT_ESCAPE, |
| 1755 | CODA_REG_BIT_BIT_STREAM_PARAM); |
| 1756 | |
| 1757 | coda_write(dev, bitstream_buf, CODA_CMD_DEC_SEQ_BB_START); |
| 1758 | coda_write(dev, bitstream_size / 1024, CODA_CMD_DEC_SEQ_BB_SIZE); |
| 1759 | val = 0; |
| 1760 | if (dev->devtype->product == CODA_7541) |
| 1761 | val |= CODA_REORDER_ENABLE; |
| 1762 | coda_write(dev, val, CODA_CMD_DEC_SEQ_OPTION); |
| 1763 | |
| 1764 | ctx->params.codec_mode = ctx->codec->mode; |
| 1765 | ctx->params.codec_mode_aux = 0; |
| 1766 | if (src_fourcc == V4L2_PIX_FMT_H264) { |
| 1767 | if (dev->devtype->product == CODA_7541) { |
| 1768 | coda_write(dev, ctx->psbuf.paddr, |
| 1769 | CODA_CMD_DEC_SEQ_PS_BB_START); |
| 1770 | coda_write(dev, (CODA7_PS_BUF_SIZE / 1024), |
| 1771 | CODA_CMD_DEC_SEQ_PS_BB_SIZE); |
| 1772 | } |
| 1773 | } |
| 1774 | |
| 1775 | if (coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT)) { |
| 1776 | v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n"); |
| 1777 | coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM); |
| 1778 | return -ETIMEDOUT; |
| 1779 | } |
| 1780 | |
| 1781 | /* Update kfifo out pointer from coda bitstream read pointer */ |
| 1782 | coda_kfifo_sync_from_device(ctx); |
| 1783 | |
| 1784 | coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM); |
| 1785 | |
| 1786 | if (coda_read(dev, CODA_RET_DEC_SEQ_SUCCESS) == 0) { |
| 1787 | v4l2_err(&dev->v4l2_dev, |
| 1788 | "CODA_COMMAND_SEQ_INIT failed, error code = %d\n", |
| 1789 | coda_read(dev, CODA_RET_DEC_SEQ_ERR_REASON)); |
| 1790 | return -EAGAIN; |
| 1791 | } |
| 1792 | |
| 1793 | val = coda_read(dev, CODA_RET_DEC_SEQ_SRC_SIZE); |
| 1794 | if (dev->devtype->product == CODA_DX6) { |
| 1795 | width = (val >> CODADX6_PICWIDTH_OFFSET) & CODADX6_PICWIDTH_MASK; |
| 1796 | height = val & CODADX6_PICHEIGHT_MASK; |
| 1797 | } else { |
| 1798 | width = (val >> CODA7_PICWIDTH_OFFSET) & CODA7_PICWIDTH_MASK; |
| 1799 | height = val & CODA7_PICHEIGHT_MASK; |
| 1800 | } |
| 1801 | |
| 1802 | if (width > q_data_dst->width || height > q_data_dst->height) { |
| 1803 | v4l2_err(&dev->v4l2_dev, "stream is %dx%d, not %dx%d\n", |
| 1804 | width, height, q_data_dst->width, q_data_dst->height); |
| 1805 | return -EINVAL; |
| 1806 | } |
| 1807 | |
| 1808 | width = round_up(width, 16); |
| 1809 | height = round_up(height, 16); |
| 1810 | |
| 1811 | v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "%s instance %d now: %dx%d\n", |
| 1812 | __func__, ctx->idx, width, height); |
| 1813 | |
| 1814 | ctx->num_internal_frames = coda_read(dev, CODA_RET_DEC_SEQ_FRAME_NEED) + 1; |
| 1815 | if (ctx->num_internal_frames > CODA_MAX_FRAMEBUFFERS) { |
| 1816 | v4l2_err(&dev->v4l2_dev, |
| 1817 | "not enough framebuffers to decode (%d < %d)\n", |
| 1818 | CODA_MAX_FRAMEBUFFERS, ctx->num_internal_frames); |
| 1819 | return -EINVAL; |
| 1820 | } |
| 1821 | |
| 1822 | ret = coda_alloc_framebuffers(ctx, q_data_dst, src_fourcc); |
| 1823 | if (ret < 0) |
| 1824 | return ret; |
| 1825 | |
| 1826 | /* Tell the decoder how many frame buffers we allocated. */ |
| 1827 | coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM); |
| 1828 | coda_write(dev, width, CODA_CMD_SET_FRAME_BUF_STRIDE); |
| 1829 | |
| 1830 | if (dev->devtype->product != CODA_DX6) { |
| 1831 | /* Set secondary AXI IRAM */ |
| 1832 | coda_setup_iram(ctx); |
| 1833 | |
| 1834 | coda_write(dev, ctx->iram_info.buf_bit_use, |
| 1835 | CODA7_CMD_SET_FRAME_AXI_BIT_ADDR); |
| 1836 | coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use, |
| 1837 | CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR); |
| 1838 | coda_write(dev, ctx->iram_info.buf_dbk_y_use, |
| 1839 | CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR); |
| 1840 | coda_write(dev, ctx->iram_info.buf_dbk_c_use, |
| 1841 | CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR); |
| 1842 | coda_write(dev, ctx->iram_info.buf_ovl_use, |
| 1843 | CODA7_CMD_SET_FRAME_AXI_OVL_ADDR); |
| 1844 | } |
| 1845 | |
| 1846 | if (src_fourcc == V4L2_PIX_FMT_H264) { |
| 1847 | coda_write(dev, ctx->slicebuf.paddr, |
| 1848 | CODA_CMD_SET_FRAME_SLICE_BB_START); |
| 1849 | coda_write(dev, ctx->slicebuf.size / 1024, |
| 1850 | CODA_CMD_SET_FRAME_SLICE_BB_SIZE); |
| 1851 | } |
| 1852 | |
| 1853 | if (dev->devtype->product == CODA_7541) { |
| 1854 | int max_mb_x = 1920 / 16; |
| 1855 | int max_mb_y = 1088 / 16; |
| 1856 | int max_mb_num = max_mb_x * max_mb_y; |
| 1857 | coda_write(dev, max_mb_num << 16 | max_mb_x << 8 | max_mb_y, |
| 1858 | CODA7_CMD_SET_FRAME_MAX_DEC_SIZE); |
| 1859 | } |
| 1860 | |
| 1861 | if (coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF)) { |
| 1862 | v4l2_err(&ctx->dev->v4l2_dev, |
| 1863 | "CODA_COMMAND_SET_FRAME_BUF timeout\n"); |
| 1864 | return -ETIMEDOUT; |
| 1865 | } |
| 1866 | |
| 1867 | return 0; |
| 1868 | } |
| 1869 | |
Philipp Zabel | d35167a | 2013-05-23 10:42:59 -0300 | [diff] [blame] | 1870 | static int coda_encode_header(struct coda_ctx *ctx, struct vb2_buffer *buf, |
| 1871 | int header_code, u8 *header, int *size) |
| 1872 | { |
| 1873 | struct coda_dev *dev = ctx->dev; |
| 1874 | int ret; |
| 1875 | |
| 1876 | coda_write(dev, vb2_dma_contig_plane_dma_addr(buf, 0), |
| 1877 | CODA_CMD_ENC_HEADER_BB_START); |
| 1878 | coda_write(dev, vb2_plane_size(buf, 0), CODA_CMD_ENC_HEADER_BB_SIZE); |
| 1879 | coda_write(dev, header_code, CODA_CMD_ENC_HEADER_CODE); |
| 1880 | ret = coda_command_sync(ctx, CODA_COMMAND_ENCODE_HEADER); |
| 1881 | if (ret < 0) { |
| 1882 | v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_ENCODE_HEADER timeout\n"); |
| 1883 | return ret; |
| 1884 | } |
Philipp Zabel | 5677e3b | 2013-06-21 03:55:30 -0300 | [diff] [blame] | 1885 | *size = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx)) - |
Philipp Zabel | d35167a | 2013-05-23 10:42:59 -0300 | [diff] [blame] | 1886 | coda_read(dev, CODA_CMD_ENC_HEADER_BB_START); |
| 1887 | memcpy(header, vb2_plane_vaddr(buf, 0), *size); |
| 1888 | |
| 1889 | return 0; |
| 1890 | } |
| 1891 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1892 | static int coda_start_streaming(struct vb2_queue *q, unsigned int count) |
| 1893 | { |
| 1894 | struct coda_ctx *ctx = vb2_get_drv_priv(q); |
| 1895 | struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev; |
| 1896 | u32 bitstream_buf, bitstream_size; |
| 1897 | struct coda_dev *dev = ctx->dev; |
| 1898 | struct coda_q_data *q_data_src, *q_data_dst; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1899 | struct vb2_buffer *buf; |
Philipp Zabel | ec25f68 | 2012-07-20 08:54:29 -0300 | [diff] [blame] | 1900 | u32 dst_fourcc; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1901 | u32 value; |
Philipp Zabel | d35167a | 2013-05-23 10:42:59 -0300 | [diff] [blame] | 1902 | int ret = 0; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1903 | |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 1904 | q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT); |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 1905 | if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) { |
| 1906 | if (q_data_src->fourcc == V4L2_PIX_FMT_H264) { |
| 1907 | if (coda_get_bitstream_payload(ctx) < 512) |
| 1908 | return -EINVAL; |
| 1909 | } else { |
| 1910 | if (count < 1) |
| 1911 | return -EINVAL; |
| 1912 | } |
| 1913 | |
| 1914 | ctx->streamon_out = 1; |
| 1915 | |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 1916 | if (coda_format_is_yuv(q_data_src->fourcc)) |
| 1917 | ctx->inst_type = CODA_INST_ENCODER; |
| 1918 | else |
| 1919 | ctx->inst_type = CODA_INST_DECODER; |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 1920 | } else { |
| 1921 | if (count < 1) |
| 1922 | return -EINVAL; |
| 1923 | |
| 1924 | ctx->streamon_cap = 1; |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 1925 | } |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1926 | |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 1927 | /* Don't start the coda unless both queues are on */ |
| 1928 | if (!(ctx->streamon_out & ctx->streamon_cap)) |
| 1929 | return 0; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1930 | |
Philipp Zabel | eb10751 | 2013-09-30 10:34:45 -0300 | [diff] [blame^] | 1931 | /* Allow decoder device_run with no new buffers queued */ |
| 1932 | if (ctx->inst_type == CODA_INST_DECODER) |
| 1933 | v4l2_m2m_set_src_buffered(ctx->m2m_ctx, true); |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 1934 | |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 1935 | ctx->gopcounter = ctx->params.gop_size - 1; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1936 | buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx); |
| 1937 | bitstream_buf = vb2_dma_contig_plane_dma_addr(buf, 0); |
| 1938 | q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE); |
| 1939 | bitstream_size = q_data_dst->sizeimage; |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 1940 | dst_fourcc = q_data_dst->fourcc; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1941 | |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 1942 | ctx->codec = coda_find_codec(ctx->dev, q_data_src->fourcc, |
| 1943 | q_data_dst->fourcc); |
| 1944 | if (!ctx->codec) { |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1945 | v4l2_err(v4l2_dev, "couldn't tell instance type.\n"); |
| 1946 | return -EINVAL; |
| 1947 | } |
| 1948 | |
Philipp Zabel | 5677e3b | 2013-06-21 03:55:30 -0300 | [diff] [blame] | 1949 | /* Allocate per-instance buffers */ |
| 1950 | ret = coda_alloc_context_buffers(ctx, q_data_src); |
| 1951 | if (ret < 0) |
| 1952 | return ret; |
| 1953 | |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 1954 | if (ctx->inst_type == CODA_INST_DECODER) { |
| 1955 | mutex_lock(&dev->coda_mutex); |
| 1956 | ret = coda_start_decoding(ctx); |
| 1957 | mutex_unlock(&dev->coda_mutex); |
| 1958 | if (ret == -EAGAIN) { |
| 1959 | return 0; |
| 1960 | } else if (ret < 0) { |
| 1961 | return ret; |
| 1962 | } else { |
| 1963 | ctx->initialized = 1; |
| 1964 | return 0; |
| 1965 | } |
| 1966 | } |
| 1967 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1968 | if (!coda_is_initialized(dev)) { |
| 1969 | v4l2_err(v4l2_dev, "coda is not initialized.\n"); |
| 1970 | return -EFAULT; |
| 1971 | } |
Philipp Zabel | fcb6282 | 2013-05-23 10:43:00 -0300 | [diff] [blame] | 1972 | |
| 1973 | mutex_lock(&dev->coda_mutex); |
| 1974 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1975 | coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR); |
Philipp Zabel | 5677e3b | 2013-06-21 03:55:30 -0300 | [diff] [blame] | 1976 | coda_write(dev, bitstream_buf, CODA_REG_BIT_RD_PTR(ctx->reg_idx)); |
| 1977 | coda_write(dev, bitstream_buf, CODA_REG_BIT_WR_PTR(ctx->reg_idx)); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1978 | switch (dev->devtype->product) { |
| 1979 | case CODA_DX6: |
| 1980 | coda_write(dev, CODADX6_STREAM_BUF_DYNALLOC_EN | |
| 1981 | CODADX6_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL); |
| 1982 | break; |
| 1983 | default: |
| 1984 | coda_write(dev, CODA7_STREAM_BUF_DYNALLOC_EN | |
| 1985 | CODA7_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL); |
| 1986 | } |
| 1987 | |
Philipp Zabel | 1043667 | 2012-07-02 09:03:55 -0300 | [diff] [blame] | 1988 | if (dev->devtype->product == CODA_DX6) { |
| 1989 | /* Configure the coda */ |
| 1990 | coda_write(dev, dev->iram_paddr, CODADX6_REG_BIT_SEARCH_RAM_BASE_ADDR); |
| 1991 | } |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1992 | |
| 1993 | /* Could set rotation here if needed */ |
| 1994 | switch (dev->devtype->product) { |
| 1995 | case CODA_DX6: |
| 1996 | value = (q_data_src->width & CODADX6_PICWIDTH_MASK) << CODADX6_PICWIDTH_OFFSET; |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 1997 | value |= (q_data_src->height & CODADX6_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1998 | break; |
| 1999 | default: |
| 2000 | value = (q_data_src->width & CODA7_PICWIDTH_MASK) << CODA7_PICWIDTH_OFFSET; |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 2001 | value |= (q_data_src->height & CODA7_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2002 | } |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2003 | coda_write(dev, value, CODA_CMD_ENC_SEQ_SRC_SIZE); |
| 2004 | coda_write(dev, ctx->params.framerate, |
| 2005 | CODA_CMD_ENC_SEQ_SRC_F_RATE); |
| 2006 | |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 2007 | ctx->params.codec_mode = ctx->codec->mode; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2008 | switch (dst_fourcc) { |
| 2009 | case V4L2_PIX_FMT_MPEG4: |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2010 | coda_write(dev, CODA_STD_MPEG4, CODA_CMD_ENC_SEQ_COD_STD); |
| 2011 | coda_write(dev, 0, CODA_CMD_ENC_SEQ_MP4_PARA); |
| 2012 | break; |
| 2013 | case V4L2_PIX_FMT_H264: |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2014 | coda_write(dev, CODA_STD_H264, CODA_CMD_ENC_SEQ_COD_STD); |
| 2015 | coda_write(dev, 0, CODA_CMD_ENC_SEQ_264_PARA); |
| 2016 | break; |
| 2017 | default: |
| 2018 | v4l2_err(v4l2_dev, |
| 2019 | "dst format (0x%08x) invalid.\n", dst_fourcc); |
Philipp Zabel | fcb6282 | 2013-05-23 10:43:00 -0300 | [diff] [blame] | 2020 | ret = -EINVAL; |
| 2021 | goto out; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2022 | } |
| 2023 | |
Philipp Zabel | c566c78 | 2012-08-08 11:59:38 -0300 | [diff] [blame] | 2024 | switch (ctx->params.slice_mode) { |
| 2025 | case V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE: |
| 2026 | value = 0; |
| 2027 | break; |
| 2028 | case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_MB: |
| 2029 | value = (ctx->params.slice_max_mb & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET; |
| 2030 | value |= (1 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2031 | value |= 1 & CODA_SLICING_MODE_MASK; |
Philipp Zabel | c566c78 | 2012-08-08 11:59:38 -0300 | [diff] [blame] | 2032 | break; |
| 2033 | case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES: |
| 2034 | value = (ctx->params.slice_max_bits & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET; |
| 2035 | value |= (0 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET; |
| 2036 | value |= 1 & CODA_SLICING_MODE_MASK; |
| 2037 | break; |
| 2038 | } |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2039 | coda_write(dev, value, CODA_CMD_ENC_SEQ_SLICE_MODE); |
Philipp Zabel | c566c78 | 2012-08-08 11:59:38 -0300 | [diff] [blame] | 2040 | value = ctx->params.gop_size & CODA_GOP_SIZE_MASK; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2041 | coda_write(dev, value, CODA_CMD_ENC_SEQ_GOP_SIZE); |
| 2042 | |
| 2043 | if (ctx->params.bitrate) { |
| 2044 | /* Rate control enabled */ |
| 2045 | value = (ctx->params.bitrate & CODA_RATECONTROL_BITRATE_MASK) << CODA_RATECONTROL_BITRATE_OFFSET; |
| 2046 | value |= 1 & CODA_RATECONTROL_ENABLE_MASK; |
| 2047 | } else { |
| 2048 | value = 0; |
| 2049 | } |
| 2050 | coda_write(dev, value, CODA_CMD_ENC_SEQ_RC_PARA); |
| 2051 | |
| 2052 | coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_BUF_SIZE); |
| 2053 | coda_write(dev, 0, CODA_CMD_ENC_SEQ_INTRA_REFRESH); |
| 2054 | |
| 2055 | coda_write(dev, bitstream_buf, CODA_CMD_ENC_SEQ_BB_START); |
| 2056 | coda_write(dev, bitstream_size / 1024, CODA_CMD_ENC_SEQ_BB_SIZE); |
| 2057 | |
| 2058 | /* set default gamma */ |
| 2059 | value = (CODA_DEFAULT_GAMMA & CODA_GAMMA_MASK) << CODA_GAMMA_OFFSET; |
| 2060 | coda_write(dev, value, CODA_CMD_ENC_SEQ_RC_GAMMA); |
| 2061 | |
Philipp Zabel | fb1fcf1 | 2013-05-23 10:42:53 -0300 | [diff] [blame] | 2062 | if (CODA_DEFAULT_GAMMA > 0) { |
| 2063 | if (dev->devtype->product == CODA_DX6) |
| 2064 | value = 1 << CODADX6_OPTION_GAMMA_OFFSET; |
| 2065 | else |
| 2066 | value = 1 << CODA7_OPTION_GAMMA_OFFSET; |
| 2067 | } else { |
| 2068 | value = 0; |
| 2069 | } |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2070 | coda_write(dev, value, CODA_CMD_ENC_SEQ_OPTION); |
| 2071 | |
Philipp Zabel | c2d2251 | 2013-06-21 03:55:28 -0300 | [diff] [blame] | 2072 | coda_setup_iram(ctx); |
| 2073 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2074 | if (dst_fourcc == V4L2_PIX_FMT_H264) { |
| 2075 | value = (FMO_SLICE_SAVE_BUF_SIZE << 7); |
| 2076 | value |= (0 & CODA_FMOPARAM_TYPE_MASK) << CODA_FMOPARAM_TYPE_OFFSET; |
| 2077 | value |= 0 & CODA_FMOPARAM_SLICENUM_MASK; |
Philipp Zabel | 1043667 | 2012-07-02 09:03:55 -0300 | [diff] [blame] | 2078 | if (dev->devtype->product == CODA_DX6) { |
| 2079 | coda_write(dev, value, CODADX6_CMD_ENC_SEQ_FMO); |
| 2080 | } else { |
Philipp Zabel | c2d2251 | 2013-06-21 03:55:28 -0300 | [diff] [blame] | 2081 | coda_write(dev, ctx->iram_info.search_ram_paddr, |
| 2082 | CODA7_CMD_ENC_SEQ_SEARCH_BASE); |
| 2083 | coda_write(dev, ctx->iram_info.search_ram_size, |
| 2084 | CODA7_CMD_ENC_SEQ_SEARCH_SIZE); |
Philipp Zabel | 1043667 | 2012-07-02 09:03:55 -0300 | [diff] [blame] | 2085 | } |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2086 | } |
| 2087 | |
Philipp Zabel | fcb6282 | 2013-05-23 10:43:00 -0300 | [diff] [blame] | 2088 | ret = coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT); |
| 2089 | if (ret < 0) { |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2090 | v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n"); |
Philipp Zabel | fcb6282 | 2013-05-23 10:43:00 -0300 | [diff] [blame] | 2091 | goto out; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2092 | } |
| 2093 | |
Philipp Zabel | fcb6282 | 2013-05-23 10:43:00 -0300 | [diff] [blame] | 2094 | if (coda_read(dev, CODA_RET_ENC_SEQ_SUCCESS) == 0) { |
| 2095 | v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT failed\n"); |
| 2096 | ret = -EFAULT; |
| 2097 | goto out; |
| 2098 | } |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2099 | |
Philipp Zabel | 2039749 | 2013-06-21 03:55:29 -0300 | [diff] [blame] | 2100 | ctx->num_internal_frames = 2; |
Philipp Zabel | ec25f68 | 2012-07-20 08:54:29 -0300 | [diff] [blame] | 2101 | ret = coda_alloc_framebuffers(ctx, q_data_src, dst_fourcc); |
Philipp Zabel | fcb6282 | 2013-05-23 10:43:00 -0300 | [diff] [blame] | 2102 | if (ret < 0) { |
| 2103 | v4l2_err(v4l2_dev, "failed to allocate framebuffers\n"); |
| 2104 | goto out; |
| 2105 | } |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2106 | |
Philipp Zabel | ec25f68 | 2012-07-20 08:54:29 -0300 | [diff] [blame] | 2107 | 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] | 2108 | coda_write(dev, round_up(q_data_src->width, 8), CODA_CMD_SET_FRAME_BUF_STRIDE); |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 2109 | if (dev->devtype->product == CODA_7541) |
| 2110 | coda_write(dev, round_up(q_data_src->width, 8), |
| 2111 | CODA7_CMD_SET_FRAME_SOURCE_BUF_STRIDE); |
Philipp Zabel | 1043667 | 2012-07-02 09:03:55 -0300 | [diff] [blame] | 2112 | if (dev->devtype->product != CODA_DX6) { |
Philipp Zabel | c2d2251 | 2013-06-21 03:55:28 -0300 | [diff] [blame] | 2113 | coda_write(dev, ctx->iram_info.buf_bit_use, |
| 2114 | CODA7_CMD_SET_FRAME_AXI_BIT_ADDR); |
| 2115 | coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use, |
| 2116 | CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR); |
| 2117 | coda_write(dev, ctx->iram_info.buf_dbk_y_use, |
| 2118 | CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR); |
| 2119 | coda_write(dev, ctx->iram_info.buf_dbk_c_use, |
| 2120 | CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR); |
| 2121 | coda_write(dev, ctx->iram_info.buf_ovl_use, |
| 2122 | CODA7_CMD_SET_FRAME_AXI_OVL_ADDR); |
Philipp Zabel | 1043667 | 2012-07-02 09:03:55 -0300 | [diff] [blame] | 2123 | } |
Philipp Zabel | fcb6282 | 2013-05-23 10:43:00 -0300 | [diff] [blame] | 2124 | ret = coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF); |
| 2125 | if (ret < 0) { |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2126 | v4l2_err(v4l2_dev, "CODA_COMMAND_SET_FRAME_BUF timeout\n"); |
Philipp Zabel | fcb6282 | 2013-05-23 10:43:00 -0300 | [diff] [blame] | 2127 | goto out; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2128 | } |
| 2129 | |
| 2130 | /* Save stream headers */ |
| 2131 | buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx); |
| 2132 | switch (dst_fourcc) { |
| 2133 | case V4L2_PIX_FMT_H264: |
| 2134 | /* |
| 2135 | * Get SPS in the first frame and copy it to an |
| 2136 | * intermediate buffer. |
| 2137 | */ |
Philipp Zabel | d35167a | 2013-05-23 10:42:59 -0300 | [diff] [blame] | 2138 | ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_SPS, |
| 2139 | &ctx->vpu_header[0][0], |
| 2140 | &ctx->vpu_header_size[0]); |
| 2141 | if (ret < 0) |
| 2142 | goto out; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2143 | |
| 2144 | /* |
| 2145 | * Get PPS in the first frame and copy it to an |
| 2146 | * intermediate buffer. |
| 2147 | */ |
Philipp Zabel | d35167a | 2013-05-23 10:42:59 -0300 | [diff] [blame] | 2148 | ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_PPS, |
| 2149 | &ctx->vpu_header[1][0], |
| 2150 | &ctx->vpu_header_size[1]); |
| 2151 | if (ret < 0) |
| 2152 | goto out; |
| 2153 | |
Javier Martin | 3f3f5c7 | 2012-10-29 05:20:29 -0300 | [diff] [blame] | 2154 | /* |
| 2155 | * Length of H.264 headers is variable and thus it might not be |
| 2156 | * aligned for the coda to append the encoded frame. In that is |
| 2157 | * the case a filler NAL must be added to header 2. |
| 2158 | */ |
| 2159 | ctx->vpu_header_size[2] = coda_h264_padding( |
| 2160 | (ctx->vpu_header_size[0] + |
| 2161 | ctx->vpu_header_size[1]), |
| 2162 | ctx->vpu_header[2]); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2163 | break; |
| 2164 | case V4L2_PIX_FMT_MPEG4: |
| 2165 | /* |
| 2166 | * Get VOS in the first frame and copy it to an |
| 2167 | * intermediate buffer |
| 2168 | */ |
Philipp Zabel | d35167a | 2013-05-23 10:42:59 -0300 | [diff] [blame] | 2169 | ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOS, |
| 2170 | &ctx->vpu_header[0][0], |
| 2171 | &ctx->vpu_header_size[0]); |
| 2172 | if (ret < 0) |
| 2173 | goto out; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2174 | |
Philipp Zabel | d35167a | 2013-05-23 10:42:59 -0300 | [diff] [blame] | 2175 | ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VIS, |
| 2176 | &ctx->vpu_header[1][0], |
| 2177 | &ctx->vpu_header_size[1]); |
| 2178 | if (ret < 0) |
| 2179 | goto out; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2180 | |
Philipp Zabel | d35167a | 2013-05-23 10:42:59 -0300 | [diff] [blame] | 2181 | ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOL, |
| 2182 | &ctx->vpu_header[2][0], |
| 2183 | &ctx->vpu_header_size[2]); |
| 2184 | if (ret < 0) |
| 2185 | goto out; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2186 | break; |
| 2187 | default: |
| 2188 | /* No more formats need to save headers at the moment */ |
| 2189 | break; |
| 2190 | } |
| 2191 | |
Philipp Zabel | d35167a | 2013-05-23 10:42:59 -0300 | [diff] [blame] | 2192 | out: |
Philipp Zabel | fcb6282 | 2013-05-23 10:43:00 -0300 | [diff] [blame] | 2193 | mutex_unlock(&dev->coda_mutex); |
Philipp Zabel | d35167a | 2013-05-23 10:42:59 -0300 | [diff] [blame] | 2194 | return ret; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2195 | } |
| 2196 | |
| 2197 | static int coda_stop_streaming(struct vb2_queue *q) |
| 2198 | { |
| 2199 | struct coda_ctx *ctx = vb2_get_drv_priv(q); |
Philipp Zabel | 2fb57f0 | 2012-07-25 09:22:07 -0300 | [diff] [blame] | 2200 | struct coda_dev *dev = ctx->dev; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2201 | |
| 2202 | if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) { |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 2203 | v4l2_dbg(1, coda_debug, &dev->v4l2_dev, |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2204 | "%s: output\n", __func__); |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 2205 | ctx->streamon_out = 0; |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 2206 | |
| 2207 | ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG; |
| 2208 | |
| 2209 | ctx->isequence = 0; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2210 | } else { |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 2211 | v4l2_dbg(1, coda_debug, &dev->v4l2_dev, |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2212 | "%s: capture\n", __func__); |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 2213 | ctx->streamon_cap = 0; |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 2214 | |
| 2215 | ctx->osequence = 0; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2216 | } |
| 2217 | |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 2218 | if (!ctx->streamon_out && !ctx->streamon_cap) { |
| 2219 | kfifo_init(&ctx->bitstream_fifo, |
| 2220 | ctx->bitstream.vaddr, ctx->bitstream.size); |
| 2221 | ctx->runcounter = 0; |
Philipp Zabel | 62bed14 | 2012-07-25 10:40:39 -0300 | [diff] [blame] | 2222 | } |
Philipp Zabel | 62bed14 | 2012-07-25 10:40:39 -0300 | [diff] [blame] | 2223 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2224 | return 0; |
| 2225 | } |
| 2226 | |
| 2227 | static struct vb2_ops coda_qops = { |
| 2228 | .queue_setup = coda_queue_setup, |
| 2229 | .buf_prepare = coda_buf_prepare, |
| 2230 | .buf_queue = coda_buf_queue, |
| 2231 | .wait_prepare = coda_wait_prepare, |
| 2232 | .wait_finish = coda_wait_finish, |
| 2233 | .start_streaming = coda_start_streaming, |
| 2234 | .stop_streaming = coda_stop_streaming, |
| 2235 | }; |
| 2236 | |
| 2237 | static int coda_s_ctrl(struct v4l2_ctrl *ctrl) |
| 2238 | { |
| 2239 | struct coda_ctx *ctx = |
| 2240 | container_of(ctrl->handler, struct coda_ctx, ctrls); |
| 2241 | |
| 2242 | v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, |
| 2243 | "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val); |
| 2244 | |
| 2245 | switch (ctrl->id) { |
Philipp Zabel | 8f35c7b | 2012-07-09 04:25:52 -0300 | [diff] [blame] | 2246 | case V4L2_CID_HFLIP: |
| 2247 | if (ctrl->val) |
| 2248 | ctx->params.rot_mode |= CODA_MIR_HOR; |
| 2249 | else |
| 2250 | ctx->params.rot_mode &= ~CODA_MIR_HOR; |
| 2251 | break; |
| 2252 | case V4L2_CID_VFLIP: |
| 2253 | if (ctrl->val) |
| 2254 | ctx->params.rot_mode |= CODA_MIR_VER; |
| 2255 | else |
| 2256 | ctx->params.rot_mode &= ~CODA_MIR_VER; |
| 2257 | break; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2258 | case V4L2_CID_MPEG_VIDEO_BITRATE: |
| 2259 | ctx->params.bitrate = ctrl->val / 1000; |
| 2260 | break; |
| 2261 | case V4L2_CID_MPEG_VIDEO_GOP_SIZE: |
| 2262 | ctx->params.gop_size = ctrl->val; |
| 2263 | break; |
| 2264 | case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP: |
| 2265 | ctx->params.h264_intra_qp = ctrl->val; |
| 2266 | break; |
| 2267 | case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP: |
| 2268 | ctx->params.h264_inter_qp = ctrl->val; |
| 2269 | break; |
| 2270 | case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP: |
| 2271 | ctx->params.mpeg4_intra_qp = ctrl->val; |
| 2272 | break; |
| 2273 | case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP: |
| 2274 | ctx->params.mpeg4_inter_qp = ctrl->val; |
| 2275 | break; |
| 2276 | case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE: |
| 2277 | ctx->params.slice_mode = ctrl->val; |
| 2278 | break; |
| 2279 | case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB: |
| 2280 | ctx->params.slice_max_mb = ctrl->val; |
| 2281 | break; |
Philipp Zabel | c566c78 | 2012-08-08 11:59:38 -0300 | [diff] [blame] | 2282 | case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES: |
| 2283 | ctx->params.slice_max_bits = ctrl->val * 8; |
| 2284 | break; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2285 | case V4L2_CID_MPEG_VIDEO_HEADER_MODE: |
| 2286 | break; |
| 2287 | default: |
| 2288 | v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, |
| 2289 | "Invalid control, id=%d, val=%d\n", |
| 2290 | ctrl->id, ctrl->val); |
| 2291 | return -EINVAL; |
| 2292 | } |
| 2293 | |
| 2294 | return 0; |
| 2295 | } |
| 2296 | |
| 2297 | static struct v4l2_ctrl_ops coda_ctrl_ops = { |
| 2298 | .s_ctrl = coda_s_ctrl, |
| 2299 | }; |
| 2300 | |
| 2301 | static int coda_ctrls_setup(struct coda_ctx *ctx) |
| 2302 | { |
| 2303 | v4l2_ctrl_handler_init(&ctx->ctrls, 9); |
| 2304 | |
| 2305 | v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops, |
Philipp Zabel | 8f35c7b | 2012-07-09 04:25:52 -0300 | [diff] [blame] | 2306 | V4L2_CID_HFLIP, 0, 1, 1, 0); |
| 2307 | v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops, |
| 2308 | V4L2_CID_VFLIP, 0, 1, 1, 0); |
| 2309 | v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops, |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2310 | V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1, 0); |
| 2311 | v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops, |
| 2312 | V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 60, 1, 16); |
| 2313 | v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops, |
| 2314 | V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 1, 51, 1, 25); |
| 2315 | v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops, |
| 2316 | V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 1, 51, 1, 25); |
| 2317 | v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops, |
| 2318 | V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2); |
| 2319 | v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops, |
| 2320 | V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2); |
| 2321 | v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops, |
| 2322 | V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE, |
Philipp Zabel | c566c78 | 2012-08-08 11:59:38 -0300 | [diff] [blame] | 2323 | V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0, |
| 2324 | V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2325 | v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops, |
| 2326 | V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1); |
Philipp Zabel | c566c78 | 2012-08-08 11:59:38 -0300 | [diff] [blame] | 2327 | v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops, |
| 2328 | V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1, 500); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2329 | v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops, |
| 2330 | V4L2_CID_MPEG_VIDEO_HEADER_MODE, |
| 2331 | V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME, |
| 2332 | (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE), |
| 2333 | V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME); |
| 2334 | |
| 2335 | if (ctx->ctrls.error) { |
| 2336 | v4l2_err(&ctx->dev->v4l2_dev, "control initialization error (%d)", |
| 2337 | ctx->ctrls.error); |
| 2338 | return -EINVAL; |
| 2339 | } |
| 2340 | |
| 2341 | return v4l2_ctrl_handler_setup(&ctx->ctrls); |
| 2342 | } |
| 2343 | |
| 2344 | static int coda_queue_init(void *priv, struct vb2_queue *src_vq, |
| 2345 | struct vb2_queue *dst_vq) |
| 2346 | { |
| 2347 | struct coda_ctx *ctx = priv; |
| 2348 | int ret; |
| 2349 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2350 | src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT; |
Philipp Zabel | 419869c | 2013-05-23 07:06:30 -0300 | [diff] [blame] | 2351 | src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2352 | src_vq->drv_priv = ctx; |
| 2353 | src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer); |
| 2354 | src_vq->ops = &coda_qops; |
| 2355 | src_vq->mem_ops = &vb2_dma_contig_memops; |
Kamil Debski | ccd571c | 2013-04-24 10:38:24 -0300 | [diff] [blame] | 2356 | src_vq->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_COPY; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2357 | |
| 2358 | ret = vb2_queue_init(src_vq); |
| 2359 | if (ret) |
| 2360 | return ret; |
| 2361 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2362 | dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
Philipp Zabel | 419869c | 2013-05-23 07:06:30 -0300 | [diff] [blame] | 2363 | dst_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2364 | dst_vq->drv_priv = ctx; |
| 2365 | dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer); |
| 2366 | dst_vq->ops = &coda_qops; |
| 2367 | dst_vq->mem_ops = &vb2_dma_contig_memops; |
Kamil Debski | ccd571c | 2013-04-24 10:38:24 -0300 | [diff] [blame] | 2368 | dst_vq->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_COPY; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2369 | |
| 2370 | return vb2_queue_init(dst_vq); |
| 2371 | } |
| 2372 | |
Philipp Zabel | e11f3e6 | 2012-07-25 09:16:58 -0300 | [diff] [blame] | 2373 | static int coda_next_free_instance(struct coda_dev *dev) |
| 2374 | { |
Philipp Zabel | 0cddc7e | 2013-09-30 10:34:44 -0300 | [diff] [blame] | 2375 | int idx = ffz(dev->instance_mask); |
| 2376 | |
| 2377 | if ((idx < 0) || |
| 2378 | (dev->devtype->product == CODA_DX6 && idx > CODADX6_MAX_INSTANCES)) |
| 2379 | return -EBUSY; |
| 2380 | |
| 2381 | return idx; |
Philipp Zabel | e11f3e6 | 2012-07-25 09:16:58 -0300 | [diff] [blame] | 2382 | } |
| 2383 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2384 | static int coda_open(struct file *file) |
| 2385 | { |
| 2386 | struct coda_dev *dev = video_drvdata(file); |
| 2387 | struct coda_ctx *ctx = NULL; |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 2388 | int ret; |
Philipp Zabel | e11f3e6 | 2012-07-25 09:16:58 -0300 | [diff] [blame] | 2389 | int idx; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2390 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2391 | ctx = kzalloc(sizeof *ctx, GFP_KERNEL); |
| 2392 | if (!ctx) |
| 2393 | return -ENOMEM; |
| 2394 | |
Fabio Estevam | f82bc20 | 2013-08-21 11:14:16 -0300 | [diff] [blame] | 2395 | idx = coda_next_free_instance(dev); |
Philipp Zabel | 0cddc7e | 2013-09-30 10:34:44 -0300 | [diff] [blame] | 2396 | if (idx < 0) { |
| 2397 | ret = idx; |
Fabio Estevam | f82bc20 | 2013-08-21 11:14:16 -0300 | [diff] [blame] | 2398 | goto err_coda_max; |
| 2399 | } |
| 2400 | set_bit(idx, &dev->instance_mask); |
| 2401 | |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 2402 | INIT_WORK(&ctx->skip_run, coda_skip_run); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2403 | v4l2_fh_init(&ctx->fh, video_devdata(file)); |
| 2404 | file->private_data = &ctx->fh; |
| 2405 | v4l2_fh_add(&ctx->fh); |
| 2406 | ctx->dev = dev; |
Philipp Zabel | e11f3e6 | 2012-07-25 09:16:58 -0300 | [diff] [blame] | 2407 | ctx->idx = idx; |
Philipp Zabel | 5677e3b | 2013-06-21 03:55:30 -0300 | [diff] [blame] | 2408 | switch (dev->devtype->product) { |
| 2409 | case CODA_7541: |
| 2410 | ctx->reg_idx = 0; |
| 2411 | break; |
| 2412 | default: |
| 2413 | ctx->reg_idx = idx; |
| 2414 | } |
Fabio Estevam | f82bc20 | 2013-08-21 11:14:16 -0300 | [diff] [blame] | 2415 | |
Fabio Estevam | 79830db | 2013-08-21 11:14:17 -0300 | [diff] [blame] | 2416 | ret = clk_prepare_enable(dev->clk_per); |
| 2417 | if (ret) |
| 2418 | goto err_clk_per; |
| 2419 | |
| 2420 | ret = clk_prepare_enable(dev->clk_ahb); |
| 2421 | if (ret) |
| 2422 | goto err_clk_ahb; |
| 2423 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2424 | set_default_params(ctx); |
| 2425 | ctx->m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx, |
| 2426 | &coda_queue_init); |
| 2427 | if (IS_ERR(ctx->m2m_ctx)) { |
Philipp Zabel | 72720ff | 2013-05-21 10:31:25 -0300 | [diff] [blame] | 2428 | ret = PTR_ERR(ctx->m2m_ctx); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2429 | |
| 2430 | v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n", |
| 2431 | __func__, ret); |
Fabio Estevam | f82bc20 | 2013-08-21 11:14:16 -0300 | [diff] [blame] | 2432 | goto err_ctx_init; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2433 | } |
| 2434 | ret = coda_ctrls_setup(ctx); |
| 2435 | if (ret) { |
| 2436 | v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n"); |
Fabio Estevam | f82bc20 | 2013-08-21 11:14:16 -0300 | [diff] [blame] | 2437 | goto err_ctrls_setup; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2438 | } |
| 2439 | |
| 2440 | ctx->fh.ctrl_handler = &ctx->ctrls; |
| 2441 | |
Philipp Zabel | 5677e3b | 2013-06-21 03:55:30 -0300 | [diff] [blame] | 2442 | ret = coda_alloc_context_buf(ctx, &ctx->parabuf, CODA_PARA_BUF_SIZE); |
| 2443 | if (ret < 0) { |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2444 | v4l2_err(&dev->v4l2_dev, "failed to allocate parabuf"); |
Fabio Estevam | f82bc20 | 2013-08-21 11:14:16 -0300 | [diff] [blame] | 2445 | goto err_dma_alloc; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2446 | } |
| 2447 | |
Philipp Zabel | 9082a7c | 2013-06-21 03:55:31 -0300 | [diff] [blame] | 2448 | ctx->bitstream.size = CODA_MAX_FRAME_SIZE; |
| 2449 | ctx->bitstream.vaddr = dma_alloc_writecombine(&dev->plat_dev->dev, |
| 2450 | ctx->bitstream.size, &ctx->bitstream.paddr, GFP_KERNEL); |
| 2451 | if (!ctx->bitstream.vaddr) { |
| 2452 | v4l2_err(&dev->v4l2_dev, "failed to allocate bitstream ringbuffer"); |
| 2453 | ret = -ENOMEM; |
Fabio Estevam | f82bc20 | 2013-08-21 11:14:16 -0300 | [diff] [blame] | 2454 | goto err_dma_writecombine; |
Philipp Zabel | 9082a7c | 2013-06-21 03:55:31 -0300 | [diff] [blame] | 2455 | } |
| 2456 | kfifo_init(&ctx->bitstream_fifo, |
| 2457 | ctx->bitstream.vaddr, ctx->bitstream.size); |
| 2458 | mutex_init(&ctx->bitstream_mutex); |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 2459 | mutex_init(&ctx->buffer_mutex); |
Philipp Zabel | 9082a7c | 2013-06-21 03:55:31 -0300 | [diff] [blame] | 2460 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2461 | coda_lock(ctx); |
Philipp Zabel | e11f3e6 | 2012-07-25 09:16:58 -0300 | [diff] [blame] | 2462 | list_add(&ctx->list, &dev->instances); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2463 | coda_unlock(ctx); |
| 2464 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2465 | v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Created instance %d (%p)\n", |
| 2466 | ctx->idx, ctx); |
| 2467 | |
| 2468 | return 0; |
| 2469 | |
Fabio Estevam | f82bc20 | 2013-08-21 11:14:16 -0300 | [diff] [blame] | 2470 | err_dma_writecombine: |
| 2471 | coda_free_context_buffers(ctx); |
| 2472 | if (ctx->dev->devtype->product == CODA_DX6) |
| 2473 | coda_free_aux_buf(dev, &ctx->workbuf); |
| 2474 | coda_free_aux_buf(dev, &ctx->parabuf); |
| 2475 | err_dma_alloc: |
| 2476 | v4l2_ctrl_handler_free(&ctx->ctrls); |
| 2477 | err_ctrls_setup: |
| 2478 | v4l2_m2m_ctx_release(ctx->m2m_ctx); |
| 2479 | err_ctx_init: |
| 2480 | clk_disable_unprepare(dev->clk_ahb); |
Fabio Estevam | 79830db | 2013-08-21 11:14:17 -0300 | [diff] [blame] | 2481 | err_clk_ahb: |
Fabio Estevam | f82bc20 | 2013-08-21 11:14:16 -0300 | [diff] [blame] | 2482 | clk_disable_unprepare(dev->clk_per); |
Fabio Estevam | 79830db | 2013-08-21 11:14:17 -0300 | [diff] [blame] | 2483 | err_clk_per: |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2484 | v4l2_fh_del(&ctx->fh); |
| 2485 | v4l2_fh_exit(&ctx->fh); |
Fabio Estevam | f82bc20 | 2013-08-21 11:14:16 -0300 | [diff] [blame] | 2486 | clear_bit(ctx->idx, &dev->instance_mask); |
| 2487 | err_coda_max: |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2488 | kfree(ctx); |
| 2489 | return ret; |
| 2490 | } |
| 2491 | |
| 2492 | static int coda_release(struct file *file) |
| 2493 | { |
| 2494 | struct coda_dev *dev = video_drvdata(file); |
| 2495 | struct coda_ctx *ctx = fh_to_ctx(file->private_data); |
| 2496 | |
| 2497 | v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Releasing instance %p\n", |
| 2498 | ctx); |
| 2499 | |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 2500 | /* If this instance is running, call .job_abort and wait for it to end */ |
| 2501 | v4l2_m2m_ctx_release(ctx->m2m_ctx); |
| 2502 | |
| 2503 | /* In case the instance was not running, we still need to call SEQ_END */ |
| 2504 | mutex_lock(&dev->coda_mutex); |
| 2505 | v4l2_dbg(1, coda_debug, &dev->v4l2_dev, |
| 2506 | "%s: sent command 'SEQ_END' to coda\n", __func__); |
| 2507 | if (coda_command_sync(ctx, CODA_COMMAND_SEQ_END)) { |
| 2508 | v4l2_err(&dev->v4l2_dev, |
| 2509 | "CODA_COMMAND_SEQ_END failed\n"); |
| 2510 | mutex_unlock(&dev->coda_mutex); |
| 2511 | return -ETIMEDOUT; |
| 2512 | } |
| 2513 | mutex_unlock(&dev->coda_mutex); |
| 2514 | |
| 2515 | coda_free_framebuffers(ctx); |
| 2516 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2517 | coda_lock(ctx); |
Philipp Zabel | e11f3e6 | 2012-07-25 09:16:58 -0300 | [diff] [blame] | 2518 | list_del(&ctx->list); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2519 | coda_unlock(ctx); |
| 2520 | |
Philipp Zabel | 9082a7c | 2013-06-21 03:55:31 -0300 | [diff] [blame] | 2521 | dma_free_writecombine(&dev->plat_dev->dev, ctx->bitstream.size, |
| 2522 | ctx->bitstream.vaddr, ctx->bitstream.paddr); |
Philipp Zabel | 5677e3b | 2013-06-21 03:55:30 -0300 | [diff] [blame] | 2523 | coda_free_context_buffers(ctx); |
| 2524 | if (ctx->dev->devtype->product == CODA_DX6) |
| 2525 | coda_free_aux_buf(dev, &ctx->workbuf); |
| 2526 | |
| 2527 | coda_free_aux_buf(dev, &ctx->parabuf); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2528 | v4l2_ctrl_handler_free(&ctx->ctrls); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2529 | clk_disable_unprepare(dev->clk_ahb); |
Fabio Estevam | f82bc20 | 2013-08-21 11:14:16 -0300 | [diff] [blame] | 2530 | clk_disable_unprepare(dev->clk_per); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2531 | v4l2_fh_del(&ctx->fh); |
| 2532 | v4l2_fh_exit(&ctx->fh); |
Philipp Zabel | e11f3e6 | 2012-07-25 09:16:58 -0300 | [diff] [blame] | 2533 | clear_bit(ctx->idx, &dev->instance_mask); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2534 | kfree(ctx); |
| 2535 | |
| 2536 | return 0; |
| 2537 | } |
| 2538 | |
| 2539 | static unsigned int coda_poll(struct file *file, |
| 2540 | struct poll_table_struct *wait) |
| 2541 | { |
| 2542 | struct coda_ctx *ctx = fh_to_ctx(file->private_data); |
| 2543 | int ret; |
| 2544 | |
| 2545 | coda_lock(ctx); |
| 2546 | ret = v4l2_m2m_poll(file, ctx->m2m_ctx, wait); |
| 2547 | coda_unlock(ctx); |
| 2548 | return ret; |
| 2549 | } |
| 2550 | |
| 2551 | static int coda_mmap(struct file *file, struct vm_area_struct *vma) |
| 2552 | { |
| 2553 | struct coda_ctx *ctx = fh_to_ctx(file->private_data); |
| 2554 | |
| 2555 | return v4l2_m2m_mmap(file, ctx->m2m_ctx, vma); |
| 2556 | } |
| 2557 | |
| 2558 | static const struct v4l2_file_operations coda_fops = { |
| 2559 | .owner = THIS_MODULE, |
| 2560 | .open = coda_open, |
| 2561 | .release = coda_release, |
| 2562 | .poll = coda_poll, |
| 2563 | .unlocked_ioctl = video_ioctl2, |
| 2564 | .mmap = coda_mmap, |
| 2565 | }; |
| 2566 | |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 2567 | static void coda_finish_decode(struct coda_ctx *ctx) |
| 2568 | { |
| 2569 | struct coda_dev *dev = ctx->dev; |
| 2570 | struct coda_q_data *q_data_src; |
| 2571 | struct coda_q_data *q_data_dst; |
| 2572 | struct vb2_buffer *dst_buf; |
| 2573 | int width, height; |
| 2574 | int decoded_idx; |
| 2575 | int display_idx; |
| 2576 | u32 src_fourcc; |
| 2577 | int success; |
| 2578 | u32 val; |
| 2579 | |
| 2580 | dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx); |
| 2581 | |
| 2582 | /* Update kfifo out pointer from coda bitstream read pointer */ |
| 2583 | coda_kfifo_sync_from_device(ctx); |
| 2584 | |
| 2585 | /* |
| 2586 | * in stream-end mode, the read pointer can overshoot the write pointer |
| 2587 | * by up to 512 bytes |
| 2588 | */ |
| 2589 | if (ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) { |
| 2590 | if (coda_get_bitstream_payload(ctx) >= 0x100000 - 512) |
| 2591 | kfifo_init(&ctx->bitstream_fifo, |
| 2592 | ctx->bitstream.vaddr, ctx->bitstream.size); |
| 2593 | } |
| 2594 | |
| 2595 | q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT); |
| 2596 | src_fourcc = q_data_src->fourcc; |
| 2597 | |
| 2598 | val = coda_read(dev, CODA_RET_DEC_PIC_SUCCESS); |
| 2599 | if (val != 1) |
| 2600 | pr_err("DEC_PIC_SUCCESS = %d\n", val); |
| 2601 | |
| 2602 | success = val & 0x1; |
| 2603 | if (!success) |
| 2604 | v4l2_err(&dev->v4l2_dev, "decode failed\n"); |
| 2605 | |
| 2606 | if (src_fourcc == V4L2_PIX_FMT_H264) { |
| 2607 | if (val & (1 << 3)) |
| 2608 | v4l2_err(&dev->v4l2_dev, |
| 2609 | "insufficient PS buffer space (%d bytes)\n", |
| 2610 | ctx->psbuf.size); |
| 2611 | if (val & (1 << 2)) |
| 2612 | v4l2_err(&dev->v4l2_dev, |
| 2613 | "insufficient slice buffer space (%d bytes)\n", |
| 2614 | ctx->slicebuf.size); |
| 2615 | } |
| 2616 | |
| 2617 | val = coda_read(dev, CODA_RET_DEC_PIC_SIZE); |
| 2618 | width = (val >> 16) & 0xffff; |
| 2619 | height = val & 0xffff; |
| 2620 | |
| 2621 | q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE); |
| 2622 | |
| 2623 | val = coda_read(dev, CODA_RET_DEC_PIC_TYPE); |
| 2624 | if ((val & 0x7) == 0) { |
| 2625 | dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME; |
| 2626 | dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME; |
| 2627 | } else { |
| 2628 | dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME; |
| 2629 | dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME; |
| 2630 | } |
| 2631 | |
| 2632 | val = coda_read(dev, CODA_RET_DEC_PIC_ERR_MB); |
| 2633 | if (val > 0) |
| 2634 | v4l2_err(&dev->v4l2_dev, |
| 2635 | "errors in %d macroblocks\n", val); |
| 2636 | |
| 2637 | if (dev->devtype->product == CODA_7541) { |
| 2638 | val = coda_read(dev, CODA_RET_DEC_PIC_OPTION); |
| 2639 | if (val == 0) { |
| 2640 | /* not enough bitstream data */ |
| 2641 | v4l2_dbg(1, coda_debug, &dev->v4l2_dev, |
| 2642 | "prescan failed: %d\n", val); |
| 2643 | ctx->prescan_failed = true; |
| 2644 | return; |
| 2645 | } |
| 2646 | } |
| 2647 | |
| 2648 | ctx->frm_dis_flg = coda_read(dev, CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx)); |
| 2649 | |
| 2650 | /* |
| 2651 | * The previous display frame was copied out by the rotator, |
| 2652 | * now it can be overwritten again |
| 2653 | */ |
| 2654 | if (ctx->display_idx >= 0 && |
| 2655 | ctx->display_idx < ctx->num_internal_frames) { |
| 2656 | ctx->frm_dis_flg &= ~(1 << ctx->display_idx); |
| 2657 | coda_write(dev, ctx->frm_dis_flg, |
| 2658 | CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx)); |
| 2659 | } |
| 2660 | |
| 2661 | /* |
| 2662 | * The index of the last decoded frame, not necessarily in |
| 2663 | * display order, and the index of the next display frame. |
| 2664 | * The latter could have been decoded in a previous run. |
| 2665 | */ |
| 2666 | decoded_idx = coda_read(dev, CODA_RET_DEC_PIC_CUR_IDX); |
| 2667 | display_idx = coda_read(dev, CODA_RET_DEC_PIC_FRAME_IDX); |
| 2668 | |
| 2669 | if (decoded_idx == -1) { |
| 2670 | /* no frame was decoded, but we might have a display frame */ |
| 2671 | if (display_idx < 0 && ctx->display_idx < 0) |
| 2672 | ctx->prescan_failed = true; |
| 2673 | } else if (decoded_idx == -2) { |
| 2674 | /* no frame was decoded, we still return the remaining buffers */ |
| 2675 | } else if (decoded_idx < 0 || decoded_idx >= ctx->num_internal_frames) { |
| 2676 | v4l2_err(&dev->v4l2_dev, |
| 2677 | "decoded frame index out of range: %d\n", decoded_idx); |
| 2678 | } |
| 2679 | |
| 2680 | if (display_idx == -1) { |
| 2681 | /* |
| 2682 | * no more frames to be decoded, but there could still |
| 2683 | * be rotator output to dequeue |
| 2684 | */ |
| 2685 | ctx->prescan_failed = true; |
| 2686 | } else if (display_idx == -3) { |
| 2687 | /* possibly prescan failure */ |
| 2688 | } else if (display_idx < 0 || display_idx >= ctx->num_internal_frames) { |
| 2689 | v4l2_err(&dev->v4l2_dev, |
| 2690 | "presentation frame index out of range: %d\n", |
| 2691 | display_idx); |
| 2692 | } |
| 2693 | |
| 2694 | /* If a frame was copied out, return it */ |
| 2695 | if (ctx->display_idx >= 0 && |
| 2696 | ctx->display_idx < ctx->num_internal_frames) { |
| 2697 | dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx); |
| 2698 | dst_buf->v4l2_buf.sequence = ctx->osequence++; |
| 2699 | |
| 2700 | vb2_set_plane_payload(dst_buf, 0, width * height * 3 / 2); |
| 2701 | |
| 2702 | v4l2_m2m_buf_done(dst_buf, success ? VB2_BUF_STATE_DONE : |
| 2703 | VB2_BUF_STATE_ERROR); |
| 2704 | |
| 2705 | v4l2_dbg(1, coda_debug, &dev->v4l2_dev, |
| 2706 | "job finished: decoding frame (%d) (%s)\n", |
| 2707 | dst_buf->v4l2_buf.sequence, |
| 2708 | (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ? |
| 2709 | "KEYFRAME" : "PFRAME"); |
| 2710 | } else { |
| 2711 | v4l2_dbg(1, coda_debug, &dev->v4l2_dev, |
| 2712 | "job finished: no frame decoded\n"); |
| 2713 | } |
| 2714 | |
| 2715 | /* The rotator will copy the current display frame next time */ |
| 2716 | ctx->display_idx = display_idx; |
| 2717 | } |
| 2718 | |
| 2719 | static void coda_finish_encode(struct coda_ctx *ctx) |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2720 | { |
Philipp Zabel | ec25f68 | 2012-07-20 08:54:29 -0300 | [diff] [blame] | 2721 | struct vb2_buffer *src_buf, *dst_buf; |
Philipp Zabel | 477c1cf | 2013-06-21 03:55:33 -0300 | [diff] [blame] | 2722 | struct coda_dev *dev = ctx->dev; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2723 | u32 wr_ptr, start_ptr; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2724 | |
Philipp Zabel | ec25f68 | 2012-07-20 08:54:29 -0300 | [diff] [blame] | 2725 | src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx); |
| 2726 | dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2727 | |
| 2728 | /* Get results from the coda */ |
| 2729 | coda_read(dev, CODA_RET_ENC_PIC_TYPE); |
| 2730 | start_ptr = coda_read(dev, CODA_CMD_ENC_PIC_BB_START); |
Philipp Zabel | 5677e3b | 2013-06-21 03:55:30 -0300 | [diff] [blame] | 2731 | wr_ptr = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx)); |
| 2732 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2733 | /* Calculate bytesused field */ |
| 2734 | if (dst_buf->v4l2_buf.sequence == 0) { |
Philipp Zabel | 366108f | 2013-06-21 03:55:27 -0300 | [diff] [blame] | 2735 | vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr + |
| 2736 | ctx->vpu_header_size[0] + |
| 2737 | ctx->vpu_header_size[1] + |
| 2738 | ctx->vpu_header_size[2]); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2739 | } else { |
Philipp Zabel | 366108f | 2013-06-21 03:55:27 -0300 | [diff] [blame] | 2740 | vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2741 | } |
| 2742 | |
| 2743 | v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, "frame size = %u\n", |
| 2744 | wr_ptr - start_ptr); |
| 2745 | |
| 2746 | coda_read(dev, CODA_RET_ENC_PIC_SLICE_NUM); |
| 2747 | coda_read(dev, CODA_RET_ENC_PIC_FLAG); |
| 2748 | |
| 2749 | if (src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) { |
| 2750 | dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME; |
| 2751 | dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME; |
| 2752 | } else { |
| 2753 | dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME; |
| 2754 | dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME; |
| 2755 | } |
| 2756 | |
Kamil Debski | ccd571c | 2013-04-24 10:38:24 -0300 | [diff] [blame] | 2757 | dst_buf->v4l2_buf.timestamp = src_buf->v4l2_buf.timestamp; |
| 2758 | dst_buf->v4l2_buf.timecode = src_buf->v4l2_buf.timecode; |
| 2759 | |
Philipp Zabel | ec25f68 | 2012-07-20 08:54:29 -0300 | [diff] [blame] | 2760 | v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2761 | v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE); |
| 2762 | |
| 2763 | ctx->gopcounter--; |
| 2764 | if (ctx->gopcounter < 0) |
| 2765 | ctx->gopcounter = ctx->params.gop_size - 1; |
| 2766 | |
| 2767 | v4l2_dbg(1, coda_debug, &dev->v4l2_dev, |
| 2768 | "job finished: encoding frame (%d) (%s)\n", |
| 2769 | dst_buf->v4l2_buf.sequence, |
| 2770 | (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ? |
| 2771 | "KEYFRAME" : "PFRAME"); |
Philipp Zabel | 477c1cf | 2013-06-21 03:55:33 -0300 | [diff] [blame] | 2772 | } |
| 2773 | |
| 2774 | static irqreturn_t coda_irq_handler(int irq, void *data) |
| 2775 | { |
| 2776 | struct coda_dev *dev = data; |
| 2777 | struct coda_ctx *ctx; |
| 2778 | |
| 2779 | cancel_delayed_work(&dev->timeout); |
| 2780 | |
| 2781 | /* read status register to attend the IRQ */ |
| 2782 | coda_read(dev, CODA_REG_BIT_INT_STATUS); |
| 2783 | coda_write(dev, CODA_REG_BIT_INT_CLEAR_SET, |
| 2784 | CODA_REG_BIT_INT_CLEAR); |
| 2785 | |
| 2786 | ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev); |
| 2787 | if (ctx == NULL) { |
| 2788 | v4l2_err(&dev->v4l2_dev, "Instance released before the end of transaction\n"); |
| 2789 | mutex_unlock(&dev->coda_mutex); |
| 2790 | return IRQ_HANDLED; |
| 2791 | } |
| 2792 | |
| 2793 | if (ctx->aborting) { |
| 2794 | v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, |
| 2795 | "task has been aborted\n"); |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 2796 | goto out; |
Philipp Zabel | 477c1cf | 2013-06-21 03:55:33 -0300 | [diff] [blame] | 2797 | } |
| 2798 | |
| 2799 | if (coda_isbusy(ctx->dev)) { |
| 2800 | v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, |
| 2801 | "coda is still busy!!!!\n"); |
| 2802 | return IRQ_NONE; |
| 2803 | } |
| 2804 | |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 2805 | if (ctx->inst_type == CODA_INST_DECODER) |
| 2806 | coda_finish_decode(ctx); |
| 2807 | else |
| 2808 | coda_finish_encode(ctx); |
| 2809 | |
| 2810 | out: |
| 2811 | if (ctx->aborting || (!ctx->streamon_cap && !ctx->streamon_out)) { |
| 2812 | v4l2_dbg(1, coda_debug, &dev->v4l2_dev, |
| 2813 | "%s: sent command 'SEQ_END' to coda\n", __func__); |
| 2814 | if (coda_command_sync(ctx, CODA_COMMAND_SEQ_END)) { |
| 2815 | v4l2_err(&dev->v4l2_dev, |
| 2816 | "CODA_COMMAND_SEQ_END failed\n"); |
| 2817 | } |
| 2818 | |
| 2819 | kfifo_init(&ctx->bitstream_fifo, |
| 2820 | ctx->bitstream.vaddr, ctx->bitstream.size); |
| 2821 | |
| 2822 | coda_free_framebuffers(ctx); |
| 2823 | coda_free_context_buffers(ctx); |
| 2824 | } |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2825 | |
Philipp Zabel | fcb6282 | 2013-05-23 10:43:00 -0300 | [diff] [blame] | 2826 | mutex_unlock(&dev->coda_mutex); |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 2827 | mutex_unlock(&ctx->buffer_mutex); |
Philipp Zabel | fcb6282 | 2013-05-23 10:43:00 -0300 | [diff] [blame] | 2828 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2829 | v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->m2m_ctx); |
| 2830 | |
| 2831 | return IRQ_HANDLED; |
| 2832 | } |
| 2833 | |
Philipp Zabel | 2fb57f0 | 2012-07-25 09:22:07 -0300 | [diff] [blame] | 2834 | static void coda_timeout(struct work_struct *work) |
| 2835 | { |
| 2836 | struct coda_ctx *ctx; |
| 2837 | struct coda_dev *dev = container_of(to_delayed_work(work), |
| 2838 | struct coda_dev, timeout); |
| 2839 | |
Philipp Zabel | 39cc029 | 2013-05-21 10:32:42 -0300 | [diff] [blame] | 2840 | 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] | 2841 | |
| 2842 | mutex_lock(&dev->dev_mutex); |
| 2843 | list_for_each_entry(ctx, &dev->instances, list) { |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 2844 | if (mutex_is_locked(&ctx->buffer_mutex)) |
| 2845 | mutex_unlock(&ctx->buffer_mutex); |
Philipp Zabel | 2fb57f0 | 2012-07-25 09:22:07 -0300 | [diff] [blame] | 2846 | v4l2_m2m_streamoff(NULL, ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT); |
| 2847 | v4l2_m2m_streamoff(NULL, ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE); |
| 2848 | } |
| 2849 | mutex_unlock(&dev->dev_mutex); |
Philipp Zabel | fcb6282 | 2013-05-23 10:43:00 -0300 | [diff] [blame] | 2850 | |
| 2851 | mutex_unlock(&dev->coda_mutex); |
| 2852 | ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev); |
| 2853 | v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->m2m_ctx); |
Philipp Zabel | 2fb57f0 | 2012-07-25 09:22:07 -0300 | [diff] [blame] | 2854 | } |
| 2855 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2856 | static u32 coda_supported_firmwares[] = { |
| 2857 | CODA_FIRMWARE_VERNUM(CODA_DX6, 2, 2, 5), |
Philipp Zabel | 5677e3b | 2013-06-21 03:55:30 -0300 | [diff] [blame] | 2858 | CODA_FIRMWARE_VERNUM(CODA_7541, 1, 4, 50), |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2859 | }; |
| 2860 | |
| 2861 | static bool coda_firmware_supported(u32 vernum) |
| 2862 | { |
| 2863 | int i; |
| 2864 | |
| 2865 | for (i = 0; i < ARRAY_SIZE(coda_supported_firmwares); i++) |
| 2866 | if (vernum == coda_supported_firmwares[i]) |
| 2867 | return true; |
| 2868 | return false; |
| 2869 | } |
| 2870 | |
| 2871 | static char *coda_product_name(int product) |
| 2872 | { |
| 2873 | static char buf[9]; |
| 2874 | |
| 2875 | switch (product) { |
| 2876 | case CODA_DX6: |
| 2877 | return "CodaDx6"; |
Philipp Zabel | df1e74c | 2012-07-02 06:07:10 -0300 | [diff] [blame] | 2878 | case CODA_7541: |
| 2879 | return "CODA7541"; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2880 | default: |
| 2881 | snprintf(buf, sizeof(buf), "(0x%04x)", product); |
| 2882 | return buf; |
| 2883 | } |
| 2884 | } |
| 2885 | |
Philipp Zabel | 87048bb | 2012-07-02 07:03:43 -0300 | [diff] [blame] | 2886 | static int coda_hw_init(struct coda_dev *dev) |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2887 | { |
| 2888 | u16 product, major, minor, release; |
| 2889 | u32 data; |
| 2890 | u16 *p; |
Fabio Estevam | 79830db | 2013-08-21 11:14:17 -0300 | [diff] [blame] | 2891 | int i, ret; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2892 | |
Fabio Estevam | 79830db | 2013-08-21 11:14:17 -0300 | [diff] [blame] | 2893 | ret = clk_prepare_enable(dev->clk_per); |
| 2894 | if (ret) |
| 2895 | return ret; |
| 2896 | |
| 2897 | ret = clk_prepare_enable(dev->clk_ahb); |
| 2898 | if (ret) |
| 2899 | goto err_clk_ahb; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2900 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2901 | /* |
| 2902 | * Copy the first CODA_ISRAM_SIZE in the internal SRAM. |
Philipp Zabel | 87048bb | 2012-07-02 07:03:43 -0300 | [diff] [blame] | 2903 | * The 16-bit chars in the code buffer are in memory access |
| 2904 | * order, re-sort them to CODA order for register download. |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2905 | * Data in this SRAM survives a reboot. |
| 2906 | */ |
Philipp Zabel | 87048bb | 2012-07-02 07:03:43 -0300 | [diff] [blame] | 2907 | p = (u16 *)dev->codebuf.vaddr; |
| 2908 | if (dev->devtype->product == CODA_DX6) { |
| 2909 | for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) { |
| 2910 | data = CODA_DOWN_ADDRESS_SET(i) | |
| 2911 | CODA_DOWN_DATA_SET(p[i ^ 1]); |
| 2912 | coda_write(dev, data, CODA_REG_BIT_CODE_DOWN); |
| 2913 | } |
| 2914 | } else { |
| 2915 | for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) { |
| 2916 | data = CODA_DOWN_ADDRESS_SET(i) | |
| 2917 | CODA_DOWN_DATA_SET(p[round_down(i, 4) + |
| 2918 | 3 - (i % 4)]); |
| 2919 | coda_write(dev, data, CODA_REG_BIT_CODE_DOWN); |
| 2920 | } |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2921 | } |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2922 | |
Philipp Zabel | 9acf769 | 2013-05-23 10:42:56 -0300 | [diff] [blame] | 2923 | /* Clear registers */ |
| 2924 | for (i = 0; i < 64; i++) |
| 2925 | coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4); |
| 2926 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2927 | /* Tell the BIT where to find everything it needs */ |
Philipp Zabel | 5677e3b | 2013-06-21 03:55:30 -0300 | [diff] [blame] | 2928 | if (dev->devtype->product == CODA_7541) { |
| 2929 | coda_write(dev, dev->tempbuf.paddr, |
| 2930 | CODA_REG_BIT_TEMP_BUF_ADDR); |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 2931 | coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM); |
Philipp Zabel | 5677e3b | 2013-06-21 03:55:30 -0300 | [diff] [blame] | 2932 | } else { |
| 2933 | coda_write(dev, dev->workbuf.paddr, |
| 2934 | CODA_REG_BIT_WORK_BUF_ADDR); |
| 2935 | } |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2936 | coda_write(dev, dev->codebuf.paddr, |
| 2937 | CODA_REG_BIT_CODE_BUF_ADDR); |
| 2938 | coda_write(dev, 0, CODA_REG_BIT_CODE_RUN); |
| 2939 | |
| 2940 | /* Set default values */ |
| 2941 | switch (dev->devtype->product) { |
| 2942 | case CODA_DX6: |
| 2943 | coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL); |
| 2944 | break; |
| 2945 | default: |
| 2946 | coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL); |
| 2947 | } |
| 2948 | coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL); |
Philipp Zabel | 1043667 | 2012-07-02 09:03:55 -0300 | [diff] [blame] | 2949 | |
| 2950 | if (dev->devtype->product != CODA_DX6) |
| 2951 | coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE); |
| 2952 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2953 | coda_write(dev, CODA_INT_INTERRUPT_ENABLE, |
| 2954 | CODA_REG_BIT_INT_ENABLE); |
| 2955 | |
| 2956 | /* Reset VPU and start processor */ |
| 2957 | data = coda_read(dev, CODA_REG_BIT_CODE_RESET); |
| 2958 | data |= CODA_REG_RESET_ENABLE; |
| 2959 | coda_write(dev, data, CODA_REG_BIT_CODE_RESET); |
| 2960 | udelay(10); |
| 2961 | data &= ~CODA_REG_RESET_ENABLE; |
| 2962 | coda_write(dev, data, CODA_REG_BIT_CODE_RESET); |
| 2963 | coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN); |
| 2964 | |
| 2965 | /* Load firmware */ |
| 2966 | coda_write(dev, 0, CODA_CMD_FIRMWARE_VERNUM); |
| 2967 | coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY); |
| 2968 | coda_write(dev, 0, CODA_REG_BIT_RUN_INDEX); |
| 2969 | coda_write(dev, 0, CODA_REG_BIT_RUN_COD_STD); |
| 2970 | coda_write(dev, CODA_COMMAND_FIRMWARE_GET, CODA_REG_BIT_RUN_COMMAND); |
| 2971 | if (coda_wait_timeout(dev)) { |
| 2972 | clk_disable_unprepare(dev->clk_per); |
| 2973 | clk_disable_unprepare(dev->clk_ahb); |
| 2974 | v4l2_err(&dev->v4l2_dev, "firmware get command error\n"); |
| 2975 | return -EIO; |
| 2976 | } |
| 2977 | |
| 2978 | /* Check we are compatible with the loaded firmware */ |
| 2979 | data = coda_read(dev, CODA_CMD_FIRMWARE_VERNUM); |
| 2980 | product = CODA_FIRMWARE_PRODUCT(data); |
| 2981 | major = CODA_FIRMWARE_MAJOR(data); |
| 2982 | minor = CODA_FIRMWARE_MINOR(data); |
| 2983 | release = CODA_FIRMWARE_RELEASE(data); |
| 2984 | |
| 2985 | clk_disable_unprepare(dev->clk_per); |
| 2986 | clk_disable_unprepare(dev->clk_ahb); |
| 2987 | |
| 2988 | if (product != dev->devtype->product) { |
| 2989 | v4l2_err(&dev->v4l2_dev, "Wrong firmware. Hw: %s, Fw: %s," |
| 2990 | " Version: %u.%u.%u\n", |
| 2991 | coda_product_name(dev->devtype->product), |
| 2992 | coda_product_name(product), major, minor, release); |
| 2993 | return -EINVAL; |
| 2994 | } |
| 2995 | |
| 2996 | v4l2_info(&dev->v4l2_dev, "Initialized %s.\n", |
| 2997 | coda_product_name(product)); |
| 2998 | |
| 2999 | if (coda_firmware_supported(data)) { |
| 3000 | v4l2_info(&dev->v4l2_dev, "Firmware version: %u.%u.%u\n", |
| 3001 | major, minor, release); |
| 3002 | } else { |
| 3003 | v4l2_warn(&dev->v4l2_dev, "Unsupported firmware version: " |
| 3004 | "%u.%u.%u\n", major, minor, release); |
| 3005 | } |
| 3006 | |
| 3007 | return 0; |
Fabio Estevam | 79830db | 2013-08-21 11:14:17 -0300 | [diff] [blame] | 3008 | |
| 3009 | err_clk_ahb: |
| 3010 | clk_disable_unprepare(dev->clk_per); |
| 3011 | return ret; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 3012 | } |
| 3013 | |
| 3014 | static void coda_fw_callback(const struct firmware *fw, void *context) |
| 3015 | { |
| 3016 | struct coda_dev *dev = context; |
| 3017 | struct platform_device *pdev = dev->plat_dev; |
| 3018 | int ret; |
| 3019 | |
| 3020 | if (!fw) { |
| 3021 | v4l2_err(&dev->v4l2_dev, "firmware request failed\n"); |
| 3022 | return; |
| 3023 | } |
| 3024 | |
| 3025 | /* allocate auxiliary per-device code buffer for the BIT processor */ |
Philipp Zabel | 5677e3b | 2013-06-21 03:55:30 -0300 | [diff] [blame] | 3026 | ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size); |
| 3027 | if (ret < 0) { |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 3028 | dev_err(&pdev->dev, "failed to allocate code buffer\n"); |
| 3029 | return; |
| 3030 | } |
| 3031 | |
Philipp Zabel | 87048bb | 2012-07-02 07:03:43 -0300 | [diff] [blame] | 3032 | /* Copy the whole firmware image to the code buffer */ |
| 3033 | memcpy(dev->codebuf.vaddr, fw->data, fw->size); |
| 3034 | release_firmware(fw); |
| 3035 | |
| 3036 | ret = coda_hw_init(dev); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 3037 | if (ret) { |
| 3038 | v4l2_err(&dev->v4l2_dev, "HW initialization failed\n"); |
| 3039 | return; |
| 3040 | } |
| 3041 | |
| 3042 | dev->vfd.fops = &coda_fops, |
| 3043 | dev->vfd.ioctl_ops = &coda_ioctl_ops; |
| 3044 | dev->vfd.release = video_device_release_empty, |
| 3045 | dev->vfd.lock = &dev->dev_mutex; |
| 3046 | dev->vfd.v4l2_dev = &dev->v4l2_dev; |
Hans Verkuil | 954f340 | 2012-09-05 06:05:50 -0300 | [diff] [blame] | 3047 | dev->vfd.vfl_dir = VFL_DIR_M2M; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 3048 | snprintf(dev->vfd.name, sizeof(dev->vfd.name), "%s", CODA_NAME); |
| 3049 | video_set_drvdata(&dev->vfd, dev); |
| 3050 | |
| 3051 | dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev); |
| 3052 | if (IS_ERR(dev->alloc_ctx)) { |
| 3053 | v4l2_err(&dev->v4l2_dev, "Failed to alloc vb2 context\n"); |
| 3054 | return; |
| 3055 | } |
| 3056 | |
| 3057 | dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops); |
| 3058 | if (IS_ERR(dev->m2m_dev)) { |
| 3059 | v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n"); |
| 3060 | goto rel_ctx; |
| 3061 | } |
| 3062 | |
| 3063 | ret = video_register_device(&dev->vfd, VFL_TYPE_GRABBER, 0); |
| 3064 | if (ret) { |
| 3065 | v4l2_err(&dev->v4l2_dev, "Failed to register video device\n"); |
| 3066 | goto rel_m2m; |
| 3067 | } |
| 3068 | v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video%d\n", |
| 3069 | dev->vfd.num); |
| 3070 | |
| 3071 | return; |
| 3072 | |
| 3073 | rel_m2m: |
| 3074 | v4l2_m2m_release(dev->m2m_dev); |
| 3075 | rel_ctx: |
| 3076 | vb2_dma_contig_cleanup_ctx(dev->alloc_ctx); |
| 3077 | } |
| 3078 | |
| 3079 | static int coda_firmware_request(struct coda_dev *dev) |
| 3080 | { |
| 3081 | char *fw = dev->devtype->firmware; |
| 3082 | |
| 3083 | dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw, |
| 3084 | coda_product_name(dev->devtype->product)); |
| 3085 | |
| 3086 | return request_firmware_nowait(THIS_MODULE, true, |
| 3087 | fw, &dev->plat_dev->dev, GFP_KERNEL, dev, coda_fw_callback); |
| 3088 | } |
| 3089 | |
| 3090 | enum coda_platform { |
| 3091 | CODA_IMX27, |
Philipp Zabel | df1e74c | 2012-07-02 06:07:10 -0300 | [diff] [blame] | 3092 | CODA_IMX53, |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 3093 | }; |
| 3094 | |
Emil Goode | c06d875 | 2012-08-14 17:44:42 -0300 | [diff] [blame] | 3095 | static const struct coda_devtype coda_devdata[] = { |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 3096 | [CODA_IMX27] = { |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 3097 | .firmware = "v4l-codadx6-imx27.bin", |
| 3098 | .product = CODA_DX6, |
| 3099 | .codecs = codadx6_codecs, |
| 3100 | .num_codecs = ARRAY_SIZE(codadx6_codecs), |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 3101 | }, |
Philipp Zabel | df1e74c | 2012-07-02 06:07:10 -0300 | [diff] [blame] | 3102 | [CODA_IMX53] = { |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 3103 | .firmware = "v4l-coda7541-imx53.bin", |
| 3104 | .product = CODA_7541, |
| 3105 | .codecs = coda7_codecs, |
| 3106 | .num_codecs = ARRAY_SIZE(coda7_codecs), |
Philipp Zabel | df1e74c | 2012-07-02 06:07:10 -0300 | [diff] [blame] | 3107 | }, |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 3108 | }; |
| 3109 | |
| 3110 | static struct platform_device_id coda_platform_ids[] = { |
| 3111 | { .name = "coda-imx27", .driver_data = CODA_IMX27 }, |
Fabio Estevam | 4e44cd0 | 2012-10-10 00:07:38 -0300 | [diff] [blame] | 3112 | { .name = "coda-imx53", .driver_data = CODA_IMX53 }, |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 3113 | { /* sentinel */ } |
| 3114 | }; |
| 3115 | MODULE_DEVICE_TABLE(platform, coda_platform_ids); |
| 3116 | |
| 3117 | #ifdef CONFIG_OF |
| 3118 | static const struct of_device_id coda_dt_ids[] = { |
Alexander Shiyan | 7b0dd9e | 2013-06-15 08:09:57 -0300 | [diff] [blame] | 3119 | { .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] }, |
Philipp Zabel | df1e74c | 2012-07-02 06:07:10 -0300 | [diff] [blame] | 3120 | { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] }, |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 3121 | { /* sentinel */ } |
| 3122 | }; |
| 3123 | MODULE_DEVICE_TABLE(of, coda_dt_ids); |
| 3124 | #endif |
| 3125 | |
Greg Kroah-Hartman | 4c62e97 | 2012-12-21 13:17:53 -0800 | [diff] [blame] | 3126 | static int coda_probe(struct platform_device *pdev) |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 3127 | { |
| 3128 | const struct of_device_id *of_id = |
| 3129 | of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev); |
| 3130 | const struct platform_device_id *pdev_id; |
Philipp Zabel | 657eee7 | 2013-04-29 16:17:14 -0700 | [diff] [blame] | 3131 | struct coda_platform_data *pdata = pdev->dev.platform_data; |
| 3132 | struct device_node *np = pdev->dev.of_node; |
| 3133 | struct gen_pool *pool; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 3134 | struct coda_dev *dev; |
| 3135 | struct resource *res; |
| 3136 | int ret, irq; |
| 3137 | |
| 3138 | dev = devm_kzalloc(&pdev->dev, sizeof *dev, GFP_KERNEL); |
| 3139 | if (!dev) { |
| 3140 | dev_err(&pdev->dev, "Not enough memory for %s\n", |
| 3141 | CODA_NAME); |
| 3142 | return -ENOMEM; |
| 3143 | } |
| 3144 | |
| 3145 | spin_lock_init(&dev->irqlock); |
Philipp Zabel | e11f3e6 | 2012-07-25 09:16:58 -0300 | [diff] [blame] | 3146 | INIT_LIST_HEAD(&dev->instances); |
Philipp Zabel | 2fb57f0 | 2012-07-25 09:22:07 -0300 | [diff] [blame] | 3147 | INIT_DELAYED_WORK(&dev->timeout, coda_timeout); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 3148 | |
| 3149 | dev->plat_dev = pdev; |
| 3150 | dev->clk_per = devm_clk_get(&pdev->dev, "per"); |
| 3151 | if (IS_ERR(dev->clk_per)) { |
| 3152 | dev_err(&pdev->dev, "Could not get per clock\n"); |
| 3153 | return PTR_ERR(dev->clk_per); |
| 3154 | } |
| 3155 | |
| 3156 | dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb"); |
| 3157 | if (IS_ERR(dev->clk_ahb)) { |
| 3158 | dev_err(&pdev->dev, "Could not get ahb clock\n"); |
| 3159 | return PTR_ERR(dev->clk_ahb); |
| 3160 | } |
| 3161 | |
| 3162 | /* Get memory for physical registers */ |
| 3163 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Philipp Zabel | 611cbbf | 2013-05-21 04:19:27 -0300 | [diff] [blame] | 3164 | dev->regs_base = devm_ioremap_resource(&pdev->dev, res); |
| 3165 | if (IS_ERR(dev->regs_base)) |
| 3166 | return PTR_ERR(dev->regs_base); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 3167 | |
| 3168 | /* IRQ */ |
| 3169 | irq = platform_get_irq(pdev, 0); |
| 3170 | if (irq < 0) { |
| 3171 | dev_err(&pdev->dev, "failed to get irq resource\n"); |
| 3172 | return -ENOENT; |
| 3173 | } |
| 3174 | |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 3175 | if (devm_request_threaded_irq(&pdev->dev, irq, NULL, coda_irq_handler, |
| 3176 | IRQF_ONESHOT, CODA_NAME, dev) < 0) { |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 3177 | dev_err(&pdev->dev, "failed to request irq\n"); |
| 3178 | return -ENOENT; |
| 3179 | } |
| 3180 | |
Philipp Zabel | 657eee7 | 2013-04-29 16:17:14 -0700 | [diff] [blame] | 3181 | /* Get IRAM pool from device tree or platform data */ |
| 3182 | pool = of_get_named_gen_pool(np, "iram", 0); |
| 3183 | if (!pool && pdata) |
| 3184 | pool = dev_get_gen_pool(pdata->iram_dev); |
| 3185 | if (!pool) { |
| 3186 | dev_err(&pdev->dev, "iram pool not available\n"); |
| 3187 | return -ENOMEM; |
| 3188 | } |
| 3189 | dev->iram_pool = pool; |
| 3190 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 3191 | ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev); |
| 3192 | if (ret) |
| 3193 | return ret; |
| 3194 | |
| 3195 | mutex_init(&dev->dev_mutex); |
Philipp Zabel | fcb6282 | 2013-05-23 10:43:00 -0300 | [diff] [blame] | 3196 | mutex_init(&dev->coda_mutex); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 3197 | |
| 3198 | pdev_id = of_id ? of_id->data : platform_get_device_id(pdev); |
| 3199 | |
| 3200 | if (of_id) { |
| 3201 | dev->devtype = of_id->data; |
| 3202 | } else if (pdev_id) { |
| 3203 | dev->devtype = &coda_devdata[pdev_id->driver_data]; |
| 3204 | } else { |
| 3205 | v4l2_device_unregister(&dev->v4l2_dev); |
| 3206 | return -EINVAL; |
| 3207 | } |
| 3208 | |
| 3209 | /* allocate auxiliary per-device buffers for the BIT processor */ |
| 3210 | switch (dev->devtype->product) { |
| 3211 | case CODA_DX6: |
Philipp Zabel | 5677e3b | 2013-06-21 03:55:30 -0300 | [diff] [blame] | 3212 | ret = coda_alloc_aux_buf(dev, &dev->workbuf, |
| 3213 | CODADX6_WORK_BUF_SIZE); |
| 3214 | if (ret < 0) { |
| 3215 | dev_err(&pdev->dev, "failed to allocate work buffer\n"); |
| 3216 | v4l2_device_unregister(&dev->v4l2_dev); |
| 3217 | return ret; |
| 3218 | } |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 3219 | break; |
Philipp Zabel | 5677e3b | 2013-06-21 03:55:30 -0300 | [diff] [blame] | 3220 | case CODA_7541: |
| 3221 | dev->tempbuf.size = CODA7_TEMP_BUF_SIZE; |
| 3222 | break; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 3223 | } |
Philipp Zabel | 5677e3b | 2013-06-21 03:55:30 -0300 | [diff] [blame] | 3224 | if (dev->tempbuf.size) { |
| 3225 | ret = coda_alloc_aux_buf(dev, &dev->tempbuf, |
| 3226 | dev->tempbuf.size); |
| 3227 | if (ret < 0) { |
| 3228 | dev_err(&pdev->dev, "failed to allocate temp buffer\n"); |
| 3229 | v4l2_device_unregister(&dev->v4l2_dev); |
| 3230 | return ret; |
| 3231 | } |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 3232 | } |
| 3233 | |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 3234 | switch (dev->devtype->product) { |
| 3235 | case CODA_DX6: |
Philipp Zabel | 657eee7 | 2013-04-29 16:17:14 -0700 | [diff] [blame] | 3236 | dev->iram_size = CODADX6_IRAM_SIZE; |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 3237 | break; |
| 3238 | case CODA_7541: |
Philipp Zabel | 657eee7 | 2013-04-29 16:17:14 -0700 | [diff] [blame] | 3239 | dev->iram_size = CODA7_IRAM_SIZE; |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 3240 | break; |
| 3241 | } |
Philipp Zabel | 657eee7 | 2013-04-29 16:17:14 -0700 | [diff] [blame] | 3242 | dev->iram_vaddr = gen_pool_alloc(dev->iram_pool, dev->iram_size); |
| 3243 | if (!dev->iram_vaddr) { |
| 3244 | dev_err(&pdev->dev, "unable to alloc iram\n"); |
| 3245 | return -ENOMEM; |
Philipp Zabel | 1043667 | 2012-07-02 09:03:55 -0300 | [diff] [blame] | 3246 | } |
Philipp Zabel | 657eee7 | 2013-04-29 16:17:14 -0700 | [diff] [blame] | 3247 | dev->iram_paddr = gen_pool_virt_to_phys(dev->iram_pool, |
| 3248 | dev->iram_vaddr); |
Philipp Zabel | 1043667 | 2012-07-02 09:03:55 -0300 | [diff] [blame] | 3249 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 3250 | platform_set_drvdata(pdev, dev); |
| 3251 | |
| 3252 | return coda_firmware_request(dev); |
| 3253 | } |
| 3254 | |
| 3255 | static int coda_remove(struct platform_device *pdev) |
| 3256 | { |
| 3257 | struct coda_dev *dev = platform_get_drvdata(pdev); |
| 3258 | |
| 3259 | video_unregister_device(&dev->vfd); |
| 3260 | if (dev->m2m_dev) |
| 3261 | v4l2_m2m_release(dev->m2m_dev); |
| 3262 | if (dev->alloc_ctx) |
| 3263 | vb2_dma_contig_cleanup_ctx(dev->alloc_ctx); |
| 3264 | v4l2_device_unregister(&dev->v4l2_dev); |
Philipp Zabel | 657eee7 | 2013-04-29 16:17:14 -0700 | [diff] [blame] | 3265 | if (dev->iram_vaddr) |
| 3266 | gen_pool_free(dev->iram_pool, dev->iram_vaddr, dev->iram_size); |
Philipp Zabel | 5677e3b | 2013-06-21 03:55:30 -0300 | [diff] [blame] | 3267 | coda_free_aux_buf(dev, &dev->codebuf); |
| 3268 | coda_free_aux_buf(dev, &dev->tempbuf); |
| 3269 | coda_free_aux_buf(dev, &dev->workbuf); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 3270 | return 0; |
| 3271 | } |
| 3272 | |
| 3273 | static struct platform_driver coda_driver = { |
| 3274 | .probe = coda_probe, |
Greg Kroah-Hartman | 4c62e97 | 2012-12-21 13:17:53 -0800 | [diff] [blame] | 3275 | .remove = coda_remove, |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 3276 | .driver = { |
| 3277 | .name = CODA_NAME, |
| 3278 | .owner = THIS_MODULE, |
| 3279 | .of_match_table = of_match_ptr(coda_dt_ids), |
| 3280 | }, |
| 3281 | .id_table = coda_platform_ids, |
| 3282 | }; |
| 3283 | |
| 3284 | module_platform_driver(coda_driver); |
| 3285 | |
| 3286 | MODULE_LICENSE("GPL"); |
| 3287 | MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>"); |
| 3288 | MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver"); |