/* *SetupACKorNACKfornextreceivedbyteandwaitforittoarrive. * *Returnsafternextbytehasarrived.
*/ staticint pca_rx_ack(struct i2c_algo_pca_data *adap, int ack)
{ int sta = pca_get_con(adap);
sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_STA|I2C_PCA_CON_SI|I2C_PCA_CON_AA);
if (ack)
sta |= I2C_PCA_CON_AA;
pca_set_con(adap, sta); return pca_wait(adap);
}
staticint pca_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs, int num)
{ struct i2c_algo_pca_data *adap = i2c_adap->algo_data; struct i2c_msg *msg = NULL; int curmsg; int numbytes = 0; int state; int ret; int completed = 1; unsignedlong timeout = jiffies + i2c_adap->timeout;
while ((state = pca_status(adap)) != 0xf8) { if (time_before(jiffies, timeout)) {
msleep(10);
} else {
dev_dbg(&i2c_adap->dev, "bus is not idle. status is " "%#04x\n", state); return -EBUSY;
}
}
DEB1("{{{ XFER %d messages\n", num);
if (i2c_debug >= 2) { for (curmsg = 0; curmsg < num; curmsg++) { int addr, i;
msg = &msgs[curmsg];
curmsg = 0;
ret = -EIO; while (curmsg < num) {
state = pca_status(adap);
DEB3("STATE is 0x%02x\n", state);
msg = &msgs[curmsg];
switch (state) { case0xf8: /* On reset or stop the bus is idle */
completed = pca_start(adap); break;
case0x08: /* A START condition has been transmitted */ case0x10: /* A repeated start condition has been transmitted */
completed = pca_address(adap, msg); break;
case0x18: /* SLA+W has been transmitted; ACK has been received */ case0x28: /* Data byte in I2CDAT has been transmitted; ACK has been received */ if (numbytes < msg->len) {
completed = pca_tx_byte(adap,
msg->buf[numbytes]);
numbytes++; break;
}
curmsg++; numbytes = 0; if (curmsg == num)
pca_stop(adap); else
completed = pca_repeated_start(adap); break;
case0x20: /* SLA+W has been transmitted; NOT ACK has been received */
DEB2("NOT ACK received after SLA+W\n");
pca_stop(adap);
ret = -ENXIO; goto out;
case0x40: /* SLA+R has been transmitted; ACK has been received */
completed = pca_rx_ack(adap, msg->len > 1); break;
case0x50: /* Data bytes has been received; ACK has been returned */ if (numbytes < msg->len) {
pca_rx_byte(adap, &msg->buf[numbytes], 1);
numbytes++;
completed = pca_rx_ack(adap,
numbytes < msg->len - 1); break;
}
curmsg++; numbytes = 0; if (curmsg == num)
pca_stop(adap); else
completed = pca_repeated_start(adap); break;
case0x48: /* SLA+R has been transmitted; NOT ACK has been received */
DEB2("NOT ACK received after SLA+R\n");
pca_stop(adap);
ret = -ENXIO; goto out;
case0x30: /* Data byte in I2CDAT has been transmitted; NOT ACK has been received */
DEB2("NOT ACK received after data byte\n");
pca_stop(adap); goto out;
case0x38: /* Arbitration lost during SLA+W, SLA+R or data bytes */
DEB2("Arbitration lost\n"); /* *ThePCA9564datasheet(2006-09-01)says"A *STARTconditionwillbetransmittedwhenthe *busbecomesfree(STOPorSCLandSDAhigh)" *whentheSTAbitisset(p.11). * *Incasethiswon'twork,trypca_reset() *instead.
*/
pca_start(adap); goto out;
case0x58: /* Data byte has been received; NOT ACK has been returned */ if (numbytes == msg->len - 1) {
pca_rx_byte(adap, &msg->buf[numbytes], 0);
curmsg++; numbytes = 0; if (curmsg == num)
pca_stop(adap); else
completed = pca_repeated_start(adap);
} else {
DEB2("NOT ACK sent after data byte received. " "Not final byte. numbytes %d. len %d\n",
numbytes, msg->len);
pca_stop(adap); goto out;
} break; case0x70: /* Bus error - SDA stuck low */
DEB2("BUS ERROR - SDA Stuck low\n");
pca_reset(adap); goto out; case0x78: /* Bus error - SCL stuck low (PCA9665) */ case0x90: /* Bus error - SCL stuck low (PCA9564) */
DEB2("BUS ERROR - SCL Stuck low\n");
pca_reset(adap); goto out; case0x00: /* Bus error during master or slave mode due to illegal START or STOP condition */
DEB2("BUS ERROR - Illegal START or STOP\n");
pca_reset(adap); goto out; default:
dev_err(&i2c_adap->dev, "unhandled SIO state 0x%02x\n", state); break;
}
if (!completed) goto out;
}
ret = curmsg;
out:
DEB1("}}} transferred %d/%d messages. " "status is %#04x. control is %#04x\n",
curmsg, num, pca_status(adap),
pca_get_con(adap)); return ret;
}
if (pca_probe_chip(adap) == I2C_PCA_CHIP_9564) { staticint freqs[] = {330, 288, 217, 146, 88, 59, 44, 36}; int clock;
if (pca_data->i2c_clock > 7) { switch (pca_data->i2c_clock) { case330000:
pca_data->i2c_clock = I2C_PCA_CON_330kHz; break; case288000:
pca_data->i2c_clock = I2C_PCA_CON_288kHz; break; case217000:
pca_data->i2c_clock = I2C_PCA_CON_217kHz; break; case146000:
pca_data->i2c_clock = I2C_PCA_CON_146kHz; break; case88000:
pca_data->i2c_clock = I2C_PCA_CON_88kHz; break; case59000:
pca_data->i2c_clock = I2C_PCA_CON_59kHz; break; case44000:
pca_data->i2c_clock = I2C_PCA_CON_44kHz; break; case36000:
pca_data->i2c_clock = I2C_PCA_CON_36kHz; break; default:
printk(KERN_WARNING "%s: Invalid I2C clock speed selected." " Using default 59kHz.\n", adap->name);
pca_data->i2c_clock = I2C_PCA_CON_59kHz;
}
} else {
printk(KERN_WARNING "%s: " "Choosing the clock frequency based on " "index is deprecated." " Use the nominal frequency.\n", adap->name);
}
clock = pca_clock(pca_data);
printk(KERN_INFO "%s: Clock frequency is %dkHz\n",
adap->name, freqs[clock]);
/* Store settings as these will be needed when the PCA chip is reset */
pca_data->bus_settings.clock_freq = clock;
pca_reset(pca_data);
} else { int clock; int mode; int tlow, thi; /* Values can be found on PCA9665 datasheet section 7.3.2.6 */ int min_tlow, min_thi; /* These values are the maximum raise and fall values allowed *bytheI2Coperationmode(Standard,FastorFast+) *Theyareused(added)belowtocalculatetheclockdividers *ofPCA9665.Notethattheyareslightlydifferentofthe *realmaximum,toallowthechangeonmodeexactlyonthe *maximumclockrateforeachmode
*/ int raise_fall_time;
if (pca_data->i2c_clock > 1265800) {
printk(KERN_WARNING "%s: I2C clock speed too high." " Using 1265.8kHz.\n", adap->name);
pca_data->i2c_clock = 1265800;
}
if (pca_data->i2c_clock < 60300) {
printk(KERN_WARNING "%s: I2C clock speed too low." " Using 60.3kHz.\n", adap->name);
pca_data->i2c_clock = 60300;
}
/* To avoid integer overflow, use clock/100 for calculations */
clock = pca_clock(pca_data) / 100;
/* Store settings as these will be needed when the PCA chip is reset */
pca_data->bus_settings.mode = mode;
pca_data->bus_settings.tlow = tlow;
pca_data->bus_settings.thi = thi;
pca_reset(pca_data);
printk(KERN_INFO "%s: Clock frequency is %dHz\n", adap->name, clock * 100);
}
udelay(500); /* 500 us for oscillator to stabilise */
return0;
}
/* *registeringfunctionstoloadalgorithmsatruntime
*/ int i2c_pca_add_bus(struct i2c_adapter *adap)
{ int rval;
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.