183d290c5STom Rini /* SPDX-License-Identifier: GPL-2.0+ */ 21f045217Swdenk /* 3385c9ef5SHeiko Schocher * Copyright (C) 2009 Sergey Kubushyn <ksi@koi8.net> 4385c9ef5SHeiko Schocher * Copyright (C) 2009 - 2013 Heiko Schocher <hs@denx.de> 5385c9ef5SHeiko Schocher * Changes for multibus/multiadapter I2C support. 6385c9ef5SHeiko Schocher * 71f045217Swdenk * (C) Copyright 2001 81f045217Swdenk * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com. 91f045217Swdenk * 101f045217Swdenk * The original I2C interface was 111f045217Swdenk * (C) 2000 by Paolo Scaffardi (arsenio@tin.it) 121f045217Swdenk * AIRVENT SAM s.p.a - RIMINI(ITALY) 131f045217Swdenk * but has been changed substantially. 141f045217Swdenk */ 151f045217Swdenk 161f045217Swdenk #ifndef _I2C_H_ 171f045217Swdenk #define _I2C_H_ 181f045217Swdenk 191f045217Swdenk /* 20c6202d85SSimon Glass * For now there are essentially two parts to this file - driver model 21c6202d85SSimon Glass * here at the top, and the older code below (with CONFIG_SYS_I2C being 22c6202d85SSimon Glass * most recent). The plan is to migrate everything to driver model. 23c6202d85SSimon Glass * The driver model structures and API are separate as they are different 24c6202d85SSimon Glass * enough as to be incompatible for compilation purposes. 25c6202d85SSimon Glass */ 26c6202d85SSimon Glass 27c6202d85SSimon Glass enum dm_i2c_chip_flags { 28c6202d85SSimon Glass DM_I2C_CHIP_10BIT = 1 << 0, /* Use 10-bit addressing */ 29c6202d85SSimon Glass DM_I2C_CHIP_RD_ADDRESS = 1 << 1, /* Send address for each read byte */ 30c6202d85SSimon Glass DM_I2C_CHIP_WR_ADDRESS = 1 << 2, /* Send address for each write byte */ 31c6202d85SSimon Glass }; 32c6202d85SSimon Glass 33fffff726SSimon Glass struct udevice; 34c6202d85SSimon Glass /** 35c6202d85SSimon Glass * struct dm_i2c_chip - information about an i2c chip 36c6202d85SSimon Glass * 37c6202d85SSimon Glass * An I2C chip is a device on the I2C bus. It sits at a particular address 38c6202d85SSimon Glass * and normally supports 7-bit or 10-bit addressing. 39c6202d85SSimon Glass * 40e6f66ec0SSimon Glass * To obtain this structure, use dev_get_parent_platdata(dev) where dev is 41e6f66ec0SSimon Glass * the chip to examine. 42c6202d85SSimon Glass * 43c6202d85SSimon Glass * @chip_addr: Chip address on bus 44c6202d85SSimon Glass * @offset_len: Length of offset in bytes. A single byte offset can 45c6202d85SSimon Glass * represent up to 256 bytes. A value larger than 1 may be 46c6202d85SSimon Glass * needed for larger devices. 47c6202d85SSimon Glass * @flags: Flags for this chip (dm_i2c_chip_flags) 48c6202d85SSimon Glass * @emul: Emulator for this chip address (only used for emulation) 49c6202d85SSimon Glass */ 50c6202d85SSimon Glass struct dm_i2c_chip { 51c6202d85SSimon Glass uint chip_addr; 52c6202d85SSimon Glass uint offset_len; 53c6202d85SSimon Glass uint flags; 54c6202d85SSimon Glass #ifdef CONFIG_SANDBOX 55c6202d85SSimon Glass struct udevice *emul; 56182bf92dSSimon Glass bool test_mode; 57c6202d85SSimon Glass #endif 58c6202d85SSimon Glass }; 59c6202d85SSimon Glass 60c6202d85SSimon Glass /** 61c6202d85SSimon Glass * struct dm_i2c_bus- information about an i2c bus 62c6202d85SSimon Glass * 63c6202d85SSimon Glass * An I2C bus contains 0 or more chips on it, each at its own address. The 64c6202d85SSimon Glass * bus can operate at different speeds (measured in Hz, typically 100KHz 65c6202d85SSimon Glass * or 400KHz). 66c6202d85SSimon Glass * 67e564f054SSimon Glass * To obtain this structure, use dev_get_uclass_priv(bus) where bus is the 68e564f054SSimon Glass * I2C bus udevice. 69c6202d85SSimon Glass * 70c6202d85SSimon Glass * @speed_hz: Bus speed in hertz (typically 100000) 71c6202d85SSimon Glass */ 72c6202d85SSimon Glass struct dm_i2c_bus { 73c6202d85SSimon Glass int speed_hz; 74c6202d85SSimon Glass }; 75c6202d85SSimon Glass 767fc65bcfSSimon Glass /* 777fc65bcfSSimon Glass * Not all of these flags are implemented in the U-Boot API 787fc65bcfSSimon Glass */ 797fc65bcfSSimon Glass enum dm_i2c_msg_flags { 807fc65bcfSSimon Glass I2C_M_TEN = 0x0010, /* ten-bit chip address */ 817fc65bcfSSimon Glass I2C_M_RD = 0x0001, /* read data, from slave to master */ 827fc65bcfSSimon Glass I2C_M_STOP = 0x8000, /* send stop after this message */ 837fc65bcfSSimon Glass I2C_M_NOSTART = 0x4000, /* no start before this message */ 847fc65bcfSSimon Glass I2C_M_REV_DIR_ADDR = 0x2000, /* invert polarity of R/W bit */ 857fc65bcfSSimon Glass I2C_M_IGNORE_NAK = 0x1000, /* continue after NAK */ 867fc65bcfSSimon Glass I2C_M_NO_RD_ACK = 0x0800, /* skip the Ack bit on reads */ 877fc65bcfSSimon Glass I2C_M_RECV_LEN = 0x0400, /* length is first received byte */ 887fc65bcfSSimon Glass }; 897fc65bcfSSimon Glass 907fc65bcfSSimon Glass /** 917fc65bcfSSimon Glass * struct i2c_msg - an I2C message 927fc65bcfSSimon Glass * 937fc65bcfSSimon Glass * @addr: Slave address 947fc65bcfSSimon Glass * @flags: Flags (see enum dm_i2c_msg_flags) 957fc65bcfSSimon Glass * @len: Length of buffer in bytes, may be 0 for a probe 967fc65bcfSSimon Glass * @buf: Buffer to send/receive, or NULL if no data 977fc65bcfSSimon Glass */ 987fc65bcfSSimon Glass struct i2c_msg { 997fc65bcfSSimon Glass uint addr; 1007fc65bcfSSimon Glass uint flags; 1017fc65bcfSSimon Glass uint len; 1027fc65bcfSSimon Glass u8 *buf; 1037fc65bcfSSimon Glass }; 1047fc65bcfSSimon Glass 1057fc65bcfSSimon Glass /** 1067fc65bcfSSimon Glass * struct i2c_msg_list - a list of I2C messages 1077fc65bcfSSimon Glass * 1087fc65bcfSSimon Glass * This is called i2c_rdwr_ioctl_data in Linux but the name does not seem 1097fc65bcfSSimon Glass * appropriate in U-Boot. 1107fc65bcfSSimon Glass * 1117fc65bcfSSimon Glass * @msg: Pointer to i2c_msg array 1127fc65bcfSSimon Glass * @nmsgs: Number of elements in the array 1137fc65bcfSSimon Glass */ 1147fc65bcfSSimon Glass struct i2c_msg_list { 1157fc65bcfSSimon Glass struct i2c_msg *msgs; 1167fc65bcfSSimon Glass uint nmsgs; 1177fc65bcfSSimon Glass }; 1187fc65bcfSSimon Glass 119c6202d85SSimon Glass /** 120f9a4c2daSSimon Glass * dm_i2c_read() - read bytes from an I2C chip 121c6202d85SSimon Glass * 122c6202d85SSimon Glass * To obtain an I2C device (called a 'chip') given the I2C bus address you 123c6202d85SSimon Glass * can use i2c_get_chip(). To obtain a bus by bus number use 124c6202d85SSimon Glass * uclass_get_device_by_seq(UCLASS_I2C, <bus number>). 125c6202d85SSimon Glass * 126c6202d85SSimon Glass * To set the address length of a devce use i2c_set_addr_len(). It 127c6202d85SSimon Glass * defaults to 1. 128c6202d85SSimon Glass * 129c6202d85SSimon Glass * @dev: Chip to read from 130c6202d85SSimon Glass * @offset: Offset within chip to start reading 131c6202d85SSimon Glass * @buffer: Place to put data 132c6202d85SSimon Glass * @len: Number of bytes to read 133c6202d85SSimon Glass * 134c6202d85SSimon Glass * @return 0 on success, -ve on failure 135c6202d85SSimon Glass */ 136f9a4c2daSSimon Glass int dm_i2c_read(struct udevice *dev, uint offset, uint8_t *buffer, int len); 137c6202d85SSimon Glass 138c6202d85SSimon Glass /** 139f9a4c2daSSimon Glass * dm_i2c_write() - write bytes to an I2C chip 140c6202d85SSimon Glass * 141f9a4c2daSSimon Glass * See notes for dm_i2c_read() above. 142c6202d85SSimon Glass * 143c6202d85SSimon Glass * @dev: Chip to write to 144c6202d85SSimon Glass * @offset: Offset within chip to start writing 145c6202d85SSimon Glass * @buffer: Buffer containing data to write 146c6202d85SSimon Glass * @len: Number of bytes to write 147c6202d85SSimon Glass * 148c6202d85SSimon Glass * @return 0 on success, -ve on failure 149c6202d85SSimon Glass */ 150f9a4c2daSSimon Glass int dm_i2c_write(struct udevice *dev, uint offset, const uint8_t *buffer, 151c6202d85SSimon Glass int len); 152c6202d85SSimon Glass 153c6202d85SSimon Glass /** 154f9a4c2daSSimon Glass * dm_i2c_probe() - probe a particular chip address 155c6202d85SSimon Glass * 156c6202d85SSimon Glass * This can be useful to check for the existence of a chip on the bus. 157c6202d85SSimon Glass * It is typically implemented by writing the chip address to the bus 158c6202d85SSimon Glass * and checking that the chip replies with an ACK. 159c6202d85SSimon Glass * 160c6202d85SSimon Glass * @bus: Bus to probe 161c6202d85SSimon Glass * @chip_addr: 7-bit address to probe (10-bit and others are not supported) 162c6202d85SSimon Glass * @chip_flags: Flags for the probe (see enum dm_i2c_chip_flags) 163c6202d85SSimon Glass * @devp: Returns the device found, or NULL if none 164c6202d85SSimon Glass * @return 0 if a chip was found at that address, -ve if not 165c6202d85SSimon Glass */ 166f9a4c2daSSimon Glass int dm_i2c_probe(struct udevice *bus, uint chip_addr, uint chip_flags, 167c6202d85SSimon Glass struct udevice **devp); 168c6202d85SSimon Glass 169c6202d85SSimon Glass /** 170ba3864f8SSimon Glass * dm_i2c_reg_read() - Read a value from an I2C register 171ba3864f8SSimon Glass * 172ba3864f8SSimon Glass * This reads a single value from the given address in an I2C chip 173ba3864f8SSimon Glass * 17425a0fb43SSimon Glass * @dev: Device to use for transfer 175ba3864f8SSimon Glass * @addr: Address to read from 176ba3864f8SSimon Glass * @return value read, or -ve on error 177ba3864f8SSimon Glass */ 178ba3864f8SSimon Glass int dm_i2c_reg_read(struct udevice *dev, uint offset); 179ba3864f8SSimon Glass 180ba3864f8SSimon Glass /** 181ba3864f8SSimon Glass * dm_i2c_reg_write() - Write a value to an I2C register 182ba3864f8SSimon Glass * 183ba3864f8SSimon Glass * This writes a single value to the given address in an I2C chip 184ba3864f8SSimon Glass * 18525a0fb43SSimon Glass * @dev: Device to use for transfer 186ba3864f8SSimon Glass * @addr: Address to write to 187ba3864f8SSimon Glass * @val: Value to write (normally a byte) 188ba3864f8SSimon Glass * @return 0 on success, -ve on error 189ba3864f8SSimon Glass */ 190ba3864f8SSimon Glass int dm_i2c_reg_write(struct udevice *dev, uint offset, unsigned int val); 191ba3864f8SSimon Glass 192ba3864f8SSimon Glass /** 193df358c6bSSimon Glass * dm_i2c_xfer() - Transfer messages over I2C 194df358c6bSSimon Glass * 195df358c6bSSimon Glass * This transfers a raw message. It is best to use dm_i2c_reg_read/write() 196df358c6bSSimon Glass * instead. 197df358c6bSSimon Glass * 198df358c6bSSimon Glass * @dev: Device to use for transfer 199df358c6bSSimon Glass * @msg: List of messages to transfer 200df358c6bSSimon Glass * @nmsgs: Number of messages to transfer 201df358c6bSSimon Glass * @return 0 on success, -ve on error 202df358c6bSSimon Glass */ 203df358c6bSSimon Glass int dm_i2c_xfer(struct udevice *dev, struct i2c_msg *msg, int nmsgs); 204df358c6bSSimon Glass 205df358c6bSSimon Glass /** 206ca88b9b9SSimon Glass * dm_i2c_set_bus_speed() - set the speed of a bus 207c6202d85SSimon Glass * 208c6202d85SSimon Glass * @bus: Bus to adjust 209c6202d85SSimon Glass * @speed: Requested speed in Hz 210c6202d85SSimon Glass * @return 0 if OK, -EINVAL for invalid values 211c6202d85SSimon Glass */ 212ca88b9b9SSimon Glass int dm_i2c_set_bus_speed(struct udevice *bus, unsigned int speed); 213c6202d85SSimon Glass 214c6202d85SSimon Glass /** 215ca88b9b9SSimon Glass * dm_i2c_get_bus_speed() - get the speed of a bus 216c6202d85SSimon Glass * 217c6202d85SSimon Glass * @bus: Bus to check 218c6202d85SSimon Glass * @return speed of selected I2C bus in Hz, -ve on error 219c6202d85SSimon Glass */ 220ca88b9b9SSimon Glass int dm_i2c_get_bus_speed(struct udevice *bus); 221c6202d85SSimon Glass 222c6202d85SSimon Glass /** 223c6202d85SSimon Glass * i2c_set_chip_flags() - set flags for a chip 224c6202d85SSimon Glass * 225c6202d85SSimon Glass * Typically addresses are 7 bits, but for 10-bit addresses you should set 226c6202d85SSimon Glass * flags to DM_I2C_CHIP_10BIT. All accesses will then use 10-bit addressing. 227c6202d85SSimon Glass * 228c6202d85SSimon Glass * @dev: Chip to adjust 229c6202d85SSimon Glass * @flags: New flags 230c6202d85SSimon Glass * @return 0 if OK, -EINVAL if value is unsupported, other -ve value on error 231c6202d85SSimon Glass */ 232c6202d85SSimon Glass int i2c_set_chip_flags(struct udevice *dev, uint flags); 233c6202d85SSimon Glass 234c6202d85SSimon Glass /** 235c6202d85SSimon Glass * i2c_get_chip_flags() - get flags for a chip 236c6202d85SSimon Glass * 237c6202d85SSimon Glass * @dev: Chip to check 238c6202d85SSimon Glass * @flagsp: Place to put flags 239c6202d85SSimon Glass * @return 0 if OK, other -ve value on error 240c6202d85SSimon Glass */ 241c6202d85SSimon Glass int i2c_get_chip_flags(struct udevice *dev, uint *flagsp); 242c6202d85SSimon Glass 243c6202d85SSimon Glass /** 244c6202d85SSimon Glass * i2c_set_offset_len() - set the offset length for a chip 245c6202d85SSimon Glass * 246c6202d85SSimon Glass * The offset used to access a chip may be up to 4 bytes long. Typically it 247c6202d85SSimon Glass * is only 1 byte, which is enough for chips with 256 bytes of memory or 248c6202d85SSimon Glass * registers. The default value is 1, but you can call this function to 249c6202d85SSimon Glass * change it. 250c6202d85SSimon Glass * 251c6202d85SSimon Glass * @offset_len: New offset length value (typically 1 or 2) 252c6202d85SSimon Glass */ 253c6202d85SSimon Glass int i2c_set_chip_offset_len(struct udevice *dev, uint offset_len); 25401501804SSimon Glass 25501501804SSimon Glass /** 25601501804SSimon Glass * i2c_get_offset_len() - get the offset length for a chip 25701501804SSimon Glass * 25801501804SSimon Glass * @return: Current offset length value (typically 1 or 2) 25901501804SSimon Glass */ 26001501804SSimon Glass int i2c_get_chip_offset_len(struct udevice *dev); 26101501804SSimon Glass 262c6202d85SSimon Glass /** 263c6202d85SSimon Glass * i2c_deblock() - recover a bus that is in an unknown state 264c6202d85SSimon Glass * 265c6202d85SSimon Glass * See the deblock() method in 'struct dm_i2c_ops' for full information 266c6202d85SSimon Glass * 267c6202d85SSimon Glass * @bus: Bus to recover 268c6202d85SSimon Glass * @return 0 if OK, -ve on error 269c6202d85SSimon Glass */ 270c6202d85SSimon Glass int i2c_deblock(struct udevice *bus); 271c6202d85SSimon Glass 27273845350SSimon Glass #ifdef CONFIG_DM_I2C_COMPAT 27373845350SSimon Glass /** 27473845350SSimon Glass * i2c_probe() - Compatibility function for driver model 27573845350SSimon Glass * 27673845350SSimon Glass * Calls dm_i2c_probe() on the current bus 27773845350SSimon Glass */ 27873845350SSimon Glass int i2c_probe(uint8_t chip_addr); 27973845350SSimon Glass 28073845350SSimon Glass /** 28173845350SSimon Glass * i2c_read() - Compatibility function for driver model 28273845350SSimon Glass * 28373845350SSimon Glass * Calls dm_i2c_read() with the device corresponding to @chip_addr, and offset 28473845350SSimon Glass * set to @addr. @alen must match the current setting for the device. 28573845350SSimon Glass */ 28673845350SSimon Glass int i2c_read(uint8_t chip_addr, unsigned int addr, int alen, uint8_t *buffer, 28773845350SSimon Glass int len); 28873845350SSimon Glass 28973845350SSimon Glass /** 29073845350SSimon Glass * i2c_write() - Compatibility function for driver model 29173845350SSimon Glass * 29273845350SSimon Glass * Calls dm_i2c_write() with the device corresponding to @chip_addr, and offset 29373845350SSimon Glass * set to @addr. @alen must match the current setting for the device. 29473845350SSimon Glass */ 29573845350SSimon Glass int i2c_write(uint8_t chip_addr, unsigned int addr, int alen, uint8_t *buffer, 29673845350SSimon Glass int len); 29773845350SSimon Glass 29873845350SSimon Glass /** 29973845350SSimon Glass * i2c_get_bus_num_fdt() - Compatibility function for driver model 30073845350SSimon Glass * 30173845350SSimon Glass * @return the bus number associated with the given device tree node 30273845350SSimon Glass */ 30373845350SSimon Glass int i2c_get_bus_num_fdt(int node); 30473845350SSimon Glass 30573845350SSimon Glass /** 30673845350SSimon Glass * i2c_get_bus_num() - Compatibility function for driver model 30773845350SSimon Glass * 30873845350SSimon Glass * @return the 'current' bus number 30973845350SSimon Glass */ 31073845350SSimon Glass unsigned int i2c_get_bus_num(void); 31173845350SSimon Glass 31273845350SSimon Glass /** 313d744d561SSimon Glass * i2c_set_bus_num() - Compatibility function for driver model 31473845350SSimon Glass * 31573845350SSimon Glass * Sets the 'current' bus 31673845350SSimon Glass */ 31773845350SSimon Glass int i2c_set_bus_num(unsigned int bus); 31873845350SSimon Glass 31973845350SSimon Glass static inline void I2C_SET_BUS(unsigned int bus) 32073845350SSimon Glass { 32173845350SSimon Glass i2c_set_bus_num(bus); 32273845350SSimon Glass } 32373845350SSimon Glass 32473845350SSimon Glass static inline unsigned int I2C_GET_BUS(void) 32573845350SSimon Glass { 32673845350SSimon Glass return i2c_get_bus_num(); 32773845350SSimon Glass } 32873845350SSimon Glass 329d744d561SSimon Glass /** 330d744d561SSimon Glass * i2c_init() - Compatibility function for driver model 331d744d561SSimon Glass * 332d744d561SSimon Glass * This function does nothing. 333d744d561SSimon Glass */ 334d744d561SSimon Glass void i2c_init(int speed, int slaveaddr); 335d744d561SSimon Glass 336d744d561SSimon Glass /** 337d744d561SSimon Glass * board_i2c_init() - Compatibility function for driver model 338d744d561SSimon Glass * 339d744d561SSimon Glass * @param blob Device tree blbo 340d744d561SSimon Glass * @return the number of I2C bus 341d744d561SSimon Glass */ 342d744d561SSimon Glass void board_i2c_init(const void *blob); 343d744d561SSimon Glass 344a2879764SSimon Glass /* 345a2879764SSimon Glass * Compatibility functions for driver model. 346a2879764SSimon Glass */ 347a2879764SSimon Glass uint8_t i2c_reg_read(uint8_t addr, uint8_t reg); 348a2879764SSimon Glass void i2c_reg_write(uint8_t addr, uint8_t reg, uint8_t val); 349a2879764SSimon Glass 35073845350SSimon Glass #endif 35173845350SSimon Glass 352c6202d85SSimon Glass /** 353c6202d85SSimon Glass * struct dm_i2c_ops - driver operations for I2C uclass 354c6202d85SSimon Glass * 355c6202d85SSimon Glass * Drivers should support these operations unless otherwise noted. These 356c6202d85SSimon Glass * operations are intended to be used by uclass code, not directly from 357c6202d85SSimon Glass * other code. 358c6202d85SSimon Glass */ 359c6202d85SSimon Glass struct dm_i2c_ops { 360c6202d85SSimon Glass /** 361c6202d85SSimon Glass * xfer() - transfer a list of I2C messages 362c6202d85SSimon Glass * 363c6202d85SSimon Glass * @bus: Bus to read from 364c6202d85SSimon Glass * @msg: List of messages to transfer 365c6202d85SSimon Glass * @nmsgs: Number of messages in the list 366c6202d85SSimon Glass * @return 0 if OK, -EREMOTEIO if the slave did not ACK a byte, 367c6202d85SSimon Glass * -ECOMM if the speed cannot be supported, -EPROTO if the chip 368c6202d85SSimon Glass * flags cannot be supported, other -ve value on some other error 369c6202d85SSimon Glass */ 370c6202d85SSimon Glass int (*xfer)(struct udevice *bus, struct i2c_msg *msg, int nmsgs); 371c6202d85SSimon Glass 372c6202d85SSimon Glass /** 373c6202d85SSimon Glass * probe_chip() - probe for the presense of a chip address 374c6202d85SSimon Glass * 375c6202d85SSimon Glass * This function is optional. If omitted, the uclass will send a zero 376c6202d85SSimon Glass * length message instead. 377c6202d85SSimon Glass * 378c6202d85SSimon Glass * @bus: Bus to probe 379c6202d85SSimon Glass * @chip_addr: Chip address to probe 380c6202d85SSimon Glass * @chip_flags: Probe flags (enum dm_i2c_chip_flags) 381c6202d85SSimon Glass * @return 0 if chip was found, -EREMOTEIO if not, -ENOSYS to fall back 382c6202d85SSimon Glass * to default probem other -ve value on error 383c6202d85SSimon Glass */ 384c6202d85SSimon Glass int (*probe_chip)(struct udevice *bus, uint chip_addr, uint chip_flags); 385c6202d85SSimon Glass 386c6202d85SSimon Glass /** 387c6202d85SSimon Glass * set_bus_speed() - set the speed of a bus (optional) 388c6202d85SSimon Glass * 389c6202d85SSimon Glass * The bus speed value will be updated by the uclass if this function 390c6202d85SSimon Glass * does not return an error. This method is optional - if it is not 391c6202d85SSimon Glass * provided then the driver can read the speed from 392e564f054SSimon Glass * dev_get_uclass_priv(bus)->speed_hz 393c6202d85SSimon Glass * 394c6202d85SSimon Glass * @bus: Bus to adjust 395c6202d85SSimon Glass * @speed: Requested speed in Hz 396c6202d85SSimon Glass * @return 0 if OK, -EINVAL for invalid values 397c6202d85SSimon Glass */ 398c6202d85SSimon Glass int (*set_bus_speed)(struct udevice *bus, unsigned int speed); 399c6202d85SSimon Glass 400c6202d85SSimon Glass /** 401c6202d85SSimon Glass * get_bus_speed() - get the speed of a bus (optional) 402c6202d85SSimon Glass * 403c6202d85SSimon Glass * Normally this can be provided by the uclass, but if you want your 404c6202d85SSimon Glass * driver to check the bus speed by looking at the hardware, you can 405c6202d85SSimon Glass * implement that here. This method is optional. This method would 406e564f054SSimon Glass * normally be expected to return dev_get_uclass_priv(bus)->speed_hz. 407c6202d85SSimon Glass * 408c6202d85SSimon Glass * @bus: Bus to check 409c6202d85SSimon Glass * @return speed of selected I2C bus in Hz, -ve on error 410c6202d85SSimon Glass */ 411c6202d85SSimon Glass int (*get_bus_speed)(struct udevice *bus); 412c6202d85SSimon Glass 413c6202d85SSimon Glass /** 414c6202d85SSimon Glass * set_flags() - set the flags for a chip (optional) 415c6202d85SSimon Glass * 416c6202d85SSimon Glass * This is generally implemented by the uclass, but drivers can 417c6202d85SSimon Glass * check the value to ensure that unsupported options are not used. 418c6202d85SSimon Glass * This method is optional. If provided, this method will always be 419c6202d85SSimon Glass * called when the flags change. 420c6202d85SSimon Glass * 421c6202d85SSimon Glass * @dev: Chip to adjust 422c6202d85SSimon Glass * @flags: New flags value 423c6202d85SSimon Glass * @return 0 if OK, -EINVAL if value is unsupported 424c6202d85SSimon Glass */ 425c6202d85SSimon Glass int (*set_flags)(struct udevice *dev, uint flags); 426c6202d85SSimon Glass 427c6202d85SSimon Glass /** 428c6202d85SSimon Glass * deblock() - recover a bus that is in an unknown state 429c6202d85SSimon Glass * 430c6202d85SSimon Glass * I2C is a synchronous protocol and resets of the processor in the 431c6202d85SSimon Glass * middle of an access can block the I2C Bus until a powerdown of 432c6202d85SSimon Glass * the full unit is done. This is because slaves can be stuck 433c6202d85SSimon Glass * waiting for addition bus transitions for a transaction that will 434c6202d85SSimon Glass * never complete. Resetting the I2C master does not help. The only 435c6202d85SSimon Glass * way is to force the bus through a series of transitions to make 436c6202d85SSimon Glass * sure that all slaves are done with the transaction. This method 437c6202d85SSimon Glass * performs this 'deblocking' if support by the driver. 438c6202d85SSimon Glass * 439c6202d85SSimon Glass * This method is optional. 440c6202d85SSimon Glass */ 441c6202d85SSimon Glass int (*deblock)(struct udevice *bus); 442c6202d85SSimon Glass }; 443c6202d85SSimon Glass 444c6202d85SSimon Glass #define i2c_get_ops(dev) ((struct dm_i2c_ops *)(dev)->driver->ops) 445c6202d85SSimon Glass 446c6202d85SSimon Glass /** 4473d1957f0SSimon Glass * struct i2c_mux_ops - operations for an I2C mux 4483d1957f0SSimon Glass * 4493d1957f0SSimon Glass * The current mux state is expected to be stored in the mux itself since 4503d1957f0SSimon Glass * it is the only thing that knows how to make things work. The mux can 4513d1957f0SSimon Glass * record the current state and then avoid switching unless it is necessary. 4523d1957f0SSimon Glass * So select() can be skipped if the mux is already in the correct state. 4533d1957f0SSimon Glass * Also deselect() can be made a nop if required. 4543d1957f0SSimon Glass */ 4553d1957f0SSimon Glass struct i2c_mux_ops { 4563d1957f0SSimon Glass /** 4573d1957f0SSimon Glass * select() - select one of of I2C buses attached to a mux 4583d1957f0SSimon Glass * 4593d1957f0SSimon Glass * This will be called when there is no bus currently selected by the 4603d1957f0SSimon Glass * mux. This method does not need to deselect the old bus since 4613d1957f0SSimon Glass * deselect() will be already have been called if necessary. 4623d1957f0SSimon Glass * 4633d1957f0SSimon Glass * @mux: Mux device 4643d1957f0SSimon Glass * @bus: I2C bus to select 4653d1957f0SSimon Glass * @channel: Channel number correponding to the bus to select 4663d1957f0SSimon Glass * @return 0 if OK, -ve on error 4673d1957f0SSimon Glass */ 4683d1957f0SSimon Glass int (*select)(struct udevice *mux, struct udevice *bus, uint channel); 4693d1957f0SSimon Glass 4703d1957f0SSimon Glass /** 4713d1957f0SSimon Glass * deselect() - select one of of I2C buses attached to a mux 4723d1957f0SSimon Glass * 4733d1957f0SSimon Glass * This is used to deselect the currently selected I2C bus. 4743d1957f0SSimon Glass * 4753d1957f0SSimon Glass * @mux: Mux device 4763d1957f0SSimon Glass * @bus: I2C bus to deselect 4773d1957f0SSimon Glass * @channel: Channel number correponding to the bus to deselect 4783d1957f0SSimon Glass * @return 0 if OK, -ve on error 4793d1957f0SSimon Glass */ 4803d1957f0SSimon Glass int (*deselect)(struct udevice *mux, struct udevice *bus, uint channel); 4813d1957f0SSimon Glass }; 4823d1957f0SSimon Glass 4833d1957f0SSimon Glass #define i2c_mux_get_ops(dev) ((struct i2c_mux_ops *)(dev)->driver->ops) 4843d1957f0SSimon Glass 4853d1957f0SSimon Glass /** 486c6202d85SSimon Glass * i2c_get_chip() - get a device to use to access a chip on a bus 487c6202d85SSimon Glass * 488c6202d85SSimon Glass * This returns the device for the given chip address. The device can then 489c6202d85SSimon Glass * be used with calls to i2c_read(), i2c_write(), i2c_probe(), etc. 490c6202d85SSimon Glass * 491c6202d85SSimon Glass * @bus: Bus to examine 492c6202d85SSimon Glass * @chip_addr: Chip address for the new device 49325ab4b03SSimon Glass * @offset_len: Length of a register offset in bytes (normally 1) 494c6202d85SSimon Glass * @devp: Returns pointer to new device if found or -ENODEV if not 495c6202d85SSimon Glass * found 496c6202d85SSimon Glass */ 49725ab4b03SSimon Glass int i2c_get_chip(struct udevice *bus, uint chip_addr, uint offset_len, 49825ab4b03SSimon Glass struct udevice **devp); 499c6202d85SSimon Glass 500c6202d85SSimon Glass /** 501a06728c8SStefan Roese * i2c_get_chip_for_busnum() - get a device to use to access a chip on 502a06728c8SStefan Roese * a bus number 503c6202d85SSimon Glass * 504c6202d85SSimon Glass * This returns the device for the given chip address on a particular bus 505c6202d85SSimon Glass * number. 506c6202d85SSimon Glass * 507c6202d85SSimon Glass * @busnum: Bus number to examine 508c6202d85SSimon Glass * @chip_addr: Chip address for the new device 50925ab4b03SSimon Glass * @offset_len: Length of a register offset in bytes (normally 1) 510c6202d85SSimon Glass * @devp: Returns pointer to new device if found or -ENODEV if not 511c6202d85SSimon Glass * found 512c6202d85SSimon Glass */ 51325ab4b03SSimon Glass int i2c_get_chip_for_busnum(int busnum, int chip_addr, uint offset_len, 51425ab4b03SSimon Glass struct udevice **devp); 515c6202d85SSimon Glass 516c6202d85SSimon Glass /** 517c6202d85SSimon Glass * i2c_chip_ofdata_to_platdata() - Decode standard I2C platform data 518c6202d85SSimon Glass * 519c6202d85SSimon Glass * This decodes the chip address from a device tree node and puts it into 520c6202d85SSimon Glass * its dm_i2c_chip structure. This should be called in your driver's 521c6202d85SSimon Glass * ofdata_to_platdata() method. 522c6202d85SSimon Glass * 523c6202d85SSimon Glass * @blob: Device tree blob 524c6202d85SSimon Glass * @node: Node offset to read from 525c6202d85SSimon Glass * @spi: Place to put the decoded information 526c6202d85SSimon Glass */ 5271704308eSSimon Glass int i2c_chip_ofdata_to_platdata(struct udevice *dev, struct dm_i2c_chip *chip); 528c6202d85SSimon Glass 5297d7db222SSimon Glass /** 5307d7db222SSimon Glass * i2c_dump_msgs() - Dump a list of I2C messages 5317d7db222SSimon Glass * 5327d7db222SSimon Glass * This may be useful for debugging. 5337d7db222SSimon Glass * 5347d7db222SSimon Glass * @msg: Message list to dump 5357d7db222SSimon Glass * @nmsgs: Number of messages 5367d7db222SSimon Glass */ 5377d7db222SSimon Glass void i2c_dump_msgs(struct i2c_msg *msg, int nmsgs); 5387d7db222SSimon Glass 539*b7c25b11SSimon Glass /** 540*b7c25b11SSimon Glass * i2c_emul_find() - Find an emulator for an i2c sandbox device 541*b7c25b11SSimon Glass * 542*b7c25b11SSimon Glass * This looks at the device's 'emul' phandle 543*b7c25b11SSimon Glass * 544*b7c25b11SSimon Glass * @dev: Device to find an emulator for 545*b7c25b11SSimon Glass * @emulp: Returns the associated emulator, if found * 546*b7c25b11SSimon Glass * @return 0 if OK, -ENOENT or -ENODEV if not found 547*b7c25b11SSimon Glass */ 548*b7c25b11SSimon Glass int i2c_emul_find(struct udevice *dev, struct udevice **emulp); 549*b7c25b11SSimon Glass 550*b7c25b11SSimon Glass /** 551*b7c25b11SSimon Glass * i2c_emul_get_device() - Find the device being emulated 552*b7c25b11SSimon Glass * 553*b7c25b11SSimon Glass * Given an emulator this returns the associated device 554*b7c25b11SSimon Glass * 555*b7c25b11SSimon Glass * @emul: Emulator for the device 556*b7c25b11SSimon Glass * @return device that @emul is emulating 557*b7c25b11SSimon Glass */ 558*b7c25b11SSimon Glass struct udevice *i2c_emul_get_device(struct udevice *emul); 559*b7c25b11SSimon Glass 560c6202d85SSimon Glass #ifndef CONFIG_DM_I2C 561c6202d85SSimon Glass 562c6202d85SSimon Glass /* 5631f045217Swdenk * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING 5641f045217Swdenk * 5651f045217Swdenk * The implementation MUST NOT use static or global variables if the 5661f045217Swdenk * I2C routines are used to read SDRAM configuration information 5671f045217Swdenk * because this is done before the memories are initialized. Limited 5681f045217Swdenk * use of stack-based variables are OK (the initial stack size is 5691f045217Swdenk * limited). 5701f045217Swdenk * 5711f045217Swdenk * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING 5721f045217Swdenk */ 5731f045217Swdenk 5741f045217Swdenk /* 5751f045217Swdenk * Configuration items. 5761f045217Swdenk */ 5771f045217Swdenk #define I2C_RXTX_LEN 128 /* maximum tx/rx buffer length */ 5781f045217Swdenk 579385c9ef5SHeiko Schocher #if !defined(CONFIG_SYS_I2C_MAX_HOPS) 580385c9ef5SHeiko Schocher /* no muxes used bus = i2c adapters */ 581385c9ef5SHeiko Schocher #define CONFIG_SYS_I2C_DIRECT_BUS 1 582385c9ef5SHeiko Schocher #define CONFIG_SYS_I2C_MAX_HOPS 0 583385c9ef5SHeiko Schocher #define CONFIG_SYS_NUM_I2C_BUSES ll_entry_count(struct i2c_adapter, i2c) 58479b2d0bbSStefan Roese #else 585385c9ef5SHeiko Schocher /* we use i2c muxes */ 586385c9ef5SHeiko Schocher #undef CONFIG_SYS_I2C_DIRECT_BUS 58779b2d0bbSStefan Roese #endif 58879b2d0bbSStefan Roese 5898c12045aSStefan Roese /* define the I2C bus number for RTC and DTT if not already done */ 5906d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #if !defined(CONFIG_SYS_RTC_BUS_NUM) 5916d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #define CONFIG_SYS_RTC_BUS_NUM 0 5928c12045aSStefan Roese #endif 5936d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #if !defined(CONFIG_SYS_SPD_BUS_NUM) 5946d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #define CONFIG_SYS_SPD_BUS_NUM 0 595d8a8ea5cSMatthias Fuchs #endif 5968c12045aSStefan Roese 597385c9ef5SHeiko Schocher struct i2c_adapter { 598385c9ef5SHeiko Schocher void (*init)(struct i2c_adapter *adap, int speed, 599385c9ef5SHeiko Schocher int slaveaddr); 600385c9ef5SHeiko Schocher int (*probe)(struct i2c_adapter *adap, uint8_t chip); 601385c9ef5SHeiko Schocher int (*read)(struct i2c_adapter *adap, uint8_t chip, 602385c9ef5SHeiko Schocher uint addr, int alen, uint8_t *buffer, 603385c9ef5SHeiko Schocher int len); 604385c9ef5SHeiko Schocher int (*write)(struct i2c_adapter *adap, uint8_t chip, 605385c9ef5SHeiko Schocher uint addr, int alen, uint8_t *buffer, 606385c9ef5SHeiko Schocher int len); 607385c9ef5SHeiko Schocher uint (*set_bus_speed)(struct i2c_adapter *adap, 608385c9ef5SHeiko Schocher uint speed); 609385c9ef5SHeiko Schocher int speed; 610d5243359SHannes Petermaier int waitdelay; 611385c9ef5SHeiko Schocher int slaveaddr; 612385c9ef5SHeiko Schocher int init_done; 613385c9ef5SHeiko Schocher int hwadapnr; 614385c9ef5SHeiko Schocher char *name; 615385c9ef5SHeiko Schocher }; 616385c9ef5SHeiko Schocher 617385c9ef5SHeiko Schocher #define U_BOOT_I2C_MKENT_COMPLETE(_init, _probe, _read, _write, \ 618385c9ef5SHeiko Schocher _set_speed, _speed, _slaveaddr, _hwadapnr, _name) \ 619385c9ef5SHeiko Schocher { \ 620385c9ef5SHeiko Schocher .init = _init, \ 621385c9ef5SHeiko Schocher .probe = _probe, \ 622385c9ef5SHeiko Schocher .read = _read, \ 623385c9ef5SHeiko Schocher .write = _write, \ 624385c9ef5SHeiko Schocher .set_bus_speed = _set_speed, \ 625385c9ef5SHeiko Schocher .speed = _speed, \ 626385c9ef5SHeiko Schocher .slaveaddr = _slaveaddr, \ 627385c9ef5SHeiko Schocher .init_done = 0, \ 628385c9ef5SHeiko Schocher .hwadapnr = _hwadapnr, \ 629385c9ef5SHeiko Schocher .name = #_name \ 630385c9ef5SHeiko Schocher }; 631385c9ef5SHeiko Schocher 632385c9ef5SHeiko Schocher #define U_BOOT_I2C_ADAP_COMPLETE(_name, _init, _probe, _read, _write, \ 633385c9ef5SHeiko Schocher _set_speed, _speed, _slaveaddr, _hwadapnr) \ 634385c9ef5SHeiko Schocher ll_entry_declare(struct i2c_adapter, _name, i2c) = \ 635385c9ef5SHeiko Schocher U_BOOT_I2C_MKENT_COMPLETE(_init, _probe, _read, _write, \ 636385c9ef5SHeiko Schocher _set_speed, _speed, _slaveaddr, _hwadapnr, _name); 637385c9ef5SHeiko Schocher 638385c9ef5SHeiko Schocher struct i2c_adapter *i2c_get_adapter(int index); 639385c9ef5SHeiko Schocher 640385c9ef5SHeiko Schocher #ifndef CONFIG_SYS_I2C_DIRECT_BUS 641385c9ef5SHeiko Schocher struct i2c_mux { 642385c9ef5SHeiko Schocher int id; 643385c9ef5SHeiko Schocher char name[16]; 644385c9ef5SHeiko Schocher }; 645385c9ef5SHeiko Schocher 646385c9ef5SHeiko Schocher struct i2c_next_hop { 647385c9ef5SHeiko Schocher struct i2c_mux mux; 648385c9ef5SHeiko Schocher uint8_t chip; 649385c9ef5SHeiko Schocher uint8_t channel; 650385c9ef5SHeiko Schocher }; 651385c9ef5SHeiko Schocher 652385c9ef5SHeiko Schocher struct i2c_bus_hose { 653385c9ef5SHeiko Schocher int adapter; 654385c9ef5SHeiko Schocher struct i2c_next_hop next_hop[CONFIG_SYS_I2C_MAX_HOPS]; 655385c9ef5SHeiko Schocher }; 656385c9ef5SHeiko Schocher #define I2C_NULL_HOP {{-1, ""}, 0, 0} 657385c9ef5SHeiko Schocher extern struct i2c_bus_hose i2c_bus[]; 658385c9ef5SHeiko Schocher 659385c9ef5SHeiko Schocher #define I2C_ADAPTER(bus) i2c_bus[bus].adapter 660385c9ef5SHeiko Schocher #else 661385c9ef5SHeiko Schocher #define I2C_ADAPTER(bus) bus 662385c9ef5SHeiko Schocher #endif 663385c9ef5SHeiko Schocher #define I2C_BUS gd->cur_i2c_bus 664385c9ef5SHeiko Schocher 665385c9ef5SHeiko Schocher #define I2C_ADAP_NR(bus) i2c_get_adapter(I2C_ADAPTER(bus)) 666385c9ef5SHeiko Schocher #define I2C_ADAP I2C_ADAP_NR(gd->cur_i2c_bus) 667385c9ef5SHeiko Schocher #define I2C_ADAP_HWNR (I2C_ADAP->hwadapnr) 668385c9ef5SHeiko Schocher 669385c9ef5SHeiko Schocher #ifndef CONFIG_SYS_I2C_DIRECT_BUS 670385c9ef5SHeiko Schocher #define I2C_MUX_PCA9540_ID 1 671385c9ef5SHeiko Schocher #define I2C_MUX_PCA9540 {I2C_MUX_PCA9540_ID, "PCA9540B"} 672385c9ef5SHeiko Schocher #define I2C_MUX_PCA9542_ID 2 673385c9ef5SHeiko Schocher #define I2C_MUX_PCA9542 {I2C_MUX_PCA9542_ID, "PCA9542A"} 674385c9ef5SHeiko Schocher #define I2C_MUX_PCA9544_ID 3 675385c9ef5SHeiko Schocher #define I2C_MUX_PCA9544 {I2C_MUX_PCA9544_ID, "PCA9544A"} 676385c9ef5SHeiko Schocher #define I2C_MUX_PCA9547_ID 4 677385c9ef5SHeiko Schocher #define I2C_MUX_PCA9547 {I2C_MUX_PCA9547_ID, "PCA9547A"} 678e6658749SMichael Burr #define I2C_MUX_PCA9548_ID 5 679e6658749SMichael Burr #define I2C_MUX_PCA9548 {I2C_MUX_PCA9548_ID, "PCA9548"} 680385c9ef5SHeiko Schocher #endif 681385c9ef5SHeiko Schocher 68298aed379SHeiko Schocher #ifndef I2C_SOFT_DECLARATIONS 6832eb48ff7SHeiko Schocher # if (defined(CONFIG_AT91RM9200) || \ 6840cf0b931SJens Scharsig defined(CONFIG_AT91SAM9260) || defined(CONFIG_AT91SAM9261) || \ 685cb96a0a4SAndreas Bießmann defined(CONFIG_AT91SAM9263)) 68678132275Sesw@bus-elektronik.de # define I2C_SOFT_DECLARATIONS at91_pio_t *pio = (at91_pio_t *) ATMEL_BASE_PIOA; 68798aed379SHeiko Schocher # else 68898aed379SHeiko Schocher # define I2C_SOFT_DECLARATIONS 68998aed379SHeiko Schocher # endif 69098aed379SHeiko Schocher #endif 691ecf5f077STimur Tabi 6929c90a2c8SPeter Tyser /* 6939c90a2c8SPeter Tyser * Many boards/controllers/drivers don't support an I2C slave interface so 6949c90a2c8SPeter Tyser * provide a default slave address for them for use in common code. A real 6959c90a2c8SPeter Tyser * value for CONFIG_SYS_I2C_SLAVE should be defined for any board which does 6969c90a2c8SPeter Tyser * support a slave interface. 6979c90a2c8SPeter Tyser */ 6989c90a2c8SPeter Tyser #ifndef CONFIG_SYS_I2C_SLAVE 6999c90a2c8SPeter Tyser #define CONFIG_SYS_I2C_SLAVE 0xfe 700ecf5f077STimur Tabi #endif 701ecf5f077STimur Tabi 7021f045217Swdenk /* 7031f045217Swdenk * Initialization, must be called once on start up, may be called 7041f045217Swdenk * repeatedly to change the speed and slave addresses. 7051f045217Swdenk */ 7069d10c2d3SYuan Yao #ifdef CONFIG_SYS_I2C_EARLY_INIT 7079d10c2d3SYuan Yao void i2c_early_init_f(void); 7089d10c2d3SYuan Yao #endif 7091f045217Swdenk void i2c_init(int speed, int slaveaddr); 71006d01dbeSwdenk void i2c_init_board(void); 7111f045217Swdenk 712385c9ef5SHeiko Schocher #ifdef CONFIG_SYS_I2C 713385c9ef5SHeiko Schocher /* 714385c9ef5SHeiko Schocher * i2c_get_bus_num: 715385c9ef5SHeiko Schocher * 716385c9ef5SHeiko Schocher * Returns index of currently active I2C bus. Zero-based. 717385c9ef5SHeiko Schocher */ 718385c9ef5SHeiko Schocher unsigned int i2c_get_bus_num(void); 71967b23a32SHeiko Schocher 720385c9ef5SHeiko Schocher /* 721385c9ef5SHeiko Schocher * i2c_set_bus_num: 722385c9ef5SHeiko Schocher * 723385c9ef5SHeiko Schocher * Change the active I2C bus. Subsequent read/write calls will 724385c9ef5SHeiko Schocher * go to this one. 725385c9ef5SHeiko Schocher * 726385c9ef5SHeiko Schocher * bus - bus index, zero based 727385c9ef5SHeiko Schocher * 728385c9ef5SHeiko Schocher * Returns: 0 on success, not 0 on failure 729385c9ef5SHeiko Schocher * 730385c9ef5SHeiko Schocher */ 731385c9ef5SHeiko Schocher int i2c_set_bus_num(unsigned int bus); 73267b23a32SHeiko Schocher 733385c9ef5SHeiko Schocher /* 734385c9ef5SHeiko Schocher * i2c_init_all(): 735385c9ef5SHeiko Schocher * 736385c9ef5SHeiko Schocher * Initializes all I2C adapters in the system. All i2c_adap structures must 737385c9ef5SHeiko Schocher * be initialized beforehead with function pointers and data, including 738385c9ef5SHeiko Schocher * speed and slaveaddr. Returns 0 on success, non-0 on failure. 739385c9ef5SHeiko Schocher */ 740385c9ef5SHeiko Schocher void i2c_init_all(void); 74167b23a32SHeiko Schocher 742385c9ef5SHeiko Schocher /* 743385c9ef5SHeiko Schocher * Probe the given I2C chip address. Returns 0 if a chip responded, 744385c9ef5SHeiko Schocher * not 0 on failure. 745385c9ef5SHeiko Schocher */ 746385c9ef5SHeiko Schocher int i2c_probe(uint8_t chip); 747385c9ef5SHeiko Schocher 748385c9ef5SHeiko Schocher /* 749385c9ef5SHeiko Schocher * Read/Write interface: 750385c9ef5SHeiko Schocher * chip: I2C chip address, range 0..127 751385c9ef5SHeiko Schocher * addr: Memory (register) address within the chip 752385c9ef5SHeiko Schocher * alen: Number of bytes to use for addr (typically 1, 2 for larger 753385c9ef5SHeiko Schocher * memories, 0 for register type devices with only one 754385c9ef5SHeiko Schocher * register) 755385c9ef5SHeiko Schocher * buffer: Where to read/write the data 756385c9ef5SHeiko Schocher * len: How many bytes to read/write 757385c9ef5SHeiko Schocher * 758385c9ef5SHeiko Schocher * Returns: 0 on success, not 0 on failure 759385c9ef5SHeiko Schocher */ 760385c9ef5SHeiko Schocher int i2c_read(uint8_t chip, unsigned int addr, int alen, 761385c9ef5SHeiko Schocher uint8_t *buffer, int len); 762385c9ef5SHeiko Schocher 763385c9ef5SHeiko Schocher int i2c_write(uint8_t chip, unsigned int addr, int alen, 764385c9ef5SHeiko Schocher uint8_t *buffer, int len); 765385c9ef5SHeiko Schocher 766385c9ef5SHeiko Schocher /* 767385c9ef5SHeiko Schocher * Utility routines to read/write registers. 768385c9ef5SHeiko Schocher */ 769385c9ef5SHeiko Schocher uint8_t i2c_reg_read(uint8_t addr, uint8_t reg); 770385c9ef5SHeiko Schocher 771385c9ef5SHeiko Schocher void i2c_reg_write(uint8_t addr, uint8_t reg, uint8_t val); 772385c9ef5SHeiko Schocher 773385c9ef5SHeiko Schocher /* 774385c9ef5SHeiko Schocher * i2c_set_bus_speed: 775385c9ef5SHeiko Schocher * 776385c9ef5SHeiko Schocher * Change the speed of the active I2C bus 777385c9ef5SHeiko Schocher * 778385c9ef5SHeiko Schocher * speed - bus speed in Hz 779385c9ef5SHeiko Schocher * 780385c9ef5SHeiko Schocher * Returns: new bus speed 781385c9ef5SHeiko Schocher * 782385c9ef5SHeiko Schocher */ 783385c9ef5SHeiko Schocher unsigned int i2c_set_bus_speed(unsigned int speed); 784385c9ef5SHeiko Schocher 785385c9ef5SHeiko Schocher /* 786385c9ef5SHeiko Schocher * i2c_get_bus_speed: 787385c9ef5SHeiko Schocher * 788385c9ef5SHeiko Schocher * Returns speed of currently active I2C bus in Hz 789385c9ef5SHeiko Schocher */ 790385c9ef5SHeiko Schocher 791385c9ef5SHeiko Schocher unsigned int i2c_get_bus_speed(void); 792385c9ef5SHeiko Schocher 793385c9ef5SHeiko Schocher #else 79467b23a32SHeiko Schocher 7951f045217Swdenk /* 7961f045217Swdenk * Probe the given I2C chip address. Returns 0 if a chip responded, 7971f045217Swdenk * not 0 on failure. 7981f045217Swdenk */ 7991f045217Swdenk int i2c_probe(uchar chip); 8001f045217Swdenk 8011f045217Swdenk /* 8021f045217Swdenk * Read/Write interface: 8031f045217Swdenk * chip: I2C chip address, range 0..127 8041f045217Swdenk * addr: Memory (register) address within the chip 8051f045217Swdenk * alen: Number of bytes to use for addr (typically 1, 2 for larger 8061f045217Swdenk * memories, 0 for register type devices with only one 8071f045217Swdenk * register) 8081f045217Swdenk * buffer: Where to read/write the data 8091f045217Swdenk * len: How many bytes to read/write 8101f045217Swdenk * 8111f045217Swdenk * Returns: 0 on success, not 0 on failure 8121f045217Swdenk */ 8131f045217Swdenk int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len); 8141f045217Swdenk int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len); 8151f045217Swdenk 8161f045217Swdenk /* 8171f045217Swdenk * Utility routines to read/write registers. 8181f045217Swdenk */ 819ecf5f077STimur Tabi static inline u8 i2c_reg_read(u8 addr, u8 reg) 820ecf5f077STimur Tabi { 821ecf5f077STimur Tabi u8 buf; 822ecf5f077STimur Tabi 823ecf5f077STimur Tabi #ifdef DEBUG 824ecf5f077STimur Tabi printf("%s: addr=0x%02x, reg=0x%02x\n", __func__, addr, reg); 825ecf5f077STimur Tabi #endif 826ecf5f077STimur Tabi 827ecf5f077STimur Tabi i2c_read(addr, reg, 1, &buf, 1); 828ecf5f077STimur Tabi 829ecf5f077STimur Tabi return buf; 830ecf5f077STimur Tabi } 831ecf5f077STimur Tabi 832ecf5f077STimur Tabi static inline void i2c_reg_write(u8 addr, u8 reg, u8 val) 833ecf5f077STimur Tabi { 834ecf5f077STimur Tabi #ifdef DEBUG 835ecf5f077STimur Tabi printf("%s: addr=0x%02x, reg=0x%02x, val=0x%02x\n", 836ecf5f077STimur Tabi __func__, addr, reg, val); 837ecf5f077STimur Tabi #endif 838ecf5f077STimur Tabi 839ecf5f077STimur Tabi i2c_write(addr, reg, 1, &val, 1); 840ecf5f077STimur Tabi } 8411f045217Swdenk 842bb99ad6dSBen Warren /* 843bb99ad6dSBen Warren * Functions for setting the current I2C bus and its speed 844bb99ad6dSBen Warren */ 845bb99ad6dSBen Warren 846bb99ad6dSBen Warren /* 847bb99ad6dSBen Warren * i2c_set_bus_num: 848bb99ad6dSBen Warren * 849bb99ad6dSBen Warren * Change the active I2C bus. Subsequent read/write calls will 850bb99ad6dSBen Warren * go to this one. 851bb99ad6dSBen Warren * 852bb99ad6dSBen Warren * bus - bus index, zero based 853bb99ad6dSBen Warren * 854bb99ad6dSBen Warren * Returns: 0 on success, not 0 on failure 855bb99ad6dSBen Warren * 856bb99ad6dSBen Warren */ 8579ca880a2STimur Tabi int i2c_set_bus_num(unsigned int bus); 858bb99ad6dSBen Warren 859bb99ad6dSBen Warren /* 860bb99ad6dSBen Warren * i2c_get_bus_num: 861bb99ad6dSBen Warren * 862bb99ad6dSBen Warren * Returns index of currently active I2C bus. Zero-based. 863bb99ad6dSBen Warren */ 864bb99ad6dSBen Warren 8659ca880a2STimur Tabi unsigned int i2c_get_bus_num(void); 866bb99ad6dSBen Warren 867bb99ad6dSBen Warren /* 868bb99ad6dSBen Warren * i2c_set_bus_speed: 869bb99ad6dSBen Warren * 870bb99ad6dSBen Warren * Change the speed of the active I2C bus 871bb99ad6dSBen Warren * 872bb99ad6dSBen Warren * speed - bus speed in Hz 873bb99ad6dSBen Warren * 874bb99ad6dSBen Warren * Returns: 0 on success, not 0 on failure 875bb99ad6dSBen Warren * 876bb99ad6dSBen Warren */ 8779ca880a2STimur Tabi int i2c_set_bus_speed(unsigned int); 878bb99ad6dSBen Warren 879bb99ad6dSBen Warren /* 880bb99ad6dSBen Warren * i2c_get_bus_speed: 881bb99ad6dSBen Warren * 882bb99ad6dSBen Warren * Returns speed of currently active I2C bus in Hz 883bb99ad6dSBen Warren */ 884bb99ad6dSBen Warren 8859ca880a2STimur Tabi unsigned int i2c_get_bus_speed(void); 886385c9ef5SHeiko Schocher #endif /* CONFIG_SYS_I2C */ 887385c9ef5SHeiko Schocher 888385c9ef5SHeiko Schocher /* 889385c9ef5SHeiko Schocher * only for backwardcompatibility, should go away if we switched 890385c9ef5SHeiko Schocher * completely to new multibus support. 891385c9ef5SHeiko Schocher */ 892385c9ef5SHeiko Schocher #if defined(CONFIG_SYS_I2C) || defined(CONFIG_I2C_MULTI_BUS) 893385c9ef5SHeiko Schocher # if !defined(CONFIG_SYS_MAX_I2C_BUS) 894385c9ef5SHeiko Schocher # define CONFIG_SYS_MAX_I2C_BUS 2 895385c9ef5SHeiko Schocher # endif 896ea0f73abSŁukasz Majewski # define I2C_MULTI_BUS 1 897385c9ef5SHeiko Schocher #else 898385c9ef5SHeiko Schocher # define CONFIG_SYS_MAX_I2C_BUS 1 899385c9ef5SHeiko Schocher # define I2C_MULTI_BUS 0 900385c9ef5SHeiko Schocher #endif 901bb99ad6dSBen Warren 902cd7b4e82SMarek Vasut /* NOTE: These two functions MUST be always_inline to avoid code growth! */ 903cd7b4e82SMarek Vasut static inline unsigned int I2C_GET_BUS(void) __attribute__((always_inline)); 904cd7b4e82SMarek Vasut static inline unsigned int I2C_GET_BUS(void) 905cd7b4e82SMarek Vasut { 906cd7b4e82SMarek Vasut return I2C_MULTI_BUS ? i2c_get_bus_num() : 0; 907cd7b4e82SMarek Vasut } 908cd7b4e82SMarek Vasut 909cd7b4e82SMarek Vasut static inline void I2C_SET_BUS(unsigned int bus) __attribute__((always_inline)); 910cd7b4e82SMarek Vasut static inline void I2C_SET_BUS(unsigned int bus) 911cd7b4e82SMarek Vasut { 912cd7b4e82SMarek Vasut if (I2C_MULTI_BUS) 913cd7b4e82SMarek Vasut i2c_set_bus_num(bus); 914cd7b4e82SMarek Vasut } 915cd7b4e82SMarek Vasut 9167ca8f73aSŁukasz Majewski /* Multi I2C definitions */ 9177ca8f73aSŁukasz Majewski enum { 9187ca8f73aSŁukasz Majewski I2C_0, I2C_1, I2C_2, I2C_3, I2C_4, I2C_5, I2C_6, I2C_7, 9197ca8f73aSŁukasz Majewski I2C_8, I2C_9, I2C_10, 9207ca8f73aSŁukasz Majewski }; 9217ca8f73aSŁukasz Majewski 922a9d2ae70SRajeshwari Shinde /** 923a9d2ae70SRajeshwari Shinde * Get FDT values for i2c bus. 924a9d2ae70SRajeshwari Shinde * 925a9d2ae70SRajeshwari Shinde * @param blob Device tree blbo 926a9d2ae70SRajeshwari Shinde * @return the number of I2C bus 927a9d2ae70SRajeshwari Shinde */ 928a9d2ae70SRajeshwari Shinde void board_i2c_init(const void *blob); 929a9d2ae70SRajeshwari Shinde 930a9d2ae70SRajeshwari Shinde /** 931a9d2ae70SRajeshwari Shinde * Find the I2C bus number by given a FDT I2C node. 932a9d2ae70SRajeshwari Shinde * 933a9d2ae70SRajeshwari Shinde * @param blob Device tree blbo 934a9d2ae70SRajeshwari Shinde * @param node FDT I2C node to find 935a9d2ae70SRajeshwari Shinde * @return the number of I2C bus (zero based), or -1 on error 936a9d2ae70SRajeshwari Shinde */ 937a9d2ae70SRajeshwari Shinde int i2c_get_bus_num_fdt(int node); 938a9d2ae70SRajeshwari Shinde 939a9d2ae70SRajeshwari Shinde /** 940a9d2ae70SRajeshwari Shinde * Reset the I2C bus represented by the given a FDT I2C node. 941a9d2ae70SRajeshwari Shinde * 942a9d2ae70SRajeshwari Shinde * @param blob Device tree blbo 943a9d2ae70SRajeshwari Shinde * @param node FDT I2C node to find 944a9d2ae70SRajeshwari Shinde * @return 0 if port was reset, -1 if not found 945a9d2ae70SRajeshwari Shinde */ 946a9d2ae70SRajeshwari Shinde int i2c_reset_port_fdt(const void *blob, int node); 947c6202d85SSimon Glass 948c6202d85SSimon Glass #endif /* !CONFIG_DM_I2C */ 949c6202d85SSimon Glass 9501f045217Swdenk #endif /* _I2C_H_ */ 951