2 * Copyright (C) 2009 Sergey Kubushyn <ksi@koi8.net>
3 * Copyright (C) 2009 - 2013 Heiko Schocher <hs@denx.de>
4 * Changes for multibus/multiadapter I2C support.
7 * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com.
9 * SPDX-License-Identifier: GPL-2.0+
11 * The original I2C interface was
12 * (C) 2000 by Paolo Scaffardi (arsenio@tin.it)
13 * AIRVENT SAM s.p.a - RIMINI(ITALY)
14 * but has been changed substantially.
21 * For now there are essentially two parts to this file - driver model
22 * here at the top, and the older code below (with CONFIG_SYS_I2C being
23 * most recent). The plan is to migrate everything to driver model.
24 * The driver model structures and API are separate as they are different
25 * enough as to be incompatible for compilation purposes.
30 enum dm_i2c_chip_flags {
31 DM_I2C_CHIP_10BIT = 1 << 0, /* Use 10-bit addressing */
32 DM_I2C_CHIP_RD_ADDRESS = 1 << 1, /* Send address for each read byte */
33 DM_I2C_CHIP_WR_ADDRESS = 1 << 2, /* Send address for each write byte */
37 * struct dm_i2c_chip - information about an i2c chip
39 * An I2C chip is a device on the I2C bus. It sits at a particular address
40 * and normally supports 7-bit or 10-bit addressing.
42 * To obtain this structure, use dev_get_parentdata(dev) where dev is the
45 * @chip_addr: Chip address on bus
46 * @offset_len: Length of offset in bytes. A single byte offset can
47 * represent up to 256 bytes. A value larger than 1 may be
48 * needed for larger devices.
49 * @flags: Flags for this chip (dm_i2c_chip_flags)
50 * @emul: Emulator for this chip address (only used for emulation)
62 * struct dm_i2c_bus- information about an i2c bus
64 * An I2C bus contains 0 or more chips on it, each at its own address. The
65 * bus can operate at different speeds (measured in Hz, typically 100KHz
68 * To obtain this structure, use bus->uclass_priv where bus is the I2C
71 * @speed_hz: Bus speed in hertz (typically 100000)
78 * dm_i2c_read() - read bytes from an I2C chip
80 * To obtain an I2C device (called a 'chip') given the I2C bus address you
81 * can use i2c_get_chip(). To obtain a bus by bus number use
82 * uclass_get_device_by_seq(UCLASS_I2C, <bus number>).
84 * To set the address length of a devce use i2c_set_addr_len(). It
87 * @dev: Chip to read from
88 * @offset: Offset within chip to start reading
89 * @buffer: Place to put data
90 * @len: Number of bytes to read
92 * @return 0 on success, -ve on failure
94 int dm_i2c_read(struct udevice *dev, uint offset, uint8_t *buffer, int len);
97 * dm_i2c_write() - write bytes to an I2C chip
99 * See notes for dm_i2c_read() above.
101 * @dev: Chip to write to
102 * @offset: Offset within chip to start writing
103 * @buffer: Buffer containing data to write
104 * @len: Number of bytes to write
106 * @return 0 on success, -ve on failure
108 int dm_i2c_write(struct udevice *dev, uint offset, const uint8_t *buffer,
112 * dm_i2c_probe() - probe a particular chip address
114 * This can be useful to check for the existence of a chip on the bus.
115 * It is typically implemented by writing the chip address to the bus
116 * and checking that the chip replies with an ACK.
119 * @chip_addr: 7-bit address to probe (10-bit and others are not supported)
120 * @chip_flags: Flags for the probe (see enum dm_i2c_chip_flags)
121 * @devp: Returns the device found, or NULL if none
122 * @return 0 if a chip was found at that address, -ve if not
124 int dm_i2c_probe(struct udevice *bus, uint chip_addr, uint chip_flags,
125 struct udevice **devp);
128 * i2c_set_bus_speed() - set the speed of a bus
130 * @bus: Bus to adjust
131 * @speed: Requested speed in Hz
132 * @return 0 if OK, -EINVAL for invalid values
134 int i2c_set_bus_speed(struct udevice *bus, unsigned int speed);
137 * i2c_get_bus_speed() - get the speed of a bus
140 * @return speed of selected I2C bus in Hz, -ve on error
142 int i2c_get_bus_speed(struct udevice *bus);
145 * i2c_set_chip_flags() - set flags for a chip
147 * Typically addresses are 7 bits, but for 10-bit addresses you should set
148 * flags to DM_I2C_CHIP_10BIT. All accesses will then use 10-bit addressing.
150 * @dev: Chip to adjust
152 * @return 0 if OK, -EINVAL if value is unsupported, other -ve value on error
154 int i2c_set_chip_flags(struct udevice *dev, uint flags);
157 * i2c_get_chip_flags() - get flags for a chip
159 * @dev: Chip to check
160 * @flagsp: Place to put flags
161 * @return 0 if OK, other -ve value on error
163 int i2c_get_chip_flags(struct udevice *dev, uint *flagsp);
166 * i2c_set_offset_len() - set the offset length for a chip
168 * The offset used to access a chip may be up to 4 bytes long. Typically it
169 * is only 1 byte, which is enough for chips with 256 bytes of memory or
170 * registers. The default value is 1, but you can call this function to
173 * @offset_len: New offset length value (typically 1 or 2)
176 int i2c_set_chip_offset_len(struct udevice *dev, uint offset_len);
178 * i2c_deblock() - recover a bus that is in an unknown state
180 * See the deblock() method in 'struct dm_i2c_ops' for full information
182 * @bus: Bus to recover
183 * @return 0 if OK, -ve on error
185 int i2c_deblock(struct udevice *bus);
188 * Not all of these flags are implemented in the U-Boot API
190 enum dm_i2c_msg_flags {
191 I2C_M_TEN = 0x0010, /* ten-bit chip address */
192 I2C_M_RD = 0x0001, /* read data, from slave to master */
193 I2C_M_STOP = 0x8000, /* send stop after this message */
194 I2C_M_NOSTART = 0x4000, /* no start before this message */
195 I2C_M_REV_DIR_ADDR = 0x2000, /* invert polarity of R/W bit */
196 I2C_M_IGNORE_NAK = 0x1000, /* continue after NAK */
197 I2C_M_NO_RD_ACK = 0x0800, /* skip the Ack bit on reads */
198 I2C_M_RECV_LEN = 0x0400, /* length is first received byte */
202 * struct i2c_msg - an I2C message
204 * @addr: Slave address
205 * @flags: Flags (see enum dm_i2c_msg_flags)
206 * @len: Length of buffer in bytes, may be 0 for a probe
207 * @buf: Buffer to send/receive, or NULL if no data
217 * struct i2c_msg_list - a list of I2C messages
219 * This is called i2c_rdwr_ioctl_data in Linux but the name does not seem
220 * appropriate in U-Boot.
222 * @msg: Pointer to i2c_msg array
223 * @nmsgs: Number of elements in the array
225 struct i2c_msg_list {
226 struct i2c_msg *msgs;
231 * struct dm_i2c_ops - driver operations for I2C uclass
233 * Drivers should support these operations unless otherwise noted. These
234 * operations are intended to be used by uclass code, not directly from
239 * xfer() - transfer a list of I2C messages
241 * @bus: Bus to read from
242 * @msg: List of messages to transfer
243 * @nmsgs: Number of messages in the list
244 * @return 0 if OK, -EREMOTEIO if the slave did not ACK a byte,
245 * -ECOMM if the speed cannot be supported, -EPROTO if the chip
246 * flags cannot be supported, other -ve value on some other error
248 int (*xfer)(struct udevice *bus, struct i2c_msg *msg, int nmsgs);
251 * probe_chip() - probe for the presense of a chip address
253 * This function is optional. If omitted, the uclass will send a zero
254 * length message instead.
257 * @chip_addr: Chip address to probe
258 * @chip_flags: Probe flags (enum dm_i2c_chip_flags)
259 * @return 0 if chip was found, -EREMOTEIO if not, -ENOSYS to fall back
260 * to default probem other -ve value on error
262 int (*probe_chip)(struct udevice *bus, uint chip_addr, uint chip_flags);
265 * set_bus_speed() - set the speed of a bus (optional)
267 * The bus speed value will be updated by the uclass if this function
268 * does not return an error. This method is optional - if it is not
269 * provided then the driver can read the speed from
270 * bus->uclass_priv->speed_hz
272 * @bus: Bus to adjust
273 * @speed: Requested speed in Hz
274 * @return 0 if OK, -EINVAL for invalid values
276 int (*set_bus_speed)(struct udevice *bus, unsigned int speed);
279 * get_bus_speed() - get the speed of a bus (optional)
281 * Normally this can be provided by the uclass, but if you want your
282 * driver to check the bus speed by looking at the hardware, you can
283 * implement that here. This method is optional. This method would
284 * normally be expected to return bus->uclass_priv->speed_hz.
287 * @return speed of selected I2C bus in Hz, -ve on error
289 int (*get_bus_speed)(struct udevice *bus);
292 * set_flags() - set the flags for a chip (optional)
294 * This is generally implemented by the uclass, but drivers can
295 * check the value to ensure that unsupported options are not used.
296 * This method is optional. If provided, this method will always be
297 * called when the flags change.
299 * @dev: Chip to adjust
300 * @flags: New flags value
301 * @return 0 if OK, -EINVAL if value is unsupported
303 int (*set_flags)(struct udevice *dev, uint flags);
306 * deblock() - recover a bus that is in an unknown state
308 * I2C is a synchronous protocol and resets of the processor in the
309 * middle of an access can block the I2C Bus until a powerdown of
310 * the full unit is done. This is because slaves can be stuck
311 * waiting for addition bus transitions for a transaction that will
312 * never complete. Resetting the I2C master does not help. The only
313 * way is to force the bus through a series of transitions to make
314 * sure that all slaves are done with the transaction. This method
315 * performs this 'deblocking' if support by the driver.
317 * This method is optional.
319 int (*deblock)(struct udevice *bus);
322 #define i2c_get_ops(dev) ((struct dm_i2c_ops *)(dev)->driver->ops)
325 * i2c_get_chip() - get a device to use to access a chip on a bus
327 * This returns the device for the given chip address. The device can then
328 * be used with calls to i2c_read(), i2c_write(), i2c_probe(), etc.
330 * @bus: Bus to examine
331 * @chip_addr: Chip address for the new device
332 * @devp: Returns pointer to new device if found or -ENODEV if not
335 int i2c_get_chip(struct udevice *bus, uint chip_addr, struct udevice **devp);
338 * i2c_get_chip() - get a device to use to access a chip on a bus number
340 * This returns the device for the given chip address on a particular bus
343 * @busnum: Bus number to examine
344 * @chip_addr: Chip address for the new device
345 * @devp: Returns pointer to new device if found or -ENODEV if not
348 int i2c_get_chip_for_busnum(int busnum, int chip_addr, struct udevice **devp);
351 * i2c_chip_ofdata_to_platdata() - Decode standard I2C platform data
353 * This decodes the chip address from a device tree node and puts it into
354 * its dm_i2c_chip structure. This should be called in your driver's
355 * ofdata_to_platdata() method.
357 * @blob: Device tree blob
358 * @node: Node offset to read from
359 * @spi: Place to put the decoded information
361 int i2c_chip_ofdata_to_platdata(const void *blob, int node,
362 struct dm_i2c_chip *chip);
366 #ifndef CONFIG_DM_I2C
369 * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
371 * The implementation MUST NOT use static or global variables if the
372 * I2C routines are used to read SDRAM configuration information
373 * because this is done before the memories are initialized. Limited
374 * use of stack-based variables are OK (the initial stack size is
377 * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
381 * Configuration items.
383 #define I2C_RXTX_LEN 128 /* maximum tx/rx buffer length */
385 #if !defined(CONFIG_SYS_I2C_MAX_HOPS)
386 /* no muxes used bus = i2c adapters */
387 #define CONFIG_SYS_I2C_DIRECT_BUS 1
388 #define CONFIG_SYS_I2C_MAX_HOPS 0
389 #define CONFIG_SYS_NUM_I2C_BUSES ll_entry_count(struct i2c_adapter, i2c)
391 /* we use i2c muxes */
392 #undef CONFIG_SYS_I2C_DIRECT_BUS
395 /* define the I2C bus number for RTC and DTT if not already done */
396 #if !defined(CONFIG_SYS_RTC_BUS_NUM)
397 #define CONFIG_SYS_RTC_BUS_NUM 0
399 #if !defined(CONFIG_SYS_DTT_BUS_NUM)
400 #define CONFIG_SYS_DTT_BUS_NUM 0
402 #if !defined(CONFIG_SYS_SPD_BUS_NUM)
403 #define CONFIG_SYS_SPD_BUS_NUM 0
407 void (*init)(struct i2c_adapter *adap, int speed,
409 int (*probe)(struct i2c_adapter *adap, uint8_t chip);
410 int (*read)(struct i2c_adapter *adap, uint8_t chip,
411 uint addr, int alen, uint8_t *buffer,
413 int (*write)(struct i2c_adapter *adap, uint8_t chip,
414 uint addr, int alen, uint8_t *buffer,
416 uint (*set_bus_speed)(struct i2c_adapter *adap,
426 #define U_BOOT_I2C_MKENT_COMPLETE(_init, _probe, _read, _write, \
427 _set_speed, _speed, _slaveaddr, _hwadapnr, _name) \
433 .set_bus_speed = _set_speed, \
435 .slaveaddr = _slaveaddr, \
437 .hwadapnr = _hwadapnr, \
441 #define U_BOOT_I2C_ADAP_COMPLETE(_name, _init, _probe, _read, _write, \
442 _set_speed, _speed, _slaveaddr, _hwadapnr) \
443 ll_entry_declare(struct i2c_adapter, _name, i2c) = \
444 U_BOOT_I2C_MKENT_COMPLETE(_init, _probe, _read, _write, \
445 _set_speed, _speed, _slaveaddr, _hwadapnr, _name);
447 struct i2c_adapter *i2c_get_adapter(int index);
449 #ifndef CONFIG_SYS_I2C_DIRECT_BUS
455 struct i2c_next_hop {
461 struct i2c_bus_hose {
463 struct i2c_next_hop next_hop[CONFIG_SYS_I2C_MAX_HOPS];
465 #define I2C_NULL_HOP {{-1, ""}, 0, 0}
466 extern struct i2c_bus_hose i2c_bus[];
468 #define I2C_ADAPTER(bus) i2c_bus[bus].adapter
470 #define I2C_ADAPTER(bus) bus
472 #define I2C_BUS gd->cur_i2c_bus
474 #define I2C_ADAP_NR(bus) i2c_get_adapter(I2C_ADAPTER(bus))
475 #define I2C_ADAP I2C_ADAP_NR(gd->cur_i2c_bus)
476 #define I2C_ADAP_HWNR (I2C_ADAP->hwadapnr)
478 #ifndef CONFIG_SYS_I2C_DIRECT_BUS
479 #define I2C_MUX_PCA9540_ID 1
480 #define I2C_MUX_PCA9540 {I2C_MUX_PCA9540_ID, "PCA9540B"}
481 #define I2C_MUX_PCA9542_ID 2
482 #define I2C_MUX_PCA9542 {I2C_MUX_PCA9542_ID, "PCA9542A"}
483 #define I2C_MUX_PCA9544_ID 3
484 #define I2C_MUX_PCA9544 {I2C_MUX_PCA9544_ID, "PCA9544A"}
485 #define I2C_MUX_PCA9547_ID 4
486 #define I2C_MUX_PCA9547 {I2C_MUX_PCA9547_ID, "PCA9547A"}
487 #define I2C_MUX_PCA9548_ID 5
488 #define I2C_MUX_PCA9548 {I2C_MUX_PCA9548_ID, "PCA9548"}
491 #ifndef I2C_SOFT_DECLARATIONS
492 # if defined(CONFIG_MPC8260)
493 # define I2C_SOFT_DECLARATIONS volatile ioport_t *iop = ioport_addr((immap_t *)CONFIG_SYS_IMMR, I2C_PORT);
494 # elif defined(CONFIG_8xx)
495 # define I2C_SOFT_DECLARATIONS volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
497 # elif (defined(CONFIG_AT91RM9200) || \
498 defined(CONFIG_AT91SAM9260) || defined(CONFIG_AT91SAM9261) || \
499 defined(CONFIG_AT91SAM9263))
500 # define I2C_SOFT_DECLARATIONS at91_pio_t *pio = (at91_pio_t *) ATMEL_BASE_PIOA;
502 # define I2C_SOFT_DECLARATIONS
507 /* Set default value for the I2C bus speed on 8xx. In the
508 * future, we'll define these in all 8xx board config files.
510 #ifndef CONFIG_SYS_I2C_SPEED
511 #define CONFIG_SYS_I2C_SPEED 50000
516 * Many boards/controllers/drivers don't support an I2C slave interface so
517 * provide a default slave address for them for use in common code. A real
518 * value for CONFIG_SYS_I2C_SLAVE should be defined for any board which does
519 * support a slave interface.
521 #ifndef CONFIG_SYS_I2C_SLAVE
522 #define CONFIG_SYS_I2C_SLAVE 0xfe
526 * Initialization, must be called once on start up, may be called
527 * repeatedly to change the speed and slave addresses.
529 void i2c_init(int speed, int slaveaddr);
530 void i2c_init_board(void);
531 #ifdef CONFIG_SYS_I2C_BOARD_LATE_INIT
532 void i2c_board_late_init(void);
535 #ifdef CONFIG_SYS_I2C
539 * Returns index of currently active I2C bus. Zero-based.
541 unsigned int i2c_get_bus_num(void);
546 * Change the active I2C bus. Subsequent read/write calls will
549 * bus - bus index, zero based
551 * Returns: 0 on success, not 0 on failure
554 int i2c_set_bus_num(unsigned int bus);
559 * Initializes all I2C adapters in the system. All i2c_adap structures must
560 * be initialized beforehead with function pointers and data, including
561 * speed and slaveaddr. Returns 0 on success, non-0 on failure.
563 void i2c_init_all(void);
566 * Probe the given I2C chip address. Returns 0 if a chip responded,
569 int i2c_probe(uint8_t chip);
572 * Read/Write interface:
573 * chip: I2C chip address, range 0..127
574 * addr: Memory (register) address within the chip
575 * alen: Number of bytes to use for addr (typically 1, 2 for larger
576 * memories, 0 for register type devices with only one
578 * buffer: Where to read/write the data
579 * len: How many bytes to read/write
581 * Returns: 0 on success, not 0 on failure
583 int i2c_read(uint8_t chip, unsigned int addr, int alen,
584 uint8_t *buffer, int len);
586 int i2c_write(uint8_t chip, unsigned int addr, int alen,
587 uint8_t *buffer, int len);
590 * Utility routines to read/write registers.
592 uint8_t i2c_reg_read(uint8_t addr, uint8_t reg);
594 void i2c_reg_write(uint8_t addr, uint8_t reg, uint8_t val);
599 * Change the speed of the active I2C bus
601 * speed - bus speed in Hz
603 * Returns: new bus speed
606 unsigned int i2c_set_bus_speed(unsigned int speed);
611 * Returns speed of currently active I2C bus in Hz
614 unsigned int i2c_get_bus_speed(void);
619 * Adjusts I2C pointers after U-Boot is relocated to DRAM
621 void i2c_reloc_fixup(void);
622 #if defined(CONFIG_SYS_I2C_SOFT)
623 void i2c_soft_init(void);
624 void i2c_soft_active(void);
625 void i2c_soft_tristate(void);
626 int i2c_soft_read(void);
627 void i2c_soft_sda(int bit);
628 void i2c_soft_scl(int bit);
629 void i2c_soft_delay(void);
634 * Probe the given I2C chip address. Returns 0 if a chip responded,
637 int i2c_probe(uchar chip);
640 * Read/Write interface:
641 * chip: I2C chip address, range 0..127
642 * addr: Memory (register) address within the chip
643 * alen: Number of bytes to use for addr (typically 1, 2 for larger
644 * memories, 0 for register type devices with only one
646 * buffer: Where to read/write the data
647 * len: How many bytes to read/write
649 * Returns: 0 on success, not 0 on failure
651 int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len);
652 int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len);
655 * Utility routines to read/write registers.
657 static inline u8 i2c_reg_read(u8 addr, u8 reg)
662 /* MPC8xx needs this. Maybe one day we can get rid of it. */
663 i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
667 printf("%s: addr=0x%02x, reg=0x%02x\n", __func__, addr, reg);
670 i2c_read(addr, reg, 1, &buf, 1);
675 static inline void i2c_reg_write(u8 addr, u8 reg, u8 val)
678 /* MPC8xx needs this. Maybe one day we can get rid of it. */
679 i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
683 printf("%s: addr=0x%02x, reg=0x%02x, val=0x%02x\n",
684 __func__, addr, reg, val);
687 i2c_write(addr, reg, 1, &val, 1);
691 * Functions for setting the current I2C bus and its speed
697 * Change the active I2C bus. Subsequent read/write calls will
700 * bus - bus index, zero based
702 * Returns: 0 on success, not 0 on failure
705 int i2c_set_bus_num(unsigned int bus);
710 * Returns index of currently active I2C bus. Zero-based.
713 unsigned int i2c_get_bus_num(void);
718 * Change the speed of the active I2C bus
720 * speed - bus speed in Hz
722 * Returns: 0 on success, not 0 on failure
725 int i2c_set_bus_speed(unsigned int);
730 * Returns speed of currently active I2C bus in Hz
733 unsigned int i2c_get_bus_speed(void);
734 #endif /* CONFIG_SYS_I2C */
737 * only for backwardcompatibility, should go away if we switched
738 * completely to new multibus support.
740 #if defined(CONFIG_SYS_I2C) || defined(CONFIG_I2C_MULTI_BUS)
741 # if !defined(CONFIG_SYS_MAX_I2C_BUS)
742 # define CONFIG_SYS_MAX_I2C_BUS 2
744 # define I2C_MULTI_BUS 1
746 # define CONFIG_SYS_MAX_I2C_BUS 1
747 # define I2C_MULTI_BUS 0
750 /* NOTE: These two functions MUST be always_inline to avoid code growth! */
751 static inline unsigned int I2C_GET_BUS(void) __attribute__((always_inline));
752 static inline unsigned int I2C_GET_BUS(void)
754 return I2C_MULTI_BUS ? i2c_get_bus_num() : 0;
757 static inline void I2C_SET_BUS(unsigned int bus) __attribute__((always_inline));
758 static inline void I2C_SET_BUS(unsigned int bus)
761 i2c_set_bus_num(bus);
764 /* Multi I2C definitions */
766 I2C_0, I2C_1, I2C_2, I2C_3, I2C_4, I2C_5, I2C_6, I2C_7,
767 I2C_8, I2C_9, I2C_10,
770 /* Multi I2C busses handling */
771 #ifdef CONFIG_SOFT_I2C_MULTI_BUS
772 extern int get_multi_scl_pin(void);
773 extern int get_multi_sda_pin(void);
774 extern int multi_i2c_init(void);
778 * Get FDT values for i2c bus.
780 * @param blob Device tree blbo
781 * @return the number of I2C bus
783 void board_i2c_init(const void *blob);
786 * Find the I2C bus number by given a FDT I2C node.
788 * @param blob Device tree blbo
789 * @param node FDT I2C node to find
790 * @return the number of I2C bus (zero based), or -1 on error
792 int i2c_get_bus_num_fdt(int node);
795 * Reset the I2C bus represented by the given a FDT I2C node.
797 * @param blob Device tree blbo
798 * @param node FDT I2C node to find
799 * @return 0 if port was reset, -1 if not found
801 int i2c_reset_port_fdt(const void *blob, int node);
803 #endif /* !CONFIG_DM_I2C */