Mike Frysinger | be853bf | 2008-10-06 04:16:47 -0400 | [diff] [blame] | 1 | /* |
Scott Jiang | fea9b69 | 2014-11-13 15:30:53 +0800 | [diff] [blame] | 2 | * i2c.c - driver for ADI TWI/I2C |
Mike Frysinger | be853bf | 2008-10-06 04:16:47 -0400 | [diff] [blame] | 3 | * |
Scott Jiang | fea9b69 | 2014-11-13 15:30:53 +0800 | [diff] [blame] | 4 | * Copyright (c) 2006-2014 Analog Devices Inc. |
Mike Frysinger | be853bf | 2008-10-06 04:16:47 -0400 | [diff] [blame] | 5 | * |
| 6 | * Licensed under the GPL-2 or later. |
| 7 | */ |
| 8 | |
| 9 | #include <common.h> |
| 10 | #include <i2c.h> |
| 11 | |
Sonic Zhang | d6a320d | 2014-01-28 13:53:34 +0800 | [diff] [blame] | 12 | #include <asm/clock.h> |
Scott Jiang | fea9b69 | 2014-11-13 15:30:53 +0800 | [diff] [blame] | 13 | #include <asm/twi.h> |
Scott Jiang | a6be70f | 2014-11-13 15:30:54 +0800 | [diff] [blame^] | 14 | #include <asm/io.h> |
Mike Frysinger | be853bf | 2008-10-06 04:16:47 -0400 | [diff] [blame] | 15 | |
Mike Frysinger | b5cebb4 | 2010-05-05 03:20:30 -0400 | [diff] [blame] | 16 | /* Every register is 32bit aligned, but only 16bits in size */ |
| 17 | #define ureg(name) u16 name; u16 __pad_##name; |
| 18 | struct twi_regs { |
| 19 | ureg(clkdiv); |
| 20 | ureg(control); |
| 21 | ureg(slave_ctl); |
| 22 | ureg(slave_stat); |
| 23 | ureg(slave_addr); |
| 24 | ureg(master_ctl); |
| 25 | ureg(master_stat); |
| 26 | ureg(master_addr); |
| 27 | ureg(int_stat); |
| 28 | ureg(int_mask); |
| 29 | ureg(fifo_ctl); |
| 30 | ureg(fifo_stat); |
| 31 | char __pad[0x50]; |
| 32 | ureg(xmt_data8); |
| 33 | ureg(xmt_data16); |
| 34 | ureg(rcv_data8); |
| 35 | ureg(rcv_data16); |
| 36 | }; |
| 37 | #undef ureg |
| 38 | |
| 39 | /* U-Boot I2C framework allows only one active device at a time. */ |
| 40 | #ifdef TWI_CLKDIV |
| 41 | #define TWI0_CLKDIV TWI_CLKDIV |
| 42 | #endif |
Scott Jiang | a6be70f | 2014-11-13 15:30:54 +0800 | [diff] [blame^] | 43 | static struct twi_regs *twi = (void *)TWI0_CLKDIV; |
Mike Frysinger | b5cebb4 | 2010-05-05 03:20:30 -0400 | [diff] [blame] | 44 | |
Mike Frysinger | be853bf | 2008-10-06 04:16:47 -0400 | [diff] [blame] | 45 | #ifdef DEBUG |
| 46 | # define dmemset(s, c, n) memset(s, c, n) |
| 47 | #else |
| 48 | # define dmemset(s, c, n) |
| 49 | #endif |
| 50 | #define debugi(fmt, args...) \ |
| 51 | debug( \ |
Mike Frysinger | b5cebb4 | 2010-05-05 03:20:30 -0400 | [diff] [blame] | 52 | "MSTAT:0x%03x FSTAT:0x%x ISTAT:0x%02x\t%-20s:%-3i: " fmt "\n", \ |
| 53 | twi->master_stat, twi->fifo_stat, twi->int_stat, \ |
Mike Frysinger | be853bf | 2008-10-06 04:16:47 -0400 | [diff] [blame] | 54 | __func__, __LINE__, ## args) |
| 55 | |
Mike Frysinger | be853bf | 2008-10-06 04:16:47 -0400 | [diff] [blame] | 56 | #ifdef CONFIG_TWICLK_KHZ |
| 57 | # error do not define CONFIG_TWICLK_KHZ ... use CONFIG_SYS_I2C_SPEED |
| 58 | #endif |
Mike Frysinger | 08a1c62 | 2009-10-14 19:27:27 -0400 | [diff] [blame] | 59 | |
| 60 | /* |
| 61 | * The way speed is changed into duty often results in integer truncation |
| 62 | * with 50% duty, so we'll force rounding up to the next duty by adding 1 |
| 63 | * to the max. In practice this will get us a speed of something like |
| 64 | * 385 KHz. The other limit is easy to handle as it is only 8 bits. |
| 65 | */ |
| 66 | #define I2C_SPEED_MAX 400000 |
| 67 | #define I2C_SPEED_TO_DUTY(speed) (5000000 / (speed)) |
| 68 | #define I2C_DUTY_MAX (I2C_SPEED_TO_DUTY(I2C_SPEED_MAX) + 1) |
| 69 | #define I2C_DUTY_MIN 0xff /* 8 bit limited */ |
| 70 | #define SYS_I2C_DUTY I2C_SPEED_TO_DUTY(CONFIG_SYS_I2C_SPEED) |
| 71 | /* Note: duty is inverse of speed, so the comparisons below are correct */ |
| 72 | #if SYS_I2C_DUTY < I2C_DUTY_MAX || SYS_I2C_DUTY > I2C_DUTY_MIN |
| 73 | # error "The Blackfin I2C hardware can only operate 20KHz - 400KHz" |
Mike Frysinger | be853bf | 2008-10-06 04:16:47 -0400 | [diff] [blame] | 74 | #endif |
| 75 | |
| 76 | /* All transfers are described by this data structure */ |
| 77 | struct i2c_msg { |
| 78 | u8 flags; |
| 79 | #define I2C_M_COMBO 0x4 |
| 80 | #define I2C_M_STOP 0x2 |
| 81 | #define I2C_M_READ 0x1 |
| 82 | int len; /* msg length */ |
| 83 | u8 *buf; /* pointer to msg data */ |
| 84 | int alen; /* addr length */ |
| 85 | u8 *abuf; /* addr buffer */ |
| 86 | }; |
| 87 | |
Mike Frysinger | 3814ea4 | 2009-10-14 19:27:26 -0400 | [diff] [blame] | 88 | /* Allow msec timeout per ~byte transfer */ |
| 89 | #define I2C_TIMEOUT 10 |
| 90 | |
Mike Frysinger | be853bf | 2008-10-06 04:16:47 -0400 | [diff] [blame] | 91 | /** |
| 92 | * wait_for_completion - manage the actual i2c transfer |
| 93 | * @msg: the i2c msg |
| 94 | */ |
| 95 | static int wait_for_completion(struct i2c_msg *msg) |
| 96 | { |
Scott Jiang | a6be70f | 2014-11-13 15:30:54 +0800 | [diff] [blame^] | 97 | u16 int_stat, ctl; |
Mike Frysinger | 3814ea4 | 2009-10-14 19:27:26 -0400 | [diff] [blame] | 98 | ulong timebase = get_timer(0); |
Mike Frysinger | be853bf | 2008-10-06 04:16:47 -0400 | [diff] [blame] | 99 | |
Mike Frysinger | 3814ea4 | 2009-10-14 19:27:26 -0400 | [diff] [blame] | 100 | do { |
Scott Jiang | a6be70f | 2014-11-13 15:30:54 +0800 | [diff] [blame^] | 101 | int_stat = readw(&twi->int_stat); |
Mike Frysinger | be853bf | 2008-10-06 04:16:47 -0400 | [diff] [blame] | 102 | |
| 103 | if (int_stat & XMTSERV) { |
| 104 | debugi("processing XMTSERV"); |
Scott Jiang | a6be70f | 2014-11-13 15:30:54 +0800 | [diff] [blame^] | 105 | writew(XMTSERV, &twi->int_stat); |
Mike Frysinger | be853bf | 2008-10-06 04:16:47 -0400 | [diff] [blame] | 106 | if (msg->alen) { |
Scott Jiang | a6be70f | 2014-11-13 15:30:54 +0800 | [diff] [blame^] | 107 | writew(*(msg->abuf++), &twi->xmt_data8); |
Mike Frysinger | be853bf | 2008-10-06 04:16:47 -0400 | [diff] [blame] | 108 | --msg->alen; |
| 109 | } else if (!(msg->flags & I2C_M_COMBO) && msg->len) { |
Scott Jiang | a6be70f | 2014-11-13 15:30:54 +0800 | [diff] [blame^] | 110 | writew(*(msg->buf++), &twi->xmt_data8); |
Mike Frysinger | be853bf | 2008-10-06 04:16:47 -0400 | [diff] [blame] | 111 | --msg->len; |
| 112 | } else { |
Scott Jiang | a6be70f | 2014-11-13 15:30:54 +0800 | [diff] [blame^] | 113 | ctl = readw(&twi->master_ctl); |
| 114 | if (msg->flags & I2C_M_COMBO) |
| 115 | writew(ctl | RSTART | MDIR, |
| 116 | &twi->master_ctl); |
| 117 | else |
| 118 | writew(ctl | STOP, &twi->master_ctl); |
Mike Frysinger | be853bf | 2008-10-06 04:16:47 -0400 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | if (int_stat & RCVSERV) { |
| 122 | debugi("processing RCVSERV"); |
Scott Jiang | a6be70f | 2014-11-13 15:30:54 +0800 | [diff] [blame^] | 123 | writew(RCVSERV, &twi->int_stat); |
Mike Frysinger | be853bf | 2008-10-06 04:16:47 -0400 | [diff] [blame] | 124 | if (msg->len) { |
Scott Jiang | a6be70f | 2014-11-13 15:30:54 +0800 | [diff] [blame^] | 125 | *(msg->buf++) = readw(&twi->rcv_data8); |
Mike Frysinger | be853bf | 2008-10-06 04:16:47 -0400 | [diff] [blame] | 126 | --msg->len; |
| 127 | } else if (msg->flags & I2C_M_STOP) { |
Scott Jiang | a6be70f | 2014-11-13 15:30:54 +0800 | [diff] [blame^] | 128 | ctl = readw(&twi->master_ctl); |
| 129 | writew(ctl | STOP, &twi->master_ctl); |
Mike Frysinger | be853bf | 2008-10-06 04:16:47 -0400 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | if (int_stat & MERR) { |
| 133 | debugi("processing MERR"); |
Scott Jiang | a6be70f | 2014-11-13 15:30:54 +0800 | [diff] [blame^] | 134 | writew(MERR, &twi->int_stat); |
Mike Frysinger | 3814ea4 | 2009-10-14 19:27:26 -0400 | [diff] [blame] | 135 | return msg->len; |
Mike Frysinger | be853bf | 2008-10-06 04:16:47 -0400 | [diff] [blame] | 136 | } |
| 137 | if (int_stat & MCOMP) { |
| 138 | debugi("processing MCOMP"); |
Scott Jiang | a6be70f | 2014-11-13 15:30:54 +0800 | [diff] [blame^] | 139 | writew(MCOMP, &twi->int_stat); |
Mike Frysinger | be853bf | 2008-10-06 04:16:47 -0400 | [diff] [blame] | 140 | if (msg->flags & I2C_M_COMBO && msg->len) { |
Scott Jiang | a6be70f | 2014-11-13 15:30:54 +0800 | [diff] [blame^] | 141 | ctl = readw(&twi->master_ctl); |
| 142 | ctl = (ctl & ~RSTART) | |
Mike Frysinger | b5cebb4 | 2010-05-05 03:20:30 -0400 | [diff] [blame] | 143 | (min(msg->len, 0xff) << 6) | MEN | MDIR; |
Scott Jiang | a6be70f | 2014-11-13 15:30:54 +0800 | [diff] [blame^] | 144 | writew(ctl, &twi->master_ctl); |
Mike Frysinger | be853bf | 2008-10-06 04:16:47 -0400 | [diff] [blame] | 145 | } else |
| 146 | break; |
| 147 | } |
Mike Frysinger | 3814ea4 | 2009-10-14 19:27:26 -0400 | [diff] [blame] | 148 | |
| 149 | /* If we were able to do something, reset timeout */ |
| 150 | if (int_stat) |
| 151 | timebase = get_timer(0); |
| 152 | |
| 153 | } while (get_timer(timebase) < I2C_TIMEOUT); |
Mike Frysinger | be853bf | 2008-10-06 04:16:47 -0400 | [diff] [blame] | 154 | |
| 155 | return msg->len; |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * i2c_transfer - setup an i2c transfer |
| 160 | * @return: 0 if things worked, non-0 if things failed |
| 161 | * |
| 162 | * Here we just get the i2c stuff all prepped and ready, and then tail off |
| 163 | * into wait_for_completion() for all the bits to go. |
| 164 | */ |
Scott Jiang | a6be70f | 2014-11-13 15:30:54 +0800 | [diff] [blame^] | 165 | static int i2c_transfer(uchar chip, uint addr, int alen, uchar *buffer, |
| 166 | int len, u8 flags) |
Mike Frysinger | be853bf | 2008-10-06 04:16:47 -0400 | [diff] [blame] | 167 | { |
Scott Jiang | a6be70f | 2014-11-13 15:30:54 +0800 | [diff] [blame^] | 168 | int ret; |
| 169 | u16 ctl; |
Mike Frysinger | be853bf | 2008-10-06 04:16:47 -0400 | [diff] [blame] | 170 | uchar addr_buffer[] = { |
| 171 | (addr >> 0), |
| 172 | (addr >> 8), |
| 173 | (addr >> 16), |
| 174 | }; |
| 175 | struct i2c_msg msg = { |
| 176 | .flags = flags | (len >= 0xff ? I2C_M_STOP : 0), |
| 177 | .buf = buffer, |
| 178 | .len = len, |
| 179 | .abuf = addr_buffer, |
| 180 | .alen = alen, |
| 181 | }; |
Mike Frysinger | be853bf | 2008-10-06 04:16:47 -0400 | [diff] [blame] | 182 | |
| 183 | dmemset(buffer, 0xff, len); |
Scott Jiang | a6be70f | 2014-11-13 15:30:54 +0800 | [diff] [blame^] | 184 | debugi("chip=0x%x addr=0x%02x alen=%i buf[0]=0x%02x len=%i ", |
| 185 | chip, addr, alen, buffer[0], len); |
| 186 | debugi("flags=0x%02x[%s] ", flags, |
| 187 | (flags & I2C_M_READ ? "rd" : "wr")); |
Mike Frysinger | be853bf | 2008-10-06 04:16:47 -0400 | [diff] [blame] | 188 | |
| 189 | /* wait for things to settle */ |
Scott Jiang | a6be70f | 2014-11-13 15:30:54 +0800 | [diff] [blame^] | 190 | while (readw(&twi->master_stat) & BUSBUSY) |
Mike Frysinger | be853bf | 2008-10-06 04:16:47 -0400 | [diff] [blame] | 191 | if (ctrlc()) |
| 192 | return 1; |
| 193 | |
| 194 | /* Set Transmit device address */ |
Scott Jiang | a6be70f | 2014-11-13 15:30:54 +0800 | [diff] [blame^] | 195 | writew(chip, &twi->master_addr); |
Mike Frysinger | be853bf | 2008-10-06 04:16:47 -0400 | [diff] [blame] | 196 | |
| 197 | /* Clear the FIFO before starting things */ |
Scott Jiang | a6be70f | 2014-11-13 15:30:54 +0800 | [diff] [blame^] | 198 | writew(XMTFLUSH | RCVFLUSH, &twi->fifo_ctl); |
| 199 | writew(0, &twi->fifo_ctl); |
Mike Frysinger | be853bf | 2008-10-06 04:16:47 -0400 | [diff] [blame] | 200 | |
| 201 | /* prime the pump */ |
| 202 | if (msg.alen) { |
Peter Meerwald | 98ab14e | 2009-06-29 15:48:33 -0400 | [diff] [blame] | 203 | len = (msg.flags & I2C_M_COMBO) ? msg.alen : msg.alen + len; |
Mike Frysinger | be853bf | 2008-10-06 04:16:47 -0400 | [diff] [blame] | 204 | debugi("first byte=0x%02x", *msg.abuf); |
Scott Jiang | a6be70f | 2014-11-13 15:30:54 +0800 | [diff] [blame^] | 205 | writew(*(msg.abuf++), &twi->xmt_data8); |
Mike Frysinger | be853bf | 2008-10-06 04:16:47 -0400 | [diff] [blame] | 206 | --msg.alen; |
| 207 | } else if (!(msg.flags & I2C_M_READ) && msg.len) { |
| 208 | debugi("first byte=0x%02x", *msg.buf); |
Scott Jiang | a6be70f | 2014-11-13 15:30:54 +0800 | [diff] [blame^] | 209 | writew(*(msg.buf++), &twi->xmt_data8); |
Mike Frysinger | be853bf | 2008-10-06 04:16:47 -0400 | [diff] [blame] | 210 | --msg.len; |
| 211 | } |
| 212 | |
| 213 | /* clear int stat */ |
Scott Jiang | a6be70f | 2014-11-13 15:30:54 +0800 | [diff] [blame^] | 214 | writew(-1, &twi->master_stat); |
| 215 | writew(-1, &twi->int_stat); |
| 216 | writew(0, &twi->int_mask); |
Mike Frysinger | be853bf | 2008-10-06 04:16:47 -0400 | [diff] [blame] | 217 | |
| 218 | /* Master enable */ |
Scott Jiang | a6be70f | 2014-11-13 15:30:54 +0800 | [diff] [blame^] | 219 | ctl = readw(&twi->master_ctl); |
| 220 | ctl = (ctl & FAST) | (min(len, 0xff) << 6) | MEN | |
| 221 | ((msg.flags & I2C_M_READ) ? MDIR : 0); |
| 222 | writew(ctl, &twi->master_ctl); |
Mike Frysinger | be853bf | 2008-10-06 04:16:47 -0400 | [diff] [blame] | 223 | |
| 224 | /* process the rest */ |
| 225 | ret = wait_for_completion(&msg); |
| 226 | debugi("ret=%d", ret); |
| 227 | |
| 228 | if (ret) { |
Scott Jiang | a6be70f | 2014-11-13 15:30:54 +0800 | [diff] [blame^] | 229 | ctl = readw(&twi->master_ctl) & ~MEN; |
| 230 | writew(ctl, &twi->master_ctl); |
| 231 | ctl = readw(&twi->control) & ~TWI_ENA; |
| 232 | writew(ctl, &twi->control); |
| 233 | ctl = readw(&twi->control) | TWI_ENA; |
| 234 | writew(ctl, &twi->control); |
Mike Frysinger | be853bf | 2008-10-06 04:16:47 -0400 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | return ret; |
| 238 | } |
| 239 | |
Mike Frysinger | 08a1c62 | 2009-10-14 19:27:27 -0400 | [diff] [blame] | 240 | /** |
| 241 | * i2c_set_bus_speed - set i2c bus speed |
| 242 | * @speed: bus speed (in HZ) |
| 243 | */ |
| 244 | int i2c_set_bus_speed(unsigned int speed) |
| 245 | { |
| 246 | u16 clkdiv = I2C_SPEED_TO_DUTY(speed); |
| 247 | |
| 248 | /* Set TWI interface clock */ |
| 249 | if (clkdiv < I2C_DUTY_MAX || clkdiv > I2C_DUTY_MIN) |
| 250 | return -1; |
Scott Jiang | a6be70f | 2014-11-13 15:30:54 +0800 | [diff] [blame^] | 251 | clkdiv = (clkdiv << 8) | (clkdiv & 0xff); |
| 252 | writew(clkdiv, &twi->clkdiv); |
Mike Frysinger | 08a1c62 | 2009-10-14 19:27:27 -0400 | [diff] [blame] | 253 | |
| 254 | /* Don't turn it on */ |
Scott Jiang | a6be70f | 2014-11-13 15:30:54 +0800 | [diff] [blame^] | 255 | writew(speed > 100000 ? FAST : 0, &twi->master_ctl); |
Mike Frysinger | 08a1c62 | 2009-10-14 19:27:27 -0400 | [diff] [blame] | 256 | |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * i2c_get_bus_speed - get i2c bus speed |
| 262 | * @speed: bus speed (in HZ) |
| 263 | */ |
| 264 | unsigned int i2c_get_bus_speed(void) |
| 265 | { |
Scott Jiang | a6be70f | 2014-11-13 15:30:54 +0800 | [diff] [blame^] | 266 | u16 clkdiv = readw(&twi->clkdiv) & 0xff; |
Mike Frysinger | 08a1c62 | 2009-10-14 19:27:27 -0400 | [diff] [blame] | 267 | /* 10 MHz / (2 * CLKDIV) -> 5 MHz / CLKDIV */ |
Scott Jiang | a6be70f | 2014-11-13 15:30:54 +0800 | [diff] [blame^] | 268 | return 5000000 / clkdiv; |
Mike Frysinger | 08a1c62 | 2009-10-14 19:27:27 -0400 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | /** |
Mike Frysinger | be853bf | 2008-10-06 04:16:47 -0400 | [diff] [blame] | 272 | * i2c_init - initialize the i2c bus |
| 273 | * @speed: bus speed (in HZ) |
| 274 | * @slaveaddr: address of device in slave mode (0 - not slave) |
| 275 | * |
| 276 | * Slave mode isn't actually implemented. It'll stay that way until |
| 277 | * we get a real request for it. |
| 278 | */ |
| 279 | void i2c_init(int speed, int slaveaddr) |
| 280 | { |
Sonic Zhang | d6a320d | 2014-01-28 13:53:34 +0800 | [diff] [blame] | 281 | uint8_t prescale = ((get_i2c_clk() / 1000 / 1000 + 5) / 10) & 0x7F; |
Mike Frysinger | be853bf | 2008-10-06 04:16:47 -0400 | [diff] [blame] | 282 | |
| 283 | /* Set TWI internal clock as 10MHz */ |
Scott Jiang | a6be70f | 2014-11-13 15:30:54 +0800 | [diff] [blame^] | 284 | writew(prescale, &twi->control); |
Mike Frysinger | be853bf | 2008-10-06 04:16:47 -0400 | [diff] [blame] | 285 | |
| 286 | /* Set TWI interface clock as specified */ |
Mike Frysinger | 08a1c62 | 2009-10-14 19:27:27 -0400 | [diff] [blame] | 287 | i2c_set_bus_speed(speed); |
Mike Frysinger | be853bf | 2008-10-06 04:16:47 -0400 | [diff] [blame] | 288 | |
Mike Frysinger | 08a1c62 | 2009-10-14 19:27:27 -0400 | [diff] [blame] | 289 | /* Enable it */ |
Scott Jiang | a6be70f | 2014-11-13 15:30:54 +0800 | [diff] [blame^] | 290 | writew(TWI_ENA | prescale, &twi->control); |
Mike Frysinger | be853bf | 2008-10-06 04:16:47 -0400 | [diff] [blame] | 291 | |
Scott Jiang | a6be70f | 2014-11-13 15:30:54 +0800 | [diff] [blame^] | 292 | debugi("CONTROL:0x%04x CLKDIV:0x%04x", readw(&twi->control), |
| 293 | readw(&twi->clkdiv)); |
Mike Frysinger | be853bf | 2008-10-06 04:16:47 -0400 | [diff] [blame] | 294 | |
| 295 | #if CONFIG_SYS_I2C_SLAVE |
| 296 | # error I2C slave support not tested/supported |
Mike Frysinger | be853bf | 2008-10-06 04:16:47 -0400 | [diff] [blame] | 297 | #endif |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * i2c_probe - test if a chip exists at a given i2c address |
| 302 | * @chip: i2c chip addr to search for |
| 303 | * @return: 0 if found, non-0 if not found |
| 304 | */ |
| 305 | int i2c_probe(uchar chip) |
| 306 | { |
| 307 | u8 byte; |
| 308 | return i2c_read(chip, 0, 0, &byte, 1); |
| 309 | } |
| 310 | |
| 311 | /** |
| 312 | * i2c_read - read data from an i2c device |
| 313 | * @chip: i2c chip addr |
| 314 | * @addr: memory (register) address in the chip |
| 315 | * @alen: byte size of address |
| 316 | * @buffer: buffer to store data read from chip |
| 317 | * @len: how many bytes to read |
| 318 | * @return: 0 on success, non-0 on failure |
| 319 | */ |
| 320 | int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len) |
| 321 | { |
Scott Jiang | a6be70f | 2014-11-13 15:30:54 +0800 | [diff] [blame^] | 322 | return i2c_transfer(chip, addr, alen, buffer, |
| 323 | len, (alen ? I2C_M_COMBO : I2C_M_READ)); |
Mike Frysinger | be853bf | 2008-10-06 04:16:47 -0400 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | /** |
| 327 | * i2c_write - write data to an i2c device |
| 328 | * @chip: i2c chip addr |
| 329 | * @addr: memory (register) address in the chip |
| 330 | * @alen: byte size of address |
Peter Meerwald | 98ab14e | 2009-06-29 15:48:33 -0400 | [diff] [blame] | 331 | * @buffer: buffer holding data to write to chip |
Mike Frysinger | be853bf | 2008-10-06 04:16:47 -0400 | [diff] [blame] | 332 | * @len: how many bytes to write |
| 333 | * @return: 0 on success, non-0 on failure |
| 334 | */ |
| 335 | int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len) |
| 336 | { |
| 337 | return i2c_transfer(chip, addr, alen, buffer, len, 0); |
| 338 | } |
Mike Frysinger | b5cebb4 | 2010-05-05 03:20:30 -0400 | [diff] [blame] | 339 | |
| 340 | /** |
| 341 | * i2c_set_bus_num - change active I2C bus |
| 342 | * @bus: bus index, zero based |
| 343 | * @returns: 0 on success, non-0 on failure |
| 344 | */ |
| 345 | int i2c_set_bus_num(unsigned int bus) |
| 346 | { |
| 347 | switch (bus) { |
| 348 | #if CONFIG_SYS_MAX_I2C_BUS > 0 |
Scott Jiang | a6be70f | 2014-11-13 15:30:54 +0800 | [diff] [blame^] | 349 | case 0: |
| 350 | twi = (void *)TWI0_CLKDIV; |
| 351 | return 0; |
Mike Frysinger | b5cebb4 | 2010-05-05 03:20:30 -0400 | [diff] [blame] | 352 | #endif |
| 353 | #if CONFIG_SYS_MAX_I2C_BUS > 1 |
Scott Jiang | a6be70f | 2014-11-13 15:30:54 +0800 | [diff] [blame^] | 354 | case 1: |
| 355 | twi = (void *)TWI1_CLKDIV; |
| 356 | return 0; |
Mike Frysinger | b5cebb4 | 2010-05-05 03:20:30 -0400 | [diff] [blame] | 357 | #endif |
| 358 | #if CONFIG_SYS_MAX_I2C_BUS > 2 |
Scott Jiang | a6be70f | 2014-11-13 15:30:54 +0800 | [diff] [blame^] | 359 | case 2: |
| 360 | twi = (void *)TWI2_CLKDIV; |
| 361 | return 0; |
Mike Frysinger | b5cebb4 | 2010-05-05 03:20:30 -0400 | [diff] [blame] | 362 | #endif |
Scott Jiang | a6be70f | 2014-11-13 15:30:54 +0800 | [diff] [blame^] | 363 | default: return -1; |
Mike Frysinger | b5cebb4 | 2010-05-05 03:20:30 -0400 | [diff] [blame] | 364 | } |
| 365 | } |
| 366 | |
| 367 | /** |
| 368 | * i2c_get_bus_num - returns index of active I2C bus |
| 369 | */ |
| 370 | unsigned int i2c_get_bus_num(void) |
| 371 | { |
| 372 | switch ((unsigned long)twi) { |
| 373 | #if CONFIG_SYS_MAX_I2C_BUS > 0 |
Scott Jiang | a6be70f | 2014-11-13 15:30:54 +0800 | [diff] [blame^] | 374 | case TWI0_CLKDIV: |
| 375 | return 0; |
Mike Frysinger | b5cebb4 | 2010-05-05 03:20:30 -0400 | [diff] [blame] | 376 | #endif |
| 377 | #if CONFIG_SYS_MAX_I2C_BUS > 1 |
Scott Jiang | a6be70f | 2014-11-13 15:30:54 +0800 | [diff] [blame^] | 378 | case TWI1_CLKDIV: |
| 379 | return 1; |
Mike Frysinger | b5cebb4 | 2010-05-05 03:20:30 -0400 | [diff] [blame] | 380 | #endif |
| 381 | #if CONFIG_SYS_MAX_I2C_BUS > 2 |
Scott Jiang | a6be70f | 2014-11-13 15:30:54 +0800 | [diff] [blame^] | 382 | case TWI2_CLKDIV: |
| 383 | return 2; |
Mike Frysinger | b5cebb4 | 2010-05-05 03:20:30 -0400 | [diff] [blame] | 384 | #endif |
Scott Jiang | a6be70f | 2014-11-13 15:30:54 +0800 | [diff] [blame^] | 385 | default: return -1; |
Mike Frysinger | b5cebb4 | 2010-05-05 03:20:30 -0400 | [diff] [blame] | 386 | } |
| 387 | } |