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