i2c_core.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. /* adapter itself */
  48. addr = (unsigned long)i2c_adap_p;
  49. addr += gd->reloc_off;
  50. i2c_adap_p = (struct i2c_adapter *)addr;
  51. /* i2c_init() */
  52. addr = (unsigned long)i2c_adap_p->init;
  53. addr += gd->reloc_off;
  54. i2c_adap_p->init = (void (*)(int, int))addr;
  55. /* i2c_probe() */
  56. addr = (unsigned long)i2c_adap_p->probe;
  57. addr += gd->reloc_off;
  58. i2c_adap_p->probe = (int (*)(uint8_t))addr;
  59. /* i2c_read() */
  60. addr = (unsigned long)i2c_adap_p->read;
  61. addr += gd->reloc_off;
  62. i2c_adap_p->read = (int (*)(uint8_t, uint, int, uint8_t *,
  63. int))addr;
  64. /* i2c_write() */
  65. addr = (unsigned long)i2c_adap_p->write;
  66. addr += gd->reloc_off;
  67. i2c_adap_p->write = (int (*)(uint8_t, uint, int, uint8_t *,
  68. int))addr;
  69. /* i2c_set_bus_speed() */
  70. addr = (unsigned long)i2c_adap_p->set_bus_speed;
  71. addr += gd->reloc_off;
  72. i2c_adap_p->set_bus_speed = (uint (*)(uint))addr;
  73. /* name */
  74. addr = (unsigned long)i2c_adap_p->name;
  75. addr += gd->reloc_off;
  76. i2c_adap_p->name = (char *)addr;
  77. tmp++;
  78. i2c_adap_p = tmp;
  79. }
  80. #endif
  81. }
  82. #ifndef CONFIG_SYS_I2C_DIRECT_BUS
  83. /*
  84. * i2c_mux_set()
  85. * -------------
  86. *
  87. * This turns on the given channel on I2C multiplexer chip connected to
  88. * a given I2C adapter directly or via other multiplexers. In the latter
  89. * case the entire multiplexer chain must be initialized first starting
  90. * with the one connected directly to the adapter. When disabling a chain
  91. * muxes must be programmed in reverse order, starting with the one
  92. * farthest from the adapter.
  93. *
  94. * mux_id is the multiplexer chip type from defined in i2c.h. So far only
  95. * NXP (Philips) PCA954x multiplexers are supported. Switches are NOT
  96. * supported (anybody uses them?)
  97. */
  98. static int i2c_mux_set(struct i2c_adapter *adap, int mux_id, int chip,
  99. int channel)
  100. {
  101. uint8_t buf;
  102. int ret;
  103. /* channel < 0 - turn off the mux */
  104. if (channel < 0) {
  105. buf = 0;
  106. ret = adap->write(adap, chip, 0, 0, &buf, 1);
  107. if (ret)
  108. printf("%s: Could not turn off the mux.\n", __func__);
  109. return ret;
  110. }
  111. switch (mux_id) {
  112. case I2C_MUX_PCA9540_ID:
  113. case I2C_MUX_PCA9542_ID:
  114. if (channel > 1)
  115. return -1;
  116. buf = (uint8_t)((channel & 0x01) | (1 << 2));
  117. break;
  118. case I2C_MUX_PCA9544_ID:
  119. if (channel > 3)
  120. return -1;
  121. buf = (uint8_t)((channel & 0x03) | (1 << 2));
  122. break;
  123. case I2C_MUX_PCA9547_ID:
  124. if (channel > 7)
  125. return -1;
  126. buf = (uint8_t)((channel & 0x07) | (1 << 3));
  127. break;
  128. default:
  129. printf("%s: wrong mux id: %d\n", __func__, mux_id);
  130. return -1;
  131. }
  132. ret = adap->write(adap, chip, 0, 0, &buf, 1);
  133. if (ret)
  134. printf("%s: could not set mux: id: %d chip: %x channel: %d\n",
  135. __func__, mux_id, chip, channel);
  136. return ret;
  137. }
  138. static int i2c_mux_set_all(void)
  139. {
  140. struct i2c_bus_hose *i2c_bus_tmp = &i2c_bus[I2C_BUS];
  141. int i;
  142. /* Connect requested bus if behind muxes */
  143. if (i2c_bus_tmp->next_hop[0].chip != 0) {
  144. /* Set all muxes along the path to that bus */
  145. for (i = 0; i < CONFIG_SYS_I2C_MAX_HOPS; i++) {
  146. int ret;
  147. if (i2c_bus_tmp->next_hop[i].chip == 0)
  148. break;
  149. ret = i2c_mux_set(I2C_ADAP,
  150. i2c_bus_tmp->next_hop[i].mux.id,
  151. i2c_bus_tmp->next_hop[i].chip,
  152. i2c_bus_tmp->next_hop[i].channel);
  153. if (ret != 0)
  154. return ret;
  155. }
  156. }
  157. return 0;
  158. }
  159. static int i2c_mux_disconnet_all(void)
  160. {
  161. struct i2c_bus_hose *i2c_bus_tmp = &i2c_bus[I2C_BUS];
  162. int i;
  163. uint8_t buf;
  164. if (I2C_ADAP->init_done == 0)
  165. return 0;
  166. /* Disconnect current bus (turn off muxes if any) */
  167. if ((i2c_bus_tmp->next_hop[0].chip != 0) &&
  168. (I2C_ADAP->init_done != 0)) {
  169. i = CONFIG_SYS_I2C_MAX_HOPS;
  170. do {
  171. uint8_t chip;
  172. int ret;
  173. chip = i2c_bus_tmp->next_hop[--i].chip;
  174. if (chip == 0)
  175. continue;
  176. ret = I2C_ADAP->write(I2C_ADAP, chip, 0, 0, &buf, 1);
  177. if (ret != 0) {
  178. printf("i2c: mux diconnect error\n");
  179. return ret;
  180. }
  181. } while (i > 0);
  182. }
  183. return 0;
  184. }
  185. #endif
  186. /*
  187. * i2c_init_bus():
  188. * ---------------
  189. *
  190. * Initializes one bus. Will initialize the parent adapter. No current bus
  191. * changes, no mux (if any) setup.
  192. */
  193. static void i2c_init_bus(unsigned int bus_no, int speed, int slaveaddr)
  194. {
  195. if (bus_no >= CONFIG_SYS_NUM_I2C_BUSES)
  196. return;
  197. I2C_ADAP->init(I2C_ADAP, speed, slaveaddr);
  198. if (gd->flags & GD_FLG_RELOC) {
  199. I2C_ADAP->init_done = 1;
  200. I2C_ADAP->speed = speed;
  201. I2C_ADAP->slaveaddr = slaveaddr;
  202. }
  203. }
  204. /* implement possible board specific board init */
  205. static void __def_i2c_init_board(void)
  206. {
  207. }
  208. void i2c_init_board(void)
  209. __attribute__((weak, alias("__def_i2c_init_board")));
  210. /*
  211. * i2c_init_all():
  212. *
  213. * not longer needed, will deleted. Actual init the SPD_BUS
  214. * for compatibility.
  215. * i2c_adap[] must be initialized beforehead with function pointers and
  216. * data, including speed and slaveaddr.
  217. */
  218. void i2c_init_all(void)
  219. {
  220. i2c_init_board();
  221. i2c_set_bus_num(CONFIG_SYS_SPD_BUS_NUM);
  222. return;
  223. }
  224. /*
  225. * i2c_get_bus_num():
  226. * ------------------
  227. *
  228. * Returns index of currently active I2C bus. Zero-based.
  229. */
  230. unsigned int i2c_get_bus_num(void)
  231. {
  232. return gd->cur_i2c_bus;
  233. }
  234. /*
  235. * i2c_set_bus_num():
  236. * ------------------
  237. *
  238. * Change the active I2C bus. Subsequent read/write calls will
  239. * go to this one. Sets all of the muxes in a proper condition
  240. * if that bus is behind muxes.
  241. * If previously selected bus is behind the muxes turns off all the
  242. * muxes along the path to that bus.
  243. *
  244. * bus - bus index, zero based
  245. *
  246. * Returns: 0 on success, not 0 on failure
  247. */
  248. int i2c_set_bus_num(unsigned int bus)
  249. {
  250. int max = ll_entry_count(struct i2c_adapter, i2c);
  251. if (I2C_ADAPTER(bus) >= max) {
  252. printf("Error, wrong i2c adapter %d max %d possible\n",
  253. I2C_ADAPTER(bus), max);
  254. return -2;
  255. }
  256. #ifndef CONFIG_SYS_I2C_DIRECT_BUS
  257. if (bus >= CONFIG_SYS_NUM_I2C_BUSES)
  258. return -1;
  259. #endif
  260. if ((bus == I2C_BUS) && (I2C_ADAP->init_done > 0))
  261. return 0;
  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;
  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")));