]>
git.ipfire.org Git - people/ms/u-boot.git/blob - drivers/i2c/i2c_core.c
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 #ifndef CONFIG_SYS_I2C_DIRECT_BUS
47 * This turns on the given channel on I2C multiplexer chip connected to
48 * a given I2C adapter directly or via other multiplexers. In the latter
49 * case the entire multiplexer chain must be initialized first starting
50 * with the one connected directly to the adapter. When disabling a chain
51 * muxes must be programmed in reverse order, starting with the one
52 * farthest from the adapter.
54 * mux_id is the multiplexer chip type from defined in i2c.h. So far only
55 * NXP (Philips) PCA954x multiplexers are supported. Switches are NOT
56 * supported (anybody uses them?)
59 static int i2c_mux_set(struct i2c_adapter
*adap
, int mux_id
, int chip
,
65 /* channel < 0 - turn off the mux */
68 ret
= adap
->write(adap
, chip
, 0, 0, &buf
, 1);
70 printf("%s: Could not turn off the mux.\n", __func__
);
75 case I2C_MUX_PCA9540_ID
:
76 case I2C_MUX_PCA9542_ID
:
79 buf
= (uint8_t)((channel
& 0x01) | (1 << 2));
81 case I2C_MUX_PCA9544_ID
:
84 buf
= (uint8_t)((channel
& 0x03) | (1 << 2));
86 case I2C_MUX_PCA9547_ID
:
89 buf
= (uint8_t)((channel
& 0x07) | (1 << 3));
91 case I2C_MUX_PCA9548_ID
:
94 buf
= (uint8_t)(0x01 << channel
);
97 printf("%s: wrong mux id: %d\n", __func__
, mux_id
);
101 ret
= adap
->write(adap
, chip
, 0, 0, &buf
, 1);
103 printf("%s: could not set mux: id: %d chip: %x channel: %d\n",
104 __func__
, mux_id
, chip
, channel
);
108 static int i2c_mux_set_all(void)
110 struct i2c_bus_hose
*i2c_bus_tmp
= &i2c_bus
[I2C_BUS
];
113 /* Connect requested bus if behind muxes */
114 if (i2c_bus_tmp
->next_hop
[0].chip
!= 0) {
115 /* Set all muxes along the path to that bus */
116 for (i
= 0; i
< CONFIG_SYS_I2C_MAX_HOPS
; i
++) {
119 if (i2c_bus_tmp
->next_hop
[i
].chip
== 0)
122 ret
= i2c_mux_set(I2C_ADAP
,
123 i2c_bus_tmp
->next_hop
[i
].mux
.id
,
124 i2c_bus_tmp
->next_hop
[i
].chip
,
125 i2c_bus_tmp
->next_hop
[i
].channel
);
133 static int i2c_mux_disconnect_all(void)
135 struct i2c_bus_hose
*i2c_bus_tmp
= &i2c_bus
[I2C_BUS
];
139 if (I2C_ADAP
->init_done
== 0)
142 /* Disconnect current bus (turn off muxes if any) */
143 if ((i2c_bus_tmp
->next_hop
[0].chip
!= 0) &&
144 (I2C_ADAP
->init_done
!= 0)) {
145 i
= CONFIG_SYS_I2C_MAX_HOPS
;
150 chip
= i2c_bus_tmp
->next_hop
[--i
].chip
;
154 ret
= I2C_ADAP
->write(I2C_ADAP
, chip
, 0, 0, &buf
, 1);
156 printf("i2c: mux disconnect error\n");
170 * Initializes one bus. Will initialize the parent adapter. No current bus
171 * changes, no mux (if any) setup.
173 static void i2c_init_bus(unsigned int bus_no
, int speed
, int slaveaddr
)
175 if (bus_no
>= CONFIG_SYS_NUM_I2C_BUSES
)
178 I2C_ADAP
->init(I2C_ADAP
, speed
, slaveaddr
);
180 if (gd
->flags
& GD_FLG_RELOC
) {
181 I2C_ADAP
->init_done
= 1;
182 I2C_ADAP
->speed
= speed
;
183 I2C_ADAP
->slaveaddr
= slaveaddr
;
187 /* implement possible board specific board init */
188 __weak
void i2c_init_board(void)
192 /* implement possible for i2c specific early i2c init */
193 __weak
void i2c_early_init_f(void)
200 * not longer needed, will deleted. Actual init the SPD_BUS
202 * i2c_adap[] must be initialized beforehead with function pointers and
203 * data, including speed and slaveaddr.
205 void i2c_init_all(void)
208 i2c_set_bus_num(CONFIG_SYS_SPD_BUS_NUM
);
216 * Returns index of currently active I2C bus. Zero-based.
218 unsigned int i2c_get_bus_num(void)
220 return gd
->cur_i2c_bus
;
227 * Change the active I2C bus. Subsequent read/write calls will
228 * go to this one. Sets all of the muxes in a proper condition
229 * if that bus is behind muxes.
230 * If previously selected bus is behind the muxes turns off all the
231 * muxes along the path to that bus.
233 * bus - bus index, zero based
235 * Returns: 0 on success, not 0 on failure
237 int i2c_set_bus_num(unsigned int bus
)
241 if ((bus
== I2C_BUS
) && (I2C_ADAP
->init_done
> 0))
244 #ifndef CONFIG_SYS_I2C_DIRECT_BUS
245 if (bus
>= CONFIG_SYS_NUM_I2C_BUSES
)
249 max
= ll_entry_count(struct i2c_adapter
, i2c
);
250 if (I2C_ADAPTER(bus
) >= max
) {
251 printf("Error, wrong i2c adapter %d max %d possible\n",
252 I2C_ADAPTER(bus
), max
);
256 #ifndef CONFIG_SYS_I2C_DIRECT_BUS
257 i2c_mux_disconnect_all();
260 gd
->cur_i2c_bus
= bus
;
261 if (I2C_ADAP
->init_done
== 0)
262 i2c_init_bus(bus
, I2C_ADAP
->speed
, I2C_ADAP
->slaveaddr
);
264 #ifndef CONFIG_SYS_I2C_DIRECT_BUS
271 * Probe the given I2C chip address. Returns 0 if a chip responded,
274 int i2c_probe(uint8_t chip
)
276 return I2C_ADAP
->probe(I2C_ADAP
, chip
);
280 * Read/Write interface:
281 * chip: I2C chip address, range 0..127
282 * addr: Memory (register) address within the chip
283 * alen: Number of bytes to use for addr (typically 1, 2 for larger
284 * memories, 0 for register type devices with only one
286 * buffer: Where to read/write the data
287 * len: How many bytes to read/write
289 * Returns: 0 on success, not 0 on failure
291 int i2c_read(uint8_t chip
, unsigned int addr
, int alen
,
292 uint8_t *buffer
, int len
)
294 return I2C_ADAP
->read(I2C_ADAP
, chip
, addr
, alen
, buffer
, len
);
297 int i2c_write(uint8_t chip
, unsigned int addr
, int alen
,
298 uint8_t *buffer
, int len
)
300 return I2C_ADAP
->write(I2C_ADAP
, chip
, addr
, alen
, buffer
, len
);
303 unsigned int i2c_set_bus_speed(unsigned int speed
)
307 if (I2C_ADAP
->set_bus_speed
== NULL
)
309 ret
= I2C_ADAP
->set_bus_speed(I2C_ADAP
, speed
);
310 if (gd
->flags
& GD_FLG_RELOC
)
311 I2C_ADAP
->speed
= (ret
== 0) ? speed
: 0;
316 unsigned int i2c_get_bus_speed(void)
318 struct i2c_adapter
*cur
= I2C_ADAP
;
322 uint8_t i2c_reg_read(uint8_t addr
, uint8_t reg
)
326 i2c_read(addr
, reg
, 1, &buf
, 1);
329 printf("%s: bus=%d addr=0x%02x, reg=0x%02x, val=0x%02x\n",
330 __func__
, i2c_get_bus_num(), addr
, reg
, buf
);
336 void i2c_reg_write(uint8_t addr
, uint8_t reg
, uint8_t val
)
339 printf("%s: bus=%d addr=0x%02x, reg=0x%02x, val=0x%02x\n",
340 __func__
, i2c_get_bus_num(), addr
, reg
, val
);
343 i2c_write(addr
, reg
, 1, &val
, 1);
346 __weak
void i2c_init(int speed
, int slaveaddr
)
348 i2c_init_bus(i2c_get_bus_num(), speed
, slaveaddr
);