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
|
// SPDX-License-Identifier: GPL-2.0
/*
* ARM Mali-C55 ISP Driver - Video capture devices
*
* Copyright (C) 2025 Ideas on Board Oy
*/
#include <linux/cleanup.h>
#include <linux/minmax.h>
#include <linux/lockdep.h>
#include <linux/pm_runtime.h>
#include <linux/string.h>
#include <linux/videodev2.h>
#include <media/v4l2-dev.h>
#include <media/v4l2-event.h>
#include <media/v4l2-ioctl.h>
#include <media/v4l2-subdev.h>
#include <media/videobuf2-core.h>
#include <media/videobuf2-dma-contig.h>
#include "mali-c55-common.h"
#include "mali-c55-registers.h"
static const struct mali_c55_format_info mali_c55_fmts[] = {
/*
* This table is missing some entries which need further work or
* investigation:
*
* Base mode 5 is "Generic Data"
* Base mode 8 is a backwards V4L2_PIX_FMT_XYUV32 - no V4L2 equivalent
* Base mode 9 seems to have no V4L2 equivalent
* Base mode 17, 19 and 20 describe formats which seem to have no V4L2
* equivalent
*/
{
.fourcc = V4L2_PIX_FMT_XBGR32,
.mbus_codes = {
MEDIA_BUS_FMT_RGB121212_1X36,
MEDIA_BUS_FMT_RGB202020_1X60,
},
.is_raw = false,
.registers = {
.base_mode = MALI_C55_OUTPUT_RGB32,
.uv_plane = MALI_C55_OUTPUT_PLANE_ALT0
}
},
{
.fourcc = V4L2_PIX_FMT_ARGB2101010,
.mbus_codes = {
MEDIA_BUS_FMT_RGB121212_1X36,
MEDIA_BUS_FMT_RGB202020_1X60,
},
.is_raw = false,
.registers = {
.base_mode = MALI_C55_OUTPUT_A2R10G10B10,
.uv_plane = MALI_C55_OUTPUT_PLANE_ALT0
}
},
{
.fourcc = V4L2_PIX_FMT_RGB565,
.mbus_codes = {
MEDIA_BUS_FMT_RGB121212_1X36,
MEDIA_BUS_FMT_RGB202020_1X60,
},
.is_raw = false,
.registers = {
.base_mode = MALI_C55_OUTPUT_RGB565,
.uv_plane = MALI_C55_OUTPUT_PLANE_ALT0
}
},
{
.fourcc = V4L2_PIX_FMT_BGR24,
.mbus_codes = {
MEDIA_BUS_FMT_RGB121212_1X36,
MEDIA_BUS_FMT_RGB202020_1X60,
},
.is_raw = false,
.registers = {
.base_mode = MALI_C55_OUTPUT_RGB24,
.uv_plane = MALI_C55_OUTPUT_PLANE_ALT0
}
},
{
.fourcc = V4L2_PIX_FMT_YUYV,
.mbus_codes = {
MEDIA_BUS_FMT_YUV10_1X30,
},
.is_raw = false,
.registers = {
.base_mode = MALI_C55_OUTPUT_YUY2,
.uv_plane = MALI_C55_OUTPUT_PLANE_ALT0
}
},
{
.fourcc = V4L2_PIX_FMT_UYVY,
.mbus_codes = {
MEDIA_BUS_FMT_YUV10_1X30,
},
.is_raw = false,
.registers = {
.base_mode = MALI_C55_OUTPUT_UYVY,
.uv_plane = MALI_C55_OUTPUT_PLANE_ALT0
}
},
{
.fourcc = V4L2_PIX_FMT_Y210,
.mbus_codes = {
MEDIA_BUS_FMT_YUV10_1X30,
},
.is_raw = false,
.registers = {
.base_mode = MALI_C55_OUTPUT_Y210,
.uv_plane = MALI_C55_OUTPUT_PLANE_ALT0
}
},
/*
* This is something of a hack, the ISP thinks it's running NV12M but
* by setting uv_plane = 0 we simply discard that planes and only output
* the Y-plane.
*/
{
.fourcc = V4L2_PIX_FMT_GREY,
.mbus_codes = {
MEDIA_BUS_FMT_YUV10_1X30,
},
.is_raw = false,
.registers = {
.base_mode = MALI_C55_OUTPUT_NV12_21,
.uv_plane = MALI_C55_OUTPUT_PLANE_ALT0
}
},
{
.fourcc = V4L2_PIX_FMT_NV12M,
.mbus_codes = {
MEDIA_BUS_FMT_YUV10_1X30,
},
.is_raw = false,
.registers = {
.base_mode = MALI_C55_OUTPUT_NV12_21,
.uv_plane = MALI_C55_OUTPUT_PLANE_ALT1
}
},
{
.fourcc = V4L2_PIX_FMT_NV21M,
.mbus_codes = {
MEDIA_BUS_FMT_YUV10_1X30,
},
.is_raw = false,
.registers = {
.base_mode = MALI_C55_OUTPUT_NV12_21,
.uv_plane = MALI_C55_OUTPUT_PLANE_ALT2
}
},
/*
* RAW uncompressed formats are all packed in 16 bpp.
* TODO: Expand this list to encompass all possible RAW formats.
*/
{
.fourcc = V4L2_PIX_FMT_SRGGB16,
.mbus_codes = {
MEDIA_BUS_FMT_SRGGB16_1X16,
},
.is_raw = true,
.registers = {
.base_mode = MALI_C55_OUTPUT_RAW16,
.uv_plane = MALI_C55_OUTPUT_PLANE_ALT0
}
},
{
.fourcc = V4L2_PIX_FMT_SBGGR16,
.mbus_codes = {
MEDIA_BUS_FMT_SBGGR16_1X16,
},
.is_raw = true,
.registers = {
.base_mode = MALI_C55_OUTPUT_RAW16,
.uv_plane = MALI_C55_OUTPUT_PLANE_ALT0
}
},
{
.fourcc = V4L2_PIX_FMT_SGBRG16,
.mbus_codes = {
MEDIA_BUS_FMT_SGBRG16_1X16,
},
.is_raw = true,
.registers = {
.base_mode = MALI_C55_OUTPUT_RAW16,
.uv_plane = MALI_C55_OUTPUT_PLANE_ALT0
}
},
{
.fourcc = V4L2_PIX_FMT_SGRBG16,
.mbus_codes = {
MEDIA_BUS_FMT_SGRBG16_1X16,
},
.is_raw = true,
.registers = {
.base_mode = MALI_C55_OUTPUT_RAW16,
.uv_plane = MALI_C55_OUTPUT_PLANE_ALT0
}
},
};
void mali_c55_cap_dev_write(struct mali_c55_cap_dev *cap_dev, unsigned int addr,
u32 val)
{
mali_c55_ctx_write(cap_dev->mali_c55, addr + cap_dev->reg_offset, val);
}
static u32 mali_c55_cap_dev_read(struct mali_c55_cap_dev *cap_dev, unsigned int addr)
{
return mali_c55_ctx_read(cap_dev->mali_c55, addr + cap_dev->reg_offset);
}
static void mali_c55_cap_dev_update_bits(struct mali_c55_cap_dev *cap_dev,
unsigned int addr, u32 mask, u32 val)
{
u32 orig, tmp;
orig = mali_c55_cap_dev_read(cap_dev, addr);
tmp = orig & ~mask;
tmp |= val & mask;
if (tmp != orig)
mali_c55_cap_dev_write(cap_dev, addr, tmp);
}
static bool
mali_c55_mbus_code_can_produce_fmt(const struct mali_c55_format_info *fmt,
u32 code)
{
unsigned int i;
for (i = 0; i < ARRAY_SIZE(fmt->mbus_codes); i++) {
if (fmt->mbus_codes[i] == code)
return true;
}
return false;
}
bool mali_c55_format_is_raw(unsigned int mbus_code)
{
unsigned int i;
for (i = 0; i < ARRAY_SIZE(mali_c55_fmts); i++) {
if (mali_c55_fmts[i].mbus_codes[0] == mbus_code)
return mali_c55_fmts[i].is_raw;
}
return false;
}
static const struct mali_c55_format_info *
mali_c55_format_from_pix(const u32 pixelformat)
{
unsigned int i;
for (i = 0; i < ARRAY_SIZE(mali_c55_fmts); i++) {
if (mali_c55_fmts[i].fourcc == pixelformat)
return &mali_c55_fmts[i];
}
/*
* If we find no matching pixelformat, we'll just default to the first
* one for now.
*/
return &mali_c55_fmts[0];
}
static const char * const capture_device_names[] = {
"mali-c55 fr",
"mali-c55 ds",
};
static int mali_c55_link_validate(struct media_link *link)
{
struct video_device *vdev =
media_entity_to_video_device(link->sink->entity);
struct mali_c55_cap_dev *cap_dev = video_get_drvdata(vdev);
struct v4l2_subdev *sd =
media_entity_to_v4l2_subdev(link->source->entity);
const struct v4l2_pix_format_mplane *pix_mp;
const struct mali_c55_format_info *cap_fmt;
struct v4l2_subdev_format sd_fmt = {
.which = V4L2_SUBDEV_FORMAT_ACTIVE,
.pad = link->source->index,
};
int ret;
ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &sd_fmt);
if (ret)
return ret;
pix_mp = &cap_dev->format.format;
cap_fmt = cap_dev->format.info;
if (sd_fmt.format.width != pix_mp->width ||
sd_fmt.format.height != pix_mp->height) {
dev_dbg(cap_dev->mali_c55->dev,
"link '%s':%u -> '%s':%u not valid: %ux%u != %ux%u\n",
link->source->entity->name, link->source->index,
link->sink->entity->name, link->sink->index,
sd_fmt.format.width, sd_fmt.format.height,
pix_mp->width, pix_mp->height);
return -EPIPE;
}
if (!mali_c55_mbus_code_can_produce_fmt(cap_fmt, sd_fmt.format.code)) {
dev_dbg(cap_dev->mali_c55->dev,
"link '%s':%u -> '%s':%u not valid: mbus_code 0x%04x cannot produce pixel format %p4cc\n",
link->source->entity->name, link->source->index,
link->sink->entity->name, link->sink->index,
sd_fmt.format.code, &pix_mp->pixelformat);
return -EPIPE;
}
return 0;
}
static const struct media_entity_operations mali_c55_media_ops = {
.link_validate = mali_c55_link_validate,
};
static int mali_c55_vb2_queue_setup(struct vb2_queue *q, unsigned int *num_buffers,
unsigned int *num_planes, unsigned int sizes[],
struct device *alloc_devs[])
{
struct mali_c55_cap_dev *cap_dev = q->drv_priv;
unsigned int i;
if (*num_planes) {
if (*num_planes != cap_dev->format.format.num_planes)
return -EINVAL;
for (i = 0; i < cap_dev->format.format.num_planes; i++)
if (sizes[i] < cap_dev->format.format.plane_fmt[i].sizeimage)
return -EINVAL;
} else {
*num_planes = cap_dev->format.format.num_planes;
for (i = 0; i < cap_dev->format.format.num_planes; i++)
sizes[i] = cap_dev->format.format.plane_fmt[i].sizeimage;
}
return 0;
}
static void mali_c55_buf_queue(struct vb2_buffer *vb)
{
struct mali_c55_cap_dev *cap_dev = vb2_get_drv_priv(vb->vb2_queue);
struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
struct mali_c55_buffer *buf = container_of(vbuf,
struct mali_c55_buffer, vb);
unsigned int i;
buf->planes_pending = cap_dev->format.format.num_planes;
for (i = 0; i < cap_dev->format.format.num_planes; i++) {
unsigned long size = cap_dev->format.format.plane_fmt[i].sizeimage;
vb2_set_plane_payload(vb, i, size);
}
buf->vb.field = V4L2_FIELD_NONE;
guard(spinlock)(&cap_dev->buffers.lock);
list_add_tail(&buf->queue, &cap_dev->buffers.input);
}
static int mali_c55_buf_init(struct vb2_buffer *vb)
{
struct mali_c55_cap_dev *cap_dev = vb2_get_drv_priv(vb->vb2_queue);
struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
struct mali_c55_buffer *buf = container_of(vbuf,
struct mali_c55_buffer, vb);
unsigned int i;
for (i = 0; i < cap_dev->format.format.num_planes; i++)
buf->addrs[i] = vb2_dma_contig_plane_dma_addr(vb, i);
return 0;
}
void mali_c55_set_next_buffer(struct mali_c55_cap_dev *cap_dev)
{
struct v4l2_pix_format_mplane *pix_mp;
struct mali_c55_buffer *buf;
dma_addr_t *addrs;
scoped_guard(spinlock, &cap_dev->buffers.lock) {
buf = list_first_entry_or_null(&cap_dev->buffers.input,
struct mali_c55_buffer, queue);
if (buf)
list_del(&buf->queue);
}
if (!buf) {
/*
* If we underflow then we can tell the ISP that we don't want
* to write out the next frame.
*/
mali_c55_cap_dev_update_bits(cap_dev,
MALI_C55_REG_Y_WRITER_MODE,
MALI_C55_WRITER_FRAME_WRITE_MASK,
0x00);
mali_c55_cap_dev_update_bits(cap_dev,
MALI_C55_REG_UV_WRITER_MODE,
MALI_C55_WRITER_FRAME_WRITE_MASK,
0x00);
return;
}
pix_mp = &cap_dev->format.format;
mali_c55_cap_dev_update_bits(cap_dev, MALI_C55_REG_Y_WRITER_MODE,
MALI_C55_WRITER_FRAME_WRITE_MASK,
MALI_C55_WRITER_FRAME_WRITE_ENABLE);
if (cap_dev->format.info->registers.uv_plane)
mali_c55_cap_dev_update_bits(cap_dev,
MALI_C55_REG_UV_WRITER_MODE,
MALI_C55_WRITER_FRAME_WRITE_MASK,
MALI_C55_WRITER_FRAME_WRITE_ENABLE);
addrs = buf->addrs;
mali_c55_cap_dev_write(cap_dev,
MALI_C55_REG_Y_WRITER_BANKS_BASE,
addrs[MALI_C55_PLANE_Y]);
mali_c55_cap_dev_write(cap_dev,
MALI_C55_REG_UV_WRITER_BANKS_BASE,
addrs[MALI_C55_PLANE_UV]);
mali_c55_cap_dev_write(cap_dev,
MALI_C55_REG_Y_WRITER_OFFSET,
pix_mp->plane_fmt[MALI_C55_PLANE_Y].bytesperline);
mali_c55_cap_dev_write(cap_dev,
MALI_C55_REG_UV_WRITER_OFFSET,
pix_mp->plane_fmt[MALI_C55_PLANE_UV].bytesperline);
guard(spinlock)(&cap_dev->buffers.processing_lock);
list_add_tail(&buf->queue, &cap_dev->buffers.processing);
}
/**
* mali_c55_set_plane_done - mark the plane as written and process the buffer if
* both planes are finished.
* @cap_dev: pointer to the fr or ds pipe output
* @plane: the plane to mark as completed
*
* The Mali C55 ISP has muliplanar outputs for some formats that come with two
* separate "buffer write completed" interrupts - we need to flag each plane's
* completion and check whether both planes are done - if so, complete the buf
* in vb2.
*/
void mali_c55_set_plane_done(struct mali_c55_cap_dev *cap_dev,
enum mali_c55_planes plane)
{
struct mali_c55_isp *isp = &cap_dev->mali_c55->isp;
struct mali_c55_buffer *buf;
scoped_guard(spinlock, &cap_dev->buffers.processing_lock) {
buf = list_first_entry_or_null(&cap_dev->buffers.processing,
struct mali_c55_buffer, queue);
/*
* If the stream was stopped, the buffer might have been sent
* back to userspace already.
*/
if (!buf || --buf->planes_pending)
return;
list_del(&buf->queue);
}
/* If the other plane is also done... */
buf->vb.vb2_buf.timestamp = ktime_get_boottime_ns();
buf->vb.sequence = isp->frame_sequence++;
vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
}
static void mali_c55_cap_dev_stream_disable(struct mali_c55_cap_dev *cap_dev)
{
mali_c55_cap_dev_update_bits(cap_dev, MALI_C55_REG_Y_WRITER_MODE,
MALI_C55_WRITER_FRAME_WRITE_MASK, 0x00);
mali_c55_cap_dev_update_bits(cap_dev, MALI_C55_REG_UV_WRITER_MODE,
MALI_C55_WRITER_FRAME_WRITE_MASK, 0x00);
}
static void mali_c55_cap_dev_stream_enable(struct mali_c55_cap_dev *cap_dev)
{
/*
* The Mali ISP can hold up to 5 buffer addresses and simply cycle
* through them, but it's not clear to me that the vb2 queue _guarantees_
* it will queue buffers to the driver in a fixed order, and ensuring
* we call vb2_buffer_done() for the right buffer seems to me to add
* pointless complexity given in multi-context mode we'd need to
* re-write those registers every frame anyway...so we tell the ISP to
* use a single register and update it for each frame.
*/
mali_c55_cap_dev_update_bits(cap_dev,
MALI_C55_REG_Y_WRITER_BANKS_CONFIG,
MALI_C55_REG_Y_WRITER_MAX_BANKS_MASK, 0);
mali_c55_cap_dev_update_bits(cap_dev,
MALI_C55_REG_UV_WRITER_BANKS_CONFIG,
MALI_C55_REG_UV_WRITER_MAX_BANKS_MASK, 0);
mali_c55_set_next_buffer(cap_dev);
}
static void mali_c55_cap_dev_return_buffers(struct mali_c55_cap_dev *cap_dev,
enum vb2_buffer_state state)
{
struct mali_c55_buffer *buf, *tmp;
scoped_guard(spinlock, &cap_dev->buffers.lock) {
list_for_each_entry_safe(buf, tmp, &cap_dev->buffers.input,
queue) {
list_del(&buf->queue);
vb2_buffer_done(&buf->vb.vb2_buf, state);
}
}
guard(spinlock)(&cap_dev->buffers.processing_lock);
list_for_each_entry_safe(buf, tmp, &cap_dev->buffers.processing, queue) {
list_del(&buf->queue);
vb2_buffer_done(&buf->vb.vb2_buf, state);
}
}
static void mali_c55_cap_dev_format_configure(struct mali_c55_cap_dev *cap_dev)
{
const struct mali_c55_format_info *capture_format = cap_dev->format.info;
const struct v4l2_pix_format_mplane *pix_mp = &cap_dev->format.format;
const struct v4l2_format_info *info;
info = v4l2_format_info(pix_mp->pixelformat);
if (WARN_ON(!info))
return;
mali_c55_cap_dev_write(cap_dev, MALI_C55_REG_Y_WRITER_MODE,
capture_format->registers.base_mode);
mali_c55_cap_dev_write(cap_dev, MALI_C55_REG_ACTIVE_OUT_Y_SIZE,
MALI_C55_REG_ACTIVE_OUT_SIZE_W(pix_mp->width) |
MALI_C55_REG_ACTIVE_OUT_SIZE_H(pix_mp->height));
if (info->mem_planes > 1) {
mali_c55_cap_dev_write(cap_dev, MALI_C55_REG_UV_WRITER_MODE,
capture_format->registers.base_mode);
mali_c55_cap_dev_update_bits(cap_dev,
MALI_C55_REG_UV_WRITER_MODE,
MALI_C55_WRITER_SUBMODE_MASK,
MALI_C55_WRITER_SUBMODE(capture_format->registers.uv_plane));
mali_c55_cap_dev_write(cap_dev, MALI_C55_REG_ACTIVE_OUT_UV_SIZE,
MALI_C55_REG_ACTIVE_OUT_SIZE_W(pix_mp->width) |
MALI_C55_REG_ACTIVE_OUT_SIZE_H(pix_mp->height));
}
if (info->pixel_enc == V4L2_PIXEL_ENC_YUV) {
mali_c55_cap_dev_write(cap_dev, MALI_C55_REG_CS_CONV_CONFIG,
MALI_C55_CS_CONV_MATRIX_MASK);
if (info->hdiv > 1)
mali_c55_cap_dev_update_bits(cap_dev,
MALI_C55_REG_CS_CONV_CONFIG,
MALI_C55_CS_CONV_HORZ_DOWNSAMPLE_MASK,
MALI_C55_CS_CONV_HORZ_DOWNSAMPLE_ENABLE);
if (info->vdiv > 1)
mali_c55_cap_dev_update_bits(cap_dev,
MALI_C55_REG_CS_CONV_CONFIG,
MALI_C55_CS_CONV_VERT_DOWNSAMPLE_MASK,
MALI_C55_CS_CONV_VERT_DOWNSAMPLE_ENABLE);
if (info->hdiv > 1 || info->vdiv > 1)
mali_c55_cap_dev_update_bits(cap_dev,
MALI_C55_REG_CS_CONV_CONFIG,
MALI_C55_CS_CONV_FILTER_MASK,
MALI_C55_CS_CONV_FILTER_ENABLE);
}
}
static int mali_c55_vb2_start_streaming(struct vb2_queue *q, unsigned int count)
{
struct mali_c55_cap_dev *cap_dev = q->drv_priv;
struct mali_c55 *mali_c55 = cap_dev->mali_c55;
struct mali_c55_resizer *rsz = cap_dev->rsz;
struct mali_c55_isp *isp = &mali_c55->isp;
int ret;
guard(mutex)(&isp->capture_lock);
ret = pm_runtime_resume_and_get(mali_c55->dev);
if (ret)
goto err_return_buffers;
ret = video_device_pipeline_alloc_start(&cap_dev->vdev);
if (ret) {
dev_dbg(mali_c55->dev, "%s failed to start media pipeline\n",
cap_dev->vdev.name);
goto err_pm_put;
}
mali_c55_cap_dev_format_configure(cap_dev);
mali_c55_cap_dev_stream_enable(cap_dev);
ret = v4l2_subdev_enable_streams(&rsz->sd, MALI_C55_RSZ_SOURCE_PAD,
BIT(0));
if (ret)
goto err_disable_cap_dev;
if (mali_c55_pipeline_ready(mali_c55)) {
ret = v4l2_subdev_enable_streams(&mali_c55->isp.sd,
MALI_C55_ISP_PAD_SOURCE_VIDEO,
BIT(0));
if (ret < 0)
goto err_disable_rsz;
}
return 0;
err_disable_rsz:
v4l2_subdev_disable_streams(&rsz->sd, MALI_C55_RSZ_SOURCE_PAD, BIT(0));
err_disable_cap_dev:
mali_c55_cap_dev_stream_disable(cap_dev);
video_device_pipeline_stop(&cap_dev->vdev);
err_pm_put:
pm_runtime_put_autosuspend(mali_c55->dev);
err_return_buffers:
mali_c55_cap_dev_return_buffers(cap_dev, VB2_BUF_STATE_QUEUED);
return ret;
}
static void mali_c55_vb2_stop_streaming(struct vb2_queue *q)
{
struct mali_c55_cap_dev *cap_dev = q->drv_priv;
struct mali_c55 *mali_c55 = cap_dev->mali_c55;
struct mali_c55_resizer *rsz = cap_dev->rsz;
struct mali_c55_isp *isp = &mali_c55->isp;
guard(mutex)(&isp->capture_lock);
if (mali_c55_pipeline_ready(mali_c55)) {
if (v4l2_subdev_is_streaming(&isp->sd))
v4l2_subdev_disable_streams(&isp->sd,
MALI_C55_ISP_PAD_SOURCE_VIDEO,
BIT(0));
}
v4l2_subdev_disable_streams(&rsz->sd, MALI_C55_RSZ_SOURCE_PAD, BIT(0));
mali_c55_cap_dev_stream_disable(cap_dev);
mali_c55_cap_dev_return_buffers(cap_dev, VB2_BUF_STATE_ERROR);
video_device_pipeline_stop(&cap_dev->vdev);
pm_runtime_put_autosuspend(mali_c55->dev);
}
static const struct vb2_ops mali_c55_vb2_ops = {
.queue_setup = &mali_c55_vb2_queue_setup,
.buf_queue = &mali_c55_buf_queue,
.buf_init = &mali_c55_buf_init,
.start_streaming = &mali_c55_vb2_start_streaming,
.stop_streaming = &mali_c55_vb2_stop_streaming,
};
static const struct v4l2_file_operations mali_c55_v4l2_fops = {
.owner = THIS_MODULE,
.unlocked_ioctl = video_ioctl2,
.open = v4l2_fh_open,
.release = vb2_fop_release,
.poll = vb2_fop_poll,
.mmap = vb2_fop_mmap,
};
static void mali_c55_try_fmt(struct v4l2_pix_format_mplane *pix_mp)
{
const struct mali_c55_format_info *capture_format;
const struct v4l2_format_info *info;
struct v4l2_plane_pix_format *plane, *y_plane;
unsigned int padding;
unsigned int i;
capture_format = mali_c55_format_from_pix(pix_mp->pixelformat);
pix_mp->pixelformat = capture_format->fourcc;
pix_mp->width = clamp(pix_mp->width, MALI_C55_MIN_WIDTH,
MALI_C55_MAX_WIDTH);
pix_mp->height = clamp(pix_mp->height, MALI_C55_MIN_HEIGHT,
MALI_C55_MAX_HEIGHT);
pix_mp->field = V4L2_FIELD_NONE;
pix_mp->colorspace = V4L2_COLORSPACE_DEFAULT;
pix_mp->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
pix_mp->quantization = V4L2_QUANTIZATION_DEFAULT;
info = v4l2_format_info(pix_mp->pixelformat);
pix_mp->num_planes = info->mem_planes;
memset(pix_mp->plane_fmt, 0, sizeof(pix_mp->plane_fmt));
y_plane = &pix_mp->plane_fmt[0];
y_plane->bytesperline = clamp(y_plane->bytesperline,
info->bpp[0] * pix_mp->width, 65535U);
/*
* The ISP requires that the stride be aligned to 16-bytes. This is not
* detailed in the documentation but has been verified experimentally.
*/
y_plane->bytesperline = ALIGN(y_plane->bytesperline, 16);
y_plane->sizeimage = y_plane->bytesperline * pix_mp->height;
padding = y_plane->bytesperline - (pix_mp->width * info->bpp[0]);
for (i = 1; i < info->comp_planes; i++) {
plane = &pix_mp->plane_fmt[i];
plane->bytesperline = DIV_ROUND_UP(info->bpp[i] * pix_mp->width,
info->hdiv) + padding;
plane->sizeimage = DIV_ROUND_UP(plane->bytesperline *
pix_mp->height, info->vdiv);
}
if (info->mem_planes == 1) {
for (i = 1; i < info->comp_planes; i++) {
plane = &pix_mp->plane_fmt[i];
y_plane->sizeimage += plane->sizeimage;
}
}
}
static int mali_c55_try_fmt_vid_cap_mplane(struct file *file, void *fh,
struct v4l2_format *f)
{
mali_c55_try_fmt(&f->fmt.pix_mp);
return 0;
}
static void mali_c55_set_format(struct mali_c55_cap_dev *cap_dev,
struct v4l2_pix_format_mplane *pix_mp)
{
mali_c55_try_fmt(pix_mp);
cap_dev->format.format = *pix_mp;
cap_dev->format.info = mali_c55_format_from_pix(pix_mp->pixelformat);
}
static int mali_c55_s_fmt_vid_cap_mplane(struct file *file, void *fh,
struct v4l2_format *f)
{
struct mali_c55_cap_dev *cap_dev = video_drvdata(file);
if (vb2_is_busy(&cap_dev->queue))
return -EBUSY;
mali_c55_set_format(cap_dev, &f->fmt.pix_mp);
return 0;
}
static int mali_c55_g_fmt_vid_cap_mplane(struct file *file, void *fh,
struct v4l2_format *f)
{
struct mali_c55_cap_dev *cap_dev = video_drvdata(file);
f->fmt.pix_mp = cap_dev->format.format;
return 0;
}
static int mali_c55_enum_fmt_vid_cap_mplane(struct file *file, void *fh,
struct v4l2_fmtdesc *f)
{
struct mali_c55_cap_dev *cap_dev = video_drvdata(file);
unsigned int j = 0;
unsigned int i;
for (i = 0; i < ARRAY_SIZE(mali_c55_fmts); i++) {
if (f->mbus_code &&
!mali_c55_mbus_code_can_produce_fmt(&mali_c55_fmts[i],
f->mbus_code))
continue;
/* Downscale pipe can't output RAW formats */
if (mali_c55_fmts[i].is_raw &&
cap_dev->reg_offset == MALI_C55_CAP_DEV_DS_REG_OFFSET)
continue;
if (j++ == f->index) {
f->pixelformat = mali_c55_fmts[i].fourcc;
return 0;
}
}
return -EINVAL;
}
static int mali_c55_querycap(struct file *file, void *fh,
struct v4l2_capability *cap)
{
strscpy(cap->driver, MALI_C55_DRIVER_NAME, sizeof(cap->driver));
strscpy(cap->card, "ARM Mali-C55 ISP", sizeof(cap->card));
return 0;
}
static const struct v4l2_ioctl_ops mali_c55_v4l2_ioctl_ops = {
.vidioc_reqbufs = vb2_ioctl_reqbufs,
.vidioc_querybuf = vb2_ioctl_querybuf,
.vidioc_create_bufs = vb2_ioctl_create_bufs,
.vidioc_qbuf = vb2_ioctl_qbuf,
.vidioc_expbuf = vb2_ioctl_expbuf,
.vidioc_dqbuf = vb2_ioctl_dqbuf,
.vidioc_prepare_buf = vb2_ioctl_prepare_buf,
.vidioc_streamon = vb2_ioctl_streamon,
.vidioc_streamoff = vb2_ioctl_streamoff,
.vidioc_try_fmt_vid_cap_mplane = mali_c55_try_fmt_vid_cap_mplane,
.vidioc_s_fmt_vid_cap_mplane = mali_c55_s_fmt_vid_cap_mplane,
.vidioc_g_fmt_vid_cap_mplane = mali_c55_g_fmt_vid_cap_mplane,
.vidioc_enum_fmt_vid_cap = mali_c55_enum_fmt_vid_cap_mplane,
.vidioc_querycap = mali_c55_querycap,
.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
};
static int mali_c55_register_cap_dev(struct mali_c55 *mali_c55,
enum mali_c55_cap_devs cap_dev_id)
{
struct mali_c55_cap_dev *cap_dev = &mali_c55->cap_devs[cap_dev_id];
struct v4l2_pix_format_mplane pix_mp;
struct video_device *vdev;
struct vb2_queue *vb2q;
int ret;
vdev = &cap_dev->vdev;
vb2q = &cap_dev->queue;
cap_dev->mali_c55 = mali_c55;
mutex_init(&cap_dev->lock);
INIT_LIST_HEAD(&cap_dev->buffers.input);
INIT_LIST_HEAD(&cap_dev->buffers.processing);
spin_lock_init(&cap_dev->buffers.lock);
spin_lock_init(&cap_dev->buffers.processing_lock);
switch (cap_dev_id) {
case MALI_C55_CAP_DEV_FR:
cap_dev->rsz = &mali_c55->resizers[MALI_C55_RSZ_FR];
cap_dev->reg_offset = MALI_C55_CAP_DEV_FR_REG_OFFSET;
break;
case MALI_C55_CAP_DEV_DS:
cap_dev->rsz = &mali_c55->resizers[MALI_C55_RSZ_DS];
cap_dev->reg_offset = MALI_C55_CAP_DEV_DS_REG_OFFSET;
break;
default:
ret = -EINVAL;
goto err_destroy_mutex;
}
cap_dev->pad.flags = MEDIA_PAD_FL_SINK;
ret = media_entity_pads_init(&cap_dev->vdev.entity, 1, &cap_dev->pad);
if (ret) {
mutex_destroy(&cap_dev->lock);
goto err_destroy_mutex;
}
vb2q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
vb2q->io_modes = VB2_MMAP | VB2_DMABUF;
vb2q->drv_priv = cap_dev;
vb2q->mem_ops = &vb2_dma_contig_memops;
vb2q->ops = &mali_c55_vb2_ops;
vb2q->buf_struct_size = sizeof(struct mali_c55_buffer);
vb2q->min_queued_buffers = 1;
vb2q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
vb2q->lock = &cap_dev->lock;
vb2q->dev = mali_c55->dev;
ret = vb2_queue_init(vb2q);
if (ret) {
dev_err(mali_c55->dev, "%s vb2 queue init failed\n",
cap_dev->vdev.name);
goto err_cleanup_media_entity;
}
strscpy(cap_dev->vdev.name, capture_device_names[cap_dev_id],
sizeof(cap_dev->vdev.name));
vdev->release = video_device_release_empty;
vdev->fops = &mali_c55_v4l2_fops;
vdev->ioctl_ops = &mali_c55_v4l2_ioctl_ops;
vdev->lock = &cap_dev->lock;
vdev->v4l2_dev = &mali_c55->v4l2_dev;
vdev->queue = &cap_dev->queue;
vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE_MPLANE |
V4L2_CAP_STREAMING | V4L2_CAP_IO_MC;
vdev->entity.ops = &mali_c55_media_ops;
video_set_drvdata(vdev, cap_dev);
memset(&pix_mp, 0, sizeof(pix_mp));
pix_mp.pixelformat = V4L2_PIX_FMT_RGB565;
pix_mp.width = MALI_C55_DEFAULT_WIDTH;
pix_mp.height = MALI_C55_DEFAULT_HEIGHT;
mali_c55_set_format(cap_dev, &pix_mp);
ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1);
if (ret) {
dev_err(mali_c55->dev,
"%s failed to register video device\n",
cap_dev->vdev.name);
goto err_release_vb2q;
}
return 0;
err_release_vb2q:
vb2_queue_release(vb2q);
err_cleanup_media_entity:
media_entity_cleanup(&cap_dev->vdev.entity);
err_destroy_mutex:
mutex_destroy(&cap_dev->lock);
return ret;
}
int mali_c55_register_capture_devs(struct mali_c55 *mali_c55)
{
int ret;
ret = mali_c55_register_cap_dev(mali_c55, MALI_C55_CAP_DEV_FR);
if (ret)
return ret;
if (mali_c55->capabilities & MALI_C55_GPS_DS_PIPE_FITTED) {
ret = mali_c55_register_cap_dev(mali_c55, MALI_C55_CAP_DEV_DS);
if (ret) {
mali_c55_unregister_capture_devs(mali_c55);
return ret;
}
}
return 0;
}
static void mali_c55_unregister_cap_dev(struct mali_c55 *mali_c55,
enum mali_c55_cap_devs cap_dev_id)
{
struct mali_c55_cap_dev *cap_dev = &mali_c55->cap_devs[cap_dev_id];
if (!video_is_registered(&cap_dev->vdev))
return;
vb2_video_unregister_device(&cap_dev->vdev);
media_entity_cleanup(&cap_dev->vdev.entity);
mutex_destroy(&cap_dev->lock);
}
void mali_c55_unregister_capture_devs(struct mali_c55 *mali_c55)
{
mali_c55_unregister_cap_dev(mali_c55, MALI_C55_CAP_DEV_FR);
if (mali_c55->capabilities & MALI_C55_GPS_DS_PIPE_FITTED)
mali_c55_unregister_cap_dev(mali_c55, MALI_C55_CAP_DEV_DS);
}
|