forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenesas-ceu.c
1749 lines (1444 loc) · 43.7 KB
/
renesas-ceu.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
/*
* V4L2 Driver for Renesas Capture Engine Unit (CEU) interface
* Copyright (C) 2017-2018 Jacopo Mondi <[email protected]>
*
* Based on soc-camera driver "soc_camera/sh_mobile_ceu_camera.c"
* Copyright (C) 2008 Magnus Damm
*
* Based on V4L2 Driver for PXA camera host - "pxa_camera.c",
* Copyright (C) 2006, Sascha Hauer, Pengutronix
* Copyright (C) 2008, Guennadi Liakhovetski <[email protected]>
*/
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/dma-mapping.h>
#include <linux/err.h>
#include <linux/errno.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/kernel.h>
#include <linux/mm.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/of_graph.h>
#include <linux/platform_device.h>
#include <linux/pm_runtime.h>
#include <linux/slab.h>
#include <linux/time.h>
#include <linux/videodev2.h>
#include <media/v4l2-async.h>
#include <media/v4l2-common.h>
#include <media/v4l2-ctrls.h>
#include <media/v4l2-dev.h>
#include <media/v4l2-device.h>
#include <media/v4l2-event.h>
#include <media/v4l2-fwnode.h>
#include <media/v4l2-image-sizes.h>
#include <media/v4l2-ioctl.h>
#include <media/v4l2-mediabus.h>
#include <media/videobuf2-dma-contig.h>
#include <media/drv-intf/renesas-ceu.h>
#define DRIVER_NAME "renesas-ceu"
/* CEU registers offsets and masks. */
#define CEU_CAPSR 0x00 /* Capture start register */
#define CEU_CAPCR 0x04 /* Capture control register */
#define CEU_CAMCR 0x08 /* Capture interface control register */
#define CEU_CAMOR 0x10 /* Capture interface offset register */
#define CEU_CAPWR 0x14 /* Capture interface width register */
#define CEU_CAIFR 0x18 /* Capture interface input format register */
#define CEU_CRCNTR 0x28 /* CEU register control register */
#define CEU_CRCMPR 0x2c /* CEU register forcible control register */
#define CEU_CFLCR 0x30 /* Capture filter control register */
#define CEU_CFSZR 0x34 /* Capture filter size clip register */
#define CEU_CDWDR 0x38 /* Capture destination width register */
#define CEU_CDAYR 0x3c /* Capture data address Y register */
#define CEU_CDACR 0x40 /* Capture data address C register */
#define CEU_CFWCR 0x5c /* Firewall operation control register */
#define CEU_CDOCR 0x64 /* Capture data output control register */
#define CEU_CEIER 0x70 /* Capture event interrupt enable register */
#define CEU_CETCR 0x74 /* Capture event flag clear register */
#define CEU_CSTSR 0x7c /* Capture status register */
#define CEU_CSRTR 0x80 /* Capture software reset register */
/* Data synchronous fetch mode. */
#define CEU_CAMCR_JPEG BIT(4)
/* Input components ordering: CEU_CAMCR.DTARY field. */
#define CEU_CAMCR_DTARY_8_UYVY (0x00 << 8)
#define CEU_CAMCR_DTARY_8_VYUY (0x01 << 8)
#define CEU_CAMCR_DTARY_8_YUYV (0x02 << 8)
#define CEU_CAMCR_DTARY_8_YVYU (0x03 << 8)
/* TODO: input components ordering for 16 bits input. */
/* Bus transfer MTU. */
#define CEU_CAPCR_BUS_WIDTH256 (0x3 << 20)
/* Bus width configuration. */
#define CEU_CAMCR_DTIF_16BITS BIT(12)
/* No downsampling to planar YUV420 in image fetch mode. */
#define CEU_CDOCR_NO_DOWSAMPLE BIT(4)
/* Swap all input data in 8-bit, 16-bits and 32-bits units (Figure 46.45). */
#define CEU_CDOCR_SWAP_ENDIANNESS (7)
/* Capture reset and enable bits. */
#define CEU_CAPSR_CPKIL BIT(16)
#define CEU_CAPSR_CE BIT(0)
/* CEU operating flag bit. */
#define CEU_CAPCR_CTNCP BIT(16)
#define CEU_CSTRST_CPTON BIT(0)
/* Platform specific IRQ source flags. */
#define CEU_CETCR_ALL_IRQS_RZ 0x397f313
#define CEU_CETCR_ALL_IRQS_SH4 0x3d7f313
/* Prohibited register access interrupt bit. */
#define CEU_CETCR_IGRW BIT(4)
/* One-frame capture end interrupt. */
#define CEU_CEIER_CPE BIT(0)
/* VBP error. */
#define CEU_CEIER_VBP BIT(20)
#define CEU_CEIER_MASK (CEU_CEIER_CPE | CEU_CEIER_VBP)
#define CEU_MAX_WIDTH 2560
#define CEU_MAX_HEIGHT 1920
#define CEU_MAX_BPL 8188
#define CEU_W_MAX(w) ((w) < CEU_MAX_WIDTH ? (w) : CEU_MAX_WIDTH)
#define CEU_H_MAX(h) ((h) < CEU_MAX_HEIGHT ? (h) : CEU_MAX_HEIGHT)
/*
* ceu_bus_fmt - describe a 8-bits yuyv format the sensor can produce
*
* @mbus_code: bus format code
* @fmt_order: CEU_CAMCR.DTARY ordering of input components (Y, Cb, Cr)
* @fmt_order_swap: swapped CEU_CAMCR.DTARY ordering of input components
* (Y, Cr, Cb)
* @swapped: does Cr appear before Cb?
* @bps: number of bits sent over bus for each sample
* @bpp: number of bits per pixels unit
*/
struct ceu_mbus_fmt {
u32 mbus_code;
u32 fmt_order;
u32 fmt_order_swap;
bool swapped;
u8 bps;
u8 bpp;
};
/*
* ceu_buffer - Link vb2 buffer to the list of available buffers.
*/
struct ceu_buffer {
struct vb2_v4l2_buffer vb;
struct list_head queue;
};
static inline struct ceu_buffer *vb2_to_ceu(struct vb2_v4l2_buffer *vbuf)
{
return container_of(vbuf, struct ceu_buffer, vb);
}
/*
* ceu_subdev - Wraps v4l2 sub-device and provides async subdevice.
*/
struct ceu_subdev {
struct v4l2_async_subdev asd;
struct v4l2_subdev *v4l2_sd;
/* per-subdevice mbus configuration options */
unsigned int mbus_flags;
struct ceu_mbus_fmt mbus_fmt;
};
static struct ceu_subdev *to_ceu_subdev(struct v4l2_async_subdev *asd)
{
return container_of(asd, struct ceu_subdev, asd);
}
/*
* ceu_device - CEU device instance
*/
struct ceu_device {
struct device *dev;
struct video_device vdev;
struct v4l2_device v4l2_dev;
/* subdevices descriptors */
struct ceu_subdev **subdevs;
/* the subdevice currently in use */
struct ceu_subdev *sd;
unsigned int sd_index;
unsigned int num_sd;
/* platform specific mask with all IRQ sources flagged */
u32 irq_mask;
/* currently configured field and pixel format */
enum v4l2_field field;
struct v4l2_pix_format_mplane v4l2_pix;
/* async subdev notification helpers */
struct v4l2_async_notifier notifier;
/* vb2 queue, capture buffer list and active buffer pointer */
struct vb2_queue vb2_vq;
struct list_head capture;
struct vb2_v4l2_buffer *active;
unsigned int sequence;
/* mlock - lock access to interface reset and vb2 queue */
struct mutex mlock;
/* lock - lock access to capture buffer queue and active buffer */
spinlock_t lock;
/* base - CEU memory base address */
void __iomem *base;
};
static inline struct ceu_device *v4l2_to_ceu(struct v4l2_device *v4l2_dev)
{
return container_of(v4l2_dev, struct ceu_device, v4l2_dev);
}
/* --- CEU memory output formats --- */
/*
* ceu_fmt - describe a memory output format supported by CEU interface.
*
* @fourcc: memory layout fourcc format code
* @bpp: number of bits for each pixel stored in memory
*/
struct ceu_fmt {
u32 fourcc;
u32 bpp;
};
/*
* ceu_format_list - List of supported memory output formats
*
* If sensor provides any YUYV bus format, all the following planar memory
* formats are available thanks to CEU re-ordering and sub-sampling
* capabilities.
*/
static const struct ceu_fmt ceu_fmt_list[] = {
{
.fourcc = V4L2_PIX_FMT_NV16,
.bpp = 16,
},
{
.fourcc = V4L2_PIX_FMT_NV61,
.bpp = 16,
},
{
.fourcc = V4L2_PIX_FMT_NV12,
.bpp = 12,
},
{
.fourcc = V4L2_PIX_FMT_NV21,
.bpp = 12,
},
{
.fourcc = V4L2_PIX_FMT_YUYV,
.bpp = 16,
},
{
.fourcc = V4L2_PIX_FMT_UYVY,
.bpp = 16,
},
{
.fourcc = V4L2_PIX_FMT_YVYU,
.bpp = 16,
},
{
.fourcc = V4L2_PIX_FMT_VYUY,
.bpp = 16,
},
};
static const struct ceu_fmt *get_ceu_fmt_from_fourcc(unsigned int fourcc)
{
const struct ceu_fmt *fmt = &ceu_fmt_list[0];
unsigned int i;
for (i = 0; i < ARRAY_SIZE(ceu_fmt_list); i++, fmt++)
if (fmt->fourcc == fourcc)
return fmt;
return NULL;
}
static bool ceu_fmt_mplane(struct v4l2_pix_format_mplane *pix)
{
switch (pix->pixelformat) {
case V4L2_PIX_FMT_YUYV:
case V4L2_PIX_FMT_UYVY:
case V4L2_PIX_FMT_YVYU:
case V4L2_PIX_FMT_VYUY:
return false;
case V4L2_PIX_FMT_NV16:
case V4L2_PIX_FMT_NV61:
case V4L2_PIX_FMT_NV12:
case V4L2_PIX_FMT_NV21:
return true;
default:
return false;
}
}
/* --- CEU HW operations --- */
static void ceu_write(struct ceu_device *priv, unsigned int reg_offs, u32 data)
{
iowrite32(data, priv->base + reg_offs);
}
static u32 ceu_read(struct ceu_device *priv, unsigned int reg_offs)
{
return ioread32(priv->base + reg_offs);
}
/*
* ceu_soft_reset() - Software reset the CEU interface.
* @ceu_device: CEU device.
*
* Returns 0 for success, -EIO for error.
*/
static int ceu_soft_reset(struct ceu_device *ceudev)
{
unsigned int i;
ceu_write(ceudev, CEU_CAPSR, CEU_CAPSR_CPKIL);
for (i = 0; i < 100; i++) {
if (!(ceu_read(ceudev, CEU_CSTSR) & CEU_CSTRST_CPTON))
break;
udelay(1);
}
if (i == 100) {
dev_err(ceudev->dev, "soft reset time out\n");
return -EIO;
}
for (i = 0; i < 100; i++) {
if (!(ceu_read(ceudev, CEU_CAPSR) & CEU_CAPSR_CPKIL))
return 0;
udelay(1);
}
/* If we get here, CEU has not reset properly. */
return -EIO;
}
/* --- CEU Capture Operations --- */
/*
* ceu_hw_config() - Configure CEU interface registers.
*/
static int ceu_hw_config(struct ceu_device *ceudev)
{
u32 camcr, cdocr, cfzsr, cdwdr, capwr;
struct v4l2_pix_format_mplane *pix = &ceudev->v4l2_pix;
struct ceu_subdev *ceu_sd = ceudev->sd;
struct ceu_mbus_fmt *mbus_fmt = &ceu_sd->mbus_fmt;
unsigned int mbus_flags = ceu_sd->mbus_flags;
/* Start configuring CEU registers */
ceu_write(ceudev, CEU_CAIFR, 0);
ceu_write(ceudev, CEU_CFWCR, 0);
ceu_write(ceudev, CEU_CRCNTR, 0);
ceu_write(ceudev, CEU_CRCMPR, 0);
/* Set the frame capture period for both image capture and data sync. */
capwr = (pix->height << 16) | pix->width * mbus_fmt->bpp / 8;
/*
* Swap input data endianness by default.
* In data fetch mode bytes are received in chunks of 8 bytes.
* D0, D1, D2, D3, D4, D5, D6, D7 (D0 received first)
* The data is however by default written to memory in reverse order:
* D7, D6, D5, D4, D3, D2, D1, D0 (D7 written to lowest byte)
*
* Use CEU_CDOCR[2:0] to swap data ordering.
*/
cdocr = CEU_CDOCR_SWAP_ENDIANNESS;
/*
* Configure CAMCR and CDOCR:
* match input components ordering with memory output format and
* handle downsampling to YUV420.
*
* If the memory output planar format is 'swapped' (Cr before Cb) and
* input format is not, use the swapped version of CAMCR.DTARY.
*
* If the memory output planar format is not 'swapped' (Cb before Cr)
* and input format is, use the swapped version of CAMCR.DTARY.
*
* CEU by default downsample to planar YUV420 (CDCOR[4] = 0).
* If output is planar YUV422 set CDOCR[4] = 1
*
* No downsample for data fetch sync mode.
*/
switch (pix->pixelformat) {
/* Data fetch sync mode */
case V4L2_PIX_FMT_YUYV:
case V4L2_PIX_FMT_YVYU:
case V4L2_PIX_FMT_UYVY:
case V4L2_PIX_FMT_VYUY:
camcr = CEU_CAMCR_JPEG;
cdocr |= CEU_CDOCR_NO_DOWSAMPLE;
cfzsr = (pix->height << 16) | pix->width;
cdwdr = pix->plane_fmt[0].bytesperline;
break;
/* Non-swapped planar image capture mode. */
case V4L2_PIX_FMT_NV16:
cdocr |= CEU_CDOCR_NO_DOWSAMPLE;
fallthrough;
case V4L2_PIX_FMT_NV12:
if (mbus_fmt->swapped)
camcr = mbus_fmt->fmt_order_swap;
else
camcr = mbus_fmt->fmt_order;
cfzsr = (pix->height << 16) | pix->width;
cdwdr = pix->width;
break;
/* Swapped planar image capture mode. */
case V4L2_PIX_FMT_NV61:
cdocr |= CEU_CDOCR_NO_DOWSAMPLE;
fallthrough;
case V4L2_PIX_FMT_NV21:
if (mbus_fmt->swapped)
camcr = mbus_fmt->fmt_order;
else
camcr = mbus_fmt->fmt_order_swap;
cfzsr = (pix->height << 16) | pix->width;
cdwdr = pix->width;
break;
default:
return -EINVAL;
}
camcr |= mbus_flags & V4L2_MBUS_VSYNC_ACTIVE_LOW ? 1 << 1 : 0;
camcr |= mbus_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW ? 1 << 0 : 0;
/* TODO: handle 16 bit bus width with DTIF bit in CAMCR */
ceu_write(ceudev, CEU_CAMCR, camcr);
ceu_write(ceudev, CEU_CDOCR, cdocr);
ceu_write(ceudev, CEU_CAPCR, CEU_CAPCR_BUS_WIDTH256);
/*
* TODO: make CAMOR offsets configurable.
* CAMOR wants to know the number of blanks between a VS/HS signal
* and valid data. This value should actually come from the sensor...
*/
ceu_write(ceudev, CEU_CAMOR, 0);
/* TODO: 16 bit bus width require re-calculation of cdwdr and cfzsr */
ceu_write(ceudev, CEU_CAPWR, capwr);
ceu_write(ceudev, CEU_CFSZR, cfzsr);
ceu_write(ceudev, CEU_CDWDR, cdwdr);
return 0;
}
/*
* ceu_capture() - Trigger start of a capture sequence.
*
* Program the CEU DMA registers with addresses where to transfer image data.
*/
static int ceu_capture(struct ceu_device *ceudev)
{
struct v4l2_pix_format_mplane *pix = &ceudev->v4l2_pix;
dma_addr_t phys_addr_top;
phys_addr_top =
vb2_dma_contig_plane_dma_addr(&ceudev->active->vb2_buf, 0);
ceu_write(ceudev, CEU_CDAYR, phys_addr_top);
/* Ignore CbCr plane for non multi-planar image formats. */
if (ceu_fmt_mplane(pix)) {
phys_addr_top =
vb2_dma_contig_plane_dma_addr(&ceudev->active->vb2_buf,
1);
ceu_write(ceudev, CEU_CDACR, phys_addr_top);
}
/*
* Trigger new capture start: once for each frame, as we work in
* one-frame capture mode.
*/
ceu_write(ceudev, CEU_CAPSR, CEU_CAPSR_CE);
return 0;
}
static irqreturn_t ceu_irq(int irq, void *data)
{
struct ceu_device *ceudev = data;
struct vb2_v4l2_buffer *vbuf;
struct ceu_buffer *buf;
u32 status;
/* Clean interrupt status. */
status = ceu_read(ceudev, CEU_CETCR);
ceu_write(ceudev, CEU_CETCR, ~ceudev->irq_mask);
/* Unexpected interrupt. */
if (!(status & CEU_CEIER_MASK))
return IRQ_NONE;
spin_lock(&ceudev->lock);
/* Stale interrupt from a released buffer, ignore it. */
vbuf = ceudev->active;
if (!vbuf) {
spin_unlock(&ceudev->lock);
return IRQ_HANDLED;
}
/*
* When a VBP interrupt occurs, no capture end interrupt will occur
* and the image of that frame is not captured correctly.
*/
if (status & CEU_CEIER_VBP) {
dev_err(ceudev->dev, "VBP interrupt: abort capture\n");
goto error_irq_out;
}
/* Prepare to return the 'previous' buffer. */
vbuf->vb2_buf.timestamp = ktime_get_ns();
vbuf->sequence = ceudev->sequence++;
vbuf->field = ceudev->field;
/* Prepare a new 'active' buffer and trigger a new capture. */
if (!list_empty(&ceudev->capture)) {
buf = list_first_entry(&ceudev->capture, struct ceu_buffer,
queue);
list_del(&buf->queue);
ceudev->active = &buf->vb;
ceu_capture(ceudev);
}
/* Return the 'previous' buffer. */
vb2_buffer_done(&vbuf->vb2_buf, VB2_BUF_STATE_DONE);
spin_unlock(&ceudev->lock);
return IRQ_HANDLED;
error_irq_out:
/* Return the 'previous' buffer and all queued ones. */
vb2_buffer_done(&vbuf->vb2_buf, VB2_BUF_STATE_ERROR);
list_for_each_entry(buf, &ceudev->capture, queue)
vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
spin_unlock(&ceudev->lock);
return IRQ_HANDLED;
}
/* --- CEU Videobuf2 operations --- */
static void ceu_update_plane_sizes(struct v4l2_plane_pix_format *plane,
unsigned int bpl, unsigned int szimage)
{
memset(plane, 0, sizeof(*plane));
plane->sizeimage = szimage;
if (plane->bytesperline < bpl || plane->bytesperline > CEU_MAX_BPL)
plane->bytesperline = bpl;
}
/*
* ceu_calc_plane_sizes() - Fill per-plane 'struct v4l2_plane_pix_format'
* information according to the currently configured
* pixel format.
* @ceu_device: CEU device.
* @ceu_fmt: Active image format.
* @pix: Pixel format information (store line width and image sizes)
*/
static void ceu_calc_plane_sizes(struct ceu_device *ceudev,
const struct ceu_fmt *ceu_fmt,
struct v4l2_pix_format_mplane *pix)
{
unsigned int bpl, szimage;
switch (pix->pixelformat) {
case V4L2_PIX_FMT_YUYV:
case V4L2_PIX_FMT_UYVY:
case V4L2_PIX_FMT_YVYU:
case V4L2_PIX_FMT_VYUY:
pix->num_planes = 1;
bpl = pix->width * ceu_fmt->bpp / 8;
szimage = pix->height * bpl;
ceu_update_plane_sizes(&pix->plane_fmt[0], bpl, szimage);
break;
case V4L2_PIX_FMT_NV12:
case V4L2_PIX_FMT_NV21:
pix->num_planes = 2;
bpl = pix->width;
szimage = pix->height * pix->width;
ceu_update_plane_sizes(&pix->plane_fmt[0], bpl, szimage);
ceu_update_plane_sizes(&pix->plane_fmt[1], bpl, szimage / 2);
break;
case V4L2_PIX_FMT_NV16:
case V4L2_PIX_FMT_NV61:
default:
pix->num_planes = 2;
bpl = pix->width;
szimage = pix->height * pix->width;
ceu_update_plane_sizes(&pix->plane_fmt[0], bpl, szimage);
ceu_update_plane_sizes(&pix->plane_fmt[1], bpl, szimage);
break;
}
}
/*
* ceu_vb2_setup() - is called to check whether the driver can accept the
* requested number of buffers and to fill in plane sizes
* for the current frame format, if required.
*/
static int ceu_vb2_setup(struct vb2_queue *vq, unsigned int *count,
unsigned int *num_planes, unsigned int sizes[],
struct device *alloc_devs[])
{
struct ceu_device *ceudev = vb2_get_drv_priv(vq);
struct v4l2_pix_format_mplane *pix = &ceudev->v4l2_pix;
unsigned int i;
/* num_planes is set: just check plane sizes. */
if (*num_planes) {
for (i = 0; i < pix->num_planes; i++)
if (sizes[i] < pix->plane_fmt[i].sizeimage)
return -EINVAL;
return 0;
}
/* num_planes not set: called from REQBUFS, just set plane sizes. */
*num_planes = pix->num_planes;
for (i = 0; i < pix->num_planes; i++)
sizes[i] = pix->plane_fmt[i].sizeimage;
return 0;
}
static void ceu_vb2_queue(struct vb2_buffer *vb)
{
struct ceu_device *ceudev = vb2_get_drv_priv(vb->vb2_queue);
struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
struct ceu_buffer *buf = vb2_to_ceu(vbuf);
unsigned long irqflags;
spin_lock_irqsave(&ceudev->lock, irqflags);
list_add_tail(&buf->queue, &ceudev->capture);
spin_unlock_irqrestore(&ceudev->lock, irqflags);
}
static int ceu_vb2_prepare(struct vb2_buffer *vb)
{
struct ceu_device *ceudev = vb2_get_drv_priv(vb->vb2_queue);
struct v4l2_pix_format_mplane *pix = &ceudev->v4l2_pix;
unsigned int i;
for (i = 0; i < pix->num_planes; i++) {
if (vb2_plane_size(vb, i) < pix->plane_fmt[i].sizeimage) {
dev_err(ceudev->dev,
"Plane size too small (%lu < %u)\n",
vb2_plane_size(vb, i),
pix->plane_fmt[i].sizeimage);
return -EINVAL;
}
vb2_set_plane_payload(vb, i, pix->plane_fmt[i].sizeimage);
}
return 0;
}
static int ceu_start_streaming(struct vb2_queue *vq, unsigned int count)
{
struct ceu_device *ceudev = vb2_get_drv_priv(vq);
struct v4l2_subdev *v4l2_sd = ceudev->sd->v4l2_sd;
struct ceu_buffer *buf;
unsigned long irqflags;
int ret;
/* Program the CEU interface according to the CEU image format. */
ret = ceu_hw_config(ceudev);
if (ret)
goto error_return_bufs;
ret = v4l2_subdev_call(v4l2_sd, video, s_stream, 1);
if (ret && ret != -ENOIOCTLCMD) {
dev_dbg(ceudev->dev,
"Subdevice failed to start streaming: %d\n", ret);
goto error_return_bufs;
}
spin_lock_irqsave(&ceudev->lock, irqflags);
ceudev->sequence = 0;
/* Grab the first available buffer and trigger the first capture. */
buf = list_first_entry(&ceudev->capture, struct ceu_buffer,
queue);
if (!buf) {
spin_unlock_irqrestore(&ceudev->lock, irqflags);
dev_dbg(ceudev->dev,
"No buffer available for capture.\n");
goto error_stop_sensor;
}
list_del(&buf->queue);
ceudev->active = &buf->vb;
/* Clean and program interrupts for first capture. */
ceu_write(ceudev, CEU_CETCR, ~ceudev->irq_mask);
ceu_write(ceudev, CEU_CEIER, CEU_CEIER_MASK);
ceu_capture(ceudev);
spin_unlock_irqrestore(&ceudev->lock, irqflags);
return 0;
error_stop_sensor:
v4l2_subdev_call(v4l2_sd, video, s_stream, 0);
error_return_bufs:
spin_lock_irqsave(&ceudev->lock, irqflags);
list_for_each_entry(buf, &ceudev->capture, queue)
vb2_buffer_done(&ceudev->active->vb2_buf,
VB2_BUF_STATE_QUEUED);
ceudev->active = NULL;
spin_unlock_irqrestore(&ceudev->lock, irqflags);
return ret;
}
static void ceu_stop_streaming(struct vb2_queue *vq)
{
struct ceu_device *ceudev = vb2_get_drv_priv(vq);
struct v4l2_subdev *v4l2_sd = ceudev->sd->v4l2_sd;
struct ceu_buffer *buf;
unsigned long irqflags;
/* Clean and disable interrupt sources. */
ceu_write(ceudev, CEU_CETCR,
ceu_read(ceudev, CEU_CETCR) & ceudev->irq_mask);
ceu_write(ceudev, CEU_CEIER, CEU_CEIER_MASK);
v4l2_subdev_call(v4l2_sd, video, s_stream, 0);
spin_lock_irqsave(&ceudev->lock, irqflags);
if (ceudev->active) {
vb2_buffer_done(&ceudev->active->vb2_buf,
VB2_BUF_STATE_ERROR);
ceudev->active = NULL;
}
/* Release all queued buffers. */
list_for_each_entry(buf, &ceudev->capture, queue)
vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
INIT_LIST_HEAD(&ceudev->capture);
spin_unlock_irqrestore(&ceudev->lock, irqflags);
ceu_soft_reset(ceudev);
}
static const struct vb2_ops ceu_vb2_ops = {
.queue_setup = ceu_vb2_setup,
.buf_queue = ceu_vb2_queue,
.buf_prepare = ceu_vb2_prepare,
.wait_prepare = vb2_ops_wait_prepare,
.wait_finish = vb2_ops_wait_finish,
.start_streaming = ceu_start_streaming,
.stop_streaming = ceu_stop_streaming,
};
/* --- CEU image formats handling --- */
/*
* __ceu_try_fmt() - test format on CEU and sensor
* @ceudev: The CEU device.
* @v4l2_fmt: format to test.
* @sd_mbus_code: the media bus code accepted by the subdevice; output param.
*
* Returns 0 for success, < 0 for errors.
*/
static int __ceu_try_fmt(struct ceu_device *ceudev, struct v4l2_format *v4l2_fmt,
u32 *sd_mbus_code)
{
struct ceu_subdev *ceu_sd = ceudev->sd;
struct v4l2_pix_format_mplane *pix = &v4l2_fmt->fmt.pix_mp;
struct v4l2_subdev *v4l2_sd = ceu_sd->v4l2_sd;
struct v4l2_subdev_pad_config pad_cfg;
struct v4l2_subdev_state pad_state = {
.pads = &pad_cfg
};
const struct ceu_fmt *ceu_fmt;
u32 mbus_code_old;
u32 mbus_code;
int ret;
/*
* Set format on sensor sub device: bus format used to produce memory
* format is selected depending on YUV component ordering or
* at initialization time.
*/
struct v4l2_subdev_format sd_format = {
.which = V4L2_SUBDEV_FORMAT_TRY,
};
mbus_code_old = ceu_sd->mbus_fmt.mbus_code;
switch (pix->pixelformat) {
case V4L2_PIX_FMT_YUYV:
mbus_code = MEDIA_BUS_FMT_YUYV8_2X8;
break;
case V4L2_PIX_FMT_UYVY:
mbus_code = MEDIA_BUS_FMT_UYVY8_2X8;
break;
case V4L2_PIX_FMT_YVYU:
mbus_code = MEDIA_BUS_FMT_YVYU8_2X8;
break;
case V4L2_PIX_FMT_VYUY:
mbus_code = MEDIA_BUS_FMT_VYUY8_2X8;
break;
case V4L2_PIX_FMT_NV16:
case V4L2_PIX_FMT_NV61:
case V4L2_PIX_FMT_NV12:
case V4L2_PIX_FMT_NV21:
mbus_code = ceu_sd->mbus_fmt.mbus_code;
break;
default:
pix->pixelformat = V4L2_PIX_FMT_NV16;
mbus_code = ceu_sd->mbus_fmt.mbus_code;
break;
}
ceu_fmt = get_ceu_fmt_from_fourcc(pix->pixelformat);
/* CFSZR requires height and width to be 4-pixel aligned. */
v4l_bound_align_image(&pix->width, 2, CEU_MAX_WIDTH, 4,
&pix->height, 4, CEU_MAX_HEIGHT, 4, 0);
v4l2_fill_mbus_format_mplane(&sd_format.format, pix);
/*
* Try with the mbus_code matching YUYV components ordering first,
* if that one fails, fallback to default selected at initialization
* time.
*/
sd_format.format.code = mbus_code;
ret = v4l2_subdev_call(v4l2_sd, pad, set_fmt, &pad_state, &sd_format);
if (ret) {
if (ret == -EINVAL) {
/* fallback */
sd_format.format.code = mbus_code_old;
ret = v4l2_subdev_call(v4l2_sd, pad, set_fmt,
&pad_state, &sd_format);
}
if (ret)
return ret;
}
/* Apply size returned by sensor as the CEU can't scale. */
v4l2_fill_pix_format_mplane(pix, &sd_format.format);
/* Calculate per-plane sizes based on image format. */
ceu_calc_plane_sizes(ceudev, ceu_fmt, pix);
/* Report to caller the configured mbus format. */
*sd_mbus_code = sd_format.format.code;
return 0;
}
/*
* ceu_try_fmt() - Wrapper for __ceu_try_fmt; discard configured mbus_fmt
*/
static int ceu_try_fmt(struct ceu_device *ceudev, struct v4l2_format *v4l2_fmt)
{
u32 mbus_code;
return __ceu_try_fmt(ceudev, v4l2_fmt, &mbus_code);
}
/*
* ceu_set_fmt() - Apply the supplied format to both sensor and CEU
*/
static int ceu_set_fmt(struct ceu_device *ceudev, struct v4l2_format *v4l2_fmt)
{
struct ceu_subdev *ceu_sd = ceudev->sd;
struct v4l2_subdev *v4l2_sd = ceu_sd->v4l2_sd;
u32 mbus_code;
int ret;
/*
* Set format on sensor sub device: bus format used to produce memory
* format is selected at initialization time.
*/
struct v4l2_subdev_format format = {
.which = V4L2_SUBDEV_FORMAT_ACTIVE,
};
ret = __ceu_try_fmt(ceudev, v4l2_fmt, &mbus_code);
if (ret)
return ret;
format.format.code = mbus_code;
v4l2_fill_mbus_format_mplane(&format.format, &v4l2_fmt->fmt.pix_mp);
ret = v4l2_subdev_call(v4l2_sd, pad, set_fmt, NULL, &format);
if (ret)
return ret;
ceudev->v4l2_pix = v4l2_fmt->fmt.pix_mp;
ceudev->field = V4L2_FIELD_NONE;
return 0;
}
/*
* ceu_set_default_fmt() - Apply default NV16 memory output format with VGA
* sizes.
*/
static int ceu_set_default_fmt(struct ceu_device *ceudev)
{
int ret;
struct v4l2_format v4l2_fmt = {
.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
.fmt.pix_mp = {
.width = VGA_WIDTH,
.height = VGA_HEIGHT,
.field = V4L2_FIELD_NONE,
.pixelformat = V4L2_PIX_FMT_NV16,
.num_planes = 2,
.plane_fmt = {
[0] = {
.sizeimage = VGA_WIDTH * VGA_HEIGHT * 2,
.bytesperline = VGA_WIDTH * 2,
},
[1] = {
.sizeimage = VGA_WIDTH * VGA_HEIGHT * 2,
.bytesperline = VGA_WIDTH * 2,
},
},
},
};
ret = ceu_try_fmt(ceudev, &v4l2_fmt);
if (ret)
return ret;
ceudev->v4l2_pix = v4l2_fmt.fmt.pix_mp;
ceudev->field = V4L2_FIELD_NONE;
return 0;
}
/*
* ceu_init_mbus_fmt() - Query sensor for supported formats and initialize
* CEU media bus format used to produce memory formats.
*
* Find out if sensor can produce a permutation of 8-bits YUYV bus format.
* From a single 8-bits YUYV bus format the CEU can produce several memory
* output formats:
* - NV[12|21|16|61] through image fetch mode;
* - YUYV422 if sensor provides YUYV422
*
* TODO: Other YUYV422 permutations through data fetch sync mode and DTARY
* TODO: Binary data (eg. JPEG) and raw formats through data fetch sync mode
*/
static int ceu_init_mbus_fmt(struct ceu_device *ceudev)
{
struct ceu_subdev *ceu_sd = ceudev->sd;
struct ceu_mbus_fmt *mbus_fmt = &ceu_sd->mbus_fmt;
struct v4l2_subdev *v4l2_sd = ceu_sd->v4l2_sd;
bool yuyv_bus_fmt = false;
struct v4l2_subdev_mbus_code_enum sd_mbus_fmt = {
.which = V4L2_SUBDEV_FORMAT_ACTIVE,
.index = 0,
};
/* Find out if sensor can produce any permutation of 8-bits YUYV422. */
while (!yuyv_bus_fmt &&
!v4l2_subdev_call(v4l2_sd, pad, enum_mbus_code,
NULL, &sd_mbus_fmt)) {
switch (sd_mbus_fmt.code) {
case MEDIA_BUS_FMT_YUYV8_2X8:
case MEDIA_BUS_FMT_YVYU8_2X8:
case MEDIA_BUS_FMT_UYVY8_2X8:
case MEDIA_BUS_FMT_VYUY8_2X8:
yuyv_bus_fmt = true;
break;
default: