Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,22 @@ reset/power-down GPIOs the device tree names for the camera node, and
`hal_cleanup` puts back exactly what it changed. It touches nothing when the
clock is already running, which is a pipeline streaming.

The XM530V200 (and the XM550V200, the same die) is the same story: nothing
clocks the sensors before the ISP daemon does, and OpenIPC's U-Boot leaves the
clock off. `hal_enable_sensor_clock` starts the sensor PLL (`0x20000010`/`0x14`)
and its divider (`0x20000044`: low half MCLK0, high half MCLK1) with the
vendor's probe-time values, and muxes each sensor's MCLK pad: GPIO37
(`0x10020094` = `0x82`) for the sensor at 0x60, GPIO236 (`0x100203b0` =
`0x88`) for the one at 0x64. All were needed on a two-sensor XM550V200 board.
A running PLL is someone else's and nothing is touched; otherwise each
register is remembered with what it held and what was left in it, and
`hal_cleanup` restores only a register that still reads what was left. The
other V200 chips (XM533, XM535, XM232) are left alone until measured.

Not every way out of the probe passes a `hal_cleanup` (the SPI probe, a bus
that opened for the arm and not for the probe, a sweep ending on an empty
bus), so `getsensorid()` runs one on its way out whenever it armed.

`ipcinfo` is in every camera image and the size check holds it to 1 KiB of
growth per PR, so it carries the least that works, behind
`#ifndef STANDALONE_LIBRARY` for the rest. It starts the CIS clock and takes
Expand Down
97 changes: 96 additions & 1 deletion src/hal/xm.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,101 @@ int xm_sensor_write_register(int fd, unsigned char i2c_addr,
return 0;
}

static void xm_hal_cleanup() { restore_printk(); }
/* The XM530V200's sensor clocks (the XM550V200 is the same die and kernel).
* Nothing starts them before the ISP does: the vendor's ISP daemon muxes the
* MCLK pads and runs the sensor PLL just before its first probe, and OpenIPC's
* U-Boot and module loader leave both off, so an unclocked sensor answers no
* address on any bus. Measured on an XM550V200 board with two SC2336s: the
* PLL, its divider (low half MCLK0, high half MCLK1) and each sensor's pad are
* all needed. The values are the vendor's probe-time ones (the SC2336 tuning's
* first PLL program).
*
* Ownership: a running PLL belongs to whoever started it, and nothing is
* touched then. Otherwise every register written is remembered with what it
* held and what was left in it, and cleanup puts back only a register that
* still reads what was left -- one changed since has another owner. */
#define XM_V200_SYS 0x20000000 /* bit 0 unlocks the clock registers */
#define XM_V200_SNS_PLL 0x20000010 /* bits 31 and 24: running */
#define XM_V200_PLL_ON 0x81000000

/* In order. A nonzero left is a first write, remembered with what the register
* held; zero is a step of the PLL's staged start. */
static const struct {
uint32_t addr, val, left;
} xm_v200_clk[] = {
{XM_V200_SNS_PLL, 0x010a8000, 0x810a8000},
{0x20000014, 0x20b60000, 0x20b60000},
{0x20000044, 0x0d620d62, 0x0d620d62}, /* low half MCLK0, high MCLK1 */
{XM_V200_SNS_PLL, 0x800a8000, 0},
{XM_V200_SNS_PLL, 0x810a8000, 0},
{0x10020094, 0x82, 0x82}, /* GPIO37: MCLK0, the sensor at 0x60 */
{0x100203b0, 0x88, 0x88}, /* GPIO236: MCLK1, at 0x64 */
};
static uint32_t xm_was[ARRCNT(xm_v200_clk)];
static unsigned xm_done; /* entries applied */

