Skip to content

sensor: imx662 on hi3516cv6xx is the module's own vendor driver - #296

Merged
openipc-ai merged 2 commits into
mainfrom
sensor-imx662-vendor-tuning
Nov 10, 2026
Merged

openipc-ai merged 2 commits into
mainfrom
sensor-imx662-vendor-tuning

Conversation

@openipc-ai

Copy link
Copy Markdown
Contributor

The IMX662 driver added in #282 had no IMX662 tuning: its ISP tables, AWB
calibration and colour matrix were seeds borrowed from smart_sc450ai, and
its README said so. This replaces it with the driver of the SIP-K662C6S
module's stock firmware (xipc, sensor type 272), recovered from that
firmware and rebuilt against this tree's sensor interface and
common/sensor_common.c:

  • the power-on register tables for 1080p30 and 1080p60 RAW12 and 1080p90
    RAW10;
  • the vendor's exposure and gain model: one 0.3 dB table for analog and
    sensor digital gain, high conversion gain above analog index 99;
  • its ISP default tables, AWB calibration and colour matrix, each a byte
    image of its B051 structure, guarded by a _Static_assert so a structure
    that stops matching refuses to compile.

The vendor driver has no mirror/flip; the previous one did. It is added:
HREVERSE/VREVERSE, kept per pipe, written by the callback and again by every
bring-up in standby, both under cis->mutex.

