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 /* 21c6202d85SSimon Glass * For now there are essentially two parts to this file - driver model 22c6202d85SSimon Glass * here at the top, and the older code below (with CONFIG_SYS_I2C being 23c6202d85SSimon Glass * most recent). The plan is to migrate everything to driver model. 24c6202d85SSimon Glass * The driver model structures and API are separate as they are different 25c6202d85SSimon Glass * enough as to be incompatible for compilation purposes. 26c6202d85SSimon Glass */ 27c6202d85SSimon Glass 28c6202d85SSimon Glass enum dm_i2c_chip_flags { 29c6202d85SSimon Glass DM_I2C_CHIP_10BIT = 1 << 0, /* Use 10-bit addressing */ 30c6202d85SSimon Glass DM_I2C_CHIP_RD_ADDRESS = 1 << 1, /* Send address for each read byte */ 31c6202d85SSimon Glass DM_I2C_CHIP_WR_ADDRESS = 1 << 2, /* Send address for each write byte */ 32c6202d85SSimon Glass }; 33c6202d85SSimon Glass 34fffff726SSimon Glass struct udevice; 35c6202d85SSimon Glass /** 36c6202d85SSimon Glass * struct dm_i2c_chip - information about an i2c chip 37c6202d85SSimon Glass * 38c6202d85SSimon Glass * An I2C chip is a device on the I2C bus. It sits at a particular address 39c6202d85SSimon Glass * and normally supports 7-bit or 10-bit addressing. 40c6202d85SSimon Glass * 41e6f66ec0SSimon Glass * To obtain this structure, use dev_get_parent_platdata(dev) where dev is 42e6f66ec0SSimon Glass * the chip to examine. 43c6202d85SSimon Glass * 44c6202d85SSimon Glass * @chip_addr: Chip address on bus 45c6202d85SSimon Glass * @offset_len: Length of offset in bytes. A single byte offset can 46c6202d85SSimon Glass * represent up to 256 bytes. A value larger than 1 may be 47c6202d85SSimon Glass * needed for larger devices. 48c6202d85SSimon Glass * @flags: Flags for this chip (dm_i2c_chip_flags) 49c6202d85SSimon Glass * @emul: Emulator for this chip address (only used for emulation) 50c6202d85SSimon Glass */ 51c6202d85SSimon Glass struct dm_i2c_chip { 52c6202d85SSimon Glass uint chip_addr; 53c6202d85SSimon Glass uint offset_len; 54c6202d85SSimon Glass uint flags; 55c6202d85SSimon Glass #ifdef CONFIG_SANDBOX 56c6202d85SSimon Glass struct udevice *emul; 57182bf92dSSimon Glass bool test_mode; 58c6202d85SSimon Glass #endif 59c6202d85SSimon Glass }; 60c6202d85SSimon Glass 61c6202d85SSimon Glass /** 62c6202d85SSimon Glass * struct dm_i2c_bus- information about an i2c bus 63c6202d85SSimon Glass * 64c6202d85SSimon Glass * An I2C bus contains 0 or more chips on it, each at its own address. The 65c6202d85SSimon Glass * bus can operate at different speeds (measured in Hz, typically 100KHz 66c6202d85SSimon Glass * or 400KHz). 67c6202d85SSimon Glass * 68e564f054SSimon Glass * To obtain this structure, use dev_get_uclass_priv(bus) where bus is the 69e564f054SSimon Glass * I2C bus udevice. 70c6202d85SSimon Glass * 71c6202d85SSimon Glass * @speed_hz: Bus speed in hertz (typically 100000) 72c6202d85SSimon Glass */ 73c6202d85SSimon Glass struct dm_i2c_bus { 74c6202d85SSimon Glass int speed_hz; 75c6202d85SSimon Glass }; 76c6202d85SSimon Glass 777fc65bcfSSimon Glass /* 787fc65bcfSSimon Glass * Not all of these flags are implemented in the U-Boot API 797fc65bcfSSimon Glass */ 807fc65bcfSSimon Glass enum dm_i2c_msg_flags { 817fc65bcfSSimon Glass I2C_M_TEN = 0x0010, /* ten-bit chip address */ 827fc65bcfSSimon Glass I2C_M_RD = 0x0001, /* read data, from slave to master */ 837fc65bcfSSimon Glass I2C_M_STOP = 0x8000, /* send stop after this message */ 847fc65bcfSSimon Glass I2C_M_NOSTART = 0x4000, /* no start before this message */ 857fc65bcfSSimon Glass I2C_M_REV_DIR_ADDR = 0x2000, /* invert polarity of R/W bit */ 867fc65bcfSSimon Glass I2C_M_IGNORE_NAK = 0x1000, /* continue after NAK */ 877fc65bcfSSimon Glass I2C_M_NO_RD_ACK = 0x0800, /* skip the Ack bit on reads */ 887fc65bcfSSimon Glass I2C_M_RECV_LEN = 0x0400, /* length is first received byte */ 897fc65bcfSSimon Glass }; 907fc65bcfSSimon Glass 917fc65bcfSSimon Glass /** 927fc65bcfSSimon Glass * struct i2c_msg - an I2C message 937fc65bcfSSimon Glass * 947fc65bcfSSimon Glass * @addr: Slave address 957fc65bcfSSimon Glass * @flags: Flags (see enum dm_i2c_msg_flags) 967fc65bcfSSimon Glass * @len: Length of buffer in bytes, may be 0 for a probe 977fc65bcfSSimon Glass * @buf: Buffer to send/receive, or NULL if no data 987fc65bcfSSimon Glass */ 997fc65bcfSSimon Glass struct i2c_msg { 1007fc65bcfSSimon Glass uint addr; 1017fc65bcfSSimon Glass uint flags; 1027fc65bcfSSimon Glass uint len; 1037fc65bcfSSimon Glass u8 *buf; 1047fc65bcfSSimon Glass }; 1057fc65bcfSSimon Glass 1067fc65bcfSSimon Glass /** 1077fc65bcfSSimon Glass * struct i2c_msg_list - a list of I2C messages 1087fc65bcfSSimon Glass * 1097fc65bcfSSimon Glass * This is called i2c_rdwr_ioctl_data in Linux but the name does not seem 1107fc65bcfSSimon Glass * appropriate in U-Boot. 1117fc65bcfSSimon Glass * 1127fc65bcfSSimon Glass * @msg: Pointer to i2c_msg array 1137fc65bcfSSimon Glass * @nmsgs: Number of elements in the array 1147fc65bcfSSimon Glass */ 1157fc65bcfSSimon Glass struct i2c_msg_list { 1167fc65bcfSSimon Glass struct i2c_msg *msgs; 1177fc65bcfSSimon Glass uint nmsgs; 1187fc65bcfSSimon Glass }; 1197fc65bcfSSimon Glass 120c6202d85SSimon Glass /** 121f9a4c2daSSimon Glass * dm_i2c_read() - read bytes from an I2C chip 122c6202d85SSimon Glass * 123c6202d85SSimon Glass * To obtain an I2C device (called a 'chip') given the I2C bus address you 124c6202d85SSimon Glass * can use i2c_get_chip(). To obtain a bus by bus number use 125c6202d85SSimon Glass * uclass_get_device_by_seq(UCLASS_I2C, <bus number>). 126c6202d85SSimon Glass * 127c6202d85SSimon Glass * To set the address length of a devce use i2c_set_addr_len(). It 128c6202d85SSimon Glass * defaults to 1. 129c6202d85SSimon Glass * 130c6202d85SSimon Glass * @dev: Chip to read from 131c6202d85SSimon Glass * @offset: Offset within chip to start reading 132c6202d85SSimon Glass * @buffer: Place to put data 133c6202d85SSimon Glass * @len: Number of bytes to read 134c6202d85SSimon Glass * 135c6202d85SSimon Glass * @return 0 on success, -ve on failure 136c6202d85SSimon Glass */ 137f9a4c2daSSimon Glass int dm_i2c_read(struct udevice *dev, uint offset, uint8_t *buffer, int len); 138c6202d85SSimon Glass 139c6202d85SSimon Glass /** 140f9a4c2daSSimon Glass * dm_i2c_write() - write bytes to an I2C chip 141c6202d85SSimon Glass * 142f9a4c2daSSimon Glass * See notes for dm_i2c_read() above. 143c6202d85SSimon Glass * 144c6202d85SSimon Glass * @dev: Chip to write to 145c6202d85SSimon Glass * @offset: Offset within chip to start writing 146c6202d85SSimon Glass * @buffer: Buffer containing data to write 147c6202d85SSimon Glass * @len: Number of bytes to write 148c6202d85SSimon Glass * 149c6202d85SSimon Glass * @return 0 on success, -ve on failure 150c6202d85SSimon Glass */ 151f9a4c2daSSimon Glass int dm_i2c_write(struct udevice *dev, uint offset, const uint8_t *buffer, 152c6202d85SSimon Glass int len); 153c6202d85SSimon Glass 154c6202d85SSimon Glass /** 155f9a4c2daSSimon Glass * dm_i2c_probe() - probe a particular chip address 156c6202d85SSimon Glass * 157c6202d85SSimon Glass * This can be useful to check for the existence of a chip on the bus. 158c6202d85SSimon Glass * It is typically implemented by writing the chip address to the bus 159c6202d85SSimon Glass * and checking that the chip replies with an ACK. 160c6202d85SSimon Glass * 161c6202d85SSimon Glass * @bus: Bus to probe 162c6202d85SSimon Glass * @chip_addr: 7-bit address to probe (10-bit and others are not supported) 163c6202d85SSimon Glass * @chip_flags: Flags for the probe (see enum dm_i2c_chip_flags) 164c6202d85SSimon Glass * @devp: Returns the device found, or NULL if none 165c6202d85SSimon Glass * @return 0 if a chip was found at that address, -ve if not 166c6202d85SSimon Glass */ 167f9a4c2daSSimon Glass int dm_i2c_probe(struct udevice *bus, uint chip_addr, uint chip_flags, 168c6202d85SSimon Glass struct udevice **devp); 169c6202d85SSimon Glass 170c6202d85SSimon Glass /** 171ba3864f8SSimon Glass * dm_i2c_reg_read() - Read a value from an I2C register 172ba3864f8SSimon Glass * 173ba3864f8SSimon Glass * This reads a single value from the given address in an I2C chip 174ba3864f8SSimon Glass * 17525a0fb43SSimon Glass * @dev: Device to use for transfer 176ba3864f8SSimon Glass * @addr: Address to read from 177ba3864f8SSimon Glass * @return value read, or -ve on error 178ba3864f8SSimon Glass */ 179ba3864f8SSimon Glass int dm_i2c_reg_read(struct udevice *dev, uint offset); 180ba3864f8SSimon Glass 181ba3864f8SSimon Glass /** 182ba3864f8SSimon Glass * dm_i2c_reg_write() - Write a value to an I2C register 183ba3864f8SSimon Glass * 184ba3864f8SSimon Glass * This writes a single value to the given address in an I2C chip 185ba3864f8SSimon Glass * 18625a0fb43SSimon Glass * @dev: Device to use for transfer 187ba3864f8SSimon Glass * @addr: Address to write to 188ba3864f8SSimon Glass * @val: Value to write (normally a byte) 189ba3864f8SSimon Glass * @return 0 on success, -ve on error 190ba3864f8SSimon Glass */ 191ba3864f8SSimon Glass int dm_i2c_reg_write(struct udevice *dev, uint offset, unsigned int val); 192ba3864f8SSimon Glass 193ba3864f8SSimon Glass /** 194*df358c6bSSimon Glass * dm_i2c_xfer() - Transfer messages over I2C 195*df358c6bSSimon Glass * 196*df358c6bSSimon Glass * This transfers a raw message. It is best to use dm_i2c_reg_read/write() 197*df358c6bSSimon Glass * instead. 198*df358c6bSSimon Glass * 199*df358c6bSSimon Glass * @dev: Device to use for transfer 200*df358c6bSSimon Glass * @msg: List of messages to transfer 201*df358c6bSSimon Glass * @nmsgs: Number of messages to transfer 202*df358c6bSSimon Glass * @return 0 on success, -ve on error 203*df358c6bSSimon Glass */ 204*df358c6bSSimon Glass int dm_i2c_xfer(struct udevice *dev, struct i2c_msg *msg, int nmsgs); 205*df358c6bSSimon Glass 206*df358c6bSSimon Glass /** 207ca88b9b9SSimon Glass * dm_i2c_set_bus_speed() - set the speed of a bus 208c6202d85SSimon Glass * 209c6202d85SSimon Glass * @bus: Bus to adjust 210c6202d85SSimon Glass * @speed: Requested speed in Hz 211c6202d85SSimon Glass * @return 0 if OK, -EINVAL for invalid values 212c6202d85SSimon Glass */ 213ca88b9b9SSimon Glass int dm_i2c_set_bus_speed(struct udevice *bus, unsigned int speed); 214c6202d85SSimon Glass 215c6202d85SSimon Glass /** 216ca88b9b9SSimon Glass * dm_i2c_get_bus_speed() - get the speed of a bus 217c6202d85SSimon Glass * 218c6202d85SSimon Glass * @bus: Bus to check 219c6202d85SSimon Glass * @return speed of selected I2C bus in Hz, -ve on error 220c6202d85SSimon Glass */ 221ca88b9b9SSimon Glass int dm_i2c_get_bus_speed(struct udevice *bus); 222c6202d85SSimon Glass 223c6202d85SSimon Glass /** 224c6202d85SSimon Glass * i2c_set_chip_flags() - set flags for a chip 225c6202d85SSimon Glass * 226c6202d85SSimon Glass * Typically addresses are 7 bits, but for 10-bit addresses you should set 227c6202d85SSimon Glass * flags to DM_I2C_CHIP_10BIT. All accesses will then use 10-bit addressing. 228c6202d85SSimon Glass * 229c6202d85SSimon Glass * @dev: Chip to adjust 230c6202d85SSimon Glass * @flags: New flags 231c6202d85SSimon Glass * @return 0 if OK, -EINVAL if value is unsupported, other -ve value on error 232c6202d85SSimon Glass */ 233c6202d85SSimon Glass int i2c_set_chip_flags(struct udevice *dev, uint flags); 234c6202d85SSimon Glass 235c6202d85SSimon Glass /** 236c6202d85SSimon Glass * i2c_get_chip_flags() - get flags for a chip 237c6202d85SSimon Glass * 238c6202d85SSimon Glass * @dev: Chip to check 239c6202d85SSimon Glass * @flagsp: Place to put flags 240c6202d85SSimon Glass * @return 0 if OK, other -ve value on error 241c6202d85SSimon Glass */ 242c6202d85SSimon Glass int i2c_get_chip_flags(struct udevice *dev, uint *flagsp); 243c6202d85SSimon Glass 244c6202d85SSimon Glass /** 245c6202d85SSimon Glass * i2c_set_offset_len() - set the offset length for a chip 246c6202d85SSimon Glass * 247c6202d85SSimon Glass * The offset used to access a chip may be up to 4 bytes long. Typically it 248c6202d85SSimon Glass * is only 1 byte, which is enough for chips with 256 bytes of memory or 249c6202d85SSimon Glass * registers. The default value is 1, but you can call this function to 250c6202d85SSimon Glass * change it. 251c6202d85SSimon Glass * 252c6202d85SSimon Glass * @offset_len: New offset length value (typically 1 or 2) 253c6202d85SSimon Glass */ 254c6202d85SSimon Glass int i2c_set_chip_offset_len(struct udevice *dev, uint offset_len); 25501501804SSimon Glass 25601501804SSimon Glass /** 25701501804SSimon Glass * i2c_get_offset_len() - get the offset length for a chip 25801501804SSimon Glass * 25901501804SSimon Glass * @return: Current offset length value (typically 1 or 2) 26001501804SSimon Glass */ 26101501804SSimon Glass int i2c_get_chip_offset_len(struct udevice *dev); 26201501804SSimon Glass 263c6202d85SSimon Glass /** 264c6202d85SSimon Glass * i2c_deblock() - recover a bus that is in an unknown state 265c6202d85SSimon Glass * 266c6202d85SSimon Glass * See the deblock() method in 'struct dm_i2c_ops' for full information 267c6202d85SSimon Glass * 268c6202d85SSimon Glass * @bus: Bus to recover 269c6202d85SSimon Glass * @return 0 if OK, -ve on error 270c6202d85SSimon Glass */ 271c6202d85SSimon Glass int i2c_deblock(struct udevice *bus); 272c6202d85SSimon Glass 27373845350SSimon Glass #ifdef CONFIG_DM_I2C_COMPAT 27473845350SSimon Glass /** 27573845350SSimon Glass * i2c_probe() - Compatibility function for driver model 27673845350SSimon Glass * 27773845350SSimon Glass * Calls dm_i2c_probe() on the current bus 27873845350SSimon Glass */ 27973845350SSimon Glass int i2c_probe(uint8_t chip_addr); 28073845350SSimon Glass 28173845350SSimon Glass /** 28273845350SSimon Glass * i2c_read() - Compatibility function for driver model 28373845350SSimon Glass * 28473845350SSimon Glass * Calls dm_i2c_read() with the device corresponding to @chip_addr, and offset 28573845350SSimon Glass * set to @addr. @alen must match the current setting for the device. 28673845350SSimon Glass */ 28773845350SSimon Glass int i2c_read(uint8_t chip_addr, unsigned int addr, int alen, uint8_t *buffer, 28873845350SSimon Glass int len); 28973845350SSimon Glass 29073845350SSimon Glass /** 29173845350SSimon Glass * i2c_write() - Compatibility function for driver model 29273845350SSimon Glass * 29373845350SSimon Glass * Calls dm_i2c_write() with the device corresponding to @chip_addr, and offset 29473845350SSimon Glass * set to @addr. @alen must match the current setting for the device. 29573845350SSimon Glass */ 29673845350SSimon Glass int i2c_write(uint8_t chip_addr, unsigned int addr, int alen, uint8_t *buffer, 29773845350SSimon Glass int len); 29873845350SSimon Glass 29973845350SSimon Glass /** 30073845350SSimon Glass * i2c_get_bus_num_fdt() - Compatibility function for driver model 30173845350SSimon Glass * 30273845350SSimon Glass * @return the bus number associated with the given device tree node 30373845350SSimon Glass */ 30473845350SSimon Glass int i2c_get_bus_num_fdt(int node); 30573845350SSimon Glass 30673845350SSimon Glass /** 30773845350SSimon Glass * i2c_get_bus_num() - Compatibility function for driver model 30873845350SSimon Glass * 30973845350SSimon Glass * @return the 'current' bus number 31073845350SSimon Glass */ 31173845350SSimon Glass unsigned int i2c_get_bus_num(void); 31273845350SSimon Glass 31373845350SSimon Glass /** 314d744d561SSimon Glass * i2c_set_bus_num() - Compatibility function for driver model 31573845350SSimon Glass * 31673845350SSimon Glass * Sets the 'current' bus 31773845350SSimon Glass */ 31873845350SSimon Glass int i2c_set_bus_num(unsigned int bus); 31973845350SSimon Glass 32073845350SSimon Glass static inline void I2C_SET_BUS(unsigned int bus) 32173845350SSimon Glass { 32273845350SSimon Glass i2c_set_bus_num(bus); 32373845350SSimon Glass } 32473845350SSimon Glass 32573845350SSimon Glass static inline unsigned int I2C_GET_BUS(void) 32673845350SSimon Glass { 32773845350SSimon Glass return i2c_get_bus_num(); 32873845350SSimon Glass } 32973845350SSimon Glass 330d744d561SSimon Glass /** 331d744d561SSimon Glass * i2c_init() - Compatibility function for driver model 332d744d561SSimon Glass * 333d744d561SSimon Glass * This function does nothing. 334d744d561SSimon Glass */ 335d744d561SSimon Glass void i2c_init(int speed, int slaveaddr); 336d744d561SSimon Glass 337d744d561SSimon Glass /** 338d744d561SSimon Glass * board_i2c_init() - Compatibility function for driver model 339d744d561SSimon Glass * 340d744d561SSimon Glass * @param blob Device tree blbo 341d744d561SSimon Glass * @return the number of I2C bus 342d744d561SSimon Glass */ 343d744d561SSimon Glass void board_i2c_init(const void *blob); 344d744d561SSimon Glass 345a2879764SSimon Glass /* 346a2879764SSimon Glass * Compatibility functions for driver model. 347a2879764SSimon Glass */ 348a2879764SSimon Glass uint8_t i2c_reg_read(uint8_t addr, uint8_t reg); 349a2879764SSimon Glass void i2c_reg_write(uint8_t addr, uint8_t reg, uint8_t val); 350a2879764SSimon Glass 35173845350SSimon Glass #endif 35273845350SSimon Glass 353c6202d85SSimon Glass /** 354c6202d85SSimon Glass * struct dm_i2c_ops - driver operations for I2C uclass 355c6202d85SSimon Glass * 356c6202d85SSimon Glass * Drivers should support these operations unless otherwise noted. These 357c6202d85SSimon Glass * operations are intended to be used by uclass code, not directly from 358c6202d85SSimon Glass * other code. 359c6202d85SSimon Glass */ 360c6202d85SSimon Glass struct dm_i2c_ops { 361c6202d85SSimon Glass /** 362c6202d85SSimon Glass * xfer() - transfer a list of I2C messages 363c6202d85SSimon Glass * 364c6202d85SSimon Glass * @bus: Bus to read from 365c6202d85SSimon Glass * @msg: List of messages to transfer 366c6202d85SSimon Glass * @nmsgs: Number of messages in the list 367c6202d85SSimon Glass * @return 0 if OK, -EREMOTEIO if the slave did not ACK a byte, 368c6202d85SSimon Glass * -ECOMM if the speed cannot be supported, -EPROTO if the chip 369c6202d85SSimon Glass * flags cannot be supported, other -ve value on some other error 370c6202d85SSimon Glass */ 371c6202d85SSimon Glass int (*xfer)(struct udevice *bus, struct i2c_msg *msg, int nmsgs); 372c6202d85SSimon Glass 373c6202d85SSimon Glass /** 374c6202d85SSimon Glass * probe_chip() - probe for the presense of a chip address 375c6202d85SSimon Glass * 376c6202d85SSimon Glass * This function is optional. If omitted, the uclass will send a zero 377c6202d85SSimon Glass * length message instead. 378c6202d85SSimon Glass * 379c6202d85SSimon Glass * @bus: Bus to probe 380c6202d85SSimon Glass * @chip_addr: Chip address to probe 381c6202d85SSimon Glass * @chip_flags: Probe flags (enum dm_i2c_chip_flags) 382c6202d85SSimon Glass * @return 0 if chip was found, -EREMOTEIO if not, -ENOSYS to fall back 383c6202d85SSimon Glass * to default probem other -ve value on error 384c6202d85SSimon Glass */ 385c6202d85SSimon Glass int (*probe_chip)(struct udevice *bus, uint chip_addr, uint chip_flags); 386c6202d85SSimon Glass 387c6202d85SSimon Glass /** 388c6202d85SSimon Glass * set_bus_speed() - set the speed of a bus (optional) 389c6202d85SSimon Glass * 390c6202d85SSimon Glass * The bus speed value will be updated by the uclass if this function 391c6202d85SSimon Glass * does not return an error. This method is optional - if it is not 392c6202d85SSimon Glass * provided then the driver can read the speed from 393e564f054SSimon Glass * dev_get_uclass_priv(bus)->speed_hz 394c6202d85SSimon Glass * 395c6202d85SSimon Glass * @bus: Bus to adjust 396c6202d85SSimon Glass * @speed: Requested speed in Hz 397c6202d85SSimon Glass * @return 0 if OK, -EINVAL for invalid values 398c6202d85SSimon Glass */ 399c6202d85SSimon Glass int (*set_bus_speed)(struct udevice *bus, unsigned int speed); 400c6202d85SSimon Glass 401c6202d85SSimon Glass /** 402c6202d85SSimon Glass * get_bus_speed() - get the speed of a bus (optional) 403c6202d85SSimon Glass * 404c6202d85SSimon Glass * Normally this can be provided by the uclass, but if you want your 405c6202d85SSimon Glass * driver to check the bus speed by looking at the hardware, you can 406c6202d85SSimon Glass * implement that here. This method is optional. This method would 407e564f054SSimon Glass * normally be expected to return dev_get_uclass_priv(bus)->speed_hz. 408c6202d85SSimon Glass * 409c6202d85SSimon Glass * @bus: Bus to check 410c6202d85SSimon Glass * @return speed of selected I2C bus in Hz, -ve on error 411c6202d85SSimon Glass */ 412c6202d85SSimon Glass int (*get_bus_speed)(struct udevice *bus); 413c6202d85SSimon Glass 414c6202d85SSimon Glass /** 415c6202d85SSimon Glass * set_flags() - set the flags for a chip (optional) 416c6202d85SSimon Glass * 417c6202d85SSimon Glass * This is generally implemented by the uclass, but drivers can 418c6202d85SSimon Glass * check the value to ensure that unsupported options are not used. 419c6202d85SSimon Glass * This method is optional. If provided, this method will always be 420c6202d85SSimon Glass * called when the flags change. 421c6202d85SSimon Glass * 422c6202d85SSimon Glass * @dev: Chip to adjust 423c6202d85SSimon Glass * @flags: New flags value 424c6202d85SSimon Glass * @return 0 if OK, -EINVAL if value is unsupported 425c6202d85SSimon Glass */ 426c6202d85SSimon Glass int (*set_flags)(struct udevice *dev, uint flags); 427c6202d85SSimon Glass 428c6202d85SSimon Glass /** 429c6202d85SSimon Glass * deblock() - recover a bus that is in an unknown state 430c6202d85SSimon Glass * 431c6202d85SSimon Glass * I2C is a synchronous protocol and resets of the processor in the 432c6202d85SSimon Glass * middle of an access can block the I2C Bus until a powerdown of 433c6202d85SSimon Glass * the full unit is done. This is because slaves can be stuck 434c6202d85SSimon Glass * waiting for addition bus transitions for a transaction that will 435c6202d85SSimon Glass * never complete. Resetting the I2C master does not help. The only 436c6202d85SSimon Glass * way is to force the bus through a series of transitions to make 437c6202d85SSimon Glass * sure that all slaves are done with the transaction. This method 438c6202d85SSimon Glass * performs this 'deblocking' if support by the driver. 439c6202d85SSimon Glass * 440c6202d85SSimon Glass * This method is optional. 441c6202d85SSimon Glass */ 442c6202d85SSimon Glass int (*deblock)(struct udevice *bus); 443c6202d85SSimon Glass }; 444c6202d85SSimon Glass 445c6202d85SSimon Glass #define i2c_get_ops(dev) ((struct dm_i2c_ops *)(dev)->driver->ops) 446c6202d85SSimon Glass 447c6202d85SSimon Glass /** 448c6202d85SSimon Glass * i2c_get_chip() - get a device to use to access a chip on a bus 449c6202d85SSimon Glass * 450c6202d85SSimon Glass * This returns the device for the given chip address. The device can then 451c6202d85SSimon Glass * be used with calls to i2c_read(), i2c_write(), i2c_probe(), etc. 452c6202d85SSimon Glass * 453c6202d85SSimon Glass * @bus: Bus to examine 454c6202d85SSimon Glass * @chip_addr: Chip address for the new device 45525ab4b03SSimon Glass * @offset_len: Length of a register offset in bytes (normally 1) 456c6202d85SSimon Glass * @devp: Returns pointer to new device if found or -ENODEV if not 457c6202d85SSimon Glass * found 458c6202d85SSimon Glass */ 45925ab4b03SSimon Glass int i2c_get_chip(struct udevice *bus, uint chip_addr, uint offset_len, 46025ab4b03SSimon Glass struct udevice **devp); 461c6202d85SSimon Glass 462c6202d85SSimon Glass /** 463c6202d85SSimon Glass * i2c_get_chip() - get a device to use to access a chip on a bus number 464c6202d85SSimon Glass * 465c6202d85SSimon Glass * This returns the device for the given chip address on a particular bus 466c6202d85SSimon Glass * number. 467c6202d85SSimon Glass * 468c6202d85SSimon Glass * @busnum: Bus number to examine 469c6202d85SSimon Glass * @chip_addr: Chip address for the new device 47025ab4b03SSimon Glass * @offset_len: Length of a register offset in bytes (normally 1) 471c6202d85SSimon Glass * @devp: Returns pointer to new device if found or -ENODEV if not 472c6202d85SSimon Glass * found 473c6202d85SSimon Glass */ 47425ab4b03SSimon Glass int i2c_get_chip_for_busnum(int busnum, int chip_addr, uint offset_len, 47525ab4b03SSimon Glass struct udevice **devp); 476c6202d85SSimon Glass 477c6202d85SSimon Glass /** 478c6202d85SSimon Glass * i2c_chip_ofdata_to_platdata() - Decode standard I2C platform data 479c6202d85SSimon Glass * 480c6202d85SSimon Glass * This decodes the chip address from a device tree node and puts it into 481c6202d85SSimon Glass * its dm_i2c_chip structure. This should be called in your driver's 482c6202d85SSimon Glass * ofdata_to_platdata() method. 483c6202d85SSimon Glass * 484c6202d85SSimon Glass * @blob: Device tree blob 485c6202d85SSimon Glass * @node: Node offset to read from 486c6202d85SSimon Glass * @spi: Place to put the decoded information 487c6202d85SSimon Glass */ 488c6202d85SSimon Glass int i2c_chip_ofdata_to_platdata(const void *blob, int node, 489c6202d85SSimon Glass struct dm_i2c_chip *chip); 490c6202d85SSimon Glass 4917d7db222SSimon Glass /** 4927d7db222SSimon Glass * i2c_dump_msgs() - Dump a list of I2C messages 4937d7db222SSimon Glass * 4947d7db222SSimon Glass * This may be useful for debugging. 4957d7db222SSimon Glass * 4967d7db222SSimon Glass * @msg: Message list to dump 4977d7db222SSimon Glass * @nmsgs: Number of messages 4987d7db222SSimon Glass */ 4997d7db222SSimon Glass void i2c_dump_msgs(struct i2c_msg *msg, int nmsgs); 5007d7db222SSimon Glass 501c6202d85SSimon Glass #ifndef CONFIG_DM_I2C 502c6202d85SSimon Glass 503c6202d85SSimon Glass /* 5041f045217Swdenk * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING 5051f045217Swdenk * 5061f045217Swdenk * The implementation MUST NOT use static or global variables if the 5071f045217Swdenk * I2C routines are used to read SDRAM configuration information 5081f045217Swdenk * because this is done before the memories are initialized. Limited 5091f045217Swdenk * use of stack-based variables are OK (the initial stack size is 5101f045217Swdenk * limited). 5111f045217Swdenk * 5121f045217Swdenk * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING 5131f045217Swdenk */ 5141f045217Swdenk 5151f045217Swdenk /* 5161f045217Swdenk * Configuration items. 5171f045217Swdenk */ 5181f045217Swdenk #define I2C_RXTX_LEN 128 /* maximum tx/rx buffer length */ 5191f045217Swdenk 520385c9ef5SHeiko Schocher #if !defined(CONFIG_SYS_I2C_MAX_HOPS) 521385c9ef5SHeiko Schocher /* no muxes used bus = i2c adapters */ 522385c9ef5SHeiko Schocher #define CONFIG_SYS_I2C_DIRECT_BUS 1 523385c9ef5SHeiko Schocher #define CONFIG_SYS_I2C_MAX_HOPS 0 524385c9ef5SHeiko Schocher #define CONFIG_SYS_NUM_I2C_BUSES ll_entry_count(struct i2c_adapter, i2c) 52579b2d0bbSStefan Roese #else 526385c9ef5SHeiko Schocher /* we use i2c muxes */ 527385c9ef5SHeiko Schocher #undef CONFIG_SYS_I2C_DIRECT_BUS 52879b2d0bbSStefan Roese #endif 52979b2d0bbSStefan Roese 5308c12045aSStefan Roese /* define the I2C bus number for RTC and DTT if not already done */ 5316d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #if !defined(CONFIG_SYS_RTC_BUS_NUM) 5326d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #define CONFIG_SYS_RTC_BUS_NUM 0 5338c12045aSStefan Roese #endif 5346d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #if !defined(CONFIG_SYS_DTT_BUS_NUM) 5356d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #define CONFIG_SYS_DTT_BUS_NUM 0 5368c12045aSStefan Roese #endif 5376d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #if !defined(CONFIG_SYS_SPD_BUS_NUM) 5386d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #define CONFIG_SYS_SPD_BUS_NUM 0 539d8a8ea5cSMatthias Fuchs #endif 5408c12045aSStefan Roese 541385c9ef5SHeiko Schocher struct i2c_adapter { 542385c9ef5SHeiko Schocher void (*init)(struct i2c_adapter *adap, int speed, 543385c9ef5SHeiko Schocher int slaveaddr); 544385c9ef5SHeiko Schocher int (*probe)(struct i2c_adapter *adap, uint8_t chip); 545385c9ef5SHeiko Schocher int (*read)(struct i2c_adapter *adap, uint8_t chip, 546385c9ef5SHeiko Schocher uint addr, int alen, uint8_t *buffer, 547385c9ef5SHeiko Schocher int len); 548385c9ef5SHeiko Schocher int (*write)(struct i2c_adapter *adap, uint8_t chip, 549385c9ef5SHeiko Schocher uint addr, int alen, uint8_t *buffer, 550385c9ef5SHeiko Schocher int len); 551385c9ef5SHeiko Schocher uint (*set_bus_speed)(struct i2c_adapter *adap, 552385c9ef5SHeiko Schocher uint speed); 553385c9ef5SHeiko Schocher int speed; 554d5243359SHannes Petermaier int waitdelay; 555385c9ef5SHeiko Schocher int slaveaddr; 556385c9ef5SHeiko Schocher int init_done; 557385c9ef5SHeiko Schocher int hwadapnr; 558385c9ef5SHeiko Schocher char *name; 559385c9ef5SHeiko Schocher }; 560385c9ef5SHeiko Schocher 561385c9ef5SHeiko Schocher #define U_BOOT_I2C_MKENT_COMPLETE(_init, _probe, _read, _write, \ 562385c9ef5SHeiko Schocher _set_speed, _speed, _slaveaddr, _hwadapnr, _name) \ 563385c9ef5SHeiko Schocher { \ 564385c9ef5SHeiko Schocher .init = _init, \ 565385c9ef5SHeiko Schocher .probe = _probe, \ 566385c9ef5SHeiko Schocher .read = _read, \ 567385c9ef5SHeiko Schocher .write = _write, \ 568385c9ef5SHeiko Schocher .set_bus_speed = _set_speed, \ 569385c9ef5SHeiko Schocher .speed = _speed, \ 570385c9ef5SHeiko Schocher .slaveaddr = _slaveaddr, \ 571385c9ef5SHeiko Schocher .init_done = 0, \ 572385c9ef5SHeiko Schocher .hwadapnr = _hwadapnr, \ 573385c9ef5SHeiko Schocher .name = #_name \ 574385c9ef5SHeiko Schocher }; 575385c9ef5SHeiko Schocher 576385c9ef5SHeiko Schocher #define U_BOOT_I2C_ADAP_COMPLETE(_name, _init, _probe, _read, _write, \ 577385c9ef5SHeiko Schocher _set_speed, _speed, _slaveaddr, _hwadapnr) \ 578385c9ef5SHeiko Schocher ll_entry_declare(struct i2c_adapter, _name, i2c) = \ 579385c9ef5SHeiko Schocher U_BOOT_I2C_MKENT_COMPLETE(_init, _probe, _read, _write, \ 580385c9ef5SHeiko Schocher _set_speed, _speed, _slaveaddr, _hwadapnr, _name); 581385c9ef5SHeiko Schocher 582385c9ef5SHeiko Schocher struct i2c_adapter *i2c_get_adapter(int index); 583385c9ef5SHeiko Schocher 584385c9ef5SHeiko Schocher #ifndef CONFIG_SYS_I2C_DIRECT_BUS 585385c9ef5SHeiko Schocher struct i2c_mux { 586385c9ef5SHeiko Schocher int id; 587385c9ef5SHeiko Schocher char name[16]; 588385c9ef5SHeiko Schocher }; 589385c9ef5SHeiko Schocher 590385c9ef5SHeiko Schocher struct i2c_next_hop { 591385c9ef5SHeiko Schocher struct i2c_mux mux; 592385c9ef5SHeiko Schocher uint8_t chip; 593385c9ef5SHeiko Schocher uint8_t channel; 594385c9ef5SHeiko Schocher }; 595385c9ef5SHeiko Schocher 596385c9ef5SHeiko Schocher struct i2c_bus_hose { 597385c9ef5SHeiko Schocher int adapter; 598385c9ef5SHeiko Schocher struct i2c_next_hop next_hop[CONFIG_SYS_I2C_MAX_HOPS]; 599385c9ef5SHeiko Schocher }; 600385c9ef5SHeiko Schocher #define I2C_NULL_HOP {{-1, ""}, 0, 0} 601385c9ef5SHeiko Schocher extern struct i2c_bus_hose i2c_bus[]; 602385c9ef5SHeiko Schocher 603385c9ef5SHeiko Schocher #define I2C_ADAPTER(bus) i2c_bus[bus].adapter 604385c9ef5SHeiko Schocher #else 605385c9ef5SHeiko Schocher #define I2C_ADAPTER(bus) bus 606385c9ef5SHeiko Schocher #endif 607385c9ef5SHeiko Schocher #define I2C_BUS gd->cur_i2c_bus 608385c9ef5SHeiko Schocher 609385c9ef5SHeiko Schocher #define I2C_ADAP_NR(bus) i2c_get_adapter(I2C_ADAPTER(bus)) 610385c9ef5SHeiko Schocher #define I2C_ADAP I2C_ADAP_NR(gd->cur_i2c_bus) 611385c9ef5SHeiko Schocher #define I2C_ADAP_HWNR (I2C_ADAP->hwadapnr) 612385c9ef5SHeiko Schocher 613385c9ef5SHeiko Schocher #ifndef CONFIG_SYS_I2C_DIRECT_BUS 614385c9ef5SHeiko Schocher #define I2C_MUX_PCA9540_ID 1 615385c9ef5SHeiko Schocher #define I2C_MUX_PCA9540 {I2C_MUX_PCA9540_ID, "PCA9540B"} 616385c9ef5SHeiko Schocher #define I2C_MUX_PCA9542_ID 2 617385c9ef5SHeiko Schocher #define I2C_MUX_PCA9542 {I2C_MUX_PCA9542_ID, "PCA9542A"} 618385c9ef5SHeiko Schocher #define I2C_MUX_PCA9544_ID 3 619385c9ef5SHeiko Schocher #define I2C_MUX_PCA9544 {I2C_MUX_PCA9544_ID, "PCA9544A"} 620385c9ef5SHeiko Schocher #define I2C_MUX_PCA9547_ID 4 621385c9ef5SHeiko Schocher #define I2C_MUX_PCA9547 {I2C_MUX_PCA9547_ID, "PCA9547A"} 622e6658749SMichael Burr #define I2C_MUX_PCA9548_ID 5 623e6658749SMichael Burr #define I2C_MUX_PCA9548 {I2C_MUX_PCA9548_ID, "PCA9548"} 624385c9ef5SHeiko Schocher #endif 625385c9ef5SHeiko Schocher 62698aed379SHeiko Schocher #ifndef I2C_SOFT_DECLARATIONS 62798aed379SHeiko Schocher # if defined(CONFIG_MPC8260) 6286d0f6bcfSJean-Christophe PLAGNIOL-VILLARD # define I2C_SOFT_DECLARATIONS volatile ioport_t *iop = ioport_addr((immap_t *)CONFIG_SYS_IMMR, I2C_PORT); 62998aed379SHeiko Schocher # elif defined(CONFIG_8xx) 6306d0f6bcfSJean-Christophe PLAGNIOL-VILLARD # define I2C_SOFT_DECLARATIONS volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR; 6310cf0b931SJens Scharsig 6320cf0b931SJens Scharsig # elif (defined(CONFIG_AT91RM9200) || \ 6330cf0b931SJens Scharsig defined(CONFIG_AT91SAM9260) || defined(CONFIG_AT91SAM9261) || \ 634cb96a0a4SAndreas Bießmann defined(CONFIG_AT91SAM9263)) 63578132275Sesw@bus-elektronik.de # define I2C_SOFT_DECLARATIONS at91_pio_t *pio = (at91_pio_t *) ATMEL_BASE_PIOA; 63698aed379SHeiko Schocher # else 63798aed379SHeiko Schocher # define I2C_SOFT_DECLARATIONS 63898aed379SHeiko Schocher # endif 63998aed379SHeiko Schocher #endif 640ecf5f077STimur Tabi 641ecf5f077STimur Tabi #ifdef CONFIG_8xx 6429c90a2c8SPeter Tyser /* Set default value for the I2C bus speed on 8xx. In the 643ecf5f077STimur Tabi * future, we'll define these in all 8xx board config files. 644ecf5f077STimur Tabi */ 645ecf5f077STimur Tabi #ifndef CONFIG_SYS_I2C_SPEED 646ecf5f077STimur Tabi #define CONFIG_SYS_I2C_SPEED 50000 647ecf5f077STimur Tabi #endif 648ecf5f077STimur Tabi #endif 6499c90a2c8SPeter Tyser 6509c90a2c8SPeter Tyser /* 6519c90a2c8SPeter Tyser * Many boards/controllers/drivers don't support an I2C slave interface so 6529c90a2c8SPeter Tyser * provide a default slave address for them for use in common code. A real 6539c90a2c8SPeter Tyser * value for CONFIG_SYS_I2C_SLAVE should be defined for any board which does 6549c90a2c8SPeter Tyser * support a slave interface. 6559c90a2c8SPeter Tyser */ 6569c90a2c8SPeter Tyser #ifndef CONFIG_SYS_I2C_SLAVE 6579c90a2c8SPeter Tyser #define CONFIG_SYS_I2C_SLAVE 0xfe 658ecf5f077STimur Tabi #endif 659ecf5f077STimur Tabi 6601f045217Swdenk /* 6611f045217Swdenk * Initialization, must be called once on start up, may be called 6621f045217Swdenk * repeatedly to change the speed and slave addresses. 6631f045217Swdenk */ 6641f045217Swdenk void i2c_init(int speed, int slaveaddr); 66506d01dbeSwdenk void i2c_init_board(void); 66626a33504SRichard Retanubun #ifdef CONFIG_SYS_I2C_BOARD_LATE_INIT 66726a33504SRichard Retanubun void i2c_board_late_init(void); 66826a33504SRichard Retanubun #endif 6691f045217Swdenk 670385c9ef5SHeiko Schocher #ifdef CONFIG_SYS_I2C 671385c9ef5SHeiko Schocher /* 672385c9ef5SHeiko Schocher * i2c_get_bus_num: 673385c9ef5SHeiko Schocher * 674385c9ef5SHeiko Schocher * Returns index of currently active I2C bus. Zero-based. 675385c9ef5SHeiko Schocher */ 676385c9ef5SHeiko Schocher unsigned int i2c_get_bus_num(void); 67767b23a32SHeiko Schocher 678385c9ef5SHeiko Schocher /* 679385c9ef5SHeiko Schocher * i2c_set_bus_num: 680385c9ef5SHeiko Schocher * 681385c9ef5SHeiko Schocher * Change the active I2C bus. Subsequent read/write calls will 682385c9ef5SHeiko Schocher * go to this one. 683385c9ef5SHeiko Schocher * 684385c9ef5SHeiko Schocher * bus - bus index, zero based 685385c9ef5SHeiko Schocher * 686385c9ef5SHeiko Schocher * Returns: 0 on success, not 0 on failure 687385c9ef5SHeiko Schocher * 688385c9ef5SHeiko Schocher */ 689385c9ef5SHeiko Schocher int i2c_set_bus_num(unsigned int bus); 69067b23a32SHeiko Schocher 691385c9ef5SHeiko Schocher /* 692385c9ef5SHeiko Schocher * i2c_init_all(): 693385c9ef5SHeiko Schocher * 694385c9ef5SHeiko Schocher * Initializes all I2C adapters in the system. All i2c_adap structures must 695385c9ef5SHeiko Schocher * be initialized beforehead with function pointers and data, including 696385c9ef5SHeiko Schocher * speed and slaveaddr. Returns 0 on success, non-0 on failure. 697385c9ef5SHeiko Schocher */ 698385c9ef5SHeiko Schocher void i2c_init_all(void); 69967b23a32SHeiko Schocher 700385c9ef5SHeiko Schocher /* 701385c9ef5SHeiko Schocher * Probe the given I2C chip address. Returns 0 if a chip responded, 702385c9ef5SHeiko Schocher * not 0 on failure. 703385c9ef5SHeiko Schocher */ 704385c9ef5SHeiko Schocher int i2c_probe(uint8_t chip); 705385c9ef5SHeiko Schocher 706385c9ef5SHeiko Schocher /* 707385c9ef5SHeiko Schocher * Read/Write interface: 708385c9ef5SHeiko Schocher * chip: I2C chip address, range 0..127 709385c9ef5SHeiko Schocher * addr: Memory (register) address within the chip 710385c9ef5SHeiko Schocher * alen: Number of bytes to use for addr (typically 1, 2 for larger 711385c9ef5SHeiko Schocher * memories, 0 for register type devices with only one 712385c9ef5SHeiko Schocher * register) 713385c9ef5SHeiko Schocher * buffer: Where to read/write the data 714385c9ef5SHeiko Schocher * len: How many bytes to read/write 715385c9ef5SHeiko Schocher * 716385c9ef5SHeiko Schocher * Returns: 0 on success, not 0 on failure 717385c9ef5SHeiko Schocher */ 718385c9ef5SHeiko Schocher int i2c_read(uint8_t chip, unsigned int addr, int alen, 719385c9ef5SHeiko Schocher uint8_t *buffer, int len); 720385c9ef5SHeiko Schocher 721385c9ef5SHeiko Schocher int i2c_write(uint8_t chip, unsigned int addr, int alen, 722385c9ef5SHeiko Schocher uint8_t *buffer, int len); 723385c9ef5SHeiko Schocher 724385c9ef5SHeiko Schocher /* 725385c9ef5SHeiko Schocher * Utility routines to read/write registers. 726385c9ef5SHeiko Schocher */ 727385c9ef5SHeiko Schocher uint8_t i2c_reg_read(uint8_t addr, uint8_t reg); 728385c9ef5SHeiko Schocher 729385c9ef5SHeiko Schocher void i2c_reg_write(uint8_t addr, uint8_t reg, uint8_t val); 730385c9ef5SHeiko Schocher 731385c9ef5SHeiko Schocher /* 732385c9ef5SHeiko Schocher * i2c_set_bus_speed: 733385c9ef5SHeiko Schocher * 734385c9ef5SHeiko Schocher * Change the speed of the active I2C bus 735385c9ef5SHeiko Schocher * 736385c9ef5SHeiko Schocher * speed - bus speed in Hz 737385c9ef5SHeiko Schocher * 738385c9ef5SHeiko Schocher * Returns: new bus speed 739385c9ef5SHeiko Schocher * 740385c9ef5SHeiko Schocher */ 741385c9ef5SHeiko Schocher unsigned int i2c_set_bus_speed(unsigned int speed); 742385c9ef5SHeiko Schocher 743385c9ef5SHeiko Schocher /* 744385c9ef5SHeiko Schocher * i2c_get_bus_speed: 745385c9ef5SHeiko Schocher * 746385c9ef5SHeiko Schocher * Returns speed of currently active I2C bus in Hz 747385c9ef5SHeiko Schocher */ 748385c9ef5SHeiko Schocher 749385c9ef5SHeiko Schocher unsigned int i2c_get_bus_speed(void); 750385c9ef5SHeiko Schocher 751385c9ef5SHeiko Schocher /* 752385c9ef5SHeiko Schocher * i2c_reloc_fixup: 753385c9ef5SHeiko Schocher * 754385c9ef5SHeiko Schocher * Adjusts I2C pointers after U-Boot is relocated to DRAM 755385c9ef5SHeiko Schocher */ 756385c9ef5SHeiko Schocher void i2c_reloc_fixup(void); 757ea818dbbSHeiko Schocher #if defined(CONFIG_SYS_I2C_SOFT) 758ea818dbbSHeiko Schocher void i2c_soft_init(void); 759ea818dbbSHeiko Schocher void i2c_soft_active(void); 760ea818dbbSHeiko Schocher void i2c_soft_tristate(void); 761ea818dbbSHeiko Schocher int i2c_soft_read(void); 762ea818dbbSHeiko Schocher void i2c_soft_sda(int bit); 763ea818dbbSHeiko Schocher void i2c_soft_scl(int bit); 764ea818dbbSHeiko Schocher void i2c_soft_delay(void); 76567b23a32SHeiko Schocher #endif 766385c9ef5SHeiko Schocher #else 76767b23a32SHeiko Schocher 7681f045217Swdenk /* 7691f045217Swdenk * Probe the given I2C chip address. Returns 0 if a chip responded, 7701f045217Swdenk * not 0 on failure. 7711f045217Swdenk */ 7721f045217Swdenk int i2c_probe(uchar chip); 7731f045217Swdenk 7741f045217Swdenk /* 7751f045217Swdenk * Read/Write interface: 7761f045217Swdenk * chip: I2C chip address, range 0..127 7771f045217Swdenk * addr: Memory (register) address within the chip 7781f045217Swdenk * alen: Number of bytes to use for addr (typically 1, 2 for larger 7791f045217Swdenk * memories, 0 for register type devices with only one 7801f045217Swdenk * register) 7811f045217Swdenk * buffer: Where to read/write the data 7821f045217Swdenk * len: How many bytes to read/write 7831f045217Swdenk * 7841f045217Swdenk * Returns: 0 on success, not 0 on failure 7851f045217Swdenk */ 7861f045217Swdenk int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len); 7871f045217Swdenk int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len); 7881f045217Swdenk 7891f045217Swdenk /* 7901f045217Swdenk * Utility routines to read/write registers. 7911f045217Swdenk */ 792ecf5f077STimur Tabi static inline u8 i2c_reg_read(u8 addr, u8 reg) 793ecf5f077STimur Tabi { 794ecf5f077STimur Tabi u8 buf; 795ecf5f077STimur Tabi 796ecf5f077STimur Tabi #ifdef CONFIG_8xx 797ecf5f077STimur Tabi /* MPC8xx needs this. Maybe one day we can get rid of it. */ 798ecf5f077STimur Tabi i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE); 799ecf5f077STimur Tabi #endif 800ecf5f077STimur Tabi 801ecf5f077STimur Tabi #ifdef DEBUG 802ecf5f077STimur Tabi printf("%s: addr=0x%02x, reg=0x%02x\n", __func__, addr, reg); 803ecf5f077STimur Tabi #endif 804ecf5f077STimur Tabi 805ecf5f077STimur Tabi i2c_read(addr, reg, 1, &buf, 1); 806ecf5f077STimur Tabi 807ecf5f077STimur Tabi return buf; 808ecf5f077STimur Tabi } 809ecf5f077STimur Tabi 810ecf5f077STimur Tabi static inline void i2c_reg_write(u8 addr, u8 reg, u8 val) 811ecf5f077STimur Tabi { 812ecf5f077STimur Tabi #ifdef CONFIG_8xx 813ecf5f077STimur Tabi /* MPC8xx needs this. Maybe one day we can get rid of it. */ 814ecf5f077STimur Tabi i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE); 815ecf5f077STimur Tabi #endif 816ecf5f077STimur Tabi 817ecf5f077STimur Tabi #ifdef DEBUG 818ecf5f077STimur Tabi printf("%s: addr=0x%02x, reg=0x%02x, val=0x%02x\n", 819ecf5f077STimur Tabi __func__, addr, reg, val); 820ecf5f077STimur Tabi #endif 821ecf5f077STimur Tabi 822ecf5f077STimur Tabi i2c_write(addr, reg, 1, &val, 1); 823ecf5f077STimur Tabi } 8241f045217Swdenk 825bb99ad6dSBen Warren /* 826bb99ad6dSBen Warren * Functions for setting the current I2C bus and its speed 827bb99ad6dSBen Warren */ 828bb99ad6dSBen Warren 829bb99ad6dSBen Warren /* 830bb99ad6dSBen Warren * i2c_set_bus_num: 831bb99ad6dSBen Warren * 832bb99ad6dSBen Warren * Change the active I2C bus. Subsequent read/write calls will 833bb99ad6dSBen Warren * go to this one. 834bb99ad6dSBen Warren * 835bb99ad6dSBen Warren * bus - bus index, zero based 836bb99ad6dSBen Warren * 837bb99ad6dSBen Warren * Returns: 0 on success, not 0 on failure 838bb99ad6dSBen Warren * 839bb99ad6dSBen Warren */ 8409ca880a2STimur Tabi int i2c_set_bus_num(unsigned int bus); 841bb99ad6dSBen Warren 842bb99ad6dSBen Warren /* 843bb99ad6dSBen Warren * i2c_get_bus_num: 844bb99ad6dSBen Warren * 845bb99ad6dSBen Warren * Returns index of currently active I2C bus. Zero-based. 846bb99ad6dSBen Warren */ 847bb99ad6dSBen Warren 8489ca880a2STimur Tabi unsigned int i2c_get_bus_num(void); 849bb99ad6dSBen Warren 850bb99ad6dSBen Warren /* 851bb99ad6dSBen Warren * i2c_set_bus_speed: 852bb99ad6dSBen Warren * 853bb99ad6dSBen Warren * Change the speed of the active I2C bus 854bb99ad6dSBen Warren * 855bb99ad6dSBen Warren * speed - bus speed in Hz 856bb99ad6dSBen Warren * 857bb99ad6dSBen Warren * Returns: 0 on success, not 0 on failure 858bb99ad6dSBen Warren * 859bb99ad6dSBen Warren */ 8609ca880a2STimur Tabi int i2c_set_bus_speed(unsigned int); 861bb99ad6dSBen Warren 862bb99ad6dSBen Warren /* 863bb99ad6dSBen Warren * i2c_get_bus_speed: 864bb99ad6dSBen Warren * 865bb99ad6dSBen Warren * Returns speed of currently active I2C bus in Hz 866bb99ad6dSBen Warren */ 867bb99ad6dSBen Warren 8689ca880a2STimur Tabi unsigned int i2c_get_bus_speed(void); 869385c9ef5SHeiko Schocher #endif /* CONFIG_SYS_I2C */ 870385c9ef5SHeiko Schocher 871385c9ef5SHeiko Schocher /* 872385c9ef5SHeiko Schocher * only for backwardcompatibility, should go away if we switched 873385c9ef5SHeiko Schocher * completely to new multibus support. 874385c9ef5SHeiko Schocher */ 875385c9ef5SHeiko Schocher #if defined(CONFIG_SYS_I2C) || defined(CONFIG_I2C_MULTI_BUS) 876385c9ef5SHeiko Schocher # if !defined(CONFIG_SYS_MAX_I2C_BUS) 877385c9ef5SHeiko Schocher # define CONFIG_SYS_MAX_I2C_BUS 2 878385c9ef5SHeiko Schocher # endif 879ea0f73abSŁukasz Majewski # define I2C_MULTI_BUS 1 880385c9ef5SHeiko Schocher #else 881385c9ef5SHeiko Schocher # define CONFIG_SYS_MAX_I2C_BUS 1 882385c9ef5SHeiko Schocher # define I2C_MULTI_BUS 0 883385c9ef5SHeiko Schocher #endif 884bb99ad6dSBen Warren 885cd7b4e82SMarek Vasut /* NOTE: These two functions MUST be always_inline to avoid code growth! */ 886cd7b4e82SMarek Vasut static inline unsigned int I2C_GET_BUS(void) __attribute__((always_inline)); 887cd7b4e82SMarek Vasut static inline unsigned int I2C_GET_BUS(void) 888cd7b4e82SMarek Vasut { 889cd7b4e82SMarek Vasut return I2C_MULTI_BUS ? i2c_get_bus_num() : 0; 890cd7b4e82SMarek Vasut } 891cd7b4e82SMarek Vasut 892cd7b4e82SMarek Vasut static inline void I2C_SET_BUS(unsigned int bus) __attribute__((always_inline)); 893cd7b4e82SMarek Vasut static inline void I2C_SET_BUS(unsigned int bus) 894cd7b4e82SMarek Vasut { 895cd7b4e82SMarek Vasut if (I2C_MULTI_BUS) 896cd7b4e82SMarek Vasut i2c_set_bus_num(bus); 897cd7b4e82SMarek Vasut } 898cd7b4e82SMarek Vasut 8997ca8f73aSŁukasz Majewski /* Multi I2C definitions */ 9007ca8f73aSŁukasz Majewski enum { 9017ca8f73aSŁukasz Majewski I2C_0, I2C_1, I2C_2, I2C_3, I2C_4, I2C_5, I2C_6, I2C_7, 9027ca8f73aSŁukasz Majewski I2C_8, I2C_9, I2C_10, 9037ca8f73aSŁukasz Majewski }; 9047ca8f73aSŁukasz Majewski 9057ca8f73aSŁukasz Majewski /* Multi I2C busses handling */ 9067ca8f73aSŁukasz Majewski #ifdef CONFIG_SOFT_I2C_MULTI_BUS 9077ca8f73aSŁukasz Majewski extern int get_multi_scl_pin(void); 9087ca8f73aSŁukasz Majewski extern int get_multi_sda_pin(void); 9097ca8f73aSŁukasz Majewski extern int multi_i2c_init(void); 9107ca8f73aSŁukasz Majewski #endif 911a9d2ae70SRajeshwari Shinde 912a9d2ae70SRajeshwari Shinde /** 913a9d2ae70SRajeshwari Shinde * Get FDT values for i2c bus. 914a9d2ae70SRajeshwari Shinde * 915a9d2ae70SRajeshwari Shinde * @param blob Device tree blbo 916a9d2ae70SRajeshwari Shinde * @return the number of I2C bus 917a9d2ae70SRajeshwari Shinde */ 918a9d2ae70SRajeshwari Shinde void board_i2c_init(const void *blob); 919a9d2ae70SRajeshwari Shinde 920a9d2ae70SRajeshwari Shinde /** 921a9d2ae70SRajeshwari Shinde * Find the I2C bus number by given a FDT I2C node. 922a9d2ae70SRajeshwari Shinde * 923a9d2ae70SRajeshwari Shinde * @param blob Device tree blbo 924a9d2ae70SRajeshwari Shinde * @param node FDT I2C node to find 925a9d2ae70SRajeshwari Shinde * @return the number of I2C bus (zero based), or -1 on error 926a9d2ae70SRajeshwari Shinde */ 927a9d2ae70SRajeshwari Shinde int i2c_get_bus_num_fdt(int node); 928a9d2ae70SRajeshwari Shinde 929a9d2ae70SRajeshwari Shinde /** 930a9d2ae70SRajeshwari Shinde * Reset the I2C bus represented by the given a FDT I2C node. 931a9d2ae70SRajeshwari Shinde * 932a9d2ae70SRajeshwari Shinde * @param blob Device tree blbo 933a9d2ae70SRajeshwari Shinde * @param node FDT I2C node to find 934a9d2ae70SRajeshwari Shinde * @return 0 if port was reset, -1 if not found 935a9d2ae70SRajeshwari Shinde */ 936a9d2ae70SRajeshwari Shinde int i2c_reset_port_fdt(const void *blob, int node); 937c6202d85SSimon Glass 938c6202d85SSimon Glass #endif /* !CONFIG_DM_I2C */ 939c6202d85SSimon Glass 9401f045217Swdenk #endif /* _I2C_H_ */ 941