static int i2c_readwrite(unsigned bus, unsigned chip, unsigned addr, unsigned alen, uint8_t *buf, unsigned len, int read) { const uint32_t max_payload = (IOHEADER_PAYLOADSIZE_MASK + 1) >> IOHEADER_PAYLOADSIZE_SHIFT; uint8_t abuf[sizeof(addr)]; int i; for (i = 0; i < alen; i++) abuf[i] = addr >> ((alen - i - 1) * 8); if (tegra_i2c_request(bus, chip, !read, 0, 0, abuf, alen)) return -1; while (len) { int todo = MIN(len, max_payload); int cont = (todo < len); if (tegra_i2c_request(bus, chip, cont, 0, read, buf, todo)) { // We should reset the controller here. return -1; } len -= todo; buf += todo; } return 0; }
static int i2c_transfer_segment(unsigned bus, unsigned chip, int restart, int read, void *buf, int len) { const uint32_t max_payload = (IOHEADER_PAYLOADSIZE_MASK + 1) >> IOHEADER_PAYLOADSIZE_SHIFT; while (len) { int todo = MIN(len, max_payload); int cont = (todo < len); if (tegra_i2c_request(bus, chip, cont, restart, read, buf, todo)) return -1; len -= todo; buf += todo; } return 0; }