GNU Linux-libre 4.14.302-gnu1
[releases.git] / drivers / media / platform / mtk-jpeg / mtk_jpeg_core.c
1 /*
2  * Copyright (c) 2016 MediaTek Inc.
3  * Author: Ming Hsiu Tsai <minghsiu.tsai@mediatek.com>
4  *         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.
14  */
15
16 #include <linux/clk.h>
17 #include <linux/err.h>
18 #include <linux/interrupt.h>
19 #include <linux/io.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/of_platform.h>
23 #include <linux/platform_device.h>
24 #include <linux/pm_runtime.h>
25 #include <linux/slab.h>
26 #include <linux/spinlock.h>
27 #include <media/v4l2-event.h>
28 #include <media/v4l2-mem2mem.h>
29 #include <media/v4l2-ioctl.h>
30 #include <media/videobuf2-core.h>
31 #include <media/videobuf2-dma-contig.h>
32 #include <soc/mediatek/smi.h>
33
34 #include "mtk_jpeg_hw.h"
35 #include "mtk_jpeg_core.h"
36 #include "mtk_jpeg_parse.h"
37
38 static struct mtk_jpeg_fmt mtk_jpeg_formats[] = {
39         {
40                 .fourcc         = V4L2_PIX_FMT_JPEG,
41                 .colplanes      = 1,
42                 .flags          = MTK_JPEG_FMT_FLAG_DEC_OUTPUT,
43         },
44         {
45                 .fourcc         = V4L2_PIX_FMT_YUV420M,
46                 .h_sample       = {4, 2, 2},
47                 .v_sample       = {4, 2, 2},
48                 .colplanes      = 3,
49                 .h_align        = 5,
50                 .v_align        = 4,
51                 .flags          = MTK_JPEG_FMT_FLAG_DEC_CAPTURE,
52         },
53         {
54                 .fourcc         = V4L2_PIX_FMT_YUV422M,
55                 .h_sample       = {4, 2, 2},
56                 .v_sample       = {4, 4, 4},
57                 .colplanes      = 3,
58                 .h_align        = 5,
59                 .v_align        = 3,
60                 .flags          = MTK_JPEG_FMT_FLAG_DEC_CAPTURE,
61         },
62 };
63
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 };
70
71 struct mtk_jpeg_src_buf {
72         struct vb2_v4l2_buffer b;
73         struct list_head list;
74         int flags;
75         struct mtk_jpeg_dec_param dec_param;
76 };
77
78 static int debug;
79 module_param(debug, int, 0644);
80
81 static inline struct mtk_jpeg_ctx *mtk_jpeg_fh_to_ctx(struct v4l2_fh *fh)
82 {
83         return container_of(fh, struct mtk_jpeg_ctx, fh);
84 }
85
86 static inline struct mtk_jpeg_src_buf *mtk_jpeg_vb2_to_srcbuf(
87                                                         struct vb2_buffer *vb)
88 {
89         return container_of(to_vb2_v4l2_buffer(vb), struct mtk_jpeg_src_buf, b);
90 }
91
92 static int mtk_jpeg_querycap(struct file *file, void *priv,
93                              struct v4l2_capability *cap)
94 {
95         struct mtk_jpeg_dev *jpeg = video_drvdata(file);
96
97         strlcpy(cap->driver, MTK_JPEG_NAME " decoder", sizeof(cap->driver));
98         strlcpy(cap->card, MTK_JPEG_NAME " decoder", sizeof(cap->card));
99         snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
100                  dev_name(jpeg->dev));
101
102         return 0;
103 }
104
105 static int mtk_jpeg_enum_fmt(struct mtk_jpeg_fmt *mtk_jpeg_formats, int n,
106                              struct v4l2_fmtdesc *f, u32 type)
107 {
108         int i, num = 0;
109
110         for (i = 0; i < n; ++i) {
111                 if (mtk_jpeg_formats[i].flags & type) {
112                         if (num == f->index)
113                                 break;
114                         ++num;
115                 }
116         }
117
118         if (i >= n)
119                 return -EINVAL;
120
121         f->pixelformat = mtk_jpeg_formats[i].fourcc;
122
123         return 0;
124 }
125
126 static int mtk_jpeg_enum_fmt_vid_cap(struct file *file, void *priv,
127                                      struct v4l2_fmtdesc *f)
128 {
129         return mtk_jpeg_enum_fmt(mtk_jpeg_formats, MTK_JPEG_NUM_FORMATS, f,
130                                  MTK_JPEG_FMT_FLAG_DEC_CAPTURE);
131 }
132
133 static int mtk_jpeg_enum_fmt_vid_out(struct file *file, void *priv,
134                                      struct v4l2_fmtdesc *f)
135 {
136         return mtk_jpeg_enum_fmt(mtk_jpeg_formats, MTK_JPEG_NUM_FORMATS, f,
137                                  MTK_JPEG_FMT_FLAG_DEC_OUTPUT);
138 }
139
140 static struct mtk_jpeg_q_data *mtk_jpeg_get_q_data(struct mtk_jpeg_ctx *ctx,
141                                                    enum v4l2_buf_type type)
142 {
143         if (V4L2_TYPE_IS_OUTPUT(type))
144                 return &ctx->out_q;
145         return &ctx->cap_q;
146 }
147
148 static struct mtk_jpeg_fmt *mtk_jpeg_find_format(struct mtk_jpeg_ctx *ctx,
149                                                  u32 pixelformat,
150                                                  unsigned int fmt_type)
151 {
152         unsigned int k, fmt_flag;
153
154         fmt_flag = (fmt_type == MTK_JPEG_FMT_TYPE_OUTPUT) ?
155                    MTK_JPEG_FMT_FLAG_DEC_OUTPUT :
156                    MTK_JPEG_FMT_FLAG_DEC_CAPTURE;
157
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)
162                         return fmt;
163         }
164
165         return NULL;
166 }
167
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)
172 {
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         int i;
193
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         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
223         pix_mp->num_planes = fmt->colplanes;
224         pix_mp->pixelformat = fmt->fourcc;
225
226         if (q_type == MTK_JPEG_FMT_TYPE_OUTPUT) {
227                 struct v4l2_plane_pix_format *pfmt = &pix_mp->plane_fmt[0];
228
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);
233
234                 memset(pfmt->reserved, 0, sizeof(pfmt->reserved));
235                 pfmt->bytesperline = 0;
236                 /* Source size must be aligned to 128 */
237                 pfmt->sizeimage = mtk_jpeg_align(pfmt->sizeimage, 128);
238                 if (pfmt->sizeimage == 0)
239                         pfmt->sizeimage = MTK_JPEG_DEFAULT_SIZEIMAGE;
240                 goto end;
241         }
242
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);
248
249         for (i = 0; i < fmt->colplanes; i++) {
250                 struct v4l2_plane_pix_format *pfmt = &pix_mp->plane_fmt[i];
251                 u32 stride = pix_mp->width * fmt->h_sample[i] / 4;
252                 u32 h = pix_mp->height * fmt->v_sample[i] / 4;
253
254                 memset(pfmt->reserved, 0, sizeof(pfmt->reserved));
255                 pfmt->bytesperline = stride;
256                 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         }
268         return 0;
269 }
270
271 static int mtk_jpeg_g_fmt_vid_mplane(struct file *file, void *priv,
272                                      struct v4l2_format *f)
273 {
274         struct vb2_queue *vq;
275         struct mtk_jpeg_q_data *q_data = NULL;
276         struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
277         struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv);
278         struct mtk_jpeg_dev *jpeg = ctx->jpeg;
279         int i;
280
281         vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
282         if (!vq)
283                 return -EINVAL;
284
285         q_data = mtk_jpeg_get_q_data(ctx, f->type);
286
287         memset(pix_mp->reserved, 0, sizeof(pix_mp->reserved));
288         pix_mp->width = q_data->w;
289         pix_mp->height = q_data->h;
290         pix_mp->field = V4L2_FIELD_NONE;
291         pix_mp->pixelformat = q_data->fmt->fourcc;
292         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;
297
298         v4l2_dbg(1, debug, &jpeg->v4l2_dev, "(%d) g_fmt:%c%c%c%c wxh:%ux%u\n",
299                  f->type,
300                  (pix_mp->pixelformat & 0xff),
301                  (pix_mp->pixelformat >>  8 & 0xff),
302                  (pix_mp->pixelformat >> 16 & 0xff),
303                  (pix_mp->pixelformat >> 24 & 0xff),
304                  pix_mp->width, pix_mp->height);
305
306         for (i = 0; i < pix_mp->num_planes; i++) {
307                 struct v4l2_plane_pix_format *pfmt = &pix_mp->plane_fmt[i];
308
309                 pfmt->bytesperline = q_data->bytesperline[i];
310                 pfmt->sizeimage = q_data->sizeimage[i];
311                 memset(pfmt->reserved, 0, sizeof(pfmt->reserved));
312
313                 v4l2_dbg(1, debug, &jpeg->v4l2_dev,
314                          "plane[%d] bpl=%u, size=%u\n",
315                          i,
316                          pfmt->bytesperline,
317                          pfmt->sizeimage);
318         }
319         return 0;
320 }
321
322 static int mtk_jpeg_try_fmt_vid_cap_mplane(struct file *file, void *priv,
323                                            struct v4l2_format *f)
324 {
325         struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv);
326         struct mtk_jpeg_fmt *fmt;
327
328         fmt = mtk_jpeg_find_format(ctx, f->fmt.pix_mp.pixelformat,
329                                    MTK_JPEG_FMT_TYPE_CAPTURE);
330         if (!fmt)
331                 fmt = ctx->cap_q.fmt;
332
333         v4l2_dbg(2, debug, &ctx->jpeg->v4l2_dev, "(%d) try_fmt:%c%c%c%c\n",
334                  f->type,
335                  (fmt->fourcc & 0xff),
336                  (fmt->fourcc >>  8 & 0xff),
337                  (fmt->fourcc >> 16 & 0xff),
338                  (fmt->fourcc >> 24 & 0xff));
339
340         return mtk_jpeg_try_fmt_mplane(f, fmt, ctx, MTK_JPEG_FMT_TYPE_CAPTURE);
341 }
342
343 static int mtk_jpeg_try_fmt_vid_out_mplane(struct file *file, void *priv,
344                                            struct v4l2_format *f)
345 {
346         struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv);
347         struct mtk_jpeg_fmt *fmt;
348
349         fmt = mtk_jpeg_find_format(ctx, f->fmt.pix_mp.pixelformat,
350                                    MTK_JPEG_FMT_TYPE_OUTPUT);
351         if (!fmt)
352                 fmt = ctx->out_q.fmt;
353
354         v4l2_dbg(2, debug, &ctx->jpeg->v4l2_dev, "(%d) try_fmt:%c%c%c%c\n",
355                  f->type,
356                  (fmt->fourcc & 0xff),
357                  (fmt->fourcc >>  8 & 0xff),
358                  (fmt->fourcc >> 16 & 0xff),
359                  (fmt->fourcc >> 24 & 0xff));
360
361         return mtk_jpeg_try_fmt_mplane(f, fmt, ctx, MTK_JPEG_FMT_TYPE_OUTPUT);
362 }
363
364 static int mtk_jpeg_s_fmt_mplane(struct mtk_jpeg_ctx *ctx,
365                                  struct v4l2_format *f)
366 {
367         struct vb2_queue *vq;
368         struct mtk_jpeg_q_data *q_data = NULL;
369         struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
370         struct mtk_jpeg_dev *jpeg = ctx->jpeg;
371         unsigned int f_type;
372         int i;
373
374         vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
375         if (!vq)
376                 return -EINVAL;
377
378         q_data = mtk_jpeg_get_q_data(ctx, f->type);
379
380         if (vb2_is_busy(vq)) {
381                 v4l2_err(&jpeg->v4l2_dev, "queue busy\n");
382                 return -EBUSY;
383         }
384
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;
395
396         v4l2_dbg(1, debug, &jpeg->v4l2_dev, "(%d) s_fmt:%c%c%c%c wxh:%ux%u\n",
397                  f->type,
398                  (q_data->fmt->fourcc & 0xff),
399                  (q_data->fmt->fourcc >>  8 & 0xff),
400                  (q_data->fmt->fourcc >> 16 & 0xff),
401                  (q_data->fmt->fourcc >> 24 & 0xff),
402                  q_data->w, q_data->h);
403
404         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;
407
408                 v4l2_dbg(1, debug, &jpeg->v4l2_dev,
409                          "plane[%d] bpl=%u, size=%u\n",
410                          i, q_data->bytesperline[i], q_data->sizeimage[i]);
411         }
412
413         return 0;
414 }
415
416 static int mtk_jpeg_s_fmt_vid_out_mplane(struct file *file, void *priv,
417                                          struct v4l2_format *f)
418 {
419         int ret;
420
421         ret = mtk_jpeg_try_fmt_vid_out_mplane(file, priv, f);
422         if (ret)
423                 return ret;
424
425         return mtk_jpeg_s_fmt_mplane(mtk_jpeg_fh_to_ctx(priv), f);
426 }
427
428 static int mtk_jpeg_s_fmt_vid_cap_mplane(struct file *file, void *priv,
429                                          struct v4l2_format *f)
430 {
431         int ret;
432
433         ret = mtk_jpeg_try_fmt_vid_cap_mplane(file, priv, f);
434         if (ret)
435                 return ret;
436
437         return mtk_jpeg_s_fmt_mplane(mtk_jpeg_fh_to_ctx(priv), f);
438 }
439
440 static void mtk_jpeg_queue_src_chg_event(struct mtk_jpeg_ctx *ctx)
441 {
442         static const struct v4l2_event ev_src_ch = {
443                 .type = V4L2_EVENT_SOURCE_CHANGE,
444                 .u.src_change.changes =
445                 V4L2_EVENT_SRC_CH_RESOLUTION,
446         };
447
448         v4l2_event_queue_fh(&ctx->fh, &ev_src_ch);
449 }
450
451 static int mtk_jpeg_subscribe_event(struct v4l2_fh *fh,
452                                     const struct v4l2_event_subscription *sub)
453 {
454         switch (sub->type) {
455         case V4L2_EVENT_SOURCE_CHANGE:
456                 return v4l2_src_change_event_subscribe(fh, sub);
457         default:
458                 return -EINVAL;
459         }
460 }
461
462 static int mtk_jpeg_g_selection(struct file *file, void *priv,
463                                 struct v4l2_selection *s)
464 {
465         struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv);
466
467         if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
468                 return -EINVAL;
469
470         switch (s->target) {
471         case V4L2_SEL_TGT_COMPOSE:
472         case V4L2_SEL_TGT_COMPOSE_DEFAULT:
473                 s->r.width = ctx->out_q.w;
474                 s->r.height = ctx->out_q.h;
475                 s->r.left = 0;
476                 s->r.top = 0;
477                 break;
478         case V4L2_SEL_TGT_COMPOSE_BOUNDS:
479         case V4L2_SEL_TGT_COMPOSE_PADDED:
480                 s->r.width = ctx->cap_q.w;
481                 s->r.height = ctx->cap_q.h;
482                 s->r.left = 0;
483                 s->r.top = 0;
484                 break;
485         default:
486                 return -EINVAL;
487         }
488         return 0;
489 }
490
491 static int mtk_jpeg_s_selection(struct file *file, void *priv,
492                                 struct v4l2_selection *s)
493 {
494         struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv);
495
496         if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
497                 return -EINVAL;
498
499         switch (s->target) {
500         case V4L2_SEL_TGT_COMPOSE:
501                 s->r.left = 0;
502                 s->r.top = 0;
503                 s->r.width = ctx->out_q.w;
504                 s->r.height = ctx->out_q.h;
505                 break;
506         default:
507                 return -EINVAL;
508         }
509         return 0;
510 }
511
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 = {
538         .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,
541         .vidioc_try_fmt_vid_cap_mplane  = mtk_jpeg_try_fmt_vid_cap_mplane,
542         .vidioc_try_fmt_vid_out_mplane  = mtk_jpeg_try_fmt_vid_out_mplane,
543         .vidioc_g_fmt_vid_cap_mplane    = mtk_jpeg_g_fmt_vid_mplane,
544         .vidioc_g_fmt_vid_out_mplane    = mtk_jpeg_g_fmt_vid_mplane,
545         .vidioc_s_fmt_vid_cap_mplane    = mtk_jpeg_s_fmt_vid_cap_mplane,
546         .vidioc_s_fmt_vid_out_mplane    = mtk_jpeg_s_fmt_vid_out_mplane,
547         .vidioc_qbuf                    = mtk_jpeg_qbuf,
548         .vidioc_subscribe_event         = mtk_jpeg_subscribe_event,
549         .vidioc_g_selection             = mtk_jpeg_g_selection,
550         .vidioc_s_selection             = mtk_jpeg_s_selection,
551
552         .vidioc_create_bufs             = v4l2_m2m_ioctl_create_bufs,
553         .vidioc_prepare_buf             = v4l2_m2m_ioctl_prepare_buf,
554         .vidioc_reqbufs                 = v4l2_m2m_ioctl_reqbufs,
555         .vidioc_querybuf                = v4l2_m2m_ioctl_querybuf,
556         .vidioc_dqbuf                   = v4l2_m2m_ioctl_dqbuf,
557         .vidioc_expbuf                  = v4l2_m2m_ioctl_expbuf,
558         .vidioc_streamon                = v4l2_m2m_ioctl_streamon,
559         .vidioc_streamoff               = v4l2_m2m_ioctl_streamoff,
560
561         .vidioc_unsubscribe_event       = v4l2_event_unsubscribe,
562 };
563
564 static int mtk_jpeg_queue_setup(struct vb2_queue *q,
565                                 unsigned int *num_buffers,
566                                 unsigned int *num_planes,
567                                 unsigned int sizes[],
568                                 struct device *alloc_ctxs[])
569 {
570         struct mtk_jpeg_ctx *ctx = vb2_get_drv_priv(q);
571         struct mtk_jpeg_q_data *q_data = NULL;
572         struct mtk_jpeg_dev *jpeg = ctx->jpeg;
573         int i;
574
575         v4l2_dbg(1, debug, &jpeg->v4l2_dev, "(%d) buf_req count=%u\n",
576                  q->type, *num_buffers);
577
578         q_data = mtk_jpeg_get_q_data(ctx, q->type);
579         if (!q_data)
580                 return -EINVAL;
581
582         if (*num_planes) {
583                 for (i = 0; i < *num_planes; i++)
584                         if (sizes[i] < q_data->sizeimage[i])
585                                 return -EINVAL;
586                 return 0;
587         }
588
589         *num_planes = q_data->fmt->colplanes;
590         for (i = 0; i < q_data->fmt->colplanes; i++) {
591                 sizes[i] = q_data->sizeimage[i];
592                 v4l2_dbg(1, debug, &jpeg->v4l2_dev, "sizeimage[%d]=%u\n",
593                          i, sizes[i]);
594         }
595
596         return 0;
597 }
598
599 static int mtk_jpeg_buf_prepare(struct vb2_buffer *vb)
600 {
601         struct mtk_jpeg_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
602         struct mtk_jpeg_q_data *q_data = NULL;
603         int i;
604
605         q_data = mtk_jpeg_get_q_data(ctx, vb->vb2_queue->type);
606         if (!q_data)
607                 return -EINVAL;
608
609         for (i = 0; i < q_data->fmt->colplanes; i++)
610                 vb2_set_plane_payload(vb, i, q_data->sizeimage[i]);
611
612         return 0;
613 }
614
615 static bool mtk_jpeg_check_resolution_change(struct mtk_jpeg_ctx *ctx,
616                                              struct mtk_jpeg_dec_param *param)
617 {
618         struct mtk_jpeg_dev *jpeg = ctx->jpeg;
619         struct mtk_jpeg_q_data *q_data;
620
621         q_data = &ctx->out_q;
622         if (q_data->w != param->pic_w || q_data->h != param->pic_h) {
623                 v4l2_dbg(1, debug, &jpeg->v4l2_dev, "Picture size change\n");
624                 return true;
625         }
626
627         q_data = &ctx->cap_q;
628         if (q_data->fmt != mtk_jpeg_find_format(ctx, param->dst_fourcc,
629                                                 MTK_JPEG_FMT_TYPE_CAPTURE)) {
630                 v4l2_dbg(1, debug, &jpeg->v4l2_dev, "format change\n");
631                 return true;
632         }
633         return false;
634 }
635
636 static void mtk_jpeg_set_queue_data(struct mtk_jpeg_ctx *ctx,
637                                     struct mtk_jpeg_dec_param *param)
638 {
639         struct mtk_jpeg_dev *jpeg = ctx->jpeg;
640         struct mtk_jpeg_q_data *q_data;
641         int i;
642
643         q_data = &ctx->out_q;
644         q_data->w = param->pic_w;
645         q_data->h = param->pic_h;
646
647         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,
651                                            param->dst_fourcc,
652                                            MTK_JPEG_FMT_TYPE_CAPTURE);
653
654         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];
657         }
658
659         v4l2_dbg(1, debug, &jpeg->v4l2_dev,
660                  "set_parse cap:%c%c%c%c pic(%u, %u), buf(%u, %u)\n",
661                  (param->dst_fourcc & 0xff),
662                  (param->dst_fourcc >>  8 & 0xff),
663                  (param->dst_fourcc >> 16 & 0xff),
664                  (param->dst_fourcc >> 24 & 0xff),
665                  param->pic_w, param->pic_h,
666                  param->dec_w, param->dec_h);
667 }
668
669 static void mtk_jpeg_buf_queue(struct vb2_buffer *vb)
670 {
671         struct mtk_jpeg_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
672         struct mtk_jpeg_dec_param *param;
673         struct mtk_jpeg_dev *jpeg = ctx->jpeg;
674         struct mtk_jpeg_src_buf *jpeg_src_buf;
675         bool header_valid;
676
677         v4l2_dbg(2, debug, &jpeg->v4l2_dev, "(%d) buf_q id=%d, vb=%p\n",
678                  vb->vb2_queue->type, vb->index, vb);
679
680         if (vb->vb2_queue->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
681                 goto end;
682
683         jpeg_src_buf = mtk_jpeg_vb2_to_srcbuf(vb);
684         param = &jpeg_src_buf->dec_param;
685         memset(param, 0, sizeof(*param));
686
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         header_valid = mtk_jpeg_parse(param, (u8 *)vb2_plane_vaddr(vb, 0),
692                                       vb2_get_plane_payload(vb, 0));
693         if (!header_valid) {
694                 v4l2_err(&jpeg->v4l2_dev, "Header invalid.\n");
695                 vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
696                 return;
697         }
698
699         if (ctx->state == MTK_JPEG_INIT) {
700                 struct vb2_queue *dst_vq = v4l2_m2m_get_vq(
701                         ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
702
703                 mtk_jpeg_queue_src_chg_event(ctx);
704                 mtk_jpeg_set_queue_data(ctx, param);
705                 ctx->state = vb2_is_streaming(dst_vq) ?
706                                 MTK_JPEG_SOURCE_CHANGE : MTK_JPEG_RUNNING;
707         }
708 end:
709         v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, to_vb2_v4l2_buffer(vb));
710 }
711
712 static struct vb2_v4l2_buffer *mtk_jpeg_buf_remove(struct mtk_jpeg_ctx *ctx,
713                                  enum v4l2_buf_type type)
714 {
715         if (V4L2_TYPE_IS_OUTPUT(type))
716                 return v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
717         else
718                 return v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
719 }
720
721 static int mtk_jpeg_start_streaming(struct vb2_queue *q, unsigned int count)
722 {
723         struct mtk_jpeg_ctx *ctx = vb2_get_drv_priv(q);
724         struct vb2_v4l2_buffer *vb;
725         int ret = 0;
726
727         ret = pm_runtime_get_sync(ctx->jpeg->dev);
728         if (ret < 0)
729                 goto err;
730
731         return 0;
732 err:
733         while ((vb = mtk_jpeg_buf_remove(ctx, q->type)))
734                 v4l2_m2m_buf_done(vb, VB2_BUF_STATE_QUEUED);
735         return ret;
736 }
737
738 static void mtk_jpeg_stop_streaming(struct vb2_queue *q)
739 {
740         struct mtk_jpeg_ctx *ctx = vb2_get_drv_priv(q);
741         struct vb2_v4l2_buffer *vb;
742
743         /*
744          * STREAMOFF is an acknowledgment for source change event.
745          * Before STREAMOFF, we still have to return the old resolution and
746          * subsampling. Update capture queue when the stream is off.
747          */
748         if (ctx->state == MTK_JPEG_SOURCE_CHANGE &&
749             !V4L2_TYPE_IS_OUTPUT(q->type)) {
750                 struct mtk_jpeg_src_buf *src_buf;
751
752                 vb = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
753                 src_buf = mtk_jpeg_vb2_to_srcbuf(&vb->vb2_buf);
754                 mtk_jpeg_set_queue_data(ctx, &src_buf->dec_param);
755                 ctx->state = MTK_JPEG_RUNNING;
756         } else if (V4L2_TYPE_IS_OUTPUT(q->type)) {
757                 ctx->state = MTK_JPEG_INIT;
758         }
759
760         while ((vb = mtk_jpeg_buf_remove(ctx, q->type)))
761                 v4l2_m2m_buf_done(vb, VB2_BUF_STATE_ERROR);
762
763         pm_runtime_put_sync(ctx->jpeg->dev);
764 }
765
766 static const struct vb2_ops mtk_jpeg_qops = {
767         .queue_setup        = mtk_jpeg_queue_setup,
768         .buf_prepare        = mtk_jpeg_buf_prepare,
769         .buf_queue          = mtk_jpeg_buf_queue,
770         .wait_prepare       = vb2_ops_wait_prepare,
771         .wait_finish        = vb2_ops_wait_finish,
772         .start_streaming    = mtk_jpeg_start_streaming,
773         .stop_streaming     = mtk_jpeg_stop_streaming,
774 };
775
776 static void mtk_jpeg_set_dec_src(struct mtk_jpeg_ctx *ctx,
777                                  struct vb2_buffer *src_buf,
778                                  struct mtk_jpeg_bs *bs)
779 {
780         bs->str_addr = vb2_dma_contig_plane_dma_addr(src_buf, 0);
781         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);
784 }
785
786 static int mtk_jpeg_set_dec_dst(struct mtk_jpeg_ctx *ctx,
787                                 struct mtk_jpeg_dec_param *param,
788                                 struct vb2_buffer *dst_buf,
789                                 struct mtk_jpeg_fb *fb)
790 {
791         int i;
792
793         if (param->comp_num != dst_buf->num_planes) {
794                 dev_err(ctx->jpeg->dev, "plane number mismatch (%u != %u)\n",
795                         param->comp_num, dst_buf->num_planes);
796                 return -EINVAL;
797         }
798
799         for (i = 0; i < dst_buf->num_planes; i++) {
800                 if (vb2_plane_size(dst_buf, i) < param->comp_size[i]) {
801                         dev_err(ctx->jpeg->dev,
802                                 "buffer size is underflow (%lu < %u)\n",
803                                 vb2_plane_size(dst_buf, 0),
804                                 param->comp_size[i]);
805                         return -EINVAL;
806                 }
807                 fb->plane_addr[i] = vb2_dma_contig_plane_dma_addr(dst_buf, i);
808         }
809
810         return 0;
811 }
812
813 static void mtk_jpeg_device_run(void *priv)
814 {
815         struct mtk_jpeg_ctx *ctx = priv;
816         struct mtk_jpeg_dev *jpeg = ctx->jpeg;
817         struct vb2_v4l2_buffer *src_buf, *dst_buf;
818         enum vb2_buffer_state buf_state = VB2_BUF_STATE_ERROR;
819         unsigned long flags;
820         struct mtk_jpeg_src_buf *jpeg_src_buf;
821         struct mtk_jpeg_bs bs;
822         struct mtk_jpeg_fb fb;
823         int i;
824
825         src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
826         dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
827         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
836         if (mtk_jpeg_check_resolution_change(ctx, &jpeg_src_buf->dec_param)) {
837                 mtk_jpeg_queue_src_chg_event(ctx);
838                 ctx->state = MTK_JPEG_SOURCE_CHANGE;
839                 v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
840                 return;
841         }
842
843         mtk_jpeg_set_dec_src(ctx, &src_buf->vb2_buf, &bs);
844         if (mtk_jpeg_set_dec_dst(ctx, &jpeg_src_buf->dec_param, &dst_buf->vb2_buf, &fb))
845                 goto dec_end;
846
847         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,
850                                 &jpeg_src_buf->dec_param, &bs, &fb);
851
852         mtk_jpeg_dec_start(jpeg->dec_reg_base);
853         spin_unlock_irqrestore(&jpeg->hw_lock, flags);
854         return;
855
856 dec_end:
857         v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
858         v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
859         v4l2_m2m_buf_done(src_buf, buf_state);
860         v4l2_m2m_buf_done(dst_buf, buf_state);
861         v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
862 }
863
864 static int mtk_jpeg_job_ready(void *priv)
865 {
866         struct mtk_jpeg_ctx *ctx = priv;
867
868         return (ctx->state == MTK_JPEG_RUNNING) ? 1 : 0;
869 }
870
871 static void mtk_jpeg_job_abort(void *priv)
872 {
873 }
874
875 static const struct v4l2_m2m_ops mtk_jpeg_m2m_ops = {
876         .device_run = mtk_jpeg_device_run,
877         .job_ready  = mtk_jpeg_job_ready,
878         .job_abort  = mtk_jpeg_job_abort,
879 };
880
881 static int mtk_jpeg_queue_init(void *priv, struct vb2_queue *src_vq,
882                                struct vb2_queue *dst_vq)
883 {
884         struct mtk_jpeg_ctx *ctx = priv;
885         int ret;
886
887         src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
888         src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
889         src_vq->drv_priv = ctx;
890         src_vq->buf_struct_size = sizeof(struct mtk_jpeg_src_buf);
891         src_vq->ops = &mtk_jpeg_qops;
892         src_vq->mem_ops = &vb2_dma_contig_memops;
893         src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
894         src_vq->lock = &ctx->jpeg->lock;
895         src_vq->dev = ctx->jpeg->dev;
896         ret = vb2_queue_init(src_vq);
897         if (ret)
898                 return ret;
899
900         dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
901         dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
902         dst_vq->drv_priv = ctx;
903         dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
904         dst_vq->ops = &mtk_jpeg_qops;
905         dst_vq->mem_ops = &vb2_dma_contig_memops;
906         dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
907         dst_vq->lock = &ctx->jpeg->lock;
908         dst_vq->dev = ctx->jpeg->dev;
909         ret = vb2_queue_init(dst_vq);
910
911         return ret;
912 }
913
914 static void mtk_jpeg_clk_on(struct mtk_jpeg_dev *jpeg)
915 {
916         int ret;
917
918         ret = mtk_smi_larb_get(jpeg->larb);
919         if (ret)
920                 dev_err(jpeg->dev, "mtk_smi_larb_get larbvdec fail %d\n", ret);
921         clk_prepare_enable(jpeg->clk_jdec_smi);
922         clk_prepare_enable(jpeg->clk_jdec);
923 }
924
925 static void mtk_jpeg_clk_off(struct mtk_jpeg_dev *jpeg)
926 {
927         clk_disable_unprepare(jpeg->clk_jdec);
928         clk_disable_unprepare(jpeg->clk_jdec_smi);
929         mtk_smi_larb_put(jpeg->larb);
930 }
931
932 static irqreturn_t mtk_jpeg_dec_irq(int irq, void *priv)
933 {
934         struct mtk_jpeg_dev *jpeg = priv;
935         struct mtk_jpeg_ctx *ctx;
936         struct vb2_v4l2_buffer *src_buf, *dst_buf;
937         struct mtk_jpeg_src_buf *jpeg_src_buf;
938         enum vb2_buffer_state buf_state = VB2_BUF_STATE_ERROR;
939         u32     dec_irq_ret;
940         u32 dec_ret;
941         int i;
942
943         dec_ret = mtk_jpeg_dec_get_int_status(jpeg->dec_reg_base);
944         dec_irq_ret = mtk_jpeg_dec_enum_result(dec_ret);
945         ctx = v4l2_m2m_get_curr_priv(jpeg->m2m_dev);
946         if (!ctx) {
947                 v4l2_err(&jpeg->v4l2_dev, "Context is NULL\n");
948                 return IRQ_HANDLED;
949         }
950
951         src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
952         dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
953         jpeg_src_buf = mtk_jpeg_vb2_to_srcbuf(&src_buf->vb2_buf);
954
955         if (dec_irq_ret >= MTK_JPEG_DEC_RESULT_UNDERFLOW)
956                 mtk_jpeg_dec_reset(jpeg->dec_reg_base);
957
958         if (dec_irq_ret != MTK_JPEG_DEC_RESULT_EOF_DONE) {
959                 dev_err(jpeg->dev, "decode failed\n");
960                 goto dec_end;
961         }
962
963         for (i = 0; i < dst_buf->vb2_buf.num_planes; i++)
964                 vb2_set_plane_payload(&dst_buf->vb2_buf, i,
965                                       jpeg_src_buf->dec_param.comp_size[i]);
966
967         buf_state = VB2_BUF_STATE_DONE;
968
969 dec_end:
970         v4l2_m2m_buf_done(src_buf, buf_state);
971         v4l2_m2m_buf_done(dst_buf, buf_state);
972         v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
973         return IRQ_HANDLED;
974 }
975
976 static void mtk_jpeg_set_default_params(struct mtk_jpeg_ctx *ctx)
977 {
978         struct mtk_jpeg_q_data *q = &ctx->out_q;
979         int i;
980
981         ctx->colorspace = V4L2_COLORSPACE_JPEG,
982         ctx->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
983         ctx->quantization = V4L2_QUANTIZATION_DEFAULT;
984         ctx->xfer_func = V4L2_XFER_FUNC_DEFAULT;
985
986         q->fmt = mtk_jpeg_find_format(ctx, V4L2_PIX_FMT_JPEG,
987                                               MTK_JPEG_FMT_TYPE_OUTPUT);
988         q->w = MTK_JPEG_MIN_WIDTH;
989         q->h = MTK_JPEG_MIN_HEIGHT;
990         q->bytesperline[0] = 0;
991         q->sizeimage[0] = MTK_JPEG_DEFAULT_SIZEIMAGE;
992
993         q = &ctx->cap_q;
994         q->fmt = mtk_jpeg_find_format(ctx, V4L2_PIX_FMT_YUV420M,
995                                               MTK_JPEG_FMT_TYPE_CAPTURE);
996         q->w = MTK_JPEG_MIN_WIDTH;
997         q->h = MTK_JPEG_MIN_HEIGHT;
998
999         for (i = 0; i < q->fmt->colplanes; i++) {
1000                 u32 stride = q->w * q->fmt->h_sample[i] / 4;
1001                 u32 h = q->h * q->fmt->v_sample[i] / 4;
1002
1003                 q->bytesperline[i] = stride;
1004                 q->sizeimage[i] = stride * h;
1005         }
1006 }
1007
1008 static int mtk_jpeg_open(struct file *file)
1009 {
1010         struct mtk_jpeg_dev *jpeg = video_drvdata(file);
1011         struct video_device *vfd = video_devdata(file);
1012         struct mtk_jpeg_ctx *ctx;
1013         int ret = 0;
1014
1015         ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1016         if (!ctx)
1017                 return -ENOMEM;
1018
1019         if (mutex_lock_interruptible(&jpeg->lock)) {
1020                 ret = -ERESTARTSYS;
1021                 goto free;
1022         }
1023
1024         v4l2_fh_init(&ctx->fh, vfd);
1025         file->private_data = &ctx->fh;
1026         v4l2_fh_add(&ctx->fh);
1027
1028         ctx->jpeg = jpeg;
1029         ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(jpeg->m2m_dev, ctx,
1030                                             mtk_jpeg_queue_init);
1031         if (IS_ERR(ctx->fh.m2m_ctx)) {
1032                 ret = PTR_ERR(ctx->fh.m2m_ctx);
1033                 goto error;
1034         }
1035
1036         mtk_jpeg_set_default_params(ctx);
1037         mutex_unlock(&jpeg->lock);
1038         return 0;
1039
1040 error:
1041         v4l2_fh_del(&ctx->fh);
1042         v4l2_fh_exit(&ctx->fh);
1043         mutex_unlock(&jpeg->lock);
1044 free:
1045         kfree(ctx);
1046         return ret;
1047 }
1048
1049 static int mtk_jpeg_release(struct file *file)
1050 {
1051         struct mtk_jpeg_dev *jpeg = video_drvdata(file);
1052         struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(file->private_data);
1053
1054         mutex_lock(&jpeg->lock);
1055         v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
1056         v4l2_fh_del(&ctx->fh);
1057         v4l2_fh_exit(&ctx->fh);
1058         kfree(ctx);
1059         mutex_unlock(&jpeg->lock);
1060         return 0;
1061 }
1062
1063 static const struct v4l2_file_operations mtk_jpeg_fops = {
1064         .owner          = THIS_MODULE,
1065         .open           = mtk_jpeg_open,
1066         .release        = mtk_jpeg_release,
1067         .poll           = v4l2_m2m_fop_poll,
1068         .unlocked_ioctl = video_ioctl2,
1069         .mmap           = v4l2_m2m_fop_mmap,
1070 };
1071
1072 static int mtk_jpeg_clk_init(struct mtk_jpeg_dev *jpeg)
1073 {
1074         struct device_node *node;
1075         struct platform_device *pdev;
1076
1077         node = of_parse_phandle(jpeg->dev->of_node, "mediatek,larb", 0);
1078         if (!node)
1079                 return -EINVAL;
1080         pdev = of_find_device_by_node(node);
1081         if (WARN_ON(!pdev)) {
1082                 of_node_put(node);
1083                 return -EINVAL;
1084         }
1085         of_node_put(node);
1086
1087         jpeg->larb = &pdev->dev;
1088
1089         jpeg->clk_jdec = devm_clk_get(jpeg->dev, "jpgdec");
1090         if (IS_ERR(jpeg->clk_jdec))
1091                 return -EINVAL;
1092
1093         jpeg->clk_jdec_smi = devm_clk_get(jpeg->dev, "jpgdec-smi");
1094         if (IS_ERR(jpeg->clk_jdec_smi))
1095                 return -EINVAL;
1096
1097         return 0;
1098 }
1099
1100 static int mtk_jpeg_probe(struct platform_device *pdev)
1101 {
1102         struct mtk_jpeg_dev *jpeg;
1103         struct resource *res;
1104         int dec_irq;
1105         int ret;
1106
1107         jpeg = devm_kzalloc(&pdev->dev, sizeof(*jpeg), GFP_KERNEL);
1108         if (!jpeg)
1109                 return -ENOMEM;
1110
1111         mutex_init(&jpeg->lock);
1112         spin_lock_init(&jpeg->hw_lock);
1113         jpeg->dev = &pdev->dev;
1114
1115         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1116         jpeg->dec_reg_base = devm_ioremap_resource(&pdev->dev, res);
1117         if (IS_ERR(jpeg->dec_reg_base)) {
1118                 ret = PTR_ERR(jpeg->dec_reg_base);
1119                 return ret;
1120         }
1121
1122         res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1123         dec_irq = platform_get_irq(pdev, 0);
1124         if (!res || dec_irq < 0) {
1125                 dev_err(&pdev->dev, "Failed to get dec_irq %d.\n", dec_irq);
1126                 ret = -EINVAL;
1127                 return ret;
1128         }
1129
1130         ret = devm_request_irq(&pdev->dev, dec_irq, mtk_jpeg_dec_irq, 0,
1131                                pdev->name, jpeg);
1132         if (ret) {
1133                 dev_err(&pdev->dev, "Failed to request dec_irq %d (%d)\n",
1134                         dec_irq, ret);
1135                 ret = -EINVAL;
1136                 goto err_req_irq;
1137         }
1138
1139         ret = mtk_jpeg_clk_init(jpeg);
1140         if (ret) {
1141                 dev_err(&pdev->dev, "Failed to init clk, err %d\n", ret);
1142                 goto err_clk_init;
1143         }
1144
1145         ret = v4l2_device_register(&pdev->dev, &jpeg->v4l2_dev);
1146         if (ret) {
1147                 dev_err(&pdev->dev, "Failed to register v4l2 device\n");
1148                 ret = -EINVAL;
1149                 goto err_dev_register;
1150         }
1151
1152         jpeg->m2m_dev = v4l2_m2m_init(&mtk_jpeg_m2m_ops);
1153         if (IS_ERR(jpeg->m2m_dev)) {
1154                 v4l2_err(&jpeg->v4l2_dev, "Failed to init mem2mem device\n");
1155                 ret = PTR_ERR(jpeg->m2m_dev);
1156                 goto err_m2m_init;
1157         }
1158
1159         jpeg->dec_vdev = video_device_alloc();
1160         if (!jpeg->dec_vdev) {
1161                 ret = -ENOMEM;
1162                 goto err_dec_vdev_alloc;
1163         }
1164         snprintf(jpeg->dec_vdev->name, sizeof(jpeg->dec_vdev->name),
1165                  "%s-dec", MTK_JPEG_NAME);
1166         jpeg->dec_vdev->fops = &mtk_jpeg_fops;
1167         jpeg->dec_vdev->ioctl_ops = &mtk_jpeg_ioctl_ops;
1168         jpeg->dec_vdev->minor = -1;
1169         jpeg->dec_vdev->release = video_device_release;
1170         jpeg->dec_vdev->lock = &jpeg->lock;
1171         jpeg->dec_vdev->v4l2_dev = &jpeg->v4l2_dev;
1172         jpeg->dec_vdev->vfl_dir = VFL_DIR_M2M;
1173         jpeg->dec_vdev->device_caps = V4L2_CAP_STREAMING |
1174                                       V4L2_CAP_VIDEO_M2M_MPLANE;
1175
1176         ret = video_register_device(jpeg->dec_vdev, VFL_TYPE_GRABBER, 3);
1177         if (ret) {
1178                 v4l2_err(&jpeg->v4l2_dev, "Failed to register video device\n");
1179                 goto err_dec_vdev_register;
1180         }
1181
1182         video_set_drvdata(jpeg->dec_vdev, jpeg);
1183         v4l2_info(&jpeg->v4l2_dev,
1184                   "decoder device registered as /dev/video%d (%d,%d)\n",
1185                   jpeg->dec_vdev->num, VIDEO_MAJOR, jpeg->dec_vdev->minor);
1186
1187         platform_set_drvdata(pdev, jpeg);
1188
1189         pm_runtime_enable(&pdev->dev);
1190
1191         return 0;
1192
1193 err_dec_vdev_register:
1194         video_device_release(jpeg->dec_vdev);
1195
1196 err_dec_vdev_alloc:
1197         v4l2_m2m_release(jpeg->m2m_dev);
1198
1199 err_m2m_init:
1200         v4l2_device_unregister(&jpeg->v4l2_dev);
1201
1202 err_dev_register:
1203
1204 err_clk_init:
1205
1206 err_req_irq:
1207
1208         return ret;
1209 }
1210
1211 static int mtk_jpeg_remove(struct platform_device *pdev)
1212 {
1213         struct mtk_jpeg_dev *jpeg = platform_get_drvdata(pdev);
1214
1215         pm_runtime_disable(&pdev->dev);
1216         video_unregister_device(jpeg->dec_vdev);
1217         video_device_release(jpeg->dec_vdev);
1218         v4l2_m2m_release(jpeg->m2m_dev);
1219         v4l2_device_unregister(&jpeg->v4l2_dev);
1220
1221         return 0;
1222 }
1223
1224 static __maybe_unused int mtk_jpeg_pm_suspend(struct device *dev)
1225 {
1226         struct mtk_jpeg_dev *jpeg = dev_get_drvdata(dev);
1227
1228         mtk_jpeg_dec_reset(jpeg->dec_reg_base);
1229         mtk_jpeg_clk_off(jpeg);
1230
1231         return 0;
1232 }
1233
1234 static __maybe_unused int mtk_jpeg_pm_resume(struct device *dev)
1235 {
1236         struct mtk_jpeg_dev *jpeg = dev_get_drvdata(dev);
1237
1238         mtk_jpeg_clk_on(jpeg);
1239         mtk_jpeg_dec_reset(jpeg->dec_reg_base);
1240
1241         return 0;
1242 }
1243
1244 static __maybe_unused int mtk_jpeg_suspend(struct device *dev)
1245 {
1246         int ret;
1247
1248         if (pm_runtime_suspended(dev))
1249                 return 0;
1250
1251         ret = mtk_jpeg_pm_suspend(dev);
1252         return ret;
1253 }
1254
1255 static __maybe_unused int mtk_jpeg_resume(struct device *dev)
1256 {
1257         int ret;
1258
1259         if (pm_runtime_suspended(dev))
1260                 return 0;
1261
1262         ret = mtk_jpeg_pm_resume(dev);
1263
1264         return ret;
1265 }
1266
1267 static const struct dev_pm_ops mtk_jpeg_pm_ops = {
1268         SET_SYSTEM_SLEEP_PM_OPS(mtk_jpeg_suspend, mtk_jpeg_resume)
1269         SET_RUNTIME_PM_OPS(mtk_jpeg_pm_suspend, mtk_jpeg_pm_resume, NULL)
1270 };
1271
1272 static const struct of_device_id mtk_jpeg_match[] = {
1273         {
1274                 .compatible = "mediatek,mt8173-jpgdec",
1275                 .data       = NULL,
1276         },
1277         {
1278                 .compatible = "mediatek,mt2701-jpgdec",
1279                 .data       = NULL,
1280         },
1281         {},
1282 };
1283
1284 MODULE_DEVICE_TABLE(of, mtk_jpeg_match);
1285
1286 static struct platform_driver mtk_jpeg_driver = {
1287         .probe = mtk_jpeg_probe,
1288         .remove = mtk_jpeg_remove,
1289         .driver = {
1290                 .name           = MTK_JPEG_NAME,
1291                 .of_match_table = mtk_jpeg_match,
1292                 .pm             = &mtk_jpeg_pm_ops,
1293         },
1294 };
1295
1296 module_platform_driver(mtk_jpeg_driver);
1297
1298 MODULE_DESCRIPTION("MediaTek JPEG codec driver");
1299 MODULE_LICENSE("GPL v2");