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