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