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