1385c9ef5SHeiko Schocher /*
2385c9ef5SHeiko Schocher * Copyright (C) 2009 Sergey Kubushyn <ksi@koi8.net>
3385c9ef5SHeiko Schocher *
4385c9ef5SHeiko Schocher * (C) Copyright 2012
5385c9ef5SHeiko Schocher * Heiko Schocher, DENX Software Engineering, hs@denx.de.
6385c9ef5SHeiko Schocher *
7385c9ef5SHeiko Schocher * Multibus/multiadapter I2C core functions (wrappers)
8385c9ef5SHeiko Schocher *
98dde4ca9STom Rini * SPDX-License-Identifier: GPL-2.0+
10385c9ef5SHeiko Schocher */
11385c9ef5SHeiko Schocher #include <common.h>
12385c9ef5SHeiko Schocher #include <i2c.h>
13385c9ef5SHeiko Schocher
i2c_get_adapter(int index)14385c9ef5SHeiko Schocher struct i2c_adapter *i2c_get_adapter(int index)
15385c9ef5SHeiko Schocher {
16385c9ef5SHeiko Schocher struct i2c_adapter *i2c_adap_p = ll_entry_start(struct i2c_adapter,
17385c9ef5SHeiko Schocher i2c);
18385c9ef5SHeiko Schocher int max = ll_entry_count(struct i2c_adapter, i2c);
19385c9ef5SHeiko Schocher int i;
20385c9ef5SHeiko Schocher
21385c9ef5SHeiko Schocher if (index >= max) {
22385c9ef5SHeiko Schocher printf("Error, wrong i2c adapter %d max %d possible\n",
23385c9ef5SHeiko Schocher index, max);
24385c9ef5SHeiko Schocher return i2c_adap_p;
25385c9ef5SHeiko Schocher }
26385c9ef5SHeiko Schocher if (index == 0)
27385c9ef5SHeiko Schocher return i2c_adap_p;
28385c9ef5SHeiko Schocher
29385c9ef5SHeiko Schocher for (i = 0; i < index; i++)
30385c9ef5SHeiko Schocher i2c_adap_p++;
31385c9ef5SHeiko Schocher
32385c9ef5SHeiko Schocher return i2c_adap_p;
33385c9ef5SHeiko Schocher }
34385c9ef5SHeiko Schocher
35385c9ef5SHeiko Schocher #if !defined(CONFIG_SYS_I2C_DIRECT_BUS)
36385c9ef5SHeiko Schocher struct i2c_bus_hose i2c_bus[CONFIG_SYS_NUM_I2C_BUSES] =
37385c9ef5SHeiko Schocher CONFIG_SYS_I2C_BUSES;
38385c9ef5SHeiko Schocher #endif
39385c9ef5SHeiko Schocher
40385c9ef5SHeiko Schocher DECLARE_GLOBAL_DATA_PTR;
41385c9ef5SHeiko Schocher
42385c9ef5SHeiko Schocher #ifndef CONFIG_SYS_I2C_DIRECT_BUS
43385c9ef5SHeiko Schocher /*
44385c9ef5SHeiko Schocher * i2c_mux_set()
45385c9ef5SHeiko Schocher * -------------
46385c9ef5SHeiko Schocher *
47385c9ef5SHeiko Schocher * This turns on the given channel on I2C multiplexer chip connected to
48385c9ef5SHeiko Schocher * a given I2C adapter directly or via other multiplexers. In the latter
49385c9ef5SHeiko Schocher * case the entire multiplexer chain must be initialized first starting
50385c9ef5SHeiko Schocher * with the one connected directly to the adapter. When disabling a chain
51385c9ef5SHeiko Schocher * muxes must be programmed in reverse order, starting with the one
52385c9ef5SHeiko Schocher * farthest from the adapter.
53385c9ef5SHeiko Schocher *
54385c9ef5SHeiko Schocher * mux_id is the multiplexer chip type from defined in i2c.h. So far only
55385c9ef5SHeiko Schocher * NXP (Philips) PCA954x multiplexers are supported. Switches are NOT
56385c9ef5SHeiko Schocher * supported (anybody uses them?)
57385c9ef5SHeiko Schocher */
58385c9ef5SHeiko Schocher
i2c_mux_set(struct i2c_adapter * adap,int mux_id,int chip,int channel)59385c9ef5SHeiko Schocher static int i2c_mux_set(struct i2c_adapter *adap, int mux_id, int chip,
60385c9ef5SHeiko Schocher int channel)
61385c9ef5SHeiko Schocher {
62385c9ef5SHeiko Schocher uint8_t buf;
63385c9ef5SHeiko Schocher int ret;
64385c9ef5SHeiko Schocher
65385c9ef5SHeiko Schocher /* channel < 0 - turn off the mux */
66385c9ef5SHeiko Schocher if (channel < 0) {
67385c9ef5SHeiko Schocher buf = 0;
68385c9ef5SHeiko Schocher ret = adap->write(adap, chip, 0, 0, &buf, 1);
69385c9ef5SHeiko Schocher if (ret)
70385c9ef5SHeiko Schocher printf("%s: Could not turn off the mux.\n", __func__);
71385c9ef5SHeiko Schocher return ret;
72385c9ef5SHeiko Schocher }
73385c9ef5SHeiko Schocher
74385c9ef5SHeiko Schocher switch (mux_id) {
75385c9ef5SHeiko Schocher case I2C_MUX_PCA9540_ID:
76385c9ef5SHeiko Schocher case I2C_MUX_PCA9542_ID:
77385c9ef5SHeiko Schocher if (channel > 1)
78385c9ef5SHeiko Schocher return -1;
79385c9ef5SHeiko Schocher buf = (uint8_t)((channel & 0x01) | (1 << 2));
80385c9ef5SHeiko Schocher break;
81385c9ef5SHeiko Schocher case I2C_MUX_PCA9544_ID:
82385c9ef5SHeiko Schocher if (channel > 3)
83385c9ef5SHeiko Schocher return -1;
84385c9ef5SHeiko Schocher buf = (uint8_t)((channel & 0x03) | (1 << 2));
85385c9ef5SHeiko Schocher break;
86385c9ef5SHeiko Schocher case I2C_MUX_PCA9547_ID:
87385c9ef5SHeiko Schocher if (channel > 7)
88385c9ef5SHeiko Schocher return -1;
89385c9ef5SHeiko Schocher buf = (uint8_t)((channel & 0x07) | (1 << 3));
90385c9ef5SHeiko Schocher break;
91e6658749SMichael Burr case I2C_MUX_PCA9548_ID:
92e6658749SMichael Burr if (channel > 7)
93e6658749SMichael Burr return -1;
94e6658749SMichael Burr buf = (uint8_t)(0x01 << channel);
95e6658749SMichael Burr break;
96385c9ef5SHeiko Schocher default:
97385c9ef5SHeiko Schocher printf("%s: wrong mux id: %d\n", __func__, mux_id);
98385c9ef5SHeiko Schocher return -1;
99385c9ef5SHeiko Schocher }
100385c9ef5SHeiko Schocher
101385c9ef5SHeiko Schocher ret = adap->write(adap, chip, 0, 0, &buf, 1);
102385c9ef5SHeiko Schocher if (ret)
103385c9ef5SHeiko Schocher printf("%s: could not set mux: id: %d chip: %x channel: %d\n",
104385c9ef5SHeiko Schocher __func__, mux_id, chip, channel);
105385c9ef5SHeiko Schocher return ret;
106385c9ef5SHeiko Schocher }
107385c9ef5SHeiko Schocher
i2c_mux_set_all(void)108385c9ef5SHeiko Schocher static int i2c_mux_set_all(void)
109385c9ef5SHeiko Schocher {
110385c9ef5SHeiko Schocher struct i2c_bus_hose *i2c_bus_tmp = &i2c_bus[I2C_BUS];
111385c9ef5SHeiko Schocher int i;
112385c9ef5SHeiko Schocher
113385c9ef5SHeiko Schocher /* Connect requested bus if behind muxes */
114385c9ef5SHeiko Schocher if (i2c_bus_tmp->next_hop[0].chip != 0) {
115385c9ef5SHeiko Schocher /* Set all muxes along the path to that bus */
116385c9ef5SHeiko Schocher for (i = 0; i < CONFIG_SYS_I2C_MAX_HOPS; i++) {
117385c9ef5SHeiko Schocher int ret;
118385c9ef5SHeiko Schocher
119385c9ef5SHeiko Schocher if (i2c_bus_tmp->next_hop[i].chip == 0)
120385c9ef5SHeiko Schocher break;
121385c9ef5SHeiko Schocher
122385c9ef5SHeiko Schocher ret = i2c_mux_set(I2C_ADAP,
123385c9ef5SHeiko Schocher i2c_bus_tmp->next_hop[i].mux.id,
124385c9ef5SHeiko Schocher i2c_bus_tmp->next_hop[i].chip,
125385c9ef5SHeiko Schocher i2c_bus_tmp->next_hop[i].channel);
126385c9ef5SHeiko Schocher if (ret != 0)
127385c9ef5SHeiko Schocher return ret;
128385c9ef5SHeiko Schocher }
129385c9ef5SHeiko Schocher }
130385c9ef5SHeiko Schocher return 0;
131385c9ef5SHeiko Schocher }
132385c9ef5SHeiko Schocher
i2c_mux_disconnect_all(void)133f4ed3696SMark Tomlinson static int i2c_mux_disconnect_all(void)
134385c9ef5SHeiko Schocher {
135385c9ef5SHeiko Schocher struct i2c_bus_hose *i2c_bus_tmp = &i2c_bus[I2C_BUS];
136385c9ef5SHeiko Schocher int i;
1372fe50ef4SMark Tomlinson uint8_t buf = 0;
138385c9ef5SHeiko Schocher
139385c9ef5SHeiko Schocher if (I2C_ADAP->init_done == 0)
140385c9ef5SHeiko Schocher return 0;
141385c9ef5SHeiko Schocher
142385c9ef5SHeiko Schocher /* Disconnect current bus (turn off muxes if any) */
143385c9ef5SHeiko Schocher if ((i2c_bus_tmp->next_hop[0].chip != 0) &&
144385c9ef5SHeiko Schocher (I2C_ADAP->init_done != 0)) {
145385c9ef5SHeiko Schocher i = CONFIG_SYS_I2C_MAX_HOPS;
146385c9ef5SHeiko Schocher do {
147385c9ef5SHeiko Schocher uint8_t chip;
148385c9ef5SHeiko Schocher int ret;
149385c9ef5SHeiko Schocher
150385c9ef5SHeiko Schocher chip = i2c_bus_tmp->next_hop[--i].chip;
151385c9ef5SHeiko Schocher if (chip == 0)
152385c9ef5SHeiko Schocher continue;
153385c9ef5SHeiko Schocher
154385c9ef5SHeiko Schocher ret = I2C_ADAP->write(I2C_ADAP, chip, 0, 0, &buf, 1);
155385c9ef5SHeiko Schocher if (ret != 0) {
156f4ed3696SMark Tomlinson printf("i2c: mux disconnect error\n");
157385c9ef5SHeiko Schocher return ret;
158385c9ef5SHeiko Schocher }
159385c9ef5SHeiko Schocher } while (i > 0);
160385c9ef5SHeiko Schocher }
161385c9ef5SHeiko Schocher
162385c9ef5SHeiko Schocher return 0;
163385c9ef5SHeiko Schocher }
164385c9ef5SHeiko Schocher #endif
165385c9ef5SHeiko Schocher
166385c9ef5SHeiko Schocher /*
167385c9ef5SHeiko Schocher * i2c_init_bus():
168385c9ef5SHeiko Schocher * ---------------
169385c9ef5SHeiko Schocher *
170385c9ef5SHeiko Schocher * Initializes one bus. Will initialize the parent adapter. No current bus
171385c9ef5SHeiko Schocher * changes, no mux (if any) setup.
172385c9ef5SHeiko Schocher */
i2c_init_bus(unsigned int bus_no,int speed,int slaveaddr)173385c9ef5SHeiko Schocher static void i2c_init_bus(unsigned int bus_no, int speed, int slaveaddr)
174385c9ef5SHeiko Schocher {
175385c9ef5SHeiko Schocher if (bus_no >= CONFIG_SYS_NUM_I2C_BUSES)
176385c9ef5SHeiko Schocher return;
177385c9ef5SHeiko Schocher
178385c9ef5SHeiko Schocher I2C_ADAP->init(I2C_ADAP, speed, slaveaddr);
179385c9ef5SHeiko Schocher
180385c9ef5SHeiko Schocher if (gd->flags & GD_FLG_RELOC) {
181385c9ef5SHeiko Schocher I2C_ADAP->init_done = 1;
182385c9ef5SHeiko Schocher I2C_ADAP->speed = speed;
183385c9ef5SHeiko Schocher I2C_ADAP->slaveaddr = slaveaddr;
184385c9ef5SHeiko Schocher }
185385c9ef5SHeiko Schocher }
186385c9ef5SHeiko Schocher
187385c9ef5SHeiko Schocher /* implement possible board specific board init */
i2c_init_board(void)18813a8b7aeSJeroen Hofstee __weak void i2c_init_board(void)
189385c9ef5SHeiko Schocher {
190385c9ef5SHeiko Schocher }
191385c9ef5SHeiko Schocher
192*9d10c2d3SYuan Yao /* implement possible for i2c specific early i2c init */
i2c_early_init_f(void)193*9d10c2d3SYuan Yao __weak void i2c_early_init_f(void)
194*9d10c2d3SYuan Yao {
195*9d10c2d3SYuan Yao }
196*9d10c2d3SYuan Yao
197385c9ef5SHeiko Schocher /*
198385c9ef5SHeiko Schocher * i2c_init_all():
199385c9ef5SHeiko Schocher *
200385c9ef5SHeiko Schocher * not longer needed, will deleted. Actual init the SPD_BUS
201385c9ef5SHeiko Schocher * for compatibility.
202385c9ef5SHeiko Schocher * i2c_adap[] must be initialized beforehead with function pointers and
203385c9ef5SHeiko Schocher * data, including speed and slaveaddr.
204385c9ef5SHeiko Schocher */
i2c_init_all(void)205385c9ef5SHeiko Schocher void i2c_init_all(void)
206385c9ef5SHeiko Schocher {
207385c9ef5SHeiko Schocher i2c_init_board();
208385c9ef5SHeiko Schocher i2c_set_bus_num(CONFIG_SYS_SPD_BUS_NUM);
209385c9ef5SHeiko Schocher return;
210385c9ef5SHeiko Schocher }
211385c9ef5SHeiko Schocher
212385c9ef5SHeiko Schocher /*
213385c9ef5SHeiko Schocher * i2c_get_bus_num():
214385c9ef5SHeiko Schocher * ------------------
215385c9ef5SHeiko Schocher *
216385c9ef5SHeiko Schocher * Returns index of currently active I2C bus. Zero-based.
217385c9ef5SHeiko Schocher */
i2c_get_bus_num(void)218385c9ef5SHeiko Schocher unsigned int i2c_get_bus_num(void)
219385c9ef5SHeiko Schocher {
220385c9ef5SHeiko Schocher return gd->cur_i2c_bus;
221385c9ef5SHeiko Schocher }
222385c9ef5SHeiko Schocher
223385c9ef5SHeiko Schocher /*
224385c9ef5SHeiko Schocher * i2c_set_bus_num():
225385c9ef5SHeiko Schocher * ------------------
226385c9ef5SHeiko Schocher *
227385c9ef5SHeiko Schocher * Change the active I2C bus. Subsequent read/write calls will
228385c9ef5SHeiko Schocher * go to this one. Sets all of the muxes in a proper condition
229385c9ef5SHeiko Schocher * if that bus is behind muxes.
230385c9ef5SHeiko Schocher * If previously selected bus is behind the muxes turns off all the
231385c9ef5SHeiko Schocher * muxes along the path to that bus.
232385c9ef5SHeiko Schocher *
233385c9ef5SHeiko Schocher * bus - bus index, zero based
234385c9ef5SHeiko Schocher *
235385c9ef5SHeiko Schocher * Returns: 0 on success, not 0 on failure
236385c9ef5SHeiko Schocher */
i2c_set_bus_num(unsigned int bus)237385c9ef5SHeiko Schocher int i2c_set_bus_num(unsigned int bus)
238385c9ef5SHeiko Schocher {
23913c2433cSHeiko Schocher int max;
240385c9ef5SHeiko Schocher
24113c2433cSHeiko Schocher if ((bus == I2C_BUS) && (I2C_ADAP->init_done > 0))
24213c2433cSHeiko Schocher return 0;
24313c2433cSHeiko Schocher
244385c9ef5SHeiko Schocher #ifndef CONFIG_SYS_I2C_DIRECT_BUS
245385c9ef5SHeiko Schocher if (bus >= CONFIG_SYS_NUM_I2C_BUSES)
246385c9ef5SHeiko Schocher return -1;
247385c9ef5SHeiko Schocher #endif
248385c9ef5SHeiko Schocher
24913c2433cSHeiko Schocher max = ll_entry_count(struct i2c_adapter, i2c);
25013c2433cSHeiko Schocher if (I2C_ADAPTER(bus) >= max) {
25113c2433cSHeiko Schocher printf("Error, wrong i2c adapter %d max %d possible\n",
25213c2433cSHeiko Schocher I2C_ADAPTER(bus), max);
25313c2433cSHeiko Schocher return -2;
25413c2433cSHeiko Schocher }
255385c9ef5SHeiko Schocher
256385c9ef5SHeiko Schocher #ifndef CONFIG_SYS_I2C_DIRECT_BUS
257f4ed3696SMark Tomlinson i2c_mux_disconnect_all();
258385c9ef5SHeiko Schocher #endif
259385c9ef5SHeiko Schocher
260385c9ef5SHeiko Schocher gd->cur_i2c_bus = bus;
261385c9ef5SHeiko Schocher if (I2C_ADAP->init_done == 0)
262385c9ef5SHeiko Schocher i2c_init_bus(bus, I2C_ADAP->speed, I2C_ADAP->slaveaddr);
263385c9ef5SHeiko Schocher
264385c9ef5SHeiko Schocher #ifndef CONFIG_SYS_I2C_DIRECT_BUS
265385c9ef5SHeiko Schocher i2c_mux_set_all();
266385c9ef5SHeiko Schocher #endif
267385c9ef5SHeiko Schocher return 0;
268385c9ef5SHeiko Schocher }
269385c9ef5SHeiko Schocher
270385c9ef5SHeiko Schocher /*
271385c9ef5SHeiko Schocher * Probe the given I2C chip address. Returns 0 if a chip responded,
272385c9ef5SHeiko Schocher * not 0 on failure.
273385c9ef5SHeiko Schocher */
i2c_probe(uint8_t chip)274385c9ef5SHeiko Schocher int i2c_probe(uint8_t chip)
275385c9ef5SHeiko Schocher {
276385c9ef5SHeiko Schocher return I2C_ADAP->probe(I2C_ADAP, chip);
277385c9ef5SHeiko Schocher }
278385c9ef5SHeiko Schocher
279385c9ef5SHeiko Schocher /*
280385c9ef5SHeiko Schocher * Read/Write interface:
281385c9ef5SHeiko Schocher * chip: I2C chip address, range 0..127
282385c9ef5SHeiko Schocher * addr: Memory (register) address within the chip
283385c9ef5SHeiko Schocher * alen: Number of bytes to use for addr (typically 1, 2 for larger
284385c9ef5SHeiko Schocher * memories, 0 for register type devices with only one
285385c9ef5SHeiko Schocher * register)
286385c9ef5SHeiko Schocher * buffer: Where to read/write the data
287385c9ef5SHeiko Schocher * len: How many bytes to read/write
288385c9ef5SHeiko Schocher *
289385c9ef5SHeiko Schocher * Returns: 0 on success, not 0 on failure
290385c9ef5SHeiko Schocher */
i2c_read(uint8_t chip,unsigned int addr,int alen,uint8_t * buffer,int len)291385c9ef5SHeiko Schocher int i2c_read(uint8_t chip, unsigned int addr, int alen,
292385c9ef5SHeiko Schocher uint8_t *buffer, int len)
293385c9ef5SHeiko Schocher {
294385c9ef5SHeiko Schocher return I2C_ADAP->read(I2C_ADAP, chip, addr, alen, buffer, len);
295385c9ef5SHeiko Schocher }
296385c9ef5SHeiko Schocher
i2c_write(uint8_t chip,unsigned int addr,int alen,uint8_t * buffer,int len)297385c9ef5SHeiko Schocher int i2c_write(uint8_t chip, unsigned int addr, int alen,
298385c9ef5SHeiko Schocher uint8_t *buffer, int len)
299385c9ef5SHeiko Schocher {
300385c9ef5SHeiko Schocher return I2C_ADAP->write(I2C_ADAP, chip, addr, alen, buffer, len);
301385c9ef5SHeiko Schocher }
302385c9ef5SHeiko Schocher
i2c_set_bus_speed(unsigned int speed)303385c9ef5SHeiko Schocher unsigned int i2c_set_bus_speed(unsigned int speed)
304385c9ef5SHeiko Schocher {
305385c9ef5SHeiko Schocher unsigned int ret;
306385c9ef5SHeiko Schocher
307385c9ef5SHeiko Schocher if (I2C_ADAP->set_bus_speed == NULL)
308385c9ef5SHeiko Schocher return 0;
309385c9ef5SHeiko Schocher ret = I2C_ADAP->set_bus_speed(I2C_ADAP, speed);
310385c9ef5SHeiko Schocher if (gd->flags & GD_FLG_RELOC)
3117cc1b02fSDarwin Rambo I2C_ADAP->speed = (ret == 0) ? speed : 0;
312385c9ef5SHeiko Schocher
313385c9ef5SHeiko Schocher return ret;
314385c9ef5SHeiko Schocher }
315385c9ef5SHeiko Schocher
i2c_get_bus_speed(void)316385c9ef5SHeiko Schocher unsigned int i2c_get_bus_speed(void)
317385c9ef5SHeiko Schocher {
318385c9ef5SHeiko Schocher struct i2c_adapter *cur = I2C_ADAP;
319385c9ef5SHeiko Schocher return cur->speed;
320385c9ef5SHeiko Schocher }
321385c9ef5SHeiko Schocher
i2c_reg_read(uint8_t addr,uint8_t reg)322385c9ef5SHeiko Schocher uint8_t i2c_reg_read(uint8_t addr, uint8_t reg)
323385c9ef5SHeiko Schocher {
324385c9ef5SHeiko Schocher uint8_t buf;
325385c9ef5SHeiko Schocher
326385c9ef5SHeiko Schocher i2c_read(addr, reg, 1, &buf, 1);
327385c9ef5SHeiko Schocher
328385c9ef5SHeiko Schocher #ifdef DEBUG
329385c9ef5SHeiko Schocher printf("%s: bus=%d addr=0x%02x, reg=0x%02x, val=0x%02x\n",
330385c9ef5SHeiko Schocher __func__, i2c_get_bus_num(), addr, reg, buf);
331385c9ef5SHeiko Schocher #endif
332385c9ef5SHeiko Schocher
333385c9ef5SHeiko Schocher return buf;
334385c9ef5SHeiko Schocher }
335385c9ef5SHeiko Schocher
i2c_reg_write(uint8_t addr,uint8_t reg,uint8_t val)336385c9ef5SHeiko Schocher void i2c_reg_write(uint8_t addr, uint8_t reg, uint8_t val)
337385c9ef5SHeiko Schocher {
338385c9ef5SHeiko Schocher #ifdef DEBUG
339385c9ef5SHeiko Schocher printf("%s: bus=%d addr=0x%02x, reg=0x%02x, val=0x%02x\n",
340385c9ef5SHeiko Schocher __func__, i2c_get_bus_num(), addr, reg, val);
341385c9ef5SHeiko Schocher #endif
342385c9ef5SHeiko Schocher
343385c9ef5SHeiko Schocher i2c_write(addr, reg, 1, &val, 1);
344385c9ef5SHeiko Schocher }
345385c9ef5SHeiko Schocher
i2c_init(int speed,int slaveaddr)34613a8b7aeSJeroen Hofstee __weak void i2c_init(int speed, int slaveaddr)
347385c9ef5SHeiko Schocher {
348385c9ef5SHeiko Schocher i2c_init_bus(i2c_get_bus_num(), speed, slaveaddr);
349385c9ef5SHeiko Schocher }
350