7 * Multibus/multiadapter I2C core functions (wrappers)
9 * SPDX-License-Identifier: GPL-2.0+
14 struct i2c_adapter *i2c_get_adapter(int index)
16 struct i2c_adapter *i2c_adap_p = ll_entry_start(struct i2c_adapter,
18 int max = ll_entry_count(struct i2c_adapter, i2c);
22 printf("Error, wrong i2c adapter %d max %d possible\n",
29 for (i = 0; i < index; i++)
35 #if !defined(CONFIG_SYS_I2C_DIRECT_BUS)
36 struct i2c_bus_hose i2c_bus[CONFIG_SYS_NUM_I2C_BUSES] =
40 DECLARE_GLOBAL_DATA_PTR;
42 void i2c_reloc_fixup(void)
44 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
45 struct i2c_adapter *i2c_adap_p = ll_entry_start(struct i2c_adapter,
47 struct i2c_adapter *tmp = i2c_adap_p;
48 int max = ll_entry_count(struct i2c_adapter, i2c);
52 if (gd->reloc_off == 0)
55 for (i = 0; i < max; i++) {
57 addr = (unsigned long)i2c_adap_p;
58 addr += gd->reloc_off;
59 i2c_adap_p = (struct i2c_adapter *)addr;
61 addr = (unsigned long)i2c_adap_p->init;
62 addr += gd->reloc_off;
63 i2c_adap_p->init = (void (*)(int, int))addr;
65 addr = (unsigned long)i2c_adap_p->probe;
66 addr += gd->reloc_off;
67 i2c_adap_p->probe = (int (*)(uint8_t))addr;
69 addr = (unsigned long)i2c_adap_p->read;
70 addr += gd->reloc_off;
71 i2c_adap_p->read = (int (*)(uint8_t, uint, int, uint8_t *,
74 addr = (unsigned long)i2c_adap_p->write;
75 addr += gd->reloc_off;
76 i2c_adap_p->write = (int (*)(uint8_t, uint, int, uint8_t *,
78 /* i2c_set_bus_speed() */
79 addr = (unsigned long)i2c_adap_p->set_bus_speed;
80 addr += gd->reloc_off;
81 i2c_adap_p->set_bus_speed = (uint (*)(uint))addr;
83 addr = (unsigned long)i2c_adap_p->name;
84 addr += gd->reloc_off;
85 i2c_adap_p->name = (char *)addr;
92 #ifndef CONFIG_SYS_I2C_DIRECT_BUS
97 * This turns on the given channel on I2C multiplexer chip connected to
98 * a given I2C adapter directly or via other multiplexers. In the latter
99 * case the entire multiplexer chain must be initialized first starting
100 * with the one connected directly to the adapter. When disabling a chain
101 * muxes must be programmed in reverse order, starting with the one
102 * farthest from the adapter.
104 * mux_id is the multiplexer chip type from defined in i2c.h. So far only
105 * NXP (Philips) PCA954x multiplexers are supported. Switches are NOT
106 * supported (anybody uses them?)
109 static int i2c_mux_set(struct i2c_adapter *adap, int mux_id, int chip,
115 /* channel < 0 - turn off the mux */
118 ret = adap->write(adap, chip, 0, 0, &buf, 1);
120 printf("%s: Could not turn off the mux.\n", __func__);
125 case I2C_MUX_PCA9540_ID:
126 case I2C_MUX_PCA9542_ID:
129 buf = (uint8_t)((channel & 0x01) | (1 << 2));
131 case I2C_MUX_PCA9544_ID:
134 buf = (uint8_t)((channel & 0x03) | (1 << 2));
136 case I2C_MUX_PCA9547_ID:
139 buf = (uint8_t)((channel & 0x07) | (1 << 3));
142 printf("%s: wrong mux id: %d\n", __func__, mux_id);
146 ret = adap->write(adap, chip, 0, 0, &buf, 1);
148 printf("%s: could not set mux: id: %d chip: %x channel: %d\n",
149 __func__, mux_id, chip, channel);
153 static int i2c_mux_set_all(void)
155 struct i2c_bus_hose *i2c_bus_tmp = &i2c_bus[I2C_BUS];
158 /* Connect requested bus if behind muxes */
159 if (i2c_bus_tmp->next_hop[0].chip != 0) {
160 /* Set all muxes along the path to that bus */
161 for (i = 0; i < CONFIG_SYS_I2C_MAX_HOPS; i++) {
164 if (i2c_bus_tmp->next_hop[i].chip == 0)
167 ret = i2c_mux_set(I2C_ADAP,
168 i2c_bus_tmp->next_hop[i].mux.id,
169 i2c_bus_tmp->next_hop[i].chip,
170 i2c_bus_tmp->next_hop[i].channel);
178 static int i2c_mux_disconnet_all(void)
180 struct i2c_bus_hose *i2c_bus_tmp = &i2c_bus[I2C_BUS];
184 if (I2C_ADAP->init_done == 0)
187 /* Disconnect current bus (turn off muxes if any) */
188 if ((i2c_bus_tmp->next_hop[0].chip != 0) &&
189 (I2C_ADAP->init_done != 0)) {
190 i = CONFIG_SYS_I2C_MAX_HOPS;
195 chip = i2c_bus_tmp->next_hop[--i].chip;
199 ret = I2C_ADAP->write(I2C_ADAP, chip, 0, 0, &buf, 1);
201 printf("i2c: mux diconnect error\n");
215 * Initializes one bus. Will initialize the parent adapter. No current bus
216 * changes, no mux (if any) setup.
218 static void i2c_init_bus(unsigned int bus_no, int speed, int slaveaddr)
220 if (bus_no >= CONFIG_SYS_NUM_I2C_BUSES)
223 I2C_ADAP->init(I2C_ADAP, speed, slaveaddr);
225 if (gd->flags & GD_FLG_RELOC) {
226 I2C_ADAP->init_done = 1;
227 I2C_ADAP->speed = speed;
228 I2C_ADAP->slaveaddr = slaveaddr;
232 /* implement possible board specific board init */
233 static void __def_i2c_init_board(void)
236 void i2c_init_board(void)
237 __attribute__((weak, alias("__def_i2c_init_board")));
242 * not longer needed, will deleted. Actual init the SPD_BUS
244 * i2c_adap[] must be initialized beforehead with function pointers and
245 * data, including speed and slaveaddr.
247 void i2c_init_all(void)
250 i2c_set_bus_num(CONFIG_SYS_SPD_BUS_NUM);
258 * Returns index of currently active I2C bus. Zero-based.
260 unsigned int i2c_get_bus_num(void)
262 return gd->cur_i2c_bus;
269 * Change the active I2C bus. Subsequent read/write calls will
270 * go to this one. Sets all of the muxes in a proper condition
271 * if that bus is behind muxes.
272 * If previously selected bus is behind the muxes turns off all the
273 * muxes along the path to that bus.
275 * bus - bus index, zero based
277 * Returns: 0 on success, not 0 on failure
279 int i2c_set_bus_num(unsigned int bus)
281 int max = ll_entry_count(struct i2c_adapter, i2c);
283 if (I2C_ADAPTER(bus) >= max) {
284 printf("Error, wrong i2c adapter %d max %d possible\n",
285 I2C_ADAPTER(bus), max);
288 #ifndef CONFIG_SYS_I2C_DIRECT_BUS
289 if (bus >= CONFIG_SYS_NUM_I2C_BUSES)
293 if ((bus == I2C_BUS) && (I2C_ADAP->init_done > 0))
296 #ifndef CONFIG_SYS_I2C_DIRECT_BUS
297 i2c_mux_disconnet_all();
300 gd->cur_i2c_bus = bus;
301 if (I2C_ADAP->init_done == 0)
302 i2c_init_bus(bus, I2C_ADAP->speed, I2C_ADAP->slaveaddr);
304 #ifndef CONFIG_SYS_I2C_DIRECT_BUS
311 * Probe the given I2C chip address. Returns 0 if a chip responded,
314 int i2c_probe(uint8_t chip)
316 return I2C_ADAP->probe(I2C_ADAP, chip);
320 * Read/Write interface:
321 * chip: I2C chip address, range 0..127
322 * addr: Memory (register) address within the chip
323 * alen: Number of bytes to use for addr (typically 1, 2 for larger
324 * memories, 0 for register type devices with only one
326 * buffer: Where to read/write the data
327 * len: How many bytes to read/write
329 * Returns: 0 on success, not 0 on failure
331 int i2c_read(uint8_t chip, unsigned int addr, int alen,
332 uint8_t *buffer, int len)
334 return I2C_ADAP->read(I2C_ADAP, chip, addr, alen, buffer, len);
337 int i2c_write(uint8_t chip, unsigned int addr, int alen,
338 uint8_t *buffer, int len)
340 return I2C_ADAP->write(I2C_ADAP, chip, addr, alen, buffer, len);
343 unsigned int i2c_set_bus_speed(unsigned int speed)
347 if (I2C_ADAP->set_bus_speed == NULL)
349 ret = I2C_ADAP->set_bus_speed(I2C_ADAP, speed);
350 if (gd->flags & GD_FLG_RELOC)
351 I2C_ADAP->speed = ret;
356 unsigned int i2c_get_bus_speed(void)
358 struct i2c_adapter *cur = I2C_ADAP;
362 uint8_t i2c_reg_read(uint8_t addr, uint8_t reg)
367 /* MPC8xx needs this. Maybe one day we can get rid of it. */
368 /* maybe it is now the time for it ... */
369 i2c_set_bus_num(i2c_get_bus_num());
371 i2c_read(addr, reg, 1, &buf, 1);
374 printf("%s: bus=%d addr=0x%02x, reg=0x%02x, val=0x%02x\n",
375 __func__, i2c_get_bus_num(), addr, reg, buf);
381 void i2c_reg_write(uint8_t addr, uint8_t reg, uint8_t val)
384 /* MPC8xx needs this. Maybe one day we can get rid of it. */
385 /* maybe it is now the time for it ... */
386 i2c_set_bus_num(i2c_get_bus_num());
390 printf("%s: bus=%d addr=0x%02x, reg=0x%02x, val=0x%02x\n",
391 __func__, i2c_get_bus_num(), addr, reg, val);
394 i2c_write(addr, reg, 1, &val, 1);
397 void __i2c_init(int speed, int slaveaddr)
399 i2c_init_bus(i2c_get_bus_num(), speed, slaveaddr);
401 void i2c_init(int speed, int slaveaddr)
402 __attribute__((weak, alias("__i2c_init")));