i2c_core.c 7.7 KB

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