Skip to content

xm: clock the XM530V200's sensor before probing it - #302

Merged
dimerr merged 3 commits into
masterfrom
xm-v200-sensor-clock
Dec 1, 2026
Merged

dimerr merged 3 commits into
masterfrom
xm-v200-sensor-clock

Conversation

@dimerr

@dimerr dimerr commented Dec 1, 2026 •

Copy link
Copy Markdown
Collaborator

On an XM530V200 (or XM550V200, the same die) running OpenIPC, ipcinfo -s prints nothing. Every I2C read on all three buses times out.

Cause. The sensors have no MCLK. On the vendor firmware, the ISP daemon muxes the MCLK pads and starts the sensor PLL just before its first probe. OpenIPC's U-Boot and module loader leave all of it off, and an unclocked sensor answers no address.

Fix. Same pattern as Anyka: hal_enable_sensor_clock sets up the clocks before the probe, and hal_cleanup puts back what it changed.

Register Value
Sensor PLL 0x20000010 / 0x14 0x810a8000 / 0x20b60000
MCLK divider (low half MCLK0, high half MCLK1) 0x20000044 0x0d620d62
GPIO37 → MCLK0 (sensor at 0x60) 0x10020094 0x82
GPIO236 → MCLK1 (sensor at 0x64) 0x100203b0 0x88

The values are the vendor's probe-time ones: the first PLL program of the SC2336 tuning.

Ownership:

  • A running PLL belongs to whoever started it, and nothing is touched, pads included.
  • Otherwise each register is remembered with what it held and what was left in it. The PLL's "left" is its final 0x810a8000, after the staged writes.
  • Cleanup restores only a register that still reads what was left, so an owner who changed it since keeps it.
  • A failed read or write during setup puts back everything done so far.
  • The clock lock 0x20000000 is restored as found after each write, and only system-controller writes go through it.

Cleanup on every exit. Some probe exits skipped hal_cleanup: the SPI probe, a bus that opened for the arm and not for the probe, and a sweep ending on an empty bus. getsensorid() now runs one on its way out whenever it armed, and never when it did not, so a restore owed to someone else is not spent. Every HAL's cleanup is a no-op once spent.

Only XM530V200 is covered. XM533, XM535 and XM232 are left as they were until measured.

Tested on hardware (XM550V200 board with two SC2336, OpenIPC xm530v200_lite):

  • Each register above was needed. With GPIO37 alone only 0x60 answers; with GPIO236 alone only 0x64; with one half of 0x44 gated, that half's sensor goes silent.
  • The first revision (53c5349): stock ipcinfo -s prints nothing; this one prints sc2336, and ipctool reports SmartSens SC2336 on bus 0 at 0x60. Afterwards the registers read what they held before. With the clock already running, nothing was written.
  • This revision (the review fixes and the second pad) has not been run on the camera yet; that will follow.

