2 * Copyright (C) 2009 Sergey Kubushyn <ksi@koi8.net>
5 * Heiko Schocher, DENX Software Engineering, hs@denx.de.
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->init;
58 addr += gd->reloc_off;
59 i2c_adap_p->init = (void (*)(int, int))addr;
61 addr = (unsigned long)i2c_adap_p->probe;
62 addr += gd->reloc_off;
63 i2c_adap_p->probe = (int (*)(uint8_t))addr;
65 addr = (unsigned long)i2c_adap_p->read;
66 addr += gd->reloc_off;
67 i2c_adap_p->read = (int (*)(uint8_t, uint, int, uint8_t *,
70 addr = (unsigned long)i2c_adap_p->write;
71 addr += gd->reloc_off;
72 i2c_adap_p->write = (int (*)(uint8_t, uint, int, uint8_t *,
74 /* i2c_set_bus_speed() */
75 addr = (unsigned long)i2c_adap_p->set_bus_speed;
76 addr += gd->reloc_off;
77 i2c_adap_p->set_bus_speed = (uint (*)(uint))addr;
79 addr = (unsigned long)i2c_adap_p->name;
80 addr += gd->reloc_off;
81 i2c_adap_p->name = (char *)addr;
88 #ifndef CONFIG_SYS_I2C_DIRECT_BUS
93 * This turns on the given channel on I2C multiplexer chip connected to
94 * a given I2C adapter directly or via other multiplexers. In the latter
95 * case the entire multiplexer chain must be initialized first starting
96 * with the one connected directly to the adapter. When disabling a chain
97 * muxes must be programmed in reverse order, starting with the one
98 * farthest from the adapter.
100 * mux_id is the multiplexer chip type from defined in i2c.h. So far only
101 * NXP (Philips) PCA954x multiplexers are supported. Switches are NOT
102 * supported (anybody uses them?)
105 static int i2c_mux_set(struct i2c_adapter *adap, int mux_id, int chip,
111 /* channel < 0 - turn off the mux */
114 ret = adap->write(adap, chip, 0, 0, &buf, 1);
116 printf("%s: Could not turn off the mux.\n", __func__);
121 case I2C_MUX_PCA9540_ID:
122 case I2C_MUX_PCA9542_ID:
125 buf = (uint8_t)((channel & 0x01) | (1 << 2));
127 case I2C_MUX_PCA9544_ID:
130 buf = (uint8_t)((channel & 0x03) | (1 << 2));
132 case I2C_MUX_PCA9547_ID:
135 buf = (uint8_t)((channel & 0x07) | (1 << 3));
137 case I2C_MUX_PCA9548_ID:
140 buf = (uint8_t)(0x01 << channel);
143 printf("%s: wrong mux id: %d\n", __func__, mux_id);
147 ret = adap->write(adap, chip, 0, 0, &buf, 1);
149 printf("%s: could not set mux: id: %d chip: %x channel: %d\n",
150 __func__, mux_id, chip, channel);
154 static int i2c_mux_set_all(void)
156 struct i2c_bus_hose *i2c_bus_tmp = &i2c_bus[I2C_BUS];
159 /* Connect requested bus if behind muxes */
160 if (i2c_bus_tmp->next_hop[0].chip != 0) {
161 /* Set all muxes along the path to that bus */
162 for (i = 0; i < CONFIG_SYS_I2C_MAX_HOPS; i++) {
165 if (i2c_bus_tmp->next_hop[i].chip == 0)
168 ret = i2c_mux_set(I2C_ADAP,
169 i2c_bus_tmp->next_hop[i].mux.id,
170 i2c_bus_tmp->next_hop[i].chip,
171 i2c_bus_tmp->next_hop[i].channel);
179 static int i2c_mux_disconnet_all(void)
181 struct i2c_bus_hose *i2c_bus_tmp = &i2c_bus[I2C_BUS];
185 if (I2C_ADAP->init_done == 0)
188 /* Disconnect current bus (turn off muxes if any) */
189 if ((i2c_bus_tmp->next_hop[0].chip != 0) &&
190 (I2C_ADAP->init_done != 0)) {
191 i = CONFIG_SYS_I2C_MAX_HOPS;
196 chip = i2c_bus_tmp->next_hop[--i].chip;
200 ret = I2C_ADAP->write(I2C_ADAP, chip, 0, 0, &buf, 1);
202 printf("i2c: mux diconnect error\n");
216 * Initializes one bus. Will initialize the parent adapter. No current bus
217 * changes, no mux (if any) setup.
219 static void i2c_init_bus(unsigned int bus_no, int speed, int slaveaddr)
221 if (bus_no >= CONFIG_SYS_NUM_I2C_BUSES)
224 I2C_ADAP->init(I2C_ADAP, speed, slaveaddr);
226 if (gd->flags & GD_FLG_RELOC) {
227 I2C_ADAP->init_done = 1;
228 I2C_ADAP->speed = speed;
229 I2C_ADAP->slaveaddr = slaveaddr;
233 /* implement possible board specific board init */
234 static void __def_i2c_init_board(void)
237 void i2c_init_board(void)
238 __attribute__((weak, alias("__def_i2c_init_board")));
243 * not longer needed, will deleted. Actual init the SPD_BUS
245 * i2c_adap[] must be initialized beforehead with function pointers and
246 * data, including speed and slaveaddr.
248 void i2c_init_all(void)
251 i2c_set_bus_num(CONFIG_SYS_SPD_BUS_NUM);
259 * Returns index of currently active I2C bus. Zero-based.
261 unsigned int i2c_get_bus_num(void)
263 return gd->cur_i2c_bus;
270 * Change the active I2C bus. Subsequent read/write calls will
271 * go to this one. Sets all of the muxes in a proper condition
272 * if that bus is behind muxes.
273 * If previously selected bus is behind the muxes turns off all the
274 * muxes along the path to that bus.
276 * bus - bus index, zero based
278 * Returns: 0 on success, not 0 on failure
280 int i2c_set_bus_num(unsigned int bus)
284 if ((bus == I2C_BUS) && (I2C_ADAP->init_done > 0))
287 #ifndef CONFIG_SYS_I2C_DIRECT_BUS
288 if (bus >= CONFIG_SYS_NUM_I2C_BUSES)
292 max = ll_entry_count(struct i2c_adapter, i2c);
293 if (I2C_ADAPTER(bus) >= max) {
294 printf("Error, wrong i2c adapter %d max %d possible\n",
295 I2C_ADAPTER(bus), max);
299 #ifndef CONFIG_SYS_I2C_DIRECT_BUS
300 i2c_mux_disconnet_all();
303 gd->cur_i2c_bus = bus;
304 if (I2C_ADAP->init_done == 0)
305 i2c_init_bus(bus, I2C_ADAP->speed, I2C_ADAP->slaveaddr);
307 #ifndef CONFIG_SYS_I2C_DIRECT_BUS
314 * Probe the given I2C chip address. Returns 0 if a chip responded,
317 int i2c_probe(uint8_t chip)
319 return I2C_ADAP->probe(I2C_ADAP, chip);
323 * Read/Write interface:
324 * chip: I2C chip address, range 0..127
325 * addr: Memory (register) address within the chip
326 * alen: Number of bytes to use for addr (typically 1, 2 for larger
327 * memories, 0 for register type devices with only one
329 * buffer: Where to read/write the data
330 * len: How many bytes to read/write
332 * Returns: 0 on success, not 0 on failure
334 int i2c_read(uint8_t chip, unsigned int addr, int alen,
335 uint8_t *buffer, int len)
337 return I2C_ADAP->read(I2C_ADAP, chip, addr, alen, buffer, len);
340 int i2c_write(uint8_t chip, unsigned int addr, int alen,
341 uint8_t *buffer, int len)
343 return I2C_ADAP->write(I2C_ADAP, chip, addr, alen, buffer, len);
346 unsigned int i2c_set_bus_speed(unsigned int speed)
350 if (I2C_ADAP->set_bus_speed == NULL)
352 ret = I2C_ADAP->set_bus_speed(I2C_ADAP, speed);
353 if (gd->flags & GD_FLG_RELOC)
354 I2C_ADAP->speed = ret;
359 unsigned int i2c_get_bus_speed(void)
361 struct i2c_adapter *cur = I2C_ADAP;
365 uint8_t i2c_reg_read(uint8_t addr, uint8_t reg)
370 /* MPC8xx needs this. Maybe one day we can get rid of it. */
371 /* maybe it is now the time for it ... */
372 i2c_set_bus_num(i2c_get_bus_num());
374 i2c_read(addr, reg, 1, &buf, 1);
377 printf("%s: bus=%d addr=0x%02x, reg=0x%02x, val=0x%02x\n",
378 __func__, i2c_get_bus_num(), addr, reg, buf);
384 void i2c_reg_write(uint8_t addr, uint8_t reg, uint8_t val)
387 /* MPC8xx needs this. Maybe one day we can get rid of it. */
388 /* maybe it is now the time for it ... */
389 i2c_set_bus_num(i2c_get_bus_num());
393 printf("%s: bus=%d addr=0x%02x, reg=0x%02x, val=0x%02x\n",
394 __func__, i2c_get_bus_num(), addr, reg, val);
397 i2c_write(addr, reg, 1, &val, 1);
400 void __i2c_init(int speed, int slaveaddr)
402 i2c_init_bus(i2c_get_bus_num(), speed, slaveaddr);
404 void i2c_init(int speed, int slaveaddr)
405 __attribute__((weak, alias("__i2c_init")));