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