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 *)addr
;
61 addr
= (unsigned long)i2c_adap_p
->probe
;
62 addr
+= gd
->reloc_off
;
63 i2c_adap_p
->probe
= (void *)addr
;
65 addr
= (unsigned long)i2c_adap_p
->read
;
66 addr
+= gd
->reloc_off
;
67 i2c_adap_p
->read
= (void *)addr
;
69 addr
= (unsigned long)i2c_adap_p
->write
;
70 addr
+= gd
->reloc_off
;
71 i2c_adap_p
->write
= (void *)addr
;
72 /* i2c_set_bus_speed() */
73 addr
= (unsigned long)i2c_adap_p
->set_bus_speed
;
74 addr
+= gd
->reloc_off
;
75 i2c_adap_p
->set_bus_speed
= (void *)addr
;
77 addr
= (unsigned long)i2c_adap_p
->name
;
78 addr
+= gd
->reloc_off
;
79 i2c_adap_p
->name
= (char *)addr
;
86 #ifndef CONFIG_SYS_I2C_DIRECT_BUS
91 * This turns on the given channel on I2C multiplexer chip connected to
92 * a given I2C adapter directly or via other multiplexers. In the latter
93 * case the entire multiplexer chain must be initialized first starting
94 * with the one connected directly to the adapter. When disabling a chain
95 * muxes must be programmed in reverse order, starting with the one
96 * farthest from the adapter.
98 * mux_id is the multiplexer chip type from defined in i2c.h. So far only
99 * NXP (Philips) PCA954x multiplexers are supported. Switches are NOT
100 * supported (anybody uses them?)
103 static int i2c_mux_set(struct i2c_adapter
*adap
, int mux_id
, int chip
,
109 /* channel < 0 - turn off the mux */
112 ret
= adap
->write(adap
, chip
, 0, 0, &buf
, 1);
114 printf("%s: Could not turn off the mux.\n", __func__
);
119 case I2C_MUX_PCA9540_ID
:
120 case I2C_MUX_PCA9542_ID
:
123 buf
= (uint8_t)((channel
& 0x01) | (1 << 2));
125 case I2C_MUX_PCA9544_ID
:
128 buf
= (uint8_t)((channel
& 0x03) | (1 << 2));
130 case I2C_MUX_PCA9547_ID
:
133 buf
= (uint8_t)((channel
& 0x07) | (1 << 3));
135 case I2C_MUX_PCA9548_ID
:
138 buf
= (uint8_t)(0x01 << channel
);
141 printf("%s: wrong mux id: %d\n", __func__
, mux_id
);
145 ret
= adap
->write(adap
, chip
, 0, 0, &buf
, 1);
147 printf("%s: could not set mux: id: %d chip: %x channel: %d\n",
148 __func__
, mux_id
, chip
, channel
);
152 static int i2c_mux_set_all(void)
154 struct i2c_bus_hose
*i2c_bus_tmp
= &i2c_bus
[I2C_BUS
];
157 /* Connect requested bus if behind muxes */
158 if (i2c_bus_tmp
->next_hop
[0].chip
!= 0) {
159 /* Set all muxes along the path to that bus */
160 for (i
= 0; i
< CONFIG_SYS_I2C_MAX_HOPS
; i
++) {
163 if (i2c_bus_tmp
->next_hop
[i
].chip
== 0)
166 ret
= i2c_mux_set(I2C_ADAP
,
167 i2c_bus_tmp
->next_hop
[i
].mux
.id
,
168 i2c_bus_tmp
->next_hop
[i
].chip
,
169 i2c_bus_tmp
->next_hop
[i
].channel
);
177 static int i2c_mux_disconnet_all(void)
179 struct i2c_bus_hose
*i2c_bus_tmp
= &i2c_bus
[I2C_BUS
];
183 if (I2C_ADAP
->init_done
== 0)
186 /* Disconnect current bus (turn off muxes if any) */
187 if ((i2c_bus_tmp
->next_hop
[0].chip
!= 0) &&
188 (I2C_ADAP
->init_done
!= 0)) {
189 i
= CONFIG_SYS_I2C_MAX_HOPS
;
194 chip
= i2c_bus_tmp
->next_hop
[--i
].chip
;
198 ret
= I2C_ADAP
->write(I2C_ADAP
, chip
, 0, 0, &buf
, 1);
200 printf("i2c: mux diconnect error\n");
214 * Initializes one bus. Will initialize the parent adapter. No current bus
215 * changes, no mux (if any) setup.
217 static void i2c_init_bus(unsigned int bus_no
, int speed
, int slaveaddr
)
219 if (bus_no
>= CONFIG_SYS_NUM_I2C_BUSES
)
222 I2C_ADAP
->init(I2C_ADAP
, speed
, slaveaddr
);
224 if (gd
->flags
& GD_FLG_RELOC
) {
225 I2C_ADAP
->init_done
= 1;
226 I2C_ADAP
->speed
= speed
;
227 I2C_ADAP
->slaveaddr
= slaveaddr
;
231 /* implement possible board specific board init */
232 static void __def_i2c_init_board(void)
235 void i2c_init_board(void)
236 __attribute__((weak
, alias("__def_i2c_init_board")));
241 * not longer needed, will deleted. Actual init the SPD_BUS
243 * i2c_adap[] must be initialized beforehead with function pointers and
244 * data, including speed and slaveaddr.
246 void i2c_init_all(void)
249 i2c_set_bus_num(CONFIG_SYS_SPD_BUS_NUM
);
257 * Returns index of currently active I2C bus. Zero-based.
259 unsigned int i2c_get_bus_num(void)
261 return gd
->cur_i2c_bus
;
268 * Change the active I2C bus. Subsequent read/write calls will
269 * go to this one. Sets all of the muxes in a proper condition
270 * if that bus is behind muxes.
271 * If previously selected bus is behind the muxes turns off all the
272 * muxes along the path to that bus.
274 * bus - bus index, zero based
276 * Returns: 0 on success, not 0 on failure
278 int i2c_set_bus_num(unsigned int bus
)
282 if ((bus
== I2C_BUS
) && (I2C_ADAP
->init_done
> 0))
285 #ifndef CONFIG_SYS_I2C_DIRECT_BUS
286 if (bus
>= CONFIG_SYS_NUM_I2C_BUSES
)
290 max
= ll_entry_count(struct i2c_adapter
, i2c
);
291 if (I2C_ADAPTER(bus
) >= max
) {
292 printf("Error, wrong i2c adapter %d max %d possible\n",
293 I2C_ADAPTER(bus
), max
);
297 #ifndef CONFIG_SYS_I2C_DIRECT_BUS
298 i2c_mux_disconnet_all();
301 gd
->cur_i2c_bus
= bus
;
302 if (I2C_ADAP
->init_done
== 0)
303 i2c_init_bus(bus
, I2C_ADAP
->speed
, I2C_ADAP
->slaveaddr
);
305 #ifndef CONFIG_SYS_I2C_DIRECT_BUS
312 * Probe the given I2C chip address. Returns 0 if a chip responded,
315 int i2c_probe(uint8_t chip
)
317 return I2C_ADAP
->probe(I2C_ADAP
, chip
);
321 * Read/Write interface:
322 * chip: I2C chip address, range 0..127
323 * addr: Memory (register) address within the chip
324 * alen: Number of bytes to use for addr (typically 1, 2 for larger
325 * memories, 0 for register type devices with only one
327 * buffer: Where to read/write the data
328 * len: How many bytes to read/write
330 * Returns: 0 on success, not 0 on failure
332 int i2c_read(uint8_t chip
, unsigned int addr
, int alen
,
333 uint8_t *buffer
, int len
)
335 return I2C_ADAP
->read(I2C_ADAP
, chip
, addr
, alen
, buffer
, len
);
338 int i2c_write(uint8_t chip
, unsigned int addr
, int alen
,
339 uint8_t *buffer
, int len
)
341 return I2C_ADAP
->write(I2C_ADAP
, chip
, addr
, alen
, buffer
, len
);
344 unsigned int i2c_set_bus_speed(unsigned int speed
)
348 if (I2C_ADAP
->set_bus_speed
== NULL
)
350 ret
= I2C_ADAP
->set_bus_speed(I2C_ADAP
, speed
);
351 if (gd
->flags
& GD_FLG_RELOC
)
352 I2C_ADAP
->speed
= (ret
== 0) ? speed
: 0;
357 unsigned int i2c_get_bus_speed(void)
359 struct i2c_adapter
*cur
= I2C_ADAP
;
363 uint8_t i2c_reg_read(uint8_t addr
, uint8_t reg
)
368 /* MPC8xx needs this. Maybe one day we can get rid of it. */
369 /* maybe it is now the time for it ... */
370 i2c_set_bus_num(i2c_get_bus_num());
372 i2c_read(addr
, reg
, 1, &buf
, 1);
375 printf("%s: bus=%d addr=0x%02x, reg=0x%02x, val=0x%02x\n",
376 __func__
, i2c_get_bus_num(), addr
, reg
, buf
);
382 void i2c_reg_write(uint8_t addr
, uint8_t reg
, uint8_t val
)
385 /* MPC8xx needs this. Maybe one day we can get rid of it. */
386 /* maybe it is now the time for it ... */
387 i2c_set_bus_num(i2c_get_bus_num());
391 printf("%s: bus=%d addr=0x%02x, reg=0x%02x, val=0x%02x\n",
392 __func__
, i2c_get_bus_num(), addr
, reg
, val
);
395 i2c_write(addr
, reg
, 1, &val
, 1);
398 void __i2c_init(int speed
, int slaveaddr
)
400 i2c_init_bus(i2c_get_bus_num(), speed
, slaveaddr
);
402 void i2c_init(int speed
, int slaveaddr
)
403 __attribute__((weak
, alias("__i2c_init")));