Skip to content

iio: adc: nvt_adc: report the thermal zone in millidegrees - #323

Merged
openipc-ai merged 1 commit into
OpenIPC:novatek-nt9856xfrom
DrSamjames:nvt-adc-millidegrees
Nov 26, 2026
Merged

openipc-ai merged 1 commit into
OpenIPC:novatek-nt9856xfrom
DrSamjames:nvt-adc-millidegrees

Conversation

@DrSamjames

Copy link
Copy Markdown

thermal_get_temp() returns whole degrees Celsius, but the thermal sysfs ABI (/sys/class/thermal/thermal_zone*/temp) and every in-kernel user of thermal_zone_get_temp() expect millidegrees. Userspace reading the nvt_thermal zone therefore sees 51 instead of 51000, i.e. 0.051 °C.

Change. Keep the same calibration (1.319 °C per ADC step on NA51089, 1.3 °C on NA51055, 30 °C at the OTP trim level) and scale it to millidegrees:

  • NA51089: (raw - trim) * 1319 + 30000
  • NA51055: (raw - trim) * 1300 + 30000

This also drops the integer division, so the reading keeps its sub-degree resolution.

Nothing in the tree consumes the nvt_thermal zone in degrees. I grepped for nvt_thermal, thermal_zone_get_temp and thermal_zone_get_zone_by_name, including drivers/soc/nvt/. None of the vendor .ko modules on the test camera reference thermal_zone or nvt_thermal either.

Tested on a camera (NT98566 / NA51089 + IMX415). The kernel was built from the OpenIPC/firmware nt98566_lite defconfig with this patch, plus CONFIG_THERMAL, CONFIG_IIO and CONFIG_NVT_ADC, which a separate firmware PR enables:

  • Boot log: nvt_adc_probe: f0260000.adc done and thermal thermal_zone0: Thermal Sensor probe. thermal_zone0/type is nvt_thermal.
  • Readings: thermal_zone0/temp against ipcinfo --temp, read together, e.g. 47147 / 47.15 and 45828 / 45.83. 9 of 10 pairs matched exactly; the other was one ADC step (1.32 °C) apart. Both read the same sensor, so this checks the scaling rather than the calibration.
  • After a reboot the sensor was still detected and the stream ran at 25/25 fps, with no new dmesg errors.

Not tested: the NA51055 branch. No NA51055 hardware was available.

🤖 Generated with Claude Code

thermal_get_temp() returns whole degrees Celsius, but the thermal sysfs
ABI (/sys/class/thermal/thermal_zone*/temp) and every in-kernel user of
thermal_zone_get_temp() expect millidegrees. Userspace reading the
nvt_thermal zone therefore sees 51 instead of 51000, i.e. 0.051 C.

Keep the same calibration (1.319 C per ADC step on NA51089, 1.3 C on
NA51055, 30 C at the OTP trim level) and scale it to millidegrees. This
also drops the integer division, so the reading keeps its sub-degree
resolution.

Nothing in the tree consumes the "nvt_thermal" zone in degrees.

Tested on NA51089 (NT98566). NA51055 is untested.

Signed-off-by: DrSamJames <DrSamJames@gmail.com>
@DrSamjames
DrSamjames marked this pull request as ready for review November 24, 2026 19:07
@qodo-free-for-open-source-projects

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

Copy link
Copy Markdown

PR Summary by Qodo

Report nvt_adc thermal temperatures in millidegrees Celsius

🐞 Bug fix 🕐 Less than 10 minutes

Grey Divider

AI Description

• Correct nvt_thermal readings to match the thermal subsystem’s millidegree Celsius ABI.
• Preserve both chip calibrations while retaining sub-degree resolution previously lost to integer
 division.
• Validate NA51089 readings on hardware; NA51055 remains untested.
Merge Risk 🟢 Low

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 raw-sensor conversion is the producer callback for the single thermal-zone device registered as nvt_thermal.
  • The register's ain_avg_out field is the direct sensor input, and the thermal-zone registration is the consumer-facing boundary; these are hops of one temperature-reporting behavior rather than independent capabilities.
Sources

drivers/iio/adc/nvt_adc.c[341-345]

	/* The thermal sysfs ABI reports millidegrees Celsius. */
	if (nvt_get_chip_id() == CHIP_NA51055)
		*temp = (temp_value - adc->trim_value) * 1300 + 30000;
	else
		*temp = (temp_value - adc->trim_value) * 1319 + 30000;
