i2c_core.c 9.2 KB

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