1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2009 Sergey Kubushyn <ksi@koi8.net>
6 * Heiko Schocher, DENX Software Engineering, hs@denx.de.
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 #if !defined(CONFIG_SYS_I2C_DIRECT_BUS)
37 struct i2c_bus_hose i2c_bus[CONFIG_SYS_NUM_I2C_BUSES] =
41 DECLARE_GLOBAL_DATA_PTR;
43 #ifndef CONFIG_SYS_I2C_DIRECT_BUS
48 * This turns on the given channel on I2C multiplexer chip connected to
49 * a given I2C adapter directly or via other multiplexers. In the latter
50 * case the entire multiplexer chain must be initialized first starting
51 * with the one connected directly to the adapter. When disabling a chain
52 * muxes must be programmed in reverse order, starting with the one
53 * farthest from the adapter.
55 * mux_id is the multiplexer chip type from defined in i2c.h. So far only
56 * NXP (Philips) PCA954x multiplexers are supported. Switches are NOT
57 * supported (anybody uses them?)
60 static int i2c_mux_set(struct i2c_adapter *adap, int mux_id, int chip,
66 /* channel < 0 - turn off the mux */
69 ret = adap->write(adap, chip, 0, 0, &buf, 1);
71 printf("%s: Could not turn off the mux.\n", __func__);
76 case I2C_MUX_PCA9540_ID:
77 case I2C_MUX_PCA9542_ID:
80 buf = (uint8_t)((channel & 0x01) | (1 << 2));
82 case I2C_MUX_PCA9544_ID:
85 buf = (uint8_t)((channel & 0x03) | (1 << 2));
87 case I2C_MUX_PCA9547_ID:
90 buf = (uint8_t)((channel & 0x07) | (1 << 3));
92 case I2C_MUX_PCA9548_ID:
95 buf = (uint8_t)(0x01 << channel);
98 printf("%s: wrong mux id: %d\n", __func__, mux_id);
102 ret = adap->write(adap, chip, 0, 0, &buf, 1);
104 printf("%s: could not set mux: id: %d chip: %x channel: %d\n",
105 __func__, mux_id, chip, channel);
109 static int i2c_mux_set_all(void)
111 struct i2c_bus_hose *i2c_bus_tmp = &i2c_bus[I2C_BUS];
114 /* Connect requested bus if behind muxes */
115 if (i2c_bus_tmp->next_hop[0].chip != 0) {
116 /* Set all muxes along the path to that bus */
117 for (i = 0; i < CONFIG_SYS_I2C_MAX_HOPS; i++) {
120 if (i2c_bus_tmp->next_hop[i].chip == 0)
123 ret = i2c_mux_set(I2C_ADAP,
124 i2c_bus_tmp->next_hop[i].mux.id,
125 i2c_bus_tmp->next_hop[i].chip,
126 i2c_bus_tmp->next_hop[i].channel);
134 static int i2c_mux_disconnect_all(void)
136 struct i2c_bus_hose *i2c_bus_tmp = &i2c_bus[I2C_BUS];
140 if (I2C_ADAP->init_done == 0)
143 /* Disconnect current bus (turn off muxes if any) */
144 if ((i2c_bus_tmp->next_hop[0].chip != 0) &&
145 (I2C_ADAP->init_done != 0)) {
146 i = CONFIG_SYS_I2C_MAX_HOPS;
151 chip = i2c_bus_tmp->next_hop[--i].chip;
155 ret = I2C_ADAP->write(I2C_ADAP, chip, 0, 0, &buf, 1);
157 printf("i2c: mux disconnect error\n");
171 * Initializes one bus. Will initialize the parent adapter. No current bus
172 * changes, no mux (if any) setup.
174 static void i2c_init_bus(unsigned int bus_no, int speed, int slaveaddr)
176 if (bus_no >= CONFIG_SYS_NUM_I2C_BUSES)
179 I2C_ADAP->init(I2C_ADAP, speed, slaveaddr);
181 if (gd->flags & GD_FLG_RELOC) {
182 I2C_ADAP->init_done = 1;
183 I2C_ADAP->speed = speed;
184 I2C_ADAP->slaveaddr = slaveaddr;
188 /* implement possible board specific board init */
189 __weak void i2c_init_board(void)
193 /* implement possible for i2c specific early i2c init */
194 __weak void i2c_early_init_f(void)
201 * not longer needed, will deleted. Actual init the SPD_BUS
203 * i2c_adap[] must be initialized beforehead with function pointers and
204 * data, including speed and slaveaddr.
206 void i2c_init_all(void)
209 i2c_set_bus_num(CONFIG_SYS_SPD_BUS_NUM);
217 * Returns index of currently active I2C bus. Zero-based.
219 unsigned int i2c_get_bus_num(void)
221 return gd->cur_i2c_bus;
228 * Change the active I2C bus. Subsequent read/write calls will
229 * go to this one. Sets all of the muxes in a proper condition
230 * if that bus is behind muxes.
231 * If previously selected bus is behind the muxes turns off all the
232 * muxes along the path to that bus.
234 * bus - bus index, zero based
236 * Returns: 0 on success, not 0 on failure
238 int i2c_set_bus_num(unsigned int bus)
242 if ((bus == I2C_BUS) && (I2C_ADAP->init_done > 0))
245 #ifndef CONFIG_SYS_I2C_DIRECT_BUS
246 if (bus >= CONFIG_SYS_NUM_I2C_BUSES)
250 max = ll_entry_count(struct i2c_adapter, i2c);
251 if (I2C_ADAPTER(bus) >= max) {
252 printf("Error, wrong i2c adapter %d max %d possible\n",
253 I2C_ADAPTER(bus), max);
257 #ifndef CONFIG_SYS_I2C_DIRECT_BUS
258 i2c_mux_disconnect_all();
261 gd->cur_i2c_bus = bus;
262 if (I2C_ADAP->init_done == 0)
263 i2c_init_bus(bus, I2C_ADAP->speed, I2C_ADAP->slaveaddr);
265 #ifndef CONFIG_SYS_I2C_DIRECT_BUS
272 * Probe the given I2C chip address. Returns 0 if a chip responded,
275 int i2c_probe(uint8_t chip)
277 return I2C_ADAP->probe(I2C_ADAP, chip);
281 * Read/Write interface:
282 * chip: I2C chip address, range 0..127
283 * addr: Memory (register) address within the chip
284 * alen: Number of bytes to use for addr (typically 1, 2 for larger
285 * memories, 0 for register type devices with only one
287 * buffer: Where to read/write the data
288 * len: How many bytes to read/write
290 * Returns: 0 on success, not 0 on failure
292 int i2c_read(uint8_t chip, unsigned int addr, int alen,
293 uint8_t *buffer, int len)
295 return I2C_ADAP->read(I2C_ADAP, chip, addr, alen, buffer, len);
298 int i2c_write(uint8_t chip, unsigned int addr, int alen,
299 uint8_t *buffer, int len)
301 return I2C_ADAP->write(I2C_ADAP, chip, addr, alen, buffer, len);
304 unsigned int i2c_set_bus_speed(unsigned int speed)
308 if (I2C_ADAP->set_bus_speed == NULL)
310 ret = I2C_ADAP->set_bus_speed(I2C_ADAP, speed);
311 if (gd->flags & GD_FLG_RELOC)
312 I2C_ADAP->speed = (ret == 0) ? speed : 0;
317 unsigned int i2c_get_bus_speed(void)
319 struct i2c_adapter *cur = I2C_ADAP;
323 uint8_t i2c_reg_read(uint8_t addr, uint8_t reg)
327 i2c_read(addr, reg, 1, &buf, 1);
330 printf("%s: bus=%d addr=0x%02x, reg=0x%02x, val=0x%02x\n",
331 __func__, i2c_get_bus_num(), addr, reg, buf);
337 void i2c_reg_write(uint8_t addr, uint8_t reg, uint8_t val)
340 printf("%s: bus=%d addr=0x%02x, reg=0x%02x, val=0x%02x\n",
341 __func__, i2c_get_bus_num(), addr, reg, val);
344 i2c_write(addr, reg, 1, &val, 1);
347 __weak void i2c_init(int speed, int slaveaddr)
349 i2c_init_bus(i2c_get_bus_num(), speed, slaveaddr);