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