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 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 struct i2c_adapter *i2c_get_adapter(int index)
28 struct i2c_adapter *i2c_adap_p = ll_entry_start(struct i2c_adapter,
30 int max = ll_entry_count(struct i2c_adapter, i2c);
34 printf("Error, wrong i2c adapter %d max %d possible\n",
41 for (i = 0; i < index; i++)
47 #if !defined(CONFIG_SYS_I2C_DIRECT_BUS)
48 struct i2c_bus_hose i2c_bus[CONFIG_SYS_NUM_I2C_BUSES] =
52 DECLARE_GLOBAL_DATA_PTR;
54 void i2c_reloc_fixup(void)
56 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
57 struct i2c_adapter *i2c_adap_p = ll_entry_start(struct i2c_adapter,
59 struct i2c_adapter *tmp = i2c_adap_p;
60 int max = ll_entry_count(struct i2c_adapter, i2c);
64 if (gd->reloc_off == 0)
67 for (i = 0; i < max; i++) {
69 addr = (unsigned long)i2c_adap_p;
70 addr += gd->reloc_off;
71 i2c_adap_p = (struct i2c_adapter *)addr;
73 addr = (unsigned long)i2c_adap_p->init;
74 addr += gd->reloc_off;
75 i2c_adap_p->init = (void (*)(int, int))addr;
77 addr = (unsigned long)i2c_adap_p->probe;
78 addr += gd->reloc_off;
79 i2c_adap_p->probe = (int (*)(uint8_t))addr;
81 addr = (unsigned long)i2c_adap_p->read;
82 addr += gd->reloc_off;
83 i2c_adap_p->read = (int (*)(uint8_t, uint, int, uint8_t *,
86 addr = (unsigned long)i2c_adap_p->write;
87 addr += gd->reloc_off;
88 i2c_adap_p->write = (int (*)(uint8_t, uint, int, uint8_t *,
90 /* i2c_set_bus_speed() */
91 addr = (unsigned long)i2c_adap_p->set_bus_speed;
92 addr += gd->reloc_off;
93 i2c_adap_p->set_bus_speed = (uint (*)(uint))addr;
95 addr = (unsigned long)i2c_adap_p->name;
96 addr += gd->reloc_off;
97 i2c_adap_p->name = (char *)addr;
104 #ifndef CONFIG_SYS_I2C_DIRECT_BUS
109 * This turns on the given channel on I2C multiplexer chip connected to
110 * a given I2C adapter directly or via other multiplexers. In the latter
111 * case the entire multiplexer chain must be initialized first starting
112 * with the one connected directly to the adapter. When disabling a chain
113 * muxes must be programmed in reverse order, starting with the one
114 * farthest from the adapter.
116 * mux_id is the multiplexer chip type from defined in i2c.h. So far only
117 * NXP (Philips) PCA954x multiplexers are supported. Switches are NOT
118 * supported (anybody uses them?)
121 static int i2c_mux_set(struct i2c_adapter *adap, int mux_id, int chip,
127 /* channel < 0 - turn off the mux */
130 ret = adap->write(adap, chip, 0, 0, &buf, 1);
132 printf("%s: Could not turn off the mux.\n", __func__);
137 case I2C_MUX_PCA9540_ID:
138 case I2C_MUX_PCA9542_ID:
141 buf = (uint8_t)((channel & 0x01) | (1 << 2));
143 case I2C_MUX_PCA9544_ID:
146 buf = (uint8_t)((channel & 0x03) | (1 << 2));
148 case I2C_MUX_PCA9547_ID:
151 buf = (uint8_t)((channel & 0x07) | (1 << 3));
154 printf("%s: wrong mux id: %d\n", __func__, mux_id);
158 ret = adap->write(adap, chip, 0, 0, &buf, 1);
160 printf("%s: could not set mux: id: %d chip: %x channel: %d\n",
161 __func__, mux_id, chip, channel);
165 static int i2c_mux_set_all(void)
167 struct i2c_bus_hose *i2c_bus_tmp = &i2c_bus[I2C_BUS];
170 /* Connect requested bus if behind muxes */
171 if (i2c_bus_tmp->next_hop[0].chip != 0) {
172 /* Set all muxes along the path to that bus */
173 for (i = 0; i < CONFIG_SYS_I2C_MAX_HOPS; i++) {
176 if (i2c_bus_tmp->next_hop[i].chip == 0)
179 ret = i2c_mux_set(I2C_ADAP,
180 i2c_bus_tmp->next_hop[i].mux.id,
181 i2c_bus_tmp->next_hop[i].chip,
182 i2c_bus_tmp->next_hop[i].channel);
190 static int i2c_mux_disconnet_all(void)
192 struct i2c_bus_hose *i2c_bus_tmp = &i2c_bus[I2C_BUS];
196 if (I2C_ADAP->init_done == 0)
199 /* Disconnect current bus (turn off muxes if any) */
200 if ((i2c_bus_tmp->next_hop[0].chip != 0) &&
201 (I2C_ADAP->init_done != 0)) {
202 i = CONFIG_SYS_I2C_MAX_HOPS;
207 chip = i2c_bus_tmp->next_hop[--i].chip;
211 ret = I2C_ADAP->write(I2C_ADAP, chip, 0, 0, &buf, 1);
213 printf("i2c: mux diconnect error\n");
227 * Initializes one bus. Will initialize the parent adapter. No current bus
228 * changes, no mux (if any) setup.
230 static void i2c_init_bus(unsigned int bus_no, int speed, int slaveaddr)
232 if (bus_no >= CONFIG_SYS_NUM_I2C_BUSES)
235 I2C_ADAP->init(I2C_ADAP, speed, slaveaddr);
237 if (gd->flags & GD_FLG_RELOC) {
238 I2C_ADAP->init_done = 1;
239 I2C_ADAP->speed = speed;
240 I2C_ADAP->slaveaddr = slaveaddr;
244 /* implement possible board specific board init */
245 static void __def_i2c_init_board(void)
248 void i2c_init_board(void)
249 __attribute__((weak, alias("__def_i2c_init_board")));
254 * not longer needed, will deleted. Actual init the SPD_BUS
256 * i2c_adap[] must be initialized beforehead with function pointers and
257 * data, including speed and slaveaddr.
259 void i2c_init_all(void)
262 i2c_set_bus_num(CONFIG_SYS_SPD_BUS_NUM);
270 * Returns index of currently active I2C bus. Zero-based.
272 unsigned int i2c_get_bus_num(void)
274 return gd->cur_i2c_bus;
281 * Change the active I2C bus. Subsequent read/write calls will
282 * go to this one. Sets all of the muxes in a proper condition
283 * if that bus is behind muxes.
284 * If previously selected bus is behind the muxes turns off all the
285 * muxes along the path to that bus.
287 * bus - bus index, zero based
289 * Returns: 0 on success, not 0 on failure
291 int i2c_set_bus_num(unsigned int bus)
293 int max = ll_entry_count(struct i2c_adapter, i2c);
295 if (I2C_ADAPTER(bus) >= max) {
296 printf("Error, wrong i2c adapter %d max %d possible\n",
297 I2C_ADAPTER(bus), max);
300 #ifndef CONFIG_SYS_I2C_DIRECT_BUS
301 if (bus >= CONFIG_SYS_NUM_I2C_BUSES)
305 if ((bus == I2C_BUS) && (I2C_ADAP->init_done > 0))
308 #ifndef CONFIG_SYS_I2C_DIRECT_BUS
309 i2c_mux_disconnet_all();
312 gd->cur_i2c_bus = bus;
313 if (I2C_ADAP->init_done == 0)
314 i2c_init_bus(bus, I2C_ADAP->speed, I2C_ADAP->slaveaddr);
316 #ifndef CONFIG_SYS_I2C_DIRECT_BUS
323 * Probe the given I2C chip address. Returns 0 if a chip responded,
326 int i2c_probe(uint8_t chip)
328 return I2C_ADAP->probe(I2C_ADAP, chip);
332 * Read/Write interface:
333 * chip: I2C chip address, range 0..127
334 * addr: Memory (register) address within the chip
335 * alen: Number of bytes to use for addr (typically 1, 2 for larger
336 * memories, 0 for register type devices with only one
338 * buffer: Where to read/write the data
339 * len: How many bytes to read/write
341 * Returns: 0 on success, not 0 on failure
343 int i2c_read(uint8_t chip, unsigned int addr, int alen,
344 uint8_t *buffer, int len)
346 return I2C_ADAP->read(I2C_ADAP, chip, addr, alen, buffer, len);
349 int i2c_write(uint8_t chip, unsigned int addr, int alen,
350 uint8_t *buffer, int len)
352 return I2C_ADAP->write(I2C_ADAP, chip, addr, alen, buffer, len);
355 unsigned int i2c_set_bus_speed(unsigned int speed)
359 if (I2C_ADAP->set_bus_speed == NULL)
361 ret = I2C_ADAP->set_bus_speed(I2C_ADAP, speed);
362 if (gd->flags & GD_FLG_RELOC)
363 I2C_ADAP->speed = ret;
368 unsigned int i2c_get_bus_speed(void)
370 struct i2c_adapter *cur = I2C_ADAP;
374 uint8_t i2c_reg_read(uint8_t addr, uint8_t reg)
379 /* MPC8xx needs this. Maybe one day we can get rid of it. */
380 /* maybe it is now the time for it ... */
381 i2c_set_bus_num(i2c_get_bus_num());
383 i2c_read(addr, reg, 1, &buf, 1);
386 printf("%s: bus=%d addr=0x%02x, reg=0x%02x, val=0x%02x\n",
387 __func__, i2c_get_bus_num(), addr, reg, buf);
393 void i2c_reg_write(uint8_t addr, uint8_t reg, uint8_t val)
396 /* MPC8xx needs this. Maybe one day we can get rid of it. */
397 /* maybe it is now the time for it ... */
398 i2c_set_bus_num(i2c_get_bus_num());
402 printf("%s: bus=%d addr=0x%02x, reg=0x%02x, val=0x%02x\n",
403 __func__, i2c_get_bus_num(), addr, reg, val);
406 i2c_write(addr, reg, 1, &val, 1);
409 void __i2c_init(int speed, int slaveaddr)
411 i2c_init_bus(i2c_get_bus_num(), speed, slaveaddr);
413 void i2c_init(int speed, int slaveaddr)
414 __attribute__((weak, alias("__i2c_init")));