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