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;
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));
141 case I2C_MUX_PCA9548_ID:
144 buf = (uint8_t)(0x01 << channel);
147 printf("%s: wrong mux id: %d\n", __func__, mux_id);
151 ret = adap->write(adap, chip, 0, 0, &buf, 1);
153 printf("%s: could not set mux: id: %d chip: %x channel: %d\n",
154 __func__, mux_id, chip, channel);
158 static int i2c_mux_set_all(void)
160 struct i2c_bus_hose *i2c_bus_tmp = &i2c_bus[I2C_BUS];
163 /* Connect requested bus if behind muxes */
164 if (i2c_bus_tmp->next_hop[0].chip != 0) {
165 /* Set all muxes along the path to that bus */
166 for (i = 0; i < CONFIG_SYS_I2C_MAX_HOPS; i++) {
169 if (i2c_bus_tmp->next_hop[i].chip == 0)
172 ret = i2c_mux_set(I2C_ADAP,
173 i2c_bus_tmp->next_hop[i].mux.id,
174 i2c_bus_tmp->next_hop[i].chip,
175 i2c_bus_tmp->next_hop[i].channel);
183 static int i2c_mux_disconnet_all(void)
185 struct i2c_bus_hose *i2c_bus_tmp = &i2c_bus[I2C_BUS];
189 if (I2C_ADAP->init_done == 0)
192 /* Disconnect current bus (turn off muxes if any) */
193 if ((i2c_bus_tmp->next_hop[0].chip != 0) &&
194 (I2C_ADAP->init_done != 0)) {
195 i = CONFIG_SYS_I2C_MAX_HOPS;
200 chip = i2c_bus_tmp->next_hop[--i].chip;
204 ret = I2C_ADAP->write(I2C_ADAP, chip, 0, 0, &buf, 1);
206 printf("i2c: mux diconnect error\n");
220 * Initializes one bus. Will initialize the parent adapter. No current bus
221 * changes, no mux (if any) setup.
223 static void i2c_init_bus(unsigned int bus_no, int speed, int slaveaddr)
225 if (bus_no >= CONFIG_SYS_NUM_I2C_BUSES)
228 I2C_ADAP->init(I2C_ADAP, speed, slaveaddr);
230 if (gd->flags & GD_FLG_RELOC) {
231 I2C_ADAP->init_done = 1;
232 I2C_ADAP->speed = speed;
233 I2C_ADAP->slaveaddr = slaveaddr;
237 /* implement possible board specific board init */
238 static void __def_i2c_init_board(void)
241 void i2c_init_board(void)
242 __attribute__((weak, alias("__def_i2c_init_board")));
247 * not longer needed, will deleted. Actual init the SPD_BUS
249 * i2c_adap[] must be initialized beforehead with function pointers and
250 * data, including speed and slaveaddr.
252 void i2c_init_all(void)
255 i2c_set_bus_num(CONFIG_SYS_SPD_BUS_NUM);
263 * Returns index of currently active I2C bus. Zero-based.
265 unsigned int i2c_get_bus_num(void)
267 return gd->cur_i2c_bus;
274 * Change the active I2C bus. Subsequent read/write calls will
275 * go to this one. Sets all of the muxes in a proper condition
276 * if that bus is behind muxes.
277 * If previously selected bus is behind the muxes turns off all the
278 * muxes along the path to that bus.
280 * bus - bus index, zero based
282 * Returns: 0 on success, not 0 on failure
284 int i2c_set_bus_num(unsigned int bus)
286 int max = ll_entry_count(struct i2c_adapter, i2c);
288 if (I2C_ADAPTER(bus) >= max) {
289 printf("Error, wrong i2c adapter %d max %d possible\n",
290 I2C_ADAPTER(bus), max);
293 #ifndef CONFIG_SYS_I2C_DIRECT_BUS
294 if (bus >= CONFIG_SYS_NUM_I2C_BUSES)
298 if ((bus == I2C_BUS) && (I2C_ADAP->init_done > 0))
301 #ifndef CONFIG_SYS_I2C_DIRECT_BUS
302 i2c_mux_disconnet_all();
305 gd->cur_i2c_bus = bus;
306 if (I2C_ADAP->init_done == 0)
307 i2c_init_bus(bus, I2C_ADAP->speed, I2C_ADAP->slaveaddr);
309 #ifndef CONFIG_SYS_I2C_DIRECT_BUS
316 * Probe the given I2C chip address. Returns 0 if a chip responded,
319 int i2c_probe(uint8_t chip)
321 return I2C_ADAP->probe(I2C_ADAP, chip);
325 * Read/Write interface:
326 * chip: I2C chip address, range 0..127
327 * addr: Memory (register) address within the chip
328 * alen: Number of bytes to use for addr (typically 1, 2 for larger
329 * memories, 0 for register type devices with only one
331 * buffer: Where to read/write the data
332 * len: How many bytes to read/write
334 * Returns: 0 on success, not 0 on failure
336 int i2c_read(uint8_t chip, unsigned int addr, int alen,
337 uint8_t *buffer, int len)
339 return I2C_ADAP->read(I2C_ADAP, chip, addr, alen, buffer, len);
342 int i2c_write(uint8_t chip, unsigned int addr, int alen,
343 uint8_t *buffer, int len)
345 return I2C_ADAP->write(I2C_ADAP, chip, addr, alen, buffer, len);
348 unsigned int i2c_set_bus_speed(unsigned int speed)
352 if (I2C_ADAP->set_bus_speed == NULL)
354 ret = I2C_ADAP->set_bus_speed(I2C_ADAP, speed);
355 if (gd->flags & GD_FLG_RELOC)
356 I2C_ADAP->speed = ret;
361 unsigned int i2c_get_bus_speed(void)
363 struct i2c_adapter *cur = I2C_ADAP;
367 uint8_t i2c_reg_read(uint8_t addr, uint8_t reg)
372 /* MPC8xx needs this. Maybe one day we can get rid of it. */
373 /* maybe it is now the time for it ... */
374 i2c_set_bus_num(i2c_get_bus_num());
376 i2c_read(addr, reg, 1, &buf, 1);
379 printf("%s: bus=%d addr=0x%02x, reg=0x%02x, val=0x%02x\n",
380 __func__, i2c_get_bus_num(), addr, reg, buf);
386 void i2c_reg_write(uint8_t addr, uint8_t reg, uint8_t val)
389 /* MPC8xx needs this. Maybe one day we can get rid of it. */
390 /* maybe it is now the time for it ... */
391 i2c_set_bus_num(i2c_get_bus_num());
395 printf("%s: bus=%d addr=0x%02x, reg=0x%02x, val=0x%02x\n",
396 __func__, i2c_get_bus_num(), addr, reg, val);
399 i2c_write(addr, reg, 1, &val, 1);
402 void __i2c_init(int speed, int slaveaddr)
404 i2c_init_bus(i2c_get_bus_num(), speed, slaveaddr);
406 void i2c_init(int speed, int slaveaddr)
407 __attribute__((weak, alias("__i2c_init")));