staticint ki2c_inuse_lock(struct ki2c *ki2c)
{
u8 sts; int ret;
/* * The I2C controller has an IN_USE bit for locking access to the * controller. This enables the use of I2C controller by other none * Linux processors. * * If the I2C controller is free, then the first read returns * IN_USE == 0. After that the I2C controller is locked and further * reads of IN_USE return 1. * * The I2C controller is unlocked by writing 1 into IN_USE. * * The IN_USE bit acts as a hardware semaphore for the I2C controller. * Poll for semaphore, but sleep while polling to free the CPU.
*/
ret = readb_poll_timeout(ki2c->base + KI2C_STATUS_REG,
sts, (sts & KI2C_STATUS_IN_USE) == 0,
KI2C_INUSE_SLEEP_US, KI2C_INUSE_TIMEOUT_US); if (ret)
dev_err(&ki2c->auxdev->auxdev.dev, "%s err!\n", __func__);
return ret;
}
staticvoid ki2c_inuse_unlock(struct ki2c *ki2c)
{ /* unlock the controller by writing 1 into IN_USE */
iowrite8(KI2C_STATUS_IN_USE, ki2c->base + KI2C_STATUS_REG);
}
/* * Resetting bus bitwise is done by checking SDA and applying clock cycles as * long as SDA is low. 9 clock cycles are applied at most. * * Clock cycles are generated and udelay() determines the duration of clock * cycles. Generated clock rate is 100 KHz and so duration of both clock levels * is: delay in ns = (10^6 / 100) / 2
*/ #define KI2C_RECOVERY_CLK_CNT (9 * 2) #define KI2C_RECOVERY_UDELAY 5 staticint ki2c_reset_bus_bitwise(struct ki2c *ki2c)
{ int val = 1; int ret = 0; int i;
/* disable I2C controller (MEN = 0) to get direct access to SCL/SDA */
iowrite8(0, ki2c->base + KI2C_CONTROL_REG);
/* generate clock cycles */
ki2c_set_scl(ki2c, val);
udelay(KI2C_RECOVERY_UDELAY); for (i = 0; i < KI2C_RECOVERY_CLK_CNT; i++) { if (val) { /* SCL shouldn't be low here */ if (!ki2c_get_scl(ki2c)) {
dev_err(&ki2c->auxdev->auxdev.dev, "SCL is stuck low!\n");
ret = -EBUSY; break;
}
/* break if SDA is high */ if (ki2c_get_sda(ki2c)) break;
}
val = !val;
ki2c_set_scl(ki2c, val);
udelay(KI2C_RECOVERY_UDELAY);
}
if (!ki2c_get_sda(ki2c)) {
dev_err(&ki2c->auxdev->auxdev.dev, "SDA is still low!\n");
ret = -EBUSY;
}
/* * Resetting bus bytewise is done by writing start bit, 9 data bits and stop * bit. * * This is not 100% safe. If target is an EEPROM and a write access was * interrupted during the ACK cycle, this approach might not be able to recover * the bus. The reason is, that after the 9 clock cycles the EEPROM will be in * ACK cycle again and will hold SDA low like it did before the start of the * routine. Furthermore the EEPROM might get written one additional byte with * 0xff into it. Thus, use bitwise approach whenever possible, especially when * EEPROMs are on the bus.
*/ staticint ki2c_reset_bus_bytewise(struct ki2c *ki2c)
{ int ret;
/* hold data line high for 9 clock cycles */
iowrite8(0xFF, ki2c->base + KI2C_DATA_REG);
staticint ki2c_reset_bus(struct ki2c *ki2c)
{ int ret;
ret = ki2c_inuse_lock(ki2c); if (ret < 0) return ret;
/* * If the I2C controller is capable of direct control of SCL/SDA, then a * bitwise reset is used. Otherwise fall back to bytewise reset.
*/ if (ki2c_has_capability(ki2c, KI2C_CAPABILITY_DC))
ret = ki2c_reset_bus_bitwise(ki2c); else
ret = ki2c_reset_bus_bytewise(ki2c);
ret = ki2c_wait_for_data_ack(ki2c); if (ret < 0) /* * For EEPROMs this is normal behavior during internal write * operation.
*/
dev_dbg(&ki2c->auxdev->auxdev.dev, "%s wait for ACK err at 0x%02x!\n", __func__, m->addr);
return ret;
}
staticint ki2c_repstart_addr(struct ki2c *ki2c, struct i2c_msg *m)
{ int ret;
/* repeated start and write is not supported */ if ((m->flags & I2C_M_RD) == 0) {
dev_err(&ki2c->auxdev->auxdev.dev, "Repeated start not supported for writes\n"); return -EINVAL;
}
staticint ki2c_write(struct ki2c *ki2c, const u8 *data, int len)
{ int ret; int i;
for (i = 0; i < len; i++) { /* write data byte */
iowrite8(data[i], ki2c->base + KI2C_DATA_REG);
ret = ki2c_wait_for_data_ack(ki2c); if (ret < 0) return ret;
}
return 0;
}
staticint ki2c_read(struct ki2c *ki2c, u8 *data, int len)
{
u8 control; int ret; int i;
if (len == 0) return 0; /* nothing to do */
control = KI2C_CONTROL_MEN | KI2C_CONTROL_MSTA;
/* if just one byte => send tx-nack after transfer */ if (len == 1)
control |= KI2C_CONTROL_TXAK;
iowrite8(control, ki2c->base + KI2C_CONTROL_REG);
/* dummy read to start transfer on bus */
ioread8(ki2c->base + KI2C_DATA_REG);
for (i = 0; i < len; i++) {
ret = ki2c_wait_for_data(ki2c); if (ret < 0) return ret;
if (i == len - 2) /* send tx-nack after transfer of last byte */
iowrite8(KI2C_CONTROL_MEN | KI2C_CONTROL_MSTA | KI2C_CONTROL_TXAK,
ki2c->base + KI2C_CONTROL_REG); elseif (i == len - 1) /* * switch to TX on last byte, so that reading DATA * register does not trigger another read transfer
*/
iowrite8(KI2C_CONTROL_MEN | KI2C_CONTROL_MSTA | KI2C_CONTROL_MTX,
ki2c->base + KI2C_CONTROL_REG);
/* read byte and start next transfer (if not last byte) */
data[i] = ioread8(ki2c->base + KI2C_DATA_REG);
}
return len;
}
staticint ki2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
{ struct ki2c *ki2c = i2c_get_adapdata(adap); int ret; int i;
ret = ki2c_inuse_lock(ki2c); if (ret < 0) return ret;
for (i = 0; i < num; i++) { struct i2c_msg *m = &msgs[i];
if (i == 0)
ret = ki2c_start_addr(ki2c, m); else
ret = ki2c_repstart_addr(ki2c, m); if (ret < 0) break;
if (m->flags & I2C_M_RD)
ret = ki2c_read(ki2c, m->buf, m->len); else
ret = ki2c_write(ki2c, m->buf, m->len); if (ret < 0) break;
}
ki2c_stop(ki2c);
ki2c_inuse_unlock(ki2c);
return ret < 0 ? ret : num;
}
staticvoid ki2c_unregister_devices(struct ki2c *ki2c)
{ int i;
for (i = 0; i < ki2c->client_size; i++)
i2c_unregister_device(ki2c->client[i]);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.