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