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 #ifdef CONFIG_DM_I2C 29c6202d85SSimon Glass 30c6202d85SSimon Glass enum dm_i2c_chip_flags { 31c6202d85SSimon Glass DM_I2C_CHIP_10BIT = 1 << 0, /* Use 10-bit addressing */ 32c6202d85SSimon Glass DM_I2C_CHIP_RD_ADDRESS = 1 << 1, /* Send address for each read byte */ 33c6202d85SSimon Glass DM_I2C_CHIP_WR_ADDRESS = 1 << 2, /* Send address for each write byte */ 34c6202d85SSimon Glass }; 35c6202d85SSimon Glass 36c6202d85SSimon Glass /** 37c6202d85SSimon Glass * struct dm_i2c_chip - information about an i2c chip 38c6202d85SSimon Glass * 39c6202d85SSimon Glass * An I2C chip is a device on the I2C bus. It sits at a particular address 40c6202d85SSimon Glass * and normally supports 7-bit or 10-bit addressing. 41c6202d85SSimon Glass * 42c6202d85SSimon Glass * To obtain this structure, use dev_get_parentdata(dev) where dev is the 43c6202d85SSimon Glass * chip to examine. 44c6202d85SSimon Glass * 45c6202d85SSimon Glass * @chip_addr: Chip address on bus 46c6202d85SSimon Glass * @offset_len: Length of offset in bytes. A single byte offset can 47c6202d85SSimon Glass * represent up to 256 bytes. A value larger than 1 may be 48c6202d85SSimon Glass * needed for larger devices. 49c6202d85SSimon Glass * @flags: Flags for this chip (dm_i2c_chip_flags) 50c6202d85SSimon Glass * @emul: Emulator for this chip address (only used for emulation) 51c6202d85SSimon Glass */ 52c6202d85SSimon Glass struct dm_i2c_chip { 53c6202d85SSimon Glass uint chip_addr; 54c6202d85SSimon Glass uint offset_len; 55c6202d85SSimon Glass uint flags; 56c6202d85SSimon Glass #ifdef CONFIG_SANDBOX 57c6202d85SSimon Glass struct udevice *emul; 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 * 68c6202d85SSimon Glass * To obtain this structure, use bus->uclass_priv where bus is the I2C 69c6202d85SSimon Glass * 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 77c6202d85SSimon Glass /** 78f9a4c2daSSimon Glass * dm_i2c_read() - read bytes from an I2C chip 79c6202d85SSimon Glass * 80c6202d85SSimon Glass * To obtain an I2C device (called a 'chip') given the I2C bus address you 81c6202d85SSimon Glass * can use i2c_get_chip(). To obtain a bus by bus number use 82c6202d85SSimon Glass * uclass_get_device_by_seq(UCLASS_I2C, <bus number>). 83c6202d85SSimon Glass * 84c6202d85SSimon Glass * To set the address length of a devce use i2c_set_addr_len(). It 85c6202d85SSimon Glass * defaults to 1. 86c6202d85SSimon Glass * 87c6202d85SSimon Glass * @dev: Chip to read from 88c6202d85SSimon Glass * @offset: Offset within chip to start reading 89c6202d85SSimon Glass * @buffer: Place to put data 90c6202d85SSimon Glass * @len: Number of bytes to read 91c6202d85SSimon Glass * 92c6202d85SSimon Glass * @return 0 on success, -ve on failure 93c6202d85SSimon Glass */ 94f9a4c2daSSimon Glass int dm_i2c_read(struct udevice *dev, uint offset, uint8_t *buffer, int len); 95c6202d85SSimon Glass 96c6202d85SSimon Glass /** 97f9a4c2daSSimon Glass * dm_i2c_write() - write bytes to an I2C chip 98c6202d85SSimon Glass * 99f9a4c2daSSimon Glass * See notes for dm_i2c_read() above. 100c6202d85SSimon Glass * 101c6202d85SSimon Glass * @dev: Chip to write to 102c6202d85SSimon Glass * @offset: Offset within chip to start writing 103c6202d85SSimon Glass * @buffer: Buffer containing data to write 104c6202d85SSimon Glass * @len: Number of bytes to write 105c6202d85SSimon Glass * 106c6202d85SSimon Glass * @return 0 on success, -ve on failure 107c6202d85SSimon Glass */ 108f9a4c2daSSimon Glass int dm_i2c_write(struct udevice *dev, uint offset, const uint8_t *buffer, 109c6202d85SSimon Glass int len); 110c6202d85SSimon Glass 111c6202d85SSimon Glass /** 112f9a4c2daSSimon Glass * dm_i2c_probe() - probe a particular chip address 113c6202d85SSimon Glass * 114c6202d85SSimon Glass * This can be useful to check for the existence of a chip on the bus. 115c6202d85SSimon Glass * It is typically implemented by writing the chip address to the bus 116c6202d85SSimon Glass * and checking that the chip replies with an ACK. 117c6202d85SSimon Glass * 118c6202d85SSimon Glass * @bus: Bus to probe 119c6202d85SSimon Glass * @chip_addr: 7-bit address to probe (10-bit and others are not supported) 120c6202d85SSimon Glass * @chip_flags: Flags for the probe (see enum dm_i2c_chip_flags) 121c6202d85SSimon Glass * @devp: Returns the device found, or NULL if none 122c6202d85SSimon Glass * @return 0 if a chip was found at that address, -ve if not 123c6202d85SSimon Glass */ 124f9a4c2daSSimon Glass int dm_i2c_probe(struct udevice *bus, uint chip_addr, uint chip_flags, 125c6202d85SSimon Glass struct udevice **devp); 126c6202d85SSimon Glass 127c6202d85SSimon Glass /** 128c6202d85SSimon Glass * i2c_set_bus_speed() - set the speed of a bus 129c6202d85SSimon Glass * 130c6202d85SSimon Glass * @bus: Bus to adjust 131c6202d85SSimon Glass * @speed: Requested speed in Hz 132c6202d85SSimon Glass * @return 0 if OK, -EINVAL for invalid values 133c6202d85SSimon Glass */ 134c6202d85SSimon Glass int i2c_set_bus_speed(struct udevice *bus, unsigned int speed); 135c6202d85SSimon Glass 136c6202d85SSimon Glass /** 137c6202d85SSimon Glass * i2c_get_bus_speed() - get the speed of a bus 138c6202d85SSimon Glass * 139c6202d85SSimon Glass * @bus: Bus to check 140c6202d85SSimon Glass * @return speed of selected I2C bus in Hz, -ve on error 141c6202d85SSimon Glass */ 142c6202d85SSimon Glass int i2c_get_bus_speed(struct udevice *bus); 143c6202d85SSimon Glass 144c6202d85SSimon Glass /** 145c6202d85SSimon Glass * i2c_set_chip_flags() - set flags for a chip 146c6202d85SSimon Glass * 147c6202d85SSimon Glass * Typically addresses are 7 bits, but for 10-bit addresses you should set 148c6202d85SSimon Glass * flags to DM_I2C_CHIP_10BIT. All accesses will then use 10-bit addressing. 149c6202d85SSimon Glass * 150c6202d85SSimon Glass * @dev: Chip to adjust 151c6202d85SSimon Glass * @flags: New flags 152c6202d85SSimon Glass * @return 0 if OK, -EINVAL if value is unsupported, other -ve value on error 153c6202d85SSimon Glass */ 154c6202d85SSimon Glass int i2c_set_chip_flags(struct udevice *dev, uint flags); 155c6202d85SSimon Glass 156c6202d85SSimon Glass /** 157c6202d85SSimon Glass * i2c_get_chip_flags() - get flags for a chip 158c6202d85SSimon Glass * 159c6202d85SSimon Glass * @dev: Chip to check 160c6202d85SSimon Glass * @flagsp: Place to put flags 161c6202d85SSimon Glass * @return 0 if OK, other -ve value on error 162c6202d85SSimon Glass */ 163c6202d85SSimon Glass int i2c_get_chip_flags(struct udevice *dev, uint *flagsp); 164c6202d85SSimon Glass 165c6202d85SSimon Glass /** 166c6202d85SSimon Glass * i2c_set_offset_len() - set the offset length for a chip 167c6202d85SSimon Glass * 168c6202d85SSimon Glass * The offset used to access a chip may be up to 4 bytes long. Typically it 169c6202d85SSimon Glass * is only 1 byte, which is enough for chips with 256 bytes of memory or 170c6202d85SSimon Glass * registers. The default value is 1, but you can call this function to 171c6202d85SSimon Glass * change it. 172c6202d85SSimon Glass * 173c6202d85SSimon Glass * @offset_len: New offset length value (typically 1 or 2) 174c6202d85SSimon Glass */ 175c6202d85SSimon Glass 176c6202d85SSimon Glass int i2c_set_chip_offset_len(struct udevice *dev, uint offset_len); 177c6202d85SSimon Glass /** 178c6202d85SSimon Glass * i2c_deblock() - recover a bus that is in an unknown state 179c6202d85SSimon Glass * 180c6202d85SSimon Glass * See the deblock() method in 'struct dm_i2c_ops' for full information 181c6202d85SSimon Glass * 182c6202d85SSimon Glass * @bus: Bus to recover 183c6202d85SSimon Glass * @return 0 if OK, -ve on error 184c6202d85SSimon Glass */ 185c6202d85SSimon Glass int i2c_deblock(struct udevice *bus); 186c6202d85SSimon Glass 18773845350SSimon Glass #ifdef CONFIG_DM_I2C_COMPAT 18873845350SSimon Glass /** 18973845350SSimon Glass * i2c_probe() - Compatibility function for driver model 19073845350SSimon Glass * 19173845350SSimon Glass * Calls dm_i2c_probe() on the current bus 19273845350SSimon Glass */ 19373845350SSimon Glass int i2c_probe(uint8_t chip_addr); 19473845350SSimon Glass 19573845350SSimon Glass /** 19673845350SSimon Glass * i2c_read() - Compatibility function for driver model 19773845350SSimon Glass * 19873845350SSimon Glass * Calls dm_i2c_read() with the device corresponding to @chip_addr, and offset 19973845350SSimon Glass * set to @addr. @alen must match the current setting for the device. 20073845350SSimon Glass */ 20173845350SSimon Glass int i2c_read(uint8_t chip_addr, unsigned int addr, int alen, uint8_t *buffer, 20273845350SSimon Glass int len); 20373845350SSimon Glass 20473845350SSimon Glass /** 20573845350SSimon Glass * i2c_write() - Compatibility function for driver model 20673845350SSimon Glass * 20773845350SSimon Glass * Calls dm_i2c_write() with the device corresponding to @chip_addr, and offset 20873845350SSimon Glass * set to @addr. @alen must match the current setting for the device. 20973845350SSimon Glass */ 21073845350SSimon Glass int i2c_write(uint8_t chip_addr, unsigned int addr, int alen, uint8_t *buffer, 21173845350SSimon Glass int len); 21273845350SSimon Glass 21373845350SSimon Glass /** 21473845350SSimon Glass * i2c_get_bus_num_fdt() - Compatibility function for driver model 21573845350SSimon Glass * 21673845350SSimon Glass * @return the bus number associated with the given device tree node 21773845350SSimon Glass */ 21873845350SSimon Glass int i2c_get_bus_num_fdt(int node); 21973845350SSimon Glass 22073845350SSimon Glass /** 22173845350SSimon Glass * i2c_get_bus_num() - Compatibility function for driver model 22273845350SSimon Glass * 22373845350SSimon Glass * @return the 'current' bus number 22473845350SSimon Glass */ 22573845350SSimon Glass unsigned int i2c_get_bus_num(void); 22673845350SSimon Glass 22773845350SSimon Glass /** 22873845350SSimon Glass * i2c_set_bus_num(): Compatibility function for driver model 22973845350SSimon Glass * 23073845350SSimon Glass * Sets the 'current' bus 23173845350SSimon Glass */ 23273845350SSimon Glass int i2c_set_bus_num(unsigned int bus); 23373845350SSimon Glass 23473845350SSimon Glass static inline void I2C_SET_BUS(unsigned int bus) 23573845350SSimon Glass { 23673845350SSimon Glass i2c_set_bus_num(bus); 23773845350SSimon Glass } 23873845350SSimon Glass 23973845350SSimon Glass static inline unsigned int I2C_GET_BUS(void) 24073845350SSimon Glass { 24173845350SSimon Glass return i2c_get_bus_num(); 24273845350SSimon Glass } 24373845350SSimon Glass 24473845350SSimon Glass #endif 24573845350SSimon Glass 246c6202d85SSimon Glass /* 247c6202d85SSimon Glass * Not all of these flags are implemented in the U-Boot API 248c6202d85SSimon Glass */ 249c6202d85SSimon Glass enum dm_i2c_msg_flags { 250c6202d85SSimon Glass I2C_M_TEN = 0x0010, /* ten-bit chip address */ 251c6202d85SSimon Glass I2C_M_RD = 0x0001, /* read data, from slave to master */ 252c6202d85SSimon Glass I2C_M_STOP = 0x8000, /* send stop after this message */ 253c6202d85SSimon Glass I2C_M_NOSTART = 0x4000, /* no start before this message */ 254c6202d85SSimon Glass I2C_M_REV_DIR_ADDR = 0x2000, /* invert polarity of R/W bit */ 255c6202d85SSimon Glass I2C_M_IGNORE_NAK = 0x1000, /* continue after NAK */ 256c6202d85SSimon Glass I2C_M_NO_RD_ACK = 0x0800, /* skip the Ack bit on reads */ 257c6202d85SSimon Glass I2C_M_RECV_LEN = 0x0400, /* length is first received byte */ 258c6202d85SSimon Glass }; 259c6202d85SSimon Glass 260c6202d85SSimon Glass /** 261c6202d85SSimon Glass * struct i2c_msg - an I2C message 262c6202d85SSimon Glass * 263c6202d85SSimon Glass * @addr: Slave address 264c6202d85SSimon Glass * @flags: Flags (see enum dm_i2c_msg_flags) 265c6202d85SSimon Glass * @len: Length of buffer in bytes, may be 0 for a probe 266c6202d85SSimon Glass * @buf: Buffer to send/receive, or NULL if no data 267c6202d85SSimon Glass */ 268c6202d85SSimon Glass struct i2c_msg { 269c6202d85SSimon Glass uint addr; 270c6202d85SSimon Glass uint flags; 271c6202d85SSimon Glass uint len; 272c6202d85SSimon Glass u8 *buf; 273c6202d85SSimon Glass }; 274c6202d85SSimon Glass 275c6202d85SSimon Glass /** 276c6202d85SSimon Glass * struct i2c_msg_list - a list of I2C messages 277c6202d85SSimon Glass * 278c6202d85SSimon Glass * This is called i2c_rdwr_ioctl_data in Linux but the name does not seem 279c6202d85SSimon Glass * appropriate in U-Boot. 280c6202d85SSimon Glass * 281c6202d85SSimon Glass * @msg: Pointer to i2c_msg array 282c6202d85SSimon Glass * @nmsgs: Number of elements in the array 283c6202d85SSimon Glass */ 284c6202d85SSimon Glass struct i2c_msg_list { 285c6202d85SSimon Glass struct i2c_msg *msgs; 286c6202d85SSimon Glass uint nmsgs; 287c6202d85SSimon Glass }; 288c6202d85SSimon Glass 289c6202d85SSimon Glass /** 290c6202d85SSimon Glass * struct dm_i2c_ops - driver operations for I2C uclass 291c6202d85SSimon Glass * 292c6202d85SSimon Glass * Drivers should support these operations unless otherwise noted. These 293c6202d85SSimon Glass * operations are intended to be used by uclass code, not directly from 294c6202d85SSimon Glass * other code. 295c6202d85SSimon Glass */ 296c6202d85SSimon Glass struct dm_i2c_ops { 297c6202d85SSimon Glass /** 298c6202d85SSimon Glass * xfer() - transfer a list of I2C messages 299c6202d85SSimon Glass * 300c6202d85SSimon Glass * @bus: Bus to read from 301c6202d85SSimon Glass * @msg: List of messages to transfer 302c6202d85SSimon Glass * @nmsgs: Number of messages in the list 303c6202d85SSimon Glass * @return 0 if OK, -EREMOTEIO if the slave did not ACK a byte, 304c6202d85SSimon Glass * -ECOMM if the speed cannot be supported, -EPROTO if the chip 305c6202d85SSimon Glass * flags cannot be supported, other -ve value on some other error 306c6202d85SSimon Glass */ 307c6202d85SSimon Glass int (*xfer)(struct udevice *bus, struct i2c_msg *msg, int nmsgs); 308c6202d85SSimon Glass 309c6202d85SSimon Glass /** 310c6202d85SSimon Glass * probe_chip() - probe for the presense of a chip address 311c6202d85SSimon Glass * 312c6202d85SSimon Glass * This function is optional. If omitted, the uclass will send a zero 313c6202d85SSimon Glass * length message instead. 314c6202d85SSimon Glass * 315c6202d85SSimon Glass * @bus: Bus to probe 316c6202d85SSimon Glass * @chip_addr: Chip address to probe 317c6202d85SSimon Glass * @chip_flags: Probe flags (enum dm_i2c_chip_flags) 318c6202d85SSimon Glass * @return 0 if chip was found, -EREMOTEIO if not, -ENOSYS to fall back 319c6202d85SSimon Glass * to default probem other -ve value on error 320c6202d85SSimon Glass */ 321c6202d85SSimon Glass int (*probe_chip)(struct udevice *bus, uint chip_addr, uint chip_flags); 322c6202d85SSimon Glass 323c6202d85SSimon Glass /** 324c6202d85SSimon Glass * set_bus_speed() - set the speed of a bus (optional) 325c6202d85SSimon Glass * 326c6202d85SSimon Glass * The bus speed value will be updated by the uclass if this function 327c6202d85SSimon Glass * does not return an error. This method is optional - if it is not 328c6202d85SSimon Glass * provided then the driver can read the speed from 329c6202d85SSimon Glass * bus->uclass_priv->speed_hz 330c6202d85SSimon Glass * 331c6202d85SSimon Glass * @bus: Bus to adjust 332c6202d85SSimon Glass * @speed: Requested speed in Hz 333c6202d85SSimon Glass * @return 0 if OK, -EINVAL for invalid values 334c6202d85SSimon Glass */ 335c6202d85SSimon Glass int (*set_bus_speed)(struct udevice *bus, unsigned int speed); 336c6202d85SSimon Glass 337c6202d85SSimon Glass /** 338c6202d85SSimon Glass * get_bus_speed() - get the speed of a bus (optional) 339c6202d85SSimon Glass * 340c6202d85SSimon Glass * Normally this can be provided by the uclass, but if you want your 341c6202d85SSimon Glass * driver to check the bus speed by looking at the hardware, you can 342c6202d85SSimon Glass * implement that here. This method is optional. This method would 343c6202d85SSimon Glass * normally be expected to return bus->uclass_priv->speed_hz. 344c6202d85SSimon Glass * 345c6202d85SSimon Glass * @bus: Bus to check 346c6202d85SSimon Glass * @return speed of selected I2C bus in Hz, -ve on error 347c6202d85SSimon Glass */ 348c6202d85SSimon Glass int (*get_bus_speed)(struct udevice *bus); 349c6202d85SSimon Glass 350c6202d85SSimon Glass /** 351c6202d85SSimon Glass * set_flags() - set the flags for a chip (optional) 352c6202d85SSimon Glass * 353c6202d85SSimon Glass * This is generally implemented by the uclass, but drivers can 354c6202d85SSimon Glass * check the value to ensure that unsupported options are not used. 355c6202d85SSimon Glass * This method is optional. If provided, this method will always be 356c6202d85SSimon Glass * called when the flags change. 357c6202d85SSimon Glass * 358c6202d85SSimon Glass * @dev: Chip to adjust 359c6202d85SSimon Glass * @flags: New flags value 360c6202d85SSimon Glass * @return 0 if OK, -EINVAL if value is unsupported 361c6202d85SSimon Glass */ 362c6202d85SSimon Glass int (*set_flags)(struct udevice *dev, uint flags); 363c6202d85SSimon Glass 364c6202d85SSimon Glass /** 365c6202d85SSimon Glass * deblock() - recover a bus that is in an unknown state 366c6202d85SSimon Glass * 367c6202d85SSimon Glass * I2C is a synchronous protocol and resets of the processor in the 368c6202d85SSimon Glass * middle of an access can block the I2C Bus until a powerdown of 369c6202d85SSimon Glass * the full unit is done. This is because slaves can be stuck 370c6202d85SSimon Glass * waiting for addition bus transitions for a transaction that will 371c6202d85SSimon Glass * never complete. Resetting the I2C master does not help. The only 372c6202d85SSimon Glass * way is to force the bus through a series of transitions to make 373c6202d85SSimon Glass * sure that all slaves are done with the transaction. This method 374c6202d85SSimon Glass * performs this 'deblocking' if support by the driver. 375c6202d85SSimon Glass * 376c6202d85SSimon Glass * This method is optional. 377c6202d85SSimon Glass */ 378c6202d85SSimon Glass int (*deblock)(struct udevice *bus); 379c6202d85SSimon Glass }; 380c6202d85SSimon Glass 381c6202d85SSimon Glass #define i2c_get_ops(dev) ((struct dm_i2c_ops *)(dev)->driver->ops) 382c6202d85SSimon Glass 383c6202d85SSimon Glass /** 384c6202d85SSimon Glass * i2c_get_chip() - get a device to use to access a chip on a bus 385c6202d85SSimon Glass * 386c6202d85SSimon Glass * This returns the device for the given chip address. The device can then 387c6202d85SSimon Glass * be used with calls to i2c_read(), i2c_write(), i2c_probe(), etc. 388c6202d85SSimon Glass * 389c6202d85SSimon Glass * @bus: Bus to examine 390c6202d85SSimon Glass * @chip_addr: Chip address for the new device 391*25ab4b03SSimon Glass * @offset_len: Length of a register offset in bytes (normally 1) 392c6202d85SSimon Glass * @devp: Returns pointer to new device if found or -ENODEV if not 393c6202d85SSimon Glass * found 394c6202d85SSimon Glass */ 395*25ab4b03SSimon Glass int i2c_get_chip(struct udevice *bus, uint chip_addr, uint offset_len, 396*25ab4b03SSimon Glass struct udevice **devp); 397c6202d85SSimon Glass 398c6202d85SSimon Glass /** 399c6202d85SSimon Glass * i2c_get_chip() - get a device to use to access a chip on a bus number 400c6202d85SSimon Glass * 401c6202d85SSimon Glass * This returns the device for the given chip address on a particular bus 402c6202d85SSimon Glass * number. 403c6202d85SSimon Glass * 404c6202d85SSimon Glass * @busnum: Bus number to examine 405c6202d85SSimon Glass * @chip_addr: Chip address for the new device 406*25ab4b03SSimon Glass * @offset_len: Length of a register offset in bytes (normally 1) 407c6202d85SSimon Glass * @devp: Returns pointer to new device if found or -ENODEV if not 408c6202d85SSimon Glass * found 409c6202d85SSimon Glass */ 410*25ab4b03SSimon Glass int i2c_get_chip_for_busnum(int busnum, int chip_addr, uint offset_len, 411*25ab4b03SSimon Glass struct udevice **devp); 412c6202d85SSimon Glass 413c6202d85SSimon Glass /** 414c6202d85SSimon Glass * i2c_chip_ofdata_to_platdata() - Decode standard I2C platform data 415c6202d85SSimon Glass * 416c6202d85SSimon Glass * This decodes the chip address from a device tree node and puts it into 417c6202d85SSimon Glass * its dm_i2c_chip structure. This should be called in your driver's 418c6202d85SSimon Glass * ofdata_to_platdata() method. 419c6202d85SSimon Glass * 420c6202d85SSimon Glass * @blob: Device tree blob 421c6202d85SSimon Glass * @node: Node offset to read from 422c6202d85SSimon Glass * @spi: Place to put the decoded information 423c6202d85SSimon Glass */ 424c6202d85SSimon Glass int i2c_chip_ofdata_to_platdata(const void *blob, int node, 425c6202d85SSimon Glass struct dm_i2c_chip *chip); 426c6202d85SSimon Glass 427c6202d85SSimon Glass #endif 428c6202d85SSimon Glass 429c6202d85SSimon Glass #ifndef CONFIG_DM_I2C 430c6202d85SSimon Glass 431c6202d85SSimon Glass /* 4321f045217Swdenk * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING 4331f045217Swdenk * 4341f045217Swdenk * The implementation MUST NOT use static or global variables if the 4351f045217Swdenk * I2C routines are used to read SDRAM configuration information 4361f045217Swdenk * because this is done before the memories are initialized. Limited 4371f045217Swdenk * use of stack-based variables are OK (the initial stack size is 4381f045217Swdenk * limited). 4391f045217Swdenk * 4401f045217Swdenk * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING 4411f045217Swdenk */ 4421f045217Swdenk 4431f045217Swdenk /* 4441f045217Swdenk * Configuration items. 4451f045217Swdenk */ 4461f045217Swdenk #define I2C_RXTX_LEN 128 /* maximum tx/rx buffer length */ 4471f045217Swdenk 448385c9ef5SHeiko Schocher #if !defined(CONFIG_SYS_I2C_MAX_HOPS) 449385c9ef5SHeiko Schocher /* no muxes used bus = i2c adapters */ 450385c9ef5SHeiko Schocher #define CONFIG_SYS_I2C_DIRECT_BUS 1 451385c9ef5SHeiko Schocher #define CONFIG_SYS_I2C_MAX_HOPS 0 452385c9ef5SHeiko Schocher #define CONFIG_SYS_NUM_I2C_BUSES ll_entry_count(struct i2c_adapter, i2c) 45379b2d0bbSStefan Roese #else 454385c9ef5SHeiko Schocher /* we use i2c muxes */ 455385c9ef5SHeiko Schocher #undef CONFIG_SYS_I2C_DIRECT_BUS 45679b2d0bbSStefan Roese #endif 45779b2d0bbSStefan Roese 4588c12045aSStefan Roese /* define the I2C bus number for RTC and DTT if not already done */ 4596d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #if !defined(CONFIG_SYS_RTC_BUS_NUM) 4606d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #define CONFIG_SYS_RTC_BUS_NUM 0 4618c12045aSStefan Roese #endif 4626d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #if !defined(CONFIG_SYS_DTT_BUS_NUM) 4636d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #define CONFIG_SYS_DTT_BUS_NUM 0 4648c12045aSStefan Roese #endif 4656d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #if !defined(CONFIG_SYS_SPD_BUS_NUM) 4666d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #define CONFIG_SYS_SPD_BUS_NUM 0 467d8a8ea5cSMatthias Fuchs #endif 4688c12045aSStefan Roese 469385c9ef5SHeiko Schocher struct i2c_adapter { 470385c9ef5SHeiko Schocher void (*init)(struct i2c_adapter *adap, int speed, 471385c9ef5SHeiko Schocher int slaveaddr); 472385c9ef5SHeiko Schocher int (*probe)(struct i2c_adapter *adap, uint8_t chip); 473385c9ef5SHeiko Schocher int (*read)(struct i2c_adapter *adap, uint8_t chip, 474385c9ef5SHeiko Schocher uint addr, int alen, uint8_t *buffer, 475385c9ef5SHeiko Schocher int len); 476385c9ef5SHeiko Schocher int (*write)(struct i2c_adapter *adap, uint8_t chip, 477385c9ef5SHeiko Schocher uint addr, int alen, uint8_t *buffer, 478385c9ef5SHeiko Schocher int len); 479385c9ef5SHeiko Schocher uint (*set_bus_speed)(struct i2c_adapter *adap, 480385c9ef5SHeiko Schocher uint speed); 481385c9ef5SHeiko Schocher int speed; 482d5243359SHannes Petermaier int waitdelay; 483385c9ef5SHeiko Schocher int slaveaddr; 484385c9ef5SHeiko Schocher int init_done; 485385c9ef5SHeiko Schocher int hwadapnr; 486385c9ef5SHeiko Schocher char *name; 487385c9ef5SHeiko Schocher }; 488385c9ef5SHeiko Schocher 489385c9ef5SHeiko Schocher #define U_BOOT_I2C_MKENT_COMPLETE(_init, _probe, _read, _write, \ 490385c9ef5SHeiko Schocher _set_speed, _speed, _slaveaddr, _hwadapnr, _name) \ 491385c9ef5SHeiko Schocher { \ 492385c9ef5SHeiko Schocher .init = _init, \ 493385c9ef5SHeiko Schocher .probe = _probe, \ 494385c9ef5SHeiko Schocher .read = _read, \ 495385c9ef5SHeiko Schocher .write = _write, \ 496385c9ef5SHeiko Schocher .set_bus_speed = _set_speed, \ 497385c9ef5SHeiko Schocher .speed = _speed, \ 498385c9ef5SHeiko Schocher .slaveaddr = _slaveaddr, \ 499385c9ef5SHeiko Schocher .init_done = 0, \ 500385c9ef5SHeiko Schocher .hwadapnr = _hwadapnr, \ 501385c9ef5SHeiko Schocher .name = #_name \ 502385c9ef5SHeiko Schocher }; 503385c9ef5SHeiko Schocher 504385c9ef5SHeiko Schocher #define U_BOOT_I2C_ADAP_COMPLETE(_name, _init, _probe, _read, _write, \ 505385c9ef5SHeiko Schocher _set_speed, _speed, _slaveaddr, _hwadapnr) \ 506385c9ef5SHeiko Schocher ll_entry_declare(struct i2c_adapter, _name, i2c) = \ 507385c9ef5SHeiko Schocher U_BOOT_I2C_MKENT_COMPLETE(_init, _probe, _read, _write, \ 508385c9ef5SHeiko Schocher _set_speed, _speed, _slaveaddr, _hwadapnr, _name); 509385c9ef5SHeiko Schocher 510385c9ef5SHeiko Schocher struct i2c_adapter *i2c_get_adapter(int index); 511385c9ef5SHeiko Schocher 512385c9ef5SHeiko Schocher #ifndef CONFIG_SYS_I2C_DIRECT_BUS 513385c9ef5SHeiko Schocher struct i2c_mux { 514385c9ef5SHeiko Schocher int id; 515385c9ef5SHeiko Schocher char name[16]; 516385c9ef5SHeiko Schocher }; 517385c9ef5SHeiko Schocher 518385c9ef5SHeiko Schocher struct i2c_next_hop { 519385c9ef5SHeiko Schocher struct i2c_mux mux; 520385c9ef5SHeiko Schocher uint8_t chip; 521385c9ef5SHeiko Schocher uint8_t channel; 522385c9ef5SHeiko Schocher }; 523385c9ef5SHeiko Schocher 524385c9ef5SHeiko Schocher struct i2c_bus_hose { 525385c9ef5SHeiko Schocher int adapter; 526385c9ef5SHeiko Schocher struct i2c_next_hop next_hop[CONFIG_SYS_I2C_MAX_HOPS]; 527385c9ef5SHeiko Schocher }; 528385c9ef5SHeiko Schocher #define I2C_NULL_HOP {{-1, ""}, 0, 0} 529385c9ef5SHeiko Schocher extern struct i2c_bus_hose i2c_bus[]; 530385c9ef5SHeiko Schocher 531385c9ef5SHeiko Schocher #define I2C_ADAPTER(bus) i2c_bus[bus].adapter 532385c9ef5SHeiko Schocher #else 533385c9ef5SHeiko Schocher #define I2C_ADAPTER(bus) bus 534385c9ef5SHeiko Schocher #endif 535385c9ef5SHeiko Schocher #define I2C_BUS gd->cur_i2c_bus 536385c9ef5SHeiko Schocher 537385c9ef5SHeiko Schocher #define I2C_ADAP_NR(bus) i2c_get_adapter(I2C_ADAPTER(bus)) 538385c9ef5SHeiko Schocher #define I2C_ADAP I2C_ADAP_NR(gd->cur_i2c_bus) 539385c9ef5SHeiko Schocher #define I2C_ADAP_HWNR (I2C_ADAP->hwadapnr) 540385c9ef5SHeiko Schocher 541385c9ef5SHeiko Schocher #ifndef CONFIG_SYS_I2C_DIRECT_BUS 542385c9ef5SHeiko Schocher #define I2C_MUX_PCA9540_ID 1 543385c9ef5SHeiko Schocher #define I2C_MUX_PCA9540 {I2C_MUX_PCA9540_ID, "PCA9540B"} 544385c9ef5SHeiko Schocher #define I2C_MUX_PCA9542_ID 2 545385c9ef5SHeiko Schocher #define I2C_MUX_PCA9542 {I2C_MUX_PCA9542_ID, "PCA9542A"} 546385c9ef5SHeiko Schocher #define I2C_MUX_PCA9544_ID 3 547385c9ef5SHeiko Schocher #define I2C_MUX_PCA9544 {I2C_MUX_PCA9544_ID, "PCA9544A"} 548385c9ef5SHeiko Schocher #define I2C_MUX_PCA9547_ID 4 549385c9ef5SHeiko Schocher #define I2C_MUX_PCA9547 {I2C_MUX_PCA9547_ID, "PCA9547A"} 550e6658749SMichael Burr #define I2C_MUX_PCA9548_ID 5 551e6658749SMichael Burr #define I2C_MUX_PCA9548 {I2C_MUX_PCA9548_ID, "PCA9548"} 552385c9ef5SHeiko Schocher #endif 553385c9ef5SHeiko Schocher 55498aed379SHeiko Schocher #ifndef I2C_SOFT_DECLARATIONS 55598aed379SHeiko Schocher # if defined(CONFIG_MPC8260) 5566d0f6bcfSJean-Christophe PLAGNIOL-VILLARD # define I2C_SOFT_DECLARATIONS volatile ioport_t *iop = ioport_addr((immap_t *)CONFIG_SYS_IMMR, I2C_PORT); 55798aed379SHeiko Schocher # elif defined(CONFIG_8xx) 5586d0f6bcfSJean-Christophe PLAGNIOL-VILLARD # define I2C_SOFT_DECLARATIONS volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR; 5590cf0b931SJens Scharsig 5600cf0b931SJens Scharsig # elif (defined(CONFIG_AT91RM9200) || \ 5610cf0b931SJens Scharsig defined(CONFIG_AT91SAM9260) || defined(CONFIG_AT91SAM9261) || \ 562cb96a0a4SAndreas Bießmann defined(CONFIG_AT91SAM9263)) 56378132275Sesw@bus-elektronik.de # define I2C_SOFT_DECLARATIONS at91_pio_t *pio = (at91_pio_t *) ATMEL_BASE_PIOA; 56498aed379SHeiko Schocher # else 56598aed379SHeiko Schocher # define I2C_SOFT_DECLARATIONS 56698aed379SHeiko Schocher # endif 56798aed379SHeiko Schocher #endif 568ecf5f077STimur Tabi 569ecf5f077STimur Tabi #ifdef CONFIG_8xx 5709c90a2c8SPeter Tyser /* Set default value for the I2C bus speed on 8xx. In the 571ecf5f077STimur Tabi * future, we'll define these in all 8xx board config files. 572ecf5f077STimur Tabi */ 573ecf5f077STimur Tabi #ifndef CONFIG_SYS_I2C_SPEED 574ecf5f077STimur Tabi #define CONFIG_SYS_I2C_SPEED 50000 575ecf5f077STimur Tabi #endif 576ecf5f077STimur Tabi #endif 5779c90a2c8SPeter Tyser 5789c90a2c8SPeter Tyser /* 5799c90a2c8SPeter Tyser * Many boards/controllers/drivers don't support an I2C slave interface so 5809c90a2c8SPeter Tyser * provide a default slave address for them for use in common code. A real 5819c90a2c8SPeter Tyser * value for CONFIG_SYS_I2C_SLAVE should be defined for any board which does 5829c90a2c8SPeter Tyser * support a slave interface. 5839c90a2c8SPeter Tyser */ 5849c90a2c8SPeter Tyser #ifndef CONFIG_SYS_I2C_SLAVE 5859c90a2c8SPeter Tyser #define CONFIG_SYS_I2C_SLAVE 0xfe 586ecf5f077STimur Tabi #endif 587ecf5f077STimur Tabi 5881f045217Swdenk /* 5891f045217Swdenk * Initialization, must be called once on start up, may be called 5901f045217Swdenk * repeatedly to change the speed and slave addresses. 5911f045217Swdenk */ 5921f045217Swdenk void i2c_init(int speed, int slaveaddr); 59306d01dbeSwdenk void i2c_init_board(void); 59426a33504SRichard Retanubun #ifdef CONFIG_SYS_I2C_BOARD_LATE_INIT 59526a33504SRichard Retanubun void i2c_board_late_init(void); 59626a33504SRichard Retanubun #endif 5971f045217Swdenk 598385c9ef5SHeiko Schocher #ifdef CONFIG_SYS_I2C 599385c9ef5SHeiko Schocher /* 600385c9ef5SHeiko Schocher * i2c_get_bus_num: 601385c9ef5SHeiko Schocher * 602385c9ef5SHeiko Schocher * Returns index of currently active I2C bus. Zero-based. 603385c9ef5SHeiko Schocher */ 604385c9ef5SHeiko Schocher unsigned int i2c_get_bus_num(void); 60567b23a32SHeiko Schocher 606385c9ef5SHeiko Schocher /* 607385c9ef5SHeiko Schocher * i2c_set_bus_num: 608385c9ef5SHeiko Schocher * 609385c9ef5SHeiko Schocher * Change the active I2C bus. Subsequent read/write calls will 610385c9ef5SHeiko Schocher * go to this one. 611385c9ef5SHeiko Schocher * 612385c9ef5SHeiko Schocher * bus - bus index, zero based 613385c9ef5SHeiko Schocher * 614385c9ef5SHeiko Schocher * Returns: 0 on success, not 0 on failure 615385c9ef5SHeiko Schocher * 616385c9ef5SHeiko Schocher */ 617385c9ef5SHeiko Schocher int i2c_set_bus_num(unsigned int bus); 61867b23a32SHeiko Schocher 619385c9ef5SHeiko Schocher /* 620385c9ef5SHeiko Schocher * i2c_init_all(): 621385c9ef5SHeiko Schocher * 622385c9ef5SHeiko Schocher * Initializes all I2C adapters in the system. All i2c_adap structures must 623385c9ef5SHeiko Schocher * be initialized beforehead with function pointers and data, including 624385c9ef5SHeiko Schocher * speed and slaveaddr. Returns 0 on success, non-0 on failure. 625385c9ef5SHeiko Schocher */ 626385c9ef5SHeiko Schocher void i2c_init_all(void); 62767b23a32SHeiko Schocher 628385c9ef5SHeiko Schocher /* 629385c9ef5SHeiko Schocher * Probe the given I2C chip address. Returns 0 if a chip responded, 630385c9ef5SHeiko Schocher * not 0 on failure. 631385c9ef5SHeiko Schocher */ 632385c9ef5SHeiko Schocher int i2c_probe(uint8_t chip); 633385c9ef5SHeiko Schocher 634385c9ef5SHeiko Schocher /* 635385c9ef5SHeiko Schocher * Read/Write interface: 636385c9ef5SHeiko Schocher * chip: I2C chip address, range 0..127 637385c9ef5SHeiko Schocher * addr: Memory (register) address within the chip 638385c9ef5SHeiko Schocher * alen: Number of bytes to use for addr (typically 1, 2 for larger 639385c9ef5SHeiko Schocher * memories, 0 for register type devices with only one 640385c9ef5SHeiko Schocher * register) 641385c9ef5SHeiko Schocher * buffer: Where to read/write the data 642385c9ef5SHeiko Schocher * len: How many bytes to read/write 643385c9ef5SHeiko Schocher * 644385c9ef5SHeiko Schocher * Returns: 0 on success, not 0 on failure 645385c9ef5SHeiko Schocher */ 646385c9ef5SHeiko Schocher int i2c_read(uint8_t chip, unsigned int addr, int alen, 647385c9ef5SHeiko Schocher uint8_t *buffer, int len); 648385c9ef5SHeiko Schocher 649385c9ef5SHeiko Schocher int i2c_write(uint8_t chip, unsigned int addr, int alen, 650385c9ef5SHeiko Schocher uint8_t *buffer, int len); 651385c9ef5SHeiko Schocher 652385c9ef5SHeiko Schocher /* 653385c9ef5SHeiko Schocher * Utility routines to read/write registers. 654385c9ef5SHeiko Schocher */ 655385c9ef5SHeiko Schocher uint8_t i2c_reg_read(uint8_t addr, uint8_t reg); 656385c9ef5SHeiko Schocher 657385c9ef5SHeiko Schocher void i2c_reg_write(uint8_t addr, uint8_t reg, uint8_t val); 658385c9ef5SHeiko Schocher 659385c9ef5SHeiko Schocher /* 660385c9ef5SHeiko Schocher * i2c_set_bus_speed: 661385c9ef5SHeiko Schocher * 662385c9ef5SHeiko Schocher * Change the speed of the active I2C bus 663385c9ef5SHeiko Schocher * 664385c9ef5SHeiko Schocher * speed - bus speed in Hz 665385c9ef5SHeiko Schocher * 666385c9ef5SHeiko Schocher * Returns: new bus speed 667385c9ef5SHeiko Schocher * 668385c9ef5SHeiko Schocher */ 669385c9ef5SHeiko Schocher unsigned int i2c_set_bus_speed(unsigned int speed); 670385c9ef5SHeiko Schocher 671385c9ef5SHeiko Schocher /* 672385c9ef5SHeiko Schocher * i2c_get_bus_speed: 673385c9ef5SHeiko Schocher * 674385c9ef5SHeiko Schocher * Returns speed of currently active I2C bus in Hz 675385c9ef5SHeiko Schocher */ 676385c9ef5SHeiko Schocher 677385c9ef5SHeiko Schocher unsigned int i2c_get_bus_speed(void); 678385c9ef5SHeiko Schocher 679385c9ef5SHeiko Schocher /* 680385c9ef5SHeiko Schocher * i2c_reloc_fixup: 681385c9ef5SHeiko Schocher * 682385c9ef5SHeiko Schocher * Adjusts I2C pointers after U-Boot is relocated to DRAM 683385c9ef5SHeiko Schocher */ 684385c9ef5SHeiko Schocher void i2c_reloc_fixup(void); 685ea818dbbSHeiko Schocher #if defined(CONFIG_SYS_I2C_SOFT) 686ea818dbbSHeiko Schocher void i2c_soft_init(void); 687ea818dbbSHeiko Schocher void i2c_soft_active(void); 688ea818dbbSHeiko Schocher void i2c_soft_tristate(void); 689ea818dbbSHeiko Schocher int i2c_soft_read(void); 690ea818dbbSHeiko Schocher void i2c_soft_sda(int bit); 691ea818dbbSHeiko Schocher void i2c_soft_scl(int bit); 692ea818dbbSHeiko Schocher void i2c_soft_delay(void); 69367b23a32SHeiko Schocher #endif 694385c9ef5SHeiko Schocher #else 69567b23a32SHeiko Schocher 6961f045217Swdenk /* 6971f045217Swdenk * Probe the given I2C chip address. Returns 0 if a chip responded, 6981f045217Swdenk * not 0 on failure. 6991f045217Swdenk */ 7001f045217Swdenk int i2c_probe(uchar chip); 7011f045217Swdenk 7021f045217Swdenk /* 7031f045217Swdenk * Read/Write interface: 7041f045217Swdenk * chip: I2C chip address, range 0..127 7051f045217Swdenk * addr: Memory (register) address within the chip 7061f045217Swdenk * alen: Number of bytes to use for addr (typically 1, 2 for larger 7071f045217Swdenk * memories, 0 for register type devices with only one 7081f045217Swdenk * register) 7091f045217Swdenk * buffer: Where to read/write the data 7101f045217Swdenk * len: How many bytes to read/write 7111f045217Swdenk * 7121f045217Swdenk * Returns: 0 on success, not 0 on failure 7131f045217Swdenk */ 7141f045217Swdenk int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len); 7151f045217Swdenk int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len); 7161f045217Swdenk 7171f045217Swdenk /* 7181f045217Swdenk * Utility routines to read/write registers. 7191f045217Swdenk */ 720ecf5f077STimur Tabi static inline u8 i2c_reg_read(u8 addr, u8 reg) 721ecf5f077STimur Tabi { 722ecf5f077STimur Tabi u8 buf; 723ecf5f077STimur Tabi 724ecf5f077STimur Tabi #ifdef CONFIG_8xx 725ecf5f077STimur Tabi /* MPC8xx needs this. Maybe one day we can get rid of it. */ 726ecf5f077STimur Tabi i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE); 727ecf5f077STimur Tabi #endif 728ecf5f077STimur Tabi 729ecf5f077STimur Tabi #ifdef DEBUG 730ecf5f077STimur Tabi printf("%s: addr=0x%02x, reg=0x%02x\n", __func__, addr, reg); 731ecf5f077STimur Tabi #endif 732ecf5f077STimur Tabi 733ecf5f077STimur Tabi i2c_read(addr, reg, 1, &buf, 1); 734ecf5f077STimur Tabi 735ecf5f077STimur Tabi return buf; 736ecf5f077STimur Tabi } 737ecf5f077STimur Tabi 738ecf5f077STimur Tabi static inline void i2c_reg_write(u8 addr, u8 reg, u8 val) 739ecf5f077STimur Tabi { 740ecf5f077STimur Tabi #ifdef CONFIG_8xx 741ecf5f077STimur Tabi /* MPC8xx needs this. Maybe one day we can get rid of it. */ 742ecf5f077STimur Tabi i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE); 743ecf5f077STimur Tabi #endif 744ecf5f077STimur Tabi 745ecf5f077STimur Tabi #ifdef DEBUG 746ecf5f077STimur Tabi printf("%s: addr=0x%02x, reg=0x%02x, val=0x%02x\n", 747ecf5f077STimur Tabi __func__, addr, reg, val); 748ecf5f077STimur Tabi #endif 749ecf5f077STimur Tabi 750ecf5f077STimur Tabi i2c_write(addr, reg, 1, &val, 1); 751ecf5f077STimur Tabi } 7521f045217Swdenk 753bb99ad6dSBen Warren /* 754bb99ad6dSBen Warren * Functions for setting the current I2C bus and its speed 755bb99ad6dSBen Warren */ 756bb99ad6dSBen Warren 757bb99ad6dSBen Warren /* 758bb99ad6dSBen Warren * i2c_set_bus_num: 759bb99ad6dSBen Warren * 760bb99ad6dSBen Warren * Change the active I2C bus. Subsequent read/write calls will 761bb99ad6dSBen Warren * go to this one. 762bb99ad6dSBen Warren * 763bb99ad6dSBen Warren * bus - bus index, zero based 764bb99ad6dSBen Warren * 765bb99ad6dSBen Warren * Returns: 0 on success, not 0 on failure 766bb99ad6dSBen Warren * 767bb99ad6dSBen Warren */ 7689ca880a2STimur Tabi int i2c_set_bus_num(unsigned int bus); 769bb99ad6dSBen Warren 770bb99ad6dSBen Warren /* 771bb99ad6dSBen Warren * i2c_get_bus_num: 772bb99ad6dSBen Warren * 773bb99ad6dSBen Warren * Returns index of currently active I2C bus. Zero-based. 774bb99ad6dSBen Warren */ 775bb99ad6dSBen Warren 7769ca880a2STimur Tabi unsigned int i2c_get_bus_num(void); 777bb99ad6dSBen Warren 778bb99ad6dSBen Warren /* 779bb99ad6dSBen Warren * i2c_set_bus_speed: 780bb99ad6dSBen Warren * 781bb99ad6dSBen Warren * Change the speed of the active I2C bus 782bb99ad6dSBen Warren * 783bb99ad6dSBen Warren * speed - bus speed in Hz 784bb99ad6dSBen Warren * 785bb99ad6dSBen Warren * Returns: 0 on success, not 0 on failure 786bb99ad6dSBen Warren * 787bb99ad6dSBen Warren */ 7889ca880a2STimur Tabi int i2c_set_bus_speed(unsigned int); 789bb99ad6dSBen Warren 790bb99ad6dSBen Warren /* 791bb99ad6dSBen Warren * i2c_get_bus_speed: 792bb99ad6dSBen Warren * 793bb99ad6dSBen Warren * Returns speed of currently active I2C bus in Hz 794bb99ad6dSBen Warren */ 795bb99ad6dSBen Warren 7969ca880a2STimur Tabi unsigned int i2c_get_bus_speed(void); 797385c9ef5SHeiko Schocher #endif /* CONFIG_SYS_I2C */ 798385c9ef5SHeiko Schocher 799385c9ef5SHeiko Schocher /* 800385c9ef5SHeiko Schocher * only for backwardcompatibility, should go away if we switched 801385c9ef5SHeiko Schocher * completely to new multibus support. 802385c9ef5SHeiko Schocher */ 803385c9ef5SHeiko Schocher #if defined(CONFIG_SYS_I2C) || defined(CONFIG_I2C_MULTI_BUS) 804385c9ef5SHeiko Schocher # if !defined(CONFIG_SYS_MAX_I2C_BUS) 805385c9ef5SHeiko Schocher # define CONFIG_SYS_MAX_I2C_BUS 2 806385c9ef5SHeiko Schocher # endif 807ea0f73abSŁukasz Majewski # define I2C_MULTI_BUS 1 808385c9ef5SHeiko Schocher #else 809385c9ef5SHeiko Schocher # define CONFIG_SYS_MAX_I2C_BUS 1 810385c9ef5SHeiko Schocher # define I2C_MULTI_BUS 0 811385c9ef5SHeiko Schocher #endif 812bb99ad6dSBen Warren 813cd7b4e82SMarek Vasut /* NOTE: These two functions MUST be always_inline to avoid code growth! */ 814cd7b4e82SMarek Vasut static inline unsigned int I2C_GET_BUS(void) __attribute__((always_inline)); 815cd7b4e82SMarek Vasut static inline unsigned int I2C_GET_BUS(void) 816cd7b4e82SMarek Vasut { 817cd7b4e82SMarek Vasut return I2C_MULTI_BUS ? i2c_get_bus_num() : 0; 818cd7b4e82SMarek Vasut } 819cd7b4e82SMarek Vasut 820cd7b4e82SMarek Vasut static inline void I2C_SET_BUS(unsigned int bus) __attribute__((always_inline)); 821cd7b4e82SMarek Vasut static inline void I2C_SET_BUS(unsigned int bus) 822cd7b4e82SMarek Vasut { 823cd7b4e82SMarek Vasut if (I2C_MULTI_BUS) 824cd7b4e82SMarek Vasut i2c_set_bus_num(bus); 825cd7b4e82SMarek Vasut } 826cd7b4e82SMarek Vasut 8277ca8f73aSŁukasz Majewski /* Multi I2C definitions */ 8287ca8f73aSŁukasz Majewski enum { 8297ca8f73aSŁukasz Majewski I2C_0, I2C_1, I2C_2, I2C_3, I2C_4, I2C_5, I2C_6, I2C_7, 8307ca8f73aSŁukasz Majewski I2C_8, I2C_9, I2C_10, 8317ca8f73aSŁukasz Majewski }; 8327ca8f73aSŁukasz Majewski 8337ca8f73aSŁukasz Majewski /* Multi I2C busses handling */ 8347ca8f73aSŁukasz Majewski #ifdef CONFIG_SOFT_I2C_MULTI_BUS 8357ca8f73aSŁukasz Majewski extern int get_multi_scl_pin(void); 8367ca8f73aSŁukasz Majewski extern int get_multi_sda_pin(void); 8377ca8f73aSŁukasz Majewski extern int multi_i2c_init(void); 8387ca8f73aSŁukasz Majewski #endif 839a9d2ae70SRajeshwari Shinde 840a9d2ae70SRajeshwari Shinde /** 841a9d2ae70SRajeshwari Shinde * Get FDT values for i2c bus. 842a9d2ae70SRajeshwari Shinde * 843a9d2ae70SRajeshwari Shinde * @param blob Device tree blbo 844a9d2ae70SRajeshwari Shinde * @return the number of I2C bus 845a9d2ae70SRajeshwari Shinde */ 846a9d2ae70SRajeshwari Shinde void board_i2c_init(const void *blob); 847a9d2ae70SRajeshwari Shinde 848a9d2ae70SRajeshwari Shinde /** 849a9d2ae70SRajeshwari Shinde * Find the I2C bus number by given a FDT I2C node. 850a9d2ae70SRajeshwari Shinde * 851a9d2ae70SRajeshwari Shinde * @param blob Device tree blbo 852a9d2ae70SRajeshwari Shinde * @param node FDT I2C node to find 853a9d2ae70SRajeshwari Shinde * @return the number of I2C bus (zero based), or -1 on error 854a9d2ae70SRajeshwari Shinde */ 855a9d2ae70SRajeshwari Shinde int i2c_get_bus_num_fdt(int node); 856a9d2ae70SRajeshwari Shinde 857a9d2ae70SRajeshwari Shinde /** 858a9d2ae70SRajeshwari Shinde * Reset the I2C bus represented by the given a FDT I2C node. 859a9d2ae70SRajeshwari Shinde * 860a9d2ae70SRajeshwari Shinde * @param blob Device tree blbo 861a9d2ae70SRajeshwari Shinde * @param node FDT I2C node to find 862a9d2ae70SRajeshwari Shinde * @return 0 if port was reset, -1 if not found 863a9d2ae70SRajeshwari Shinde */ 864a9d2ae70SRajeshwari Shinde int i2c_reset_port_fdt(const void *blob, int node); 865c6202d85SSimon Glass 866c6202d85SSimon Glass #endif /* !CONFIG_DM_I2C */ 867c6202d85SSimon Glass 8681f045217Swdenk #endif /* _I2C_H_ */ 869