From db919d8d3475e7b2f53af0bb8d2832398d1596a2 Mon Sep 17 00:00:00 2001 From: AI Dev Date: Thu, 26 Nov 2026 08:56:46 +0000 Subject: [PATCH 1/2] novatek: read nvt_adc's thermal zone when the kernel has one, and the trim once On NA51055/NA51084/NA51089 the temperature came from the registers first: ungate the ADC, start channel 3, and run the eFuse open/row/read/close sequence for the trim on every call. The kernel serialises the eFuse with a semaphore userspace cannot take, and it reads the eFuse at run time, not only at boot: kdrv_h26x.ko calls h26x_efuse_check() from kdrv_videoenc_set() and kdrv_videodec_set(), kdrv_ai.ko calls efuse_check_available(), and nvt_usb2dev.ko calls efuse_readParamOps(). Our sequence interleaved with one of those can hand the kernel our row or close the controller under it. A metrics exporter that reports the temperature on every scrape (OpenIPC/majestic#386) would keep reopening that window. Where the kernel carries nvt_adc (OpenIPC/firmware#2950), its zone is the reading, found by type "nvt_thermal" rather than by number, and no register is touched; the kernel read the trim once, at probe, under its own lock. Without the zone, the register path is unchanged except that a completed trim read is kept for the life of the process. One that did not complete (the controller was open, or the two reads disagreed) is not kept, so the next call tries again rather than living with the default. A zone is read in either unit. The vendor driver reports whole degrees and OpenIPC/linux#323 makes ours millidegrees; a die reads -40..125 degC, so a whole-degree value stays inside [-100, 200], and nvt_adc's millidegrees land in there only at 100 (an NA51055 at 0.1 degC). The other Novatek dies' zone0 reader goes through the same conversion, which changes nothing for a whole-degree driver. --- src/hal/novatek.c | 95 ++++++++++++++++++++++++++---------- src/hal/novatek_test.c | 107 +++++++++++++++++++++++++++++++++++------ 2 files changed, 160 insertions(+), 42 deletions(-) diff --git a/src/hal/novatek.c b/src/hal/novatek.c index 26379d5..b337a32 100644 --- a/src/hal/novatek.c +++ b/src/hal/novatek.c @@ -122,25 +122,56 @@ unsigned long novatek_totalmem(unsigned long *media_mem) { return *media_mem + kernel_mem(); } -/* The vendor nvt_adc driver's thermal zone. It reports whole degrees, not the - * millidegrees the sysfs ABI promises, so the value is taken as it stands. */ +/* A thermal zone in either unit. The vendor nvt_adc driver reports whole + * degrees; OpenIPC's reports the millidegrees the sysfs ABI promises + * (OpenIPC/linux#323). A die reads -40..125 degC, so whole degrees stay inside + * [-100, 200], and nvt_adc's millidegrees -- 30000 + k * 1319, or k * 1300 on + * NA51055 -- land in there only at 100: an NA51055 at 0.1 degC. */ +static float zone_celsius(const char *path) { + char buf[16], *end; + if (!line_from_file(path, "(-?[0-9]+)", buf, sizeof(buf))) + return NAN; + const long v = strtol(buf, &end, 10); + if (end == buf) + return NAN; + return v < -100 || v > 200 ? v / 1000.0f : v; +} + float novatek_get_temp() { - char buf[16]; - if (line_from_file("/sys/class/thermal/thermal_zone0/temp", "(.+)", buf, - sizeof(buf))) - return strtof(buf, NULL); - return NAN; + return zone_celsius("/sys/class/thermal/thermal_zone0/temp"); +} + +/* nvt_adc's own zone, by type rather than by number. Where the kernel has + * it, the ADC and the eFuse are the kernel's: it read the trim once, at + * probe, under the semaphore that also orders the encoder's eFuse check + * (kdrv_h26x calls it from kdrv_videoenc_set), and the register path below + * cannot take that semaphore. So with the zone there, no register is read. */ +static bool nvt_zone_temp(float *t) { + char path[64], type[32]; + for (int i = 0;; i++) { + snprintf(path, sizeof(path), "/sys/class/thermal/thermal_zone%d/type", + i); + if (!line_from_file(path, "([a-z0-9_]+)", type, sizeof(type))) + return false; + if (!strcmp(type, "nvt_thermal")) { + snprintf(path, sizeof(path), + "/sys/class/thermal/thermal_zone%d/temp", i); + *t = zone_celsius(path); + return true; + } + } } /* The die thermometer of NA51055, NA51084 and NA51089 -- one SAR ADC, clock * gate and eFuse block at the same addresses on all three, per the SDK's * plat-na51055 (which serves NA51055 and NA51084) and plat-na51089 -- is - * channel 3 of the ADC, averaged by the ADC into its own register. OpenIPC's - * kernel builds no nvt_adc driver, so the ADC is found gated and idle, and is - * started here the way the RTOS side's thermal_channel_init() does: clock on, - * channel 3 continuous, converter on. Both are left running, as the vendor - * driver leaves them; turning a clock off is how a probe takes a peripheral - * away from its owner (OpenIPC/firmware#2439). */ + * channel 3 of the ADC, averaged by the ADC into its own register. A kernel + * without nvt_adc (OpenIPC's, until OpenIPC/firmware#2950) leaves the ADC + * gated and idle, so it is started here the way the RTOS side's + * thermal_channel_init() does: clock on, channel 3 continuous, converter on. + * Both are left running, as the vendor driver leaves them; turning a clock off + * is how a probe takes a peripheral away from its owner + * (OpenIPC/firmware#2439). */ #define NVT_CG_CLK_EN1 0xF0020074 #define NVT_CG_ADC (1u << 13) #define NVT_ADC_CTRL 0xF0260000 @@ -221,19 +252,32 @@ static bool nvt_efuse_row(uint32_t row, uint32_t *data) { * * The row is read twice and taken only if both agree. Nothing here can lock * out the kernel or a second ipctool, and a sequence one of them interleaved - * with comes back as a different word, which costs the calibration and - * nothing else. */ + * with comes back as a different word, which costs us the calibration. It can + * also hand the kernel's reader our row, which is why the result is kept: a + * fuse does not change, and a libipchw consumer such as a metrics exporter + * may ask for the temperature every few seconds. A read that did not complete + * is not kept, so the next call tries again rather than living with the + * default. */ +static int nvt_trim = -1; + static int nvt_thermal_trim() { + if (nvt_trim >= 0) + return nvt_trim; + const int dflt = chip_generation == CHIP_NA51055 ? NA51055_DEFAULT_TRIM + : NVT_DEFAULT_TRIM; uint32_t row, again; - if (nvt_efuse_row(NVT_THERMAL_TRIM_ROW, &row) && - nvt_efuse_row(NVT_THERMAL_TRIM_ROW, &again) && row == again) - for (int half = 0; half < 2; half++) { - const uint32_t w = (row >> (16 * half)) & 0xffff; - if ((w & 0xc000) == 0x8000) - return w & 0x3fff; + if (!nvt_efuse_row(NVT_THERMAL_TRIM_ROW, &row) || + !nvt_efuse_row(NVT_THERMAL_TRIM_ROW, &again) || row != again) + return dflt; + nvt_trim = dflt; + for (int half = 0; half < 2; half++) { + const uint32_t w = (row >> (16 * half)) & 0xffff; + if ((w & 0xc000) == 0x8000) { + nvt_trim = w & 0x3fff; + break; } - return chip_generation == CHIP_NA51055 ? NA51055_DEFAULT_TRIM - : NVT_DEFAULT_TRIM; + } + return nvt_trim; } static float nvt_read_temp() { @@ -269,10 +313,9 @@ static float nvt_read_temp() { return ((int)code - nvt_thermal_trim()) * slope + 30; } -/* A kernel that withholds /dev/mem may still carry the vendor driver. */ static float nvt_get_temp() { - const float t = nvt_read_temp(); - return isnan(t) ? novatek_get_temp() : t; + float t; + return nvt_zone_temp(&t) ? t : nvt_read_temp(); } static int novatek_open_i2c_fd(int i2c_adapter_nr) { diff --git a/src/hal/novatek_test.c b/src/hal/novatek_test.c index 08f294f..a221c3e 100644 --- a/src/hal/novatek_test.c +++ b/src/hal/novatek_test.c @@ -43,10 +43,13 @@ static struct { uint32_t second_read; /* if set, what an interleaved second read sees */ } M; -static int efuse_writes, adc_writes, cg_writes; +static int efuse_writes, adc_writes, cg_writes, mmio_calls; static const char *illegal; -static bool mmio_fails; /* /dev/mem withheld */ -static const char *dt_model, *sysfs; /* line_from_file's answers */ +static bool mmio_fails; /* /dev/mem withheld */ +static const char *dt_model; /* line_from_file's answers */ +static struct { + const char *type, *temp; /* NULL type: no such zone */ +} zone[4]; static void fresh(void) { memset(&M, 0, sizeof M); @@ -56,11 +59,13 @@ static void fresh(void) { M.efuse_sts = 0x0000000C; M.fuse[6] = 0x000080E4; M.code = 265; - efuse_writes = adc_writes = cg_writes = 0; + efuse_writes = adc_writes = cg_writes = mmio_calls = 0; illegal = NULL; mmio_fails = false; - dt_model = sysfs = NULL; + dt_model = NULL; + memset(zone, 0, sizeof zone); chip_generation = CHIP_NA51089; + nvt_trim = -1; } static bool converting(void) { @@ -77,6 +82,7 @@ static void complete_read(void) { } bool mem_reg(uint32_t addr, uint32_t *data, enum REG_OPS op) { + mmio_calls++; if (mmio_fails) return false; if (op == OP_READ) { @@ -162,10 +168,14 @@ bool mem_reg(uint32_t addr, uint32_t *data, enum REG_OPS op) { bool line_from_file(const char *filename, const char *re, char *buf, size_t n) { (void)re; const char *ans = NULL; + int i; + char leaf[8]; if (!strcmp(filename, "/proc/device-tree/model")) ans = dt_model; - else if (!strcmp(filename, "/sys/class/thermal/thermal_zone0/temp")) - ans = sysfs; + else if (sscanf(filename, "/sys/class/thermal/thermal_zone%d/%7s", &i, + leaf) == 2 && + i >= 0 && i < 4 && zone[i].type) + ans = !strcmp(leaf, "type") ? zone[i].type : zone[i].temp; if (!ans) return false; snprintf(buf, n, "%s", ans); @@ -205,11 +215,12 @@ int main(void) { CHECK(efuse_closed()); CHECK(illegal == NULL); - /* A second read touches neither the clock nor the ADC. */ - cg_writes = adc_writes = 0; + /* A second read touches neither the clock nor the ADC, and the trim is + * not read again: every sequence is a window on the kernel's readers. */ + cg_writes = adc_writes = efuse_writes = 0; t = hal_temperature(); CHECK(near(t, 265, 228)); - CHECK(cg_writes == 0 && adc_writes == 0); + CHECK(cg_writes == 0 && adc_writes == 0 && efuse_writes == 0); /* An ADC someone else runs is read, not reconfigured: the vendor driver * leaves clkdiv 1 and all four channels continuous. */ @@ -246,13 +257,17 @@ int main(void) { CHECK(near(nvt_get_temp(), 265, NVT_DEFAULT_TRIM)); /* The kernel mid-sequence: the controller is left alone entirely, and - * the reading falls back to the default trim rather than failing. */ + * the reading falls back to the default trim rather than failing -- for + * this call only. Once the kernel is done, the fused trim is read. */ fresh(); M.efuse_ctrl = 1; M.efuse_sts |= 1; t = nvt_get_temp(); CHECK(near(t, 265, NVT_DEFAULT_TRIM)); CHECK(efuse_writes == 0); + M.efuse_ctrl = 0; + M.efuse_sts &= ~1u; + CHECK(near(nvt_get_temp(), 265, 228)); /* A read-done bit someone left standing, with their row still in DATA: * that word is not row 6, and must not be taken for it. */ @@ -268,14 +283,62 @@ int main(void) { M.second_read = 0x000080F0; CHECK(near(nvt_get_temp(), 265, NVT_DEFAULT_TRIM)); CHECK(M.reads_of_row6 == 2); + M.second_read = 0; + CHECK(near(nvt_get_temp(), 265, 228)); + + /* An unfused die is an answer, not a failure: kept like a fused one. */ + fresh(); + M.fuse[6] = 0; + CHECK(near(nvt_get_temp(), 265, NVT_DEFAULT_TRIM)); + efuse_writes = 0; + CHECK(near(nvt_get_temp(), 265, NVT_DEFAULT_TRIM)); + CHECK(efuse_writes == 0); - /* /dev/mem withheld: the vendor driver's thermal zone, if there is one, - * still answers -- and with none, there is no reading. */ + /* The kernel carries nvt_adc (OpenIPC/firmware#2950, with the units of + * OpenIPC/linux#323): its zone is read, and no register is touched. */ + fresh(); + zone[0].type = "nvt_thermal"; + zone[0].temp = "47147"; + CHECK(fabsf(nvt_get_temp() - 47.147f) < 0.001f); + CHECK(mmio_calls == 0); + + /* The vendor's own kernel, in whole degrees. */ + zone[0].temp = "52"; + CHECK(nvt_get_temp() == 52.0f); + CHECK(mmio_calls == 0); + + /* Below zero either way: nvt_adc's millidegrees nearest 0 on NA51089 + * are -337 and 982, both outside the whole-degree window. */ + zone[0].temp = "-337"; + CHECK(fabsf(nvt_get_temp() + 0.337f) < 0.001f); + zone[0].temp = "-12"; + CHECK(nvt_get_temp() == -12.0f); + + /* Found by type, not by number. */ + fresh(); + zone[0].type = "cpu_thermal"; + zone[0].temp = "99000"; + zone[1].type = "nvt_thermal"; + zone[1].temp = "45828"; + CHECK(fabsf(nvt_get_temp() - 45.828f) < 0.001f); + CHECK(mmio_calls == 0); + + /* The zone is there but will not read: no reading, and still no + * register -- the ADC and the eFuse are the kernel's. */ + fresh(); + zone[0].type = "nvt_thermal"; + CHECK(isnan(nvt_get_temp())); + CHECK(mmio_calls == 0); + + /* Some other zone only: the registers, as with none. */ + fresh(); + zone[0].type = "cpu_thermal"; + zone[0].temp = "99000"; + CHECK(near(nvt_get_temp(), 265, 228)); + + /* /dev/mem withheld and no nvt_adc: no reading. */ fresh(); mmio_fails = true; - sysfs = "66"; - CHECK(nvt_get_temp() == 66.0f); - sysfs = NULL; CHECK(isnan(nvt_get_temp())); /* A kernel without nvt_pinmux: the device tree names the die, and that @@ -305,6 +368,7 @@ int main(void) { CHECK(hal_temperature == nvt_get_temp); CHECK(near(hal_temperature(), 265, 228)); M.fuse[6] = 0; + nvt_trim = -1; /* another die, so another process */ CHECK(near(nvt_get_temp(), 265, NVT_DEFAULT_TRIM)); /* NA51055 has its own slope and default: 1.3 degC per code, trim 259. */ @@ -315,6 +379,7 @@ int main(void) { CHECK(hal_temperature == nvt_get_temp); CHECK(near55(hal_temperature(), 265, 228)); M.fuse[6] = 0; + nvt_trim = -1; CHECK(near55(nvt_get_temp(), 265, NA51055_DEFAULT_TRIM)); CHECK(illegal == NULL); @@ -342,6 +407,16 @@ int main(void) { CHECK(hal_temperature != nvt_get_temp); } + /* Their zone is read in whichever unit its driver uses. */ + fresh(); + zone[0].type = "thermal"; + zone[0].temp = "66"; + CHECK(novatek_get_temp() == 66.0f); + zone[0].temp = "45000"; + CHECK(novatek_get_temp() == 45.0f); + zone[0].type = NULL; + CHECK(isnan(novatek_get_temp())); + if (failures) fprintf(stderr, "novatek_test: %d failure(s)\n", failures); else From 9391cc5cc57541ab0712340bb6fae0ec96d7b683 Mon Sep 17 00:00:00 2001 From: AI Dev Date: Thu, 26 Nov 2026 09:04:10 +0000 Subject: [PATCH 2/2] novatek: step over gaps in the zone numbering, keep the zone0 fallback, store the trim once From review: a driver that unregisters its zone leaves a gap, and the scan stopped at the first missing number; a kernel that withholds /dev/mem got no reading even with a readable zone of another type, which the reader before this change returned; and the cache held the default for a moment before the fused value replaced it. --- src/hal/novatek.c | 22 +++++++++++++++------- src/hal/novatek_test.c | 14 +++++++++++++- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/src/hal/novatek.c b/src/hal/novatek.c index b337a32..27a4d1a 100644 --- a/src/hal/novatek.c +++ b/src/hal/novatek.c @@ -145,14 +145,16 @@ float novatek_get_temp() { * it, the ADC and the eFuse are the kernel's: it read the trim once, at * probe, under the semaphore that also orders the encoder's eFuse check * (kdrv_h26x calls it from kdrv_videoenc_set), and the register path below - * cannot take that semaphore. So with the zone there, no register is read. */ + * cannot take that semaphore. So with the zone there, no register is read. + * The scan steps over missing numbers: a driver that unregisters its zone + * leaves a gap. */ static bool nvt_zone_temp(float *t) { char path[64], type[32]; - for (int i = 0;; i++) { + for (int i = 0; i < 16; i++) { snprintf(path, sizeof(path), "/sys/class/thermal/thermal_zone%d/type", i); if (!line_from_file(path, "([a-z0-9_]+)", type, sizeof(type))) - return false; + continue; if (!strcmp(type, "nvt_thermal")) { snprintf(path, sizeof(path), "/sys/class/thermal/thermal_zone%d/temp", i); @@ -160,6 +162,7 @@ static bool nvt_zone_temp(float *t) { return true; } } + return false; } /* The die thermometer of NA51055, NA51084 and NA51089 -- one SAR ADC, clock @@ -269,15 +272,16 @@ static int nvt_thermal_trim() { if (!nvt_efuse_row(NVT_THERMAL_TRIM_ROW, &row) || !nvt_efuse_row(NVT_THERMAL_TRIM_ROW, &again) || row != again) return dflt; - nvt_trim = dflt; + int trim = dflt; for (int half = 0; half < 2; half++) { const uint32_t w = (row >> (16 * half)) & 0xffff; if ((w & 0xc000) == 0x8000) { - nvt_trim = w & 0x3fff; + trim = w & 0x3fff; break; } } - return nvt_trim; + nvt_trim = trim; /* stored once, never half-decided */ + return trim; } static float nvt_read_temp() { @@ -315,7 +319,11 @@ static float nvt_read_temp() { static float nvt_get_temp() { float t; - return nvt_zone_temp(&t) ? t : nvt_read_temp(); + if (nvt_zone_temp(&t)) + return t; + t = nvt_read_temp(); + /* A kernel that withholds /dev/mem may still have a zone of its own. */ + return isnan(t) ? novatek_get_temp() : t; } static int novatek_open_i2c_fd(int i2c_adapter_nr) { diff --git a/src/hal/novatek_test.c b/src/hal/novatek_test.c index a221c3e..21aa446 100644 --- a/src/hal/novatek_test.c +++ b/src/hal/novatek_test.c @@ -314,6 +314,13 @@ int main(void) { zone[0].temp = "-12"; CHECK(nvt_get_temp() == -12.0f); + /* A gap in the numbering: a driver that unregistered its zone. */ + fresh(); + zone[1].type = "nvt_thermal"; + zone[1].temp = "45828"; + CHECK(fabsf(nvt_get_temp() - 45.828f) < 0.001f); + CHECK(mmio_calls == 0); + /* Found by type, not by number. */ fresh(); zone[0].type = "cpu_thermal"; @@ -336,9 +343,14 @@ int main(void) { zone[0].temp = "99000"; CHECK(near(nvt_get_temp(), 265, 228)); - /* /dev/mem withheld and no nvt_adc: no reading. */ + /* /dev/mem withheld and no nvt_adc: some other zone if there is one, + * else no reading. */ fresh(); mmio_fails = true; + zone[0].type = "cpu_thermal"; + zone[0].temp = "66000"; + CHECK(nvt_get_temp() == 66.0f); + zone[0].type = NULL; CHECK(isnan(nvt_get_temp())); /* A kernel without nvt_pinmux: the device tree names the die, and that