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