On the module at night, with majestic and the same IQ profile:

  • AE settled at the same exposure and gain with both drivers (50 ms, about
    37x, isp_avelum 42-43). A street at night stays in colour with this
    driver where the previous one gave an almost grey picture, at the cost of
    a slightly softer image.
  • All four orientations come out right, set live (majestic logs "Live image
    orientation (sensor)") and from a restart. R/G and B/G stay within 0.04 of
    the upright picture, so the Bayer phase does not move.

Neither driver reads registers back (cis_read_reg is a stub tree-wide), and
the previous driver's 100 fps mode was declared but refused at the 37.125
MHz sensor clock, so neither is lost.

The IMX662 driver added in #282 had no IMX662 tuning: its ISP tables, AWB
calibration and colour matrix were seeds borrowed from smart_sc450ai, and
its README said so. This replaces it with the driver of the SIP-K662C6S
module's stock firmware (xipc, sensor type 272), recovered from that
firmware and rebuilt against this tree's sensor interface and
common/sensor_common.c:

- the power-on register tables for 1080p30 and 1080p60 RAW12 and 1080p90
  RAW10;
- the vendor's exposure and gain model: one 0.3 dB table for analog and
  sensor digital gain, high conversion gain above analog index 99;
- its ISP default tables, AWB calibration and colour matrix, each a byte
  image of its B051 structure, guarded by a _Static_assert so a structure
  that stops matching refuses to compile.

The vendor driver has no mirror/flip; the previous one did. It is added:
HREVERSE/VREVERSE, kept per pipe, written by the callback and again by every
bring-up in standby, both under cis->mutex.

On the module at night, with majestic and the same IQ profile:

- AE settled at the same exposure and gain with both drivers (50 ms, about
  37x, isp_avelum 42-43). A street at night stays in colour with this
  driver where the previous one gave an almost grey picture, at the cost of
  a slightly softer image.
- All four orientations come out right, set live (majestic logs "Live image
  orientation (sensor)") and from a restart. R/G and B/G stay within 0.04 of
  the upright picture, so the Bayer phase does not move.

Neither driver reads registers back (cis_read_reg is a stub tree-wide), and
the previous driver's 100 fps mode was declared but refused at the 37.125
MHz sensor clock, so neither is lost.
@qodo-free-for-open-source-projects

Copy link
Copy Markdown

PR Summary by Qodo

Replace IMX662 scaffold with SIP-K662C6S vendor driver

✨ Enhancement 🐞 Bug fix 📝 Documentation 🕐 40+ Minutes

Grey Divider

AI Description

• Replace borrowed IMX662 tuning and initialization with the module vendor’s sensor behavior.
• Restore vendor exposure, gain, and mode handling while preserving live and restart orientation.
• Document supported modes, calibration provenance, hardware observations, and remaining
 limitations.
Diagram

graph TD
  ISP["ISP and AE"] --> CMOS["IMX662 callbacks"] --> CTRL["Sensor bring-up"] --> COMMON["Common sensor I2C"] --> SENSOR["IMX662 sensor"]
  CFG["Vendor mode tables"] --> CTRL
  TUNING["Vendor ISP tuning"] --> CMOS
  CMOS --> COMMON
Loading
High-Level Assessment

The following are alternative approaches to this PR:

1. Retain the scaffold and replace only tuning
  • ➕ Smaller change to the existing callback and transport paths.
  • ➖ Leaves the scaffold’s mode sequences and gain behavior unlike the stock module driver.
  • ➖ Retains duplicate I2C and registration handling instead of using sensor_common.

Recommendation: The vendor-driver adaptation is the better fit for a module-specific driver: it replaces borrowed calibration and aligns initialization and AE behavior with the stock firmware. Review the extracted data and mode transitions closely, since the broader replacement carries more integration risk than a tuning-only change.

Files changed (6) +1484 / -1069

Enhancement (5) +1422 / -1008
imx662_cfg.hSupply vendor register tables for three linear modes +316/-183

Supply vendor register tables for three linear modes

• Replaces the shared initialization sequence with separate 1080p30 and 1080p60 RAW12 and 1080p90 RAW10 power-on tables. The tables are consumed by the new bring-up path.

libraries/sensor/hi3516cv6xx/sony_imx662/imx662_cfg.h

imx662_cmos.cAdapt vendor AE, AWB, ISP, and registration callbacks +834/-656

Adapt vendor AE, AWB, ISP, and registration callbacks

• Reworks per-pipe state and registration around sensor_common, adds the vendor gain ladder and exposure limits, and supplies vendor-based AWB, ISP, black-level, and DNG defaults. Selects the three supported modes and connects mirror/flip requests to the new orientation handler.

libraries/sensor/hi3516cv6xx/sony_imx662/imx662_cmos.c

imx662_cmos.hDefine vendor timing, gain, calibration, and register constants +107/-169

Define vendor timing, gain, calibration, and register constants

• Replaces scaffold constants with the stock driver’s mode, AE, AWB, black-level, and register definitions. Removes the unavailable 100 fps mode and defines indexes for frame-synchronous register updates.

libraries/sensor/hi3516cv6xx/sony_imx662/imx662_cmos.h

imx662_sensor_ctrl.cBring up vendor modes and persist orientation per pipe +143/-0

Bring up vendor modes and persist orientation per pipe

• Programs each mode’s register table in standby, applies frame-register defaults and the requested orientation, then starts the sensor. Serializes bring-up and live orientation writes with the per-pipe mutex and reapplies orientation on subsequent bring-ups.

libraries/sensor/hi3516cv6xx/sony_imx662/imx662_sensor_ctrl.c

imx662_sensor_ctrl.hExpose mode bring-up and orientation operations +22/-0

Expose mode bring-up and orientation operations

• Declares the three mode initializers and the per-pipe orientation set and clear functions used by the IMX662 callbacks.

libraries/sensor/hi3516cv6xx/sony_imx662/imx662_sensor_ctrl.h

Documentation (1) +62 / -61
README.mdDocument vendor provenance and supported IMX662 behavior +62/-61

Document vendor provenance and supported IMX662 behavior

• Replaces the scaffold and borrowed-tuning guidance with the stock-driver provenance, three supported linear modes, exposure and gain limits, and orientation behavior. Records on-module image comparisons and the remaining register-read and 100 fps limitations.

libraries/sensor/hi3516cv6xx/sony_imx662/README.md

@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 (0) 📘 Rule violations (0) 📎 Requirement gaps (0) 🎨 UX issues (0) 🔗 Cross-repo conflicts (0) 📜 Skill insights (0)

Grey Divider


Action required

1. First startup can write a zero frame length ✓ Resolved
Description
imx662_default_reg_init() replays register data without ensuring that the VMAX slots contain a
frame length; neither the mode tables nor the register-info initializer supplies one. On a fresh
session before an AE frame-rate update, those slots remain zero from global initialization, so the
first register packet can program VMAX as zero instead of the selected mode's 1251 lines.

2. Switching back to 12-bit retains 10-bit timing ✓ Resolved
Description
imx662_linear_2m90_10bit writes the RAW10 ADC-timing triplet, but neither RAW12 mode table writes
its corresponding RAW12 values. A switch from 90 fps to 30 or 60 fps enters standby without
resetting those registers, leaving the RAW10 timing programmed alongside the RAW12 bit-depth
selection.

3. Failed mode setup is marked initialized ✓ Resolved
Description
cmos_isp_init() discards the result of each mode-specific initializer and sets
sensor_state->init to true unconditionally. If a standby, table, orientation, or start write
fails, the sensor can remain stopped or partially configured while subsequent callbacks treat
initialization as complete.


Remediation recommended

4. Failed exposure writes still start streaming ✓ Resolved
Description
imx662_default_reg_init() logs accumulated register-write failures but returns no status to
imx662_reg_init(). If an exposure, gain, or frame-length write fails while later writes succeed,
bring-up still cancels standby, starts the master, and prints its success banner.

5. Long exposure requests become short exposures ✓ Resolved
Description
cmos_inttime_update() subtracts int_time from the unsigned frame length before bounding the
request. If AE passes an exposure longer than the current frame during a timing transition, the
subtraction wraps and the subsequent clamp selects an exposure near two lines rather than the
maximum available exposure.

6. Teardown can race with a live flip ✓ Resolved
Description
imx662_set_orientation() checks and writes through the I²C descriptor under cis->mutex, but
cmos_isp_exit() closes that descriptor without the same lock. If a live orientation callback
overlaps sensor exit, its writes can run against a descriptor being closed or subsequently reused.

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

The vendor built its NR default against an SDK whose ot_isp_nr_attr has no
load_ref_en or snr_version, so its bytes from 152 on are B051's from 160 on.
Taken as it was, the ISP held an NR default with both fields 0x006C006C (two
coarse strengths) and every later field 8 bytes off, and
ot_mpi_isp_get_nr_attr refused it: majestic logged "IQ: reading the chip
for [static_nr] failed: ERR_ISP_0x47" with this driver and not with the one
it replaced. Re-laid with both fields 0 and the 8 bytes read past the
vendor's table dropped; [static_nr] now applies. The other 16 tables decode
against B051 with every enum and switch in range.

From review:

- The bring-up replays the AE register slots before AE has filled them, and
  global init left VMAX at 0 and SHR0 below its minimum. The unset ones are
  seeded with the mode's frame length and the shortest exposure; what AE
  already wrote is left alone.
- An exposure as long as the frame is the longest SHR0 allows, not an
  unsigned wrap that landed on the shortest.
- The RAW12 tables write the 12-bit AD timing (0x3A50-52, the sensor's own
  defaults), so a switch back from the 90 fps RAW10 mode does not keep the
  10-bit form.
- A failed register write fails the bring-up, which then is not marked
  initialised.
- The I2C descriptor is opened and closed under the sensor lock, which the
  orientation callback holds while it uses it.

On the module: bring-up at 30 fps, live flip, a pipeline rebuild, and
[static_nr] applied, with the picture as before.
@openipc-ai

Copy link
Copy Markdown
Contributor Author

Addressed in 3e2664e:

  1. Zero frame length at first startup: fixed. The VMAX and SHR0 slots are seeded the first time they are set up: the mode's frame length and the shortest exposure. Values AE has already written are left alone.
  2. RAW10 timing kept after a switch back to RAW12: fixed. Both RAW12 tables write the 12-bit AD timing (0x3A50-52 = 0xFF/0x03/0x00, the sensor's power-on values). The shipped sensor config caps the sensor at 30 fps, so this switch could not be exercised on the module.
  3. Failed mode setup marked initialized: fixed. The bring-up result is checked before init is set.
  4. Failed exposure writes still start streaming: fixed. A failed write stops the bring-up before standby is cancelled.
  5. Long exposure requests wrap to short: fixed. A request as long as the frame is clamped to the longest SHR0 allows.
  6. Teardown race with a live flip: fixed. cis_i2c_init/cis_i2c_exit run under cis->mutex, which the orientation callback holds.

While testing these on the module, a seventh problem showed up. The vendor's Bayer NR default table is laid out for an SDK without load_ref_en/snr_version, so it was 8 bytes off from offset 152. The ISP refused to return it (ot_mpi_isp_get_nr_attr 0xA01C8047). It is now re-laid against B051, and an NR profile on top of it applies. The other 16 tables decode cleanly.

@openipc-ai
openipc-ai merged commit b45b6ae into main Nov 10, 2026
37 checks passed
@openipc-ai
openipc-ai deleted the sensor-imx662-vendor-tuning branch November 10, 2026 19:17
widgetii added a commit that referenced this pull request Nov 13, 2026
…ets and upstream STARVIS 2 (#301)

* sensor: imx675 for hi3516cv6xx, its bring-up from the K662C6S firmware

An IMX675 (STARVIS 2, 5 MP) driver on the cv6xx cis framework. The SIP-K662C6S
module's stock firmware has no MPP driver for it, only the register bring-up,
inlined (sub_AB358 for 5M, sub_ABB80 for 4M). Those tables are taken as they
stand. The register map, exposure and gain model, AWB and ISP tables come from
sony_imx664, the nearest STARVIS 2 sibling the firmware carries, and are
untuned for the IMX675.

- 2592x1944 and 2592x1520 (an even, centred crop), 30 fps RAW10, 4 lanes at
  594 Mbps, 37.125 MHz; VMAX 2200, HMAX 1125.
- Mirror and flip on HREVERSE/VREVERSE, kept per pipe, written in standby by
  every bring-up and live by the callback.
- The imx662 hardening: sensor lock around bring-up, SHR0 underflow guard,
  VMAX/SHR0 seeded before AE, checked bring-up, _Static_assert on each ISP
  byte image, and the Bayer NR re-lay of #296.

Checked against Sony's IMX675 Standard Register Setting Ver1.0 (column 4lane,
10bit, 594Mbps, 30fps) and the upstream Linux sony-starvis2 v7 series
(OpenIPC/firmware#2736). Every register the sheet sets matches, except 25
analog values where the vendor table is an earlier revision: 0x3c0a-0x3c0f,
0x4550-0x4564 and 0x347b. Both references agree on those, so they follow Sony;
each is marked with the vendor's value. LANEMODE is left at reset (4 lanes),
where HMAX 1125 clears both references' shortest line.

Not run on an IMX675 yet.

* sensor: imx664 for hi3516cv6xx, from the K662C6S firmware

The IMX664 (STARVIS 2, 4 MP) driver of the SIP-K662C6S module's stock firmware,
recovered from that firmware and rebuilt against this tree's sensor interface
and common/sensor_common.c, as sony_imx662 was in #296: its power-on table,
exposure and gain model (0.3 dB ladder, high conversion gain above analog index
99), AWB calibration and ISP default tables.

- 2688x1520 30 fps RAW12, 2 lanes at 1188 Mbps, 37.125 MHz.
- Mirror and flip on HREVERSE/VREVERSE, per pipe, as sony_imx662.
- The imx662 hardening, and the same Bayer NR re-lay: the vendor's NR image is
  byte-identical to imx662's.

The table is the vendor's, byte for byte. Neither Sony's IMX662 sheet nor the
upstream STARVIS 2 driver covers the IMX664, so it has no register oracle. Its
ADBIT (0x3022) value 2 is not one the sibling sheets define; it is kept and
noted.

Not run on an IMX664 yet.

* sensor: imx678 for hi3516cv6xx, checked against the upstream STARVIS 2 driver

The K662C6S firmware has only scene tuning for the IMX678 (STARVIS 2, 8 MP),
no driver. This one starts from Sony's Hi3519DV500 driver, a self-contained
driver with its own I2C layer, and is rewritten onto the cv6xx cis framework
with sony_imx662's structure and hardening.

- One mode: 3840x2160 30 fps RAW12, 4 lanes at 1188 Mbps, 37.125 MHz;
  VMAX 2250, HMAX 1100. The source's 60 fps mode is dropped: its init only
  ever wrote the 4K30 table.
- The init table is the source's, with SHR0 seeded at 4 (Sony's minimum, not
  3). It also adds the 13 registers the upstream Linux sony-starvis2 v7 table
  sets and the source did not. Against that table there are no value
  mismatches over its ~370 registers (OpenIPC/firmware#2736).
- Gain: the IMX678's own 27-step HCG offset, switched above analog index 99.
  The source switched at 27, which would write gain codes below 34 under
  HCG, and Sony's reference forbids those.
- The IMX678's own AWB, colour matrix and DNG colour. Its Bayer NR, DRC, LSC
  and ACS are imx664 byte images, and its CA is sc450ai's; each is a
  _Static_assert-guarded b[N]. Mesh shading off, as the vendor ships every
  CV610 sensor. The sharpen fields CV610 has and the Hi3519DV500 lacks are
  filled from imx664.
- Mirror and flip on HREVERSE/VREVERSE; RGGB in every orientation, as
  upstream.

Not run on an IMX678 yet, and 4K30 throughput on CV610 is unmeasured.

* sensor: imx415 for hi3516cv6xx, from the K662C6S firmware

The K662C6S board's default sensor, the IMX415 (original STARVIS, 8 MP). The
bring-up is the stock firmware's (sub_A9278 8M, sub_A9A8C 2M binning). The
gen-1 exposure and gain model (no conversion gain) and the AWB come from the
Hi3516CV500 driver, and the ISP tables are the firmware's IMX335 tables,
untuned for the IMX415.

- 3840x2160 and 1920x1080 2x2 binning, 30 fps RAW10, 4 lanes at 891 Mbps,
  37.125 MHz; VMAX 2250, HMAX 1100.
- The CFA is GBRG, as the CV500 driver, mainline Linux (SGBRG10) and the
  OpenIPC CV500 profile have it; the extraction had carried RGGB over from
  its IMX335 template.
- Mirror and flip on 0x3030. The inverted readout starts one pixel further in,
  so the Bayer phase stays put.
- The imx662 hardening and Bayer NR re-lay; mesh shading off, since the
  IMX335 grid fits no IMX415 mode (#299).

Checked against mainline drivers/media/i2c/imx415.c (its init table, the
891 Mbps link table and the 37.125 MHz clock parameters): no value mismatches.
HMAX 1100 is the 4-lane shortest line at that rate. 0x30c0/0x30c1 (sync pin
outputs) stay at reset, high impedance, as the vendor runs them.

Not run on an IMX415 yet.

* sensor: sc850sl for hi3516cv6xx, from the K662C6S firmware

The SmartSens SC850SL (8 MP) driver of the SIP-K662C6S stock firmware, which
has no SDK counterpart. Its init is unrolled in the firmware, not a table, and
was recovered write by write. Its ISP default tables come byte for byte from
the firmware's callback set (sub_B4538); the gain tables are SmartSens-standard
and were checked against the binary. Laid out as smart_sc450ai.

- 27 MHz sensor clock ("27MInput_4lane_1080Mbps_12bit").
- Both Bayer NR images had the #296 layout skew and are re-laid.
- Hardening as sony_imx662. Standby and restart drive 0x0100; the vendor's were
  empty.
- Mirror and flip on 0x3221, relative to the 0x66 the init writes (the module
  is mounted rotated). BGGR in every orientation, as the XMedia SC850SL driver
  reports it.

Not run on an SC850SL yet.

* sensor: gc2063 for hi3516cv6xx, from the K662C6S firmware

The GalaxyCore GC2063 (2 MP, GC2053 register-compatible), laid out as
galaxycore_gc4653. The init is the stock firmware's (sub_AE770), and it is
write for write the Hi3516CV500 GC2053 1080p30 sequence. The exposure and gain
model and the AWB come from that driver. The ISP tables are gc4023's, untuned
for the GC2063.

- 1920x1080 30 fps RAW10, 27 MHz ("gc2063 27Mclk 2M30fps(MIPI)").
- AE as the GC2053 drivers and gc4653 sync it: shutter and gain one frame
  after VMAX.
- RGGB, the order the OpenIPC GC2053 profiles run this same table with.
- Mirror and flip on page-0 0x17. The GC2053 references move no window for
  it. Not verified on a GC2063.
- Hardening as sony_imx662. Standby and restart use 0xf2/0xfc, as the CV500
  driver does.

Not run on a GC2063 yet.

* sensor: imx662 holds SHR0 at Sony's minimum of 4

Sony's IMX662 register setting sheet and the upstream Linux STARVIS 2 driver
(since v7's exposure fix) put SHR0 in [4, VMAX - 1]. The driver clamped it at
3, and it told AE that VMAX - 2 lines was the longest exposure, which that
clamp never allowed. Both are now 4. The longest exposure is one line shorter,
and AE is told exactly what it gets.

Found while checking the cv6xx STARVIS 2 drivers against those references
(OpenIPC/firmware#2736).

* sys_config: hi3516cv6xx clocks for gc2063, sc850sl, imx415, imx664, imx675, imx678

GC2063 and SC850SL run at 27 MHz, as their vendor banners say. The four Sony
sensors run at 37.125 MHz, as INCK_SEL in each init table says. Without an
entry, the sensor clock is written 0 and the sensor stays dark.

* README: list the hi3516cv6xx sensors

Adds the six drivers above, and GC4653, MIS20S1, CV2003 and VS4388, which had drivers but no rows.

* sensor: review round on the recovered hi3516cv6xx drivers

- sc850sl: the bring-up seeded only the linear-mode slots, so a 2-to-1 WDR
  bring-up replayed zero long and short exposure, gains and short-exposure
  limit over the WDR table. Each is now seeded from the WDR table's own
  values (long 0x1000, short 0x100, limit 0x10a, 1x gain), and only where AE
  has not written yet.
- sc850sl: the pixel-calibration hooks wrote VMAX low byte first to 0x320e,
  its high byte, and the exposure as plain bytes. On leaving they wrote back
  fl_std, which is in half lines. They now write VMAX high byte first and the
  exposure in half lines with the 4/8/4 split cmos_inttime_update_linear()
  uses, and leave with fl_std >> 1.
- imx675, imx664, imx415: AE was told the longest exposure was VMAX - 2
  (VMAX - 8 on the IMX415), but SHR0 stays at 6 (9) or above, so the longest
  is VMAX - 6 (VMAX - 9). FL_OFFSET_LINEAR now equals SHR0_MIN, and AE is
  told what it gets.
- gc2063: the analog-gain ceiling stays at 113168, below the table's last
  entry, as the Hi3516CV500 GC2053 driver has it; now said beside the define.
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