i2c_core.c 7.7 KB

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