11f045217Swdenk /* 2*385c9ef5SHeiko Schocher * Copyright (C) 2009 Sergey Kubushyn <ksi@koi8.net> 3*385c9ef5SHeiko Schocher * Copyright (C) 2009 - 2013 Heiko Schocher <hs@denx.de> 4*385c9ef5SHeiko Schocher * Changes for multibus/multiadapter I2C support. 5*385c9ef5SHeiko Schocher * 61f045217Swdenk * (C) Copyright 2001 71f045217Swdenk * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com. 81f045217Swdenk * 91f045217Swdenk * See file CREDITS for list of people who contributed to this 101f045217Swdenk * project. 111f045217Swdenk * 121f045217Swdenk * This program is free software; you can redistribute it and/or 131f045217Swdenk * modify it under the terms of the GNU General Public License as 141f045217Swdenk * published by the Free Software Foundation; either version 2 of 151f045217Swdenk * the License, or (at your option) any later version. 161f045217Swdenk * 171f045217Swdenk * This program is distributed in the hope that it will be useful, 181f045217Swdenk * but WITHOUT ANY WARRANTY; without even the implied warranty of 191f045217Swdenk * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 201f045217Swdenk * GNU General Public License for more details. 211f045217Swdenk * 221f045217Swdenk * You should have received a copy of the GNU General Public License 231f045217Swdenk * along with this program; if not, write to the Free Software 241f045217Swdenk * Foundation, Inc., 59 Temple Place, Suite 330, Boston, 251f045217Swdenk * MA 02111-1307 USA 261f045217Swdenk * 271f045217Swdenk * The original I2C interface was 281f045217Swdenk * (C) 2000 by Paolo Scaffardi (arsenio@tin.it) 291f045217Swdenk * AIRVENT SAM s.p.a - RIMINI(ITALY) 301f045217Swdenk * but has been changed substantially. 311f045217Swdenk */ 321f045217Swdenk 331f045217Swdenk #ifndef _I2C_H_ 341f045217Swdenk #define _I2C_H_ 351f045217Swdenk 361f045217Swdenk /* 371f045217Swdenk * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING 381f045217Swdenk * 391f045217Swdenk * The implementation MUST NOT use static or global variables if the 401f045217Swdenk * I2C routines are used to read SDRAM configuration information 411f045217Swdenk * because this is done before the memories are initialized. Limited 421f045217Swdenk * use of stack-based variables are OK (the initial stack size is 431f045217Swdenk * limited). 441f045217Swdenk * 451f045217Swdenk * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING 461f045217Swdenk */ 471f045217Swdenk 481f045217Swdenk /* 491f045217Swdenk * Configuration items. 501f045217Swdenk */ 511f045217Swdenk #define I2C_RXTX_LEN 128 /* maximum tx/rx buffer length */ 521f045217Swdenk 53*385c9ef5SHeiko Schocher #if !defined(CONFIG_SYS_I2C_MAX_HOPS) 54*385c9ef5SHeiko Schocher /* no muxes used bus = i2c adapters */ 55*385c9ef5SHeiko Schocher #define CONFIG_SYS_I2C_DIRECT_BUS 1 56*385c9ef5SHeiko Schocher #define CONFIG_SYS_I2C_MAX_HOPS 0 57*385c9ef5SHeiko Schocher #define CONFIG_SYS_NUM_I2C_BUSES ll_entry_count(struct i2c_adapter, i2c) 5879b2d0bbSStefan Roese #else 59*385c9ef5SHeiko Schocher /* we use i2c muxes */ 60*385c9ef5SHeiko Schocher #undef CONFIG_SYS_I2C_DIRECT_BUS 6179b2d0bbSStefan Roese #endif 6279b2d0bbSStefan Roese 638c12045aSStefan Roese /* define the I2C bus number for RTC and DTT if not already done */ 646d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #if !defined(CONFIG_SYS_RTC_BUS_NUM) 656d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #define CONFIG_SYS_RTC_BUS_NUM 0 668c12045aSStefan Roese #endif 676d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #if !defined(CONFIG_SYS_DTT_BUS_NUM) 686d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #define CONFIG_SYS_DTT_BUS_NUM 0 698c12045aSStefan Roese #endif 706d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #if !defined(CONFIG_SYS_SPD_BUS_NUM) 716d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #define CONFIG_SYS_SPD_BUS_NUM 0 72d8a8ea5cSMatthias Fuchs #endif 738c12045aSStefan Roese 74*385c9ef5SHeiko Schocher struct i2c_adapter { 75*385c9ef5SHeiko Schocher void (*init)(struct i2c_adapter *adap, int speed, 76*385c9ef5SHeiko Schocher int slaveaddr); 77*385c9ef5SHeiko Schocher int (*probe)(struct i2c_adapter *adap, uint8_t chip); 78*385c9ef5SHeiko Schocher int (*read)(struct i2c_adapter *adap, uint8_t chip, 79*385c9ef5SHeiko Schocher uint addr, int alen, uint8_t *buffer, 80*385c9ef5SHeiko Schocher int len); 81*385c9ef5SHeiko Schocher int (*write)(struct i2c_adapter *adap, uint8_t chip, 82*385c9ef5SHeiko Schocher uint addr, int alen, uint8_t *buffer, 83*385c9ef5SHeiko Schocher int len); 84*385c9ef5SHeiko Schocher uint (*set_bus_speed)(struct i2c_adapter *adap, 85*385c9ef5SHeiko Schocher uint speed); 86*385c9ef5SHeiko Schocher int speed; 87*385c9ef5SHeiko Schocher int slaveaddr; 88*385c9ef5SHeiko Schocher int init_done; 89*385c9ef5SHeiko Schocher int hwadapnr; 90*385c9ef5SHeiko Schocher char *name; 91*385c9ef5SHeiko Schocher }; 92*385c9ef5SHeiko Schocher 93*385c9ef5SHeiko Schocher #define U_BOOT_I2C_MKENT_COMPLETE(_init, _probe, _read, _write, \ 94*385c9ef5SHeiko Schocher _set_speed, _speed, _slaveaddr, _hwadapnr, _name) \ 95*385c9ef5SHeiko Schocher { \ 96*385c9ef5SHeiko Schocher .init = _init, \ 97*385c9ef5SHeiko Schocher .probe = _probe, \ 98*385c9ef5SHeiko Schocher .read = _read, \ 99*385c9ef5SHeiko Schocher .write = _write, \ 100*385c9ef5SHeiko Schocher .set_bus_speed = _set_speed, \ 101*385c9ef5SHeiko Schocher .speed = _speed, \ 102*385c9ef5SHeiko Schocher .slaveaddr = _slaveaddr, \ 103*385c9ef5SHeiko Schocher .init_done = 0, \ 104*385c9ef5SHeiko Schocher .hwadapnr = _hwadapnr, \ 105*385c9ef5SHeiko Schocher .name = #_name \ 106*385c9ef5SHeiko Schocher }; 107*385c9ef5SHeiko Schocher 108*385c9ef5SHeiko Schocher #define U_BOOT_I2C_ADAP_COMPLETE(_name, _init, _probe, _read, _write, \ 109*385c9ef5SHeiko Schocher _set_speed, _speed, _slaveaddr, _hwadapnr) \ 110*385c9ef5SHeiko Schocher ll_entry_declare(struct i2c_adapter, _name, i2c) = \ 111*385c9ef5SHeiko Schocher U_BOOT_I2C_MKENT_COMPLETE(_init, _probe, _read, _write, \ 112*385c9ef5SHeiko Schocher _set_speed, _speed, _slaveaddr, _hwadapnr, _name); 113*385c9ef5SHeiko Schocher 114*385c9ef5SHeiko Schocher struct i2c_adapter *i2c_get_adapter(int index); 115*385c9ef5SHeiko Schocher 116*385c9ef5SHeiko Schocher #ifndef CONFIG_SYS_I2C_DIRECT_BUS 117*385c9ef5SHeiko Schocher struct i2c_mux { 118*385c9ef5SHeiko Schocher int id; 119*385c9ef5SHeiko Schocher char name[16]; 120*385c9ef5SHeiko Schocher }; 121*385c9ef5SHeiko Schocher 122*385c9ef5SHeiko Schocher struct i2c_next_hop { 123*385c9ef5SHeiko Schocher struct i2c_mux mux; 124*385c9ef5SHeiko Schocher uint8_t chip; 125*385c9ef5SHeiko Schocher uint8_t channel; 126*385c9ef5SHeiko Schocher }; 127*385c9ef5SHeiko Schocher 128*385c9ef5SHeiko Schocher struct i2c_bus_hose { 129*385c9ef5SHeiko Schocher int adapter; 130*385c9ef5SHeiko Schocher struct i2c_next_hop next_hop[CONFIG_SYS_I2C_MAX_HOPS]; 131*385c9ef5SHeiko Schocher }; 132*385c9ef5SHeiko Schocher #define I2C_NULL_HOP {{-1, ""}, 0, 0} 133*385c9ef5SHeiko Schocher extern struct i2c_bus_hose i2c_bus[]; 134*385c9ef5SHeiko Schocher 135*385c9ef5SHeiko Schocher #define I2C_ADAPTER(bus) i2c_bus[bus].adapter 136*385c9ef5SHeiko Schocher #else 137*385c9ef5SHeiko Schocher #define I2C_ADAPTER(bus) bus 138*385c9ef5SHeiko Schocher #endif 139*385c9ef5SHeiko Schocher #define I2C_BUS gd->cur_i2c_bus 140*385c9ef5SHeiko Schocher 141*385c9ef5SHeiko Schocher #define I2C_ADAP_NR(bus) i2c_get_adapter(I2C_ADAPTER(bus)) 142*385c9ef5SHeiko Schocher #define I2C_ADAP I2C_ADAP_NR(gd->cur_i2c_bus) 143*385c9ef5SHeiko Schocher #define I2C_ADAP_HWNR (I2C_ADAP->hwadapnr) 144*385c9ef5SHeiko Schocher 145*385c9ef5SHeiko Schocher #ifndef CONFIG_SYS_I2C_DIRECT_BUS 146*385c9ef5SHeiko Schocher #define I2C_MUX_PCA9540_ID 1 147*385c9ef5SHeiko Schocher #define I2C_MUX_PCA9540 {I2C_MUX_PCA9540_ID, "PCA9540B"} 148*385c9ef5SHeiko Schocher #define I2C_MUX_PCA9542_ID 2 149*385c9ef5SHeiko Schocher #define I2C_MUX_PCA9542 {I2C_MUX_PCA9542_ID, "PCA9542A"} 150*385c9ef5SHeiko Schocher #define I2C_MUX_PCA9544_ID 3 151*385c9ef5SHeiko Schocher #define I2C_MUX_PCA9544 {I2C_MUX_PCA9544_ID, "PCA9544A"} 152*385c9ef5SHeiko Schocher #define I2C_MUX_PCA9547_ID 4 153*385c9ef5SHeiko Schocher #define I2C_MUX_PCA9547 {I2C_MUX_PCA9547_ID, "PCA9547A"} 154*385c9ef5SHeiko Schocher #endif 155*385c9ef5SHeiko Schocher 15698aed379SHeiko Schocher #ifndef I2C_SOFT_DECLARATIONS 15798aed379SHeiko Schocher # if defined(CONFIG_MPC8260) 1586d0f6bcfSJean-Christophe PLAGNIOL-VILLARD # define I2C_SOFT_DECLARATIONS volatile ioport_t *iop = ioport_addr((immap_t *)CONFIG_SYS_IMMR, I2C_PORT); 15998aed379SHeiko Schocher # elif defined(CONFIG_8xx) 1606d0f6bcfSJean-Christophe PLAGNIOL-VILLARD # define I2C_SOFT_DECLARATIONS volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR; 1610cf0b931SJens Scharsig 1620cf0b931SJens Scharsig # elif (defined(CONFIG_AT91RM9200) || \ 1630cf0b931SJens Scharsig defined(CONFIG_AT91SAM9260) || defined(CONFIG_AT91SAM9261) || \ 1640cf0b931SJens Scharsig defined(CONFIG_AT91SAM9263)) && !defined(CONFIG_AT91_LEGACY) 16578132275Sesw@bus-elektronik.de # define I2C_SOFT_DECLARATIONS at91_pio_t *pio = (at91_pio_t *) ATMEL_BASE_PIOA; 16698aed379SHeiko Schocher # else 16798aed379SHeiko Schocher # define I2C_SOFT_DECLARATIONS 16898aed379SHeiko Schocher # endif 16998aed379SHeiko Schocher #endif 170ecf5f077STimur Tabi 171ecf5f077STimur Tabi #ifdef CONFIG_8xx 1729c90a2c8SPeter Tyser /* Set default value for the I2C bus speed on 8xx. In the 173ecf5f077STimur Tabi * future, we'll define these in all 8xx board config files. 174ecf5f077STimur Tabi */ 175ecf5f077STimur Tabi #ifndef CONFIG_SYS_I2C_SPEED 176ecf5f077STimur Tabi #define CONFIG_SYS_I2C_SPEED 50000 177ecf5f077STimur Tabi #endif 178ecf5f077STimur Tabi #endif 1799c90a2c8SPeter Tyser 1809c90a2c8SPeter Tyser /* 1819c90a2c8SPeter Tyser * Many boards/controllers/drivers don't support an I2C slave interface so 1829c90a2c8SPeter Tyser * provide a default slave address for them for use in common code. A real 1839c90a2c8SPeter Tyser * value for CONFIG_SYS_I2C_SLAVE should be defined for any board which does 1849c90a2c8SPeter Tyser * support a slave interface. 1859c90a2c8SPeter Tyser */ 1869c90a2c8SPeter Tyser #ifndef CONFIG_SYS_I2C_SLAVE 1879c90a2c8SPeter Tyser #define CONFIG_SYS_I2C_SLAVE 0xfe 188ecf5f077STimur Tabi #endif 189ecf5f077STimur Tabi 1901f045217Swdenk /* 1911f045217Swdenk * Initialization, must be called once on start up, may be called 1921f045217Swdenk * repeatedly to change the speed and slave addresses. 1931f045217Swdenk */ 1941f045217Swdenk void i2c_init(int speed, int slaveaddr); 19506d01dbeSwdenk void i2c_init_board(void); 19626a33504SRichard Retanubun #ifdef CONFIG_SYS_I2C_BOARD_LATE_INIT 19726a33504SRichard Retanubun void i2c_board_late_init(void); 19826a33504SRichard Retanubun #endif 1991f045217Swdenk 20067b23a32SHeiko Schocher #if defined(CONFIG_I2C_MUX) 20167b23a32SHeiko Schocher 20267b23a32SHeiko Schocher typedef struct _mux { 20367b23a32SHeiko Schocher uchar chip; 20467b23a32SHeiko Schocher uchar channel; 20567b23a32SHeiko Schocher char *name; 20667b23a32SHeiko Schocher struct _mux *next; 20767b23a32SHeiko Schocher } I2C_MUX; 20867b23a32SHeiko Schocher 20967b23a32SHeiko Schocher typedef struct _mux_device { 21067b23a32SHeiko Schocher int busid; 21167b23a32SHeiko Schocher I2C_MUX *mux; /* List of muxes, to reach the device */ 21267b23a32SHeiko Schocher struct _mux_device *next; 21367b23a32SHeiko Schocher } I2C_MUX_DEVICE; 21467b23a32SHeiko Schocher 21567b23a32SHeiko Schocher I2C_MUX_DEVICE *i2c_mux_search_device(int id); 21667b23a32SHeiko Schocher I2C_MUX_DEVICE *i2c_mux_ident_muxstring (uchar *buf); 21767b23a32SHeiko Schocher int i2x_mux_select_mux(int bus); 21867b23a32SHeiko Schocher int i2c_mux_ident_muxstring_f (uchar *buf); 21967b23a32SHeiko Schocher #endif 22067b23a32SHeiko Schocher 221*385c9ef5SHeiko Schocher #ifdef CONFIG_SYS_I2C 222*385c9ef5SHeiko Schocher /* 223*385c9ef5SHeiko Schocher * Initialization, must be called once on start up, may be called 224*385c9ef5SHeiko Schocher * repeatedly to change the speed and slave addresses. 225*385c9ef5SHeiko Schocher */ 226*385c9ef5SHeiko Schocher void i2c_init(unsigned int speed, int slaveaddr); 227*385c9ef5SHeiko Schocher #ifdef CONFIG_SYS_I2C_INIT_BOARD 228*385c9ef5SHeiko Schocher void i2c_init_board(void); 229*385c9ef5SHeiko Schocher #endif 230*385c9ef5SHeiko Schocher 231*385c9ef5SHeiko Schocher /* 232*385c9ef5SHeiko Schocher * i2c_get_bus_num: 233*385c9ef5SHeiko Schocher * 234*385c9ef5SHeiko Schocher * Returns index of currently active I2C bus. Zero-based. 235*385c9ef5SHeiko Schocher */ 236*385c9ef5SHeiko Schocher unsigned int i2c_get_bus_num(void); 237*385c9ef5SHeiko Schocher 238*385c9ef5SHeiko Schocher /* 239*385c9ef5SHeiko Schocher * i2c_set_bus_num: 240*385c9ef5SHeiko Schocher * 241*385c9ef5SHeiko Schocher * Change the active I2C bus. Subsequent read/write calls will 242*385c9ef5SHeiko Schocher * go to this one. 243*385c9ef5SHeiko Schocher * 244*385c9ef5SHeiko Schocher * bus - bus index, zero based 245*385c9ef5SHeiko Schocher * 246*385c9ef5SHeiko Schocher * Returns: 0 on success, not 0 on failure 247*385c9ef5SHeiko Schocher * 248*385c9ef5SHeiko Schocher */ 249*385c9ef5SHeiko Schocher int i2c_set_bus_num(unsigned int bus); 250*385c9ef5SHeiko Schocher 251*385c9ef5SHeiko Schocher /* 252*385c9ef5SHeiko Schocher * i2c_init_all(): 253*385c9ef5SHeiko Schocher * 254*385c9ef5SHeiko Schocher * Initializes all I2C adapters in the system. All i2c_adap structures must 255*385c9ef5SHeiko Schocher * be initialized beforehead with function pointers and data, including 256*385c9ef5SHeiko Schocher * speed and slaveaddr. Returns 0 on success, non-0 on failure. 257*385c9ef5SHeiko Schocher */ 258*385c9ef5SHeiko Schocher void i2c_init_all(void); 259*385c9ef5SHeiko Schocher 260*385c9ef5SHeiko Schocher /* 261*385c9ef5SHeiko Schocher * Probe the given I2C chip address. Returns 0 if a chip responded, 262*385c9ef5SHeiko Schocher * not 0 on failure. 263*385c9ef5SHeiko Schocher */ 264*385c9ef5SHeiko Schocher int i2c_probe(uint8_t chip); 265*385c9ef5SHeiko Schocher 266*385c9ef5SHeiko Schocher /* 267*385c9ef5SHeiko Schocher * Read/Write interface: 268*385c9ef5SHeiko Schocher * chip: I2C chip address, range 0..127 269*385c9ef5SHeiko Schocher * addr: Memory (register) address within the chip 270*385c9ef5SHeiko Schocher * alen: Number of bytes to use for addr (typically 1, 2 for larger 271*385c9ef5SHeiko Schocher * memories, 0 for register type devices with only one 272*385c9ef5SHeiko Schocher * register) 273*385c9ef5SHeiko Schocher * buffer: Where to read/write the data 274*385c9ef5SHeiko Schocher * len: How many bytes to read/write 275*385c9ef5SHeiko Schocher * 276*385c9ef5SHeiko Schocher * Returns: 0 on success, not 0 on failure 277*385c9ef5SHeiko Schocher */ 278*385c9ef5SHeiko Schocher int i2c_read(uint8_t chip, unsigned int addr, int alen, 279*385c9ef5SHeiko Schocher uint8_t *buffer, int len); 280*385c9ef5SHeiko Schocher 281*385c9ef5SHeiko Schocher int i2c_write(uint8_t chip, unsigned int addr, int alen, 282*385c9ef5SHeiko Schocher uint8_t *buffer, int len); 283*385c9ef5SHeiko Schocher 284*385c9ef5SHeiko Schocher /* 285*385c9ef5SHeiko Schocher * Utility routines to read/write registers. 286*385c9ef5SHeiko Schocher */ 287*385c9ef5SHeiko Schocher uint8_t i2c_reg_read(uint8_t addr, uint8_t reg); 288*385c9ef5SHeiko Schocher 289*385c9ef5SHeiko Schocher void i2c_reg_write(uint8_t addr, uint8_t reg, uint8_t val); 290*385c9ef5SHeiko Schocher 291*385c9ef5SHeiko Schocher /* 292*385c9ef5SHeiko Schocher * i2c_set_bus_speed: 293*385c9ef5SHeiko Schocher * 294*385c9ef5SHeiko Schocher * Change the speed of the active I2C bus 295*385c9ef5SHeiko Schocher * 296*385c9ef5SHeiko Schocher * speed - bus speed in Hz 297*385c9ef5SHeiko Schocher * 298*385c9ef5SHeiko Schocher * Returns: new bus speed 299*385c9ef5SHeiko Schocher * 300*385c9ef5SHeiko Schocher */ 301*385c9ef5SHeiko Schocher unsigned int i2c_set_bus_speed(unsigned int speed); 302*385c9ef5SHeiko Schocher 303*385c9ef5SHeiko Schocher /* 304*385c9ef5SHeiko Schocher * i2c_get_bus_speed: 305*385c9ef5SHeiko Schocher * 306*385c9ef5SHeiko Schocher * Returns speed of currently active I2C bus in Hz 307*385c9ef5SHeiko Schocher */ 308*385c9ef5SHeiko Schocher 309*385c9ef5SHeiko Schocher unsigned int i2c_get_bus_speed(void); 310*385c9ef5SHeiko Schocher 311*385c9ef5SHeiko Schocher /* 312*385c9ef5SHeiko Schocher * i2c_reloc_fixup: 313*385c9ef5SHeiko Schocher * 314*385c9ef5SHeiko Schocher * Adjusts I2C pointers after U-Boot is relocated to DRAM 315*385c9ef5SHeiko Schocher */ 316*385c9ef5SHeiko Schocher void i2c_reloc_fixup(void); 317*385c9ef5SHeiko Schocher #else 318*385c9ef5SHeiko Schocher 3191f045217Swdenk /* 3201f045217Swdenk * Probe the given I2C chip address. Returns 0 if a chip responded, 3211f045217Swdenk * not 0 on failure. 3221f045217Swdenk */ 3231f045217Swdenk int i2c_probe(uchar chip); 3241f045217Swdenk 3251f045217Swdenk /* 3261f045217Swdenk * Read/Write interface: 3271f045217Swdenk * chip: I2C chip address, range 0..127 3281f045217Swdenk * addr: Memory (register) address within the chip 3291f045217Swdenk * alen: Number of bytes to use for addr (typically 1, 2 for larger 3301f045217Swdenk * memories, 0 for register type devices with only one 3311f045217Swdenk * register) 3321f045217Swdenk * buffer: Where to read/write the data 3331f045217Swdenk * len: How many bytes to read/write 3341f045217Swdenk * 3351f045217Swdenk * Returns: 0 on success, not 0 on failure 3361f045217Swdenk */ 3371f045217Swdenk int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len); 3381f045217Swdenk int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len); 3391f045217Swdenk 3401f045217Swdenk /* 3411f045217Swdenk * Utility routines to read/write registers. 3421f045217Swdenk */ 343ecf5f077STimur Tabi static inline u8 i2c_reg_read(u8 addr, u8 reg) 344ecf5f077STimur Tabi { 345ecf5f077STimur Tabi u8 buf; 346ecf5f077STimur Tabi 347ecf5f077STimur Tabi #ifdef CONFIG_8xx 348ecf5f077STimur Tabi /* MPC8xx needs this. Maybe one day we can get rid of it. */ 349ecf5f077STimur Tabi i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE); 350ecf5f077STimur Tabi #endif 351ecf5f077STimur Tabi 352ecf5f077STimur Tabi #ifdef DEBUG 353ecf5f077STimur Tabi printf("%s: addr=0x%02x, reg=0x%02x\n", __func__, addr, reg); 354ecf5f077STimur Tabi #endif 355ecf5f077STimur Tabi 356ecf5f077STimur Tabi i2c_read(addr, reg, 1, &buf, 1); 357ecf5f077STimur Tabi 358ecf5f077STimur Tabi return buf; 359ecf5f077STimur Tabi } 360ecf5f077STimur Tabi 361ecf5f077STimur Tabi static inline void i2c_reg_write(u8 addr, u8 reg, u8 val) 362ecf5f077STimur Tabi { 363ecf5f077STimur Tabi #ifdef CONFIG_8xx 364ecf5f077STimur Tabi /* MPC8xx needs this. Maybe one day we can get rid of it. */ 365ecf5f077STimur Tabi i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE); 366ecf5f077STimur Tabi #endif 367ecf5f077STimur Tabi 368ecf5f077STimur Tabi #ifdef DEBUG 369ecf5f077STimur Tabi printf("%s: addr=0x%02x, reg=0x%02x, val=0x%02x\n", 370ecf5f077STimur Tabi __func__, addr, reg, val); 371ecf5f077STimur Tabi #endif 372ecf5f077STimur Tabi 373ecf5f077STimur Tabi i2c_write(addr, reg, 1, &val, 1); 374ecf5f077STimur Tabi } 3751f045217Swdenk 376bb99ad6dSBen Warren /* 377bb99ad6dSBen Warren * Functions for setting the current I2C bus and its speed 378bb99ad6dSBen Warren */ 379bb99ad6dSBen Warren 380bb99ad6dSBen Warren /* 381bb99ad6dSBen Warren * i2c_set_bus_num: 382bb99ad6dSBen Warren * 383bb99ad6dSBen Warren * Change the active I2C bus. Subsequent read/write calls will 384bb99ad6dSBen Warren * go to this one. 385bb99ad6dSBen Warren * 386bb99ad6dSBen Warren * bus - bus index, zero based 387bb99ad6dSBen Warren * 388bb99ad6dSBen Warren * Returns: 0 on success, not 0 on failure 389bb99ad6dSBen Warren * 390bb99ad6dSBen Warren */ 3919ca880a2STimur Tabi int i2c_set_bus_num(unsigned int bus); 392bb99ad6dSBen Warren 393bb99ad6dSBen Warren /* 394bb99ad6dSBen Warren * i2c_get_bus_num: 395bb99ad6dSBen Warren * 396bb99ad6dSBen Warren * Returns index of currently active I2C bus. Zero-based. 397bb99ad6dSBen Warren */ 398bb99ad6dSBen Warren 3999ca880a2STimur Tabi unsigned int i2c_get_bus_num(void); 400bb99ad6dSBen Warren 401bb99ad6dSBen Warren /* 402bb99ad6dSBen Warren * i2c_set_bus_speed: 403bb99ad6dSBen Warren * 404bb99ad6dSBen Warren * Change the speed of the active I2C bus 405bb99ad6dSBen Warren * 406bb99ad6dSBen Warren * speed - bus speed in Hz 407bb99ad6dSBen Warren * 408bb99ad6dSBen Warren * Returns: 0 on success, not 0 on failure 409bb99ad6dSBen Warren * 410bb99ad6dSBen Warren */ 4119ca880a2STimur Tabi int i2c_set_bus_speed(unsigned int); 412bb99ad6dSBen Warren 413bb99ad6dSBen Warren /* 414bb99ad6dSBen Warren * i2c_get_bus_speed: 415bb99ad6dSBen Warren * 416bb99ad6dSBen Warren * Returns speed of currently active I2C bus in Hz 417bb99ad6dSBen Warren */ 418bb99ad6dSBen Warren 4199ca880a2STimur Tabi unsigned int i2c_get_bus_speed(void); 420*385c9ef5SHeiko Schocher #endif /* CONFIG_SYS_I2C */ 421*385c9ef5SHeiko Schocher 422*385c9ef5SHeiko Schocher /* 423*385c9ef5SHeiko Schocher * only for backwardcompatibility, should go away if we switched 424*385c9ef5SHeiko Schocher * completely to new multibus support. 425*385c9ef5SHeiko Schocher */ 426*385c9ef5SHeiko Schocher #if defined(CONFIG_SYS_I2C) || defined(CONFIG_I2C_MULTI_BUS) 427*385c9ef5SHeiko Schocher # if !defined(CONFIG_SYS_MAX_I2C_BUS) 428*385c9ef5SHeiko Schocher # define CONFIG_SYS_MAX_I2C_BUS 2 429*385c9ef5SHeiko Schocher # endif 430*385c9ef5SHeiko Schocher # define I2C_MULTI_BUS 0 431*385c9ef5SHeiko Schocher #else 432*385c9ef5SHeiko Schocher # define CONFIG_SYS_MAX_I2C_BUS 1 433*385c9ef5SHeiko Schocher # define I2C_MULTI_BUS 0 434*385c9ef5SHeiko Schocher #endif 435bb99ad6dSBen Warren 436cd7b4e82SMarek Vasut /* NOTE: These two functions MUST be always_inline to avoid code growth! */ 437cd7b4e82SMarek Vasut static inline unsigned int I2C_GET_BUS(void) __attribute__((always_inline)); 438cd7b4e82SMarek Vasut static inline unsigned int I2C_GET_BUS(void) 439cd7b4e82SMarek Vasut { 440cd7b4e82SMarek Vasut return I2C_MULTI_BUS ? i2c_get_bus_num() : 0; 441cd7b4e82SMarek Vasut } 442cd7b4e82SMarek Vasut 443cd7b4e82SMarek Vasut static inline void I2C_SET_BUS(unsigned int bus) __attribute__((always_inline)); 444cd7b4e82SMarek Vasut static inline void I2C_SET_BUS(unsigned int bus) 445cd7b4e82SMarek Vasut { 446cd7b4e82SMarek Vasut if (I2C_MULTI_BUS) 447cd7b4e82SMarek Vasut i2c_set_bus_num(bus); 448cd7b4e82SMarek Vasut } 449cd7b4e82SMarek Vasut 4507ca8f73aSŁukasz Majewski /* Multi I2C definitions */ 4517ca8f73aSŁukasz Majewski enum { 4527ca8f73aSŁukasz Majewski I2C_0, I2C_1, I2C_2, I2C_3, I2C_4, I2C_5, I2C_6, I2C_7, 4537ca8f73aSŁukasz Majewski I2C_8, I2C_9, I2C_10, 4547ca8f73aSŁukasz Majewski }; 4557ca8f73aSŁukasz Majewski 4567ca8f73aSŁukasz Majewski /* Multi I2C busses handling */ 4577ca8f73aSŁukasz Majewski #ifdef CONFIG_SOFT_I2C_MULTI_BUS 4587ca8f73aSŁukasz Majewski extern int get_multi_scl_pin(void); 4597ca8f73aSŁukasz Majewski extern int get_multi_sda_pin(void); 4607ca8f73aSŁukasz Majewski extern int multi_i2c_init(void); 4617ca8f73aSŁukasz Majewski #endif 462a9d2ae70SRajeshwari Shinde 463a9d2ae70SRajeshwari Shinde /** 464a9d2ae70SRajeshwari Shinde * Get FDT values for i2c bus. 465a9d2ae70SRajeshwari Shinde * 466a9d2ae70SRajeshwari Shinde * @param blob Device tree blbo 467a9d2ae70SRajeshwari Shinde * @return the number of I2C bus 468a9d2ae70SRajeshwari Shinde */ 469a9d2ae70SRajeshwari Shinde void board_i2c_init(const void *blob); 470a9d2ae70SRajeshwari Shinde 471a9d2ae70SRajeshwari Shinde /** 472a9d2ae70SRajeshwari Shinde * Find the I2C bus number by given a FDT I2C node. 473a9d2ae70SRajeshwari Shinde * 474a9d2ae70SRajeshwari Shinde * @param blob Device tree blbo 475a9d2ae70SRajeshwari Shinde * @param node FDT I2C node to find 476a9d2ae70SRajeshwari Shinde * @return the number of I2C bus (zero based), or -1 on error 477a9d2ae70SRajeshwari Shinde */ 478a9d2ae70SRajeshwari Shinde int i2c_get_bus_num_fdt(int node); 479a9d2ae70SRajeshwari Shinde 480a9d2ae70SRajeshwari Shinde /** 481a9d2ae70SRajeshwari Shinde * Reset the I2C bus represented by the given a FDT I2C node. 482a9d2ae70SRajeshwari Shinde * 483a9d2ae70SRajeshwari Shinde * @param blob Device tree blbo 484a9d2ae70SRajeshwari Shinde * @param node FDT I2C node to find 485a9d2ae70SRajeshwari Shinde * @return 0 if port was reset, -1 if not found 486a9d2ae70SRajeshwari Shinde */ 487a9d2ae70SRajeshwari Shinde int i2c_reset_port_fdt(const void *blob, int node); 4881f045217Swdenk #endif /* _I2C_H_ */ 489