blob: 85cf75ecd9221c0f71824b914c971e0f643334a4 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Heiko Schochere7d9c4f2012-01-16 21:12:23 +00002/*
3 * Copyright (C) 2009 Sergey Kubushyn <ksi@koi8.net>
4 *
5 * (C) Copyright 2012
6 * Heiko Schocher, DENX Software Engineering, hs@denx.de.
7 *
8 * Multibus/multiadapter I2C core functions (wrappers)
Heiko Schochere7d9c4f2012-01-16 21:12:23 +00009 */
10#include <common.h>
11#include <i2c.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <linker_lists.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060013#include <asm/global_data.h>
Heiko Schochere7d9c4f2012-01-16 21:12:23 +000014
15struct i2c_adapter *i2c_get_adapter(int index)
16{
17 struct i2c_adapter *i2c_adap_p = ll_entry_start(struct i2c_adapter,
18 i2c);
19 int max = ll_entry_count(struct i2c_adapter, i2c);
20 int i;
21
22 if (index >= max) {
23 printf("Error, wrong i2c adapter %d max %d possible\n",
24 index, max);
25 return i2c_adap_p;
26 }
27 if (index == 0)
28 return i2c_adap_p;
29
30 for (i = 0; i < index; i++)
31 i2c_adap_p++;
32
33 return i2c_adap_p;
34}
35
36#if !defined(CONFIG_SYS_I2C_DIRECT_BUS)
37struct i2c_bus_hose i2c_bus[CONFIG_SYS_NUM_I2C_BUSES] =
38 CONFIG_SYS_I2C_BUSES;
39#endif
40
41DECLARE_GLOBAL_DATA_PTR;
42
Heiko Schochere7d9c4f2012-01-16 21:12:23 +000043#ifndef CONFIG_SYS_I2C_DIRECT_BUS
44/*
45 * i2c_mux_set()
46 * -------------
47 *
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.
54 *
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?)
58 */
59
60static int i2c_mux_set(struct i2c_adapter *adap, int mux_id, int chip,
61 int channel)
62{
63 uint8_t buf;
64 int ret;
65
66 /* channel < 0 - turn off the mux */
67 if (channel < 0) {
68 buf = 0;
69 ret = adap->write(adap, chip, 0, 0, &buf, 1);
70 if (ret)
71 printf("%s: Could not turn off the mux.\n", __func__);
72 return ret;
73 }
74
75 switch (mux_id) {
76 case I2C_MUX_PCA9540_ID:
77 case I2C_MUX_PCA9542_ID:
78 if (channel > 1)
79 return -1;
80 buf = (uint8_t)((channel & 0x01) | (1 << 2));
81 break;
82 case I2C_MUX_PCA9544_ID:
83 if (channel > 3)
84 return -1;
85 buf = (uint8_t)((channel & 0x03) | (1 << 2));
86 break;
87 case I2C_MUX_PCA9547_ID:
88 if (channel > 7)
89 return -1;
90 buf = (uint8_t)((channel & 0x07) | (1 << 3));
91 break;
Michael Burra04a55c2013-09-23 22:35:45 +000092 case I2C_MUX_PCA9548_ID:
93 if (channel > 7)
94 return -1;
95 buf = (uint8_t)(0x01 << channel);
96 break;
Heiko Schochere7d9c4f2012-01-16 21:12:23 +000097 default:
98 printf("%s: wrong mux id: %d\n", __func__, mux_id);
99 return -1;
100 }
101
102 ret = adap->write(adap, chip, 0, 0, &buf, 1);
103 if (ret)
104 printf("%s: could not set mux: id: %d chip: %x channel: %d\n",
105 __func__, mux_id, chip, channel);
106 return ret;
107}
108
109static int i2c_mux_set_all(void)
110{
111 struct i2c_bus_hose *i2c_bus_tmp = &i2c_bus[I2C_BUS];
112 int i;
113
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++) {
118 int ret;
119
120 if (i2c_bus_tmp->next_hop[i].chip == 0)
121 break;
122
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);
127 if (ret != 0)
128 return ret;
129 }
130 }
131 return 0;
132}
133
Mark Tomlinsonb896fee2014-12-02 08:49:19 +1300134static int i2c_mux_disconnect_all(void)
Heiko Schochere7d9c4f2012-01-16 21:12:23 +0000135{
136 struct i2c_bus_hose *i2c_bus_tmp = &i2c_bus[I2C_BUS];
137 int i;
Mark Tomlinson77535522014-12-02 08:49:18 +1300138 uint8_t buf = 0;
Heiko Schochere7d9c4f2012-01-16 21:12:23 +0000139
140 if (I2C_ADAP->init_done == 0)
141 return 0;
142
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;
147 do {
148 uint8_t chip;
149 int ret;
150
151 chip = i2c_bus_tmp->next_hop[--i].chip;
152 if (chip == 0)
153 continue;
154
155 ret = I2C_ADAP->write(I2C_ADAP, chip, 0, 0, &buf, 1);
156 if (ret != 0) {
Mark Tomlinsonb896fee2014-12-02 08:49:19 +1300157 printf("i2c: mux disconnect error\n");
Heiko Schochere7d9c4f2012-01-16 21:12:23 +0000158 return ret;
159 }
160 } while (i > 0);
161 }
162
163 return 0;
164}
165#endif
166
167/*
168 * i2c_init_bus():
169 * ---------------
170 *
171 * Initializes one bus. Will initialize the parent adapter. No current bus
172 * changes, no mux (if any) setup.
173 */
174static void i2c_init_bus(unsigned int bus_no, int speed, int slaveaddr)
175{
176 if (bus_no >= CONFIG_SYS_NUM_I2C_BUSES)
177 return;
178
179 I2C_ADAP->init(I2C_ADAP, speed, slaveaddr);
180
181 if (gd->flags & GD_FLG_RELOC) {
182 I2C_ADAP->init_done = 1;
183 I2C_ADAP->speed = speed;
184 I2C_ADAP->slaveaddr = slaveaddr;
185 }
186}
187
188/* implement possible board specific board init */
Jeroen Hofsteef458c4d2014-10-27 22:27:18 +0100189__weak void i2c_init_board(void)
Heiko Schochere7d9c4f2012-01-16 21:12:23 +0000190{
191}
Heiko Schochere7d9c4f2012-01-16 21:12:23 +0000192
Yuan Yaod40c8852016-06-08 18:24:51 +0800193/* implement possible for i2c specific early i2c init */
194__weak void i2c_early_init_f(void)
195{
196}
197
Heiko Schochere7d9c4f2012-01-16 21:12:23 +0000198/*
199 * i2c_init_all():
200 *
201 * not longer needed, will deleted. Actual init the SPD_BUS
202 * for compatibility.
203 * i2c_adap[] must be initialized beforehead with function pointers and
204 * data, including speed and slaveaddr.
205 */
206void i2c_init_all(void)
207{
208 i2c_init_board();
209 i2c_set_bus_num(CONFIG_SYS_SPD_BUS_NUM);
210 return;
211}
212
213/*
214 * i2c_get_bus_num():
215 * ------------------
216 *
217 * Returns index of currently active I2C bus. Zero-based.
218 */
219unsigned int i2c_get_bus_num(void)
220{
221 return gd->cur_i2c_bus;
222}
223
224/*
225 * i2c_set_bus_num():
226 * ------------------
227 *
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.
233 *
234 * bus - bus index, zero based
235 *
236 * Returns: 0 on success, not 0 on failure
237 */
238int i2c_set_bus_num(unsigned int bus)
239{
Heiko Schocher48e17bf2013-10-04 07:36:34 +0200240 int max;
Heiko Schochere7d9c4f2012-01-16 21:12:23 +0000241
Heiko Schocher48e17bf2013-10-04 07:36:34 +0200242 if ((bus == I2C_BUS) && (I2C_ADAP->init_done > 0))
243 return 0;
244
Heiko Schochere7d9c4f2012-01-16 21:12:23 +0000245#ifndef CONFIG_SYS_I2C_DIRECT_BUS
246 if (bus >= CONFIG_SYS_NUM_I2C_BUSES)
247 return -1;
248#endif
249
Heiko Schocher48e17bf2013-10-04 07:36:34 +0200250 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);
254 return -2;
255 }
Heiko Schochere7d9c4f2012-01-16 21:12:23 +0000256
257#ifndef CONFIG_SYS_I2C_DIRECT_BUS
Mark Tomlinsonb896fee2014-12-02 08:49:19 +1300258 i2c_mux_disconnect_all();
Heiko Schochere7d9c4f2012-01-16 21:12:23 +0000259#endif
260
261 gd->cur_i2c_bus = bus;
262 if (I2C_ADAP->init_done == 0)
263 i2c_init_bus(bus, I2C_ADAP->speed, I2C_ADAP->slaveaddr);
264
265#ifndef CONFIG_SYS_I2C_DIRECT_BUS
266 i2c_mux_set_all();
267#endif
268 return 0;
269}
270
271/*
272 * Probe the given I2C chip address. Returns 0 if a chip responded,
273 * not 0 on failure.
274 */
275int i2c_probe(uint8_t chip)
276{
277 return I2C_ADAP->probe(I2C_ADAP, chip);
278}
279
280/*
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
286 * register)
287 * buffer: Where to read/write the data
288 * len: How many bytes to read/write
289 *
290 * Returns: 0 on success, not 0 on failure
291 */
292int i2c_read(uint8_t chip, unsigned int addr, int alen,
293 uint8_t *buffer, int len)
294{
295 return I2C_ADAP->read(I2C_ADAP, chip, addr, alen, buffer, len);
296}
297
298int i2c_write(uint8_t chip, unsigned int addr, int alen,
299 uint8_t *buffer, int len)
300{
301 return I2C_ADAP->write(I2C_ADAP, chip, addr, alen, buffer, len);
302}
303
304unsigned int i2c_set_bus_speed(unsigned int speed)
305{
306 unsigned int ret;
307
308 if (I2C_ADAP->set_bus_speed == NULL)
309 return 0;
310 ret = I2C_ADAP->set_bus_speed(I2C_ADAP, speed);
311 if (gd->flags & GD_FLG_RELOC)
Darwin Rambo3fc1a002013-12-13 12:57:26 -0800312 I2C_ADAP->speed = (ret == 0) ? speed : 0;
Heiko Schochere7d9c4f2012-01-16 21:12:23 +0000313
314 return ret;
315}
316
317unsigned int i2c_get_bus_speed(void)
318{
319 struct i2c_adapter *cur = I2C_ADAP;
320 return cur->speed;
321}
322
323uint8_t i2c_reg_read(uint8_t addr, uint8_t reg)
324{
325 uint8_t buf;
326
Heiko Schochere7d9c4f2012-01-16 21:12:23 +0000327 i2c_read(addr, reg, 1, &buf, 1);
328
329#ifdef DEBUG
330 printf("%s: bus=%d addr=0x%02x, reg=0x%02x, val=0x%02x\n",
331 __func__, i2c_get_bus_num(), addr, reg, buf);
332#endif
333
334 return buf;
335}
336
337void i2c_reg_write(uint8_t addr, uint8_t reg, uint8_t val)
338{
Heiko Schochere7d9c4f2012-01-16 21:12:23 +0000339#ifdef DEBUG
340 printf("%s: bus=%d addr=0x%02x, reg=0x%02x, val=0x%02x\n",
341 __func__, i2c_get_bus_num(), addr, reg, val);
342#endif
343
344 i2c_write(addr, reg, 1, &val, 1);
345}
346
Jeroen Hofsteef458c4d2014-10-27 22:27:18 +0100347__weak void i2c_init(int speed, int slaveaddr)
Heiko Schochere7d9c4f2012-01-16 21:12:23 +0000348{
349 i2c_init_bus(i2c_get_bus_num(), speed, slaveaddr);
350}