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