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