xref: /openbmc/u-boot/drivers/i2c/i2c_core.c (revision aa04fef4)
1 /*
2  * Copyright (C) 2009 Sergey Kubushyn <ksi@koi8.net>
3  *
4  * (C) Copyright 2012
5  * Heiko Schocher, DENX Software Engineering, hs@denx.de.
6  *
7  * Multibus/multiadapter I2C core functions (wrappers)
8  *
9  * SPDX-License-Identifier:	GPL-2.0+
10  */
11 #include <common.h>
12 #include <i2c.h>
13 
14 struct i2c_adapter *i2c_get_adapter(int index)
15 {
16 	struct i2c_adapter *i2c_adap_p = ll_entry_start(struct i2c_adapter,
17 						i2c);
18 	int max = ll_entry_count(struct i2c_adapter, i2c);
19 	int i;
20 
21 	if (index >= max) {
22 		printf("Error, wrong i2c adapter %d max %d possible\n",
23 		       index, max);
24 		return i2c_adap_p;
25 	}
26 	if (index == 0)
27 		return i2c_adap_p;
28 
29 	for (i = 0; i < index; i++)
30 		i2c_adap_p++;
31 
32 	return i2c_adap_p;
33 }
34 
35 #if !defined(CONFIG_SYS_I2C_DIRECT_BUS)
36 struct i2c_bus_hose i2c_bus[CONFIG_SYS_NUM_I2C_BUSES] =
37 			CONFIG_SYS_I2C_BUSES;
38 #endif
39 
40 DECLARE_GLOBAL_DATA_PTR;
41 
42 void i2c_reloc_fixup(void)
43 {
44 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
45 	struct i2c_adapter *i2c_adap_p = ll_entry_start(struct i2c_adapter,
46 						i2c);
47 	struct i2c_adapter *tmp = i2c_adap_p;
48 	int max = ll_entry_count(struct i2c_adapter, i2c);
49 	int		i;
50 	unsigned long	addr;
51 
52 	if (gd->reloc_off == 0)
53 		return;
54 
55 	for (i = 0; i < max; i++) {
56 		/* adapter itself */
57 		addr = (unsigned long)i2c_adap_p;
58 		addr += gd->reloc_off;
59 		i2c_adap_p = (struct i2c_adapter *)addr;
60 		/* i2c_init() */
61 		addr = (unsigned long)i2c_adap_p->init;
62 		addr += gd->reloc_off;
63 		i2c_adap_p->init = (void (*)(int, int))addr;
64 		/* i2c_probe() */
65 		addr = (unsigned long)i2c_adap_p->probe;
66 		addr += gd->reloc_off;
67 		i2c_adap_p->probe = (int (*)(uint8_t))addr;
68 		/* i2c_read() */
69 		addr = (unsigned long)i2c_adap_p->read;
70 		addr += gd->reloc_off;
71 		i2c_adap_p->read = (int (*)(uint8_t, uint, int, uint8_t *,
72 					int))addr;
73 		/* i2c_write() */
74 		addr = (unsigned long)i2c_adap_p->write;
75 		addr += gd->reloc_off;
76 		i2c_adap_p->write = (int (*)(uint8_t, uint, int, uint8_t *,
77 					int))addr;
78 		/* i2c_set_bus_speed() */
79 		addr = (unsigned long)i2c_adap_p->set_bus_speed;
80 		addr += gd->reloc_off;
81 		i2c_adap_p->set_bus_speed = (uint (*)(uint))addr;
82 		/* name */
83 		addr = (unsigned long)i2c_adap_p->name;
84 		addr += gd->reloc_off;
85 		i2c_adap_p->name = (char *)addr;
86 		tmp++;
87 		i2c_adap_p = tmp;
88 	}
89 #endif
90 }
91 
92 #ifndef CONFIG_SYS_I2C_DIRECT_BUS
93 /*
94  * i2c_mux_set()
95  * -------------
96  *
97  * This turns on the given channel on I2C multiplexer chip connected to
98  * a given I2C adapter directly or via other multiplexers. In the latter
99  * case the entire multiplexer chain must be initialized first starting
100  * with the one connected directly to the adapter. When disabling a chain
101  * muxes must be programmed in reverse order, starting with the one
102  * farthest from the adapter.
103  *
104  * mux_id is the multiplexer chip type from defined in i2c.h. So far only
105  * NXP (Philips) PCA954x multiplexers are supported. Switches are NOT
106  * supported (anybody uses them?)
107  */
108 
109 static int i2c_mux_set(struct i2c_adapter *adap, int mux_id, int chip,
110 			int channel)
111 {
112 	uint8_t	buf;
113 	int ret;
114 
115 	/* channel < 0 - turn off the mux */
116 	if (channel < 0) {
117 		buf = 0;
118 		ret = adap->write(adap, chip, 0, 0, &buf, 1);
119 		if (ret)
120 			printf("%s: Could not turn off the mux.\n", __func__);
121 		return ret;
122 	}
123 
124 	switch (mux_id) {
125 	case I2C_MUX_PCA9540_ID:
126 	case I2C_MUX_PCA9542_ID:
127 		if (channel > 1)
128 			return -1;
129 		buf = (uint8_t)((channel & 0x01) | (1 << 2));
130 		break;
131 	case I2C_MUX_PCA9544_ID:
132 		if (channel > 3)
133 			return -1;
134 		buf = (uint8_t)((channel & 0x03) | (1 << 2));
135 		break;
136 	case I2C_MUX_PCA9547_ID:
137 		if (channel > 7)
138 			return -1;
139 		buf = (uint8_t)((channel & 0x07) | (1 << 3));
140 		break;
141 	default:
142 		printf("%s: wrong mux id: %d\n", __func__, mux_id);
143 		return -1;
144 	}
145 
146 	ret = adap->write(adap, chip, 0, 0, &buf, 1);
147 	if (ret)
148 		printf("%s: could not set mux: id: %d chip: %x channel: %d\n",
149 		       __func__, mux_id, chip, channel);
150 	return ret;
151 }
152 
153 static int i2c_mux_set_all(void)
154 {
155 	struct i2c_bus_hose *i2c_bus_tmp = &i2c_bus[I2C_BUS];
156 	int i;
157 
158 	/* Connect requested bus if behind muxes */
159 	if (i2c_bus_tmp->next_hop[0].chip != 0) {
160 		/* Set all muxes along the path to that bus */
161 		for (i = 0; i < CONFIG_SYS_I2C_MAX_HOPS; i++) {
162 			int	ret;
163 
164 			if (i2c_bus_tmp->next_hop[i].chip == 0)
165 				break;
166 
167 			ret = i2c_mux_set(I2C_ADAP,
168 					i2c_bus_tmp->next_hop[i].mux.id,
169 					i2c_bus_tmp->next_hop[i].chip,
170 					i2c_bus_tmp->next_hop[i].channel);
171 			if (ret != 0)
172 				return ret;
173 		}
174 	}
175 	return 0;
176 }
177 
178 static int i2c_mux_disconnet_all(void)
179 {
180 	struct	i2c_bus_hose *i2c_bus_tmp = &i2c_bus[I2C_BUS];
181 	int	i;
182 	uint8_t	buf;
183 
184 	if (I2C_ADAP->init_done == 0)
185 		return 0;
186 
187 	/* Disconnect current bus (turn off muxes if any) */
188 	if ((i2c_bus_tmp->next_hop[0].chip != 0) &&
189 	    (I2C_ADAP->init_done != 0)) {
190 		i = CONFIG_SYS_I2C_MAX_HOPS;
191 		do {
192 			uint8_t	chip;
193 			int ret;
194 
195 			chip = i2c_bus_tmp->next_hop[--i].chip;
196 			if (chip == 0)
197 				continue;
198 
199 			ret = I2C_ADAP->write(I2C_ADAP, chip, 0, 0, &buf, 1);
200 			if (ret != 0) {
201 				printf("i2c: mux diconnect error\n");
202 				return ret;
203 			}
204 		} while (i > 0);
205 	}
206 
207 	return 0;
208 }
209 #endif
210 
211 /*
212  * i2c_init_bus():
213  * ---------------
214  *
215  * Initializes one bus. Will initialize the parent adapter. No current bus
216  * changes, no mux (if any) setup.
217  */
218 static void i2c_init_bus(unsigned int bus_no, int speed, int slaveaddr)
219 {
220 	if (bus_no >= CONFIG_SYS_NUM_I2C_BUSES)
221 		return;
222 
223 	I2C_ADAP->init(I2C_ADAP, speed, slaveaddr);
224 
225 	if (gd->flags & GD_FLG_RELOC) {
226 		I2C_ADAP->init_done = 1;
227 		I2C_ADAP->speed = speed;
228 		I2C_ADAP->slaveaddr = slaveaddr;
229 	}
230 }
231 
232 /* implement possible board specific board init */
233 static void __def_i2c_init_board(void)
234 {
235 }
236 void i2c_init_board(void)
237 	__attribute__((weak, alias("__def_i2c_init_board")));
238 
239 /*
240  * i2c_init_all():
241  *
242  * not longer needed, will deleted. Actual init the SPD_BUS
243  * for compatibility.
244  * i2c_adap[] must be initialized beforehead with function pointers and
245  * data, including speed and slaveaddr.
246  */
247 void i2c_init_all(void)
248 {
249 	i2c_init_board();
250 	i2c_set_bus_num(CONFIG_SYS_SPD_BUS_NUM);
251 	return;
252 }
253 
254 /*
255  * i2c_get_bus_num():
256  * ------------------
257  *
258  *  Returns index of currently active I2C bus.  Zero-based.
259  */
260 unsigned int i2c_get_bus_num(void)
261 {
262 	return gd->cur_i2c_bus;
263 }
264 
265 /*
266  * i2c_set_bus_num():
267  * ------------------
268  *
269  *  Change the active I2C bus. Subsequent read/write calls will
270  *  go to this one. Sets all of the muxes in a proper condition
271  *  if that bus is behind muxes.
272  *  If previously selected bus is behind the muxes turns off all the
273  *  muxes along the path to that bus.
274  *
275  *	bus - bus index, zero based
276  *
277  *	Returns: 0 on success, not 0 on failure
278  */
279 int i2c_set_bus_num(unsigned int bus)
280 {
281 	int max = ll_entry_count(struct i2c_adapter, i2c);
282 
283 	if (I2C_ADAPTER(bus) >= max) {
284 		printf("Error, wrong i2c adapter %d max %d possible\n",
285 		       I2C_ADAPTER(bus), max);
286 		return -2;
287 	}
288 #ifndef CONFIG_SYS_I2C_DIRECT_BUS
289 	if (bus >= CONFIG_SYS_NUM_I2C_BUSES)
290 		return -1;
291 #endif
292 
293 	if ((bus == I2C_BUS) && (I2C_ADAP->init_done > 0))
294 		return 0;
295 
296 #ifndef CONFIG_SYS_I2C_DIRECT_BUS
297 	i2c_mux_disconnet_all();
298 #endif
299 
300 	gd->cur_i2c_bus = bus;
301 	if (I2C_ADAP->init_done == 0)
302 		i2c_init_bus(bus, I2C_ADAP->speed, I2C_ADAP->slaveaddr);
303 
304 #ifndef CONFIG_SYS_I2C_DIRECT_BUS
305 	i2c_mux_set_all();
306 #endif
307 	return 0;
308 }
309 
310 /*
311  * Probe the given I2C chip address.  Returns 0 if a chip responded,
312  * not 0 on failure.
313  */
314 int i2c_probe(uint8_t chip)
315 {
316 	return I2C_ADAP->probe(I2C_ADAP, chip);
317 }
318 
319 /*
320  * Read/Write interface:
321  *   chip:    I2C chip address, range 0..127
322  *   addr:    Memory (register) address within the chip
323  *   alen:    Number of bytes to use for addr (typically 1, 2 for larger
324  *              memories, 0 for register type devices with only one
325  *              register)
326  *   buffer:  Where to read/write the data
327  *   len:     How many bytes to read/write
328  *
329  *   Returns: 0 on success, not 0 on failure
330  */
331 int i2c_read(uint8_t chip, unsigned int addr, int alen,
332 				uint8_t *buffer, int len)
333 {
334 	return I2C_ADAP->read(I2C_ADAP, chip, addr, alen, buffer, len);
335 }
336 
337 int i2c_write(uint8_t chip, unsigned int addr, int alen,
338 				uint8_t *buffer, int len)
339 {
340 	return I2C_ADAP->write(I2C_ADAP, chip, addr, alen, buffer, len);
341 }
342 
343 unsigned int i2c_set_bus_speed(unsigned int speed)
344 {
345 	unsigned int ret;
346 
347 	if (I2C_ADAP->set_bus_speed == NULL)
348 		return 0;
349 	ret = I2C_ADAP->set_bus_speed(I2C_ADAP, speed);
350 	if (gd->flags & GD_FLG_RELOC)
351 		I2C_ADAP->speed = ret;
352 
353 	return ret;
354 }
355 
356 unsigned int i2c_get_bus_speed(void)
357 {
358 	struct i2c_adapter *cur = I2C_ADAP;
359 	return cur->speed;
360 }
361 
362 uint8_t i2c_reg_read(uint8_t addr, uint8_t reg)
363 {
364 	uint8_t buf;
365 
366 #ifdef CONFIG_8xx
367 	/* MPC8xx needs this.  Maybe one day we can get rid of it. */
368 	/* maybe it is now the time for it ... */
369 	i2c_set_bus_num(i2c_get_bus_num());
370 #endif
371 	i2c_read(addr, reg, 1, &buf, 1);
372 
373 #ifdef DEBUG
374 	printf("%s: bus=%d addr=0x%02x, reg=0x%02x, val=0x%02x\n",
375 	       __func__, i2c_get_bus_num(), addr, reg, buf);
376 #endif
377 
378 	return buf;
379 }
380 
381 void i2c_reg_write(uint8_t addr, uint8_t reg, uint8_t val)
382 {
383 #ifdef CONFIG_8xx
384 	/* MPC8xx needs this.  Maybe one day we can get rid of it. */
385 	/* maybe it is now the time for it ... */
386 	i2c_set_bus_num(i2c_get_bus_num());
387 #endif
388 
389 #ifdef DEBUG
390 	printf("%s: bus=%d addr=0x%02x, reg=0x%02x, val=0x%02x\n",
391 	       __func__, i2c_get_bus_num(), addr, reg, val);
392 #endif
393 
394 	i2c_write(addr, reg, 1, &val, 1);
395 }
396 
397 void __i2c_init(int speed, int slaveaddr)
398 {
399 	i2c_init_bus(i2c_get_bus_num(), speed, slaveaddr);
400 }
401 void i2c_init(int speed, int slaveaddr)
402 	__attribute__((weak, alias("__i2c_init")));
403