forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 449
/
Copy pathv4l2-fwnode.c
1299 lines (1115 loc) · 32.8 KB
/
v4l2-fwnode.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-only
/*
* V4L2 fwnode binding parsing library
*
* The origins of the V4L2 fwnode library are in V4L2 OF library that
* formerly was located in v4l2-of.c.
*
* Copyright (c) 2016 Intel Corporation.
* Author: Sakari Ailus <[email protected]>
*
* Copyright (C) 2012 - 2013 Samsung Electronics Co., Ltd.
* Author: Sylwester Nawrocki <[email protected]>
*
* Copyright (C) 2012 Renesas Electronics Corp.
* Author: Guennadi Liakhovetski <[email protected]>
*/
#include <linux/acpi.h>
#include <linux/kernel.h>
#include <linux/mm.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/property.h>
#include <linux/slab.h>
#include <linux/string.h>
#include <linux/types.h>
#include <media/v4l2-async.h>
#include <media/v4l2-fwnode.h>
#include <media/v4l2-subdev.h>
#include "v4l2-subdev-priv.h"
static const struct v4l2_fwnode_bus_conv {
enum v4l2_fwnode_bus_type fwnode_bus_type;
enum v4l2_mbus_type mbus_type;
const char *name;
} buses[] = {
{
V4L2_FWNODE_BUS_TYPE_GUESS,
V4L2_MBUS_UNKNOWN,
"not specified",
}, {
V4L2_FWNODE_BUS_TYPE_CSI2_CPHY,
V4L2_MBUS_CSI2_CPHY,
"MIPI CSI-2 C-PHY",
}, {
V4L2_FWNODE_BUS_TYPE_CSI1,
V4L2_MBUS_CSI1,
"MIPI CSI-1",
}, {
V4L2_FWNODE_BUS_TYPE_CCP2,
V4L2_MBUS_CCP2,
"compact camera port 2",
}, {
V4L2_FWNODE_BUS_TYPE_CSI2_DPHY,
V4L2_MBUS_CSI2_DPHY,
"MIPI CSI-2 D-PHY",
}, {
V4L2_FWNODE_BUS_TYPE_PARALLEL,
V4L2_MBUS_PARALLEL,
"parallel",
}, {
V4L2_FWNODE_BUS_TYPE_BT656,
V4L2_MBUS_BT656,
"Bt.656",
}, {
V4L2_FWNODE_BUS_TYPE_DPI,
V4L2_MBUS_DPI,
"DPI",
}
};
static const struct v4l2_fwnode_bus_conv *
get_v4l2_fwnode_bus_conv_by_fwnode_bus(enum v4l2_fwnode_bus_type type)
{
unsigned int i;
for (i = 0; i < ARRAY_SIZE(buses); i++)
if (buses[i].fwnode_bus_type == type)
return &buses[i];
return NULL;
}
static enum v4l2_mbus_type
v4l2_fwnode_bus_type_to_mbus(enum v4l2_fwnode_bus_type type)
{
const struct v4l2_fwnode_bus_conv *conv =
get_v4l2_fwnode_bus_conv_by_fwnode_bus(type);
return conv ? conv->mbus_type : V4L2_MBUS_INVALID;
}
static const char *
v4l2_fwnode_bus_type_to_string(enum v4l2_fwnode_bus_type type)
{
const struct v4l2_fwnode_bus_conv *conv =
get_v4l2_fwnode_bus_conv_by_fwnode_bus(type);
return conv ? conv->name : "not found";
}
static const struct v4l2_fwnode_bus_conv *
get_v4l2_fwnode_bus_conv_by_mbus(enum v4l2_mbus_type type)
{
unsigned int i;
for (i = 0; i < ARRAY_SIZE(buses); i++)
if (buses[i].mbus_type == type)
return &buses[i];
return NULL;
}
static const char *
v4l2_fwnode_mbus_type_to_string(enum v4l2_mbus_type type)
{
const struct v4l2_fwnode_bus_conv *conv =
get_v4l2_fwnode_bus_conv_by_mbus(type);
return conv ? conv->name : "not found";
}
static int v4l2_fwnode_endpoint_parse_csi2_bus(struct fwnode_handle *fwnode,
struct v4l2_fwnode_endpoint *vep,
enum v4l2_mbus_type bus_type)
{
struct v4l2_mbus_config_mipi_csi2 *bus = &vep->bus.mipi_csi2;
bool have_clk_lane = false, have_data_lanes = false,
have_lane_polarities = false, have_line_orders = false;
unsigned int flags = 0, lanes_used = 0;
u32 array[1 + V4L2_MBUS_CSI2_MAX_DATA_LANES];
u32 clock_lane = 0;
unsigned int num_data_lanes = 0;
bool use_default_lane_mapping = false;
unsigned int i;
u32 v;
int rval;
if (bus_type == V4L2_MBUS_CSI2_DPHY ||
bus_type == V4L2_MBUS_CSI2_CPHY) {
use_default_lane_mapping = true;
num_data_lanes = min_t(u32, bus->num_data_lanes,
V4L2_MBUS_CSI2_MAX_DATA_LANES);
clock_lane = bus->clock_lane;
if (clock_lane)
use_default_lane_mapping = false;
for (i = 0; i < num_data_lanes; i++) {
array[i] = bus->data_lanes[i];
if (array[i])
use_default_lane_mapping = false;
}
if (use_default_lane_mapping)
pr_debug("no lane mapping given, using defaults\n");
}
rval = fwnode_property_count_u32(fwnode, "data-lanes");
if (rval > 0) {
num_data_lanes =
min_t(int, V4L2_MBUS_CSI2_MAX_DATA_LANES, rval);
fwnode_property_read_u32_array(fwnode, "data-lanes", array,
num_data_lanes);
have_data_lanes = true;
if (use_default_lane_mapping) {
pr_debug("data-lanes property exists; disabling default mapping\n");
use_default_lane_mapping = false;
}
}
for (i = 0; i < num_data_lanes; i++) {
if (lanes_used & BIT(array[i])) {
if (have_data_lanes || !use_default_lane_mapping)
pr_warn("duplicated lane %u in data-lanes, using defaults\n",
array[i]);
use_default_lane_mapping = true;
}
lanes_used |= BIT(array[i]);
if (have_data_lanes)
pr_debug("lane %u position %u\n", i, array[i]);
}
rval = fwnode_property_count_u32(fwnode, "lane-polarities");
if (rval > 0) {
if (rval != 1 + num_data_lanes /* clock+data */) {
pr_warn("invalid number of lane-polarities entries (need %u, got %u)\n",
1 + num_data_lanes, rval);
return -EINVAL;
}
have_lane_polarities = true;
}
rval = fwnode_property_count_u32(fwnode, "line-orders");
if (rval > 0) {
if (rval != num_data_lanes) {
pr_warn("invalid number of line-orders entries (need %u, got %u)\n",
num_data_lanes, rval);
return -EINVAL;
}
have_line_orders = true;
}
if (!fwnode_property_read_u32(fwnode, "clock-lanes", &v)) {
clock_lane = v;
pr_debug("clock lane position %u\n", v);
have_clk_lane = true;
}
if (have_clk_lane && lanes_used & BIT(clock_lane) &&
!use_default_lane_mapping) {
pr_warn("duplicated lane %u in clock-lanes, using defaults\n",
v);
use_default_lane_mapping = true;
}
if (fwnode_property_present(fwnode, "clock-noncontinuous")) {
flags |= V4L2_MBUS_CSI2_NONCONTINUOUS_CLOCK;
pr_debug("non-continuous clock\n");
}
if (bus_type == V4L2_MBUS_CSI2_DPHY ||
bus_type == V4L2_MBUS_CSI2_CPHY ||
lanes_used || have_clk_lane || flags) {
/* Only D-PHY has a clock lane. */
unsigned int dfl_data_lane_index =
bus_type == V4L2_MBUS_CSI2_DPHY;
bus->flags = flags;
if (bus_type == V4L2_MBUS_UNKNOWN)
vep->bus_type = V4L2_MBUS_CSI2_DPHY;
bus->num_data_lanes = num_data_lanes;
if (use_default_lane_mapping) {
bus->clock_lane = 0;
for (i = 0; i < num_data_lanes; i++)
bus->data_lanes[i] = dfl_data_lane_index + i;
} else {
bus->clock_lane = clock_lane;
for (i = 0; i < num_data_lanes; i++)
bus->data_lanes[i] = array[i];
}
if (have_lane_polarities) {
fwnode_property_read_u32_array(fwnode,
"lane-polarities", array,
1 + num_data_lanes);
for (i = 0; i < 1 + num_data_lanes; i++) {
bus->lane_polarities[i] = array[i];
pr_debug("lane %u polarity %sinverted",
i, array[i] ? "" : "not ");
}
} else {
pr_debug("no lane polarities defined, assuming not inverted\n");
}
if (have_line_orders) {
fwnode_property_read_u32_array(fwnode,
"line-orders", array,
num_data_lanes);
for (i = 0; i < num_data_lanes; i++) {
static const char * const orders[] = {
"ABC", "ACB", "BAC", "BCA", "CAB", "CBA"
};
if (array[i] >= ARRAY_SIZE(orders)) {
pr_warn("lane %u invalid line-order assuming ABC (got %u)\n",
i, array[i]);
bus->line_orders[i] =
V4L2_MBUS_CSI2_CPHY_LINE_ORDER_ABC;
continue;
}
bus->line_orders[i] = array[i];
pr_debug("lane %u line order %s", i,
orders[array[i]]);
}
} else {
for (i = 0; i < num_data_lanes; i++)
bus->line_orders[i] =
V4L2_MBUS_CSI2_CPHY_LINE_ORDER_ABC;
pr_debug("no line orders defined, assuming ABC\n");
}
}
return 0;
}
#define PARALLEL_MBUS_FLAGS (V4L2_MBUS_HSYNC_ACTIVE_HIGH | \
V4L2_MBUS_HSYNC_ACTIVE_LOW | \
V4L2_MBUS_VSYNC_ACTIVE_HIGH | \
V4L2_MBUS_VSYNC_ACTIVE_LOW | \
V4L2_MBUS_FIELD_EVEN_HIGH | \
V4L2_MBUS_FIELD_EVEN_LOW)
static void
v4l2_fwnode_endpoint_parse_parallel_bus(struct fwnode_handle *fwnode,
struct v4l2_fwnode_endpoint *vep,
enum v4l2_mbus_type bus_type)
{
struct v4l2_mbus_config_parallel *bus = &vep->bus.parallel;
unsigned int flags = 0;
u32 v;
if (bus_type == V4L2_MBUS_PARALLEL || bus_type == V4L2_MBUS_BT656)
flags = bus->flags;
if (!fwnode_property_read_u32(fwnode, "hsync-active", &v)) {
flags &= ~(V4L2_MBUS_HSYNC_ACTIVE_HIGH |
V4L2_MBUS_HSYNC_ACTIVE_LOW);
flags |= v ? V4L2_MBUS_HSYNC_ACTIVE_HIGH :
V4L2_MBUS_HSYNC_ACTIVE_LOW;
pr_debug("hsync-active %s\n", v ? "high" : "low");
}
if (!fwnode_property_read_u32(fwnode, "vsync-active", &v)) {
flags &= ~(V4L2_MBUS_VSYNC_ACTIVE_HIGH |
V4L2_MBUS_VSYNC_ACTIVE_LOW);
flags |= v ? V4L2_MBUS_VSYNC_ACTIVE_HIGH :
V4L2_MBUS_VSYNC_ACTIVE_LOW;
pr_debug("vsync-active %s\n", v ? "high" : "low");
}
if (!fwnode_property_read_u32(fwnode, "field-even-active", &v)) {
flags &= ~(V4L2_MBUS_FIELD_EVEN_HIGH |
V4L2_MBUS_FIELD_EVEN_LOW);
flags |= v ? V4L2_MBUS_FIELD_EVEN_HIGH :
V4L2_MBUS_FIELD_EVEN_LOW;
pr_debug("field-even-active %s\n", v ? "high" : "low");
}
if (!fwnode_property_read_u32(fwnode, "pclk-sample", &v)) {
flags &= ~(V4L2_MBUS_PCLK_SAMPLE_RISING |
V4L2_MBUS_PCLK_SAMPLE_FALLING |
V4L2_MBUS_PCLK_SAMPLE_DUALEDGE);
switch (v) {
case 0:
flags |= V4L2_MBUS_PCLK_SAMPLE_FALLING;
pr_debug("pclk-sample low\n");
break;
case 1:
flags |= V4L2_MBUS_PCLK_SAMPLE_RISING;
pr_debug("pclk-sample high\n");
break;
case 2:
flags |= V4L2_MBUS_PCLK_SAMPLE_DUALEDGE;
pr_debug("pclk-sample dual edge\n");
break;
default:
pr_warn("invalid argument for pclk-sample");
break;
}
}
if (!fwnode_property_read_u32(fwnode, "data-active", &v)) {
flags &= ~(V4L2_MBUS_DATA_ACTIVE_HIGH |
V4L2_MBUS_DATA_ACTIVE_LOW);
flags |= v ? V4L2_MBUS_DATA_ACTIVE_HIGH :
V4L2_MBUS_DATA_ACTIVE_LOW;
pr_debug("data-active %s\n", v ? "high" : "low");
}
if (fwnode_property_present(fwnode, "slave-mode")) {
pr_debug("slave mode\n");
flags &= ~V4L2_MBUS_MASTER;
flags |= V4L2_MBUS_SLAVE;
} else {
flags &= ~V4L2_MBUS_SLAVE;
flags |= V4L2_MBUS_MASTER;
}
if (!fwnode_property_read_u32(fwnode, "bus-width", &v)) {
bus->bus_width = v;
pr_debug("bus-width %u\n", v);
}
if (!fwnode_property_read_u32(fwnode, "data-shift", &v)) {
bus->data_shift = v;
pr_debug("data-shift %u\n", v);
}
if (!fwnode_property_read_u32(fwnode, "sync-on-green-active", &v)) {
flags &= ~(V4L2_MBUS_VIDEO_SOG_ACTIVE_HIGH |
V4L2_MBUS_VIDEO_SOG_ACTIVE_LOW);
flags |= v ? V4L2_MBUS_VIDEO_SOG_ACTIVE_HIGH :
V4L2_MBUS_VIDEO_SOG_ACTIVE_LOW;
pr_debug("sync-on-green-active %s\n", v ? "high" : "low");
}
if (!fwnode_property_read_u32(fwnode, "data-enable-active", &v)) {
flags &= ~(V4L2_MBUS_DATA_ENABLE_HIGH |
V4L2_MBUS_DATA_ENABLE_LOW);
flags |= v ? V4L2_MBUS_DATA_ENABLE_HIGH :
V4L2_MBUS_DATA_ENABLE_LOW;
pr_debug("data-enable-active %s\n", v ? "high" : "low");
}
switch (bus_type) {
default:
bus->flags = flags;
if (flags & PARALLEL_MBUS_FLAGS)
vep->bus_type = V4L2_MBUS_PARALLEL;
else
vep->bus_type = V4L2_MBUS_BT656;
break;
case V4L2_MBUS_PARALLEL:
vep->bus_type = V4L2_MBUS_PARALLEL;
bus->flags = flags;
break;
case V4L2_MBUS_BT656:
vep->bus_type = V4L2_MBUS_BT656;
bus->flags = flags & ~PARALLEL_MBUS_FLAGS;
break;
}
}
static void
v4l2_fwnode_endpoint_parse_csi1_bus(struct fwnode_handle *fwnode,
struct v4l2_fwnode_endpoint *vep,
enum v4l2_mbus_type bus_type)
{
struct v4l2_mbus_config_mipi_csi1 *bus = &vep->bus.mipi_csi1;
u32 v;
if (!fwnode_property_read_u32(fwnode, "clock-inv", &v)) {
bus->clock_inv = v;
pr_debug("clock-inv %u\n", v);
}
if (!fwnode_property_read_u32(fwnode, "strobe", &v)) {
bus->strobe = v;
pr_debug("strobe %u\n", v);
}
if (!fwnode_property_read_u32(fwnode, "data-lanes", &v)) {
bus->data_lane = v;
pr_debug("data-lanes %u\n", v);
}
if (!fwnode_property_read_u32(fwnode, "clock-lanes", &v)) {
bus->clock_lane = v;
pr_debug("clock-lanes %u\n", v);
}
if (bus_type == V4L2_MBUS_CCP2)
vep->bus_type = V4L2_MBUS_CCP2;
else
vep->bus_type = V4L2_MBUS_CSI1;
}
static int __v4l2_fwnode_endpoint_parse(struct fwnode_handle *fwnode,
struct v4l2_fwnode_endpoint *vep)
{
u32 bus_type = V4L2_FWNODE_BUS_TYPE_GUESS;
enum v4l2_mbus_type mbus_type;
int rval;
pr_debug("===== begin parsing endpoint %pfw\n", fwnode);
fwnode_property_read_u32(fwnode, "bus-type", &bus_type);
pr_debug("fwnode video bus type %s (%u), mbus type %s (%u)\n",
v4l2_fwnode_bus_type_to_string(bus_type), bus_type,
v4l2_fwnode_mbus_type_to_string(vep->bus_type),
vep->bus_type);
mbus_type = v4l2_fwnode_bus_type_to_mbus(bus_type);
if (mbus_type == V4L2_MBUS_INVALID) {
pr_debug("unsupported bus type %u\n", bus_type);
return -EINVAL;
}
if (vep->bus_type != V4L2_MBUS_UNKNOWN) {
if (mbus_type != V4L2_MBUS_UNKNOWN &&
vep->bus_type != mbus_type) {
pr_debug("expecting bus type %s\n",
v4l2_fwnode_mbus_type_to_string(vep->bus_type));
return -ENXIO;
}
} else {
vep->bus_type = mbus_type;
}
switch (vep->bus_type) {
case V4L2_MBUS_UNKNOWN:
rval = v4l2_fwnode_endpoint_parse_csi2_bus(fwnode, vep,
V4L2_MBUS_UNKNOWN);
if (rval)
return rval;
if (vep->bus_type == V4L2_MBUS_UNKNOWN)
v4l2_fwnode_endpoint_parse_parallel_bus(fwnode, vep,
V4L2_MBUS_UNKNOWN);
pr_debug("assuming media bus type %s (%u)\n",
v4l2_fwnode_mbus_type_to_string(vep->bus_type),
vep->bus_type);
break;
case V4L2_MBUS_CCP2:
case V4L2_MBUS_CSI1:
v4l2_fwnode_endpoint_parse_csi1_bus(fwnode, vep, vep->bus_type);
break;
case V4L2_MBUS_CSI2_DPHY:
case V4L2_MBUS_CSI2_CPHY:
rval = v4l2_fwnode_endpoint_parse_csi2_bus(fwnode, vep,
vep->bus_type);
if (rval)
return rval;
break;
case V4L2_MBUS_PARALLEL:
case V4L2_MBUS_BT656:
v4l2_fwnode_endpoint_parse_parallel_bus(fwnode, vep,
vep->bus_type);
break;
default:
pr_warn("unsupported bus type %u\n", mbus_type);
return -EINVAL;
}
fwnode_graph_parse_endpoint(fwnode, &vep->base);
return 0;
}
int v4l2_fwnode_endpoint_parse(struct fwnode_handle *fwnode,
struct v4l2_fwnode_endpoint *vep)
{
int ret;
ret = __v4l2_fwnode_endpoint_parse(fwnode, vep);
pr_debug("===== end parsing endpoint %pfw\n", fwnode);
return ret;
}
EXPORT_SYMBOL_GPL(v4l2_fwnode_endpoint_parse);
void v4l2_fwnode_endpoint_free(struct v4l2_fwnode_endpoint *vep)
{
if (IS_ERR_OR_NULL(vep))
return;
kfree(vep->link_frequencies);
vep->link_frequencies = NULL;
}
EXPORT_SYMBOL_GPL(v4l2_fwnode_endpoint_free);
int v4l2_fwnode_endpoint_alloc_parse(struct fwnode_handle *fwnode,
struct v4l2_fwnode_endpoint *vep)
{
int rval;
rval = __v4l2_fwnode_endpoint_parse(fwnode, vep);
if (rval < 0)
return rval;
rval = fwnode_property_count_u64(fwnode, "link-frequencies");
if (rval > 0) {
unsigned int i;
vep->link_frequencies =
kmalloc_array(rval, sizeof(*vep->link_frequencies),
GFP_KERNEL);
if (!vep->link_frequencies)
return -ENOMEM;
vep->nr_of_link_frequencies = rval;
rval = fwnode_property_read_u64_array(fwnode,
"link-frequencies",
vep->link_frequencies,
vep->nr_of_link_frequencies);
if (rval < 0) {
v4l2_fwnode_endpoint_free(vep);
return rval;
}
for (i = 0; i < vep->nr_of_link_frequencies; i++)
pr_debug("link-frequencies %u value %llu\n", i,
vep->link_frequencies[i]);
}
pr_debug("===== end parsing endpoint %pfw\n", fwnode);
return 0;
}
EXPORT_SYMBOL_GPL(v4l2_fwnode_endpoint_alloc_parse);
int v4l2_fwnode_parse_link(struct fwnode_handle *fwnode,
struct v4l2_fwnode_link *link)
{
struct fwnode_endpoint fwep;
memset(link, 0, sizeof(*link));
fwnode_graph_parse_endpoint(fwnode, &fwep);
link->local_id = fwep.id;
link->local_port = fwep.port;
link->local_node = fwnode_graph_get_port_parent(fwnode);
if (!link->local_node)
return -ENOLINK;
fwnode = fwnode_graph_get_remote_endpoint(fwnode);
if (!fwnode)
goto err_put_local_node;
fwnode_graph_parse_endpoint(fwnode, &fwep);
link->remote_id = fwep.id;
link->remote_port = fwep.port;
link->remote_node = fwnode_graph_get_port_parent(fwnode);
if (!link->remote_node)
goto err_put_remote_endpoint;
return 0;
err_put_remote_endpoint:
fwnode_handle_put(fwnode);
err_put_local_node:
fwnode_handle_put(link->local_node);
return -ENOLINK;
}
EXPORT_SYMBOL_GPL(v4l2_fwnode_parse_link);
void v4l2_fwnode_put_link(struct v4l2_fwnode_link *link)
{
fwnode_handle_put(link->local_node);
fwnode_handle_put(link->remote_node);
}
EXPORT_SYMBOL_GPL(v4l2_fwnode_put_link);
static const struct v4l2_fwnode_connector_conv {
enum v4l2_connector_type type;
const char *compatible;
} connectors[] = {
{
.type = V4L2_CONN_COMPOSITE,
.compatible = "composite-video-connector",
}, {
.type = V4L2_CONN_SVIDEO,
.compatible = "svideo-connector",
},
};
static enum v4l2_connector_type
v4l2_fwnode_string_to_connector_type(const char *con_str)
{
unsigned int i;
for (i = 0; i < ARRAY_SIZE(connectors); i++)
if (!strcmp(con_str, connectors[i].compatible))
return connectors[i].type;
return V4L2_CONN_UNKNOWN;
}
static void
v4l2_fwnode_connector_parse_analog(struct fwnode_handle *fwnode,
struct v4l2_fwnode_connector *vc)
{
u32 stds;
int ret;
ret = fwnode_property_read_u32(fwnode, "sdtv-standards", &stds);
/* The property is optional. */
vc->connector.analog.sdtv_stds = ret ? V4L2_STD_ALL : stds;
}
void v4l2_fwnode_connector_free(struct v4l2_fwnode_connector *connector)
{
struct v4l2_connector_link *link, *tmp;
if (IS_ERR_OR_NULL(connector) || connector->type == V4L2_CONN_UNKNOWN)
return;
list_for_each_entry_safe(link, tmp, &connector->links, head) {
v4l2_fwnode_put_link(&link->fwnode_link);
list_del(&link->head);
kfree(link);
}
kfree(connector->label);
connector->label = NULL;
connector->type = V4L2_CONN_UNKNOWN;
}
EXPORT_SYMBOL_GPL(v4l2_fwnode_connector_free);
static enum v4l2_connector_type
v4l2_fwnode_get_connector_type(struct fwnode_handle *fwnode)
{
const char *type_name;
int err;
if (!fwnode)
return V4L2_CONN_UNKNOWN;
/* The connector-type is stored within the compatible string. */
err = fwnode_property_read_string(fwnode, "compatible", &type_name);
if (err)
return V4L2_CONN_UNKNOWN;
return v4l2_fwnode_string_to_connector_type(type_name);
}
int v4l2_fwnode_connector_parse(struct fwnode_handle *fwnode,
struct v4l2_fwnode_connector *connector)
{
struct fwnode_handle *connector_node;
enum v4l2_connector_type connector_type;
const char *label;
int err;
if (!fwnode)
return -EINVAL;
memset(connector, 0, sizeof(*connector));
INIT_LIST_HEAD(&connector->links);
connector_node = fwnode_graph_get_port_parent(fwnode);
connector_type = v4l2_fwnode_get_connector_type(connector_node);
if (connector_type == V4L2_CONN_UNKNOWN) {
fwnode_handle_put(connector_node);
connector_node = fwnode_graph_get_remote_port_parent(fwnode);
connector_type = v4l2_fwnode_get_connector_type(connector_node);
}
if (connector_type == V4L2_CONN_UNKNOWN) {
pr_err("Unknown connector type\n");
err = -ENOTCONN;
goto out;
}
connector->type = connector_type;
connector->name = fwnode_get_name(connector_node);
err = fwnode_property_read_string(connector_node, "label", &label);
connector->label = err ? NULL : kstrdup_const(label, GFP_KERNEL);
/* Parse the connector specific properties. */
switch (connector->type) {
case V4L2_CONN_COMPOSITE:
case V4L2_CONN_SVIDEO:
v4l2_fwnode_connector_parse_analog(connector_node, connector);
break;
/* Avoid compiler warnings */
case V4L2_CONN_UNKNOWN:
break;
}
out:
fwnode_handle_put(connector_node);
return err;
}
EXPORT_SYMBOL_GPL(v4l2_fwnode_connector_parse);
int v4l2_fwnode_connector_add_link(struct fwnode_handle *fwnode,
struct v4l2_fwnode_connector *connector)
{
struct fwnode_handle *connector_ep;
struct v4l2_connector_link *link;
int err;
if (!fwnode || !connector || connector->type == V4L2_CONN_UNKNOWN)
return -EINVAL;
connector_ep = fwnode_graph_get_remote_endpoint(fwnode);
if (!connector_ep)
return -ENOTCONN;
link = kzalloc(sizeof(*link), GFP_KERNEL);
if (!link) {
err = -ENOMEM;
goto err;
}
err = v4l2_fwnode_parse_link(connector_ep, &link->fwnode_link);
if (err)
goto err;
fwnode_handle_put(connector_ep);
list_add(&link->head, &connector->links);
connector->nr_of_links++;
return 0;
err:
kfree(link);
fwnode_handle_put(connector_ep);
return err;
}
EXPORT_SYMBOL_GPL(v4l2_fwnode_connector_add_link);
int v4l2_fwnode_device_parse(struct device *dev,
struct v4l2_fwnode_device_properties *props)
{
struct fwnode_handle *fwnode = dev_fwnode(dev);
u32 val;
int ret;
memset(props, 0, sizeof(*props));
props->orientation = V4L2_FWNODE_PROPERTY_UNSET;
ret = fwnode_property_read_u32(fwnode, "orientation", &val);
if (!ret) {
switch (val) {
case V4L2_FWNODE_ORIENTATION_FRONT:
case V4L2_FWNODE_ORIENTATION_BACK:
case V4L2_FWNODE_ORIENTATION_EXTERNAL:
break;
default:
dev_warn(dev, "Unsupported device orientation: %u\n", val);
return -EINVAL;
}
props->orientation = val;
dev_dbg(dev, "device orientation: %u\n", val);
}
props->rotation = V4L2_FWNODE_PROPERTY_UNSET;
ret = fwnode_property_read_u32(fwnode, "rotation", &val);
if (!ret) {
if (val >= 360) {
dev_warn(dev, "Unsupported device rotation: %u\n", val);
return -EINVAL;
}
props->rotation = val;
dev_dbg(dev, "device rotation: %u\n", val);
}
return 0;
}
EXPORT_SYMBOL_GPL(v4l2_fwnode_device_parse);
/*
* v4l2_fwnode_reference_parse - parse references for async sub-devices
* @dev: the device node the properties of which are parsed for references
* @notifier: the async notifier where the async subdevs will be added
* @prop: the name of the property
*
* Return: 0 on success
* -ENOENT if no entries were found
* -ENOMEM if memory allocation failed
* -EINVAL if property parsing failed
*/
static int v4l2_fwnode_reference_parse(struct device *dev,
struct v4l2_async_notifier *notifier,
const char *prop)
{
struct fwnode_reference_args args;
unsigned int index;
int ret;
for (index = 0;
!(ret = fwnode_property_get_reference_args(dev_fwnode(dev), prop,
NULL, 0, index, &args));
index++) {
struct v4l2_async_connection *asd;
asd = v4l2_async_nf_add_fwnode(notifier, args.fwnode,
struct v4l2_async_connection);
fwnode_handle_put(args.fwnode);
if (IS_ERR(asd)) {
/* not an error if asd already exists */
if (PTR_ERR(asd) == -EEXIST)
continue;
return PTR_ERR(asd);
}
}
/* -ENOENT here means successful parsing */
if (ret != -ENOENT)
return ret;
/* Return -ENOENT if no references were found */
return index ? 0 : -ENOENT;
}
/*
* v4l2_fwnode_reference_get_int_prop - parse a reference with integer
* arguments
* @fwnode: fwnode to read @prop from
* @notifier: notifier for @dev
* @prop: the name of the property
* @index: the index of the reference to get
* @props: the array of integer property names
* @nprops: the number of integer property names in @nprops
*
* First find an fwnode referred to by the reference at @index in @prop.
*
* Then under that fwnode, @nprops times, for each property in @props,
* iteratively follow child nodes starting from fwnode such that they have the
* property in @props array at the index of the child node distance from the
* root node and the value of that property matching with the integer argument
* of the reference, at the same index.
*
* The child fwnode reached at the end of the iteration is then returned to the
* caller.
*
* The core reason for this is that you cannot refer to just any node in ACPI.
* So to refer to an endpoint (easy in DT) you need to refer to a device, then
* provide a list of (property name, property value) tuples where each tuple
* uniquely identifies a child node. The first tuple identifies a child directly
* underneath the device fwnode, the next tuple identifies a child node
* underneath the fwnode identified by the previous tuple, etc. until you
* reached the fwnode you need.
*
* THIS EXAMPLE EXISTS MERELY TO DOCUMENT THIS FUNCTION. DO NOT USE IT AS A
* REFERENCE IN HOW ACPI TABLES SHOULD BE WRITTEN!! See documentation under
* Documentation/firmware-guide/acpi/dsd/ instead and especially graph.txt,
* data-node-references.txt and leds.txt .
*
* Scope (\_SB.PCI0.I2C2)
* {
* Device (CAM0)
* {
* Name (_DSD, Package () {
* ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
* Package () {
* Package () {
* "compatible",
* Package () { "nokia,smia" }
* },
* },
* ToUUID("dbb8e3e6-5886-4ba6-8795-1319f52a966b"),
* Package () {
* Package () { "port0", "PRT0" },
* }
* })
* Name (PRT0, Package() {
* ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
* Package () {
* Package () { "port", 0 },
* },
* ToUUID("dbb8e3e6-5886-4ba6-8795-1319f52a966b"),
* Package () {
* Package () { "endpoint0", "EP00" },
* }
* })
* Name (EP00, Package() {
* ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
* Package () {
* Package () { "endpoint", 0 },
* Package () {
* "remote-endpoint",
* Package() {
* \_SB.PCI0.ISP, 4, 0
* }
* },
* }
* })
* }
* }
*
* Scope (\_SB.PCI0)
* {
* Device (ISP)
* {
* Name (_DSD, Package () {
* ToUUID("dbb8e3e6-5886-4ba6-8795-1319f52a966b"),
* Package () {
* Package () { "port4", "PRT4" },
* }
* })
*
* Name (PRT4, Package() {
* ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
* Package () {
* Package () { "port", 4 },
* },
* ToUUID("dbb8e3e6-5886-4ba6-8795-1319f52a966b"),
* Package () {
* Package () { "endpoint0", "EP40" },
* }
* })
*
* Name (EP40, Package() {
* ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
* Package () {
* Package () { "endpoint", 0 },
* Package () {
* "remote-endpoint",