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