1 // SPDX-License-Identifier: GPL-2.0+
8 * Multibus/multiadapter I2C core functions (wrappers)
13 struct i2c_adapter *i2c_get_adapter(int index)
15 struct i2c_adapter *i2c_adap_p = ll_entry_start(struct i2c_adapter,
17 int max = ll_entry_count(struct i2c_adapter, i2c);
21 printf("Error, wrong i2c adapter %d max %d possible\n",
28 for (i = 0; i < index; i++)
34 #if !defined(CONFIG_SYS_I2C_DIRECT_BUS)
35 struct i2c_bus_hose i2c_bus[CONFIG_SYS_NUM_I2C_BUSES] =
39 DECLARE_GLOBAL_DATA_PTR;
41 #ifndef CONFIG_SYS_I2C_DIRECT_BUS
46 * This turns on the given channel on I2C multiplexer chip connected to
47 * a given I2C adapter directly or via other multiplexers. In the latter
48 * case the entire multiplexer chain must be initialized first starting
49 * with the one connected directly to the adapter. When disabling a chain
50 * muxes must be programmed in reverse order, starting with the one
51 * farthest from the adapter.
53 * mux_id is the multiplexer chip type from defined in i2c.h. So far only
54 * NXP (Philips) PCA954x multiplexers are supported. Switches are NOT
55 * supported (anybody uses them?)
58 static int i2c_mux_set(struct i2c_adapter *adap, int mux_id, int chip,
64 /* channel < 0 - turn off the mux */
67 ret = adap->write(adap, chip, 0, 0, &buf, 1);
69 printf("%s: Could not turn off the mux.\n", __func__);
74 case I2C_MUX_PCA9540_ID:
75 case I2C_MUX_PCA9542_ID:
78 buf = (uint8_t)((channel & 0x01) | (1 << 2));
80 case I2C_MUX_PCA9544_ID:
83 buf = (uint8_t)((channel & 0x03) | (1 << 2));
85 case I2C_MUX_PCA9547_ID:
88 buf = (uint8_t)((channel & 0x07) | (1 << 3));
90 case I2C_MUX_PCA9548_ID:
93 buf = (uint8_t)(0x01 << channel);
96 printf("%s: wrong mux id: %d\n", __func__, mux_id);
100 ret = adap->write(adap, chip, 0, 0, &buf, 1);
102 printf("%s: could not set mux: id: %d chip: %x channel: %d\n",
103 __func__, mux_id, chip, channel);
107 static int i2c_mux_set_all(void)
109 struct i2c_bus_hose *i2c_bus_tmp = &i2c_bus[I2C_BUS];
112 /* Connect requested bus if behind muxes */
113 if (i2c_bus_tmp->next_hop[0].chip != 0) {
114 /* Set all muxes along the path to that bus */
115 for (i = 0; i < CONFIG_SYS_I2C_MAX_HOPS; i++) {
118 if (i2c_bus_tmp->next_hop[i].chip == 0)
121 ret = i2c_mux_set(I2C_ADAP,
122 i2c_bus_tmp->next_hop[i].mux.id,
123 i2c_bus_tmp->next_hop[i].chip,
124 i2c_bus_tmp->next_hop[i].channel);
132 static int i2c_mux_disconnect_all(void)
134 struct i2c_bus_hose *i2c_bus_tmp = &i2c_bus[I2C_BUS];
138 if (I2C_ADAP->init_done == 0)
141 /* Disconnect current bus (turn off muxes if any) */
142 if ((i2c_bus_tmp->next_hop[0].chip != 0) &&
143 (I2C_ADAP->init_done != 0)) {
144 i = CONFIG_SYS_I2C_MAX_HOPS;
149 chip = i2c_bus_tmp->next_hop[--i].chip;
153 ret = I2C_ADAP->write(I2C_ADAP, chip, 0, 0, &buf, 1);
155 printf("i2c: mux disconnect error\n");
169 * Initializes one bus. Will initialize the parent adapter. No current bus
170 * changes, no mux (if any) setup.
172 static void i2c_init_bus(unsigned int bus_no, int speed, int slaveaddr)
174 if (bus_no >= CONFIG_SYS_NUM_I2C_BUSES)
177 I2C_ADAP->init(I2C_ADAP, speed, slaveaddr);
179 if (gd->flags & GD_FLG_RELOC) {
180 I2C_ADAP->init_done = 1;
181 I2C_ADAP->speed = speed;
182 I2C_ADAP->slaveaddr = slaveaddr;
186 /* implement possible board specific board init */
187 __weak void i2c_init_board(void)
191 /* implement possible for i2c specific early i2c init */
192 __weak void i2c_early_init_f(void)
199 * not longer needed, will deleted. Actual init the SPD_BUS
201 * i2c_adap[] must be initialized beforehead with function pointers and
202 * data, including speed and slaveaddr.
204 void i2c_init_all(void)
207 i2c_set_bus_num(CONFIG_SYS_SPD_BUS_NUM);
215 * Returns index of currently active I2C bus. Zero-based.
217 unsigned int i2c_get_bus_num(void)
219 return gd->cur_i2c_bus;
226 * Change the active I2C bus. Subsequent read/write calls will
227 * go to this one. Sets all of the muxes in a proper condition
228 * if that bus is behind muxes.
229 * If previously selected bus is behind the muxes turns off all the
230 * muxes along the path to that bus.
232 * bus - bus index, zero based
234 * Returns: 0 on success, not 0 on failure
236 int i2c_set_bus_num(unsigned int bus)
240 if ((bus == I2C_BUS) && (I2C_ADAP->init_done > 0))
243 #ifndef CONFIG_SYS_I2C_DIRECT_BUS
244 if (bus >= CONFIG_SYS_NUM_I2C_BUSES)
248 max = ll_entry_count(struct i2c_adapter, i2c);
249 if (I2C_ADAPTER(bus) >= max) {
250 printf("Error, wrong i2c adapter %d max %d possible\n",
251 I2C_ADAPTER(bus), max);
255 #ifndef CONFIG_SYS_I2C_DIRECT_BUS
256 i2c_mux_disconnect_all();
259 gd->cur_i2c_bus = bus;
260 if (I2C_ADAP->init_done == 0)
261 i2c_init_bus(bus, I2C_ADAP->speed, I2C_ADAP->slaveaddr);
263 #ifndef CONFIG_SYS_I2C_DIRECT_BUS
270 * Probe the given I2C chip address. Returns 0 if a chip responded,
273 int i2c_probe(uint8_t chip)
275 return I2C_ADAP->probe(I2C_ADAP, chip);
279 * Read/Write interface:
280 * chip: I2C chip address, range 0..127
281 * addr: Memory (register) address within the chip
282 * alen: Number of bytes to use for addr (typically 1, 2 for larger
283 * memories, 0 for register type devices with only one
285 * buffer: Where to read/write the data
286 * len: How many bytes to read/write
288 * Returns: 0 on success, not 0 on failure
290 int i2c_read(uint8_t chip, unsigned int addr, int alen,
291 uint8_t *buffer, int len)
293 return I2C_ADAP->read(I2C_ADAP, chip, addr, alen, buffer, len);
296 int i2c_write(uint8_t chip, unsigned int addr, int alen,
297 uint8_t *buffer, int len)
299 return I2C_ADAP->write(I2C_ADAP, chip, addr, alen, buffer, len);
302 unsigned int i2c_set_bus_speed(unsigned int speed)
306 if (I2C_ADAP->set_bus_speed == NULL)
308 ret = I2C_ADAP->set_bus_speed(I2C_ADAP, speed);
309 if (gd->flags & GD_FLG_RELOC)
310 I2C_ADAP->speed = (ret == 0) ? speed : 0;
315 unsigned int i2c_get_bus_speed(void)
317 struct i2c_adapter *cur = I2C_ADAP;
321 uint8_t i2c_reg_read(uint8_t addr, uint8_t reg)
325 i2c_read(addr, reg, 1, &buf, 1);
328 printf("%s: bus=%d addr=0x%02x, reg=0x%02x, val=0x%02x\n",
329 __func__, i2c_get_bus_num(), addr, reg, buf);
335 void i2c_reg_write(uint8_t addr, uint8_t reg, uint8_t val)
338 printf("%s: bus=%d addr=0x%02x, reg=0x%02x, val=0x%02x\n",
339 __func__, i2c_get_bus_num(), addr, reg, val);
342 i2c_write(addr, reg, 1, &val, 1);
345 __weak void i2c_init(int speed, int slaveaddr)
347 i2c_init_bus(i2c_get_bus_num(), speed, slaveaddr);