i2c_core.c 7.7 KB

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