i2c-uclass.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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 dm_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 dm_i2c_write(struct udevice *dev, uint offset, const uint8_t *buffer,
  115. int len)
  116. {
  117. struct dm_i2c_chip *chip = dev_get_parentdata(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. ret = device_bind_driver(bus, "i2c_generic_chip_drv", str, &dev);
  202. debug("%s: device_bind_driver: ret=%d\n", __func__, ret);
  203. if (ret)
  204. goto err_bind;
  205. /* Tell the device what we know about it */
  206. memset(&chip, '\0', sizeof(chip));
  207. chip.chip_addr = chip_addr;
  208. chip.offset_len = offset_len;
  209. ret = device_probe_child(dev, &chip);
  210. debug("%s: device_probe_child: ret=%d\n", __func__, ret);
  211. if (ret)
  212. goto err_probe;
  213. *devp = dev;
  214. return 0;
  215. err_probe:
  216. device_unbind(dev);
  217. err_bind:
  218. free(str);
  219. return ret;
  220. }
  221. int i2c_get_chip(struct udevice *bus, uint chip_addr, uint offset_len,
  222. struct udevice **devp)
  223. {
  224. struct udevice *dev;
  225. debug("%s: Searching bus '%s' for address %02x: ", __func__,
  226. bus->name, chip_addr);
  227. for (device_find_first_child(bus, &dev); dev;
  228. device_find_next_child(&dev)) {
  229. struct dm_i2c_chip store;
  230. struct dm_i2c_chip *chip = dev_get_parentdata(dev);
  231. int ret;
  232. if (!chip) {
  233. chip = &store;
  234. i2c_chip_ofdata_to_platdata(gd->fdt_blob,
  235. dev->of_offset, chip);
  236. }
  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 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. /*
  302. * i2c_get_bus_speed:
  303. *
  304. * Returns speed of selected I2C bus in Hz
  305. */
  306. int i2c_get_bus_speed(struct udevice *bus)
  307. {
  308. struct dm_i2c_ops *ops = i2c_get_ops(bus);
  309. struct dm_i2c_bus *i2c = bus->uclass_priv;
  310. if (!ops->get_bus_speed)
  311. return i2c->speed_hz;
  312. return ops->get_bus_speed(bus);
  313. }
  314. int i2c_set_chip_flags(struct udevice *dev, uint flags)
  315. {
  316. struct udevice *bus = dev->parent;
  317. struct dm_i2c_chip *chip = dev_get_parentdata(dev);
  318. struct dm_i2c_ops *ops = i2c_get_ops(bus);
  319. int ret;
  320. if (ops->set_flags) {
  321. ret = ops->set_flags(dev, flags);
  322. if (ret)
  323. return ret;
  324. }
  325. chip->flags = flags;
  326. return 0;
  327. }
  328. int i2c_get_chip_flags(struct udevice *dev, uint *flagsp)
  329. {
  330. struct dm_i2c_chip *chip = dev_get_parentdata(dev);
  331. *flagsp = chip->flags;
  332. return 0;
  333. }
  334. int i2c_set_chip_offset_len(struct udevice *dev, uint offset_len)
  335. {
  336. struct dm_i2c_chip *chip = dev_get_parentdata(dev);
  337. if (offset_len > I2C_MAX_OFFSET_LEN)
  338. return -EINVAL;
  339. chip->offset_len = offset_len;
  340. return 0;
  341. }
  342. int i2c_deblock(struct udevice *bus)
  343. {
  344. struct dm_i2c_ops *ops = i2c_get_ops(bus);
  345. /*
  346. * We could implement a software deblocking here if we could get
  347. * access to the GPIOs used by I2C, and switch them to GPIO mode
  348. * and then back to I2C. This is somewhat beyond our powers in
  349. * driver model at present, so for now just fail.
  350. *
  351. * See https://patchwork.ozlabs.org/patch/399040/
  352. */
  353. if (!ops->deblock)
  354. return -ENOSYS;
  355. return ops->deblock(bus);
  356. }
  357. int i2c_chip_ofdata_to_platdata(const void *blob, int node,
  358. struct dm_i2c_chip *chip)
  359. {
  360. chip->offset_len = 1; /* default */
  361. chip->flags = 0;
  362. chip->chip_addr = fdtdec_get_int(gd->fdt_blob, node, "reg", -1);
  363. if (chip->chip_addr == -1) {
  364. debug("%s: I2C Node '%s' has no 'reg' property\n", __func__,
  365. fdt_get_name(blob, node, NULL));
  366. return -EINVAL;
  367. }
  368. return 0;
  369. }
  370. static int i2c_post_probe(struct udevice *dev)
  371. {
  372. struct dm_i2c_bus *i2c = dev->uclass_priv;
  373. i2c->speed_hz = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
  374. "clock-frequency", 100000);
  375. return i2c_set_bus_speed(dev, i2c->speed_hz);
  376. }
  377. int i2c_post_bind(struct udevice *dev)
  378. {
  379. /* Scan the bus for devices */
  380. return dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset, false);
  381. }
  382. UCLASS_DRIVER(i2c) = {
  383. .id = UCLASS_I2C,
  384. .name = "i2c",
  385. .per_device_auto_alloc_size = sizeof(struct dm_i2c_bus),
  386. .post_bind = i2c_post_bind,
  387. .post_probe = i2c_post_probe,
  388. };
  389. UCLASS_DRIVER(i2c_generic) = {
  390. .id = UCLASS_I2C_GENERIC,
  391. .name = "i2c_generic",
  392. };
  393. U_BOOT_DRIVER(i2c_generic_chip_drv) = {
  394. .name = "i2c_generic_chip_drv",
  395. .id = UCLASS_I2C_GENERIC,
  396. };