Skip to content

novatek: read NA51089 die temperature from the registers - #276

Merged
widgetii merged 2 commits into
masterfrom
novatek-direct-temp
Nov 10, 2026
Merged

widgetii merged 2 commits into
masterfrom
novatek-direct-temp

Conversation

@widgetii

Copy link
Copy Markdown
Member

Closes #275.

ipctool --temp and ipcinfo --temp on an NT98562/NT98566 only knew the vendor nvt_adc driver's thermal zone. OpenIPC's kernel doesn't build that driver, so on a stock image both printed "Temperature cannot be retrieved".

This reads the sensor directly, the way the HiSilicon readers do:

  • Sensor. Channel 3 of the SAR ADC, averaged into 0xF0260040 [24:16]. The ADC clock (CG 0xF0020074 bit 13) is turned on if it is off, and ain3_mode | adc_en are set in 0xF0260000 if they are missing. Setting adc_en alone converts nothing. Nothing is ever cleared, so an ADC the vendor driver already runs is read as it is.
  • Calibration. The trim comes from eFuse row 6, the same value echo trim > /proc/nvt_otp_op/otp_trim prints as [6]. The read sequence is taken from the SDK's binary otp_protected.o_shipped (efuse_open(0,0,0) / efuse_read / efuse_close) and stays in read mode throughout. If no copy in the row is valid, or the kernel has the controller open at that moment, the trim falls back to nvt_adc's default of 231.
  • Formula. nvt_adc's formula for NA51089 is (code - trim) * 1.319 + 30. The script in pls add cpu temp for novatek cpu #275 uses the NA51055 slope (* 13 / 10) with a hand-picked trim.
  • Other dies keep the sysfs reader. It now reports no reading instead of -237.

Tested

Lab NT98566 + SC501AI, OpenIPC nightly 2026-10-21, arm32 build from the CI toolchain (hi3516cv100):

before: en1=0xB0440FF1 adc=0x5000F000 efctl=0x00000000 efsts=0x0000000C
# ipcinfo --chip-name; ipcinfo --temp
nt98566
74.85
after:  en1=0xB0442FF1 adc=0x5080F008 efctl=0x00000000 efsts=0x0000000C efrow=0x00000000 efmode=0x00000000 efdata=0x000080E4
# 10 x ipctool --temp
74.85 74.85 74.85 74.85 74.85 74.85 74.85 74.85 76.17 74.85

Raw code 262 and trim 0xE4 = 228 give 74.85. There was no external thermometer, so the absolute value is only as good as the vendor formula.

novatek_test (new, in the unit-tests job) simulates the eFuse controller and the ADC. It fails on any write that takes the eFuse out of read mode, or that clears a clock or ADC bit someone else set. I confirmed it fails when ain3_mode is dropped or MODE is set to 1.

Size

ipcinfo, measured as the size step does: +928 bytes with IPCHW_VENDORS=all, 0 with none. That's under the 1024-byte budget. It lands in ipcinfo because ipcinfo --temp is what #275 asks for.

Only NA51089 is covered. NA51055 and NA51084 may share the layout, but they are untested.

The only Novatek reader was the vendor nvt_adc driver's thermal zone,
which OpenIPC's kernel does not build, so `ipctool --temp` and
`ipcinfo --temp` had nothing to say on an NT98562/NT98566.

The die thermometer is channel 3 of the SAR ADC, which averages it into
0xF0260040 [24:16]. The ADC is found gated and idle, so it is started
the way the RTOS side's thermal_channel_init() does: ADC clock on (CG
0xF0020074 bit 13), channel 3 continuous and the converter on
(0xF0260000 bits 3 and 23). adc_en alone converts nothing. Only missing
bits are set and nothing is turned off afterwards, so a converter the
vendor driver runs is read as it stands.

The per-die calibration is eFuse row 6 (EFUSE_THERMAL_TRIM_DATA). Its
read sequence exists only in the SDK's binary otp_protected.o_shipped;
this is that blob's efuse_open(0, 0, 0) / efuse_read() / efuse_close(),
read mode throughout. A row carries two copies, each with a programmed
(bit 15) and an invalidated (bit 14) flag, and efuse_readParamOps()
takes the first good one. With none, or with the controller already
open (the kernel mid-read, behind a semaphore userspace cannot take),
the trim is nvt_adc's default, 231. The formula is nvt_adc's for every
die but NA51055: (code - trim) * 1.319 + 30.

The sysfs reader stays for the other dies, and now reports no reading
instead of -237 degC when the file cannot be read.

novatek_test models the eFuse controller and the ADC, and fails on a
write that leaves read mode or clears a bit someone else set.

