Skip to content

novatek: read nvt_adc's thermal zone when the kernel has one, and the trim once - #296

Merged
openipc-ai merged 2 commits into
masterfrom
novatek-temp-prefer-thermal-zone
Nov 26, 2026
Merged

openipc-ai merged 2 commits into
masterfrom
novatek-temp-prefer-thermal-zone

Conversation

@openipc-ai

@openipc-ai openipc-ai commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Problem

On NA51055, NA51084 and NA51089, hal_temperature reads the die sensor through /dev/mem (#276). Every call:

  • ungates the ADC clock if needed,
  • starts channel 3,
  • runs the eFuse open / select row / read / close sequence twice to get the trim.

The kernel orders eFuse access with a semaphore that userspace cannot take. It also reads the eFuse at run time, not only at boot. These vendor modules in novatek-osdrv-nt9856x import eFuse functions:

module import called from
kdrv_h26x.ko h26x_efuse_check kdrv_videoenc_set, kdrv_videodec_set
kdrv_builtin.ko efuse_check_available, efuse_check_available_extend h26x_efuse_check, cnn_eng_valid
kdrv_ai.ko efuse_check_available
nvt_usb2dev.ko efuse_readParamOps

If our sequence overlaps one of these, the kernel can read our row, or have the controller closed under it. The guard that skips an already-open controller is check-then-act. The double read protects our own result, not the kernel's.

This hasn't caused a failure yet: no caller reads the temperature repeatedly. That changes with OpenIPC/majestic#386, which asks for the temperature in majestic's /metrics on nt9856x, so it would be read on every scrape while the camera is streaming.

OpenIPC/firmware#2950 enables nvt_adc on nt9856x, and OpenIPC/linux#323 makes its nvt_thermal zone report millidegrees. Once both land, the old fallback, which takes thermal_zone0 as whole degrees, would report 47147 °C.

Change

  • With the kernel's nvt_thermal zone: that zone is the reading, and no register is touched. The kernel reads the trim once, at probe, under its own lock. The zone is found by its type, not by assuming thermal_zone0, and the scan of thermal_zone0..15 steps over missing numbers. If the zone exists but can't be read, the result is "no reading", not a fallback to the registers.
  • Without the zone (every current OpenIPC nt9856x image, and kernels without /sys/class/thermal): the register path works as before. The one difference is that a completed trim read is now kept for the rest of the process.
    • A read that didn't complete is not kept, so the next call retries. That happens when the controller was already open, or when the two reads disagree.
    • An unfused die is kept too: it is a definite answer.
    • If the register read fails (a kernel that withholds /dev/mem), thermal_zone0 of any type is used, as before.
  • Zone units: a zone reading is accepted in either unit, because the vendor driver reports whole degrees and #323 makes ours millidegrees.
    • A die reads -40..125 °C, so a whole-degree value stays inside [-100, 200].
    • nvt_adc's millidegree values are 30000 + k·1319, or 30000 + k·1300 on NA51055. The only one inside that window is 100, i.e. an NA51055 at 0.1 °C.
    • The zone0 reader for the other Novatek dies uses the same conversion. That changes nothing for a driver that reports whole degrees.
  • The comment that said OpenIPC's kernel builds no nvt_adc now says "until nt9856x: enable the SoC thermal sensor firmware#2950".

Tests

novatek_test passes, as do the other unit tests (cYAML_test, reginfo_test, longse_test, anyka_test, ambarella_test, sstar_test, hal_hisi_test, axera_test). New cases:

  • the zone is read with no register access at all (a counter on mem_reg()), in millidegrees, in whole degrees, and below zero;
  • the zone is found at thermal_zone1 when zone0 is another type, and when zone0 is missing;
  • a present but unreadable zone gives NaN, still with no register access;
  • a zone of another type only falls back to the registers, and is used when /dev/mem is withheld;
  • a second read does no eFuse sequence;
  • an eFuse read that didn't complete (controller open, or the two reads disagreed) isn't kept, and the next call gets the fused trim;
  • the unfused default is kept;
  • the zone0 reader for the other dies, in both units.

Two existing cases changed a fuse in the middle of one process. They now reset the cache first, to stand for a new process.

Hardware: lab NT98566 (NA51089) + SC501AI, nightly-20261021-57ae948, kernel 4.19.91. That image has no /sys/class/thermal. Static arm32 builds of this branch and of master.

The zone cases (C1 to D) run ipcinfo through a small helper that gives it a private mount namespace with a tmpfs over /sys/class. The rest of the camera never sees the fake zone. This exercises the real line_from_file() regexes against files with trailing newlines, which the unit test's mock does not. ADC_CTRL (0xF0260000) is private to the ADC, so it is put back to its boot value before the zone cases: any register-path read would visibly set it to 0x5080F008.

== as found
   CG=0xB0440FF1 ADC_CTRL=0x5000F000
== A. no thermal zone: branch, register path, from the gated boot state
74.85
   CG=0xB0442FF1 ADC_CTRL=0x5080F008
== A. branch x5 vs master x5, alternating
   branch 74.85  master 73.53
   branch 74.85  master 74.85
   branch 74.85  master 74.85
   branch 74.85  master 74.85
   branch 74.85  master 74.85
== B. put the ADC control back to its boot value (ADC-private register)
   CG=0xB0442FF1 ADC_CTRL=0x5000F000
== C1. nvt_thermal 47147 (OpenIPC/linux#323 units)
47.15
   CG=0xB0442FF1 ADC_CTRL=0x5000F000
== C2. nvt_thermal 52 (vendor kernel, whole degrees)
52.00
   CG=0xB0442FF1 ADC_CTRL=0x5000F000
== C3. zone1 is nvt_thermal, zone0 another type
45.83
   CG=0xB0442FF1 ADC_CTRL=0x5000F000
== C4. nvt_thermal that reads below zero, m°C
-0.34
   CG=0xB0442FF1 ADC_CTRL=0x5000F000
== C5. nvt_thermal with an unreadable value
Temperature cannot be retrieved
   CG=0xB0442FF1 ADC_CTRL=0x5000F000
== D. master with the same zone 47147 (ignores it, uses registers)
74.85
   CG=0xB0442FF1 ADC_CTRL=0x5080F008
== E. another type only: branch falls back to registers
74.85
   CG=0xB0442FF1 ADC_CTRL=0x5080F008
== restore as found
   CG=0xB0440FF1 ADC_CTRL=0x5000F000
  • Register path: this branch and master agree. The one master reading of 73.53 is one ADC step (1.32 °C) lower.
  • Zone path: ADC_CTRL stays at its boot value through C1 to C5. Only master (D) and the fallback for a zone of another type (E) start the ADC.

After the review fixes, same camera and same method, with a numbering gap added (C6):

== as found
   CG=0xB0440FF1 ADC_CTRL=0x5000F000
== A. no zone: register path from the gated boot state, x3
77.48
77.48
77.48
   CG=0xB0442FF1 ADC_CTRL=0x5080F008
== ADC_CTRL back to boot value
   CG=0xB0442FF1 ADC_CTRL=0x5000F000
== C1. nvt_thermal 47147
47.15
   CG=0xB0442FF1 ADC_CTRL=0x5000F000
== C2. nvt_thermal 52
52.00
   CG=0xB0442FF1 ADC_CTRL=0x5000F000
== C3. zone1 nvt_thermal, zone0 another type
45.83
   CG=0xB0442FF1 ADC_CTRL=0x5000F000
== C6. gap: no zone0, zone1 nvt_thermal
45.83
   CG=0xB0442FF1 ADC_CTRL=0x5000F000
== C5. nvt_thermal unreadable
Temperature cannot be retrieved
   CG=0xB0442FF1 ADC_CTRL=0x5000F000
== E. another type only: registers
78.80
   CG=0xB0442FF1 ADC_CTRL=0x5080F008
== restore as found
   CG=0xB0440FF1 ADC_CTRL=0x5000F000

Not tested

Size

ipcinfo, measured the way the ipcinfo size step does (shared, SKIP_VERSION, text + data + bss) with the OpenIPC arm musl toolchain:

  • IPCHW_VENDORS=all: 54322 → 54824 (+502)
  • IPCHW_VENDORS=none: 38508 → 38508 (0)

… 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.
@openipc-ai
openipc-ai force-pushed the novatek-temp-prefer-thermal-zone branch from 94dabfb to db919d8 Compare November 26, 2026 08:57
@qodo-free-for-open-source-projects

qodo-free-for-open-source-projects Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

PR Summary by Qodo

Prefer Novatek thermal zones and cache eFuse trim

🐞 Bug fix ✨ Enhancement 🧪 Tests 🕐 20-40 Minutes

Grey Divider

AI Description

• Prefer the kernel’s nvt_thermal zone to avoid conflicting userspace ADC and eFuse access.
• Accept whole-degree and millidegree zones while retaining register fallback on kernels without
 nvt_thermal.
• Cache completed trim reads and test zone selection, fallback, and retry behavior.
Merge Risk 🟡 Medium

Blast radius 🟢 Low 💥 1 capability

  • 1 affected end-to-end capability after fault-specific containment; implementation boundaries do not widen its functional scope.
  • The changed paths all produce one end-to-end Novatek die-temperature result consumed through hal_temperature; zone parsing, zone selection, direct ADC acquisition and eFuse calibration are implementation hops of that bounded capability, not independent user outcomes.
  • This is the same bounded temperature-reporting capability: the zone-first branch can return an unavailable result when the selected zone cannot provide a value, without creating a separate functional scope.
Behavioral complexity 🟡 Medium 🧩 Interacting branches, Compatibility cases

  • Before, supported Novatek die temperature reads used direct ADC/eFuse acquisition and the generic reader directly read thermal_zone0.
  • After, the reader must select among an nvt_thermal zone, direct MMIO acquisition, and generic thermal-zone fallback, while interpreting whole-degree and millidegree formats and caching successful calibration.
  • New correctness conditions are that kernel-owned ADC/eFuse resources are not accessed when nvt_thermal exists, missing zone numbers are skipped, unit conversion remains correct across supported drivers, and a failed calibration is not permanently cached.
  • The direct ADC and eFuse helpers themselves remain established mechanisms; the added complexity comes from their new ordering and compatibility interactions.
Potential damage 🟡 Medium 🩹 Incorrect results

  • If the new unit heuristic, zone selection, or cached calibration result is wrong, the reported Novatek die temperature can be numerically incorrect.
  • This is conditional behavior assessment, not a confirmed defect.
  • Reverting restores the prior reader and no persistent state or external effect requires repair.
  • A code revert restores the previous temperature acquisition path; no persistent temperature data or external action is created by this capability.
Assessed commit 9391cc5
Diagram

graph TD
  H["Temperature reader"] --> Z{"nvt zone found?"} -->|no| R["ADC register read"] --> K{"Trim cached?"} -->|no| E["eFuse trim read"] --> C["Celsius result"]
  Z -->|yes| T["Thermal zone read"] --> C
  K -->|yes| C
Loading
High-Level Assessment

The following are alternative approaches to this PR:

1. Require a kernel thermal zone
  • ➕ Eliminates userspace eFuse access entirely.
  • ➖ Removes temperature readings on current kernels without nvt_adc.

Recommendation: Keep the zone-first approach with a lazy, retryable trim cache. It avoids register access when the kernel owns the sensor without dropping support for existing images; requiring a zone would make those images lose temperature readings.

Files changed (2) +159 / -42

Bug fix (1) +68 / -26
novatek.cPrefer nvt_thermal and retain completed trim reads +68/-26

Prefer nvt_thermal and retain completed trim reads

• Selects nvt_thermal by type and reads it without MMIO, returning no reading rather than using registers if that zone is unreadable. Converts whole-degree or millidegree zone values for this path and the other Novatek dies’ zone0 reader. When no nvt_thermal zone exists, the register path caches completed fused or unfused trim reads but retries incomplete ones.

src/hal/novatek.c

Tests (1) +91 / -16
novatek_test.cCover zone precedence, units, and trim-cache retries +91/-16

Cover zone precedence, units, and trim-cache retries

• Extends the thermal-zone mock and counts MMIO calls to verify that a selected kernel zone never touches registers. Adds cases for zone numbering, both units, unreadable zones, register fallback, cached fused and unfused trims, and retries after incomplete eFuse reads.

src/hal/novatek_test.c

@qodo-free-for-open-source-projects

qodo-free-for-open-source-projects Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (1) 📘 Rule violations (0) 📎 Requirement gaps (0) 🎨 UX issues (0) 🔗 Cross-repo conflicts (0) 📜 Skill insights (0)

Grey Divider


Remediation recommended

1. Concurrent reads can get default trim ✓ Resolved
Description
nvt_thermal_trim writes nvt_trim = dflt at line 271 before the eFuse row is parsed, and only
overwrites it with the fused value at line 275. A second thread calling hal_temperature while the
first is still inside the loop sees nvt_trim >= 0, returns the default trim, and gets a reading
off by the trim difference. The PR says majestic calls this on every /metrics scrape from a
different thread, and the register path (nvt_read_temp) runs mem_reg() with no serialization, so
that overlap is expected. The cache is a plain static int with no atomic or lock, so the first
reader's write is not ordered against the second reader's check.

2. A later kernel zone can be missed ✓ Resolved
Description
nvt_zone_temp() stops scanning when any thermal_zoneN/type is missing or unreadable, rather than
checking whether a later zone has type nvt_thermal. With a gap before that zone, nvt_get_temp()
takes the register path and can access the ADC and eFuse despite the kernel owning them.

3. A readable zone no longer rescues a failed read ✓ Resolved
Description
nvt_get_temp() no longer calls novatek_get_temp() when the register reader returns NAN and no
nvt_thermal zone was found. If /dev/mem is denied while thermal_zone0/temp remains readable
under another type, the temperature command fails where the previous fallback returned that reading.

View review recommended (1)
4. One near-freezing reading becomes 100 °C 🐞 Bug ≡ Correctness
Description
zone_celsius() treats the sysfs value 100 as whole degrees, although the stated NA51055
millidegree conversion also produces 100 at 0.1 °C. When that die's kernel zone reports this
value, both the typed-zone reader and its callers report 100 °C rather than 0.1 °C.
Code

src/hal/novatek.c[137]

+    return v < -100 || v > 200 ? v / 1000.0f : v;
Evidence
The new comment identifies 100 as a possible NA51055 millidegree value, while the new threshold
returns it unchanged. Both temperature entry points use this conversion.

src/hal/novatek.c[125-141]
src/hal/novatek.c[156-160]
src/hal/novatek.c[315-317]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
The value-based unit rule cannot distinguish NA51055's valid 100-millidegree reading from a vendor whole-degree reading of 100.
## Fix Focus Areas
- src/hal/novatek.c[125-141]
- src/hal/novatek.c[149-160]
## Recommended Fix
Select units using a reliable driver or kernel distinction rather than magnitude alone, while retaining support for vendor whole-degree zones. Add tests for both interpretations of the value 100.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Grey Divider

Tip of the day
💡 Did you know, you can tweak Display settings with a live preview to see your comment before it ships

More tips ↗ | Customize Qodo ↗ | Qodo docs ↗

Grey Divider

Qodo Logo

…k, 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.
@openipc-ai

openipc-ai commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor Author

Qodo's review, point by point (fixed in 9391cc5; the second hardware run in the description covers it):

  1. Trim cache published before it was final — fixed: the trim is decided in a local and stored once. No lock added: mem_reg() is not thread-safe either, and the repository already asks callers to serialise register access.
  2. A gap in the zone numbering hides a later nvt_thermal — fixed: thermal_zone0..15 are scanned, stepping over missing numbers. Tested in novatek_test and on the NT98566 (C6).
  3. The zone0 fallback for a withheld /dev/mem was lost — restored: after a failed register read, thermal_zone0 of any type answers, as before. Tested in novatek_test.
  4. 100 reads as 100 °C, not 0.1 °C — kept as is. The only kernels on NA51055 are the vendor's, whose nvt_adc reports whole degrees, so there 100 is 100 °C; the 0.1 °C reading needs an NA51055 running iio: adc: nvt_adc: report the thermal zone in millidegrees linux#323's units, which no image builds. The comment on zone_celsius() says so.

@openipc-ai
openipc-ai merged commit b51e0f9 into master Nov 26, 2026
5 checks passed
@openipc-ai
openipc-ai deleted the novatek-temp-prefer-thermal-zone branch November 26, 2026 09:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant