i2c-uclass.c 12 KB

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