Native tests: all 17 build/*_test pass.

ipcinfo size (the CI check, on its toolchain): +962 bytes with all vendors, +104 with none, inside the 1024-byte budget. The clock setup is one table walked by one loop.

Nothing clocks the sensor on an XM530V200 (or XM550V200, the same die)
before the ISP daemon does: the vendor's daemon muxes the MCLK pad and
starts the sensor PLL just before its first probe, and OpenIPC's U-Boot
and module loader leave both off. An unclocked sensor answers no
address, so ipcinfo -s printed nothing and every bus timed out.

hal_enable_sensor_clock now muxes GPIO37 to MCLK (0x10020094 = 0x82) and
starts the sensor PLL (0x20000010/0x14) and its divider (0x20000044) with
the vendor's probe-time values. hal_cleanup restores each register it
changed, and nothing is touched while the clock is running (a streaming
pipeline). XM533, XM535 and XM232 are left as they were.

Tested on an XM550V200 board with an SC2336, OpenIPC firmware: stock
ipcinfo -s prints nothing; this one prints sc2336, and ipctool reports
SmartSens SC2336 on bus 0 at 0x60. Afterwards the pad, the PLL and the
divider read what they held before. With the clock already running,
nothing is written. ipcinfo (shared, -Os): +452 bytes with all vendors,
+0 with none.
@qodo-free-for-open-source-projects

qodo-free-for-open-source-projects Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

PR Summary by Qodo

xm: clock the XM530V200's sensor before probing it

🐞 Bug fix 📝 Documentation 🕐 20-40 Minutes

Grey Divider

AI Description

• Starts the XM530V200 sensor clock (MCLK pad and sensor PLL) before the first probe.
• Restores every clock register it changed in hal_cleanup; does nothing if the clock already runs.
• Documents the XM530V200 clock sequence in CLAUDE.md; XM533, XM535 and XM232 are unchanged.
Merge Risk 🟡 Medium

Blast radius 🟡 Medium 💥 2 capabilities

  • 2 affected capabilities within one bounded functional scope: sensor identification and probing.
  • The changed HAL clock setup is a prerequisite for the same end-to-end XM530V200 sensor-identification operation performed by the probe; the register writes are implementation steps, not an independent user capability.
  • The flag and final cleanup govern one bounded sensor-identification operation across HAL-specific buses and probe modes; SPI, preferred-bus, and sweep paths are entry paths to the same outcome rather than independent functional scopes.
Sources

src/sensors.c[1568-1574]

static bool sensor_armed;

static void arm_sensor_clock(void) {
    sensor_armed = true;
    if (hal_enable_sensor_clock)
        hal_enable_sensor_clock();
}
Behavioral complexity 🔴 High 🧩 Coordinated invariants, State transitions

  • Before, XM probing used the existing HAL cleanup path without this register protocol.
  • After, it conditionally performs an ordered PLL, divider, staged-PLL, and pad sequence, records prior values, and restores only values still owned by this process.
  • The new correctness conditions are ordering, partial-failure rollback, ownership detection, and cleanup across preferred, SPI, fallback, and empty-bus exits.
  • The staged writes and xm_done state interact across probe attempts, so this is more than a direct mapping.
Potential damage 🟡 Medium 🩹 Temporary loss

  • If the changed clock sequence or ownership cleanup regresses, XM530V200 sensor identification may fail or leave clock configuration needing a later probe or restart; the source does not show irreversible data or external financial effects.
  • Reverting the code stops new incorrect setup attempts.
  • A failed discovery can generally be retried after the clock state is corrected; persistent register state may require a restart or manual hardware reset if cleanup cannot restore it.
Assessed commit 861ffdd
Diagram

graph TD
A["ipcinfo probe"] --> B["hal_enable_sensor_clock"] --> C["SoC clock registers"] --> D["Sensor SC2336"]
B --> E["hal_cleanup restore"]
E --> C
Loading
High-Level Assessment

The following are alternative approaches to this PR:

1. Start the clock in U-Boot or the kernel module loader
  • ➕ Clock is set once for every userspace tool, not just ipcinfo
  • ➕ Avoids raw register writes from userspace and the undo bookkeeping
  • ➖ Changes boot firmware or the module loader, a wider scope
  • ➖ The vendor ISP daemon still reprograms the clock, so a boot-time setup may conflict with it
  • ➖ Harder to test on the same board in isolation
2. Reuse the existing mem_reg path with a generic per-SoC clock table
  • ➕ Removes the duplicated register-write helper if other V200 chips are added later
  • ➕ Keeps the clock values in data rather than code
  • ➖ Premature: only one chip is covered, and XM533, XM535 and XM232 are not measured yet
  • ➖ Adds abstraction before the register values for the other chips are known

Recommendation: The PR's approach fits the existing Anyka pattern: a per-chip hook in the HAL that is undone in hal_cleanup. It limits the change to the probe path and leaves boot firmware alone. A boot-time fix would be cleaner for the whole system, but it would widen the scope and may conflict with the vendor daemon. The table-driven alternative is premature until the other chips are measured.

Files changed (2) +82 / -1

Bug fix (1) +73 / -1
xm.cEnables the XM530V200 sensor clock before probing and restores it after +73/-1

Enables the XM530V200 sensor clock before probing and restores it after

• Adds xm_v200_enable_sensor_clock, which writes the MCLK pad and sensor PLL registers with the vendor's probe-time values when the clock is not running. It records each old value in an undo log, and xm_hal_cleanup writes them back in reverse order. The hook is set only for XM530V200 and is skipped if the clock is already running.

src/hal/xm.c

Documentation (1) +9 / -0
CLAUDE.mdDocuments the XM530V200 sensor clock sequence +9/-0

Documents the XM530V200 sensor clock sequence

• Adds a paragraph describing the XM530V200 and XM550V200 clock sequence: GPIO37 MCLK mux, sensor PLL and divider values, and the restore rule. It also states that the other V200 chips are not covered yet.

CLAUDE.md

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

qodo-free-for-open-source-projects Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Code Review by Qodo

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

Grey Divider


Remediation recommended

1. A failed probe leaves the sensor clock on ✓ Resolved Bug Reliability
Description
xm_v200_enable_sensor_clock() arms the clock after a successful preliminary bus open, but that
open does not guarantee the subsequent probe opens will succeed. If the liveness check or actual
probe open then fails, the preferred-bus path can exit without hal_cleanup(), leaving the
registers changed and xm_undo_n set so later probes cannot re-arm the clock.

2. Probing remuxes a pad while its clock runs ✓ Resolved Bug Correctness
Description
xm_v200_enable_sensor_clock() returns for a running sensor PLL only when GPIO37 also reads 0x82;
otherwise it writes 0x82 to the pad. When the PLL is already running but the pad has another
assignment, a probe changes that assignment despite the running-clock no-touch rule, until cleanup
restores it.

3. A register read failure can erase settings ✓ Resolved Bug Reliability
Description
xm_v200_poke() increments the undo count without checking whether mem_reg(..., OP_READ)
populated the saved value, then proceeds with the write. If a later register mapping fails after the
initial pad and PLL reads succeeded, cleanup restores an unpopulated undo value instead of the
register’s original setting.

View review recommended (2)
4. Cleanup can turn off another clock owner ✓ Resolved Bug Reliability
Description
xm_hal_cleanup() unconditionally writes the values captured before this probe, without checking
whether another owner has changed the registers in the meantime. If the ISP starts its clock during
the probe, cleanup can replace its live PLL and pad settings with the earlier boot values.

5. Clock setup changes the unlock state ✓ Resolved Bug Correctness
Description
xm_v200_clk_write() always writes 1 and then 0 to XM_V200_SYS, but the undo stack never
saves that register’s previous value. If it was already 1 before an otherwise successful probe,
setup and cleanup leave it at 0 rather than restoring the pre-probe state.

Grey Divider

Tip of the day
💡 Did you know, you can pick a display mode instead of tweaking every Display option by hand

More tips ↗ | Customize Qodo ↗ | Qodo docs ↗

Grey Divider

Qodo Logo

The XM550V200 board has a second SC2336 at 0x64. Its clock leaves on
GPIO236 (0x100203b0 = 0x88), not GPIO37, and it takes the divider's
high half (0x20000044 = 0x0d620d62 already enables both). Both pads are
now muxed.

The review's points:
- A running PLL is another owner's: nothing at all is touched, pads
  included (a running PLL with GPIO37 elsewhere used to be remuxed).
- A register is remembered only once it has been read; if any read or
  write in the setup fails, everything done so far is put back and the
  probe goes on unclocked.
- Each undo records what was left in the register (the PLL's final
  0x810a8000 after its staged writes). Cleanup restores only a register
  that still reads that, so an owner who changed it since keeps it.
- The clock lock (0x20000000) is put back as found after each write,
  not forced to 0, and only system controller writes go through it.
- Not every way out of the probe passed a hal_cleanup(): the SPI probe,
  a bus that opened for the arm and not for the probe, and a sweep that
  ends on an empty bus. getsensorid() now runs one on its way out
  whenever it armed (never when it did not, so a restore owed to someone
  else is not spent); each HAL's cleanup is a no-op once spent.

ipcinfo (shared, -Os) against master: +704 bytes with all vendors, +52
with none.
ipcinfo grew 1146 bytes (IPCHW_VENDORS=all) with the clock setup, over
the 1024-byte budget. The PLL program and both MCLK pads are now one
table walked by one loop, with what each first write found kept beside
it, in place of a poke helper and its three-word undo records. The
behaviour is unchanged: nothing is touched while the PLL runs, a failed
read or write puts back everything done so far, cleanup restores only a
register that still reads what was left in it, and the lock register is
put back as found.

ipcinfo (CI's measure): +962 bytes with all vendors, +104 with none.
Rechecked on the XM550V200 board: ipcinfo -s and ipctool find the
SC2336, the registers read as before afterwards, and a running clock is
left as it was.
@dimerr
dimerr merged commit 2f6167e into master Dec 1, 2026
5 checks passed
@dimerr
dimerr deleted the xm-v200-sensor-clock branch December 1, 2026 18:27
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