i2c-uclass.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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. int dm_i2c_reg_read(struct udevice *dev, uint offset)
  164. {
  165. uint8_t val;
  166. int ret;
  167. ret = dm_i2c_read(dev, offset, &val, 1);
  168. if (ret < 0)
  169. return ret;
  170. return val;
  171. }
  172. int dm_i2c_reg_write(struct udevice *dev, uint offset, uint value)
  173. {
  174. uint8_t val = value;
  175. return dm_i2c_write(dev, offset, &val, 1);
  176. }
  177. /**
  178. * i2c_probe_chip() - probe for a chip on a bus
  179. *
  180. * @bus: Bus to probe
  181. * @chip_addr: Chip address to probe
  182. * @flags: Flags for the chip
  183. * @return 0 if found, -ENOSYS if the driver is invalid, -EREMOTEIO if the chip
  184. * does not respond to probe
  185. */
  186. static int i2c_probe_chip(struct udevice *bus, uint chip_addr,
  187. enum dm_i2c_chip_flags chip_flags)
  188. {
  189. struct dm_i2c_ops *ops = i2c_get_ops(bus);
  190. struct i2c_msg msg[1];
  191. int ret;
  192. if (ops->probe_chip) {
  193. ret = ops->probe_chip(bus, chip_addr, chip_flags);
  194. if (!ret || ret != -ENOSYS)
  195. return ret;
  196. }
  197. if (!ops->xfer)
  198. return -ENOSYS;
  199. /* Probe with a zero-length message */
  200. msg->addr = chip_addr;
  201. msg->flags = chip_flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
  202. msg->len = 0;
  203. msg->buf = NULL;
  204. return ops->xfer(bus, msg, 1);
  205. }
  206. static int i2c_bind_driver(struct udevice *bus, uint chip_addr, uint offset_len,
  207. struct udevice **devp)
  208. {
  209. struct dm_i2c_chip *chip;
  210. char name[30], *str;
  211. struct udevice *dev;
  212. int ret;
  213. snprintf(name, sizeof(name), "generic_%x", chip_addr);
  214. str = strdup(name);
  215. if (!str)
  216. return -ENOMEM;
  217. ret = device_bind_driver(bus, "i2c_generic_chip_drv", str, &dev);
  218. debug("%s: device_bind_driver: ret=%d\n", __func__, ret);
  219. if (ret)
  220. goto err_bind;
  221. /* Tell the device what we know about it */
  222. chip = dev_get_parent_platdata(dev);
  223. chip->chip_addr = chip_addr;
  224. chip->offset_len = offset_len;
  225. ret = device_probe(dev);
  226. debug("%s: device_probe: ret=%d\n", __func__, ret);
  227. if (ret)
  228. goto err_probe;
  229. *devp = dev;
  230. return 0;
  231. err_probe:
  232. /*
  233. * If the device failed to probe, unbind it. There is nothing there
  234. * on the bus so we don't want to leave it lying around
  235. */
  236. device_unbind(dev);
  237. err_bind:
  238. free(str);
  239. return ret;
  240. }
  241. int i2c_get_chip(struct udevice *bus, uint chip_addr, uint offset_len,
  242. struct udevice **devp)
  243. {
  244. struct udevice *dev;
  245. debug("%s: Searching bus '%s' for address %02x: ", __func__,
  246. bus->name, chip_addr);
  247. for (device_find_first_child(bus, &dev); dev;
  248. device_find_next_child(&dev)) {
  249. struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
  250. int ret;
  251. if (chip->chip_addr == chip_addr) {
  252. ret = device_probe(dev);
  253. debug("found, ret=%d\n", ret);
  254. if (ret)
  255. return ret;
  256. *devp = dev;
  257. return 0;
  258. }
  259. }
  260. debug("not found\n");
  261. return i2c_bind_driver(bus, chip_addr, offset_len, devp);
  262. }
  263. int i2c_get_chip_for_busnum(int busnum, int chip_addr, uint offset_len,
  264. struct udevice **devp)
  265. {
  266. struct udevice *bus;
  267. int ret;
  268. ret = uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus);
  269. if (ret) {
  270. debug("Cannot find I2C bus %d\n", busnum);
  271. return ret;
  272. }
  273. ret = i2c_get_chip(bus, chip_addr, offset_len, devp);
  274. if (ret) {
  275. debug("Cannot find I2C chip %02x on bus %d\n", chip_addr,
  276. busnum);
  277. return ret;
  278. }
  279. return 0;
  280. }
  281. int dm_i2c_probe(struct udevice *bus, uint chip_addr, uint chip_flags,
  282. struct udevice **devp)
  283. {
  284. int ret;
  285. *devp = NULL;
  286. /* First probe that chip */
  287. ret = i2c_probe_chip(bus, chip_addr, chip_flags);
  288. debug("%s: bus='%s', address %02x, ret=%d\n", __func__, bus->name,
  289. chip_addr, ret);
  290. if (ret)
  291. return ret;
  292. /* The chip was found, see if we have a driver, and probe it */
  293. ret = i2c_get_chip(bus, chip_addr, 1, devp);
  294. debug("%s: i2c_get_chip: ret=%d\n", __func__, ret);
  295. return ret;
  296. }
  297. int dm_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
  298. {
  299. struct dm_i2c_ops *ops = i2c_get_ops(bus);
  300. struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
  301. int ret;
  302. /*
  303. * If we have a method, call it. If not then the driver probably wants
  304. * to deal with speed changes on the next transfer. It can easily read
  305. * the current speed from this uclass
  306. */
  307. if (ops->set_bus_speed) {
  308. ret = ops->set_bus_speed(bus, speed);
  309. if (ret)
  310. return ret;
  311. }
  312. i2c->speed_hz = speed;
  313. return 0;
  314. }
  315. int dm_i2c_get_bus_speed(struct udevice *bus)
  316. {
  317. struct dm_i2c_ops *ops = i2c_get_ops(bus);
  318. struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
  319. if (!ops->get_bus_speed)
  320. return i2c->speed_hz;
  321. return ops->get_bus_speed(bus);
  322. }
  323. int i2c_set_chip_flags(struct udevice *dev, uint flags)
  324. {
  325. struct udevice *bus = dev->parent;
  326. struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
  327. struct dm_i2c_ops *ops = i2c_get_ops(bus);
  328. int ret;
  329. if (ops->set_flags) {
  330. ret = ops->set_flags(dev, flags);
  331. if (ret)
  332. return ret;
  333. }
  334. chip->flags = flags;
  335. return 0;
  336. }
  337. int i2c_get_chip_flags(struct udevice *dev, uint *flagsp)
  338. {
  339. struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
  340. *flagsp = chip->flags;
  341. return 0;
  342. }
  343. int i2c_set_chip_offset_len(struct udevice *dev, uint offset_len)
  344. {
  345. struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
  346. if (offset_len > I2C_MAX_OFFSET_LEN)
  347. return -EINVAL;
  348. chip->offset_len = offset_len;
  349. return 0;
  350. }
  351. int i2c_get_chip_offset_len(struct udevice *dev)
  352. {
  353. struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
  354. return chip->offset_len;
  355. }
  356. int i2c_deblock(struct udevice *bus)
  357. {
  358. struct dm_i2c_ops *ops = i2c_get_ops(bus);
  359. /*
  360. * We could implement a software deblocking here if we could get
  361. * access to the GPIOs used by I2C, and switch them to GPIO mode
  362. * and then back to I2C. This is somewhat beyond our powers in
  363. * driver model at present, so for now just fail.
  364. *
  365. * See https://patchwork.ozlabs.org/patch/399040/
  366. */
  367. if (!ops->deblock)
  368. return -ENOSYS;
  369. return ops->deblock(bus);
  370. }
  371. int i2c_chip_ofdata_to_platdata(const void *blob, int node,
  372. struct dm_i2c_chip *chip)
  373. {
  374. chip->offset_len = fdtdec_get_int(gd->fdt_blob, node,
  375. "u-boot,i2c-offset-len", 1);
  376. chip->flags = 0;
  377. chip->chip_addr = fdtdec_get_int(gd->fdt_blob, node, "reg", -1);
  378. if (chip->chip_addr == -1) {
  379. debug("%s: I2C Node '%s' has no 'reg' property\n", __func__,
  380. fdt_get_name(blob, node, NULL));
  381. return -EINVAL;
  382. }
  383. return 0;
  384. }
  385. static int i2c_post_probe(struct udevice *dev)
  386. {
  387. struct dm_i2c_bus *i2c = dev_get_uclass_priv(dev);
  388. i2c->speed_hz = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
  389. "clock-frequency", 100000);
  390. return dm_i2c_set_bus_speed(dev, i2c->speed_hz);
  391. }
  392. static int i2c_post_bind(struct udevice *dev)
  393. {
  394. /* Scan the bus for devices */
  395. return dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset, false);
  396. }
  397. static int i2c_child_post_bind(struct udevice *dev)
  398. {
  399. struct dm_i2c_chip *plat = dev_get_parent_platdata(dev);
  400. if (dev->of_offset == -1)
  401. return 0;
  402. return i2c_chip_ofdata_to_platdata(gd->fdt_blob, dev->of_offset, plat);
  403. }
  404. UCLASS_DRIVER(i2c) = {
  405. .id = UCLASS_I2C,
  406. .name = "i2c",
  407. .flags = DM_UC_FLAG_SEQ_ALIAS,
  408. .post_bind = i2c_post_bind,
  409. .post_probe = i2c_post_probe,
  410. .per_device_auto_alloc_size = sizeof(struct dm_i2c_bus),
  411. .per_child_platdata_auto_alloc_size = sizeof(struct dm_i2c_chip),
  412. .child_post_bind = i2c_child_post_bind,
  413. };
  414. UCLASS_DRIVER(i2c_generic) = {
  415. .id = UCLASS_I2C_GENERIC,
  416. .name = "i2c_generic",
  417. };
  418. U_BOOT_DRIVER(i2c_generic_chip_drv) = {
  419. .name = "i2c_generic_chip_drv",
  420. .id = UCLASS_I2C_GENERIC,
  421. };