Lab NT98566 + SC501AI on OpenIPC's 2026-10-21 nightly: trim 0xE4 (228)
from the eFuse, the same value `echo trim > /proc/nvt_otp_op/otp_trim`
prints; code 262 for 74.85 degC, steady over ten reads; eFuse left
closed. ipcinfo grows 928 bytes with IPCHW_VENDORS=all, none with none.
@qodo-free-for-open-source-projects

Copy link
Copy Markdown

PR Summary by Qodo

Read NA51089 die temperature directly from ADC registers

✨ Enhancement 🧪 Tests 🕐 20-40 Minutes

Grey Divider

AI Description

• Enable temperature readings on NT98562/NT98566 images without the vendor thermal driver.
• Apply eFuse calibration when available and preserve existing ADC settings.
• Add simulated hardware tests for calibration, fallback, and register safety.
Diagram

graph TD
  CLI["Temperature commands"] --> D{"NA51089?"} -->|yes| N["Direct reader"] --> A["SAR ADC"] --> T["Temperature result"]
  N --> E["eFuse trim"] --> T
  D -->|no| S["Sysfs reader"] --> T
Loading
High-Level Assessment

The following are alternative approaches to this PR:

1. Enable the vendor thermal driver
  • ➕ Keeps ADC and eFuse access in the kernel
  • ➕ Uses the existing thermal-zone interface
  • ➖ Requires kernel image changes
  • ➖ Does not provide readings on existing stock images without that driver

Recommendation: Keep the chip-scoped direct reader: it solves the missing-reading problem on stock images without changing kernel builds, while leaving other dies on sysfs. The vendor-driver route is preferable for kernel-managed hardware access but does not meet that deployment goal.

Files changed (4) +392 / -6

Enhancement (1) +136 / -5
novatek.cRead NA51089 temperature from calibrated ADC samples +136/-5

Read NA51089 temperature from calibrated ADC samples

• Selects a direct temperature reader for NA51089, enables missing ADC bits without clearing existing settings, and applies valid eFuse row-6 trim or a default. Other dies retain sysfs; failed sysfs reads now return NAN instead of -237.

src/hal/novatek.c

Tests (3) +256 / -1
pr-build-check.ymlRun Novatek hardware simulation in CI +2/-1

Run Novatek hardware simulation in CI

• Adds novatek_test to the unit-test job's build targets and execution list.

.github/workflows/pr-build-check.yml

CMakeLists.txtBuild the standalone Novatek test target +8/-0

Build the standalone Novatek test target

• Defines novatek_test with the HAL source included by the test, common HAL support, and required compile and link settings.

CMakeLists.txt

novatek_test.cSimulate ADC and eFuse temperature reads +246/-0

Simulate ADC and eFuse temperature reads

• Models register behavior to test reader selection, ADC startup and reuse, trim-copy validation, fallback behavior, and absent samples. Checks reject writes that leave eFuse read mode or clear existing clock and ADC bits.

src/hal/novatek_test.c

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

qodo-free-for-open-source-projects Bot commented Nov 10, 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. Some Novatek cameras still have no temperature ✓ Resolved
Description
novatek_setup_hal() selects the direct reader only when chip_generation is CHIP_NA51089, but
the device-tree detection fallback names the chip without setting that value. On an NA51089 camera
without the chip-ID proc entry or a thermal sysfs file, detection succeeds but neither temperature
reader is installed.

2. A working thermal file can be ignored ✓ Resolved
Description
novatek_setup_hal() replaces the sysfs reader unconditionally on NA51089 rather than using it when
the direct reader fails. When the thermal file is readable but /dev/mem cannot be opened or
mapped, the previously available temperature becomes NAN.

3. Old fuse data can skew temperatures ✓ Resolved
Description
nvt_efuse_row() waits for the read-done status bit without clearing a completion left by an
earlier operation before triggering its own read. If that bit is already set, it can accept the old
data register as row 6 and use its first valid-looking half as the thermal trim.

View medium (1)
4. Concurrent fuse reads can disrupt each other 🐞 Bug ☼ Reliability
Description
nvt_efuse_row() checks whether the controller is closed and opens it in separate operations
without acquiring the serialization used by its other owner. Two overlapping temperature queries can
both pass that check, then overwrite each other's row selection or close the controller while the
other read is underway.
Code

src/hal/novatek.c[R172-175]

+    if (!mem_reg(EFUSE_CTRL, &val, OP_READ) || (val & 1))
+        return false;
+    val |= 1;
+    if (!mem_reg(EFUSE_CTRL, &val, OP_WRITE))
Evidence
The code acknowledges the kernel semaphore cannot be taken, uses separate MMIO calls for check and
open, and later writes shared row and control state. Its close path also resets the row and clears
the open bit.

