1 /* linux/drivers/media/platform/s5p-jpeg/jpeg-core.c
3 * Copyright (c) 2011-2013 Samsung Electronics Co., Ltd.
4 * http://www.samsung.com
6 * Author: Andrzej Pietrasiewicz <andrzej.p@samsung.com>
7 * Author: Jacek Anaszewski <j.anaszewski@samsung.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
14 #include <linux/clk.h>
15 #include <linux/err.h>
16 #include <linux/gfp.h>
17 #include <linux/interrupt.h>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
22 #include <linux/platform_device.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/slab.h>
25 #include <linux/spinlock.h>
26 #include <linux/string.h>
27 #include <media/v4l2-mem2mem.h>
28 #include <media/v4l2-ioctl.h>
29 #include <media/videobuf2-core.h>
30 #include <media/videobuf2-dma-contig.h>
32 #include "jpeg-core.h"
33 #include "jpeg-hw-s5p.h"
34 #include "jpeg-hw-exynos4.h"
35 #include "jpeg-regs.h"
37 static struct s5p_jpeg_fmt sjpeg_formats[] = {
40 .fourcc = V4L2_PIX_FMT_JPEG,
41 .flags = SJPEG_FMT_FLAG_ENC_CAPTURE |
42 SJPEG_FMT_FLAG_DEC_OUTPUT |
44 SJPEG_FMT_FLAG_EXYNOS4,
47 .name = "YUV 4:2:2 packed, YCbYCr",
48 .fourcc = V4L2_PIX_FMT_YUYV,
53 .flags = SJPEG_FMT_FLAG_ENC_OUTPUT |
54 SJPEG_FMT_FLAG_DEC_CAPTURE |
57 .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_422,
60 .name = "YUV 4:2:2 packed, YCbYCr",
61 .fourcc = V4L2_PIX_FMT_YUYV,
66 .flags = SJPEG_FMT_FLAG_ENC_OUTPUT |
67 SJPEG_FMT_FLAG_DEC_CAPTURE |
68 SJPEG_FMT_FLAG_EXYNOS4 |
70 .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_422,
73 .name = "YUV 4:2:2 packed, YCrYCb",
74 .fourcc = V4L2_PIX_FMT_YVYU,
79 .flags = SJPEG_FMT_FLAG_ENC_OUTPUT |
80 SJPEG_FMT_FLAG_DEC_CAPTURE |
81 SJPEG_FMT_FLAG_EXYNOS4 |
83 .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_422,
87 .fourcc = V4L2_PIX_FMT_RGB565,
92 .flags = SJPEG_FMT_FLAG_ENC_OUTPUT |
93 SJPEG_FMT_FLAG_DEC_CAPTURE |
94 SJPEG_FMT_FLAG_EXYNOS4 |
96 .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_444,
100 .fourcc = V4L2_PIX_FMT_RGB565,
105 .flags = SJPEG_FMT_FLAG_ENC_OUTPUT |
108 .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_444,
111 .name = "ARGB8888, 32 bpp",
112 .fourcc = V4L2_PIX_FMT_RGB32,
117 .flags = SJPEG_FMT_FLAG_ENC_OUTPUT |
118 SJPEG_FMT_FLAG_DEC_CAPTURE |
119 SJPEG_FMT_FLAG_EXYNOS4 |
121 .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_444,
124 .name = "YUV 4:4:4 planar, Y/CbCr",
125 .fourcc = V4L2_PIX_FMT_NV24,
130 .flags = SJPEG_FMT_FLAG_ENC_OUTPUT |
131 SJPEG_FMT_FLAG_DEC_CAPTURE |
132 SJPEG_FMT_FLAG_EXYNOS4 |
134 .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_444,
137 .name = "YUV 4:4:4 planar, Y/CrCb",
138 .fourcc = V4L2_PIX_FMT_NV42,
143 .flags = SJPEG_FMT_FLAG_ENC_OUTPUT |
144 SJPEG_FMT_FLAG_DEC_CAPTURE |
145 SJPEG_FMT_FLAG_EXYNOS4 |
147 .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_444,
150 .name = "YUV 4:2:2 planar, Y/CrCb",
151 .fourcc = V4L2_PIX_FMT_NV61,
156 .flags = SJPEG_FMT_FLAG_ENC_OUTPUT |
157 SJPEG_FMT_FLAG_DEC_CAPTURE |
158 SJPEG_FMT_FLAG_EXYNOS4 |
160 .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_422,
163 .name = "YUV 4:2:2 planar, Y/CbCr",
164 .fourcc = V4L2_PIX_FMT_NV16,
169 .flags = SJPEG_FMT_FLAG_ENC_OUTPUT |
170 SJPEG_FMT_FLAG_DEC_CAPTURE |
171 SJPEG_FMT_FLAG_EXYNOS4 |
173 .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_422,
176 .name = "YUV 4:2:0 planar, Y/CbCr",
177 .fourcc = V4L2_PIX_FMT_NV12,
182 .flags = SJPEG_FMT_FLAG_ENC_OUTPUT |
183 SJPEG_FMT_FLAG_DEC_CAPTURE |
184 SJPEG_FMT_FLAG_EXYNOS4 |
186 .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_420,
189 .name = "YUV 4:2:0 planar, Y/CbCr",
190 .fourcc = V4L2_PIX_FMT_NV12,
195 .flags = SJPEG_FMT_FLAG_ENC_OUTPUT |
196 SJPEG_FMT_FLAG_DEC_CAPTURE |
199 .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_420,
202 .name = "YUV 4:2:0 planar, Y/CrCb",
203 .fourcc = V4L2_PIX_FMT_NV21,
208 .flags = SJPEG_FMT_FLAG_ENC_OUTPUT |
209 SJPEG_FMT_FLAG_DEC_CAPTURE |
210 SJPEG_FMT_FLAG_EXYNOS4 |
212 .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_420,
215 .name = "YUV 4:2:0 contiguous 3-planar, Y/Cb/Cr",
216 .fourcc = V4L2_PIX_FMT_YUV420,
221 .flags = SJPEG_FMT_FLAG_ENC_OUTPUT |
222 SJPEG_FMT_FLAG_DEC_CAPTURE |
223 SJPEG_FMT_FLAG_EXYNOS4 |
225 .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_420,
229 .fourcc = V4L2_PIX_FMT_GREY,
232 .flags = SJPEG_FMT_FLAG_ENC_OUTPUT |
233 SJPEG_FMT_FLAG_DEC_CAPTURE |
234 SJPEG_FMT_FLAG_EXYNOS4 |
236 .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_GRAY,
239 #define SJPEG_NUM_FORMATS ARRAY_SIZE(sjpeg_formats)
241 static const unsigned char qtbl_luminance[4][64] = {
242 {/*level 0 - high compression quality */
243 20, 16, 25, 39, 50, 46, 62, 68,
244 16, 18, 23, 38, 38, 53, 65, 68,
245 25, 23, 31, 38, 53, 65, 68, 68,
246 39, 38, 38, 53, 65, 68, 68, 68,
247 50, 38, 53, 65, 68, 68, 68, 68,
248 46, 53, 65, 68, 68, 68, 68, 68,
249 62, 65, 68, 68, 68, 68, 68, 68,
250 68, 68, 68, 68, 68, 68, 68, 68
253 16, 11, 11, 16, 23, 27, 31, 30,
254 11, 12, 12, 15, 20, 23, 23, 30,
255 11, 12, 13, 16, 23, 26, 35, 47,
256 16, 15, 16, 23, 26, 37, 47, 64,
257 23, 20, 23, 26, 39, 51, 64, 64,
258 27, 23, 26, 37, 51, 64, 64, 64,
259 31, 23, 35, 47, 64, 64, 64, 64,
260 30, 30, 47, 64, 64, 64, 64, 64
263 12, 8, 8, 12, 17, 21, 24, 23,
264 8, 9, 9, 11, 15, 19, 18, 23,
265 8, 9, 10, 12, 19, 20, 27, 36,
266 12, 11, 12, 21, 20, 28, 36, 53,
267 17, 15, 19, 20, 30, 39, 51, 59,
268 21, 19, 20, 28, 39, 51, 59, 59,
269 24, 18, 27, 36, 51, 59, 59, 59,
270 23, 23, 36, 53, 59, 59, 59, 59
272 {/* level 3 - low compression quality */
273 8, 6, 6, 8, 12, 14, 16, 17,
274 6, 6, 6, 8, 10, 13, 12, 15,
275 6, 6, 7, 8, 13, 14, 18, 24,
276 8, 8, 8, 14, 13, 19, 24, 35,
277 12, 10, 13, 13, 20, 26, 34, 39,
278 14, 13, 14, 19, 26, 34, 39, 39,
279 16, 12, 18, 24, 34, 39, 39, 39,
280 17, 15, 24, 35, 39, 39, 39, 39
284 static const unsigned char qtbl_chrominance[4][64] = {
285 {/*level 0 - high compression quality */
286 21, 25, 32, 38, 54, 68, 68, 68,
287 25, 28, 24, 38, 54, 68, 68, 68,
288 32, 24, 32, 43, 66, 68, 68, 68,
289 38, 38, 43, 53, 68, 68, 68, 68,
290 54, 54, 66, 68, 68, 68, 68, 68,
291 68, 68, 68, 68, 68, 68, 68, 68,
292 68, 68, 68, 68, 68, 68, 68, 68,
293 68, 68, 68, 68, 68, 68, 68, 68
296 17, 15, 17, 21, 20, 26, 38, 48,
297 15, 19, 18, 17, 20, 26, 35, 43,
298 17, 18, 20, 22, 26, 30, 46, 53,
299 21, 17, 22, 28, 30, 39, 53, 64,
300 20, 20, 26, 30, 39, 48, 64, 64,
301 26, 26, 30, 39, 48, 63, 64, 64,
302 38, 35, 46, 53, 64, 64, 64, 64,
303 48, 43, 53, 64, 64, 64, 64, 64
306 13, 11, 13, 16, 20, 20, 29, 37,
307 11, 14, 14, 14, 16, 20, 26, 32,
308 13, 14, 15, 17, 20, 23, 35, 40,
309 16, 14, 17, 21, 23, 30, 40, 50,
310 20, 16, 20, 23, 30, 37, 50, 59,
311 20, 20, 23, 30, 37, 48, 59, 59,
312 29, 26, 35, 40, 50, 59, 59, 59,
313 37, 32, 40, 50, 59, 59, 59, 59
315 {/* level 3 - low compression quality */
316 9, 8, 9, 11, 14, 17, 19, 24,
317 8, 10, 9, 11, 14, 13, 17, 22,
318 9, 9, 13, 14, 13, 15, 23, 26,
319 11, 11, 14, 14, 15, 20, 26, 33,
320 14, 14, 13, 15, 20, 24, 33, 39,
321 17, 13, 15, 20, 24, 32, 39, 39,
322 19, 17, 23, 26, 33, 39, 39, 39,
323 24, 22, 26, 33, 39, 39, 39, 39
327 static const unsigned char hdctbl0[16] = {
328 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0
331 static const unsigned char hdctblg0[12] = {
332 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb
334 static const unsigned char hactbl0[16] = {
335 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d
337 static const unsigned char hactblg0[162] = {
338 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
339 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
340 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
341 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
342 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
343 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
344 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
345 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
346 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
347 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
348 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
349 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
350 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
351 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
352 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
353 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
354 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
355 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
356 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
357 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
362 * Fourcc downgrade schema lookup tables for 422 and 420
363 * chroma subsampling - fourcc on each position maps on the
364 * fourcc from the table fourcc_to_dwngrd_schema_id which allows
365 * to get the most suitable fourcc counterpart for the given
366 * downgraded subsampling property.
368 static const u32 subs422_fourcc_dwngrd_schema[] = {
373 static const u32 subs420_fourcc_dwngrd_schema[] = {
387 * Lookup table for translation of a fourcc to the position
388 * of its downgraded counterpart in the *fourcc_dwngrd_schema
391 static const u32 fourcc_to_dwngrd_schema_id[] = {
404 static int s5p_jpeg_get_dwngrd_sch_id_by_fourcc(u32 fourcc)
407 for (i = 0; i < ARRAY_SIZE(fourcc_to_dwngrd_schema_id); ++i) {
408 if (fourcc_to_dwngrd_schema_id[i] == fourcc)
415 static int s5p_jpeg_adjust_fourcc_to_subsampling(
416 enum v4l2_jpeg_chroma_subsampling subs,
419 struct s5p_jpeg_ctx *ctx)
423 if (ctx->subsampling != V4L2_JPEG_CHROMA_SUBSAMPLING_GRAY) {
425 s5p_jpeg_get_dwngrd_sch_id_by_fourcc(in_fourcc);
426 if (dwngrd_sch_id < 0)
430 switch (ctx->subsampling) {
431 case V4L2_JPEG_CHROMA_SUBSAMPLING_GRAY:
432 *out_fourcc = V4L2_PIX_FMT_GREY;
434 case V4L2_JPEG_CHROMA_SUBSAMPLING_420:
436 ARRAY_SIZE(subs420_fourcc_dwngrd_schema) - 1)
438 *out_fourcc = subs420_fourcc_dwngrd_schema[dwngrd_sch_id];
440 case V4L2_JPEG_CHROMA_SUBSAMPLING_422:
442 ARRAY_SIZE(subs422_fourcc_dwngrd_schema) - 1)
444 *out_fourcc = subs422_fourcc_dwngrd_schema[dwngrd_sch_id];
447 *out_fourcc = V4L2_PIX_FMT_GREY;
454 static int exynos4x12_decoded_subsampling[] = {
455 V4L2_JPEG_CHROMA_SUBSAMPLING_GRAY,
456 V4L2_JPEG_CHROMA_SUBSAMPLING_444,
457 V4L2_JPEG_CHROMA_SUBSAMPLING_422,
458 V4L2_JPEG_CHROMA_SUBSAMPLING_420,
461 static inline struct s5p_jpeg_ctx *ctrl_to_ctx(struct v4l2_ctrl *c)
463 return container_of(c->handler, struct s5p_jpeg_ctx, ctrl_handler);
466 static inline struct s5p_jpeg_ctx *fh_to_ctx(struct v4l2_fh *fh)
468 return container_of(fh, struct s5p_jpeg_ctx, fh);
471 static int s5p_jpeg_to_user_subsampling(struct s5p_jpeg_ctx *ctx)
473 WARN_ON(ctx->subsampling > 3);
475 if (ctx->jpeg->variant->version == SJPEG_S5P) {
476 if (ctx->subsampling > 2)
477 return V4L2_JPEG_CHROMA_SUBSAMPLING_GRAY;
478 return ctx->subsampling;
480 if (ctx->subsampling > 2)
481 return V4L2_JPEG_CHROMA_SUBSAMPLING_420;
482 return exynos4x12_decoded_subsampling[ctx->subsampling];
486 static inline void s5p_jpeg_set_qtbl(void __iomem *regs,
487 const unsigned char *qtbl,
488 unsigned long tab, int len)
492 for (i = 0; i < len; i++)
493 writel((unsigned int)qtbl[i], regs + tab + (i * 0x04));
496 static inline void s5p_jpeg_set_qtbl_lum(void __iomem *regs, int quality)
498 /* this driver fills quantisation table 0 with data for luma */
499 s5p_jpeg_set_qtbl(regs, qtbl_luminance[quality],
500 S5P_JPG_QTBL_CONTENT(0),
501 ARRAY_SIZE(qtbl_luminance[quality]));
504 static inline void s5p_jpeg_set_qtbl_chr(void __iomem *regs, int quality)
506 /* this driver fills quantisation table 1 with data for chroma */
507 s5p_jpeg_set_qtbl(regs, qtbl_chrominance[quality],
508 S5P_JPG_QTBL_CONTENT(1),
509 ARRAY_SIZE(qtbl_chrominance[quality]));
512 static inline void s5p_jpeg_set_htbl(void __iomem *regs,
513 const unsigned char *htbl,
514 unsigned long tab, int len)
518 for (i = 0; i < len; i++)
519 writel((unsigned int)htbl[i], regs + tab + (i * 0x04));
522 static inline void s5p_jpeg_set_hdctbl(void __iomem *regs)
524 /* this driver fills table 0 for this component */
525 s5p_jpeg_set_htbl(regs, hdctbl0, S5P_JPG_HDCTBL(0),
526 ARRAY_SIZE(hdctbl0));
529 static inline void s5p_jpeg_set_hdctblg(void __iomem *regs)
531 /* this driver fills table 0 for this component */
532 s5p_jpeg_set_htbl(regs, hdctblg0, S5P_JPG_HDCTBLG(0),
533 ARRAY_SIZE(hdctblg0));
536 static inline void s5p_jpeg_set_hactbl(void __iomem *regs)
538 /* this driver fills table 0 for this component */
539 s5p_jpeg_set_htbl(regs, hactbl0, S5P_JPG_HACTBL(0),
540 ARRAY_SIZE(hactbl0));
543 static inline void s5p_jpeg_set_hactblg(void __iomem *regs)
545 /* this driver fills table 0 for this component */
546 s5p_jpeg_set_htbl(regs, hactblg0, S5P_JPG_HACTBLG(0),
547 ARRAY_SIZE(hactblg0));
550 static inline void exynos4_jpeg_set_tbl(void __iomem *regs,
551 const unsigned char *tbl,
552 unsigned long tab, int len)
557 for (i = 0; i < len; i += 4) {
562 writel(dword, regs + tab + i);
566 static inline void exynos4_jpeg_set_qtbl_lum(void __iomem *regs, int quality)
568 /* this driver fills quantisation table 0 with data for luma */
569 exynos4_jpeg_set_tbl(regs, qtbl_luminance[quality],
570 EXYNOS4_QTBL_CONTENT(0),
571 ARRAY_SIZE(qtbl_luminance[quality]));
574 static inline void exynos4_jpeg_set_qtbl_chr(void __iomem *regs, int quality)
576 /* this driver fills quantisation table 1 with data for chroma */
577 exynos4_jpeg_set_tbl(regs, qtbl_chrominance[quality],
578 EXYNOS4_QTBL_CONTENT(1),
579 ARRAY_SIZE(qtbl_chrominance[quality]));
582 void exynos4_jpeg_set_huff_tbl(void __iomem *base)
584 exynos4_jpeg_set_tbl(base, hdctbl0, EXYNOS4_HUFF_TBL_HDCLL,
585 ARRAY_SIZE(hdctbl0));
586 exynos4_jpeg_set_tbl(base, hdctbl0, EXYNOS4_HUFF_TBL_HDCCL,
587 ARRAY_SIZE(hdctbl0));
588 exynos4_jpeg_set_tbl(base, hdctblg0, EXYNOS4_HUFF_TBL_HDCLV,
589 ARRAY_SIZE(hdctblg0));
590 exynos4_jpeg_set_tbl(base, hdctblg0, EXYNOS4_HUFF_TBL_HDCCV,
591 ARRAY_SIZE(hdctblg0));
592 exynos4_jpeg_set_tbl(base, hactbl0, EXYNOS4_HUFF_TBL_HACLL,
593 ARRAY_SIZE(hactbl0));
594 exynos4_jpeg_set_tbl(base, hactbl0, EXYNOS4_HUFF_TBL_HACCL,
595 ARRAY_SIZE(hactbl0));
596 exynos4_jpeg_set_tbl(base, hactblg0, EXYNOS4_HUFF_TBL_HACLV,
597 ARRAY_SIZE(hactblg0));
598 exynos4_jpeg_set_tbl(base, hactblg0, EXYNOS4_HUFF_TBL_HACCV,
599 ARRAY_SIZE(hactblg0));
603 * ============================================================================
604 * Device file operations
605 * ============================================================================
608 static int queue_init(void *priv, struct vb2_queue *src_vq,
609 struct vb2_queue *dst_vq);
610 static struct s5p_jpeg_fmt *s5p_jpeg_find_format(struct s5p_jpeg_ctx *ctx,
611 __u32 pixelformat, unsigned int fmt_type);
612 static int s5p_jpeg_controls_create(struct s5p_jpeg_ctx *ctx);
614 static int s5p_jpeg_open(struct file *file)
616 struct s5p_jpeg *jpeg = video_drvdata(file);
617 struct video_device *vfd = video_devdata(file);
618 struct s5p_jpeg_ctx *ctx;
619 struct s5p_jpeg_fmt *out_fmt, *cap_fmt;
622 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
626 if (mutex_lock_interruptible(&jpeg->lock)) {
631 v4l2_fh_init(&ctx->fh, vfd);
632 /* Use separate control handler per file handle */
633 ctx->fh.ctrl_handler = &ctx->ctrl_handler;
634 file->private_data = &ctx->fh;
635 v4l2_fh_add(&ctx->fh);
638 if (vfd == jpeg->vfd_encoder) {
639 ctx->mode = S5P_JPEG_ENCODE;
640 out_fmt = s5p_jpeg_find_format(ctx, V4L2_PIX_FMT_RGB565,
642 cap_fmt = s5p_jpeg_find_format(ctx, V4L2_PIX_FMT_JPEG,
645 ctx->mode = S5P_JPEG_DECODE;
646 out_fmt = s5p_jpeg_find_format(ctx, V4L2_PIX_FMT_JPEG,
648 cap_fmt = s5p_jpeg_find_format(ctx, V4L2_PIX_FMT_YUYV,
652 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(jpeg->m2m_dev, ctx, queue_init);
653 if (IS_ERR(ctx->fh.m2m_ctx)) {
654 ret = PTR_ERR(ctx->fh.m2m_ctx);
658 ctx->out_q.fmt = out_fmt;
659 ctx->cap_q.fmt = cap_fmt;
661 ret = s5p_jpeg_controls_create(ctx);
665 mutex_unlock(&jpeg->lock);
669 v4l2_fh_del(&ctx->fh);
670 v4l2_fh_exit(&ctx->fh);
671 mutex_unlock(&jpeg->lock);
677 static int s5p_jpeg_release(struct file *file)
679 struct s5p_jpeg *jpeg = video_drvdata(file);
680 struct s5p_jpeg_ctx *ctx = fh_to_ctx(file->private_data);
682 mutex_lock(&jpeg->lock);
683 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
684 v4l2_ctrl_handler_free(&ctx->ctrl_handler);
685 v4l2_fh_del(&ctx->fh);
686 v4l2_fh_exit(&ctx->fh);
688 mutex_unlock(&jpeg->lock);
693 static const struct v4l2_file_operations s5p_jpeg_fops = {
694 .owner = THIS_MODULE,
695 .open = s5p_jpeg_open,
696 .release = s5p_jpeg_release,
697 .poll = v4l2_m2m_fop_poll,
698 .unlocked_ioctl = video_ioctl2,
699 .mmap = v4l2_m2m_fop_mmap,
703 * ============================================================================
704 * video ioctl operations
705 * ============================================================================
708 static int get_byte(struct s5p_jpeg_buffer *buf)
710 if (buf->curr >= buf->size)
713 return ((unsigned char *)buf->data)[buf->curr++];
716 static int get_word_be(struct s5p_jpeg_buffer *buf, unsigned int *word)
721 byte = get_byte(buf);
725 byte = get_byte(buf);
728 *word = (unsigned int)byte | temp;
732 static void skip(struct s5p_jpeg_buffer *buf, long len)
741 static bool s5p_jpeg_parse_hdr(struct s5p_jpeg_q_data *result,
742 unsigned long buffer, unsigned long size,
743 struct s5p_jpeg_ctx *ctx)
745 int c, components, notfound;
746 unsigned int height, width, word, subsampling = 0;
748 struct s5p_jpeg_buffer jpeg_buffer;
750 jpeg_buffer.size = size;
751 jpeg_buffer.data = buffer;
752 jpeg_buffer.curr = 0;
756 c = get_byte(&jpeg_buffer);
762 c = get_byte(&jpeg_buffer);
770 /* SOF0: baseline JPEG */
772 if (get_word_be(&jpeg_buffer, &word))
774 if (get_byte(&jpeg_buffer) == -1)
776 if (get_word_be(&jpeg_buffer, &height))
778 if (get_word_be(&jpeg_buffer, &width))
780 components = get_byte(&jpeg_buffer);
781 if (components == -1)
785 if (components == 1) {
788 skip(&jpeg_buffer, 1);
789 subsampling = get_byte(&jpeg_buffer);
790 skip(&jpeg_buffer, 1);
793 skip(&jpeg_buffer, components * 2);
796 /* skip payload-less markers */
797 case RST ... RST + 7:
803 /* skip uninteresting payload markers */
805 if (get_word_be(&jpeg_buffer, &word))
807 length = (long)word - 2;
808 skip(&jpeg_buffer, length);
814 result->size = components;
816 switch (subsampling) {
818 ctx->subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_444;
821 ctx->subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_422;
824 ctx->subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_420;
827 ctx->subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_GRAY;
836 static int s5p_jpeg_querycap(struct file *file, void *priv,
837 struct v4l2_capability *cap)
839 struct s5p_jpeg_ctx *ctx = fh_to_ctx(priv);
841 if (ctx->mode == S5P_JPEG_ENCODE) {
842 strlcpy(cap->driver, S5P_JPEG_M2M_NAME " encoder",
843 sizeof(cap->driver));
844 strlcpy(cap->card, S5P_JPEG_M2M_NAME " encoder",
847 strlcpy(cap->driver, S5P_JPEG_M2M_NAME " decoder",
848 sizeof(cap->driver));
849 strlcpy(cap->card, S5P_JPEG_M2M_NAME " decoder",
852 cap->bus_info[0] = 0;
854 * This is only a mem-to-mem video device. The capture and output
855 * device capability flags are left only for backward compatibility
856 * and are scheduled for removal.
858 cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_M2M |
859 V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT;
863 static int enum_fmt(struct s5p_jpeg_fmt *sjpeg_formats, int n,
864 struct v4l2_fmtdesc *f, u32 type)
868 for (i = 0; i < n; ++i) {
869 if (sjpeg_formats[i].flags & type) {
870 /* index-th format of type type found ? */
873 /* Correct type but haven't reached our index yet,
874 * just increment per-type index */
879 /* Format not found */
883 strlcpy(f->description, sjpeg_formats[i].name, sizeof(f->description));
884 f->pixelformat = sjpeg_formats[i].fourcc;
889 static int s5p_jpeg_enum_fmt_vid_cap(struct file *file, void *priv,
890 struct v4l2_fmtdesc *f)
892 struct s5p_jpeg_ctx *ctx = fh_to_ctx(priv);
894 if (ctx->mode == S5P_JPEG_ENCODE)
895 return enum_fmt(sjpeg_formats, SJPEG_NUM_FORMATS, f,
896 SJPEG_FMT_FLAG_ENC_CAPTURE);
898 return enum_fmt(sjpeg_formats, SJPEG_NUM_FORMATS, f,
899 SJPEG_FMT_FLAG_DEC_CAPTURE);
902 static int s5p_jpeg_enum_fmt_vid_out(struct file *file, void *priv,
903 struct v4l2_fmtdesc *f)
905 struct s5p_jpeg_ctx *ctx = fh_to_ctx(priv);
907 if (ctx->mode == S5P_JPEG_ENCODE)
908 return enum_fmt(sjpeg_formats, SJPEG_NUM_FORMATS, f,
909 SJPEG_FMT_FLAG_ENC_OUTPUT);
911 return enum_fmt(sjpeg_formats, SJPEG_NUM_FORMATS, f,
912 SJPEG_FMT_FLAG_DEC_OUTPUT);
915 static struct s5p_jpeg_q_data *get_q_data(struct s5p_jpeg_ctx *ctx,
916 enum v4l2_buf_type type)
918 if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
920 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
926 static int s5p_jpeg_g_fmt(struct file *file, void *priv, struct v4l2_format *f)
928 struct vb2_queue *vq;
929 struct s5p_jpeg_q_data *q_data = NULL;
930 struct v4l2_pix_format *pix = &f->fmt.pix;
931 struct s5p_jpeg_ctx *ct = fh_to_ctx(priv);
933 vq = v4l2_m2m_get_vq(ct->fh.m2m_ctx, f->type);
937 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
938 ct->mode == S5P_JPEG_DECODE && !ct->hdr_parsed)
940 q_data = get_q_data(ct, f->type);
941 BUG_ON(q_data == NULL);
943 pix->width = q_data->w;
944 pix->height = q_data->h;
945 pix->field = V4L2_FIELD_NONE;
946 pix->pixelformat = q_data->fmt->fourcc;
947 pix->bytesperline = 0;
948 if (q_data->fmt->fourcc != V4L2_PIX_FMT_JPEG) {
950 if (q_data->fmt->colplanes == 1)
951 bpl = (bpl * q_data->fmt->depth) >> 3;
952 pix->bytesperline = bpl;
954 pix->sizeimage = q_data->size;
959 static struct s5p_jpeg_fmt *s5p_jpeg_find_format(struct s5p_jpeg_ctx *ctx,
960 u32 pixelformat, unsigned int fmt_type)
962 unsigned int k, fmt_flag;
964 if (ctx->mode == S5P_JPEG_ENCODE)
965 fmt_flag = (fmt_type == FMT_TYPE_OUTPUT) ?
966 SJPEG_FMT_FLAG_ENC_OUTPUT :
967 SJPEG_FMT_FLAG_ENC_CAPTURE;
969 fmt_flag = (fmt_type == FMT_TYPE_OUTPUT) ?
970 SJPEG_FMT_FLAG_DEC_OUTPUT :
971 SJPEG_FMT_FLAG_DEC_CAPTURE;
973 for (k = 0; k < ARRAY_SIZE(sjpeg_formats); k++) {
974 struct s5p_jpeg_fmt *fmt = &sjpeg_formats[k];
975 if (fmt->fourcc == pixelformat &&
976 fmt->flags & fmt_flag &&
977 fmt->flags & ctx->jpeg->variant->fmt_ver_flag) {
985 static void jpeg_bound_align_image(u32 *w, unsigned int wmin, unsigned int wmax,
987 u32 *h, unsigned int hmin, unsigned int hmax,
990 int width, height, w_step, h_step;
995 w_step = 1 << walign;
996 h_step = 1 << halign;
997 v4l_bound_align_image(w, wmin, wmax, walign, h, hmin, hmax, halign, 0);
999 if (*w < width && (*w + w_step) < wmax)
1001 if (*h < height && (*h + h_step) < hmax)
1006 static int vidioc_try_fmt(struct v4l2_format *f, struct s5p_jpeg_fmt *fmt,
1007 struct s5p_jpeg_ctx *ctx, int q_type)
1009 struct v4l2_pix_format *pix = &f->fmt.pix;
1011 if (pix->field == V4L2_FIELD_ANY)
1012 pix->field = V4L2_FIELD_NONE;
1013 else if (pix->field != V4L2_FIELD_NONE)
1016 /* V4L2 specification suggests the driver corrects the format struct
1017 * if any of the dimensions is unsupported */
1018 if (q_type == FMT_TYPE_OUTPUT)
1019 jpeg_bound_align_image(&pix->width, S5P_JPEG_MIN_WIDTH,
1020 S5P_JPEG_MAX_WIDTH, 0,
1021 &pix->height, S5P_JPEG_MIN_HEIGHT,
1022 S5P_JPEG_MAX_HEIGHT, 0);
1024 jpeg_bound_align_image(&pix->width, S5P_JPEG_MIN_WIDTH,
1025 S5P_JPEG_MAX_WIDTH, fmt->h_align,
1026 &pix->height, S5P_JPEG_MIN_HEIGHT,
1027 S5P_JPEG_MAX_HEIGHT, fmt->v_align);
1029 if (fmt->fourcc == V4L2_PIX_FMT_JPEG) {
1030 if (pix->sizeimage <= 0)
1031 pix->sizeimage = PAGE_SIZE;
1032 pix->bytesperline = 0;
1034 u32 bpl = pix->bytesperline;
1036 if (fmt->colplanes > 1 && bpl < pix->width)
1037 bpl = pix->width; /* planar */
1039 if (fmt->colplanes == 1 && /* packed */
1040 (bpl << 3) / fmt->depth < pix->width)
1041 bpl = (pix->width * fmt->depth) >> 3;
1043 pix->bytesperline = bpl;
1044 pix->sizeimage = (pix->width * pix->height * fmt->depth) >> 3;
1050 static int s5p_jpeg_try_fmt_vid_cap(struct file *file, void *priv,
1051 struct v4l2_format *f)
1053 struct s5p_jpeg_ctx *ctx = fh_to_ctx(priv);
1054 struct v4l2_pix_format *pix = &f->fmt.pix;
1055 struct s5p_jpeg_fmt *fmt;
1058 fmt = s5p_jpeg_find_format(ctx, f->fmt.pix.pixelformat,
1061 v4l2_err(&ctx->jpeg->v4l2_dev,
1062 "Fourcc format (0x%08x) invalid.\n",
1063 f->fmt.pix.pixelformat);
1068 * The exynos4x12 device requires resulting YUV image
1069 * subsampling not to be lower than the input jpeg subsampling.
1070 * If this requirement is not met then downgrade the requested
1071 * capture format to the one with subsampling equal to the input jpeg.
1073 if ((ctx->jpeg->variant->version == SJPEG_EXYNOS4) &&
1074 (ctx->mode == S5P_JPEG_DECODE) &&
1075 (fmt->flags & SJPEG_FMT_NON_RGB) &&
1076 (fmt->subsampling < ctx->subsampling)) {
1077 ret = s5p_jpeg_adjust_fourcc_to_subsampling(ctx->subsampling,
1082 pix->pixelformat = V4L2_PIX_FMT_GREY;
1084 fmt = s5p_jpeg_find_format(ctx, pix->pixelformat,
1088 return vidioc_try_fmt(f, fmt, ctx, FMT_TYPE_CAPTURE);
1091 static int s5p_jpeg_try_fmt_vid_out(struct file *file, void *priv,
1092 struct v4l2_format *f)
1094 struct s5p_jpeg_ctx *ctx = fh_to_ctx(priv);
1095 struct s5p_jpeg_fmt *fmt;
1097 fmt = s5p_jpeg_find_format(ctx, f->fmt.pix.pixelformat,
1100 v4l2_err(&ctx->jpeg->v4l2_dev,
1101 "Fourcc format (0x%08x) invalid.\n",
1102 f->fmt.pix.pixelformat);
1106 return vidioc_try_fmt(f, fmt, ctx, FMT_TYPE_OUTPUT);
1109 static int exynos4_jpeg_get_output_buffer_size(struct s5p_jpeg_ctx *ctx,
1110 struct v4l2_format *f,
1113 struct v4l2_pix_format *pix = &f->fmt.pix;
1114 u32 pix_fmt = f->fmt.pix.pixelformat;
1115 int w = pix->width, h = pix->height, wh_align;
1117 if (pix_fmt == V4L2_PIX_FMT_RGB32 ||
1118 pix_fmt == V4L2_PIX_FMT_NV24 ||
1119 pix_fmt == V4L2_PIX_FMT_NV42 ||
1120 pix_fmt == V4L2_PIX_FMT_NV12 ||
1121 pix_fmt == V4L2_PIX_FMT_NV21 ||
1122 pix_fmt == V4L2_PIX_FMT_YUV420)
1127 jpeg_bound_align_image(&w, S5P_JPEG_MIN_WIDTH,
1128 S5P_JPEG_MAX_WIDTH, wh_align,
1129 &h, S5P_JPEG_MIN_HEIGHT,
1130 S5P_JPEG_MAX_HEIGHT, wh_align);
1132 return w * h * fmt_depth >> 3;
1135 static int s5p_jpeg_s_fmt(struct s5p_jpeg_ctx *ct, struct v4l2_format *f)
1137 struct vb2_queue *vq;
1138 struct s5p_jpeg_q_data *q_data = NULL;
1139 struct v4l2_pix_format *pix = &f->fmt.pix;
1140 struct v4l2_ctrl *ctrl_subs;
1141 unsigned int f_type;
1143 vq = v4l2_m2m_get_vq(ct->fh.m2m_ctx, f->type);
1147 q_data = get_q_data(ct, f->type);
1148 BUG_ON(q_data == NULL);
1150 if (vb2_is_busy(vq)) {
1151 v4l2_err(&ct->jpeg->v4l2_dev, "%s queue busy\n", __func__);
1155 f_type = V4L2_TYPE_IS_OUTPUT(f->type) ?
1156 FMT_TYPE_OUTPUT : FMT_TYPE_CAPTURE;
1158 q_data->fmt = s5p_jpeg_find_format(ct, pix->pixelformat, f_type);
1159 q_data->w = pix->width;
1160 q_data->h = pix->height;
1161 if (q_data->fmt->fourcc != V4L2_PIX_FMT_JPEG) {
1163 * During encoding Exynos4x12 SoCs access wider memory area
1164 * than it results from Image_x and Image_y values written to
1165 * the JPEG_IMAGE_SIZE register. In order to avoid sysmmu
1166 * page fault calculate proper buffer size in such a case.
1168 if (ct->jpeg->variant->version == SJPEG_EXYNOS4 &&
1169 f_type == FMT_TYPE_OUTPUT && ct->mode == S5P_JPEG_ENCODE)
1170 q_data->size = exynos4_jpeg_get_output_buffer_size(ct,
1172 q_data->fmt->depth);
1174 q_data->size = q_data->w * q_data->h *
1175 q_data->fmt->depth >> 3;
1177 q_data->size = pix->sizeimage;
1180 if (f_type == FMT_TYPE_OUTPUT) {
1181 ctrl_subs = v4l2_ctrl_find(&ct->ctrl_handler,
1182 V4L2_CID_JPEG_CHROMA_SUBSAMPLING);
1184 v4l2_ctrl_s_ctrl(ctrl_subs, q_data->fmt->subsampling);
1190 static int s5p_jpeg_s_fmt_vid_cap(struct file *file, void *priv,
1191 struct v4l2_format *f)
1195 ret = s5p_jpeg_try_fmt_vid_cap(file, priv, f);
1199 return s5p_jpeg_s_fmt(fh_to_ctx(priv), f);
1202 static int s5p_jpeg_s_fmt_vid_out(struct file *file, void *priv,
1203 struct v4l2_format *f)
1207 ret = s5p_jpeg_try_fmt_vid_out(file, priv, f);
1211 return s5p_jpeg_s_fmt(fh_to_ctx(priv), f);
1214 static int s5p_jpeg_g_selection(struct file *file, void *priv,
1215 struct v4l2_selection *s)
1217 struct s5p_jpeg_ctx *ctx = fh_to_ctx(priv);
1219 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
1220 s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1223 /* For JPEG blob active == default == bounds */
1224 switch (s->target) {
1225 case V4L2_SEL_TGT_CROP:
1226 case V4L2_SEL_TGT_CROP_BOUNDS:
1227 case V4L2_SEL_TGT_CROP_DEFAULT:
1228 case V4L2_SEL_TGT_COMPOSE:
1229 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
1230 s->r.width = ctx->out_q.w;
1231 s->r.height = ctx->out_q.h;
1233 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1234 case V4L2_SEL_TGT_COMPOSE_PADDED:
1235 s->r.width = ctx->cap_q.w;
1236 s->r.height = ctx->cap_q.h;
1250 static int s5p_jpeg_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
1252 struct s5p_jpeg_ctx *ctx = ctrl_to_ctx(ctrl);
1253 struct s5p_jpeg *jpeg = ctx->jpeg;
1254 unsigned long flags;
1257 case V4L2_CID_JPEG_CHROMA_SUBSAMPLING:
1258 spin_lock_irqsave(&jpeg->slock, flags);
1259 ctrl->val = s5p_jpeg_to_user_subsampling(ctx);
1260 spin_unlock_irqrestore(&jpeg->slock, flags);
1267 static int s5p_jpeg_try_ctrl(struct v4l2_ctrl *ctrl)
1269 struct s5p_jpeg_ctx *ctx = ctrl_to_ctx(ctrl);
1270 unsigned long flags;
1273 spin_lock_irqsave(&ctx->jpeg->slock, flags);
1275 if (ctrl->id == V4L2_CID_JPEG_CHROMA_SUBSAMPLING) {
1276 if (ctx->jpeg->variant->version == SJPEG_S5P)
1279 * The exynos4x12 device requires input raw image fourcc
1280 * to be V4L2_PIX_FMT_GREY if gray jpeg format
1283 if (ctx->out_q.fmt->fourcc != V4L2_PIX_FMT_GREY &&
1284 ctrl->val == V4L2_JPEG_CHROMA_SUBSAMPLING_GRAY) {
1289 * The exynos4x12 device requires resulting jpeg subsampling
1290 * not to be lower than the input raw image subsampling.
1292 if (ctx->out_q.fmt->subsampling > ctrl->val)
1293 ctrl->val = ctx->out_q.fmt->subsampling;
1297 spin_unlock_irqrestore(&ctx->jpeg->slock, flags);
1301 static int s5p_jpeg_s_ctrl(struct v4l2_ctrl *ctrl)
1303 struct s5p_jpeg_ctx *ctx = ctrl_to_ctx(ctrl);
1304 unsigned long flags;
1306 spin_lock_irqsave(&ctx->jpeg->slock, flags);
1309 case V4L2_CID_JPEG_COMPRESSION_QUALITY:
1310 ctx->compr_quality = ctrl->val;
1312 case V4L2_CID_JPEG_RESTART_INTERVAL:
1313 ctx->restart_interval = ctrl->val;
1315 case V4L2_CID_JPEG_CHROMA_SUBSAMPLING:
1316 ctx->subsampling = ctrl->val;
1320 spin_unlock_irqrestore(&ctx->jpeg->slock, flags);
1324 static const struct v4l2_ctrl_ops s5p_jpeg_ctrl_ops = {
1325 .g_volatile_ctrl = s5p_jpeg_g_volatile_ctrl,
1326 .try_ctrl = s5p_jpeg_try_ctrl,
1327 .s_ctrl = s5p_jpeg_s_ctrl,
1330 static int s5p_jpeg_controls_create(struct s5p_jpeg_ctx *ctx)
1332 unsigned int mask = ~0x27; /* 444, 422, 420, GRAY */
1333 struct v4l2_ctrl *ctrl;
1336 v4l2_ctrl_handler_init(&ctx->ctrl_handler, 3);
1338 if (ctx->mode == S5P_JPEG_ENCODE) {
1339 v4l2_ctrl_new_std(&ctx->ctrl_handler, &s5p_jpeg_ctrl_ops,
1340 V4L2_CID_JPEG_COMPRESSION_QUALITY,
1341 0, 3, 1, S5P_JPEG_COMPR_QUAL_WORST);
1343 v4l2_ctrl_new_std(&ctx->ctrl_handler, &s5p_jpeg_ctrl_ops,
1344 V4L2_CID_JPEG_RESTART_INTERVAL,
1346 if (ctx->jpeg->variant->version == SJPEG_S5P)
1347 mask = ~0x06; /* 422, 420 */
1350 ctrl = v4l2_ctrl_new_std_menu(&ctx->ctrl_handler, &s5p_jpeg_ctrl_ops,
1351 V4L2_CID_JPEG_CHROMA_SUBSAMPLING,
1352 V4L2_JPEG_CHROMA_SUBSAMPLING_GRAY, mask,
1353 V4L2_JPEG_CHROMA_SUBSAMPLING_422);
1355 if (ctx->ctrl_handler.error) {
1356 ret = ctx->ctrl_handler.error;
1360 if (ctx->mode == S5P_JPEG_DECODE)
1361 ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE |
1362 V4L2_CTRL_FLAG_READ_ONLY;
1364 ret = v4l2_ctrl_handler_setup(&ctx->ctrl_handler);
1371 v4l2_ctrl_handler_free(&ctx->ctrl_handler);
1375 static const struct v4l2_ioctl_ops s5p_jpeg_ioctl_ops = {
1376 .vidioc_querycap = s5p_jpeg_querycap,
1378 .vidioc_enum_fmt_vid_cap = s5p_jpeg_enum_fmt_vid_cap,
1379 .vidioc_enum_fmt_vid_out = s5p_jpeg_enum_fmt_vid_out,
1381 .vidioc_g_fmt_vid_cap = s5p_jpeg_g_fmt,
1382 .vidioc_g_fmt_vid_out = s5p_jpeg_g_fmt,
1384 .vidioc_try_fmt_vid_cap = s5p_jpeg_try_fmt_vid_cap,
1385 .vidioc_try_fmt_vid_out = s5p_jpeg_try_fmt_vid_out,
1387 .vidioc_s_fmt_vid_cap = s5p_jpeg_s_fmt_vid_cap,
1388 .vidioc_s_fmt_vid_out = s5p_jpeg_s_fmt_vid_out,
1390 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
1391 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
1392 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
1393 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
1395 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
1396 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
1398 .vidioc_g_selection = s5p_jpeg_g_selection,
1402 * ============================================================================
1404 * ============================================================================
1407 static void s5p_jpeg_device_run(void *priv)
1409 struct s5p_jpeg_ctx *ctx = priv;
1410 struct s5p_jpeg *jpeg = ctx->jpeg;
1411 struct vb2_buffer *src_buf, *dst_buf;
1412 unsigned long src_addr, dst_addr, flags;
1414 spin_lock_irqsave(&ctx->jpeg->slock, flags);
1416 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
1417 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
1418 src_addr = vb2_dma_contig_plane_dma_addr(src_buf, 0);
1419 dst_addr = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
1421 s5p_jpeg_reset(jpeg->regs);
1422 s5p_jpeg_poweron(jpeg->regs);
1423 s5p_jpeg_proc_mode(jpeg->regs, ctx->mode);
1424 if (ctx->mode == S5P_JPEG_ENCODE) {
1425 if (ctx->out_q.fmt->fourcc == V4L2_PIX_FMT_RGB565)
1426 s5p_jpeg_input_raw_mode(jpeg->regs,
1427 S5P_JPEG_RAW_IN_565);
1429 s5p_jpeg_input_raw_mode(jpeg->regs,
1430 S5P_JPEG_RAW_IN_422);
1431 s5p_jpeg_subsampling_mode(jpeg->regs, ctx->subsampling);
1432 s5p_jpeg_dri(jpeg->regs, ctx->restart_interval);
1433 s5p_jpeg_x(jpeg->regs, ctx->out_q.w);
1434 s5p_jpeg_y(jpeg->regs, ctx->out_q.h);
1435 s5p_jpeg_imgadr(jpeg->regs, src_addr);
1436 s5p_jpeg_jpgadr(jpeg->regs, dst_addr);
1438 /* ultimately comes from sizeimage from userspace */
1439 s5p_jpeg_enc_stream_int(jpeg->regs, ctx->cap_q.size);
1441 /* JPEG RGB to YCbCr conversion matrix */
1442 s5p_jpeg_coef(jpeg->regs, 1, 1, S5P_JPEG_COEF11);
1443 s5p_jpeg_coef(jpeg->regs, 1, 2, S5P_JPEG_COEF12);
1444 s5p_jpeg_coef(jpeg->regs, 1, 3, S5P_JPEG_COEF13);
1445 s5p_jpeg_coef(jpeg->regs, 2, 1, S5P_JPEG_COEF21);
1446 s5p_jpeg_coef(jpeg->regs, 2, 2, S5P_JPEG_COEF22);
1447 s5p_jpeg_coef(jpeg->regs, 2, 3, S5P_JPEG_COEF23);
1448 s5p_jpeg_coef(jpeg->regs, 3, 1, S5P_JPEG_COEF31);
1449 s5p_jpeg_coef(jpeg->regs, 3, 2, S5P_JPEG_COEF32);
1450 s5p_jpeg_coef(jpeg->regs, 3, 3, S5P_JPEG_COEF33);
1453 * JPEG IP allows storing 4 quantization tables
1454 * We fill table 0 for luma and table 1 for chroma
1456 s5p_jpeg_set_qtbl_lum(jpeg->regs, ctx->compr_quality);
1457 s5p_jpeg_set_qtbl_chr(jpeg->regs, ctx->compr_quality);
1458 /* use table 0 for Y */
1459 s5p_jpeg_qtbl(jpeg->regs, 1, 0);
1460 /* use table 1 for Cb and Cr*/
1461 s5p_jpeg_qtbl(jpeg->regs, 2, 1);
1462 s5p_jpeg_qtbl(jpeg->regs, 3, 1);
1464 /* Y, Cb, Cr use Huffman table 0 */
1465 s5p_jpeg_htbl_ac(jpeg->regs, 1);
1466 s5p_jpeg_htbl_dc(jpeg->regs, 1);
1467 s5p_jpeg_htbl_ac(jpeg->regs, 2);
1468 s5p_jpeg_htbl_dc(jpeg->regs, 2);
1469 s5p_jpeg_htbl_ac(jpeg->regs, 3);
1470 s5p_jpeg_htbl_dc(jpeg->regs, 3);
1471 } else { /* S5P_JPEG_DECODE */
1472 s5p_jpeg_rst_int_enable(jpeg->regs, true);
1473 s5p_jpeg_data_num_int_enable(jpeg->regs, true);
1474 s5p_jpeg_final_mcu_num_int_enable(jpeg->regs, true);
1475 if (ctx->cap_q.fmt->fourcc == V4L2_PIX_FMT_YUYV)
1476 s5p_jpeg_outform_raw(jpeg->regs, S5P_JPEG_RAW_OUT_422);
1478 s5p_jpeg_outform_raw(jpeg->regs, S5P_JPEG_RAW_OUT_420);
1479 s5p_jpeg_jpgadr(jpeg->regs, src_addr);
1480 s5p_jpeg_imgadr(jpeg->regs, dst_addr);
1483 s5p_jpeg_start(jpeg->regs);
1485 spin_unlock_irqrestore(&ctx->jpeg->slock, flags);
1488 static void exynos4_jpeg_set_img_addr(struct s5p_jpeg_ctx *ctx)
1490 struct s5p_jpeg *jpeg = ctx->jpeg;
1491 struct s5p_jpeg_fmt *fmt;
1492 struct vb2_buffer *vb;
1493 struct s5p_jpeg_addr jpeg_addr;
1494 u32 pix_size, padding_bytes = 0;
1496 pix_size = ctx->cap_q.w * ctx->cap_q.h;
1498 if (ctx->mode == S5P_JPEG_ENCODE) {
1499 vb = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
1500 fmt = ctx->out_q.fmt;
1501 if (ctx->out_q.w % 2 && fmt->h_align > 0)
1502 padding_bytes = ctx->out_q.h;
1504 fmt = ctx->cap_q.fmt;
1505 vb = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
1508 jpeg_addr.y = vb2_dma_contig_plane_dma_addr(vb, 0);
1510 if (fmt->colplanes == 2) {
1511 jpeg_addr.cb = jpeg_addr.y + pix_size - padding_bytes;
1512 } else if (fmt->colplanes == 3) {
1513 jpeg_addr.cb = jpeg_addr.y + pix_size;
1514 if (fmt->fourcc == V4L2_PIX_FMT_YUV420)
1515 jpeg_addr.cr = jpeg_addr.cb + pix_size / 4;
1517 jpeg_addr.cr = jpeg_addr.cb + pix_size / 2;
1520 exynos4_jpeg_set_frame_buf_address(jpeg->regs, &jpeg_addr);
1523 static void exynos4_jpeg_set_jpeg_addr(struct s5p_jpeg_ctx *ctx)
1525 struct s5p_jpeg *jpeg = ctx->jpeg;
1526 struct vb2_buffer *vb;
1527 unsigned int jpeg_addr = 0;
1529 if (ctx->mode == S5P_JPEG_ENCODE)
1530 vb = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
1532 vb = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
1534 jpeg_addr = vb2_dma_contig_plane_dma_addr(vb, 0);
1535 exynos4_jpeg_set_stream_buf_address(jpeg->regs, jpeg_addr);
1538 static void exynos4_jpeg_device_run(void *priv)
1540 struct s5p_jpeg_ctx *ctx = priv;
1541 struct s5p_jpeg *jpeg = ctx->jpeg;
1542 unsigned int bitstream_size;
1543 unsigned long flags;
1545 spin_lock_irqsave(&ctx->jpeg->slock, flags);
1547 if (ctx->mode == S5P_JPEG_ENCODE) {
1548 exynos4_jpeg_sw_reset(jpeg->regs);
1549 exynos4_jpeg_set_interrupt(jpeg->regs);
1550 exynos4_jpeg_set_huf_table_enable(jpeg->regs, 1);
1552 exynos4_jpeg_set_huff_tbl(jpeg->regs);
1555 * JPEG IP allows storing 4 quantization tables
1556 * We fill table 0 for luma and table 1 for chroma
1558 exynos4_jpeg_set_qtbl_lum(jpeg->regs, ctx->compr_quality);
1559 exynos4_jpeg_set_qtbl_chr(jpeg->regs, ctx->compr_quality);
1561 exynos4_jpeg_set_encode_tbl_select(jpeg->regs,
1562 ctx->compr_quality);
1563 exynos4_jpeg_set_stream_size(jpeg->regs, ctx->cap_q.w,
1566 exynos4_jpeg_set_enc_out_fmt(jpeg->regs, ctx->subsampling);
1567 exynos4_jpeg_set_img_fmt(jpeg->regs, ctx->out_q.fmt->fourcc);
1568 exynos4_jpeg_set_img_addr(ctx);
1569 exynos4_jpeg_set_jpeg_addr(ctx);
1570 exynos4_jpeg_set_encode_hoff_cnt(jpeg->regs,
1571 ctx->out_q.fmt->fourcc);
1573 exynos4_jpeg_sw_reset(jpeg->regs);
1574 exynos4_jpeg_set_interrupt(jpeg->regs);
1575 exynos4_jpeg_set_img_addr(ctx);
1576 exynos4_jpeg_set_jpeg_addr(ctx);
1577 exynos4_jpeg_set_img_fmt(jpeg->regs, ctx->cap_q.fmt->fourcc);
1579 bitstream_size = DIV_ROUND_UP(ctx->out_q.size, 32);
1581 exynos4_jpeg_set_dec_bitstream_size(jpeg->regs, bitstream_size);
1584 exynos4_jpeg_set_enc_dec_mode(jpeg->regs, ctx->mode);
1586 spin_unlock_irqrestore(&ctx->jpeg->slock, flags);
1589 static int s5p_jpeg_job_ready(void *priv)
1591 struct s5p_jpeg_ctx *ctx = priv;
1593 if (ctx->mode == S5P_JPEG_DECODE)
1594 return ctx->hdr_parsed;
1598 static void s5p_jpeg_job_abort(void *priv)
1602 static struct v4l2_m2m_ops s5p_jpeg_m2m_ops = {
1603 .device_run = s5p_jpeg_device_run,
1604 .job_ready = s5p_jpeg_job_ready,
1605 .job_abort = s5p_jpeg_job_abort,
1608 static struct v4l2_m2m_ops exynos4_jpeg_m2m_ops = {
1609 .device_run = exynos4_jpeg_device_run,
1610 .job_ready = s5p_jpeg_job_ready,
1611 .job_abort = s5p_jpeg_job_abort,
1615 * ============================================================================
1617 * ============================================================================
1620 static int s5p_jpeg_queue_setup(struct vb2_queue *vq,
1621 const struct v4l2_format *fmt,
1622 unsigned int *nbuffers, unsigned int *nplanes,
1623 unsigned int sizes[], void *alloc_ctxs[])
1625 struct s5p_jpeg_ctx *ctx = vb2_get_drv_priv(vq);
1626 struct s5p_jpeg_q_data *q_data = NULL;
1627 unsigned int size, count = *nbuffers;
1629 q_data = get_q_data(ctx, vq->type);
1630 BUG_ON(q_data == NULL);
1632 size = q_data->size;
1635 * header is parsed during decoding and parsed information stored
1636 * in the context so we do not allow another buffer to overwrite it
1638 if (ctx->mode == S5P_JPEG_DECODE)
1644 alloc_ctxs[0] = ctx->jpeg->alloc_ctx;
1649 static int s5p_jpeg_buf_prepare(struct vb2_buffer *vb)
1651 struct s5p_jpeg_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1652 struct s5p_jpeg_q_data *q_data = NULL;
1654 q_data = get_q_data(ctx, vb->vb2_queue->type);
1655 BUG_ON(q_data == NULL);
1657 if (vb2_plane_size(vb, 0) < q_data->size) {
1658 pr_err("%s data will not fit into plane (%lu < %lu)\n",
1659 __func__, vb2_plane_size(vb, 0),
1660 (long)q_data->size);
1664 vb2_set_plane_payload(vb, 0, q_data->size);
1669 static void s5p_jpeg_buf_queue(struct vb2_buffer *vb)
1671 struct s5p_jpeg_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1673 if (ctx->mode == S5P_JPEG_DECODE &&
1674 vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1675 struct s5p_jpeg_q_data tmp, *q_data;
1676 ctx->hdr_parsed = s5p_jpeg_parse_hdr(&tmp,
1677 (unsigned long)vb2_plane_vaddr(vb, 0),
1678 min((unsigned long)ctx->out_q.size,
1679 vb2_get_plane_payload(vb, 0)), ctx);
1680 if (!ctx->hdr_parsed) {
1681 vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
1685 q_data = &ctx->out_q;
1689 q_data = &ctx->cap_q;
1694 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
1697 static int s5p_jpeg_start_streaming(struct vb2_queue *q, unsigned int count)
1699 struct s5p_jpeg_ctx *ctx = vb2_get_drv_priv(q);
1702 ret = pm_runtime_get_sync(ctx->jpeg->dev);
1704 return ret > 0 ? 0 : ret;
1707 static void s5p_jpeg_stop_streaming(struct vb2_queue *q)
1709 struct s5p_jpeg_ctx *ctx = vb2_get_drv_priv(q);
1711 pm_runtime_put(ctx->jpeg->dev);
1714 static struct vb2_ops s5p_jpeg_qops = {
1715 .queue_setup = s5p_jpeg_queue_setup,
1716 .buf_prepare = s5p_jpeg_buf_prepare,
1717 .buf_queue = s5p_jpeg_buf_queue,
1718 .wait_prepare = vb2_ops_wait_prepare,
1719 .wait_finish = vb2_ops_wait_finish,
1720 .start_streaming = s5p_jpeg_start_streaming,
1721 .stop_streaming = s5p_jpeg_stop_streaming,
1724 static int queue_init(void *priv, struct vb2_queue *src_vq,
1725 struct vb2_queue *dst_vq)
1727 struct s5p_jpeg_ctx *ctx = priv;
1730 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1731 src_vq->io_modes = VB2_MMAP | VB2_USERPTR;
1732 src_vq->drv_priv = ctx;
1733 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1734 src_vq->ops = &s5p_jpeg_qops;
1735 src_vq->mem_ops = &vb2_dma_contig_memops;
1736 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1737 src_vq->lock = &ctx->jpeg->lock;
1739 ret = vb2_queue_init(src_vq);
1743 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1744 dst_vq->io_modes = VB2_MMAP | VB2_USERPTR;
1745 dst_vq->drv_priv = ctx;
1746 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1747 dst_vq->ops = &s5p_jpeg_qops;
1748 dst_vq->mem_ops = &vb2_dma_contig_memops;
1749 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1750 dst_vq->lock = &ctx->jpeg->lock;
1752 return vb2_queue_init(dst_vq);
1756 * ============================================================================
1758 * ============================================================================
1761 static irqreturn_t s5p_jpeg_irq(int irq, void *dev_id)
1763 struct s5p_jpeg *jpeg = dev_id;
1764 struct s5p_jpeg_ctx *curr_ctx;
1765 struct vb2_buffer *src_buf, *dst_buf;
1766 unsigned long payload_size = 0;
1767 enum vb2_buffer_state state = VB2_BUF_STATE_DONE;
1768 bool enc_jpeg_too_large = false;
1769 bool timer_elapsed = false;
1770 bool op_completed = false;
1772 spin_lock(&jpeg->slock);
1774 curr_ctx = v4l2_m2m_get_curr_priv(jpeg->m2m_dev);
1776 src_buf = v4l2_m2m_src_buf_remove(curr_ctx->fh.m2m_ctx);
1777 dst_buf = v4l2_m2m_dst_buf_remove(curr_ctx->fh.m2m_ctx);
1779 if (curr_ctx->mode == S5P_JPEG_ENCODE)
1780 enc_jpeg_too_large = s5p_jpeg_enc_stream_stat(jpeg->regs);
1781 timer_elapsed = s5p_jpeg_timer_stat(jpeg->regs);
1782 op_completed = s5p_jpeg_result_stat_ok(jpeg->regs);
1783 if (curr_ctx->mode == S5P_JPEG_DECODE)
1784 op_completed = op_completed &&
1785 s5p_jpeg_stream_stat_ok(jpeg->regs);
1787 if (enc_jpeg_too_large) {
1788 state = VB2_BUF_STATE_ERROR;
1789 s5p_jpeg_clear_enc_stream_stat(jpeg->regs);
1790 } else if (timer_elapsed) {
1791 state = VB2_BUF_STATE_ERROR;
1792 s5p_jpeg_clear_timer_stat(jpeg->regs);
1793 } else if (!op_completed) {
1794 state = VB2_BUF_STATE_ERROR;
1796 payload_size = s5p_jpeg_compressed_size(jpeg->regs);
1799 dst_buf->v4l2_buf.timecode = src_buf->v4l2_buf.timecode;
1800 dst_buf->v4l2_buf.timestamp = src_buf->v4l2_buf.timestamp;
1801 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
1802 dst_buf->v4l2_buf.flags |=
1803 src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
1805 v4l2_m2m_buf_done(src_buf, state);
1806 if (curr_ctx->mode == S5P_JPEG_ENCODE)
1807 vb2_set_plane_payload(dst_buf, 0, payload_size);
1808 v4l2_m2m_buf_done(dst_buf, state);
1809 v4l2_m2m_job_finish(jpeg->m2m_dev, curr_ctx->fh.m2m_ctx);
1811 curr_ctx->subsampling = s5p_jpeg_get_subsampling_mode(jpeg->regs);
1812 spin_unlock(&jpeg->slock);
1814 s5p_jpeg_clear_int(jpeg->regs);
1819 static irqreturn_t exynos4_jpeg_irq(int irq, void *priv)
1821 unsigned int int_status;
1822 struct vb2_buffer *src_vb, *dst_vb;
1823 struct s5p_jpeg *jpeg = priv;
1824 struct s5p_jpeg_ctx *curr_ctx;
1825 unsigned long payload_size = 0;
1827 spin_lock(&jpeg->slock);
1829 curr_ctx = v4l2_m2m_get_curr_priv(jpeg->m2m_dev);
1831 src_vb = v4l2_m2m_src_buf_remove(curr_ctx->fh.m2m_ctx);
1832 dst_vb = v4l2_m2m_dst_buf_remove(curr_ctx->fh.m2m_ctx);
1834 int_status = exynos4_jpeg_get_int_status(jpeg->regs);
1837 switch (int_status & 0x1f) {
1839 jpeg->irq_ret = ERR_PROT;
1842 jpeg->irq_ret = OK_ENC_OR_DEC;
1845 jpeg->irq_ret = ERR_DEC_INVALID_FORMAT;
1848 jpeg->irq_ret = ERR_MULTI_SCAN;
1851 jpeg->irq_ret = ERR_FRAME;
1854 jpeg->irq_ret = ERR_UNKNOWN;
1858 jpeg->irq_ret = ERR_UNKNOWN;
1861 if (jpeg->irq_ret == OK_ENC_OR_DEC) {
1862 if (curr_ctx->mode == S5P_JPEG_ENCODE) {
1863 payload_size = exynos4_jpeg_get_stream_size(jpeg->regs);
1864 vb2_set_plane_payload(dst_vb, 0, payload_size);
1866 v4l2_m2m_buf_done(src_vb, VB2_BUF_STATE_DONE);
1867 v4l2_m2m_buf_done(dst_vb, VB2_BUF_STATE_DONE);
1869 v4l2_m2m_buf_done(src_vb, VB2_BUF_STATE_ERROR);
1870 v4l2_m2m_buf_done(dst_vb, VB2_BUF_STATE_ERROR);
1873 v4l2_m2m_job_finish(jpeg->m2m_dev, curr_ctx->fh.m2m_ctx);
1874 curr_ctx->subsampling = exynos4_jpeg_get_frame_fmt(jpeg->regs);
1876 spin_unlock(&jpeg->slock);
1880 static void *jpeg_get_drv_data(struct platform_device *pdev);
1883 * ============================================================================
1884 * Driver basic infrastructure
1885 * ============================================================================
1888 static int s5p_jpeg_probe(struct platform_device *pdev)
1890 struct s5p_jpeg *jpeg;
1891 struct resource *res;
1894 if (!pdev->dev.of_node)
1897 /* JPEG IP abstraction struct */
1898 jpeg = devm_kzalloc(&pdev->dev, sizeof(struct s5p_jpeg), GFP_KERNEL);
1902 jpeg->variant = jpeg_get_drv_data(pdev);
1904 mutex_init(&jpeg->lock);
1905 spin_lock_init(&jpeg->slock);
1906 jpeg->dev = &pdev->dev;
1908 /* memory-mapped registers */
1909 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1911 jpeg->regs = devm_ioremap_resource(&pdev->dev, res);
1912 if (IS_ERR(jpeg->regs))
1913 return PTR_ERR(jpeg->regs);
1915 /* interrupt service routine registration */
1916 jpeg->irq = ret = platform_get_irq(pdev, 0);
1918 dev_err(&pdev->dev, "cannot find IRQ\n");
1922 ret = devm_request_irq(&pdev->dev, jpeg->irq, jpeg->variant->jpeg_irq,
1923 0, dev_name(&pdev->dev), jpeg);
1925 dev_err(&pdev->dev, "cannot claim IRQ %d\n", jpeg->irq);
1930 jpeg->clk = clk_get(&pdev->dev, "jpeg");
1931 if (IS_ERR(jpeg->clk)) {
1932 dev_err(&pdev->dev, "cannot get clock\n");
1933 ret = PTR_ERR(jpeg->clk);
1936 dev_dbg(&pdev->dev, "clock source %p\n", jpeg->clk);
1939 ret = v4l2_device_register(&pdev->dev, &jpeg->v4l2_dev);
1941 dev_err(&pdev->dev, "Failed to register v4l2 device\n");
1942 goto clk_get_rollback;
1945 /* mem2mem device */
1946 jpeg->m2m_dev = v4l2_m2m_init(jpeg->variant->m2m_ops);
1947 if (IS_ERR(jpeg->m2m_dev)) {
1948 v4l2_err(&jpeg->v4l2_dev, "Failed to init mem2mem device\n");
1949 ret = PTR_ERR(jpeg->m2m_dev);
1950 goto device_register_rollback;
1953 jpeg->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
1954 if (IS_ERR(jpeg->alloc_ctx)) {
1955 v4l2_err(&jpeg->v4l2_dev, "Failed to init memory allocator\n");
1956 ret = PTR_ERR(jpeg->alloc_ctx);
1957 goto m2m_init_rollback;
1960 /* JPEG encoder /dev/videoX node */
1961 jpeg->vfd_encoder = video_device_alloc();
1962 if (!jpeg->vfd_encoder) {
1963 v4l2_err(&jpeg->v4l2_dev, "Failed to allocate video device\n");
1965 goto vb2_allocator_rollback;
1967 snprintf(jpeg->vfd_encoder->name, sizeof(jpeg->vfd_encoder->name),
1968 "%s-enc", S5P_JPEG_M2M_NAME);
1969 jpeg->vfd_encoder->fops = &s5p_jpeg_fops;
1970 jpeg->vfd_encoder->ioctl_ops = &s5p_jpeg_ioctl_ops;
1971 jpeg->vfd_encoder->minor = -1;
1972 jpeg->vfd_encoder->release = video_device_release;
1973 jpeg->vfd_encoder->lock = &jpeg->lock;
1974 jpeg->vfd_encoder->v4l2_dev = &jpeg->v4l2_dev;
1975 jpeg->vfd_encoder->vfl_dir = VFL_DIR_M2M;
1977 ret = video_register_device(jpeg->vfd_encoder, VFL_TYPE_GRABBER, -1);
1979 v4l2_err(&jpeg->v4l2_dev, "Failed to register video device\n");
1980 goto enc_vdev_alloc_rollback;
1983 video_set_drvdata(jpeg->vfd_encoder, jpeg);
1984 v4l2_info(&jpeg->v4l2_dev,
1985 "encoder device registered as /dev/video%d\n",
1986 jpeg->vfd_encoder->num);
1988 /* JPEG decoder /dev/videoX node */
1989 jpeg->vfd_decoder = video_device_alloc();
1990 if (!jpeg->vfd_decoder) {
1991 v4l2_err(&jpeg->v4l2_dev, "Failed to allocate video device\n");
1993 goto enc_vdev_register_rollback;
1995 snprintf(jpeg->vfd_decoder->name, sizeof(jpeg->vfd_decoder->name),
1996 "%s-dec", S5P_JPEG_M2M_NAME);
1997 jpeg->vfd_decoder->fops = &s5p_jpeg_fops;
1998 jpeg->vfd_decoder->ioctl_ops = &s5p_jpeg_ioctl_ops;
1999 jpeg->vfd_decoder->minor = -1;
2000 jpeg->vfd_decoder->release = video_device_release;
2001 jpeg->vfd_decoder->lock = &jpeg->lock;
2002 jpeg->vfd_decoder->v4l2_dev = &jpeg->v4l2_dev;
2003 jpeg->vfd_decoder->vfl_dir = VFL_DIR_M2M;
2005 ret = video_register_device(jpeg->vfd_decoder, VFL_TYPE_GRABBER, -1);
2007 v4l2_err(&jpeg->v4l2_dev, "Failed to register video device\n");
2008 goto dec_vdev_alloc_rollback;
2011 video_set_drvdata(jpeg->vfd_decoder, jpeg);
2012 v4l2_info(&jpeg->v4l2_dev,
2013 "decoder device registered as /dev/video%d\n",
2014 jpeg->vfd_decoder->num);
2016 /* final statements & power management */
2017 platform_set_drvdata(pdev, jpeg);
2019 pm_runtime_enable(&pdev->dev);
2021 v4l2_info(&jpeg->v4l2_dev, "Samsung S5P JPEG codec\n");
2025 dec_vdev_alloc_rollback:
2026 video_device_release(jpeg->vfd_decoder);
2028 enc_vdev_register_rollback:
2029 video_unregister_device(jpeg->vfd_encoder);
2031 enc_vdev_alloc_rollback:
2032 video_device_release(jpeg->vfd_encoder);
2034 vb2_allocator_rollback:
2035 vb2_dma_contig_cleanup_ctx(jpeg->alloc_ctx);
2038 v4l2_m2m_release(jpeg->m2m_dev);
2040 device_register_rollback:
2041 v4l2_device_unregister(&jpeg->v4l2_dev);
2049 static int s5p_jpeg_remove(struct platform_device *pdev)
2051 struct s5p_jpeg *jpeg = platform_get_drvdata(pdev);
2053 pm_runtime_disable(jpeg->dev);
2055 video_unregister_device(jpeg->vfd_decoder);
2056 video_device_release(jpeg->vfd_decoder);
2057 video_unregister_device(jpeg->vfd_encoder);
2058 video_device_release(jpeg->vfd_encoder);
2059 vb2_dma_contig_cleanup_ctx(jpeg->alloc_ctx);
2060 v4l2_m2m_release(jpeg->m2m_dev);
2061 v4l2_device_unregister(&jpeg->v4l2_dev);
2063 if (!pm_runtime_status_suspended(&pdev->dev))
2064 clk_disable_unprepare(jpeg->clk);
2071 static int s5p_jpeg_runtime_suspend(struct device *dev)
2073 struct s5p_jpeg *jpeg = dev_get_drvdata(dev);
2075 clk_disable_unprepare(jpeg->clk);
2080 static int s5p_jpeg_runtime_resume(struct device *dev)
2082 struct s5p_jpeg *jpeg = dev_get_drvdata(dev);
2083 unsigned long flags;
2086 ret = clk_prepare_enable(jpeg->clk);
2090 spin_lock_irqsave(&jpeg->slock, flags);
2093 * JPEG IP allows storing two Huffman tables for each component
2094 * We fill table 0 for each component and do this here only
2095 * for S5PC210 device as Exynos4x12 requires programming its
2096 * Huffman tables each time the encoding process is initialized.
2098 if (jpeg->variant->version == SJPEG_S5P) {
2099 s5p_jpeg_set_hdctbl(jpeg->regs);
2100 s5p_jpeg_set_hdctblg(jpeg->regs);
2101 s5p_jpeg_set_hactbl(jpeg->regs);
2102 s5p_jpeg_set_hactblg(jpeg->regs);
2105 spin_unlock_irqrestore(&jpeg->slock, flags);
2110 static int s5p_jpeg_suspend(struct device *dev)
2112 if (pm_runtime_suspended(dev))
2115 return s5p_jpeg_runtime_suspend(dev);
2118 static int s5p_jpeg_resume(struct device *dev)
2120 if (pm_runtime_suspended(dev))
2123 return s5p_jpeg_runtime_resume(dev);
2126 static const struct dev_pm_ops s5p_jpeg_pm_ops = {
2127 SET_SYSTEM_SLEEP_PM_OPS(s5p_jpeg_suspend, s5p_jpeg_resume)
2128 SET_RUNTIME_PM_OPS(s5p_jpeg_runtime_suspend, s5p_jpeg_runtime_resume, NULL)
2132 static struct s5p_jpeg_variant s5p_jpeg_drvdata = {
2133 .version = SJPEG_S5P,
2134 .jpeg_irq = s5p_jpeg_irq,
2135 .m2m_ops = &s5p_jpeg_m2m_ops,
2136 .fmt_ver_flag = SJPEG_FMT_FLAG_S5P,
2139 static struct s5p_jpeg_variant exynos4_jpeg_drvdata = {
2140 .version = SJPEG_EXYNOS4,
2141 .jpeg_irq = exynos4_jpeg_irq,
2142 .m2m_ops = &exynos4_jpeg_m2m_ops,
2143 .fmt_ver_flag = SJPEG_FMT_FLAG_EXYNOS4,
2146 static const struct of_device_id samsung_jpeg_match[] = {
2148 .compatible = "samsung,s5pv210-jpeg",
2149 .data = &s5p_jpeg_drvdata,
2151 .compatible = "samsung,exynos4210-jpeg",
2152 .data = &s5p_jpeg_drvdata,
2154 .compatible = "samsung,exynos4212-jpeg",
2155 .data = &exynos4_jpeg_drvdata,
2160 MODULE_DEVICE_TABLE(of, samsung_jpeg_match);
2162 static void *jpeg_get_drv_data(struct platform_device *pdev)
2164 struct s5p_jpeg_variant *driver_data = NULL;
2165 const struct of_device_id *match;
2167 match = of_match_node(of_match_ptr(samsung_jpeg_match),
2170 driver_data = (struct s5p_jpeg_variant *)match->data;
2176 static struct platform_driver s5p_jpeg_driver = {
2177 .probe = s5p_jpeg_probe,
2178 .remove = s5p_jpeg_remove,
2180 .of_match_table = of_match_ptr(samsung_jpeg_match),
2181 .owner = THIS_MODULE,
2182 .name = S5P_JPEG_M2M_NAME,
2183 .pm = &s5p_jpeg_pm_ops,
2187 module_platform_driver(s5p_jpeg_driver);
2189 MODULE_AUTHOR("Andrzej Pietrasiewicz <andrzej.p@samsung.com>");
2190 MODULE_AUTHOR("Jacek Anaszewski <j.anaszewski@samsung.com>");
2191 MODULE_DESCRIPTION("Samsung JPEG codec driver");
2192 MODULE_LICENSE("GPL");