/* One write; the system controller's go through its lock, which is put back
* as it was found. */
static bool xm_v200_write(uint32_t addr, uint32_t val) {
uint32_t lock, unlock = 1;
bool ok;

if ((addr & 0xfff00000) != XM_V200_SYS)
return mem_reg(addr, &val, OP_WRITE);
if (!mem_reg(XM_V200_SYS, &lock, OP_READ))
return false;
ok = mem_reg(XM_V200_SYS, &unlock, OP_WRITE) &&
mem_reg(addr, &val, OP_WRITE);
mem_reg(XM_V200_SYS, &lock, OP_WRITE);
return ok;
}

/* Put back what was changed: all of it when setup failed half way, else only
* registers that still hold what was left in them. */
static void xm_v200_undo(bool all) {
while (xm_done) {
uint32_t now, i = --xm_done;

if (xm_v200_clk[i].left &&
(all || (mem_reg(xm_v200_clk[i].addr, &now, OP_READ) &&
now == xm_v200_clk[i].left)))
xm_v200_write(xm_v200_clk[i].addr, xm_was[i]);
}
}

static void xm_v200_enable_sensor_clock() {
uint32_t pll;

if (xm_done || strcmp(chip_name, "XM530V200") ||
!mem_reg(XM_V200_SNS_PLL, &pll, OP_READ) ||
(pll & XM_V200_PLL_ON) == XM_V200_PLL_ON)
return;

/* A clock started for a bus that will not open is stopped by
* getsensorid() on its way out. */
int fd = open_i2c_sensor_fd(i2c_adapter_nr);
if (fd < 0)
return;
close_sensor_fd(fd);

while (xm_done < ARRCNT(xm_v200_clk)) {
unsigned i = xm_done;

if ((xm_v200_clk[i].left &&
!mem_reg(xm_v200_clk[i].addr, &xm_was[i], OP_READ)) ||
!xm_v200_write(xm_v200_clk[i].addr, xm_v200_clk[i].val)) {
xm_v200_undo(true);
return;
}
xm_done++;
}
usleep(20000);
}

static void xm_hal_cleanup() {
xm_v200_undo(false);
restore_printk();
}

void setup_hal_xm() {
open_i2c_sensor_fd = xm_open_sensor_fd;
Expand All @@ -137,6 +231,7 @@ void setup_hal_xm() {
if (xm_is_v200(chip_name)) {
open_i2c_sensor_fd = xm_v200_open_sensor_fd;
possible_i2c_addrs = xm_v200_possible_i2c_addrs;
hal_enable_sensor_clock = xm_v200_enable_sensor_clock;
}
hal_cleanup = xm_hal_cleanup;
#ifndef STANDALONE_LIBRARY
Expand Down
14 changes: 13 additions & 1 deletion src/sensors.c
Original file line number Diff line number Diff line change
Expand Up @@ -1565,7 +1565,10 @@ static bool i2c_bus_openable(int adapter_nr, dev_t *dev) {
* rather than unconditional: it undoes an ungate this process performed and
* nothing else, so it no longer fights the sweep, nor a consumer whose
* pipeline is streaming while we probe (OpenIPC/firmware#2439). */
static bool sensor_armed;

static void arm_sensor_clock(void) {
sensor_armed = true;
if (hal_enable_sensor_clock)
hal_enable_sensor_clock();
}
Expand Down Expand Up @@ -1657,9 +1660,18 @@ static bool probe_sensor(sensor_ctx_t *ctx) {
* console, and not every way out of it passes a
* hal_cleanup(): a bus that opens and then has no sensor on it, for one. The
* console comes back here, on every path, for a consumer that lives on after
* the probe -- majestic does, and it ran with the console off all along. */
* the probe -- majestic does, and it ran with the console off all along.
*
* So does what an arm switched on: the SPI probe, a bus that opened for the
* arm and not for the probe, and a sweep that ends on an empty bus all return
* without a cleanup. Only after an arm, which a probe that never got a chip
* name did not make; after a cleanup that ran this is a no-op, each undo
* being spent. */
bool getsensorid(sensor_ctx_t *ctx) {
sensor_armed = false;
bool found = probe_sensor(ctx);
if (sensor_armed && hal_cleanup)
hal_cleanup();
restore_printk();
return found;
}
Expand Down
Loading