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
37 changes: 37 additions & 0 deletions include/ipchw.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,43 @@ const char *getsensoridentity();
const char *getsensorshort();
float gethwtemp();

/* ------------------------------------------------------------------------
* Sensor connectors
*
* A board family with more than one sensor connector, and a HAL that knows
* which i2c bus each one is wired to, can be asked one connector at a time.
* Today that is the Hi3519DV500 reference design: SENSOR0..3 on I2C4, I2C5,
* I2C3 and I2C6. Everywhere else ipchw_sensor_ports() is 0 and
* getsensorshort() remains the question to ask.
*
* Sensors on these boards come and go with their ribbon cables, so a consumer
* that wants to know what is fitted asks at start-up rather than trusting a
* name saved on an earlier boot.
*
* The probe ungates the connector's sensor clock where it has to, and on
* DV500 switches SENSOR1's I2C5 pads from GPIO for the duration; both go back
* as they were found. A consumer that goes on to run that sensor muxes the
* pads itself (ipchw_padmux_set()).
*
* Threading: serialised with getsensoridentity() and getsensorshort(). It
* reaches registers through the same unlocked window as ipchw_padmux_get().
* ---------------------------------------------------------------------- */
typedef struct {
int bus; /* the connector's i2c adapter */
char model[16]; /* as getsensorshort() spells it: "os08a10" */
char twin[16]; /* another part with the same ID registers, spelled the
* same way ("os08a20"), or "" */
} ipchw_sensor_t;

/* The number of connectors, 0 where the board family has one or the HAL does
* not know the wiring. */
int ipchw_sensor_ports(void);

/* What is on connector `port` (0-based): 1 and *out filled, 0 when nothing
* answers on its bus (out->bus still says which bus was asked), -1 for a port
* this board family does not have. */
int ipchw_sensor_probe(int port, ipchw_sensor_t *out);

/* ------------------------------------------------------------------------
* Pad multiplexing
*
Expand Down
76 changes: 74 additions & 2 deletions src/sensors.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "cjson/cJSON.h"
#include "hal/common.h"
#include "hal/xm.h"
#include "ipchw.h"
#include "sensors.h"
#include "tools.h"

Expand Down Expand Up @@ -1663,8 +1664,6 @@ bool getsensorid(sensor_ctx_t *ctx) {
return found;
}

#ifndef STANDALONE_LIBRARY

/* Parts that answer with the same ID registers, so a probe cannot tell them
* apart. sensor_id keeps the name it always had -- libipchw consumers pick a
* driver by it -- and the report says what else it could be. */
Expand All @@ -1677,6 +1676,8 @@ static const char *sensor_id_shared_with(const char *sensor_id) {
return NULL;
}

#ifndef STANDALONE_LIBRARY

