/* * I2C bus protocol basic routines */ static int i2c_put_data(unsigned char data) { unsigned int timeout = 1000; __i2c_write(data); __i2c_set_drf(); timeout = 1000; while (__i2c_check_drf() != 0 && timeout)timeout--; if (timeout) ;//return 0; else return -ETIMEDOUT; timeout = 1000; while (!__i2c_transmit_ended() && timeout)timeout--; if (timeout) ;//return 0; else return -ETIMEDOUT; timeout = 1000; while (!__i2c_received_ack() && timeout) timeout--; if (timeout) return 0; else return -ETIMEDOUT; }
int early_i2c_read (u8 dev, uint addr, int alen, u8 * data, int length) { int i = 0; u8 *a = (u8 *) & addr; /* wait while i2c bus is busy */ if (i2c_wait4bus () < 0) goto exit; /* transmite the slave_addr+read/write phase */ if (i2c_write_addr (dev, I2C_WRITE, 0) == 0) goto exit; /* select the i2c slave internal regs phase */ /* because u32 -> u8, and big endian, so if alen is 1, write a[3] */ if (__i2c_write (&a[4 - alen], alen) != alen) goto exit; /* dummy read phase */ if (i2c_write_addr (dev, I2C_READ, 1) == 0) goto exit; /* transmite data phase */ i = __i2c_read (data, length); exit: writel (M83xx_CCR_MEN, I2CCCR); return !(i == length); }
static int i2c_put_data_nack(unsigned char data) { unsigned int timeout = TIMEOUT*10; __i2c_write(data); __i2c_set_drf(); while (__i2c_check_drf() != 0); while (!__i2c_transmit_ended()); while (timeout--); return 0; }
int i2c_write (u8 dev, uint addr, int alen, u8 *data, int length) { int i = 0; u8 *a = (u8*)&addr; if (i2c_wait4bus () < 0) goto exit; if (i2c_write_addr (dev, I2C_WRITE, 0) == 0) goto exit; if (__i2c_write (&a[4 - alen], alen) != alen) goto exit; i = __i2c_write (data, length); exit: writeb(I2C_CR_MEN, &I2C->cr); return !(i == length); }
int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len) { int rc; struct u5500_i2c_regs *i2c_regs; i2c_regs = i2c_dev[i2c_bus_num]; rc = __i2c_write(i2c_regs, chip, addr, alen, buffer, len); if (rc != 0) { debug("I2C write: I/O error\n"); i2c_abort(i2c_regs); return rc; } return 0; }
/* * I2C bus protocol basic routines */ static int i2c_put_data(unsigned char data) { unsigned int timeout = TIMEOUT*10; __i2c_write(data); __i2c_set_drf(); while (__i2c_check_drf() != 0); while (!__i2c_transmit_ended()); while (!__i2c_received_ack() && timeout) timeout--; if (timeout) return 0; else return -1; }
static int i2c_jz_xfer(struct i2c_adapter *adap, struct i2c_msg *pmsg, int num) { int ret, i; // printk(KERN_INFO "i2c_jz_xfer %d messages\n", num); dev_dbg(&adap->dev, "jz47xx_xfer: processing %d messages:\n", num); for (i = 0; i < num; i++, pmsg++) { unsigned char *tmpbuf = pmsg->buf; int cnt = (pmsg->flags & I2C_M_TEN) ? -2:-1; // prepare for sending address; ret = num; // assume ok dev_dbg(&adap->dev, " #%d: %s %d byte%s %s 0x%02x flags %04x\n", i, pmsg->flags & I2C_M_RD ? "reading" : "writing", pmsg->len, pmsg->len > 1 ? "s" : "", pmsg->flags & I2C_M_RD ? "from" : "to", pmsg->addr, pmsg->flags); if (pmsg->flags & (/*I2C_M_TEN|I2C_M_NOSTART|*/I2C_M_REV_DIR_ADDR|/*I2C_M_IGNORE_NAK|*/I2C_M_NO_RD_ACK|I2C_M_RECV_LEN)) { dev_dbg(&adap->dev, "jz47xx_xfer: flags=%04x not supported\n", pmsg->flags); return -EINVAL; } if (!pmsg->buf) continue; /* sanity check */ // printk(KERN_INFO "addr=%x flags=%04x\n", pmsg->addr, pmsg->flags); if (!(pmsg->flags & I2C_M_NOSTART)) __i2c_send_start(); __i2c_send_ack(); // default (only last byte during receive gets nack) for(; cnt < pmsg->len; cnt++) { // printk(KERN_INFO "%d (%d)", cnt, pmsg->len); #if 0 if(cnt == pmsg->len-1 && i == num-1) { printk(" send stop\n"); __i2c_send_stop(); // last byte of last message } #endif if (cnt >= 0 && pmsg->flags & I2C_M_RD) { // read data int timeout = TIMEOUT; while(!__i2c_check_drf() && timeout--) // wait for data to arrive udelay(10); if (timeout < 0) ret = -ETIMEDOUT; else { if(cnt == pmsg->len-2) __i2c_send_nack(); // nack last byte *tmpbuf++ = __i2c_read(); // read data byte // printk(" r: %02x\n", tmpbuf[-1]); __i2c_clear_drf(); } } else { int timeout = TIMEOUT; if(cnt == -2) { // send first byte of 10-bit address // printk(" w: %02x\n", ((pmsg->addr >> 7) & 0x06) | ((pmsg->flags & I2C_M_RD) ? (0xf0 | I2C_READ) : (0xf0 | I2C_WRITE))); __i2c_write(((pmsg->addr >> 7) & 0x06) | ((pmsg->flags & I2C_M_RD) ? (0xf0 | I2C_READ) : (0xf0 | I2C_WRITE)) ); // first 2 bits } else if(cnt == -1) { // send 7 bit address or second byte if ((pmsg->flags & I2C_M_TEN)) { // printk(" w: %02x\n", pmsg->addr & 0xff); __i2c_write(pmsg->addr); // final 8 bits } else { // printk(" w: %02x\n", (pmsg->addr << 1) | ((pmsg->flags & I2C_M_RD) ? I2C_READ : I2C_WRITE)); __i2c_write((pmsg->addr << 1) | ((pmsg->flags & I2C_M_RD) ? I2C_READ : I2C_WRITE)); // set data to be written } } else { // send data // printk(" w: %02x\n", *tmpbuf); __i2c_write(*tmpbuf++); } __i2c_set_drf(); // data is ready flag while(__i2c_check_drf() && timeout--) // wait until we can push the next byte udelay(10); if(cnt == -1 || cnt == pmsg->len-1) { // last byte, wait for end of transission and final ACK // printk(" wait transmit_ended\n"); timeout = TIMEOUT; while(!__i2c_transmit_ended() && timeout--) udelay(10); } if (timeout < 0) ret = -ETIMEDOUT; else if (!(pmsg->flags & I2C_M_IGNORE_NAK) && !__i2c_received_ack()) ret = -EIO; } if(ret < 0) break; }