b263562edf268ac16342f2906f64a013ec237f08
[platform/kernel/u-boot.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  * 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         case I2C_MUX_PCA9548_ID:
142                 if (channel > 7)
143                         return -1;
144                 buf = (uint8_t)(0x01 << channel);
145                 break;
146         default:
147                 printf("%s: wrong mux id: %d\n", __func__, mux_id);
148                 return -1;
149         }
150
151         ret = adap->write(adap, chip, 0, 0, &buf, 1);
152         if (ret)
153                 printf("%s: could not set mux: id: %d chip: %x channel: %d\n",
154                        __func__, mux_id, chip, channel);
155         return ret;
156 }
157
158 static int i2c_mux_set_all(void)
159 {
160         struct i2c_bus_hose *i2c_bus_tmp = &i2c_bus[I2C_BUS];
161         int i;
162
163         /* Connect requested bus if behind muxes */
164         if (i2c_bus_tmp->next_hop[0].chip != 0) {
165                 /* Set all muxes along the path to that bus */
166                 for (i = 0; i < CONFIG_SYS_I2C_MAX_HOPS; i++) {
167                         int     ret;
168
169                         if (i2c_bus_tmp->next_hop[i].chip == 0)
170                                 break;
171
172                         ret = i2c_mux_set(I2C_ADAP,
173                                         i2c_bus_tmp->next_hop[i].mux.id,
174                                         i2c_bus_tmp->next_hop[i].chip,
175                                         i2c_bus_tmp->next_hop[i].channel);
176                         if (ret != 0)
177                                 return ret;
178                 }
179         }
180         return 0;
181 }
182
183 static int i2c_mux_disconnet_all(void)
184 {
185         struct  i2c_bus_hose *i2c_bus_tmp = &i2c_bus[I2C_BUS];
186         int     i;
187         uint8_t buf;
188
189         if (I2C_ADAP->init_done == 0)
190                 return 0;
191
192         /* Disconnect current bus (turn off muxes if any) */
193         if ((i2c_bus_tmp->next_hop[0].chip != 0) &&
194             (I2C_ADAP->init_done != 0)) {
195                 i = CONFIG_SYS_I2C_MAX_HOPS;
196                 do {
197                         uint8_t chip;
198                         int ret;
199
200                         chip = i2c_bus_tmp->next_hop[--i].chip;
201                         if (chip == 0)
202                                 continue;
203
204                         ret = I2C_ADAP->write(I2C_ADAP, chip, 0, 0, &buf, 1);
205                         if (ret != 0) {
206                                 printf("i2c: mux diconnect error\n");
207                                 return ret;
208                         }
209                 } while (i > 0);
210         }
211
212         return 0;
213 }
214 #endif
215
216 /*
217  * i2c_init_bus():
218  * ---------------
219  *
220  * Initializes one bus. Will initialize the parent adapter. No current bus
221  * changes, no mux (if any) setup.
222  */
223 static void i2c_init_bus(unsigned int bus_no, int speed, int slaveaddr)
224 {
225         if (bus_no >= CONFIG_SYS_NUM_I2C_BUSES)
226                 return;
227
228         I2C_ADAP->init(I2C_ADAP, speed, slaveaddr);
229
230         if (gd->flags & GD_FLG_RELOC) {
231                 I2C_ADAP->init_done = 1;
232                 I2C_ADAP->speed = speed;
233                 I2C_ADAP->slaveaddr = slaveaddr;
234         }
235 }
236
237 /* implement possible board specific board init */
238 static void __def_i2c_init_board(void)
239 {
240 }
241 void i2c_init_board(void)
242         __attribute__((weak, alias("__def_i2c_init_board")));
243
244 /*
245  * i2c_init_all():
246  *
247  * not longer needed, will deleted. Actual init the SPD_BUS
248  * for compatibility.
249  * i2c_adap[] must be initialized beforehead with function pointers and
250  * data, including speed and slaveaddr.
251  */
252 void i2c_init_all(void)
253 {
254         i2c_init_board();
255         i2c_set_bus_num(CONFIG_SYS_SPD_BUS_NUM);
256         return;
257 }
258
259 /*
260  * i2c_get_bus_num():
261  * ------------------
262  *
263  *  Returns index of currently active I2C bus.  Zero-based.
264  */
265 unsigned int i2c_get_bus_num(void)
266 {
267         return gd->cur_i2c_bus;
268 }
269
270 /*
271  * i2c_set_bus_num():
272  * ------------------
273  *
274  *  Change the active I2C bus. Subsequent read/write calls will
275  *  go to this one. Sets all of the muxes in a proper condition
276  *  if that bus is behind muxes.
277  *  If previously selected bus is behind the muxes turns off all the
278  *  muxes along the path to that bus.
279  *
280  *      bus - bus index, zero based
281  *
282  *      Returns: 0 on success, not 0 on failure
283  */
284 int i2c_set_bus_num(unsigned int bus)
285 {
286         int max = ll_entry_count(struct i2c_adapter, i2c);
287
288         if (I2C_ADAPTER(bus) >= max) {
289                 printf("Error, wrong i2c adapter %d max %d possible\n",
290                        I2C_ADAPTER(bus), max);
291                 return -2;
292         }
293 #ifndef CONFIG_SYS_I2C_DIRECT_BUS
294         if (bus >= CONFIG_SYS_NUM_I2C_BUSES)
295                 return -1;
296 #endif
297
298         if ((bus == I2C_BUS) && (I2C_ADAP->init_done > 0))
299                 return 0;
300
301 #ifndef CONFIG_SYS_I2C_DIRECT_BUS
302         i2c_mux_disconnet_all();
303 #endif
304
305         gd->cur_i2c_bus = bus;
306         if (I2C_ADAP->init_done == 0)
307                 i2c_init_bus(bus, I2C_ADAP->speed, I2C_ADAP->slaveaddr);
308
309 #ifndef CONFIG_SYS_I2C_DIRECT_BUS
310         i2c_mux_set_all();
311 #endif
312         return 0;
313 }
314
315 /*
316  * Probe the given I2C chip address.  Returns 0 if a chip responded,
317  * not 0 on failure.
318  */
319 int i2c_probe(uint8_t chip)
320 {
321         return I2C_ADAP->probe(I2C_ADAP, chip);
322 }
323
324 /*
325  * Read/Write interface:
326  *   chip:    I2C chip address, range 0..127
327  *   addr:    Memory (register) address within the chip
328  *   alen:    Number of bytes to use for addr (typically 1, 2 for larger
329  *              memories, 0 for register type devices with only one
330  *              register)
331  *   buffer:  Where to read/write the data
332  *   len:     How many bytes to read/write
333  *
334  *   Returns: 0 on success, not 0 on failure
335  */
336 int i2c_read(uint8_t chip, unsigned int addr, int alen,
337                                 uint8_t *buffer, int len)
338 {
339         return I2C_ADAP->read(I2C_ADAP, chip, addr, alen, buffer, len);
340 }
341
342 int i2c_write(uint8_t chip, unsigned int addr, int alen,
343                                 uint8_t *buffer, int len)
344 {
345         return I2C_ADAP->write(I2C_ADAP, chip, addr, alen, buffer, len);
346 }
347
348 unsigned int i2c_set_bus_speed(unsigned int speed)
349 {
350         unsigned int ret;
351
352         if (I2C_ADAP->set_bus_speed == NULL)
353                 return 0;
354         ret = I2C_ADAP->set_bus_speed(I2C_ADAP, speed);
355         if (gd->flags & GD_FLG_RELOC)
356                 I2C_ADAP->speed = ret;
357
358         return ret;
359 }
360
361 unsigned int i2c_get_bus_speed(void)
362 {
363         struct i2c_adapter *cur = I2C_ADAP;
364         return cur->speed;
365 }
366
367 uint8_t i2c_reg_read(uint8_t addr, uint8_t reg)
368 {
369         uint8_t buf;
370
371 #ifdef CONFIG_8xx
372         /* MPC8xx needs this.  Maybe one day we can get rid of it. */
373         /* maybe it is now the time for it ... */
374         i2c_set_bus_num(i2c_get_bus_num());
375 #endif
376         i2c_read(addr, reg, 1, &buf, 1);
377
378 #ifdef DEBUG
379         printf("%s: bus=%d addr=0x%02x, reg=0x%02x, val=0x%02x\n",
380                __func__, i2c_get_bus_num(), addr, reg, buf);
381 #endif
382
383         return buf;
384 }
385
386 void i2c_reg_write(uint8_t addr, uint8_t reg, uint8_t val)
387 {
388 #ifdef CONFIG_8xx
389         /* MPC8xx needs this.  Maybe one day we can get rid of it. */
390         /* maybe it is now the time for it ... */
391         i2c_set_bus_num(i2c_get_bus_num());
392 #endif
393
394 #ifdef DEBUG
395         printf("%s: bus=%d addr=0x%02x, reg=0x%02x, val=0x%02x\n",
396                __func__, i2c_get_bus_num(), addr, reg, val);
397 #endif
398
399         i2c_write(addr, reg, 1, &val, 1);
400 }
401
402 void __i2c_init(int speed, int slaveaddr)
403 {
404         i2c_init_bus(i2c_get_bus_num(), speed, slaveaddr);
405 }
406 void i2c_init(int speed, int slaveaddr)
407         __attribute__((weak, alias("__i2c_init")));