i2c_core.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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 = 0;
  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. __weak void i2c_init_board(void)
  205. {
  206. }
  207. /*
  208. * i2c_init_all():
  209. *
  210. * not longer needed, will deleted. Actual init the SPD_BUS
  211. * for compatibility.
  212. * i2c_adap[] must be initialized beforehead with function pointers and
  213. * data, including speed and slaveaddr.
  214. */
  215. void i2c_init_all(void)
  216. {
  217. i2c_init_board();
  218. i2c_set_bus_num(CONFIG_SYS_SPD_BUS_NUM);
  219. return;
  220. }
  221. /*
  222. * i2c_get_bus_num():
  223. * ------------------
  224. *
  225. * Returns index of currently active I2C bus. Zero-based.
  226. */
  227. unsigned int i2c_get_bus_num(void)
  228. {
  229. return gd->cur_i2c_bus;
  230. }
  231. /*
  232. * i2c_set_bus_num():
  233. * ------------------
  234. *
  235. * Change the active I2C bus. Subsequent read/write calls will
  236. * go to this one. Sets all of the muxes in a proper condition
  237. * if that bus is behind muxes.
  238. * If previously selected bus is behind the muxes turns off all the
  239. * muxes along the path to that bus.
  240. *
  241. * bus - bus index, zero based
  242. *
  243. * Returns: 0 on success, not 0 on failure
  244. */
  245. int i2c_set_bus_num(unsigned int bus)
  246. {
  247. int max;
  248. if ((bus == I2C_BUS) && (I2C_ADAP->init_done > 0))
  249. return 0;
  250. #ifndef CONFIG_SYS_I2C_DIRECT_BUS
  251. if (bus >= CONFIG_SYS_NUM_I2C_BUSES)
  252. return -1;
  253. #endif
  254. max = ll_entry_count(struct i2c_adapter, i2c);
  255. if (I2C_ADAPTER(bus) >= max) {
  256. printf("Error, wrong i2c adapter %d max %d possible\n",
  257. I2C_ADAPTER(bus), max);
  258. return -2;
  259. }
  260. #ifndef CONFIG_SYS_I2C_DIRECT_BUS
  261. i2c_mux_disconnet_all();
  262. #endif
  263. gd->cur_i2c_bus = bus;
  264. if (I2C_ADAP->init_done == 0)
  265. i2c_init_bus(bus, I2C_ADAP->speed, I2C_ADAP->slaveaddr);
  266. #ifndef CONFIG_SYS_I2C_DIRECT_BUS
  267. i2c_mux_set_all();
  268. #endif
  269. return 0;
  270. }
  271. /*
  272. * Probe the given I2C chip address. Returns 0 if a chip responded,
  273. * not 0 on failure.
  274. */
  275. int i2c_probe(uint8_t chip)
  276. {
  277. return I2C_ADAP->probe(I2C_ADAP, chip);
  278. }
  279. /*
  280. * Read/Write interface:
  281. * chip: I2C chip address, range 0..127
  282. * addr: Memory (register) address within the chip
  283. * alen: Number of bytes to use for addr (typically 1, 2 for larger
  284. * memories, 0 for register type devices with only one
  285. * register)
  286. * buffer: Where to read/write the data
  287. * len: How many bytes to read/write
  288. *
  289. * Returns: 0 on success, not 0 on failure
  290. */
  291. int i2c_read(uint8_t chip, unsigned int addr, int alen,
  292. uint8_t *buffer, int len)
  293. {
  294. return I2C_ADAP->read(I2C_ADAP, chip, addr, alen, buffer, len);
  295. }
  296. int i2c_write(uint8_t chip, unsigned int addr, int alen,
  297. uint8_t *buffer, int len)
  298. {
  299. return I2C_ADAP->write(I2C_ADAP, chip, addr, alen, buffer, len);
  300. }
  301. unsigned int i2c_set_bus_speed(unsigned int speed)
  302. {
  303. unsigned int ret;
  304. if (I2C_ADAP->set_bus_speed == NULL)
  305. return 0;
  306. ret = I2C_ADAP->set_bus_speed(I2C_ADAP, speed);
  307. if (gd->flags & GD_FLG_RELOC)
  308. I2C_ADAP->speed = (ret == 0) ? speed : 0;
  309. return ret;
  310. }
  311. unsigned int i2c_get_bus_speed(void)
  312. {
  313. struct i2c_adapter *cur = I2C_ADAP;
  314. return cur->speed;
  315. }
  316. uint8_t i2c_reg_read(uint8_t addr, uint8_t reg)
  317. {
  318. uint8_t buf;
  319. #ifdef CONFIG_8xx
  320. /* MPC8xx needs this. Maybe one day we can get rid of it. */
  321. /* maybe it is now the time for it ... */
  322. i2c_set_bus_num(i2c_get_bus_num());
  323. #endif
  324. i2c_read(addr, reg, 1, &buf, 1);
  325. #ifdef DEBUG
  326. printf("%s: bus=%d addr=0x%02x, reg=0x%02x, val=0x%02x\n",
  327. __func__, i2c_get_bus_num(), addr, reg, buf);
  328. #endif
  329. return buf;
  330. }
  331. void i2c_reg_write(uint8_t addr, uint8_t reg, uint8_t val)
  332. {
  333. #ifdef CONFIG_8xx
  334. /* MPC8xx needs this. Maybe one day we can get rid of it. */
  335. /* maybe it is now the time for it ... */
  336. i2c_set_bus_num(i2c_get_bus_num());
  337. #endif
  338. #ifdef DEBUG
  339. printf("%s: bus=%d addr=0x%02x, reg=0x%02x, val=0x%02x\n",
  340. __func__, i2c_get_bus_num(), addr, reg, val);
  341. #endif
  342. i2c_write(addr, reg, 1, &val, 1);
  343. }
  344. __weak void i2c_init(int speed, int slaveaddr)
  345. {
  346. i2c_init_bus(i2c_get_bus_num(), speed, slaveaddr);
  347. }