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