Behavioral complexity 🟢 Low 🧩 Straightforward

  • Before, each chip-ID branch calculated a temperature using the same raw value and trim value, then added 30 after integer division.
  • After, the same existing branch structure and inputs remain, but the output is scaled and offset directly in millidegrees Celsius: 1300 or 1319 millidegrees per sensor count plus 30000.
  • The new correctness condition is unit consistency with the thermal sysfs ABI; it does not add interacting states, new compatibility modes, coordinated invariants, concurrency, ordering, or a new algorithm.
  • The 9-bit ain_avg_out field bounds the transformed input.
Sources

drivers/iio/adc/nvt_adc.c[341-345]

	/* The thermal sysfs ABI reports millidegrees Celsius. */
	if (nvt_get_chip_id() == CHIP_NA51055)
		*temp = (temp_value - adc->trim_value) * 1300 + 30000;
	else
		*temp = (temp_value - adc->trim_value) * 1319 + 30000;
Potential damage 🟡 Medium 🩹 Incorrect results

  • If the changed conversion regresses, the thermal-zone API can report an incorrect temperature, potentially causing userspace or thermal-framework consumers to make decisions from a wrong sensor value.
  • The source shows a single registered zone and no changed persistent state or external write; no evidence supports data loss, confidentiality impact, or irreversible external effects.
  • The 9-bit sensor field bounds the arithmetic, so overflow is not a supported concern for the observed input representation.
  • A code correction or rollback restores the calculation for subsequent reads.
  • Previously emitted incorrect readings are not persistent state in this driver, but any external decision already made from a bad reading would require that consumer to observe later correct readings or perform its own recovery; no automatic compensation path is established here.
Sources

drivers/iio/adc/nvt_adc.c[341-345]

	/* The thermal sysfs ABI reports millidegrees Celsius. */
	if (nvt_get_chip_id() == CHIP_NA51055)
		*temp = (temp_value - adc->trim_value) * 1300 + 30000;
	else
		*temp = (temp_value - adc->trim_value) * 1319 + 30000;
Assessed commit ed5e079
Diagram

graph TD
  ADC["ADC sensor"] --> Convert["Temperature conversion"] --> Zone["Thermal zone"] --> Sysfs["Sysfs temp"]
  Trim["OTP trim"] --> Convert
  Chip["Chip ID"] --> Convert
Loading
High-Level Assessment

Changing the thermal callback’s output units directly is appropriate: it preserves the existing per-chip calibration and satisfies the thermal ABI without changing IIO readings or thermal-zone registration. A broader conversion refactor offers little benefit for this localized fix.

Files changed (1) +5 / -7

Bug fix (1) +5 / -7
nvt_adc.cReturn calibrated thermal readings in millidegrees +5/-7

Return calibrated thermal readings in millidegrees

• Changes thermal_get_temp() to return millidegrees Celsius using the existing NA51055 and other-chip calibration slopes. Removes integer division, preserving sub-degree resolution in the reported value.

drivers/iio/adc/nvt_adc.c

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

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (0)

Grey Divider

Great, no issues found!

Qodo reviewed your code and found no material issues that require review

Grey Divider

Tip of the day
💡 Did you know, you can show, collapse, or hide each part of a finding: code, evidence, and all

More tips ↗ | Customize Qodo ↗ | Qodo docs ↗

Grey Divider

Qodo Logo

openipc-ai added a commit to OpenIPC/ipctool that referenced this pull request Nov 26, 2026
… 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 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The scaling is right: 13/10 → 1300 and 1319/1000 → 1319 m°C per code, +30000 m°C, the same calibration without the truncation. The zone registers no trips, so the unit change cannot start a thermal shutdown. The untested NA51055 branch is reachable by no OpenIPC firmware board: the tree ships nt98562/nt98566 (NA51089) only. ipctool reads the zone in either unit from OpenIPC/ipctool#296.

@openipc-ai
openipc-ai merged commit 641f15d into OpenIPC:novatek-nt9856x Nov 26, 2026
openipc-ai added a commit to OpenIPC/ipctool that referenced this pull request Nov 26, 2026
… trim once (#296)

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.

The scan steps over gaps in the zone numbering, and a kernel that withholds
/dev/mem still falls back to thermal_zone0 of any type, as before.
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.

2 participants