]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/i2c/i2c_core.c
3c01893c9c4742b6203ae85e4601cdf65f2d3c1e
[karo-tx-uboot.git] / drivers / i2c / i2c_core.c
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  * 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.
13  *
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.
18  *
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.
22  */
23 #include <common.h>
24 #include <i2c.h>
25
26 struct i2c_adapter *i2c_get_adapter(int index)
27 {
28         struct i2c_adapter *i2c_adap_p = ll_entry_start(struct i2c_adapter,
29                                                 i2c);
30         int max = ll_entry_count(struct i2c_adapter, i2c);
31         int i;
32
33         if (index >= max) {
34                 printf("Error, wrong i2c adapter %d max %d possible\n",
35                        index, max);
36                 return i2c_adap_p;
37         }
38         if (index == 0)
39                 return i2c_adap_p;
40
41         for (i = 0; i < index; i++)
42                 i2c_adap_p++;
43
44         return i2c_adap_p;
45 }
46
47 #if !defined(CONFIG_SYS_I2C_DIRECT_BUS)
48 struct i2c_bus_hose i2c_bus[CONFIG_SYS_NUM_I2C_BUSES] =
49                         CONFIG_SYS_I2C_BUSES;
50 #endif
51
52 DECLARE_GLOBAL_DATA_PTR;
53
54 void i2c_reloc_fixup(void)
55 {
56 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
57         struct i2c_adapter *i2c_adap_p = ll_entry_start(struct i2c_adapter,
58                                                 i2c);
59         struct i2c_adapter *tmp = i2c_adap_p;
60         int max = ll_entry_count(struct i2c_adapter, i2c);
61         int             i;
62         unsigned long   addr;
63
64         if (gd->reloc_off == 0)
65                 return;
66
67         for (i = 0; i < max; i++) {
68                 /* adapter itself */
69                 addr = (unsigned long)i2c_adap_p;
70                 addr += gd->reloc_off;
71                 i2c_adap_p = (struct i2c_adapter *)addr;
72                 /* i2c_init() */
73                 addr = (unsigned long)i2c_adap_p->init;
74                 addr += gd->reloc_off;
75                 i2c_adap_p->init = (void (*)(int, int))addr;
76                 /* i2c_probe() */
77                 addr = (unsigned long)i2c_adap_p->probe;
78                 addr += gd->reloc_off;
79                 i2c_adap_p->probe = (int (*)(uint8_t))addr;
80                 /* i2c_read() */
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 *,
84                                         int))addr;
85                 /* i2c_write() */
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 *,
89                                         int))addr;
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;
94                 /* name */
95                 addr = (unsigned long)i2c_adap_p->name;
96                 addr += gd->reloc_off;
97                 i2c_adap_p->name = (char *)addr;
98                 tmp++;
99                 i2c_adap_p = tmp;
100         }
101 #endif
102 }
103
104 #ifndef CONFIG_SYS_I2C_DIRECT_BUS
105 /*
106  * i2c_mux_set()
107  * -------------
108  *
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.
115  *
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?)
119  */
120
121 static int i2c_mux_set(struct i2c_adapter *adap, int mux_id, int chip,
122                         int channel)
123 {
124         uint8_t buf;
125         int ret;
126
127         /* channel < 0 - turn off the mux */
128         if (channel < 0) {
129                 buf = 0;
130                 ret = adap->write(adap, chip, 0, 0, &buf, 1);
131                 if (ret)
132                         printf("%s: Could not turn off the mux.\n", __func__);
133                 return ret;
134         }
135
136         switch (mux_id) {
137         case I2C_MUX_PCA9540_ID:
138         case I2C_MUX_PCA9542_ID:
139                 if (channel > 1)
140                         return -1;
141                 buf = (uint8_t)((channel & 0x01) | (1 << 2));
142                 break;
143         case I2C_MUX_PCA9544_ID:
144                 if (channel > 3)
145                         return -1;
146                 buf = (uint8_t)((channel & 0x03) | (1 << 2));
147                 break;
148         case I2C_MUX_PCA9547_ID:
149                 if (channel > 7)
150                         return -1;
151                 buf = (uint8_t)((channel & 0x07) | (1 << 3));
152                 break;
153         default:
154                 printf("%s: wrong mux id: %d\n", __func__, mux_id);
155                 return -1;
156         }
157
158         ret = adap->write(adap, chip, 0, 0, &buf, 1);
159         if (ret)
160                 printf("%s: could not set mux: id: %d chip: %x channel: %d\n",
161                        __func__, mux_id, chip, channel);
162         return ret;
163 }
164
165 static int i2c_mux_set_all(void)
166 {
167         struct i2c_bus_hose *i2c_bus_tmp = &i2c_bus[I2C_BUS];
168         int i;
169
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++) {
174                         int     ret;
175
176                         if (i2c_bus_tmp->next_hop[i].chip == 0)
177                                 break;
178
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);
183                         if (ret != 0)
184                                 return ret;
185                 }
186         }
187         return 0;
188 }
189
190 static int i2c_mux_disconnet_all(void)
191 {
192         struct  i2c_bus_hose *i2c_bus_tmp = &i2c_bus[I2C_BUS];
193         int     i;
194         uint8_t buf;
195
196         if (I2C_ADAP->init_done == 0)
197                 return 0;
198
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;
203                 do {
204                         uint8_t chip;
205                         int ret;
206
207                         chip = i2c_bus_tmp->next_hop[--i].chip;
208                         if (chip == 0)
209                                 continue;
210
211                         ret = I2C_ADAP->write(I2C_ADAP, chip, 0, 0, &buf, 1);
212                         if (ret != 0) {
213                                 printf("i2c: mux diconnect error\n");
214                                 return ret;
215                         }
216                 } while (i > 0);
217         }
218
219         return 0;
220 }
221 #endif
222
223 /*
224  * i2c_init_bus():
225  * ---------------
226  *
227  * Initializes one bus. Will initialize the parent adapter. No current bus
228  * changes, no mux (if any) setup.
229  */
230 static void i2c_init_bus(unsigned int bus_no, int speed, int slaveaddr)
231 {
232         if (bus_no >= CONFIG_SYS_NUM_I2C_BUSES)
233                 return;
234
235         I2C_ADAP->init(I2C_ADAP, speed, slaveaddr);
236
237         if (gd->flags & GD_FLG_RELOC) {
238                 I2C_ADAP->init_done = 1;
239                 I2C_ADAP->speed = speed;
240                 I2C_ADAP->slaveaddr = slaveaddr;
241         }
242 }
243
244 /* implement possible board specific board init */
245 static void __def_i2c_init_board(void)
246 {
247 }
248 void i2c_init_board(void)
249         __attribute__((weak, alias("__def_i2c_init_board")));
250
251 /*
252  * i2c_init_all():
253  *
254  * not longer needed, will deleted. Actual init the SPD_BUS
255  * for compatibility.
256  * i2c_adap[] must be initialized beforehead with function pointers and
257  * data, including speed and slaveaddr.
258  */
259 void i2c_init_all(void)
260 {
261         i2c_init_board();
262         i2c_set_bus_num(CONFIG_SYS_SPD_BUS_NUM);
263         return;
264 }
265
266 /*
267  * i2c_get_bus_num():
268  * ------------------
269  *
270  *  Returns index of currently active I2C bus.  Zero-based.
271  */
272 unsigned int i2c_get_bus_num(void)
273 {
274         return gd->cur_i2c_bus;
275 }
276
277 /*
278  * i2c_set_bus_num():
279  * ------------------
280  *
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.
286  *
287  *      bus - bus index, zero based
288  *
289  *      Returns: 0 on success, not 0 on failure
290  */
291 int i2c_set_bus_num(unsigned int bus)
292 {
293         int max = ll_entry_count(struct i2c_adapter, i2c);
294
295         if (I2C_ADAPTER(bus) >= max) {
296                 printf("Error, wrong i2c adapter %d max %d possible\n",
297                        I2C_ADAPTER(bus), max);
298                 return -2;
299         }
300 #ifndef CONFIG_SYS_I2C_DIRECT_BUS
301         if (bus >= CONFIG_SYS_NUM_I2C_BUSES)
302                 return -1;
303 #endif
304
305         if ((bus == I2C_BUS) && (I2C_ADAP->init_done > 0))
306                 return 0;
307
308 #ifndef CONFIG_SYS_I2C_DIRECT_BUS
309         i2c_mux_disconnet_all();
310 #endif
311
312         gd->cur_i2c_bus = bus;
313         if (I2C_ADAP->init_done == 0)
314                 i2c_init_bus(bus, I2C_ADAP->speed, I2C_ADAP->slaveaddr);
315
316 #ifndef CONFIG_SYS_I2C_DIRECT_BUS
317         i2c_mux_set_all();
318 #endif
319         return 0;
320 }
321
322 /*
323  * Probe the given I2C chip address.  Returns 0 if a chip responded,
324  * not 0 on failure.
325  */
326 int i2c_probe(uint8_t chip)
327 {
328         return I2C_ADAP->probe(I2C_ADAP, chip);
329 }
330
331 /*
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
337  *              register)
338  *   buffer:  Where to read/write the data
339  *   len:     How many bytes to read/write
340  *
341  *   Returns: 0 on success, not 0 on failure
342  */
343 int i2c_read(uint8_t chip, unsigned int addr, int alen,
344                                 uint8_t *buffer, int len)
345 {
346         return I2C_ADAP->read(I2C_ADAP, chip, addr, alen, buffer, len);
347 }
348
349 int i2c_write(uint8_t chip, unsigned int addr, int alen,
350                                 uint8_t *buffer, int len)
351 {
352         return I2C_ADAP->write(I2C_ADAP, chip, addr, alen, buffer, len);
353 }
354
355 unsigned int i2c_set_bus_speed(unsigned int speed)
356 {
357         unsigned int ret;
358
359         if (I2C_ADAP->set_bus_speed == NULL)
360                 return 0;
361         ret = I2C_ADAP->set_bus_speed(I2C_ADAP, speed);
362         if (gd->flags & GD_FLG_RELOC)
363                 I2C_ADAP->speed = ret;
364
365         return ret;
366 }
367
368 unsigned int i2c_get_bus_speed(void)
369 {
370         struct i2c_adapter *cur = I2C_ADAP;
371         return cur->speed;
372 }
373
374 uint8_t i2c_reg_read(uint8_t addr, uint8_t reg)
375 {
376         uint8_t buf;
377
378 #ifdef CONFIG_8xx
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());
382 #endif
383         i2c_read(addr, reg, 1, &buf, 1);
384
385 #ifdef DEBUG
386         printf("%s: bus=%d addr=0x%02x, reg=0x%02x, val=0x%02x\n",
387                __func__, i2c_get_bus_num(), addr, reg, buf);
388 #endif
389
390         return buf;
391 }
392
393 void i2c_reg_write(uint8_t addr, uint8_t reg, uint8_t val)
394 {
395 #ifdef CONFIG_8xx
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());
399 #endif
400
401 #ifdef DEBUG
402         printf("%s: bus=%d addr=0x%02x, reg=0x%02x, val=0x%02x\n",
403                __func__, i2c_get_bus_num(), addr, reg, val);
404 #endif
405
406         i2c_write(addr, reg, 1, &val, 1);
407 }
408
409 void __i2c_init(int speed, int slaveaddr)
410 {
411         i2c_init_bus(i2c_get_bus_num(), speed, slaveaddr);
412 }
413 void i2c_init(int speed, int slaveaddr)
414         __attribute__((weak, alias("__i2c_init")));