static void add_sensor_entry(cJSON *j_sensors, sensor_ctx_t *ctx, int bus,
bool vi_info) {
cJSON *j_inner = ctx->j_sensor;
Expand Down Expand Up @@ -1814,3 +1815,74 @@ const char *getsensorshort() {
pthread_mutex_unlock(&sensor_indentity_mtx);
return ret;
}

/* How many sensor connectors this board family has, where it has more than
* one and the HAL knows which bus each is on (hal_sensor_buses). Called with
* the identity lock held: getchipname() sets the HAL up on first use and is
* not safe to race. */
static int sensor_ports_locked(void) {
if (!getchipname() || !hal_sensor_buses)
return 0;
int n = 0;
while (hal_sensor_buses[n] >= 0)
n++;
return n;
}

int ipchw_sensor_ports(void) {
pthread_mutex_lock(&sensor_indentity_mtx);
const int n = sensor_ports_locked();
pthread_mutex_unlock(&sensor_indentity_mtx);
return n;
}

/* One connector's bus and nothing else: no fallback to SPI or to the other
* buses, which getsensorid() does and which would name the sensor on the
* next connector as this one's. Everything from the HAL's first-use setup to
* the restore of the process-global adapter number runs under the identity
* lock, as getsensoridentity() does. */
int ipchw_sensor_probe(int port, ipchw_sensor_t *out) {
if (!out || port < 0)
return -1;
memset(out, 0, sizeof(*out));

pthread_mutex_lock(&sensor_indentity_mtx);
if (port >= sensor_ports_locked()) {
pthread_mutex_unlock(&sensor_indentity_mtx);
return -1;
}
const int bus = hal_sensor_buses[port];
out->bus = bus;

const int saved = i2c_adapter_nr;
/* Before the open: the HAL openers take no argument and open the adapter
* this global names. */
i2c_adapter_nr = bus;
int found = 0;
dev_t dev;
if (i2c_bus_openable(bus, &dev)) {
sensor_ctx_t ctx;
memset(&ctx, 0, sizeof(ctx));
ctx.data_width = 1;
ctx.reg_width = 2;
arm_sensor_clock();
if (get_sensor_id_i2c(&ctx)) {
found = 1;
lsnprintf(out->model, sizeof(out->model), "%s", ctx.sensor_id);
const char *twin = sensor_id_shared_with(ctx.sensor_id);
if (twin)
lsnprintf(out->twin, sizeof(out->twin), "%s", twin);
}
/* Only after an arm: the undo it owes is this probe's. A probe whose
* own open fails returns without one, and after one that ran this is
* a no-op, each undo being spent. A bus that would not open was never
* armed, and a cleanup there would spend a restore somebody else is
* owed -- the HAL's setup ungates the sensor clock too. */
if (hal_cleanup)
hal_cleanup();
}
i2c_adapter_nr = saved;
restore_printk();
pthread_mutex_unlock(&sensor_indentity_mtx);
return found;
}
92 changes: 92 additions & 0 deletions src/sensors_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,97 @@ static void check_bus_sweep(void) {
i2c_read_register = mock_read;
}

/* ---- one connector at a time ------------------------------------------- */

/* A board with two connectors and the sensor fitted to the second one only:
* asking the first must not answer with the second's sensor, as getsensorid()
* would after its sweep, and a port the board does not have is refused. The
* bus each probe opened is recorded, because the openers take no argument and
* read the process-global adapter number. */
static int ports_sensor_bus;
static int ports_opened[8];
static int ports_nopened;

static int ports_open(int adapter_nr) {
if (ports_nopened < (int)ARRCNT(ports_opened))
ports_opened[ports_nopened++] = adapter_nr;
if (adapter_nr != 4 && adapter_nr != 5)
return -1;
return universal_open_sensor_fd("/dev/null");
}

/* A cleanup with no arm before it spends a restore somebody else is owed --
* the HAL's own setup ungates the sensor clock -- so it is counted. */
static int ports_cleanups;
static void ports_cleanup(void) { ports_cleanups++; }

static int ports_read(int fd, unsigned char addr, uint32_t reg,
unsigned int reg_width, unsigned int data_width) {
if (i2c_adapter_nr != ports_sensor_bus)
return -1;
return mock_read(fd, addr, reg, reg_width, data_width);
}

static void check_sensor_ports(void) {
static unsigned char addrs[] = {0x60, 0};
static sensor_addr_t table[] = {{SENSOR_OMNIVISION, addrs}, {0, NULL}};
/* Bus 6 has no adapter: the third connector's open fails. */
static const int buses[] = {4, 5, 6, -1};
ipchw_sensor_t s;

setup_hal_fallback();
CHECK(ipchw_sensor_ports() == 0);
CHECK(ipchw_sensor_probe(0, &s) == -1);

hal_sensor_buses = buses;
hal_cleanup = ports_cleanup;
open_i2c_sensor_fd = ports_open;
i2c_read_register = ports_read;
i2c_change_addr = mock_change_addr;
possible_i2c_addrs = table;
mock_clear();
mock_set(0x300A, 2, 1, 0x53);
mock_set(0x300B, 2, 1, 0x08);
i2c_adapter_nr = 0;

CHECK(ipchw_sensor_ports() == 3);

ports_sensor_bus = 5;
ports_nopened = 0;
CHECK(ipchw_sensor_probe(0, &s) == 0);
CHECK(s.bus == 4 && !s.model[0] && !s.twin[0]);
for (int i = 0; i < ports_nopened; i++)
CHECK(ports_opened[i] == 4);

CHECK(ipchw_sensor_probe(1, &s) == 1);
CHECK(s.bus == 5);
CHECK(!strcmp(s.model, "os08a10"));
CHECK(!strcmp(s.twin, "os08a20"));
if (strcmp(s.model, "os08a10") || strcmp(s.twin, "os08a20"))
fprintf(stderr, " (port 1: got \"%s\" / \"%s\")\n", s.model, s.twin);

/* Connector 2's bus does not open: nothing armed, so nothing undone. */
ports_cleanups = 0;
CHECK(ipchw_sensor_probe(2, &s) == 0);
CHECK(s.bus == 6 && !s.model[0]);
CHECK(ports_cleanups == 0);
/* An armed probe is undone, found or not. */
CHECK(ipchw_sensor_probe(0, &s) == 0);
CHECK(ports_cleanups > 0);

CHECK(ipchw_sensor_probe(3, &s) == -1);
CHECK(ipchw_sensor_probe(-1, &s) == -1);
CHECK(ipchw_sensor_probe(0, NULL) == -1);
/* The global is put back for `ipctool i2cget` and friends. */
CHECK(i2c_adapter_nr == 0);

hal_sensor_buses = NULL;
hal_cleanup = NULL;
possible_i2c_addrs = NULL;
i2c_read_register = mock_read;
mock_clear();
}

int main(int argc, char **argv) {
i2c_read_register = mock_read;
i2c_write_register = mock_write;
Expand Down Expand Up @@ -835,6 +926,7 @@ int main(int argc, char **argv) {
check_galaxycore_reg_width();
check_probe_restores_console();
check_superpix_spared_by_galaxycore();
check_sensor_ports();
check_bus_sweep();

if (failures)
Expand Down
Loading