i2c-uclass.c 12 KB

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