Merge branch 'next' of git://git.kernel.org/pub/scm/linux/kernel/git/benh/powerpc
[firefly-linux-kernel-4.4.55.git] / drivers / media / platform / s5p-jpeg / jpeg-core.c
1 /* linux/drivers/media/platform/s5p-jpeg/jpeg-core.c
2  *
3  * Copyright (c) 2011-2013 Samsung Electronics Co., Ltd.
4  *              http://www.samsung.com
5  *
6  * Author: Andrzej Pietrasiewicz <andrzej.p@samsung.com>
7  * Author: Jacek Anaszewski <j.anaszewski@samsung.com>
8  *
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.
12  */
13
14 #include <linux/clk.h>
15 #include <linux/err.h>
16 #include <linux/gfp.h>
17 #include <linux/interrupt.h>
18 #include <linux/io.h>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/of.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>
31
32 #include "jpeg-core.h"
33 #include "jpeg-hw-s5p.h"
34 #include "jpeg-hw-exynos4.h"
35 #include "jpeg-regs.h"
36
37 static struct s5p_jpeg_fmt sjpeg_formats[] = {
38         {
39                 .name           = "JPEG JFIF",
40                 .fourcc         = V4L2_PIX_FMT_JPEG,
41                 .flags          = SJPEG_FMT_FLAG_ENC_CAPTURE |
42                                   SJPEG_FMT_FLAG_DEC_OUTPUT |
43                                   SJPEG_FMT_FLAG_S5P |
44                                   SJPEG_FMT_FLAG_EXYNOS4,
45         },
46         {
47                 .name           = "YUV 4:2:2 packed, YCbYCr",
48                 .fourcc         = V4L2_PIX_FMT_YUYV,
49                 .depth          = 16,
50                 .colplanes      = 1,
51                 .h_align        = 4,
52                 .v_align        = 3,
53                 .flags          = SJPEG_FMT_FLAG_ENC_OUTPUT |
54                                   SJPEG_FMT_FLAG_DEC_CAPTURE |
55                                   SJPEG_FMT_FLAG_S5P |
56                                   SJPEG_FMT_NON_RGB,
57                 .subsampling    = V4L2_JPEG_CHROMA_SUBSAMPLING_422,
58         },
59         {
60                 .name           = "YUV 4:2:2 packed, YCbYCr",
61                 .fourcc         = V4L2_PIX_FMT_YUYV,
62                 .depth          = 16,
63                 .colplanes      = 1,
64                 .h_align        = 1,
65                 .v_align        = 0,
66                 .flags          = SJPEG_FMT_FLAG_ENC_OUTPUT |
67                                   SJPEG_FMT_FLAG_DEC_CAPTURE |
68                                   SJPEG_FMT_FLAG_EXYNOS4 |
69                                   SJPEG_FMT_NON_RGB,
70                 .subsampling    = V4L2_JPEG_CHROMA_SUBSAMPLING_422,
71         },
72         {
73                 .name           = "YUV 4:2:2 packed, YCrYCb",
74                 .fourcc         = V4L2_PIX_FMT_YVYU,
75                 .depth          = 16,
76                 .colplanes      = 1,
77                 .h_align        = 1,
78                 .v_align        = 0,
79                 .flags          = SJPEG_FMT_FLAG_ENC_OUTPUT |
80                                   SJPEG_FMT_FLAG_DEC_CAPTURE |
81                                   SJPEG_FMT_FLAG_EXYNOS4 |
82                                   SJPEG_FMT_NON_RGB,
83                 .subsampling    = V4L2_JPEG_CHROMA_SUBSAMPLING_422,
84         },
85         {
86                 .name           = "RGB565",
87                 .fourcc         = V4L2_PIX_FMT_RGB565,
88                 .depth          = 16,
89                 .colplanes      = 1,
90                 .h_align        = 0,
91                 .v_align        = 0,
92                 .flags          = SJPEG_FMT_FLAG_ENC_OUTPUT |
93                                   SJPEG_FMT_FLAG_DEC_CAPTURE |
94                                   SJPEG_FMT_FLAG_EXYNOS4 |
95                                   SJPEG_FMT_RGB,
96                 .subsampling    = V4L2_JPEG_CHROMA_SUBSAMPLING_444,
97         },
98         {
99                 .name           = "RGB565",
100                 .fourcc         = V4L2_PIX_FMT_RGB565,
101                 .depth          = 16,
102                 .colplanes      = 1,
103                 .h_align        = 0,
104                 .v_align        = 0,
105                 .flags          = SJPEG_FMT_FLAG_ENC_OUTPUT |
106                                   SJPEG_FMT_FLAG_S5P |
107                                   SJPEG_FMT_RGB,
108                 .subsampling    = V4L2_JPEG_CHROMA_SUBSAMPLING_444,
109         },
110         {
111                 .name           = "ARGB8888, 32 bpp",
112                 .fourcc         = V4L2_PIX_FMT_RGB32,
113                 .depth          = 32,
114                 .colplanes      = 1,
115                 .h_align        = 0,
116                 .v_align        = 0,
117                 .flags          = SJPEG_FMT_FLAG_ENC_OUTPUT |
118                                   SJPEG_FMT_FLAG_DEC_CAPTURE |
119                                   SJPEG_FMT_FLAG_EXYNOS4 |
120                                   SJPEG_FMT_RGB,
121                 .subsampling    = V4L2_JPEG_CHROMA_SUBSAMPLING_444,
122         },
123         {
124                 .name           = "YUV 4:4:4 planar, Y/CbCr",
125                 .fourcc         = V4L2_PIX_FMT_NV24,
126                 .depth          = 24,
127                 .colplanes      = 2,
128                 .h_align        = 0,
129                 .v_align        = 0,
130                 .flags          = SJPEG_FMT_FLAG_ENC_OUTPUT |
131                                   SJPEG_FMT_FLAG_DEC_CAPTURE |
132                                   SJPEG_FMT_FLAG_EXYNOS4 |
133                                   SJPEG_FMT_NON_RGB,
134                 .subsampling    = V4L2_JPEG_CHROMA_SUBSAMPLING_444,
135         },
136         {
137                 .name           = "YUV 4:4:4 planar, Y/CrCb",
138                 .fourcc         = V4L2_PIX_FMT_NV42,
139                 .depth          = 24,
140                 .colplanes      = 2,
141                 .h_align        = 0,
142                 .v_align        = 0,
143                 .flags          = SJPEG_FMT_FLAG_ENC_OUTPUT |
144                                   SJPEG_FMT_FLAG_DEC_CAPTURE |
145                                   SJPEG_FMT_FLAG_EXYNOS4 |
146                                   SJPEG_FMT_NON_RGB,
147                 .subsampling    = V4L2_JPEG_CHROMA_SUBSAMPLING_444,
148         },
149         {
150                 .name           = "YUV 4:2:2 planar, Y/CrCb",
151                 .fourcc         = V4L2_PIX_FMT_NV61,
152                 .depth          = 16,
153                 .colplanes      = 2,
154                 .h_align        = 1,
155                 .v_align        = 0,
156                 .flags          = SJPEG_FMT_FLAG_ENC_OUTPUT |
157                                   SJPEG_FMT_FLAG_DEC_CAPTURE |
158                                   SJPEG_FMT_FLAG_EXYNOS4 |
159                                   SJPEG_FMT_NON_RGB,
160                 .subsampling    = V4L2_JPEG_CHROMA_SUBSAMPLING_422,
161         },
162         {
163                 .name           = "YUV 4:2:2 planar, Y/CbCr",
164                 .fourcc         = V4L2_PIX_FMT_NV16,
165                 .depth          = 16,
166                 .colplanes      = 2,
167                 .h_align        = 1,
168                 .v_align        = 0,
169                 .flags          = SJPEG_FMT_FLAG_ENC_OUTPUT |
170                                   SJPEG_FMT_FLAG_DEC_CAPTURE |
171                                   SJPEG_FMT_FLAG_EXYNOS4 |
172                                   SJPEG_FMT_NON_RGB,
173                 .subsampling    = V4L2_JPEG_CHROMA_SUBSAMPLING_422,
174         },
175         {
176                 .name           = "YUV 4:2:0 planar, Y/CbCr",
177                 .fourcc         = V4L2_PIX_FMT_NV12,
178                 .depth          = 12,
179                 .colplanes      = 2,
180                 .h_align        = 1,
181                 .v_align        = 1,
182                 .flags          = SJPEG_FMT_FLAG_ENC_OUTPUT |
183                                   SJPEG_FMT_FLAG_DEC_CAPTURE |
184                                   SJPEG_FMT_FLAG_EXYNOS4 |
185                                   SJPEG_FMT_NON_RGB,
186                 .subsampling    = V4L2_JPEG_CHROMA_SUBSAMPLING_420,
187         },
188         {
189                 .name           = "YUV 4:2:0 planar, Y/CbCr",
190                 .fourcc         = V4L2_PIX_FMT_NV12,
191                 .depth          = 12,
192                 .colplanes      = 2,
193                 .h_align        = 4,
194                 .v_align        = 4,
195                 .flags          = SJPEG_FMT_FLAG_DEC_CAPTURE |
196                                   SJPEG_FMT_FLAG_S5P |
197                                   SJPEG_FMT_NON_RGB,
198                 .subsampling    = V4L2_JPEG_CHROMA_SUBSAMPLING_420,
199         },
200         {
201                 .name           = "YUV 4:2:0 planar, Y/CrCb",
202                 .fourcc         = V4L2_PIX_FMT_NV21,
203                 .depth          = 12,
204                 .colplanes      = 2,
205                 .h_align        = 1,
206                 .v_align        = 1,
207                 .flags          = SJPEG_FMT_FLAG_ENC_OUTPUT |
208                                   SJPEG_FMT_FLAG_DEC_CAPTURE |
209                                   SJPEG_FMT_FLAG_EXYNOS4 |
210                                   SJPEG_FMT_NON_RGB,
211                 .subsampling    = V4L2_JPEG_CHROMA_SUBSAMPLING_420,
212         },
213         {
214                 .name           = "YUV 4:2:0 contiguous 3-planar, Y/Cb/Cr",
215                 .fourcc         = V4L2_PIX_FMT_YUV420,
216                 .depth          = 12,
217                 .colplanes      = 3,
218                 .h_align        = 1,
219                 .v_align        = 1,
220                 .flags          = SJPEG_FMT_FLAG_ENC_OUTPUT |
221                                   SJPEG_FMT_FLAG_DEC_CAPTURE |
222                                   SJPEG_FMT_FLAG_EXYNOS4 |
223                                   SJPEG_FMT_NON_RGB,
224                 .subsampling    = V4L2_JPEG_CHROMA_SUBSAMPLING_420,
225         },
226         {
227                 .name           = "Gray",
228                 .fourcc         = V4L2_PIX_FMT_GREY,
229                 .depth          = 8,
230                 .colplanes      = 1,
231                 .flags          = SJPEG_FMT_FLAG_ENC_OUTPUT |
232                                   SJPEG_FMT_FLAG_DEC_CAPTURE |
233                                   SJPEG_FMT_FLAG_EXYNOS4 |
234                                   SJPEG_FMT_NON_RGB,
235                 .subsampling    = V4L2_JPEG_CHROMA_SUBSAMPLING_GRAY,
236         },
237 };
238 #define SJPEG_NUM_FORMATS ARRAY_SIZE(sjpeg_formats)
239
240 static const unsigned char qtbl_luminance[4][64] = {
241         {/*level 0 - high compression quality */
242                 20, 16, 25, 39, 50, 46, 62, 68,
243                 16, 18, 23, 38, 38, 53, 65, 68,
244                 25, 23, 31, 38, 53, 65, 68, 68,
245                 39, 38, 38, 53, 65, 68, 68, 68,
246                 50, 38, 53, 65, 68, 68, 68, 68,
247                 46, 53, 65, 68, 68, 68, 68, 68,
248                 62, 65, 68, 68, 68, 68, 68, 68,
249                 68, 68, 68, 68, 68, 68, 68, 68
250         },
251         {/* level 1 */
252                 16, 11, 11, 16, 23, 27, 31, 30,
253                 11, 12, 12, 15, 20, 23, 23, 30,
254                 11, 12, 13, 16, 23, 26, 35, 47,
255                 16, 15, 16, 23, 26, 37, 47, 64,
256                 23, 20, 23, 26, 39, 51, 64, 64,
257                 27, 23, 26, 37, 51, 64, 64, 64,
258                 31, 23, 35, 47, 64, 64, 64, 64,
259                 30, 30, 47, 64, 64, 64, 64, 64
260         },
261         {/* level 2 */
262                 12,  8,  8, 12, 17, 21, 24, 23,
263                  8,  9,  9, 11, 15, 19, 18, 23,
264                  8,  9, 10, 12, 19, 20, 27, 36,
265                 12, 11, 12, 21, 20, 28, 36, 53,
266                 17, 15, 19, 20, 30, 39, 51, 59,
267                 21, 19, 20, 28, 39, 51, 59, 59,
268                 24, 18, 27, 36, 51, 59, 59, 59,
269                 23, 23, 36, 53, 59, 59, 59, 59
270         },
271         {/* level 3 - low compression quality */
272                  8,  6,  6,  8, 12, 14, 16, 17,
273                  6,  6,  6,  8, 10, 13, 12, 15,
274                  6,  6,  7,  8, 13, 14, 18, 24,
275                  8,  8,  8, 14, 13, 19, 24, 35,
276                 12, 10, 13, 13, 20, 26, 34, 39,
277                 14, 13, 14, 19, 26, 34, 39, 39,
278                 16, 12, 18, 24, 34, 39, 39, 39,
279                 17, 15, 24, 35, 39, 39, 39, 39
280         }
281 };
282
283 static const unsigned char qtbl_chrominance[4][64] = {
284         {/*level 0 - high compression quality */
285                 21, 25, 32, 38, 54, 68, 68, 68,
286                 25, 28, 24, 38, 54, 68, 68, 68,
287                 32, 24, 32, 43, 66, 68, 68, 68,
288                 38, 38, 43, 53, 68, 68, 68, 68,
289                 54, 54, 66, 68, 68, 68, 68, 68,
290                 68, 68, 68, 68, 68, 68, 68, 68,
291                 68, 68, 68, 68, 68, 68, 68, 68,
292                 68, 68, 68, 68, 68, 68, 68, 68
293         },
294         {/* level 1 */
295                 17, 15, 17, 21, 20, 26, 38, 48,
296                 15, 19, 18, 17, 20, 26, 35, 43,
297                 17, 18, 20, 22, 26, 30, 46, 53,
298                 21, 17, 22, 28, 30, 39, 53, 64,
299                 20, 20, 26, 30, 39, 48, 64, 64,
300                 26, 26, 30, 39, 48, 63, 64, 64,
301                 38, 35, 46, 53, 64, 64, 64, 64,
302                 48, 43, 53, 64, 64, 64, 64, 64
303         },
304         {/* level 2 */
305                 13, 11, 13, 16, 20, 20, 29, 37,
306                 11, 14, 14, 14, 16, 20, 26, 32,
307                 13, 14, 15, 17, 20, 23, 35, 40,
308                 16, 14, 17, 21, 23, 30, 40, 50,
309                 20, 16, 20, 23, 30, 37, 50, 59,
310                 20, 20, 23, 30, 37, 48, 59, 59,
311                 29, 26, 35, 40, 50, 59, 59, 59,
312                 37, 32, 40, 50, 59, 59, 59, 59
313         },
314         {/* level 3 - low compression quality */
315                  9,  8,  9, 11, 14, 17, 19, 24,
316                  8, 10,  9, 11, 14, 13, 17, 22,
317                  9,  9, 13, 14, 13, 15, 23, 26,
318                 11, 11, 14, 14, 15, 20, 26, 33,
319                 14, 14, 13, 15, 20, 24, 33, 39,
320                 17, 13, 15, 20, 24, 32, 39, 39,
321                 19, 17, 23, 26, 33, 39, 39, 39,
322                 24, 22, 26, 33, 39, 39, 39, 39
323         }
324 };
325
326 static const unsigned char hdctbl0[16] = {
327         0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0
328 };
329
330 static const unsigned char hdctblg0[12] = {
331         0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb
332 };
333 static const unsigned char hactbl0[16] = {
334         0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d
335 };
336 static const unsigned char hactblg0[162] = {
337         0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
338         0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
339         0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
340         0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
341         0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
342         0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
343         0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
344         0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
345         0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
346         0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
347         0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
348         0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
349         0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
350         0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
351         0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
352         0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
353         0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
354         0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
355         0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
356         0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
357         0xf9, 0xfa
358 };
359
360 /*
361  * Fourcc downgrade schema lookup tables for 422 and 420
362  * chroma subsampling - fourcc on each position maps on the
363  * fourcc from the table fourcc_to_dwngrd_schema_id which allows
364  * to get the most suitable fourcc counterpart for the given
365  * downgraded subsampling property.
366  */
367 static const u32 subs422_fourcc_dwngrd_schema[] = {
368         V4L2_PIX_FMT_NV16,
369         V4L2_PIX_FMT_NV61,
370 };
371
372 static const u32 subs420_fourcc_dwngrd_schema[] = {
373         V4L2_PIX_FMT_NV12,
374         V4L2_PIX_FMT_NV21,
375         V4L2_PIX_FMT_NV12,
376         V4L2_PIX_FMT_NV21,
377         V4L2_PIX_FMT_NV12,
378         V4L2_PIX_FMT_NV21,
379         V4L2_PIX_FMT_GREY,
380         V4L2_PIX_FMT_GREY,
381         V4L2_PIX_FMT_GREY,
382         V4L2_PIX_FMT_GREY,
383 };
384
385 /*
386  * Lookup table for translation of a fourcc to the position
387  * of its downgraded counterpart in the *fourcc_dwngrd_schema
388  * tables.
389  */
390 static const u32 fourcc_to_dwngrd_schema_id[] = {
391         V4L2_PIX_FMT_NV24,
392         V4L2_PIX_FMT_NV42,
393         V4L2_PIX_FMT_NV16,
394         V4L2_PIX_FMT_NV61,
395         V4L2_PIX_FMT_YUYV,
396         V4L2_PIX_FMT_YVYU,
397         V4L2_PIX_FMT_NV12,
398         V4L2_PIX_FMT_NV21,
399         V4L2_PIX_FMT_YUV420,
400         V4L2_PIX_FMT_GREY,
401 };
402
403 static int s5p_jpeg_get_dwngrd_sch_id_by_fourcc(u32 fourcc)
404 {
405         int i;
406         for (i = 0; i < ARRAY_SIZE(fourcc_to_dwngrd_schema_id); ++i) {
407                 if (fourcc_to_dwngrd_schema_id[i] == fourcc)
408                         return i;
409         }
410
411         return -EINVAL;
412 }
413
414 static int s5p_jpeg_adjust_fourcc_to_subsampling(
415                                         enum v4l2_jpeg_chroma_subsampling subs,
416                                         u32 in_fourcc,
417                                         u32 *out_fourcc,
418                                         struct s5p_jpeg_ctx *ctx)
419 {
420         int dwngrd_sch_id;
421
422         if (ctx->subsampling != V4L2_JPEG_CHROMA_SUBSAMPLING_GRAY) {
423                 dwngrd_sch_id =
424                         s5p_jpeg_get_dwngrd_sch_id_by_fourcc(in_fourcc);
425                 if (dwngrd_sch_id < 0)
426                         return -EINVAL;
427         }
428
429         switch (ctx->subsampling) {
430         case V4L2_JPEG_CHROMA_SUBSAMPLING_GRAY:
431                 *out_fourcc = V4L2_PIX_FMT_GREY;
432                 break;
433         case V4L2_JPEG_CHROMA_SUBSAMPLING_420:
434                 if (dwngrd_sch_id >
435                                 ARRAY_SIZE(subs420_fourcc_dwngrd_schema) - 1)
436                         return -EINVAL;
437                 *out_fourcc = subs420_fourcc_dwngrd_schema[dwngrd_sch_id];
438                 break;
439         case V4L2_JPEG_CHROMA_SUBSAMPLING_422:
440                 if (dwngrd_sch_id >
441                                 ARRAY_SIZE(subs422_fourcc_dwngrd_schema) - 1)
442                         return -EINVAL;
443                 *out_fourcc = subs422_fourcc_dwngrd_schema[dwngrd_sch_id];
444                 break;
445         default:
446                 *out_fourcc = V4L2_PIX_FMT_GREY;
447                 break;
448         }
449
450         return 0;
451 }
452
453 static int exynos4x12_decoded_subsampling[] = {
454         V4L2_JPEG_CHROMA_SUBSAMPLING_GRAY,
455         V4L2_JPEG_CHROMA_SUBSAMPLING_444,
456         V4L2_JPEG_CHROMA_SUBSAMPLING_422,
457         V4L2_JPEG_CHROMA_SUBSAMPLING_420,
458 };
459
460 static inline struct s5p_jpeg_ctx *ctrl_to_ctx(struct v4l2_ctrl *c)
461 {
462         return container_of(c->handler, struct s5p_jpeg_ctx, ctrl_handler);
463 }
464
465 static inline struct s5p_jpeg_ctx *fh_to_ctx(struct v4l2_fh *fh)
466 {
467         return container_of(fh, struct s5p_jpeg_ctx, fh);
468 }
469
470 static int s5p_jpeg_to_user_subsampling(struct s5p_jpeg_ctx *ctx)
471 {
472         WARN_ON(ctx->subsampling > 3);
473
474         if (ctx->jpeg->variant->version == SJPEG_S5P) {
475                 if (ctx->subsampling > 2)
476                         return V4L2_JPEG_CHROMA_SUBSAMPLING_GRAY;
477                 return ctx->subsampling;
478         } else {
479                 if (ctx->subsampling > 2)
480                         return V4L2_JPEG_CHROMA_SUBSAMPLING_420;
481                 return exynos4x12_decoded_subsampling[ctx->subsampling];
482         }
483 }
484
485 static inline void s5p_jpeg_set_qtbl(void __iomem *regs,
486                                      const unsigned char *qtbl,
487                                      unsigned long tab, int len)
488 {
489         int i;
490
491         for (i = 0; i < len; i++)
492                 writel((unsigned int)qtbl[i], regs + tab + (i * 0x04));
493 }
494
495 static inline void s5p_jpeg_set_qtbl_lum(void __iomem *regs, int quality)
496 {
497         /* this driver fills quantisation table 0 with data for luma */
498         s5p_jpeg_set_qtbl(regs, qtbl_luminance[quality],
499                           S5P_JPG_QTBL_CONTENT(0),
500                           ARRAY_SIZE(qtbl_luminance[quality]));
501 }
502
503 static inline void s5p_jpeg_set_qtbl_chr(void __iomem *regs, int quality)
504 {
505         /* this driver fills quantisation table 1 with data for chroma */
506         s5p_jpeg_set_qtbl(regs, qtbl_chrominance[quality],
507                           S5P_JPG_QTBL_CONTENT(1),
508                           ARRAY_SIZE(qtbl_chrominance[quality]));
509 }
510
511 static inline void s5p_jpeg_set_htbl(void __iomem *regs,
512                                      const unsigned char *htbl,
513                                      unsigned long tab, int len)
514 {
515         int i;
516
517         for (i = 0; i < len; i++)
518                 writel((unsigned int)htbl[i], regs + tab + (i * 0x04));
519 }
520
521 static inline void s5p_jpeg_set_hdctbl(void __iomem *regs)
522 {
523         /* this driver fills table 0 for this component */
524         s5p_jpeg_set_htbl(regs, hdctbl0, S5P_JPG_HDCTBL(0),
525                                                 ARRAY_SIZE(hdctbl0));
526 }
527
528 static inline void s5p_jpeg_set_hdctblg(void __iomem *regs)
529 {
530         /* this driver fills table 0 for this component */
531         s5p_jpeg_set_htbl(regs, hdctblg0, S5P_JPG_HDCTBLG(0),
532                                                 ARRAY_SIZE(hdctblg0));
533 }
534
535 static inline void s5p_jpeg_set_hactbl(void __iomem *regs)
536 {
537         /* this driver fills table 0 for this component */
538         s5p_jpeg_set_htbl(regs, hactbl0, S5P_JPG_HACTBL(0),
539                                                 ARRAY_SIZE(hactbl0));
540 }
541
542 static inline void s5p_jpeg_set_hactblg(void __iomem *regs)
543 {
544         /* this driver fills table 0 for this component */
545         s5p_jpeg_set_htbl(regs, hactblg0, S5P_JPG_HACTBLG(0),
546                                                 ARRAY_SIZE(hactblg0));
547 }
548
549 static inline void exynos4_jpeg_set_tbl(void __iomem *regs,
550                                         const unsigned char *tbl,
551                                         unsigned long tab, int len)
552 {
553         int i;
554         unsigned int dword;
555
556         for (i = 0; i < len; i += 4) {
557                 dword = tbl[i] |
558                         (tbl[i + 1] << 8) |
559                         (tbl[i + 2] << 16) |
560                         (tbl[i + 3] << 24);
561                 writel(dword, regs + tab + i);
562         }
563 }
564
565 static inline void exynos4_jpeg_set_qtbl_lum(void __iomem *regs, int quality)
566 {
567         /* this driver fills quantisation table 0 with data for luma */
568         exynos4_jpeg_set_tbl(regs, qtbl_luminance[quality],
569                              EXYNOS4_QTBL_CONTENT(0),
570                              ARRAY_SIZE(qtbl_luminance[quality]));
571 }
572
573 static inline void exynos4_jpeg_set_qtbl_chr(void __iomem *regs, int quality)
574 {
575         /* this driver fills quantisation table 1 with data for chroma */
576         exynos4_jpeg_set_tbl(regs, qtbl_chrominance[quality],
577                              EXYNOS4_QTBL_CONTENT(1),
578                              ARRAY_SIZE(qtbl_chrominance[quality]));
579 }
580
581 void exynos4_jpeg_set_huff_tbl(void __iomem *base)
582 {
583         exynos4_jpeg_set_tbl(base, hdctbl0, EXYNOS4_HUFF_TBL_HDCLL,
584                                                         ARRAY_SIZE(hdctbl0));
585         exynos4_jpeg_set_tbl(base, hdctbl0, EXYNOS4_HUFF_TBL_HDCCL,
586                                                         ARRAY_SIZE(hdctbl0));
587         exynos4_jpeg_set_tbl(base, hdctblg0, EXYNOS4_HUFF_TBL_HDCLV,
588                                                         ARRAY_SIZE(hdctblg0));
589         exynos4_jpeg_set_tbl(base, hdctblg0, EXYNOS4_HUFF_TBL_HDCCV,
590                                                         ARRAY_SIZE(hdctblg0));
591         exynos4_jpeg_set_tbl(base, hactbl0, EXYNOS4_HUFF_TBL_HACLL,
592                                                         ARRAY_SIZE(hactbl0));
593         exynos4_jpeg_set_tbl(base, hactbl0, EXYNOS4_HUFF_TBL_HACCL,
594                                                         ARRAY_SIZE(hactbl0));
595         exynos4_jpeg_set_tbl(base, hactblg0, EXYNOS4_HUFF_TBL_HACLV,
596                                                         ARRAY_SIZE(hactblg0));
597         exynos4_jpeg_set_tbl(base, hactblg0, EXYNOS4_HUFF_TBL_HACCV,
598                                                         ARRAY_SIZE(hactblg0));
599 }
600
601 /*
602  * ============================================================================
603  * Device file operations
604  * ============================================================================
605  */
606
607 static int queue_init(void *priv, struct vb2_queue *src_vq,
608                       struct vb2_queue *dst_vq);
609 static struct s5p_jpeg_fmt *s5p_jpeg_find_format(struct s5p_jpeg_ctx *ctx,
610                                 __u32 pixelformat, unsigned int fmt_type);
611 static int s5p_jpeg_controls_create(struct s5p_jpeg_ctx *ctx);
612
613 static int s5p_jpeg_open(struct file *file)
614 {
615         struct s5p_jpeg *jpeg = video_drvdata(file);
616         struct video_device *vfd = video_devdata(file);
617         struct s5p_jpeg_ctx *ctx;
618         struct s5p_jpeg_fmt *out_fmt, *cap_fmt;
619         int ret = 0;
620
621         ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
622         if (!ctx)
623                 return -ENOMEM;
624
625         if (mutex_lock_interruptible(&jpeg->lock)) {
626                 ret = -ERESTARTSYS;
627                 goto free;
628         }
629
630         v4l2_fh_init(&ctx->fh, vfd);
631         /* Use separate control handler per file handle */
632         ctx->fh.ctrl_handler = &ctx->ctrl_handler;
633         file->private_data = &ctx->fh;
634         v4l2_fh_add(&ctx->fh);
635
636         ctx->jpeg = jpeg;
637         if (vfd == jpeg->vfd_encoder) {
638                 ctx->mode = S5P_JPEG_ENCODE;
639                 out_fmt = s5p_jpeg_find_format(ctx, V4L2_PIX_FMT_RGB565,
640                                                         FMT_TYPE_OUTPUT);
641                 cap_fmt = s5p_jpeg_find_format(ctx, V4L2_PIX_FMT_JPEG,
642                                                         FMT_TYPE_CAPTURE);
643         } else {
644                 ctx->mode = S5P_JPEG_DECODE;
645                 out_fmt = s5p_jpeg_find_format(ctx, V4L2_PIX_FMT_JPEG,
646                                                         FMT_TYPE_OUTPUT);
647                 cap_fmt = s5p_jpeg_find_format(ctx, V4L2_PIX_FMT_YUYV,
648                                                         FMT_TYPE_CAPTURE);
649         }
650
651         ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(jpeg->m2m_dev, ctx, queue_init);
652         if (IS_ERR(ctx->fh.m2m_ctx)) {
653                 ret = PTR_ERR(ctx->fh.m2m_ctx);
654                 goto error;
655         }
656
657         ctx->out_q.fmt = out_fmt;
658         ctx->cap_q.fmt = cap_fmt;
659
660         ret = s5p_jpeg_controls_create(ctx);
661         if (ret < 0)
662                 goto error;
663
664         mutex_unlock(&jpeg->lock);
665         return 0;
666
667 error:
668         v4l2_fh_del(&ctx->fh);
669         v4l2_fh_exit(&ctx->fh);
670         mutex_unlock(&jpeg->lock);
671 free:
672         kfree(ctx);
673         return ret;
674 }
675
676 static int s5p_jpeg_release(struct file *file)
677 {
678         struct s5p_jpeg *jpeg = video_drvdata(file);
679         struct s5p_jpeg_ctx *ctx = fh_to_ctx(file->private_data);
680
681         mutex_lock(&jpeg->lock);
682         v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
683         v4l2_ctrl_handler_free(&ctx->ctrl_handler);
684         v4l2_fh_del(&ctx->fh);
685         v4l2_fh_exit(&ctx->fh);
686         kfree(ctx);
687         mutex_unlock(&jpeg->lock);
688
689         return 0;
690 }
691
692 static const struct v4l2_file_operations s5p_jpeg_fops = {
693         .owner          = THIS_MODULE,
694         .open           = s5p_jpeg_open,
695         .release        = s5p_jpeg_release,
696         .poll           = v4l2_m2m_fop_poll,
697         .unlocked_ioctl = video_ioctl2,
698         .mmap           = v4l2_m2m_fop_mmap,
699 };
700
701 /*
702  * ============================================================================
703  * video ioctl operations
704  * ============================================================================
705  */
706
707 static int get_byte(struct s5p_jpeg_buffer *buf)
708 {
709         if (buf->curr >= buf->size)
710                 return -1;
711
712         return ((unsigned char *)buf->data)[buf->curr++];
713 }
714
715 static int get_word_be(struct s5p_jpeg_buffer *buf, unsigned int *word)
716 {
717         unsigned int temp;
718         int byte;
719
720         byte = get_byte(buf);
721         if (byte == -1)
722                 return -1;
723         temp = byte << 8;
724         byte = get_byte(buf);
725         if (byte == -1)
726                 return -1;
727         *word = (unsigned int)byte | temp;
728         return 0;
729 }
730
731 static void skip(struct s5p_jpeg_buffer *buf, long len)
732 {
733         if (len <= 0)
734                 return;
735
736         while (len--)
737                 get_byte(buf);
738 }
739
740 static bool s5p_jpeg_parse_hdr(struct s5p_jpeg_q_data *result,
741                                unsigned long buffer, unsigned long size,
742                                struct s5p_jpeg_ctx *ctx)
743 {
744         int c, components, notfound;
745         unsigned int height, width, word, subsampling = 0;
746         long length;
747         struct s5p_jpeg_buffer jpeg_buffer;
748
749         jpeg_buffer.size = size;
750         jpeg_buffer.data = buffer;
751         jpeg_buffer.curr = 0;
752
753         notfound = 1;
754         while (notfound) {
755                 c = get_byte(&jpeg_buffer);
756                 if (c == -1)
757                         break;
758                 if (c != 0xff)
759                         continue;
760                 do
761                         c = get_byte(&jpeg_buffer);
762                 while (c == 0xff);
763                 if (c == -1)
764                         break;
765                 if (c == 0)
766                         continue;
767                 length = 0;
768                 switch (c) {
769                 /* SOF0: baseline JPEG */
770                 case SOF0:
771                         if (get_word_be(&jpeg_buffer, &word))
772                                 break;
773                         if (get_byte(&jpeg_buffer) == -1)
774                                 break;
775                         if (get_word_be(&jpeg_buffer, &height))
776                                 break;
777                         if (get_word_be(&jpeg_buffer, &width))
778                                 break;
779                         components = get_byte(&jpeg_buffer);
780                         if (components == -1)
781                                 break;
782                         notfound = 0;
783
784                         if (components == 1) {
785                                 subsampling = 0x33;
786                         } else {
787                                 skip(&jpeg_buffer, 1);
788                                 subsampling = get_byte(&jpeg_buffer);
789                                 skip(&jpeg_buffer, 1);
790                         }
791
792                         skip(&jpeg_buffer, components * 2);
793                         break;
794
795                 /* skip payload-less markers */
796                 case RST ... RST + 7:
797                 case SOI:
798                 case EOI:
799                 case TEM:
800                         break;
801
802                 /* skip uninteresting payload markers */
803                 default:
804                         if (get_word_be(&jpeg_buffer, &word))
805                                 break;
806                         length = (long)word - 2;
807                         skip(&jpeg_buffer, length);
808                         break;
809                 }
810         }
811         result->w = width;
812         result->h = height;
813         result->size = components;
814
815         switch (subsampling) {
816         case 0x11:
817                 ctx->subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_444;
818                 break;
819         case 0x21:
820                 ctx->subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_422;
821                 break;
822         case 0x22:
823                 ctx->subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_420;
824                 break;
825         case 0x33:
826                 ctx->subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_GRAY;
827                 break;
828         default:
829                 return false;
830         }
831
832         return !notfound;
833 }
834
835 static int s5p_jpeg_querycap(struct file *file, void *priv,
836                            struct v4l2_capability *cap)
837 {
838         struct s5p_jpeg_ctx *ctx = fh_to_ctx(priv);
839
840         if (ctx->mode == S5P_JPEG_ENCODE) {
841                 strlcpy(cap->driver, S5P_JPEG_M2M_NAME " encoder",
842                         sizeof(cap->driver));
843                 strlcpy(cap->card, S5P_JPEG_M2M_NAME " encoder",
844                         sizeof(cap->card));
845         } else {
846                 strlcpy(cap->driver, S5P_JPEG_M2M_NAME " decoder",
847                         sizeof(cap->driver));
848                 strlcpy(cap->card, S5P_JPEG_M2M_NAME " decoder",
849                         sizeof(cap->card));
850         }
851         cap->bus_info[0] = 0;
852         /*
853          * This is only a mem-to-mem video device. The capture and output
854          * device capability flags are left only for backward compatibility
855          * and are scheduled for removal.
856          */
857         cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_M2M |
858                             V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT;
859         return 0;
860 }
861
862 static int enum_fmt(struct s5p_jpeg_fmt *sjpeg_formats, int n,
863                     struct v4l2_fmtdesc *f, u32 type)
864 {
865         int i, num = 0;
866
867         for (i = 0; i < n; ++i) {
868                 if (sjpeg_formats[i].flags & type) {
869                         /* index-th format of type type found ? */
870                         if (num == f->index)
871                                 break;
872                         /* Correct type but haven't reached our index yet,
873                          * just increment per-type index */
874                         ++num;
875                 }
876         }
877
878         /* Format not found */
879         if (i >= n)
880                 return -EINVAL;
881
882         strlcpy(f->description, sjpeg_formats[i].name, sizeof(f->description));
883         f->pixelformat = sjpeg_formats[i].fourcc;
884
885         return 0;
886 }
887
888 static int s5p_jpeg_enum_fmt_vid_cap(struct file *file, void *priv,
889                                    struct v4l2_fmtdesc *f)
890 {
891         struct s5p_jpeg_ctx *ctx = fh_to_ctx(priv);
892
893         if (ctx->mode == S5P_JPEG_ENCODE)
894                 return enum_fmt(sjpeg_formats, SJPEG_NUM_FORMATS, f,
895                                 SJPEG_FMT_FLAG_ENC_CAPTURE);
896
897         return enum_fmt(sjpeg_formats, SJPEG_NUM_FORMATS, f,
898                                         SJPEG_FMT_FLAG_DEC_CAPTURE);
899 }
900
901 static int s5p_jpeg_enum_fmt_vid_out(struct file *file, void *priv,
902                                    struct v4l2_fmtdesc *f)
903 {
904         struct s5p_jpeg_ctx *ctx = fh_to_ctx(priv);
905
906         if (ctx->mode == S5P_JPEG_ENCODE)
907                 return enum_fmt(sjpeg_formats, SJPEG_NUM_FORMATS, f,
908                                 SJPEG_FMT_FLAG_ENC_OUTPUT);
909
910         return enum_fmt(sjpeg_formats, SJPEG_NUM_FORMATS, f,
911                                         SJPEG_FMT_FLAG_DEC_OUTPUT);
912 }
913
914 static struct s5p_jpeg_q_data *get_q_data(struct s5p_jpeg_ctx *ctx,
915                                           enum v4l2_buf_type type)
916 {
917         if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
918                 return &ctx->out_q;
919         if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
920                 return &ctx->cap_q;
921
922         return NULL;
923 }
924
925 static int s5p_jpeg_g_fmt(struct file *file, void *priv, struct v4l2_format *f)
926 {
927         struct vb2_queue *vq;
928         struct s5p_jpeg_q_data *q_data = NULL;
929         struct v4l2_pix_format *pix = &f->fmt.pix;
930         struct s5p_jpeg_ctx *ct = fh_to_ctx(priv);
931
932         vq = v4l2_m2m_get_vq(ct->fh.m2m_ctx, f->type);
933         if (!vq)
934                 return -EINVAL;
935
936         if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
937             ct->mode == S5P_JPEG_DECODE && !ct->hdr_parsed)
938                 return -EINVAL;
939         q_data = get_q_data(ct, f->type);
940         BUG_ON(q_data == NULL);
941
942         pix->width = q_data->w;
943         pix->height = q_data->h;
944         pix->field = V4L2_FIELD_NONE;
945         pix->pixelformat = q_data->fmt->fourcc;
946         pix->bytesperline = 0;
947         if (q_data->fmt->fourcc != V4L2_PIX_FMT_JPEG) {
948                 u32 bpl = q_data->w;
949                 if (q_data->fmt->colplanes == 1)
950                         bpl = (bpl * q_data->fmt->depth) >> 3;
951                 pix->bytesperline = bpl;
952         }
953         pix->sizeimage = q_data->size;
954
955         return 0;
956 }
957
958 static struct s5p_jpeg_fmt *s5p_jpeg_find_format(struct s5p_jpeg_ctx *ctx,
959                                 u32 pixelformat, unsigned int fmt_type)
960 {
961         unsigned int k, fmt_flag;
962
963         if (ctx->mode == S5P_JPEG_ENCODE)
964                 fmt_flag = (fmt_type == FMT_TYPE_OUTPUT) ?
965                                 SJPEG_FMT_FLAG_ENC_OUTPUT :
966                                 SJPEG_FMT_FLAG_ENC_CAPTURE;
967         else
968                 fmt_flag = (fmt_type == FMT_TYPE_OUTPUT) ?
969                                 SJPEG_FMT_FLAG_DEC_OUTPUT :
970                                 SJPEG_FMT_FLAG_DEC_CAPTURE;
971
972         for (k = 0; k < ARRAY_SIZE(sjpeg_formats); k++) {
973                 struct s5p_jpeg_fmt *fmt = &sjpeg_formats[k];
974                 if (fmt->fourcc == pixelformat &&
975                     fmt->flags & fmt_flag &&
976                     fmt->flags & ctx->jpeg->variant->fmt_ver_flag) {
977                         return fmt;
978                 }
979         }
980
981         return NULL;
982 }
983
984 static void jpeg_bound_align_image(u32 *w, unsigned int wmin, unsigned int wmax,
985                                    unsigned int walign,
986                                    u32 *h, unsigned int hmin, unsigned int hmax,
987                                    unsigned int halign)
988 {
989         int width, height, w_step, h_step;
990
991         width = *w;
992         height = *h;
993
994         w_step = 1 << walign;
995         h_step = 1 << halign;
996         v4l_bound_align_image(w, wmin, wmax, walign, h, hmin, hmax, halign, 0);
997
998         if (*w < width && (*w + w_step) < wmax)
999                 *w += w_step;
1000         if (*h < height && (*h + h_step) < hmax)
1001                 *h += h_step;
1002
1003 }
1004
1005 static int vidioc_try_fmt(struct v4l2_format *f, struct s5p_jpeg_fmt *fmt,
1006                           struct s5p_jpeg_ctx *ctx, int q_type)
1007 {
1008         struct v4l2_pix_format *pix = &f->fmt.pix;
1009
1010         if (pix->field == V4L2_FIELD_ANY)
1011                 pix->field = V4L2_FIELD_NONE;
1012         else if (pix->field != V4L2_FIELD_NONE)
1013                 return -EINVAL;
1014
1015         /* V4L2 specification suggests the driver corrects the format struct
1016          * if any of the dimensions is unsupported */
1017         if (q_type == FMT_TYPE_OUTPUT)
1018                 jpeg_bound_align_image(&pix->width, S5P_JPEG_MIN_WIDTH,
1019                                        S5P_JPEG_MAX_WIDTH, 0,
1020                                        &pix->height, S5P_JPEG_MIN_HEIGHT,
1021                                        S5P_JPEG_MAX_HEIGHT, 0);
1022         else
1023                 jpeg_bound_align_image(&pix->width, S5P_JPEG_MIN_WIDTH,
1024                                        S5P_JPEG_MAX_WIDTH, fmt->h_align,
1025                                        &pix->height, S5P_JPEG_MIN_HEIGHT,
1026                                        S5P_JPEG_MAX_HEIGHT, fmt->v_align);
1027
1028         if (fmt->fourcc == V4L2_PIX_FMT_JPEG) {
1029                 if (pix->sizeimage <= 0)
1030                         pix->sizeimage = PAGE_SIZE;
1031                 pix->bytesperline = 0;
1032         } else {
1033                 u32 bpl = pix->bytesperline;
1034
1035                 if (fmt->colplanes > 1 && bpl < pix->width)
1036                         bpl = pix->width; /* planar */
1037
1038                 if (fmt->colplanes == 1 && /* packed */
1039                     (bpl << 3) / fmt->depth < pix->width)
1040                         bpl = (pix->width * fmt->depth) >> 3;
1041
1042                 pix->bytesperline = bpl;
1043                 pix->sizeimage = (pix->width * pix->height * fmt->depth) >> 3;
1044         }
1045
1046         return 0;
1047 }
1048
1049 static int s5p_jpeg_try_fmt_vid_cap(struct file *file, void *priv,
1050                                   struct v4l2_format *f)
1051 {
1052         struct s5p_jpeg_ctx *ctx = fh_to_ctx(priv);
1053         struct v4l2_pix_format *pix = &f->fmt.pix;
1054         struct s5p_jpeg_fmt *fmt;
1055         int ret;
1056
1057         fmt = s5p_jpeg_find_format(ctx, f->fmt.pix.pixelformat,
1058                                                 FMT_TYPE_CAPTURE);
1059         if (!fmt) {
1060                 v4l2_err(&ctx->jpeg->v4l2_dev,
1061                          "Fourcc format (0x%08x) invalid.\n",
1062                          f->fmt.pix.pixelformat);
1063                 return -EINVAL;
1064         }
1065
1066         if ((ctx->jpeg->variant->version != SJPEG_EXYNOS4) ||
1067             (ctx->mode != S5P_JPEG_DECODE))
1068                 goto exit;
1069
1070         /*
1071          * The exynos4x12 device requires resulting YUV image
1072          * subsampling not to be lower than the input jpeg subsampling.
1073          * If this requirement is not met then downgrade the requested
1074          * capture format to the one with subsampling equal to the input jpeg.
1075          */
1076         if ((fmt->flags & SJPEG_FMT_NON_RGB) &&
1077             (fmt->subsampling < ctx->subsampling)) {
1078                 ret = s5p_jpeg_adjust_fourcc_to_subsampling(ctx->subsampling,
1079                                                             fmt->fourcc,
1080                                                             &pix->pixelformat,
1081                                                             ctx);
1082                 if (ret < 0)
1083                         pix->pixelformat = V4L2_PIX_FMT_GREY;
1084
1085                 fmt = s5p_jpeg_find_format(ctx, pix->pixelformat,
1086                                                         FMT_TYPE_CAPTURE);
1087         }
1088
1089         /*
1090          * Decompression of a JPEG file with 4:2:0 subsampling and odd
1091          * width to the YUV 4:2:0 compliant formats produces a raw image
1092          * with broken luma component. Adjust capture format to RGB565
1093          * in such a case.
1094          */
1095         if (ctx->subsampling == V4L2_JPEG_CHROMA_SUBSAMPLING_420 &&
1096             (ctx->out_q.w & 1) &&
1097             (pix->pixelformat == V4L2_PIX_FMT_NV12 ||
1098              pix->pixelformat == V4L2_PIX_FMT_NV21 ||
1099              pix->pixelformat == V4L2_PIX_FMT_YUV420)) {
1100                 pix->pixelformat = V4L2_PIX_FMT_RGB565;
1101                 fmt = s5p_jpeg_find_format(ctx, pix->pixelformat,
1102                                                         FMT_TYPE_CAPTURE);
1103         }
1104
1105 exit:
1106         return vidioc_try_fmt(f, fmt, ctx, FMT_TYPE_CAPTURE);
1107 }
1108
1109 static int s5p_jpeg_try_fmt_vid_out(struct file *file, void *priv,
1110                                   struct v4l2_format *f)
1111 {
1112         struct s5p_jpeg_ctx *ctx = fh_to_ctx(priv);
1113         struct s5p_jpeg_fmt *fmt;
1114
1115         fmt = s5p_jpeg_find_format(ctx, f->fmt.pix.pixelformat,
1116                                                 FMT_TYPE_OUTPUT);
1117         if (!fmt) {
1118                 v4l2_err(&ctx->jpeg->v4l2_dev,
1119                          "Fourcc format (0x%08x) invalid.\n",
1120                          f->fmt.pix.pixelformat);
1121                 return -EINVAL;
1122         }
1123
1124         return vidioc_try_fmt(f, fmt, ctx, FMT_TYPE_OUTPUT);
1125 }
1126
1127 static int exynos4_jpeg_get_output_buffer_size(struct s5p_jpeg_ctx *ctx,
1128                                                 struct v4l2_format *f,
1129                                                 int fmt_depth)
1130 {
1131         struct v4l2_pix_format *pix = &f->fmt.pix;
1132         u32 pix_fmt = f->fmt.pix.pixelformat;
1133         int w = pix->width, h = pix->height, wh_align;
1134
1135         if (pix_fmt == V4L2_PIX_FMT_RGB32 ||
1136             pix_fmt == V4L2_PIX_FMT_NV24 ||
1137             pix_fmt == V4L2_PIX_FMT_NV42 ||
1138             pix_fmt == V4L2_PIX_FMT_NV12 ||
1139             pix_fmt == V4L2_PIX_FMT_NV21 ||
1140             pix_fmt == V4L2_PIX_FMT_YUV420)
1141                 wh_align = 4;
1142         else
1143                 wh_align = 1;
1144
1145         jpeg_bound_align_image(&w, S5P_JPEG_MIN_WIDTH,
1146                                S5P_JPEG_MAX_WIDTH, wh_align,
1147                                &h, S5P_JPEG_MIN_HEIGHT,
1148                                S5P_JPEG_MAX_HEIGHT, wh_align);
1149
1150         return w * h * fmt_depth >> 3;
1151 }
1152
1153 static int s5p_jpeg_s_fmt(struct s5p_jpeg_ctx *ct, struct v4l2_format *f)
1154 {
1155         struct vb2_queue *vq;
1156         struct s5p_jpeg_q_data *q_data = NULL;
1157         struct v4l2_pix_format *pix = &f->fmt.pix;
1158         struct v4l2_ctrl *ctrl_subs;
1159         unsigned int f_type;
1160
1161         vq = v4l2_m2m_get_vq(ct->fh.m2m_ctx, f->type);
1162         if (!vq)
1163                 return -EINVAL;
1164
1165         q_data = get_q_data(ct, f->type);
1166         BUG_ON(q_data == NULL);
1167
1168         if (vb2_is_busy(vq)) {
1169                 v4l2_err(&ct->jpeg->v4l2_dev, "%s queue busy\n", __func__);
1170                 return -EBUSY;
1171         }
1172
1173         f_type = V4L2_TYPE_IS_OUTPUT(f->type) ?
1174                         FMT_TYPE_OUTPUT : FMT_TYPE_CAPTURE;
1175
1176         q_data->fmt = s5p_jpeg_find_format(ct, pix->pixelformat, f_type);
1177         q_data->w = pix->width;
1178         q_data->h = pix->height;
1179         if (q_data->fmt->fourcc != V4L2_PIX_FMT_JPEG) {
1180                 /*
1181                  * During encoding Exynos4x12 SoCs access wider memory area
1182                  * than it results from Image_x and Image_y values written to
1183                  * the JPEG_IMAGE_SIZE register. In order to avoid sysmmu
1184                  * page fault calculate proper buffer size in such a case.
1185                  */
1186                 if (ct->jpeg->variant->version == SJPEG_EXYNOS4 &&
1187                     f_type == FMT_TYPE_OUTPUT && ct->mode == S5P_JPEG_ENCODE)
1188                         q_data->size = exynos4_jpeg_get_output_buffer_size(ct,
1189                                                         f,
1190                                                         q_data->fmt->depth);
1191                 else
1192                         q_data->size = q_data->w * q_data->h *
1193                                                 q_data->fmt->depth >> 3;
1194         } else {
1195                 q_data->size = pix->sizeimage;
1196         }
1197
1198         if (f_type == FMT_TYPE_OUTPUT) {
1199                 ctrl_subs = v4l2_ctrl_find(&ct->ctrl_handler,
1200                                         V4L2_CID_JPEG_CHROMA_SUBSAMPLING);
1201                 if (ctrl_subs)
1202                         v4l2_ctrl_s_ctrl(ctrl_subs, q_data->fmt->subsampling);
1203         }
1204
1205         return 0;
1206 }
1207
1208 static int s5p_jpeg_s_fmt_vid_cap(struct file *file, void *priv,
1209                                 struct v4l2_format *f)
1210 {
1211         int ret;
1212
1213         ret = s5p_jpeg_try_fmt_vid_cap(file, priv, f);
1214         if (ret)
1215                 return ret;
1216
1217         return s5p_jpeg_s_fmt(fh_to_ctx(priv), f);
1218 }
1219
1220 static int s5p_jpeg_s_fmt_vid_out(struct file *file, void *priv,
1221                                 struct v4l2_format *f)
1222 {
1223         int ret;
1224
1225         ret = s5p_jpeg_try_fmt_vid_out(file, priv, f);
1226         if (ret)
1227                 return ret;
1228
1229         return s5p_jpeg_s_fmt(fh_to_ctx(priv), f);
1230 }
1231
1232 static int s5p_jpeg_g_selection(struct file *file, void *priv,
1233                          struct v4l2_selection *s)
1234 {
1235         struct s5p_jpeg_ctx *ctx = fh_to_ctx(priv);
1236
1237         if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
1238             s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1239                 return -EINVAL;
1240
1241         /* For JPEG blob active == default == bounds */
1242         switch (s->target) {
1243         case V4L2_SEL_TGT_CROP:
1244         case V4L2_SEL_TGT_CROP_BOUNDS:
1245         case V4L2_SEL_TGT_CROP_DEFAULT:
1246         case V4L2_SEL_TGT_COMPOSE:
1247         case V4L2_SEL_TGT_COMPOSE_DEFAULT:
1248                 s->r.width = ctx->out_q.w;
1249                 s->r.height = ctx->out_q.h;
1250                 break;
1251         case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1252         case V4L2_SEL_TGT_COMPOSE_PADDED:
1253                 s->r.width = ctx->cap_q.w;
1254                 s->r.height = ctx->cap_q.h;
1255                 break;
1256         default:
1257                 return -EINVAL;
1258         }
1259         s->r.left = 0;
1260         s->r.top = 0;
1261         return 0;
1262 }
1263
1264 /*
1265  * V4L2 controls
1266  */
1267
1268 static int s5p_jpeg_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
1269 {
1270         struct s5p_jpeg_ctx *ctx = ctrl_to_ctx(ctrl);
1271         struct s5p_jpeg *jpeg = ctx->jpeg;
1272         unsigned long flags;
1273
1274         switch (ctrl->id) {
1275         case V4L2_CID_JPEG_CHROMA_SUBSAMPLING:
1276                 spin_lock_irqsave(&jpeg->slock, flags);
1277                 ctrl->val = s5p_jpeg_to_user_subsampling(ctx);
1278                 spin_unlock_irqrestore(&jpeg->slock, flags);
1279                 break;
1280         }
1281
1282         return 0;
1283 }
1284
1285 static int s5p_jpeg_try_ctrl(struct v4l2_ctrl *ctrl)
1286 {
1287         struct s5p_jpeg_ctx *ctx = ctrl_to_ctx(ctrl);
1288         unsigned long flags;
1289         int ret = 0;
1290
1291         spin_lock_irqsave(&ctx->jpeg->slock, flags);
1292
1293         if (ctrl->id == V4L2_CID_JPEG_CHROMA_SUBSAMPLING) {
1294                 if (ctx->jpeg->variant->version == SJPEG_S5P)
1295                         goto error_free;
1296                 /*
1297                  * The exynos4x12 device requires input raw image fourcc
1298                  * to be V4L2_PIX_FMT_GREY if gray jpeg format
1299                  * is to be set.
1300                  */
1301                 if (ctx->out_q.fmt->fourcc != V4L2_PIX_FMT_GREY &&
1302                     ctrl->val == V4L2_JPEG_CHROMA_SUBSAMPLING_GRAY) {
1303                         ret = -EINVAL;
1304                         goto error_free;
1305                 }
1306                 /*
1307                  * The exynos4x12 device requires resulting jpeg subsampling
1308                  * not to be lower than the input raw image subsampling.
1309                  */
1310                 if (ctx->out_q.fmt->subsampling > ctrl->val)
1311                         ctrl->val = ctx->out_q.fmt->subsampling;
1312         }
1313
1314 error_free:
1315         spin_unlock_irqrestore(&ctx->jpeg->slock, flags);
1316         return ret;
1317 }
1318
1319 static int s5p_jpeg_s_ctrl(struct v4l2_ctrl *ctrl)
1320 {
1321         struct s5p_jpeg_ctx *ctx = ctrl_to_ctx(ctrl);
1322         unsigned long flags;
1323
1324         spin_lock_irqsave(&ctx->jpeg->slock, flags);
1325
1326         switch (ctrl->id) {
1327         case V4L2_CID_JPEG_COMPRESSION_QUALITY:
1328                 ctx->compr_quality = ctrl->val;
1329                 break;
1330         case V4L2_CID_JPEG_RESTART_INTERVAL:
1331                 ctx->restart_interval = ctrl->val;
1332                 break;
1333         case V4L2_CID_JPEG_CHROMA_SUBSAMPLING:
1334                 ctx->subsampling = ctrl->val;
1335                 break;
1336         }
1337
1338         spin_unlock_irqrestore(&ctx->jpeg->slock, flags);
1339         return 0;
1340 }
1341
1342 static const struct v4l2_ctrl_ops s5p_jpeg_ctrl_ops = {
1343         .g_volatile_ctrl        = s5p_jpeg_g_volatile_ctrl,
1344         .try_ctrl               = s5p_jpeg_try_ctrl,
1345         .s_ctrl                 = s5p_jpeg_s_ctrl,
1346 };
1347
1348 static int s5p_jpeg_controls_create(struct s5p_jpeg_ctx *ctx)
1349 {
1350         unsigned int mask = ~0x27; /* 444, 422, 420, GRAY */
1351         struct v4l2_ctrl *ctrl;
1352         int ret;
1353
1354         v4l2_ctrl_handler_init(&ctx->ctrl_handler, 3);
1355
1356         if (ctx->mode == S5P_JPEG_ENCODE) {
1357                 v4l2_ctrl_new_std(&ctx->ctrl_handler, &s5p_jpeg_ctrl_ops,
1358                                   V4L2_CID_JPEG_COMPRESSION_QUALITY,
1359                                   0, 3, 1, S5P_JPEG_COMPR_QUAL_WORST);
1360
1361                 v4l2_ctrl_new_std(&ctx->ctrl_handler, &s5p_jpeg_ctrl_ops,
1362                                   V4L2_CID_JPEG_RESTART_INTERVAL,
1363                                   0, 3, 0xffff, 0);
1364                 if (ctx->jpeg->variant->version == SJPEG_S5P)
1365                         mask = ~0x06; /* 422, 420 */
1366         }
1367
1368         ctrl = v4l2_ctrl_new_std_menu(&ctx->ctrl_handler, &s5p_jpeg_ctrl_ops,
1369                                       V4L2_CID_JPEG_CHROMA_SUBSAMPLING,
1370                                       V4L2_JPEG_CHROMA_SUBSAMPLING_GRAY, mask,
1371                                       V4L2_JPEG_CHROMA_SUBSAMPLING_422);
1372
1373         if (ctx->ctrl_handler.error) {
1374                 ret = ctx->ctrl_handler.error;
1375                 goto error_free;
1376         }
1377
1378         if (ctx->mode == S5P_JPEG_DECODE)
1379                 ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE |
1380                         V4L2_CTRL_FLAG_READ_ONLY;
1381
1382         ret = v4l2_ctrl_handler_setup(&ctx->ctrl_handler);
1383         if (ret < 0)
1384                 goto error_free;
1385
1386         return ret;
1387
1388 error_free:
1389         v4l2_ctrl_handler_free(&ctx->ctrl_handler);
1390         return ret;
1391 }
1392
1393 static const struct v4l2_ioctl_ops s5p_jpeg_ioctl_ops = {
1394         .vidioc_querycap                = s5p_jpeg_querycap,
1395
1396         .vidioc_enum_fmt_vid_cap        = s5p_jpeg_enum_fmt_vid_cap,
1397         .vidioc_enum_fmt_vid_out        = s5p_jpeg_enum_fmt_vid_out,
1398
1399         .vidioc_g_fmt_vid_cap           = s5p_jpeg_g_fmt,
1400         .vidioc_g_fmt_vid_out           = s5p_jpeg_g_fmt,
1401
1402         .vidioc_try_fmt_vid_cap         = s5p_jpeg_try_fmt_vid_cap,
1403         .vidioc_try_fmt_vid_out         = s5p_jpeg_try_fmt_vid_out,
1404
1405         .vidioc_s_fmt_vid_cap           = s5p_jpeg_s_fmt_vid_cap,
1406         .vidioc_s_fmt_vid_out           = s5p_jpeg_s_fmt_vid_out,
1407
1408         .vidioc_reqbufs                 = v4l2_m2m_ioctl_reqbufs,
1409         .vidioc_querybuf                = v4l2_m2m_ioctl_querybuf,
1410         .vidioc_qbuf                    = v4l2_m2m_ioctl_qbuf,
1411         .vidioc_dqbuf                   = v4l2_m2m_ioctl_dqbuf,
1412
1413         .vidioc_streamon                = v4l2_m2m_ioctl_streamon,
1414         .vidioc_streamoff               = v4l2_m2m_ioctl_streamoff,
1415
1416         .vidioc_g_selection             = s5p_jpeg_g_selection,
1417 };
1418
1419 /*
1420  * ============================================================================
1421  * mem2mem callbacks
1422  * ============================================================================
1423  */
1424
1425 static void s5p_jpeg_device_run(void *priv)
1426 {
1427         struct s5p_jpeg_ctx *ctx = priv;
1428         struct s5p_jpeg *jpeg = ctx->jpeg;
1429         struct vb2_buffer *src_buf, *dst_buf;
1430         unsigned long src_addr, dst_addr, flags;
1431
1432         spin_lock_irqsave(&ctx->jpeg->slock, flags);
1433
1434         src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
1435         dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
1436         src_addr = vb2_dma_contig_plane_dma_addr(src_buf, 0);
1437         dst_addr = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
1438
1439         s5p_jpeg_reset(jpeg->regs);
1440         s5p_jpeg_poweron(jpeg->regs);
1441         s5p_jpeg_proc_mode(jpeg->regs, ctx->mode);
1442         if (ctx->mode == S5P_JPEG_ENCODE) {
1443                 if (ctx->out_q.fmt->fourcc == V4L2_PIX_FMT_RGB565)
1444                         s5p_jpeg_input_raw_mode(jpeg->regs,
1445                                                         S5P_JPEG_RAW_IN_565);
1446                 else
1447                         s5p_jpeg_input_raw_mode(jpeg->regs,
1448                                                         S5P_JPEG_RAW_IN_422);
1449                 s5p_jpeg_subsampling_mode(jpeg->regs, ctx->subsampling);
1450                 s5p_jpeg_dri(jpeg->regs, ctx->restart_interval);
1451                 s5p_jpeg_x(jpeg->regs, ctx->out_q.w);
1452                 s5p_jpeg_y(jpeg->regs, ctx->out_q.h);
1453                 s5p_jpeg_imgadr(jpeg->regs, src_addr);
1454                 s5p_jpeg_jpgadr(jpeg->regs, dst_addr);
1455
1456                 /* ultimately comes from sizeimage from userspace */
1457                 s5p_jpeg_enc_stream_int(jpeg->regs, ctx->cap_q.size);
1458
1459                 /* JPEG RGB to YCbCr conversion matrix */
1460                 s5p_jpeg_coef(jpeg->regs, 1, 1, S5P_JPEG_COEF11);
1461                 s5p_jpeg_coef(jpeg->regs, 1, 2, S5P_JPEG_COEF12);
1462                 s5p_jpeg_coef(jpeg->regs, 1, 3, S5P_JPEG_COEF13);
1463                 s5p_jpeg_coef(jpeg->regs, 2, 1, S5P_JPEG_COEF21);
1464                 s5p_jpeg_coef(jpeg->regs, 2, 2, S5P_JPEG_COEF22);
1465                 s5p_jpeg_coef(jpeg->regs, 2, 3, S5P_JPEG_COEF23);
1466                 s5p_jpeg_coef(jpeg->regs, 3, 1, S5P_JPEG_COEF31);
1467                 s5p_jpeg_coef(jpeg->regs, 3, 2, S5P_JPEG_COEF32);
1468                 s5p_jpeg_coef(jpeg->regs, 3, 3, S5P_JPEG_COEF33);
1469
1470                 /*
1471                  * JPEG IP allows storing 4 quantization tables
1472                  * We fill table 0 for luma and table 1 for chroma
1473                  */
1474                 s5p_jpeg_set_qtbl_lum(jpeg->regs, ctx->compr_quality);
1475                 s5p_jpeg_set_qtbl_chr(jpeg->regs, ctx->compr_quality);
1476                 /* use table 0 for Y */
1477                 s5p_jpeg_qtbl(jpeg->regs, 1, 0);
1478                 /* use table 1 for Cb and Cr*/
1479                 s5p_jpeg_qtbl(jpeg->regs, 2, 1);
1480                 s5p_jpeg_qtbl(jpeg->regs, 3, 1);
1481
1482                 /* Y, Cb, Cr use Huffman table 0 */
1483                 s5p_jpeg_htbl_ac(jpeg->regs, 1);
1484                 s5p_jpeg_htbl_dc(jpeg->regs, 1);
1485                 s5p_jpeg_htbl_ac(jpeg->regs, 2);
1486                 s5p_jpeg_htbl_dc(jpeg->regs, 2);
1487                 s5p_jpeg_htbl_ac(jpeg->regs, 3);
1488                 s5p_jpeg_htbl_dc(jpeg->regs, 3);
1489         } else { /* S5P_JPEG_DECODE */
1490                 s5p_jpeg_rst_int_enable(jpeg->regs, true);
1491                 s5p_jpeg_data_num_int_enable(jpeg->regs, true);
1492                 s5p_jpeg_final_mcu_num_int_enable(jpeg->regs, true);
1493                 if (ctx->cap_q.fmt->fourcc == V4L2_PIX_FMT_YUYV)
1494                         s5p_jpeg_outform_raw(jpeg->regs, S5P_JPEG_RAW_OUT_422);
1495                 else
1496                         s5p_jpeg_outform_raw(jpeg->regs, S5P_JPEG_RAW_OUT_420);
1497                 s5p_jpeg_jpgadr(jpeg->regs, src_addr);
1498                 s5p_jpeg_imgadr(jpeg->regs, dst_addr);
1499         }
1500
1501         s5p_jpeg_start(jpeg->regs);
1502
1503         spin_unlock_irqrestore(&ctx->jpeg->slock, flags);
1504 }
1505
1506 static void exynos4_jpeg_set_img_addr(struct s5p_jpeg_ctx *ctx)
1507 {
1508         struct s5p_jpeg *jpeg = ctx->jpeg;
1509         struct s5p_jpeg_fmt *fmt;
1510         struct vb2_buffer *vb;
1511         struct s5p_jpeg_addr jpeg_addr;
1512         u32 pix_size, padding_bytes = 0;
1513
1514         pix_size = ctx->cap_q.w * ctx->cap_q.h;
1515
1516         if (ctx->mode == S5P_JPEG_ENCODE) {
1517                 vb = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
1518                 fmt = ctx->out_q.fmt;
1519                 if (ctx->out_q.w % 2 && fmt->h_align > 0)
1520                         padding_bytes = ctx->out_q.h;
1521         } else {
1522                 fmt = ctx->cap_q.fmt;
1523                 vb = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
1524         }
1525
1526         jpeg_addr.y = vb2_dma_contig_plane_dma_addr(vb, 0);
1527
1528         if (fmt->colplanes == 2) {
1529                 jpeg_addr.cb = jpeg_addr.y + pix_size - padding_bytes;
1530         } else if (fmt->colplanes == 3) {
1531                 jpeg_addr.cb = jpeg_addr.y + pix_size;
1532                 if (fmt->fourcc == V4L2_PIX_FMT_YUV420)
1533                         jpeg_addr.cr = jpeg_addr.cb + pix_size / 4;
1534                 else
1535                         jpeg_addr.cr = jpeg_addr.cb + pix_size / 2;
1536         }
1537
1538         exynos4_jpeg_set_frame_buf_address(jpeg->regs, &jpeg_addr);
1539 }
1540
1541 static void exynos4_jpeg_set_jpeg_addr(struct s5p_jpeg_ctx *ctx)
1542 {
1543         struct s5p_jpeg *jpeg = ctx->jpeg;
1544         struct vb2_buffer *vb;
1545         unsigned int jpeg_addr = 0;
1546
1547         if (ctx->mode == S5P_JPEG_ENCODE)
1548                 vb = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
1549         else
1550                 vb = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
1551
1552         jpeg_addr = vb2_dma_contig_plane_dma_addr(vb, 0);
1553         exynos4_jpeg_set_stream_buf_address(jpeg->regs, jpeg_addr);
1554 }
1555
1556 static void exynos4_jpeg_device_run(void *priv)
1557 {
1558         struct s5p_jpeg_ctx *ctx = priv;
1559         struct s5p_jpeg *jpeg = ctx->jpeg;
1560         unsigned int bitstream_size;
1561         unsigned long flags;
1562
1563         spin_lock_irqsave(&ctx->jpeg->slock, flags);
1564
1565         if (ctx->mode == S5P_JPEG_ENCODE) {
1566                 exynos4_jpeg_sw_reset(jpeg->regs);
1567                 exynos4_jpeg_set_interrupt(jpeg->regs);
1568                 exynos4_jpeg_set_huf_table_enable(jpeg->regs, 1);
1569
1570                 exynos4_jpeg_set_huff_tbl(jpeg->regs);
1571
1572                 /*
1573                  * JPEG IP allows storing 4 quantization tables
1574                  * We fill table 0 for luma and table 1 for chroma
1575                  */
1576                 exynos4_jpeg_set_qtbl_lum(jpeg->regs, ctx->compr_quality);
1577                 exynos4_jpeg_set_qtbl_chr(jpeg->regs, ctx->compr_quality);
1578
1579                 exynos4_jpeg_set_encode_tbl_select(jpeg->regs,
1580                                                         ctx->compr_quality);
1581                 exynos4_jpeg_set_stream_size(jpeg->regs, ctx->cap_q.w,
1582                                                         ctx->cap_q.h);
1583
1584                 exynos4_jpeg_set_enc_out_fmt(jpeg->regs, ctx->subsampling);
1585                 exynos4_jpeg_set_img_fmt(jpeg->regs, ctx->out_q.fmt->fourcc);
1586                 exynos4_jpeg_set_img_addr(ctx);
1587                 exynos4_jpeg_set_jpeg_addr(ctx);
1588                 exynos4_jpeg_set_encode_hoff_cnt(jpeg->regs,
1589                                                         ctx->out_q.fmt->fourcc);
1590         } else {
1591                 exynos4_jpeg_sw_reset(jpeg->regs);
1592                 exynos4_jpeg_set_interrupt(jpeg->regs);
1593                 exynos4_jpeg_set_img_addr(ctx);
1594                 exynos4_jpeg_set_jpeg_addr(ctx);
1595                 exynos4_jpeg_set_img_fmt(jpeg->regs, ctx->cap_q.fmt->fourcc);
1596
1597                 bitstream_size = DIV_ROUND_UP(ctx->out_q.size, 32);
1598
1599                 exynos4_jpeg_set_dec_bitstream_size(jpeg->regs, bitstream_size);
1600         }
1601
1602         exynos4_jpeg_set_enc_dec_mode(jpeg->regs, ctx->mode);
1603
1604         spin_unlock_irqrestore(&ctx->jpeg->slock, flags);
1605 }
1606
1607 static int s5p_jpeg_job_ready(void *priv)
1608 {
1609         struct s5p_jpeg_ctx *ctx = priv;
1610
1611         if (ctx->mode == S5P_JPEG_DECODE)
1612                 return ctx->hdr_parsed;
1613         return 1;
1614 }
1615
1616 static void s5p_jpeg_job_abort(void *priv)
1617 {
1618 }
1619
1620 static struct v4l2_m2m_ops s5p_jpeg_m2m_ops = {
1621         .device_run     = s5p_jpeg_device_run,
1622         .job_ready      = s5p_jpeg_job_ready,
1623         .job_abort      = s5p_jpeg_job_abort,
1624 }
1625 ;
1626 static struct v4l2_m2m_ops exynos4_jpeg_m2m_ops = {
1627         .device_run     = exynos4_jpeg_device_run,
1628         .job_ready      = s5p_jpeg_job_ready,
1629         .job_abort      = s5p_jpeg_job_abort,
1630 };
1631
1632 /*
1633  * ============================================================================
1634  * Queue operations
1635  * ============================================================================
1636  */
1637
1638 static int s5p_jpeg_queue_setup(struct vb2_queue *vq,
1639                            const struct v4l2_format *fmt,
1640                            unsigned int *nbuffers, unsigned int *nplanes,
1641                            unsigned int sizes[], void *alloc_ctxs[])
1642 {
1643         struct s5p_jpeg_ctx *ctx = vb2_get_drv_priv(vq);
1644         struct s5p_jpeg_q_data *q_data = NULL;
1645         unsigned int size, count = *nbuffers;
1646
1647         q_data = get_q_data(ctx, vq->type);
1648         BUG_ON(q_data == NULL);
1649
1650         size = q_data->size;
1651
1652         /*
1653          * header is parsed during decoding and parsed information stored
1654          * in the context so we do not allow another buffer to overwrite it
1655          */
1656         if (ctx->mode == S5P_JPEG_DECODE)
1657                 count = 1;
1658
1659         *nbuffers = count;
1660         *nplanes = 1;
1661         sizes[0] = size;
1662         alloc_ctxs[0] = ctx->jpeg->alloc_ctx;
1663
1664         return 0;
1665 }
1666
1667 static int s5p_jpeg_buf_prepare(struct vb2_buffer *vb)
1668 {
1669         struct s5p_jpeg_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1670         struct s5p_jpeg_q_data *q_data = NULL;
1671
1672         q_data = get_q_data(ctx, vb->vb2_queue->type);
1673         BUG_ON(q_data == NULL);
1674
1675         if (vb2_plane_size(vb, 0) < q_data->size) {
1676                 pr_err("%s data will not fit into plane (%lu < %lu)\n",
1677                                 __func__, vb2_plane_size(vb, 0),
1678                                 (long)q_data->size);
1679                 return -EINVAL;
1680         }
1681
1682         vb2_set_plane_payload(vb, 0, q_data->size);
1683
1684         return 0;
1685 }
1686
1687 static void s5p_jpeg_buf_queue(struct vb2_buffer *vb)
1688 {
1689         struct s5p_jpeg_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1690
1691         if (ctx->mode == S5P_JPEG_DECODE &&
1692             vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1693                 struct s5p_jpeg_q_data tmp, *q_data;
1694                 ctx->hdr_parsed = s5p_jpeg_parse_hdr(&tmp,
1695                      (unsigned long)vb2_plane_vaddr(vb, 0),
1696                      min((unsigned long)ctx->out_q.size,
1697                          vb2_get_plane_payload(vb, 0)), ctx);
1698                 if (!ctx->hdr_parsed) {
1699                         vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
1700                         return;
1701                 }
1702
1703                 q_data = &ctx->out_q;
1704                 q_data->w = tmp.w;
1705                 q_data->h = tmp.h;
1706
1707                 q_data = &ctx->cap_q;
1708                 q_data->w = tmp.w;
1709                 q_data->h = tmp.h;
1710         }
1711
1712         v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
1713 }
1714
1715 static int s5p_jpeg_start_streaming(struct vb2_queue *q, unsigned int count)
1716 {
1717         struct s5p_jpeg_ctx *ctx = vb2_get_drv_priv(q);
1718         int ret;
1719
1720         ret = pm_runtime_get_sync(ctx->jpeg->dev);
1721
1722         return ret > 0 ? 0 : ret;
1723 }
1724
1725 static void s5p_jpeg_stop_streaming(struct vb2_queue *q)
1726 {
1727         struct s5p_jpeg_ctx *ctx = vb2_get_drv_priv(q);
1728
1729         pm_runtime_put(ctx->jpeg->dev);
1730 }
1731
1732 static struct vb2_ops s5p_jpeg_qops = {
1733         .queue_setup            = s5p_jpeg_queue_setup,
1734         .buf_prepare            = s5p_jpeg_buf_prepare,
1735         .buf_queue              = s5p_jpeg_buf_queue,
1736         .wait_prepare           = vb2_ops_wait_prepare,
1737         .wait_finish            = vb2_ops_wait_finish,
1738         .start_streaming        = s5p_jpeg_start_streaming,
1739         .stop_streaming         = s5p_jpeg_stop_streaming,
1740 };
1741
1742 static int queue_init(void *priv, struct vb2_queue *src_vq,
1743                       struct vb2_queue *dst_vq)
1744 {
1745         struct s5p_jpeg_ctx *ctx = priv;
1746         int ret;
1747
1748         src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1749         src_vq->io_modes = VB2_MMAP | VB2_USERPTR;
1750         src_vq->drv_priv = ctx;
1751         src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1752         src_vq->ops = &s5p_jpeg_qops;
1753         src_vq->mem_ops = &vb2_dma_contig_memops;
1754         src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1755         src_vq->lock = &ctx->jpeg->lock;
1756
1757         ret = vb2_queue_init(src_vq);
1758         if (ret)
1759                 return ret;
1760
1761         dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1762         dst_vq->io_modes = VB2_MMAP | VB2_USERPTR;
1763         dst_vq->drv_priv = ctx;
1764         dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1765         dst_vq->ops = &s5p_jpeg_qops;
1766         dst_vq->mem_ops = &vb2_dma_contig_memops;
1767         dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1768         dst_vq->lock = &ctx->jpeg->lock;
1769
1770         return vb2_queue_init(dst_vq);
1771 }
1772
1773 /*
1774  * ============================================================================
1775  * ISR
1776  * ============================================================================
1777  */
1778
1779 static irqreturn_t s5p_jpeg_irq(int irq, void *dev_id)
1780 {
1781         struct s5p_jpeg *jpeg = dev_id;
1782         struct s5p_jpeg_ctx *curr_ctx;
1783         struct vb2_buffer *src_buf, *dst_buf;
1784         unsigned long payload_size = 0;
1785         enum vb2_buffer_state state = VB2_BUF_STATE_DONE;
1786         bool enc_jpeg_too_large = false;
1787         bool timer_elapsed = false;
1788         bool op_completed = false;
1789
1790         spin_lock(&jpeg->slock);
1791
1792         curr_ctx = v4l2_m2m_get_curr_priv(jpeg->m2m_dev);
1793
1794         src_buf = v4l2_m2m_src_buf_remove(curr_ctx->fh.m2m_ctx);
1795         dst_buf = v4l2_m2m_dst_buf_remove(curr_ctx->fh.m2m_ctx);
1796
1797         if (curr_ctx->mode == S5P_JPEG_ENCODE)
1798                 enc_jpeg_too_large = s5p_jpeg_enc_stream_stat(jpeg->regs);
1799         timer_elapsed = s5p_jpeg_timer_stat(jpeg->regs);
1800         op_completed = s5p_jpeg_result_stat_ok(jpeg->regs);
1801         if (curr_ctx->mode == S5P_JPEG_DECODE)
1802                 op_completed = op_completed &&
1803                                         s5p_jpeg_stream_stat_ok(jpeg->regs);
1804
1805         if (enc_jpeg_too_large) {
1806                 state = VB2_BUF_STATE_ERROR;
1807                 s5p_jpeg_clear_enc_stream_stat(jpeg->regs);
1808         } else if (timer_elapsed) {
1809                 state = VB2_BUF_STATE_ERROR;
1810                 s5p_jpeg_clear_timer_stat(jpeg->regs);
1811         } else if (!op_completed) {
1812                 state = VB2_BUF_STATE_ERROR;
1813         } else {
1814                 payload_size = s5p_jpeg_compressed_size(jpeg->regs);
1815         }
1816
1817         dst_buf->v4l2_buf.timecode = src_buf->v4l2_buf.timecode;
1818         dst_buf->v4l2_buf.timestamp = src_buf->v4l2_buf.timestamp;
1819         dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
1820         dst_buf->v4l2_buf.flags |=
1821                 src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
1822
1823         v4l2_m2m_buf_done(src_buf, state);
1824         if (curr_ctx->mode == S5P_JPEG_ENCODE)
1825                 vb2_set_plane_payload(dst_buf, 0, payload_size);
1826         v4l2_m2m_buf_done(dst_buf, state);
1827         v4l2_m2m_job_finish(jpeg->m2m_dev, curr_ctx->fh.m2m_ctx);
1828
1829         curr_ctx->subsampling = s5p_jpeg_get_subsampling_mode(jpeg->regs);
1830         spin_unlock(&jpeg->slock);
1831
1832         s5p_jpeg_clear_int(jpeg->regs);
1833
1834         return IRQ_HANDLED;
1835 }
1836
1837 static irqreturn_t exynos4_jpeg_irq(int irq, void *priv)
1838 {
1839         unsigned int int_status;
1840         struct vb2_buffer *src_vb, *dst_vb;
1841         struct s5p_jpeg *jpeg = priv;
1842         struct s5p_jpeg_ctx *curr_ctx;
1843         unsigned long payload_size = 0;
1844
1845         spin_lock(&jpeg->slock);
1846
1847         curr_ctx = v4l2_m2m_get_curr_priv(jpeg->m2m_dev);
1848
1849         src_vb = v4l2_m2m_src_buf_remove(curr_ctx->fh.m2m_ctx);
1850         dst_vb = v4l2_m2m_dst_buf_remove(curr_ctx->fh.m2m_ctx);
1851
1852         int_status = exynos4_jpeg_get_int_status(jpeg->regs);
1853
1854         if (int_status) {
1855                 switch (int_status & 0x1f) {
1856                 case 0x1:
1857                         jpeg->irq_ret = ERR_PROT;
1858                         break;
1859                 case 0x2:
1860                         jpeg->irq_ret = OK_ENC_OR_DEC;
1861                         break;
1862                 case 0x4:
1863                         jpeg->irq_ret = ERR_DEC_INVALID_FORMAT;
1864                         break;
1865                 case 0x8:
1866                         jpeg->irq_ret = ERR_MULTI_SCAN;
1867                         break;
1868                 case 0x10:
1869                         jpeg->irq_ret = ERR_FRAME;
1870                         break;
1871                 default:
1872                         jpeg->irq_ret = ERR_UNKNOWN;
1873                         break;
1874                 }
1875         } else {
1876                 jpeg->irq_ret = ERR_UNKNOWN;
1877         }
1878
1879         if (jpeg->irq_ret == OK_ENC_OR_DEC) {
1880                 if (curr_ctx->mode == S5P_JPEG_ENCODE) {
1881                         payload_size = exynos4_jpeg_get_stream_size(jpeg->regs);
1882                         vb2_set_plane_payload(dst_vb, 0, payload_size);
1883                 }
1884                 v4l2_m2m_buf_done(src_vb, VB2_BUF_STATE_DONE);
1885                 v4l2_m2m_buf_done(dst_vb, VB2_BUF_STATE_DONE);
1886         } else {
1887                 v4l2_m2m_buf_done(src_vb, VB2_BUF_STATE_ERROR);
1888                 v4l2_m2m_buf_done(dst_vb, VB2_BUF_STATE_ERROR);
1889         }
1890
1891         v4l2_m2m_job_finish(jpeg->m2m_dev, curr_ctx->fh.m2m_ctx);
1892         curr_ctx->subsampling = exynos4_jpeg_get_frame_fmt(jpeg->regs);
1893
1894         spin_unlock(&jpeg->slock);
1895         return IRQ_HANDLED;
1896 }
1897
1898 static void *jpeg_get_drv_data(struct device *dev);
1899
1900 /*
1901  * ============================================================================
1902  * Driver basic infrastructure
1903  * ============================================================================
1904  */
1905
1906 static int s5p_jpeg_probe(struct platform_device *pdev)
1907 {
1908         struct s5p_jpeg *jpeg;
1909         struct resource *res;
1910         int ret;
1911
1912         /* JPEG IP abstraction struct */
1913         jpeg = devm_kzalloc(&pdev->dev, sizeof(struct s5p_jpeg), GFP_KERNEL);
1914         if (!jpeg)
1915                 return -ENOMEM;
1916
1917         jpeg->variant = jpeg_get_drv_data(&pdev->dev);
1918
1919         mutex_init(&jpeg->lock);
1920         spin_lock_init(&jpeg->slock);
1921         jpeg->dev = &pdev->dev;
1922
1923         /* memory-mapped registers */
1924         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1925
1926         jpeg->regs = devm_ioremap_resource(&pdev->dev, res);
1927         if (IS_ERR(jpeg->regs))
1928                 return PTR_ERR(jpeg->regs);
1929
1930         /* interrupt service routine registration */
1931         jpeg->irq = ret = platform_get_irq(pdev, 0);
1932         if (ret < 0) {
1933                 dev_err(&pdev->dev, "cannot find IRQ\n");
1934                 return ret;
1935         }
1936
1937         ret = devm_request_irq(&pdev->dev, jpeg->irq, jpeg->variant->jpeg_irq,
1938                                 0, dev_name(&pdev->dev), jpeg);
1939         if (ret) {
1940                 dev_err(&pdev->dev, "cannot claim IRQ %d\n", jpeg->irq);
1941                 return ret;
1942         }
1943
1944         /* clocks */
1945         jpeg->clk = clk_get(&pdev->dev, "jpeg");
1946         if (IS_ERR(jpeg->clk)) {
1947                 dev_err(&pdev->dev, "cannot get clock\n");
1948                 ret = PTR_ERR(jpeg->clk);
1949                 return ret;
1950         }
1951         dev_dbg(&pdev->dev, "clock source %p\n", jpeg->clk);
1952
1953         /* v4l2 device */
1954         ret = v4l2_device_register(&pdev->dev, &jpeg->v4l2_dev);
1955         if (ret) {
1956                 dev_err(&pdev->dev, "Failed to register v4l2 device\n");
1957                 goto clk_get_rollback;
1958         }
1959
1960         /* mem2mem device */
1961         jpeg->m2m_dev = v4l2_m2m_init(jpeg->variant->m2m_ops);
1962         if (IS_ERR(jpeg->m2m_dev)) {
1963                 v4l2_err(&jpeg->v4l2_dev, "Failed to init mem2mem device\n");
1964                 ret = PTR_ERR(jpeg->m2m_dev);
1965                 goto device_register_rollback;
1966         }
1967
1968         jpeg->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
1969         if (IS_ERR(jpeg->alloc_ctx)) {
1970                 v4l2_err(&jpeg->v4l2_dev, "Failed to init memory allocator\n");
1971                 ret = PTR_ERR(jpeg->alloc_ctx);
1972                 goto m2m_init_rollback;
1973         }
1974
1975         /* JPEG encoder /dev/videoX node */
1976         jpeg->vfd_encoder = video_device_alloc();
1977         if (!jpeg->vfd_encoder) {
1978                 v4l2_err(&jpeg->v4l2_dev, "Failed to allocate video device\n");
1979                 ret = -ENOMEM;
1980                 goto vb2_allocator_rollback;
1981         }
1982         snprintf(jpeg->vfd_encoder->name, sizeof(jpeg->vfd_encoder->name),
1983                                 "%s-enc", S5P_JPEG_M2M_NAME);
1984         jpeg->vfd_encoder->fops         = &s5p_jpeg_fops;
1985         jpeg->vfd_encoder->ioctl_ops    = &s5p_jpeg_ioctl_ops;
1986         jpeg->vfd_encoder->minor        = -1;
1987         jpeg->vfd_encoder->release      = video_device_release;
1988         jpeg->vfd_encoder->lock         = &jpeg->lock;
1989         jpeg->vfd_encoder->v4l2_dev     = &jpeg->v4l2_dev;
1990         jpeg->vfd_encoder->vfl_dir      = VFL_DIR_M2M;
1991
1992         ret = video_register_device(jpeg->vfd_encoder, VFL_TYPE_GRABBER, -1);
1993         if (ret) {
1994                 v4l2_err(&jpeg->v4l2_dev, "Failed to register video device\n");
1995                 goto enc_vdev_alloc_rollback;
1996         }
1997
1998         video_set_drvdata(jpeg->vfd_encoder, jpeg);
1999         v4l2_info(&jpeg->v4l2_dev,
2000                   "encoder device registered as /dev/video%d\n",
2001                   jpeg->vfd_encoder->num);
2002
2003         /* JPEG decoder /dev/videoX node */
2004         jpeg->vfd_decoder = video_device_alloc();
2005         if (!jpeg->vfd_decoder) {
2006                 v4l2_err(&jpeg->v4l2_dev, "Failed to allocate video device\n");
2007                 ret = -ENOMEM;
2008                 goto enc_vdev_register_rollback;
2009         }
2010         snprintf(jpeg->vfd_decoder->name, sizeof(jpeg->vfd_decoder->name),
2011                                 "%s-dec", S5P_JPEG_M2M_NAME);
2012         jpeg->vfd_decoder->fops         = &s5p_jpeg_fops;
2013         jpeg->vfd_decoder->ioctl_ops    = &s5p_jpeg_ioctl_ops;
2014         jpeg->vfd_decoder->minor        = -1;
2015         jpeg->vfd_decoder->release      = video_device_release;
2016         jpeg->vfd_decoder->lock         = &jpeg->lock;
2017         jpeg->vfd_decoder->v4l2_dev     = &jpeg->v4l2_dev;
2018         jpeg->vfd_decoder->vfl_dir      = VFL_DIR_M2M;
2019
2020         ret = video_register_device(jpeg->vfd_decoder, VFL_TYPE_GRABBER, -1);
2021         if (ret) {
2022                 v4l2_err(&jpeg->v4l2_dev, "Failed to register video device\n");
2023                 goto dec_vdev_alloc_rollback;
2024         }
2025
2026         video_set_drvdata(jpeg->vfd_decoder, jpeg);
2027         v4l2_info(&jpeg->v4l2_dev,
2028                   "decoder device registered as /dev/video%d\n",
2029                   jpeg->vfd_decoder->num);
2030
2031         /* final statements & power management */
2032         platform_set_drvdata(pdev, jpeg);
2033
2034         pm_runtime_enable(&pdev->dev);
2035
2036         v4l2_info(&jpeg->v4l2_dev, "Samsung S5P JPEG codec\n");
2037
2038         return 0;
2039
2040 dec_vdev_alloc_rollback:
2041         video_device_release(jpeg->vfd_decoder);
2042
2043 enc_vdev_register_rollback:
2044         video_unregister_device(jpeg->vfd_encoder);
2045
2046 enc_vdev_alloc_rollback:
2047         video_device_release(jpeg->vfd_encoder);
2048
2049 vb2_allocator_rollback:
2050         vb2_dma_contig_cleanup_ctx(jpeg->alloc_ctx);
2051
2052 m2m_init_rollback:
2053         v4l2_m2m_release(jpeg->m2m_dev);
2054
2055 device_register_rollback:
2056         v4l2_device_unregister(&jpeg->v4l2_dev);
2057
2058 clk_get_rollback:
2059         clk_put(jpeg->clk);
2060
2061         return ret;
2062 }
2063
2064 static int s5p_jpeg_remove(struct platform_device *pdev)
2065 {
2066         struct s5p_jpeg *jpeg = platform_get_drvdata(pdev);
2067
2068         pm_runtime_disable(jpeg->dev);
2069
2070         video_unregister_device(jpeg->vfd_decoder);
2071         video_device_release(jpeg->vfd_decoder);
2072         video_unregister_device(jpeg->vfd_encoder);
2073         video_device_release(jpeg->vfd_encoder);
2074         vb2_dma_contig_cleanup_ctx(jpeg->alloc_ctx);
2075         v4l2_m2m_release(jpeg->m2m_dev);
2076         v4l2_device_unregister(&jpeg->v4l2_dev);
2077
2078         if (!pm_runtime_status_suspended(&pdev->dev))
2079                 clk_disable_unprepare(jpeg->clk);
2080
2081         clk_put(jpeg->clk);
2082
2083         return 0;
2084 }
2085
2086 static int s5p_jpeg_runtime_suspend(struct device *dev)
2087 {
2088         struct s5p_jpeg *jpeg = dev_get_drvdata(dev);
2089
2090         clk_disable_unprepare(jpeg->clk);
2091
2092         return 0;
2093 }
2094
2095 static int s5p_jpeg_runtime_resume(struct device *dev)
2096 {
2097         struct s5p_jpeg *jpeg = dev_get_drvdata(dev);
2098         unsigned long flags;
2099         int ret;
2100
2101         ret = clk_prepare_enable(jpeg->clk);
2102         if (ret < 0)
2103                 return ret;
2104
2105         spin_lock_irqsave(&jpeg->slock, flags);
2106
2107         /*
2108          * JPEG IP allows storing two Huffman tables for each component
2109          * We fill table 0 for each component and do this here only
2110          * for S5PC210 device as Exynos4x12 requires programming its
2111          * Huffman tables each time the encoding process is initialized.
2112          */
2113         if (jpeg->variant->version == SJPEG_S5P) {
2114                 s5p_jpeg_set_hdctbl(jpeg->regs);
2115                 s5p_jpeg_set_hdctblg(jpeg->regs);
2116                 s5p_jpeg_set_hactbl(jpeg->regs);
2117                 s5p_jpeg_set_hactblg(jpeg->regs);
2118         }
2119
2120         spin_unlock_irqrestore(&jpeg->slock, flags);
2121
2122         return 0;
2123 }
2124
2125 static int s5p_jpeg_suspend(struct device *dev)
2126 {
2127         if (pm_runtime_suspended(dev))
2128                 return 0;
2129
2130         return s5p_jpeg_runtime_suspend(dev);
2131 }
2132
2133 static int s5p_jpeg_resume(struct device *dev)
2134 {
2135         if (pm_runtime_suspended(dev))
2136                 return 0;
2137
2138         return s5p_jpeg_runtime_resume(dev);
2139 }
2140
2141 static const struct dev_pm_ops s5p_jpeg_pm_ops = {
2142         SET_SYSTEM_SLEEP_PM_OPS(s5p_jpeg_suspend, s5p_jpeg_resume)
2143         SET_RUNTIME_PM_OPS(s5p_jpeg_runtime_suspend, s5p_jpeg_runtime_resume, NULL)
2144 };
2145
2146 static struct s5p_jpeg_variant s5p_jpeg_drvdata = {
2147         .version        = SJPEG_S5P,
2148         .jpeg_irq       = s5p_jpeg_irq,
2149         .m2m_ops        = &s5p_jpeg_m2m_ops,
2150         .fmt_ver_flag   = SJPEG_FMT_FLAG_S5P,
2151 };
2152
2153 static struct s5p_jpeg_variant exynos4_jpeg_drvdata = {
2154         .version        = SJPEG_EXYNOS4,
2155         .jpeg_irq       = exynos4_jpeg_irq,
2156         .m2m_ops        = &exynos4_jpeg_m2m_ops,
2157         .fmt_ver_flag   = SJPEG_FMT_FLAG_EXYNOS4,
2158 };
2159
2160 static const struct of_device_id samsung_jpeg_match[] = {
2161         {
2162                 .compatible = "samsung,s5pv210-jpeg",
2163                 .data = &s5p_jpeg_drvdata,
2164         }, {
2165                 .compatible = "samsung,exynos4210-jpeg",
2166                 .data = &s5p_jpeg_drvdata,
2167         }, {
2168                 .compatible = "samsung,exynos4212-jpeg",
2169                 .data = &exynos4_jpeg_drvdata,
2170         },
2171         {},
2172 };
2173
2174 MODULE_DEVICE_TABLE(of, samsung_jpeg_match);
2175
2176 static void *jpeg_get_drv_data(struct device *dev)
2177 {
2178         struct s5p_jpeg_variant *driver_data = NULL;
2179         const struct of_device_id *match;
2180
2181         if (!IS_ENABLED(CONFIG_OF) || !dev->of_node)
2182                 return &s5p_jpeg_drvdata;
2183
2184         match = of_match_node(samsung_jpeg_match, dev->of_node);
2185
2186         if (match)
2187                 driver_data = (struct s5p_jpeg_variant *)match->data;
2188
2189         return driver_data;
2190 }
2191
2192 static struct platform_driver s5p_jpeg_driver = {
2193         .probe = s5p_jpeg_probe,
2194         .remove = s5p_jpeg_remove,
2195         .driver = {
2196                 .of_match_table = of_match_ptr(samsung_jpeg_match),
2197                 .owner          = THIS_MODULE,
2198                 .name           = S5P_JPEG_M2M_NAME,
2199                 .pm             = &s5p_jpeg_pm_ops,
2200         },
2201 };
2202
2203 module_platform_driver(s5p_jpeg_driver);
2204
2205 MODULE_AUTHOR("Andrzej Pietrasiewicz <andrzej.p@samsung.com>");
2206 MODULE_AUTHOR("Jacek Anaszewski <j.anaszewski@samsung.com>");
2207 MODULE_DESCRIPTION("Samsung JPEG codec driver");
2208 MODULE_LICENSE("GPL");