1 /* SPDX-License-Identifier: GPL-2.0+ */
5 * Changes for multibus/multiadapter I2C support.
10 * The original I2C interface was
12 * AIRVENT SAM s.p.a - RIMINI(ITALY)
13 * but has been changed substantially.
19 #include <linker_lists.h>
22 * For now there are essentially two parts to this file - driver model
23 * here at the top, and the older code below (with CONFIG_SYS_I2C_LEGACY being
24 * most recent). The plan is to migrate everything to driver model.
25 * The driver model structures and API are separate as they are different
26 * enough as to be incompatible for compilation purposes.
29 enum dm_i2c_chip_flags {
30 DM_I2C_CHIP_10BIT = 1 << 0, /* Use 10-bit addressing */
31 DM_I2C_CHIP_RD_ADDRESS = 1 << 1, /* Send address for each read byte */
32 DM_I2C_CHIP_WR_ADDRESS = 1 << 2, /* Send address for each write byte */
35 /** enum i2c_speed_mode - standard I2C speed modes */
37 IC_SPEED_MODE_STANDARD,
39 IC_SPEED_MODE_FAST_PLUS,
41 IC_SPEED_MODE_FAST_ULTRA,
46 /** enum i2c_speed_rate - standard I2C speeds in Hz */
48 I2C_SPEED_STANDARD_RATE = 100000,
49 I2C_SPEED_FAST_RATE = 400000,
50 I2C_SPEED_FAST_PLUS_RATE = 1000000,
51 I2C_SPEED_HIGH_RATE = 3400000,
52 I2C_SPEED_FAST_ULTRA_RATE = 5000000,
55 /** enum i2c_address_mode - available address modes */
56 enum i2c_address_mode {
61 /** enum i2c_device_t - Types of I2C devices, used for compatible strings */
64 I2C_DEVICE_HID_OVER_I2C,
69 * struct dm_i2c_chip - information about an i2c chip
71 * An I2C chip is a device on the I2C bus. It sits at a particular address
72 * and normally supports 7-bit or 10-bit addressing.
74 * To obtain this structure, use dev_get_parent_plat(dev) where dev is
75 * the chip to examine.
77 * @chip_addr: Chip address on bus
78 * @offset_len: Length of offset in bytes. A single byte offset can
79 * represent up to 256 bytes. A value larger than 1 may be
80 * needed for larger devices.
81 * @flags: Flags for this chip (dm_i2c_chip_flags)
82 * @chip_addr_offset_mask: Mask of offset bits within chip_addr. Used for
83 * devices which steal addresses as part of offset.
84 * If offset_len is zero, then the offset is encoded
85 * completely within the chip address itself.
86 * e.g. a devce with chip address of 0x2c with 512
87 * registers might use the bottom bit of the address
88 * to indicate which half of the address space is being
89 * accessed while still only using 1 byte offset.
90 * This means it will respond to chip address 0x2c and
92 * A real world example is the Atmel AT24C04. It's
93 * datasheet explains it's usage of this addressing
95 * @emul: Emulator for this chip address (only used for emulation)
96 * @emul_idx: Emulator index, used for of-platdata and set by each i2c chip's
97 * bind() method. This allows i2c_emul_find() to work with of-platdata.
103 uint chip_addr_offset_mask;
104 #ifdef CONFIG_SANDBOX
105 struct udevice *emul;
112 * struct dm_i2c_bus- information about an i2c bus
114 * An I2C bus contains 0 or more chips on it, each at its own address. The
115 * bus can operate at different speeds (measured in Hz, typically 100KHz
118 * To obtain this structure, use dev_get_uclass_priv(bus) where bus is the
121 * @speed_hz: Bus speed in hertz (typically 100000)
122 * @max_transaction_bytes: Maximal size of single I2C transfer
126 int max_transaction_bytes;
130 * Not all of these flags are implemented in the U-Boot API
132 enum dm_i2c_msg_flags {
133 I2C_M_TEN = 0x0010, /* ten-bit chip address */
134 I2C_M_RD = 0x0001, /* read data, from slave to master */
135 I2C_M_STOP = 0x8000, /* send stop after this message */
136 I2C_M_NOSTART = 0x4000, /* no start before this message */
137 I2C_M_REV_DIR_ADDR = 0x2000, /* invert polarity of R/W bit */
138 I2C_M_IGNORE_NAK = 0x1000, /* continue after NAK */
139 I2C_M_NO_RD_ACK = 0x0800, /* skip the Ack bit on reads */
140 I2C_M_RECV_LEN = 0x0400, /* length is first received byte */
144 * struct i2c_msg - an I2C message
146 * @addr: Slave address
147 * @flags: Flags (see enum dm_i2c_msg_flags)
148 * @len: Length of buffer in bytes, may be 0 for a probe
149 * @buf: Buffer to send/receive, or NULL if no data
159 * struct i2c_msg_list - a list of I2C messages
161 * This is called i2c_rdwr_ioctl_data in Linux but the name does not seem
162 * appropriate in U-Boot.
164 * @msg: Pointer to i2c_msg array
165 * @nmsgs: Number of elements in the array
167 struct i2c_msg_list {
168 struct i2c_msg *msgs;
173 * dm_i2c_read() - read bytes from an I2C chip
175 * To obtain an I2C device (called a 'chip') given the I2C bus address you
176 * can use i2c_get_chip(). To obtain a bus by bus number use
177 * uclass_get_device_by_seq(UCLASS_I2C, <bus number>).
179 * To set the address length of a devce use i2c_set_addr_len(). It
182 * @dev: Chip to read from
183 * @offset: Offset within chip to start reading
184 * @buffer: Place to put data
185 * @len: Number of bytes to read
187 * Return: 0 on success, -ve on failure
189 int dm_i2c_read(struct udevice *dev, uint offset, uint8_t *buffer, int len);
192 * dm_i2c_write() - write bytes to an I2C chip
194 * See notes for dm_i2c_read() above.
196 * @dev: Chip to write to
197 * @offset: Offset within chip to start writing
198 * @buffer: Buffer containing data to write
199 * @len: Number of bytes to write
201 * Return: 0 on success, -ve on failure
203 int dm_i2c_write(struct udevice *dev, uint offset, const uint8_t *buffer,
207 * dm_i2c_probe() - probe a particular chip address
209 * This can be useful to check for the existence of a chip on the bus.
210 * It is typically implemented by writing the chip address to the bus
211 * and checking that the chip replies with an ACK.
214 * @chip_addr: 7-bit address to probe (10-bit and others are not supported)
215 * @chip_flags: Flags for the probe (see enum dm_i2c_chip_flags)
216 * @devp: Returns the device found, or NULL if none
217 * Return: 0 if a chip was found at that address, -ve if not
219 int dm_i2c_probe(struct udevice *bus, uint chip_addr, uint chip_flags,
220 struct udevice **devp);
223 * dm_i2c_reg_read() - Read a value from an I2C register
225 * This reads a single value from the given address in an I2C chip
227 * @dev: Device to use for transfer
228 * @addr: Address to read from
229 * Return: value read, or -ve on error
231 int dm_i2c_reg_read(struct udevice *dev, uint offset);
234 * dm_i2c_reg_write() - Write a value to an I2C register
236 * This writes a single value to the given address in an I2C chip
238 * @dev: Device to use for transfer
239 * @addr: Address to write to
240 * @val: Value to write (normally a byte)
241 * Return: 0 on success, -ve on error
243 int dm_i2c_reg_write(struct udevice *dev, uint offset, unsigned int val);
246 * dm_i2c_reg_clrset() - Apply bitmask to an I2C register
248 * Read value, apply bitmask and write modified value back to the
249 * given address in an I2C chip
251 * @dev: Device to use for transfer
252 * @offset: Address for the R/W operation
253 * @clr: Bitmask of bits that should be cleared
254 * @set: Bitmask of bits that should be set
255 * Return: 0 on success, -ve on error
257 int dm_i2c_reg_clrset(struct udevice *dev, uint offset, u32 clr, u32 set);
260 * dm_i2c_xfer() - Transfer messages over I2C
262 * This transfers a raw message. It is best to use dm_i2c_reg_read/write()
265 * @dev: Device to use for transfer
266 * @msg: List of messages to transfer
267 * @nmsgs: Number of messages to transfer
268 * Return: 0 on success, -ve on error
270 int dm_i2c_xfer(struct udevice *dev, struct i2c_msg *msg, int nmsgs);
273 * dm_i2c_set_bus_speed() - set the speed of a bus
275 * @bus: Bus to adjust
276 * @speed: Requested speed in Hz
277 * Return: 0 if OK, -EINVAL for invalid values
279 int dm_i2c_set_bus_speed(struct udevice *bus, unsigned int speed);
282 * dm_i2c_get_bus_speed() - get the speed of a bus
285 * Return: speed of selected I2C bus in Hz, -ve on error
287 int dm_i2c_get_bus_speed(struct udevice *bus);
290 * i2c_set_chip_flags() - set flags for a chip
292 * Typically addresses are 7 bits, but for 10-bit addresses you should set
293 * flags to DM_I2C_CHIP_10BIT. All accesses will then use 10-bit addressing.
295 * @dev: Chip to adjust
297 * Return: 0 if OK, -EINVAL if value is unsupported, other -ve value on error
299 int i2c_set_chip_flags(struct udevice *dev, uint flags);
302 * i2c_get_chip_flags() - get flags for a chip
304 * @dev: Chip to check
305 * @flagsp: Place to put flags
306 * Return: 0 if OK, other -ve value on error
308 int i2c_get_chip_flags(struct udevice *dev, uint *flagsp);
311 * i2c_set_offset_len() - set the offset length for a chip
313 * The offset used to access a chip may be up to 4 bytes long. Typically it
314 * is only 1 byte, which is enough for chips with 256 bytes of memory or
315 * registers. The default value is 1, but you can call this function to
318 * @offset_len: New offset length value (typically 1 or 2)
320 int i2c_set_chip_offset_len(struct udevice *dev, uint offset_len);
323 * i2c_get_offset_len() - get the offset length for a chip
325 * @return: Current offset length value (typically 1 or 2)
327 int i2c_get_chip_offset_len(struct udevice *dev);
330 * i2c_set_chip_addr_offset_mask() - set mask of address bits usable by offset
332 * Some devices listen on multiple chip addresses to achieve larger offsets
333 * than their single or multiple byte offsets would allow for. You can use this
334 * function to set the bits that are valid to be used for offset overflow.
336 * @mask: The mask to be used for high offset bits within address
337 * Return: 0 if OK, other -ve value on error
339 int i2c_set_chip_addr_offset_mask(struct udevice *dev, uint mask);
342 * i2c_get_chip_addr_offset_mask() - get mask of address bits usable by offset
344 * Return: current chip addr offset mask
346 uint i2c_get_chip_addr_offset_mask(struct udevice *dev);
349 * i2c_deblock() - recover a bus that is in an unknown state
351 * See the deblock() method in 'struct dm_i2c_ops' for full information
353 * @bus: Bus to recover
354 * Return: 0 if OK, -ve on error
356 int i2c_deblock(struct udevice *bus);
359 * i2c_deblock_gpio_loop() - recover a bus from an unknown state by toggling SDA/SCL
361 * This is the inner logic used for toggling I2C SDA/SCL lines as GPIOs
362 * for deblocking the I2C bus.
366 * @scl_count: Number of SCL clock cycles generated to deblock SDA
367 * @start_count:Number of I2C start conditions sent after deblocking SDA
368 * @delay: Delay between SCL clock line changes
369 * Return: 0 if OK, -ve on error
372 int i2c_deblock_gpio_loop(struct gpio_desc *sda_pin, struct gpio_desc *scl_pin,
373 unsigned int scl_count, unsigned int start_count,
377 * struct dm_i2c_ops - driver operations for I2C uclass
379 * Drivers should support these operations unless otherwise noted. These
380 * operations are intended to be used by uclass code, not directly from
385 * xfer() - transfer a list of I2C messages
387 * @bus: Bus to read from
388 * @msg: List of messages to transfer
389 * @nmsgs: Number of messages in the list
390 * @return 0 if OK, -EREMOTEIO if the slave did not ACK a byte,
391 * -ECOMM if the speed cannot be supported, -EPROTO if the chip
392 * flags cannot be supported, other -ve value on some other error
394 int (*xfer)(struct udevice *bus, struct i2c_msg *msg, int nmsgs);
397 * probe_chip() - probe for the presense of a chip address
399 * This function is optional. If omitted, the uclass will send a zero
400 * length message instead.
403 * @chip_addr: Chip address to probe
404 * @chip_flags: Probe flags (enum dm_i2c_chip_flags)
405 * @return 0 if chip was found, -EREMOTEIO if not, -ENOSYS to fall back
406 * to default probem other -ve value on error
408 int (*probe_chip)(struct udevice *bus, uint chip_addr, uint chip_flags);
411 * set_bus_speed() - set the speed of a bus (optional)
413 * The bus speed value will be updated by the uclass if this function
414 * does not return an error. This method is optional - if it is not
415 * provided then the driver can read the speed from
416 * dev_get_uclass_priv(bus)->speed_hz
418 * @bus: Bus to adjust
419 * @speed: Requested speed in Hz
420 * @return 0 if OK, -EINVAL for invalid values
422 int (*set_bus_speed)(struct udevice *bus, unsigned int speed);
425 * get_bus_speed() - get the speed of a bus (optional)
427 * Normally this can be provided by the uclass, but if you want your
428 * driver to check the bus speed by looking at the hardware, you can
429 * implement that here. This method is optional. This method would
430 * normally be expected to return dev_get_uclass_priv(bus)->speed_hz.
433 * @return speed of selected I2C bus in Hz, -ve on error
435 int (*get_bus_speed)(struct udevice *bus);
438 * set_flags() - set the flags for a chip (optional)
440 * This is generally implemented by the uclass, but drivers can
441 * check the value to ensure that unsupported options are not used.
442 * This method is optional. If provided, this method will always be
443 * called when the flags change.
445 * @dev: Chip to adjust
446 * @flags: New flags value
447 * @return 0 if OK, -EINVAL if value is unsupported
449 int (*set_flags)(struct udevice *dev, uint flags);
452 * deblock() - recover a bus that is in an unknown state
454 * I2C is a synchronous protocol and resets of the processor in the
455 * middle of an access can block the I2C Bus until a powerdown of
456 * the full unit is done. This is because slaves can be stuck
457 * waiting for addition bus transitions for a transaction that will
458 * never complete. Resetting the I2C master does not help. The only
459 * way is to force the bus through a series of transitions to make
460 * sure that all slaves are done with the transaction. This method
461 * performs this 'deblocking' if support by the driver.
463 * This method is optional.
465 int (*deblock)(struct udevice *bus);
468 #define i2c_get_ops(dev) ((struct dm_i2c_ops *)(dev)->driver->ops)
471 * struct i2c_mux_ops - operations for an I2C mux
473 * The current mux state is expected to be stored in the mux itself since
474 * it is the only thing that knows how to make things work. The mux can
475 * record the current state and then avoid switching unless it is necessary.
476 * So select() can be skipped if the mux is already in the correct state.
477 * Also deselect() can be made a nop if required.
481 * select() - select one of of I2C buses attached to a mux
483 * This will be called when there is no bus currently selected by the
484 * mux. This method does not need to deselect the old bus since
485 * deselect() will be already have been called if necessary.
488 * @bus: I2C bus to select
489 * @channel: Channel number correponding to the bus to select
490 * @return 0 if OK, -ve on error
492 int (*select)(struct udevice *mux, struct udevice *bus, uint channel);
495 * deselect() - select one of of I2C buses attached to a mux
497 * This is used to deselect the currently selected I2C bus.
500 * @bus: I2C bus to deselect
501 * @channel: Channel number correponding to the bus to deselect
502 * @return 0 if OK, -ve on error
504 int (*deselect)(struct udevice *mux, struct udevice *bus, uint channel);
507 #define i2c_mux_get_ops(dev) ((struct i2c_mux_ops *)(dev)->driver->ops)
510 * i2c_get_chip() - get a device to use to access a chip on a bus
512 * This returns the device for the given chip address. The device can then
513 * be used with calls to i2c_read(), i2c_write(), i2c_probe(), etc.
515 * @bus: Bus to examine
516 * @chip_addr: Chip address for the new device
517 * @offset_len: Length of a register offset in bytes (normally 1)
518 * @devp: Returns pointer to new device if found or -ENODEV if not
521 int i2c_get_chip(struct udevice *bus, uint chip_addr, uint offset_len,
522 struct udevice **devp);
525 * i2c_get_chip_for_busnum() - get a device to use to access a chip on
528 * This returns the device for the given chip address on a particular bus
531 * @busnum: Bus number to examine
532 * @chip_addr: Chip address for the new device
533 * @offset_len: Length of a register offset in bytes (normally 1)
534 * @devp: Returns pointer to new device if found or -ENODEV if not
537 int i2c_get_chip_for_busnum(int busnum, int chip_addr, uint offset_len,
538 struct udevice **devp);
541 * i2c_get_chip_by_phandle() - get a device to use to access a chip
542 * based on a phandle property pointing to it
544 * @parent: Parent device containing the phandle pointer
545 * @name: Name of phandle property in the parent device node
546 * @devp: Returns pointer to new device or NULL if not found
547 * Return: 0 on success, -ve on failure
549 int i2c_get_chip_by_phandle(const struct udevice *parent, const char *prop_name,
550 struct udevice **devp);
553 * i2c_chip_of_to_plat() - Decode standard I2C platform data
555 * This decodes the chip address from a device tree node and puts it into
556 * its dm_i2c_chip structure. This should be called in your driver's
557 * of_to_plat() method.
559 * @blob: Device tree blob
560 * @node: Node offset to read from
561 * @spi: Place to put the decoded information
563 int i2c_chip_of_to_plat(struct udevice *dev, struct dm_i2c_chip *chip);
566 * i2c_dump_msgs() - Dump a list of I2C messages
568 * This may be useful for debugging.
570 * @msg: Message list to dump
571 * @nmsgs: Number of messages
573 void i2c_dump_msgs(struct i2c_msg *msg, int nmsgs);
576 * i2c_emul_find() - Find an emulator for an i2c sandbox device
578 * This looks at the device's 'emul' phandle
580 * @dev: Device to find an emulator for
581 * @emulp: Returns the associated emulator, if found *
582 * Return: 0 if OK, -ENOENT or -ENODEV if not found
584 int i2c_emul_find(struct udevice *dev, struct udevice **emulp);
587 * i2c_emul_set_idx() - Set the emulator index for an i2c sandbox device
589 * With of-platdata we cannot find the emulator using the device tree, so rely
590 * on the bind() method of each i2c driver calling this function to tell us
591 * the of-platdata idx of the emulator
593 * @dev: i2c device to set the emulator for
594 * @emul_idx: of-platdata index for that emulator
596 void i2c_emul_set_idx(struct udevice *dev, int emul_idx);
599 * i2c_emul_get_device() - Find the device being emulated
601 * Given an emulator this returns the associated device
603 * @emul: Emulator for the device
604 * Return: device that @emul is emulating
606 struct udevice *i2c_emul_get_device(struct udevice *emul);
608 /* ACPI operations for generic I2C devices */
609 extern struct acpi_ops i2c_acpi_ops;
612 * acpi_i2c_of_to_plat() - Read properties intended for ACPI
614 * This reads the generic I2C properties from the device tree, so that these
615 * can be used to create ACPI information for the device.
617 * See the i2c/generic-acpi.txt binding file for information about the
620 * @dev: I2C device to process
621 * Return: 0 if OK, -EINVAL if acpi,hid is not present
623 int acpi_i2c_of_to_plat(struct udevice *dev);
625 #ifdef CONFIG_SYS_I2C_EARLY_INIT
626 void i2c_early_init_f(void);
629 #if !CONFIG_IS_ENABLED(DM_I2C)
632 * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
634 * The implementation MUST NOT use static or global variables if the
635 * I2C routines are used to read SDRAM configuration information
636 * because this is done before the memories are initialized. Limited
637 * use of stack-based variables are OK (the initial stack size is
640 * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
644 * Configuration items.
646 #define I2C_RXTX_LEN 128 /* maximum tx/rx buffer length */
648 /* no muxes used bus = i2c adapters */
649 #define CFG_SYS_NUM_I2C_BUSES ll_entry_count(struct i2c_adapter, i2c)
652 void (*init)(struct i2c_adapter *adap, int speed,
654 int (*probe)(struct i2c_adapter *adap, uint8_t chip);
655 int (*read)(struct i2c_adapter *adap, uint8_t chip,
656 uint addr, int alen, uint8_t *buffer,
658 int (*write)(struct i2c_adapter *adap, uint8_t chip,
659 uint addr, int alen, uint8_t *buffer,
661 uint (*set_bus_speed)(struct i2c_adapter *adap,
671 #define U_BOOT_I2C_MKENT_COMPLETE(_init, _probe, _read, _write, \
672 _set_speed, _speed, _slaveaddr, _hwadapnr, _name) \
678 .set_bus_speed = _set_speed, \
680 .slaveaddr = _slaveaddr, \
682 .hwadapnr = _hwadapnr, \
686 #define U_BOOT_I2C_ADAP_COMPLETE(_name, _init, _probe, _read, _write, \
687 _set_speed, _speed, _slaveaddr, _hwadapnr) \
688 ll_entry_declare(struct i2c_adapter, _name, i2c) = \
689 U_BOOT_I2C_MKENT_COMPLETE(_init, _probe, _read, _write, \
690 _set_speed, _speed, _slaveaddr, _hwadapnr, _name);
692 struct i2c_adapter *i2c_get_adapter(int index);
694 #define I2C_ADAPTER(bus) bus
695 #define I2C_BUS gd->cur_i2c_bus
697 #define I2C_ADAP_NR(bus) i2c_get_adapter(I2C_ADAPTER(bus))
698 #define I2C_ADAP I2C_ADAP_NR(gd->cur_i2c_bus)
699 #define I2C_ADAP_HWNR (I2C_ADAP->hwadapnr)
701 #ifndef I2C_SOFT_DECLARATIONS
702 # if (defined(CONFIG_AT91RM9200) || \
703 defined(CONFIG_AT91SAM9260) || defined(CONFIG_AT91SAM9261) || \
704 defined(CONFIG_AT91SAM9263))
705 # define I2C_SOFT_DECLARATIONS at91_pio_t *pio = (at91_pio_t *) ATMEL_BASE_PIOA;
707 # define I2C_SOFT_DECLARATIONS
712 * Initialization, must be called once on start up, may be called
713 * repeatedly to change the speed and slave addresses.
715 void i2c_init(int speed, int slaveaddr);
716 void i2c_init_board(void);
718 #if CONFIG_IS_ENABLED(SYS_I2C_LEGACY)
722 * Returns index of currently active I2C bus. Zero-based.
724 unsigned int i2c_get_bus_num(void);
729 * Change the active I2C bus. Subsequent read/write calls will
732 * bus - bus index, zero based
734 * Returns: 0 on success, not 0 on failure
737 int i2c_set_bus_num(unsigned int bus);
742 * Initializes all I2C adapters in the system. All i2c_adap structures must
743 * be initialized beforehead with function pointers and data, including
744 * speed and slaveaddr. Returns 0 on success, non-0 on failure.
746 void i2c_init_all(void);
749 * Probe the given I2C chip address. Returns 0 if a chip responded,
752 int i2c_probe(uint8_t chip);
755 * Read/Write interface:
756 * chip: I2C chip address, range 0..127
757 * addr: Memory (register) address within the chip
758 * alen: Number of bytes to use for addr (typically 1, 2 for larger
759 * memories, 0 for register type devices with only one
761 * buffer: Where to read/write the data
762 * len: How many bytes to read/write
764 * Returns: 0 on success, not 0 on failure
766 int i2c_read(uint8_t chip, unsigned int addr, int alen,
767 uint8_t *buffer, int len);
769 int i2c_write(uint8_t chip, unsigned int addr, int alen,
770 uint8_t *buffer, int len);
773 * Utility routines to read/write registers.
775 uint8_t i2c_reg_read(uint8_t addr, uint8_t reg);
777 void i2c_reg_write(uint8_t addr, uint8_t reg, uint8_t val);
782 * Change the speed of the active I2C bus
784 * speed - bus speed in Hz
786 * Returns: new bus speed
789 unsigned int i2c_set_bus_speed(unsigned int speed);
794 * Returns speed of currently active I2C bus in Hz
797 unsigned int i2c_get_bus_speed(void);
802 * Probe the given I2C chip address. Returns 0 if a chip responded,
805 int i2c_probe(uchar chip);
808 * Read/Write interface:
809 * chip: I2C chip address, range 0..127
810 * addr: Memory (register) address within the chip
811 * alen: Number of bytes to use for addr (typically 1, 2 for larger
812 * memories, 0 for register type devices with only one
814 * buffer: Where to read/write the data
815 * len: How many bytes to read/write
817 * Returns: 0 on success, not 0 on failure
819 int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len);
820 int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len);
823 * Utility routines to read/write registers.
825 static inline u8 i2c_reg_read(u8 addr, u8 reg)
830 printf("%s: addr=0x%02x, reg=0x%02x\n", __func__, addr, reg);
833 i2c_read(addr, reg, 1, &buf, 1);
838 static inline void i2c_reg_write(u8 addr, u8 reg, u8 val)
841 printf("%s: addr=0x%02x, reg=0x%02x, val=0x%02x\n",
842 __func__, addr, reg, val);
845 i2c_write(addr, reg, 1, &val, 1);
849 * Functions for setting the current I2C bus and its speed
855 * Change the active I2C bus. Subsequent read/write calls will
858 * bus - bus index, zero based
860 * Returns: 0 on success, not 0 on failure
863 int i2c_set_bus_num(unsigned int bus);
868 * Returns index of currently active I2C bus. Zero-based.
871 unsigned int i2c_get_bus_num(void);
876 * Change the speed of the active I2C bus
878 * speed - bus speed in Hz
880 * Returns: 0 on success, not 0 on failure
883 int i2c_set_bus_speed(unsigned int);
888 * Returns speed of currently active I2C bus in Hz
891 unsigned int i2c_get_bus_speed(void);
892 #endif /* CONFIG_SYS_I2C_LEGACY */
894 #endif /* !CONFIG_DM_I2C */