123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983 |
- #ifndef _I2C_H_
- #define _I2C_H_
- enum dm_i2c_chip_flags {
- DM_I2C_CHIP_10BIT = 1 << 0,
- DM_I2C_CHIP_RD_ADDRESS = 1 << 1,
- DM_I2C_CHIP_WR_ADDRESS = 1 << 2,
- };
- struct udevice;
- struct dm_i2c_chip {
- uint chip_addr;
- uint offset_len;
- uint flags;
- #ifdef CONFIG_SANDBOX
- struct udevice *emul;
- bool test_mode;
- #endif
- };
- struct dm_i2c_bus {
- int speed_hz;
- };
- enum dm_i2c_msg_flags {
- I2C_M_TEN = 0x0010,
- I2C_M_RD = 0x0001,
- I2C_M_STOP = 0x8000,
- I2C_M_NOSTART = 0x4000,
- I2C_M_REV_DIR_ADDR = 0x2000,
- I2C_M_IGNORE_NAK = 0x1000,
- I2C_M_NO_RD_ACK = 0x0800,
- I2C_M_RECV_LEN = 0x0400,
- };
- struct i2c_msg {
- uint addr;
- uint flags;
- uint len;
- u8 *buf;
- };
- struct i2c_msg_list {
- struct i2c_msg *msgs;
- uint nmsgs;
- };
- int dm_i2c_read(struct udevice *dev, uint offset, uint8_t *buffer, int len);
- int dm_i2c_write(struct udevice *dev, uint offset, const uint8_t *buffer,
- int len);
- int dm_i2c_probe(struct udevice *bus, uint chip_addr, uint chip_flags,
- struct udevice **devp);
- int dm_i2c_reg_read(struct udevice *dev, uint offset);
- int dm_i2c_reg_write(struct udevice *dev, uint offset, unsigned int val);
- int dm_i2c_xfer(struct udevice *dev, struct i2c_msg *msg, int nmsgs);
- int dm_i2c_set_bus_speed(struct udevice *bus, unsigned int speed);
- int dm_i2c_get_bus_speed(struct udevice *bus);
- int i2c_set_chip_flags(struct udevice *dev, uint flags);
- int i2c_get_chip_flags(struct udevice *dev, uint *flagsp);
- int i2c_set_chip_offset_len(struct udevice *dev, uint offset_len);
- int i2c_get_chip_offset_len(struct udevice *dev);
- int i2c_deblock(struct udevice *bus);
- #ifdef CONFIG_DM_I2C_COMPAT
- int i2c_probe(uint8_t chip_addr);
- int i2c_read(uint8_t chip_addr, unsigned int addr, int alen, uint8_t *buffer,
- int len);
- int i2c_write(uint8_t chip_addr, unsigned int addr, int alen, uint8_t *buffer,
- int len);
- int i2c_get_bus_num_fdt(int node);
- unsigned int i2c_get_bus_num(void);
- int i2c_set_bus_num(unsigned int bus);
- static inline void I2C_SET_BUS(unsigned int bus)
- {
- i2c_set_bus_num(bus);
- }
- static inline unsigned int I2C_GET_BUS(void)
- {
- return i2c_get_bus_num();
- }
- void i2c_init(int speed, int slaveaddr);
- void board_i2c_init(const void *blob);
- uint8_t i2c_reg_read(uint8_t addr, uint8_t reg);
- void i2c_reg_write(uint8_t addr, uint8_t reg, uint8_t val);
- #endif
- struct dm_i2c_ops {
-
- int (*xfer)(struct udevice *bus, struct i2c_msg *msg, int nmsgs);
-
- int (*probe_chip)(struct udevice *bus, uint chip_addr, uint chip_flags);
-
- int (*set_bus_speed)(struct udevice *bus, unsigned int speed);
-
- int (*get_bus_speed)(struct udevice *bus);
-
- int (*set_flags)(struct udevice *dev, uint flags);
-
- int (*deblock)(struct udevice *bus);
- };
- #define i2c_get_ops(dev) ((struct dm_i2c_ops *)(dev)->driver->ops)
- struct i2c_mux_ops {
-
- int (*select)(struct udevice *mux, struct udevice *bus, uint channel);
-
- int (*deselect)(struct udevice *mux, struct udevice *bus, uint channel);
- };
- #define i2c_mux_get_ops(dev) ((struct i2c_mux_ops *)(dev)->driver->ops)
- int i2c_get_chip(struct udevice *bus, uint chip_addr, uint offset_len,
- struct udevice **devp);
- int i2c_get_chip_for_busnum(int busnum, int chip_addr, uint offset_len,
- struct udevice **devp);
- int i2c_chip_ofdata_to_platdata(const void *blob, int node,
- struct dm_i2c_chip *chip);
- void i2c_dump_msgs(struct i2c_msg *msg, int nmsgs);
- #ifndef CONFIG_DM_I2C
- #define I2C_RXTX_LEN 128
- #if !defined(CONFIG_SYS_I2C_MAX_HOPS)
- #define CONFIG_SYS_I2C_DIRECT_BUS 1
- #define CONFIG_SYS_I2C_MAX_HOPS 0
- #define CONFIG_SYS_NUM_I2C_BUSES ll_entry_count(struct i2c_adapter, i2c)
- #else
- #undef CONFIG_SYS_I2C_DIRECT_BUS
- #endif
- #if !defined(CONFIG_SYS_RTC_BUS_NUM)
- #define CONFIG_SYS_RTC_BUS_NUM 0
- #endif
- #if !defined(CONFIG_SYS_DTT_BUS_NUM)
- #define CONFIG_SYS_DTT_BUS_NUM 0
- #endif
- #if !defined(CONFIG_SYS_SPD_BUS_NUM)
- #define CONFIG_SYS_SPD_BUS_NUM 0
- #endif
- struct i2c_adapter {
- void (*init)(struct i2c_adapter *adap, int speed,
- int slaveaddr);
- int (*probe)(struct i2c_adapter *adap, uint8_t chip);
- int (*read)(struct i2c_adapter *adap, uint8_t chip,
- uint addr, int alen, uint8_t *buffer,
- int len);
- int (*write)(struct i2c_adapter *adap, uint8_t chip,
- uint addr, int alen, uint8_t *buffer,
- int len);
- uint (*set_bus_speed)(struct i2c_adapter *adap,
- uint speed);
- int speed;
- int waitdelay;
- int slaveaddr;
- int init_done;
- int hwadapnr;
- char *name;
- };
- #define U_BOOT_I2C_MKENT_COMPLETE(_init, _probe, _read, _write, \
- _set_speed, _speed, _slaveaddr, _hwadapnr, _name) \
- { \
- .init = _init, \
- .probe = _probe, \
- .read = _read, \
- .write = _write, \
- .set_bus_speed = _set_speed, \
- .speed = _speed, \
- .slaveaddr = _slaveaddr, \
- .init_done = 0, \
- .hwadapnr = _hwadapnr, \
- .name = #_name \
- };
- #define U_BOOT_I2C_ADAP_COMPLETE(_name, _init, _probe, _read, _write, \
- _set_speed, _speed, _slaveaddr, _hwadapnr) \
- ll_entry_declare(struct i2c_adapter, _name, i2c) = \
- U_BOOT_I2C_MKENT_COMPLETE(_init, _probe, _read, _write, \
- _set_speed, _speed, _slaveaddr, _hwadapnr, _name);
- struct i2c_adapter *i2c_get_adapter(int index);
- #ifndef CONFIG_SYS_I2C_DIRECT_BUS
- struct i2c_mux {
- int id;
- char name[16];
- };
- struct i2c_next_hop {
- struct i2c_mux mux;
- uint8_t chip;
- uint8_t channel;
- };
- struct i2c_bus_hose {
- int adapter;
- struct i2c_next_hop next_hop[CONFIG_SYS_I2C_MAX_HOPS];
- };
- #define I2C_NULL_HOP {{-1, ""}, 0, 0}
- extern struct i2c_bus_hose i2c_bus[];
- #define I2C_ADAPTER(bus) i2c_bus[bus].adapter
- #else
- #define I2C_ADAPTER(bus) bus
- #endif
- #define I2C_BUS gd->cur_i2c_bus
- #define I2C_ADAP_NR(bus) i2c_get_adapter(I2C_ADAPTER(bus))
- #define I2C_ADAP I2C_ADAP_NR(gd->cur_i2c_bus)
- #define I2C_ADAP_HWNR (I2C_ADAP->hwadapnr)
- #ifndef CONFIG_SYS_I2C_DIRECT_BUS
- #define I2C_MUX_PCA9540_ID 1
- #define I2C_MUX_PCA9540 {I2C_MUX_PCA9540_ID, "PCA9540B"}
- #define I2C_MUX_PCA9542_ID 2
- #define I2C_MUX_PCA9542 {I2C_MUX_PCA9542_ID, "PCA9542A"}
- #define I2C_MUX_PCA9544_ID 3
- #define I2C_MUX_PCA9544 {I2C_MUX_PCA9544_ID, "PCA9544A"}
- #define I2C_MUX_PCA9547_ID 4
- #define I2C_MUX_PCA9547 {I2C_MUX_PCA9547_ID, "PCA9547A"}
- #define I2C_MUX_PCA9548_ID 5
- #define I2C_MUX_PCA9548 {I2C_MUX_PCA9548_ID, "PCA9548"}
- #endif
- #ifndef I2C_SOFT_DECLARATIONS
- # if defined(CONFIG_MPC8260)
- # define I2C_SOFT_DECLARATIONS volatile ioport_t *iop = ioport_addr((immap_t *)CONFIG_SYS_IMMR, I2C_PORT);
- # elif defined(CONFIG_8xx)
- # define I2C_SOFT_DECLARATIONS volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
- # elif (defined(CONFIG_AT91RM9200) || \
- defined(CONFIG_AT91SAM9260) || defined(CONFIG_AT91SAM9261) || \
- defined(CONFIG_AT91SAM9263))
- # define I2C_SOFT_DECLARATIONS at91_pio_t *pio = (at91_pio_t *) ATMEL_BASE_PIOA;
- # else
- # define I2C_SOFT_DECLARATIONS
- # endif
- #endif
- #ifdef CONFIG_8xx
- #ifndef CONFIG_SYS_I2C_SPEED
- #define CONFIG_SYS_I2C_SPEED 50000
- #endif
- #endif
- #ifndef CONFIG_SYS_I2C_SLAVE
- #define CONFIG_SYS_I2C_SLAVE 0xfe
- #endif
- #ifdef CONFIG_SYS_I2C_EARLY_INIT
- void i2c_early_init_f(void);
- #endif
- void i2c_init(int speed, int slaveaddr);
- void i2c_init_board(void);
- #ifdef CONFIG_SYS_I2C_BOARD_LATE_INIT
- void i2c_board_late_init(void);
- #endif
- #ifdef CONFIG_SYS_I2C
- unsigned int i2c_get_bus_num(void);
- int i2c_set_bus_num(unsigned int bus);
- void i2c_init_all(void);
- int i2c_probe(uint8_t chip);
- int i2c_read(uint8_t chip, unsigned int addr, int alen,
- uint8_t *buffer, int len);
- int i2c_write(uint8_t chip, unsigned int addr, int alen,
- uint8_t *buffer, int len);
- uint8_t i2c_reg_read(uint8_t addr, uint8_t reg);
- void i2c_reg_write(uint8_t addr, uint8_t reg, uint8_t val);
- unsigned int i2c_set_bus_speed(unsigned int speed);
- unsigned int i2c_get_bus_speed(void);
- void i2c_reloc_fixup(void);
- #if defined(CONFIG_SYS_I2C_SOFT)
- void i2c_soft_init(void);
- void i2c_soft_active(void);
- void i2c_soft_tristate(void);
- int i2c_soft_read(void);
- void i2c_soft_sda(int bit);
- void i2c_soft_scl(int bit);
- void i2c_soft_delay(void);
- #endif
- #else
- int i2c_probe(uchar chip);
- int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len);
- int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len);
- static inline u8 i2c_reg_read(u8 addr, u8 reg)
- {
- u8 buf;
- #ifdef CONFIG_8xx
-
- i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
- #endif
- #ifdef DEBUG
- printf("%s: addr=0x%02x, reg=0x%02x\n", __func__, addr, reg);
- #endif
- i2c_read(addr, reg, 1, &buf, 1);
- return buf;
- }
- static inline void i2c_reg_write(u8 addr, u8 reg, u8 val)
- {
- #ifdef CONFIG_8xx
-
- i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
- #endif
- #ifdef DEBUG
- printf("%s: addr=0x%02x, reg=0x%02x, val=0x%02x\n",
- __func__, addr, reg, val);
- #endif
- i2c_write(addr, reg, 1, &val, 1);
- }
- int i2c_set_bus_num(unsigned int bus);
- unsigned int i2c_get_bus_num(void);
- int i2c_set_bus_speed(unsigned int);
- unsigned int i2c_get_bus_speed(void);
- #endif
- #if defined(CONFIG_SYS_I2C) || defined(CONFIG_I2C_MULTI_BUS)
- # if !defined(CONFIG_SYS_MAX_I2C_BUS)
- # define CONFIG_SYS_MAX_I2C_BUS 2
- # endif
- # define I2C_MULTI_BUS 1
- #else
- # define CONFIG_SYS_MAX_I2C_BUS 1
- # define I2C_MULTI_BUS 0
- #endif
- static inline unsigned int I2C_GET_BUS(void) __attribute__((always_inline));
- static inline unsigned int I2C_GET_BUS(void)
- {
- return I2C_MULTI_BUS ? i2c_get_bus_num() : 0;
- }
- static inline void I2C_SET_BUS(unsigned int bus) __attribute__((always_inline));
- static inline void I2C_SET_BUS(unsigned int bus)
- {
- if (I2C_MULTI_BUS)
- i2c_set_bus_num(bus);
- }
- enum {
- I2C_0, I2C_1, I2C_2, I2C_3, I2C_4, I2C_5, I2C_6, I2C_7,
- I2C_8, I2C_9, I2C_10,
- };
- #ifdef CONFIG_SOFT_I2C_MULTI_BUS
- extern int get_multi_scl_pin(void);
- extern int get_multi_sda_pin(void);
- extern int multi_i2c_init(void);
- #endif
- void board_i2c_init(const void *blob);
- int i2c_get_bus_num_fdt(int node);
- int i2c_reset_port_fdt(const void *blob, int node);
- #endif
- #endif
|