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 | cde29ef | 2015-07-16 13:13:24 -0300 | [diff] [blame] | 18 | #include <linux/gcd.h> |
Philipp Zabel | 657eee7 | 2013-04-29 16:17:14 -0700 | [diff] [blame] | 19 | #include <linux/genalloc.h> |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 20 | #include <linux/interrupt.h> |
| 21 | #include <linux/io.h> |
| 22 | #include <linux/irq.h> |
Philipp Zabel | 9082a7c | 2013-06-21 03:55:31 -0300 | [diff] [blame] | 23 | #include <linux/kfifo.h> |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 24 | #include <linux/module.h> |
| 25 | #include <linux/of_device.h> |
| 26 | #include <linux/platform_device.h> |
Philipp Zabel | 1e17273 | 2014-07-11 06:36:23 -0300 | [diff] [blame] | 27 | #include <linux/pm_runtime.h> |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 28 | #include <linux/slab.h> |
| 29 | #include <linux/videodev2.h> |
| 30 | #include <linux/of.h> |
Mauro Carvalho Chehab | a71daaa | 2015-11-17 07:11:13 -0200 | [diff] [blame] | 31 | #include <linux/platform_data/media/coda.h> |
Philipp Zabel | 8f45284 | 2014-07-11 06:36:35 -0300 | [diff] [blame] | 32 | #include <linux/reset.h> |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 33 | |
| 34 | #include <media/v4l2-ctrls.h> |
| 35 | #include <media/v4l2-device.h> |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 36 | #include <media/v4l2-event.h> |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 37 | #include <media/v4l2-ioctl.h> |
| 38 | #include <media/v4l2-mem2mem.h> |
Junghak Sung | c139990 | 2015-09-22 10:30:29 -0300 | [diff] [blame] | 39 | #include <media/videobuf2-v4l2.h> |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 40 | #include <media/videobuf2-dma-contig.h> |
Philipp Zabel | bb04aa6 | 2015-01-23 13:51:30 -0300 | [diff] [blame] | 41 | #include <media/videobuf2-vmalloc.h> |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 42 | |
Philipp Zabel | a2b3e46 | 2014-07-23 12:28:39 -0300 | [diff] [blame] | 43 | #include "coda.h" |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 44 | |
| 45 | #define CODA_NAME "coda" |
| 46 | |
Philipp Zabel | 0cddc7e | 2013-09-30 10:34:44 -0300 | [diff] [blame] | 47 | #define CODADX6_MAX_INSTANCES 4 |
Philipp Zabel | 2c11d1b | 2014-10-02 14:08:28 -0300 | [diff] [blame] | 48 | #define CODA_MAX_FORMATS 4 |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 49 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 50 | #define CODA_ISRAM_SIZE (2048 * 2) |
| 51 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 52 | #define MIN_W 176 |
| 53 | #define MIN_H 144 |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 54 | |
| 55 | #define S_ALIGN 1 /* multiple of 2 */ |
| 56 | #define W_ALIGN 1 /* multiple of 2 */ |
| 57 | #define H_ALIGN 1 /* multiple of 2 */ |
| 58 | |
| 59 | #define fh_to_ctx(__fh) container_of(__fh, struct coda_ctx, fh) |
| 60 | |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 61 | int coda_debug; |
Philipp Zabel | c4eb1bfc | 2013-05-21 04:24:16 -0300 | [diff] [blame] | 62 | module_param(coda_debug, int, 0644); |
Philipp Zabel | d60b18b | 2014-08-05 14:00:15 -0300 | [diff] [blame] | 63 | MODULE_PARM_DESC(coda_debug, "Debug level (0-2)"); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 64 | |
Philipp Zabel | a269e53 | 2015-07-16 13:19:38 -0300 | [diff] [blame] | 65 | static int disable_tiling; |
| 66 | module_param(disable_tiling, int, 0644); |
| 67 | MODULE_PARM_DESC(disable_tiling, "Disable tiled frame buffers"); |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 68 | |
Philipp Zabel | a2b3e46 | 2014-07-23 12:28:39 -0300 | [diff] [blame] | 69 | void coda_write(struct coda_dev *dev, u32 data, u32 reg) |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 70 | { |
Philipp Zabel | d60b18b | 2014-08-05 14:00:15 -0300 | [diff] [blame] | 71 | v4l2_dbg(2, coda_debug, &dev->v4l2_dev, |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 72 | "%s: data=0x%x, reg=0x%x\n", __func__, data, reg); |
| 73 | writel(data, dev->regs_base + reg); |
| 74 | } |
| 75 | |
Philipp Zabel | a2b3e46 | 2014-07-23 12:28:39 -0300 | [diff] [blame] | 76 | unsigned int coda_read(struct coda_dev *dev, u32 reg) |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 77 | { |
| 78 | u32 data; |
Philipp Zabel | f23797b | 2014-08-06 08:02:23 -0300 | [diff] [blame] | 79 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 80 | data = readl(dev->regs_base + reg); |
Philipp Zabel | d60b18b | 2014-08-05 14:00:15 -0300 | [diff] [blame] | 81 | v4l2_dbg(2, coda_debug, &dev->v4l2_dev, |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 82 | "%s: data=0x%x, reg=0x%x\n", __func__, data, reg); |
| 83 | return data; |
| 84 | } |
| 85 | |
Philipp Zabel | 856d7d9 | 2014-09-29 09:53:44 -0300 | [diff] [blame] | 86 | void coda_write_base(struct coda_ctx *ctx, struct coda_q_data *q_data, |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 87 | struct vb2_v4l2_buffer *buf, unsigned int reg_y) |
Philipp Zabel | 856d7d9 | 2014-09-29 09:53:44 -0300 | [diff] [blame] | 88 | { |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 89 | u32 base_y = vb2_dma_contig_plane_dma_addr(&buf->vb2_buf, 0); |
Philipp Zabel | 856d7d9 | 2014-09-29 09:53:44 -0300 | [diff] [blame] | 90 | u32 base_cb, base_cr; |
| 91 | |
| 92 | switch (q_data->fourcc) { |
Philipp Zabel | 6727d4f | 2015-07-16 13:19:39 -0300 | [diff] [blame] | 93 | case V4L2_PIX_FMT_NV12: |
| 94 | case V4L2_PIX_FMT_YUV420: |
| 95 | default: |
| 96 | base_cb = base_y + q_data->bytesperline * q_data->height; |
| 97 | base_cr = base_cb + q_data->bytesperline * q_data->height / 4; |
| 98 | break; |
Philipp Zabel | 856d7d9 | 2014-09-29 09:53:44 -0300 | [diff] [blame] | 99 | case V4L2_PIX_FMT_YVU420: |
| 100 | /* Switch Cb and Cr for YVU420 format */ |
| 101 | base_cr = base_y + q_data->bytesperline * q_data->height; |
| 102 | base_cb = base_cr + q_data->bytesperline * q_data->height / 4; |
| 103 | break; |
Philipp Zabel | 4de6931 | 2014-10-02 14:08:26 -0300 | [diff] [blame] | 104 | case V4L2_PIX_FMT_YUV422P: |
| 105 | base_cb = base_y + q_data->bytesperline * q_data->height; |
| 106 | base_cr = base_cb + q_data->bytesperline * q_data->height / 2; |
Philipp Zabel | 856d7d9 | 2014-09-29 09:53:44 -0300 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | coda_write(ctx->dev, base_y, reg_y); |
| 110 | coda_write(ctx->dev, base_cb, reg_y + 4); |
| 111 | coda_write(ctx->dev, base_cr, reg_y + 8); |
| 112 | } |
| 113 | |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 114 | #define CODA_CODEC(mode, src_fourcc, dst_fourcc, max_w, max_h) \ |
| 115 | { mode, src_fourcc, dst_fourcc, max_w, max_h } |
| 116 | |
| 117 | /* |
| 118 | * Arrays of codecs supported by each given version of Coda: |
| 119 | * i.MX27 -> codadx6 |
| 120 | * i.MX5x -> coda7 |
| 121 | * i.MX6 -> coda960 |
| 122 | * Use V4L2_PIX_FMT_YUV420 as placeholder for all supported YUV 4:2:0 variants |
| 123 | */ |
Philipp Zabel | 814c376 | 2014-07-18 07:22:45 -0300 | [diff] [blame] | 124 | static const struct coda_codec codadx6_codecs[] = { |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 125 | CODA_CODEC(CODADX6_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 720, 576), |
| 126 | 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] | 127 | }; |
| 128 | |
Philipp Zabel | 814c376 | 2014-07-18 07:22:45 -0300 | [diff] [blame] | 129 | static const struct coda_codec coda7_codecs[] = { |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 130 | CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1280, 720), |
| 131 | CODA_CODEC(CODA7_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1280, 720), |
Philipp Zabel | cb1d3a3 | 2014-10-02 14:08:31 -0300 | [diff] [blame] | 132 | CODA_CODEC(CODA7_MODE_ENCODE_MJPG, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_JPEG, 8192, 8192), |
Philipp Zabel | b0ed05b | 2014-08-05 14:00:14 -0300 | [diff] [blame] | 133 | CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1088), |
| 134 | CODA_CODEC(CODA7_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1088), |
Philipp Zabel | cb1d3a3 | 2014-10-02 14:08:31 -0300 | [diff] [blame] | 135 | CODA_CODEC(CODA7_MODE_DECODE_MJPG, V4L2_PIX_FMT_JPEG, V4L2_PIX_FMT_YUV420, 8192, 8192), |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 136 | }; |
| 137 | |
Philipp Zabel | 814c376 | 2014-07-18 07:22:45 -0300 | [diff] [blame] | 138 | static const struct coda_codec coda9_codecs[] = { |
Philipp Zabel | b0ed05b | 2014-08-05 14:00:14 -0300 | [diff] [blame] | 139 | CODA_CODEC(CODA9_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1920, 1088), |
| 140 | CODA_CODEC(CODA9_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1920, 1088), |
| 141 | CODA_CODEC(CODA9_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1088), |
| 142 | CODA_CODEC(CODA9_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1088), |
Philipp Zabel | 8954844 | 2014-07-11 06:36:17 -0300 | [diff] [blame] | 143 | }; |
| 144 | |
Philipp Zabel | 2c11d1b | 2014-10-02 14:08:28 -0300 | [diff] [blame] | 145 | struct coda_video_device { |
| 146 | const char *name; |
| 147 | enum coda_inst_type type; |
| 148 | const struct coda_context_ops *ops; |
Philipp Zabel | a22496c | 2015-01-23 13:51:33 -0300 | [diff] [blame] | 149 | bool direct; |
Philipp Zabel | 2c11d1b | 2014-10-02 14:08:28 -0300 | [diff] [blame] | 150 | u32 src_formats[CODA_MAX_FORMATS]; |
| 151 | u32 dst_formats[CODA_MAX_FORMATS]; |
| 152 | }; |
| 153 | |
| 154 | static const struct coda_video_device coda_bit_encoder = { |
| 155 | .name = "coda-encoder", |
| 156 | .type = CODA_INST_ENCODER, |
| 157 | .ops = &coda_bit_encode_ops, |
| 158 | .src_formats = { |
Philipp Zabel | 6727d4f | 2015-07-16 13:19:39 -0300 | [diff] [blame] | 159 | V4L2_PIX_FMT_NV12, |
Philipp Zabel | 2c11d1b | 2014-10-02 14:08:28 -0300 | [diff] [blame] | 160 | V4L2_PIX_FMT_YUV420, |
| 161 | V4L2_PIX_FMT_YVU420, |
Philipp Zabel | 2c11d1b | 2014-10-02 14:08:28 -0300 | [diff] [blame] | 162 | }, |
| 163 | .dst_formats = { |
| 164 | V4L2_PIX_FMT_H264, |
| 165 | V4L2_PIX_FMT_MPEG4, |
| 166 | }, |
| 167 | }; |
| 168 | |
Philipp Zabel | cb1d3a3 | 2014-10-02 14:08:31 -0300 | [diff] [blame] | 169 | static const struct coda_video_device coda_bit_jpeg_encoder = { |
| 170 | .name = "coda-jpeg-encoder", |
| 171 | .type = CODA_INST_ENCODER, |
| 172 | .ops = &coda_bit_encode_ops, |
| 173 | .src_formats = { |
Philipp Zabel | 6727d4f | 2015-07-16 13:19:39 -0300 | [diff] [blame] | 174 | V4L2_PIX_FMT_NV12, |
Philipp Zabel | cb1d3a3 | 2014-10-02 14:08:31 -0300 | [diff] [blame] | 175 | V4L2_PIX_FMT_YUV420, |
| 176 | V4L2_PIX_FMT_YVU420, |
Philipp Zabel | cb1d3a3 | 2014-10-02 14:08:31 -0300 | [diff] [blame] | 177 | V4L2_PIX_FMT_YUV422P, |
| 178 | }, |
| 179 | .dst_formats = { |
| 180 | V4L2_PIX_FMT_JPEG, |
| 181 | }, |
| 182 | }; |
| 183 | |
Philipp Zabel | 2c11d1b | 2014-10-02 14:08:28 -0300 | [diff] [blame] | 184 | static const struct coda_video_device coda_bit_decoder = { |
| 185 | .name = "coda-decoder", |
| 186 | .type = CODA_INST_DECODER, |
| 187 | .ops = &coda_bit_decode_ops, |
| 188 | .src_formats = { |
| 189 | V4L2_PIX_FMT_H264, |
| 190 | V4L2_PIX_FMT_MPEG4, |
| 191 | }, |
| 192 | .dst_formats = { |
Philipp Zabel | 6727d4f | 2015-07-16 13:19:39 -0300 | [diff] [blame] | 193 | V4L2_PIX_FMT_NV12, |
Philipp Zabel | 2c11d1b | 2014-10-02 14:08:28 -0300 | [diff] [blame] | 194 | V4L2_PIX_FMT_YUV420, |
| 195 | V4L2_PIX_FMT_YVU420, |
Philipp Zabel | 2c11d1b | 2014-10-02 14:08:28 -0300 | [diff] [blame] | 196 | }, |
| 197 | }; |
| 198 | |
Philipp Zabel | cb1d3a3 | 2014-10-02 14:08:31 -0300 | [diff] [blame] | 199 | static const struct coda_video_device coda_bit_jpeg_decoder = { |
| 200 | .name = "coda-jpeg-decoder", |
| 201 | .type = CODA_INST_DECODER, |
| 202 | .ops = &coda_bit_decode_ops, |
| 203 | .src_formats = { |
| 204 | V4L2_PIX_FMT_JPEG, |
| 205 | }, |
| 206 | .dst_formats = { |
Philipp Zabel | 6727d4f | 2015-07-16 13:19:39 -0300 | [diff] [blame] | 207 | V4L2_PIX_FMT_NV12, |
Philipp Zabel | cb1d3a3 | 2014-10-02 14:08:31 -0300 | [diff] [blame] | 208 | V4L2_PIX_FMT_YUV420, |
| 209 | V4L2_PIX_FMT_YVU420, |
Philipp Zabel | cb1d3a3 | 2014-10-02 14:08:31 -0300 | [diff] [blame] | 210 | V4L2_PIX_FMT_YUV422P, |
| 211 | }, |
| 212 | }; |
| 213 | |
Philipp Zabel | 2c11d1b | 2014-10-02 14:08:28 -0300 | [diff] [blame] | 214 | static const struct coda_video_device *codadx6_video_devices[] = { |
| 215 | &coda_bit_encoder, |
| 216 | }; |
| 217 | |
| 218 | static const struct coda_video_device *coda7_video_devices[] = { |
Philipp Zabel | cb1d3a3 | 2014-10-02 14:08:31 -0300 | [diff] [blame] | 219 | &coda_bit_jpeg_encoder, |
| 220 | &coda_bit_jpeg_decoder, |
Philipp Zabel | 2c11d1b | 2014-10-02 14:08:28 -0300 | [diff] [blame] | 221 | &coda_bit_encoder, |
| 222 | &coda_bit_decoder, |
| 223 | }; |
| 224 | |
| 225 | static const struct coda_video_device *coda9_video_devices[] = { |
| 226 | &coda_bit_encoder, |
| 227 | &coda_bit_decoder, |
| 228 | }; |
| 229 | |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 230 | /* |
| 231 | * Normalize all supported YUV 4:2:0 formats to the value used in the codec |
| 232 | * tables. |
| 233 | */ |
| 234 | static u32 coda_format_normalize_yuv(u32 fourcc) |
| 235 | { |
Philipp Zabel | 8076c7e | 2015-07-09 07:10:18 -0300 | [diff] [blame] | 236 | switch (fourcc) { |
Philipp Zabel | 6727d4f | 2015-07-16 13:19:39 -0300 | [diff] [blame] | 237 | case V4L2_PIX_FMT_NV12: |
Philipp Zabel | 8076c7e | 2015-07-09 07:10:18 -0300 | [diff] [blame] | 238 | case V4L2_PIX_FMT_YUV420: |
| 239 | case V4L2_PIX_FMT_YVU420: |
Philipp Zabel | 8076c7e | 2015-07-09 07:10:18 -0300 | [diff] [blame] | 240 | case V4L2_PIX_FMT_YUV422P: |
| 241 | return V4L2_PIX_FMT_YUV420; |
| 242 | default: |
| 243 | return fourcc; |
| 244 | } |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 245 | } |
| 246 | |
Philipp Zabel | 814c376 | 2014-07-18 07:22:45 -0300 | [diff] [blame] | 247 | static const struct coda_codec *coda_find_codec(struct coda_dev *dev, |
| 248 | int src_fourcc, int dst_fourcc) |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 249 | { |
Philipp Zabel | 814c376 | 2014-07-18 07:22:45 -0300 | [diff] [blame] | 250 | const struct coda_codec *codecs = dev->devtype->codecs; |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 251 | int num_codecs = dev->devtype->num_codecs; |
| 252 | int k; |
| 253 | |
| 254 | src_fourcc = coda_format_normalize_yuv(src_fourcc); |
| 255 | dst_fourcc = coda_format_normalize_yuv(dst_fourcc); |
| 256 | if (src_fourcc == dst_fourcc) |
| 257 | return NULL; |
| 258 | |
| 259 | for (k = 0; k < num_codecs; k++) { |
| 260 | if (codecs[k].src_fourcc == src_fourcc && |
| 261 | codecs[k].dst_fourcc == dst_fourcc) |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 262 | break; |
| 263 | } |
| 264 | |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 265 | if (k == num_codecs) |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 266 | return NULL; |
| 267 | |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 268 | return &codecs[k]; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 269 | } |
| 270 | |
Philipp Zabel | 5762559 | 2013-09-30 10:34:51 -0300 | [diff] [blame] | 271 | static void coda_get_max_dimensions(struct coda_dev *dev, |
Philipp Zabel | 814c376 | 2014-07-18 07:22:45 -0300 | [diff] [blame] | 272 | const struct coda_codec *codec, |
Philipp Zabel | 5762559 | 2013-09-30 10:34:51 -0300 | [diff] [blame] | 273 | int *max_w, int *max_h) |
| 274 | { |
Philipp Zabel | 814c376 | 2014-07-18 07:22:45 -0300 | [diff] [blame] | 275 | const struct coda_codec *codecs = dev->devtype->codecs; |
Philipp Zabel | 5762559 | 2013-09-30 10:34:51 -0300 | [diff] [blame] | 276 | int num_codecs = dev->devtype->num_codecs; |
| 277 | unsigned int w, h; |
| 278 | int k; |
| 279 | |
| 280 | if (codec) { |
| 281 | w = codec->max_w; |
| 282 | h = codec->max_h; |
| 283 | } else { |
| 284 | for (k = 0, w = 0, h = 0; k < num_codecs; k++) { |
| 285 | w = max(w, codecs[k].max_w); |
| 286 | h = max(h, codecs[k].max_h); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | if (max_w) |
| 291 | *max_w = w; |
| 292 | if (max_h) |
| 293 | *max_h = h; |
| 294 | } |
| 295 | |
Philipp Zabel | 5542570 | 2015-12-02 14:58:50 -0200 | [diff] [blame] | 296 | static const struct coda_video_device *to_coda_video_device(struct video_device |
| 297 | *vdev) |
Philipp Zabel | 2c11d1b | 2014-10-02 14:08:28 -0300 | [diff] [blame] | 298 | { |
| 299 | struct coda_dev *dev = video_get_drvdata(vdev); |
| 300 | unsigned int i = vdev - dev->vfd; |
| 301 | |
| 302 | if (i >= dev->devtype->num_vdevs) |
| 303 | return NULL; |
| 304 | |
| 305 | return dev->devtype->vdevs[i]; |
| 306 | } |
| 307 | |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 308 | const char *coda_product_name(int product) |
Philipp Zabel | 927933f | 2013-09-30 10:34:48 -0300 | [diff] [blame] | 309 | { |
| 310 | static char buf[9]; |
| 311 | |
| 312 | switch (product) { |
| 313 | case CODA_DX6: |
| 314 | return "CodaDx6"; |
| 315 | case CODA_7541: |
| 316 | return "CODA7541"; |
Philipp Zabel | 8954844 | 2014-07-11 06:36:17 -0300 | [diff] [blame] | 317 | case CODA_960: |
| 318 | return "CODA960"; |
Philipp Zabel | 927933f | 2013-09-30 10:34:48 -0300 | [diff] [blame] | 319 | default: |
| 320 | snprintf(buf, sizeof(buf), "(0x%04x)", product); |
| 321 | return buf; |
| 322 | } |
| 323 | } |
| 324 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 325 | /* |
| 326 | * V4L2 ioctl() operations. |
| 327 | */ |
Philipp Zabel | 2e9e4f1 | 2013-09-30 10:34:50 -0300 | [diff] [blame] | 328 | static int coda_querycap(struct file *file, void *priv, |
| 329 | struct v4l2_capability *cap) |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 330 | { |
Philipp Zabel | 927933f | 2013-09-30 10:34:48 -0300 | [diff] [blame] | 331 | struct coda_ctx *ctx = fh_to_ctx(priv); |
| 332 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 333 | strlcpy(cap->driver, CODA_NAME, sizeof(cap->driver)); |
Philipp Zabel | 927933f | 2013-09-30 10:34:48 -0300 | [diff] [blame] | 334 | strlcpy(cap->card, coda_product_name(ctx->dev->devtype->product), |
| 335 | sizeof(cap->card)); |
Philipp Zabel | dad0e1c | 2013-05-21 04:18:22 -0300 | [diff] [blame] | 336 | strlcpy(cap->bus_info, "platform:" CODA_NAME, sizeof(cap->bus_info)); |
Philipp Zabel | 3898e7a | 2014-07-18 07:22:37 -0300 | [diff] [blame] | 337 | cap->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 338 | cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS; |
| 339 | |
| 340 | return 0; |
| 341 | } |
| 342 | |
Philipp Zabel | 22e244b | 2014-07-18 07:22:43 -0300 | [diff] [blame] | 343 | static int coda_enum_fmt(struct file *file, void *priv, |
| 344 | struct v4l2_fmtdesc *f) |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 345 | { |
Philipp Zabel | 2c11d1b | 2014-10-02 14:08:28 -0300 | [diff] [blame] | 346 | struct video_device *vdev = video_devdata(file); |
| 347 | const struct coda_video_device *cvd = to_coda_video_device(vdev); |
| 348 | const u32 *formats; |
Philipp Zabel | 22e244b | 2014-07-18 07:22:43 -0300 | [diff] [blame] | 349 | |
Philipp Zabel | 2c11d1b | 2014-10-02 14:08:28 -0300 | [diff] [blame] | 350 | if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) |
| 351 | formats = cvd->src_formats; |
| 352 | else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 353 | formats = cvd->dst_formats; |
Philipp Zabel | 22e244b | 2014-07-18 07:22:43 -0300 | [diff] [blame] | 354 | else |
Philipp Zabel | 2c11d1b | 2014-10-02 14:08:28 -0300 | [diff] [blame] | 355 | return -EINVAL; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 356 | |
Philipp Zabel | 2c11d1b | 2014-10-02 14:08:28 -0300 | [diff] [blame] | 357 | if (f->index >= CODA_MAX_FORMATS || formats[f->index] == 0) |
| 358 | return -EINVAL; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 359 | |
Philipp Zabel | 2c11d1b | 2014-10-02 14:08:28 -0300 | [diff] [blame] | 360 | f->pixelformat = formats[f->index]; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 361 | |
Philipp Zabel | 2c11d1b | 2014-10-02 14:08:28 -0300 | [diff] [blame] | 362 | return 0; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 363 | } |
| 364 | |
Philipp Zabel | 2e9e4f1 | 2013-09-30 10:34:50 -0300 | [diff] [blame] | 365 | static int coda_g_fmt(struct file *file, void *priv, |
| 366 | struct v4l2_format *f) |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 367 | { |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 368 | struct coda_q_data *q_data; |
| 369 | struct coda_ctx *ctx = fh_to_ctx(priv); |
| 370 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 371 | q_data = get_q_data(ctx, f->type); |
Philipp Zabel | b973629 | 2014-07-11 06:36:18 -0300 | [diff] [blame] | 372 | if (!q_data) |
| 373 | return -EINVAL; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 374 | |
| 375 | f->fmt.pix.field = V4L2_FIELD_NONE; |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 376 | f->fmt.pix.pixelformat = q_data->fourcc; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 377 | f->fmt.pix.width = q_data->width; |
| 378 | f->fmt.pix.height = q_data->height; |
Philipp Zabel | 2fd6a37 | 2014-07-11 06:36:36 -0300 | [diff] [blame] | 379 | f->fmt.pix.bytesperline = q_data->bytesperline; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 380 | |
| 381 | f->fmt.pix.sizeimage = q_data->sizeimage; |
Philipp Zabel | cb1d3a3 | 2014-10-02 14:08:31 -0300 | [diff] [blame] | 382 | if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_JPEG) |
| 383 | f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG; |
| 384 | else |
| 385 | f->fmt.pix.colorspace = ctx->colorspace; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 386 | |
| 387 | return 0; |
| 388 | } |
| 389 | |
Philipp Zabel | 2c11d1b | 2014-10-02 14:08:28 -0300 | [diff] [blame] | 390 | static int coda_try_pixelformat(struct coda_ctx *ctx, struct v4l2_format *f) |
| 391 | { |
| 392 | struct coda_q_data *q_data; |
| 393 | const u32 *formats; |
| 394 | int i; |
| 395 | |
| 396 | if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) |
| 397 | formats = ctx->cvd->src_formats; |
| 398 | else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 399 | formats = ctx->cvd->dst_formats; |
| 400 | else |
| 401 | return -EINVAL; |
| 402 | |
| 403 | for (i = 0; i < CODA_MAX_FORMATS; i++) { |
| 404 | if (formats[i] == f->fmt.pix.pixelformat) { |
| 405 | f->fmt.pix.pixelformat = formats[i]; |
| 406 | return 0; |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | /* Fall back to currently set pixelformat */ |
| 411 | q_data = get_q_data(ctx, f->type); |
| 412 | f->fmt.pix.pixelformat = q_data->fourcc; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 413 | |
| 414 | return 0; |
| 415 | } |
| 416 | |
Philipp Zabel | 8e428d5 | 2015-01-23 13:51:29 -0300 | [diff] [blame] | 417 | static unsigned int coda_estimate_sizeimage(struct coda_ctx *ctx, u32 sizeimage, |
| 418 | u32 width, u32 height) |
| 419 | { |
| 420 | /* |
| 421 | * This is a rough estimate for sensible compressed buffer |
| 422 | * sizes (between 1 and 16 bits per pixel). This could be |
| 423 | * improved by better format specific worst case estimates. |
| 424 | */ |
| 425 | return round_up(clamp(sizeimage, width * height / 8, |
| 426 | width * height * 2), PAGE_SIZE); |
| 427 | } |
| 428 | |
Philipp Zabel | 814c376 | 2014-07-18 07:22:45 -0300 | [diff] [blame] | 429 | static int coda_try_fmt(struct coda_ctx *ctx, const struct coda_codec *codec, |
Philipp Zabel | 5762559 | 2013-09-30 10:34:51 -0300 | [diff] [blame] | 430 | struct v4l2_format *f) |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 431 | { |
Philipp Zabel | 5762559 | 2013-09-30 10:34:51 -0300 | [diff] [blame] | 432 | struct coda_dev *dev = ctx->dev; |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 433 | unsigned int max_w, max_h; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 434 | enum v4l2_field field; |
| 435 | |
| 436 | field = f->fmt.pix.field; |
| 437 | if (field == V4L2_FIELD_ANY) |
| 438 | field = V4L2_FIELD_NONE; |
| 439 | else if (V4L2_FIELD_NONE != field) |
| 440 | return -EINVAL; |
| 441 | |
| 442 | /* V4L2 specification suggests the driver corrects the format struct |
| 443 | * if any of the dimensions is unsupported */ |
| 444 | f->fmt.pix.field = field; |
| 445 | |
Philipp Zabel | 5762559 | 2013-09-30 10:34:51 -0300 | [diff] [blame] | 446 | coda_get_max_dimensions(dev, codec, &max_w, &max_h); |
| 447 | v4l_bound_align_image(&f->fmt.pix.width, MIN_W, max_w, W_ALIGN, |
| 448 | &f->fmt.pix.height, MIN_H, max_h, H_ALIGN, |
| 449 | S_ALIGN); |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 450 | |
Philipp Zabel | 5762559 | 2013-09-30 10:34:51 -0300 | [diff] [blame] | 451 | switch (f->fmt.pix.pixelformat) { |
Philipp Zabel | 6727d4f | 2015-07-16 13:19:39 -0300 | [diff] [blame] | 452 | case V4L2_PIX_FMT_NV12: |
Philipp Zabel | 5762559 | 2013-09-30 10:34:51 -0300 | [diff] [blame] | 453 | case V4L2_PIX_FMT_YUV420: |
| 454 | case V4L2_PIX_FMT_YVU420: |
Philipp Zabel | cb1d3a3 | 2014-10-02 14:08:31 -0300 | [diff] [blame] | 455 | /* |
| 456 | * Frame stride must be at least multiple of 8, |
| 457 | * but multiple of 16 for h.264 or JPEG 4:2:x |
| 458 | */ |
Philipp Zabel | 451dfe5 | 2014-07-11 06:36:39 -0300 | [diff] [blame] | 459 | f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16); |
Philipp Zabel | 47cf0c6 | 2013-05-23 10:42:54 -0300 | [diff] [blame] | 460 | f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * |
Philipp Zabel | 451d43a | 2012-07-26 09:18:27 -0300 | [diff] [blame] | 461 | f->fmt.pix.height * 3 / 2; |
Philipp Zabel | 5762559 | 2013-09-30 10:34:51 -0300 | [diff] [blame] | 462 | break; |
Philipp Zabel | 4de6931 | 2014-10-02 14:08:26 -0300 | [diff] [blame] | 463 | case V4L2_PIX_FMT_YUV422P: |
| 464 | f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16); |
| 465 | f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * |
| 466 | f->fmt.pix.height * 2; |
| 467 | break; |
Philipp Zabel | cb1d3a3 | 2014-10-02 14:08:31 -0300 | [diff] [blame] | 468 | case V4L2_PIX_FMT_JPEG: |
| 469 | f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG; |
| 470 | /* fallthrough */ |
Philipp Zabel | 5762559 | 2013-09-30 10:34:51 -0300 | [diff] [blame] | 471 | case V4L2_PIX_FMT_H264: |
| 472 | case V4L2_PIX_FMT_MPEG4: |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 473 | f->fmt.pix.bytesperline = 0; |
Philipp Zabel | 8e428d5 | 2015-01-23 13:51:29 -0300 | [diff] [blame] | 474 | f->fmt.pix.sizeimage = coda_estimate_sizeimage(ctx, |
| 475 | f->fmt.pix.sizeimage, |
| 476 | f->fmt.pix.width, |
| 477 | f->fmt.pix.height); |
Philipp Zabel | 5762559 | 2013-09-30 10:34:51 -0300 | [diff] [blame] | 478 | break; |
| 479 | default: |
| 480 | BUG(); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | return 0; |
| 484 | } |
| 485 | |
Philipp Zabel | 2e9e4f1 | 2013-09-30 10:34:50 -0300 | [diff] [blame] | 486 | static int coda_try_fmt_vid_cap(struct file *file, void *priv, |
| 487 | struct v4l2_format *f) |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 488 | { |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 489 | struct coda_ctx *ctx = fh_to_ctx(priv); |
Philipp Zabel | 2c11d1b | 2014-10-02 14:08:28 -0300 | [diff] [blame] | 490 | const struct coda_q_data *q_data_src; |
| 491 | const struct coda_codec *codec; |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 492 | struct vb2_queue *src_vq; |
| 493 | int ret; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 494 | |
Philipp Zabel | 2c11d1b | 2014-10-02 14:08:28 -0300 | [diff] [blame] | 495 | ret = coda_try_pixelformat(ctx, f); |
| 496 | if (ret < 0) |
| 497 | return ret; |
| 498 | |
| 499 | q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT); |
| 500 | |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 501 | /* |
Philipp Zabel | 2c11d1b | 2014-10-02 14:08:28 -0300 | [diff] [blame] | 502 | * If the source format is already fixed, only allow the same output |
| 503 | * resolution |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 504 | */ |
Philipp Zabel | 14604e3 | 2014-07-11 06:36:22 -0300 | [diff] [blame] | 505 | 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] | 506 | if (vb2_is_streaming(src_vq)) { |
Philipp Zabel | 91b5841e | 2014-07-18 07:22:41 -0300 | [diff] [blame] | 507 | f->fmt.pix.width = q_data_src->width; |
| 508 | f->fmt.pix.height = q_data_src->height; |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 509 | } |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 510 | |
| 511 | f->fmt.pix.colorspace = ctx->colorspace; |
| 512 | |
Philipp Zabel | 2c11d1b | 2014-10-02 14:08:28 -0300 | [diff] [blame] | 513 | q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT); |
| 514 | codec = coda_find_codec(ctx->dev, q_data_src->fourcc, |
| 515 | f->fmt.pix.pixelformat); |
| 516 | if (!codec) |
| 517 | return -EINVAL; |
| 518 | |
Philipp Zabel | 5762559 | 2013-09-30 10:34:51 -0300 | [diff] [blame] | 519 | ret = coda_try_fmt(ctx, codec, f); |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 520 | if (ret < 0) |
| 521 | return ret; |
| 522 | |
| 523 | /* The h.264 decoder only returns complete 16x16 macroblocks */ |
| 524 | if (codec && codec->src_fourcc == V4L2_PIX_FMT_H264) { |
Philipp Zabel | 1b0ed7b | 2014-07-11 06:36:37 -0300 | [diff] [blame] | 525 | f->fmt.pix.width = f->fmt.pix.width; |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 526 | f->fmt.pix.height = round_up(f->fmt.pix.height, 16); |
Philipp Zabel | 1b0ed7b | 2014-07-11 06:36:37 -0300 | [diff] [blame] | 527 | f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16); |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 528 | f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * |
| 529 | f->fmt.pix.height * 3 / 2; |
| 530 | } |
| 531 | |
| 532 | return 0; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 533 | } |
| 534 | |
Philipp Zabel | 2e9e4f1 | 2013-09-30 10:34:50 -0300 | [diff] [blame] | 535 | static int coda_try_fmt_vid_out(struct file *file, void *priv, |
| 536 | struct v4l2_format *f) |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 537 | { |
| 538 | struct coda_ctx *ctx = fh_to_ctx(priv); |
Philipp Zabel | 2c11d1b | 2014-10-02 14:08:28 -0300 | [diff] [blame] | 539 | struct coda_dev *dev = ctx->dev; |
| 540 | const struct coda_q_data *q_data_dst; |
| 541 | const struct coda_codec *codec; |
| 542 | int ret; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 543 | |
Philipp Zabel | 2c11d1b | 2014-10-02 14:08:28 -0300 | [diff] [blame] | 544 | ret = coda_try_pixelformat(ctx, f); |
| 545 | if (ret < 0) |
| 546 | return ret; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 547 | |
Philipp Zabel | 5f9826e | 2015-01-23 13:51:21 -0300 | [diff] [blame] | 548 | switch (f->fmt.pix.colorspace) { |
| 549 | case V4L2_COLORSPACE_REC709: |
| 550 | case V4L2_COLORSPACE_JPEG: |
| 551 | break; |
| 552 | default: |
Philipp Zabel | cb1d3a3 | 2014-10-02 14:08:31 -0300 | [diff] [blame] | 553 | if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_JPEG) |
| 554 | f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG; |
| 555 | else |
| 556 | f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 557 | } |
| 558 | |
Philipp Zabel | 2c11d1b | 2014-10-02 14:08:28 -0300 | [diff] [blame] | 559 | q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE); |
| 560 | codec = coda_find_codec(dev, f->fmt.pix.pixelformat, q_data_dst->fourcc); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 561 | |
| 562 | return coda_try_fmt(ctx, codec, f); |
| 563 | } |
| 564 | |
Philipp Zabel | 2e9e4f1 | 2013-09-30 10:34:50 -0300 | [diff] [blame] | 565 | 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] | 566 | { |
| 567 | struct coda_q_data *q_data; |
| 568 | struct vb2_queue *vq; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 569 | |
Philipp Zabel | 14604e3 | 2014-07-11 06:36:22 -0300 | [diff] [blame] | 570 | vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 571 | if (!vq) |
| 572 | return -EINVAL; |
| 573 | |
| 574 | q_data = get_q_data(ctx, f->type); |
| 575 | if (!q_data) |
| 576 | return -EINVAL; |
| 577 | |
| 578 | if (vb2_is_busy(vq)) { |
| 579 | v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__); |
| 580 | return -EBUSY; |
| 581 | } |
| 582 | |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 583 | q_data->fourcc = f->fmt.pix.pixelformat; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 584 | q_data->width = f->fmt.pix.width; |
| 585 | q_data->height = f->fmt.pix.height; |
Philipp Zabel | 2fd6a37 | 2014-07-11 06:36:36 -0300 | [diff] [blame] | 586 | q_data->bytesperline = f->fmt.pix.bytesperline; |
Philipp Zabel | 451d43a | 2012-07-26 09:18:27 -0300 | [diff] [blame] | 587 | q_data->sizeimage = f->fmt.pix.sizeimage; |
Philipp Zabel | 52c4167 | 2014-07-11 06:36:19 -0300 | [diff] [blame] | 588 | q_data->rect.left = 0; |
| 589 | q_data->rect.top = 0; |
| 590 | q_data->rect.width = f->fmt.pix.width; |
| 591 | q_data->rect.height = f->fmt.pix.height; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 592 | |
Philipp Zabel | a269e53 | 2015-07-16 13:19:38 -0300 | [diff] [blame] | 593 | switch (f->fmt.pix.pixelformat) { |
| 594 | case V4L2_PIX_FMT_NV12: |
| 595 | if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) { |
| 596 | ctx->tiled_map_type = GDI_TILED_FRAME_MB_RASTER_MAP; |
| 597 | if (!disable_tiling) |
| 598 | break; |
| 599 | } |
| 600 | /* else fall through */ |
| 601 | case V4L2_PIX_FMT_YUV420: |
| 602 | case V4L2_PIX_FMT_YVU420: |
| 603 | ctx->tiled_map_type = GDI_LINEAR_FRAME_MAP; |
| 604 | break; |
| 605 | default: |
| 606 | break; |
| 607 | } |
| 608 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 609 | v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, |
| 610 | "Setting format for type %d, wxh: %dx%d, fmt: %d\n", |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 611 | f->type, q_data->width, q_data->height, q_data->fourcc); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 612 | |
| 613 | return 0; |
| 614 | } |
| 615 | |
Philipp Zabel | 2e9e4f1 | 2013-09-30 10:34:50 -0300 | [diff] [blame] | 616 | static int coda_s_fmt_vid_cap(struct file *file, void *priv, |
| 617 | struct v4l2_format *f) |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 618 | { |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 619 | struct coda_ctx *ctx = fh_to_ctx(priv); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 620 | int ret; |
| 621 | |
Philipp Zabel | 2e9e4f1 | 2013-09-30 10:34:50 -0300 | [diff] [blame] | 622 | ret = coda_try_fmt_vid_cap(file, priv, f); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 623 | if (ret) |
| 624 | return ret; |
| 625 | |
Philipp Zabel | 2e9e4f1 | 2013-09-30 10:34:50 -0300 | [diff] [blame] | 626 | return coda_s_fmt(ctx, f); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 627 | } |
| 628 | |
Philipp Zabel | 2e9e4f1 | 2013-09-30 10:34:50 -0300 | [diff] [blame] | 629 | static int coda_s_fmt_vid_out(struct file *file, void *priv, |
| 630 | struct v4l2_format *f) |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 631 | { |
| 632 | struct coda_ctx *ctx = fh_to_ctx(priv); |
Philipp Zabel | f95a6ce | 2014-08-05 14:00:19 -0300 | [diff] [blame] | 633 | struct v4l2_format f_cap; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 634 | int ret; |
| 635 | |
Philipp Zabel | 2e9e4f1 | 2013-09-30 10:34:50 -0300 | [diff] [blame] | 636 | ret = coda_try_fmt_vid_out(file, priv, f); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 637 | if (ret) |
| 638 | return ret; |
| 639 | |
Philipp Zabel | 2e9e4f1 | 2013-09-30 10:34:50 -0300 | [diff] [blame] | 640 | ret = coda_s_fmt(ctx, f); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 641 | if (ret) |
Philipp Zabel | 2dc546d | 2014-08-05 14:00:18 -0300 | [diff] [blame] | 642 | return ret; |
| 643 | |
| 644 | ctx->colorspace = f->fmt.pix.colorspace; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 645 | |
Philipp Zabel | 1c2f6a9 | 2015-01-23 13:51:22 -0300 | [diff] [blame] | 646 | memset(&f_cap, 0, sizeof(f_cap)); |
Philipp Zabel | f95a6ce | 2014-08-05 14:00:19 -0300 | [diff] [blame] | 647 | f_cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 648 | coda_g_fmt(file, priv, &f_cap); |
| 649 | f_cap.fmt.pix.width = f->fmt.pix.width; |
| 650 | f_cap.fmt.pix.height = f->fmt.pix.height; |
| 651 | |
| 652 | ret = coda_try_fmt_vid_cap(file, priv, &f_cap); |
| 653 | if (ret) |
| 654 | return ret; |
| 655 | |
| 656 | return coda_s_fmt(ctx, &f_cap); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 657 | } |
| 658 | |
Philipp Zabel | 73751da | 2015-03-24 14:30:51 -0300 | [diff] [blame] | 659 | static int coda_reqbufs(struct file *file, void *priv, |
| 660 | struct v4l2_requestbuffers *rb) |
| 661 | { |
| 662 | struct coda_ctx *ctx = fh_to_ctx(priv); |
| 663 | int ret; |
| 664 | |
| 665 | ret = v4l2_m2m_reqbufs(file, ctx->fh.m2m_ctx, rb); |
| 666 | if (ret) |
| 667 | return ret; |
| 668 | |
| 669 | /* |
| 670 | * Allow to allocate instance specific per-context buffers, such as |
| 671 | * bitstream ringbuffer, slice buffer, work buffer, etc. if needed. |
| 672 | */ |
| 673 | if (rb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT && ctx->ops->reqbufs) |
| 674 | return ctx->ops->reqbufs(ctx, rb); |
| 675 | |
| 676 | return 0; |
| 677 | } |
| 678 | |
Philipp Zabel | 2e9e4f1 | 2013-09-30 10:34:50 -0300 | [diff] [blame] | 679 | static int coda_qbuf(struct file *file, void *priv, |
| 680 | struct v4l2_buffer *buf) |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 681 | { |
| 682 | struct coda_ctx *ctx = fh_to_ctx(priv); |
| 683 | |
Philipp Zabel | 14604e3 | 2014-07-11 06:36:22 -0300 | [diff] [blame] | 684 | return v4l2_m2m_qbuf(file, ctx->fh.m2m_ctx, buf); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 685 | } |
| 686 | |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 687 | static bool coda_buf_is_end_of_stream(struct coda_ctx *ctx, |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 688 | struct vb2_v4l2_buffer *buf) |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 689 | { |
| 690 | struct vb2_queue *src_vq; |
| 691 | |
Philipp Zabel | 14604e3 | 2014-07-11 06:36:22 -0300 | [diff] [blame] | 692 | 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] | 693 | |
| 694 | return ((ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) && |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 695 | (buf->sequence == (ctx->qsequence - 1))); |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 696 | } |
| 697 | |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 698 | void coda_m2m_buf_done(struct coda_ctx *ctx, struct vb2_v4l2_buffer *buf, |
Philipp Zabel | 9f2bfb3 | 2015-05-04 07:51:07 -0300 | [diff] [blame] | 699 | enum vb2_buffer_state state) |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 700 | { |
Philipp Zabel | 9f2bfb3 | 2015-05-04 07:51:07 -0300 | [diff] [blame] | 701 | const struct v4l2_event eos_event = { |
| 702 | .type = V4L2_EVENT_EOS |
| 703 | }; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 704 | |
Philipp Zabel | 9f2bfb3 | 2015-05-04 07:51:07 -0300 | [diff] [blame] | 705 | if (coda_buf_is_end_of_stream(ctx, buf)) { |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 706 | buf->flags |= V4L2_BUF_FLAG_LAST; |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 707 | |
| 708 | v4l2_event_queue_fh(&ctx->fh, &eos_event); |
| 709 | } |
| 710 | |
Philipp Zabel | 9f2bfb3 | 2015-05-04 07:51:07 -0300 | [diff] [blame] | 711 | v4l2_m2m_buf_done(buf, state); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 712 | } |
| 713 | |
Philipp Zabel | 52c4167 | 2014-07-11 06:36:19 -0300 | [diff] [blame] | 714 | static int coda_g_selection(struct file *file, void *fh, |
| 715 | struct v4l2_selection *s) |
| 716 | { |
| 717 | struct coda_ctx *ctx = fh_to_ctx(fh); |
| 718 | struct coda_q_data *q_data; |
| 719 | struct v4l2_rect r, *rsel; |
| 720 | |
| 721 | q_data = get_q_data(ctx, s->type); |
| 722 | if (!q_data) |
| 723 | return -EINVAL; |
| 724 | |
| 725 | r.left = 0; |
| 726 | r.top = 0; |
| 727 | r.width = q_data->width; |
| 728 | r.height = q_data->height; |
| 729 | rsel = &q_data->rect; |
| 730 | |
| 731 | switch (s->target) { |
| 732 | case V4L2_SEL_TGT_CROP_DEFAULT: |
| 733 | case V4L2_SEL_TGT_CROP_BOUNDS: |
| 734 | rsel = &r; |
| 735 | /* fallthrough */ |
| 736 | case V4L2_SEL_TGT_CROP: |
| 737 | if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) |
| 738 | return -EINVAL; |
| 739 | break; |
| 740 | case V4L2_SEL_TGT_COMPOSE_BOUNDS: |
| 741 | case V4L2_SEL_TGT_COMPOSE_PADDED: |
| 742 | rsel = &r; |
| 743 | /* fallthrough */ |
| 744 | case V4L2_SEL_TGT_COMPOSE: |
| 745 | case V4L2_SEL_TGT_COMPOSE_DEFAULT: |
| 746 | if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 747 | return -EINVAL; |
| 748 | break; |
| 749 | default: |
| 750 | return -EINVAL; |
| 751 | } |
| 752 | |
| 753 | s->r = *rsel; |
| 754 | |
| 755 | return 0; |
| 756 | } |
| 757 | |
Philipp Zabel | 64ba40a | 2013-09-30 10:34:52 -0300 | [diff] [blame] | 758 | static int coda_try_decoder_cmd(struct file *file, void *fh, |
| 759 | struct v4l2_decoder_cmd *dc) |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 760 | { |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 761 | if (dc->cmd != V4L2_DEC_CMD_STOP) |
| 762 | return -EINVAL; |
| 763 | |
Philipp Zabel | 64ba40a | 2013-09-30 10:34:52 -0300 | [diff] [blame] | 764 | if (dc->flags & V4L2_DEC_CMD_STOP_TO_BLACK) |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 765 | return -EINVAL; |
| 766 | |
Philipp Zabel | 64ba40a | 2013-09-30 10:34:52 -0300 | [diff] [blame] | 767 | if (!(dc->flags & V4L2_DEC_CMD_STOP_IMMEDIATELY) && (dc->stop.pts != 0)) |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 768 | return -EINVAL; |
| 769 | |
Philipp Zabel | 64ba40a | 2013-09-30 10:34:52 -0300 | [diff] [blame] | 770 | return 0; |
| 771 | } |
| 772 | |
| 773 | static int coda_decoder_cmd(struct file *file, void *fh, |
| 774 | struct v4l2_decoder_cmd *dc) |
| 775 | { |
| 776 | struct coda_ctx *ctx = fh_to_ctx(fh); |
| 777 | int ret; |
| 778 | |
| 779 | ret = coda_try_decoder_cmd(file, fh, dc); |
| 780 | if (ret < 0) |
| 781 | return ret; |
| 782 | |
| 783 | /* Ignore decoder stop command silently in encoder context */ |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 784 | if (ctx->inst_type != CODA_INST_DECODER) |
Philipp Zabel | 64ba40a | 2013-09-30 10:34:52 -0300 | [diff] [blame] | 785 | return 0; |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 786 | |
Philipp Zabel | 347bb7f | 2014-07-23 12:28:42 -0300 | [diff] [blame] | 787 | /* Set the stream-end flag on this context */ |
| 788 | coda_bit_stream_end_flag(ctx); |
Philipp Zabel | 84e2365 | 2014-07-11 06:36:34 -0300 | [diff] [blame] | 789 | ctx->hold = false; |
Michael Olbrich | f3497da | 2014-07-11 06:36:30 -0300 | [diff] [blame] | 790 | v4l2_m2m_try_schedule(ctx->fh.m2m_ctx); |
Philipp Zabel | 8954844 | 2014-07-11 06:36:17 -0300 | [diff] [blame] | 791 | |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 792 | return 0; |
| 793 | } |
| 794 | |
Philipp Zabel | cde29ef | 2015-07-16 13:13:24 -0300 | [diff] [blame] | 795 | static int coda_g_parm(struct file *file, void *fh, struct v4l2_streamparm *a) |
| 796 | { |
| 797 | struct coda_ctx *ctx = fh_to_ctx(fh); |
| 798 | struct v4l2_fract *tpf; |
| 799 | |
| 800 | if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) |
| 801 | return -EINVAL; |
| 802 | |
| 803 | a->parm.output.capability = V4L2_CAP_TIMEPERFRAME; |
| 804 | tpf = &a->parm.output.timeperframe; |
| 805 | tpf->denominator = ctx->params.framerate & CODA_FRATE_RES_MASK; |
| 806 | tpf->numerator = 1 + (ctx->params.framerate >> |
| 807 | CODA_FRATE_DIV_OFFSET); |
| 808 | |
| 809 | return 0; |
| 810 | } |
| 811 | |
| 812 | /* |
| 813 | * Approximate timeperframe v4l2_fract with values that can be written |
| 814 | * into the 16-bit CODA_FRATE_DIV and CODA_FRATE_RES fields. |
| 815 | */ |
| 816 | static void coda_approximate_timeperframe(struct v4l2_fract *timeperframe) |
| 817 | { |
| 818 | struct v4l2_fract s = *timeperframe; |
| 819 | struct v4l2_fract f0; |
| 820 | struct v4l2_fract f1 = { 1, 0 }; |
| 821 | struct v4l2_fract f2 = { 0, 1 }; |
| 822 | unsigned int i, div, s_denominator; |
| 823 | |
| 824 | /* Lower bound is 1/65535 */ |
| 825 | if (s.numerator == 0 || s.denominator / s.numerator > 65535) { |
| 826 | timeperframe->numerator = 1; |
| 827 | timeperframe->denominator = 65535; |
| 828 | return; |
| 829 | } |
| 830 | |
| 831 | /* Upper bound is 65536/1, map everything above to infinity */ |
| 832 | if (s.denominator == 0 || s.numerator / s.denominator > 65536) { |
| 833 | timeperframe->numerator = 1; |
| 834 | timeperframe->denominator = 0; |
| 835 | return; |
| 836 | } |
| 837 | |
| 838 | /* Reduce fraction to lowest terms */ |
| 839 | div = gcd(s.numerator, s.denominator); |
| 840 | if (div > 1) { |
| 841 | s.numerator /= div; |
| 842 | s.denominator /= div; |
| 843 | } |
| 844 | |
| 845 | if (s.numerator <= 65536 && s.denominator < 65536) { |
| 846 | *timeperframe = s; |
| 847 | return; |
| 848 | } |
| 849 | |
| 850 | /* Find successive convergents from continued fraction expansion */ |
| 851 | while (f2.numerator <= 65536 && f2.denominator < 65536) { |
| 852 | f0 = f1; |
| 853 | f1 = f2; |
| 854 | |
| 855 | /* Stop when f2 exactly equals timeperframe */ |
| 856 | if (s.numerator == 0) |
| 857 | break; |
| 858 | |
| 859 | i = s.denominator / s.numerator; |
| 860 | |
| 861 | f2.numerator = f0.numerator + i * f1.numerator; |
| 862 | f2.denominator = f0.denominator + i * f2.denominator; |
| 863 | |
| 864 | s_denominator = s.numerator; |
| 865 | s.numerator = s.denominator % s.numerator; |
| 866 | s.denominator = s_denominator; |
| 867 | } |
| 868 | |
| 869 | *timeperframe = f1; |
| 870 | } |
| 871 | |
| 872 | static uint32_t coda_timeperframe_to_frate(struct v4l2_fract *timeperframe) |
| 873 | { |
| 874 | return ((timeperframe->numerator - 1) << CODA_FRATE_DIV_OFFSET) | |
| 875 | timeperframe->denominator; |
| 876 | } |
| 877 | |
| 878 | static int coda_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a) |
| 879 | { |
| 880 | struct coda_ctx *ctx = fh_to_ctx(fh); |
| 881 | struct v4l2_fract *tpf; |
| 882 | |
| 883 | if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) |
| 884 | return -EINVAL; |
| 885 | |
| 886 | tpf = &a->parm.output.timeperframe; |
| 887 | coda_approximate_timeperframe(tpf); |
| 888 | ctx->params.framerate = coda_timeperframe_to_frate(tpf); |
| 889 | |
| 890 | return 0; |
| 891 | } |
| 892 | |
Philipp Zabel | 2e9e4f1 | 2013-09-30 10:34:50 -0300 | [diff] [blame] | 893 | static int coda_subscribe_event(struct v4l2_fh *fh, |
| 894 | const struct v4l2_event_subscription *sub) |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 895 | { |
| 896 | switch (sub->type) { |
| 897 | case V4L2_EVENT_EOS: |
| 898 | return v4l2_event_subscribe(fh, sub, 0, NULL); |
| 899 | default: |
| 900 | return v4l2_ctrl_subscribe_event(fh, sub); |
| 901 | } |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 902 | } |
| 903 | |
| 904 | static const struct v4l2_ioctl_ops coda_ioctl_ops = { |
Philipp Zabel | 2e9e4f1 | 2013-09-30 10:34:50 -0300 | [diff] [blame] | 905 | .vidioc_querycap = coda_querycap, |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 906 | |
Philipp Zabel | 22e244b | 2014-07-18 07:22:43 -0300 | [diff] [blame] | 907 | .vidioc_enum_fmt_vid_cap = coda_enum_fmt, |
Philipp Zabel | 2e9e4f1 | 2013-09-30 10:34:50 -0300 | [diff] [blame] | 908 | .vidioc_g_fmt_vid_cap = coda_g_fmt, |
| 909 | .vidioc_try_fmt_vid_cap = coda_try_fmt_vid_cap, |
| 910 | .vidioc_s_fmt_vid_cap = coda_s_fmt_vid_cap, |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 911 | |
Philipp Zabel | 22e244b | 2014-07-18 07:22:43 -0300 | [diff] [blame] | 912 | .vidioc_enum_fmt_vid_out = coda_enum_fmt, |
Philipp Zabel | 2e9e4f1 | 2013-09-30 10:34:50 -0300 | [diff] [blame] | 913 | .vidioc_g_fmt_vid_out = coda_g_fmt, |
| 914 | .vidioc_try_fmt_vid_out = coda_try_fmt_vid_out, |
| 915 | .vidioc_s_fmt_vid_out = coda_s_fmt_vid_out, |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 916 | |
Philipp Zabel | 73751da | 2015-03-24 14:30:51 -0300 | [diff] [blame] | 917 | .vidioc_reqbufs = coda_reqbufs, |
Philipp Zabel | 152ea1c | 2014-07-11 06:36:21 -0300 | [diff] [blame] | 918 | .vidioc_querybuf = v4l2_m2m_ioctl_querybuf, |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 919 | |
Philipp Zabel | 2e9e4f1 | 2013-09-30 10:34:50 -0300 | [diff] [blame] | 920 | .vidioc_qbuf = coda_qbuf, |
Philipp Zabel | 152ea1c | 2014-07-11 06:36:21 -0300 | [diff] [blame] | 921 | .vidioc_expbuf = v4l2_m2m_ioctl_expbuf, |
Philipp Zabel | 9f2bfb3 | 2015-05-04 07:51:07 -0300 | [diff] [blame] | 922 | .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf, |
Philipp Zabel | 152ea1c | 2014-07-11 06:36:21 -0300 | [diff] [blame] | 923 | .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs, |
Philipp Zabel | bb757d7 | 2015-12-02 14:58:52 -0200 | [diff] [blame^] | 924 | .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf, |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 925 | |
Philipp Zabel | 152ea1c | 2014-07-11 06:36:21 -0300 | [diff] [blame] | 926 | .vidioc_streamon = v4l2_m2m_ioctl_streamon, |
| 927 | .vidioc_streamoff = v4l2_m2m_ioctl_streamoff, |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 928 | |
Philipp Zabel | 52c4167 | 2014-07-11 06:36:19 -0300 | [diff] [blame] | 929 | .vidioc_g_selection = coda_g_selection, |
| 930 | |
Philipp Zabel | 64ba40a | 2013-09-30 10:34:52 -0300 | [diff] [blame] | 931 | .vidioc_try_decoder_cmd = coda_try_decoder_cmd, |
Philipp Zabel | 2e9e4f1 | 2013-09-30 10:34:50 -0300 | [diff] [blame] | 932 | .vidioc_decoder_cmd = coda_decoder_cmd, |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 933 | |
Philipp Zabel | cde29ef | 2015-07-16 13:13:24 -0300 | [diff] [blame] | 934 | .vidioc_g_parm = coda_g_parm, |
| 935 | .vidioc_s_parm = coda_s_parm, |
| 936 | |
Philipp Zabel | 2e9e4f1 | 2013-09-30 10:34:50 -0300 | [diff] [blame] | 937 | .vidioc_subscribe_event = coda_subscribe_event, |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 938 | .vidioc_unsubscribe_event = v4l2_event_unsubscribe, |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 939 | }; |
| 940 | |
| 941 | /* |
| 942 | * Mem-to-mem operations. |
| 943 | */ |
Philipp Zabel | 477c1cf | 2013-06-21 03:55:33 -0300 | [diff] [blame] | 944 | |
| 945 | static void coda_device_run(void *m2m_priv) |
| 946 | { |
| 947 | struct coda_ctx *ctx = m2m_priv; |
| 948 | struct coda_dev *dev = ctx->dev; |
Philipp Zabel | 32b6f20 | 2014-07-11 06:36:20 -0300 | [diff] [blame] | 949 | |
| 950 | queue_work(dev->workqueue, &ctx->pic_run_work); |
| 951 | } |
| 952 | |
Philipp Zabel | 32b6f20 | 2014-07-11 06:36:20 -0300 | [diff] [blame] | 953 | static void coda_pic_run_work(struct work_struct *work) |
| 954 | { |
| 955 | struct coda_ctx *ctx = container_of(work, struct coda_ctx, pic_run_work); |
| 956 | struct coda_dev *dev = ctx->dev; |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 957 | int ret; |
| 958 | |
| 959 | mutex_lock(&ctx->buffer_mutex); |
Philipp Zabel | 477c1cf | 2013-06-21 03:55:33 -0300 | [diff] [blame] | 960 | mutex_lock(&dev->coda_mutex); |
| 961 | |
Philipp Zabel | a1192a1 | 2014-07-23 12:28:40 -0300 | [diff] [blame] | 962 | ret = ctx->ops->prepare_run(ctx); |
| 963 | if (ret < 0 && ctx->inst_type == CODA_INST_DECODER) { |
| 964 | mutex_unlock(&dev->coda_mutex); |
| 965 | mutex_unlock(&ctx->buffer_mutex); |
| 966 | /* job_finish scheduled by prepare_decode */ |
| 967 | return; |
Philipp Zabel | 1043667 | 2012-07-02 09:03:55 -0300 | [diff] [blame] | 968 | } |
| 969 | |
Philipp Zabel | f23797b | 2014-08-06 08:02:23 -0300 | [diff] [blame] | 970 | if (!wait_for_completion_timeout(&ctx->completion, |
| 971 | msecs_to_jiffies(1000))) { |
Philipp Zabel | 32b6f20 | 2014-07-11 06:36:20 -0300 | [diff] [blame] | 972 | dev_err(&dev->plat_dev->dev, "CODA PIC_RUN timeout\n"); |
Philipp Zabel | 84e2365 | 2014-07-11 06:36:34 -0300 | [diff] [blame] | 973 | |
| 974 | ctx->hold = true; |
Philipp Zabel | 8f45284 | 2014-07-11 06:36:35 -0300 | [diff] [blame] | 975 | |
| 976 | coda_hw_reset(ctx); |
Philipp Zabel | 32b6f20 | 2014-07-11 06:36:20 -0300 | [diff] [blame] | 977 | } else if (!ctx->aborting) { |
Philipp Zabel | a1192a1 | 2014-07-23 12:28:40 -0300 | [diff] [blame] | 978 | ctx->ops->finish_run(ctx); |
Philipp Zabel | 32b6f20 | 2014-07-11 06:36:20 -0300 | [diff] [blame] | 979 | } |
| 980 | |
Philipp Zabel | 747d764 | 2015-01-23 13:51:31 -0300 | [diff] [blame] | 981 | if ((ctx->aborting || (!ctx->streamon_cap && !ctx->streamon_out)) && |
| 982 | ctx->ops->seq_end_work) |
Philipp Zabel | 32b6f20 | 2014-07-11 06:36:20 -0300 | [diff] [blame] | 983 | queue_work(dev->workqueue, &ctx->seq_end_work); |
| 984 | |
| 985 | mutex_unlock(&dev->coda_mutex); |
| 986 | mutex_unlock(&ctx->buffer_mutex); |
| 987 | |
Philipp Zabel | 14604e3 | 2014-07-11 06:36:22 -0300 | [diff] [blame] | 988 | v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 989 | } |
| 990 | |
| 991 | static int coda_job_ready(void *m2m_priv) |
| 992 | { |
| 993 | struct coda_ctx *ctx = m2m_priv; |
Philipp Zabel | 2cf251c | 2015-07-09 07:10:20 -0300 | [diff] [blame] | 994 | int src_bufs = v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 995 | |
| 996 | /* |
| 997 | * For both 'P' and 'key' frame cases 1 picture |
Philipp Zabel | 9082a7c | 2013-06-21 03:55:31 -0300 | [diff] [blame] | 998 | * and 1 frame are needed. In the decoder case, |
| 999 | * the compressed frame can be in the bitstream. |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1000 | */ |
Philipp Zabel | 2cf251c | 2015-07-09 07:10:20 -0300 | [diff] [blame] | 1001 | if (!src_bufs && ctx->inst_type != CODA_INST_DECODER) { |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1002 | v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, |
| 1003 | "not ready: not enough video buffers.\n"); |
| 1004 | return 0; |
| 1005 | } |
| 1006 | |
Philipp Zabel | 14604e3 | 2014-07-11 06:36:22 -0300 | [diff] [blame] | 1007 | if (!v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx)) { |
Philipp Zabel | 9082a7c | 2013-06-21 03:55:31 -0300 | [diff] [blame] | 1008 | v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, |
| 1009 | "not ready: not enough video capture buffers.\n"); |
| 1010 | return 0; |
| 1011 | } |
| 1012 | |
Philipp Zabel | a22496c | 2015-01-23 13:51:33 -0300 | [diff] [blame] | 1013 | if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit) { |
Philipp Zabel | 47f3fa6 | 2015-07-09 07:10:21 -0300 | [diff] [blame] | 1014 | bool stream_end = ctx->bit_stream_param & |
| 1015 | CODA_BIT_STREAM_END_FLAG; |
| 1016 | int num_metas = ctx->num_metas; |
Philipp Zabel | f77fd8a | 2015-01-23 13:51:20 -0300 | [diff] [blame] | 1017 | |
Philipp Zabel | 2cf251c | 2015-07-09 07:10:20 -0300 | [diff] [blame] | 1018 | if (ctx->hold && !src_bufs) { |
Philipp Zabel | f77fd8a | 2015-01-23 13:51:20 -0300 | [diff] [blame] | 1019 | v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, |
| 1020 | "%d: not ready: on hold for more buffers.\n", |
| 1021 | ctx->idx); |
| 1022 | return 0; |
| 1023 | } |
| 1024 | |
Philipp Zabel | f77fd8a | 2015-01-23 13:51:20 -0300 | [diff] [blame] | 1025 | if (!stream_end && (num_metas + src_bufs) < 2) { |
| 1026 | v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, |
| 1027 | "%d: not ready: need 2 buffers available (%d, %d)\n", |
| 1028 | ctx->idx, num_metas, src_bufs); |
| 1029 | return 0; |
| 1030 | } |
| 1031 | |
| 1032 | |
Philipp Zabel | 2cf251c | 2015-07-09 07:10:20 -0300 | [diff] [blame] | 1033 | if (!src_bufs && !stream_end && |
| 1034 | (coda_get_bitstream_payload(ctx) < 512)) { |
Philipp Zabel | f77fd8a | 2015-01-23 13:51:20 -0300 | [diff] [blame] | 1035 | v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, |
| 1036 | "%d: not ready: not enough bitstream data (%d).\n", |
| 1037 | ctx->idx, coda_get_bitstream_payload(ctx)); |
| 1038 | return 0; |
| 1039 | } |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 1040 | } |
| 1041 | |
Philipp Zabel | 3e74826 | 2013-05-23 10:43:01 -0300 | [diff] [blame] | 1042 | if (ctx->aborting) { |
| 1043 | v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, |
| 1044 | "not ready: aborting\n"); |
| 1045 | return 0; |
| 1046 | } |
| 1047 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1048 | v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, |
| 1049 | "job ready\n"); |
Philipp Zabel | 47f3fa6 | 2015-07-09 07:10:21 -0300 | [diff] [blame] | 1050 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1051 | return 1; |
| 1052 | } |
| 1053 | |
| 1054 | static void coda_job_abort(void *priv) |
| 1055 | { |
| 1056 | struct coda_ctx *ctx = priv; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1057 | |
| 1058 | ctx->aborting = 1; |
| 1059 | |
| 1060 | v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, |
| 1061 | "Aborting task\n"); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1062 | } |
| 1063 | |
| 1064 | static void coda_lock(void *m2m_priv) |
| 1065 | { |
| 1066 | struct coda_ctx *ctx = m2m_priv; |
| 1067 | struct coda_dev *pcdev = ctx->dev; |
Philipp Zabel | f23797b | 2014-08-06 08:02:23 -0300 | [diff] [blame] | 1068 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1069 | mutex_lock(&pcdev->dev_mutex); |
| 1070 | } |
| 1071 | |
| 1072 | static void coda_unlock(void *m2m_priv) |
| 1073 | { |
| 1074 | struct coda_ctx *ctx = m2m_priv; |
| 1075 | struct coda_dev *pcdev = ctx->dev; |
Philipp Zabel | f23797b | 2014-08-06 08:02:23 -0300 | [diff] [blame] | 1076 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1077 | mutex_unlock(&pcdev->dev_mutex); |
| 1078 | } |
| 1079 | |
Philipp Zabel | 814c376 | 2014-07-18 07:22:45 -0300 | [diff] [blame] | 1080 | static const struct v4l2_m2m_ops coda_m2m_ops = { |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1081 | .device_run = coda_device_run, |
| 1082 | .job_ready = coda_job_ready, |
| 1083 | .job_abort = coda_job_abort, |
| 1084 | .lock = coda_lock, |
| 1085 | .unlock = coda_unlock, |
| 1086 | }; |
| 1087 | |
| 1088 | static void set_default_params(struct coda_ctx *ctx) |
| 1089 | { |
Philipp Zabel | 8e428d5 | 2015-01-23 13:51:29 -0300 | [diff] [blame] | 1090 | unsigned int max_w, max_h, usize, csize; |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 1091 | |
Philipp Zabel | 2c11d1b | 2014-10-02 14:08:28 -0300 | [diff] [blame] | 1092 | ctx->codec = coda_find_codec(ctx->dev, ctx->cvd->src_formats[0], |
| 1093 | ctx->cvd->dst_formats[0]); |
Philipp Zabel | d4c6a41 | 2014-10-02 14:08:35 -0300 | [diff] [blame] | 1094 | max_w = min(ctx->codec->max_w, 1920U); |
| 1095 | max_h = min(ctx->codec->max_h, 1088U); |
Philipp Zabel | 8e428d5 | 2015-01-23 13:51:29 -0300 | [diff] [blame] | 1096 | usize = max_w * max_h * 3 / 2; |
| 1097 | csize = coda_estimate_sizeimage(ctx, usize, max_w, max_h); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1098 | |
Philipp Zabel | 121cacf | 2014-07-18 07:22:42 -0300 | [diff] [blame] | 1099 | ctx->params.codec_mode = ctx->codec->mode; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1100 | ctx->colorspace = V4L2_COLORSPACE_REC709; |
| 1101 | ctx->params.framerate = 30; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1102 | |
| 1103 | /* Default formats for output and input queues */ |
Philipp Zabel | 6727d4f | 2015-07-16 13:19:39 -0300 | [diff] [blame] | 1104 | ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->cvd->src_formats[0]; |
| 1105 | ctx->q_data[V4L2_M2M_DST].fourcc = ctx->cvd->dst_formats[0]; |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 1106 | ctx->q_data[V4L2_M2M_SRC].width = max_w; |
| 1107 | ctx->q_data[V4L2_M2M_SRC].height = max_h; |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 1108 | ctx->q_data[V4L2_M2M_DST].width = max_w; |
| 1109 | ctx->q_data[V4L2_M2M_DST].height = max_h; |
Philipp Zabel | 121cacf | 2014-07-18 07:22:42 -0300 | [diff] [blame] | 1110 | if (ctx->codec->src_fourcc == V4L2_PIX_FMT_YUV420) { |
| 1111 | ctx->q_data[V4L2_M2M_SRC].bytesperline = max_w; |
Philipp Zabel | 8e428d5 | 2015-01-23 13:51:29 -0300 | [diff] [blame] | 1112 | ctx->q_data[V4L2_M2M_SRC].sizeimage = usize; |
Philipp Zabel | 121cacf | 2014-07-18 07:22:42 -0300 | [diff] [blame] | 1113 | ctx->q_data[V4L2_M2M_DST].bytesperline = 0; |
Philipp Zabel | 8e428d5 | 2015-01-23 13:51:29 -0300 | [diff] [blame] | 1114 | ctx->q_data[V4L2_M2M_DST].sizeimage = csize; |
Philipp Zabel | 121cacf | 2014-07-18 07:22:42 -0300 | [diff] [blame] | 1115 | } else { |
| 1116 | ctx->q_data[V4L2_M2M_SRC].bytesperline = 0; |
Philipp Zabel | 8e428d5 | 2015-01-23 13:51:29 -0300 | [diff] [blame] | 1117 | ctx->q_data[V4L2_M2M_SRC].sizeimage = csize; |
Philipp Zabel | 121cacf | 2014-07-18 07:22:42 -0300 | [diff] [blame] | 1118 | ctx->q_data[V4L2_M2M_DST].bytesperline = max_w; |
Philipp Zabel | 8e428d5 | 2015-01-23 13:51:29 -0300 | [diff] [blame] | 1119 | ctx->q_data[V4L2_M2M_DST].sizeimage = usize; |
Philipp Zabel | 121cacf | 2014-07-18 07:22:42 -0300 | [diff] [blame] | 1120 | } |
Philipp Zabel | 52c4167 | 2014-07-11 06:36:19 -0300 | [diff] [blame] | 1121 | ctx->q_data[V4L2_M2M_SRC].rect.width = max_w; |
| 1122 | ctx->q_data[V4L2_M2M_SRC].rect.height = max_h; |
| 1123 | ctx->q_data[V4L2_M2M_DST].rect.width = max_w; |
| 1124 | ctx->q_data[V4L2_M2M_DST].rect.height = max_h; |
Philipp Zabel | 8954844 | 2014-07-11 06:36:17 -0300 | [diff] [blame] | 1125 | |
Philipp Zabel | a269e53 | 2015-07-16 13:19:38 -0300 | [diff] [blame] | 1126 | /* |
| 1127 | * Since the RBC2AXI logic only supports a single chroma plane, |
| 1128 | * macroblock tiling only works for to NV12 pixel format. |
| 1129 | */ |
| 1130 | ctx->tiled_map_type = GDI_LINEAR_FRAME_MAP; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1131 | } |
| 1132 | |
| 1133 | /* |
| 1134 | * Queue operations |
| 1135 | */ |
Hans Verkuil | df9ecb0 | 2015-10-28 00:50:37 -0200 | [diff] [blame] | 1136 | static int coda_queue_setup(struct vb2_queue *vq, |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1137 | unsigned int *nbuffers, unsigned int *nplanes, |
| 1138 | unsigned int sizes[], void *alloc_ctxs[]) |
| 1139 | { |
| 1140 | struct coda_ctx *ctx = vb2_get_drv_priv(vq); |
Philipp Zabel | e34db06 | 2012-08-29 08:22:00 -0300 | [diff] [blame] | 1141 | struct coda_q_data *q_data; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1142 | unsigned int size; |
| 1143 | |
Philipp Zabel | e34db06 | 2012-08-29 08:22:00 -0300 | [diff] [blame] | 1144 | q_data = get_q_data(ctx, vq->type); |
| 1145 | size = q_data->sizeimage; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1146 | |
| 1147 | *nplanes = 1; |
| 1148 | sizes[0] = size; |
| 1149 | |
Philipp Zabel | bb04aa6 | 2015-01-23 13:51:30 -0300 | [diff] [blame] | 1150 | /* Set to vb2-dma-contig allocator context, ignored by vb2-vmalloc */ |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1151 | alloc_ctxs[0] = ctx->dev->alloc_ctx; |
| 1152 | |
| 1153 | v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, |
| 1154 | "get %d buffer(s) of size %d each.\n", *nbuffers, size); |
| 1155 | |
| 1156 | return 0; |
| 1157 | } |
| 1158 | |
| 1159 | static int coda_buf_prepare(struct vb2_buffer *vb) |
| 1160 | { |
| 1161 | struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); |
| 1162 | struct coda_q_data *q_data; |
| 1163 | |
| 1164 | q_data = get_q_data(ctx, vb->vb2_queue->type); |
| 1165 | |
| 1166 | if (vb2_plane_size(vb, 0) < q_data->sizeimage) { |
| 1167 | v4l2_warn(&ctx->dev->v4l2_dev, |
| 1168 | "%s data will not fit into plane (%lu < %lu)\n", |
| 1169 | __func__, vb2_plane_size(vb, 0), |
| 1170 | (long)q_data->sizeimage); |
| 1171 | return -EINVAL; |
| 1172 | } |
| 1173 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1174 | return 0; |
| 1175 | } |
| 1176 | |
| 1177 | static void coda_buf_queue(struct vb2_buffer *vb) |
| 1178 | { |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 1179 | struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1180 | struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); |
Philipp Zabel | cb06b70 | 2015-01-23 13:51:35 -0300 | [diff] [blame] | 1181 | struct vb2_queue *vq = vb->vb2_queue; |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 1182 | struct coda_q_data *q_data; |
| 1183 | |
| 1184 | q_data = get_q_data(ctx, vb->vb2_queue->type); |
| 1185 | |
| 1186 | /* |
| 1187 | * In the decoder case, immediately try to copy the buffer into the |
| 1188 | * bitstream ringbuffer and mark it as ready to be dequeued. |
| 1189 | */ |
Philipp Zabel | cb06b70 | 2015-01-23 13:51:35 -0300 | [diff] [blame] | 1190 | if (ctx->bitstream.size && vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) { |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 1191 | /* |
Jonathan McCrohan | 39c1cb2 | 2013-10-20 21:34:01 -0300 | [diff] [blame] | 1192 | * For backwards compatibility, queuing an empty buffer marks |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 1193 | * the stream end |
| 1194 | */ |
Philipp Zabel | 347bb7f | 2014-07-23 12:28:42 -0300 | [diff] [blame] | 1195 | if (vb2_get_plane_payload(vb, 0) == 0) |
| 1196 | coda_bit_stream_end_flag(ctx); |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 1197 | mutex_lock(&ctx->bitstream_mutex); |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 1198 | v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf); |
Michael Olbrich | eabed93 | 2014-07-18 07:22:40 -0300 | [diff] [blame] | 1199 | if (vb2_is_streaming(vb->vb2_queue)) |
Philipp Zabel | 582d8872 | 2015-03-24 14:30:57 -0300 | [diff] [blame] | 1200 | coda_fill_bitstream(ctx, true); |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 1201 | mutex_unlock(&ctx->bitstream_mutex); |
| 1202 | } else { |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 1203 | v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf); |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 1204 | } |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1205 | } |
| 1206 | |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 1207 | int coda_alloc_aux_buf(struct coda_dev *dev, struct coda_aux_buf *buf, |
| 1208 | size_t size, const char *name, struct dentry *parent) |
Philipp Zabel | 5677e3b | 2013-06-21 03:55:30 -0300 | [diff] [blame] | 1209 | { |
| 1210 | buf->vaddr = dma_alloc_coherent(&dev->plat_dev->dev, size, &buf->paddr, |
| 1211 | GFP_KERNEL); |
Philipp Zabel | 68fc31c | 2014-08-05 14:00:16 -0300 | [diff] [blame] | 1212 | if (!buf->vaddr) { |
| 1213 | v4l2_err(&dev->v4l2_dev, |
| 1214 | "Failed to allocate %s buffer of size %u\n", |
| 1215 | name, size); |
Philipp Zabel | 5677e3b | 2013-06-21 03:55:30 -0300 | [diff] [blame] | 1216 | return -ENOMEM; |
Philipp Zabel | 68fc31c | 2014-08-05 14:00:16 -0300 | [diff] [blame] | 1217 | } |
Philipp Zabel | 5677e3b | 2013-06-21 03:55:30 -0300 | [diff] [blame] | 1218 | |
| 1219 | buf->size = size; |
| 1220 | |
Philipp Zabel | f61c0b1 | 2014-07-11 06:36:40 -0300 | [diff] [blame] | 1221 | if (name && parent) { |
| 1222 | buf->blob.data = buf->vaddr; |
| 1223 | buf->blob.size = size; |
Philipp Zabel | f23797b | 2014-08-06 08:02:23 -0300 | [diff] [blame] | 1224 | buf->dentry = debugfs_create_blob(name, 0644, parent, |
| 1225 | &buf->blob); |
Philipp Zabel | f61c0b1 | 2014-07-11 06:36:40 -0300 | [diff] [blame] | 1226 | if (!buf->dentry) |
| 1227 | dev_warn(&dev->plat_dev->dev, |
| 1228 | "failed to create debugfs entry %s\n", name); |
| 1229 | } |
| 1230 | |
Philipp Zabel | 5677e3b | 2013-06-21 03:55:30 -0300 | [diff] [blame] | 1231 | return 0; |
| 1232 | } |
| 1233 | |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 1234 | void coda_free_aux_buf(struct coda_dev *dev, |
| 1235 | struct coda_aux_buf *buf) |
Philipp Zabel | 5677e3b | 2013-06-21 03:55:30 -0300 | [diff] [blame] | 1236 | { |
| 1237 | if (buf->vaddr) { |
| 1238 | dma_free_coherent(&dev->plat_dev->dev, buf->size, |
| 1239 | buf->vaddr, buf->paddr); |
| 1240 | buf->vaddr = NULL; |
| 1241 | buf->size = 0; |
Peter Seiderer | d446ec8 | 2015-03-24 14:30:48 -0300 | [diff] [blame] | 1242 | debugfs_remove(buf->dentry); |
| 1243 | buf->dentry = NULL; |
Philipp Zabel | 5677e3b | 2013-06-21 03:55:30 -0300 | [diff] [blame] | 1244 | } |
| 1245 | } |
| 1246 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1247 | static int coda_start_streaming(struct vb2_queue *q, unsigned int count) |
| 1248 | { |
| 1249 | struct coda_ctx *ctx = vb2_get_drv_priv(q); |
| 1250 | struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1251 | struct coda_q_data *q_data_src, *q_data_dst; |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 1252 | struct vb2_v4l2_buffer *buf; |
Philipp Zabel | d35167a | 2013-05-23 10:42:59 -0300 | [diff] [blame] | 1253 | int ret = 0; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1254 | |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 1255 | q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT); |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 1256 | if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) { |
Philipp Zabel | 58bc7ed | 2015-07-09 07:10:14 -0300 | [diff] [blame] | 1257 | if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit) { |
Philipp Zabel | 582d8872 | 2015-03-24 14:30:57 -0300 | [diff] [blame] | 1258 | /* copy the buffers that were queued before streamon */ |
Michael Olbrich | eabed93 | 2014-07-18 07:22:40 -0300 | [diff] [blame] | 1259 | mutex_lock(&ctx->bitstream_mutex); |
Philipp Zabel | 582d8872 | 2015-03-24 14:30:57 -0300 | [diff] [blame] | 1260 | coda_fill_bitstream(ctx, false); |
Michael Olbrich | eabed93 | 2014-07-18 07:22:40 -0300 | [diff] [blame] | 1261 | mutex_unlock(&ctx->bitstream_mutex); |
| 1262 | |
Philipp Zabel | b906352 | 2014-08-05 14:00:10 -0300 | [diff] [blame] | 1263 | if (coda_get_bitstream_payload(ctx) < 512) { |
| 1264 | ret = -EINVAL; |
| 1265 | goto err; |
| 1266 | } |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 1267 | } else { |
Philipp Zabel | b906352 | 2014-08-05 14:00:10 -0300 | [diff] [blame] | 1268 | if (count < 1) { |
| 1269 | ret = -EINVAL; |
| 1270 | goto err; |
| 1271 | } |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 1272 | } |
| 1273 | |
| 1274 | ctx->streamon_out = 1; |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 1275 | } else { |
Philipp Zabel | b906352 | 2014-08-05 14:00:10 -0300 | [diff] [blame] | 1276 | if (count < 1) { |
| 1277 | ret = -EINVAL; |
| 1278 | goto err; |
| 1279 | } |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 1280 | |
| 1281 | ctx->streamon_cap = 1; |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 1282 | } |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1283 | |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 1284 | /* Don't start the coda unless both queues are on */ |
| 1285 | if (!(ctx->streamon_out & ctx->streamon_cap)) |
| 1286 | return 0; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1287 | |
Philipp Zabel | 23b6ee5 | 2015-03-24 14:30:55 -0300 | [diff] [blame] | 1288 | q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE); |
| 1289 | if ((q_data_src->width != q_data_dst->width && |
| 1290 | round_up(q_data_src->width, 16) != q_data_dst->width) || |
| 1291 | (q_data_src->height != q_data_dst->height && |
| 1292 | round_up(q_data_src->height, 16) != q_data_dst->height)) { |
| 1293 | v4l2_err(v4l2_dev, "can't convert %dx%d to %dx%d\n", |
| 1294 | q_data_src->width, q_data_src->height, |
| 1295 | q_data_dst->width, q_data_dst->height); |
| 1296 | ret = -EINVAL; |
| 1297 | goto err; |
| 1298 | } |
| 1299 | |
Philipp Zabel | cb1d3a3 | 2014-10-02 14:08:31 -0300 | [diff] [blame] | 1300 | /* Allow BIT decoder device_run with no new buffers queued */ |
Philipp Zabel | a22496c | 2015-01-23 13:51:33 -0300 | [diff] [blame] | 1301 | if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit) |
Philipp Zabel | 14604e3 | 2014-07-11 06:36:22 -0300 | [diff] [blame] | 1302 | v4l2_m2m_set_src_buffered(ctx->fh.m2m_ctx, true); |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 1303 | |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 1304 | ctx->gopcounter = ctx->params.gop_size - 1; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1305 | |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 1306 | ctx->codec = coda_find_codec(ctx->dev, q_data_src->fourcc, |
| 1307 | q_data_dst->fourcc); |
| 1308 | if (!ctx->codec) { |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1309 | v4l2_err(v4l2_dev, "couldn't tell instance type.\n"); |
Philipp Zabel | b906352 | 2014-08-05 14:00:10 -0300 | [diff] [blame] | 1310 | ret = -EINVAL; |
| 1311 | goto err; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1312 | } |
| 1313 | |
Philipp Zabel | cb1d3a3 | 2014-10-02 14:08:31 -0300 | [diff] [blame] | 1314 | if (q_data_dst->fourcc == V4L2_PIX_FMT_JPEG) |
| 1315 | ctx->params.gop_size = 1; |
| 1316 | ctx->gopcounter = ctx->params.gop_size - 1; |
| 1317 | |
Philipp Zabel | a1192a1 | 2014-07-23 12:28:40 -0300 | [diff] [blame] | 1318 | ret = ctx->ops->start_streaming(ctx); |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 1319 | if (ctx->inst_type == CODA_INST_DECODER) { |
Philipp Zabel | 8954844 | 2014-07-11 06:36:17 -0300 | [diff] [blame] | 1320 | if (ret == -EAGAIN) |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 1321 | return 0; |
Philipp Zabel | 8954844 | 2014-07-11 06:36:17 -0300 | [diff] [blame] | 1322 | else if (ret < 0) |
Philipp Zabel | b906352 | 2014-08-05 14:00:10 -0300 | [diff] [blame] | 1323 | goto err; |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 1324 | } |
| 1325 | |
Philipp Zabel | 8954844 | 2014-07-11 06:36:17 -0300 | [diff] [blame] | 1326 | return ret; |
Philipp Zabel | b906352 | 2014-08-05 14:00:10 -0300 | [diff] [blame] | 1327 | |
| 1328 | err: |
| 1329 | if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) { |
| 1330 | while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx))) |
Philipp Zabel | b8b1b58 | 2014-10-21 13:25:52 -0300 | [diff] [blame] | 1331 | v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED); |
Philipp Zabel | b906352 | 2014-08-05 14:00:10 -0300 | [diff] [blame] | 1332 | } else { |
| 1333 | while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx))) |
Philipp Zabel | b8b1b58 | 2014-10-21 13:25:52 -0300 | [diff] [blame] | 1334 | v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED); |
Philipp Zabel | b906352 | 2014-08-05 14:00:10 -0300 | [diff] [blame] | 1335 | } |
| 1336 | return ret; |
Philipp Zabel | 8954844 | 2014-07-11 06:36:17 -0300 | [diff] [blame] | 1337 | } |
| 1338 | |
Hans Verkuil | e37559b | 2014-04-17 02:47:21 -0300 | [diff] [blame] | 1339 | static void coda_stop_streaming(struct vb2_queue *q) |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1340 | { |
| 1341 | struct coda_ctx *ctx = vb2_get_drv_priv(q); |
Philipp Zabel | 2fb57f0 | 2012-07-25 09:22:07 -0300 | [diff] [blame] | 1342 | struct coda_dev *dev = ctx->dev; |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 1343 | struct vb2_v4l2_buffer *buf; |
Philipp Zabel | 47f3fa6 | 2015-07-09 07:10:21 -0300 | [diff] [blame] | 1344 | unsigned long flags; |
Philipp Zabel | 5c76c2c | 2015-03-24 14:30:56 -0300 | [diff] [blame] | 1345 | bool stop; |
| 1346 | |
| 1347 | stop = ctx->streamon_out && ctx->streamon_cap; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1348 | |
| 1349 | if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) { |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 1350 | v4l2_dbg(1, coda_debug, &dev->v4l2_dev, |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1351 | "%s: output\n", __func__); |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 1352 | ctx->streamon_out = 0; |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 1353 | |
Philipp Zabel | 347bb7f | 2014-07-23 12:28:42 -0300 | [diff] [blame] | 1354 | coda_bit_stream_end_flag(ctx); |
Philipp Zabel | 4a31b52 | 2014-08-05 14:00:11 -0300 | [diff] [blame] | 1355 | |
Philipp Zabel | 6dd5ef5 | 2015-01-23 13:51:26 -0300 | [diff] [blame] | 1356 | ctx->qsequence = 0; |
Philipp Zabel | 4a31b52 | 2014-08-05 14:00:11 -0300 | [diff] [blame] | 1357 | |
| 1358 | while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx))) |
| 1359 | v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1360 | } else { |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 1361 | v4l2_dbg(1, coda_debug, &dev->v4l2_dev, |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1362 | "%s: capture\n", __func__); |
Philipp Zabel | b96904e | 2013-05-23 10:42:58 -0300 | [diff] [blame] | 1363 | ctx->streamon_cap = 0; |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 1364 | |
| 1365 | ctx->osequence = 0; |
Philipp Zabel | cb2c028 | 2014-07-11 06:36:33 -0300 | [diff] [blame] | 1366 | ctx->sequence_offset = 0; |
Philipp Zabel | 4a31b52 | 2014-08-05 14:00:11 -0300 | [diff] [blame] | 1367 | |
| 1368 | while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx))) |
| 1369 | v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1370 | } |
| 1371 | |
Philipp Zabel | 5c76c2c | 2015-03-24 14:30:56 -0300 | [diff] [blame] | 1372 | if (stop) { |
Philipp Zabel | 7cbb105 | 2014-10-02 14:08:32 -0300 | [diff] [blame] | 1373 | struct coda_buffer_meta *meta; |
Philipp Zabel | 846ced9 | 2014-07-11 06:36:31 -0300 | [diff] [blame] | 1374 | |
Philipp Zabel | f4706d6 | 2015-01-23 13:51:27 -0300 | [diff] [blame] | 1375 | if (ctx->ops->seq_end_work) { |
| 1376 | queue_work(dev->workqueue, &ctx->seq_end_work); |
| 1377 | flush_work(&ctx->seq_end_work); |
| 1378 | } |
Philipp Zabel | 47f3fa6 | 2015-07-09 07:10:21 -0300 | [diff] [blame] | 1379 | spin_lock_irqsave(&ctx->buffer_meta_lock, flags); |
Philipp Zabel | 7cbb105 | 2014-10-02 14:08:32 -0300 | [diff] [blame] | 1380 | while (!list_empty(&ctx->buffer_meta_list)) { |
| 1381 | meta = list_first_entry(&ctx->buffer_meta_list, |
| 1382 | struct coda_buffer_meta, list); |
| 1383 | list_del(&meta->list); |
| 1384 | kfree(meta); |
Philipp Zabel | 846ced9 | 2014-07-11 06:36:31 -0300 | [diff] [blame] | 1385 | } |
Philipp Zabel | 47f3fa6 | 2015-07-09 07:10:21 -0300 | [diff] [blame] | 1386 | ctx->num_metas = 0; |
| 1387 | spin_unlock_irqrestore(&ctx->buffer_meta_lock, flags); |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 1388 | kfifo_init(&ctx->bitstream_fifo, |
| 1389 | ctx->bitstream.vaddr, ctx->bitstream.size); |
| 1390 | ctx->runcounter = 0; |
Philipp Zabel | f157cf4 | 2014-09-29 09:53:42 -0300 | [diff] [blame] | 1391 | ctx->aborting = 0; |
Philipp Zabel | 62bed14 | 2012-07-25 10:40:39 -0300 | [diff] [blame] | 1392 | } |
Philipp Zabel | c1ae0b2 | 2015-07-09 07:10:17 -0300 | [diff] [blame] | 1393 | |
| 1394 | if (!ctx->streamon_out && !ctx->streamon_cap) |
| 1395 | ctx->bit_stream_param &= ~CODA_BIT_STREAM_END_FLAG; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1396 | } |
| 1397 | |
Philipp Zabel | 121cacf | 2014-07-18 07:22:42 -0300 | [diff] [blame] | 1398 | static const struct vb2_ops coda_qops = { |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1399 | .queue_setup = coda_queue_setup, |
| 1400 | .buf_prepare = coda_buf_prepare, |
| 1401 | .buf_queue = coda_buf_queue, |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1402 | .start_streaming = coda_start_streaming, |
| 1403 | .stop_streaming = coda_stop_streaming, |
Philipp Zabel | 152ea1c | 2014-07-11 06:36:21 -0300 | [diff] [blame] | 1404 | .wait_prepare = vb2_ops_wait_prepare, |
| 1405 | .wait_finish = vb2_ops_wait_finish, |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1406 | }; |
| 1407 | |
| 1408 | static int coda_s_ctrl(struct v4l2_ctrl *ctrl) |
| 1409 | { |
| 1410 | struct coda_ctx *ctx = |
| 1411 | container_of(ctrl->handler, struct coda_ctx, ctrls); |
| 1412 | |
| 1413 | v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, |
| 1414 | "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val); |
| 1415 | |
| 1416 | switch (ctrl->id) { |
Philipp Zabel | 8f35c7b | 2012-07-09 04:25:52 -0300 | [diff] [blame] | 1417 | case V4L2_CID_HFLIP: |
| 1418 | if (ctrl->val) |
| 1419 | ctx->params.rot_mode |= CODA_MIR_HOR; |
| 1420 | else |
| 1421 | ctx->params.rot_mode &= ~CODA_MIR_HOR; |
| 1422 | break; |
| 1423 | case V4L2_CID_VFLIP: |
| 1424 | if (ctrl->val) |
| 1425 | ctx->params.rot_mode |= CODA_MIR_VER; |
| 1426 | else |
| 1427 | ctx->params.rot_mode &= ~CODA_MIR_VER; |
| 1428 | break; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1429 | case V4L2_CID_MPEG_VIDEO_BITRATE: |
| 1430 | ctx->params.bitrate = ctrl->val / 1000; |
| 1431 | break; |
| 1432 | case V4L2_CID_MPEG_VIDEO_GOP_SIZE: |
| 1433 | ctx->params.gop_size = ctrl->val; |
| 1434 | break; |
| 1435 | case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP: |
| 1436 | ctx->params.h264_intra_qp = ctrl->val; |
| 1437 | break; |
| 1438 | case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP: |
| 1439 | ctx->params.h264_inter_qp = ctrl->val; |
| 1440 | break; |
Philipp Zabel | 1a5567e | 2014-07-11 06:36:26 -0300 | [diff] [blame] | 1441 | case V4L2_CID_MPEG_VIDEO_H264_MIN_QP: |
| 1442 | ctx->params.h264_min_qp = ctrl->val; |
| 1443 | break; |
| 1444 | case V4L2_CID_MPEG_VIDEO_H264_MAX_QP: |
| 1445 | ctx->params.h264_max_qp = ctrl->val; |
| 1446 | break; |
Philipp Zabel | de23b1d | 2014-07-11 06:36:27 -0300 | [diff] [blame] | 1447 | case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA: |
| 1448 | ctx->params.h264_deblk_alpha = ctrl->val; |
| 1449 | break; |
| 1450 | case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA: |
| 1451 | ctx->params.h264_deblk_beta = ctrl->val; |
| 1452 | break; |
| 1453 | case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE: |
| 1454 | ctx->params.h264_deblk_enabled = (ctrl->val == |
| 1455 | V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED); |
| 1456 | break; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1457 | case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP: |
| 1458 | ctx->params.mpeg4_intra_qp = ctrl->val; |
| 1459 | break; |
| 1460 | case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP: |
| 1461 | ctx->params.mpeg4_inter_qp = ctrl->val; |
| 1462 | break; |
| 1463 | case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE: |
| 1464 | ctx->params.slice_mode = ctrl->val; |
| 1465 | break; |
| 1466 | case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB: |
| 1467 | ctx->params.slice_max_mb = ctrl->val; |
| 1468 | break; |
Philipp Zabel | c566c78 | 2012-08-08 11:59:38 -0300 | [diff] [blame] | 1469 | case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES: |
| 1470 | ctx->params.slice_max_bits = ctrl->val * 8; |
| 1471 | break; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1472 | case V4L2_CID_MPEG_VIDEO_HEADER_MODE: |
| 1473 | break; |
Philipp Zabel | f38f79d | 2014-07-11 06:36:28 -0300 | [diff] [blame] | 1474 | case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB: |
| 1475 | ctx->params.intra_refresh = ctrl->val; |
| 1476 | break; |
Philipp Zabel | cb1d3a3 | 2014-10-02 14:08:31 -0300 | [diff] [blame] | 1477 | case V4L2_CID_JPEG_COMPRESSION_QUALITY: |
| 1478 | coda_set_jpeg_compression_quality(ctx, ctrl->val); |
| 1479 | break; |
| 1480 | case V4L2_CID_JPEG_RESTART_INTERVAL: |
| 1481 | ctx->params.jpeg_restart_interval = ctrl->val; |
| 1482 | break; |
Philipp Zabel | da2b3b3 | 2015-07-10 10:37:52 -0300 | [diff] [blame] | 1483 | case V4L2_CID_MPEG_VIDEO_VBV_DELAY: |
| 1484 | ctx->params.vbv_delay = ctrl->val; |
| 1485 | break; |
| 1486 | case V4L2_CID_MPEG_VIDEO_VBV_SIZE: |
| 1487 | ctx->params.vbv_size = min(ctrl->val * 8192, 0x7fffffff); |
| 1488 | break; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1489 | default: |
| 1490 | v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, |
| 1491 | "Invalid control, id=%d, val=%d\n", |
| 1492 | ctrl->id, ctrl->val); |
| 1493 | return -EINVAL; |
| 1494 | } |
| 1495 | |
| 1496 | return 0; |
| 1497 | } |
| 1498 | |
Philipp Zabel | 814c376 | 2014-07-18 07:22:45 -0300 | [diff] [blame] | 1499 | static const struct v4l2_ctrl_ops coda_ctrl_ops = { |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1500 | .s_ctrl = coda_s_ctrl, |
| 1501 | }; |
| 1502 | |
Philipp Zabel | bfc732f | 2014-10-02 14:08:29 -0300 | [diff] [blame] | 1503 | static void coda_encode_ctrls(struct coda_ctx *ctx) |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1504 | { |
Philipp Zabel | 8f35c7b | 2012-07-09 04:25:52 -0300 | [diff] [blame] | 1505 | v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops, |
Philipp Zabel | 92b042e | 2015-03-18 08:15:36 -0300 | [diff] [blame] | 1506 | V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1000, 0); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1507 | v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops, |
| 1508 | V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 60, 1, 16); |
| 1509 | v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops, |
Philipp Zabel | 594a750 | 2014-07-11 06:36:14 -0300 | [diff] [blame] | 1510 | V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 0, 51, 1, 25); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1511 | v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops, |
Philipp Zabel | 594a750 | 2014-07-11 06:36:14 -0300 | [diff] [blame] | 1512 | V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 0, 51, 1, 25); |
Philipp Zabel | 1a5567e | 2014-07-11 06:36:26 -0300 | [diff] [blame] | 1513 | if (ctx->dev->devtype->product != CODA_960) { |
| 1514 | v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops, |
| 1515 | V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 0, 51, 1, 12); |
| 1516 | } |
| 1517 | v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops, |
| 1518 | V4L2_CID_MPEG_VIDEO_H264_MAX_QP, 0, 51, 1, 51); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1519 | v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops, |
Philipp Zabel | de23b1d | 2014-07-11 06:36:27 -0300 | [diff] [blame] | 1520 | V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA, 0, 15, 1, 0); |
| 1521 | v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops, |
| 1522 | V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA, 0, 15, 1, 0); |
| 1523 | v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops, |
| 1524 | V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE, |
| 1525 | V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED, 0x0, |
| 1526 | V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED); |
| 1527 | v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops, |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1528 | V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2); |
| 1529 | v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops, |
| 1530 | V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2); |
| 1531 | v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops, |
| 1532 | V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE, |
Philipp Zabel | c566c78 | 2012-08-08 11:59:38 -0300 | [diff] [blame] | 1533 | V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0, |
| 1534 | V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1535 | v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops, |
| 1536 | V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1); |
Philipp Zabel | c566c78 | 2012-08-08 11:59:38 -0300 | [diff] [blame] | 1537 | v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops, |
Philipp Zabel | f23797b | 2014-08-06 08:02:23 -0300 | [diff] [blame] | 1538 | V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1, |
| 1539 | 500); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1540 | v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops, |
| 1541 | V4L2_CID_MPEG_VIDEO_HEADER_MODE, |
| 1542 | V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME, |
| 1543 | (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE), |
| 1544 | V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME); |
Philipp Zabel | f38f79d | 2014-07-11 06:36:28 -0300 | [diff] [blame] | 1545 | v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops, |
Philipp Zabel | f23797b | 2014-08-06 08:02:23 -0300 | [diff] [blame] | 1546 | V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB, 0, |
| 1547 | 1920 * 1088 / 256, 1, 0); |
Philipp Zabel | da2b3b3 | 2015-07-10 10:37:52 -0300 | [diff] [blame] | 1548 | v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops, |
| 1549 | V4L2_CID_MPEG_VIDEO_VBV_DELAY, 0, 0x7fff, 1, 0); |
| 1550 | /* |
| 1551 | * The maximum VBV size value is 0x7fffffff bits, |
| 1552 | * one bit less than 262144 KiB |
| 1553 | */ |
| 1554 | v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops, |
| 1555 | V4L2_CID_MPEG_VIDEO_VBV_SIZE, 0, 262144, 1, 0); |
Philipp Zabel | bfc732f | 2014-10-02 14:08:29 -0300 | [diff] [blame] | 1556 | } |
| 1557 | |
Philipp Zabel | cb1d3a3 | 2014-10-02 14:08:31 -0300 | [diff] [blame] | 1558 | static void coda_jpeg_encode_ctrls(struct coda_ctx *ctx) |
| 1559 | { |
| 1560 | v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops, |
| 1561 | V4L2_CID_JPEG_COMPRESSION_QUALITY, 5, 100, 1, 50); |
| 1562 | v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops, |
| 1563 | V4L2_CID_JPEG_RESTART_INTERVAL, 0, 100, 1, 0); |
| 1564 | } |
| 1565 | |
Philipp Zabel | bfc732f | 2014-10-02 14:08:29 -0300 | [diff] [blame] | 1566 | static int coda_ctrls_setup(struct coda_ctx *ctx) |
| 1567 | { |
| 1568 | v4l2_ctrl_handler_init(&ctx->ctrls, 2); |
| 1569 | |
| 1570 | v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops, |
| 1571 | V4L2_CID_HFLIP, 0, 1, 1, 0); |
| 1572 | v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops, |
| 1573 | V4L2_CID_VFLIP, 0, 1, 1, 0); |
Philipp Zabel | cb1d3a3 | 2014-10-02 14:08:31 -0300 | [diff] [blame] | 1574 | if (ctx->inst_type == CODA_INST_ENCODER) { |
| 1575 | if (ctx->cvd->dst_formats[0] == V4L2_PIX_FMT_JPEG) |
| 1576 | coda_jpeg_encode_ctrls(ctx); |
| 1577 | else |
| 1578 | coda_encode_ctrls(ctx); |
| 1579 | } |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1580 | |
| 1581 | if (ctx->ctrls.error) { |
Philipp Zabel | f23797b | 2014-08-06 08:02:23 -0300 | [diff] [blame] | 1582 | v4l2_err(&ctx->dev->v4l2_dev, |
| 1583 | "control initialization error (%d)", |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1584 | ctx->ctrls.error); |
| 1585 | return -EINVAL; |
| 1586 | } |
| 1587 | |
| 1588 | return v4l2_ctrl_handler_setup(&ctx->ctrls); |
| 1589 | } |
| 1590 | |
Philipp Zabel | 121cacf | 2014-07-18 07:22:42 -0300 | [diff] [blame] | 1591 | static int coda_queue_init(struct coda_ctx *ctx, struct vb2_queue *vq) |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1592 | { |
Philipp Zabel | 121cacf | 2014-07-18 07:22:42 -0300 | [diff] [blame] | 1593 | vq->drv_priv = ctx; |
| 1594 | vq->ops = &coda_qops; |
| 1595 | vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer); |
| 1596 | vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; |
| 1597 | vq->lock = &ctx->dev->dev_mutex; |
Kamil Debski | e4af23d | 2015-02-23 09:26:18 -0300 | [diff] [blame] | 1598 | /* One way to indicate end-of-stream for coda is to set the |
| 1599 | * bytesused == 0. However by default videobuf2 handles bytesused |
| 1600 | * equal to 0 as a special case and changes its value to the size |
| 1601 | * of the buffer. Set the allow_zero_bytesused flag, so |
| 1602 | * that videobuf2 will keep the value of bytesused intact. |
| 1603 | */ |
| 1604 | vq->allow_zero_bytesused = 1; |
Philipp Zabel | 121cacf | 2014-07-18 07:22:42 -0300 | [diff] [blame] | 1605 | |
| 1606 | return vb2_queue_init(vq); |
| 1607 | } |
| 1608 | |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 1609 | int coda_encoder_queue_init(void *priv, struct vb2_queue *src_vq, |
| 1610 | struct vb2_queue *dst_vq) |
Philipp Zabel | 121cacf | 2014-07-18 07:22:42 -0300 | [diff] [blame] | 1611 | { |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1612 | int ret; |
| 1613 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1614 | src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT; |
Philipp Zabel | d29a8cf | 2014-07-18 07:22:38 -0300 | [diff] [blame] | 1615 | src_vq->io_modes = VB2_DMABUF | VB2_MMAP; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1616 | src_vq->mem_ops = &vb2_dma_contig_memops; |
| 1617 | |
Philipp Zabel | 121cacf | 2014-07-18 07:22:42 -0300 | [diff] [blame] | 1618 | ret = coda_queue_init(priv, src_vq); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1619 | if (ret) |
| 1620 | return ret; |
| 1621 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1622 | dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
Philipp Zabel | d29a8cf | 2014-07-18 07:22:38 -0300 | [diff] [blame] | 1623 | dst_vq->io_modes = VB2_DMABUF | VB2_MMAP; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1624 | dst_vq->mem_ops = &vb2_dma_contig_memops; |
| 1625 | |
Philipp Zabel | 121cacf | 2014-07-18 07:22:42 -0300 | [diff] [blame] | 1626 | return coda_queue_init(priv, dst_vq); |
| 1627 | } |
| 1628 | |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 1629 | int coda_decoder_queue_init(void *priv, struct vb2_queue *src_vq, |
| 1630 | struct vb2_queue *dst_vq) |
Philipp Zabel | 121cacf | 2014-07-18 07:22:42 -0300 | [diff] [blame] | 1631 | { |
| 1632 | int ret; |
| 1633 | |
| 1634 | src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT; |
Philipp Zabel | bb04aa6 | 2015-01-23 13:51:30 -0300 | [diff] [blame] | 1635 | src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR; |
| 1636 | src_vq->mem_ops = &vb2_vmalloc_memops; |
Philipp Zabel | 121cacf | 2014-07-18 07:22:42 -0300 | [diff] [blame] | 1637 | |
| 1638 | ret = coda_queue_init(priv, src_vq); |
| 1639 | if (ret) |
| 1640 | return ret; |
| 1641 | |
| 1642 | dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 1643 | dst_vq->io_modes = VB2_DMABUF | VB2_MMAP; |
| 1644 | dst_vq->mem_ops = &vb2_dma_contig_memops; |
| 1645 | |
| 1646 | return coda_queue_init(priv, dst_vq); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1647 | } |
| 1648 | |
Philipp Zabel | e11f3e6 | 2012-07-25 09:16:58 -0300 | [diff] [blame] | 1649 | static int coda_next_free_instance(struct coda_dev *dev) |
| 1650 | { |
Philipp Zabel | 0cddc7e | 2013-09-30 10:34:44 -0300 | [diff] [blame] | 1651 | int idx = ffz(dev->instance_mask); |
| 1652 | |
| 1653 | if ((idx < 0) || |
| 1654 | (dev->devtype->product == CODA_DX6 && idx > CODADX6_MAX_INSTANCES)) |
| 1655 | return -EBUSY; |
| 1656 | |
| 1657 | return idx; |
Philipp Zabel | e11f3e6 | 2012-07-25 09:16:58 -0300 | [diff] [blame] | 1658 | } |
| 1659 | |
Philipp Zabel | 2c11d1b | 2014-10-02 14:08:28 -0300 | [diff] [blame] | 1660 | /* |
| 1661 | * File operations |
| 1662 | */ |
| 1663 | |
| 1664 | static int coda_open(struct file *file) |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1665 | { |
Philipp Zabel | 2c11d1b | 2014-10-02 14:08:28 -0300 | [diff] [blame] | 1666 | struct video_device *vdev = video_devdata(file); |
| 1667 | struct coda_dev *dev = video_get_drvdata(vdev); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1668 | struct coda_ctx *ctx = NULL; |
Philipp Zabel | f61c0b1 | 2014-07-11 06:36:40 -0300 | [diff] [blame] | 1669 | char *name; |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 1670 | int ret; |
Philipp Zabel | e11f3e6 | 2012-07-25 09:16:58 -0300 | [diff] [blame] | 1671 | int idx; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1672 | |
Philipp Zabel | f23797b | 2014-08-06 08:02:23 -0300 | [diff] [blame] | 1673 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1674 | if (!ctx) |
| 1675 | return -ENOMEM; |
| 1676 | |
Fabio Estevam | f82bc20 | 2013-08-21 11:14:16 -0300 | [diff] [blame] | 1677 | idx = coda_next_free_instance(dev); |
Philipp Zabel | 0cddc7e | 2013-09-30 10:34:44 -0300 | [diff] [blame] | 1678 | if (idx < 0) { |
| 1679 | ret = idx; |
Fabio Estevam | f82bc20 | 2013-08-21 11:14:16 -0300 | [diff] [blame] | 1680 | goto err_coda_max; |
| 1681 | } |
| 1682 | set_bit(idx, &dev->instance_mask); |
| 1683 | |
Philipp Zabel | f61c0b1 | 2014-07-11 06:36:40 -0300 | [diff] [blame] | 1684 | name = kasprintf(GFP_KERNEL, "context%d", idx); |
Peter Seiderer | a7f933a | 2015-03-24 14:30:47 -0300 | [diff] [blame] | 1685 | if (!name) { |
| 1686 | ret = -ENOMEM; |
| 1687 | goto err_coda_name_init; |
| 1688 | } |
| 1689 | |
Philipp Zabel | f61c0b1 | 2014-07-11 06:36:40 -0300 | [diff] [blame] | 1690 | ctx->debugfs_entry = debugfs_create_dir(name, dev->debugfs_root); |
| 1691 | kfree(name); |
| 1692 | |
Philipp Zabel | 2c11d1b | 2014-10-02 14:08:28 -0300 | [diff] [blame] | 1693 | ctx->cvd = to_coda_video_device(vdev); |
| 1694 | ctx->inst_type = ctx->cvd->type; |
| 1695 | ctx->ops = ctx->cvd->ops; |
Philipp Zabel | a22496c | 2015-01-23 13:51:33 -0300 | [diff] [blame] | 1696 | ctx->use_bit = !ctx->cvd->direct; |
Philipp Zabel | 32b6f20 | 2014-07-11 06:36:20 -0300 | [diff] [blame] | 1697 | init_completion(&ctx->completion); |
| 1698 | INIT_WORK(&ctx->pic_run_work, coda_pic_run_work); |
Philipp Zabel | 747d764 | 2015-01-23 13:51:31 -0300 | [diff] [blame] | 1699 | if (ctx->ops->seq_end_work) |
| 1700 | INIT_WORK(&ctx->seq_end_work, ctx->ops->seq_end_work); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1701 | v4l2_fh_init(&ctx->fh, video_devdata(file)); |
| 1702 | file->private_data = &ctx->fh; |
| 1703 | v4l2_fh_add(&ctx->fh); |
| 1704 | ctx->dev = dev; |
Philipp Zabel | e11f3e6 | 2012-07-25 09:16:58 -0300 | [diff] [blame] | 1705 | ctx->idx = idx; |
Philipp Zabel | 5677e3b | 2013-06-21 03:55:30 -0300 | [diff] [blame] | 1706 | switch (dev->devtype->product) { |
Philipp Zabel | 8954844 | 2014-07-11 06:36:17 -0300 | [diff] [blame] | 1707 | case CODA_960: |
Philipp Zabel | 2bf299c | 2014-09-29 09:53:46 -0300 | [diff] [blame] | 1708 | ctx->frame_mem_ctrl = 1 << 12; |
| 1709 | /* fallthrough */ |
| 1710 | case CODA_7541: |
Philipp Zabel | 5677e3b | 2013-06-21 03:55:30 -0300 | [diff] [blame] | 1711 | ctx->reg_idx = 0; |
| 1712 | break; |
| 1713 | default: |
| 1714 | ctx->reg_idx = idx; |
| 1715 | } |
Fabio Estevam | f82bc20 | 2013-08-21 11:14:16 -0300 | [diff] [blame] | 1716 | |
Philipp Zabel | 1e17273 | 2014-07-11 06:36:23 -0300 | [diff] [blame] | 1717 | /* Power up and upload firmware if necessary */ |
| 1718 | ret = pm_runtime_get_sync(&dev->plat_dev->dev); |
| 1719 | if (ret < 0) { |
| 1720 | v4l2_err(&dev->v4l2_dev, "failed to power up: %d\n", ret); |
| 1721 | goto err_pm_get; |
| 1722 | } |
| 1723 | |
Fabio Estevam | 79830db | 2013-08-21 11:14:17 -0300 | [diff] [blame] | 1724 | ret = clk_prepare_enable(dev->clk_per); |
| 1725 | if (ret) |
| 1726 | goto err_clk_per; |
| 1727 | |
| 1728 | ret = clk_prepare_enable(dev->clk_ahb); |
| 1729 | if (ret) |
| 1730 | goto err_clk_ahb; |
| 1731 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1732 | set_default_params(ctx); |
Philipp Zabel | a1192a1 | 2014-07-23 12:28:40 -0300 | [diff] [blame] | 1733 | ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx, |
| 1734 | ctx->ops->queue_init); |
Philipp Zabel | 14604e3 | 2014-07-11 06:36:22 -0300 | [diff] [blame] | 1735 | if (IS_ERR(ctx->fh.m2m_ctx)) { |
| 1736 | ret = PTR_ERR(ctx->fh.m2m_ctx); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1737 | |
| 1738 | v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n", |
| 1739 | __func__, ret); |
Fabio Estevam | f82bc20 | 2013-08-21 11:14:16 -0300 | [diff] [blame] | 1740 | goto err_ctx_init; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1741 | } |
Philipp Zabel | 152ea1c | 2014-07-11 06:36:21 -0300 | [diff] [blame] | 1742 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1743 | ret = coda_ctrls_setup(ctx); |
| 1744 | if (ret) { |
| 1745 | v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n"); |
Fabio Estevam | f82bc20 | 2013-08-21 11:14:16 -0300 | [diff] [blame] | 1746 | goto err_ctrls_setup; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1747 | } |
| 1748 | |
| 1749 | ctx->fh.ctrl_handler = &ctx->ctrls; |
| 1750 | |
Philipp Zabel | 9082a7c | 2013-06-21 03:55:31 -0300 | [diff] [blame] | 1751 | mutex_init(&ctx->bitstream_mutex); |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 1752 | mutex_init(&ctx->buffer_mutex); |
Philipp Zabel | 7cbb105 | 2014-10-02 14:08:32 -0300 | [diff] [blame] | 1753 | INIT_LIST_HEAD(&ctx->buffer_meta_list); |
Philipp Zabel | 47f3fa6 | 2015-07-09 07:10:21 -0300 | [diff] [blame] | 1754 | spin_lock_init(&ctx->buffer_meta_lock); |
Philipp Zabel | 9082a7c | 2013-06-21 03:55:31 -0300 | [diff] [blame] | 1755 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1756 | coda_lock(ctx); |
Philipp Zabel | e11f3e6 | 2012-07-25 09:16:58 -0300 | [diff] [blame] | 1757 | list_add(&ctx->list, &dev->instances); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1758 | coda_unlock(ctx); |
| 1759 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1760 | v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Created instance %d (%p)\n", |
| 1761 | ctx->idx, ctx); |
| 1762 | |
| 1763 | return 0; |
| 1764 | |
Fabio Estevam | f82bc20 | 2013-08-21 11:14:16 -0300 | [diff] [blame] | 1765 | err_ctrls_setup: |
Philipp Zabel | 14604e3 | 2014-07-11 06:36:22 -0300 | [diff] [blame] | 1766 | v4l2_m2m_ctx_release(ctx->fh.m2m_ctx); |
Fabio Estevam | f82bc20 | 2013-08-21 11:14:16 -0300 | [diff] [blame] | 1767 | err_ctx_init: |
| 1768 | clk_disable_unprepare(dev->clk_ahb); |
Fabio Estevam | 79830db | 2013-08-21 11:14:17 -0300 | [diff] [blame] | 1769 | err_clk_ahb: |
Fabio Estevam | f82bc20 | 2013-08-21 11:14:16 -0300 | [diff] [blame] | 1770 | clk_disable_unprepare(dev->clk_per); |
Fabio Estevam | 79830db | 2013-08-21 11:14:17 -0300 | [diff] [blame] | 1771 | err_clk_per: |
Philipp Zabel | 1e17273 | 2014-07-11 06:36:23 -0300 | [diff] [blame] | 1772 | pm_runtime_put_sync(&dev->plat_dev->dev); |
| 1773 | err_pm_get: |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1774 | v4l2_fh_del(&ctx->fh); |
| 1775 | v4l2_fh_exit(&ctx->fh); |
Fabio Estevam | f82bc20 | 2013-08-21 11:14:16 -0300 | [diff] [blame] | 1776 | clear_bit(ctx->idx, &dev->instance_mask); |
Peter Seiderer | a7f933a | 2015-03-24 14:30:47 -0300 | [diff] [blame] | 1777 | err_coda_name_init: |
Fabio Estevam | f82bc20 | 2013-08-21 11:14:16 -0300 | [diff] [blame] | 1778 | err_coda_max: |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1779 | kfree(ctx); |
| 1780 | return ret; |
| 1781 | } |
| 1782 | |
| 1783 | static int coda_release(struct file *file) |
| 1784 | { |
| 1785 | struct coda_dev *dev = video_drvdata(file); |
| 1786 | struct coda_ctx *ctx = fh_to_ctx(file->private_data); |
| 1787 | |
| 1788 | v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Releasing instance %p\n", |
| 1789 | ctx); |
| 1790 | |
Philipp Zabel | a22496c | 2015-01-23 13:51:33 -0300 | [diff] [blame] | 1791 | if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit) |
Philipp Zabel | d4bb75f | 2014-10-08 13:09:11 -0300 | [diff] [blame] | 1792 | coda_bit_stream_end_flag(ctx); |
| 1793 | |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 1794 | /* 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] | 1795 | v4l2_m2m_ctx_release(ctx->fh.m2m_ctx); |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 1796 | |
| 1797 | /* In case the instance was not running, we still need to call SEQ_END */ |
Philipp Zabel | 5c718bb | 2015-07-09 07:10:16 -0300 | [diff] [blame] | 1798 | if (ctx->ops->seq_end_work) { |
Philipp Zabel | 32b6f20 | 2014-07-11 06:36:20 -0300 | [diff] [blame] | 1799 | queue_work(dev->workqueue, &ctx->seq_end_work); |
| 1800 | flush_work(&ctx->seq_end_work); |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 1801 | } |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 1802 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1803 | coda_lock(ctx); |
Philipp Zabel | e11f3e6 | 2012-07-25 09:16:58 -0300 | [diff] [blame] | 1804 | list_del(&ctx->list); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1805 | coda_unlock(ctx); |
| 1806 | |
Philipp Zabel | 5677e3b | 2013-06-21 03:55:30 -0300 | [diff] [blame] | 1807 | if (ctx->dev->devtype->product == CODA_DX6) |
| 1808 | coda_free_aux_buf(dev, &ctx->workbuf); |
| 1809 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1810 | v4l2_ctrl_handler_free(&ctx->ctrls); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1811 | clk_disable_unprepare(dev->clk_ahb); |
Fabio Estevam | f82bc20 | 2013-08-21 11:14:16 -0300 | [diff] [blame] | 1812 | clk_disable_unprepare(dev->clk_per); |
Philipp Zabel | 1e17273 | 2014-07-11 06:36:23 -0300 | [diff] [blame] | 1813 | pm_runtime_put_sync(&dev->plat_dev->dev); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1814 | v4l2_fh_del(&ctx->fh); |
| 1815 | v4l2_fh_exit(&ctx->fh); |
Philipp Zabel | e11f3e6 | 2012-07-25 09:16:58 -0300 | [diff] [blame] | 1816 | clear_bit(ctx->idx, &dev->instance_mask); |
Philipp Zabel | 58b7677 | 2014-07-23 12:28:43 -0300 | [diff] [blame] | 1817 | if (ctx->ops->release) |
| 1818 | ctx->ops->release(ctx); |
Philipp Zabel | e1519e8 | 2015-01-23 13:51:17 -0300 | [diff] [blame] | 1819 | debugfs_remove_recursive(ctx->debugfs_entry); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1820 | kfree(ctx); |
| 1821 | |
| 1822 | return 0; |
| 1823 | } |
| 1824 | |
Philipp Zabel | 2c11d1b | 2014-10-02 14:08:28 -0300 | [diff] [blame] | 1825 | static const struct v4l2_file_operations coda_fops = { |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1826 | .owner = THIS_MODULE, |
Philipp Zabel | 2c11d1b | 2014-10-02 14:08:28 -0300 | [diff] [blame] | 1827 | .open = coda_open, |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1828 | .release = coda_release, |
Philipp Zabel | 152ea1c | 2014-07-11 06:36:21 -0300 | [diff] [blame] | 1829 | .poll = v4l2_m2m_fop_poll, |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1830 | .unlocked_ioctl = video_ioctl2, |
Philipp Zabel | 152ea1c | 2014-07-11 06:36:21 -0300 | [diff] [blame] | 1831 | .mmap = v4l2_m2m_fop_mmap, |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1832 | }; |
| 1833 | |
Philipp Zabel | 87048bb | 2012-07-02 07:03:43 -0300 | [diff] [blame] | 1834 | static int coda_hw_init(struct coda_dev *dev) |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1835 | { |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1836 | u32 data; |
| 1837 | u16 *p; |
Fabio Estevam | 79830db | 2013-08-21 11:14:17 -0300 | [diff] [blame] | 1838 | int i, ret; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1839 | |
Fabio Estevam | 79830db | 2013-08-21 11:14:17 -0300 | [diff] [blame] | 1840 | ret = clk_prepare_enable(dev->clk_per); |
| 1841 | if (ret) |
Philipp Zabel | 1e17273 | 2014-07-11 06:36:23 -0300 | [diff] [blame] | 1842 | goto err_clk_per; |
Fabio Estevam | 79830db | 2013-08-21 11:14:17 -0300 | [diff] [blame] | 1843 | |
| 1844 | ret = clk_prepare_enable(dev->clk_ahb); |
| 1845 | if (ret) |
| 1846 | goto err_clk_ahb; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1847 | |
Philipp Zabel | 8f45284 | 2014-07-11 06:36:35 -0300 | [diff] [blame] | 1848 | if (dev->rstc) |
| 1849 | reset_control_reset(dev->rstc); |
| 1850 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1851 | /* |
| 1852 | * Copy the first CODA_ISRAM_SIZE in the internal SRAM. |
Philipp Zabel | 87048bb | 2012-07-02 07:03:43 -0300 | [diff] [blame] | 1853 | * The 16-bit chars in the code buffer are in memory access |
| 1854 | * order, re-sort them to CODA order for register download. |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1855 | * Data in this SRAM survives a reboot. |
| 1856 | */ |
Philipp Zabel | 87048bb | 2012-07-02 07:03:43 -0300 | [diff] [blame] | 1857 | p = (u16 *)dev->codebuf.vaddr; |
| 1858 | if (dev->devtype->product == CODA_DX6) { |
| 1859 | for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) { |
| 1860 | data = CODA_DOWN_ADDRESS_SET(i) | |
| 1861 | CODA_DOWN_DATA_SET(p[i ^ 1]); |
| 1862 | coda_write(dev, data, CODA_REG_BIT_CODE_DOWN); |
| 1863 | } |
| 1864 | } else { |
| 1865 | for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) { |
| 1866 | data = CODA_DOWN_ADDRESS_SET(i) | |
| 1867 | CODA_DOWN_DATA_SET(p[round_down(i, 4) + |
| 1868 | 3 - (i % 4)]); |
| 1869 | coda_write(dev, data, CODA_REG_BIT_CODE_DOWN); |
| 1870 | } |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1871 | } |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1872 | |
Philipp Zabel | 9acf769 | 2013-05-23 10:42:56 -0300 | [diff] [blame] | 1873 | /* Clear registers */ |
| 1874 | for (i = 0; i < 64; i++) |
| 1875 | coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4); |
| 1876 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1877 | /* Tell the BIT where to find everything it needs */ |
Philipp Zabel | 8954844 | 2014-07-11 06:36:17 -0300 | [diff] [blame] | 1878 | if (dev->devtype->product == CODA_960 || |
| 1879 | dev->devtype->product == CODA_7541) { |
Philipp Zabel | 5677e3b | 2013-06-21 03:55:30 -0300 | [diff] [blame] | 1880 | coda_write(dev, dev->tempbuf.paddr, |
| 1881 | CODA_REG_BIT_TEMP_BUF_ADDR); |
Philipp Zabel | 918c66f | 2013-06-27 06:59:01 -0300 | [diff] [blame] | 1882 | coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM); |
Philipp Zabel | 5677e3b | 2013-06-21 03:55:30 -0300 | [diff] [blame] | 1883 | } else { |
| 1884 | coda_write(dev, dev->workbuf.paddr, |
| 1885 | CODA_REG_BIT_WORK_BUF_ADDR); |
| 1886 | } |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1887 | coda_write(dev, dev->codebuf.paddr, |
| 1888 | CODA_REG_BIT_CODE_BUF_ADDR); |
| 1889 | coda_write(dev, 0, CODA_REG_BIT_CODE_RUN); |
| 1890 | |
| 1891 | /* Set default values */ |
| 1892 | switch (dev->devtype->product) { |
| 1893 | case CODA_DX6: |
Philipp Zabel | f23797b | 2014-08-06 08:02:23 -0300 | [diff] [blame] | 1894 | coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH, |
| 1895 | CODA_REG_BIT_STREAM_CTRL); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1896 | break; |
| 1897 | default: |
Philipp Zabel | f23797b | 2014-08-06 08:02:23 -0300 | [diff] [blame] | 1898 | coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH, |
| 1899 | CODA_REG_BIT_STREAM_CTRL); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1900 | } |
Philipp Zabel | 8954844 | 2014-07-11 06:36:17 -0300 | [diff] [blame] | 1901 | if (dev->devtype->product == CODA_960) |
| 1902 | coda_write(dev, 1 << 12, CODA_REG_BIT_FRAME_MEM_CTRL); |
| 1903 | else |
| 1904 | coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL); |
Philipp Zabel | 1043667 | 2012-07-02 09:03:55 -0300 | [diff] [blame] | 1905 | |
| 1906 | if (dev->devtype->product != CODA_DX6) |
| 1907 | coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE); |
| 1908 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1909 | coda_write(dev, CODA_INT_INTERRUPT_ENABLE, |
| 1910 | CODA_REG_BIT_INT_ENABLE); |
| 1911 | |
| 1912 | /* Reset VPU and start processor */ |
| 1913 | data = coda_read(dev, CODA_REG_BIT_CODE_RESET); |
| 1914 | data |= CODA_REG_RESET_ENABLE; |
| 1915 | coda_write(dev, data, CODA_REG_BIT_CODE_RESET); |
| 1916 | udelay(10); |
| 1917 | data &= ~CODA_REG_RESET_ENABLE; |
| 1918 | coda_write(dev, data, CODA_REG_BIT_CODE_RESET); |
| 1919 | coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN); |
| 1920 | |
Philipp Zabel | fe7554e | 2014-07-11 06:36:24 -0300 | [diff] [blame] | 1921 | clk_disable_unprepare(dev->clk_ahb); |
| 1922 | clk_disable_unprepare(dev->clk_per); |
| 1923 | |
| 1924 | return 0; |
| 1925 | |
| 1926 | err_clk_ahb: |
| 1927 | clk_disable_unprepare(dev->clk_per); |
| 1928 | err_clk_per: |
| 1929 | return ret; |
| 1930 | } |
| 1931 | |
Philipp Zabel | 2c11d1b | 2014-10-02 14:08:28 -0300 | [diff] [blame] | 1932 | static int coda_register_device(struct coda_dev *dev, int i) |
Philipp Zabel | 121cacf | 2014-07-18 07:22:42 -0300 | [diff] [blame] | 1933 | { |
Philipp Zabel | 2c11d1b | 2014-10-02 14:08:28 -0300 | [diff] [blame] | 1934 | struct video_device *vfd = &dev->vfd[i]; |
| 1935 | |
Dan Carpenter | 88ca3af | 2015-01-08 07:07:08 -0300 | [diff] [blame] | 1936 | if (i >= dev->devtype->num_vdevs) |
Philipp Zabel | 2c11d1b | 2014-10-02 14:08:28 -0300 | [diff] [blame] | 1937 | return -EINVAL; |
| 1938 | |
Philipp Zabel | bf249da | 2015-03-24 14:30:50 -0300 | [diff] [blame] | 1939 | strlcpy(vfd->name, dev->devtype->vdevs[i]->name, sizeof(vfd->name)); |
Philipp Zabel | 2c11d1b | 2014-10-02 14:08:28 -0300 | [diff] [blame] | 1940 | vfd->fops = &coda_fops; |
| 1941 | vfd->ioctl_ops = &coda_ioctl_ops; |
Philipp Zabel | 121cacf | 2014-07-18 07:22:42 -0300 | [diff] [blame] | 1942 | vfd->release = video_device_release_empty, |
| 1943 | vfd->lock = &dev->dev_mutex; |
| 1944 | vfd->v4l2_dev = &dev->v4l2_dev; |
| 1945 | vfd->vfl_dir = VFL_DIR_M2M; |
| 1946 | video_set_drvdata(vfd, dev); |
| 1947 | |
Philipp Zabel | a188a66 | 2014-08-05 14:00:20 -0300 | [diff] [blame] | 1948 | /* Not applicable, use the selection API instead */ |
| 1949 | v4l2_disable_ioctl(vfd, VIDIOC_CROPCAP); |
| 1950 | v4l2_disable_ioctl(vfd, VIDIOC_G_CROP); |
| 1951 | v4l2_disable_ioctl(vfd, VIDIOC_S_CROP); |
| 1952 | |
Philipp Zabel | 121cacf | 2014-07-18 07:22:42 -0300 | [diff] [blame] | 1953 | return video_register_device(vfd, VFL_TYPE_GRABBER, 0); |
| 1954 | } |
| 1955 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1956 | static void coda_fw_callback(const struct firmware *fw, void *context) |
| 1957 | { |
| 1958 | struct coda_dev *dev = context; |
| 1959 | struct platform_device *pdev = dev->plat_dev; |
Philipp Zabel | 2c11d1b | 2014-10-02 14:08:28 -0300 | [diff] [blame] | 1960 | int i, ret; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1961 | |
| 1962 | if (!fw) { |
| 1963 | v4l2_err(&dev->v4l2_dev, "firmware request failed\n"); |
Ulf Hansson | c5d28e2 | 2014-09-22 13:05:56 -0300 | [diff] [blame] | 1964 | goto put_pm; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1965 | } |
| 1966 | |
| 1967 | /* allocate auxiliary per-device code buffer for the BIT processor */ |
Philipp Zabel | f61c0b1 | 2014-07-11 06:36:40 -0300 | [diff] [blame] | 1968 | ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size, "codebuf", |
| 1969 | dev->debugfs_root); |
Philipp Zabel | 6ba53b8 | 2015-03-24 14:30:54 -0300 | [diff] [blame] | 1970 | if (ret < 0) |
Ulf Hansson | c5d28e2 | 2014-09-22 13:05:56 -0300 | [diff] [blame] | 1971 | goto put_pm; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1972 | |
Philipp Zabel | 87048bb | 2012-07-02 07:03:43 -0300 | [diff] [blame] | 1973 | /* Copy the whole firmware image to the code buffer */ |
| 1974 | memcpy(dev->codebuf.vaddr, fw->data, fw->size); |
| 1975 | release_firmware(fw); |
| 1976 | |
Ulf Hansson | c5d28e2 | 2014-09-22 13:05:56 -0300 | [diff] [blame] | 1977 | ret = coda_hw_init(dev); |
| 1978 | if (ret < 0) { |
| 1979 | v4l2_err(&dev->v4l2_dev, "HW initialization failed\n"); |
| 1980 | goto put_pm; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1981 | } |
| 1982 | |
Ulf Hansson | c5d28e2 | 2014-09-22 13:05:56 -0300 | [diff] [blame] | 1983 | ret = coda_check_firmware(dev); |
| 1984 | if (ret < 0) |
| 1985 | goto put_pm; |
| 1986 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1987 | dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev); |
| 1988 | if (IS_ERR(dev->alloc_ctx)) { |
| 1989 | v4l2_err(&dev->v4l2_dev, "Failed to alloc vb2 context\n"); |
Ulf Hansson | c5d28e2 | 2014-09-22 13:05:56 -0300 | [diff] [blame] | 1990 | goto put_pm; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 1991 | } |
| 1992 | |
| 1993 | dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops); |
| 1994 | if (IS_ERR(dev->m2m_dev)) { |
| 1995 | v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n"); |
| 1996 | goto rel_ctx; |
| 1997 | } |
| 1998 | |
Philipp Zabel | 2c11d1b | 2014-10-02 14:08:28 -0300 | [diff] [blame] | 1999 | for (i = 0; i < dev->devtype->num_vdevs; i++) { |
| 2000 | ret = coda_register_device(dev, i); |
| 2001 | if (ret) { |
| 2002 | v4l2_err(&dev->v4l2_dev, |
| 2003 | "Failed to register %s video device: %d\n", |
| 2004 | dev->devtype->vdevs[i]->name, ret); |
| 2005 | goto rel_vfd; |
| 2006 | } |
Philipp Zabel | 121cacf | 2014-07-18 07:22:42 -0300 | [diff] [blame] | 2007 | } |
| 2008 | |
| 2009 | v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video[%d-%d]\n", |
Philipp Zabel | 2c11d1b | 2014-10-02 14:08:28 -0300 | [diff] [blame] | 2010 | dev->vfd[0].num, dev->vfd[i - 1].num); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2011 | |
Ulf Hansson | c5d28e2 | 2014-09-22 13:05:56 -0300 | [diff] [blame] | 2012 | pm_runtime_put_sync(&pdev->dev); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2013 | return; |
| 2014 | |
Philipp Zabel | 2c11d1b | 2014-10-02 14:08:28 -0300 | [diff] [blame] | 2015 | rel_vfd: |
| 2016 | while (--i >= 0) |
| 2017 | video_unregister_device(&dev->vfd[i]); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2018 | v4l2_m2m_release(dev->m2m_dev); |
| 2019 | rel_ctx: |
| 2020 | vb2_dma_contig_cleanup_ctx(dev->alloc_ctx); |
Ulf Hansson | c5d28e2 | 2014-09-22 13:05:56 -0300 | [diff] [blame] | 2021 | put_pm: |
| 2022 | pm_runtime_put_sync(&pdev->dev); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2023 | } |
| 2024 | |
| 2025 | static int coda_firmware_request(struct coda_dev *dev) |
| 2026 | { |
| 2027 | char *fw = dev->devtype->firmware; |
| 2028 | |
| 2029 | dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw, |
| 2030 | coda_product_name(dev->devtype->product)); |
| 2031 | |
| 2032 | return request_firmware_nowait(THIS_MODULE, true, |
| 2033 | fw, &dev->plat_dev->dev, GFP_KERNEL, dev, coda_fw_callback); |
| 2034 | } |
| 2035 | |
| 2036 | enum coda_platform { |
| 2037 | CODA_IMX27, |
Philipp Zabel | df1e74c | 2012-07-02 06:07:10 -0300 | [diff] [blame] | 2038 | CODA_IMX53, |
Philipp Zabel | 8954844 | 2014-07-11 06:36:17 -0300 | [diff] [blame] | 2039 | CODA_IMX6Q, |
| 2040 | CODA_IMX6DL, |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2041 | }; |
| 2042 | |
Emil Goode | c06d875 | 2012-08-14 17:44:42 -0300 | [diff] [blame] | 2043 | static const struct coda_devtype coda_devdata[] = { |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2044 | [CODA_IMX27] = { |
Philipp Zabel | e5b0d1c | 2014-07-11 06:36:41 -0300 | [diff] [blame] | 2045 | .firmware = "v4l-codadx6-imx27.bin", |
| 2046 | .product = CODA_DX6, |
| 2047 | .codecs = codadx6_codecs, |
| 2048 | .num_codecs = ARRAY_SIZE(codadx6_codecs), |
Philipp Zabel | 2c11d1b | 2014-10-02 14:08:28 -0300 | [diff] [blame] | 2049 | .vdevs = codadx6_video_devices, |
| 2050 | .num_vdevs = ARRAY_SIZE(codadx6_video_devices), |
Philipp Zabel | e5b0d1c | 2014-07-11 06:36:41 -0300 | [diff] [blame] | 2051 | .workbuf_size = 288 * 1024 + FMO_SLICE_SAVE_BUF_SIZE * 8 * 1024, |
Philipp Zabel | 401e972 | 2014-07-11 06:36:43 -0300 | [diff] [blame] | 2052 | .iram_size = 0xb000, |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2053 | }, |
Philipp Zabel | df1e74c | 2012-07-02 06:07:10 -0300 | [diff] [blame] | 2054 | [CODA_IMX53] = { |
Philipp Zabel | e5b0d1c | 2014-07-11 06:36:41 -0300 | [diff] [blame] | 2055 | .firmware = "v4l-coda7541-imx53.bin", |
| 2056 | .product = CODA_7541, |
| 2057 | .codecs = coda7_codecs, |
| 2058 | .num_codecs = ARRAY_SIZE(coda7_codecs), |
Philipp Zabel | 2c11d1b | 2014-10-02 14:08:28 -0300 | [diff] [blame] | 2059 | .vdevs = coda7_video_devices, |
| 2060 | .num_vdevs = ARRAY_SIZE(coda7_video_devices), |
Philipp Zabel | e5b0d1c | 2014-07-11 06:36:41 -0300 | [diff] [blame] | 2061 | .workbuf_size = 128 * 1024, |
Philipp Zabel | 5d73fad | 2014-07-11 06:36:42 -0300 | [diff] [blame] | 2062 | .tempbuf_size = 304 * 1024, |
Philipp Zabel | 401e972 | 2014-07-11 06:36:43 -0300 | [diff] [blame] | 2063 | .iram_size = 0x14000, |
Philipp Zabel | df1e74c | 2012-07-02 06:07:10 -0300 | [diff] [blame] | 2064 | }, |
Philipp Zabel | 8954844 | 2014-07-11 06:36:17 -0300 | [diff] [blame] | 2065 | [CODA_IMX6Q] = { |
Philipp Zabel | e5b0d1c | 2014-07-11 06:36:41 -0300 | [diff] [blame] | 2066 | .firmware = "v4l-coda960-imx6q.bin", |
| 2067 | .product = CODA_960, |
| 2068 | .codecs = coda9_codecs, |
| 2069 | .num_codecs = ARRAY_SIZE(coda9_codecs), |
Philipp Zabel | 2c11d1b | 2014-10-02 14:08:28 -0300 | [diff] [blame] | 2070 | .vdevs = coda9_video_devices, |
| 2071 | .num_vdevs = ARRAY_SIZE(coda9_video_devices), |
Philipp Zabel | e5b0d1c | 2014-07-11 06:36:41 -0300 | [diff] [blame] | 2072 | .workbuf_size = 80 * 1024, |
Philipp Zabel | 5d73fad | 2014-07-11 06:36:42 -0300 | [diff] [blame] | 2073 | .tempbuf_size = 204 * 1024, |
Philipp Zabel | 401e972 | 2014-07-11 06:36:43 -0300 | [diff] [blame] | 2074 | .iram_size = 0x21000, |
Philipp Zabel | 8954844 | 2014-07-11 06:36:17 -0300 | [diff] [blame] | 2075 | }, |
| 2076 | [CODA_IMX6DL] = { |
Philipp Zabel | e5b0d1c | 2014-07-11 06:36:41 -0300 | [diff] [blame] | 2077 | .firmware = "v4l-coda960-imx6dl.bin", |
| 2078 | .product = CODA_960, |
| 2079 | .codecs = coda9_codecs, |
| 2080 | .num_codecs = ARRAY_SIZE(coda9_codecs), |
Philipp Zabel | 2c11d1b | 2014-10-02 14:08:28 -0300 | [diff] [blame] | 2081 | .vdevs = coda9_video_devices, |
| 2082 | .num_vdevs = ARRAY_SIZE(coda9_video_devices), |
Philipp Zabel | e5b0d1c | 2014-07-11 06:36:41 -0300 | [diff] [blame] | 2083 | .workbuf_size = 80 * 1024, |
Philipp Zabel | 5d73fad | 2014-07-11 06:36:42 -0300 | [diff] [blame] | 2084 | .tempbuf_size = 204 * 1024, |
Philipp Zabel | 401e972 | 2014-07-11 06:36:43 -0300 | [diff] [blame] | 2085 | .iram_size = 0x20000, |
Philipp Zabel | 8954844 | 2014-07-11 06:36:17 -0300 | [diff] [blame] | 2086 | }, |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2087 | }; |
| 2088 | |
| 2089 | static struct platform_device_id coda_platform_ids[] = { |
| 2090 | { .name = "coda-imx27", .driver_data = CODA_IMX27 }, |
| 2091 | { /* sentinel */ } |
| 2092 | }; |
| 2093 | MODULE_DEVICE_TABLE(platform, coda_platform_ids); |
| 2094 | |
| 2095 | #ifdef CONFIG_OF |
| 2096 | static const struct of_device_id coda_dt_ids[] = { |
Alexander Shiyan | 7b0dd9e | 2013-06-15 08:09:57 -0300 | [diff] [blame] | 2097 | { .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] }, |
Philipp Zabel | df1e74c | 2012-07-02 06:07:10 -0300 | [diff] [blame] | 2098 | { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] }, |
Philipp Zabel | 8954844 | 2014-07-11 06:36:17 -0300 | [diff] [blame] | 2099 | { .compatible = "fsl,imx6q-vpu", .data = &coda_devdata[CODA_IMX6Q] }, |
| 2100 | { .compatible = "fsl,imx6dl-vpu", .data = &coda_devdata[CODA_IMX6DL] }, |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2101 | { /* sentinel */ } |
| 2102 | }; |
| 2103 | MODULE_DEVICE_TABLE(of, coda_dt_ids); |
| 2104 | #endif |
| 2105 | |
Greg Kroah-Hartman | 4c62e97 | 2012-12-21 13:17:53 -0800 | [diff] [blame] | 2106 | static int coda_probe(struct platform_device *pdev) |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2107 | { |
| 2108 | const struct of_device_id *of_id = |
| 2109 | of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev); |
| 2110 | const struct platform_device_id *pdev_id; |
Philipp Zabel | 657eee7 | 2013-04-29 16:17:14 -0700 | [diff] [blame] | 2111 | struct coda_platform_data *pdata = pdev->dev.platform_data; |
| 2112 | struct device_node *np = pdev->dev.of_node; |
| 2113 | struct gen_pool *pool; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2114 | struct coda_dev *dev; |
| 2115 | struct resource *res; |
| 2116 | int ret, irq; |
| 2117 | |
Philipp Zabel | f23797b | 2014-08-06 08:02:23 -0300 | [diff] [blame] | 2118 | dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL); |
Philipp Zabel | 6da999d | 2014-09-29 09:53:43 -0300 | [diff] [blame] | 2119 | if (!dev) |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2120 | return -ENOMEM; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2121 | |
Philipp Zabel | b2f91ae | 2014-10-02 14:08:27 -0300 | [diff] [blame] | 2122 | pdev_id = of_id ? of_id->data : platform_get_device_id(pdev); |
| 2123 | |
Fabio Estevam | b7bd660 | 2014-10-04 16:40:50 -0300 | [diff] [blame] | 2124 | if (of_id) { |
Philipp Zabel | b2f91ae | 2014-10-02 14:08:27 -0300 | [diff] [blame] | 2125 | dev->devtype = of_id->data; |
Fabio Estevam | b7bd660 | 2014-10-04 16:40:50 -0300 | [diff] [blame] | 2126 | } else if (pdev_id) { |
Philipp Zabel | b2f91ae | 2014-10-02 14:08:27 -0300 | [diff] [blame] | 2127 | dev->devtype = &coda_devdata[pdev_id->driver_data]; |
Fabio Estevam | b7bd660 | 2014-10-04 16:40:50 -0300 | [diff] [blame] | 2128 | } else { |
| 2129 | ret = -EINVAL; |
| 2130 | goto err_v4l2_register; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2131 | } |
| 2132 | |
| 2133 | spin_lock_init(&dev->irqlock); |
Philipp Zabel | e11f3e6 | 2012-07-25 09:16:58 -0300 | [diff] [blame] | 2134 | INIT_LIST_HEAD(&dev->instances); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2135 | |
| 2136 | dev->plat_dev = pdev; |
| 2137 | dev->clk_per = devm_clk_get(&pdev->dev, "per"); |
| 2138 | if (IS_ERR(dev->clk_per)) { |
| 2139 | dev_err(&pdev->dev, "Could not get per clock\n"); |
| 2140 | return PTR_ERR(dev->clk_per); |
| 2141 | } |
| 2142 | |
| 2143 | dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb"); |
| 2144 | if (IS_ERR(dev->clk_ahb)) { |
| 2145 | dev_err(&pdev->dev, "Could not get ahb clock\n"); |
| 2146 | return PTR_ERR(dev->clk_ahb); |
| 2147 | } |
| 2148 | |
| 2149 | /* Get memory for physical registers */ |
| 2150 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Philipp Zabel | 611cbbf | 2013-05-21 04:19:27 -0300 | [diff] [blame] | 2151 | dev->regs_base = devm_ioremap_resource(&pdev->dev, res); |
| 2152 | if (IS_ERR(dev->regs_base)) |
| 2153 | return PTR_ERR(dev->regs_base); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2154 | |
| 2155 | /* IRQ */ |
Philipp Zabel | 540b72e | 2014-08-05 14:00:09 -0300 | [diff] [blame] | 2156 | irq = platform_get_irq_byname(pdev, "bit"); |
| 2157 | if (irq < 0) |
| 2158 | irq = platform_get_irq(pdev, 0); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2159 | if (irq < 0) { |
| 2160 | dev_err(&pdev->dev, "failed to get irq resource\n"); |
Fabio Estevam | 7d37743 | 2014-06-04 15:46:23 -0300 | [diff] [blame] | 2161 | return irq; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2162 | } |
| 2163 | |
Fabio Estevam | 08c8d14 | 2014-06-04 15:46:24 -0300 | [diff] [blame] | 2164 | ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, coda_irq_handler, |
| 2165 | IRQF_ONESHOT, dev_name(&pdev->dev), dev); |
| 2166 | if (ret < 0) { |
| 2167 | dev_err(&pdev->dev, "failed to request irq: %d\n", ret); |
| 2168 | return ret; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2169 | } |
| 2170 | |
Philipp Zabel | ee27aa9 | 2014-07-24 16:50:03 -0300 | [diff] [blame] | 2171 | dev->rstc = devm_reset_control_get_optional(&pdev->dev, NULL); |
Philipp Zabel | 8f45284 | 2014-07-11 06:36:35 -0300 | [diff] [blame] | 2172 | if (IS_ERR(dev->rstc)) { |
| 2173 | ret = PTR_ERR(dev->rstc); |
Philipp Zabel | ee27aa9 | 2014-07-24 16:50:03 -0300 | [diff] [blame] | 2174 | if (ret == -ENOENT || ret == -ENOSYS) { |
Philipp Zabel | 8f45284 | 2014-07-11 06:36:35 -0300 | [diff] [blame] | 2175 | dev->rstc = NULL; |
| 2176 | } else { |
Philipp Zabel | f23797b | 2014-08-06 08:02:23 -0300 | [diff] [blame] | 2177 | dev_err(&pdev->dev, "failed get reset control: %d\n", |
| 2178 | ret); |
Philipp Zabel | 8f45284 | 2014-07-11 06:36:35 -0300 | [diff] [blame] | 2179 | return ret; |
| 2180 | } |
| 2181 | } |
| 2182 | |
Philipp Zabel | 657eee7 | 2013-04-29 16:17:14 -0700 | [diff] [blame] | 2183 | /* Get IRAM pool from device tree or platform data */ |
Vladimir Zapolskiy | abdd4a7 | 2015-06-30 15:00:07 -0700 | [diff] [blame] | 2184 | pool = of_gen_pool_get(np, "iram", 0); |
Philipp Zabel | 657eee7 | 2013-04-29 16:17:14 -0700 | [diff] [blame] | 2185 | if (!pool && pdata) |
Vladimir Zapolskiy | 7385817 | 2015-09-04 15:47:43 -0700 | [diff] [blame] | 2186 | pool = gen_pool_get(pdata->iram_dev, NULL); |
Philipp Zabel | 657eee7 | 2013-04-29 16:17:14 -0700 | [diff] [blame] | 2187 | if (!pool) { |
| 2188 | dev_err(&pdev->dev, "iram pool not available\n"); |
| 2189 | return -ENOMEM; |
| 2190 | } |
| 2191 | dev->iram_pool = pool; |
| 2192 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2193 | ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev); |
| 2194 | if (ret) |
| 2195 | return ret; |
| 2196 | |
| 2197 | mutex_init(&dev->dev_mutex); |
Philipp Zabel | fcb6282 | 2013-05-23 10:43:00 -0300 | [diff] [blame] | 2198 | mutex_init(&dev->coda_mutex); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2199 | |
Philipp Zabel | f61c0b1 | 2014-07-11 06:36:40 -0300 | [diff] [blame] | 2200 | dev->debugfs_root = debugfs_create_dir("coda", NULL); |
| 2201 | if (!dev->debugfs_root) |
| 2202 | dev_warn(&pdev->dev, "failed to create debugfs root\n"); |
| 2203 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2204 | /* allocate auxiliary per-device buffers for the BIT processor */ |
Philipp Zabel | 5d73fad | 2014-07-11 06:36:42 -0300 | [diff] [blame] | 2205 | if (dev->devtype->product == CODA_DX6) { |
Philipp Zabel | 5677e3b | 2013-06-21 03:55:30 -0300 | [diff] [blame] | 2206 | ret = coda_alloc_aux_buf(dev, &dev->workbuf, |
Philipp Zabel | e5b0d1c | 2014-07-11 06:36:41 -0300 | [diff] [blame] | 2207 | dev->devtype->workbuf_size, "workbuf", |
Philipp Zabel | f61c0b1 | 2014-07-11 06:36:40 -0300 | [diff] [blame] | 2208 | dev->debugfs_root); |
Philipp Zabel | 6ba53b8 | 2015-03-24 14:30:54 -0300 | [diff] [blame] | 2209 | if (ret < 0) |
Fabio Estevam | b7bd660 | 2014-10-04 16:40:50 -0300 | [diff] [blame] | 2210 | goto err_v4l2_register; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2211 | } |
Philipp Zabel | 5d73fad | 2014-07-11 06:36:42 -0300 | [diff] [blame] | 2212 | |
| 2213 | if (dev->devtype->tempbuf_size) { |
Philipp Zabel | 5677e3b | 2013-06-21 03:55:30 -0300 | [diff] [blame] | 2214 | ret = coda_alloc_aux_buf(dev, &dev->tempbuf, |
Philipp Zabel | 5d73fad | 2014-07-11 06:36:42 -0300 | [diff] [blame] | 2215 | dev->devtype->tempbuf_size, "tempbuf", |
Philipp Zabel | f61c0b1 | 2014-07-11 06:36:40 -0300 | [diff] [blame] | 2216 | dev->debugfs_root); |
Philipp Zabel | 6ba53b8 | 2015-03-24 14:30:54 -0300 | [diff] [blame] | 2217 | if (ret < 0) |
Fabio Estevam | b7bd660 | 2014-10-04 16:40:50 -0300 | [diff] [blame] | 2218 | goto err_v4l2_register; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2219 | } |
| 2220 | |
Philipp Zabel | 401e972 | 2014-07-11 06:36:43 -0300 | [diff] [blame] | 2221 | dev->iram.size = dev->devtype->iram_size; |
Philipp Zabel | b313bcc | 2014-07-11 06:36:16 -0300 | [diff] [blame] | 2222 | dev->iram.vaddr = gen_pool_dma_alloc(dev->iram_pool, dev->iram.size, |
| 2223 | &dev->iram.paddr); |
| 2224 | if (!dev->iram.vaddr) { |
Philipp Zabel | 8be31c8 | 2014-08-05 14:00:13 -0300 | [diff] [blame] | 2225 | dev_warn(&pdev->dev, "unable to alloc iram\n"); |
| 2226 | } else { |
Philipp Zabel | 811a693 | 2015-01-23 13:51:23 -0300 | [diff] [blame] | 2227 | memset(dev->iram.vaddr, 0, dev->iram.size); |
Philipp Zabel | 8be31c8 | 2014-08-05 14:00:13 -0300 | [diff] [blame] | 2228 | dev->iram.blob.data = dev->iram.vaddr; |
| 2229 | dev->iram.blob.size = dev->iram.size; |
| 2230 | dev->iram.dentry = debugfs_create_blob("iram", 0644, |
| 2231 | dev->debugfs_root, |
| 2232 | &dev->iram.blob); |
Philipp Zabel | 1043667 | 2012-07-02 09:03:55 -0300 | [diff] [blame] | 2233 | } |
| 2234 | |
Philipp Zabel | 32b6f20 | 2014-07-11 06:36:20 -0300 | [diff] [blame] | 2235 | dev->workqueue = alloc_workqueue("coda", WQ_UNBOUND | WQ_MEM_RECLAIM, 1); |
| 2236 | if (!dev->workqueue) { |
| 2237 | dev_err(&pdev->dev, "unable to alloc workqueue\n"); |
Fabio Estevam | 74d08d5 | 2014-10-04 16:40:51 -0300 | [diff] [blame] | 2238 | ret = -ENOMEM; |
| 2239 | goto err_v4l2_register; |
Philipp Zabel | 32b6f20 | 2014-07-11 06:36:20 -0300 | [diff] [blame] | 2240 | } |
| 2241 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2242 | platform_set_drvdata(pdev, dev); |
| 2243 | |
Ulf Hansson | c5d28e2 | 2014-09-22 13:05:56 -0300 | [diff] [blame] | 2244 | /* |
| 2245 | * Start activated so we can directly call coda_hw_init in |
Rafael J. Wysocki | e243c7c | 2014-12-04 01:10:10 +0100 | [diff] [blame] | 2246 | * coda_fw_callback regardless of whether CONFIG_PM is |
Ulf Hansson | c5d28e2 | 2014-09-22 13:05:56 -0300 | [diff] [blame] | 2247 | * enabled or whether the device is associated with a PM domain. |
| 2248 | */ |
| 2249 | pm_runtime_get_noresume(&pdev->dev); |
| 2250 | pm_runtime_set_active(&pdev->dev); |
Philipp Zabel | 1e17273 | 2014-07-11 06:36:23 -0300 | [diff] [blame] | 2251 | pm_runtime_enable(&pdev->dev); |
| 2252 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2253 | return coda_firmware_request(dev); |
Fabio Estevam | b7bd660 | 2014-10-04 16:40:50 -0300 | [diff] [blame] | 2254 | |
| 2255 | err_v4l2_register: |
| 2256 | v4l2_device_unregister(&dev->v4l2_dev); |
| 2257 | return ret; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2258 | } |
| 2259 | |
| 2260 | static int coda_remove(struct platform_device *pdev) |
| 2261 | { |
| 2262 | struct coda_dev *dev = platform_get_drvdata(pdev); |
Philipp Zabel | 2c11d1b | 2014-10-02 14:08:28 -0300 | [diff] [blame] | 2263 | int i; |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2264 | |
Philipp Zabel | 2c11d1b | 2014-10-02 14:08:28 -0300 | [diff] [blame] | 2265 | for (i = 0; i < ARRAY_SIZE(dev->vfd); i++) { |
| 2266 | if (video_get_drvdata(&dev->vfd[i])) |
| 2267 | video_unregister_device(&dev->vfd[i]); |
| 2268 | } |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2269 | if (dev->m2m_dev) |
| 2270 | v4l2_m2m_release(dev->m2m_dev); |
Philipp Zabel | 1e17273 | 2014-07-11 06:36:23 -0300 | [diff] [blame] | 2271 | pm_runtime_disable(&pdev->dev); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2272 | if (dev->alloc_ctx) |
| 2273 | vb2_dma_contig_cleanup_ctx(dev->alloc_ctx); |
| 2274 | v4l2_device_unregister(&dev->v4l2_dev); |
Philipp Zabel | 32b6f20 | 2014-07-11 06:36:20 -0300 | [diff] [blame] | 2275 | destroy_workqueue(dev->workqueue); |
Philipp Zabel | b313bcc | 2014-07-11 06:36:16 -0300 | [diff] [blame] | 2276 | if (dev->iram.vaddr) |
| 2277 | gen_pool_free(dev->iram_pool, (unsigned long)dev->iram.vaddr, |
| 2278 | dev->iram.size); |
Philipp Zabel | 5677e3b | 2013-06-21 03:55:30 -0300 | [diff] [blame] | 2279 | coda_free_aux_buf(dev, &dev->codebuf); |
| 2280 | coda_free_aux_buf(dev, &dev->tempbuf); |
| 2281 | coda_free_aux_buf(dev, &dev->workbuf); |
Philipp Zabel | f61c0b1 | 2014-07-11 06:36:40 -0300 | [diff] [blame] | 2282 | debugfs_remove_recursive(dev->debugfs_root); |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2283 | return 0; |
| 2284 | } |
| 2285 | |
Rafael J. Wysocki | e243c7c | 2014-12-04 01:10:10 +0100 | [diff] [blame] | 2286 | #ifdef CONFIG_PM |
Philipp Zabel | 1e17273 | 2014-07-11 06:36:23 -0300 | [diff] [blame] | 2287 | static int coda_runtime_resume(struct device *dev) |
| 2288 | { |
| 2289 | struct coda_dev *cdev = dev_get_drvdata(dev); |
| 2290 | int ret = 0; |
| 2291 | |
Philipp Zabel | 65919e6 | 2014-07-18 07:22:36 -0300 | [diff] [blame] | 2292 | if (dev->pm_domain && cdev->codebuf.vaddr) { |
Philipp Zabel | 1e17273 | 2014-07-11 06:36:23 -0300 | [diff] [blame] | 2293 | ret = coda_hw_init(cdev); |
| 2294 | if (ret) |
| 2295 | v4l2_err(&cdev->v4l2_dev, "HW initialization failed\n"); |
| 2296 | } |
| 2297 | |
| 2298 | return ret; |
| 2299 | } |
| 2300 | #endif |
| 2301 | |
| 2302 | static const struct dev_pm_ops coda_pm_ops = { |
| 2303 | SET_RUNTIME_PM_OPS(NULL, coda_runtime_resume, NULL) |
| 2304 | }; |
| 2305 | |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2306 | static struct platform_driver coda_driver = { |
| 2307 | .probe = coda_probe, |
Greg Kroah-Hartman | 4c62e97 | 2012-12-21 13:17:53 -0800 | [diff] [blame] | 2308 | .remove = coda_remove, |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2309 | .driver = { |
| 2310 | .name = CODA_NAME, |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2311 | .of_match_table = of_match_ptr(coda_dt_ids), |
Philipp Zabel | 1e17273 | 2014-07-11 06:36:23 -0300 | [diff] [blame] | 2312 | .pm = &coda_pm_ops, |
Javier Martin | 186b250 | 2012-07-26 05:53:35 -0300 | [diff] [blame] | 2313 | }, |
| 2314 | .id_table = coda_platform_ids, |
| 2315 | }; |
| 2316 | |
| 2317 | module_platform_driver(coda_driver); |
| 2318 | |
| 2319 | MODULE_LICENSE("GPL"); |
| 2320 | MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>"); |
| 2321 | MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver"); |