From 8db43d7f1177473f3779be4e11e452e368d5e381 Mon Sep 17 00:00:00 2001 From: Dmitry Ilyin <6576495+widgetii@users.noreply.github.com> Date: Tue, 24 Nov 2026 16:35:37 +0300 Subject: [PATCH 1/2] hisi: report every sensor on Hi3519DV500 dual-sensor boards; fix V5 DDR rate, DV500 CPU clock A Hi3519DV500 kit with an OS04A10 on SENSOR0 and an OS08A20 on SENSOR1 reported one sensor. Three things stood in the way, and the clock report was wrong in two places. - The report stopped at the first sensor. detect_sensors() now probes the remaining i2c buses and lists every sensor it finds, one entry per bus. Both sweeps now run to bus 7: the vendor SDK puts sensors 0..3 on I2C4, I2C5, I2C3 and I2C6. Buses the first probe already found empty are not probed again. getsensorid() and libipchw are unchanged. - On DV500 the SENSOR1 bus is I2C5, whose pads (iocfg_reg109/110 at 0x0EFF0024/28) the stock sys_config.ko leaves as GPIO1_0/1_1 unless built with I2C_EX_EN, so every transfer timed out. The OT arm switches them to function 2 (I2C5) only while both are GPIO inputs -- never in SPI1 mode, never a GPIO the board drives -- and puts back what it found if the pad still holds our word. Our word keeps the found drive and pull bits, unlike the vendor's 0x1752, so the undo can survive the SPI arm, which has no cleanup of its own. - PERI_CRG8464/8472 reset to cksel 0, which is 74.25 MHz on CV610 and DV500 alike, above the 27 MHz an OS08A10/A20 or the 36 MHz an OS04A10 accepts. A clock we ungate from that state now gets 24 MHz. A running clock, or one whose select somebody chose, is left alone. - OS08A10 and OS08A20 share 0x300A..C = 53 08 41 and 0x302A = 0xB0 in every data sheet revision; only Nyxel NIR sensitivity differs. sensor_id stays OS08A10, which is what consumers select a driver by, and the report adds `id_shared_with: OS08A20`. - clocks: the V5 DDR rate was the DRAM clock. With DFI 1:2 the DRAM runs at twice the DPLL and moves data on both edges, so the rate is DPLL * (1 << dficlk_ratio) * 2. The DV500 data sheet's clock tree runs the DPLL at 667 MHz for "up to 2666 Mbps DDR4"; the kit (boot table ..._DDR4_2666M-4GB) reads 664 MHz and now reports 2656 MT/s, not 1328. - clocks: on DV500 the A55 does not have to run from the APLL. PERI_CRG2064 a55_cksel [10:8] picks one of seven sources; the kit reads 6, the fixed 1000 MHz source, with the APLL at 750 MHz beside it. A new `cpu` entry decodes it. CV610's A7 uses a different register and is left out. hal_hisi_test gains ten cases for the clock select and the I2C5 pads. Breaking the 24 MHz value or the pad undo makes them fail. Verified on the kit (MPP V2.0.1.1): both sensors found with the pads already muxed (0x1752, left untouched), with stock pads (0x1300, restored afterwards), and with both sensor CRGs at their reset value 0x3. --- src/hal/hisi/clocks_v5.c | 72 ++++++++++++++++++--- src/hal/hisi/hal_hisi.c | 97 +++++++++++++++++++++++++--- src/hal/hisi/hal_hisi_test.c | 114 ++++++++++++++++++++++++++++++++- src/hal/hisi/ispreg.h | 6 ++ src/sensors.c | 119 +++++++++++++++++++++++++++++------ 5 files changed, 374 insertions(+), 34 deletions(-) diff --git a/src/hal/hisi/clocks_v5.c b/src/hal/hisi/clocks_v5.c index 0220e129..95c8c808 100644 --- a/src/hal/hisi/clocks_v5.c +++ b/src/hal/hisi/clocks_v5.c @@ -46,6 +46,9 @@ * bits[9:0]) is surfaced through the standard hpm_info table. */ +#include + +#include "chipid.h" #include "cjson/cJSON.h" #include "clocks.h" #include "hal/hisi/hal_hisi.h" @@ -159,12 +162,65 @@ static const char *v5_dram_type_name(uint32_t t) { /* Decode the actual DDR data rate. The chain is: * ddr_cksel (CRG+0x2000 bits [18:16]) -- 0 = 24 MHz, 1 = clk_dpll_pst - * DPLL VCO (CRG+0x180 / 0x184, post-divided) -- already decoded as - * ddr_pll dficlk_ratio (DDR_PHY0+0x78 bits [1:0]) -- 0=1:1, 1=1:2, 2=1:4 DRAM - * data rate (MT/s) = phy_clk_mhz * (1 << dficlk_ratio). In the typical config - * (DDR3/DDR4 -> 1:2, LPDDR4 -> 1:4) the multiplier matches the DDR burst - * factor, so this is also "post-divided PLL frequency times the bus - * double-data-rate". */ + * DPLL VCO (CRG+0x180 / 0x184, post-divided) -- the DFI clock + * dficlk_ratio (DDR_PHY0+0x78 bits [1:0]) -- DFI:DRAM clock, 0=1:1, 1=1:2, + * 2=1:4, so the DRAM clock is DFI * (1 << dficlk_ratio) + * and the data rate is twice the DRAM clock, data being moved on both edges: + * data rate (MT/s) = phy_clk_mhz * (1 << dficlk_ratio) * 2. + * The Hi3519DV500 data sheet's clock tree runs the DPLL at 667 MHz for its + * "up to 2666 Mbps DDR4"; a Taobao DV500 kit whose boot table is + * ..._DDR4_2666M-4GB reads DPLL 664 MHz, ratio 1:2 -> 2656 MT/s. Without the + * final factor of two this reported the DRAM clock, 1328, as the rate. */ +/* Hi3519DV500/Hi3516DV500 A55 core clock: PERI_CRG2064 (CRG+0x2040) + * a55_cksel [10:8] picks one of seven sources, and only one of them is the + * APLL -- so "cpu_pll" above is not the CPU's frequency. Encoding from the + * DV500 data sheet; 6 (1000 MHz) needs cpu_cksel_limit_en=0xA. A Taobao + * DV500 kit reads 0x2600: a55_cksel 6, the CPU at 1 GHz with the APLL at + * 750 MHz beside it. The CV610's single A7 has a different register, so this + * is DV500-only. */ +static void v5_dv500_cpu(cJSON *root, bool brief) { + static const unsigned fixed_mhz[8] = {24, 675, 0, 792, 833, 900, 1000, 0}; + uint32_t crg2064, apll0, apll1; + + if (!strstr(chip_name, "DV500") || + !mem_reg(V5_CRG_BASE + 0x2040, &crg2064, OP_READ)) + return; + + const unsigned sel = (crg2064 >> 8) & 0x7; + double mhz = fixed_mhz[sel]; + const char *source = "fixed"; + if (sel == 2) { + if (!mem_reg(V5_CRG_BASE + 0x000, &apll0, OP_READ) || + !mem_reg(V5_CRG_BASE + 0x004, &apll1, OP_READ)) + return; + unsigned fbdiv = apll1 & 0xFFF; + unsigned refdiv = (apll1 >> 12) & 0x3F; + unsigned p1 = (apll0 >> 24) & 0x7; + unsigned p2 = (apll0 >> 28) & 0x7; + if (!refdiv) + refdiv = 1; + if (!p1) + p1 = 1; + if (!p2) + p2 = 1; + mhz = 24.0 * (fbdiv + (double)(apll0 & 0xFFFFFF) / (double)(1u << 24)) / + (double)(refdiv * p1 * p2); + source = "cpu_pll"; + } + if (mhz == 0) + return; /* 7: reserved */ + + cJSON *cpu = cJSON_CreateObject(); + cJSON *j_inner = cpu; + ADD_PARAM_NUM("freq_mhz", mhz); + ADD_PARAM("source", source); + if (!brief) { + ADD_PARAM_FMT("crg2064", "0x%08x", crg2064); + ADD_PARAM_NUM("a55_cksel", sel); + } + cJSON_AddItemToObject(root, "cpu", cpu); +} + static void v5_extra(cJSON *root, bool brief) { uint32_t crg_cksel = 0; uint32_t phy_ctrl0 = 0; @@ -199,7 +255,7 @@ static void v5_extra(cJSON *root, bool brief) { /* Source: at boot/training time ddr_cksel can momentarily switch to * 24 MHz; in normal operation it stays on the DPLL post-divider. */ double phy_clk_mhz = (ddr_cksel == 0) ? 24.0 : dpll_mhz; - double data_rate_mtps = phy_clk_mhz * (double)(1u << dficlk_ratio); + double data_rate_mtps = phy_clk_mhz * (double)(1u << dficlk_ratio) * 2.0; cJSON *ddr = cJSON_CreateObject(); cJSON *j_inner = ddr; @@ -216,6 +272,8 @@ static void v5_extra(cJSON *root, bool brief) { } cJSON_AddItemToObject(root, "ddr", ddr); + v5_dv500_cpu(root, brief); + /* SSMOD (DPLL spread-spectrum) status. */ uint32_t ssmod_raw = 0; if (mem_reg(V5_CRG_BASE + V5_SSMOD_CTRL_OFF, &ssmod_raw, OP_READ)) { diff --git a/src/hal/hisi/hal_hisi.c b/src/hal/hisi/hal_hisi.c index 40aeb094..a963e111 100644 --- a/src/hal/hisi/hal_hisi.c +++ b/src/hal/hisi/hal_hisi.c @@ -473,8 +473,13 @@ typedef struct { bool owed; /* an undo is outstanding */ } sensor_crg_t; -static void sensor_crg_enable( - sensor_crg_t *st, uint32_t addr, uint32_t cken, uint32_t srst) { +/* sel_mask/sel_safe: a clock-select field and the value to put in it when + * we ungate a clock whose select still holds its reset value (0). Only a + * clock we find gated gets one: a running clock was set up by somebody, at + * whatever rate they chose. Pass 0/0 where the register has no such field. */ +static void sensor_crg_enable(sensor_crg_t *st, uint32_t addr, uint32_t cken, + uint32_t srst, uint32_t sel_mask, + uint32_t sel_safe) { uint32_t cur; /* A read that failed taught us nothing. Drop any entitlement rather than @@ -486,7 +491,9 @@ static void sensor_crg_enable( return; } - const uint32_t want = (cur | cken) & ~srst; + uint32_t want = (cur | cken) & ~srst; + if (sel_mask && !(cur & cken) && !(cur & sel_mask)) + want = (want & ~sel_mask) | sel_safe; if (want == cur) { /* Already fit for a probe, so this arm takes nothing -- and drops * anything an earlier one held. @@ -545,7 +552,8 @@ static sensor_crg_t gk7206_crg; static void gk7206_ensure_sensor_enabled() { sensor_crg_enable(&gk7206_crg, EV300_PERI_CRG60_ADDR, - EV300_PERI_CRG60_SENSOR0_CKEN, EV300_PERI_CRG60_SENSOR0_SRST); + EV300_PERI_CRG60_SENSOR0_CKEN, + EV300_PERI_CRG60_SENSOR0_SRST, 0, 0); } static void gk7206_ensure_sensor_restored() { @@ -558,7 +566,8 @@ static sensor_crg_t v4_crg; static void v4_ensure_sensor_enabled() { sensor_crg_enable(&v4_crg, EV300_PERI_CRG60_ADDR, - EV300_PERI_CRG60_SENSOR0_CKEN, EV300_PERI_CRG60_SENSOR0_SRST); + EV300_PERI_CRG60_SENSOR0_CKEN, + EV300_PERI_CRG60_SENSOR0_SRST, 0, 0); } static void v4_ensure_sensor_restored() { @@ -575,15 +584,89 @@ static const uint32_t ot_crg_addr[2] = { }; static sensor_crg_t ot_crg[2]; +/* Hi3519DV500/Hi3516DV500: the SENSOR1 connector of the reference design is + * controlled over I2C5, whose pads are SPI1_SCLK (iocfg_reg109) and SPI1_SDO + * (iocfg_reg110) -- function 0 GPIO1_0/GPIO1_1, 1 SPI1, 2 I2C5 (data sheet + * pin table). The vendor sys_config.ko only muxes them when built with + * I2C_EX_EN, which the stock SDK is not, so on a stock board /dev/i2c-5 + * exists, every transfer on it times out, and a sensor on SENSOR1 is + * invisible. Measured on a Taobao DV500 kit with an OS08A20 there. + * + * Same rules as the clock above. We switch a pad to I2C5 only while it is an + * unclaimed GPIO input -- never one in SPI1 mode, never a GPIO the board + * drives as an output -- and put back exactly what we found, and only if the + * pad still holds what we left in it. */ +#define DV500_IOCFG_SPI1_SCLK 0x0EFF0024u +#define DV500_IOCFG_SPI1_SDO 0x0EFF0028u +#define DV500_GPIO1_DIR 0x11091400u /* PL061 GPIODIR, bank 1 */ +#define DV500_IOCFG_FUNC_MASK 0xFu +#define DV500_IOCFG_FUNC_GPIO 0x0u +#define DV500_IOCFG_FUNC_I2C5 0x2u + +static const uint32_t dv500_i2c5_pad[2] = { + DV500_IOCFG_SPI1_SCLK, /* GPIO1_0 */ + DV500_IOCFG_SPI1_SDO, /* GPIO1_1 */ +}; +static sensor_crg_t dv500_i2c5[2]; + +static bool is_dv500(void) { return strstr(chip_name, "DV500") != NULL; } + +static void dv500_i2c5_enable(void) { + uint32_t cur[2], dir; + + if (!mem_reg(dv500_i2c5_pad[0], &cur[0], OP_READ) || + !mem_reg(dv500_i2c5_pad[1], &cur[1], OP_READ) || + !mem_reg(DV500_GPIO1_DIR, &dir, OP_READ)) { + for (size_t i = 0; i < ARRCNT(dv500_i2c5); i++) + dv500_i2c5[i].owed = false; /* we no longer know what is there */ + return; + } + + /* An undo outlives a re-arm only while the pad still holds our word. The + * clock above cannot do this -- its word is the SDK's word too -- but + * ours keeps the found drive/pull bits and changes only the function, + * while the vendor's I2C5 setup writes its own (0x1752), so "still ours" + * is readable here. Without it the arm that follows the SPI probe, which + * has no cleanup of its own, would find I2C5 already set, take nothing, + * and leave the pads switched after we exit. */ + for (size_t i = 0; i < 2; i++) + if (dv500_i2c5[i].owed && cur[i] != dv500_i2c5[i].wrote) + dv500_i2c5[i].owed = false; + + for (size_t i = 0; i < 2; i++) { + const uint32_t func = cur[i] & DV500_IOCFG_FUNC_MASK; + if (func == DV500_IOCFG_FUNC_I2C5) + continue; + if (func != DV500_IOCFG_FUNC_GPIO || (dir & (1u << i))) + return; /* somebody's SPI1 or GPIO output: not ours, either pad */ + } + + for (size_t i = 0; i < 2; i++) { + if ((cur[i] & DV500_IOCFG_FUNC_MASK) == DV500_IOCFG_FUNC_I2C5) + continue; + sensor_crg_t *st = &dv500_i2c5[i]; + st->found = cur[i]; + st->wrote = (cur[i] & ~DV500_IOCFG_FUNC_MASK) | DV500_IOCFG_FUNC_I2C5; + if (mem_reg(dv500_i2c5_pad[i], &st->wrote, OP_WRITE)) + st->owed = true; + } +} + static void ot_ensure_sensor_enabled(void) { for (size_t i = 0; i < ARRCNT(ot_crg_addr); i++) - sensor_crg_enable(&ot_crg[i], ot_crg_addr[i], - CV610_PERI_CRG_SENSOR0_CKEN, CV610_PERI_CRG_SENSOR0_SRST); + sensor_crg_enable( + &ot_crg[i], ot_crg_addr[i], CV610_PERI_CRG_SENSOR0_CKEN, + CV610_PERI_CRG_SENSOR0_SRST, CV610_PERI_CRG_SENSOR_CKSEL_MASK, + CV610_PERI_CRG_SENSOR_CKSEL_24M); + if (is_dv500()) + dv500_i2c5_enable(); } static void ot_ensure_sensor_restored() { for (size_t i = 0; i < ARRCNT(ot_crg_addr); i++) sensor_crg_restore(&ot_crg[i], ot_crg_addr[i]); + for (size_t i = 0; i < ARRCNT(dv500_i2c5); i++) + sensor_crg_restore(&dv500_i2c5[i], dv500_i2c5_pad[i]); } /* Installed as hal_enable_sensor_clock so the bus sweep in getsensorid() can diff --git a/src/hal/hisi/hal_hisi_test.c b/src/hal/hisi/hal_hisi_test.c index ac85f904..322da881 100644 --- a/src/hal/hisi/hal_hisi_test.c +++ b/src/hal/hisi/hal_hisi_test.c @@ -40,7 +40,7 @@ static int failures; /* ---- the fake register file -------------------------------------------- */ -#define SLOTS 4 +#define SLOTS 8 static struct { uint32_t addr; @@ -164,6 +164,7 @@ void restore_printk(void) {} /* chipid.c's, normally. Naming it here is what keeps chipid.c and every other * vendor HAL out of the link. */ int chip_generation; +char chip_name[128]; /* ---- helpers ----------------------------------------------------------- */ @@ -172,6 +173,8 @@ static void fresh(uint32_t addr, uint32_t val, int gen) { memset(&UNDO, 0, sizeof UNDO); memset(&v4_crg, 0, sizeof v4_crg); memset(ot_crg, 0, sizeof ot_crg); + memset(dv500_i2c5, 0, sizeof dv500_i2c5); + strcpy(chip_name, gen == HISI_OT ? "3516CV610" : ""); chip_generation = gen; seed(addr, val); if (addr == CV610_PERI_CRG8464_ADDR) @@ -353,6 +356,115 @@ int main(void) { CHECK(peek(CV610_PERI_CRG8464_ADDR) == (CV610_PERI_CRG_SENSOR0_CKEN | CV610_PERI_CRG_SENSOR0_SRST)); + /* ---- OT: the clock select a gated clock is given -------------------- + * PERI_CRG8464/8472 reset to 0x3: cksel 0 = 74.25 MHz, above what an + * OS08A/OS04A takes. A clock we ungate from that state gets 24 MHz; the + * cleanup puts the reset value back. */ + fresh(OT, 0x3, HISI_OT); + setup_hal_hisi(); + CHECK(peek(CV610_PERI_CRG8464_ADDR) == 0x4012); + CHECK(peek(CV610_PERI_CRG8472_ADDR) == 0x4012); + hal_cleanup(); + CHECK(peek(CV610_PERI_CRG8464_ADDR) == 0x3); + CHECK(peek(CV610_PERI_CRG8472_ADDR) == 0x3); + CHECK(illegal == NULL); + + /* A gated clock whose select somebody already chose keeps it. */ + fresh(OT, 0xA003, HISI_OT); + setup_hal_hisi(); + CHECK(peek(CV610_PERI_CRG8464_ADDR) == 0xA012); + + /* A running clock, even at 74.25 MHz, is somebody's: untouched. */ + fresh(OT, 0x0012, HISI_OT); + setup_hal_hisi(); + reset_counters(); + sweep(); + CHECK(writes == 0); + CHECK(peek(CV610_PERI_CRG8464_ADDR) == 0x0012); + + /* ---- DV500: the I2C5 pads of the SENSOR1 bus ------------------------ */ +#define SCLK DV500_IOCFG_SPI1_SCLK +#define SDO DV500_IOCFG_SPI1_SDO +#define DIR DV500_GPIO1_DIR + /* Stock sys_config leaves them GPIO inputs (0x1300): switched to I2C5, + * function bits only, and put back by the cleanup. */ + fresh(OT, 0x4012, HISI_OT); + strcpy(chip_name, "3519DV500"); + seed(SCLK, 0x1300); + seed(SDO, 0x1300); + seed(DIR, 0x0); + setup_hal_hisi(); + CHECK(peek(SCLK) == 0x1302); + CHECK(peek(SDO) == 0x1302); + hal_cleanup(); + CHECK(peek(SCLK) == 0x1300); + CHECK(peek(SDO) == 0x1300); + + /* The whole sweep, SPI arm included (no cleanup of its own): the pads + * still end where they started. */ + hal_enable_sensor_clock(); + hal_cleanup(); + sweep(); + CHECK(peek(SCLK) == 0x1300); + CHECK(peek(SDO) == 0x1300); + + /* In SPI1 mode they are the SPI controller's: neither pad is touched. */ + fresh(OT, 0x4012, HISI_OT); + strcpy(chip_name, "3519DV500"); + seed(SCLK, 0x1301); + seed(SDO, 0x1300); + seed(DIR, 0x0); + setup_hal_hisi(); + CHECK(peek(SCLK) == 0x1301); + CHECK(peek(SDO) == 0x1300); + + /* A GPIO the board drives (an IR-cut, a LED) is not ours either. */ + fresh(OT, 0x4012, HISI_OT); + strcpy(chip_name, "3519DV500"); + seed(SCLK, 0x1300); + seed(SDO, 0x1300); + seed(DIR, 0x2); /* GPIO1_1 is an output */ + setup_hal_hisi(); + CHECK(peek(SCLK) == 0x1300); + CHECK(peek(SDO) == 0x1300); + + /* Already I2C5 (the board's rcS did it): no write at all, and the + * cleanup does not "restore" anything. */ + fresh(OT, 0x4012, HISI_OT); + strcpy(chip_name, "3519DV500"); + seed(SCLK, 0x1752); + seed(SDO, 0x1752); + seed(DIR, 0x0); + reset_counters(); + setup_hal_hisi(); + sweep(); + CHECK(writes == 0); + CHECK(peek(SCLK) == 0x1752); + + /* Re-muxed by somebody else between our arm and our cleanup: theirs. */ + fresh(OT, 0x4012, HISI_OT); + strcpy(chip_name, "3519DV500"); + seed(SCLK, 0x1300); + seed(SDO, 0x1300); + seed(DIR, 0x0); + setup_hal_hisi(); + poke(SCLK, 0x1752); + hal_cleanup(); + CHECK(peek(SCLK) == 0x1752); + CHECK(peek(SDO) == 0x1300); + + /* Not a DV500: the pads are not even looked at. */ + fresh(OT, 0x4012, HISI_OT); + seed(SCLK, 0x1300); + seed(SDO, 0x1300); + seed(DIR, 0x0); + setup_hal_hisi(); + sweep(); + CHECK(peek(SCLK) == 0x1300); +#undef SCLK +#undef SDO +#undef DIR + /* V3 ungates without ever restoring, which is exactly why it cannot * suffer this. A guard, so it does not grow a restore by symmetry. */ fresh(CV300_PERI_CRG11_ADDR, 0, HISI_V3); diff --git a/src/hal/hisi/ispreg.h b/src/hal/hisi/ispreg.h index a15d2e6f..00e8c6b2 100644 --- a/src/hal/hisi/ispreg.h +++ b/src/hal/hisi/ispreg.h @@ -43,6 +43,12 @@ struct CV610_PERI_CRG8464 { /* And the same for OT, where the two fields sit in different places. */ #define CV610_PERI_CRG_SENSOR0_SRST 0x1u #define CV610_PERI_CRG_SENSOR0_CKEN 0x10u +/* sensorN_cksel [15:12]. The reset value 0 is 74.25 MHz on both CV610 and + * DV500 (data sheets, PERI_CRG8464/8472), above the 27 MHz an OS08A10/A20 or + * the 36 MHz an OS04A10 accepts; 4 is 24 MHz, which every sensor we probe + * takes. */ +#define CV610_PERI_CRG_SENSOR_CKSEL_MASK 0xF000u +#define CV610_PERI_CRG_SENSOR_CKSEL_24M 0x4000u const char *hisi_detect_fmc(); void hisi_chip_properties(cJSON *j_inner); diff --git a/src/sensors.c b/src/sensors.c index 90db8160..d87f5eaa 100644 --- a/src/sensors.c +++ b/src/sensors.c @@ -1483,11 +1483,24 @@ static void arm_sensor_clock(void) { hal_enable_sensor_clock(); } +/* Highest /dev/i2c-N the sweeps look at. Hi3519DV500 puts sensors 0..3 on + * I2C4, I2C5, I2C3 and I2C6 in the vendor SDK, so stopping at 5 missed the + * last of them. A bus that is not there costs one failed open(). */ +#define SENSOR_MAX_I2C_BUS 7 + +/* What the last probe_sensor() already covered, so detect_sensors() does not + * probe an empty bus twice -- on a SoC with unmuxed adapters every probe of + * one is a run of controller timeouts. */ +static int probed_preferred_adapter = -1; +static bool found_by_sweep; + static bool probe_sensor(sensor_ctx_t *ctx) { if (!getchipname()) return false; const int preferred_adapter = i2c_adapter_nr; + probed_preferred_adapter = preferred_adapter; + found_by_sweep = false; // Use common settings as default ctx->data_width = 1; @@ -1514,7 +1527,7 @@ static bool probe_sensor(sensor_ctx_t *ctx) { * the numbering, and /dev/i2c-1 is missing on most single-bus boards, so * the sweep died one iteration in and never reached the buses behind it. * The fallback has been dead on that hardware since it was written. */ - for (int i = 0; i <= 5; i++) { + for (int i = 0; i <= SENSOR_MAX_I2C_BUS; i++) { if (i == preferred_adapter) continue; @@ -1529,6 +1542,7 @@ static bool probe_sensor(sensor_ctx_t *ctx) { arm_sensor_clock(); if (get_sensor_id_i2c(ctx)) { strcpy(ctx->control, "i2c"); + found_by_sweep = true; return true; } } @@ -1556,35 +1570,102 @@ bool getsensorid(sensor_ctx_t *ctx) { #ifndef STANDALONE_LIBRARY +/* Parts that answer with the same ID registers, so a probe cannot tell them + * apart. sensor_id keeps the name it always had -- libipchw consumers pick a + * driver by it -- and the report says what else it could be. */ +static const char *sensor_id_shared_with(const char *sensor_id) { + /* OS08A10 and OS08A20: 0x300A..C = 53 08 41 and 0x302A = 0xB0 in every + * revision of both data sheets; the A20 differs only by Nyxel NIR + * sensitivity, which no register shows. */ + if (!strcmp(sensor_id, "OS08A10")) + return "OS08A20"; + return NULL; +} + +static void add_sensor_entry(cJSON *j_sensors, sensor_ctx_t *ctx, int bus, + bool vi_info) { + cJSON *j_inner = ctx->j_sensor; + cJSON_AddItemToArray(j_sensors, j_inner); + + ADD_PARAM("vendor", ctx->vendor); + ADD_PARAM("model", ctx->sensor_id); + const char *twin = sensor_id_shared_with(ctx->sensor_id); + if (twin) + ADD_PARAM("id_shared_with", twin); + { + cJSON *j_inner = cJSON_CreateObject(); + cJSON_AddItemToObject(ctx->j_sensor, "control", j_inner); + ADD_PARAM_NUM("bus", bus); + ADD_PARAM("type", ctx->control); + if (ctx->addr) + ADD_PARAM_FMT("addr", "0x%x", ctx->addr); + if (ctx->j_params) + cJSON_AddItemToObject(ctx->j_sensor, "params", ctx->j_params); + + /* The VI/MIPI state in /proc/umap describes the pipeline, not a bus, + * so it is reported once, with the first sensor. */ + if (vi_info) + hisi_vi_information(ctx); + } +} + +/* Every sensor on the board, not just the first one: dual-sensor designs + * (the Hi3519DV500 reference kit's SENSOR0/SENSOR1, stereo rigs) carry one + * per i2c bus. getsensorid() finds the first, as it always has, and then the + * remaining buses are probed one by one. A bus with nothing on it costs one + * probe; it does not end the sweep. */ cJSON *detect_sensors() { sensor_ctx_t ctx; memset(&ctx, 0, sizeof(ctx)); - - cJSON *j_sensors = cJSON_CreateArray(); ctx.j_sensor = cJSON_CreateObject(); - cJSON *j_inner = ctx.j_sensor; - cJSON_AddItemToArray(j_sensors, j_inner); if (!getsensorid(&ctx)) { - cJSON_Delete(j_sensors); + cJSON_Delete(ctx.j_sensor); return NULL; } - ADD_PARAM("vendor", ctx.vendor); - ADD_PARAM("model", ctx.sensor_id); - { - cJSON *j_inner = cJSON_CreateObject(); - cJSON_AddItemToObject(ctx.j_sensor, "control", j_inner); - ADD_PARAM_NUM("bus", i2c_adapter_nr); - ADD_PARAM("type", ctx.control); - if (ctx.addr) - ADD_PARAM_FMT("addr", "0x%x", ctx.addr); - if (ctx.j_params) - cJSON_AddItemToObject(ctx.j_sensor, "params", ctx.j_params); - - hisi_vi_information(&ctx); + cJSON *j_sensors = cJSON_CreateArray(); + const int first_bus = i2c_adapter_nr; + add_sensor_entry(j_sensors, &ctx, first_bus, true); + + /* Only for an i2c hit: an SPI sensor leaves i2c_adapter_nr wherever the + * HAL put it, and sweeping from there would report nothing new. */ + if (strcmp(ctx.control, "i2c")) + return j_sensors; + + for (int bus = 0; bus <= SENSOR_MAX_I2C_BUS; bus++) { + if (bus == first_bus) + continue; + /* Found by the fallback sweep: the HAL's own bus and every bus below + * this one were probed on the way and had nothing. */ + if (found_by_sweep && + (bus < first_bus || bus == probed_preferred_adapter)) + continue; + if (!i2c_bus_openable(bus)) + continue; + + sensor_ctx_t more; + memset(&more, 0, sizeof(more)); + more.data_width = 1; + more.reg_width = 2; + more.j_sensor = cJSON_CreateObject(); + + i2c_adapter_nr = bus; + arm_sensor_clock(); + if (get_sensor_id_i2c(&more)) { + strcpy(more.control, "i2c"); + add_sensor_entry(j_sensors, &more, bus, false); + } else { + cJSON_Delete(more.j_sensor); + if (more.j_params) + cJSON_Delete(more.j_params); + } } + /* Process-global, read by `ipctool i2cget` and friends: leave it on the + * bus the first sensor was found on, as before. */ + i2c_adapter_nr = first_bus; + restore_printk(); return j_sensors; } From 3939e17e9a6e700f7119475b1451e0a7e2983f8b Mon Sep 17 00:00:00 2001 From: Dmitry Ilyin <6576495+widgetii@users.noreply.github.com> Date: Tue, 24 Nov 2026 16:44:44 +0300 Subject: [PATCH 2/2] hisi: sweep only the buses a multi-sensor HAL names; arm DV500 pads only for a probe Review fixes (Qodo on #294): - The extra sweep ran on every platform after every hit, and a probe is not a read: the two-byte register address is a write to a one-byte part, and some families write page registers. It is now opt-in through a new HAL global, hal_sensor_buses, and covers only the buses listed there. DV500 lists the vendor SDK's SENSOR0..3 buses, {4, 5, 3, 6}. Everywhere else the report stops at the first sensor, as before, and the fallback sweep is back to buses 0..5. This also rules out a sensor being listed under several bus numbers by an opener that ignores i2c_adapter_nr (V1, XM, Allwinner, Tegra, Rockchip, Novatek): only a HAL whose opener honours it may set the list. - i2c_bus_openable() was called before i2c_adapter_nr was set, and every opener reads the global, so it tested the previous adapter. - setup_hal_hisi() arms the clocks, and with them the DV500 pads, but a chip-name or clocks query never reaches the cleanup that puts the pads back. The pads are now armed only by hal_enable_sensor_clock, which is to say by a probe; setup still arms the clocks alone, as before. - A probe whose own open fails returns without a cleanup; the sweep now runs one after any failed probe. A cleanup after one that already ran is a no-op, because each undo is spent. - clocks: the DV500 CPU decoder runs first, on its own registers, so a failed DDR PHY read no longer hides it. It reads the APLL through the PLL table, via a new pll_read_freq_mhz() that decode_pll() now shares, instead of a second copy of the field layout. It moved above the DDR comment, which again sits directly over v5_extra(). hal_hisi_test checks that setup alone leaves the pads alone; that check fails if setup arms them. It also checks that hal_sensor_buses is set on DV500 and on nothing else. On the DV500 kit: `ipctool -c` and `ipctool clocks` leave stock pads at 0x1300; the report finds both sensors with stock pads (restored after), muxed pads, and sensor CRGs at reset; 14.6 s instead of 15.0 s. ipcinfo is unchanged in size on arm32. --- src/clocks.c | 52 ++++++++++++++++++++++++++++-------- src/clocks.h | 4 +++ src/hal/common.c | 1 + src/hal/common.h | 6 +++++ src/hal/hisi/clocks_v5.c | 45 ++++++++++++------------------- src/hal/hisi/hal_hisi.c | 24 ++++++++++++++--- src/hal/hisi/hal_hisi_test.c | 17 ++++++++++-- src/sensors.c | 41 +++++++++++++++++----------- 8 files changed, 130 insertions(+), 60 deletions(-) diff --git a/src/clocks.c b/src/clocks.c index 5ee2f56c..02fe682d 100644 --- a/src/clocks.c +++ b/src/clocks.c @@ -45,6 +45,46 @@ static uint32_t extract_field(uint32_t raw, uint8_t shift, uint8_t width) { return (raw >> shift) & mask; } +/* f = input * (FBDIV + FRACDIV/2^frac_width) / (REFDIV * POSTDIV1 * + * POSTDIV2), from the two raw control words. 0 when the fields cannot make a + * frequency (FBDIV or a divider of 0). */ +static double pll_freq_from_raw(const struct pll_info *pll, uint32_t raw1, + uint32_t raw2) { + uint32_t fracdiv = extract_field(raw1, pll->frac_shift, pll->frac_width); + uint32_t pdiv1 = + extract_field(raw1, pll->postdiv1_shift, pll->postdiv1_width); + uint32_t pdiv2 = + extract_field(raw1, pll->postdiv2_shift, pll->postdiv2_width); + uint32_t fbdiv = extract_field(raw2, pll->fbdiv_shift, pll->fbdiv_width); + uint32_t refdiv = extract_field(raw2, pll->refdiv_shift, pll->refdiv_width); + if (pll->postdiv1_width == 0) + pdiv1 = 1; + if (pll->postdiv2_width == 0) + pdiv2 = 1; + if (pll->refdiv_width == 0) + refdiv = 1; + + uint32_t denom = refdiv * pdiv1 * pdiv2; + if (fbdiv == 0 || denom == 0) + return 0.0; + uint64_t numer_khz = (uint64_t)pll->input_khz * fbdiv; + if (pll->frac_width) + numer_khz += ((uint64_t)pll->input_khz * fracdiv) >> pll->frac_width; + return (double)numer_khz / (double)denom / 1000.0; +} + +bool pll_read_freq_mhz(const struct pll_info *pll, double *mhz) { + uint32_t raw1 = 0, raw2; + if (!mem_reg(pll->ctrl_reg1, &raw1, OP_READ)) + return false; + raw2 = raw1; + if (pll->ctrl_reg2 && pll->ctrl_reg2 != pll->ctrl_reg1 && + !mem_reg(pll->ctrl_reg2, &raw2, OP_READ)) + return false; + *mhz = pll_freq_from_raw(pll, raw1, raw2); + return *mhz != 0.0; +} + static cJSON *decode_pll(const struct pll_info *pll, bool brief) { cJSON *j_inner = cJSON_CreateObject(); @@ -77,17 +117,7 @@ static cJSON *decode_pll(const struct pll_info *pll, bool brief) { if (pll->refdiv_width == 0) refdiv = 1; - /* Compute frequency. f = input * (FBDIV + FRACDIV/2^frac_width) / - * (REFDIV * POSTDIV1 * POSTDIV2). */ - double freq_mhz = 0.0; - uint32_t denom = refdiv * pdiv1 * pdiv2; - if (fbdiv != 0 && denom != 0) { - uint64_t numer_khz = (uint64_t)pll->input_khz * fbdiv; - if (pll->frac_width) - numer_khz += - ((uint64_t)pll->input_khz * fracdiv) >> pll->frac_width; - freq_mhz = (double)numer_khz / (double)denom / 1000.0; - } + double freq_mhz = pll_freq_from_raw(pll, raw1, raw2); if (brief) { ADD_PARAM_NUM("freq_mhz", freq_mhz); diff --git a/src/clocks.h b/src/clocks.h index 8b5bb281..701debc7 100644 --- a/src/clocks.h +++ b/src/clocks.h @@ -92,6 +92,10 @@ struct clock_family { * HPM aux register, etc.) -- used by `ipctool clocks`. */ cJSON *clocks_build_json(bool brief); +/* One PLL's output frequency, read live. False if a register read fails or + * the fields make no frequency. */ +bool pll_read_freq_mhz(const struct pll_info *pll, double *mhz); + int clocks_cmd(int argc, char **argv); #endif /* CLOCKS_H */ diff --git a/src/hal/common.c b/src/hal/common.c index 7cefdc63..6404dad9 100644 --- a/src/hal/common.c +++ b/src/hal/common.c @@ -12,6 +12,7 @@ #include "tools.h" int i2c_adapter_nr = 0; +const int *hal_sensor_buses; sensor_addr_t *possible_i2c_addrs; int (*open_i2c_sensor_fd)(); int (*open_spi_sensor_fd)(); diff --git a/src/hal/common.h b/src/hal/common.h index a6391d87..6acd5e53 100644 --- a/src/hal/common.h +++ b/src/hal/common.h @@ -76,6 +76,12 @@ typedef struct { } sensor_addr_t; extern int i2c_adapter_nr; +/* The i2c buses a board family carries sensors on, -1-terminated, for SoCs + * with more than one sensor interface. NULL (the default) means one sensor: + * the report stops at the first hit, as it always has. Only set it where + * open_i2c_sensor_fd() opens /dev/i2c- as asked, or the + * sweep would find one sensor on every number it tries. */ +extern const int *hal_sensor_buses; extern sensor_addr_t *possible_i2c_addrs; typedef int (*read_register_t)(int fd, unsigned char i2c_addr, diff --git a/src/hal/hisi/clocks_v5.c b/src/hal/hisi/clocks_v5.c index 95c8c808..e4df7c9a 100644 --- a/src/hal/hisi/clocks_v5.c +++ b/src/hal/hisi/clocks_v5.c @@ -160,17 +160,6 @@ static const char *v5_dram_type_name(uint32_t t) { } } -/* Decode the actual DDR data rate. The chain is: - * ddr_cksel (CRG+0x2000 bits [18:16]) -- 0 = 24 MHz, 1 = clk_dpll_pst - * DPLL VCO (CRG+0x180 / 0x184, post-divided) -- the DFI clock - * dficlk_ratio (DDR_PHY0+0x78 bits [1:0]) -- DFI:DRAM clock, 0=1:1, 1=1:2, - * 2=1:4, so the DRAM clock is DFI * (1 << dficlk_ratio) - * and the data rate is twice the DRAM clock, data being moved on both edges: - * data rate (MT/s) = phy_clk_mhz * (1 << dficlk_ratio) * 2. - * The Hi3519DV500 data sheet's clock tree runs the DPLL at 667 MHz for its - * "up to 2666 Mbps DDR4"; a Taobao DV500 kit whose boot table is - * ..._DDR4_2666M-4GB reads DPLL 664 MHz, ratio 1:2 -> 2656 MT/s. Without the - * final factor of two this reported the DRAM clock, 1328, as the rate. */ /* Hi3519DV500/Hi3516DV500 A55 core clock: PERI_CRG2064 (CRG+0x2040) * a55_cksel [10:8] picks one of seven sources, and only one of them is the * APLL -- so "cpu_pll" above is not the CPU's frequency. Encoding from the @@ -180,7 +169,7 @@ static const char *v5_dram_type_name(uint32_t t) { * is DV500-only. */ static void v5_dv500_cpu(cJSON *root, bool brief) { static const unsigned fixed_mhz[8] = {24, 675, 0, 792, 833, 900, 1000, 0}; - uint32_t crg2064, apll0, apll1; + uint32_t crg2064; if (!strstr(chip_name, "DV500") || !mem_reg(V5_CRG_BASE + 0x2040, &crg2064, OP_READ)) @@ -190,21 +179,8 @@ static void v5_dv500_cpu(cJSON *root, bool brief) { double mhz = fixed_mhz[sel]; const char *source = "fixed"; if (sel == 2) { - if (!mem_reg(V5_CRG_BASE + 0x000, &apll0, OP_READ) || - !mem_reg(V5_CRG_BASE + 0x004, &apll1, OP_READ)) + if (!pll_read_freq_mhz(&v5_plls[0], &mhz)) /* cpu_pll: the APLL */ return; - unsigned fbdiv = apll1 & 0xFFF; - unsigned refdiv = (apll1 >> 12) & 0x3F; - unsigned p1 = (apll0 >> 24) & 0x7; - unsigned p2 = (apll0 >> 28) & 0x7; - if (!refdiv) - refdiv = 1; - if (!p1) - p1 = 1; - if (!p2) - p2 = 1; - mhz = 24.0 * (fbdiv + (double)(apll0 & 0xFFFFFF) / (double)(1u << 24)) / - (double)(refdiv * p1 * p2); source = "cpu_pll"; } if (mhz == 0) @@ -221,7 +197,22 @@ static void v5_dv500_cpu(cJSON *root, bool brief) { cJSON_AddItemToObject(root, "cpu", cpu); } +/* Decode the actual DDR data rate. The chain is: + * ddr_cksel (CRG+0x2000 bits [18:16]) -- 0 = 24 MHz, 1 = clk_dpll_pst + * DPLL VCO (CRG+0x180 / 0x184, post-divided) -- the DFI clock + * dficlk_ratio (DDR_PHY0+0x78 bits [1:0]) -- DFI:DRAM clock, 0=1:1, 1=1:2, + * 2=1:4, so the DRAM clock is DFI * (1 << dficlk_ratio) + * and the data rate is twice the DRAM clock, data being moved on both edges: + * data rate (MT/s) = phy_clk_mhz * (1 << dficlk_ratio) * 2. + * The Hi3519DV500 data sheet's clock tree runs the DPLL at 667 MHz for its + * "up to 2666 Mbps DDR4"; a Taobao DV500 kit whose boot table is + * ..._DDR4_2666M-4GB reads DPLL 664 MHz, ratio 1:2 -> 2656 MT/s. Without the + * final factor of two this reported the DRAM clock, 1328, as the rate. */ static void v5_extra(cJSON *root, bool brief) { + /* First, and on its own registers: a DDR read failing below must not + * take the CPU clock with it. */ + v5_dv500_cpu(root, brief); + uint32_t crg_cksel = 0; uint32_t phy_ctrl0 = 0; uint32_t phy_dramcfg = 0; @@ -272,8 +263,6 @@ static void v5_extra(cJSON *root, bool brief) { } cJSON_AddItemToObject(root, "ddr", ddr); - v5_dv500_cpu(root, brief); - /* SSMOD (DPLL spread-spectrum) status. */ uint32_t ssmod_raw = 0; if (mem_reg(V5_CRG_BASE + V5_SSMOD_CTRL_OFF, &ssmod_raw, OP_READ)) { diff --git a/src/hal/hisi/hal_hisi.c b/src/hal/hisi/hal_hisi.c index a963e111..94a5a278 100644 --- a/src/hal/hisi/hal_hisi.c +++ b/src/hal/hisi/hal_hisi.c @@ -658,10 +658,12 @@ static void ot_ensure_sensor_enabled(void) { &ot_crg[i], ot_crg_addr[i], CV610_PERI_CRG_SENSOR0_CKEN, CV610_PERI_CRG_SENSOR0_SRST, CV610_PERI_CRG_SENSOR_CKSEL_MASK, CV610_PERI_CRG_SENSOR_CKSEL_24M); - if (is_dv500()) - dv500_i2c5_enable(); } +/* The vendor SDK's sensor buses on DV500: SENSOR0..3 = I2C4, I2C5, I2C3, + * I2C6 (sample_comm_vi.c, sample_vio.c, sample_dis_two_pipe.c). */ +static const int dv500_sensor_buses[] = {4, 5, 3, 6, -1}; + static void ot_ensure_sensor_restored() { for (size_t i = 0; i < ARRCNT(ot_crg_addr); i++) sensor_crg_restore(&ot_crg[i], ot_crg_addr[i]); @@ -711,6 +713,18 @@ static void hisi_ensure_sensor_enabled() { } } +/* What a probe arms: the clocks, as setup_hal_hisi() also does, plus on DV500 + * the I2C5 pads. Those are switched only here, never at setup, because only a + * probe is followed by the hal_cleanup() that switches them back -- a + * chip-name or clocks query goes through setup and nothing else. */ +static void hisi_ensure_sensor_ready() { + hisi_ensure_sensor_enabled(); +#ifdef IPCHW_HISI_V5 + if (chip_generation == HISI_OT && is_dv500()) + dv500_i2c5_enable(); +#endif +} + static void hisi_hal_cleanup() { switch (chip_generation) { #ifdef IPCHW_HISI_V4 @@ -759,8 +773,12 @@ static void get_hisi_sdk(cJSON *j_inner) { #endif void setup_hal_hisi() { - hal_enable_sensor_clock = hisi_ensure_sensor_enabled; + hal_enable_sensor_clock = hisi_ensure_sensor_ready; hisi_ensure_sensor_enabled(); +#ifdef IPCHW_HISI_V5 + if (chip_generation == HISI_OT && is_dv500()) + hal_sensor_buses = dv500_sensor_buses; +#endif open_i2c_sensor_fd = hisi_open_i2c_fd; open_spi_sensor_fd = hisi_open_spi_fd; diff --git a/src/hal/hisi/hal_hisi_test.c b/src/hal/hisi/hal_hisi_test.c index 322da881..e44149ed 100644 --- a/src/hal/hisi/hal_hisi_test.c +++ b/src/hal/hisi/hal_hisi_test.c @@ -174,6 +174,7 @@ static void fresh(uint32_t addr, uint32_t val, int gen) { memset(&v4_crg, 0, sizeof v4_crg); memset(ot_crg, 0, sizeof ot_crg); memset(dv500_i2c5, 0, sizeof dv500_i2c5); + hal_sensor_buses = NULL; strcpy(chip_name, gen == HISI_OT ? "3516CV610" : ""); chip_generation = gen; seed(addr, val); @@ -386,7 +387,9 @@ int main(void) { #define SCLK DV500_IOCFG_SPI1_SCLK #define SDO DV500_IOCFG_SPI1_SDO #define DIR DV500_GPIO1_DIR - /* Stock sys_config leaves them GPIO inputs (0x1300): switched to I2C5, + /* Setup alone -- a chip-name or clocks query, which never reaches a + * cleanup -- leaves the pads alone; only a probe's arm switches them. + * Stock sys_config leaves them GPIO inputs (0x1300): switched to I2C5, * function bits only, and put back by the cleanup. */ fresh(OT, 0x4012, HISI_OT); strcpy(chip_name, "3519DV500"); @@ -394,6 +397,11 @@ int main(void) { seed(SDO, 0x1300); seed(DIR, 0x0); setup_hal_hisi(); + CHECK(peek(SCLK) == 0x1300); + CHECK(peek(SDO) == 0x1300); + CHECK(hal_sensor_buses && hal_sensor_buses[0] == 4 && + hal_sensor_buses[1] == 5); + hal_enable_sensor_clock(); CHECK(peek(SCLK) == 0x1302); CHECK(peek(SDO) == 0x1302); hal_cleanup(); @@ -415,6 +423,7 @@ int main(void) { seed(SDO, 0x1300); seed(DIR, 0x0); setup_hal_hisi(); + hal_enable_sensor_clock(); CHECK(peek(SCLK) == 0x1301); CHECK(peek(SDO) == 0x1300); @@ -425,6 +434,7 @@ int main(void) { seed(SDO, 0x1300); seed(DIR, 0x2); /* GPIO1_1 is an output */ setup_hal_hisi(); + hal_enable_sensor_clock(); CHECK(peek(SCLK) == 0x1300); CHECK(peek(SDO) == 0x1300); @@ -448,12 +458,14 @@ int main(void) { seed(SDO, 0x1300); seed(DIR, 0x0); setup_hal_hisi(); + hal_enable_sensor_clock(); poke(SCLK, 0x1752); hal_cleanup(); CHECK(peek(SCLK) == 0x1752); CHECK(peek(SDO) == 0x1300); - /* Not a DV500: the pads are not even looked at. */ + /* Not a DV500: the pads are not even looked at, and the report stays + * single-sensor. */ fresh(OT, 0x4012, HISI_OT); seed(SCLK, 0x1300); seed(SDO, 0x1300); @@ -461,6 +473,7 @@ int main(void) { setup_hal_hisi(); sweep(); CHECK(peek(SCLK) == 0x1300); + CHECK(hal_sensor_buses == NULL); #undef SCLK #undef SDO #undef DIR diff --git a/src/sensors.c b/src/sensors.c index d87f5eaa..d9555036 100644 --- a/src/sensors.c +++ b/src/sensors.c @@ -1483,11 +1483,6 @@ static void arm_sensor_clock(void) { hal_enable_sensor_clock(); } -/* Highest /dev/i2c-N the sweeps look at. Hi3519DV500 puts sensors 0..3 on - * I2C4, I2C5, I2C3 and I2C6 in the vendor SDK, so stopping at 5 missed the - * last of them. A bus that is not there costs one failed open(). */ -#define SENSOR_MAX_I2C_BUS 7 - /* What the last probe_sensor() already covered, so detect_sensors() does not * probe an empty bus twice -- on a SoC with unmuxed adapters every probe of * one is a run of controller timeouts. */ @@ -1527,7 +1522,7 @@ static bool probe_sensor(sensor_ctx_t *ctx) { * the numbering, and /dev/i2c-1 is missing on most single-bus boards, so * the sweep died one iteration in and never reached the buses behind it. * The fallback has been dead on that hardware since it was written. */ - for (int i = 0; i <= SENSOR_MAX_I2C_BUS; i++) { + for (int i = 0; i <= 5; i++) { if (i == preferred_adapter) continue; @@ -1609,11 +1604,16 @@ static void add_sensor_entry(cJSON *j_sensors, sensor_ctx_t *ctx, int bus, } } -/* Every sensor on the board, not just the first one: dual-sensor designs - * (the Hi3519DV500 reference kit's SENSOR0/SENSOR1, stereo rigs) carry one - * per i2c bus. getsensorid() finds the first, as it always has, and then the - * remaining buses are probed one by one. A bus with nothing on it costs one - * probe; it does not end the sweep. */ +/* Every sensor on the board, not just the first one, where the HAL says the + * board family has more than one (hal_sensor_buses: the Hi3519DV500 reference + * design's SENSOR0..3 sit on I2C4, I2C5, I2C3 and I2C6). getsensorid() finds + * the first, as it always has; then only the listed buses are probed. + * + * Opt-in, and only the listed buses, because a probe is not a read: the + * two-byte register address is a write to a one-byte-address part, and some + * families write page registers. Sweeping every adapter after every hit would + * do that to whatever codec, PMIC or EEPROM shares a sensor address on any + * camera. */ cJSON *detect_sensors() { sensor_ctx_t ctx; memset(&ctx, 0, sizeof(ctx)); @@ -1628,12 +1628,13 @@ cJSON *detect_sensors() { const int first_bus = i2c_adapter_nr; add_sensor_entry(j_sensors, &ctx, first_bus, true); - /* Only for an i2c hit: an SPI sensor leaves i2c_adapter_nr wherever the - * HAL put it, and sweeping from there would report nothing new. */ - if (strcmp(ctx.control, "i2c")) + /* An SPI sensor leaves i2c_adapter_nr wherever the HAL put it, so there + * is no i2c bus to sweep around. */ + if (!hal_sensor_buses || strcmp(ctx.control, "i2c")) return j_sensors; - for (int bus = 0; bus <= SENSOR_MAX_I2C_BUS; bus++) { + for (const int *b = hal_sensor_buses; *b >= 0; b++) { + const int bus = *b; if (bus == first_bus) continue; /* Found by the fallback sweep: the HAL's own bus and every bus below @@ -1641,6 +1642,10 @@ cJSON *detect_sensors() { if (found_by_sweep && (bus < first_bus || bus == probed_preferred_adapter)) continue; + + /* Before the open: the HAL openers take no argument and open the + * adapter this global names. */ + i2c_adapter_nr = bus; if (!i2c_bus_openable(bus)) continue; @@ -1650,12 +1655,16 @@ cJSON *detect_sensors() { more.reg_width = 2; more.j_sensor = cJSON_CreateObject(); - i2c_adapter_nr = bus; arm_sensor_clock(); if (get_sensor_id_i2c(&more)) { strcpy(more.control, "i2c"); add_sensor_entry(j_sensors, &more, bus, false); } else { + /* get_sensor_id_i2c() returns without a cleanup when its own + * open fails, and what the arm switched must go back. A second + * cleanup after one that ran is a no-op: each undo is spent. */ + if (hal_cleanup) + hal_cleanup(); cJSON_Delete(more.j_sensor); if (more.j_params) cJSON_Delete(more.j_params);