1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * Copyright (C) 2009 Sergey Kubushyn <ksi@koi8.net>
4 * Copyright (C) 2009 - 2013 Heiko Schocher <hs@denx.de>
5 * Changes for multibus/multiadapter I2C support.
8 * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com.
10 * The original I2C interface was
11 * (C) 2000 by Paolo Scaffardi (arsenio@tin.it)
12 * AIRVENT SAM s.p.a - RIMINI(ITALY)
13 * but has been changed substantially.
20 * For now there are essentially two parts to this file - driver model
21 * here at the top, and the older code below (with CONFIG_SYS_I2C being
22 * most recent). The plan is to migrate everything to driver model.
23 * The driver model structures and API are separate as they are different
24 * enough as to be incompatible for compilation purposes.
27 enum dm_i2c_chip_flags {
28 DM_I2C_CHIP_10BIT = 1 << 0, /* Use 10-bit addressing */
29 DM_I2C_CHIP_RD_ADDRESS = 1 << 1, /* Send address for each read byte */
30 DM_I2C_CHIP_WR_ADDRESS = 1 << 2, /* Send address for each write byte */
35 * struct dm_i2c_chip - information about an i2c chip
37 * An I2C chip is a device on the I2C bus. It sits at a particular address
38 * and normally supports 7-bit or 10-bit addressing.
40 * To obtain this structure, use dev_get_parent_platdata(dev) where dev is
41 * the chip to examine.
43 * @chip_addr: Chip address on bus
44 * @offset_len: Length of offset in bytes. A single byte offset can
45 * represent up to 256 bytes. A value larger than 1 may be
46 * needed for larger devices.
47 * @flags: Flags for this chip (dm_i2c_chip_flags)
48 * @chip_addr_offset_mask: Mask of offset bits within chip_addr. Used for
49 * devices which steal addresses as part of offset.
50 * If offset_len is zero, then the offset is encoded
51 * completely within the chip address itself.
52 * e.g. a devce with chip address of 0x2c with 512
53 * registers might use the bottom bit of the address
54 * to indicate which half of the address space is being
55 * accessed while still only using 1 byte offset.
56 * This means it will respond to chip address 0x2c and
58 * A real world example is the Atmel AT24C04. It's
59 * datasheet explains it's usage of this addressing
61 * @emul: Emulator for this chip address (only used for emulation)
67 uint chip_addr_offset_mask;
75 * struct dm_i2c_bus- information about an i2c bus
77 * An I2C bus contains 0 or more chips on it, each at its own address. The
78 * bus can operate at different speeds (measured in Hz, typically 100KHz
81 * To obtain this structure, use dev_get_uclass_priv(bus) where bus is the
84 * @speed_hz: Bus speed in hertz (typically 100000)
85 * @max_transaction_bytes: Maximal size of single I2C transfer
89 int max_transaction_bytes;
93 * Not all of these flags are implemented in the U-Boot API
95 enum dm_i2c_msg_flags {
96 I2C_M_TEN = 0x0010, /* ten-bit chip address */
97 I2C_M_RD = 0x0001, /* read data, from slave to master */
98 I2C_M_STOP = 0x8000, /* send stop after this message */
99 I2C_M_NOSTART = 0x4000, /* no start before this message */
100 I2C_M_REV_DIR_ADDR = 0x2000, /* invert polarity of R/W bit */
101 I2C_M_IGNORE_NAK = 0x1000, /* continue after NAK */
102 I2C_M_NO_RD_ACK = 0x0800, /* skip the Ack bit on reads */
103 I2C_M_RECV_LEN = 0x0400, /* length is first received byte */
107 * struct i2c_msg - an I2C message
109 * @addr: Slave address
110 * @flags: Flags (see enum dm_i2c_msg_flags)
111 * @len: Length of buffer in bytes, may be 0 for a probe
112 * @buf: Buffer to send/receive, or NULL if no data
122 * struct i2c_msg_list - a list of I2C messages
124 * This is called i2c_rdwr_ioctl_data in Linux but the name does not seem
125 * appropriate in U-Boot.
127 * @msg: Pointer to i2c_msg array
128 * @nmsgs: Number of elements in the array
130 struct i2c_msg_list {
131 struct i2c_msg *msgs;
136 * dm_i2c_read() - read bytes from an I2C chip
138 * To obtain an I2C device (called a 'chip') given the I2C bus address you
139 * can use i2c_get_chip(). To obtain a bus by bus number use
140 * uclass_get_device_by_seq(UCLASS_I2C, <bus number>).
142 * To set the address length of a devce use i2c_set_addr_len(). It
145 * @dev: Chip to read from
146 * @offset: Offset within chip to start reading
147 * @buffer: Place to put data
148 * @len: Number of bytes to read
150 * @return 0 on success, -ve on failure
152 int dm_i2c_read(struct udevice *dev, uint offset, uint8_t *buffer, int len);
155 * dm_i2c_write() - write bytes to an I2C chip
157 * See notes for dm_i2c_read() above.
159 * @dev: Chip to write to
160 * @offset: Offset within chip to start writing
161 * @buffer: Buffer containing data to write
162 * @len: Number of bytes to write
164 * @return 0 on success, -ve on failure
166 int dm_i2c_write(struct udevice *dev, uint offset, const uint8_t *buffer,
170 * dm_i2c_probe() - probe a particular chip address
172 * This can be useful to check for the existence of a chip on the bus.
173 * It is typically implemented by writing the chip address to the bus
174 * and checking that the chip replies with an ACK.
177 * @chip_addr: 7-bit address to probe (10-bit and others are not supported)
178 * @chip_flags: Flags for the probe (see enum dm_i2c_chip_flags)
179 * @devp: Returns the device found, or NULL if none
180 * @return 0 if a chip was found at that address, -ve if not
182 int dm_i2c_probe(struct udevice *bus, uint chip_addr, uint chip_flags,
183 struct udevice **devp);
186 * dm_i2c_reg_read() - Read a value from an I2C register
188 * This reads a single value from the given address in an I2C chip
190 * @dev: Device to use for transfer
191 * @addr: Address to read from
192 * @return value read, or -ve on error
194 int dm_i2c_reg_read(struct udevice *dev, uint offset);
197 * dm_i2c_reg_write() - Write a value to an I2C register
199 * This writes a single value to the given address in an I2C chip
201 * @dev: Device to use for transfer
202 * @addr: Address to write to
203 * @val: Value to write (normally a byte)
204 * @return 0 on success, -ve on error
206 int dm_i2c_reg_write(struct udevice *dev, uint offset, unsigned int val);
209 * dm_i2c_xfer() - Transfer messages over I2C
211 * This transfers a raw message. It is best to use dm_i2c_reg_read/write()
214 * @dev: Device to use for transfer
215 * @msg: List of messages to transfer
216 * @nmsgs: Number of messages to transfer
217 * @return 0 on success, -ve on error
219 int dm_i2c_xfer(struct udevice *dev, struct i2c_msg *msg, int nmsgs);
222 * dm_i2c_set_bus_speed() - set the speed of a bus
224 * @bus: Bus to adjust
225 * @speed: Requested speed in Hz
226 * @return 0 if OK, -EINVAL for invalid values
228 int dm_i2c_set_bus_speed(struct udevice *bus, unsigned int speed);
231 * dm_i2c_get_bus_speed() - get the speed of a bus
234 * @return speed of selected I2C bus in Hz, -ve on error
236 int dm_i2c_get_bus_speed(struct udevice *bus);
239 * i2c_set_chip_flags() - set flags for a chip
241 * Typically addresses are 7 bits, but for 10-bit addresses you should set
242 * flags to DM_I2C_CHIP_10BIT. All accesses will then use 10-bit addressing.
244 * @dev: Chip to adjust
246 * @return 0 if OK, -EINVAL if value is unsupported, other -ve value on error
248 int i2c_set_chip_flags(struct udevice *dev, uint flags);
251 * i2c_get_chip_flags() - get flags for a chip
253 * @dev: Chip to check
254 * @flagsp: Place to put flags
255 * @return 0 if OK, other -ve value on error
257 int i2c_get_chip_flags(struct udevice *dev, uint *flagsp);
260 * i2c_set_offset_len() - set the offset length for a chip
262 * The offset used to access a chip may be up to 4 bytes long. Typically it
263 * is only 1 byte, which is enough for chips with 256 bytes of memory or
264 * registers. The default value is 1, but you can call this function to
267 * @offset_len: New offset length value (typically 1 or 2)
269 int i2c_set_chip_offset_len(struct udevice *dev, uint offset_len);
272 * i2c_get_offset_len() - get the offset length for a chip
274 * @return: Current offset length value (typically 1 or 2)
276 int i2c_get_chip_offset_len(struct udevice *dev);
279 * i2c_set_chip_addr_offset_mask() - set mask of address bits usable by offset
281 * Some devices listen on multiple chip addresses to achieve larger offsets
282 * than their single or multiple byte offsets would allow for. You can use this
283 * function to set the bits that are valid to be used for offset overflow.
285 * @mask: The mask to be used for high offset bits within address
286 * @return 0 if OK, other -ve value on error
288 int i2c_set_chip_addr_offset_mask(struct udevice *dev, uint mask);
291 * i2c_get_chip_addr_offset_mask() - get mask of address bits usable by offset
293 * @return current chip addr offset mask
295 uint i2c_get_chip_addr_offset_mask(struct udevice *dev);
298 * i2c_deblock() - recover a bus that is in an unknown state
300 * See the deblock() method in 'struct dm_i2c_ops' for full information
302 * @bus: Bus to recover
303 * @return 0 if OK, -ve on error
305 int i2c_deblock(struct udevice *bus);
308 * struct dm_i2c_ops - driver operations for I2C uclass
310 * Drivers should support these operations unless otherwise noted. These
311 * operations are intended to be used by uclass code, not directly from
316 * xfer() - transfer a list of I2C messages
318 * @bus: Bus to read from
319 * @msg: List of messages to transfer
320 * @nmsgs: Number of messages in the list
321 * @return 0 if OK, -EREMOTEIO if the slave did not ACK a byte,
322 * -ECOMM if the speed cannot be supported, -EPROTO if the chip
323 * flags cannot be supported, other -ve value on some other error
325 int (*xfer)(struct udevice *bus, struct i2c_msg *msg, int nmsgs);
328 * probe_chip() - probe for the presense of a chip address
330 * This function is optional. If omitted, the uclass will send a zero
331 * length message instead.
334 * @chip_addr: Chip address to probe
335 * @chip_flags: Probe flags (enum dm_i2c_chip_flags)
336 * @return 0 if chip was found, -EREMOTEIO if not, -ENOSYS to fall back
337 * to default probem other -ve value on error
339 int (*probe_chip)(struct udevice *bus, uint chip_addr, uint chip_flags);
342 * set_bus_speed() - set the speed of a bus (optional)
344 * The bus speed value will be updated by the uclass if this function
345 * does not return an error. This method is optional - if it is not
346 * provided then the driver can read the speed from
347 * dev_get_uclass_priv(bus)->speed_hz
349 * @bus: Bus to adjust
350 * @speed: Requested speed in Hz
351 * @return 0 if OK, -EINVAL for invalid values
353 int (*set_bus_speed)(struct udevice *bus, unsigned int speed);
356 * get_bus_speed() - get the speed of a bus (optional)
358 * Normally this can be provided by the uclass, but if you want your
359 * driver to check the bus speed by looking at the hardware, you can
360 * implement that here. This method is optional. This method would
361 * normally be expected to return dev_get_uclass_priv(bus)->speed_hz.
364 * @return speed of selected I2C bus in Hz, -ve on error
366 int (*get_bus_speed)(struct udevice *bus);
369 * set_flags() - set the flags for a chip (optional)
371 * This is generally implemented by the uclass, but drivers can
372 * check the value to ensure that unsupported options are not used.
373 * This method is optional. If provided, this method will always be
374 * called when the flags change.
376 * @dev: Chip to adjust
377 * @flags: New flags value
378 * @return 0 if OK, -EINVAL if value is unsupported
380 int (*set_flags)(struct udevice *dev, uint flags);
383 * deblock() - recover a bus that is in an unknown state
385 * I2C is a synchronous protocol and resets of the processor in the
386 * middle of an access can block the I2C Bus until a powerdown of
387 * the full unit is done. This is because slaves can be stuck
388 * waiting for addition bus transitions for a transaction that will
389 * never complete. Resetting the I2C master does not help. The only
390 * way is to force the bus through a series of transitions to make
391 * sure that all slaves are done with the transaction. This method
392 * performs this 'deblocking' if support by the driver.
394 * This method is optional.
396 int (*deblock)(struct udevice *bus);
399 #define i2c_get_ops(dev) ((struct dm_i2c_ops *)(dev)->driver->ops)
402 * struct i2c_mux_ops - operations for an I2C mux
404 * The current mux state is expected to be stored in the mux itself since
405 * it is the only thing that knows how to make things work. The mux can
406 * record the current state and then avoid switching unless it is necessary.
407 * So select() can be skipped if the mux is already in the correct state.
408 * Also deselect() can be made a nop if required.
412 * select() - select one of of I2C buses attached to a mux
414 * This will be called when there is no bus currently selected by the
415 * mux. This method does not need to deselect the old bus since
416 * deselect() will be already have been called if necessary.
419 * @bus: I2C bus to select
420 * @channel: Channel number correponding to the bus to select
421 * @return 0 if OK, -ve on error
423 int (*select)(struct udevice *mux, struct udevice *bus, uint channel);
426 * deselect() - select one of of I2C buses attached to a mux
428 * This is used to deselect the currently selected I2C bus.
431 * @bus: I2C bus to deselect
432 * @channel: Channel number correponding to the bus to deselect
433 * @return 0 if OK, -ve on error
435 int (*deselect)(struct udevice *mux, struct udevice *bus, uint channel);
438 #define i2c_mux_get_ops(dev) ((struct i2c_mux_ops *)(dev)->driver->ops)
441 * i2c_get_chip() - get a device to use to access a chip on a bus
443 * This returns the device for the given chip address. The device can then
444 * be used with calls to i2c_read(), i2c_write(), i2c_probe(), etc.
446 * @bus: Bus to examine
447 * @chip_addr: Chip address for the new device
448 * @offset_len: Length of a register offset in bytes (normally 1)
449 * @devp: Returns pointer to new device if found or -ENODEV if not
452 int i2c_get_chip(struct udevice *bus, uint chip_addr, uint offset_len,
453 struct udevice **devp);
456 * i2c_get_chip_for_busnum() - get a device to use to access a chip on
459 * This returns the device for the given chip address on a particular bus
462 * @busnum: Bus number to examine
463 * @chip_addr: Chip address for the new device
464 * @offset_len: Length of a register offset in bytes (normally 1)
465 * @devp: Returns pointer to new device if found or -ENODEV if not
468 int i2c_get_chip_for_busnum(int busnum, int chip_addr, uint offset_len,
469 struct udevice **devp);
472 * i2c_chip_ofdata_to_platdata() - Decode standard I2C platform data
474 * This decodes the chip address from a device tree node and puts it into
475 * its dm_i2c_chip structure. This should be called in your driver's
476 * ofdata_to_platdata() method.
478 * @blob: Device tree blob
479 * @node: Node offset to read from
480 * @spi: Place to put the decoded information
482 int i2c_chip_ofdata_to_platdata(struct udevice *dev, struct dm_i2c_chip *chip);
485 * i2c_dump_msgs() - Dump a list of I2C messages
487 * This may be useful for debugging.
489 * @msg: Message list to dump
490 * @nmsgs: Number of messages
492 void i2c_dump_msgs(struct i2c_msg *msg, int nmsgs);
495 * i2c_emul_find() - Find an emulator for an i2c sandbox device
497 * This looks at the device's 'emul' phandle
499 * @dev: Device to find an emulator for
500 * @emulp: Returns the associated emulator, if found *
501 * @return 0 if OK, -ENOENT or -ENODEV if not found
503 int i2c_emul_find(struct udevice *dev, struct udevice **emulp);
506 * i2c_emul_get_device() - Find the device being emulated
508 * Given an emulator this returns the associated device
510 * @emul: Emulator for the device
511 * @return device that @emul is emulating
513 struct udevice *i2c_emul_get_device(struct udevice *emul);
515 #ifndef CONFIG_DM_I2C
518 * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
520 * The implementation MUST NOT use static or global variables if the
521 * I2C routines are used to read SDRAM configuration information
522 * because this is done before the memories are initialized. Limited
523 * use of stack-based variables are OK (the initial stack size is
526 * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
530 * Configuration items.
532 #define I2C_RXTX_LEN 128 /* maximum tx/rx buffer length */
534 #if !defined(CONFIG_SYS_I2C_MAX_HOPS)
535 /* no muxes used bus = i2c adapters */
536 #define CONFIG_SYS_I2C_DIRECT_BUS 1
537 #define CONFIG_SYS_I2C_MAX_HOPS 0
538 #define CONFIG_SYS_NUM_I2C_BUSES ll_entry_count(struct i2c_adapter, i2c)
540 /* we use i2c muxes */
541 #undef CONFIG_SYS_I2C_DIRECT_BUS
544 /* define the I2C bus number for RTC and DTT if not already done */
545 #if !defined(CONFIG_SYS_RTC_BUS_NUM)
546 #define CONFIG_SYS_RTC_BUS_NUM 0
548 #if !defined(CONFIG_SYS_SPD_BUS_NUM)
549 #define CONFIG_SYS_SPD_BUS_NUM 0
553 void (*init)(struct i2c_adapter *adap, int speed,
555 int (*probe)(struct i2c_adapter *adap, uint8_t chip);
556 int (*read)(struct i2c_adapter *adap, uint8_t chip,
557 uint addr, int alen, uint8_t *buffer,
559 int (*write)(struct i2c_adapter *adap, uint8_t chip,
560 uint addr, int alen, uint8_t *buffer,
562 uint (*set_bus_speed)(struct i2c_adapter *adap,
572 #define U_BOOT_I2C_MKENT_COMPLETE(_init, _probe, _read, _write, \
573 _set_speed, _speed, _slaveaddr, _hwadapnr, _name) \
579 .set_bus_speed = _set_speed, \
581 .slaveaddr = _slaveaddr, \
583 .hwadapnr = _hwadapnr, \
587 #define U_BOOT_I2C_ADAP_COMPLETE(_name, _init, _probe, _read, _write, \
588 _set_speed, _speed, _slaveaddr, _hwadapnr) \
589 ll_entry_declare(struct i2c_adapter, _name, i2c) = \
590 U_BOOT_I2C_MKENT_COMPLETE(_init, _probe, _read, _write, \
591 _set_speed, _speed, _slaveaddr, _hwadapnr, _name);
593 struct i2c_adapter *i2c_get_adapter(int index);
595 #ifndef CONFIG_SYS_I2C_DIRECT_BUS
601 struct i2c_next_hop {
607 struct i2c_bus_hose {
609 struct i2c_next_hop next_hop[CONFIG_SYS_I2C_MAX_HOPS];
611 #define I2C_NULL_HOP {{-1, ""}, 0, 0}
612 extern struct i2c_bus_hose i2c_bus[];
614 #define I2C_ADAPTER(bus) i2c_bus[bus].adapter
616 #define I2C_ADAPTER(bus) bus
618 #define I2C_BUS gd->cur_i2c_bus
620 #define I2C_ADAP_NR(bus) i2c_get_adapter(I2C_ADAPTER(bus))
621 #define I2C_ADAP I2C_ADAP_NR(gd->cur_i2c_bus)
622 #define I2C_ADAP_HWNR (I2C_ADAP->hwadapnr)
624 #ifndef CONFIG_SYS_I2C_DIRECT_BUS
625 #define I2C_MUX_PCA9540_ID 1
626 #define I2C_MUX_PCA9540 {I2C_MUX_PCA9540_ID, "PCA9540B"}
627 #define I2C_MUX_PCA9542_ID 2
628 #define I2C_MUX_PCA9542 {I2C_MUX_PCA9542_ID, "PCA9542A"}
629 #define I2C_MUX_PCA9544_ID 3
630 #define I2C_MUX_PCA9544 {I2C_MUX_PCA9544_ID, "PCA9544A"}
631 #define I2C_MUX_PCA9547_ID 4
632 #define I2C_MUX_PCA9547 {I2C_MUX_PCA9547_ID, "PCA9547A"}
633 #define I2C_MUX_PCA9548_ID 5
634 #define I2C_MUX_PCA9548 {I2C_MUX_PCA9548_ID, "PCA9548"}
637 #ifndef I2C_SOFT_DECLARATIONS
638 # if (defined(CONFIG_AT91RM9200) || \
639 defined(CONFIG_AT91SAM9260) || defined(CONFIG_AT91SAM9261) || \
640 defined(CONFIG_AT91SAM9263))
641 # define I2C_SOFT_DECLARATIONS at91_pio_t *pio = (at91_pio_t *) ATMEL_BASE_PIOA;
643 # define I2C_SOFT_DECLARATIONS
648 * Many boards/controllers/drivers don't support an I2C slave interface so
649 * provide a default slave address for them for use in common code. A real
650 * value for CONFIG_SYS_I2C_SLAVE should be defined for any board which does
651 * support a slave interface.
653 #ifndef CONFIG_SYS_I2C_SLAVE
654 #define CONFIG_SYS_I2C_SLAVE 0xfe
658 * Initialization, must be called once on start up, may be called
659 * repeatedly to change the speed and slave addresses.
661 #ifdef CONFIG_SYS_I2C_EARLY_INIT
662 void i2c_early_init_f(void);
664 void i2c_init(int speed, int slaveaddr);
665 void i2c_init_board(void);
667 #ifdef CONFIG_SYS_I2C
671 * Returns index of currently active I2C bus. Zero-based.
673 unsigned int i2c_get_bus_num(void);
678 * Change the active I2C bus. Subsequent read/write calls will
681 * bus - bus index, zero based
683 * Returns: 0 on success, not 0 on failure
686 int i2c_set_bus_num(unsigned int bus);
691 * Initializes all I2C adapters in the system. All i2c_adap structures must
692 * be initialized beforehead with function pointers and data, including
693 * speed and slaveaddr. Returns 0 on success, non-0 on failure.
695 void i2c_init_all(void);
698 * Probe the given I2C chip address. Returns 0 if a chip responded,
701 int i2c_probe(uint8_t chip);
704 * Read/Write interface:
705 * chip: I2C chip address, range 0..127
706 * addr: Memory (register) address within the chip
707 * alen: Number of bytes to use for addr (typically 1, 2 for larger
708 * memories, 0 for register type devices with only one
710 * buffer: Where to read/write the data
711 * len: How many bytes to read/write
713 * Returns: 0 on success, not 0 on failure
715 int i2c_read(uint8_t chip, unsigned int addr, int alen,
716 uint8_t *buffer, int len);
718 int i2c_write(uint8_t chip, unsigned int addr, int alen,
719 uint8_t *buffer, int len);
722 * Utility routines to read/write registers.
724 uint8_t i2c_reg_read(uint8_t addr, uint8_t reg);
726 void i2c_reg_write(uint8_t addr, uint8_t reg, uint8_t val);
731 * Change the speed of the active I2C bus
733 * speed - bus speed in Hz
735 * Returns: new bus speed
738 unsigned int i2c_set_bus_speed(unsigned int speed);
743 * Returns speed of currently active I2C bus in Hz
746 unsigned int i2c_get_bus_speed(void);
751 * Probe the given I2C chip address. Returns 0 if a chip responded,
754 int i2c_probe(uchar chip);
757 * Read/Write interface:
758 * chip: I2C chip address, range 0..127
759 * addr: Memory (register) address within the chip
760 * alen: Number of bytes to use for addr (typically 1, 2 for larger
761 * memories, 0 for register type devices with only one
763 * buffer: Where to read/write the data
764 * len: How many bytes to read/write
766 * Returns: 0 on success, not 0 on failure
768 int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len);
769 int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len);
772 * Utility routines to read/write registers.
774 static inline u8 i2c_reg_read(u8 addr, u8 reg)
779 printf("%s: addr=0x%02x, reg=0x%02x\n", __func__, addr, reg);
782 i2c_read(addr, reg, 1, &buf, 1);
787 static inline void i2c_reg_write(u8 addr, u8 reg, u8 val)
790 printf("%s: addr=0x%02x, reg=0x%02x, val=0x%02x\n",
791 __func__, addr, reg, val);
794 i2c_write(addr, reg, 1, &val, 1);
798 * Functions for setting the current I2C bus and its speed
804 * Change the active I2C bus. Subsequent read/write calls will
807 * bus - bus index, zero based
809 * Returns: 0 on success, not 0 on failure
812 int i2c_set_bus_num(unsigned int bus);
817 * Returns index of currently active I2C bus. Zero-based.
820 unsigned int i2c_get_bus_num(void);
825 * Change the speed of the active I2C bus
827 * speed - bus speed in Hz
829 * Returns: 0 on success, not 0 on failure
832 int i2c_set_bus_speed(unsigned int);
837 * Returns speed of currently active I2C bus in Hz
840 unsigned int i2c_get_bus_speed(void);
841 #endif /* CONFIG_SYS_I2C */
844 * only for backwardcompatibility, should go away if we switched
845 * completely to new multibus support.
847 #if defined(CONFIG_SYS_I2C) || defined(CONFIG_I2C_MULTI_BUS)
848 # if !defined(CONFIG_SYS_MAX_I2C_BUS)
849 # define CONFIG_SYS_MAX_I2C_BUS 2
851 # define I2C_MULTI_BUS 1
853 # define CONFIG_SYS_MAX_I2C_BUS 1
854 # define I2C_MULTI_BUS 0
857 /* NOTE: These two functions MUST be always_inline to avoid code growth! */
858 static inline unsigned int I2C_GET_BUS(void) __attribute__((always_inline));
859 static inline unsigned int I2C_GET_BUS(void)
861 return I2C_MULTI_BUS ? i2c_get_bus_num() : 0;
864 static inline void I2C_SET_BUS(unsigned int bus) __attribute__((always_inline));
865 static inline void I2C_SET_BUS(unsigned int bus)
868 i2c_set_bus_num(bus);
871 /* Multi I2C definitions */
873 I2C_0, I2C_1, I2C_2, I2C_3, I2C_4, I2C_5, I2C_6, I2C_7,
874 I2C_8, I2C_9, I2C_10,
878 * Get FDT values for i2c bus.
880 * @param blob Device tree blbo
881 * @return the number of I2C bus
883 void board_i2c_init(const void *blob);
886 * Find the I2C bus number by given a FDT I2C node.
888 * @param blob Device tree blbo
889 * @param node FDT I2C node to find
890 * @return the number of I2C bus (zero based), or -1 on error
892 int i2c_get_bus_num_fdt(int node);
895 * Reset the I2C bus represented by the given a FDT I2C node.
897 * @param blob Device tree blbo
898 * @param node FDT I2C node to find
899 * @return 0 if port was reset, -1 if not found
901 int i2c_reset_port_fdt(const void *blob, int node);
903 #endif /* !CONFIG_DM_I2C */