1 // SPDX-License-Identifier: GPL-2.0+
8 * Multibus/multiadapter I2C core functions (wrappers)
12 #include <linker_lists.h>
13 #include <asm/global_data.h>
15 struct i2c_adapter *i2c_get_adapter(int index)
17 struct i2c_adapter *i2c_adap_p = ll_entry_start(struct i2c_adapter,
19 int max = ll_entry_count(struct i2c_adapter, i2c);
23 printf("Error, wrong i2c adapter %d max %d possible\n",
30 for (i = 0; i < index; i++)
36 DECLARE_GLOBAL_DATA_PTR;
42 * Initializes one bus. Will initialize the parent adapter. No current bus
43 * changes, no mux (if any) setup.
45 static void i2c_init_bus(unsigned int bus_no, int speed, int slaveaddr)
47 if (bus_no >= CFG_SYS_NUM_I2C_BUSES)
50 I2C_ADAP->init(I2C_ADAP, speed, slaveaddr);
52 if (gd->flags & GD_FLG_RELOC) {
53 I2C_ADAP->init_done = 1;
54 I2C_ADAP->speed = speed;
55 I2C_ADAP->slaveaddr = slaveaddr;
59 /* implement possible board specific board init */
60 __weak void i2c_init_board(void)
67 * not longer needed, will deleted. Actual init the SPD_BUS
69 * i2c_adap[] must be initialized beforehead with function pointers and
70 * data, including speed and slaveaddr.
72 void i2c_init_all(void)
75 i2c_set_bus_num(CONFIG_SYS_SPD_BUS_NUM);
83 * Returns index of currently active I2C bus. Zero-based.
85 unsigned int i2c_get_bus_num(void)
87 return gd->cur_i2c_bus;
94 * Change the active I2C bus. Subsequent read/write calls will
95 * go to this one. Sets all of the muxes in a proper condition
96 * if that bus is behind muxes.
97 * If previously selected bus is behind the muxes turns off all the
98 * muxes along the path to that bus.
100 * bus - bus index, zero based
102 * Returns: 0 on success, not 0 on failure
104 int i2c_set_bus_num(unsigned int bus)
108 if ((bus == I2C_BUS) && (I2C_ADAP->init_done > 0))
111 max = ll_entry_count(struct i2c_adapter, i2c);
112 if (I2C_ADAPTER(bus) >= max) {
113 printf("Error, wrong i2c adapter %d max %d possible\n",
114 I2C_ADAPTER(bus), max);
118 gd->cur_i2c_bus = bus;
119 if (I2C_ADAP->init_done == 0)
120 i2c_init_bus(bus, I2C_ADAP->speed, I2C_ADAP->slaveaddr);
126 * Probe the given I2C chip address. Returns 0 if a chip responded,
129 int i2c_probe(uint8_t chip)
131 return I2C_ADAP->probe(I2C_ADAP, chip);
135 * Read/Write interface:
136 * chip: I2C chip address, range 0..127
137 * addr: Memory (register) address within the chip
138 * alen: Number of bytes to use for addr (typically 1, 2 for larger
139 * memories, 0 for register type devices with only one
141 * buffer: Where to read/write the data
142 * len: How many bytes to read/write
144 * Returns: 0 on success, not 0 on failure
146 int i2c_read(uint8_t chip, unsigned int addr, int alen,
147 uint8_t *buffer, int len)
149 return I2C_ADAP->read(I2C_ADAP, chip, addr, alen, buffer, len);
152 int i2c_write(uint8_t chip, unsigned int addr, int alen,
153 uint8_t *buffer, int len)
155 return I2C_ADAP->write(I2C_ADAP, chip, addr, alen, buffer, len);
158 unsigned int i2c_set_bus_speed(unsigned int speed)
162 if (I2C_ADAP->set_bus_speed == NULL)
164 ret = I2C_ADAP->set_bus_speed(I2C_ADAP, speed);
165 if (gd->flags & GD_FLG_RELOC)
166 I2C_ADAP->speed = (ret == 0) ? speed : 0;
171 unsigned int i2c_get_bus_speed(void)
173 struct i2c_adapter *cur = I2C_ADAP;
177 uint8_t i2c_reg_read(uint8_t addr, uint8_t reg)
181 i2c_read(addr, reg, 1, &buf, 1);
184 printf("%s: bus=%d addr=0x%02x, reg=0x%02x, val=0x%02x\n",
185 __func__, i2c_get_bus_num(), addr, reg, buf);
191 void i2c_reg_write(uint8_t addr, uint8_t reg, uint8_t val)
194 printf("%s: bus=%d addr=0x%02x, reg=0x%02x, val=0x%02x\n",
195 __func__, i2c_get_bus_num(), addr, reg, val);
198 i2c_write(addr, reg, 1, &val, 1);
201 __weak void i2c_init(int speed, int slaveaddr)
203 i2c_init_bus(i2c_get_bus_num(), speed, slaveaddr);