treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / media / platform / s5p-jpeg / jpeg-core.c
blobac2162235cefd9cbe7181ab339cde451e98c6da0
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* linux/drivers/media/platform/s5p-jpeg/jpeg-core.c
4 * Copyright (c) 2011-2014 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com
7 * Author: Andrzej Pietrasiewicz <andrzejtp2010@gmail.com>
8 * Author: Jacek Anaszewski <j.anaszewski@samsung.com>
9 */
11 #include <linux/clk.h>
12 #include <linux/err.h>
13 #include <linux/gfp.h>
14 #include <linux/interrupt.h>
15 #include <linux/io.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/of.h>
19 #include <linux/platform_device.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/slab.h>
22 #include <linux/spinlock.h>
23 #include <linux/string.h>
24 #include <media/v4l2-event.h>
25 #include <media/v4l2-mem2mem.h>
26 #include <media/v4l2-ioctl.h>
27 #include <media/videobuf2-v4l2.h>
28 #include <media/videobuf2-dma-contig.h>
30 #include "jpeg-core.h"
31 #include "jpeg-hw-s5p.h"
32 #include "jpeg-hw-exynos4.h"
33 #include "jpeg-hw-exynos3250.h"
34 #include "jpeg-regs.h"
36 static struct s5p_jpeg_fmt sjpeg_formats[] = {
38 .fourcc = V4L2_PIX_FMT_JPEG,
39 .flags = SJPEG_FMT_FLAG_ENC_CAPTURE |
40 SJPEG_FMT_FLAG_DEC_OUTPUT |
41 SJPEG_FMT_FLAG_S5P |
42 SJPEG_FMT_FLAG_EXYNOS3250 |
43 SJPEG_FMT_FLAG_EXYNOS4,
46 .fourcc = V4L2_PIX_FMT_YUYV,
47 .depth = 16,
48 .colplanes = 1,
49 .h_align = 4,
50 .v_align = 3,
51 .flags = SJPEG_FMT_FLAG_ENC_OUTPUT |
52 SJPEG_FMT_FLAG_DEC_CAPTURE |
53 SJPEG_FMT_FLAG_S5P |
54 SJPEG_FMT_NON_RGB,
55 .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_422,
58 .fourcc = V4L2_PIX_FMT_YUYV,
59 .depth = 16,
60 .colplanes = 1,
61 .h_align = 1,
62 .v_align = 0,
63 .flags = SJPEG_FMT_FLAG_ENC_OUTPUT |
64 SJPEG_FMT_FLAG_DEC_CAPTURE |
65 SJPEG_FMT_FLAG_EXYNOS4 |
66 SJPEG_FMT_NON_RGB,
67 .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_422,
70 .fourcc = V4L2_PIX_FMT_YUYV,
71 .depth = 16,
72 .colplanes = 1,
73 .h_align = 2,
74 .v_align = 0,
75 .flags = SJPEG_FMT_FLAG_ENC_OUTPUT |
76 SJPEG_FMT_FLAG_DEC_CAPTURE |
77 SJPEG_FMT_FLAG_EXYNOS3250 |
78 SJPEG_FMT_NON_RGB,
79 .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_422,
82 .fourcc = V4L2_PIX_FMT_YVYU,
83 .depth = 16,
84 .colplanes = 1,
85 .h_align = 1,
86 .v_align = 0,
87 .flags = SJPEG_FMT_FLAG_ENC_OUTPUT |
88 SJPEG_FMT_FLAG_DEC_CAPTURE |
89 SJPEG_FMT_FLAG_EXYNOS4 |
90 SJPEG_FMT_NON_RGB,
91 .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_422,
94 .fourcc = V4L2_PIX_FMT_YVYU,
95 .depth = 16,
96 .colplanes = 1,
97 .h_align = 2,
98 .v_align = 0,
99 .flags = SJPEG_FMT_FLAG_ENC_OUTPUT |
100 SJPEG_FMT_FLAG_DEC_CAPTURE |
101 SJPEG_FMT_FLAG_EXYNOS3250 |
102 SJPEG_FMT_NON_RGB,
103 .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_422,
106 .fourcc = V4L2_PIX_FMT_UYVY,
107 .depth = 16,
108 .colplanes = 1,
109 .h_align = 2,
110 .v_align = 0,
111 .flags = SJPEG_FMT_FLAG_ENC_OUTPUT |
112 SJPEG_FMT_FLAG_DEC_CAPTURE |
113 SJPEG_FMT_FLAG_EXYNOS3250 |
114 SJPEG_FMT_NON_RGB,
115 .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_422,
118 .fourcc = V4L2_PIX_FMT_VYUY,
119 .depth = 16,
120 .colplanes = 1,
121 .h_align = 2,
122 .v_align = 0,
123 .flags = SJPEG_FMT_FLAG_ENC_OUTPUT |
124 SJPEG_FMT_FLAG_DEC_CAPTURE |
125 SJPEG_FMT_FLAG_EXYNOS3250 |
126 SJPEG_FMT_NON_RGB,
127 .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_422,
130 .fourcc = V4L2_PIX_FMT_RGB565,
131 .depth = 16,
132 .colplanes = 1,
133 .h_align = 0,
134 .v_align = 0,
135 .flags = SJPEG_FMT_FLAG_ENC_OUTPUT |
136 SJPEG_FMT_FLAG_DEC_CAPTURE |
137 SJPEG_FMT_FLAG_EXYNOS4 |
138 SJPEG_FMT_RGB,
139 .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_444,
142 .fourcc = V4L2_PIX_FMT_RGB565,
143 .depth = 16,
144 .colplanes = 1,
145 .h_align = 2,
146 .v_align = 0,
147 .flags = SJPEG_FMT_FLAG_ENC_OUTPUT |
148 SJPEG_FMT_FLAG_DEC_CAPTURE |
149 SJPEG_FMT_FLAG_EXYNOS3250 |
150 SJPEG_FMT_RGB,
151 .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_444,
154 .fourcc = V4L2_PIX_FMT_RGB565X,
155 .depth = 16,
156 .colplanes = 1,
157 .h_align = 2,
158 .v_align = 0,
159 .flags = SJPEG_FMT_FLAG_ENC_OUTPUT |
160 SJPEG_FMT_FLAG_DEC_CAPTURE |
161 SJPEG_FMT_FLAG_EXYNOS3250 |
162 SJPEG_FMT_RGB,
163 .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_444,
166 .fourcc = V4L2_PIX_FMT_RGB565,
167 .depth = 16,
168 .colplanes = 1,
169 .h_align = 0,
170 .v_align = 0,
171 .flags = SJPEG_FMT_FLAG_ENC_OUTPUT |
172 SJPEG_FMT_FLAG_S5P |
173 SJPEG_FMT_RGB,
174 .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_444,
177 .fourcc = V4L2_PIX_FMT_RGB32,
178 .depth = 32,
179 .colplanes = 1,
180 .h_align = 0,
181 .v_align = 0,
182 .flags = SJPEG_FMT_FLAG_ENC_OUTPUT |
183 SJPEG_FMT_FLAG_DEC_CAPTURE |
184 SJPEG_FMT_FLAG_EXYNOS4 |
185 SJPEG_FMT_RGB,
186 .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_444,
189 .fourcc = V4L2_PIX_FMT_RGB32,
190 .depth = 32,
191 .colplanes = 1,
192 .h_align = 2,
193 .v_align = 0,
194 .flags = SJPEG_FMT_FLAG_ENC_OUTPUT |
195 SJPEG_FMT_FLAG_DEC_CAPTURE |
196 SJPEG_FMT_FLAG_EXYNOS3250 |
197 SJPEG_FMT_RGB,
198 .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_444,
201 .fourcc = V4L2_PIX_FMT_NV24,
202 .depth = 24,
203 .colplanes = 2,
204 .h_align = 0,
205 .v_align = 0,
206 .flags = SJPEG_FMT_FLAG_ENC_OUTPUT |
207 SJPEG_FMT_FLAG_DEC_CAPTURE |
208 SJPEG_FMT_FLAG_EXYNOS4 |
209 SJPEG_FMT_NON_RGB,
210 .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_444,
213 .fourcc = V4L2_PIX_FMT_NV42,
214 .depth = 24,
215 .colplanes = 2,
216 .h_align = 0,
217 .v_align = 0,
218 .flags = SJPEG_FMT_FLAG_ENC_OUTPUT |
219 SJPEG_FMT_FLAG_DEC_CAPTURE |
220 SJPEG_FMT_FLAG_EXYNOS4 |
221 SJPEG_FMT_NON_RGB,
222 .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_444,
225 .fourcc = V4L2_PIX_FMT_NV61,
226 .depth = 16,
227 .colplanes = 2,
228 .h_align = 1,
229 .v_align = 0,
230 .flags = SJPEG_FMT_FLAG_ENC_OUTPUT |
231 SJPEG_FMT_FLAG_DEC_CAPTURE |
232 SJPEG_FMT_FLAG_EXYNOS4 |
233 SJPEG_FMT_NON_RGB,
234 .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_422,
237 .fourcc = V4L2_PIX_FMT_NV16,
238 .depth = 16,
239 .colplanes = 2,
240 .h_align = 1,
241 .v_align = 0,
242 .flags = SJPEG_FMT_FLAG_ENC_OUTPUT |
243 SJPEG_FMT_FLAG_DEC_CAPTURE |
244 SJPEG_FMT_FLAG_EXYNOS4 |
245 SJPEG_FMT_NON_RGB,
246 .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_422,
249 .fourcc = V4L2_PIX_FMT_NV12,
250 .depth = 12,
251 .colplanes = 2,
252 .h_align = 1,
253 .v_align = 1,
254 .flags = SJPEG_FMT_FLAG_ENC_OUTPUT |
255 SJPEG_FMT_FLAG_DEC_CAPTURE |
256 SJPEG_FMT_FLAG_EXYNOS4 |
257 SJPEG_FMT_NON_RGB,
258 .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_420,
261 .fourcc = V4L2_PIX_FMT_NV12,
262 .depth = 12,
263 .colplanes = 2,
264 .h_align = 3,
265 .v_align = 3,
266 .flags = SJPEG_FMT_FLAG_ENC_OUTPUT |
267 SJPEG_FMT_FLAG_DEC_CAPTURE |
268 SJPEG_FMT_FLAG_EXYNOS3250 |
269 SJPEG_FMT_NON_RGB,
270 .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_420,
273 .fourcc = V4L2_PIX_FMT_NV12,
274 .depth = 12,
275 .colplanes = 2,
276 .h_align = 4,
277 .v_align = 4,
278 .flags = SJPEG_FMT_FLAG_ENC_OUTPUT |
279 SJPEG_FMT_FLAG_DEC_CAPTURE |
280 SJPEG_FMT_FLAG_S5P |
281 SJPEG_FMT_NON_RGB,
282 .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_420,
285 .fourcc = V4L2_PIX_FMT_NV21,
286 .depth = 12,
287 .colplanes = 2,
288 .h_align = 3,
289 .v_align = 3,
290 .flags = SJPEG_FMT_FLAG_ENC_OUTPUT |
291 SJPEG_FMT_FLAG_DEC_CAPTURE |
292 SJPEG_FMT_FLAG_EXYNOS3250 |
293 SJPEG_FMT_NON_RGB,
294 .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_420,
297 .fourcc = V4L2_PIX_FMT_NV21,
298 .depth = 12,
299 .colplanes = 2,
300 .h_align = 1,
301 .v_align = 1,
302 .flags = SJPEG_FMT_FLAG_ENC_OUTPUT |
303 SJPEG_FMT_FLAG_DEC_CAPTURE |
304 SJPEG_FMT_FLAG_EXYNOS3250 |
305 SJPEG_FMT_FLAG_EXYNOS4 |
306 SJPEG_FMT_NON_RGB,
307 .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_420,
310 .fourcc = V4L2_PIX_FMT_YUV420,
311 .depth = 12,
312 .colplanes = 3,
313 .h_align = 1,
314 .v_align = 1,
315 .flags = SJPEG_FMT_FLAG_ENC_OUTPUT |
316 SJPEG_FMT_FLAG_DEC_CAPTURE |
317 SJPEG_FMT_FLAG_EXYNOS4 |
318 SJPEG_FMT_NON_RGB,
319 .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_420,
322 .fourcc = V4L2_PIX_FMT_YUV420,
323 .depth = 12,
324 .colplanes = 3,
325 .h_align = 4,
326 .v_align = 4,
327 .flags = SJPEG_FMT_FLAG_ENC_OUTPUT |
328 SJPEG_FMT_FLAG_DEC_CAPTURE |
329 SJPEG_FMT_FLAG_EXYNOS3250 |
330 SJPEG_FMT_NON_RGB,
331 .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_420,
334 .fourcc = V4L2_PIX_FMT_GREY,
335 .depth = 8,
336 .colplanes = 1,
337 .flags = SJPEG_FMT_FLAG_ENC_OUTPUT |
338 SJPEG_FMT_FLAG_DEC_CAPTURE |
339 SJPEG_FMT_FLAG_EXYNOS4 |
340 SJPEG_FMT_NON_RGB,
341 .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_GRAY,
344 #define SJPEG_NUM_FORMATS ARRAY_SIZE(sjpeg_formats)
346 static const unsigned char qtbl_luminance[4][64] = {
347 {/*level 0 - high compression quality */
348 20, 16, 25, 39, 50, 46, 62, 68,
349 16, 18, 23, 38, 38, 53, 65, 68,
350 25, 23, 31, 38, 53, 65, 68, 68,
351 39, 38, 38, 53, 65, 68, 68, 68,
352 50, 38, 53, 65, 68, 68, 68, 68,
353 46, 53, 65, 68, 68, 68, 68, 68,
354 62, 65, 68, 68, 68, 68, 68, 68,
355 68, 68, 68, 68, 68, 68, 68, 68
357 {/* level 1 */
358 16, 11, 11, 16, 23, 27, 31, 30,
359 11, 12, 12, 15, 20, 23, 23, 30,
360 11, 12, 13, 16, 23, 26, 35, 47,
361 16, 15, 16, 23, 26, 37, 47, 64,
362 23, 20, 23, 26, 39, 51, 64, 64,
363 27, 23, 26, 37, 51, 64, 64, 64,
364 31, 23, 35, 47, 64, 64, 64, 64,
365 30, 30, 47, 64, 64, 64, 64, 64
367 {/* level 2 */
368 12, 8, 8, 12, 17, 21, 24, 23,
369 8, 9, 9, 11, 15, 19, 18, 23,
370 8, 9, 10, 12, 19, 20, 27, 36,
371 12, 11, 12, 21, 20, 28, 36, 53,
372 17, 15, 19, 20, 30, 39, 51, 59,
373 21, 19, 20, 28, 39, 51, 59, 59,
374 24, 18, 27, 36, 51, 59, 59, 59,
375 23, 23, 36, 53, 59, 59, 59, 59
377 {/* level 3 - low compression quality */
378 8, 6, 6, 8, 12, 14, 16, 17,
379 6, 6, 6, 8, 10, 13, 12, 15,
380 6, 6, 7, 8, 13, 14, 18, 24,
381 8, 8, 8, 14, 13, 19, 24, 35,
382 12, 10, 13, 13, 20, 26, 34, 39,
383 14, 13, 14, 19, 26, 34, 39, 39,
384 16, 12, 18, 24, 34, 39, 39, 39,
385 17, 15, 24, 35, 39, 39, 39, 39
389 static const unsigned char qtbl_chrominance[4][64] = {
390 {/*level 0 - high compression quality */
391 21, 25, 32, 38, 54, 68, 68, 68,
392 25, 28, 24, 38, 54, 68, 68, 68,
393 32, 24, 32, 43, 66, 68, 68, 68,
394 38, 38, 43, 53, 68, 68, 68, 68,
395 54, 54, 66, 68, 68, 68, 68, 68,
396 68, 68, 68, 68, 68, 68, 68, 68,
397 68, 68, 68, 68, 68, 68, 68, 68,
398 68, 68, 68, 68, 68, 68, 68, 68
400 {/* level 1 */
401 17, 15, 17, 21, 20, 26, 38, 48,
402 15, 19, 18, 17, 20, 26, 35, 43,
403 17, 18, 20, 22, 26, 30, 46, 53,
404 21, 17, 22, 28, 30, 39, 53, 64,
405 20, 20, 26, 30, 39, 48, 64, 64,
406 26, 26, 30, 39, 48, 63, 64, 64,
407 38, 35, 46, 53, 64, 64, 64, 64,
408 48, 43, 53, 64, 64, 64, 64, 64
410 {/* level 2 */
411 13, 11, 13, 16, 20, 20, 29, 37,
412 11, 14, 14, 14, 16, 20, 26, 32,
413 13, 14, 15, 17, 20, 23, 35, 40,
414 16, 14, 17, 21, 23, 30, 40, 50,
415 20, 16, 20, 23, 30, 37, 50, 59,
416 20, 20, 23, 30, 37, 48, 59, 59,
417 29, 26, 35, 40, 50, 59, 59, 59,
418 37, 32, 40, 50, 59, 59, 59, 59
420 {/* level 3 - low compression quality */
421 9, 8, 9, 11, 14, 17, 19, 24,
422 8, 10, 9, 11, 14, 13, 17, 22,
423 9, 9, 13, 14, 13, 15, 23, 26,
424 11, 11, 14, 14, 15, 20, 26, 33,
425 14, 14, 13, 15, 20, 24, 33, 39,
426 17, 13, 15, 20, 24, 32, 39, 39,
427 19, 17, 23, 26, 33, 39, 39, 39,
428 24, 22, 26, 33, 39, 39, 39, 39
432 static const unsigned char hdctbl0[16] = {
433 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0
436 static const unsigned char hdctblg0[12] = {
437 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb
439 static const unsigned char hactbl0[16] = {
440 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d
442 static const unsigned char hactblg0[162] = {
443 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
444 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
445 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
446 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
447 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
448 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
449 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
450 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
451 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
452 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
453 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
454 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
455 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
456 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
457 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
458 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
459 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
460 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
461 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
462 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
463 0xf9, 0xfa
467 * Fourcc downgrade schema lookup tables for 422 and 420
468 * chroma subsampling - fourcc on each position maps on the
469 * fourcc from the table fourcc_to_dwngrd_schema_id which allows
470 * to get the most suitable fourcc counterpart for the given
471 * downgraded subsampling property.
473 static const u32 subs422_fourcc_dwngrd_schema[] = {
474 V4L2_PIX_FMT_NV16,
475 V4L2_PIX_FMT_NV61,
478 static const u32 subs420_fourcc_dwngrd_schema[] = {
479 V4L2_PIX_FMT_NV12,
480 V4L2_PIX_FMT_NV21,
481 V4L2_PIX_FMT_NV12,
482 V4L2_PIX_FMT_NV21,
483 V4L2_PIX_FMT_NV12,
484 V4L2_PIX_FMT_NV21,
485 V4L2_PIX_FMT_GREY,
486 V4L2_PIX_FMT_GREY,
487 V4L2_PIX_FMT_GREY,
488 V4L2_PIX_FMT_GREY,
492 * Lookup table for translation of a fourcc to the position
493 * of its downgraded counterpart in the *fourcc_dwngrd_schema
494 * tables.
496 static const u32 fourcc_to_dwngrd_schema_id[] = {
497 V4L2_PIX_FMT_NV24,
498 V4L2_PIX_FMT_NV42,
499 V4L2_PIX_FMT_NV16,
500 V4L2_PIX_FMT_NV61,
501 V4L2_PIX_FMT_YUYV,
502 V4L2_PIX_FMT_YVYU,
503 V4L2_PIX_FMT_NV12,
504 V4L2_PIX_FMT_NV21,
505 V4L2_PIX_FMT_YUV420,
506 V4L2_PIX_FMT_GREY,
509 static int s5p_jpeg_get_dwngrd_sch_id_by_fourcc(u32 fourcc)
511 int i;
513 for (i = 0; i < ARRAY_SIZE(fourcc_to_dwngrd_schema_id); ++i) {
514 if (fourcc_to_dwngrd_schema_id[i] == fourcc)
515 return i;
518 return -EINVAL;
521 static int s5p_jpeg_adjust_fourcc_to_subsampling(
522 enum v4l2_jpeg_chroma_subsampling subs,
523 u32 in_fourcc,
524 u32 *out_fourcc,
525 struct s5p_jpeg_ctx *ctx)
527 int dwngrd_sch_id;
529 if (ctx->subsampling != V4L2_JPEG_CHROMA_SUBSAMPLING_GRAY) {
530 dwngrd_sch_id =
531 s5p_jpeg_get_dwngrd_sch_id_by_fourcc(in_fourcc);
532 if (dwngrd_sch_id < 0)
533 return -EINVAL;
536 switch (ctx->subsampling) {
537 case V4L2_JPEG_CHROMA_SUBSAMPLING_GRAY:
538 *out_fourcc = V4L2_PIX_FMT_GREY;
539 break;
540 case V4L2_JPEG_CHROMA_SUBSAMPLING_420:
541 if (dwngrd_sch_id >
542 ARRAY_SIZE(subs420_fourcc_dwngrd_schema) - 1)
543 return -EINVAL;
544 *out_fourcc = subs420_fourcc_dwngrd_schema[dwngrd_sch_id];
545 break;
546 case V4L2_JPEG_CHROMA_SUBSAMPLING_422:
547 if (dwngrd_sch_id >
548 ARRAY_SIZE(subs422_fourcc_dwngrd_schema) - 1)
549 return -EINVAL;
550 *out_fourcc = subs422_fourcc_dwngrd_schema[dwngrd_sch_id];
551 break;
552 default:
553 *out_fourcc = V4L2_PIX_FMT_GREY;
554 break;
557 return 0;
560 static int exynos4x12_decoded_subsampling[] = {
561 V4L2_JPEG_CHROMA_SUBSAMPLING_GRAY,
562 V4L2_JPEG_CHROMA_SUBSAMPLING_444,
563 V4L2_JPEG_CHROMA_SUBSAMPLING_422,
564 V4L2_JPEG_CHROMA_SUBSAMPLING_420,
567 static int exynos3250_decoded_subsampling[] = {
568 V4L2_JPEG_CHROMA_SUBSAMPLING_444,
569 V4L2_JPEG_CHROMA_SUBSAMPLING_422,
570 V4L2_JPEG_CHROMA_SUBSAMPLING_420,
571 V4L2_JPEG_CHROMA_SUBSAMPLING_GRAY,
574 V4L2_JPEG_CHROMA_SUBSAMPLING_411,
577 static inline struct s5p_jpeg_ctx *ctrl_to_ctx(struct v4l2_ctrl *c)
579 return container_of(c->handler, struct s5p_jpeg_ctx, ctrl_handler);
582 static inline struct s5p_jpeg_ctx *fh_to_ctx(struct v4l2_fh *fh)
584 return container_of(fh, struct s5p_jpeg_ctx, fh);
587 static int s5p_jpeg_to_user_subsampling(struct s5p_jpeg_ctx *ctx)
589 switch (ctx->jpeg->variant->version) {
590 case SJPEG_S5P:
591 WARN_ON(ctx->subsampling > 3);
592 if (ctx->subsampling > 2)
593 return V4L2_JPEG_CHROMA_SUBSAMPLING_GRAY;
594 return ctx->subsampling;
595 case SJPEG_EXYNOS3250:
596 case SJPEG_EXYNOS5420:
597 WARN_ON(ctx->subsampling > 6);
598 if (ctx->subsampling > 3)
599 return V4L2_JPEG_CHROMA_SUBSAMPLING_411;
600 return exynos3250_decoded_subsampling[ctx->subsampling];
601 case SJPEG_EXYNOS4:
602 WARN_ON(ctx->subsampling > 3);
603 if (ctx->subsampling > 2)
604 return V4L2_JPEG_CHROMA_SUBSAMPLING_420;
605 return exynos4x12_decoded_subsampling[ctx->subsampling];
606 case SJPEG_EXYNOS5433:
607 return ctx->subsampling; /* parsed from header */
608 default:
609 WARN_ON(ctx->subsampling > 3);
610 return V4L2_JPEG_CHROMA_SUBSAMPLING_GRAY;
614 static inline void s5p_jpeg_set_qtbl(void __iomem *regs,
615 const unsigned char *qtbl,
616 unsigned long tab, int len)
618 int i;
620 for (i = 0; i < len; i++)
621 writel((unsigned int)qtbl[i], regs + tab + (i * 0x04));
624 static inline void s5p_jpeg_set_qtbl_lum(void __iomem *regs, int quality)
626 /* this driver fills quantisation table 0 with data for luma */
627 s5p_jpeg_set_qtbl(regs, qtbl_luminance[quality],
628 S5P_JPG_QTBL_CONTENT(0),
629 ARRAY_SIZE(qtbl_luminance[quality]));
632 static inline void s5p_jpeg_set_qtbl_chr(void __iomem *regs, int quality)
634 /* this driver fills quantisation table 1 with data for chroma */
635 s5p_jpeg_set_qtbl(regs, qtbl_chrominance[quality],
636 S5P_JPG_QTBL_CONTENT(1),
637 ARRAY_SIZE(qtbl_chrominance[quality]));
640 static inline void s5p_jpeg_set_htbl(void __iomem *regs,
641 const unsigned char *htbl,
642 unsigned long tab, int len)
644 int i;
646 for (i = 0; i < len; i++)
647 writel((unsigned int)htbl[i], regs + tab + (i * 0x04));
650 static inline void s5p_jpeg_set_hdctbl(void __iomem *regs)
652 /* this driver fills table 0 for this component */
653 s5p_jpeg_set_htbl(regs, hdctbl0, S5P_JPG_HDCTBL(0),
654 ARRAY_SIZE(hdctbl0));
657 static inline void s5p_jpeg_set_hdctblg(void __iomem *regs)
659 /* this driver fills table 0 for this component */
660 s5p_jpeg_set_htbl(regs, hdctblg0, S5P_JPG_HDCTBLG(0),
661 ARRAY_SIZE(hdctblg0));
664 static inline void s5p_jpeg_set_hactbl(void __iomem *regs)
666 /* this driver fills table 0 for this component */
667 s5p_jpeg_set_htbl(regs, hactbl0, S5P_JPG_HACTBL(0),
668 ARRAY_SIZE(hactbl0));
671 static inline void s5p_jpeg_set_hactblg(void __iomem *regs)
673 /* this driver fills table 0 for this component */
674 s5p_jpeg_set_htbl(regs, hactblg0, S5P_JPG_HACTBLG(0),
675 ARRAY_SIZE(hactblg0));
678 static inline void exynos4_jpeg_set_tbl(void __iomem *regs,
679 const unsigned char *tbl,
680 unsigned long tab, int len)
682 int i;
683 unsigned int dword;
685 for (i = 0; i < len; i += 4) {
686 dword = tbl[i] |
687 (tbl[i + 1] << 8) |
688 (tbl[i + 2] << 16) |
689 (tbl[i + 3] << 24);
690 writel(dword, regs + tab + i);
694 static inline void exynos4_jpeg_set_qtbl_lum(void __iomem *regs, int quality)
696 /* this driver fills quantisation table 0 with data for luma */
697 exynos4_jpeg_set_tbl(regs, qtbl_luminance[quality],
698 EXYNOS4_QTBL_CONTENT(0),
699 ARRAY_SIZE(qtbl_luminance[quality]));
702 static inline void exynos4_jpeg_set_qtbl_chr(void __iomem *regs, int quality)
704 /* this driver fills quantisation table 1 with data for chroma */
705 exynos4_jpeg_set_tbl(regs, qtbl_chrominance[quality],
706 EXYNOS4_QTBL_CONTENT(1),
707 ARRAY_SIZE(qtbl_chrominance[quality]));
710 static void exynos4_jpeg_set_huff_tbl(void __iomem *base)
712 exynos4_jpeg_set_tbl(base, hdctbl0, EXYNOS4_HUFF_TBL_HDCLL,
713 ARRAY_SIZE(hdctbl0));
714 exynos4_jpeg_set_tbl(base, hdctbl0, EXYNOS4_HUFF_TBL_HDCCL,
715 ARRAY_SIZE(hdctbl0));
716 exynos4_jpeg_set_tbl(base, hdctblg0, EXYNOS4_HUFF_TBL_HDCLV,
717 ARRAY_SIZE(hdctblg0));
718 exynos4_jpeg_set_tbl(base, hdctblg0, EXYNOS4_HUFF_TBL_HDCCV,
719 ARRAY_SIZE(hdctblg0));
720 exynos4_jpeg_set_tbl(base, hactbl0, EXYNOS4_HUFF_TBL_HACLL,
721 ARRAY_SIZE(hactbl0));
722 exynos4_jpeg_set_tbl(base, hactbl0, EXYNOS4_HUFF_TBL_HACCL,
723 ARRAY_SIZE(hactbl0));
724 exynos4_jpeg_set_tbl(base, hactblg0, EXYNOS4_HUFF_TBL_HACLV,
725 ARRAY_SIZE(hactblg0));
726 exynos4_jpeg_set_tbl(base, hactblg0, EXYNOS4_HUFF_TBL_HACCV,
727 ARRAY_SIZE(hactblg0));
730 static inline int __exynos4_huff_tbl(int class, int id, bool lenval)
733 * class: 0 - DC, 1 - AC
734 * id: 0 - Y, 1 - Cb/Cr
736 if (class) {
737 if (id)
738 return lenval ? EXYNOS4_HUFF_TBL_HACCL :
739 EXYNOS4_HUFF_TBL_HACCV;
740 return lenval ? EXYNOS4_HUFF_TBL_HACLL : EXYNOS4_HUFF_TBL_HACLV;
743 /* class == 0 */
744 if (id)
745 return lenval ? EXYNOS4_HUFF_TBL_HDCCL : EXYNOS4_HUFF_TBL_HDCCV;
747 return lenval ? EXYNOS4_HUFF_TBL_HDCLL : EXYNOS4_HUFF_TBL_HDCLV;
750 static inline int exynos4_huff_tbl_len(int class, int id)
752 return __exynos4_huff_tbl(class, id, true);
755 static inline int exynos4_huff_tbl_val(int class, int id)
757 return __exynos4_huff_tbl(class, id, false);
760 static int get_byte(struct s5p_jpeg_buffer *buf);
761 static int get_word_be(struct s5p_jpeg_buffer *buf, unsigned int *word);
762 static void skip(struct s5p_jpeg_buffer *buf, long len);
764 static void exynos4_jpeg_parse_decode_h_tbl(struct s5p_jpeg_ctx *ctx)
766 struct s5p_jpeg *jpeg = ctx->jpeg;
767 struct vb2_v4l2_buffer *vb = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
768 struct s5p_jpeg_buffer jpeg_buffer;
769 unsigned int word;
770 int c, x, components;
772 jpeg_buffer.size = 2; /* Ls */
773 jpeg_buffer.data =
774 (unsigned long)vb2_plane_vaddr(&vb->vb2_buf, 0) + ctx->out_q.sos + 2;
775 jpeg_buffer.curr = 0;
777 word = 0;
779 if (get_word_be(&jpeg_buffer, &word))
780 return;
781 jpeg_buffer.size = (long)word - 2;
782 jpeg_buffer.data += 2;
783 jpeg_buffer.curr = 0;
785 components = get_byte(&jpeg_buffer);
786 if (components == -1)
787 return;
788 while (components--) {
789 c = get_byte(&jpeg_buffer);
790 if (c == -1)
791 return;
792 x = get_byte(&jpeg_buffer);
793 if (x == -1)
794 return;
795 exynos4_jpeg_select_dec_h_tbl(jpeg->regs, c,
796 (((x >> 4) & 0x1) << 1) | (x & 0x1));
801 static void exynos4_jpeg_parse_huff_tbl(struct s5p_jpeg_ctx *ctx)
803 struct s5p_jpeg *jpeg = ctx->jpeg;
804 struct vb2_v4l2_buffer *vb = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
805 struct s5p_jpeg_buffer jpeg_buffer;
806 unsigned int word;
807 int c, i, n, j;
809 for (j = 0; j < ctx->out_q.dht.n; ++j) {
810 jpeg_buffer.size = ctx->out_q.dht.len[j];
811 jpeg_buffer.data = (unsigned long)vb2_plane_vaddr(&vb->vb2_buf, 0) +
812 ctx->out_q.dht.marker[j];
813 jpeg_buffer.curr = 0;
815 word = 0;
816 while (jpeg_buffer.curr < jpeg_buffer.size) {
817 char id, class;
819 c = get_byte(&jpeg_buffer);
820 if (c == -1)
821 return;
822 id = c & 0xf;
823 class = (c >> 4) & 0xf;
824 n = 0;
825 for (i = 0; i < 16; ++i) {
826 c = get_byte(&jpeg_buffer);
827 if (c == -1)
828 return;
829 word |= c << ((i % 4) * 8);
830 if ((i + 1) % 4 == 0) {
831 writel(word, jpeg->regs +
832 exynos4_huff_tbl_len(class, id) +
833 (i / 4) * 4);
834 word = 0;
836 n += c;
838 word = 0;
839 for (i = 0; i < n; ++i) {
840 c = get_byte(&jpeg_buffer);
841 if (c == -1)
842 return;
843 word |= c << ((i % 4) * 8);
844 if ((i + 1) % 4 == 0) {
845 writel(word, jpeg->regs +
846 exynos4_huff_tbl_val(class, id) +
847 (i / 4) * 4);
848 word = 0;
851 if (i % 4) {
852 writel(word, jpeg->regs +
853 exynos4_huff_tbl_val(class, id) + (i / 4) * 4);
855 word = 0;
860 static void exynos4_jpeg_parse_decode_q_tbl(struct s5p_jpeg_ctx *ctx)
862 struct s5p_jpeg *jpeg = ctx->jpeg;
863 struct vb2_v4l2_buffer *vb = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
864 struct s5p_jpeg_buffer jpeg_buffer;
865 int c, x, components;
867 jpeg_buffer.size = ctx->out_q.sof_len;
868 jpeg_buffer.data =
869 (unsigned long)vb2_plane_vaddr(&vb->vb2_buf, 0) + ctx->out_q.sof;
870 jpeg_buffer.curr = 0;
872 skip(&jpeg_buffer, 5); /* P, Y, X */
873 components = get_byte(&jpeg_buffer);
874 if (components == -1)
875 return;
877 exynos4_jpeg_set_dec_components(jpeg->regs, components);
879 while (components--) {
880 c = get_byte(&jpeg_buffer);
881 if (c == -1)
882 return;
883 skip(&jpeg_buffer, 1);
884 x = get_byte(&jpeg_buffer);
885 if (x == -1)
886 return;
887 exynos4_jpeg_select_dec_q_tbl(jpeg->regs, c, x);
891 static void exynos4_jpeg_parse_q_tbl(struct s5p_jpeg_ctx *ctx)
893 struct s5p_jpeg *jpeg = ctx->jpeg;
894 struct vb2_v4l2_buffer *vb = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
895 struct s5p_jpeg_buffer jpeg_buffer;
896 unsigned int word;
897 int c, i, j;
899 for (j = 0; j < ctx->out_q.dqt.n; ++j) {
900 jpeg_buffer.size = ctx->out_q.dqt.len[j];
901 jpeg_buffer.data = (unsigned long)vb2_plane_vaddr(&vb->vb2_buf, 0) +
902 ctx->out_q.dqt.marker[j];
903 jpeg_buffer.curr = 0;
905 word = 0;
906 while (jpeg_buffer.size - jpeg_buffer.curr >= 65) {
907 char id;
909 c = get_byte(&jpeg_buffer);
910 if (c == -1)
911 return;
912 id = c & 0xf;
913 /* nonzero means extended mode - not supported */
914 if ((c >> 4) & 0xf)
915 return;
916 for (i = 0; i < 64; ++i) {
917 c = get_byte(&jpeg_buffer);
918 if (c == -1)
919 return;
920 word |= c << ((i % 4) * 8);
921 if ((i + 1) % 4 == 0) {
922 writel(word, jpeg->regs +
923 EXYNOS4_QTBL_CONTENT(id) + (i / 4) * 4);
924 word = 0;
927 word = 0;
933 * ============================================================================
934 * Device file operations
935 * ============================================================================
938 static int queue_init(void *priv, struct vb2_queue *src_vq,
939 struct vb2_queue *dst_vq);
940 static struct s5p_jpeg_fmt *s5p_jpeg_find_format(struct s5p_jpeg_ctx *ctx,
941 __u32 pixelformat, unsigned int fmt_type);
942 static int s5p_jpeg_controls_create(struct s5p_jpeg_ctx *ctx);
944 static int s5p_jpeg_open(struct file *file)
946 struct s5p_jpeg *jpeg = video_drvdata(file);
947 struct video_device *vfd = video_devdata(file);
948 struct s5p_jpeg_ctx *ctx;
949 struct s5p_jpeg_fmt *out_fmt, *cap_fmt;
950 int ret = 0;
952 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
953 if (!ctx)
954 return -ENOMEM;
956 if (mutex_lock_interruptible(&jpeg->lock)) {
957 ret = -ERESTARTSYS;
958 goto free;
961 v4l2_fh_init(&ctx->fh, vfd);
962 /* Use separate control handler per file handle */
963 ctx->fh.ctrl_handler = &ctx->ctrl_handler;
964 file->private_data = &ctx->fh;
965 v4l2_fh_add(&ctx->fh);
967 ctx->jpeg = jpeg;
968 if (vfd == jpeg->vfd_encoder) {
969 ctx->mode = S5P_JPEG_ENCODE;
970 out_fmt = s5p_jpeg_find_format(ctx, V4L2_PIX_FMT_RGB565,
971 FMT_TYPE_OUTPUT);
972 cap_fmt = s5p_jpeg_find_format(ctx, V4L2_PIX_FMT_JPEG,
973 FMT_TYPE_CAPTURE);
974 } else {
975 ctx->mode = S5P_JPEG_DECODE;
976 out_fmt = s5p_jpeg_find_format(ctx, V4L2_PIX_FMT_JPEG,
977 FMT_TYPE_OUTPUT);
978 cap_fmt = s5p_jpeg_find_format(ctx, V4L2_PIX_FMT_YUYV,
979 FMT_TYPE_CAPTURE);
980 ctx->scale_factor = EXYNOS3250_DEC_SCALE_FACTOR_8_8;
983 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(jpeg->m2m_dev, ctx, queue_init);
984 if (IS_ERR(ctx->fh.m2m_ctx)) {
985 ret = PTR_ERR(ctx->fh.m2m_ctx);
986 goto error;
989 ctx->out_q.fmt = out_fmt;
990 ctx->cap_q.fmt = cap_fmt;
992 ret = s5p_jpeg_controls_create(ctx);
993 if (ret < 0)
994 goto error;
996 mutex_unlock(&jpeg->lock);
997 return 0;
999 error:
1000 v4l2_fh_del(&ctx->fh);
1001 v4l2_fh_exit(&ctx->fh);
1002 mutex_unlock(&jpeg->lock);
1003 free:
1004 kfree(ctx);
1005 return ret;
1008 static int s5p_jpeg_release(struct file *file)
1010 struct s5p_jpeg *jpeg = video_drvdata(file);
1011 struct s5p_jpeg_ctx *ctx = fh_to_ctx(file->private_data);
1013 mutex_lock(&jpeg->lock);
1014 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
1015 v4l2_ctrl_handler_free(&ctx->ctrl_handler);
1016 v4l2_fh_del(&ctx->fh);
1017 v4l2_fh_exit(&ctx->fh);
1018 kfree(ctx);
1019 mutex_unlock(&jpeg->lock);
1021 return 0;
1024 static const struct v4l2_file_operations s5p_jpeg_fops = {
1025 .owner = THIS_MODULE,
1026 .open = s5p_jpeg_open,
1027 .release = s5p_jpeg_release,
1028 .poll = v4l2_m2m_fop_poll,
1029 .unlocked_ioctl = video_ioctl2,
1030 .mmap = v4l2_m2m_fop_mmap,
1034 * ============================================================================
1035 * video ioctl operations
1036 * ============================================================================
1039 static int get_byte(struct s5p_jpeg_buffer *buf)
1041 if (buf->curr >= buf->size)
1042 return -1;
1044 return ((unsigned char *)buf->data)[buf->curr++];
1047 static int get_word_be(struct s5p_jpeg_buffer *buf, unsigned int *word)
1049 unsigned int temp;
1050 int byte;
1052 byte = get_byte(buf);
1053 if (byte == -1)
1054 return -1;
1055 temp = byte << 8;
1056 byte = get_byte(buf);
1057 if (byte == -1)
1058 return -1;
1059 *word = (unsigned int)byte | temp;
1060 return 0;
1063 static void skip(struct s5p_jpeg_buffer *buf, long len)
1065 if (len <= 0)
1066 return;
1068 while (len--)
1069 get_byte(buf);
1072 static bool s5p_jpeg_subsampling_decode(struct s5p_jpeg_ctx *ctx,
1073 unsigned int subsampling)
1075 unsigned int version;
1077 switch (subsampling) {
1078 case 0x11:
1079 ctx->subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_444;
1080 break;
1081 case 0x21:
1082 ctx->subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_422;
1083 break;
1084 case 0x22:
1085 ctx->subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_420;
1086 break;
1087 case 0x33:
1088 ctx->subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_GRAY;
1089 break;
1090 case 0x41:
1092 * 4:1:1 subsampling only supported by 3250, 5420, and 5433
1093 * variants
1095 version = ctx->jpeg->variant->version;
1096 if (version != SJPEG_EXYNOS3250 &&
1097 version != SJPEG_EXYNOS5420 &&
1098 version != SJPEG_EXYNOS5433)
1099 return false;
1101 ctx->subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_411;
1102 break;
1103 default:
1104 return false;
1107 return true;
1110 static bool s5p_jpeg_parse_hdr(struct s5p_jpeg_q_data *result,
1111 unsigned long buffer, unsigned long size,
1112 struct s5p_jpeg_ctx *ctx)
1114 int c, components = 0, notfound, n_dht = 0, n_dqt = 0;
1115 unsigned int height = 0, width = 0, word, subsampling = 0;
1116 unsigned int sos = 0, sof = 0, sof_len = 0;
1117 unsigned int dht[S5P_JPEG_MAX_MARKER], dht_len[S5P_JPEG_MAX_MARKER];
1118 unsigned int dqt[S5P_JPEG_MAX_MARKER], dqt_len[S5P_JPEG_MAX_MARKER];
1119 long length;
1120 struct s5p_jpeg_buffer jpeg_buffer;
1122 jpeg_buffer.size = size;
1123 jpeg_buffer.data = buffer;
1124 jpeg_buffer.curr = 0;
1126 notfound = 1;
1127 while (notfound || !sos) {
1128 c = get_byte(&jpeg_buffer);
1129 if (c == -1)
1130 return false;
1131 if (c != 0xff)
1132 continue;
1134 c = get_byte(&jpeg_buffer);
1135 while (c == 0xff);
1136 if (c == -1)
1137 return false;
1138 if (c == 0)
1139 continue;
1140 length = 0;
1141 switch (c) {
1142 /* SOF0: baseline JPEG */
1143 case SOF0:
1144 if (get_word_be(&jpeg_buffer, &word))
1145 break;
1146 length = (long)word - 2;
1147 if (!length)
1148 return false;
1149 sof = jpeg_buffer.curr; /* after 0xffc0 */
1150 sof_len = length;
1151 if (get_byte(&jpeg_buffer) == -1)
1152 break;
1153 if (get_word_be(&jpeg_buffer, &height))
1154 break;
1155 if (get_word_be(&jpeg_buffer, &width))
1156 break;
1157 components = get_byte(&jpeg_buffer);
1158 if (components == -1)
1159 break;
1161 if (components == 1) {
1162 subsampling = 0x33;
1163 } else {
1164 skip(&jpeg_buffer, 1);
1165 subsampling = get_byte(&jpeg_buffer);
1166 skip(&jpeg_buffer, 1);
1168 if (components > 3)
1169 return false;
1170 skip(&jpeg_buffer, components * 2);
1171 notfound = 0;
1172 break;
1174 case DQT:
1175 if (get_word_be(&jpeg_buffer, &word))
1176 break;
1177 length = (long)word - 2;
1178 if (!length)
1179 return false;
1180 if (n_dqt >= S5P_JPEG_MAX_MARKER)
1181 return false;
1182 dqt[n_dqt] = jpeg_buffer.curr; /* after 0xffdb */
1183 dqt_len[n_dqt++] = length;
1184 skip(&jpeg_buffer, length);
1185 break;
1187 case DHT:
1188 if (get_word_be(&jpeg_buffer, &word))
1189 break;
1190 length = (long)word - 2;
1191 if (!length)
1192 return false;
1193 if (n_dht >= S5P_JPEG_MAX_MARKER)
1194 return false;
1195 dht[n_dht] = jpeg_buffer.curr; /* after 0xffc4 */
1196 dht_len[n_dht++] = length;
1197 skip(&jpeg_buffer, length);
1198 break;
1200 case SOS:
1201 sos = jpeg_buffer.curr - 2; /* 0xffda */
1202 break;
1204 /* skip payload-less markers */
1205 case RST ... RST + 7:
1206 case SOI:
1207 case EOI:
1208 case TEM:
1209 break;
1211 /* skip uninteresting payload markers */
1212 default:
1213 if (get_word_be(&jpeg_buffer, &word))
1214 break;
1215 length = (long)word - 2;
1216 skip(&jpeg_buffer, length);
1217 break;
1221 if (notfound || !sos || !s5p_jpeg_subsampling_decode(ctx, subsampling))
1222 return false;
1224 result->w = width;
1225 result->h = height;
1226 result->sos = sos;
1227 result->dht.n = n_dht;
1228 while (n_dht--) {
1229 result->dht.marker[n_dht] = dht[n_dht];
1230 result->dht.len[n_dht] = dht_len[n_dht];
1232 result->dqt.n = n_dqt;
1233 while (n_dqt--) {
1234 result->dqt.marker[n_dqt] = dqt[n_dqt];
1235 result->dqt.len[n_dqt] = dqt_len[n_dqt];
1237 result->sof = sof;
1238 result->sof_len = sof_len;
1240 return true;
1243 static int s5p_jpeg_querycap(struct file *file, void *priv,
1244 struct v4l2_capability *cap)
1246 struct s5p_jpeg_ctx *ctx = fh_to_ctx(priv);
1248 if (ctx->mode == S5P_JPEG_ENCODE) {
1249 strscpy(cap->driver, S5P_JPEG_M2M_NAME,
1250 sizeof(cap->driver));
1251 strscpy(cap->card, S5P_JPEG_M2M_NAME " encoder",
1252 sizeof(cap->card));
1253 } else {
1254 strscpy(cap->driver, S5P_JPEG_M2M_NAME,
1255 sizeof(cap->driver));
1256 strscpy(cap->card, S5P_JPEG_M2M_NAME " decoder",
1257 sizeof(cap->card));
1259 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
1260 dev_name(ctx->jpeg->dev));
1261 return 0;
1264 static int enum_fmt(struct s5p_jpeg_ctx *ctx,
1265 struct s5p_jpeg_fmt *sjpeg_formats, int n,
1266 struct v4l2_fmtdesc *f, u32 type)
1268 int i, num = 0;
1269 unsigned int fmt_ver_flag = ctx->jpeg->variant->fmt_ver_flag;
1271 for (i = 0; i < n; ++i) {
1272 if (sjpeg_formats[i].flags & type &&
1273 sjpeg_formats[i].flags & fmt_ver_flag) {
1274 /* index-th format of type type found ? */
1275 if (num == f->index)
1276 break;
1277 /* Correct type but haven't reached our index yet,
1278 * just increment per-type index
1280 ++num;
1284 /* Format not found */
1285 if (i >= n)
1286 return -EINVAL;
1288 f->pixelformat = sjpeg_formats[i].fourcc;
1290 return 0;
1293 static int s5p_jpeg_enum_fmt_vid_cap(struct file *file, void *priv,
1294 struct v4l2_fmtdesc *f)
1296 struct s5p_jpeg_ctx *ctx = fh_to_ctx(priv);
1298 if (ctx->mode == S5P_JPEG_ENCODE)
1299 return enum_fmt(ctx, sjpeg_formats, SJPEG_NUM_FORMATS, f,
1300 SJPEG_FMT_FLAG_ENC_CAPTURE);
1302 return enum_fmt(ctx, sjpeg_formats, SJPEG_NUM_FORMATS, f,
1303 SJPEG_FMT_FLAG_DEC_CAPTURE);
1306 static int s5p_jpeg_enum_fmt_vid_out(struct file *file, void *priv,
1307 struct v4l2_fmtdesc *f)
1309 struct s5p_jpeg_ctx *ctx = fh_to_ctx(priv);
1311 if (ctx->mode == S5P_JPEG_ENCODE)
1312 return enum_fmt(ctx, sjpeg_formats, SJPEG_NUM_FORMATS, f,
1313 SJPEG_FMT_FLAG_ENC_OUTPUT);
1315 return enum_fmt(ctx, sjpeg_formats, SJPEG_NUM_FORMATS, f,
1316 SJPEG_FMT_FLAG_DEC_OUTPUT);
1319 static struct s5p_jpeg_q_data *get_q_data(struct s5p_jpeg_ctx *ctx,
1320 enum v4l2_buf_type type)
1322 if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1323 return &ctx->out_q;
1324 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1325 return &ctx->cap_q;
1327 return NULL;
1330 static int s5p_jpeg_g_fmt(struct file *file, void *priv, struct v4l2_format *f)
1332 struct vb2_queue *vq;
1333 struct s5p_jpeg_q_data *q_data = NULL;
1334 struct v4l2_pix_format *pix = &f->fmt.pix;
1335 struct s5p_jpeg_ctx *ct = fh_to_ctx(priv);
1337 vq = v4l2_m2m_get_vq(ct->fh.m2m_ctx, f->type);
1338 if (!vq)
1339 return -EINVAL;
1341 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1342 ct->mode == S5P_JPEG_DECODE && !ct->hdr_parsed)
1343 return -EINVAL;
1344 q_data = get_q_data(ct, f->type);
1345 BUG_ON(q_data == NULL);
1347 pix->width = q_data->w;
1348 pix->height = q_data->h;
1349 pix->field = V4L2_FIELD_NONE;
1350 pix->pixelformat = q_data->fmt->fourcc;
1351 pix->bytesperline = 0;
1352 if (q_data->fmt->fourcc != V4L2_PIX_FMT_JPEG) {
1353 u32 bpl = q_data->w;
1355 if (q_data->fmt->colplanes == 1)
1356 bpl = (bpl * q_data->fmt->depth) >> 3;
1357 pix->bytesperline = bpl;
1359 pix->sizeimage = q_data->size;
1361 return 0;
1364 static struct s5p_jpeg_fmt *s5p_jpeg_find_format(struct s5p_jpeg_ctx *ctx,
1365 u32 pixelformat, unsigned int fmt_type)
1367 unsigned int k, fmt_flag;
1369 if (ctx->mode == S5P_JPEG_ENCODE)
1370 fmt_flag = (fmt_type == FMT_TYPE_OUTPUT) ?
1371 SJPEG_FMT_FLAG_ENC_OUTPUT :
1372 SJPEG_FMT_FLAG_ENC_CAPTURE;
1373 else
1374 fmt_flag = (fmt_type == FMT_TYPE_OUTPUT) ?
1375 SJPEG_FMT_FLAG_DEC_OUTPUT :
1376 SJPEG_FMT_FLAG_DEC_CAPTURE;
1378 for (k = 0; k < ARRAY_SIZE(sjpeg_formats); k++) {
1379 struct s5p_jpeg_fmt *fmt = &sjpeg_formats[k];
1381 if (fmt->fourcc == pixelformat &&
1382 fmt->flags & fmt_flag &&
1383 fmt->flags & ctx->jpeg->variant->fmt_ver_flag) {
1384 return fmt;
1388 return NULL;
1391 static void jpeg_bound_align_image(struct s5p_jpeg_ctx *ctx,
1392 u32 *w, unsigned int wmin, unsigned int wmax,
1393 unsigned int walign,
1394 u32 *h, unsigned int hmin, unsigned int hmax,
1395 unsigned int halign)
1397 int width, height, w_step, h_step;
1399 width = *w;
1400 height = *h;
1402 w_step = 1 << walign;
1403 h_step = 1 << halign;
1405 if (ctx->jpeg->variant->hw3250_compat) {
1407 * Rightmost and bottommost pixels are cropped by the
1408 * Exynos3250/compatible JPEG IP for RGB formats, for the
1409 * specific width and height values respectively. This
1410 * assignment will result in v4l_bound_align_image returning
1411 * dimensions reduced by 1 for the aforementioned cases.
1413 if (w_step == 4 && ((width & 3) == 1)) {
1414 wmax = width;
1415 hmax = height;
1419 v4l_bound_align_image(w, wmin, wmax, walign, h, hmin, hmax, halign, 0);
1421 if (*w < width && (*w + w_step) < wmax)
1422 *w += w_step;
1423 if (*h < height && (*h + h_step) < hmax)
1424 *h += h_step;
1427 static int vidioc_try_fmt(struct v4l2_format *f, struct s5p_jpeg_fmt *fmt,
1428 struct s5p_jpeg_ctx *ctx, int q_type)
1430 struct v4l2_pix_format *pix = &f->fmt.pix;
1432 if (pix->field == V4L2_FIELD_ANY)
1433 pix->field = V4L2_FIELD_NONE;
1434 else if (pix->field != V4L2_FIELD_NONE)
1435 return -EINVAL;
1437 /* V4L2 specification suggests the driver corrects the format struct
1438 * if any of the dimensions is unsupported
1440 if (q_type == FMT_TYPE_OUTPUT)
1441 jpeg_bound_align_image(ctx, &pix->width, S5P_JPEG_MIN_WIDTH,
1442 S5P_JPEG_MAX_WIDTH, 0,
1443 &pix->height, S5P_JPEG_MIN_HEIGHT,
1444 S5P_JPEG_MAX_HEIGHT, 0);
1445 else
1446 jpeg_bound_align_image(ctx, &pix->width, S5P_JPEG_MIN_WIDTH,
1447 S5P_JPEG_MAX_WIDTH, fmt->h_align,
1448 &pix->height, S5P_JPEG_MIN_HEIGHT,
1449 S5P_JPEG_MAX_HEIGHT, fmt->v_align);
1451 if (fmt->fourcc == V4L2_PIX_FMT_JPEG) {
1452 if (pix->sizeimage <= 0)
1453 pix->sizeimage = PAGE_SIZE;
1454 pix->bytesperline = 0;
1455 } else {
1456 u32 bpl = pix->bytesperline;
1458 if (fmt->colplanes > 1 && bpl < pix->width)
1459 bpl = pix->width; /* planar */
1461 if (fmt->colplanes == 1 && /* packed */
1462 (bpl << 3) / fmt->depth < pix->width)
1463 bpl = (pix->width * fmt->depth) >> 3;
1465 pix->bytesperline = bpl;
1466 pix->sizeimage = (pix->width * pix->height * fmt->depth) >> 3;
1469 return 0;
1472 static int s5p_jpeg_try_fmt_vid_cap(struct file *file, void *priv,
1473 struct v4l2_format *f)
1475 struct s5p_jpeg_ctx *ctx = fh_to_ctx(priv);
1476 struct v4l2_pix_format *pix = &f->fmt.pix;
1477 struct s5p_jpeg_fmt *fmt;
1478 int ret;
1480 fmt = s5p_jpeg_find_format(ctx, f->fmt.pix.pixelformat,
1481 FMT_TYPE_CAPTURE);
1482 if (!fmt) {
1483 v4l2_err(&ctx->jpeg->v4l2_dev,
1484 "Fourcc format (0x%08x) invalid.\n",
1485 f->fmt.pix.pixelformat);
1486 return -EINVAL;
1489 if (!ctx->jpeg->variant->hw_ex4_compat || ctx->mode != S5P_JPEG_DECODE)
1490 goto exit;
1493 * The exynos4x12 device requires resulting YUV image
1494 * subsampling not to be lower than the input jpeg subsampling.
1495 * If this requirement is not met then downgrade the requested
1496 * capture format to the one with subsampling equal to the input jpeg.
1498 if ((fmt->flags & SJPEG_FMT_NON_RGB) &&
1499 (fmt->subsampling < ctx->subsampling)) {
1500 ret = s5p_jpeg_adjust_fourcc_to_subsampling(ctx->subsampling,
1501 fmt->fourcc,
1502 &pix->pixelformat,
1503 ctx);
1504 if (ret < 0)
1505 pix->pixelformat = V4L2_PIX_FMT_GREY;
1507 fmt = s5p_jpeg_find_format(ctx, pix->pixelformat,
1508 FMT_TYPE_CAPTURE);
1512 * Decompression of a JPEG file with 4:2:0 subsampling and odd
1513 * width to the YUV 4:2:0 compliant formats produces a raw image
1514 * with broken luma component. Adjust capture format to RGB565
1515 * in such a case.
1517 if (ctx->subsampling == V4L2_JPEG_CHROMA_SUBSAMPLING_420 &&
1518 (ctx->out_q.w & 1) &&
1519 (pix->pixelformat == V4L2_PIX_FMT_NV12 ||
1520 pix->pixelformat == V4L2_PIX_FMT_NV21 ||
1521 pix->pixelformat == V4L2_PIX_FMT_YUV420)) {
1522 pix->pixelformat = V4L2_PIX_FMT_RGB565;
1523 fmt = s5p_jpeg_find_format(ctx, pix->pixelformat,
1524 FMT_TYPE_CAPTURE);
1527 exit:
1528 return vidioc_try_fmt(f, fmt, ctx, FMT_TYPE_CAPTURE);
1531 static int s5p_jpeg_try_fmt_vid_out(struct file *file, void *priv,
1532 struct v4l2_format *f)
1534 struct s5p_jpeg_ctx *ctx = fh_to_ctx(priv);
1535 struct s5p_jpeg_fmt *fmt;
1537 fmt = s5p_jpeg_find_format(ctx, f->fmt.pix.pixelformat,
1538 FMT_TYPE_OUTPUT);
1539 if (!fmt) {
1540 v4l2_err(&ctx->jpeg->v4l2_dev,
1541 "Fourcc format (0x%08x) invalid.\n",
1542 f->fmt.pix.pixelformat);
1543 return -EINVAL;
1546 return vidioc_try_fmt(f, fmt, ctx, FMT_TYPE_OUTPUT);
1549 static int exynos4_jpeg_get_output_buffer_size(struct s5p_jpeg_ctx *ctx,
1550 struct v4l2_format *f,
1551 int fmt_depth)
1553 struct v4l2_pix_format *pix = &f->fmt.pix;
1554 u32 pix_fmt = f->fmt.pix.pixelformat;
1555 int w = pix->width, h = pix->height, wh_align;
1556 int padding = 0;
1558 if (pix_fmt == V4L2_PIX_FMT_RGB32 ||
1559 pix_fmt == V4L2_PIX_FMT_RGB565 ||
1560 pix_fmt == V4L2_PIX_FMT_NV24 ||
1561 pix_fmt == V4L2_PIX_FMT_NV42 ||
1562 pix_fmt == V4L2_PIX_FMT_NV12 ||
1563 pix_fmt == V4L2_PIX_FMT_NV21 ||
1564 pix_fmt == V4L2_PIX_FMT_YUV420)
1565 wh_align = 4;
1566 else
1567 wh_align = 1;
1569 jpeg_bound_align_image(ctx, &w, S5P_JPEG_MIN_WIDTH,
1570 S5P_JPEG_MAX_WIDTH, wh_align,
1571 &h, S5P_JPEG_MIN_HEIGHT,
1572 S5P_JPEG_MAX_HEIGHT, wh_align);
1574 if (ctx->jpeg->variant->version == SJPEG_EXYNOS4)
1575 padding = PAGE_SIZE;
1577 return (w * h * fmt_depth >> 3) + padding;
1580 static int exynos3250_jpeg_try_downscale(struct s5p_jpeg_ctx *ctx,
1581 struct v4l2_rect *r);
1583 static int s5p_jpeg_s_fmt(struct s5p_jpeg_ctx *ct, struct v4l2_format *f)
1585 struct vb2_queue *vq;
1586 struct s5p_jpeg_q_data *q_data = NULL;
1587 struct v4l2_pix_format *pix = &f->fmt.pix;
1588 struct v4l2_ctrl *ctrl_subs;
1589 struct v4l2_rect scale_rect;
1590 unsigned int f_type;
1592 vq = v4l2_m2m_get_vq(ct->fh.m2m_ctx, f->type);
1593 if (!vq)
1594 return -EINVAL;
1596 q_data = get_q_data(ct, f->type);
1597 BUG_ON(q_data == NULL);
1599 if (vb2_is_busy(vq)) {
1600 v4l2_err(&ct->jpeg->v4l2_dev, "%s queue busy\n", __func__);
1601 return -EBUSY;
1604 f_type = V4L2_TYPE_IS_OUTPUT(f->type) ?
1605 FMT_TYPE_OUTPUT : FMT_TYPE_CAPTURE;
1607 q_data->fmt = s5p_jpeg_find_format(ct, pix->pixelformat, f_type);
1608 if (ct->mode == S5P_JPEG_ENCODE ||
1609 (ct->mode == S5P_JPEG_DECODE &&
1610 q_data->fmt->fourcc != V4L2_PIX_FMT_JPEG)) {
1611 q_data->w = pix->width;
1612 q_data->h = pix->height;
1614 if (q_data->fmt->fourcc != V4L2_PIX_FMT_JPEG) {
1616 * During encoding Exynos4x12 SoCs access wider memory area
1617 * than it results from Image_x and Image_y values written to
1618 * the JPEG_IMAGE_SIZE register. In order to avoid sysmmu
1619 * page fault calculate proper buffer size in such a case.
1621 if (ct->jpeg->variant->hw_ex4_compat &&
1622 f_type == FMT_TYPE_OUTPUT && ct->mode == S5P_JPEG_ENCODE)
1623 q_data->size = exynos4_jpeg_get_output_buffer_size(ct,
1625 q_data->fmt->depth);
1626 else
1627 q_data->size = q_data->w * q_data->h *
1628 q_data->fmt->depth >> 3;
1629 } else {
1630 q_data->size = pix->sizeimage;
1633 if (f_type == FMT_TYPE_OUTPUT) {
1634 ctrl_subs = v4l2_ctrl_find(&ct->ctrl_handler,
1635 V4L2_CID_JPEG_CHROMA_SUBSAMPLING);
1636 if (ctrl_subs)
1637 v4l2_ctrl_s_ctrl(ctrl_subs, q_data->fmt->subsampling);
1638 ct->crop_altered = false;
1642 * For decoding init crop_rect with capture buffer dimmensions which
1643 * contain aligned dimensions of the input JPEG image and do it only
1644 * if crop rectangle hasn't been altered by the user space e.g. with
1645 * S_SELECTION ioctl. For encoding assign output buffer dimensions.
1647 if (!ct->crop_altered &&
1648 ((ct->mode == S5P_JPEG_DECODE && f_type == FMT_TYPE_CAPTURE) ||
1649 (ct->mode == S5P_JPEG_ENCODE && f_type == FMT_TYPE_OUTPUT))) {
1650 ct->crop_rect.width = pix->width;
1651 ct->crop_rect.height = pix->height;
1655 * Prevent downscaling to YUV420 format by more than 2
1656 * for Exynos3250/compatible SoC as it produces broken raw image
1657 * in such cases.
1659 if (ct->mode == S5P_JPEG_DECODE &&
1660 f_type == FMT_TYPE_CAPTURE &&
1661 ct->jpeg->variant->hw3250_compat &&
1662 pix->pixelformat == V4L2_PIX_FMT_YUV420 &&
1663 ct->scale_factor > 2) {
1664 scale_rect.width = ct->out_q.w / 2;
1665 scale_rect.height = ct->out_q.h / 2;
1666 exynos3250_jpeg_try_downscale(ct, &scale_rect);
1669 return 0;
1672 static int s5p_jpeg_s_fmt_vid_cap(struct file *file, void *priv,
1673 struct v4l2_format *f)
1675 int ret;
1677 ret = s5p_jpeg_try_fmt_vid_cap(file, priv, f);
1678 if (ret)
1679 return ret;
1681 return s5p_jpeg_s_fmt(fh_to_ctx(priv), f);
1684 static int s5p_jpeg_s_fmt_vid_out(struct file *file, void *priv,
1685 struct v4l2_format *f)
1687 int ret;
1689 ret = s5p_jpeg_try_fmt_vid_out(file, priv, f);
1690 if (ret)
1691 return ret;
1693 return s5p_jpeg_s_fmt(fh_to_ctx(priv), f);
1696 static int s5p_jpeg_subscribe_event(struct v4l2_fh *fh,
1697 const struct v4l2_event_subscription *sub)
1699 if (sub->type == V4L2_EVENT_SOURCE_CHANGE)
1700 return v4l2_src_change_event_subscribe(fh, sub);
1702 return -EINVAL;
1705 static int exynos3250_jpeg_try_downscale(struct s5p_jpeg_ctx *ctx,
1706 struct v4l2_rect *r)
1708 int w_ratio, h_ratio, scale_factor, cur_ratio, i;
1710 w_ratio = ctx->out_q.w / r->width;
1711 h_ratio = ctx->out_q.h / r->height;
1713 scale_factor = w_ratio > h_ratio ? w_ratio : h_ratio;
1714 scale_factor = clamp_val(scale_factor, 1, 8);
1716 /* Align scale ratio to the nearest power of 2 */
1717 for (i = 0; i <= 3; ++i) {
1718 cur_ratio = 1 << i;
1719 if (scale_factor <= cur_ratio) {
1720 ctx->scale_factor = cur_ratio;
1721 break;
1725 r->width = round_down(ctx->out_q.w / ctx->scale_factor, 2);
1726 r->height = round_down(ctx->out_q.h / ctx->scale_factor, 2);
1728 ctx->crop_rect.width = r->width;
1729 ctx->crop_rect.height = r->height;
1730 ctx->crop_rect.left = 0;
1731 ctx->crop_rect.top = 0;
1733 ctx->crop_altered = true;
1735 return 0;
1738 /* Return 1 if rectangle a is enclosed in rectangle b, or 0 otherwise. */
1739 static int enclosed_rectangle(struct v4l2_rect *a, struct v4l2_rect *b)
1741 if (a->left < b->left || a->top < b->top)
1742 return 0;
1743 if (a->left + a->width > b->left + b->width)
1744 return 0;
1745 if (a->top + a->height > b->top + b->height)
1746 return 0;
1748 return 1;
1751 static int exynos3250_jpeg_try_crop(struct s5p_jpeg_ctx *ctx,
1752 struct v4l2_rect *r)
1754 struct v4l2_rect base_rect;
1755 int w_step, h_step;
1757 switch (ctx->cap_q.fmt->fourcc) {
1758 case V4L2_PIX_FMT_NV12:
1759 case V4L2_PIX_FMT_NV21:
1760 w_step = 1;
1761 h_step = 2;
1762 break;
1763 case V4L2_PIX_FMT_YUV420:
1764 w_step = 2;
1765 h_step = 2;
1766 break;
1767 default:
1768 w_step = 1;
1769 h_step = 1;
1770 break;
1773 base_rect.top = 0;
1774 base_rect.left = 0;
1775 base_rect.width = ctx->out_q.w;
1776 base_rect.height = ctx->out_q.h;
1778 r->width = round_down(r->width, w_step);
1779 r->height = round_down(r->height, h_step);
1780 r->left = round_down(r->left, 2);
1781 r->top = round_down(r->top, 2);
1783 if (!enclosed_rectangle(r, &base_rect))
1784 return -EINVAL;
1786 ctx->crop_rect.left = r->left;
1787 ctx->crop_rect.top = r->top;
1788 ctx->crop_rect.width = r->width;
1789 ctx->crop_rect.height = r->height;
1791 ctx->crop_altered = true;
1793 return 0;
1797 * V4L2 controls
1800 static int s5p_jpeg_g_selection(struct file *file, void *priv,
1801 struct v4l2_selection *s)
1803 struct s5p_jpeg_ctx *ctx = fh_to_ctx(priv);
1805 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
1806 s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1807 return -EINVAL;
1809 /* For JPEG blob active == default == bounds */
1810 switch (s->target) {
1811 case V4L2_SEL_TGT_CROP:
1812 case V4L2_SEL_TGT_CROP_BOUNDS:
1813 case V4L2_SEL_TGT_CROP_DEFAULT:
1814 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
1815 s->r.width = ctx->out_q.w;
1816 s->r.height = ctx->out_q.h;
1817 s->r.left = 0;
1818 s->r.top = 0;
1819 break;
1820 case V4L2_SEL_TGT_COMPOSE:
1821 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1822 case V4L2_SEL_TGT_COMPOSE_PADDED:
1823 s->r.width = ctx->crop_rect.width;
1824 s->r.height = ctx->crop_rect.height;
1825 s->r.left = ctx->crop_rect.left;
1826 s->r.top = ctx->crop_rect.top;
1827 break;
1828 default:
1829 return -EINVAL;
1831 return 0;
1835 * V4L2 controls
1837 static int s5p_jpeg_s_selection(struct file *file, void *fh,
1838 struct v4l2_selection *s)
1840 struct s5p_jpeg_ctx *ctx = fh_to_ctx(file->private_data);
1841 struct v4l2_rect *rect = &s->r;
1842 int ret = -EINVAL;
1844 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1845 return -EINVAL;
1847 if (s->target == V4L2_SEL_TGT_COMPOSE) {
1848 if (ctx->mode != S5P_JPEG_DECODE)
1849 return -EINVAL;
1850 if (ctx->jpeg->variant->hw3250_compat)
1851 ret = exynos3250_jpeg_try_downscale(ctx, rect);
1852 } else if (s->target == V4L2_SEL_TGT_CROP) {
1853 if (ctx->mode != S5P_JPEG_ENCODE)
1854 return -EINVAL;
1855 if (ctx->jpeg->variant->hw3250_compat)
1856 ret = exynos3250_jpeg_try_crop(ctx, rect);
1859 return ret;
1862 static int s5p_jpeg_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
1864 struct s5p_jpeg_ctx *ctx = ctrl_to_ctx(ctrl);
1865 struct s5p_jpeg *jpeg = ctx->jpeg;
1866 unsigned long flags;
1868 switch (ctrl->id) {
1869 case V4L2_CID_JPEG_CHROMA_SUBSAMPLING:
1870 spin_lock_irqsave(&jpeg->slock, flags);
1871 ctrl->val = s5p_jpeg_to_user_subsampling(ctx);
1872 spin_unlock_irqrestore(&jpeg->slock, flags);
1873 break;
1876 return 0;
1879 static int s5p_jpeg_adjust_subs_ctrl(struct s5p_jpeg_ctx *ctx, int *ctrl_val)
1881 switch (ctx->jpeg->variant->version) {
1882 case SJPEG_S5P:
1883 return 0;
1884 case SJPEG_EXYNOS3250:
1885 case SJPEG_EXYNOS5420:
1887 * The exynos3250/compatible device can produce JPEG image only
1888 * of 4:4:4 subsampling when given RGB32 source image.
1890 if (ctx->out_q.fmt->fourcc == V4L2_PIX_FMT_RGB32)
1891 *ctrl_val = 0;
1892 break;
1893 case SJPEG_EXYNOS4:
1895 * The exynos4x12 device requires input raw image fourcc
1896 * to be V4L2_PIX_FMT_GREY if gray jpeg format
1897 * is to be set.
1899 if (ctx->out_q.fmt->fourcc != V4L2_PIX_FMT_GREY &&
1900 *ctrl_val == V4L2_JPEG_CHROMA_SUBSAMPLING_GRAY)
1901 return -EINVAL;
1902 break;
1906 * The exynos4x12 and exynos3250/compatible devices require resulting
1907 * jpeg subsampling not to be lower than the input raw image
1908 * subsampling.
1910 if (ctx->out_q.fmt->subsampling > *ctrl_val)
1911 *ctrl_val = ctx->out_q.fmt->subsampling;
1913 return 0;
1916 static int s5p_jpeg_try_ctrl(struct v4l2_ctrl *ctrl)
1918 struct s5p_jpeg_ctx *ctx = ctrl_to_ctx(ctrl);
1919 unsigned long flags;
1920 int ret = 0;
1922 spin_lock_irqsave(&ctx->jpeg->slock, flags);
1924 if (ctrl->id == V4L2_CID_JPEG_CHROMA_SUBSAMPLING)
1925 ret = s5p_jpeg_adjust_subs_ctrl(ctx, &ctrl->val);
1927 spin_unlock_irqrestore(&ctx->jpeg->slock, flags);
1928 return ret;
1931 static int s5p_jpeg_s_ctrl(struct v4l2_ctrl *ctrl)
1933 struct s5p_jpeg_ctx *ctx = ctrl_to_ctx(ctrl);
1934 unsigned long flags;
1936 spin_lock_irqsave(&ctx->jpeg->slock, flags);
1938 switch (ctrl->id) {
1939 case V4L2_CID_JPEG_COMPRESSION_QUALITY:
1940 ctx->compr_quality = ctrl->val;
1941 break;
1942 case V4L2_CID_JPEG_RESTART_INTERVAL:
1943 ctx->restart_interval = ctrl->val;
1944 break;
1945 case V4L2_CID_JPEG_CHROMA_SUBSAMPLING:
1946 ctx->subsampling = ctrl->val;
1947 break;
1950 spin_unlock_irqrestore(&ctx->jpeg->slock, flags);
1951 return 0;
1954 static const struct v4l2_ctrl_ops s5p_jpeg_ctrl_ops = {
1955 .g_volatile_ctrl = s5p_jpeg_g_volatile_ctrl,
1956 .try_ctrl = s5p_jpeg_try_ctrl,
1957 .s_ctrl = s5p_jpeg_s_ctrl,
1960 static int s5p_jpeg_controls_create(struct s5p_jpeg_ctx *ctx)
1962 unsigned int mask = ~0x27; /* 444, 422, 420, GRAY */
1963 struct v4l2_ctrl *ctrl;
1964 int ret;
1966 v4l2_ctrl_handler_init(&ctx->ctrl_handler, 3);
1968 if (ctx->mode == S5P_JPEG_ENCODE) {
1969 v4l2_ctrl_new_std(&ctx->ctrl_handler, &s5p_jpeg_ctrl_ops,
1970 V4L2_CID_JPEG_COMPRESSION_QUALITY,
1971 0, 3, 1, S5P_JPEG_COMPR_QUAL_WORST);
1973 v4l2_ctrl_new_std(&ctx->ctrl_handler, &s5p_jpeg_ctrl_ops,
1974 V4L2_CID_JPEG_RESTART_INTERVAL,
1975 0, 0xffff, 1, 0);
1976 if (ctx->jpeg->variant->version == SJPEG_S5P)
1977 mask = ~0x06; /* 422, 420 */
1980 ctrl = v4l2_ctrl_new_std_menu(&ctx->ctrl_handler, &s5p_jpeg_ctrl_ops,
1981 V4L2_CID_JPEG_CHROMA_SUBSAMPLING,
1982 V4L2_JPEG_CHROMA_SUBSAMPLING_GRAY, mask,
1983 V4L2_JPEG_CHROMA_SUBSAMPLING_422);
1985 if (ctx->ctrl_handler.error) {
1986 ret = ctx->ctrl_handler.error;
1987 goto error_free;
1990 if (ctx->mode == S5P_JPEG_DECODE)
1991 ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE |
1992 V4L2_CTRL_FLAG_READ_ONLY;
1994 ret = v4l2_ctrl_handler_setup(&ctx->ctrl_handler);
1995 if (ret < 0)
1996 goto error_free;
1998 return ret;
2000 error_free:
2001 v4l2_ctrl_handler_free(&ctx->ctrl_handler);
2002 return ret;
2005 static const struct v4l2_ioctl_ops s5p_jpeg_ioctl_ops = {
2006 .vidioc_querycap = s5p_jpeg_querycap,
2008 .vidioc_enum_fmt_vid_cap = s5p_jpeg_enum_fmt_vid_cap,
2009 .vidioc_enum_fmt_vid_out = s5p_jpeg_enum_fmt_vid_out,
2011 .vidioc_g_fmt_vid_cap = s5p_jpeg_g_fmt,
2012 .vidioc_g_fmt_vid_out = s5p_jpeg_g_fmt,
2014 .vidioc_try_fmt_vid_cap = s5p_jpeg_try_fmt_vid_cap,
2015 .vidioc_try_fmt_vid_out = s5p_jpeg_try_fmt_vid_out,
2017 .vidioc_s_fmt_vid_cap = s5p_jpeg_s_fmt_vid_cap,
2018 .vidioc_s_fmt_vid_out = s5p_jpeg_s_fmt_vid_out,
2020 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
2021 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
2022 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
2023 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
2025 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
2026 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
2028 .vidioc_g_selection = s5p_jpeg_g_selection,
2029 .vidioc_s_selection = s5p_jpeg_s_selection,
2031 .vidioc_subscribe_event = s5p_jpeg_subscribe_event,
2032 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
2036 * ============================================================================
2037 * mem2mem callbacks
2038 * ============================================================================
2041 static void s5p_jpeg_device_run(void *priv)
2043 struct s5p_jpeg_ctx *ctx = priv;
2044 struct s5p_jpeg *jpeg = ctx->jpeg;
2045 struct vb2_v4l2_buffer *src_buf, *dst_buf;
2046 unsigned long src_addr, dst_addr, flags;
2048 spin_lock_irqsave(&ctx->jpeg->slock, flags);
2050 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
2051 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
2052 src_addr = vb2_dma_contig_plane_dma_addr(&src_buf->vb2_buf, 0);
2053 dst_addr = vb2_dma_contig_plane_dma_addr(&dst_buf->vb2_buf, 0);
2055 s5p_jpeg_reset(jpeg->regs);
2056 s5p_jpeg_poweron(jpeg->regs);
2057 s5p_jpeg_proc_mode(jpeg->regs, ctx->mode);
2058 if (ctx->mode == S5P_JPEG_ENCODE) {
2059 if (ctx->out_q.fmt->fourcc == V4L2_PIX_FMT_RGB565)
2060 s5p_jpeg_input_raw_mode(jpeg->regs,
2061 S5P_JPEG_RAW_IN_565);
2062 else
2063 s5p_jpeg_input_raw_mode(jpeg->regs,
2064 S5P_JPEG_RAW_IN_422);
2065 s5p_jpeg_subsampling_mode(jpeg->regs, ctx->subsampling);
2066 s5p_jpeg_dri(jpeg->regs, ctx->restart_interval);
2067 s5p_jpeg_x(jpeg->regs, ctx->out_q.w);
2068 s5p_jpeg_y(jpeg->regs, ctx->out_q.h);
2069 s5p_jpeg_imgadr(jpeg->regs, src_addr);
2070 s5p_jpeg_jpgadr(jpeg->regs, dst_addr);
2072 /* ultimately comes from sizeimage from userspace */
2073 s5p_jpeg_enc_stream_int(jpeg->regs, ctx->cap_q.size);
2075 /* JPEG RGB to YCbCr conversion matrix */
2076 s5p_jpeg_coef(jpeg->regs, 1, 1, S5P_JPEG_COEF11);
2077 s5p_jpeg_coef(jpeg->regs, 1, 2, S5P_JPEG_COEF12);
2078 s5p_jpeg_coef(jpeg->regs, 1, 3, S5P_JPEG_COEF13);
2079 s5p_jpeg_coef(jpeg->regs, 2, 1, S5P_JPEG_COEF21);
2080 s5p_jpeg_coef(jpeg->regs, 2, 2, S5P_JPEG_COEF22);
2081 s5p_jpeg_coef(jpeg->regs, 2, 3, S5P_JPEG_COEF23);
2082 s5p_jpeg_coef(jpeg->regs, 3, 1, S5P_JPEG_COEF31);
2083 s5p_jpeg_coef(jpeg->regs, 3, 2, S5P_JPEG_COEF32);
2084 s5p_jpeg_coef(jpeg->regs, 3, 3, S5P_JPEG_COEF33);
2087 * JPEG IP allows storing 4 quantization tables
2088 * We fill table 0 for luma and table 1 for chroma
2090 s5p_jpeg_set_qtbl_lum(jpeg->regs, ctx->compr_quality);
2091 s5p_jpeg_set_qtbl_chr(jpeg->regs, ctx->compr_quality);
2092 /* use table 0 for Y */
2093 s5p_jpeg_qtbl(jpeg->regs, 1, 0);
2094 /* use table 1 for Cb and Cr*/
2095 s5p_jpeg_qtbl(jpeg->regs, 2, 1);
2096 s5p_jpeg_qtbl(jpeg->regs, 3, 1);
2098 /* Y, Cb, Cr use Huffman table 0 */
2099 s5p_jpeg_htbl_ac(jpeg->regs, 1);
2100 s5p_jpeg_htbl_dc(jpeg->regs, 1);
2101 s5p_jpeg_htbl_ac(jpeg->regs, 2);
2102 s5p_jpeg_htbl_dc(jpeg->regs, 2);
2103 s5p_jpeg_htbl_ac(jpeg->regs, 3);
2104 s5p_jpeg_htbl_dc(jpeg->regs, 3);
2105 } else { /* S5P_JPEG_DECODE */
2106 s5p_jpeg_rst_int_enable(jpeg->regs, true);
2107 s5p_jpeg_data_num_int_enable(jpeg->regs, true);
2108 s5p_jpeg_final_mcu_num_int_enable(jpeg->regs, true);
2109 if (ctx->cap_q.fmt->fourcc == V4L2_PIX_FMT_YUYV)
2110 s5p_jpeg_outform_raw(jpeg->regs, S5P_JPEG_RAW_OUT_422);
2111 else
2112 s5p_jpeg_outform_raw(jpeg->regs, S5P_JPEG_RAW_OUT_420);
2113 s5p_jpeg_jpgadr(jpeg->regs, src_addr);
2114 s5p_jpeg_imgadr(jpeg->regs, dst_addr);
2117 s5p_jpeg_start(jpeg->regs);
2119 spin_unlock_irqrestore(&ctx->jpeg->slock, flags);
2122 static void exynos4_jpeg_set_img_addr(struct s5p_jpeg_ctx *ctx)
2124 struct s5p_jpeg *jpeg = ctx->jpeg;
2125 struct s5p_jpeg_fmt *fmt;
2126 struct vb2_v4l2_buffer *vb;
2127 struct s5p_jpeg_addr jpeg_addr = {};
2128 u32 pix_size, padding_bytes = 0;
2130 jpeg_addr.cb = 0;
2131 jpeg_addr.cr = 0;
2133 pix_size = ctx->cap_q.w * ctx->cap_q.h;
2135 if (ctx->mode == S5P_JPEG_ENCODE) {
2136 vb = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
2137 fmt = ctx->out_q.fmt;
2138 if (ctx->out_q.w % 2 && fmt->h_align > 0)
2139 padding_bytes = ctx->out_q.h;
2140 } else {
2141 fmt = ctx->cap_q.fmt;
2142 vb = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
2145 jpeg_addr.y = vb2_dma_contig_plane_dma_addr(&vb->vb2_buf, 0);
2147 if (fmt->colplanes == 2) {
2148 jpeg_addr.cb = jpeg_addr.y + pix_size - padding_bytes;
2149 } else if (fmt->colplanes == 3) {
2150 jpeg_addr.cb = jpeg_addr.y + pix_size;
2151 if (fmt->fourcc == V4L2_PIX_FMT_YUV420)
2152 jpeg_addr.cr = jpeg_addr.cb + pix_size / 4;
2153 else
2154 jpeg_addr.cr = jpeg_addr.cb + pix_size / 2;
2157 exynos4_jpeg_set_frame_buf_address(jpeg->regs, &jpeg_addr);
2160 static void exynos4_jpeg_set_jpeg_addr(struct s5p_jpeg_ctx *ctx)
2162 struct s5p_jpeg *jpeg = ctx->jpeg;
2163 struct vb2_v4l2_buffer *vb;
2164 unsigned int jpeg_addr = 0;
2166 if (ctx->mode == S5P_JPEG_ENCODE)
2167 vb = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
2168 else
2169 vb = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
2171 jpeg_addr = vb2_dma_contig_plane_dma_addr(&vb->vb2_buf, 0);
2172 if (jpeg->variant->version == SJPEG_EXYNOS5433 &&
2173 ctx->mode == S5P_JPEG_DECODE)
2174 jpeg_addr += ctx->out_q.sos;
2175 exynos4_jpeg_set_stream_buf_address(jpeg->regs, jpeg_addr);
2178 static inline void exynos4_jpeg_set_img_fmt(void __iomem *base,
2179 unsigned int img_fmt)
2181 __exynos4_jpeg_set_img_fmt(base, img_fmt, SJPEG_EXYNOS4);
2184 static inline void exynos5433_jpeg_set_img_fmt(void __iomem *base,
2185 unsigned int img_fmt)
2187 __exynos4_jpeg_set_img_fmt(base, img_fmt, SJPEG_EXYNOS5433);
2190 static inline void exynos4_jpeg_set_enc_out_fmt(void __iomem *base,
2191 unsigned int out_fmt)
2193 __exynos4_jpeg_set_enc_out_fmt(base, out_fmt, SJPEG_EXYNOS4);
2196 static inline void exynos5433_jpeg_set_enc_out_fmt(void __iomem *base,
2197 unsigned int out_fmt)
2199 __exynos4_jpeg_set_enc_out_fmt(base, out_fmt, SJPEG_EXYNOS5433);
2202 static void exynos4_jpeg_device_run(void *priv)
2204 struct s5p_jpeg_ctx *ctx = priv;
2205 struct s5p_jpeg *jpeg = ctx->jpeg;
2206 unsigned int bitstream_size;
2207 unsigned long flags;
2209 spin_lock_irqsave(&jpeg->slock, flags);
2211 if (ctx->mode == S5P_JPEG_ENCODE) {
2212 exynos4_jpeg_sw_reset(jpeg->regs);
2213 exynos4_jpeg_set_interrupt(jpeg->regs, jpeg->variant->version);
2214 exynos4_jpeg_set_huf_table_enable(jpeg->regs, 1);
2216 exynos4_jpeg_set_huff_tbl(jpeg->regs);
2219 * JPEG IP allows storing 4 quantization tables
2220 * We fill table 0 for luma and table 1 for chroma
2222 exynos4_jpeg_set_qtbl_lum(jpeg->regs, ctx->compr_quality);
2223 exynos4_jpeg_set_qtbl_chr(jpeg->regs, ctx->compr_quality);
2225 exynos4_jpeg_set_encode_tbl_select(jpeg->regs,
2226 ctx->compr_quality);
2227 exynos4_jpeg_set_stream_size(jpeg->regs, ctx->cap_q.w,
2228 ctx->cap_q.h);
2230 if (ctx->jpeg->variant->version == SJPEG_EXYNOS4) {
2231 exynos4_jpeg_set_enc_out_fmt(jpeg->regs,
2232 ctx->subsampling);
2233 exynos4_jpeg_set_img_fmt(jpeg->regs,
2234 ctx->out_q.fmt->fourcc);
2235 } else {
2236 exynos5433_jpeg_set_enc_out_fmt(jpeg->regs,
2237 ctx->subsampling);
2238 exynos5433_jpeg_set_img_fmt(jpeg->regs,
2239 ctx->out_q.fmt->fourcc);
2241 exynos4_jpeg_set_img_addr(ctx);
2242 exynos4_jpeg_set_jpeg_addr(ctx);
2243 exynos4_jpeg_set_encode_hoff_cnt(jpeg->regs,
2244 ctx->out_q.fmt->fourcc);
2245 } else {
2246 exynos4_jpeg_sw_reset(jpeg->regs);
2247 exynos4_jpeg_set_interrupt(jpeg->regs,
2248 jpeg->variant->version);
2249 exynos4_jpeg_set_img_addr(ctx);
2250 exynos4_jpeg_set_jpeg_addr(ctx);
2252 if (jpeg->variant->version == SJPEG_EXYNOS5433) {
2253 exynos4_jpeg_parse_huff_tbl(ctx);
2254 exynos4_jpeg_parse_decode_h_tbl(ctx);
2256 exynos4_jpeg_parse_q_tbl(ctx);
2257 exynos4_jpeg_parse_decode_q_tbl(ctx);
2259 exynos4_jpeg_set_huf_table_enable(jpeg->regs, 1);
2261 exynos4_jpeg_set_stream_size(jpeg->regs, ctx->cap_q.w,
2262 ctx->cap_q.h);
2263 exynos5433_jpeg_set_enc_out_fmt(jpeg->regs,
2264 ctx->subsampling);
2265 exynos5433_jpeg_set_img_fmt(jpeg->regs,
2266 ctx->cap_q.fmt->fourcc);
2267 bitstream_size = DIV_ROUND_UP(ctx->out_q.size, 16);
2268 } else {
2269 exynos4_jpeg_set_img_fmt(jpeg->regs,
2270 ctx->cap_q.fmt->fourcc);
2271 bitstream_size = DIV_ROUND_UP(ctx->out_q.size, 32);
2274 exynos4_jpeg_set_dec_bitstream_size(jpeg->regs, bitstream_size);
2277 exynos4_jpeg_set_sys_int_enable(jpeg->regs, 1);
2278 exynos4_jpeg_set_enc_dec_mode(jpeg->regs, ctx->mode);
2280 spin_unlock_irqrestore(&jpeg->slock, flags);
2283 static void exynos3250_jpeg_set_img_addr(struct s5p_jpeg_ctx *ctx)
2285 struct s5p_jpeg *jpeg = ctx->jpeg;
2286 struct s5p_jpeg_fmt *fmt;
2287 struct vb2_v4l2_buffer *vb;
2288 struct s5p_jpeg_addr jpeg_addr = {};
2289 u32 pix_size;
2291 pix_size = ctx->cap_q.w * ctx->cap_q.h;
2293 if (ctx->mode == S5P_JPEG_ENCODE) {
2294 vb = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
2295 fmt = ctx->out_q.fmt;
2296 } else {
2297 vb = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
2298 fmt = ctx->cap_q.fmt;
2301 jpeg_addr.y = vb2_dma_contig_plane_dma_addr(&vb->vb2_buf, 0);
2303 if (fmt->colplanes == 2) {
2304 jpeg_addr.cb = jpeg_addr.y + pix_size;
2305 } else if (fmt->colplanes == 3) {
2306 jpeg_addr.cb = jpeg_addr.y + pix_size;
2307 if (fmt->fourcc == V4L2_PIX_FMT_YUV420)
2308 jpeg_addr.cr = jpeg_addr.cb + pix_size / 4;
2309 else
2310 jpeg_addr.cr = jpeg_addr.cb + pix_size / 2;
2313 exynos3250_jpeg_imgadr(jpeg->regs, &jpeg_addr);
2316 static void exynos3250_jpeg_set_jpeg_addr(struct s5p_jpeg_ctx *ctx)
2318 struct s5p_jpeg *jpeg = ctx->jpeg;
2319 struct vb2_v4l2_buffer *vb;
2320 unsigned int jpeg_addr = 0;
2322 if (ctx->mode == S5P_JPEG_ENCODE)
2323 vb = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
2324 else
2325 vb = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
2327 jpeg_addr = vb2_dma_contig_plane_dma_addr(&vb->vb2_buf, 0);
2328 exynos3250_jpeg_jpgadr(jpeg->regs, jpeg_addr);
2331 static void exynos3250_jpeg_device_run(void *priv)
2333 struct s5p_jpeg_ctx *ctx = priv;
2334 struct s5p_jpeg *jpeg = ctx->jpeg;
2335 unsigned long flags;
2337 spin_lock_irqsave(&ctx->jpeg->slock, flags);
2339 exynos3250_jpeg_reset(jpeg->regs);
2340 exynos3250_jpeg_set_dma_num(jpeg->regs);
2341 exynos3250_jpeg_poweron(jpeg->regs);
2342 exynos3250_jpeg_clk_set(jpeg->regs);
2343 exynos3250_jpeg_proc_mode(jpeg->regs, ctx->mode);
2345 if (ctx->mode == S5P_JPEG_ENCODE) {
2346 exynos3250_jpeg_input_raw_fmt(jpeg->regs,
2347 ctx->out_q.fmt->fourcc);
2348 exynos3250_jpeg_dri(jpeg->regs, ctx->restart_interval);
2351 * JPEG IP allows storing 4 quantization tables
2352 * We fill table 0 for luma and table 1 for chroma
2354 s5p_jpeg_set_qtbl_lum(jpeg->regs, ctx->compr_quality);
2355 s5p_jpeg_set_qtbl_chr(jpeg->regs, ctx->compr_quality);
2356 /* use table 0 for Y */
2357 exynos3250_jpeg_qtbl(jpeg->regs, 1, 0);
2358 /* use table 1 for Cb and Cr*/
2359 exynos3250_jpeg_qtbl(jpeg->regs, 2, 1);
2360 exynos3250_jpeg_qtbl(jpeg->regs, 3, 1);
2363 * Some SoCs require setting Huffman tables before each run
2365 if (jpeg->variant->htbl_reinit) {
2366 s5p_jpeg_set_hdctbl(jpeg->regs);
2367 s5p_jpeg_set_hdctblg(jpeg->regs);
2368 s5p_jpeg_set_hactbl(jpeg->regs);
2369 s5p_jpeg_set_hactblg(jpeg->regs);
2372 /* Y, Cb, Cr use Huffman table 0 */
2373 exynos3250_jpeg_htbl_ac(jpeg->regs, 1);
2374 exynos3250_jpeg_htbl_dc(jpeg->regs, 1);
2375 exynos3250_jpeg_htbl_ac(jpeg->regs, 2);
2376 exynos3250_jpeg_htbl_dc(jpeg->regs, 2);
2377 exynos3250_jpeg_htbl_ac(jpeg->regs, 3);
2378 exynos3250_jpeg_htbl_dc(jpeg->regs, 3);
2380 exynos3250_jpeg_set_x(jpeg->regs, ctx->crop_rect.width);
2381 exynos3250_jpeg_set_y(jpeg->regs, ctx->crop_rect.height);
2382 exynos3250_jpeg_stride(jpeg->regs, ctx->out_q.fmt->fourcc,
2383 ctx->out_q.w);
2384 exynos3250_jpeg_offset(jpeg->regs, ctx->crop_rect.left,
2385 ctx->crop_rect.top);
2386 exynos3250_jpeg_set_img_addr(ctx);
2387 exynos3250_jpeg_set_jpeg_addr(ctx);
2388 exynos3250_jpeg_subsampling_mode(jpeg->regs, ctx->subsampling);
2390 /* ultimately comes from sizeimage from userspace */
2391 exynos3250_jpeg_enc_stream_bound(jpeg->regs, ctx->cap_q.size);
2393 if (ctx->out_q.fmt->fourcc == V4L2_PIX_FMT_RGB565 ||
2394 ctx->out_q.fmt->fourcc == V4L2_PIX_FMT_RGB565X ||
2395 ctx->out_q.fmt->fourcc == V4L2_PIX_FMT_RGB32)
2396 exynos3250_jpeg_set_y16(jpeg->regs, true);
2397 } else {
2398 exynos3250_jpeg_set_img_addr(ctx);
2399 exynos3250_jpeg_set_jpeg_addr(ctx);
2400 exynos3250_jpeg_stride(jpeg->regs, ctx->cap_q.fmt->fourcc,
2401 ctx->cap_q.w);
2402 exynos3250_jpeg_offset(jpeg->regs, 0, 0);
2403 exynos3250_jpeg_dec_scaling_ratio(jpeg->regs,
2404 ctx->scale_factor);
2405 exynos3250_jpeg_dec_stream_size(jpeg->regs, ctx->out_q.size);
2406 exynos3250_jpeg_output_raw_fmt(jpeg->regs,
2407 ctx->cap_q.fmt->fourcc);
2410 exynos3250_jpeg_interrupts_enable(jpeg->regs);
2412 /* JPEG RGB to YCbCr conversion matrix */
2413 exynos3250_jpeg_coef(jpeg->regs, ctx->mode);
2415 exynos3250_jpeg_set_timer(jpeg->regs, EXYNOS3250_IRQ_TIMEOUT);
2416 jpeg->irq_status = 0;
2417 exynos3250_jpeg_start(jpeg->regs);
2419 spin_unlock_irqrestore(&ctx->jpeg->slock, flags);
2422 static int s5p_jpeg_job_ready(void *priv)
2424 struct s5p_jpeg_ctx *ctx = priv;
2426 if (ctx->mode == S5P_JPEG_DECODE) {
2428 * We have only one input buffer and one output buffer. If there
2429 * is a resolution change event, no need to continue decoding.
2431 if (ctx->state == JPEGCTX_RESOLUTION_CHANGE)
2432 return 0;
2434 return ctx->hdr_parsed;
2437 return 1;
2440 static struct v4l2_m2m_ops s5p_jpeg_m2m_ops = {
2441 .device_run = s5p_jpeg_device_run,
2442 .job_ready = s5p_jpeg_job_ready,
2445 static struct v4l2_m2m_ops exynos3250_jpeg_m2m_ops = {
2446 .device_run = exynos3250_jpeg_device_run,
2447 .job_ready = s5p_jpeg_job_ready,
2450 static struct v4l2_m2m_ops exynos4_jpeg_m2m_ops = {
2451 .device_run = exynos4_jpeg_device_run,
2452 .job_ready = s5p_jpeg_job_ready,
2456 * ============================================================================
2457 * Queue operations
2458 * ============================================================================
2461 static int s5p_jpeg_queue_setup(struct vb2_queue *vq,
2462 unsigned int *nbuffers, unsigned int *nplanes,
2463 unsigned int sizes[], struct device *alloc_devs[])
2465 struct s5p_jpeg_ctx *ctx = vb2_get_drv_priv(vq);
2466 struct s5p_jpeg_q_data *q_data = NULL;
2467 unsigned int size, count = *nbuffers;
2469 q_data = get_q_data(ctx, vq->type);
2470 BUG_ON(q_data == NULL);
2472 size = q_data->size;
2475 * header is parsed during decoding and parsed information stored
2476 * in the context so we do not allow another buffer to overwrite it
2478 if (ctx->mode == S5P_JPEG_DECODE)
2479 count = 1;
2481 *nbuffers = count;
2482 *nplanes = 1;
2483 sizes[0] = size;
2485 return 0;
2488 static int s5p_jpeg_buf_prepare(struct vb2_buffer *vb)
2490 struct s5p_jpeg_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
2491 struct s5p_jpeg_q_data *q_data = NULL;
2493 q_data = get_q_data(ctx, vb->vb2_queue->type);
2494 BUG_ON(q_data == NULL);
2496 if (vb2_plane_size(vb, 0) < q_data->size) {
2497 pr_err("%s data will not fit into plane (%lu < %lu)\n",
2498 __func__, vb2_plane_size(vb, 0),
2499 (long)q_data->size);
2500 return -EINVAL;
2503 vb2_set_plane_payload(vb, 0, q_data->size);
2505 return 0;
2508 static void s5p_jpeg_set_capture_queue_data(struct s5p_jpeg_ctx *ctx)
2510 struct s5p_jpeg_q_data *q_data = &ctx->cap_q;
2512 q_data->w = ctx->out_q.w;
2513 q_data->h = ctx->out_q.h;
2516 * This call to jpeg_bound_align_image() takes care of width and
2517 * height values alignment when user space calls the QBUF of
2518 * OUTPUT buffer after the S_FMT of CAPTURE buffer.
2519 * Please note that on Exynos4x12 SoCs, resigning from executing
2520 * S_FMT on capture buffer for each JPEG image can result in a
2521 * hardware hangup if subsampling is lower than the one of input
2522 * JPEG.
2524 jpeg_bound_align_image(ctx, &q_data->w, S5P_JPEG_MIN_WIDTH,
2525 S5P_JPEG_MAX_WIDTH, q_data->fmt->h_align,
2526 &q_data->h, S5P_JPEG_MIN_HEIGHT,
2527 S5P_JPEG_MAX_HEIGHT, q_data->fmt->v_align);
2529 q_data->size = q_data->w * q_data->h * q_data->fmt->depth >> 3;
2532 static void s5p_jpeg_buf_queue(struct vb2_buffer *vb)
2534 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
2535 struct s5p_jpeg_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
2537 if (ctx->mode == S5P_JPEG_DECODE &&
2538 vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
2539 static const struct v4l2_event ev_src_ch = {
2540 .type = V4L2_EVENT_SOURCE_CHANGE,
2541 .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION,
2543 struct vb2_queue *dst_vq;
2544 u32 ori_w;
2545 u32 ori_h;
2547 dst_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
2548 V4L2_BUF_TYPE_VIDEO_CAPTURE);
2549 ori_w = ctx->out_q.w;
2550 ori_h = ctx->out_q.h;
2552 ctx->hdr_parsed = s5p_jpeg_parse_hdr(&ctx->out_q,
2553 (unsigned long)vb2_plane_vaddr(vb, 0),
2554 min((unsigned long)ctx->out_q.size,
2555 vb2_get_plane_payload(vb, 0)), ctx);
2556 if (!ctx->hdr_parsed) {
2557 vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
2558 return;
2562 * If there is a resolution change event, only update capture
2563 * queue when it is not streaming. Otherwise, update it in
2564 * STREAMOFF. See s5p_jpeg_stop_streaming for detail.
2566 if (ctx->out_q.w != ori_w || ctx->out_q.h != ori_h) {
2567 v4l2_event_queue_fh(&ctx->fh, &ev_src_ch);
2568 if (vb2_is_streaming(dst_vq))
2569 ctx->state = JPEGCTX_RESOLUTION_CHANGE;
2570 else
2571 s5p_jpeg_set_capture_queue_data(ctx);
2575 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
2578 static int s5p_jpeg_start_streaming(struct vb2_queue *q, unsigned int count)
2580 struct s5p_jpeg_ctx *ctx = vb2_get_drv_priv(q);
2581 int ret;
2583 ret = pm_runtime_get_sync(ctx->jpeg->dev);
2585 return ret > 0 ? 0 : ret;
2588 static void s5p_jpeg_stop_streaming(struct vb2_queue *q)
2590 struct s5p_jpeg_ctx *ctx = vb2_get_drv_priv(q);
2593 * STREAMOFF is an acknowledgment for resolution change event.
2594 * Before STREAMOFF, we still have to return the old resolution and
2595 * subsampling. Update capture queue when the stream is off.
2597 if (ctx->state == JPEGCTX_RESOLUTION_CHANGE &&
2598 q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2599 s5p_jpeg_set_capture_queue_data(ctx);
2600 ctx->state = JPEGCTX_RUNNING;
2603 pm_runtime_put(ctx->jpeg->dev);
2606 static const struct vb2_ops s5p_jpeg_qops = {
2607 .queue_setup = s5p_jpeg_queue_setup,
2608 .buf_prepare = s5p_jpeg_buf_prepare,
2609 .buf_queue = s5p_jpeg_buf_queue,
2610 .wait_prepare = vb2_ops_wait_prepare,
2611 .wait_finish = vb2_ops_wait_finish,
2612 .start_streaming = s5p_jpeg_start_streaming,
2613 .stop_streaming = s5p_jpeg_stop_streaming,
2616 static int queue_init(void *priv, struct vb2_queue *src_vq,
2617 struct vb2_queue *dst_vq)
2619 struct s5p_jpeg_ctx *ctx = priv;
2620 int ret;
2622 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2623 src_vq->io_modes = VB2_MMAP | VB2_USERPTR;
2624 src_vq->drv_priv = ctx;
2625 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
2626 src_vq->ops = &s5p_jpeg_qops;
2627 src_vq->mem_ops = &vb2_dma_contig_memops;
2628 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
2629 src_vq->lock = &ctx->jpeg->lock;
2630 src_vq->dev = ctx->jpeg->dev;
2632 ret = vb2_queue_init(src_vq);
2633 if (ret)
2634 return ret;
2636 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2637 dst_vq->io_modes = VB2_MMAP | VB2_USERPTR;
2638 dst_vq->drv_priv = ctx;
2639 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
2640 dst_vq->ops = &s5p_jpeg_qops;
2641 dst_vq->mem_ops = &vb2_dma_contig_memops;
2642 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
2643 dst_vq->lock = &ctx->jpeg->lock;
2644 dst_vq->dev = ctx->jpeg->dev;
2646 return vb2_queue_init(dst_vq);
2650 * ============================================================================
2651 * ISR
2652 * ============================================================================
2655 static irqreturn_t s5p_jpeg_irq(int irq, void *dev_id)
2657 struct s5p_jpeg *jpeg = dev_id;
2658 struct s5p_jpeg_ctx *curr_ctx;
2659 struct vb2_v4l2_buffer *src_buf, *dst_buf;
2660 unsigned long payload_size = 0;
2661 enum vb2_buffer_state state = VB2_BUF_STATE_DONE;
2662 bool enc_jpeg_too_large = false;
2663 bool timer_elapsed = false;
2664 bool op_completed = false;
2666 spin_lock(&jpeg->slock);
2668 curr_ctx = v4l2_m2m_get_curr_priv(jpeg->m2m_dev);
2670 src_buf = v4l2_m2m_src_buf_remove(curr_ctx->fh.m2m_ctx);
2671 dst_buf = v4l2_m2m_dst_buf_remove(curr_ctx->fh.m2m_ctx);
2673 if (curr_ctx->mode == S5P_JPEG_ENCODE)
2674 enc_jpeg_too_large = s5p_jpeg_enc_stream_stat(jpeg->regs);
2675 timer_elapsed = s5p_jpeg_timer_stat(jpeg->regs);
2676 op_completed = s5p_jpeg_result_stat_ok(jpeg->regs);
2677 if (curr_ctx->mode == S5P_JPEG_DECODE)
2678 op_completed = op_completed &&
2679 s5p_jpeg_stream_stat_ok(jpeg->regs);
2681 if (enc_jpeg_too_large) {
2682 state = VB2_BUF_STATE_ERROR;
2683 s5p_jpeg_clear_enc_stream_stat(jpeg->regs);
2684 } else if (timer_elapsed) {
2685 state = VB2_BUF_STATE_ERROR;
2686 s5p_jpeg_clear_timer_stat(jpeg->regs);
2687 } else if (!op_completed) {
2688 state = VB2_BUF_STATE_ERROR;
2689 } else {
2690 payload_size = s5p_jpeg_compressed_size(jpeg->regs);
2693 dst_buf->timecode = src_buf->timecode;
2694 dst_buf->vb2_buf.timestamp = src_buf->vb2_buf.timestamp;
2695 dst_buf->flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
2696 dst_buf->flags |=
2697 src_buf->flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
2699 v4l2_m2m_buf_done(src_buf, state);
2700 if (curr_ctx->mode == S5P_JPEG_ENCODE)
2701 vb2_set_plane_payload(&dst_buf->vb2_buf, 0, payload_size);
2702 v4l2_m2m_buf_done(dst_buf, state);
2704 curr_ctx->subsampling = s5p_jpeg_get_subsampling_mode(jpeg->regs);
2705 spin_unlock(&jpeg->slock);
2707 s5p_jpeg_clear_int(jpeg->regs);
2709 v4l2_m2m_job_finish(jpeg->m2m_dev, curr_ctx->fh.m2m_ctx);
2710 return IRQ_HANDLED;
2713 static irqreturn_t exynos4_jpeg_irq(int irq, void *priv)
2715 unsigned int int_status;
2716 struct vb2_v4l2_buffer *src_vb, *dst_vb;
2717 struct s5p_jpeg *jpeg = priv;
2718 struct s5p_jpeg_ctx *curr_ctx;
2719 unsigned long payload_size = 0;
2721 spin_lock(&jpeg->slock);
2723 exynos4_jpeg_set_sys_int_enable(jpeg->regs, 0);
2725 curr_ctx = v4l2_m2m_get_curr_priv(jpeg->m2m_dev);
2727 src_vb = v4l2_m2m_src_buf_remove(curr_ctx->fh.m2m_ctx);
2728 dst_vb = v4l2_m2m_dst_buf_remove(curr_ctx->fh.m2m_ctx);
2730 int_status = exynos4_jpeg_get_int_status(jpeg->regs);
2732 if (int_status) {
2733 switch (int_status & 0x1f) {
2734 case 0x1:
2735 jpeg->irq_ret = ERR_PROT;
2736 break;
2737 case 0x2:
2738 jpeg->irq_ret = OK_ENC_OR_DEC;
2739 break;
2740 case 0x4:
2741 jpeg->irq_ret = ERR_DEC_INVALID_FORMAT;
2742 break;
2743 case 0x8:
2744 jpeg->irq_ret = ERR_MULTI_SCAN;
2745 break;
2746 case 0x10:
2747 jpeg->irq_ret = ERR_FRAME;
2748 break;
2749 default:
2750 jpeg->irq_ret = ERR_UNKNOWN;
2751 break;
2753 } else {
2754 jpeg->irq_ret = ERR_UNKNOWN;
2757 if (jpeg->irq_ret == OK_ENC_OR_DEC) {
2758 if (curr_ctx->mode == S5P_JPEG_ENCODE) {
2759 payload_size = exynos4_jpeg_get_stream_size(jpeg->regs);
2760 vb2_set_plane_payload(&dst_vb->vb2_buf,
2761 0, payload_size);
2763 v4l2_m2m_buf_done(src_vb, VB2_BUF_STATE_DONE);
2764 v4l2_m2m_buf_done(dst_vb, VB2_BUF_STATE_DONE);
2765 } else {
2766 v4l2_m2m_buf_done(src_vb, VB2_BUF_STATE_ERROR);
2767 v4l2_m2m_buf_done(dst_vb, VB2_BUF_STATE_ERROR);
2770 if (jpeg->variant->version == SJPEG_EXYNOS4)
2771 curr_ctx->subsampling = exynos4_jpeg_get_frame_fmt(jpeg->regs);
2773 exynos4_jpeg_set_enc_dec_mode(jpeg->regs, S5P_JPEG_DISABLE);
2775 spin_unlock(&jpeg->slock);
2777 v4l2_m2m_job_finish(jpeg->m2m_dev, curr_ctx->fh.m2m_ctx);
2778 return IRQ_HANDLED;
2781 static irqreturn_t exynos3250_jpeg_irq(int irq, void *dev_id)
2783 struct s5p_jpeg *jpeg = dev_id;
2784 struct s5p_jpeg_ctx *curr_ctx;
2785 struct vb2_v4l2_buffer *src_buf, *dst_buf;
2786 unsigned long payload_size = 0;
2787 enum vb2_buffer_state state = VB2_BUF_STATE_DONE;
2788 bool interrupt_timeout = false;
2789 bool stream_error = false;
2790 u32 irq_status;
2792 spin_lock(&jpeg->slock);
2794 irq_status = exynos3250_jpeg_get_timer_status(jpeg->regs);
2795 if (irq_status & EXYNOS3250_TIMER_INT_STAT) {
2796 exynos3250_jpeg_clear_timer_status(jpeg->regs);
2797 interrupt_timeout = true;
2798 dev_err(jpeg->dev, "Interrupt timeout occurred.\n");
2801 irq_status = exynos3250_jpeg_get_int_status(jpeg->regs);
2802 exynos3250_jpeg_clear_int_status(jpeg->regs, irq_status);
2804 jpeg->irq_status |= irq_status;
2806 if (jpeg->variant->version == SJPEG_EXYNOS5420 &&
2807 irq_status & EXYNOS3250_STREAM_STAT) {
2808 stream_error = true;
2809 dev_err(jpeg->dev, "Syntax error or unrecoverable error occurred.\n");
2812 curr_ctx = v4l2_m2m_get_curr_priv(jpeg->m2m_dev);
2814 if (!curr_ctx)
2815 goto exit_unlock;
2817 if ((irq_status & EXYNOS3250_HEADER_STAT) &&
2818 (curr_ctx->mode == S5P_JPEG_DECODE)) {
2819 exynos3250_jpeg_rstart(jpeg->regs);
2820 goto exit_unlock;
2823 if (jpeg->irq_status & (EXYNOS3250_JPEG_DONE |
2824 EXYNOS3250_WDMA_DONE |
2825 EXYNOS3250_RDMA_DONE |
2826 EXYNOS3250_RESULT_STAT))
2827 payload_size = exynos3250_jpeg_compressed_size(jpeg->regs);
2828 else if (interrupt_timeout || stream_error)
2829 state = VB2_BUF_STATE_ERROR;
2830 else
2831 goto exit_unlock;
2833 src_buf = v4l2_m2m_src_buf_remove(curr_ctx->fh.m2m_ctx);
2834 dst_buf = v4l2_m2m_dst_buf_remove(curr_ctx->fh.m2m_ctx);
2836 dst_buf->timecode = src_buf->timecode;
2837 dst_buf->vb2_buf.timestamp = src_buf->vb2_buf.timestamp;
2839 v4l2_m2m_buf_done(src_buf, state);
2840 if (curr_ctx->mode == S5P_JPEG_ENCODE)
2841 vb2_set_plane_payload(&dst_buf->vb2_buf, 0, payload_size);
2842 v4l2_m2m_buf_done(dst_buf, state);
2844 curr_ctx->subsampling =
2845 exynos3250_jpeg_get_subsampling_mode(jpeg->regs);
2847 spin_unlock(&jpeg->slock);
2849 v4l2_m2m_job_finish(jpeg->m2m_dev, curr_ctx->fh.m2m_ctx);
2850 return IRQ_HANDLED;
2852 exit_unlock:
2853 spin_unlock(&jpeg->slock);
2854 return IRQ_HANDLED;
2857 static void *jpeg_get_drv_data(struct device *dev);
2860 * ============================================================================
2861 * Driver basic infrastructure
2862 * ============================================================================
2865 static int s5p_jpeg_probe(struct platform_device *pdev)
2867 struct s5p_jpeg *jpeg;
2868 struct resource *res;
2869 int i, ret;
2871 /* JPEG IP abstraction struct */
2872 jpeg = devm_kzalloc(&pdev->dev, sizeof(struct s5p_jpeg), GFP_KERNEL);
2873 if (!jpeg)
2874 return -ENOMEM;
2876 jpeg->variant = jpeg_get_drv_data(&pdev->dev);
2878 mutex_init(&jpeg->lock);
2879 spin_lock_init(&jpeg->slock);
2880 jpeg->dev = &pdev->dev;
2882 /* memory-mapped registers */
2883 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2885 jpeg->regs = devm_ioremap_resource(&pdev->dev, res);
2886 if (IS_ERR(jpeg->regs))
2887 return PTR_ERR(jpeg->regs);
2889 /* interrupt service routine registration */
2890 jpeg->irq = ret = platform_get_irq(pdev, 0);
2891 if (ret < 0) {
2892 dev_err(&pdev->dev, "cannot find IRQ\n");
2893 return ret;
2896 ret = devm_request_irq(&pdev->dev, jpeg->irq, jpeg->variant->jpeg_irq,
2897 0, dev_name(&pdev->dev), jpeg);
2898 if (ret) {
2899 dev_err(&pdev->dev, "cannot claim IRQ %d\n", jpeg->irq);
2900 return ret;
2903 /* clocks */
2904 for (i = 0; i < jpeg->variant->num_clocks; i++) {
2905 jpeg->clocks[i] = devm_clk_get(&pdev->dev,
2906 jpeg->variant->clk_names[i]);
2907 if (IS_ERR(jpeg->clocks[i])) {
2908 dev_err(&pdev->dev, "failed to get clock: %s\n",
2909 jpeg->variant->clk_names[i]);
2910 return PTR_ERR(jpeg->clocks[i]);
2914 /* v4l2 device */
2915 ret = v4l2_device_register(&pdev->dev, &jpeg->v4l2_dev);
2916 if (ret) {
2917 dev_err(&pdev->dev, "Failed to register v4l2 device\n");
2918 return ret;
2921 /* mem2mem device */
2922 jpeg->m2m_dev = v4l2_m2m_init(jpeg->variant->m2m_ops);
2923 if (IS_ERR(jpeg->m2m_dev)) {
2924 v4l2_err(&jpeg->v4l2_dev, "Failed to init mem2mem device\n");
2925 ret = PTR_ERR(jpeg->m2m_dev);
2926 goto device_register_rollback;
2929 vb2_dma_contig_set_max_seg_size(&pdev->dev, DMA_BIT_MASK(32));
2931 /* JPEG encoder /dev/videoX node */
2932 jpeg->vfd_encoder = video_device_alloc();
2933 if (!jpeg->vfd_encoder) {
2934 v4l2_err(&jpeg->v4l2_dev, "Failed to allocate video device\n");
2935 ret = -ENOMEM;
2936 goto m2m_init_rollback;
2938 snprintf(jpeg->vfd_encoder->name, sizeof(jpeg->vfd_encoder->name),
2939 "%s-enc", S5P_JPEG_M2M_NAME);
2940 jpeg->vfd_encoder->fops = &s5p_jpeg_fops;
2941 jpeg->vfd_encoder->ioctl_ops = &s5p_jpeg_ioctl_ops;
2942 jpeg->vfd_encoder->minor = -1;
2943 jpeg->vfd_encoder->release = video_device_release;
2944 jpeg->vfd_encoder->lock = &jpeg->lock;
2945 jpeg->vfd_encoder->v4l2_dev = &jpeg->v4l2_dev;
2946 jpeg->vfd_encoder->vfl_dir = VFL_DIR_M2M;
2947 jpeg->vfd_encoder->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_M2M;
2949 ret = video_register_device(jpeg->vfd_encoder, VFL_TYPE_GRABBER, -1);
2950 if (ret) {
2951 v4l2_err(&jpeg->v4l2_dev, "Failed to register video device\n");
2952 video_device_release(jpeg->vfd_encoder);
2953 goto m2m_init_rollback;
2956 video_set_drvdata(jpeg->vfd_encoder, jpeg);
2957 v4l2_info(&jpeg->v4l2_dev,
2958 "encoder device registered as /dev/video%d\n",
2959 jpeg->vfd_encoder->num);
2961 /* JPEG decoder /dev/videoX node */
2962 jpeg->vfd_decoder = video_device_alloc();
2963 if (!jpeg->vfd_decoder) {
2964 v4l2_err(&jpeg->v4l2_dev, "Failed to allocate video device\n");
2965 ret = -ENOMEM;
2966 goto enc_vdev_register_rollback;
2968 snprintf(jpeg->vfd_decoder->name, sizeof(jpeg->vfd_decoder->name),
2969 "%s-dec", S5P_JPEG_M2M_NAME);
2970 jpeg->vfd_decoder->fops = &s5p_jpeg_fops;
2971 jpeg->vfd_decoder->ioctl_ops = &s5p_jpeg_ioctl_ops;
2972 jpeg->vfd_decoder->minor = -1;
2973 jpeg->vfd_decoder->release = video_device_release;
2974 jpeg->vfd_decoder->lock = &jpeg->lock;
2975 jpeg->vfd_decoder->v4l2_dev = &jpeg->v4l2_dev;
2976 jpeg->vfd_decoder->vfl_dir = VFL_DIR_M2M;
2977 jpeg->vfd_decoder->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_M2M;
2979 ret = video_register_device(jpeg->vfd_decoder, VFL_TYPE_GRABBER, -1);
2980 if (ret) {
2981 v4l2_err(&jpeg->v4l2_dev, "Failed to register video device\n");
2982 video_device_release(jpeg->vfd_decoder);
2983 goto enc_vdev_register_rollback;
2986 video_set_drvdata(jpeg->vfd_decoder, jpeg);
2987 v4l2_info(&jpeg->v4l2_dev,
2988 "decoder device registered as /dev/video%d\n",
2989 jpeg->vfd_decoder->num);
2991 /* final statements & power management */
2992 platform_set_drvdata(pdev, jpeg);
2994 pm_runtime_enable(&pdev->dev);
2996 v4l2_info(&jpeg->v4l2_dev, "Samsung S5P JPEG codec\n");
2998 return 0;
3000 enc_vdev_register_rollback:
3001 video_unregister_device(jpeg->vfd_encoder);
3003 m2m_init_rollback:
3004 v4l2_m2m_release(jpeg->m2m_dev);
3006 device_register_rollback:
3007 v4l2_device_unregister(&jpeg->v4l2_dev);
3009 return ret;
3012 static int s5p_jpeg_remove(struct platform_device *pdev)
3014 struct s5p_jpeg *jpeg = platform_get_drvdata(pdev);
3015 int i;
3017 pm_runtime_disable(jpeg->dev);
3019 video_unregister_device(jpeg->vfd_decoder);
3020 video_unregister_device(jpeg->vfd_encoder);
3021 vb2_dma_contig_clear_max_seg_size(&pdev->dev);
3022 v4l2_m2m_release(jpeg->m2m_dev);
3023 v4l2_device_unregister(&jpeg->v4l2_dev);
3025 if (!pm_runtime_status_suspended(&pdev->dev)) {
3026 for (i = jpeg->variant->num_clocks - 1; i >= 0; i--)
3027 clk_disable_unprepare(jpeg->clocks[i]);
3030 return 0;
3033 #ifdef CONFIG_PM
3034 static int s5p_jpeg_runtime_suspend(struct device *dev)
3036 struct s5p_jpeg *jpeg = dev_get_drvdata(dev);
3037 int i;
3039 for (i = jpeg->variant->num_clocks - 1; i >= 0; i--)
3040 clk_disable_unprepare(jpeg->clocks[i]);
3042 return 0;
3045 static int s5p_jpeg_runtime_resume(struct device *dev)
3047 struct s5p_jpeg *jpeg = dev_get_drvdata(dev);
3048 unsigned long flags;
3049 int i, ret;
3051 for (i = 0; i < jpeg->variant->num_clocks; i++) {
3052 ret = clk_prepare_enable(jpeg->clocks[i]);
3053 if (ret) {
3054 while (--i >= 0)
3055 clk_disable_unprepare(jpeg->clocks[i]);
3056 return ret;
3060 spin_lock_irqsave(&jpeg->slock, flags);
3063 * JPEG IP allows storing two Huffman tables for each component.
3064 * We fill table 0 for each component and do this here only
3065 * for S5PC210 and Exynos3250 SoCs. Exynos4x12 and Exynos542x SoC
3066 * require programming their Huffman tables each time the encoding
3067 * process is initialized, and thus it is accomplished in the
3068 * device_run callback of m2m_ops.
3070 if (!jpeg->variant->htbl_reinit) {
3071 s5p_jpeg_set_hdctbl(jpeg->regs);
3072 s5p_jpeg_set_hdctblg(jpeg->regs);
3073 s5p_jpeg_set_hactbl(jpeg->regs);
3074 s5p_jpeg_set_hactblg(jpeg->regs);
3077 spin_unlock_irqrestore(&jpeg->slock, flags);
3079 return 0;
3081 #endif /* CONFIG_PM */
3083 static const struct dev_pm_ops s5p_jpeg_pm_ops = {
3084 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
3085 pm_runtime_force_resume)
3086 SET_RUNTIME_PM_OPS(s5p_jpeg_runtime_suspend, s5p_jpeg_runtime_resume,
3087 NULL)
3090 static struct s5p_jpeg_variant s5p_jpeg_drvdata = {
3091 .version = SJPEG_S5P,
3092 .jpeg_irq = s5p_jpeg_irq,
3093 .m2m_ops = &s5p_jpeg_m2m_ops,
3094 .fmt_ver_flag = SJPEG_FMT_FLAG_S5P,
3095 .clk_names = {"jpeg"},
3096 .num_clocks = 1,
3099 static struct s5p_jpeg_variant exynos3250_jpeg_drvdata = {
3100 .version = SJPEG_EXYNOS3250,
3101 .jpeg_irq = exynos3250_jpeg_irq,
3102 .m2m_ops = &exynos3250_jpeg_m2m_ops,
3103 .fmt_ver_flag = SJPEG_FMT_FLAG_EXYNOS3250,
3104 .hw3250_compat = 1,
3105 .clk_names = {"jpeg", "sclk"},
3106 .num_clocks = 2,
3109 static struct s5p_jpeg_variant exynos4_jpeg_drvdata = {
3110 .version = SJPEG_EXYNOS4,
3111 .jpeg_irq = exynos4_jpeg_irq,
3112 .m2m_ops = &exynos4_jpeg_m2m_ops,
3113 .fmt_ver_flag = SJPEG_FMT_FLAG_EXYNOS4,
3114 .htbl_reinit = 1,
3115 .clk_names = {"jpeg"},
3116 .num_clocks = 1,
3117 .hw_ex4_compat = 1,
3120 static struct s5p_jpeg_variant exynos5420_jpeg_drvdata = {
3121 .version = SJPEG_EXYNOS5420,
3122 .jpeg_irq = exynos3250_jpeg_irq, /* intentionally 3250 */
3123 .m2m_ops = &exynos3250_jpeg_m2m_ops, /* intentionally 3250 */
3124 .fmt_ver_flag = SJPEG_FMT_FLAG_EXYNOS3250, /* intentionally 3250 */
3125 .hw3250_compat = 1,
3126 .htbl_reinit = 1,
3127 .clk_names = {"jpeg"},
3128 .num_clocks = 1,
3131 static struct s5p_jpeg_variant exynos5433_jpeg_drvdata = {
3132 .version = SJPEG_EXYNOS5433,
3133 .jpeg_irq = exynos4_jpeg_irq,
3134 .m2m_ops = &exynos4_jpeg_m2m_ops,
3135 .fmt_ver_flag = SJPEG_FMT_FLAG_EXYNOS4,
3136 .htbl_reinit = 1,
3137 .clk_names = {"pclk", "aclk", "aclk_xiu", "sclk"},
3138 .num_clocks = 4,
3139 .hw_ex4_compat = 1,
3142 static const struct of_device_id samsung_jpeg_match[] = {
3144 .compatible = "samsung,s5pv210-jpeg",
3145 .data = &s5p_jpeg_drvdata,
3146 }, {
3147 .compatible = "samsung,exynos3250-jpeg",
3148 .data = &exynos3250_jpeg_drvdata,
3149 }, {
3150 .compatible = "samsung,exynos4210-jpeg",
3151 .data = &exynos4_jpeg_drvdata,
3152 }, {
3153 .compatible = "samsung,exynos4212-jpeg",
3154 .data = &exynos4_jpeg_drvdata,
3155 }, {
3156 .compatible = "samsung,exynos5420-jpeg",
3157 .data = &exynos5420_jpeg_drvdata,
3158 }, {
3159 .compatible = "samsung,exynos5433-jpeg",
3160 .data = &exynos5433_jpeg_drvdata,
3165 MODULE_DEVICE_TABLE(of, samsung_jpeg_match);
3167 static void *jpeg_get_drv_data(struct device *dev)
3169 struct s5p_jpeg_variant *driver_data = NULL;
3170 const struct of_device_id *match;
3172 if (!IS_ENABLED(CONFIG_OF) || !dev->of_node)
3173 return &s5p_jpeg_drvdata;
3175 match = of_match_node(samsung_jpeg_match, dev->of_node);
3177 if (match)
3178 driver_data = (struct s5p_jpeg_variant *)match->data;
3180 return driver_data;
3183 static struct platform_driver s5p_jpeg_driver = {
3184 .probe = s5p_jpeg_probe,
3185 .remove = s5p_jpeg_remove,
3186 .driver = {
3187 .of_match_table = of_match_ptr(samsung_jpeg_match),
3188 .name = S5P_JPEG_M2M_NAME,
3189 .pm = &s5p_jpeg_pm_ops,
3193 module_platform_driver(s5p_jpeg_driver);
3195 MODULE_AUTHOR("Andrzej Pietrasiewicz <andrzejtp2010@gmail.com>");
3196 MODULE_AUTHOR("Jacek Anaszewski <j.anaszewski@samsung.com>");
3197 MODULE_DESCRIPTION("Samsung JPEG codec driver");
3198 MODULE_LICENSE("GPL");