i2c-uclass.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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_parentdata(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_parentdata(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 i2c_read(struct udevice *dev, uint offset, uint8_t *buffer, int len)
  89. {
  90. struct dm_i2c_chip *chip = dev_get_parentdata(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 i2c_write(struct udevice *dev, uint offset, const uint8_t *buffer, int len)
  115. {
  116. struct dm_i2c_chip *chip = dev_get_parentdata(dev);
  117. struct udevice *bus = dev_get_parent(dev);
  118. struct dm_i2c_ops *ops = i2c_get_ops(bus);
  119. struct i2c_msg msg[1];
  120. if (!ops->xfer)
  121. return -ENOSYS;
  122. if (chip->flags & DM_I2C_CHIP_WR_ADDRESS)
  123. return i2c_write_bytewise(dev, offset, buffer, len);
  124. /*
  125. * The simple approach would be to send two messages here: one to
  126. * set the offset and one to write the bytes. However some drivers
  127. * will not be expecting this, and some chips won't like how the
  128. * driver presents this on the I2C bus.
  129. *
  130. * The API does not support separate offset and data. We could extend
  131. * it with a flag indicating that there is data in the next message
  132. * that needs to be processed in the same transaction. We could
  133. * instead add an additional buffer to each message. For now, handle
  134. * this in the uclass since it isn't clear what the impact on drivers
  135. * would be with this extra complication. Unfortunately this means
  136. * copying the message.
  137. *
  138. * Use the stack for small messages, malloc() for larger ones. We
  139. * need to allow space for the offset (up to 4 bytes) and the message
  140. * itself.
  141. */
  142. if (len < 64) {
  143. uint8_t buf[I2C_MAX_OFFSET_LEN + len];
  144. i2c_setup_offset(chip, offset, buf, msg);
  145. msg->len += len;
  146. memcpy(buf + chip->offset_len, buffer, len);
  147. return ops->xfer(bus, msg, 1);
  148. } else {
  149. uint8_t *buf;
  150. int ret;
  151. buf = malloc(I2C_MAX_OFFSET_LEN + len);
  152. if (!buf)
  153. return -ENOMEM;
  154. i2c_setup_offset(chip, offset, buf, msg);
  155. msg->len += len;
  156. memcpy(buf + chip->offset_len, buffer, len);
  157. ret = ops->xfer(bus, msg, 1);
  158. free(buf);
  159. return ret;
  160. }
  161. }
  162. /**
  163. * i2c_probe_chip() - probe for a chip on a bus
  164. *
  165. * @bus: Bus to probe
  166. * @chip_addr: Chip address to probe
  167. * @flags: Flags for the chip
  168. * @return 0 if found, -ENOSYS if the driver is invalid, -EREMOTEIO if the chip
  169. * does not respond to probe
  170. */
  171. static int i2c_probe_chip(struct udevice *bus, uint chip_addr,
  172. enum dm_i2c_chip_flags chip_flags)
  173. {
  174. struct dm_i2c_ops *ops = i2c_get_ops(bus);
  175. struct i2c_msg msg[1];
  176. int ret;
  177. if (ops->probe_chip) {
  178. ret = ops->probe_chip(bus, chip_addr, chip_flags);
  179. if (!ret || ret != -ENOSYS)
  180. return ret;
  181. }
  182. if (!ops->xfer)
  183. return -ENOSYS;
  184. /* Probe with a zero-length message */
  185. msg->addr = chip_addr;
  186. msg->flags = chip_flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
  187. msg->len = 0;
  188. msg->buf = NULL;
  189. return ops->xfer(bus, msg, 1);
  190. }
  191. static int i2c_bind_driver(struct udevice *bus, uint chip_addr,
  192. struct udevice **devp)
  193. {
  194. struct dm_i2c_chip chip;
  195. char name[30], *str;
  196. struct udevice *dev;
  197. int ret;
  198. snprintf(name, sizeof(name), "generic_%x", chip_addr);
  199. str = strdup(name);
  200. ret = device_bind_driver(bus, "i2c_generic_chip_drv", str, &dev);
  201. debug("%s: device_bind_driver: ret=%d\n", __func__, ret);
  202. if (ret)
  203. goto err_bind;
  204. /* Tell the device what we know about it */
  205. memset(&chip, '\0', sizeof(chip));
  206. chip.chip_addr = chip_addr;
  207. chip.offset_len = 1; /* we assume */
  208. ret = device_probe_child(dev, &chip);
  209. debug("%s: device_probe_child: ret=%d\n", __func__, ret);
  210. if (ret)
  211. goto err_probe;
  212. *devp = dev;
  213. return 0;
  214. err_probe:
  215. device_unbind(dev);
  216. err_bind:
  217. free(str);
  218. return ret;
  219. }
  220. int i2c_get_chip(struct udevice *bus, uint chip_addr, struct udevice **devp)
  221. {
  222. struct udevice *dev;
  223. debug("%s: Searching bus '%s' for address %02x: ", __func__,
  224. bus->name, chip_addr);
  225. for (device_find_first_child(bus, &dev); dev;
  226. device_find_next_child(&dev)) {
  227. struct dm_i2c_chip store;
  228. struct dm_i2c_chip *chip = dev_get_parentdata(dev);
  229. int ret;
  230. if (!chip) {
  231. chip = &store;
  232. i2c_chip_ofdata_to_platdata(gd->fdt_blob,
  233. dev->of_offset, chip);
  234. }
  235. if (chip->chip_addr == chip_addr) {
  236. ret = device_probe(dev);
  237. debug("found, ret=%d\n", ret);
  238. if (ret)
  239. return ret;
  240. *devp = dev;
  241. return 0;
  242. }
  243. }
  244. debug("not found\n");
  245. return i2c_bind_driver(bus, chip_addr, devp);
  246. }
  247. int i2c_get_chip_for_busnum(int busnum, int chip_addr, struct udevice **devp)
  248. {
  249. struct udevice *bus;
  250. int ret;
  251. ret = uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus);
  252. if (ret) {
  253. debug("Cannot find I2C bus %d\n", busnum);
  254. return ret;
  255. }
  256. ret = i2c_get_chip(bus, chip_addr, devp);
  257. if (ret) {
  258. debug("Cannot find I2C chip %02x on bus %d\n", chip_addr,
  259. busnum);
  260. return ret;
  261. }
  262. return 0;
  263. }
  264. int i2c_probe(struct udevice *bus, uint chip_addr, uint chip_flags,
  265. struct udevice **devp)
  266. {
  267. int ret;
  268. *devp = NULL;
  269. /* First probe that chip */
  270. ret = i2c_probe_chip(bus, chip_addr, chip_flags);
  271. debug("%s: bus='%s', address %02x, ret=%d\n", __func__, bus->name,
  272. chip_addr, ret);
  273. if (ret)
  274. return ret;
  275. /* The chip was found, see if we have a driver, and probe it */
  276. ret = i2c_get_chip(bus, chip_addr, devp);
  277. debug("%s: i2c_get_chip: ret=%d\n", __func__, ret);
  278. return ret;
  279. }
  280. int i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
  281. {
  282. struct dm_i2c_ops *ops = i2c_get_ops(bus);
  283. struct dm_i2c_bus *i2c = bus->uclass_priv;
  284. int ret;
  285. /*
  286. * If we have a method, call it. If not then the driver probably wants
  287. * to deal with speed changes on the next transfer. It can easily read
  288. * the current speed from this uclass
  289. */
  290. if (ops->set_bus_speed) {
  291. ret = ops->set_bus_speed(bus, speed);
  292. if (ret)
  293. return ret;
  294. }
  295. i2c->speed_hz = speed;
  296. return 0;
  297. }
  298. /*
  299. * i2c_get_bus_speed:
  300. *
  301. * Returns speed of selected I2C bus in Hz
  302. */
  303. int i2c_get_bus_speed(struct udevice *bus)
  304. {
  305. struct dm_i2c_ops *ops = i2c_get_ops(bus);
  306. struct dm_i2c_bus *i2c = bus->uclass_priv;
  307. if (!ops->get_bus_speed)
  308. return i2c->speed_hz;
  309. return ops->get_bus_speed(bus);
  310. }
  311. int i2c_set_chip_flags(struct udevice *dev, uint flags)
  312. {
  313. struct udevice *bus = dev->parent;
  314. struct dm_i2c_chip *chip = dev_get_parentdata(dev);
  315. struct dm_i2c_ops *ops = i2c_get_ops(bus);
  316. int ret;
  317. if (ops->set_flags) {
  318. ret = ops->set_flags(dev, flags);
  319. if (ret)
  320. return ret;
  321. }
  322. chip->flags = flags;
  323. return 0;
  324. }
  325. int i2c_get_chip_flags(struct udevice *dev, uint *flagsp)
  326. {
  327. struct dm_i2c_chip *chip = dev_get_parentdata(dev);
  328. *flagsp = chip->flags;
  329. return 0;
  330. }
  331. int i2c_set_chip_offset_len(struct udevice *dev, uint offset_len)
  332. {
  333. struct dm_i2c_chip *chip = dev_get_parentdata(dev);
  334. if (offset_len > I2C_MAX_OFFSET_LEN)
  335. return -EINVAL;
  336. chip->offset_len = offset_len;
  337. return 0;
  338. }
  339. int i2c_deblock(struct udevice *bus)
  340. {
  341. struct dm_i2c_ops *ops = i2c_get_ops(bus);
  342. /*
  343. * We could implement a software deblocking here if we could get
  344. * access to the GPIOs used by I2C, and switch them to GPIO mode
  345. * and then back to I2C. This is somewhat beyond our powers in
  346. * driver model at present, so for now just fail.
  347. *
  348. * See https://patchwork.ozlabs.org/patch/399040/
  349. */
  350. if (!ops->deblock)
  351. return -ENOSYS;
  352. return ops->deblock(bus);
  353. }
  354. int i2c_chip_ofdata_to_platdata(const void *blob, int node,
  355. struct dm_i2c_chip *chip)
  356. {
  357. chip->offset_len = 1; /* default */
  358. chip->flags = 0;
  359. chip->chip_addr = fdtdec_get_int(gd->fdt_blob, node, "reg", -1);
  360. if (chip->chip_addr == -1) {
  361. debug("%s: I2C Node '%s' has no 'reg' property\n", __func__,
  362. fdt_get_name(blob, node, NULL));
  363. return -EINVAL;
  364. }
  365. return 0;
  366. }
  367. static int i2c_post_probe(struct udevice *dev)
  368. {
  369. struct dm_i2c_bus *i2c = dev->uclass_priv;
  370. i2c->speed_hz = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
  371. "clock-frequency", 100000);
  372. return i2c_set_bus_speed(dev, i2c->speed_hz);
  373. }
  374. int i2c_post_bind(struct udevice *dev)
  375. {
  376. /* Scan the bus for devices */
  377. return dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset, false);
  378. }
  379. UCLASS_DRIVER(i2c) = {
  380. .id = UCLASS_I2C,
  381. .name = "i2c",
  382. .per_device_auto_alloc_size = sizeof(struct dm_i2c_bus),
  383. .post_bind = i2c_post_bind,
  384. .post_probe = i2c_post_probe,
  385. };
  386. UCLASS_DRIVER(i2c_generic) = {
  387. .id = UCLASS_I2C_GENERIC,
  388. .name = "i2c_generic",
  389. };
  390. U_BOOT_DRIVER(i2c_generic_chip_drv) = {
  391. .name = "i2c_generic_chip_drv",
  392. .id = UCLASS_I2C_GENERIC,
  393. };