i2c-uclass.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. /*
  2. * Copyright (c) 2014 Google, Inc
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <common.h>
  7. #include <dm.h>
  8. #include <errno.h>
  9. #include <fdtdec.h>
  10. #include <i2c.h>
  11. #include <malloc.h>
  12. #include <dm/device-internal.h>
  13. #include <dm/lists.h>
  14. #include <dm/root.h>
  15. DECLARE_GLOBAL_DATA_PTR;
  16. #define I2C_MAX_OFFSET_LEN 4
  17. /**
  18. * i2c_setup_offset() - Set up a new message with a chip offset
  19. *
  20. * @chip: Chip to use
  21. * @offset: Byte offset within chip
  22. * @offset_buf: Place to put byte offset
  23. * @msg: Message buffer
  24. * @return 0 if OK, -EADDRNOTAVAIL if the offset length is 0. In that case the
  25. * message is still set up but will not contain an offset.
  26. */
  27. static int i2c_setup_offset(struct dm_i2c_chip *chip, uint offset,
  28. uint8_t offset_buf[], struct i2c_msg *msg)
  29. {
  30. int offset_len;
  31. msg->addr = chip->chip_addr;
  32. msg->flags = chip->flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
  33. msg->len = chip->offset_len;
  34. msg->buf = offset_buf;
  35. if (!chip->offset_len)
  36. return -EADDRNOTAVAIL;
  37. assert(chip->offset_len <= I2C_MAX_OFFSET_LEN);
  38. offset_len = chip->offset_len;
  39. while (offset_len--)
  40. *offset_buf++ = offset >> (8 * offset_len);
  41. return 0;
  42. }
  43. static int i2c_read_bytewise(struct udevice *dev, uint offset,
  44. uint8_t *buffer, int len)
  45. {
  46. struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
  47. struct udevice *bus = dev_get_parent(dev);
  48. struct dm_i2c_ops *ops = i2c_get_ops(bus);
  49. struct i2c_msg msg[2], *ptr;
  50. uint8_t offset_buf[I2C_MAX_OFFSET_LEN];
  51. int ret;
  52. int i;
  53. for (i = 0; i < len; i++) {
  54. if (i2c_setup_offset(chip, offset + i, offset_buf, msg))
  55. return -EINVAL;
  56. ptr = msg + 1;
  57. ptr->addr = chip->chip_addr;
  58. ptr->flags = msg->flags | I2C_M_RD;
  59. ptr->len = 1;
  60. ptr->buf = &buffer[i];
  61. ptr++;
  62. ret = ops->xfer(bus, msg, ptr - msg);
  63. if (ret)
  64. return ret;
  65. }
  66. return 0;
  67. }
  68. static int i2c_write_bytewise(struct udevice *dev, uint offset,
  69. const uint8_t *buffer, int len)
  70. {
  71. struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
  72. struct udevice *bus = dev_get_parent(dev);
  73. struct dm_i2c_ops *ops = i2c_get_ops(bus);
  74. struct i2c_msg msg[1];
  75. uint8_t buf[I2C_MAX_OFFSET_LEN + 1];
  76. int ret;
  77. int i;
  78. for (i = 0; i < len; i++) {
  79. if (i2c_setup_offset(chip, offset + i, buf, msg))
  80. return -EINVAL;
  81. buf[msg->len++] = buffer[i];
  82. ret = ops->xfer(bus, msg, 1);
  83. if (ret)
  84. return ret;
  85. }
  86. return 0;
  87. }
  88. int dm_i2c_read(struct udevice *dev, uint offset, uint8_t *buffer, int len)
  89. {
  90. struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
  91. struct udevice *bus = dev_get_parent(dev);
  92. struct dm_i2c_ops *ops = i2c_get_ops(bus);
  93. struct i2c_msg msg[2], *ptr;
  94. uint8_t offset_buf[I2C_MAX_OFFSET_LEN];
  95. int msg_count;
  96. if (!ops->xfer)
  97. return -ENOSYS;
  98. if (chip->flags & DM_I2C_CHIP_RD_ADDRESS)
  99. return i2c_read_bytewise(dev, offset, buffer, len);
  100. ptr = msg;
  101. if (!i2c_setup_offset(chip, offset, offset_buf, ptr))
  102. ptr++;
  103. if (len) {
  104. ptr->addr = chip->chip_addr;
  105. ptr->flags = chip->flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
  106. ptr->flags |= I2C_M_RD;
  107. ptr->len = len;
  108. ptr->buf = buffer;
  109. ptr++;
  110. }
  111. msg_count = ptr - msg;
  112. return ops->xfer(bus, msg, msg_count);
  113. }
  114. int dm_i2c_write(struct udevice *dev, uint offset, const uint8_t *buffer,
  115. int len)
  116. {
  117. struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
  118. struct udevice *bus = dev_get_parent(dev);
  119. struct dm_i2c_ops *ops = i2c_get_ops(bus);
  120. struct i2c_msg msg[1];
  121. if (!ops->xfer)
  122. return -ENOSYS;
  123. if (chip->flags & DM_I2C_CHIP_WR_ADDRESS)
  124. return i2c_write_bytewise(dev, offset, buffer, len);
  125. /*
  126. * The simple approach would be to send two messages here: one to
  127. * set the offset and one to write the bytes. However some drivers
  128. * will not be expecting this, and some chips won't like how the
  129. * driver presents this on the I2C bus.
  130. *
  131. * The API does not support separate offset and data. We could extend
  132. * it with a flag indicating that there is data in the next message
  133. * that needs to be processed in the same transaction. We could
  134. * instead add an additional buffer to each message. For now, handle
  135. * this in the uclass since it isn't clear what the impact on drivers
  136. * would be with this extra complication. Unfortunately this means
  137. * copying the message.
  138. *
  139. * Use the stack for small messages, malloc() for larger ones. We
  140. * need to allow space for the offset (up to 4 bytes) and the message
  141. * itself.
  142. */
  143. if (len < 64) {
  144. uint8_t buf[I2C_MAX_OFFSET_LEN + len];
  145. i2c_setup_offset(chip, offset, buf, msg);
  146. msg->len += len;
  147. memcpy(buf + chip->offset_len, buffer, len);
  148. return ops->xfer(bus, msg, 1);
  149. } else {
  150. uint8_t *buf;
  151. int ret;
  152. buf = malloc(I2C_MAX_OFFSET_LEN + len);
  153. if (!buf)
  154. return -ENOMEM;
  155. i2c_setup_offset(chip, offset, buf, msg);
  156. msg->len += len;
  157. memcpy(buf + chip->offset_len, buffer, len);
  158. ret = ops->xfer(bus, msg, 1);
  159. free(buf);
  160. return ret;
  161. }
  162. }
  163. /**
  164. * i2c_probe_chip() - probe for a chip on a bus
  165. *
  166. * @bus: Bus to probe
  167. * @chip_addr: Chip address to probe
  168. * @flags: Flags for the chip
  169. * @return 0 if found, -ENOSYS if the driver is invalid, -EREMOTEIO if the chip
  170. * does not respond to probe
  171. */
  172. static int i2c_probe_chip(struct udevice *bus, uint chip_addr,
  173. enum dm_i2c_chip_flags chip_flags)
  174. {
  175. struct dm_i2c_ops *ops = i2c_get_ops(bus);
  176. struct i2c_msg msg[1];
  177. int ret;
  178. if (ops->probe_chip) {
  179. ret = ops->probe_chip(bus, chip_addr, chip_flags);
  180. if (!ret || ret != -ENOSYS)
  181. return ret;
  182. }
  183. if (!ops->xfer)
  184. return -ENOSYS;
  185. /* Probe with a zero-length message */
  186. msg->addr = chip_addr;
  187. msg->flags = chip_flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
  188. msg->len = 0;
  189. msg->buf = NULL;
  190. return ops->xfer(bus, msg, 1);
  191. }
  192. static int i2c_bind_driver(struct udevice *bus, uint chip_addr, uint offset_len,
  193. struct udevice **devp)
  194. {
  195. struct dm_i2c_chip *chip;
  196. char name[30], *str;
  197. struct udevice *dev;
  198. int ret;
  199. snprintf(name, sizeof(name), "generic_%x", chip_addr);
  200. str = strdup(name);
  201. if (!str)
  202. return -ENOMEM;
  203. ret = device_bind_driver(bus, "i2c_generic_chip_drv", str, &dev);
  204. debug("%s: device_bind_driver: ret=%d\n", __func__, ret);
  205. if (ret)
  206. goto err_bind;
  207. /* Tell the device what we know about it */
  208. chip = dev_get_parent_platdata(dev);
  209. chip->chip_addr = chip_addr;
  210. chip->offset_len = offset_len;
  211. ret = device_probe(dev);
  212. debug("%s: device_probe: ret=%d\n", __func__, ret);
  213. if (ret)
  214. goto err_probe;
  215. *devp = dev;
  216. return 0;
  217. err_probe:
  218. /*
  219. * If the device failed to probe, unbind it. There is nothing there
  220. * on the bus so we don't want to leave it lying around
  221. */
  222. device_unbind(dev);
  223. err_bind:
  224. free(str);
  225. return ret;
  226. }
  227. int i2c_get_chip(struct udevice *bus, uint chip_addr, uint offset_len,
  228. struct udevice **devp)
  229. {
  230. struct udevice *dev;
  231. debug("%s: Searching bus '%s' for address %02x: ", __func__,
  232. bus->name, chip_addr);
  233. for (device_find_first_child(bus, &dev); dev;
  234. device_find_next_child(&dev)) {
  235. struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
  236. int ret;
  237. if (chip->chip_addr == chip_addr) {
  238. ret = device_probe(dev);
  239. debug("found, ret=%d\n", ret);
  240. if (ret)
  241. return ret;
  242. *devp = dev;
  243. return 0;
  244. }
  245. }
  246. debug("not found\n");
  247. return i2c_bind_driver(bus, chip_addr, offset_len, devp);
  248. }
  249. int i2c_get_chip_for_busnum(int busnum, int chip_addr, uint offset_len,
  250. struct udevice **devp)
  251. {
  252. struct udevice *bus;
  253. int ret;
  254. ret = uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus);
  255. if (ret) {
  256. debug("Cannot find I2C bus %d\n", busnum);
  257. return ret;
  258. }
  259. ret = i2c_get_chip(bus, chip_addr, offset_len, devp);
  260. if (ret) {
  261. debug("Cannot find I2C chip %02x on bus %d\n", chip_addr,
  262. busnum);
  263. return ret;
  264. }
  265. return 0;
  266. }
  267. int dm_i2c_probe(struct udevice *bus, uint chip_addr, uint chip_flags,
  268. struct udevice **devp)
  269. {
  270. int ret;
  271. *devp = NULL;
  272. /* First probe that chip */
  273. ret = i2c_probe_chip(bus, chip_addr, chip_flags);
  274. debug("%s: bus='%s', address %02x, ret=%d\n", __func__, bus->name,
  275. chip_addr, ret);
  276. if (ret)
  277. return ret;
  278. /* The chip was found, see if we have a driver, and probe it */
  279. ret = i2c_get_chip(bus, chip_addr, 1, devp);
  280. debug("%s: i2c_get_chip: ret=%d\n", __func__, ret);
  281. return ret;
  282. }
  283. int dm_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
  284. {
  285. struct dm_i2c_ops *ops = i2c_get_ops(bus);
  286. struct dm_i2c_bus *i2c = bus->uclass_priv;
  287. int ret;
  288. /*
  289. * If we have a method, call it. If not then the driver probably wants
  290. * to deal with speed changes on the next transfer. It can easily read
  291. * the current speed from this uclass
  292. */
  293. if (ops->set_bus_speed) {
  294. ret = ops->set_bus_speed(bus, speed);
  295. if (ret)
  296. return ret;
  297. }
  298. i2c->speed_hz = speed;
  299. return 0;
  300. }
  301. int dm_i2c_get_bus_speed(struct udevice *bus)
  302. {
  303. struct dm_i2c_ops *ops = i2c_get_ops(bus);
  304. struct dm_i2c_bus *i2c = bus->uclass_priv;
  305. if (!ops->get_bus_speed)
  306. return i2c->speed_hz;
  307. return ops->get_bus_speed(bus);
  308. }
  309. int i2c_set_chip_flags(struct udevice *dev, uint flags)
  310. {
  311. struct udevice *bus = dev->parent;
  312. struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
  313. struct dm_i2c_ops *ops = i2c_get_ops(bus);
  314. int ret;
  315. if (ops->set_flags) {
  316. ret = ops->set_flags(dev, flags);
  317. if (ret)
  318. return ret;
  319. }
  320. chip->flags = flags;
  321. return 0;
  322. }
  323. int i2c_get_chip_flags(struct udevice *dev, uint *flagsp)
  324. {
  325. struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
  326. *flagsp = chip->flags;
  327. return 0;
  328. }
  329. int i2c_set_chip_offset_len(struct udevice *dev, uint offset_len)
  330. {
  331. struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
  332. if (offset_len > I2C_MAX_OFFSET_LEN)
  333. return -EINVAL;
  334. chip->offset_len = offset_len;
  335. return 0;
  336. }
  337. int i2c_deblock(struct udevice *bus)
  338. {
  339. struct dm_i2c_ops *ops = i2c_get_ops(bus);
  340. /*
  341. * We could implement a software deblocking here if we could get
  342. * access to the GPIOs used by I2C, and switch them to GPIO mode
  343. * and then back to I2C. This is somewhat beyond our powers in
  344. * driver model at present, so for now just fail.
  345. *
  346. * See https://patchwork.ozlabs.org/patch/399040/
  347. */
  348. if (!ops->deblock)
  349. return -ENOSYS;
  350. return ops->deblock(bus);
  351. }
  352. int i2c_chip_ofdata_to_platdata(const void *blob, int node,
  353. struct dm_i2c_chip *chip)
  354. {
  355. chip->offset_len = fdtdec_get_int(gd->fdt_blob, node,
  356. "u-boot,i2c-offset-len", 1);
  357. chip->flags = 0;
  358. chip->chip_addr = fdtdec_get_int(gd->fdt_blob, node, "reg", -1);
  359. if (chip->chip_addr == -1) {
  360. debug("%s: I2C Node '%s' has no 'reg' property\n", __func__,
  361. fdt_get_name(blob, node, NULL));
  362. return -EINVAL;
  363. }
  364. return 0;
  365. }
  366. static int i2c_post_probe(struct udevice *dev)
  367. {
  368. struct dm_i2c_bus *i2c = dev->uclass_priv;
  369. i2c->speed_hz = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
  370. "clock-frequency", 100000);
  371. return dm_i2c_set_bus_speed(dev, i2c->speed_hz);
  372. }
  373. static int i2c_post_bind(struct udevice *dev)
  374. {
  375. /* Scan the bus for devices */
  376. return dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset, false);
  377. }
  378. static int i2c_child_post_bind(struct udevice *dev)
  379. {
  380. struct dm_i2c_chip *plat = dev_get_parent_platdata(dev);
  381. if (dev->of_offset == -1)
  382. return 0;
  383. return i2c_chip_ofdata_to_platdata(gd->fdt_blob, dev->of_offset, plat);
  384. }
  385. UCLASS_DRIVER(i2c) = {
  386. .id = UCLASS_I2C,
  387. .name = "i2c",
  388. .flags = DM_UC_FLAG_SEQ_ALIAS,
  389. .post_bind = i2c_post_bind,
  390. .post_probe = i2c_post_probe,
  391. .per_device_auto_alloc_size = sizeof(struct dm_i2c_bus),
  392. .per_child_platdata_auto_alloc_size = sizeof(struct dm_i2c_chip),
  393. .child_post_bind = i2c_child_post_bind,
  394. };
  395. UCLASS_DRIVER(i2c_generic) = {
  396. .id = UCLASS_I2C_GENERIC,
  397. .name = "i2c_generic",
  398. };
  399. U_BOOT_DRIVER(i2c_generic_chip_drv) = {
  400. .name = "i2c_generic_chip_drv",
  401. .id = UCLASS_I2C_GENERIC,
  402. };