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