wdenk | 1f04521 | 2002-03-10 14:37:15 +0000 | [diff] [blame] | 1 | /* |
Heiko Schocher | e7d9c4f | 2012-01-16 21:12:23 +0000 | [diff] [blame] | 2 | * Copyright (C) 2009 Sergey Kubushyn <ksi@koi8.net> |
| 3 | * Copyright (C) 2009 - 2013 Heiko Schocher <hs@denx.de> |
| 4 | * Changes for multibus/multiadapter I2C support. |
| 5 | * |
wdenk | 1f04521 | 2002-03-10 14:37:15 +0000 | [diff] [blame] | 6 | * (C) Copyright 2001 |
| 7 | * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com. |
| 8 | * |
| 9 | * See file CREDITS for list of people who contributed to this |
| 10 | * project. |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU General Public License as |
| 14 | * published by the Free Software Foundation; either version 2 of |
| 15 | * the License, or (at your option) any later version. |
| 16 | * |
| 17 | * This program is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | * GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License |
| 23 | * along with this program; if not, write to the Free Software |
| 24 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 25 | * MA 02111-1307 USA |
| 26 | * |
| 27 | * The original I2C interface was |
| 28 | * (C) 2000 by Paolo Scaffardi (arsenio@tin.it) |
| 29 | * AIRVENT SAM s.p.a - RIMINI(ITALY) |
| 30 | * but has been changed substantially. |
| 31 | */ |
| 32 | |
| 33 | #ifndef _I2C_H_ |
| 34 | #define _I2C_H_ |
| 35 | |
| 36 | /* |
| 37 | * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING |
| 38 | * |
| 39 | * The implementation MUST NOT use static or global variables if the |
| 40 | * I2C routines are used to read SDRAM configuration information |
| 41 | * because this is done before the memories are initialized. Limited |
| 42 | * use of stack-based variables are OK (the initial stack size is |
| 43 | * limited). |
| 44 | * |
| 45 | * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING |
| 46 | */ |
| 47 | |
| 48 | /* |
| 49 | * Configuration items. |
| 50 | */ |
| 51 | #define I2C_RXTX_LEN 128 /* maximum tx/rx buffer length */ |
| 52 | |
Heiko Schocher | e7d9c4f | 2012-01-16 21:12:23 +0000 | [diff] [blame] | 53 | #if !defined(CONFIG_SYS_I2C_MAX_HOPS) |
| 54 | /* no muxes used bus = i2c adapters */ |
| 55 | #define CONFIG_SYS_I2C_DIRECT_BUS 1 |
| 56 | #define CONFIG_SYS_I2C_MAX_HOPS 0 |
| 57 | #define CONFIG_SYS_NUM_I2C_BUSES ll_entry_count(struct i2c_adapter, i2c) |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 58 | #else |
Heiko Schocher | e7d9c4f | 2012-01-16 21:12:23 +0000 | [diff] [blame] | 59 | /* we use i2c muxes */ |
| 60 | #undef CONFIG_SYS_I2C_DIRECT_BUS |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 61 | #endif |
| 62 | |
Stefan Roese | 9de4fcb | 2007-03-01 07:03:25 +0100 | [diff] [blame] | 63 | /* define the I2C bus number for RTC and DTT if not already done */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 64 | #if !defined(CONFIG_SYS_RTC_BUS_NUM) |
| 65 | #define CONFIG_SYS_RTC_BUS_NUM 0 |
Stefan Roese | 9de4fcb | 2007-03-01 07:03:25 +0100 | [diff] [blame] | 66 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 67 | #if !defined(CONFIG_SYS_DTT_BUS_NUM) |
| 68 | #define CONFIG_SYS_DTT_BUS_NUM 0 |
Stefan Roese | 9de4fcb | 2007-03-01 07:03:25 +0100 | [diff] [blame] | 69 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 70 | #if !defined(CONFIG_SYS_SPD_BUS_NUM) |
| 71 | #define CONFIG_SYS_SPD_BUS_NUM 0 |
Matthias Fuchs | 66067c3 | 2007-03-08 16:20:32 +0100 | [diff] [blame] | 72 | #endif |
Heiko Schocher | e7d9c4f | 2012-01-16 21:12:23 +0000 | [diff] [blame] | 73 | |
| 74 | struct i2c_adapter { |
| 75 | void (*init)(struct i2c_adapter *adap, int speed, |
| 76 | int slaveaddr); |
| 77 | int (*probe)(struct i2c_adapter *adap, uint8_t chip); |
| 78 | int (*read)(struct i2c_adapter *adap, uint8_t chip, |
| 79 | uint addr, int alen, uint8_t *buffer, |
| 80 | int len); |
| 81 | int (*write)(struct i2c_adapter *adap, uint8_t chip, |
| 82 | uint addr, int alen, uint8_t *buffer, |
| 83 | int len); |
| 84 | uint (*set_bus_speed)(struct i2c_adapter *adap, |
| 85 | uint speed); |
| 86 | int speed; |
| 87 | int slaveaddr; |
| 88 | int init_done; |
| 89 | int hwadapnr; |
| 90 | char *name; |
| 91 | }; |
| 92 | |
| 93 | #define U_BOOT_I2C_MKENT_COMPLETE(_init, _probe, _read, _write, \ |
| 94 | _set_speed, _speed, _slaveaddr, _hwadapnr, _name) \ |
| 95 | { \ |
| 96 | .init = _init, \ |
| 97 | .probe = _probe, \ |
| 98 | .read = _read, \ |
| 99 | .write = _write, \ |
| 100 | .set_bus_speed = _set_speed, \ |
| 101 | .speed = _speed, \ |
| 102 | .slaveaddr = _slaveaddr, \ |
| 103 | .init_done = 0, \ |
| 104 | .hwadapnr = _hwadapnr, \ |
| 105 | .name = #_name \ |
| 106 | }; |
| 107 | |
| 108 | #define U_BOOT_I2C_ADAP_COMPLETE(_name, _init, _probe, _read, _write, \ |
| 109 | _set_speed, _speed, _slaveaddr, _hwadapnr) \ |
| 110 | ll_entry_declare(struct i2c_adapter, _name, i2c) = \ |
| 111 | U_BOOT_I2C_MKENT_COMPLETE(_init, _probe, _read, _write, \ |
| 112 | _set_speed, _speed, _slaveaddr, _hwadapnr, _name); |
| 113 | |
| 114 | struct i2c_adapter *i2c_get_adapter(int index); |
| 115 | |
| 116 | #ifndef CONFIG_SYS_I2C_DIRECT_BUS |
| 117 | struct i2c_mux { |
| 118 | int id; |
| 119 | char name[16]; |
| 120 | }; |
| 121 | |
| 122 | struct i2c_next_hop { |
| 123 | struct i2c_mux mux; |
| 124 | uint8_t chip; |
| 125 | uint8_t channel; |
| 126 | }; |
| 127 | |
| 128 | struct i2c_bus_hose { |
| 129 | int adapter; |
| 130 | struct i2c_next_hop next_hop[CONFIG_SYS_I2C_MAX_HOPS]; |
| 131 | }; |
| 132 | #define I2C_NULL_HOP {{-1, ""}, 0, 0} |
| 133 | extern struct i2c_bus_hose i2c_bus[]; |
| 134 | |
| 135 | #define I2C_ADAPTER(bus) i2c_bus[bus].adapter |
| 136 | #else |
| 137 | #define I2C_ADAPTER(bus) bus |
| 138 | #endif |
| 139 | #define I2C_BUS gd->cur_i2c_bus |
| 140 | |
| 141 | #define I2C_ADAP_NR(bus) i2c_get_adapter(I2C_ADAPTER(bus)) |
| 142 | #define I2C_ADAP I2C_ADAP_NR(gd->cur_i2c_bus) |
| 143 | #define I2C_ADAP_HWNR (I2C_ADAP->hwadapnr) |
| 144 | |
| 145 | #ifndef CONFIG_SYS_I2C_DIRECT_BUS |
| 146 | #define I2C_MUX_PCA9540_ID 1 |
| 147 | #define I2C_MUX_PCA9540 {I2C_MUX_PCA9540_ID, "PCA9540B"} |
| 148 | #define I2C_MUX_PCA9542_ID 2 |
| 149 | #define I2C_MUX_PCA9542 {I2C_MUX_PCA9542_ID, "PCA9542A"} |
| 150 | #define I2C_MUX_PCA9544_ID 3 |
| 151 | #define I2C_MUX_PCA9544 {I2C_MUX_PCA9544_ID, "PCA9544A"} |
| 152 | #define I2C_MUX_PCA9547_ID 4 |
| 153 | #define I2C_MUX_PCA9547 {I2C_MUX_PCA9547_ID, "PCA9547A"} |
| 154 | #endif |
Stefan Roese | 9de4fcb | 2007-03-01 07:03:25 +0100 | [diff] [blame] | 155 | |
Heiko Schocher | dc7d22a | 2008-10-15 09:35:26 +0200 | [diff] [blame] | 156 | #ifndef I2C_SOFT_DECLARATIONS |
| 157 | # if defined(CONFIG_MPC8260) |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 158 | # define I2C_SOFT_DECLARATIONS volatile ioport_t *iop = ioport_addr((immap_t *)CONFIG_SYS_IMMR, I2C_PORT); |
Heiko Schocher | dc7d22a | 2008-10-15 09:35:26 +0200 | [diff] [blame] | 159 | # elif defined(CONFIG_8xx) |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 160 | # define I2C_SOFT_DECLARATIONS volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR; |
Jens Scharsig | a4db1ca | 2010-02-03 22:46:58 +0100 | [diff] [blame] | 161 | |
| 162 | # elif (defined(CONFIG_AT91RM9200) || \ |
| 163 | defined(CONFIG_AT91SAM9260) || defined(CONFIG_AT91SAM9261) || \ |
| 164 | defined(CONFIG_AT91SAM9263)) && !defined(CONFIG_AT91_LEGACY) |
esw@bus-elektronik.de | 5082bab | 2011-12-20 06:05:30 +0000 | [diff] [blame] | 165 | # define I2C_SOFT_DECLARATIONS at91_pio_t *pio = (at91_pio_t *) ATMEL_BASE_PIOA; |
Heiko Schocher | dc7d22a | 2008-10-15 09:35:26 +0200 | [diff] [blame] | 166 | # else |
| 167 | # define I2C_SOFT_DECLARATIONS |
| 168 | # endif |
| 169 | #endif |
Timur Tabi | 13d598f | 2008-12-03 11:28:30 -0600 | [diff] [blame] | 170 | |
| 171 | #ifdef CONFIG_8xx |
Peter Tyser | 769069e | 2009-04-24 15:34:05 -0500 | [diff] [blame] | 172 | /* Set default value for the I2C bus speed on 8xx. In the |
Timur Tabi | 13d598f | 2008-12-03 11:28:30 -0600 | [diff] [blame] | 173 | * future, we'll define these in all 8xx board config files. |
| 174 | */ |
| 175 | #ifndef CONFIG_SYS_I2C_SPEED |
| 176 | #define CONFIG_SYS_I2C_SPEED 50000 |
| 177 | #endif |
Peter Tyser | 769069e | 2009-04-24 15:34:05 -0500 | [diff] [blame] | 178 | #endif |
Timur Tabi | 13d598f | 2008-12-03 11:28:30 -0600 | [diff] [blame] | 179 | |
Peter Tyser | 769069e | 2009-04-24 15:34:05 -0500 | [diff] [blame] | 180 | /* |
| 181 | * Many boards/controllers/drivers don't support an I2C slave interface so |
| 182 | * provide a default slave address for them for use in common code. A real |
| 183 | * value for CONFIG_SYS_I2C_SLAVE should be defined for any board which does |
| 184 | * support a slave interface. |
| 185 | */ |
Timur Tabi | 13d598f | 2008-12-03 11:28:30 -0600 | [diff] [blame] | 186 | #ifndef CONFIG_SYS_I2C_SLAVE |
Peter Tyser | 769069e | 2009-04-24 15:34:05 -0500 | [diff] [blame] | 187 | #define CONFIG_SYS_I2C_SLAVE 0xfe |
Timur Tabi | 13d598f | 2008-12-03 11:28:30 -0600 | [diff] [blame] | 188 | #endif |
| 189 | |
wdenk | 1f04521 | 2002-03-10 14:37:15 +0000 | [diff] [blame] | 190 | /* |
| 191 | * Initialization, must be called once on start up, may be called |
| 192 | * repeatedly to change the speed and slave addresses. |
| 193 | */ |
| 194 | void i2c_init(int speed, int slaveaddr); |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 195 | void i2c_init_board(void); |
Richard Retanubun | df0149c | 2010-04-12 15:08:17 -0400 | [diff] [blame] | 196 | #ifdef CONFIG_SYS_I2C_BOARD_LATE_INIT |
| 197 | void i2c_board_late_init(void); |
| 198 | #endif |
wdenk | 1f04521 | 2002-03-10 14:37:15 +0000 | [diff] [blame] | 199 | |
Heiko Schocher | 6ee861b | 2008-10-15 09:39:47 +0200 | [diff] [blame] | 200 | #if defined(CONFIG_I2C_MUX) |
| 201 | |
| 202 | typedef struct _mux { |
| 203 | uchar chip; |
| 204 | uchar channel; |
| 205 | char *name; |
| 206 | struct _mux *next; |
| 207 | } I2C_MUX; |
| 208 | |
| 209 | typedef struct _mux_device { |
| 210 | int busid; |
| 211 | I2C_MUX *mux; /* List of muxes, to reach the device */ |
| 212 | struct _mux_device *next; |
| 213 | } I2C_MUX_DEVICE; |
| 214 | |
Heiko Schocher | 6ee861b | 2008-10-15 09:39:47 +0200 | [diff] [blame] | 215 | I2C_MUX_DEVICE *i2c_mux_search_device(int id); |
| 216 | I2C_MUX_DEVICE *i2c_mux_ident_muxstring (uchar *buf); |
| 217 | int i2x_mux_select_mux(int bus); |
| 218 | int i2c_mux_ident_muxstring_f (uchar *buf); |
| 219 | #endif |
| 220 | |
Heiko Schocher | e7d9c4f | 2012-01-16 21:12:23 +0000 | [diff] [blame] | 221 | #ifdef CONFIG_SYS_I2C |
| 222 | /* |
Heiko Schocher | e7d9c4f | 2012-01-16 21:12:23 +0000 | [diff] [blame] | 223 | * i2c_get_bus_num: |
| 224 | * |
| 225 | * Returns index of currently active I2C bus. Zero-based. |
| 226 | */ |
| 227 | unsigned int i2c_get_bus_num(void); |
| 228 | |
| 229 | /* |
| 230 | * i2c_set_bus_num: |
| 231 | * |
| 232 | * Change the active I2C bus. Subsequent read/write calls will |
| 233 | * go to this one. |
| 234 | * |
| 235 | * bus - bus index, zero based |
| 236 | * |
| 237 | * Returns: 0 on success, not 0 on failure |
| 238 | * |
| 239 | */ |
| 240 | int i2c_set_bus_num(unsigned int bus); |
| 241 | |
| 242 | /* |
| 243 | * i2c_init_all(): |
| 244 | * |
| 245 | * Initializes all I2C adapters in the system. All i2c_adap structures must |
| 246 | * be initialized beforehead with function pointers and data, including |
| 247 | * speed and slaveaddr. Returns 0 on success, non-0 on failure. |
| 248 | */ |
| 249 | void i2c_init_all(void); |
| 250 | |
| 251 | /* |
| 252 | * Probe the given I2C chip address. Returns 0 if a chip responded, |
| 253 | * not 0 on failure. |
| 254 | */ |
| 255 | int i2c_probe(uint8_t chip); |
| 256 | |
| 257 | /* |
| 258 | * Read/Write interface: |
| 259 | * chip: I2C chip address, range 0..127 |
| 260 | * addr: Memory (register) address within the chip |
| 261 | * alen: Number of bytes to use for addr (typically 1, 2 for larger |
| 262 | * memories, 0 for register type devices with only one |
| 263 | * register) |
| 264 | * buffer: Where to read/write the data |
| 265 | * len: How many bytes to read/write |
| 266 | * |
| 267 | * Returns: 0 on success, not 0 on failure |
| 268 | */ |
| 269 | int i2c_read(uint8_t chip, unsigned int addr, int alen, |
| 270 | uint8_t *buffer, int len); |
| 271 | |
| 272 | int i2c_write(uint8_t chip, unsigned int addr, int alen, |
| 273 | uint8_t *buffer, int len); |
| 274 | |
| 275 | /* |
| 276 | * Utility routines to read/write registers. |
| 277 | */ |
| 278 | uint8_t i2c_reg_read(uint8_t addr, uint8_t reg); |
| 279 | |
| 280 | void i2c_reg_write(uint8_t addr, uint8_t reg, uint8_t val); |
| 281 | |
| 282 | /* |
| 283 | * i2c_set_bus_speed: |
| 284 | * |
| 285 | * Change the speed of the active I2C bus |
| 286 | * |
| 287 | * speed - bus speed in Hz |
| 288 | * |
| 289 | * Returns: new bus speed |
| 290 | * |
| 291 | */ |
| 292 | unsigned int i2c_set_bus_speed(unsigned int speed); |
| 293 | |
| 294 | /* |
| 295 | * i2c_get_bus_speed: |
| 296 | * |
| 297 | * Returns speed of currently active I2C bus in Hz |
| 298 | */ |
| 299 | |
| 300 | unsigned int i2c_get_bus_speed(void); |
| 301 | |
| 302 | /* |
| 303 | * i2c_reloc_fixup: |
| 304 | * |
| 305 | * Adjusts I2C pointers after U-Boot is relocated to DRAM |
| 306 | */ |
| 307 | void i2c_reloc_fixup(void); |
Heiko Schocher | 479a4cf | 2013-01-29 08:53:15 +0100 | [diff] [blame^] | 308 | #if defined(CONFIG_SYS_I2C_SOFT) |
| 309 | void i2c_soft_init(void); |
| 310 | void i2c_soft_active(void); |
| 311 | void i2c_soft_tristate(void); |
| 312 | int i2c_soft_read(void); |
| 313 | void i2c_soft_sda(int bit); |
| 314 | void i2c_soft_scl(int bit); |
| 315 | void i2c_soft_delay(void); |
| 316 | #endif |
Heiko Schocher | e7d9c4f | 2012-01-16 21:12:23 +0000 | [diff] [blame] | 317 | #else |
| 318 | |
| 319 | /* |
wdenk | 1f04521 | 2002-03-10 14:37:15 +0000 | [diff] [blame] | 320 | * Probe the given I2C chip address. Returns 0 if a chip responded, |
| 321 | * not 0 on failure. |
| 322 | */ |
| 323 | int i2c_probe(uchar chip); |
| 324 | |
| 325 | /* |
| 326 | * Read/Write interface: |
| 327 | * chip: I2C chip address, range 0..127 |
| 328 | * addr: Memory (register) address within the chip |
| 329 | * alen: Number of bytes to use for addr (typically 1, 2 for larger |
| 330 | * memories, 0 for register type devices with only one |
| 331 | * register) |
| 332 | * buffer: Where to read/write the data |
| 333 | * len: How many bytes to read/write |
| 334 | * |
| 335 | * Returns: 0 on success, not 0 on failure |
| 336 | */ |
| 337 | int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len); |
| 338 | int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len); |
| 339 | |
| 340 | /* |
| 341 | * Utility routines to read/write registers. |
| 342 | */ |
Timur Tabi | 13d598f | 2008-12-03 11:28:30 -0600 | [diff] [blame] | 343 | static inline u8 i2c_reg_read(u8 addr, u8 reg) |
| 344 | { |
| 345 | u8 buf; |
| 346 | |
| 347 | #ifdef CONFIG_8xx |
| 348 | /* MPC8xx needs this. Maybe one day we can get rid of it. */ |
| 349 | i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE); |
| 350 | #endif |
| 351 | |
| 352 | #ifdef DEBUG |
| 353 | printf("%s: addr=0x%02x, reg=0x%02x\n", __func__, addr, reg); |
| 354 | #endif |
| 355 | |
Timur Tabi | 13d598f | 2008-12-03 11:28:30 -0600 | [diff] [blame] | 356 | i2c_read(addr, reg, 1, &buf, 1); |
Timur Tabi | 13d598f | 2008-12-03 11:28:30 -0600 | [diff] [blame] | 357 | |
| 358 | return buf; |
| 359 | } |
| 360 | |
| 361 | static inline void i2c_reg_write(u8 addr, u8 reg, u8 val) |
| 362 | { |
| 363 | #ifdef CONFIG_8xx |
| 364 | /* MPC8xx needs this. Maybe one day we can get rid of it. */ |
| 365 | i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE); |
| 366 | #endif |
| 367 | |
| 368 | #ifdef DEBUG |
| 369 | printf("%s: addr=0x%02x, reg=0x%02x, val=0x%02x\n", |
| 370 | __func__, addr, reg, val); |
| 371 | #endif |
| 372 | |
Timur Tabi | 13d598f | 2008-12-03 11:28:30 -0600 | [diff] [blame] | 373 | i2c_write(addr, reg, 1, &val, 1); |
Timur Tabi | 13d598f | 2008-12-03 11:28:30 -0600 | [diff] [blame] | 374 | } |
wdenk | 1f04521 | 2002-03-10 14:37:15 +0000 | [diff] [blame] | 375 | |
Ben Warren | 4565715 | 2006-09-07 16:50:54 -0400 | [diff] [blame] | 376 | /* |
| 377 | * Functions for setting the current I2C bus and its speed |
| 378 | */ |
| 379 | |
| 380 | /* |
| 381 | * i2c_set_bus_num: |
| 382 | * |
| 383 | * Change the active I2C bus. Subsequent read/write calls will |
| 384 | * go to this one. |
| 385 | * |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 386 | * bus - bus index, zero based |
Ben Warren | 4565715 | 2006-09-07 16:50:54 -0400 | [diff] [blame] | 387 | * |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 388 | * Returns: 0 on success, not 0 on failure |
Ben Warren | 4565715 | 2006-09-07 16:50:54 -0400 | [diff] [blame] | 389 | * |
| 390 | */ |
Timur Tabi | c0b114a | 2006-10-31 21:23:16 -0600 | [diff] [blame] | 391 | int i2c_set_bus_num(unsigned int bus); |
Ben Warren | 4565715 | 2006-09-07 16:50:54 -0400 | [diff] [blame] | 392 | |
| 393 | /* |
| 394 | * i2c_get_bus_num: |
| 395 | * |
| 396 | * Returns index of currently active I2C bus. Zero-based. |
| 397 | */ |
| 398 | |
Timur Tabi | c0b114a | 2006-10-31 21:23:16 -0600 | [diff] [blame] | 399 | unsigned int i2c_get_bus_num(void); |
Ben Warren | 4565715 | 2006-09-07 16:50:54 -0400 | [diff] [blame] | 400 | |
| 401 | /* |
| 402 | * i2c_set_bus_speed: |
| 403 | * |
| 404 | * Change the speed of the active I2C bus |
| 405 | * |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 406 | * speed - bus speed in Hz |
Ben Warren | 4565715 | 2006-09-07 16:50:54 -0400 | [diff] [blame] | 407 | * |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 408 | * Returns: 0 on success, not 0 on failure |
Ben Warren | 4565715 | 2006-09-07 16:50:54 -0400 | [diff] [blame] | 409 | * |
| 410 | */ |
Timur Tabi | c0b114a | 2006-10-31 21:23:16 -0600 | [diff] [blame] | 411 | int i2c_set_bus_speed(unsigned int); |
Ben Warren | 4565715 | 2006-09-07 16:50:54 -0400 | [diff] [blame] | 412 | |
| 413 | /* |
| 414 | * i2c_get_bus_speed: |
| 415 | * |
| 416 | * Returns speed of currently active I2C bus in Hz |
| 417 | */ |
| 418 | |
Timur Tabi | c0b114a | 2006-10-31 21:23:16 -0600 | [diff] [blame] | 419 | unsigned int i2c_get_bus_speed(void); |
Heiko Schocher | e7d9c4f | 2012-01-16 21:12:23 +0000 | [diff] [blame] | 420 | #endif /* CONFIG_SYS_I2C */ |
| 421 | |
| 422 | /* |
| 423 | * only for backwardcompatibility, should go away if we switched |
| 424 | * completely to new multibus support. |
| 425 | */ |
| 426 | #if defined(CONFIG_SYS_I2C) || defined(CONFIG_I2C_MULTI_BUS) |
| 427 | # if !defined(CONFIG_SYS_MAX_I2C_BUS) |
| 428 | # define CONFIG_SYS_MAX_I2C_BUS 2 |
| 429 | # endif |
| 430 | # define I2C_MULTI_BUS 0 |
| 431 | #else |
| 432 | # define CONFIG_SYS_MAX_I2C_BUS 1 |
| 433 | # define I2C_MULTI_BUS 0 |
| 434 | #endif |
Ben Warren | 4565715 | 2006-09-07 16:50:54 -0400 | [diff] [blame] | 435 | |
Marek Vasut | 513b820 | 2011-10-25 11:40:57 +0200 | [diff] [blame] | 436 | /* NOTE: These two functions MUST be always_inline to avoid code growth! */ |
| 437 | static inline unsigned int I2C_GET_BUS(void) __attribute__((always_inline)); |
| 438 | static inline unsigned int I2C_GET_BUS(void) |
| 439 | { |
| 440 | return I2C_MULTI_BUS ? i2c_get_bus_num() : 0; |
| 441 | } |
| 442 | |
| 443 | static inline void I2C_SET_BUS(unsigned int bus) __attribute__((always_inline)); |
| 444 | static inline void I2C_SET_BUS(unsigned int bus) |
| 445 | { |
| 446 | if (I2C_MULTI_BUS) |
| 447 | i2c_set_bus_num(bus); |
| 448 | } |
| 449 | |
Ćukasz Majewski | b2d7652 | 2012-09-04 23:15:20 +0000 | [diff] [blame] | 450 | /* Multi I2C definitions */ |
| 451 | enum { |
| 452 | I2C_0, I2C_1, I2C_2, I2C_3, I2C_4, I2C_5, I2C_6, I2C_7, |
| 453 | I2C_8, I2C_9, I2C_10, |
| 454 | }; |
| 455 | |
| 456 | /* Multi I2C busses handling */ |
| 457 | #ifdef CONFIG_SOFT_I2C_MULTI_BUS |
| 458 | extern int get_multi_scl_pin(void); |
| 459 | extern int get_multi_sda_pin(void); |
| 460 | extern int multi_i2c_init(void); |
| 461 | #endif |
Rajeshwari Shinde | 53cfac5 | 2012-12-26 20:03:12 +0000 | [diff] [blame] | 462 | |
| 463 | /** |
| 464 | * Get FDT values for i2c bus. |
| 465 | * |
| 466 | * @param blob Device tree blbo |
| 467 | * @return the number of I2C bus |
| 468 | */ |
| 469 | void board_i2c_init(const void *blob); |
| 470 | |
| 471 | /** |
| 472 | * Find the I2C bus number by given a FDT I2C node. |
| 473 | * |
| 474 | * @param blob Device tree blbo |
| 475 | * @param node FDT I2C node to find |
| 476 | * @return the number of I2C bus (zero based), or -1 on error |
| 477 | */ |
| 478 | int i2c_get_bus_num_fdt(int node); |
| 479 | |
| 480 | /** |
| 481 | * Reset the I2C bus represented by the given a FDT I2C node. |
| 482 | * |
| 483 | * @param blob Device tree blbo |
| 484 | * @param node FDT I2C node to find |
| 485 | * @return 0 if port was reset, -1 if not found |
| 486 | */ |
| 487 | int i2c_reset_port_fdt(const void *blob, int node); |
wdenk | 1f04521 | 2002-03-10 14:37:15 +0000 | [diff] [blame] | 488 | #endif /* _I2C_H_ */ |