11f045217Swdenk /* 2385c9ef5SHeiko Schocher * Copyright (C) 2009 Sergey Kubushyn <ksi@koi8.net> 3385c9ef5SHeiko Schocher * Copyright (C) 2009 - 2013 Heiko Schocher <hs@denx.de> 4385c9ef5SHeiko Schocher * Changes for multibus/multiadapter I2C support. 5385c9ef5SHeiko Schocher * 61f045217Swdenk * (C) Copyright 2001 71f045217Swdenk * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com. 81f045217Swdenk * 91a459660SWolfgang Denk * SPDX-License-Identifier: GPL-2.0+ 101f045217Swdenk * 111f045217Swdenk * The original I2C interface was 121f045217Swdenk * (C) 2000 by Paolo Scaffardi (arsenio@tin.it) 131f045217Swdenk * AIRVENT SAM s.p.a - RIMINI(ITALY) 141f045217Swdenk * but has been changed substantially. 151f045217Swdenk */ 161f045217Swdenk 171f045217Swdenk #ifndef _I2C_H_ 181f045217Swdenk #define _I2C_H_ 191f045217Swdenk 201f045217Swdenk /* 211f045217Swdenk * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING 221f045217Swdenk * 231f045217Swdenk * The implementation MUST NOT use static or global variables if the 241f045217Swdenk * I2C routines are used to read SDRAM configuration information 251f045217Swdenk * because this is done before the memories are initialized. Limited 261f045217Swdenk * use of stack-based variables are OK (the initial stack size is 271f045217Swdenk * limited). 281f045217Swdenk * 291f045217Swdenk * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING 301f045217Swdenk */ 311f045217Swdenk 321f045217Swdenk /* 331f045217Swdenk * Configuration items. 341f045217Swdenk */ 351f045217Swdenk #define I2C_RXTX_LEN 128 /* maximum tx/rx buffer length */ 361f045217Swdenk 37385c9ef5SHeiko Schocher #if !defined(CONFIG_SYS_I2C_MAX_HOPS) 38385c9ef5SHeiko Schocher /* no muxes used bus = i2c adapters */ 39385c9ef5SHeiko Schocher #define CONFIG_SYS_I2C_DIRECT_BUS 1 40385c9ef5SHeiko Schocher #define CONFIG_SYS_I2C_MAX_HOPS 0 41385c9ef5SHeiko Schocher #define CONFIG_SYS_NUM_I2C_BUSES ll_entry_count(struct i2c_adapter, i2c) 4279b2d0bbSStefan Roese #else 43385c9ef5SHeiko Schocher /* we use i2c muxes */ 44385c9ef5SHeiko Schocher #undef CONFIG_SYS_I2C_DIRECT_BUS 4579b2d0bbSStefan Roese #endif 4679b2d0bbSStefan Roese 478c12045aSStefan Roese /* define the I2C bus number for RTC and DTT if not already done */ 486d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #if !defined(CONFIG_SYS_RTC_BUS_NUM) 496d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #define CONFIG_SYS_RTC_BUS_NUM 0 508c12045aSStefan Roese #endif 516d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #if !defined(CONFIG_SYS_DTT_BUS_NUM) 526d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #define CONFIG_SYS_DTT_BUS_NUM 0 538c12045aSStefan Roese #endif 546d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #if !defined(CONFIG_SYS_SPD_BUS_NUM) 556d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #define CONFIG_SYS_SPD_BUS_NUM 0 56d8a8ea5cSMatthias Fuchs #endif 578c12045aSStefan Roese 58385c9ef5SHeiko Schocher struct i2c_adapter { 59385c9ef5SHeiko Schocher void (*init)(struct i2c_adapter *adap, int speed, 60385c9ef5SHeiko Schocher int slaveaddr); 61385c9ef5SHeiko Schocher int (*probe)(struct i2c_adapter *adap, uint8_t chip); 62385c9ef5SHeiko Schocher int (*read)(struct i2c_adapter *adap, uint8_t chip, 63385c9ef5SHeiko Schocher uint addr, int alen, uint8_t *buffer, 64385c9ef5SHeiko Schocher int len); 65385c9ef5SHeiko Schocher int (*write)(struct i2c_adapter *adap, uint8_t chip, 66385c9ef5SHeiko Schocher uint addr, int alen, uint8_t *buffer, 67385c9ef5SHeiko Schocher int len); 68385c9ef5SHeiko Schocher uint (*set_bus_speed)(struct i2c_adapter *adap, 69385c9ef5SHeiko Schocher uint speed); 70385c9ef5SHeiko Schocher int speed; 71385c9ef5SHeiko Schocher int slaveaddr; 72385c9ef5SHeiko Schocher int init_done; 73385c9ef5SHeiko Schocher int hwadapnr; 74385c9ef5SHeiko Schocher char *name; 75385c9ef5SHeiko Schocher }; 76385c9ef5SHeiko Schocher 77385c9ef5SHeiko Schocher #define U_BOOT_I2C_MKENT_COMPLETE(_init, _probe, _read, _write, \ 78385c9ef5SHeiko Schocher _set_speed, _speed, _slaveaddr, _hwadapnr, _name) \ 79385c9ef5SHeiko Schocher { \ 80385c9ef5SHeiko Schocher .init = _init, \ 81385c9ef5SHeiko Schocher .probe = _probe, \ 82385c9ef5SHeiko Schocher .read = _read, \ 83385c9ef5SHeiko Schocher .write = _write, \ 84385c9ef5SHeiko Schocher .set_bus_speed = _set_speed, \ 85385c9ef5SHeiko Schocher .speed = _speed, \ 86385c9ef5SHeiko Schocher .slaveaddr = _slaveaddr, \ 87385c9ef5SHeiko Schocher .init_done = 0, \ 88385c9ef5SHeiko Schocher .hwadapnr = _hwadapnr, \ 89385c9ef5SHeiko Schocher .name = #_name \ 90385c9ef5SHeiko Schocher }; 91385c9ef5SHeiko Schocher 92385c9ef5SHeiko Schocher #define U_BOOT_I2C_ADAP_COMPLETE(_name, _init, _probe, _read, _write, \ 93385c9ef5SHeiko Schocher _set_speed, _speed, _slaveaddr, _hwadapnr) \ 94385c9ef5SHeiko Schocher ll_entry_declare(struct i2c_adapter, _name, i2c) = \ 95385c9ef5SHeiko Schocher U_BOOT_I2C_MKENT_COMPLETE(_init, _probe, _read, _write, \ 96385c9ef5SHeiko Schocher _set_speed, _speed, _slaveaddr, _hwadapnr, _name); 97385c9ef5SHeiko Schocher 98385c9ef5SHeiko Schocher struct i2c_adapter *i2c_get_adapter(int index); 99385c9ef5SHeiko Schocher 100385c9ef5SHeiko Schocher #ifndef CONFIG_SYS_I2C_DIRECT_BUS 101385c9ef5SHeiko Schocher struct i2c_mux { 102385c9ef5SHeiko Schocher int id; 103385c9ef5SHeiko Schocher char name[16]; 104385c9ef5SHeiko Schocher }; 105385c9ef5SHeiko Schocher 106385c9ef5SHeiko Schocher struct i2c_next_hop { 107385c9ef5SHeiko Schocher struct i2c_mux mux; 108385c9ef5SHeiko Schocher uint8_t chip; 109385c9ef5SHeiko Schocher uint8_t channel; 110385c9ef5SHeiko Schocher }; 111385c9ef5SHeiko Schocher 112385c9ef5SHeiko Schocher struct i2c_bus_hose { 113385c9ef5SHeiko Schocher int adapter; 114385c9ef5SHeiko Schocher struct i2c_next_hop next_hop[CONFIG_SYS_I2C_MAX_HOPS]; 115385c9ef5SHeiko Schocher }; 116385c9ef5SHeiko Schocher #define I2C_NULL_HOP {{-1, ""}, 0, 0} 117385c9ef5SHeiko Schocher extern struct i2c_bus_hose i2c_bus[]; 118385c9ef5SHeiko Schocher 119385c9ef5SHeiko Schocher #define I2C_ADAPTER(bus) i2c_bus[bus].adapter 120385c9ef5SHeiko Schocher #else 121385c9ef5SHeiko Schocher #define I2C_ADAPTER(bus) bus 122385c9ef5SHeiko Schocher #endif 123385c9ef5SHeiko Schocher #define I2C_BUS gd->cur_i2c_bus 124385c9ef5SHeiko Schocher 125385c9ef5SHeiko Schocher #define I2C_ADAP_NR(bus) i2c_get_adapter(I2C_ADAPTER(bus)) 126385c9ef5SHeiko Schocher #define I2C_ADAP I2C_ADAP_NR(gd->cur_i2c_bus) 127385c9ef5SHeiko Schocher #define I2C_ADAP_HWNR (I2C_ADAP->hwadapnr) 128385c9ef5SHeiko Schocher 129385c9ef5SHeiko Schocher #ifndef CONFIG_SYS_I2C_DIRECT_BUS 130385c9ef5SHeiko Schocher #define I2C_MUX_PCA9540_ID 1 131385c9ef5SHeiko Schocher #define I2C_MUX_PCA9540 {I2C_MUX_PCA9540_ID, "PCA9540B"} 132385c9ef5SHeiko Schocher #define I2C_MUX_PCA9542_ID 2 133385c9ef5SHeiko Schocher #define I2C_MUX_PCA9542 {I2C_MUX_PCA9542_ID, "PCA9542A"} 134385c9ef5SHeiko Schocher #define I2C_MUX_PCA9544_ID 3 135385c9ef5SHeiko Schocher #define I2C_MUX_PCA9544 {I2C_MUX_PCA9544_ID, "PCA9544A"} 136385c9ef5SHeiko Schocher #define I2C_MUX_PCA9547_ID 4 137385c9ef5SHeiko Schocher #define I2C_MUX_PCA9547 {I2C_MUX_PCA9547_ID, "PCA9547A"} 138385c9ef5SHeiko Schocher #endif 139385c9ef5SHeiko Schocher 14098aed379SHeiko Schocher #ifndef I2C_SOFT_DECLARATIONS 14198aed379SHeiko Schocher # if defined(CONFIG_MPC8260) 1426d0f6bcfSJean-Christophe PLAGNIOL-VILLARD # define I2C_SOFT_DECLARATIONS volatile ioport_t *iop = ioport_addr((immap_t *)CONFIG_SYS_IMMR, I2C_PORT); 14398aed379SHeiko Schocher # elif defined(CONFIG_8xx) 1446d0f6bcfSJean-Christophe PLAGNIOL-VILLARD # define I2C_SOFT_DECLARATIONS volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR; 1450cf0b931SJens Scharsig 1460cf0b931SJens Scharsig # elif (defined(CONFIG_AT91RM9200) || \ 1470cf0b931SJens Scharsig defined(CONFIG_AT91SAM9260) || defined(CONFIG_AT91SAM9261) || \ 1480cf0b931SJens Scharsig defined(CONFIG_AT91SAM9263)) && !defined(CONFIG_AT91_LEGACY) 14978132275Sesw@bus-elektronik.de # define I2C_SOFT_DECLARATIONS at91_pio_t *pio = (at91_pio_t *) ATMEL_BASE_PIOA; 15098aed379SHeiko Schocher # else 15198aed379SHeiko Schocher # define I2C_SOFT_DECLARATIONS 15298aed379SHeiko Schocher # endif 15398aed379SHeiko Schocher #endif 154ecf5f077STimur Tabi 155ecf5f077STimur Tabi #ifdef CONFIG_8xx 1569c90a2c8SPeter Tyser /* Set default value for the I2C bus speed on 8xx. In the 157ecf5f077STimur Tabi * future, we'll define these in all 8xx board config files. 158ecf5f077STimur Tabi */ 159ecf5f077STimur Tabi #ifndef CONFIG_SYS_I2C_SPEED 160ecf5f077STimur Tabi #define CONFIG_SYS_I2C_SPEED 50000 161ecf5f077STimur Tabi #endif 162ecf5f077STimur Tabi #endif 1639c90a2c8SPeter Tyser 1649c90a2c8SPeter Tyser /* 1659c90a2c8SPeter Tyser * Many boards/controllers/drivers don't support an I2C slave interface so 1669c90a2c8SPeter Tyser * provide a default slave address for them for use in common code. A real 1679c90a2c8SPeter Tyser * value for CONFIG_SYS_I2C_SLAVE should be defined for any board which does 1689c90a2c8SPeter Tyser * support a slave interface. 1699c90a2c8SPeter Tyser */ 1709c90a2c8SPeter Tyser #ifndef CONFIG_SYS_I2C_SLAVE 1719c90a2c8SPeter Tyser #define CONFIG_SYS_I2C_SLAVE 0xfe 172ecf5f077STimur Tabi #endif 173ecf5f077STimur Tabi 1741f045217Swdenk /* 1751f045217Swdenk * Initialization, must be called once on start up, may be called 1761f045217Swdenk * repeatedly to change the speed and slave addresses. 1771f045217Swdenk */ 1781f045217Swdenk void i2c_init(int speed, int slaveaddr); 17906d01dbeSwdenk void i2c_init_board(void); 18026a33504SRichard Retanubun #ifdef CONFIG_SYS_I2C_BOARD_LATE_INIT 18126a33504SRichard Retanubun void i2c_board_late_init(void); 18226a33504SRichard Retanubun #endif 1831f045217Swdenk 184385c9ef5SHeiko Schocher #ifdef CONFIG_SYS_I2C 185385c9ef5SHeiko Schocher /* 186385c9ef5SHeiko Schocher * i2c_get_bus_num: 187385c9ef5SHeiko Schocher * 188385c9ef5SHeiko Schocher * Returns index of currently active I2C bus. Zero-based. 189385c9ef5SHeiko Schocher */ 190385c9ef5SHeiko Schocher unsigned int i2c_get_bus_num(void); 19167b23a32SHeiko Schocher 192385c9ef5SHeiko Schocher /* 193385c9ef5SHeiko Schocher * i2c_set_bus_num: 194385c9ef5SHeiko Schocher * 195385c9ef5SHeiko Schocher * Change the active I2C bus. Subsequent read/write calls will 196385c9ef5SHeiko Schocher * go to this one. 197385c9ef5SHeiko Schocher * 198385c9ef5SHeiko Schocher * bus - bus index, zero based 199385c9ef5SHeiko Schocher * 200385c9ef5SHeiko Schocher * Returns: 0 on success, not 0 on failure 201385c9ef5SHeiko Schocher * 202385c9ef5SHeiko Schocher */ 203385c9ef5SHeiko Schocher int i2c_set_bus_num(unsigned int bus); 20467b23a32SHeiko Schocher 205385c9ef5SHeiko Schocher /* 206385c9ef5SHeiko Schocher * i2c_init_all(): 207385c9ef5SHeiko Schocher * 208385c9ef5SHeiko Schocher * Initializes all I2C adapters in the system. All i2c_adap structures must 209385c9ef5SHeiko Schocher * be initialized beforehead with function pointers and data, including 210385c9ef5SHeiko Schocher * speed and slaveaddr. Returns 0 on success, non-0 on failure. 211385c9ef5SHeiko Schocher */ 212385c9ef5SHeiko Schocher void i2c_init_all(void); 21367b23a32SHeiko Schocher 214385c9ef5SHeiko Schocher /* 215385c9ef5SHeiko Schocher * Probe the given I2C chip address. Returns 0 if a chip responded, 216385c9ef5SHeiko Schocher * not 0 on failure. 217385c9ef5SHeiko Schocher */ 218385c9ef5SHeiko Schocher int i2c_probe(uint8_t chip); 219385c9ef5SHeiko Schocher 220385c9ef5SHeiko Schocher /* 221385c9ef5SHeiko Schocher * Read/Write interface: 222385c9ef5SHeiko Schocher * chip: I2C chip address, range 0..127 223385c9ef5SHeiko Schocher * addr: Memory (register) address within the chip 224385c9ef5SHeiko Schocher * alen: Number of bytes to use for addr (typically 1, 2 for larger 225385c9ef5SHeiko Schocher * memories, 0 for register type devices with only one 226385c9ef5SHeiko Schocher * register) 227385c9ef5SHeiko Schocher * buffer: Where to read/write the data 228385c9ef5SHeiko Schocher * len: How many bytes to read/write 229385c9ef5SHeiko Schocher * 230385c9ef5SHeiko Schocher * Returns: 0 on success, not 0 on failure 231385c9ef5SHeiko Schocher */ 232385c9ef5SHeiko Schocher int i2c_read(uint8_t chip, unsigned int addr, int alen, 233385c9ef5SHeiko Schocher uint8_t *buffer, int len); 234385c9ef5SHeiko Schocher 235385c9ef5SHeiko Schocher int i2c_write(uint8_t chip, unsigned int addr, int alen, 236385c9ef5SHeiko Schocher uint8_t *buffer, int len); 237385c9ef5SHeiko Schocher 238385c9ef5SHeiko Schocher /* 239385c9ef5SHeiko Schocher * Utility routines to read/write registers. 240385c9ef5SHeiko Schocher */ 241385c9ef5SHeiko Schocher uint8_t i2c_reg_read(uint8_t addr, uint8_t reg); 242385c9ef5SHeiko Schocher 243385c9ef5SHeiko Schocher void i2c_reg_write(uint8_t addr, uint8_t reg, uint8_t val); 244385c9ef5SHeiko Schocher 245385c9ef5SHeiko Schocher /* 246385c9ef5SHeiko Schocher * i2c_set_bus_speed: 247385c9ef5SHeiko Schocher * 248385c9ef5SHeiko Schocher * Change the speed of the active I2C bus 249385c9ef5SHeiko Schocher * 250385c9ef5SHeiko Schocher * speed - bus speed in Hz 251385c9ef5SHeiko Schocher * 252385c9ef5SHeiko Schocher * Returns: new bus speed 253385c9ef5SHeiko Schocher * 254385c9ef5SHeiko Schocher */ 255385c9ef5SHeiko Schocher unsigned int i2c_set_bus_speed(unsigned int speed); 256385c9ef5SHeiko Schocher 257385c9ef5SHeiko Schocher /* 258385c9ef5SHeiko Schocher * i2c_get_bus_speed: 259385c9ef5SHeiko Schocher * 260385c9ef5SHeiko Schocher * Returns speed of currently active I2C bus in Hz 261385c9ef5SHeiko Schocher */ 262385c9ef5SHeiko Schocher 263385c9ef5SHeiko Schocher unsigned int i2c_get_bus_speed(void); 264385c9ef5SHeiko Schocher 265385c9ef5SHeiko Schocher /* 266385c9ef5SHeiko Schocher * i2c_reloc_fixup: 267385c9ef5SHeiko Schocher * 268385c9ef5SHeiko Schocher * Adjusts I2C pointers after U-Boot is relocated to DRAM 269385c9ef5SHeiko Schocher */ 270385c9ef5SHeiko Schocher void i2c_reloc_fixup(void); 271ea818dbbSHeiko Schocher #if defined(CONFIG_SYS_I2C_SOFT) 272ea818dbbSHeiko Schocher void i2c_soft_init(void); 273ea818dbbSHeiko Schocher void i2c_soft_active(void); 274ea818dbbSHeiko Schocher void i2c_soft_tristate(void); 275ea818dbbSHeiko Schocher int i2c_soft_read(void); 276ea818dbbSHeiko Schocher void i2c_soft_sda(int bit); 277ea818dbbSHeiko Schocher void i2c_soft_scl(int bit); 278ea818dbbSHeiko Schocher void i2c_soft_delay(void); 27967b23a32SHeiko Schocher #endif 280385c9ef5SHeiko Schocher #else 28167b23a32SHeiko Schocher 2821f045217Swdenk /* 2831f045217Swdenk * Probe the given I2C chip address. Returns 0 if a chip responded, 2841f045217Swdenk * not 0 on failure. 2851f045217Swdenk */ 2861f045217Swdenk int i2c_probe(uchar chip); 2871f045217Swdenk 2881f045217Swdenk /* 2891f045217Swdenk * Read/Write interface: 2901f045217Swdenk * chip: I2C chip address, range 0..127 2911f045217Swdenk * addr: Memory (register) address within the chip 2921f045217Swdenk * alen: Number of bytes to use for addr (typically 1, 2 for larger 2931f045217Swdenk * memories, 0 for register type devices with only one 2941f045217Swdenk * register) 2951f045217Swdenk * buffer: Where to read/write the data 2961f045217Swdenk * len: How many bytes to read/write 2971f045217Swdenk * 2981f045217Swdenk * Returns: 0 on success, not 0 on failure 2991f045217Swdenk */ 3001f045217Swdenk int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len); 3011f045217Swdenk int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len); 3021f045217Swdenk 3031f045217Swdenk /* 3041f045217Swdenk * Utility routines to read/write registers. 3051f045217Swdenk */ 306ecf5f077STimur Tabi static inline u8 i2c_reg_read(u8 addr, u8 reg) 307ecf5f077STimur Tabi { 308ecf5f077STimur Tabi u8 buf; 309ecf5f077STimur Tabi 310ecf5f077STimur Tabi #ifdef CONFIG_8xx 311ecf5f077STimur Tabi /* MPC8xx needs this. Maybe one day we can get rid of it. */ 312ecf5f077STimur Tabi i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE); 313ecf5f077STimur Tabi #endif 314ecf5f077STimur Tabi 315ecf5f077STimur Tabi #ifdef DEBUG 316ecf5f077STimur Tabi printf("%s: addr=0x%02x, reg=0x%02x\n", __func__, addr, reg); 317ecf5f077STimur Tabi #endif 318ecf5f077STimur Tabi 319ecf5f077STimur Tabi i2c_read(addr, reg, 1, &buf, 1); 320ecf5f077STimur Tabi 321ecf5f077STimur Tabi return buf; 322ecf5f077STimur Tabi } 323ecf5f077STimur Tabi 324ecf5f077STimur Tabi static inline void i2c_reg_write(u8 addr, u8 reg, u8 val) 325ecf5f077STimur Tabi { 326ecf5f077STimur Tabi #ifdef CONFIG_8xx 327ecf5f077STimur Tabi /* MPC8xx needs this. Maybe one day we can get rid of it. */ 328ecf5f077STimur Tabi i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE); 329ecf5f077STimur Tabi #endif 330ecf5f077STimur Tabi 331ecf5f077STimur Tabi #ifdef DEBUG 332ecf5f077STimur Tabi printf("%s: addr=0x%02x, reg=0x%02x, val=0x%02x\n", 333ecf5f077STimur Tabi __func__, addr, reg, val); 334ecf5f077STimur Tabi #endif 335ecf5f077STimur Tabi 336ecf5f077STimur Tabi i2c_write(addr, reg, 1, &val, 1); 337ecf5f077STimur Tabi } 3381f045217Swdenk 339bb99ad6dSBen Warren /* 340bb99ad6dSBen Warren * Functions for setting the current I2C bus and its speed 341bb99ad6dSBen Warren */ 342bb99ad6dSBen Warren 343bb99ad6dSBen Warren /* 344bb99ad6dSBen Warren * i2c_set_bus_num: 345bb99ad6dSBen Warren * 346bb99ad6dSBen Warren * Change the active I2C bus. Subsequent read/write calls will 347bb99ad6dSBen Warren * go to this one. 348bb99ad6dSBen Warren * 349bb99ad6dSBen Warren * bus - bus index, zero based 350bb99ad6dSBen Warren * 351bb99ad6dSBen Warren * Returns: 0 on success, not 0 on failure 352bb99ad6dSBen Warren * 353bb99ad6dSBen Warren */ 3549ca880a2STimur Tabi int i2c_set_bus_num(unsigned int bus); 355bb99ad6dSBen Warren 356bb99ad6dSBen Warren /* 357bb99ad6dSBen Warren * i2c_get_bus_num: 358bb99ad6dSBen Warren * 359bb99ad6dSBen Warren * Returns index of currently active I2C bus. Zero-based. 360bb99ad6dSBen Warren */ 361bb99ad6dSBen Warren 3629ca880a2STimur Tabi unsigned int i2c_get_bus_num(void); 363bb99ad6dSBen Warren 364bb99ad6dSBen Warren /* 365bb99ad6dSBen Warren * i2c_set_bus_speed: 366bb99ad6dSBen Warren * 367bb99ad6dSBen Warren * Change the speed of the active I2C bus 368bb99ad6dSBen Warren * 369bb99ad6dSBen Warren * speed - bus speed in Hz 370bb99ad6dSBen Warren * 371bb99ad6dSBen Warren * Returns: 0 on success, not 0 on failure 372bb99ad6dSBen Warren * 373bb99ad6dSBen Warren */ 3749ca880a2STimur Tabi int i2c_set_bus_speed(unsigned int); 375bb99ad6dSBen Warren 376bb99ad6dSBen Warren /* 377bb99ad6dSBen Warren * i2c_get_bus_speed: 378bb99ad6dSBen Warren * 379bb99ad6dSBen Warren * Returns speed of currently active I2C bus in Hz 380bb99ad6dSBen Warren */ 381bb99ad6dSBen Warren 3829ca880a2STimur Tabi unsigned int i2c_get_bus_speed(void); 383385c9ef5SHeiko Schocher #endif /* CONFIG_SYS_I2C */ 384385c9ef5SHeiko Schocher 385385c9ef5SHeiko Schocher /* 386385c9ef5SHeiko Schocher * only for backwardcompatibility, should go away if we switched 387385c9ef5SHeiko Schocher * completely to new multibus support. 388385c9ef5SHeiko Schocher */ 389385c9ef5SHeiko Schocher #if defined(CONFIG_SYS_I2C) || defined(CONFIG_I2C_MULTI_BUS) 390385c9ef5SHeiko Schocher # if !defined(CONFIG_SYS_MAX_I2C_BUS) 391385c9ef5SHeiko Schocher # define CONFIG_SYS_MAX_I2C_BUS 2 392385c9ef5SHeiko Schocher # endif 393*ea0f73abSŁukasz Majewski # define I2C_MULTI_BUS 1 394385c9ef5SHeiko Schocher #else 395385c9ef5SHeiko Schocher # define CONFIG_SYS_MAX_I2C_BUS 1 396385c9ef5SHeiko Schocher # define I2C_MULTI_BUS 0 397385c9ef5SHeiko Schocher #endif 398bb99ad6dSBen Warren 399cd7b4e82SMarek Vasut /* NOTE: These two functions MUST be always_inline to avoid code growth! */ 400cd7b4e82SMarek Vasut static inline unsigned int I2C_GET_BUS(void) __attribute__((always_inline)); 401cd7b4e82SMarek Vasut static inline unsigned int I2C_GET_BUS(void) 402cd7b4e82SMarek Vasut { 403cd7b4e82SMarek Vasut return I2C_MULTI_BUS ? i2c_get_bus_num() : 0; 404cd7b4e82SMarek Vasut } 405cd7b4e82SMarek Vasut 406cd7b4e82SMarek Vasut static inline void I2C_SET_BUS(unsigned int bus) __attribute__((always_inline)); 407cd7b4e82SMarek Vasut static inline void I2C_SET_BUS(unsigned int bus) 408cd7b4e82SMarek Vasut { 409cd7b4e82SMarek Vasut if (I2C_MULTI_BUS) 410cd7b4e82SMarek Vasut i2c_set_bus_num(bus); 411cd7b4e82SMarek Vasut } 412cd7b4e82SMarek Vasut 4137ca8f73aSŁukasz Majewski /* Multi I2C definitions */ 4147ca8f73aSŁukasz Majewski enum { 4157ca8f73aSŁukasz Majewski I2C_0, I2C_1, I2C_2, I2C_3, I2C_4, I2C_5, I2C_6, I2C_7, 4167ca8f73aSŁukasz Majewski I2C_8, I2C_9, I2C_10, 4177ca8f73aSŁukasz Majewski }; 4187ca8f73aSŁukasz Majewski 4197ca8f73aSŁukasz Majewski /* Multi I2C busses handling */ 4207ca8f73aSŁukasz Majewski #ifdef CONFIG_SOFT_I2C_MULTI_BUS 4217ca8f73aSŁukasz Majewski extern int get_multi_scl_pin(void); 4227ca8f73aSŁukasz Majewski extern int get_multi_sda_pin(void); 4237ca8f73aSŁukasz Majewski extern int multi_i2c_init(void); 4247ca8f73aSŁukasz Majewski #endif 425a9d2ae70SRajeshwari Shinde 426a9d2ae70SRajeshwari Shinde /** 427a9d2ae70SRajeshwari Shinde * Get FDT values for i2c bus. 428a9d2ae70SRajeshwari Shinde * 429a9d2ae70SRajeshwari Shinde * @param blob Device tree blbo 430a9d2ae70SRajeshwari Shinde * @return the number of I2C bus 431a9d2ae70SRajeshwari Shinde */ 432a9d2ae70SRajeshwari Shinde void board_i2c_init(const void *blob); 433a9d2ae70SRajeshwari Shinde 434a9d2ae70SRajeshwari Shinde /** 435a9d2ae70SRajeshwari Shinde * Find the I2C bus number by given a FDT I2C node. 436a9d2ae70SRajeshwari Shinde * 437a9d2ae70SRajeshwari Shinde * @param blob Device tree blbo 438a9d2ae70SRajeshwari Shinde * @param node FDT I2C node to find 439a9d2ae70SRajeshwari Shinde * @return the number of I2C bus (zero based), or -1 on error 440a9d2ae70SRajeshwari Shinde */ 441a9d2ae70SRajeshwari Shinde int i2c_get_bus_num_fdt(int node); 442a9d2ae70SRajeshwari Shinde 443a9d2ae70SRajeshwari Shinde /** 444a9d2ae70SRajeshwari Shinde * Reset the I2C bus represented by the given a FDT I2C node. 445a9d2ae70SRajeshwari Shinde * 446a9d2ae70SRajeshwari Shinde * @param blob Device tree blbo 447a9d2ae70SRajeshwari Shinde * @param node FDT I2C node to find 448a9d2ae70SRajeshwari Shinde * @return 0 if port was reset, -1 if not found 449a9d2ae70SRajeshwari Shinde */ 450a9d2ae70SRajeshwari Shinde int i2c_reset_port_fdt(const void *blob, int node); 4511f045217Swdenk #endif /* _I2C_H_ */ 452