i2c-uclass.c 12 KB

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