forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrcar_jpu.c
1764 lines (1458 loc) · 48.6 KB
/
rcar_jpu.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-2.0
/*
* Author: Mikhail Ulyanov
* Copyright (C) 2014-2015 Cogent Embedded, Inc. <[email protected]>
* Copyright (C) 2014-2015 Renesas Electronics Corporation
*
* This is based on the drivers/media/platform/s5p-jpeg driver by
* Andrzej Pietrasiewicz and Jacek Anaszewski.
* Some portions of code inspired by VSP1 driver by Laurent Pinchart.
*
* TODO in order of priority:
* 1) Rotation
* 2) Cropping
* 3) V4L2_CID_JPEG_ACTIVE_MARKER
*/
#include <asm/unaligned.h>
#include <linux/clk.h>
#include <linux/err.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/platform_device.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/string.h>
#include <linux/videodev2.h>
#include <media/v4l2-ctrls.h>
#include <media/v4l2-device.h>
#include <media/v4l2-event.h>
#include <media/v4l2-fh.h>
#include <media/v4l2-mem2mem.h>
#include <media/v4l2-ioctl.h>
#include <media/videobuf2-v4l2.h>
#include <media/videobuf2-dma-contig.h>
#define DRV_NAME "rcar_jpu"
/*
* Align JPEG header end to cache line to make sure we will not have any issues
* with cache; additionally to requirement (33.3.27 R01UH0501EJ0100 Rev.1.00)
*/
#define JPU_JPEG_HDR_SIZE (ALIGN(0x258, L1_CACHE_BYTES))
#define JPU_JPEG_MAX_BYTES_PER_PIXEL 2 /* 16 bit precision format */
#define JPU_JPEG_MIN_SIZE 25 /* SOI + SOF + EOI */
#define JPU_JPEG_QTBL_SIZE 0x40
#define JPU_JPEG_HDCTBL_SIZE 0x1c
#define JPU_JPEG_HACTBL_SIZE 0xb2
#define JPU_JPEG_HEIGHT_OFFSET 0x91
#define JPU_JPEG_WIDTH_OFFSET 0x93
#define JPU_JPEG_SUBS_OFFSET 0x97
#define JPU_JPEG_QTBL_LUM_OFFSET 0x07
#define JPU_JPEG_QTBL_CHR_OFFSET 0x4c
#define JPU_JPEG_HDCTBL_LUM_OFFSET 0xa4
#define JPU_JPEG_HACTBL_LUM_OFFSET 0xc5
#define JPU_JPEG_HDCTBL_CHR_OFFSET 0x17c
#define JPU_JPEG_HACTBL_CHR_OFFSET 0x19d
#define JPU_JPEG_PADDING_OFFSET 0x24f
#define JPU_JPEG_LUM 0x00
#define JPU_JPEG_CHR 0x01
#define JPU_JPEG_DC 0x00
#define JPU_JPEG_AC 0x10
#define JPU_JPEG_422 0x21
#define JPU_JPEG_420 0x22
#define JPU_JPEG_DEFAULT_422_PIX_FMT V4L2_PIX_FMT_NV16M
#define JPU_JPEG_DEFAULT_420_PIX_FMT V4L2_PIX_FMT_NV12M
/* JPEG markers */
#define TEM 0x01
#define SOF0 0xc0
#define RST 0xd0
#define SOI 0xd8
#define EOI 0xd9
#define DHP 0xde
#define DHT 0xc4
#define COM 0xfe
#define DQT 0xdb
#define DRI 0xdd
#define APP0 0xe0
#define JPU_RESET_TIMEOUT 100 /* ms */
#define JPU_JOB_TIMEOUT 300 /* ms */
#define JPU_MAX_QUALITY 4
#define JPU_WIDTH_MIN 16
#define JPU_HEIGHT_MIN 16
#define JPU_WIDTH_MAX 4096
#define JPU_HEIGHT_MAX 4096
#define JPU_MEMALIGN 8
/* Flags that indicate a format can be used for capture/output */
#define JPU_FMT_TYPE_OUTPUT 0
#define JPU_FMT_TYPE_CAPTURE 1
#define JPU_ENC_CAPTURE (1 << 0)
#define JPU_ENC_OUTPUT (1 << 1)
#define JPU_DEC_CAPTURE (1 << 2)
#define JPU_DEC_OUTPUT (1 << 3)
/*
* JPEG registers and bits
*/
/* JPEG code mode register */
#define JCMOD 0x00
#define JCMOD_PCTR (1 << 7)
#define JCMOD_MSKIP_ENABLE (1 << 5)
#define JCMOD_DSP_ENC (0 << 3)
#define JCMOD_DSP_DEC (1 << 3)
#define JCMOD_REDU (7 << 0)
#define JCMOD_REDU_422 (1 << 0)
#define JCMOD_REDU_420 (2 << 0)
/* JPEG code command register */
#define JCCMD 0x04
#define JCCMD_SRST (1 << 12)
#define JCCMD_JEND (1 << 2)
#define JCCMD_JSRT (1 << 0)
/* JPEG code quantization table number register */
#define JCQTN 0x0c
#define JCQTN_SHIFT(t) (((t) - 1) << 1)
/* JPEG code Huffman table number register */
#define JCHTN 0x10
#define JCHTN_AC_SHIFT(t) (((t) << 1) - 1)
#define JCHTN_DC_SHIFT(t) (((t) - 1) << 1)
#define JCVSZU 0x1c /* JPEG code vertical size upper register */
#define JCVSZD 0x20 /* JPEG code vertical size lower register */
#define JCHSZU 0x24 /* JPEG code horizontal size upper register */
#define JCHSZD 0x28 /* JPEG code horizontal size lower register */
#define JCSZ_MASK 0xff /* JPEG code h/v size register contains only 1 byte*/
#define JCDTCU 0x2c /* JPEG code data count upper register */
#define JCDTCM 0x30 /* JPEG code data count middle register */
#define JCDTCD 0x34 /* JPEG code data count lower register */
/* JPEG interrupt enable register */
#define JINTE 0x38
#define JINTE_ERR (7 << 5) /* INT5 + INT6 + INT7 */
#define JINTE_TRANSF_COMPL (1 << 10)
/* JPEG interrupt status register */
#define JINTS 0x3c
#define JINTS_MASK 0x7c68
#define JINTS_ERR (1 << 5)
#define JINTS_PROCESS_COMPL (1 << 6)
#define JINTS_TRANSF_COMPL (1 << 10)
#define JCDERR 0x40 /* JPEG code decode error register */
#define JCDERR_MASK 0xf /* JPEG code decode error register mask*/
/* JPEG interface encoding */
#define JIFECNT 0x70
#define JIFECNT_INFT_422 0
#define JIFECNT_INFT_420 1
#define JIFECNT_SWAP_WB (3 << 4) /* to JPU */
#define JIFESYA1 0x74 /* encode source Y address register 1 */
#define JIFESCA1 0x78 /* encode source C address register 1 */
#define JIFESYA2 0x7c /* encode source Y address register 2 */
#define JIFESCA2 0x80 /* encode source C address register 2 */
#define JIFESMW 0x84 /* encode source memory width register */
#define JIFESVSZ 0x88 /* encode source vertical size register */
#define JIFESHSZ 0x8c /* encode source horizontal size register */
#define JIFEDA1 0x90 /* encode destination address register 1 */
#define JIFEDA2 0x94 /* encode destination address register 2 */
/* JPEG decoding control register */
#define JIFDCNT 0xa0
#define JIFDCNT_SWAP_WB (3 << 1) /* from JPU */
#define JIFDSA1 0xa4 /* decode source address register 1 */
#define JIFDDMW 0xb0 /* decode destination memory width register */
#define JIFDDVSZ 0xb4 /* decode destination vert. size register */
#define JIFDDHSZ 0xb8 /* decode destination horiz. size register */
#define JIFDDYA1 0xbc /* decode destination Y address register 1 */
#define JIFDDCA1 0xc0 /* decode destination C address register 1 */
#define JCQTBL(n) (0x10000 + (n) * 0x40) /* quantization tables regs */
#define JCHTBD(n) (0x10100 + (n) * 0x100) /* Huffman table DC regs */
#define JCHTBA(n) (0x10120 + (n) * 0x100) /* Huffman table AC regs */
/**
* struct jpu - JPEG IP abstraction
* @mutex: the mutex protecting this structure
* @lock: spinlock protecting the device contexts
* @v4l2_dev: v4l2 device for mem2mem mode
* @vfd_encoder: video device node for encoder mem2mem mode
* @vfd_decoder: video device node for decoder mem2mem mode
* @m2m_dev: v4l2 mem2mem device data
* @curr: pointer to current context
* @regs: JPEG IP registers mapping
* @irq: JPEG IP irq
* @clk: JPEG IP clock
* @dev: JPEG IP struct device
* @ref_count: reference counter
*/
struct jpu {
struct mutex mutex;
spinlock_t lock;
struct v4l2_device v4l2_dev;
struct video_device vfd_encoder;
struct video_device vfd_decoder;
struct v4l2_m2m_dev *m2m_dev;
struct jpu_ctx *curr;
void __iomem *regs;
unsigned int irq;
struct clk *clk;
struct device *dev;
int ref_count;
};
/**
* struct jpu_buffer - driver's specific video buffer
* @buf: m2m buffer
* @compr_quality: destination image quality in compression mode
* @subsampling: source image subsampling in decompression mode
*/
struct jpu_buffer {
struct v4l2_m2m_buffer buf;
unsigned short compr_quality;
unsigned char subsampling;
};
/**
* struct jpu_fmt - driver's internal format data
* @fourcc: the fourcc code, 0 if not applicable
* @colorspace: the colorspace specifier
* @bpp: number of bits per pixel per plane
* @h_align: horizontal alignment order (align to 2^h_align)
* @v_align: vertical alignment order (align to 2^v_align)
* @subsampling: (horizontal:4 | vertical:4) subsampling factor
* @num_planes: number of planes
* @types: types of queue this format is applicable to
*/
struct jpu_fmt {
u32 fourcc;
u32 colorspace;
u8 bpp[2];
u8 h_align;
u8 v_align;
u8 subsampling;
u8 num_planes;
u16 types;
};
/**
* struct jpu_q_data - parameters of one queue
* @fmtinfo: driver-specific format of this queue
* @format: multiplanar format of this queue
* @sequence: sequence number
*/
struct jpu_q_data {
struct jpu_fmt *fmtinfo;
struct v4l2_pix_format_mplane format;
unsigned int sequence;
};
/**
* struct jpu_ctx - the device context data
* @jpu: JPEG IP device for this context
* @encoder: compression (encode) operation or decompression (decode)
* @compr_quality: destination image quality in compression (encode) mode
* @out_q: source (output) queue information
* @cap_q: destination (capture) queue information
* @fh: file handler
* @ctrl_handler: controls handler
*/
struct jpu_ctx {
struct jpu *jpu;
bool encoder;
unsigned short compr_quality;
struct jpu_q_data out_q;
struct jpu_q_data cap_q;
struct v4l2_fh fh;
struct v4l2_ctrl_handler ctrl_handler;
};
/**
* jpeg_buffer - description of memory containing input JPEG data
* @end: end position in the buffer
* @curr: current position in the buffer
*/
struct jpeg_buffer {
void *end;
void *curr;
};
static struct jpu_fmt jpu_formats[] = {
{ V4L2_PIX_FMT_JPEG, V4L2_COLORSPACE_JPEG,
{0, 0}, 0, 0, 0, 1, JPU_ENC_CAPTURE | JPU_DEC_OUTPUT },
{ V4L2_PIX_FMT_NV16M, V4L2_COLORSPACE_SRGB,
{8, 8}, 2, 2, JPU_JPEG_422, 2, JPU_ENC_OUTPUT | JPU_DEC_CAPTURE },
{ V4L2_PIX_FMT_NV12M, V4L2_COLORSPACE_SRGB,
{8, 4}, 2, 2, JPU_JPEG_420, 2, JPU_ENC_OUTPUT | JPU_DEC_CAPTURE },
{ V4L2_PIX_FMT_NV16, V4L2_COLORSPACE_SRGB,
{16, 0}, 2, 2, JPU_JPEG_422, 1, JPU_ENC_OUTPUT | JPU_DEC_CAPTURE },
{ V4L2_PIX_FMT_NV12, V4L2_COLORSPACE_SRGB,
{12, 0}, 2, 2, JPU_JPEG_420, 1, JPU_ENC_OUTPUT | JPU_DEC_CAPTURE },
};
static const u8 zigzag[] = {
0x03, 0x02, 0x0b, 0x13, 0x0a, 0x01, 0x00, 0x09,
0x12, 0x1b, 0x23, 0x1a, 0x11, 0x08, 0x07, 0x06,
0x0f, 0x10, 0x19, 0x22, 0x2b, 0x33, 0x2a, 0x21,
0x18, 0x17, 0x0e, 0x05, 0x04, 0x0d, 0x16, 0x1f,
0x20, 0x29, 0x32, 0x3b, 0x3a, 0x31, 0x28, 0x27,
0x1e, 0x15, 0x0e, 0x14, 0x10, 0x26, 0x2f, 0x30,
0x39, 0x38, 0x37, 0x2e, 0x25, 0x1c, 0x24, 0x2b,
0x36, 0x3f, 0x3e, 0x35, 0x2c, 0x34, 0x3d, 0x3c
};
#define QTBL_SIZE (ALIGN(JPU_JPEG_QTBL_SIZE, \
sizeof(unsigned int)) / sizeof(unsigned int))
#define HDCTBL_SIZE (ALIGN(JPU_JPEG_HDCTBL_SIZE, \
sizeof(unsigned int)) / sizeof(unsigned int))
#define HACTBL_SIZE (ALIGN(JPU_JPEG_HACTBL_SIZE, \
sizeof(unsigned int)) / sizeof(unsigned int))
/*
* Start of image; Quantization tables
* SOF0 (17 bytes payload) is Baseline DCT - Sample precision, height, width,
* Number of image components, (Ci:8 - Hi:4 - Vi:4 - Tq:8) * 3 - Y,Cb,Cr;
* Huffman tables; Padding with 0xff (33.3.27 R01UH0501EJ0100 Rev.1.00)
*/
#define JPU_JPEG_HDR_BLOB { \
0xff, SOI, 0xff, DQT, 0x00, JPU_JPEG_QTBL_SIZE + 0x3, JPU_JPEG_LUM, \
[JPU_JPEG_QTBL_LUM_OFFSET ... \
JPU_JPEG_QTBL_LUM_OFFSET + JPU_JPEG_QTBL_SIZE - 1] = 0x00, \
0xff, DQT, 0x00, JPU_JPEG_QTBL_SIZE + 0x3, JPU_JPEG_CHR, \
[JPU_JPEG_QTBL_CHR_OFFSET ... JPU_JPEG_QTBL_CHR_OFFSET + \
JPU_JPEG_QTBL_SIZE - 1] = 0x00, 0xff, SOF0, 0x00, 0x11, 0x08, \
[JPU_JPEG_HEIGHT_OFFSET ... JPU_JPEG_HEIGHT_OFFSET + 1] = 0x00, \
[JPU_JPEG_WIDTH_OFFSET ... JPU_JPEG_WIDTH_OFFSET + 1] = 0x00, \
0x03, 0x01, [JPU_JPEG_SUBS_OFFSET] = 0x00, JPU_JPEG_LUM, \
0x02, 0x11, JPU_JPEG_CHR, 0x03, 0x11, JPU_JPEG_CHR, \
0xff, DHT, 0x00, JPU_JPEG_HDCTBL_SIZE + 0x3, JPU_JPEG_LUM|JPU_JPEG_DC, \
[JPU_JPEG_HDCTBL_LUM_OFFSET ... \
JPU_JPEG_HDCTBL_LUM_OFFSET + JPU_JPEG_HDCTBL_SIZE - 1] = 0x00, \
0xff, DHT, 0x00, JPU_JPEG_HACTBL_SIZE + 0x3, JPU_JPEG_LUM|JPU_JPEG_AC, \
[JPU_JPEG_HACTBL_LUM_OFFSET ... \
JPU_JPEG_HACTBL_LUM_OFFSET + JPU_JPEG_HACTBL_SIZE - 1] = 0x00, \
0xff, DHT, 0x00, JPU_JPEG_HDCTBL_SIZE + 0x3, JPU_JPEG_CHR|JPU_JPEG_DC, \
[JPU_JPEG_HDCTBL_CHR_OFFSET ... \
JPU_JPEG_HDCTBL_CHR_OFFSET + JPU_JPEG_HDCTBL_SIZE - 1] = 0x00, \
0xff, DHT, 0x00, JPU_JPEG_HACTBL_SIZE + 0x3, JPU_JPEG_CHR|JPU_JPEG_AC, \
[JPU_JPEG_HACTBL_CHR_OFFSET ... \
JPU_JPEG_HACTBL_CHR_OFFSET + JPU_JPEG_HACTBL_SIZE - 1] = 0x00, \
[JPU_JPEG_PADDING_OFFSET ... JPU_JPEG_HDR_SIZE - 1] = 0xff \
}
static unsigned char jpeg_hdrs[JPU_MAX_QUALITY][JPU_JPEG_HDR_SIZE] = {
[0 ... JPU_MAX_QUALITY - 1] = JPU_JPEG_HDR_BLOB
};
static const unsigned int qtbl_lum[JPU_MAX_QUALITY][QTBL_SIZE] = {
{
0x14101927, 0x322e3e44, 0x10121726, 0x26354144,
0x19171f26, 0x35414444, 0x27262635, 0x41444444,
0x32263541, 0x44444444, 0x2e354144, 0x44444444,
0x3e414444, 0x44444444, 0x44444444, 0x44444444
},
{
0x100b0b10, 0x171b1f1e, 0x0b0c0c0f, 0x1417171e,
0x0b0c0d10, 0x171a232f, 0x100f1017, 0x1a252f40,
0x1714171a, 0x27334040, 0x1b171a25, 0x33404040,
0x1f17232f, 0x40404040, 0x1e1e2f40, 0x40404040
},
{
0x0c08080c, 0x11151817, 0x0809090b, 0x0f131217,
0x08090a0c, 0x13141b24, 0x0c0b0c15, 0x141c2435,
0x110f1314, 0x1e27333b, 0x1513141c, 0x27333b3b,
0x18121b24, 0x333b3b3b, 0x17172435, 0x3b3b3b3b
},
{
0x08060608, 0x0c0e1011, 0x06060608, 0x0a0d0c0f,
0x06060708, 0x0d0e1218, 0x0808080e, 0x0d131823,
0x0c0a0d0d, 0x141a2227, 0x0e0d0e13, 0x1a222727,
0x100c1318, 0x22272727, 0x110f1823, 0x27272727
}
};
static const unsigned int qtbl_chr[JPU_MAX_QUALITY][QTBL_SIZE] = {
{
0x15192026, 0x36444444, 0x191c1826, 0x36444444,
0x2018202b, 0x42444444, 0x26262b35, 0x44444444,
0x36424444, 0x44444444, 0x44444444, 0x44444444,
0x44444444, 0x44444444, 0x44444444, 0x44444444
},
{
0x110f1115, 0x141a2630, 0x0f131211, 0x141a232b,
0x11121416, 0x1a1e2e35, 0x1511161c, 0x1e273540,
0x14141a1e, 0x27304040, 0x1a1a1e27, 0x303f4040,
0x26232e35, 0x40404040, 0x302b3540, 0x40404040
},
{
0x0d0b0d10, 0x14141d25, 0x0b0e0e0e, 0x10141a20,
0x0d0e0f11, 0x14172328, 0x100e1115, 0x171e2832,
0x14101417, 0x1e25323b, 0x1414171e, 0x25303b3b,
0x1d1a2328, 0x323b3b3b, 0x25202832, 0x3b3b3b3b
},
{
0x0908090b, 0x0e111318, 0x080a090b, 0x0e0d1116,
0x09090d0e, 0x0d0f171a, 0x0b0b0e0e, 0x0f141a21,
0x0e0e0d0f, 0x14182127, 0x110d0f14, 0x18202727,
0x1311171a, 0x21272727, 0x18161a21, 0x27272727
}
};
static const unsigned int hdctbl_lum[HDCTBL_SIZE] = {
0x00010501, 0x01010101, 0x01000000, 0x00000000,
0x00010203, 0x04050607, 0x08090a0b
};
static const unsigned int hdctbl_chr[HDCTBL_SIZE] = {
0x00010501, 0x01010101, 0x01000000, 0x00000000,
0x00010203, 0x04050607, 0x08090a0b
};
static const unsigned int hactbl_lum[HACTBL_SIZE] = {
0x00020103, 0x03020403, 0x05050404, 0x0000017d, 0x01020300, 0x04110512,
0x21314106, 0x13516107, 0x22711432, 0x8191a108, 0x2342b1c1, 0x1552d1f0,
0x24336272, 0x82090a16, 0x1718191a, 0x25262728, 0x292a3435, 0x36373839,
0x3a434445, 0x46474849, 0x4a535455, 0x56575859, 0x5a636465, 0x66676869,
0x6a737475, 0x76777879, 0x7a838485, 0x86878889, 0x8a929394, 0x95969798,
0x999aa2a3, 0xa4a5a6a7, 0xa8a9aab2, 0xb3b4b5b6, 0xb7b8b9ba, 0xc2c3c4c5,
0xc6c7c8c9, 0xcad2d3d4, 0xd5d6d7d8, 0xd9dae1e2, 0xe3e4e5e6, 0xe7e8e9ea,
0xf1f2f3f4, 0xf5f6f7f8, 0xf9fa0000
};
static const unsigned int hactbl_chr[HACTBL_SIZE] = {
0x00020103, 0x03020403, 0x05050404, 0x0000017d, 0x01020300, 0x04110512,
0x21314106, 0x13516107, 0x22711432, 0x8191a108, 0x2342b1c1, 0x1552d1f0,
0x24336272, 0x82090a16, 0x1718191a, 0x25262728, 0x292a3435, 0x36373839,
0x3a434445, 0x46474849, 0x4a535455, 0x56575859, 0x5a636465, 0x66676869,
0x6a737475, 0x76777879, 0x7a838485, 0x86878889, 0x8a929394, 0x95969798,
0x999aa2a3, 0xa4a5a6a7, 0xa8a9aab2, 0xb3b4b5b6, 0xb7b8b9ba, 0xc2c3c4c5,
0xc6c7c8c9, 0xcad2d3d4, 0xd5d6d7d8, 0xd9dae1e2, 0xe3e4e5e6, 0xe7e8e9ea,
0xf1f2f3f4, 0xf5f6f7f8, 0xf9fa0000
};
static const char *error_to_text[16] = {
"Normal",
"SOI not detected",
"SOF1 to SOFF detected",
"Subsampling not detected",
"SOF accuracy error",
"DQT accuracy error",
"Component error 1",
"Component error 2",
"SOF0, DQT, and DHT not detected when SOS detected",
"SOS not detected",
"EOI not detected",
"Restart interval data number error detected",
"Image size error",
"Last MCU data number error",
"Block data number error",
"Unknown"
};
static struct jpu_buffer *vb2_to_jpu_buffer(struct vb2_v4l2_buffer *vb)
{
struct v4l2_m2m_buffer *b =
container_of(vb, struct v4l2_m2m_buffer, vb);
return container_of(b, struct jpu_buffer, buf);
}
static u32 jpu_read(struct jpu *jpu, unsigned int reg)
{
return ioread32(jpu->regs + reg);
}
static void jpu_write(struct jpu *jpu, u32 val, unsigned int reg)
{
iowrite32(val, jpu->regs + reg);
}
static struct jpu_ctx *ctrl_to_ctx(struct v4l2_ctrl *c)
{
return container_of(c->handler, struct jpu_ctx, ctrl_handler);
}
static struct jpu_ctx *fh_to_ctx(struct v4l2_fh *fh)
{
return container_of(fh, struct jpu_ctx, fh);
}
static void jpu_set_tbl(struct jpu *jpu, u32 reg, const unsigned int *tbl,
unsigned int len) {
unsigned int i;
for (i = 0; i < len; i++)
jpu_write(jpu, tbl[i], reg + (i << 2));
}
static void jpu_set_qtbl(struct jpu *jpu, unsigned short quality)
{
jpu_set_tbl(jpu, JCQTBL(0), qtbl_lum[quality], QTBL_SIZE);
jpu_set_tbl(jpu, JCQTBL(1), qtbl_chr[quality], QTBL_SIZE);
}
static void jpu_set_htbl(struct jpu *jpu)
{
jpu_set_tbl(jpu, JCHTBD(0), hdctbl_lum, HDCTBL_SIZE);
jpu_set_tbl(jpu, JCHTBA(0), hactbl_lum, HACTBL_SIZE);
jpu_set_tbl(jpu, JCHTBD(1), hdctbl_chr, HDCTBL_SIZE);
jpu_set_tbl(jpu, JCHTBA(1), hactbl_chr, HACTBL_SIZE);
}
static int jpu_wait_reset(struct jpu *jpu)
{
unsigned long timeout;
timeout = jiffies + msecs_to_jiffies(JPU_RESET_TIMEOUT);
while (jpu_read(jpu, JCCMD) & JCCMD_SRST) {
if (time_after(jiffies, timeout)) {
dev_err(jpu->dev, "timed out in reset\n");
return -ETIMEDOUT;
}
schedule();
}
return 0;
}
static int jpu_reset(struct jpu *jpu)
{
jpu_write(jpu, JCCMD_SRST, JCCMD);
return jpu_wait_reset(jpu);
}
/*
* ============================================================================
* video ioctl operations
* ============================================================================
*/
static void put_qtbl(u8 *p, const u8 *qtbl)
{
unsigned int i;
for (i = 0; i < ARRAY_SIZE(zigzag); i++)
p[i] = *(qtbl + zigzag[i]);
}
static void put_htbl(u8 *p, const u8 *htbl, unsigned int len)
{
unsigned int i, j;
for (i = 0; i < len; i += 4)
for (j = 0; j < 4 && (i + j) < len; ++j)
p[i + j] = htbl[i + 3 - j];
}
static void jpu_generate_hdr(unsigned short quality, unsigned char *p)
{
put_qtbl(p + JPU_JPEG_QTBL_LUM_OFFSET, (const u8 *)qtbl_lum[quality]);
put_qtbl(p + JPU_JPEG_QTBL_CHR_OFFSET, (const u8 *)qtbl_chr[quality]);
put_htbl(p + JPU_JPEG_HDCTBL_LUM_OFFSET, (const u8 *)hdctbl_lum,
JPU_JPEG_HDCTBL_SIZE);
put_htbl(p + JPU_JPEG_HACTBL_LUM_OFFSET, (const u8 *)hactbl_lum,
JPU_JPEG_HACTBL_SIZE);
put_htbl(p + JPU_JPEG_HDCTBL_CHR_OFFSET, (const u8 *)hdctbl_chr,
JPU_JPEG_HDCTBL_SIZE);
put_htbl(p + JPU_JPEG_HACTBL_CHR_OFFSET, (const u8 *)hactbl_chr,
JPU_JPEG_HACTBL_SIZE);
}
static int get_byte(struct jpeg_buffer *buf)
{
if (buf->curr >= buf->end)
return -1;
return *(u8 *)buf->curr++;
}
static int get_word_be(struct jpeg_buffer *buf, unsigned int *word)
{
if (buf->end - buf->curr < 2)
return -1;
*word = get_unaligned_be16(buf->curr);
buf->curr += 2;
return 0;
}
static void skip(struct jpeg_buffer *buf, unsigned long len)
{
buf->curr += min((unsigned long)(buf->end - buf->curr), len);
}
static u8 jpu_parse_hdr(void *buffer, unsigned long size, unsigned int *width,
unsigned int *height)
{
struct jpeg_buffer jpeg_buffer;
unsigned int word;
bool soi = false;
jpeg_buffer.end = buffer + size;
jpeg_buffer.curr = buffer;
/*
* basic size check and EOI - we don't want to let JPU cross
* buffer bounds in any case. Hope it's stopping by EOI.
*/
if (size < JPU_JPEG_MIN_SIZE || *(u8 *)(buffer + size - 1) != EOI)
return 0;
for (;;) {
int c;
/* skip preceding filler bytes */
do
c = get_byte(&jpeg_buffer);
while (c == 0xff || c == 0);
if (!soi && c == SOI) {
soi = true;
continue;
} else if (soi != (c != SOI))
return 0;
switch (c) {
case SOF0: /* SOF0: baseline JPEG */
skip(&jpeg_buffer, 3); /* segment length and bpp */
if (get_word_be(&jpeg_buffer, height) ||
get_word_be(&jpeg_buffer, width) ||
get_byte(&jpeg_buffer) != 3) /* YCbCr only */
return 0;
skip(&jpeg_buffer, 1);
return get_byte(&jpeg_buffer);
case DHT:
case DQT:
case COM:
case DRI:
case APP0 ... APP0 + 0x0f:
if (get_word_be(&jpeg_buffer, &word))
return 0;
skip(&jpeg_buffer, (long)word - 2);
break;
case 0:
break;
default:
return 0;
}
}
return 0;
}
static int jpu_querycap(struct file *file, void *priv,
struct v4l2_capability *cap)
{
struct jpu_ctx *ctx = fh_to_ctx(priv);
if (ctx->encoder)
strscpy(cap->card, DRV_NAME " encoder", sizeof(cap->card));
else
strscpy(cap->card, DRV_NAME " decoder", sizeof(cap->card));
strscpy(cap->driver, DRV_NAME, sizeof(cap->driver));
snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
dev_name(ctx->jpu->dev));
memset(cap->reserved, 0, sizeof(cap->reserved));
return 0;
}
static struct jpu_fmt *jpu_find_format(bool encoder, u32 pixelformat,
unsigned int fmt_type)
{
unsigned int i, fmt_flag;
if (encoder)
fmt_flag = fmt_type == JPU_FMT_TYPE_OUTPUT ? JPU_ENC_OUTPUT :
JPU_ENC_CAPTURE;
else
fmt_flag = fmt_type == JPU_FMT_TYPE_OUTPUT ? JPU_DEC_OUTPUT :
JPU_DEC_CAPTURE;
for (i = 0; i < ARRAY_SIZE(jpu_formats); i++) {
struct jpu_fmt *fmt = &jpu_formats[i];
if (fmt->fourcc == pixelformat && fmt->types & fmt_flag)
return fmt;
}
return NULL;
}
static int jpu_enum_fmt(struct v4l2_fmtdesc *f, u32 type)
{
unsigned int i, num = 0;
for (i = 0; i < ARRAY_SIZE(jpu_formats); ++i) {
if (jpu_formats[i].types & type) {
if (num == f->index)
break;
++num;
}
}
if (i >= ARRAY_SIZE(jpu_formats))
return -EINVAL;
f->pixelformat = jpu_formats[i].fourcc;
return 0;
}
static int jpu_enum_fmt_cap(struct file *file, void *priv,
struct v4l2_fmtdesc *f)
{
struct jpu_ctx *ctx = fh_to_ctx(priv);
return jpu_enum_fmt(f, ctx->encoder ? JPU_ENC_CAPTURE :
JPU_DEC_CAPTURE);
}
static int jpu_enum_fmt_out(struct file *file, void *priv,
struct v4l2_fmtdesc *f)
{
struct jpu_ctx *ctx = fh_to_ctx(priv);
return jpu_enum_fmt(f, ctx->encoder ? JPU_ENC_OUTPUT : JPU_DEC_OUTPUT);
}
static struct jpu_q_data *jpu_get_q_data(struct jpu_ctx *ctx,
enum v4l2_buf_type type)
{
if (V4L2_TYPE_IS_OUTPUT(type))
return &ctx->out_q;
else
return &ctx->cap_q;
}
static void jpu_bound_align_image(u32 *w, unsigned int w_min,
unsigned int w_max, unsigned int w_align,
u32 *h, unsigned int h_min,
unsigned int h_max, unsigned int h_align)
{
unsigned int width, height, w_step, h_step;
width = *w;
height = *h;
w_step = 1U << w_align;
h_step = 1U << h_align;
v4l_bound_align_image(w, w_min, w_max, w_align, h, h_min, h_max,
h_align, 3);
if (*w < width && *w + w_step < w_max)
*w += w_step;
if (*h < height && *h + h_step < h_max)
*h += h_step;
}
static int __jpu_try_fmt(struct jpu_ctx *ctx, struct jpu_fmt **fmtinfo,
struct v4l2_pix_format_mplane *pix,
enum v4l2_buf_type type)
{
struct jpu_fmt *fmt;
unsigned int f_type, w, h;
f_type = V4L2_TYPE_IS_OUTPUT(type) ? JPU_FMT_TYPE_OUTPUT :
JPU_FMT_TYPE_CAPTURE;
fmt = jpu_find_format(ctx->encoder, pix->pixelformat, f_type);
if (!fmt) {
unsigned int pixelformat;
dev_dbg(ctx->jpu->dev, "unknown format; set default format\n");
if (ctx->encoder)
pixelformat = f_type == JPU_FMT_TYPE_OUTPUT ?
V4L2_PIX_FMT_NV16M : V4L2_PIX_FMT_JPEG;
else
pixelformat = f_type == JPU_FMT_TYPE_CAPTURE ?
V4L2_PIX_FMT_NV16M : V4L2_PIX_FMT_JPEG;
fmt = jpu_find_format(ctx->encoder, pixelformat, f_type);
}
pix->pixelformat = fmt->fourcc;
pix->colorspace = fmt->colorspace;
pix->field = V4L2_FIELD_NONE;
pix->num_planes = fmt->num_planes;
jpu_bound_align_image(&pix->width, JPU_WIDTH_MIN, JPU_WIDTH_MAX,
fmt->h_align, &pix->height, JPU_HEIGHT_MIN,
JPU_HEIGHT_MAX, fmt->v_align);
w = pix->width;
h = pix->height;
if (fmt->fourcc == V4L2_PIX_FMT_JPEG) {
/* ignore userspaces's sizeimage for encoding */
if (pix->plane_fmt[0].sizeimage <= 0 || ctx->encoder)
pix->plane_fmt[0].sizeimage = JPU_JPEG_HDR_SIZE +
(JPU_JPEG_MAX_BYTES_PER_PIXEL * w * h);
pix->plane_fmt[0].bytesperline = 0;
} else {
unsigned int i, bpl = 0;
for (i = 0; i < pix->num_planes; ++i)
bpl = max(bpl, pix->plane_fmt[i].bytesperline);
bpl = clamp_t(unsigned int, bpl, w, JPU_WIDTH_MAX);
bpl = round_up(bpl, JPU_MEMALIGN);
for (i = 0; i < pix->num_planes; ++i) {
pix->plane_fmt[i].bytesperline = bpl;
pix->plane_fmt[i].sizeimage = bpl * h * fmt->bpp[i] / 8;
}
}
if (fmtinfo)
*fmtinfo = fmt;
return 0;
}
static int jpu_try_fmt(struct file *file, void *priv, struct v4l2_format *f)
{
struct jpu_ctx *ctx = fh_to_ctx(priv);
if (!v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type))
return -EINVAL;
return __jpu_try_fmt(ctx, NULL, &f->fmt.pix_mp, f->type);
}
static int jpu_s_fmt(struct file *file, void *priv, struct v4l2_format *f)
{
struct vb2_queue *vq;
struct jpu_ctx *ctx = fh_to_ctx(priv);
struct v4l2_m2m_ctx *m2m_ctx = ctx->fh.m2m_ctx;
struct jpu_fmt *fmtinfo;
struct jpu_q_data *q_data;
int ret;
vq = v4l2_m2m_get_vq(m2m_ctx, f->type);
if (!vq)
return -EINVAL;
if (vb2_is_busy(vq)) {
v4l2_err(&ctx->jpu->v4l2_dev, "%s queue busy\n", __func__);
return -EBUSY;
}
ret = __jpu_try_fmt(ctx, &fmtinfo, &f->fmt.pix_mp, f->type);
if (ret < 0)
return ret;
q_data = jpu_get_q_data(ctx, f->type);
q_data->format = f->fmt.pix_mp;
q_data->fmtinfo = fmtinfo;
return 0;
}
static int jpu_g_fmt(struct file *file, void *priv, struct v4l2_format *f)
{
struct jpu_q_data *q_data;
struct jpu_ctx *ctx = fh_to_ctx(priv);
if (!v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type))
return -EINVAL;
q_data = jpu_get_q_data(ctx, f->type);
f->fmt.pix_mp = q_data->format;
return 0;
}
/*
* V4L2 controls
*/
static int jpu_s_ctrl(struct v4l2_ctrl *ctrl)
{
struct jpu_ctx *ctx = ctrl_to_ctx(ctrl);
unsigned long flags;
spin_lock_irqsave(&ctx->jpu->lock, flags);
if (ctrl->id == V4L2_CID_JPEG_COMPRESSION_QUALITY)
ctx->compr_quality = ctrl->val;
spin_unlock_irqrestore(&ctx->jpu->lock, flags);
return 0;
}
static const struct v4l2_ctrl_ops jpu_ctrl_ops = {
.s_ctrl = jpu_s_ctrl,
};
static int jpu_streamon(struct file *file, void *priv, enum v4l2_buf_type type)
{
struct jpu_ctx *ctx = fh_to_ctx(priv);
struct jpu_q_data *src_q_data, *dst_q_data, *orig, adj, *ref;
enum v4l2_buf_type adj_type;
src_q_data = jpu_get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
dst_q_data = jpu_get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
if (ctx->encoder) {
adj = *src_q_data;
orig = src_q_data;
ref = dst_q_data;
adj_type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
} else {
adj = *dst_q_data;
orig = dst_q_data;
ref = src_q_data;
adj_type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
}
adj.format.width = ref->format.width;
adj.format.height = ref->format.height;
__jpu_try_fmt(ctx, NULL, &adj.format, adj_type);
if (adj.format.width != orig->format.width ||
adj.format.height != orig->format.height) {
dev_err(ctx->jpu->dev, "src and dst formats do not match.\n");
/* maybe we can return -EPIPE here? */
return -EINVAL;
}
return v4l2_m2m_streamon(file, ctx->fh.m2m_ctx, type);
}
static const struct v4l2_ioctl_ops jpu_ioctl_ops = {
.vidioc_querycap = jpu_querycap,
.vidioc_enum_fmt_vid_cap = jpu_enum_fmt_cap,
.vidioc_enum_fmt_vid_out = jpu_enum_fmt_out,
.vidioc_g_fmt_vid_cap_mplane = jpu_g_fmt,
.vidioc_g_fmt_vid_out_mplane = jpu_g_fmt,
.vidioc_try_fmt_vid_cap_mplane = jpu_try_fmt,
.vidioc_try_fmt_vid_out_mplane = jpu_try_fmt,
.vidioc_s_fmt_vid_cap_mplane = jpu_s_fmt,
.vidioc_s_fmt_vid_out_mplane = jpu_s_fmt,
.vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
.vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
.vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
.vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
.vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
.vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
.vidioc_streamon = jpu_streamon,
.vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
.vidioc_unsubscribe_event = v4l2_event_unsubscribe
};
static int jpu_controls_create(struct jpu_ctx *ctx)
{
struct v4l2_ctrl *ctrl;
int ret;
v4l2_ctrl_handler_init(&ctx->ctrl_handler, 1);
ctrl = v4l2_ctrl_new_std(&ctx->ctrl_handler, &jpu_ctrl_ops,
V4L2_CID_JPEG_COMPRESSION_QUALITY,
0, JPU_MAX_QUALITY - 1, 1, 0);
if (ctx->ctrl_handler.error) {
ret = ctx->ctrl_handler.error;
goto error_free;
}
if (!ctx->encoder)
ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE |
V4L2_CTRL_FLAG_READ_ONLY;
ret = v4l2_ctrl_handler_setup(&ctx->ctrl_handler);
if (ret < 0)
goto error_free;
return 0;
error_free:
v4l2_ctrl_handler_free(&ctx->ctrl_handler);
return ret;
}
/*
* ============================================================================