| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * Copyright (c) 2016 MediaTek Inc. |
|---|
| 3 | 4 | * Author: Ming Hsiu Tsai <minghsiu.tsai@mediatek.com> |
|---|
| 4 | 5 | * Rick Chang <rick.chang@mediatek.com> |
|---|
| 5 | | - * |
|---|
| 6 | | - * This program is free software; you can redistribute it and/or modify |
|---|
| 7 | | - * it under the terms of the GNU General Public License version 2 as |
|---|
| 8 | | - * published by the Free Software Foundation. |
|---|
| 9 | | - * |
|---|
| 10 | | - * This program is distributed in the hope that it will be useful, |
|---|
| 11 | | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 12 | | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 13 | | - * GNU General Public License for more details. |
|---|
| 6 | + * Xia Jiang <xia.jiang@mediatek.com> |
|---|
| 14 | 7 | */ |
|---|
| 15 | 8 | |
|---|
| 16 | 9 | #include <linux/clk.h> |
|---|
| .. | .. |
|---|
| 31 | 24 | #include <media/videobuf2-dma-contig.h> |
|---|
| 32 | 25 | #include <soc/mediatek/smi.h> |
|---|
| 33 | 26 | |
|---|
| 34 | | -#include "mtk_jpeg_hw.h" |
|---|
| 27 | +#include "mtk_jpeg_enc_hw.h" |
|---|
| 28 | +#include "mtk_jpeg_dec_hw.h" |
|---|
| 35 | 29 | #include "mtk_jpeg_core.h" |
|---|
| 36 | | -#include "mtk_jpeg_parse.h" |
|---|
| 30 | +#include "mtk_jpeg_dec_parse.h" |
|---|
| 37 | 31 | |
|---|
| 38 | | -static struct mtk_jpeg_fmt mtk_jpeg_formats[] = { |
|---|
| 32 | +static struct mtk_jpeg_fmt mtk_jpeg_enc_formats[] = { |
|---|
| 39 | 33 | { |
|---|
| 40 | 34 | .fourcc = V4L2_PIX_FMT_JPEG, |
|---|
| 41 | 35 | .colplanes = 1, |
|---|
| 42 | | - .flags = MTK_JPEG_FMT_FLAG_DEC_OUTPUT, |
|---|
| 36 | + .flags = MTK_JPEG_FMT_FLAG_CAPTURE, |
|---|
| 37 | + }, |
|---|
| 38 | + { |
|---|
| 39 | + .fourcc = V4L2_PIX_FMT_NV12M, |
|---|
| 40 | + .hw_format = JPEG_ENC_YUV_FORMAT_NV12, |
|---|
| 41 | + .h_sample = {4, 4}, |
|---|
| 42 | + .v_sample = {4, 2}, |
|---|
| 43 | + .colplanes = 2, |
|---|
| 44 | + .h_align = 4, |
|---|
| 45 | + .v_align = 4, |
|---|
| 46 | + .flags = MTK_JPEG_FMT_FLAG_OUTPUT, |
|---|
| 47 | + }, |
|---|
| 48 | + { |
|---|
| 49 | + .fourcc = V4L2_PIX_FMT_NV21M, |
|---|
| 50 | + .hw_format = JEPG_ENC_YUV_FORMAT_NV21, |
|---|
| 51 | + .h_sample = {4, 4}, |
|---|
| 52 | + .v_sample = {4, 2}, |
|---|
| 53 | + .colplanes = 2, |
|---|
| 54 | + .h_align = 4, |
|---|
| 55 | + .v_align = 4, |
|---|
| 56 | + .flags = MTK_JPEG_FMT_FLAG_OUTPUT, |
|---|
| 57 | + }, |
|---|
| 58 | + { |
|---|
| 59 | + .fourcc = V4L2_PIX_FMT_YUYV, |
|---|
| 60 | + .hw_format = JPEG_ENC_YUV_FORMAT_YUYV, |
|---|
| 61 | + .h_sample = {8}, |
|---|
| 62 | + .v_sample = {4}, |
|---|
| 63 | + .colplanes = 1, |
|---|
| 64 | + .h_align = 5, |
|---|
| 65 | + .v_align = 3, |
|---|
| 66 | + .flags = MTK_JPEG_FMT_FLAG_OUTPUT, |
|---|
| 67 | + }, |
|---|
| 68 | + { |
|---|
| 69 | + .fourcc = V4L2_PIX_FMT_YVYU, |
|---|
| 70 | + .hw_format = JPEG_ENC_YUV_FORMAT_YVYU, |
|---|
| 71 | + .h_sample = {8}, |
|---|
| 72 | + .v_sample = {4}, |
|---|
| 73 | + .colplanes = 1, |
|---|
| 74 | + .h_align = 5, |
|---|
| 75 | + .v_align = 3, |
|---|
| 76 | + .flags = MTK_JPEG_FMT_FLAG_OUTPUT, |
|---|
| 77 | + }, |
|---|
| 78 | +}; |
|---|
| 79 | + |
|---|
| 80 | +static struct mtk_jpeg_fmt mtk_jpeg_dec_formats[] = { |
|---|
| 81 | + { |
|---|
| 82 | + .fourcc = V4L2_PIX_FMT_JPEG, |
|---|
| 83 | + .colplanes = 1, |
|---|
| 84 | + .flags = MTK_JPEG_FMT_FLAG_OUTPUT, |
|---|
| 43 | 85 | }, |
|---|
| 44 | 86 | { |
|---|
| 45 | 87 | .fourcc = V4L2_PIX_FMT_YUV420M, |
|---|
| .. | .. |
|---|
| 48 | 90 | .colplanes = 3, |
|---|
| 49 | 91 | .h_align = 5, |
|---|
| 50 | 92 | .v_align = 4, |
|---|
| 51 | | - .flags = MTK_JPEG_FMT_FLAG_DEC_CAPTURE, |
|---|
| 93 | + .flags = MTK_JPEG_FMT_FLAG_CAPTURE, |
|---|
| 52 | 94 | }, |
|---|
| 53 | 95 | { |
|---|
| 54 | 96 | .fourcc = V4L2_PIX_FMT_YUV422M, |
|---|
| .. | .. |
|---|
| 57 | 99 | .colplanes = 3, |
|---|
| 58 | 100 | .h_align = 5, |
|---|
| 59 | 101 | .v_align = 3, |
|---|
| 60 | | - .flags = MTK_JPEG_FMT_FLAG_DEC_CAPTURE, |
|---|
| 102 | + .flags = MTK_JPEG_FMT_FLAG_CAPTURE, |
|---|
| 61 | 103 | }, |
|---|
| 62 | 104 | }; |
|---|
| 63 | 105 | |
|---|
| 64 | | -#define MTK_JPEG_NUM_FORMATS ARRAY_SIZE(mtk_jpeg_formats) |
|---|
| 65 | | - |
|---|
| 66 | | -enum { |
|---|
| 67 | | - MTK_JPEG_BUF_FLAGS_INIT = 0, |
|---|
| 68 | | - MTK_JPEG_BUF_FLAGS_LAST_FRAME = 1, |
|---|
| 69 | | -}; |
|---|
| 106 | +#define MTK_JPEG_ENC_NUM_FORMATS ARRAY_SIZE(mtk_jpeg_enc_formats) |
|---|
| 107 | +#define MTK_JPEG_DEC_NUM_FORMATS ARRAY_SIZE(mtk_jpeg_dec_formats) |
|---|
| 70 | 108 | |
|---|
| 71 | 109 | struct mtk_jpeg_src_buf { |
|---|
| 72 | 110 | struct vb2_v4l2_buffer b; |
|---|
| 73 | 111 | struct list_head list; |
|---|
| 74 | | - int flags; |
|---|
| 75 | 112 | struct mtk_jpeg_dec_param dec_param; |
|---|
| 76 | 113 | }; |
|---|
| 77 | 114 | |
|---|
| 78 | 115 | static int debug; |
|---|
| 79 | 116 | module_param(debug, int, 0644); |
|---|
| 117 | + |
|---|
| 118 | +static inline struct mtk_jpeg_ctx *ctrl_to_ctx(struct v4l2_ctrl *ctrl) |
|---|
| 119 | +{ |
|---|
| 120 | + return container_of(ctrl->handler, struct mtk_jpeg_ctx, ctrl_hdl); |
|---|
| 121 | +} |
|---|
| 80 | 122 | |
|---|
| 81 | 123 | static inline struct mtk_jpeg_ctx *mtk_jpeg_fh_to_ctx(struct v4l2_fh *fh) |
|---|
| 82 | 124 | { |
|---|
| .. | .. |
|---|
| 94 | 136 | { |
|---|
| 95 | 137 | struct mtk_jpeg_dev *jpeg = video_drvdata(file); |
|---|
| 96 | 138 | |
|---|
| 97 | | - strlcpy(cap->driver, MTK_JPEG_NAME " decoder", sizeof(cap->driver)); |
|---|
| 98 | | - strlcpy(cap->card, MTK_JPEG_NAME " decoder", sizeof(cap->card)); |
|---|
| 139 | + strscpy(cap->driver, jpeg->variant->dev_name, sizeof(cap->driver)); |
|---|
| 140 | + strscpy(cap->card, jpeg->variant->dev_name, sizeof(cap->card)); |
|---|
| 99 | 141 | snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s", |
|---|
| 100 | 142 | dev_name(jpeg->dev)); |
|---|
| 143 | + |
|---|
| 144 | + return 0; |
|---|
| 145 | +} |
|---|
| 146 | + |
|---|
| 147 | +static int vidioc_jpeg_enc_s_ctrl(struct v4l2_ctrl *ctrl) |
|---|
| 148 | +{ |
|---|
| 149 | + struct mtk_jpeg_ctx *ctx = ctrl_to_ctx(ctrl); |
|---|
| 150 | + |
|---|
| 151 | + switch (ctrl->id) { |
|---|
| 152 | + case V4L2_CID_JPEG_RESTART_INTERVAL: |
|---|
| 153 | + ctx->restart_interval = ctrl->val; |
|---|
| 154 | + break; |
|---|
| 155 | + case V4L2_CID_JPEG_COMPRESSION_QUALITY: |
|---|
| 156 | + ctx->enc_quality = ctrl->val; |
|---|
| 157 | + break; |
|---|
| 158 | + case V4L2_CID_JPEG_ACTIVE_MARKER: |
|---|
| 159 | + ctx->enable_exif = ctrl->val & V4L2_JPEG_ACTIVE_MARKER_APP1; |
|---|
| 160 | + break; |
|---|
| 161 | + } |
|---|
| 162 | + |
|---|
| 163 | + return 0; |
|---|
| 164 | +} |
|---|
| 165 | + |
|---|
| 166 | +static const struct v4l2_ctrl_ops mtk_jpeg_enc_ctrl_ops = { |
|---|
| 167 | + .s_ctrl = vidioc_jpeg_enc_s_ctrl, |
|---|
| 168 | +}; |
|---|
| 169 | + |
|---|
| 170 | +static int mtk_jpeg_enc_ctrls_setup(struct mtk_jpeg_ctx *ctx) |
|---|
| 171 | +{ |
|---|
| 172 | + const struct v4l2_ctrl_ops *ops = &mtk_jpeg_enc_ctrl_ops; |
|---|
| 173 | + struct v4l2_ctrl_handler *handler = &ctx->ctrl_hdl; |
|---|
| 174 | + |
|---|
| 175 | + v4l2_ctrl_handler_init(handler, 3); |
|---|
| 176 | + |
|---|
| 177 | + v4l2_ctrl_new_std(handler, ops, V4L2_CID_JPEG_RESTART_INTERVAL, 0, 100, |
|---|
| 178 | + 1, 0); |
|---|
| 179 | + v4l2_ctrl_new_std(handler, ops, V4L2_CID_JPEG_COMPRESSION_QUALITY, 48, |
|---|
| 180 | + 100, 1, 90); |
|---|
| 181 | + v4l2_ctrl_new_std(handler, ops, V4L2_CID_JPEG_ACTIVE_MARKER, 0, |
|---|
| 182 | + V4L2_JPEG_ACTIVE_MARKER_APP1, 0, 0); |
|---|
| 183 | + |
|---|
| 184 | + if (handler->error) { |
|---|
| 185 | + v4l2_ctrl_handler_free(&ctx->ctrl_hdl); |
|---|
| 186 | + return handler->error; |
|---|
| 187 | + } |
|---|
| 188 | + |
|---|
| 189 | + v4l2_ctrl_handler_setup(&ctx->ctrl_hdl); |
|---|
| 101 | 190 | |
|---|
| 102 | 191 | return 0; |
|---|
| 103 | 192 | } |
|---|
| .. | .. |
|---|
| 126 | 215 | static int mtk_jpeg_enum_fmt_vid_cap(struct file *file, void *priv, |
|---|
| 127 | 216 | struct v4l2_fmtdesc *f) |
|---|
| 128 | 217 | { |
|---|
| 129 | | - return mtk_jpeg_enum_fmt(mtk_jpeg_formats, MTK_JPEG_NUM_FORMATS, f, |
|---|
| 130 | | - MTK_JPEG_FMT_FLAG_DEC_CAPTURE); |
|---|
| 218 | + struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv); |
|---|
| 219 | + struct mtk_jpeg_dev *jpeg = ctx->jpeg; |
|---|
| 220 | + |
|---|
| 221 | + return mtk_jpeg_enum_fmt(jpeg->variant->formats, |
|---|
| 222 | + jpeg->variant->num_formats, f, |
|---|
| 223 | + MTK_JPEG_FMT_FLAG_CAPTURE); |
|---|
| 131 | 224 | } |
|---|
| 132 | 225 | |
|---|
| 133 | 226 | static int mtk_jpeg_enum_fmt_vid_out(struct file *file, void *priv, |
|---|
| 134 | 227 | struct v4l2_fmtdesc *f) |
|---|
| 135 | 228 | { |
|---|
| 136 | | - return mtk_jpeg_enum_fmt(mtk_jpeg_formats, MTK_JPEG_NUM_FORMATS, f, |
|---|
| 137 | | - MTK_JPEG_FMT_FLAG_DEC_OUTPUT); |
|---|
| 229 | + struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv); |
|---|
| 230 | + struct mtk_jpeg_dev *jpeg = ctx->jpeg; |
|---|
| 231 | + |
|---|
| 232 | + return mtk_jpeg_enum_fmt(jpeg->variant->formats, |
|---|
| 233 | + jpeg->variant->num_formats, f, |
|---|
| 234 | + MTK_JPEG_FMT_FLAG_OUTPUT); |
|---|
| 138 | 235 | } |
|---|
| 139 | 236 | |
|---|
| 140 | 237 | static struct mtk_jpeg_q_data *mtk_jpeg_get_q_data(struct mtk_jpeg_ctx *ctx, |
|---|
| .. | .. |
|---|
| 145 | 242 | return &ctx->cap_q; |
|---|
| 146 | 243 | } |
|---|
| 147 | 244 | |
|---|
| 148 | | -static struct mtk_jpeg_fmt *mtk_jpeg_find_format(struct mtk_jpeg_ctx *ctx, |
|---|
| 149 | | - u32 pixelformat, |
|---|
| 150 | | - unsigned int fmt_type) |
|---|
| 245 | +static struct mtk_jpeg_fmt * |
|---|
| 246 | +mtk_jpeg_find_format(struct mtk_jpeg_fmt *mtk_jpeg_formats, int num_formats, |
|---|
| 247 | + u32 pixelformat, unsigned int fmt_type) |
|---|
| 151 | 248 | { |
|---|
| 152 | | - unsigned int k, fmt_flag; |
|---|
| 249 | + unsigned int k; |
|---|
| 250 | + struct mtk_jpeg_fmt *fmt; |
|---|
| 153 | 251 | |
|---|
| 154 | | - fmt_flag = (fmt_type == MTK_JPEG_FMT_TYPE_OUTPUT) ? |
|---|
| 155 | | - MTK_JPEG_FMT_FLAG_DEC_OUTPUT : |
|---|
| 156 | | - MTK_JPEG_FMT_FLAG_DEC_CAPTURE; |
|---|
| 252 | + for (k = 0; k < num_formats; k++) { |
|---|
| 253 | + fmt = &mtk_jpeg_formats[k]; |
|---|
| 157 | 254 | |
|---|
| 158 | | - for (k = 0; k < MTK_JPEG_NUM_FORMATS; k++) { |
|---|
| 159 | | - struct mtk_jpeg_fmt *fmt = &mtk_jpeg_formats[k]; |
|---|
| 160 | | - |
|---|
| 161 | | - if (fmt->fourcc == pixelformat && fmt->flags & fmt_flag) |
|---|
| 255 | + if (fmt->fourcc == pixelformat && fmt->flags & fmt_type) |
|---|
| 162 | 256 | return fmt; |
|---|
| 163 | 257 | } |
|---|
| 164 | 258 | |
|---|
| 165 | 259 | return NULL; |
|---|
| 166 | 260 | } |
|---|
| 167 | 261 | |
|---|
| 168 | | -static void mtk_jpeg_bound_align_image(u32 *w, unsigned int wmin, |
|---|
| 169 | | - unsigned int wmax, unsigned int walign, |
|---|
| 170 | | - u32 *h, unsigned int hmin, |
|---|
| 171 | | - unsigned int hmax, unsigned int halign) |
|---|
| 262 | +static int mtk_jpeg_try_fmt_mplane(struct v4l2_pix_format_mplane *pix_mp, |
|---|
| 263 | + struct mtk_jpeg_fmt *fmt) |
|---|
| 172 | 264 | { |
|---|
| 173 | | - int width, height, w_step, h_step; |
|---|
| 174 | | - |
|---|
| 175 | | - width = *w; |
|---|
| 176 | | - height = *h; |
|---|
| 177 | | - w_step = 1 << walign; |
|---|
| 178 | | - h_step = 1 << halign; |
|---|
| 179 | | - |
|---|
| 180 | | - v4l_bound_align_image(w, wmin, wmax, walign, h, hmin, hmax, halign, 0); |
|---|
| 181 | | - if (*w < width && (*w + w_step) <= wmax) |
|---|
| 182 | | - *w += w_step; |
|---|
| 183 | | - if (*h < height && (*h + h_step) <= hmax) |
|---|
| 184 | | - *h += h_step; |
|---|
| 185 | | -} |
|---|
| 186 | | - |
|---|
| 187 | | -static void mtk_jpeg_adjust_fmt_mplane(struct mtk_jpeg_ctx *ctx, |
|---|
| 188 | | - struct v4l2_format *f) |
|---|
| 189 | | -{ |
|---|
| 190 | | - struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp; |
|---|
| 191 | | - struct mtk_jpeg_q_data *q_data; |
|---|
| 192 | 265 | int i; |
|---|
| 193 | 266 | |
|---|
| 194 | | - q_data = mtk_jpeg_get_q_data(ctx, f->type); |
|---|
| 195 | | - |
|---|
| 196 | | - pix_mp->width = q_data->w; |
|---|
| 197 | | - pix_mp->height = q_data->h; |
|---|
| 198 | | - pix_mp->pixelformat = q_data->fmt->fourcc; |
|---|
| 199 | | - pix_mp->num_planes = q_data->fmt->colplanes; |
|---|
| 200 | | - |
|---|
| 201 | | - for (i = 0; i < pix_mp->num_planes; i++) { |
|---|
| 202 | | - pix_mp->plane_fmt[i].bytesperline = q_data->bytesperline[i]; |
|---|
| 203 | | - pix_mp->plane_fmt[i].sizeimage = q_data->sizeimage[i]; |
|---|
| 204 | | - } |
|---|
| 205 | | -} |
|---|
| 206 | | - |
|---|
| 207 | | -static int mtk_jpeg_try_fmt_mplane(struct v4l2_format *f, |
|---|
| 208 | | - struct mtk_jpeg_fmt *fmt, |
|---|
| 209 | | - struct mtk_jpeg_ctx *ctx, int q_type) |
|---|
| 210 | | -{ |
|---|
| 211 | | - struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp; |
|---|
| 212 | | - struct mtk_jpeg_dev *jpeg = ctx->jpeg; |
|---|
| 213 | | - int i; |
|---|
| 214 | | - |
|---|
| 215 | | - memset(pix_mp->reserved, 0, sizeof(pix_mp->reserved)); |
|---|
| 216 | 267 | pix_mp->field = V4L2_FIELD_NONE; |
|---|
| 217 | | - |
|---|
| 218 | | - if (ctx->state != MTK_JPEG_INIT) { |
|---|
| 219 | | - mtk_jpeg_adjust_fmt_mplane(ctx, f); |
|---|
| 220 | | - goto end; |
|---|
| 221 | | - } |
|---|
| 222 | 268 | |
|---|
| 223 | 269 | pix_mp->num_planes = fmt->colplanes; |
|---|
| 224 | 270 | pix_mp->pixelformat = fmt->fourcc; |
|---|
| 225 | 271 | |
|---|
| 226 | | - if (q_type == MTK_JPEG_FMT_TYPE_OUTPUT) { |
|---|
| 272 | + if (fmt->fourcc == V4L2_PIX_FMT_JPEG) { |
|---|
| 227 | 273 | struct v4l2_plane_pix_format *pfmt = &pix_mp->plane_fmt[0]; |
|---|
| 228 | 274 | |
|---|
| 229 | | - mtk_jpeg_bound_align_image(&pix_mp->width, MTK_JPEG_MIN_WIDTH, |
|---|
| 230 | | - MTK_JPEG_MAX_WIDTH, 0, |
|---|
| 231 | | - &pix_mp->height, MTK_JPEG_MIN_HEIGHT, |
|---|
| 232 | | - MTK_JPEG_MAX_HEIGHT, 0); |
|---|
| 275 | + pix_mp->height = clamp(pix_mp->height, MTK_JPEG_MIN_HEIGHT, |
|---|
| 276 | + MTK_JPEG_MAX_HEIGHT); |
|---|
| 277 | + pix_mp->width = clamp(pix_mp->width, MTK_JPEG_MIN_WIDTH, |
|---|
| 278 | + MTK_JPEG_MAX_WIDTH); |
|---|
| 233 | 279 | |
|---|
| 234 | | - memset(pfmt->reserved, 0, sizeof(pfmt->reserved)); |
|---|
| 235 | 280 | pfmt->bytesperline = 0; |
|---|
| 236 | 281 | /* Source size must be aligned to 128 */ |
|---|
| 237 | | - pfmt->sizeimage = mtk_jpeg_align(pfmt->sizeimage, 128); |
|---|
| 282 | + pfmt->sizeimage = round_up(pfmt->sizeimage, 128); |
|---|
| 238 | 283 | if (pfmt->sizeimage == 0) |
|---|
| 239 | 284 | pfmt->sizeimage = MTK_JPEG_DEFAULT_SIZEIMAGE; |
|---|
| 240 | | - goto end; |
|---|
| 285 | + return 0; |
|---|
| 241 | 286 | } |
|---|
| 242 | 287 | |
|---|
| 243 | | - /* type is MTK_JPEG_FMT_TYPE_CAPTURE */ |
|---|
| 244 | | - mtk_jpeg_bound_align_image(&pix_mp->width, MTK_JPEG_MIN_WIDTH, |
|---|
| 245 | | - MTK_JPEG_MAX_WIDTH, fmt->h_align, |
|---|
| 246 | | - &pix_mp->height, MTK_JPEG_MIN_HEIGHT, |
|---|
| 247 | | - MTK_JPEG_MAX_HEIGHT, fmt->v_align); |
|---|
| 288 | + /* other fourcc */ |
|---|
| 289 | + pix_mp->height = clamp(round_up(pix_mp->height, fmt->v_align), |
|---|
| 290 | + MTK_JPEG_MIN_HEIGHT, MTK_JPEG_MAX_HEIGHT); |
|---|
| 291 | + pix_mp->width = clamp(round_up(pix_mp->width, fmt->h_align), |
|---|
| 292 | + MTK_JPEG_MIN_WIDTH, MTK_JPEG_MAX_WIDTH); |
|---|
| 248 | 293 | |
|---|
| 249 | 294 | for (i = 0; i < fmt->colplanes; i++) { |
|---|
| 250 | 295 | struct v4l2_plane_pix_format *pfmt = &pix_mp->plane_fmt[i]; |
|---|
| 251 | 296 | u32 stride = pix_mp->width * fmt->h_sample[i] / 4; |
|---|
| 252 | 297 | u32 h = pix_mp->height * fmt->v_sample[i] / 4; |
|---|
| 253 | 298 | |
|---|
| 254 | | - memset(pfmt->reserved, 0, sizeof(pfmt->reserved)); |
|---|
| 255 | 299 | pfmt->bytesperline = stride; |
|---|
| 256 | 300 | pfmt->sizeimage = stride * h; |
|---|
| 257 | | - } |
|---|
| 258 | | -end: |
|---|
| 259 | | - v4l2_dbg(2, debug, &jpeg->v4l2_dev, "wxh:%ux%u\n", |
|---|
| 260 | | - pix_mp->width, pix_mp->height); |
|---|
| 261 | | - for (i = 0; i < pix_mp->num_planes; i++) { |
|---|
| 262 | | - v4l2_dbg(2, debug, &jpeg->v4l2_dev, |
|---|
| 263 | | - "plane[%d] bpl=%u, size=%u\n", |
|---|
| 264 | | - i, |
|---|
| 265 | | - pix_mp->plane_fmt[i].bytesperline, |
|---|
| 266 | | - pix_mp->plane_fmt[i].sizeimage); |
|---|
| 267 | 301 | } |
|---|
| 268 | 302 | return 0; |
|---|
| 269 | 303 | } |
|---|
| .. | .. |
|---|
| 284 | 318 | |
|---|
| 285 | 319 | q_data = mtk_jpeg_get_q_data(ctx, f->type); |
|---|
| 286 | 320 | |
|---|
| 287 | | - memset(pix_mp->reserved, 0, sizeof(pix_mp->reserved)); |
|---|
| 288 | | - pix_mp->width = q_data->w; |
|---|
| 289 | | - pix_mp->height = q_data->h; |
|---|
| 321 | + pix_mp->width = q_data->pix_mp.width; |
|---|
| 322 | + pix_mp->height = q_data->pix_mp.height; |
|---|
| 290 | 323 | pix_mp->field = V4L2_FIELD_NONE; |
|---|
| 291 | 324 | pix_mp->pixelformat = q_data->fmt->fourcc; |
|---|
| 292 | 325 | pix_mp->num_planes = q_data->fmt->colplanes; |
|---|
| 293 | | - pix_mp->colorspace = ctx->colorspace; |
|---|
| 294 | | - pix_mp->ycbcr_enc = ctx->ycbcr_enc; |
|---|
| 295 | | - pix_mp->xfer_func = ctx->xfer_func; |
|---|
| 296 | | - pix_mp->quantization = ctx->quantization; |
|---|
| 326 | + pix_mp->colorspace = q_data->pix_mp.colorspace; |
|---|
| 327 | + pix_mp->ycbcr_enc = q_data->pix_mp.ycbcr_enc; |
|---|
| 328 | + pix_mp->xfer_func = q_data->pix_mp.xfer_func; |
|---|
| 329 | + pix_mp->quantization = q_data->pix_mp.quantization; |
|---|
| 297 | 330 | |
|---|
| 298 | 331 | v4l2_dbg(1, debug, &jpeg->v4l2_dev, "(%d) g_fmt:%c%c%c%c wxh:%ux%u\n", |
|---|
| 299 | 332 | f->type, |
|---|
| .. | .. |
|---|
| 306 | 339 | for (i = 0; i < pix_mp->num_planes; i++) { |
|---|
| 307 | 340 | struct v4l2_plane_pix_format *pfmt = &pix_mp->plane_fmt[i]; |
|---|
| 308 | 341 | |
|---|
| 309 | | - pfmt->bytesperline = q_data->bytesperline[i]; |
|---|
| 310 | | - pfmt->sizeimage = q_data->sizeimage[i]; |
|---|
| 311 | | - memset(pfmt->reserved, 0, sizeof(pfmt->reserved)); |
|---|
| 342 | + pfmt->bytesperline = q_data->pix_mp.plane_fmt[i].bytesperline; |
|---|
| 343 | + pfmt->sizeimage = q_data->pix_mp.plane_fmt[i].sizeimage; |
|---|
| 312 | 344 | |
|---|
| 313 | 345 | v4l2_dbg(1, debug, &jpeg->v4l2_dev, |
|---|
| 314 | 346 | "plane[%d] bpl=%u, size=%u\n", |
|---|
| .. | .. |
|---|
| 323 | 355 | struct v4l2_format *f) |
|---|
| 324 | 356 | { |
|---|
| 325 | 357 | struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv); |
|---|
| 358 | + struct mtk_jpeg_dev *jpeg = ctx->jpeg; |
|---|
| 326 | 359 | struct mtk_jpeg_fmt *fmt; |
|---|
| 327 | 360 | |
|---|
| 328 | | - fmt = mtk_jpeg_find_format(ctx, f->fmt.pix_mp.pixelformat, |
|---|
| 329 | | - MTK_JPEG_FMT_TYPE_CAPTURE); |
|---|
| 361 | + fmt = mtk_jpeg_find_format(jpeg->variant->formats, |
|---|
| 362 | + jpeg->variant->num_formats, |
|---|
| 363 | + f->fmt.pix_mp.pixelformat, |
|---|
| 364 | + MTK_JPEG_FMT_FLAG_CAPTURE); |
|---|
| 330 | 365 | if (!fmt) |
|---|
| 331 | 366 | fmt = ctx->cap_q.fmt; |
|---|
| 332 | 367 | |
|---|
| .. | .. |
|---|
| 337 | 372 | (fmt->fourcc >> 16 & 0xff), |
|---|
| 338 | 373 | (fmt->fourcc >> 24 & 0xff)); |
|---|
| 339 | 374 | |
|---|
| 340 | | - return mtk_jpeg_try_fmt_mplane(f, fmt, ctx, MTK_JPEG_FMT_TYPE_CAPTURE); |
|---|
| 375 | + if (ctx->state != MTK_JPEG_INIT) { |
|---|
| 376 | + mtk_jpeg_g_fmt_vid_mplane(file, priv, f); |
|---|
| 377 | + return 0; |
|---|
| 378 | + } |
|---|
| 379 | + |
|---|
| 380 | + return mtk_jpeg_try_fmt_mplane(&f->fmt.pix_mp, fmt); |
|---|
| 341 | 381 | } |
|---|
| 342 | 382 | |
|---|
| 343 | 383 | static int mtk_jpeg_try_fmt_vid_out_mplane(struct file *file, void *priv, |
|---|
| 344 | 384 | struct v4l2_format *f) |
|---|
| 345 | 385 | { |
|---|
| 346 | 386 | struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv); |
|---|
| 387 | + struct mtk_jpeg_dev *jpeg = ctx->jpeg; |
|---|
| 347 | 388 | struct mtk_jpeg_fmt *fmt; |
|---|
| 348 | 389 | |
|---|
| 349 | | - fmt = mtk_jpeg_find_format(ctx, f->fmt.pix_mp.pixelformat, |
|---|
| 350 | | - MTK_JPEG_FMT_TYPE_OUTPUT); |
|---|
| 390 | + fmt = mtk_jpeg_find_format(jpeg->variant->formats, |
|---|
| 391 | + jpeg->variant->num_formats, |
|---|
| 392 | + f->fmt.pix_mp.pixelformat, |
|---|
| 393 | + MTK_JPEG_FMT_FLAG_OUTPUT); |
|---|
| 351 | 394 | if (!fmt) |
|---|
| 352 | 395 | fmt = ctx->out_q.fmt; |
|---|
| 353 | 396 | |
|---|
| .. | .. |
|---|
| 358 | 401 | (fmt->fourcc >> 16 & 0xff), |
|---|
| 359 | 402 | (fmt->fourcc >> 24 & 0xff)); |
|---|
| 360 | 403 | |
|---|
| 361 | | - return mtk_jpeg_try_fmt_mplane(f, fmt, ctx, MTK_JPEG_FMT_TYPE_OUTPUT); |
|---|
| 404 | + if (ctx->state != MTK_JPEG_INIT) { |
|---|
| 405 | + mtk_jpeg_g_fmt_vid_mplane(file, priv, f); |
|---|
| 406 | + return 0; |
|---|
| 407 | + } |
|---|
| 408 | + |
|---|
| 409 | + return mtk_jpeg_try_fmt_mplane(&f->fmt.pix_mp, fmt); |
|---|
| 362 | 410 | } |
|---|
| 363 | 411 | |
|---|
| 364 | 412 | static int mtk_jpeg_s_fmt_mplane(struct mtk_jpeg_ctx *ctx, |
|---|
| 365 | | - struct v4l2_format *f) |
|---|
| 413 | + struct v4l2_format *f, unsigned int fmt_type) |
|---|
| 366 | 414 | { |
|---|
| 367 | 415 | struct vb2_queue *vq; |
|---|
| 368 | 416 | struct mtk_jpeg_q_data *q_data = NULL; |
|---|
| 369 | 417 | struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp; |
|---|
| 370 | 418 | struct mtk_jpeg_dev *jpeg = ctx->jpeg; |
|---|
| 371 | | - unsigned int f_type; |
|---|
| 372 | 419 | int i; |
|---|
| 373 | 420 | |
|---|
| 374 | 421 | vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type); |
|---|
| .. | .. |
|---|
| 382 | 429 | return -EBUSY; |
|---|
| 383 | 430 | } |
|---|
| 384 | 431 | |
|---|
| 385 | | - f_type = V4L2_TYPE_IS_OUTPUT(f->type) ? |
|---|
| 386 | | - MTK_JPEG_FMT_TYPE_OUTPUT : MTK_JPEG_FMT_TYPE_CAPTURE; |
|---|
| 387 | | - |
|---|
| 388 | | - q_data->fmt = mtk_jpeg_find_format(ctx, pix_mp->pixelformat, f_type); |
|---|
| 389 | | - q_data->w = pix_mp->width; |
|---|
| 390 | | - q_data->h = pix_mp->height; |
|---|
| 391 | | - ctx->colorspace = pix_mp->colorspace; |
|---|
| 392 | | - ctx->ycbcr_enc = pix_mp->ycbcr_enc; |
|---|
| 393 | | - ctx->xfer_func = pix_mp->xfer_func; |
|---|
| 394 | | - ctx->quantization = pix_mp->quantization; |
|---|
| 432 | + q_data->fmt = mtk_jpeg_find_format(jpeg->variant->formats, |
|---|
| 433 | + jpeg->variant->num_formats, |
|---|
| 434 | + pix_mp->pixelformat, fmt_type); |
|---|
| 435 | + q_data->pix_mp.width = pix_mp->width; |
|---|
| 436 | + q_data->pix_mp.height = pix_mp->height; |
|---|
| 437 | + q_data->enc_crop_rect.width = pix_mp->width; |
|---|
| 438 | + q_data->enc_crop_rect.height = pix_mp->height; |
|---|
| 439 | + q_data->pix_mp.colorspace = V4L2_COLORSPACE_SRGB; |
|---|
| 440 | + q_data->pix_mp.ycbcr_enc = V4L2_YCBCR_ENC_601; |
|---|
| 441 | + q_data->pix_mp.xfer_func = V4L2_XFER_FUNC_SRGB; |
|---|
| 442 | + q_data->pix_mp.quantization = V4L2_QUANTIZATION_FULL_RANGE; |
|---|
| 395 | 443 | |
|---|
| 396 | 444 | v4l2_dbg(1, debug, &jpeg->v4l2_dev, "(%d) s_fmt:%c%c%c%c wxh:%ux%u\n", |
|---|
| 397 | 445 | f->type, |
|---|
| .. | .. |
|---|
| 399 | 447 | (q_data->fmt->fourcc >> 8 & 0xff), |
|---|
| 400 | 448 | (q_data->fmt->fourcc >> 16 & 0xff), |
|---|
| 401 | 449 | (q_data->fmt->fourcc >> 24 & 0xff), |
|---|
| 402 | | - q_data->w, q_data->h); |
|---|
| 450 | + q_data->pix_mp.width, q_data->pix_mp.height); |
|---|
| 403 | 451 | |
|---|
| 404 | 452 | for (i = 0; i < q_data->fmt->colplanes; i++) { |
|---|
| 405 | | - q_data->bytesperline[i] = pix_mp->plane_fmt[i].bytesperline; |
|---|
| 406 | | - q_data->sizeimage[i] = pix_mp->plane_fmt[i].sizeimage; |
|---|
| 453 | + q_data->pix_mp.plane_fmt[i].bytesperline = |
|---|
| 454 | + pix_mp->plane_fmt[i].bytesperline; |
|---|
| 455 | + q_data->pix_mp.plane_fmt[i].sizeimage = |
|---|
| 456 | + pix_mp->plane_fmt[i].sizeimage; |
|---|
| 407 | 457 | |
|---|
| 408 | 458 | v4l2_dbg(1, debug, &jpeg->v4l2_dev, |
|---|
| 409 | 459 | "plane[%d] bpl=%u, size=%u\n", |
|---|
| 410 | | - i, q_data->bytesperline[i], q_data->sizeimage[i]); |
|---|
| 460 | + i, q_data->pix_mp.plane_fmt[i].bytesperline, |
|---|
| 461 | + q_data->pix_mp.plane_fmt[i].sizeimage); |
|---|
| 411 | 462 | } |
|---|
| 412 | 463 | |
|---|
| 413 | 464 | return 0; |
|---|
| .. | .. |
|---|
| 422 | 473 | if (ret) |
|---|
| 423 | 474 | return ret; |
|---|
| 424 | 475 | |
|---|
| 425 | | - return mtk_jpeg_s_fmt_mplane(mtk_jpeg_fh_to_ctx(priv), f); |
|---|
| 476 | + return mtk_jpeg_s_fmt_mplane(mtk_jpeg_fh_to_ctx(priv), f, |
|---|
| 477 | + MTK_JPEG_FMT_FLAG_OUTPUT); |
|---|
| 426 | 478 | } |
|---|
| 427 | 479 | |
|---|
| 428 | 480 | static int mtk_jpeg_s_fmt_vid_cap_mplane(struct file *file, void *priv, |
|---|
| .. | .. |
|---|
| 434 | 486 | if (ret) |
|---|
| 435 | 487 | return ret; |
|---|
| 436 | 488 | |
|---|
| 437 | | - return mtk_jpeg_s_fmt_mplane(mtk_jpeg_fh_to_ctx(priv), f); |
|---|
| 489 | + return mtk_jpeg_s_fmt_mplane(mtk_jpeg_fh_to_ctx(priv), f, |
|---|
| 490 | + MTK_JPEG_FMT_FLAG_CAPTURE); |
|---|
| 438 | 491 | } |
|---|
| 439 | 492 | |
|---|
| 440 | 493 | static void mtk_jpeg_queue_src_chg_event(struct mtk_jpeg_ctx *ctx) |
|---|
| .. | .. |
|---|
| 454 | 507 | switch (sub->type) { |
|---|
| 455 | 508 | case V4L2_EVENT_SOURCE_CHANGE: |
|---|
| 456 | 509 | return v4l2_src_change_event_subscribe(fh, sub); |
|---|
| 510 | + } |
|---|
| 511 | + |
|---|
| 512 | + return v4l2_ctrl_subscribe_event(fh, sub); |
|---|
| 513 | +} |
|---|
| 514 | + |
|---|
| 515 | +static int mtk_jpeg_enc_g_selection(struct file *file, void *priv, |
|---|
| 516 | + struct v4l2_selection *s) |
|---|
| 517 | +{ |
|---|
| 518 | + struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv); |
|---|
| 519 | + |
|---|
| 520 | + if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) |
|---|
| 521 | + return -EINVAL; |
|---|
| 522 | + |
|---|
| 523 | + switch (s->target) { |
|---|
| 524 | + case V4L2_SEL_TGT_CROP: |
|---|
| 525 | + s->r = ctx->out_q.enc_crop_rect; |
|---|
| 526 | + break; |
|---|
| 527 | + case V4L2_SEL_TGT_CROP_BOUNDS: |
|---|
| 528 | + case V4L2_SEL_TGT_CROP_DEFAULT: |
|---|
| 529 | + s->r.width = ctx->out_q.pix_mp.width; |
|---|
| 530 | + s->r.height = ctx->out_q.pix_mp.height; |
|---|
| 531 | + s->r.left = 0; |
|---|
| 532 | + s->r.top = 0; |
|---|
| 533 | + break; |
|---|
| 457 | 534 | default: |
|---|
| 458 | 535 | return -EINVAL; |
|---|
| 459 | 536 | } |
|---|
| 537 | + return 0; |
|---|
| 460 | 538 | } |
|---|
| 461 | 539 | |
|---|
| 462 | | -static int mtk_jpeg_g_selection(struct file *file, void *priv, |
|---|
| 463 | | - struct v4l2_selection *s) |
|---|
| 540 | +static int mtk_jpeg_dec_g_selection(struct file *file, void *priv, |
|---|
| 541 | + struct v4l2_selection *s) |
|---|
| 464 | 542 | { |
|---|
| 465 | 543 | struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv); |
|---|
| 466 | 544 | |
|---|
| .. | .. |
|---|
| 470 | 548 | switch (s->target) { |
|---|
| 471 | 549 | case V4L2_SEL_TGT_COMPOSE: |
|---|
| 472 | 550 | case V4L2_SEL_TGT_COMPOSE_DEFAULT: |
|---|
| 473 | | - s->r.width = ctx->out_q.w; |
|---|
| 474 | | - s->r.height = ctx->out_q.h; |
|---|
| 551 | + s->r.width = ctx->out_q.pix_mp.width; |
|---|
| 552 | + s->r.height = ctx->out_q.pix_mp.height; |
|---|
| 475 | 553 | s->r.left = 0; |
|---|
| 476 | 554 | s->r.top = 0; |
|---|
| 477 | 555 | break; |
|---|
| 478 | 556 | case V4L2_SEL_TGT_COMPOSE_BOUNDS: |
|---|
| 479 | 557 | case V4L2_SEL_TGT_COMPOSE_PADDED: |
|---|
| 480 | | - s->r.width = ctx->cap_q.w; |
|---|
| 481 | | - s->r.height = ctx->cap_q.h; |
|---|
| 558 | + s->r.width = ctx->cap_q.pix_mp.width; |
|---|
| 559 | + s->r.height = ctx->cap_q.pix_mp.height; |
|---|
| 482 | 560 | s->r.left = 0; |
|---|
| 483 | 561 | s->r.top = 0; |
|---|
| 484 | 562 | break; |
|---|
| .. | .. |
|---|
| 488 | 566 | return 0; |
|---|
| 489 | 567 | } |
|---|
| 490 | 568 | |
|---|
| 491 | | -static int mtk_jpeg_s_selection(struct file *file, void *priv, |
|---|
| 492 | | - struct v4l2_selection *s) |
|---|
| 569 | +static int mtk_jpeg_enc_s_selection(struct file *file, void *priv, |
|---|
| 570 | + struct v4l2_selection *s) |
|---|
| 493 | 571 | { |
|---|
| 494 | 572 | struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv); |
|---|
| 495 | 573 | |
|---|
| 496 | | - if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
|---|
| 574 | + if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) |
|---|
| 497 | 575 | return -EINVAL; |
|---|
| 498 | 576 | |
|---|
| 499 | 577 | switch (s->target) { |
|---|
| 500 | | - case V4L2_SEL_TGT_COMPOSE: |
|---|
| 578 | + case V4L2_SEL_TGT_CROP: |
|---|
| 501 | 579 | s->r.left = 0; |
|---|
| 502 | 580 | s->r.top = 0; |
|---|
| 503 | | - s->r.width = ctx->out_q.w; |
|---|
| 504 | | - s->r.height = ctx->out_q.h; |
|---|
| 581 | + s->r.width = min(s->r.width, ctx->out_q.pix_mp.width); |
|---|
| 582 | + s->r.height = min(s->r.height, ctx->out_q.pix_mp.height); |
|---|
| 583 | + ctx->out_q.enc_crop_rect = s->r; |
|---|
| 505 | 584 | break; |
|---|
| 506 | 585 | default: |
|---|
| 507 | 586 | return -EINVAL; |
|---|
| 508 | 587 | } |
|---|
| 588 | + |
|---|
| 509 | 589 | return 0; |
|---|
| 510 | 590 | } |
|---|
| 511 | 591 | |
|---|
| 512 | | -static int mtk_jpeg_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf) |
|---|
| 513 | | -{ |
|---|
| 514 | | - struct v4l2_fh *fh = file->private_data; |
|---|
| 515 | | - struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv); |
|---|
| 516 | | - struct vb2_queue *vq; |
|---|
| 517 | | - struct vb2_buffer *vb; |
|---|
| 518 | | - struct mtk_jpeg_src_buf *jpeg_src_buf; |
|---|
| 519 | | - |
|---|
| 520 | | - if (buf->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) |
|---|
| 521 | | - goto end; |
|---|
| 522 | | - |
|---|
| 523 | | - vq = v4l2_m2m_get_vq(fh->m2m_ctx, buf->type); |
|---|
| 524 | | - if (buf->index >= vq->num_buffers) { |
|---|
| 525 | | - dev_err(ctx->jpeg->dev, "buffer index out of range\n"); |
|---|
| 526 | | - return -EINVAL; |
|---|
| 527 | | - } |
|---|
| 528 | | - |
|---|
| 529 | | - vb = vq->bufs[buf->index]; |
|---|
| 530 | | - jpeg_src_buf = mtk_jpeg_vb2_to_srcbuf(vb); |
|---|
| 531 | | - jpeg_src_buf->flags = (buf->m.planes[0].bytesused == 0) ? |
|---|
| 532 | | - MTK_JPEG_BUF_FLAGS_LAST_FRAME : MTK_JPEG_BUF_FLAGS_INIT; |
|---|
| 533 | | -end: |
|---|
| 534 | | - return v4l2_m2m_qbuf(file, fh->m2m_ctx, buf); |
|---|
| 535 | | -} |
|---|
| 536 | | - |
|---|
| 537 | | -static const struct v4l2_ioctl_ops mtk_jpeg_ioctl_ops = { |
|---|
| 592 | +static const struct v4l2_ioctl_ops mtk_jpeg_enc_ioctl_ops = { |
|---|
| 538 | 593 | .vidioc_querycap = mtk_jpeg_querycap, |
|---|
| 539 | | - .vidioc_enum_fmt_vid_cap_mplane = mtk_jpeg_enum_fmt_vid_cap, |
|---|
| 540 | | - .vidioc_enum_fmt_vid_out_mplane = mtk_jpeg_enum_fmt_vid_out, |
|---|
| 594 | + .vidioc_enum_fmt_vid_cap = mtk_jpeg_enum_fmt_vid_cap, |
|---|
| 595 | + .vidioc_enum_fmt_vid_out = mtk_jpeg_enum_fmt_vid_out, |
|---|
| 541 | 596 | .vidioc_try_fmt_vid_cap_mplane = mtk_jpeg_try_fmt_vid_cap_mplane, |
|---|
| 542 | 597 | .vidioc_try_fmt_vid_out_mplane = mtk_jpeg_try_fmt_vid_out_mplane, |
|---|
| 543 | 598 | .vidioc_g_fmt_vid_cap_mplane = mtk_jpeg_g_fmt_vid_mplane, |
|---|
| 544 | 599 | .vidioc_g_fmt_vid_out_mplane = mtk_jpeg_g_fmt_vid_mplane, |
|---|
| 545 | 600 | .vidioc_s_fmt_vid_cap_mplane = mtk_jpeg_s_fmt_vid_cap_mplane, |
|---|
| 546 | 601 | .vidioc_s_fmt_vid_out_mplane = mtk_jpeg_s_fmt_vid_out_mplane, |
|---|
| 547 | | - .vidioc_qbuf = mtk_jpeg_qbuf, |
|---|
| 602 | + .vidioc_qbuf = v4l2_m2m_ioctl_qbuf, |
|---|
| 548 | 603 | .vidioc_subscribe_event = mtk_jpeg_subscribe_event, |
|---|
| 549 | | - .vidioc_g_selection = mtk_jpeg_g_selection, |
|---|
| 550 | | - .vidioc_s_selection = mtk_jpeg_s_selection, |
|---|
| 604 | + .vidioc_g_selection = mtk_jpeg_enc_g_selection, |
|---|
| 605 | + .vidioc_s_selection = mtk_jpeg_enc_s_selection, |
|---|
| 606 | + |
|---|
| 607 | + .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs, |
|---|
| 608 | + .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf, |
|---|
| 609 | + .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs, |
|---|
| 610 | + .vidioc_querybuf = v4l2_m2m_ioctl_querybuf, |
|---|
| 611 | + .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf, |
|---|
| 612 | + .vidioc_expbuf = v4l2_m2m_ioctl_expbuf, |
|---|
| 613 | + .vidioc_streamon = v4l2_m2m_ioctl_streamon, |
|---|
| 614 | + .vidioc_streamoff = v4l2_m2m_ioctl_streamoff, |
|---|
| 615 | + |
|---|
| 616 | + .vidioc_unsubscribe_event = v4l2_event_unsubscribe, |
|---|
| 617 | +}; |
|---|
| 618 | + |
|---|
| 619 | +static const struct v4l2_ioctl_ops mtk_jpeg_dec_ioctl_ops = { |
|---|
| 620 | + .vidioc_querycap = mtk_jpeg_querycap, |
|---|
| 621 | + .vidioc_enum_fmt_vid_cap = mtk_jpeg_enum_fmt_vid_cap, |
|---|
| 622 | + .vidioc_enum_fmt_vid_out = mtk_jpeg_enum_fmt_vid_out, |
|---|
| 623 | + .vidioc_try_fmt_vid_cap_mplane = mtk_jpeg_try_fmt_vid_cap_mplane, |
|---|
| 624 | + .vidioc_try_fmt_vid_out_mplane = mtk_jpeg_try_fmt_vid_out_mplane, |
|---|
| 625 | + .vidioc_g_fmt_vid_cap_mplane = mtk_jpeg_g_fmt_vid_mplane, |
|---|
| 626 | + .vidioc_g_fmt_vid_out_mplane = mtk_jpeg_g_fmt_vid_mplane, |
|---|
| 627 | + .vidioc_s_fmt_vid_cap_mplane = mtk_jpeg_s_fmt_vid_cap_mplane, |
|---|
| 628 | + .vidioc_s_fmt_vid_out_mplane = mtk_jpeg_s_fmt_vid_out_mplane, |
|---|
| 629 | + .vidioc_qbuf = v4l2_m2m_ioctl_qbuf, |
|---|
| 630 | + .vidioc_subscribe_event = mtk_jpeg_subscribe_event, |
|---|
| 631 | + .vidioc_g_selection = mtk_jpeg_dec_g_selection, |
|---|
| 551 | 632 | |
|---|
| 552 | 633 | .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs, |
|---|
| 553 | 634 | .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf, |
|---|
| .. | .. |
|---|
| 581 | 662 | |
|---|
| 582 | 663 | if (*num_planes) { |
|---|
| 583 | 664 | for (i = 0; i < *num_planes; i++) |
|---|
| 584 | | - if (sizes[i] < q_data->sizeimage[i]) |
|---|
| 665 | + if (sizes[i] < q_data->pix_mp.plane_fmt[i].sizeimage) |
|---|
| 585 | 666 | return -EINVAL; |
|---|
| 586 | 667 | return 0; |
|---|
| 587 | 668 | } |
|---|
| 588 | 669 | |
|---|
| 589 | 670 | *num_planes = q_data->fmt->colplanes; |
|---|
| 590 | 671 | for (i = 0; i < q_data->fmt->colplanes; i++) { |
|---|
| 591 | | - sizes[i] = q_data->sizeimage[i]; |
|---|
| 672 | + sizes[i] = q_data->pix_mp.plane_fmt[i].sizeimage; |
|---|
| 592 | 673 | v4l2_dbg(1, debug, &jpeg->v4l2_dev, "sizeimage[%d]=%u\n", |
|---|
| 593 | 674 | i, sizes[i]); |
|---|
| 594 | 675 | } |
|---|
| .. | .. |
|---|
| 600 | 681 | { |
|---|
| 601 | 682 | struct mtk_jpeg_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); |
|---|
| 602 | 683 | struct mtk_jpeg_q_data *q_data = NULL; |
|---|
| 684 | + struct v4l2_plane_pix_format plane_fmt = {}; |
|---|
| 603 | 685 | int i; |
|---|
| 604 | 686 | |
|---|
| 605 | 687 | q_data = mtk_jpeg_get_q_data(ctx, vb->vb2_queue->type); |
|---|
| 606 | 688 | if (!q_data) |
|---|
| 607 | 689 | return -EINVAL; |
|---|
| 608 | 690 | |
|---|
| 609 | | - for (i = 0; i < q_data->fmt->colplanes; i++) |
|---|
| 610 | | - vb2_set_plane_payload(vb, i, q_data->sizeimage[i]); |
|---|
| 691 | + for (i = 0; i < q_data->fmt->colplanes; i++) { |
|---|
| 692 | + plane_fmt = q_data->pix_mp.plane_fmt[i]; |
|---|
| 693 | + if (ctx->enable_exif && |
|---|
| 694 | + q_data->fmt->fourcc == V4L2_PIX_FMT_JPEG) |
|---|
| 695 | + vb2_set_plane_payload(vb, i, plane_fmt.sizeimage + |
|---|
| 696 | + MTK_JPEG_MAX_EXIF_SIZE); |
|---|
| 697 | + else |
|---|
| 698 | + vb2_set_plane_payload(vb, i, plane_fmt.sizeimage); |
|---|
| 699 | + } |
|---|
| 611 | 700 | |
|---|
| 612 | 701 | return 0; |
|---|
| 613 | 702 | } |
|---|
| .. | .. |
|---|
| 619 | 708 | struct mtk_jpeg_q_data *q_data; |
|---|
| 620 | 709 | |
|---|
| 621 | 710 | q_data = &ctx->out_q; |
|---|
| 622 | | - if (q_data->w != param->pic_w || q_data->h != param->pic_h) { |
|---|
| 711 | + if (q_data->pix_mp.width != param->pic_w || |
|---|
| 712 | + q_data->pix_mp.height != param->pic_h) { |
|---|
| 623 | 713 | v4l2_dbg(1, debug, &jpeg->v4l2_dev, "Picture size change\n"); |
|---|
| 624 | 714 | return true; |
|---|
| 625 | 715 | } |
|---|
| 626 | 716 | |
|---|
| 627 | 717 | q_data = &ctx->cap_q; |
|---|
| 628 | | - if (q_data->fmt != mtk_jpeg_find_format(ctx, param->dst_fourcc, |
|---|
| 629 | | - MTK_JPEG_FMT_TYPE_CAPTURE)) { |
|---|
| 718 | + if (q_data->fmt != |
|---|
| 719 | + mtk_jpeg_find_format(jpeg->variant->formats, |
|---|
| 720 | + jpeg->variant->num_formats, param->dst_fourcc, |
|---|
| 721 | + MTK_JPEG_FMT_FLAG_CAPTURE)) { |
|---|
| 630 | 722 | v4l2_dbg(1, debug, &jpeg->v4l2_dev, "format change\n"); |
|---|
| 631 | 723 | return true; |
|---|
| 632 | 724 | } |
|---|
| .. | .. |
|---|
| 641 | 733 | int i; |
|---|
| 642 | 734 | |
|---|
| 643 | 735 | q_data = &ctx->out_q; |
|---|
| 644 | | - q_data->w = param->pic_w; |
|---|
| 645 | | - q_data->h = param->pic_h; |
|---|
| 736 | + q_data->pix_mp.width = param->pic_w; |
|---|
| 737 | + q_data->pix_mp.height = param->pic_h; |
|---|
| 646 | 738 | |
|---|
| 647 | 739 | q_data = &ctx->cap_q; |
|---|
| 648 | | - q_data->w = param->dec_w; |
|---|
| 649 | | - q_data->h = param->dec_h; |
|---|
| 650 | | - q_data->fmt = mtk_jpeg_find_format(ctx, |
|---|
| 740 | + q_data->pix_mp.width = param->dec_w; |
|---|
| 741 | + q_data->pix_mp.height = param->dec_h; |
|---|
| 742 | + q_data->fmt = mtk_jpeg_find_format(jpeg->variant->formats, |
|---|
| 743 | + jpeg->variant->num_formats, |
|---|
| 651 | 744 | param->dst_fourcc, |
|---|
| 652 | | - MTK_JPEG_FMT_TYPE_CAPTURE); |
|---|
| 745 | + MTK_JPEG_FMT_FLAG_CAPTURE); |
|---|
| 653 | 746 | |
|---|
| 654 | 747 | for (i = 0; i < q_data->fmt->colplanes; i++) { |
|---|
| 655 | | - q_data->bytesperline[i] = param->mem_stride[i]; |
|---|
| 656 | | - q_data->sizeimage[i] = param->comp_size[i]; |
|---|
| 748 | + q_data->pix_mp.plane_fmt[i].bytesperline = param->mem_stride[i]; |
|---|
| 749 | + q_data->pix_mp.plane_fmt[i].sizeimage = param->comp_size[i]; |
|---|
| 657 | 750 | } |
|---|
| 658 | 751 | |
|---|
| 659 | 752 | v4l2_dbg(1, debug, &jpeg->v4l2_dev, |
|---|
| .. | .. |
|---|
| 666 | 759 | param->dec_w, param->dec_h); |
|---|
| 667 | 760 | } |
|---|
| 668 | 761 | |
|---|
| 669 | | -static void mtk_jpeg_buf_queue(struct vb2_buffer *vb) |
|---|
| 762 | +static void mtk_jpeg_enc_buf_queue(struct vb2_buffer *vb) |
|---|
| 763 | +{ |
|---|
| 764 | + struct mtk_jpeg_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); |
|---|
| 765 | + struct mtk_jpeg_dev *jpeg = ctx->jpeg; |
|---|
| 766 | + |
|---|
| 767 | + v4l2_dbg(2, debug, &jpeg->v4l2_dev, "(%d) buf_q id=%d, vb=%p\n", |
|---|
| 768 | + vb->vb2_queue->type, vb->index, vb); |
|---|
| 769 | + |
|---|
| 770 | + v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, to_vb2_v4l2_buffer(vb)); |
|---|
| 771 | +} |
|---|
| 772 | + |
|---|
| 773 | +static void mtk_jpeg_dec_buf_queue(struct vb2_buffer *vb) |
|---|
| 670 | 774 | { |
|---|
| 671 | 775 | struct mtk_jpeg_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); |
|---|
| 672 | 776 | struct mtk_jpeg_dec_param *param; |
|---|
| .. | .. |
|---|
| 684 | 788 | param = &jpeg_src_buf->dec_param; |
|---|
| 685 | 789 | memset(param, 0, sizeof(*param)); |
|---|
| 686 | 790 | |
|---|
| 687 | | - if (jpeg_src_buf->flags & MTK_JPEG_BUF_FLAGS_LAST_FRAME) { |
|---|
| 688 | | - v4l2_dbg(1, debug, &jpeg->v4l2_dev, "Got eos\n"); |
|---|
| 689 | | - goto end; |
|---|
| 690 | | - } |
|---|
| 691 | 791 | header_valid = mtk_jpeg_parse(param, (u8 *)vb2_plane_vaddr(vb, 0), |
|---|
| 692 | 792 | vb2_get_plane_payload(vb, 0)); |
|---|
| 693 | 793 | if (!header_valid) { |
|---|
| .. | .. |
|---|
| 718 | 818 | return v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx); |
|---|
| 719 | 819 | } |
|---|
| 720 | 820 | |
|---|
| 721 | | -static int mtk_jpeg_start_streaming(struct vb2_queue *q, unsigned int count) |
|---|
| 821 | +static void mtk_jpeg_enc_stop_streaming(struct vb2_queue *q) |
|---|
| 722 | 822 | { |
|---|
| 723 | 823 | struct mtk_jpeg_ctx *ctx = vb2_get_drv_priv(q); |
|---|
| 724 | 824 | struct vb2_v4l2_buffer *vb; |
|---|
| 725 | | - int ret = 0; |
|---|
| 726 | 825 | |
|---|
| 727 | | - ret = pm_runtime_get_sync(ctx->jpeg->dev); |
|---|
| 728 | | - if (ret < 0) |
|---|
| 729 | | - goto err; |
|---|
| 730 | | - |
|---|
| 731 | | - return 0; |
|---|
| 732 | | -err: |
|---|
| 733 | 826 | while ((vb = mtk_jpeg_buf_remove(ctx, q->type))) |
|---|
| 734 | | - v4l2_m2m_buf_done(vb, VB2_BUF_STATE_QUEUED); |
|---|
| 735 | | - return ret; |
|---|
| 827 | + v4l2_m2m_buf_done(vb, VB2_BUF_STATE_ERROR); |
|---|
| 736 | 828 | } |
|---|
| 737 | 829 | |
|---|
| 738 | | -static void mtk_jpeg_stop_streaming(struct vb2_queue *q) |
|---|
| 830 | +static void mtk_jpeg_dec_stop_streaming(struct vb2_queue *q) |
|---|
| 739 | 831 | { |
|---|
| 740 | 832 | struct mtk_jpeg_ctx *ctx = vb2_get_drv_priv(q); |
|---|
| 741 | 833 | struct vb2_v4l2_buffer *vb; |
|---|
| .. | .. |
|---|
| 746 | 838 | * subsampling. Update capture queue when the stream is off. |
|---|
| 747 | 839 | */ |
|---|
| 748 | 840 | if (ctx->state == MTK_JPEG_SOURCE_CHANGE && |
|---|
| 749 | | - !V4L2_TYPE_IS_OUTPUT(q->type)) { |
|---|
| 841 | + V4L2_TYPE_IS_CAPTURE(q->type)) { |
|---|
| 750 | 842 | struct mtk_jpeg_src_buf *src_buf; |
|---|
| 751 | 843 | |
|---|
| 752 | 844 | vb = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx); |
|---|
| .. | .. |
|---|
| 759 | 851 | |
|---|
| 760 | 852 | while ((vb = mtk_jpeg_buf_remove(ctx, q->type))) |
|---|
| 761 | 853 | v4l2_m2m_buf_done(vb, VB2_BUF_STATE_ERROR); |
|---|
| 762 | | - |
|---|
| 763 | | - pm_runtime_put_sync(ctx->jpeg->dev); |
|---|
| 764 | 854 | } |
|---|
| 765 | 855 | |
|---|
| 766 | | -static const struct vb2_ops mtk_jpeg_qops = { |
|---|
| 856 | +static const struct vb2_ops mtk_jpeg_dec_qops = { |
|---|
| 767 | 857 | .queue_setup = mtk_jpeg_queue_setup, |
|---|
| 768 | 858 | .buf_prepare = mtk_jpeg_buf_prepare, |
|---|
| 769 | | - .buf_queue = mtk_jpeg_buf_queue, |
|---|
| 859 | + .buf_queue = mtk_jpeg_dec_buf_queue, |
|---|
| 770 | 860 | .wait_prepare = vb2_ops_wait_prepare, |
|---|
| 771 | 861 | .wait_finish = vb2_ops_wait_finish, |
|---|
| 772 | | - .start_streaming = mtk_jpeg_start_streaming, |
|---|
| 773 | | - .stop_streaming = mtk_jpeg_stop_streaming, |
|---|
| 862 | + .stop_streaming = mtk_jpeg_dec_stop_streaming, |
|---|
| 863 | +}; |
|---|
| 864 | + |
|---|
| 865 | +static const struct vb2_ops mtk_jpeg_enc_qops = { |
|---|
| 866 | + .queue_setup = mtk_jpeg_queue_setup, |
|---|
| 867 | + .buf_prepare = mtk_jpeg_buf_prepare, |
|---|
| 868 | + .buf_queue = mtk_jpeg_enc_buf_queue, |
|---|
| 869 | + .wait_prepare = vb2_ops_wait_prepare, |
|---|
| 870 | + .wait_finish = vb2_ops_wait_finish, |
|---|
| 871 | + .stop_streaming = mtk_jpeg_enc_stop_streaming, |
|---|
| 774 | 872 | }; |
|---|
| 775 | 873 | |
|---|
| 776 | 874 | static void mtk_jpeg_set_dec_src(struct mtk_jpeg_ctx *ctx, |
|---|
| .. | .. |
|---|
| 779 | 877 | { |
|---|
| 780 | 878 | bs->str_addr = vb2_dma_contig_plane_dma_addr(src_buf, 0); |
|---|
| 781 | 879 | bs->end_addr = bs->str_addr + |
|---|
| 782 | | - mtk_jpeg_align(vb2_get_plane_payload(src_buf, 0), 16); |
|---|
| 783 | | - bs->size = mtk_jpeg_align(vb2_plane_size(src_buf, 0), 128); |
|---|
| 880 | + round_up(vb2_get_plane_payload(src_buf, 0), 16); |
|---|
| 881 | + bs->size = round_up(vb2_plane_size(src_buf, 0), 128); |
|---|
| 784 | 882 | } |
|---|
| 785 | 883 | |
|---|
| 786 | 884 | static int mtk_jpeg_set_dec_dst(struct mtk_jpeg_ctx *ctx, |
|---|
| .. | .. |
|---|
| 810 | 908 | return 0; |
|---|
| 811 | 909 | } |
|---|
| 812 | 910 | |
|---|
| 813 | | -static void mtk_jpeg_device_run(void *priv) |
|---|
| 911 | +static void mtk_jpeg_enc_device_run(void *priv) |
|---|
| 912 | +{ |
|---|
| 913 | + struct mtk_jpeg_ctx *ctx = priv; |
|---|
| 914 | + struct mtk_jpeg_dev *jpeg = ctx->jpeg; |
|---|
| 915 | + struct vb2_v4l2_buffer *src_buf, *dst_buf; |
|---|
| 916 | + enum vb2_buffer_state buf_state = VB2_BUF_STATE_ERROR; |
|---|
| 917 | + unsigned long flags; |
|---|
| 918 | + int ret; |
|---|
| 919 | + |
|---|
| 920 | + src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx); |
|---|
| 921 | + dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx); |
|---|
| 922 | + |
|---|
| 923 | + ret = pm_runtime_get_sync(jpeg->dev); |
|---|
| 924 | + if (ret < 0) |
|---|
| 925 | + goto enc_end; |
|---|
| 926 | + |
|---|
| 927 | + schedule_delayed_work(&jpeg->job_timeout_work, |
|---|
| 928 | + msecs_to_jiffies(MTK_JPEG_HW_TIMEOUT_MSEC)); |
|---|
| 929 | + |
|---|
| 930 | + spin_lock_irqsave(&jpeg->hw_lock, flags); |
|---|
| 931 | + |
|---|
| 932 | + /* |
|---|
| 933 | + * Resetting the hardware every frame is to ensure that all the |
|---|
| 934 | + * registers are cleared. This is a hardware requirement. |
|---|
| 935 | + */ |
|---|
| 936 | + mtk_jpeg_enc_reset(jpeg->reg_base); |
|---|
| 937 | + |
|---|
| 938 | + mtk_jpeg_set_enc_src(ctx, jpeg->reg_base, &src_buf->vb2_buf); |
|---|
| 939 | + mtk_jpeg_set_enc_dst(ctx, jpeg->reg_base, &dst_buf->vb2_buf); |
|---|
| 940 | + mtk_jpeg_set_enc_params(ctx, jpeg->reg_base); |
|---|
| 941 | + mtk_jpeg_enc_start(jpeg->reg_base); |
|---|
| 942 | + spin_unlock_irqrestore(&jpeg->hw_lock, flags); |
|---|
| 943 | + return; |
|---|
| 944 | + |
|---|
| 945 | +enc_end: |
|---|
| 946 | + v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx); |
|---|
| 947 | + v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx); |
|---|
| 948 | + v4l2_m2m_buf_done(src_buf, buf_state); |
|---|
| 949 | + v4l2_m2m_buf_done(dst_buf, buf_state); |
|---|
| 950 | + v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx); |
|---|
| 951 | +} |
|---|
| 952 | + |
|---|
| 953 | +static void mtk_jpeg_dec_device_run(void *priv) |
|---|
| 814 | 954 | { |
|---|
| 815 | 955 | struct mtk_jpeg_ctx *ctx = priv; |
|---|
| 816 | 956 | struct mtk_jpeg_dev *jpeg = ctx->jpeg; |
|---|
| .. | .. |
|---|
| 820 | 960 | struct mtk_jpeg_src_buf *jpeg_src_buf; |
|---|
| 821 | 961 | struct mtk_jpeg_bs bs; |
|---|
| 822 | 962 | struct mtk_jpeg_fb fb; |
|---|
| 823 | | - int i; |
|---|
| 963 | + int ret; |
|---|
| 824 | 964 | |
|---|
| 825 | 965 | src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx); |
|---|
| 826 | 966 | dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx); |
|---|
| 827 | 967 | jpeg_src_buf = mtk_jpeg_vb2_to_srcbuf(&src_buf->vb2_buf); |
|---|
| 828 | | - |
|---|
| 829 | | - if (jpeg_src_buf->flags & MTK_JPEG_BUF_FLAGS_LAST_FRAME) { |
|---|
| 830 | | - for (i = 0; i < dst_buf->vb2_buf.num_planes; i++) |
|---|
| 831 | | - vb2_set_plane_payload(&dst_buf->vb2_buf, i, 0); |
|---|
| 832 | | - buf_state = VB2_BUF_STATE_DONE; |
|---|
| 833 | | - goto dec_end; |
|---|
| 834 | | - } |
|---|
| 835 | 968 | |
|---|
| 836 | 969 | if (mtk_jpeg_check_resolution_change(ctx, &jpeg_src_buf->dec_param)) { |
|---|
| 837 | 970 | mtk_jpeg_queue_src_chg_event(ctx); |
|---|
| .. | .. |
|---|
| 840 | 973 | return; |
|---|
| 841 | 974 | } |
|---|
| 842 | 975 | |
|---|
| 976 | + ret = pm_runtime_get_sync(jpeg->dev); |
|---|
| 977 | + if (ret < 0) |
|---|
| 978 | + goto dec_end; |
|---|
| 979 | + |
|---|
| 980 | + schedule_delayed_work(&jpeg->job_timeout_work, |
|---|
| 981 | + msecs_to_jiffies(MTK_JPEG_HW_TIMEOUT_MSEC)); |
|---|
| 982 | + |
|---|
| 843 | 983 | mtk_jpeg_set_dec_src(ctx, &src_buf->vb2_buf, &bs); |
|---|
| 844 | 984 | if (mtk_jpeg_set_dec_dst(ctx, &jpeg_src_buf->dec_param, &dst_buf->vb2_buf, &fb)) |
|---|
| 845 | 985 | goto dec_end; |
|---|
| 846 | 986 | |
|---|
| 847 | 987 | spin_lock_irqsave(&jpeg->hw_lock, flags); |
|---|
| 848 | | - mtk_jpeg_dec_reset(jpeg->dec_reg_base); |
|---|
| 849 | | - mtk_jpeg_dec_set_config(jpeg->dec_reg_base, |
|---|
| 988 | + mtk_jpeg_dec_reset(jpeg->reg_base); |
|---|
| 989 | + mtk_jpeg_dec_set_config(jpeg->reg_base, |
|---|
| 850 | 990 | &jpeg_src_buf->dec_param, &bs, &fb); |
|---|
| 851 | 991 | |
|---|
| 852 | | - mtk_jpeg_dec_start(jpeg->dec_reg_base); |
|---|
| 992 | + mtk_jpeg_dec_start(jpeg->reg_base); |
|---|
| 853 | 993 | spin_unlock_irqrestore(&jpeg->hw_lock, flags); |
|---|
| 854 | 994 | return; |
|---|
| 855 | 995 | |
|---|
| .. | .. |
|---|
| 861 | 1001 | v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx); |
|---|
| 862 | 1002 | } |
|---|
| 863 | 1003 | |
|---|
| 864 | | -static int mtk_jpeg_job_ready(void *priv) |
|---|
| 1004 | +static int mtk_jpeg_dec_job_ready(void *priv) |
|---|
| 865 | 1005 | { |
|---|
| 866 | 1006 | struct mtk_jpeg_ctx *ctx = priv; |
|---|
| 867 | 1007 | |
|---|
| 868 | 1008 | return (ctx->state == MTK_JPEG_RUNNING) ? 1 : 0; |
|---|
| 869 | 1009 | } |
|---|
| 870 | 1010 | |
|---|
| 871 | | -static const struct v4l2_m2m_ops mtk_jpeg_m2m_ops = { |
|---|
| 872 | | - .device_run = mtk_jpeg_device_run, |
|---|
| 873 | | - .job_ready = mtk_jpeg_job_ready, |
|---|
| 1011 | +static const struct v4l2_m2m_ops mtk_jpeg_enc_m2m_ops = { |
|---|
| 1012 | + .device_run = mtk_jpeg_enc_device_run, |
|---|
| 1013 | +}; |
|---|
| 1014 | + |
|---|
| 1015 | +static const struct v4l2_m2m_ops mtk_jpeg_dec_m2m_ops = { |
|---|
| 1016 | + .device_run = mtk_jpeg_dec_device_run, |
|---|
| 1017 | + .job_ready = mtk_jpeg_dec_job_ready, |
|---|
| 874 | 1018 | }; |
|---|
| 875 | 1019 | |
|---|
| 876 | 1020 | static int mtk_jpeg_queue_init(void *priv, struct vb2_queue *src_vq, |
|---|
| 877 | 1021 | struct vb2_queue *dst_vq) |
|---|
| 878 | 1022 | { |
|---|
| 879 | 1023 | struct mtk_jpeg_ctx *ctx = priv; |
|---|
| 1024 | + struct mtk_jpeg_dev *jpeg = ctx->jpeg; |
|---|
| 880 | 1025 | int ret; |
|---|
| 881 | 1026 | |
|---|
| 882 | 1027 | src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; |
|---|
| 883 | 1028 | src_vq->io_modes = VB2_DMABUF | VB2_MMAP; |
|---|
| 884 | 1029 | src_vq->drv_priv = ctx; |
|---|
| 885 | 1030 | src_vq->buf_struct_size = sizeof(struct mtk_jpeg_src_buf); |
|---|
| 886 | | - src_vq->ops = &mtk_jpeg_qops; |
|---|
| 1031 | + src_vq->ops = jpeg->variant->qops; |
|---|
| 887 | 1032 | src_vq->mem_ops = &vb2_dma_contig_memops; |
|---|
| 888 | 1033 | src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; |
|---|
| 889 | 1034 | src_vq->lock = &ctx->jpeg->lock; |
|---|
| .. | .. |
|---|
| 896 | 1041 | dst_vq->io_modes = VB2_DMABUF | VB2_MMAP; |
|---|
| 897 | 1042 | dst_vq->drv_priv = ctx; |
|---|
| 898 | 1043 | dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer); |
|---|
| 899 | | - dst_vq->ops = &mtk_jpeg_qops; |
|---|
| 1044 | + dst_vq->ops = jpeg->variant->qops; |
|---|
| 900 | 1045 | dst_vq->mem_ops = &vb2_dma_contig_memops; |
|---|
| 901 | 1046 | dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; |
|---|
| 902 | 1047 | dst_vq->lock = &ctx->jpeg->lock; |
|---|
| .. | .. |
|---|
| 913 | 1058 | ret = mtk_smi_larb_get(jpeg->larb); |
|---|
| 914 | 1059 | if (ret) |
|---|
| 915 | 1060 | dev_err(jpeg->dev, "mtk_smi_larb_get larbvdec fail %d\n", ret); |
|---|
| 916 | | - clk_prepare_enable(jpeg->clk_jdec_smi); |
|---|
| 917 | | - clk_prepare_enable(jpeg->clk_jdec); |
|---|
| 1061 | + |
|---|
| 1062 | + ret = clk_bulk_prepare_enable(jpeg->variant->num_clks, |
|---|
| 1063 | + jpeg->variant->clks); |
|---|
| 1064 | + if (ret) |
|---|
| 1065 | + dev_err(jpeg->dev, "Failed to open jpeg clk: %d\n", ret); |
|---|
| 918 | 1066 | } |
|---|
| 919 | 1067 | |
|---|
| 920 | 1068 | static void mtk_jpeg_clk_off(struct mtk_jpeg_dev *jpeg) |
|---|
| 921 | 1069 | { |
|---|
| 922 | | - clk_disable_unprepare(jpeg->clk_jdec); |
|---|
| 923 | | - clk_disable_unprepare(jpeg->clk_jdec_smi); |
|---|
| 1070 | + clk_bulk_disable_unprepare(jpeg->variant->num_clks, |
|---|
| 1071 | + jpeg->variant->clks); |
|---|
| 924 | 1072 | mtk_smi_larb_put(jpeg->larb); |
|---|
| 1073 | +} |
|---|
| 1074 | + |
|---|
| 1075 | +static irqreturn_t mtk_jpeg_enc_done(struct mtk_jpeg_dev *jpeg) |
|---|
| 1076 | +{ |
|---|
| 1077 | + struct mtk_jpeg_ctx *ctx; |
|---|
| 1078 | + struct vb2_v4l2_buffer *src_buf, *dst_buf; |
|---|
| 1079 | + enum vb2_buffer_state buf_state = VB2_BUF_STATE_ERROR; |
|---|
| 1080 | + u32 result_size; |
|---|
| 1081 | + |
|---|
| 1082 | + ctx = v4l2_m2m_get_curr_priv(jpeg->m2m_dev); |
|---|
| 1083 | + if (!ctx) { |
|---|
| 1084 | + v4l2_err(&jpeg->v4l2_dev, "Context is NULL\n"); |
|---|
| 1085 | + return IRQ_HANDLED; |
|---|
| 1086 | + } |
|---|
| 1087 | + |
|---|
| 1088 | + src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx); |
|---|
| 1089 | + dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx); |
|---|
| 1090 | + |
|---|
| 1091 | + result_size = mtk_jpeg_enc_get_file_size(jpeg->reg_base); |
|---|
| 1092 | + vb2_set_plane_payload(&dst_buf->vb2_buf, 0, result_size); |
|---|
| 1093 | + |
|---|
| 1094 | + buf_state = VB2_BUF_STATE_DONE; |
|---|
| 1095 | + |
|---|
| 1096 | + v4l2_m2m_buf_done(src_buf, buf_state); |
|---|
| 1097 | + v4l2_m2m_buf_done(dst_buf, buf_state); |
|---|
| 1098 | + v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx); |
|---|
| 1099 | + pm_runtime_put(ctx->jpeg->dev); |
|---|
| 1100 | + return IRQ_HANDLED; |
|---|
| 1101 | +} |
|---|
| 1102 | + |
|---|
| 1103 | +static irqreturn_t mtk_jpeg_enc_irq(int irq, void *priv) |
|---|
| 1104 | +{ |
|---|
| 1105 | + struct mtk_jpeg_dev *jpeg = priv; |
|---|
| 1106 | + u32 irq_status; |
|---|
| 1107 | + irqreturn_t ret = IRQ_NONE; |
|---|
| 1108 | + |
|---|
| 1109 | + cancel_delayed_work(&jpeg->job_timeout_work); |
|---|
| 1110 | + |
|---|
| 1111 | + irq_status = readl(jpeg->reg_base + JPEG_ENC_INT_STS) & |
|---|
| 1112 | + JPEG_ENC_INT_STATUS_MASK_ALLIRQ; |
|---|
| 1113 | + if (irq_status) |
|---|
| 1114 | + writel(0, jpeg->reg_base + JPEG_ENC_INT_STS); |
|---|
| 1115 | + |
|---|
| 1116 | + if (!(irq_status & JPEG_ENC_INT_STATUS_DONE)) |
|---|
| 1117 | + return ret; |
|---|
| 1118 | + |
|---|
| 1119 | + ret = mtk_jpeg_enc_done(jpeg); |
|---|
| 1120 | + return ret; |
|---|
| 925 | 1121 | } |
|---|
| 926 | 1122 | |
|---|
| 927 | 1123 | static irqreturn_t mtk_jpeg_dec_irq(int irq, void *priv) |
|---|
| .. | .. |
|---|
| 935 | 1131 | u32 dec_ret; |
|---|
| 936 | 1132 | int i; |
|---|
| 937 | 1133 | |
|---|
| 938 | | - dec_ret = mtk_jpeg_dec_get_int_status(jpeg->dec_reg_base); |
|---|
| 1134 | + cancel_delayed_work(&jpeg->job_timeout_work); |
|---|
| 1135 | + |
|---|
| 1136 | + dec_ret = mtk_jpeg_dec_get_int_status(jpeg->reg_base); |
|---|
| 939 | 1137 | dec_irq_ret = mtk_jpeg_dec_enum_result(dec_ret); |
|---|
| 940 | 1138 | ctx = v4l2_m2m_get_curr_priv(jpeg->m2m_dev); |
|---|
| 941 | 1139 | if (!ctx) { |
|---|
| .. | .. |
|---|
| 948 | 1146 | jpeg_src_buf = mtk_jpeg_vb2_to_srcbuf(&src_buf->vb2_buf); |
|---|
| 949 | 1147 | |
|---|
| 950 | 1148 | if (dec_irq_ret >= MTK_JPEG_DEC_RESULT_UNDERFLOW) |
|---|
| 951 | | - mtk_jpeg_dec_reset(jpeg->dec_reg_base); |
|---|
| 1149 | + mtk_jpeg_dec_reset(jpeg->reg_base); |
|---|
| 952 | 1150 | |
|---|
| 953 | 1151 | if (dec_irq_ret != MTK_JPEG_DEC_RESULT_EOF_DONE) { |
|---|
| 954 | 1152 | dev_err(jpeg->dev, "decode failed\n"); |
|---|
| .. | .. |
|---|
| 965 | 1163 | v4l2_m2m_buf_done(src_buf, buf_state); |
|---|
| 966 | 1164 | v4l2_m2m_buf_done(dst_buf, buf_state); |
|---|
| 967 | 1165 | v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx); |
|---|
| 1166 | + pm_runtime_put(ctx->jpeg->dev); |
|---|
| 968 | 1167 | return IRQ_HANDLED; |
|---|
| 969 | 1168 | } |
|---|
| 970 | 1169 | |
|---|
| 971 | 1170 | static void mtk_jpeg_set_default_params(struct mtk_jpeg_ctx *ctx) |
|---|
| 972 | 1171 | { |
|---|
| 973 | 1172 | struct mtk_jpeg_q_data *q = &ctx->out_q; |
|---|
| 974 | | - int i; |
|---|
| 1173 | + struct mtk_jpeg_dev *jpeg = ctx->jpeg; |
|---|
| 975 | 1174 | |
|---|
| 976 | | - ctx->colorspace = V4L2_COLORSPACE_JPEG, |
|---|
| 977 | | - ctx->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT; |
|---|
| 978 | | - ctx->quantization = V4L2_QUANTIZATION_DEFAULT; |
|---|
| 979 | | - ctx->xfer_func = V4L2_XFER_FUNC_DEFAULT; |
|---|
| 1175 | + ctx->fh.ctrl_handler = &ctx->ctrl_hdl; |
|---|
| 1176 | + q->pix_mp.colorspace = V4L2_COLORSPACE_SRGB; |
|---|
| 1177 | + q->pix_mp.ycbcr_enc = V4L2_YCBCR_ENC_601; |
|---|
| 1178 | + q->pix_mp.quantization = V4L2_QUANTIZATION_FULL_RANGE; |
|---|
| 1179 | + q->pix_mp.xfer_func = V4L2_XFER_FUNC_SRGB; |
|---|
| 980 | 1180 | |
|---|
| 981 | | - q->fmt = mtk_jpeg_find_format(ctx, V4L2_PIX_FMT_JPEG, |
|---|
| 982 | | - MTK_JPEG_FMT_TYPE_OUTPUT); |
|---|
| 983 | | - q->w = MTK_JPEG_MIN_WIDTH; |
|---|
| 984 | | - q->h = MTK_JPEG_MIN_HEIGHT; |
|---|
| 985 | | - q->bytesperline[0] = 0; |
|---|
| 986 | | - q->sizeimage[0] = MTK_JPEG_DEFAULT_SIZEIMAGE; |
|---|
| 1181 | + q->fmt = mtk_jpeg_find_format(jpeg->variant->formats, |
|---|
| 1182 | + jpeg->variant->num_formats, |
|---|
| 1183 | + jpeg->variant->out_q_default_fourcc, |
|---|
| 1184 | + MTK_JPEG_FMT_FLAG_OUTPUT); |
|---|
| 1185 | + q->pix_mp.width = MTK_JPEG_MIN_WIDTH; |
|---|
| 1186 | + q->pix_mp.height = MTK_JPEG_MIN_HEIGHT; |
|---|
| 1187 | + mtk_jpeg_try_fmt_mplane(&q->pix_mp, q->fmt); |
|---|
| 987 | 1188 | |
|---|
| 988 | 1189 | q = &ctx->cap_q; |
|---|
| 989 | | - q->fmt = mtk_jpeg_find_format(ctx, V4L2_PIX_FMT_YUV420M, |
|---|
| 990 | | - MTK_JPEG_FMT_TYPE_CAPTURE); |
|---|
| 991 | | - q->w = MTK_JPEG_MIN_WIDTH; |
|---|
| 992 | | - q->h = MTK_JPEG_MIN_HEIGHT; |
|---|
| 1190 | + q->fmt = mtk_jpeg_find_format(jpeg->variant->formats, |
|---|
| 1191 | + jpeg->variant->num_formats, |
|---|
| 1192 | + jpeg->variant->cap_q_default_fourcc, |
|---|
| 1193 | + MTK_JPEG_FMT_FLAG_CAPTURE); |
|---|
| 1194 | + q->pix_mp.colorspace = V4L2_COLORSPACE_SRGB; |
|---|
| 1195 | + q->pix_mp.ycbcr_enc = V4L2_YCBCR_ENC_601; |
|---|
| 1196 | + q->pix_mp.quantization = V4L2_QUANTIZATION_FULL_RANGE; |
|---|
| 1197 | + q->pix_mp.xfer_func = V4L2_XFER_FUNC_SRGB; |
|---|
| 1198 | + q->pix_mp.width = MTK_JPEG_MIN_WIDTH; |
|---|
| 1199 | + q->pix_mp.height = MTK_JPEG_MIN_HEIGHT; |
|---|
| 993 | 1200 | |
|---|
| 994 | | - for (i = 0; i < q->fmt->colplanes; i++) { |
|---|
| 995 | | - u32 stride = q->w * q->fmt->h_sample[i] / 4; |
|---|
| 996 | | - u32 h = q->h * q->fmt->v_sample[i] / 4; |
|---|
| 997 | | - |
|---|
| 998 | | - q->bytesperline[i] = stride; |
|---|
| 999 | | - q->sizeimage[i] = stride * h; |
|---|
| 1000 | | - } |
|---|
| 1201 | + mtk_jpeg_try_fmt_mplane(&q->pix_mp, q->fmt); |
|---|
| 1001 | 1202 | } |
|---|
| 1002 | 1203 | |
|---|
| 1003 | 1204 | static int mtk_jpeg_open(struct file *file) |
|---|
| .. | .. |
|---|
| 1028 | 1229 | goto error; |
|---|
| 1029 | 1230 | } |
|---|
| 1030 | 1231 | |
|---|
| 1232 | + if (jpeg->variant->cap_q_default_fourcc == V4L2_PIX_FMT_JPEG) { |
|---|
| 1233 | + ret = mtk_jpeg_enc_ctrls_setup(ctx); |
|---|
| 1234 | + if (ret) { |
|---|
| 1235 | + v4l2_err(&jpeg->v4l2_dev, "Failed to setup jpeg enc controls\n"); |
|---|
| 1236 | + goto error; |
|---|
| 1237 | + } |
|---|
| 1238 | + } else { |
|---|
| 1239 | + v4l2_ctrl_handler_init(&ctx->ctrl_hdl, 0); |
|---|
| 1240 | + } |
|---|
| 1031 | 1241 | mtk_jpeg_set_default_params(ctx); |
|---|
| 1032 | 1242 | mutex_unlock(&jpeg->lock); |
|---|
| 1033 | 1243 | return 0; |
|---|
| .. | .. |
|---|
| 1048 | 1258 | |
|---|
| 1049 | 1259 | mutex_lock(&jpeg->lock); |
|---|
| 1050 | 1260 | v4l2_m2m_ctx_release(ctx->fh.m2m_ctx); |
|---|
| 1261 | + v4l2_ctrl_handler_free(&ctx->ctrl_hdl); |
|---|
| 1051 | 1262 | v4l2_fh_del(&ctx->fh); |
|---|
| 1052 | 1263 | v4l2_fh_exit(&ctx->fh); |
|---|
| 1053 | 1264 | kfree(ctx); |
|---|
| .. | .. |
|---|
| 1064 | 1275 | .mmap = v4l2_m2m_fop_mmap, |
|---|
| 1065 | 1276 | }; |
|---|
| 1066 | 1277 | |
|---|
| 1278 | +static struct clk_bulk_data mt8173_jpeg_dec_clocks[] = { |
|---|
| 1279 | + { .id = "jpgdec-smi" }, |
|---|
| 1280 | + { .id = "jpgdec" }, |
|---|
| 1281 | +}; |
|---|
| 1282 | + |
|---|
| 1283 | +static struct clk_bulk_data mtk_jpeg_clocks[] = { |
|---|
| 1284 | + { .id = "jpgenc" }, |
|---|
| 1285 | +}; |
|---|
| 1286 | + |
|---|
| 1067 | 1287 | static int mtk_jpeg_clk_init(struct mtk_jpeg_dev *jpeg) |
|---|
| 1068 | 1288 | { |
|---|
| 1069 | 1289 | struct device_node *node; |
|---|
| 1070 | 1290 | struct platform_device *pdev; |
|---|
| 1291 | + int ret; |
|---|
| 1071 | 1292 | |
|---|
| 1072 | 1293 | node = of_parse_phandle(jpeg->dev->of_node, "mediatek,larb", 0); |
|---|
| 1073 | 1294 | if (!node) |
|---|
| .. | .. |
|---|
| 1081 | 1302 | |
|---|
| 1082 | 1303 | jpeg->larb = &pdev->dev; |
|---|
| 1083 | 1304 | |
|---|
| 1084 | | - jpeg->clk_jdec = devm_clk_get(jpeg->dev, "jpgdec"); |
|---|
| 1085 | | - if (IS_ERR(jpeg->clk_jdec)) |
|---|
| 1086 | | - return PTR_ERR(jpeg->clk_jdec); |
|---|
| 1305 | + ret = devm_clk_bulk_get(jpeg->dev, jpeg->variant->num_clks, |
|---|
| 1306 | + jpeg->variant->clks); |
|---|
| 1307 | + if (ret) { |
|---|
| 1308 | + dev_err(&pdev->dev, "failed to get jpeg clock:%d\n", ret); |
|---|
| 1309 | + put_device(&pdev->dev); |
|---|
| 1310 | + return ret; |
|---|
| 1311 | + } |
|---|
| 1087 | 1312 | |
|---|
| 1088 | | - jpeg->clk_jdec_smi = devm_clk_get(jpeg->dev, "jpgdec-smi"); |
|---|
| 1089 | | - return PTR_ERR_OR_ZERO(jpeg->clk_jdec_smi); |
|---|
| 1313 | + return 0; |
|---|
| 1314 | +} |
|---|
| 1315 | + |
|---|
| 1316 | +static void mtk_jpeg_job_timeout_work(struct work_struct *work) |
|---|
| 1317 | +{ |
|---|
| 1318 | + struct mtk_jpeg_dev *jpeg = container_of(work, struct mtk_jpeg_dev, |
|---|
| 1319 | + job_timeout_work.work); |
|---|
| 1320 | + struct mtk_jpeg_ctx *ctx; |
|---|
| 1321 | + struct vb2_v4l2_buffer *src_buf, *dst_buf; |
|---|
| 1322 | + |
|---|
| 1323 | + ctx = v4l2_m2m_get_curr_priv(jpeg->m2m_dev); |
|---|
| 1324 | + src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx); |
|---|
| 1325 | + dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx); |
|---|
| 1326 | + |
|---|
| 1327 | + jpeg->variant->hw_reset(jpeg->reg_base); |
|---|
| 1328 | + |
|---|
| 1329 | + pm_runtime_put(jpeg->dev); |
|---|
| 1330 | + |
|---|
| 1331 | + v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_ERROR); |
|---|
| 1332 | + v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_ERROR); |
|---|
| 1333 | + v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx); |
|---|
| 1334 | +} |
|---|
| 1335 | + |
|---|
| 1336 | +static inline void mtk_jpeg_clk_release(struct mtk_jpeg_dev *jpeg) |
|---|
| 1337 | +{ |
|---|
| 1338 | + put_device(jpeg->larb); |
|---|
| 1090 | 1339 | } |
|---|
| 1091 | 1340 | |
|---|
| 1092 | 1341 | static int mtk_jpeg_probe(struct platform_device *pdev) |
|---|
| 1093 | 1342 | { |
|---|
| 1094 | 1343 | struct mtk_jpeg_dev *jpeg; |
|---|
| 1095 | 1344 | struct resource *res; |
|---|
| 1096 | | - int dec_irq; |
|---|
| 1345 | + int jpeg_irq; |
|---|
| 1097 | 1346 | int ret; |
|---|
| 1098 | 1347 | |
|---|
| 1099 | 1348 | jpeg = devm_kzalloc(&pdev->dev, sizeof(*jpeg), GFP_KERNEL); |
|---|
| .. | .. |
|---|
| 1103 | 1352 | mutex_init(&jpeg->lock); |
|---|
| 1104 | 1353 | spin_lock_init(&jpeg->hw_lock); |
|---|
| 1105 | 1354 | jpeg->dev = &pdev->dev; |
|---|
| 1355 | + jpeg->variant = of_device_get_match_data(jpeg->dev); |
|---|
| 1356 | + INIT_DELAYED_WORK(&jpeg->job_timeout_work, mtk_jpeg_job_timeout_work); |
|---|
| 1106 | 1357 | |
|---|
| 1107 | 1358 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
|---|
| 1108 | | - jpeg->dec_reg_base = devm_ioremap_resource(&pdev->dev, res); |
|---|
| 1109 | | - if (IS_ERR(jpeg->dec_reg_base)) { |
|---|
| 1110 | | - ret = PTR_ERR(jpeg->dec_reg_base); |
|---|
| 1359 | + jpeg->reg_base = devm_ioremap_resource(&pdev->dev, res); |
|---|
| 1360 | + if (IS_ERR(jpeg->reg_base)) { |
|---|
| 1361 | + ret = PTR_ERR(jpeg->reg_base); |
|---|
| 1111 | 1362 | return ret; |
|---|
| 1112 | 1363 | } |
|---|
| 1113 | 1364 | |
|---|
| 1114 | | - res = platform_get_resource(pdev, IORESOURCE_IRQ, 0); |
|---|
| 1115 | | - dec_irq = platform_get_irq(pdev, 0); |
|---|
| 1116 | | - if (!res || dec_irq < 0) { |
|---|
| 1117 | | - dev_err(&pdev->dev, "Failed to get dec_irq %d.\n", dec_irq); |
|---|
| 1118 | | - ret = -EINVAL; |
|---|
| 1119 | | - return ret; |
|---|
| 1365 | + jpeg_irq = platform_get_irq(pdev, 0); |
|---|
| 1366 | + if (jpeg_irq < 0) { |
|---|
| 1367 | + dev_err(&pdev->dev, "Failed to get jpeg_irq %d.\n", jpeg_irq); |
|---|
| 1368 | + return jpeg_irq; |
|---|
| 1120 | 1369 | } |
|---|
| 1121 | 1370 | |
|---|
| 1122 | | - ret = devm_request_irq(&pdev->dev, dec_irq, mtk_jpeg_dec_irq, 0, |
|---|
| 1123 | | - pdev->name, jpeg); |
|---|
| 1371 | + ret = devm_request_irq(&pdev->dev, jpeg_irq, |
|---|
| 1372 | + jpeg->variant->irq_handler, 0, pdev->name, jpeg); |
|---|
| 1124 | 1373 | if (ret) { |
|---|
| 1125 | | - dev_err(&pdev->dev, "Failed to request dec_irq %d (%d)\n", |
|---|
| 1126 | | - dec_irq, ret); |
|---|
| 1127 | | - ret = -EINVAL; |
|---|
| 1374 | + dev_err(&pdev->dev, "Failed to request jpeg_irq %d (%d)\n", |
|---|
| 1375 | + jpeg_irq, ret); |
|---|
| 1128 | 1376 | goto err_req_irq; |
|---|
| 1129 | 1377 | } |
|---|
| 1130 | 1378 | |
|---|
| .. | .. |
|---|
| 1141 | 1389 | goto err_dev_register; |
|---|
| 1142 | 1390 | } |
|---|
| 1143 | 1391 | |
|---|
| 1144 | | - jpeg->m2m_dev = v4l2_m2m_init(&mtk_jpeg_m2m_ops); |
|---|
| 1392 | + jpeg->m2m_dev = v4l2_m2m_init(jpeg->variant->m2m_ops); |
|---|
| 1393 | + |
|---|
| 1145 | 1394 | if (IS_ERR(jpeg->m2m_dev)) { |
|---|
| 1146 | 1395 | v4l2_err(&jpeg->v4l2_dev, "Failed to init mem2mem device\n"); |
|---|
| 1147 | 1396 | ret = PTR_ERR(jpeg->m2m_dev); |
|---|
| 1148 | 1397 | goto err_m2m_init; |
|---|
| 1149 | 1398 | } |
|---|
| 1150 | 1399 | |
|---|
| 1151 | | - jpeg->dec_vdev = video_device_alloc(); |
|---|
| 1152 | | - if (!jpeg->dec_vdev) { |
|---|
| 1400 | + jpeg->vdev = video_device_alloc(); |
|---|
| 1401 | + if (!jpeg->vdev) { |
|---|
| 1153 | 1402 | ret = -ENOMEM; |
|---|
| 1154 | | - goto err_dec_vdev_alloc; |
|---|
| 1403 | + goto err_vfd_jpeg_alloc; |
|---|
| 1155 | 1404 | } |
|---|
| 1156 | | - snprintf(jpeg->dec_vdev->name, sizeof(jpeg->dec_vdev->name), |
|---|
| 1157 | | - "%s-dec", MTK_JPEG_NAME); |
|---|
| 1158 | | - jpeg->dec_vdev->fops = &mtk_jpeg_fops; |
|---|
| 1159 | | - jpeg->dec_vdev->ioctl_ops = &mtk_jpeg_ioctl_ops; |
|---|
| 1160 | | - jpeg->dec_vdev->minor = -1; |
|---|
| 1161 | | - jpeg->dec_vdev->release = video_device_release; |
|---|
| 1162 | | - jpeg->dec_vdev->lock = &jpeg->lock; |
|---|
| 1163 | | - jpeg->dec_vdev->v4l2_dev = &jpeg->v4l2_dev; |
|---|
| 1164 | | - jpeg->dec_vdev->vfl_dir = VFL_DIR_M2M; |
|---|
| 1165 | | - jpeg->dec_vdev->device_caps = V4L2_CAP_STREAMING | |
|---|
| 1166 | | - V4L2_CAP_VIDEO_M2M_MPLANE; |
|---|
| 1405 | + snprintf(jpeg->vdev->name, sizeof(jpeg->vdev->name), |
|---|
| 1406 | + "%s", jpeg->variant->dev_name); |
|---|
| 1407 | + jpeg->vdev->fops = &mtk_jpeg_fops; |
|---|
| 1408 | + jpeg->vdev->ioctl_ops = jpeg->variant->ioctl_ops; |
|---|
| 1409 | + jpeg->vdev->minor = -1; |
|---|
| 1410 | + jpeg->vdev->release = video_device_release; |
|---|
| 1411 | + jpeg->vdev->lock = &jpeg->lock; |
|---|
| 1412 | + jpeg->vdev->v4l2_dev = &jpeg->v4l2_dev; |
|---|
| 1413 | + jpeg->vdev->vfl_dir = VFL_DIR_M2M; |
|---|
| 1414 | + jpeg->vdev->device_caps = V4L2_CAP_STREAMING | |
|---|
| 1415 | + V4L2_CAP_VIDEO_M2M_MPLANE; |
|---|
| 1167 | 1416 | |
|---|
| 1168 | | - ret = video_register_device(jpeg->dec_vdev, VFL_TYPE_GRABBER, 3); |
|---|
| 1417 | + ret = video_register_device(jpeg->vdev, VFL_TYPE_VIDEO, -1); |
|---|
| 1169 | 1418 | if (ret) { |
|---|
| 1170 | 1419 | v4l2_err(&jpeg->v4l2_dev, "Failed to register video device\n"); |
|---|
| 1171 | | - goto err_dec_vdev_register; |
|---|
| 1420 | + goto err_vfd_jpeg_register; |
|---|
| 1172 | 1421 | } |
|---|
| 1173 | 1422 | |
|---|
| 1174 | | - video_set_drvdata(jpeg->dec_vdev, jpeg); |
|---|
| 1423 | + video_set_drvdata(jpeg->vdev, jpeg); |
|---|
| 1175 | 1424 | v4l2_info(&jpeg->v4l2_dev, |
|---|
| 1176 | | - "decoder device registered as /dev/video%d (%d,%d)\n", |
|---|
| 1177 | | - jpeg->dec_vdev->num, VIDEO_MAJOR, jpeg->dec_vdev->minor); |
|---|
| 1425 | + "%s device registered as /dev/video%d (%d,%d)\n", |
|---|
| 1426 | + jpeg->variant->dev_name, jpeg->vdev->num, |
|---|
| 1427 | + VIDEO_MAJOR, jpeg->vdev->minor); |
|---|
| 1178 | 1428 | |
|---|
| 1179 | 1429 | platform_set_drvdata(pdev, jpeg); |
|---|
| 1180 | 1430 | |
|---|
| .. | .. |
|---|
| 1182 | 1432 | |
|---|
| 1183 | 1433 | return 0; |
|---|
| 1184 | 1434 | |
|---|
| 1185 | | -err_dec_vdev_register: |
|---|
| 1186 | | - video_device_release(jpeg->dec_vdev); |
|---|
| 1435 | +err_vfd_jpeg_register: |
|---|
| 1436 | + video_device_release(jpeg->vdev); |
|---|
| 1187 | 1437 | |
|---|
| 1188 | | -err_dec_vdev_alloc: |
|---|
| 1438 | +err_vfd_jpeg_alloc: |
|---|
| 1189 | 1439 | v4l2_m2m_release(jpeg->m2m_dev); |
|---|
| 1190 | 1440 | |
|---|
| 1191 | 1441 | err_m2m_init: |
|---|
| 1192 | 1442 | v4l2_device_unregister(&jpeg->v4l2_dev); |
|---|
| 1193 | 1443 | |
|---|
| 1194 | 1444 | err_dev_register: |
|---|
| 1445 | + mtk_jpeg_clk_release(jpeg); |
|---|
| 1195 | 1446 | |
|---|
| 1196 | 1447 | err_clk_init: |
|---|
| 1197 | 1448 | |
|---|
| .. | .. |
|---|
| 1205 | 1456 | struct mtk_jpeg_dev *jpeg = platform_get_drvdata(pdev); |
|---|
| 1206 | 1457 | |
|---|
| 1207 | 1458 | pm_runtime_disable(&pdev->dev); |
|---|
| 1208 | | - video_unregister_device(jpeg->dec_vdev); |
|---|
| 1209 | | - video_device_release(jpeg->dec_vdev); |
|---|
| 1459 | + video_unregister_device(jpeg->vdev); |
|---|
| 1460 | + video_device_release(jpeg->vdev); |
|---|
| 1210 | 1461 | v4l2_m2m_release(jpeg->m2m_dev); |
|---|
| 1211 | 1462 | v4l2_device_unregister(&jpeg->v4l2_dev); |
|---|
| 1463 | + mtk_jpeg_clk_release(jpeg); |
|---|
| 1212 | 1464 | |
|---|
| 1213 | 1465 | return 0; |
|---|
| 1214 | 1466 | } |
|---|
| .. | .. |
|---|
| 1217 | 1469 | { |
|---|
| 1218 | 1470 | struct mtk_jpeg_dev *jpeg = dev_get_drvdata(dev); |
|---|
| 1219 | 1471 | |
|---|
| 1220 | | - mtk_jpeg_dec_reset(jpeg->dec_reg_base); |
|---|
| 1221 | 1472 | mtk_jpeg_clk_off(jpeg); |
|---|
| 1222 | 1473 | |
|---|
| 1223 | 1474 | return 0; |
|---|
| .. | .. |
|---|
| 1228 | 1479 | struct mtk_jpeg_dev *jpeg = dev_get_drvdata(dev); |
|---|
| 1229 | 1480 | |
|---|
| 1230 | 1481 | mtk_jpeg_clk_on(jpeg); |
|---|
| 1231 | | - mtk_jpeg_dec_reset(jpeg->dec_reg_base); |
|---|
| 1232 | 1482 | |
|---|
| 1233 | 1483 | return 0; |
|---|
| 1234 | 1484 | } |
|---|
| 1235 | 1485 | |
|---|
| 1236 | 1486 | static __maybe_unused int mtk_jpeg_suspend(struct device *dev) |
|---|
| 1237 | 1487 | { |
|---|
| 1238 | | - int ret; |
|---|
| 1488 | + struct mtk_jpeg_dev *jpeg = dev_get_drvdata(dev); |
|---|
| 1239 | 1489 | |
|---|
| 1240 | | - if (pm_runtime_suspended(dev)) |
|---|
| 1241 | | - return 0; |
|---|
| 1242 | | - |
|---|
| 1243 | | - ret = mtk_jpeg_pm_suspend(dev); |
|---|
| 1244 | | - return ret; |
|---|
| 1490 | + v4l2_m2m_suspend(jpeg->m2m_dev); |
|---|
| 1491 | + return pm_runtime_force_suspend(dev); |
|---|
| 1245 | 1492 | } |
|---|
| 1246 | 1493 | |
|---|
| 1247 | 1494 | static __maybe_unused int mtk_jpeg_resume(struct device *dev) |
|---|
| 1248 | 1495 | { |
|---|
| 1496 | + struct mtk_jpeg_dev *jpeg = dev_get_drvdata(dev); |
|---|
| 1249 | 1497 | int ret; |
|---|
| 1250 | 1498 | |
|---|
| 1251 | | - if (pm_runtime_suspended(dev)) |
|---|
| 1252 | | - return 0; |
|---|
| 1499 | + ret = pm_runtime_force_resume(dev); |
|---|
| 1500 | + if (ret < 0) |
|---|
| 1501 | + return ret; |
|---|
| 1253 | 1502 | |
|---|
| 1254 | | - ret = mtk_jpeg_pm_resume(dev); |
|---|
| 1255 | | - |
|---|
| 1503 | + v4l2_m2m_resume(jpeg->m2m_dev); |
|---|
| 1256 | 1504 | return ret; |
|---|
| 1257 | 1505 | } |
|---|
| 1258 | 1506 | |
|---|
| .. | .. |
|---|
| 1261 | 1509 | SET_RUNTIME_PM_OPS(mtk_jpeg_pm_suspend, mtk_jpeg_pm_resume, NULL) |
|---|
| 1262 | 1510 | }; |
|---|
| 1263 | 1511 | |
|---|
| 1512 | +static const struct mtk_jpeg_variant mt8173_jpeg_drvdata = { |
|---|
| 1513 | + .clks = mt8173_jpeg_dec_clocks, |
|---|
| 1514 | + .num_clks = ARRAY_SIZE(mt8173_jpeg_dec_clocks), |
|---|
| 1515 | + .formats = mtk_jpeg_dec_formats, |
|---|
| 1516 | + .num_formats = MTK_JPEG_DEC_NUM_FORMATS, |
|---|
| 1517 | + .qops = &mtk_jpeg_dec_qops, |
|---|
| 1518 | + .irq_handler = mtk_jpeg_dec_irq, |
|---|
| 1519 | + .hw_reset = mtk_jpeg_dec_reset, |
|---|
| 1520 | + .m2m_ops = &mtk_jpeg_dec_m2m_ops, |
|---|
| 1521 | + .dev_name = "mtk-jpeg-dec", |
|---|
| 1522 | + .ioctl_ops = &mtk_jpeg_dec_ioctl_ops, |
|---|
| 1523 | + .out_q_default_fourcc = V4L2_PIX_FMT_JPEG, |
|---|
| 1524 | + .cap_q_default_fourcc = V4L2_PIX_FMT_YUV420M, |
|---|
| 1525 | +}; |
|---|
| 1526 | + |
|---|
| 1527 | +static const struct mtk_jpeg_variant mtk_jpeg_drvdata = { |
|---|
| 1528 | + .clks = mtk_jpeg_clocks, |
|---|
| 1529 | + .num_clks = ARRAY_SIZE(mtk_jpeg_clocks), |
|---|
| 1530 | + .formats = mtk_jpeg_enc_formats, |
|---|
| 1531 | + .num_formats = MTK_JPEG_ENC_NUM_FORMATS, |
|---|
| 1532 | + .qops = &mtk_jpeg_enc_qops, |
|---|
| 1533 | + .irq_handler = mtk_jpeg_enc_irq, |
|---|
| 1534 | + .hw_reset = mtk_jpeg_enc_reset, |
|---|
| 1535 | + .m2m_ops = &mtk_jpeg_enc_m2m_ops, |
|---|
| 1536 | + .dev_name = "mtk-jpeg-enc", |
|---|
| 1537 | + .ioctl_ops = &mtk_jpeg_enc_ioctl_ops, |
|---|
| 1538 | + .out_q_default_fourcc = V4L2_PIX_FMT_YUYV, |
|---|
| 1539 | + .cap_q_default_fourcc = V4L2_PIX_FMT_JPEG, |
|---|
| 1540 | +}; |
|---|
| 1541 | + |
|---|
| 1264 | 1542 | static const struct of_device_id mtk_jpeg_match[] = { |
|---|
| 1265 | 1543 | { |
|---|
| 1266 | 1544 | .compatible = "mediatek,mt8173-jpgdec", |
|---|
| 1267 | | - .data = NULL, |
|---|
| 1545 | + .data = &mt8173_jpeg_drvdata, |
|---|
| 1268 | 1546 | }, |
|---|
| 1269 | 1547 | { |
|---|
| 1270 | 1548 | .compatible = "mediatek,mt2701-jpgdec", |
|---|
| 1271 | | - .data = NULL, |
|---|
| 1549 | + .data = &mt8173_jpeg_drvdata, |
|---|
| 1550 | + }, |
|---|
| 1551 | + { |
|---|
| 1552 | + .compatible = "mediatek,mtk-jpgenc", |
|---|
| 1553 | + .data = &mtk_jpeg_drvdata, |
|---|
| 1272 | 1554 | }, |
|---|
| 1273 | 1555 | {}, |
|---|
| 1274 | 1556 | }; |
|---|