src/hal/novatek.c[166-187]
src/hal/novatek.c[193-200]
src/tools.c[60-75]

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 controller-open check is not atomic with opening it, so overlapping readers can alter each other's eFuse sequence.
## Fix Focus Areas
- src/hal/novatek.c[166-201]
## Recommended Fix
Use a controller read interface that shares the owner's serialization, or otherwise ensure access is exclusive across processes and the kernel before writing controller registers. Do not reset the row or close a controller owned by another operation.

ⓘ 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 move or hide resolved findings to focus on what still needs action

More tips ↗ | Customize Qodo ↗ | Qodo docs ↗

Grey Divider

Qodo Logo

…to sysfs

From review on #276:

- A kernel without nvt_pinmux names the die through its device-tree
  model ("Novatek NA51089") but left chip_generation unset, so no
  temperature reader was installed. The model now sets it.
- With /dev/mem withheld, the direct reader returned no reading even
  where the vendor driver's thermal zone works. It now falls back to it.
- A read-done bit left standing by an earlier reader passed the wait
  for ours while DATA still held that reader's row. It is cleared
  before the trigger.
- Nothing here can take the kernel's eFuse semaphore or lock out a
  second ipctool, so the row is read twice and taken only if both
  agree; an interleaved sequence costs the calibration, not a wrong one.

The register accesses go through two small helpers, which pays for all
of it: ipcinfo now grows 880 bytes over master (IPCHW_VENDORS=all), not
928. novatek_test covers each case with a model whose eFuse reads take
a few polls to land. Lab NT98566: code 259, trim 228, 70.89 degC.
@widgetii

Copy link
Copy Markdown
Member Author

Qodo's four findings, all addressed in 0b5eb87:

  1. No reader on a device-tree-only detect. Confirmed: the lab NT98566's model is Novatek NA51089, and without /proc/nvt_info/nvt_pinmux/chip_id that path never set chip_generation. Detection now sets it from the model. Covered by a test with the chip-ID file absent.
  2. sysfs ignored when /dev/mem is withheld. The direct reader now falls back to the vendor driver's thermal zone when it gets no reading. The first register read is the one that is checked, because it is what tells a withheld /dev/mem apart from an idle ADC.
  3. Stale read-done. The bit is now cleared (write-one-to-clear) and confirmed clear before the trigger. The test model now takes a few polls to finish a read, and starts with read-done already set and row 7's word in DATA. Without the fix, that word decodes to a "valid" trim of 7, and the test catches it.
  4. Concurrent readers. Nothing in userspace can take the kernel's semaphore, so the row is read twice and taken only if both reads agree. A sequence that something else interleaved comes back as a different word and costs the calibration (default 231), never a wrong one. A cross-process lock would cover only ipctool and not the kernel, and ipcinfo has no bytes to spare for it.

The register accesses now go through two small helpers, which pays for all of the above: ipcinfo is +880 bytes over master with IPCHW_VENDORS=all (was +928), and 0 with none.

I re-ran it on the lab NT98566 with the CI toolchain build. ipcinfo --temp printed 72.21. The raw reading was 259 with trim 228 → 70.89. The eFuse was left closed (ctl=0 sts=0xC row=0 mode=0).

@widgetii
widgetii merged commit 0a708af into master Nov 10, 2026
5 checks passed
@widgetii
widgetii deleted the novatek-direct-temp branch November 10, 2026 13:28
widgetii added a commit that referenced this pull request Nov 10, 2026
#276's reader is not NA51089's alone. The SDK's plat-na51055, which
serves both NA51055 and NA51084, matches plat-na51089 on everything the
reader touches:
- the CG and eFuse bases, and the ADC gate (CG_CLK_EN_REG1 bit 13);
- adc_reg.h, byte for byte;
- EFUSE_THERMAL_TRIM_DATA (0x105);
- the eFuse open/read/close sequence and the row-6 decode, both
  decompiled from na51055/otp_protected.o_shipped.

nvt_adc's thermal_get_temp() differs only on NA51055: 1.3 degC per code
and a default trim of 259. Every later die uses 1.319 and 231, a
default the SDK names for the NT98528, an NA51084.

The reader is now installed for all three IDs, choosing NA51055's slope
and default by ID. The NA51055 and NA51084 device-tree models set the ID.
NA51090, NA51103 and NA51023 keep the sysfs reader.

Not measured on NA51055 or NA51084 hardware. NA51089 is unchanged: lab
NT98566 code 257, trim 228, 68.25 degC. ipcinfo +180 bytes
(IPCHW_VENDORS=all).
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.

pls add cpu temp for novatek cpu

1 participant