-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsinfo.c
More file actions
1565 lines (1397 loc) · 39.2 KB
/
Copy pathsinfo.c
File metadata and controls
1565 lines (1397 loc) · 39.2 KB
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 */
/*
* sinfo - userspace Ingenic image sensor detector
*
* Userspace port of the ingenic-sdk sinfo kernel module. Probes the sensor
* I2C bus for every sensor in the database, driving the sensor MCLK (CGU CIM
* clock via /dev/mem) and the reset/pwdn GPIOs (via /sys/class/gpio) the same
* way the kernel module does, then reads and compares ID registers through
* /dev/i2c-N (I2C_RDWR).
*
* One static binary runs on any kernel; no per-kernel-version .ko needed.
*
* Requirements: root, CONFIG_I2C_CHARDEV, CONFIG_GPIO_SYSFS, CONFIG_DEVMEM.
*
* Usage:
* sinfo [-s soc] [-b bus|all] [-m mclk] [-r gpio] [-p gpio] [-v] [command]
* Commands:
* probe scan for sensors and print report (default)
* open <sensor> set MCLK + reset for a named sensor, leave on
* release stop MCLK, free GPIOs
* i2c-r <addr> <len> raw I2C read (like echo i2c-r:... > proc)
* i2c-w <addr> <data> <len> raw I2C write (like echo i2c-w:... > proc)
* Run sinfo -h for the option reference.
*/
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>
#include <errno.h>
#include <linux/i2c.h>
#include "sensors.h"
#include "sinfo_hw.h"
#define MAX_DETECTED_SENSORS 4
#define MAX_I2C_SCAN_RESULTS 128
#define MAX_I2C_BUSES 8
#define EXTAL_KHZ 24000u
/* ---------------------------------------------------------------- SoC data */
/*
* All supported SoCs: CPM at 0x10000000. The CIM MCLK divider register
* (CIMCDR on XBurst1 at +0x7c, CIM0CDR/CIM1CDR on XBurst2 at +0x90/+0x94)
* shares one layout: parent mux in the top bits, CE bit 29, BUSY bit 28,
* STOP bit 27, 8-bit divider. Parent maps and PLL register formats differ
* per SoC (source: ingenic-u-boot-xburst1/-xburst2 per-SoC clk.c/cpm.h).
*/
#define CPM_PHYS 0x10000000
#define GPIO_PHYS 0x10010000
#define CE_BIT 29
#define BUSY_BIT 28
#define STOP_BIT 27
#define DIV_MASK 0xff
enum { PLL_NONE = 0, PLL_A, PLL_M, PLL_V, PLL_E };
/* rate = EXTAL * m/n/od0/od1 m[31:20] n[19:14] od1[13:11] od0[10:8] */
#define PLLSTYLE_NEW 0
/* rate = EXTAL * 2*(m+1)/(n+1)/2^od m[28:20] n[19:14] od[13:11] */
#define PLLSTYLE_OLD 1
/* rate = EXTAL * 2*(m+1)/((n+1) * 2^od0 * (od1+1))
* m[28:20] n[19:14] od1[13:11] od0[10:7] */
#define PLLSTYLE_T41 2
/*
* One selectable MCLK output block. XBurst1 SoCs have a single CIMCDR
* and no pin fixup; T40 has three blocks (CIM0/1/2, pins PC31/30/29);
* T41 has one block whose pin serves every mclk id the vendor SDK
* accepts.
*/
struct mclk_blk {
uint32_t cdr_off; /* MCLK divider register, CPM offset */
int8_t port; /* pin function fixup: GPIO port, -1 = none */
int8_t pin;
int8_t func;
};
struct soc_desc {
const char *name;
int pll_style;
const struct mclk_blk *mclk;
uint8_t num_mclk;
uint8_t default_mclk;
uint8_t mux_shift;
uint8_t mux_width;
uint8_t parent[4]; /* PLL per mux field value */
uint8_t default_mux; /* used when current parent is off/invalid */
int i2c_bus;
int reset_gpio;
int tested; /* HW-validated */
};
static const struct mclk_blk xb1_mclk[] = {
{.cdr_off = 0x7c, .port = -1},
};
/* T40 pins from the vendor set_sensor_mclk_function() (sensor-common.h) */
static const struct mclk_blk t40_mclk[] = {
{.cdr_off = 0x90, .port = 2, .pin = 31, .func = 1},
{.cdr_off = 0x94, .port = 2, .pin = 30, .func = 1},
{.cdr_off = 0x98, .port = 2, .pin = 29, .func = 1},
};
static const struct mclk_blk t41_mclk[] = {
{.cdr_off = 0x90, .port = 0, .pin = 15, .func = 1},
};
/*
* XBurst2 MCLK pin fixups mirror the kernel module's jzgpio_set_func()
* calls: T40 cim1_gpio PC30 FUNC_1, T41 cim_gpio PA15 FUNC_1.
* XBurst1 entries deliberately have none (the module doesn't either;
* the boot chain / a previously loaded sensor driver sets the mux).
*/
static const struct soc_desc soc_table[] = {
{
.name = "c100",
.pll_style = PLLSTYLE_NEW,
.mclk = xb1_mclk,
.num_mclk = 1,
.mux_shift = 30,
.mux_width = 2,
.parent = {PLL_A, PLL_M, PLL_V, PLL_NONE},
.default_mux = 2,
.i2c_bus = 0,
.reset_gpio = 18,
.tested = 0,
},
{
.name = "t10",
.pll_style = PLLSTYLE_NEW,
.mclk = xb1_mclk,
.num_mclk = 1,
.mux_shift = 31,
.mux_width = 1,
.parent = {PLL_A, PLL_M, PLL_NONE, PLL_NONE},
.default_mux = 1,
.i2c_bus = 0,
.reset_gpio = 18,
.tested = 1,
},
{
.name = "t20",
.pll_style = PLLSTYLE_NEW,
.mclk = xb1_mclk,
.num_mclk = 1,
.mux_shift = 30,
.mux_width = 2,
.parent = {PLL_A, PLL_M, PLL_V, PLL_V},
.default_mux = 2,
.i2c_bus = 0,
.reset_gpio = 18,
.tested = 1,
},
{
.name = "t21",
.pll_style = PLLSTYLE_OLD,
.mclk = xb1_mclk,
.num_mclk = 1,
.mux_shift = 30,
.mux_width = 2,
.parent = {PLL_A, PLL_M, PLL_V, PLL_E},
.default_mux = 2,
.i2c_bus = 0,
.reset_gpio = 18,
.tested = 1,
},
{
.name = "t23",
.pll_style = PLLSTYLE_NEW,
.mclk = xb1_mclk,
.num_mclk = 1,
.mux_shift = 30,
.mux_width = 2,
.parent = {PLL_A, PLL_M, PLL_NONE, PLL_NONE},
.default_mux = 1,
.i2c_bus = 0,
.reset_gpio = 18,
.tested = 1,
},
{
.name = "t30",
.pll_style = PLLSTYLE_OLD,
.mclk = xb1_mclk,
.num_mclk = 1,
.mux_shift = 30,
.mux_width = 2,
.parent = {PLL_A, PLL_M, PLL_V, PLL_E},
.default_mux = 2,
.i2c_bus = 0,
.reset_gpio = 18,
.tested = 1,
},
{
.name = "t31",
.pll_style = PLLSTYLE_NEW,
.mclk = xb1_mclk,
.num_mclk = 1,
.mux_shift = 30,
.mux_width = 2,
.parent = {PLL_A, PLL_M, PLL_V, PLL_NONE},
.default_mux = 2,
.i2c_bus = 0,
.reset_gpio = 18,
.tested = 1,
},
{
.name = "t40",
.pll_style = PLLSTYLE_NEW,
.mclk = t40_mclk,
.num_mclk = 3,
.default_mclk = 1,
.mux_shift = 30,
.mux_width = 2,
.parent = {PLL_A, PLL_M, PLL_V, PLL_E},
.default_mux = 2,
.i2c_bus = 1,
.reset_gpio = 91,
.tested = 1,
},
{
.name = "t41",
.pll_style = PLLSTYLE_T41,
.mclk = t41_mclk,
.num_mclk = 1,
.mux_shift = 30,
.mux_width = 2,
.parent = {PLL_A, PLL_M, PLL_V, PLL_NONE},
.default_mux = 2,
.i2c_bus = 0,
.reset_gpio = 92,
.tested = 1,
},
};
#define SOC_COUNT (sizeof(soc_table) / sizeof(soc_table[0]))
/* ------------------------------------------------------------------ state */
static const struct soc_desc *cur_soc;
static const struct mclk_blk *cur_mclk;
static int bus_nr = -1; /* -1 = use SoC default */
static int bus_all;
static int quiet; /* -b all: scan every /dev/i2c-* */
static int mclk_sel = -1; /* -1 = use SoC default */
static int reset_pin = -9999;
static int pwdn_pin = -1;
static int verbose;
/*
* int, not int8_t: the table has ~290 entries, so an int8_t index
* overflows for any sensor past index 127 (the kernel module has this
* exact bug: its int8_t primary_idx corrupts the primary sensor and
* IOCTL_SINFO_GET for the whole SmartSens block).
*/
static int primary_idx = -1;
static int match_idx[MAX_DETECTED_SENSORS];
static int num_matches;
static int reset_owned, pwdn_owned;
static int reset_warned, pwdn_warned;
struct i2c_scan_result {
uint8_t bus;
uint8_t i2c_addr;
uint8_t responded;
uint32_t reg_values[8];
uint32_t reg_addrs[8];
uint8_t num_regs;
char sensor_name[32];
};
static struct i2c_scan_result scan_results[MAX_I2C_SCAN_RESULTS];
static int num_scan_results;
/* observed ID reads per match, for grouping indistinguishable entries */
static struct i2c_scan_result match_res[MAX_DETECTED_SENSORS];
#define vlog(...) \
do { \
if (verbose) \
fprintf(stderr, "sinfo: " __VA_ARGS__); \
} while (0)
#define elog(...) fprintf(stderr, "sinfo: [Error] " __VA_ARGS__)
/* ------------------------------------------------------------- /dev/mem */
static int cim_wait_not_busy(void)
{
int i;
/* BUSY settles in nanoseconds; a bounded spin needs no sleep */
for (i = 0; i < 100000; i++) {
if (!(hw_cpm_rd(cur_mclk->cdr_off) & (1u << BUSY_BIT)))
return 0;
}
elog("CIMCDR busy bit stuck\n");
return -1;
}
/* Rates in kHz: every supported PLL and MCLK rate is kHz-exact, and
* 32-bit kHz math keeps libgcc's 64-bit division helpers unlinked. */
static uint32_t pll_rate(int pll)
{
uint32_t off, r, m, n;
switch (pll) {
case PLL_A:
off = 0x10;
break;
case PLL_M:
off = 0x14;
break;
case PLL_V:
off = 0xe0;
break;
case PLL_E:
off = 0x58;
break;
default:
return 0;
}
r = hw_cpm_rd(off);
if (cur_soc->pll_style == PLLSTYLE_NEW) {
uint32_t od1, od0;
if (!(r & 1)) /* PLL not on */
return 0;
m = (r >> 20) & 0xfff;
n = (r >> 14) & 0x3f;
od1 = (r >> 11) & 0x7;
od0 = (r >> 8) & 0x7;
if (!n || !od0 || !od1)
return 0;
return EXTAL_KHZ * m / n / od0 / od1;
} else if (cur_soc->pll_style == PLLSTYLE_T41) {
uint32_t od1, od0;
if (!(r & 1)) /* PLL not on */
return 0;
m = (r >> 20) & 0x1ff;
n = (r >> 14) & 0x3f;
od1 = (r >> 11) & 0x7;
od0 = (r >> 7) & 0xf;
return EXTAL_KHZ * 2 * (m + 1) / ((n + 1) * (1u << od0) * (od1 + 1));
} else {
uint32_t od;
if (!(r & 1)) /* PLL not on (e.g. T30 EPLL) */
return 0;
m = (r >> 20) & 0x1ff;
n = (r >> 14) & 0x3f;
od = (r >> 11) & 0x7;
return EXTAL_KHZ * 2 * (m + 1) / (n + 1) / (1u << od);
}
}
/*
* Set the CIM MCLK to the requested rate and ungate it. Mirrors what the
* kernel clk framework does for clk_set_rate("cgu_cim")+clk_enable():
* smallest divider with parent/div <= rate, programmed under the CE
* handshake, STOP cleared.
*/
static int mclk_enable(uint32_t hz)
{
uint32_t v, muxmask, mux, div, nv, khz = hz / 1000;
uint32_t prate;
v = hw_cpm_rd(cur_mclk->cdr_off);
muxmask = (1u << cur_soc->mux_width) - 1;
mux = (v >> cur_soc->mux_shift) & muxmask;
prate = pll_rate(cur_soc->parent[mux]);
if (!prate) {
/*
* Unprogrammed/parked CDR, or its parent PLL is off (seen on
* T20: u-boot's nominal CIM parent is VPLL but VPLL is not
* running). Try the SoC default, then any parent whose PLL
* is actually alive.
*/
uint32_t m;
mux = cur_soc->default_mux;
prate = pll_rate(cur_soc->parent[mux]);
for (m = 0; !prate && m <= muxmask; m++) {
mux = m;
prate = pll_rate(cur_soc->parent[mux]);
}
if (!prate) {
elog("no usable CIM parent clock\n");
return -1;
}
}
div = (prate + khz - 1) / khz;
if (div < 1)
div = 1;
if (div > DIV_MASK + 1)
div = DIV_MASK + 1;
if (cim_wait_not_busy())
return -1;
nv = v & ~(muxmask << cur_soc->mux_shift) & ~DIV_MASK & ~(1u << STOP_BIT);
nv |= (mux << cur_soc->mux_shift) | (div - 1) | (1u << CE_BIT);
hw_cpm_wr(cur_mclk->cdr_off, nv);
if (cim_wait_not_busy())
return -1;
hw_cpm_wr(cur_mclk->cdr_off, nv & ~(1u << CE_BIT));
vlog("MCLK: parent %u kHz / %u = %u kHz (CIMCDR 0x%08x)\n", prate, div, prate / div,
hw_cpm_rd(cur_mclk->cdr_off));
return 0;
}
static void mclk_disable(void)
{
uint32_t v;
if (cim_wait_not_busy())
return;
v = hw_cpm_rd(cur_mclk->cdr_off) | (1u << CE_BIT) | (1u << STOP_BIT);
hw_cpm_wr(cur_mclk->cdr_off, v);
cim_wait_not_busy();
hw_cpm_wr(cur_mclk->cdr_off, v & ~(1u << CE_BIT));
}
/*
* T21 kernel module init does: *(volatile u32 *)0xB0010104 = 0x1;
* (GPIO block, phys 0x10010104). Replicated verbatim from the module.
*/
static void t21_init_quirk(void)
{
volatile uint32_t *gpio = hw_map_phys(GPIO_PHYS, 0x1000);
if (gpio)
gpio[0x104 / 4] = 0x1;
}
/*
* XBurst2 MCLK pin function select. Ports sit 0x1000 apart and expose
* set/clear registers for INT/MSK/PAT1/PAT0, so the function bits can be
* programmed without read-modify-write (same scheme as the kernel's
* jzgpio_set_func() and u-boot's gpio_set_func()). Pull config is left
* untouched.
*/
static void xb2_mclk_pin_mux(void)
{
volatile uint32_t *port;
uint32_t pins;
if (cur_mclk->port < 0)
return;
/*
* T40 vendor SDK writes this "VDD select 1.8V" word before muxing
* any MCLK pin (set_sensor_mclk_function() in sensor-common.h).
* Needed when no sensor driver has run since boot.
*/
if (!strcmp(cur_soc->name, "t40")) {
volatile uint32_t *pa = hw_map_phys(GPIO_PHYS, 0x1000);
if (pa)
pa[0x130 / 4] = 0x2aaa000;
}
port = hw_map_phys(GPIO_PHYS + (uint32_t)cur_mclk->port * 0x1000, 0x1000);
if (!port) {
elog("cannot map GPIO port %c\n", 'A' + cur_mclk->port);
return;
}
pins = 1u << cur_mclk->pin;
port[(cur_mclk->func & 0x8 ? 0x14 : 0x18) / 4] = pins; /* INT S/C */
port[(cur_mclk->func & 0x4 ? 0x24 : 0x28) / 4] = pins; /* MSK S/C */
port[(cur_mclk->func & 0x2 ? 0x34 : 0x38) / 4] = pins; /* PAT1 S/C */
port[(cur_mclk->func & 0x1 ? 0x44 : 0x48) / 4] = pins; /* PAT0 S/C */
vlog("MCLK pin mux: P%c%d -> function %d\n", 'A' + cur_mclk->port, cur_mclk->pin,
cur_mclk->func);
}
/* ------------------------------------------------------------ GPIO sysfs */
/*
* Equivalent of gpio_request(): claim via sysfs export. *owned records
* whether the export was ours, so release can restore the pre-run state
* instead of unexporting a pin someone else set up.
*/
static int gpio_claim(int gpio, int *owned)
{
char buf[64], path[80];
*owned = 0;
snprintf(path, sizeof(path), "/sys/class/gpio/gpio%d/direction", gpio);
if (hw_path_writable(path))
return 0;
snprintf(buf, sizeof(buf), "%d", gpio);
if (hw_sysfs_write("/sys/class/gpio/export", buf) < 0)
return -1;
if (!hw_path_writable(path))
return -1;
*owned = 1;
return 0;
}
static void gpio_release(int gpio, int owned)
{
char buf[64];
if (!owned)
return;
snprintf(buf, sizeof(buf), "%d", gpio);
hw_sysfs_write("/sys/class/gpio/unexport", buf);
}
/* Equivalent of gpio_direction_output(): atomic level set via direction. */
static int gpio_out(int gpio, int level)
{
char path[80];
snprintf(path, sizeof(path), "/sys/class/gpio/gpio%d/direction", gpio);
return hw_sysfs_write(path, level ? "high" : "low");
}
/* --------------------------------------------------------------- I2C */
/*
* Read one ID register. Register address width = id_addr_len bytes,
* value width = id_value_len bytes, both big-endian on the wire,
* exactly like the kernel module's sensor_read().
*/
static int sensor_read(const struct sensor_def *s, uint32_t addr, uint32_t *value)
{
uint8_t buf[4] = {0}, data[4] = {0};
uint8_t rlen = s->id_value_len;
uint8_t wlen = s->id_addr_len;
int i, ret;
struct i2c_msg msg[2] = {
{.addr = s->i2c_addr, .flags = 0, .len = wlen, .buf = buf},
{.addr = s->i2c_addr, .flags = I2C_M_RD, .len = rlen, .buf = data},
};
if (wlen < 1 || wlen > 4 || rlen < 1 || rlen > 4) {
elog("invalid reg/value width %u/%u\n", wlen, rlen);
return -1;
}
for (i = 0; i < wlen; i++)
buf[i] = (addr >> (8 * (wlen - 1 - i))) & 0xff;
ret = hw_i2c_xfer(msg, 2);
*value = 0;
for (i = 0; i < rlen; i++)
*value = (*value << 8) | data[i];
if (ret == 0)
vlog("read 0x%x = 0x%x (addr 0x%02x)\n", addr, *value, s->i2c_addr);
return ret;
}
/* 16-bit register, 8-bit value write, like the module's sensor_write(). */
static int sensor_write(const struct sensor_def *s, uint16_t reg, uint8_t value)
{
uint8_t buf[3] = {(reg >> 8) & 0xff, reg & 0xff, value};
struct i2c_msg msg = {.addr = s->i2c_addr, .flags = 0, .len = 3, .buf = buf};
return hw_i2c_xfer(&msg, 1);
}
/* ----------------------------------------------------------- probe logic */
static int sensor_matches_soc(const struct sensor_def *s)
{
int is_t41 = strcmp(cur_soc->name, "t41") == 0;
if (s->soc == S_T41_ONLY)
return is_t41;
if (s->soc == S_NOT_T41)
return !is_t41;
return 1;
}
/*
* Reset/power-down dance for one probe attempt.
* Faithful port of the sequences in process_one_adapter().
*/
static void sensor_hw_prepare(const struct sensor_def *s)
{
if (reset_pin != -1) {
if (gpio_claim(reset_pin, &reset_owned) == 0) {
gpio_out(reset_pin, 1);
hw_msleep(20);
gpio_out(reset_pin, 0);
if (!strcmp(s->name, "sp1409")) {
hw_msleep(600);
} else if (!strcmp(s->name, "sc2336p") || !strcmp(s->name, "sc2337p") ||
!strcmp(s->name, "sc3336p")) {
hw_msleep(250);
gpio_out(reset_pin, 1);
hw_msleep(20);
} else {
hw_msleep(20);
gpio_out(reset_pin, 1);
hw_msleep(20);
}
} else if (reset_warned == 0) {
reset_warned++;
fprintf(stderr,
"sinfo: [Warning] cannot claim reset GPIO %d (a sensor driver "
"probably holds it); probing without the reset dance, "
"use -r -1 to silence\n",
reset_pin);
}
}
if (pwdn_pin != -1) {
if (gpio_claim(pwdn_pin, &pwdn_owned) == 0) {
gpio_out(pwdn_pin, 1);
hw_msleep(150);
gpio_out(pwdn_pin, 0);
if (!strcmp(s->name, "sp1409"))
hw_msleep(600);
else
hw_msleep(10);
} else if (pwdn_warned == 0) {
pwdn_warned++;
fprintf(stderr,
"sinfo: [Warning] cannot claim pwdn GPIO %d (a sensor driver "
"probably holds it); probing without the pwdn dance, "
"use -p -1 to silence\n",
pwdn_pin);
}
}
}
static void sensor_hw_release(void)
{
if (reset_pin != -1)
gpio_release(reset_pin, reset_owned);
if (pwdn_pin != -1)
gpio_release(pwdn_pin, pwdn_owned);
}
/*
* Reset-behavior classes: entries whose probe sequence differs enough
* that one entry's silence must not veto another's attempt.
*/
enum {
QUIRK_STD,
QUIRK_SP1409, /* 600 ms reset hold */
QUIRK_SC233XP, /* unlock writes + long reset release */
QUIRK_SC3336P, /* unlock writes */
};
static int quirk_class(const struct sensor_def *s)
{
if (!strcmp(s->name, "sp1409"))
return QUIRK_SP1409;
if (!strcmp(s->name, "sc2336p") || !strcmp(s->name, "sc2337p"))
return QUIRK_SC233XP;
if (!strcmp(s->name, "sc3336p"))
return QUIRK_SC3336P;
return QUIRK_STD;
}
/*
* Nothing answered for an entry: every other entry with the same bus,
* address, MCLK rate and reset behavior is provably dead too, so the
* per-transfer kernel I2C overhead is paid once per combination
* instead of once per table entry (~45 addresses cover ~290 entries).
*/
struct dead_probe {
uint32_t rate;
uint8_t bus;
uint8_t addr;
uint8_t cls;
};
#define MAX_DEAD_PROBES 1024
static struct dead_probe dead_probes[MAX_DEAD_PROBES];
static int num_dead_probes;
static int probe_is_dead(int bus, const struct sensor_def *s)
{
int cls = quirk_class(s), i;
for (i = 0; i < num_dead_probes; i++)
if (dead_probes[i].bus == bus && dead_probes[i].addr == s->i2c_addr &&
dead_probes[i].rate == s->clk && dead_probes[i].cls == cls)
return 1;
return 0;
}
static void mark_dead(int bus, const struct sensor_def *s)
{
if (num_dead_probes >= MAX_DEAD_PROBES)
return; /* cache full: keep probing, correctness first */
dead_probes[num_dead_probes].bus = bus;
dead_probes[num_dead_probes].addr = s->i2c_addr;
dead_probes[num_dead_probes].rate = s->clk;
dead_probes[num_dead_probes].cls = quirk_class(s);
num_dead_probes++;
}
/*
* Progress on stderr: a live counter on a terminal, one line per bus
* otherwise, nothing in verbose mode (the per-entry log already shows
* activity). stdout carries only the report.
*/
static void probe_progress(int bus, unsigned int done, unsigned int total)
{
if (verbose || quiet)
return;
if (isatty(2)) {
fprintf(stderr, "\rProbing bus %d... %u/%u", bus, done, total);
if (done == total)
fprintf(stderr, "\n");
} else if (done == 0) {
fprintf(stderr, "sinfo: probing %u sensor entries on bus %d...\n", total, bus);
}
}
/*
* Probe every database entry. Faithful port of process_one_adapter():
* per-sensor MCLK + reset dance, ID register compare with the sc2336p/
* sc2337p/sc3336p unlock writes, the sc2336p-vs-sc2337p disambiguation
* via reg 0x801e, and the ov2735b alternate-ID quirk.
*/
static uint32_t mclk_cur_rate;
static int scanned_buses[MAX_I2C_BUSES];
static int num_scanned;
static int do_probe(int bus)
{
unsigned i;
int j;
if (hw_i2c_open(bus) < 0)
return -1;
for (i = 0; i < SENSOR_COUNT; i++) {
const struct sensor_def *s = &sensor_db[i];
struct i2c_scan_result scan_res;
uint8_t idcnt = s->id_cnt;
int ret;
int any_ack = 0;
probe_progress(bus, i, SENSOR_COUNT);
if (!sensor_matches_soc(s))
continue;
if (probe_is_dead(bus, s)) {
vlog("skipping %s @ 0x%02x (address already silent)\n", s->name,
s->i2c_addr);
continue;
}
vlog("probing %s @ 0x%02x (MCLK %u Hz)\n", s->name, s->i2c_addr, s->clk);
if (s->clk != mclk_cur_rate) {
if (mclk_enable(s->clk) < 0)
return -1;
mclk_cur_rate = s->clk;
}
sensor_hw_prepare(s);
memset(&scan_res, 0, sizeof(scan_res));
scan_res.bus = bus;
scan_res.i2c_addr = s->i2c_addr;
for (j = 0; j < idcnt; j++) {
uint32_t value = 0;
if (j == 0 &&
(!strcmp(s->name, "sc2336p") || !strcmp(s->name, "sc2337p"))) {
ret = sensor_write(s, 0x301a, 0xf8);
ret += sensor_write(s, 0x0100, 0x01);
if (ret != 0)
break;
any_ack = 1;
hw_msleep(5);
} else if (j == 0 && !strcmp(s->name, "sc3336p")) {
ret = sensor_write(s, 0x440d, 0x10);
ret += sensor_write(s, 0x4400, 0x11);
if (ret != 0)
break;
any_ack = 1;
hw_msleep(10);
}
ret = sensor_read(s, s->id_addr[j], &value);
if (scan_res.num_regs < 8) {
scan_res.reg_addrs[scan_res.num_regs] = s->id_addr[j];
scan_res.reg_values[scan_res.num_regs] = value;
scan_res.num_regs++;
}
if (ret != 0)
break;
any_ack = 1;
scan_res.responded = 1;
if ((!strcmp(s->name, "sc2336p") || !strcmp(s->name, "sc2337p")) &&
j == 1) {
uint32_t reg_val = 0;
ret = sensor_read(s, 0x801e, ®_val);
if (ret != 0)
break;
sensor_write(s, 0x0100, 0x00);
if (!strcmp(s->name, "sc2336p") && (reg_val & 0x0f) != 0) {
j--;
break;
}
if (!strcmp(s->name, "sc2337p") && (reg_val & 0x0f) == 0) {
j--;
break;
}
}
if (!strcmp(s->name, "ov2735b") && j == 2) {
if (value == s->id_value[j])
j++;
} else {
if (value != s->id_value[j])
break;
}
}
sensor_hw_release();
if (!any_ack)
mark_dead(bus, s);
if (j == idcnt) {
strncpy(scan_res.sensor_name, s->name, sizeof(scan_res.sensor_name) - 1);
if (num_matches < MAX_DETECTED_SENSORS) {
match_res[num_matches] = scan_res;
match_idx[num_matches++] = i;
vlog("MATCH: %s, I2C bus %d, address 0x%02X\n", s->name, bus,
s->i2c_addr);
}
}
if (scan_res.responded && num_scan_results < MAX_I2C_SCAN_RESULTS)
scan_results[num_scan_results++] = scan_res;
}
probe_progress(bus, SENSOR_COUNT, SENSOR_COUNT);
return 0;
}
/*
* Scan one bus (-b N), or every /dev/i2c-* the kernel exposes (-b all)
* for units with sensors on more than one bus. One MCLK block drives
* the whole run; a second block needs a second run with -m.
*/
static int probe_all(void)
{
int buses[MAX_I2C_BUSES];
int i, n;
num_matches = 0;
num_scan_results = 0;
primary_idx = -1;
reset_warned = 0;
pwdn_warned = 0;
num_dead_probes = 0;
mclk_cur_rate = 0;
xb2_mclk_pin_mux();
if (bus_all) {
n = hw_i2c_buses(buses, MAX_I2C_BUSES);
if (n <= 0) {
elog("no /dev/i2c-* buses found\n");
return -1;
}
} else {
buses[0] = bus_nr;
n = 1;
}
num_scanned = n;
for (i = 0; i < n; i++) {
scanned_buses[i] = buses[i];
if (do_probe(buses[i]) < 0)
return -1;
}
if (mclk_cur_rate)
mclk_disable();
if (num_matches > 0)
primary_idx = match_idx[0];
return 0;
}
/* ---------------------------------------------------------------- report */
/*
* Several table entries are rebadges with identical I2C address, ID
* registers and ID values (e.g. gc5603/gc5613, gc2053/gc2063). One
* physical chip matches all of them, so group matches whose observed
* reads are identical and report one device with the others as aliases.
*/
static int same_device(const struct i2c_scan_result *a, const struct i2c_scan_result *b)
{
int j;
if (a->bus != b->bus || a->i2c_addr != b->i2c_addr || a->num_regs != b->num_regs)
return 0;
for (j = 0; j < a->num_regs; j++)
if (a->reg_addrs[j] != b->reg_addrs[j] || a->reg_values[j] != b->reg_values[j])
return 0;
return 1;
}
static int num_devices;
static void fmt_mhz(uint32_t hz, char *buf, size_t len)
{
if (hz % 1000000 == 0)
snprintf(buf, len, "%u MHz", hz / 1000000);
else
snprintf(buf, len, "%u.%03u MHz", hz / 1000000, (hz % 1000000) / 1000);
}
static void print_scan_scope(void)
{
int i;
if (!bus_all) {
printf("on bus %d", bus_nr);
return;
}
printf("on any bus (");
for (i = 0; i < num_scanned; i++)
printf("%s%d", i ? ", " : "", scanned_buses[i]);
printf(")");
}
/*
* Verbose only: every address that answered, one row per (bus, addr)
* with the union of distinct register reads across all probe attempts.
* Distinct value pairs are kept on purpose: a register that reads
* differently across attempts (sc2336p before/after unlock) shows both.
*/
static void print_devices_seen(void)
{
int printed[MAX_I2C_SCAN_RESULTS] = {0};
int i, j, k, ndev = 0;
for (i = 0; i < num_scan_results; i++) {
if (printed[i])
continue;
for (j = 0; j < i; j++)
if (scan_results[j].bus == scan_results[i].bus &&
scan_results[j].i2c_addr == scan_results[i].i2c_addr)
break;
if (j == i)
ndev++;
}
if (!ndev) {
printf("no I2C devices answered\n");
return;
}
printf("I2C devices seen (%d):\n", ndev);
for (i = 0; i < num_scan_results; i++) {
const char *name = NULL;
uint32_t pr[32], pv[32];
int np = 0, over = 0;
if (printed[i])
continue;
for (j = i; j < num_scan_results; j++) {
struct i2c_scan_result *r = &scan_results[j];
if (r->bus != scan_results[i].bus ||
r->i2c_addr != scan_results[i].i2c_addr)
continue;
printed[j] = 1;
if (!name && r->sensor_name[0])
name = r->sensor_name;
for (k = 0; k < r->num_regs; k++) {
int seen = 0, p;
for (p = 0; p < np; p++)
if (pr[p] == r->reg_addrs[k] && pv[p] == r->reg_values[k])
seen = 1;
if (seen)
continue;
if (np < 32) {
pr[np] = r->reg_addrs[k];
pv[np] = r->reg_values[k];
np++;
} else {
over = 1;
}
}