i2c-uclass.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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. 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. chip = dev_get_parent_platdata(dev);
  207. chip->chip_addr = chip_addr;
  208. chip->offset_len = offset_len;
  209. ret = device_probe(dev);
  210. debug("%s: device_probe: ret=%d\n", __func__, ret);
  211. if (ret)
  212. goto err_probe;
  213. *devp = dev;
  214. return 0;
  215. err_probe:
  216. /*
  217. * If the device failed to probe, unbind it. There is nothing there
  218. * on the bus so we don't want to leave it lying around
  219. */
  220. device_unbind(dev);
  221. err_bind:
  222. free(str);
  223. return ret;
  224. }
  225. int i2c_get_chip(struct udevice *bus, uint chip_addr, uint offset_len,
  226. struct udevice **devp)
  227. {
  228. struct udevice *dev;
  229. debug("%s: Searching bus '%s' for address %02x: ", __func__,
  230. bus->name, chip_addr);
  231. for (device_find_first_child(bus, &dev); dev;
  232. device_find_next_child(&dev)) {
  233. struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
  234. int ret;
  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, offset_len, devp);
  246. }
  247. int i2c_get_chip_for_busnum(int busnum, int chip_addr, uint offset_len,
  248. struct udevice **devp)
  249. {
  250. struct udevice *bus;
  251. int ret;
  252. ret = uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus);
  253. if (ret) {
  254. debug("Cannot find I2C bus %d\n", busnum);
  255. return ret;
  256. }
  257. ret = i2c_get_chip(bus, chip_addr, offset_len, devp);
  258. if (ret) {
  259. debug("Cannot find I2C chip %02x on bus %d\n", chip_addr,
  260. busnum);
  261. return ret;
  262. }
  263. return 0;
  264. }
  265. int dm_i2c_probe(struct udevice *bus, uint chip_addr, uint chip_flags,
  266. struct udevice **devp)
  267. {
  268. int ret;
  269. *devp = NULL;
  270. /* First probe that chip */
  271. ret = i2c_probe_chip(bus, chip_addr, chip_flags);
  272. debug("%s: bus='%s', address %02x, ret=%d\n", __func__, bus->name,
  273. chip_addr, ret);
  274. if (ret)
  275. return ret;
  276. /* The chip was found, see if we have a driver, and probe it */
  277. ret = i2c_get_chip(bus, chip_addr, 1, devp);
  278. debug("%s: i2c_get_chip: ret=%d\n", __func__, ret);
  279. return ret;
  280. }
  281. int i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
  282. {
  283. struct dm_i2c_ops *ops = i2c_get_ops(bus);
  284. struct dm_i2c_bus *i2c = bus->uclass_priv;
  285. int ret;
  286. /*
  287. * If we have a method, call it. If not then the driver probably wants
  288. * to deal with speed changes on the next transfer. It can easily read
  289. * the current speed from this uclass
  290. */
  291. if (ops->set_bus_speed) {
  292. ret = ops->set_bus_speed(bus, speed);
  293. if (ret)
  294. return ret;
  295. }
  296. i2c->speed_hz = speed;
  297. return 0;
  298. }
  299. /*
  300. * i2c_get_bus_speed:
  301. *
  302. * Returns speed of selected I2C bus in Hz
  303. */
  304. int i2c_get_bus_speed(struct udevice *bus)
  305. {
  306. struct dm_i2c_ops *ops = i2c_get_ops(bus);
  307. struct dm_i2c_bus *i2c = bus->uclass_priv;
  308. if (!ops->get_bus_speed)
  309. return i2c->speed_hz;
  310. return ops->get_bus_speed(bus);
  311. }
  312. int i2c_set_chip_flags(struct udevice *dev, uint flags)
  313. {
  314. struct udevice *bus = dev->parent;
  315. struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
  316. struct dm_i2c_ops *ops = i2c_get_ops(bus);
  317. int ret;
  318. if (ops->set_flags) {
  319. ret = ops->set_flags(dev, flags);
  320. if (ret)
  321. return ret;
  322. }
  323. chip->flags = flags;
  324. return 0;
  325. }
  326. int i2c_get_chip_flags(struct udevice *dev, uint *flagsp)
  327. {
  328. struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
  329. *flagsp = chip->flags;
  330. return 0;
  331. }
  332. int i2c_set_chip_offset_len(struct udevice *dev, uint offset_len)
  333. {
  334. struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
  335. if (offset_len > I2C_MAX_OFFSET_LEN)
  336. return -EINVAL;
  337. chip->offset_len = offset_len;
  338. return 0;
  339. }
  340. int i2c_deblock(struct udevice *bus)
  341. {
  342. struct dm_i2c_ops *ops = i2c_get_ops(bus);
  343. /*
  344. * We could implement a software deblocking here if we could get
  345. * access to the GPIOs used by I2C, and switch them to GPIO mode
  346. * and then back to I2C. This is somewhat beyond our powers in
  347. * driver model at present, so for now just fail.
  348. *
  349. * See https://patchwork.ozlabs.org/patch/399040/
  350. */
  351. if (!ops->deblock)
  352. return -ENOSYS;
  353. return ops->deblock(bus);
  354. }
  355. int i2c_chip_ofdata_to_platdata(const void *blob, int node,
  356. struct dm_i2c_chip *chip)
  357. {
  358. chip->offset_len = 1; /* default */
  359. chip->flags = 0;
  360. chip->chip_addr = fdtdec_get_int(gd->fdt_blob, node, "reg", -1);
  361. if (chip->chip_addr == -1) {
  362. debug("%s: I2C Node '%s' has no 'reg' property\n", __func__,
  363. fdt_get_name(blob, node, NULL));
  364. return -EINVAL;
  365. }
  366. return 0;
  367. }
  368. static int i2c_post_probe(struct udevice *dev)
  369. {
  370. struct dm_i2c_bus *i2c = dev->uclass_priv;
  371. i2c->speed_hz = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
  372. "clock-frequency", 100000);
  373. return i2c_set_bus_speed(dev, i2c->speed_hz);
  374. }
  375. static int i2c_post_bind(struct udevice *dev)
  376. {
  377. /* Scan the bus for devices */
  378. return dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset, false);
  379. }
  380. static int i2c_child_post_bind(struct udevice *dev)
  381. {
  382. struct dm_i2c_chip *plat = dev_get_parent_platdata(dev);
  383. if (dev->of_offset == -1)
  384. return 0;
  385. return i2c_chip_ofdata_to_platdata(gd->fdt_blob, dev->of_offset, plat);
  386. }
  387. UCLASS_DRIVER(i2c) = {
  388. .id = UCLASS_I2C,
  389. .name = "i2c",
  390. .flags = DM_UC_FLAG_SEQ_ALIAS,
  391. .post_bind = i2c_post_bind,
  392. .post_probe = i2c_post_probe,
  393. .per_device_auto_alloc_size = sizeof(struct dm_i2c_bus),
  394. .per_child_platdata_auto_alloc_size = sizeof(struct dm_i2c_chip),
  395. .child_post_bind = i2c_child_post_bind,
  396. };
  397. UCLASS_DRIVER(i2c_generic) = {
  398. .id = UCLASS_I2C_GENERIC,
  399. .name = "i2c_generic",
  400. };
  401. U_BOOT_DRIVER(i2c_generic_chip_drv) = {
  402. .name = "i2c_generic_chip_drv",
  403. .id = UCLASS_I2C_GENERIC,
  404. };