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