i2c-uclass.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2014 Google, Inc
  4. */
  5. #include <common.h>
  6. #include <dm.h>
  7. #include <errno.h>
  8. #include <i2c.h>
  9. #include <log.h>
  10. #include <malloc.h>
  11. #include <dm/device-internal.h>
  12. #include <dm/lists.h>
  13. #include <dm/pinctrl.h>
  14. #if CONFIG_IS_ENABLED(DM_GPIO)
  15. #include <asm/gpio.h>
  16. #endif
  17. #include <linux/delay.h>
  18. #define I2C_MAX_OFFSET_LEN 4
  19. enum {
  20. PIN_SDA = 0,
  21. PIN_SCL,
  22. PIN_COUNT,
  23. };
  24. /* Useful debugging function */
  25. void i2c_dump_msgs(struct i2c_msg *msg, int nmsgs)
  26. {
  27. int i;
  28. for (i = 0; i < nmsgs; i++) {
  29. struct i2c_msg *m = &msg[i];
  30. printf(" %s %x len=%x", m->flags & I2C_M_RD ? "R" : "W",
  31. msg->addr, msg->len);
  32. if (!(m->flags & I2C_M_RD))
  33. printf(": %x", m->buf[0]);
  34. printf("\n");
  35. }
  36. }
  37. /**
  38. * i2c_setup_offset() - Set up a new message with a chip offset
  39. *
  40. * @chip: Chip to use
  41. * @offset: Byte offset within chip
  42. * @offset_buf: Place to put byte offset
  43. * @msg: Message buffer
  44. * @return 0 if OK, -EADDRNOTAVAIL if the offset length is 0. In that case the
  45. * message is still set up but will not contain an offset.
  46. */
  47. static int i2c_setup_offset(struct dm_i2c_chip *chip, uint offset,
  48. uint8_t offset_buf[], struct i2c_msg *msg)
  49. {
  50. int offset_len = chip->offset_len;
  51. msg->addr = chip->chip_addr;
  52. if (chip->chip_addr_offset_mask)
  53. msg->addr |= (offset >> (8 * offset_len)) &
  54. chip->chip_addr_offset_mask;
  55. msg->flags = chip->flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
  56. msg->len = chip->offset_len;
  57. msg->buf = offset_buf;
  58. if (!offset_len)
  59. return -EADDRNOTAVAIL;
  60. assert(offset_len <= I2C_MAX_OFFSET_LEN);
  61. while (offset_len--)
  62. *offset_buf++ = offset >> (8 * offset_len);
  63. return 0;
  64. }
  65. static int i2c_read_bytewise(struct udevice *dev, uint offset,
  66. uint8_t *buffer, int len)
  67. {
  68. struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
  69. struct udevice *bus = dev_get_parent(dev);
  70. struct dm_i2c_ops *ops = i2c_get_ops(bus);
  71. struct i2c_msg msg[2], *ptr;
  72. uint8_t offset_buf[I2C_MAX_OFFSET_LEN];
  73. int ret;
  74. int i;
  75. for (i = 0; i < len; i++) {
  76. if (i2c_setup_offset(chip, offset + i, offset_buf, msg))
  77. return -EINVAL;
  78. ptr = msg + 1;
  79. ptr->addr = msg->addr;
  80. ptr->flags = msg->flags | I2C_M_RD;
  81. ptr->len = 1;
  82. ptr->buf = &buffer[i];
  83. ptr++;
  84. ret = ops->xfer(bus, msg, ptr - msg);
  85. if (ret)
  86. return ret;
  87. }
  88. return 0;
  89. }
  90. static int i2c_write_bytewise(struct udevice *dev, uint offset,
  91. const uint8_t *buffer, int len)
  92. {
  93. struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
  94. struct udevice *bus = dev_get_parent(dev);
  95. struct dm_i2c_ops *ops = i2c_get_ops(bus);
  96. struct i2c_msg msg[1];
  97. uint8_t buf[I2C_MAX_OFFSET_LEN + 1];
  98. int ret;
  99. int i;
  100. for (i = 0; i < len; i++) {
  101. if (i2c_setup_offset(chip, offset + i, buf, msg))
  102. return -EINVAL;
  103. buf[msg->len++] = buffer[i];
  104. ret = ops->xfer(bus, msg, 1);
  105. if (ret)
  106. return ret;
  107. }
  108. return 0;
  109. }
  110. int dm_i2c_read(struct udevice *dev, uint offset, uint8_t *buffer, int len)
  111. {
  112. struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
  113. struct udevice *bus = dev_get_parent(dev);
  114. struct dm_i2c_ops *ops = i2c_get_ops(bus);
  115. struct i2c_msg msg[2], *ptr;
  116. uint8_t offset_buf[I2C_MAX_OFFSET_LEN];
  117. int msg_count;
  118. if (!ops->xfer)
  119. return -ENOSYS;
  120. if (chip->flags & DM_I2C_CHIP_RD_ADDRESS)
  121. return i2c_read_bytewise(dev, offset, buffer, len);
  122. ptr = msg;
  123. if (!i2c_setup_offset(chip, offset, offset_buf, ptr))
  124. ptr++;
  125. if (len) {
  126. ptr->addr = msg->addr;
  127. ptr->flags = chip->flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
  128. ptr->flags |= I2C_M_RD;
  129. ptr->len = len;
  130. ptr->buf = buffer;
  131. ptr++;
  132. }
  133. msg_count = ptr - msg;
  134. return ops->xfer(bus, msg, msg_count);
  135. }
  136. int dm_i2c_write(struct udevice *dev, uint offset, const uint8_t *buffer,
  137. int len)
  138. {
  139. struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
  140. struct udevice *bus = dev_get_parent(dev);
  141. struct dm_i2c_ops *ops = i2c_get_ops(bus);
  142. struct i2c_msg msg[1];
  143. if (!ops->xfer)
  144. return -ENOSYS;
  145. if (chip->flags & DM_I2C_CHIP_WR_ADDRESS)
  146. return i2c_write_bytewise(dev, offset, buffer, len);
  147. /*
  148. * The simple approach would be to send two messages here: one to
  149. * set the offset and one to write the bytes. However some drivers
  150. * will not be expecting this, and some chips won't like how the
  151. * driver presents this on the I2C bus.
  152. *
  153. * The API does not support separate offset and data. We could extend
  154. * it with a flag indicating that there is data in the next message
  155. * that needs to be processed in the same transaction. We could
  156. * instead add an additional buffer to each message. For now, handle
  157. * this in the uclass since it isn't clear what the impact on drivers
  158. * would be with this extra complication. Unfortunately this means
  159. * copying the message.
  160. *
  161. * Use the stack for small messages, malloc() for larger ones. We
  162. * need to allow space for the offset (up to 4 bytes) and the message
  163. * itself.
  164. */
  165. if (len < 64) {
  166. uint8_t buf[I2C_MAX_OFFSET_LEN + len];
  167. i2c_setup_offset(chip, offset, buf, msg);
  168. msg->len += len;
  169. memcpy(buf + chip->offset_len, buffer, len);
  170. return ops->xfer(bus, msg, 1);
  171. } else {
  172. uint8_t *buf;
  173. int ret;
  174. buf = malloc(I2C_MAX_OFFSET_LEN + len);
  175. if (!buf)
  176. return -ENOMEM;
  177. i2c_setup_offset(chip, offset, buf, msg);
  178. msg->len += len;
  179. memcpy(buf + chip->offset_len, buffer, len);
  180. ret = ops->xfer(bus, msg, 1);
  181. free(buf);
  182. return ret;
  183. }
  184. }
  185. int dm_i2c_xfer(struct udevice *dev, struct i2c_msg *msg, int nmsgs)
  186. {
  187. struct udevice *bus = dev_get_parent(dev);
  188. struct dm_i2c_ops *ops = i2c_get_ops(bus);
  189. if (!ops->xfer)
  190. return -ENOSYS;
  191. return ops->xfer(bus, msg, nmsgs);
  192. }
  193. int dm_i2c_reg_read(struct udevice *dev, uint offset)
  194. {
  195. uint8_t val;
  196. int ret;
  197. ret = dm_i2c_read(dev, offset, &val, 1);
  198. if (ret < 0)
  199. return ret;
  200. return val;
  201. }
  202. int dm_i2c_reg_write(struct udevice *dev, uint offset, uint value)
  203. {
  204. uint8_t val = value;
  205. return dm_i2c_write(dev, offset, &val, 1);
  206. }
  207. /**
  208. * i2c_probe_chip() - probe for a chip on a bus
  209. *
  210. * @bus: Bus to probe
  211. * @chip_addr: Chip address to probe
  212. * @flags: Flags for the chip
  213. * @return 0 if found, -ENOSYS if the driver is invalid, -EREMOTEIO if the chip
  214. * does not respond to probe
  215. */
  216. static int i2c_probe_chip(struct udevice *bus, uint chip_addr,
  217. enum dm_i2c_chip_flags chip_flags)
  218. {
  219. struct dm_i2c_ops *ops = i2c_get_ops(bus);
  220. struct i2c_msg msg[1];
  221. int ret;
  222. if (ops->probe_chip) {
  223. ret = ops->probe_chip(bus, chip_addr, chip_flags);
  224. if (!ret || ret != -ENOSYS)
  225. return ret;
  226. }
  227. if (!ops->xfer)
  228. return -ENOSYS;
  229. /* Probe with a zero-length message */
  230. msg->addr = chip_addr;
  231. msg->flags = chip_flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
  232. msg->len = 0;
  233. msg->buf = NULL;
  234. return ops->xfer(bus, msg, 1);
  235. }
  236. static int i2c_bind_driver(struct udevice *bus, uint chip_addr, uint offset_len,
  237. struct udevice **devp)
  238. {
  239. struct dm_i2c_chip *chip;
  240. char name[30], *str;
  241. struct udevice *dev;
  242. int ret;
  243. snprintf(name, sizeof(name), "generic_%x", chip_addr);
  244. str = strdup(name);
  245. if (!str)
  246. return -ENOMEM;
  247. ret = device_bind_driver(bus, "i2c_generic_chip_drv", str, &dev);
  248. debug("%s: device_bind_driver: ret=%d\n", __func__, ret);
  249. if (ret)
  250. goto err_bind;
  251. /* Tell the device what we know about it */
  252. chip = dev_get_parent_platdata(dev);
  253. chip->chip_addr = chip_addr;
  254. chip->offset_len = offset_len;
  255. ret = device_probe(dev);
  256. debug("%s: device_probe: ret=%d\n", __func__, ret);
  257. if (ret)
  258. goto err_probe;
  259. *devp = dev;
  260. return 0;
  261. err_probe:
  262. /*
  263. * If the device failed to probe, unbind it. There is nothing there
  264. * on the bus so we don't want to leave it lying around
  265. */
  266. device_unbind(dev);
  267. err_bind:
  268. free(str);
  269. return ret;
  270. }
  271. int i2c_get_chip(struct udevice *bus, uint chip_addr, uint offset_len,
  272. struct udevice **devp)
  273. {
  274. struct udevice *dev;
  275. debug("%s: Searching bus '%s' for address %02x: ", __func__,
  276. bus->name, chip_addr);
  277. for (device_find_first_child(bus, &dev); dev;
  278. device_find_next_child(&dev)) {
  279. struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
  280. int ret;
  281. if (chip->chip_addr == (chip_addr &
  282. ~chip->chip_addr_offset_mask)) {
  283. ret = device_probe(dev);
  284. debug("found, ret=%d\n", ret);
  285. if (ret)
  286. return ret;
  287. *devp = dev;
  288. return 0;
  289. }
  290. }
  291. debug("not found\n");
  292. return i2c_bind_driver(bus, chip_addr, offset_len, devp);
  293. }
  294. int i2c_get_chip_for_busnum(int busnum, int chip_addr, uint offset_len,
  295. struct udevice **devp)
  296. {
  297. struct udevice *bus;
  298. int ret;
  299. ret = uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus);
  300. if (ret) {
  301. debug("Cannot find I2C bus %d\n", busnum);
  302. return ret;
  303. }
  304. /* detect the presence of the chip on the bus */
  305. ret = i2c_probe_chip(bus, chip_addr, 0);
  306. debug("%s: bus='%s', address %02x, ret=%d\n", __func__, bus->name,
  307. chip_addr, ret);
  308. if (ret) {
  309. debug("Cannot detect I2C chip %02x on bus %d\n", chip_addr,
  310. busnum);
  311. return ret;
  312. }
  313. ret = i2c_get_chip(bus, chip_addr, offset_len, devp);
  314. if (ret) {
  315. debug("Cannot find I2C chip %02x on bus %d\n", chip_addr,
  316. busnum);
  317. return ret;
  318. }
  319. return 0;
  320. }
  321. int dm_i2c_probe(struct udevice *bus, uint chip_addr, uint chip_flags,
  322. struct udevice **devp)
  323. {
  324. int ret;
  325. *devp = NULL;
  326. /* First probe that chip */
  327. ret = i2c_probe_chip(bus, chip_addr, chip_flags);
  328. debug("%s: bus='%s', address %02x, ret=%d\n", __func__, bus->name,
  329. chip_addr, ret);
  330. if (ret)
  331. return ret;
  332. /* The chip was found, see if we have a driver, and probe it */
  333. ret = i2c_get_chip(bus, chip_addr, 1, devp);
  334. debug("%s: i2c_get_chip: ret=%d\n", __func__, ret);
  335. return ret;
  336. }
  337. int dm_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
  338. {
  339. struct dm_i2c_ops *ops = i2c_get_ops(bus);
  340. struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
  341. int ret;
  342. /*
  343. * If we have a method, call it. If not then the driver probably wants
  344. * to deal with speed changes on the next transfer. It can easily read
  345. * the current speed from this uclass
  346. */
  347. if (ops->set_bus_speed) {
  348. ret = ops->set_bus_speed(bus, speed);
  349. if (ret)
  350. return ret;
  351. }
  352. i2c->speed_hz = speed;
  353. return 0;
  354. }
  355. int dm_i2c_get_bus_speed(struct udevice *bus)
  356. {
  357. struct dm_i2c_ops *ops = i2c_get_ops(bus);
  358. struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
  359. if (!ops->get_bus_speed)
  360. return i2c->speed_hz;
  361. return ops->get_bus_speed(bus);
  362. }
  363. int i2c_set_chip_flags(struct udevice *dev, uint flags)
  364. {
  365. struct udevice *bus = dev->parent;
  366. struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
  367. struct dm_i2c_ops *ops = i2c_get_ops(bus);
  368. int ret;
  369. if (ops->set_flags) {
  370. ret = ops->set_flags(dev, flags);
  371. if (ret)
  372. return ret;
  373. }
  374. chip->flags = flags;
  375. return 0;
  376. }
  377. int i2c_get_chip_flags(struct udevice *dev, uint *flagsp)
  378. {
  379. struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
  380. *flagsp = chip->flags;
  381. return 0;
  382. }
  383. int i2c_set_chip_offset_len(struct udevice *dev, uint offset_len)
  384. {
  385. struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
  386. if (offset_len > I2C_MAX_OFFSET_LEN)
  387. return -EINVAL;
  388. chip->offset_len = offset_len;
  389. return 0;
  390. }
  391. int i2c_get_chip_offset_len(struct udevice *dev)
  392. {
  393. struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
  394. return chip->offset_len;
  395. }
  396. int i2c_set_chip_addr_offset_mask(struct udevice *dev, uint mask)
  397. {
  398. struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
  399. chip->chip_addr_offset_mask = mask;
  400. return 0;
  401. }
  402. uint i2c_get_chip_addr_offset_mask(struct udevice *dev)
  403. {
  404. struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
  405. return chip->chip_addr_offset_mask;
  406. }
  407. #if CONFIG_IS_ENABLED(DM_GPIO)
  408. static void i2c_gpio_set_pin(struct gpio_desc *pin, int bit)
  409. {
  410. if (bit)
  411. dm_gpio_set_dir_flags(pin, GPIOD_IS_IN);
  412. else
  413. dm_gpio_set_dir_flags(pin, GPIOD_IS_OUT |
  414. GPIOD_ACTIVE_LOW |
  415. GPIOD_IS_OUT_ACTIVE);
  416. }
  417. static int i2c_gpio_get_pin(struct gpio_desc *pin)
  418. {
  419. return dm_gpio_get_value(pin);
  420. }
  421. int i2c_deblock_gpio_loop(struct gpio_desc *sda_pin,
  422. struct gpio_desc *scl_pin,
  423. unsigned int scl_count,
  424. unsigned int start_count,
  425. unsigned int delay)
  426. {
  427. int i, ret = -EREMOTEIO;
  428. i2c_gpio_set_pin(sda_pin, 1);
  429. i2c_gpio_set_pin(scl_pin, 1);
  430. udelay(delay);
  431. /* Toggle SCL until slave release SDA */
  432. for (; scl_count; --scl_count) {
  433. i2c_gpio_set_pin(scl_pin, 1);
  434. udelay(delay);
  435. i2c_gpio_set_pin(scl_pin, 0);
  436. udelay(delay);
  437. if (i2c_gpio_get_pin(sda_pin)) {
  438. ret = 0;
  439. break;
  440. }
  441. }
  442. if (!ret && start_count) {
  443. for (i = 0; i < start_count; i++) {
  444. /* Send start condition */
  445. udelay(delay);
  446. i2c_gpio_set_pin(sda_pin, 1);
  447. udelay(delay);
  448. i2c_gpio_set_pin(scl_pin, 1);
  449. udelay(delay);
  450. i2c_gpio_set_pin(sda_pin, 0);
  451. udelay(delay);
  452. i2c_gpio_set_pin(scl_pin, 0);
  453. }
  454. }
  455. /* Then, send I2C stop */
  456. i2c_gpio_set_pin(sda_pin, 0);
  457. udelay(delay);
  458. i2c_gpio_set_pin(scl_pin, 1);
  459. udelay(delay);
  460. i2c_gpio_set_pin(sda_pin, 1);
  461. udelay(delay);
  462. if (!i2c_gpio_get_pin(sda_pin) || !i2c_gpio_get_pin(scl_pin))
  463. ret = -EREMOTEIO;
  464. return ret;
  465. }
  466. static int i2c_deblock_gpio(struct udevice *bus)
  467. {
  468. struct gpio_desc gpios[PIN_COUNT];
  469. int ret, ret0;
  470. ret = gpio_request_list_by_name(bus, "gpios", gpios,
  471. ARRAY_SIZE(gpios), GPIOD_IS_IN);
  472. if (ret != ARRAY_SIZE(gpios)) {
  473. debug("%s: I2C Node '%s' has no 'gpios' property %s\n",
  474. __func__, dev_read_name(bus), bus->name);
  475. if (ret >= 0) {
  476. gpio_free_list(bus, gpios, ret);
  477. ret = -ENOENT;
  478. }
  479. goto out;
  480. }
  481. ret = pinctrl_select_state(bus, "gpio");
  482. if (ret) {
  483. debug("%s: I2C Node '%s' has no 'gpio' pinctrl state. %s\n",
  484. __func__, dev_read_name(bus), bus->name);
  485. goto out_no_pinctrl;
  486. }
  487. ret0 = i2c_deblock_gpio_loop(&gpios[PIN_SDA], &gpios[PIN_SCL], 9, 0, 5);
  488. ret = pinctrl_select_state(bus, "default");
  489. if (ret) {
  490. debug("%s: I2C Node '%s' has no 'default' pinctrl state. %s\n",
  491. __func__, dev_read_name(bus), bus->name);
  492. }
  493. ret = !ret ? ret0 : ret;
  494. out_no_pinctrl:
  495. gpio_free_list(bus, gpios, ARRAY_SIZE(gpios));
  496. out:
  497. return ret;
  498. }
  499. #else
  500. static int i2c_deblock_gpio(struct udevice *bus)
  501. {
  502. return -ENOSYS;
  503. }
  504. #endif /* DM_GPIO */
  505. int i2c_deblock(struct udevice *bus)
  506. {
  507. struct dm_i2c_ops *ops = i2c_get_ops(bus);
  508. if (!ops->deblock)
  509. return i2c_deblock_gpio(bus);
  510. return ops->deblock(bus);
  511. }
  512. #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
  513. int i2c_chip_ofdata_to_platdata(struct udevice *dev, struct dm_i2c_chip *chip)
  514. {
  515. int addr;
  516. chip->offset_len = dev_read_u32_default(dev, "u-boot,i2c-offset-len",
  517. 1);
  518. chip->flags = 0;
  519. addr = dev_read_u32_default(dev, "reg", -1);
  520. if (addr == -1) {
  521. debug("%s: I2C Node '%s' has no 'reg' property %s\n", __func__,
  522. dev_read_name(dev), dev->name);
  523. return -EINVAL;
  524. }
  525. chip->chip_addr = addr;
  526. return 0;
  527. }
  528. #endif
  529. static int i2c_pre_probe(struct udevice *dev)
  530. {
  531. #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
  532. struct dm_i2c_bus *i2c = dev_get_uclass_priv(dev);
  533. unsigned int max = 0;
  534. ofnode node;
  535. int ret;
  536. i2c->max_transaction_bytes = 0;
  537. dev_for_each_subnode(node, dev) {
  538. ret = ofnode_read_u32(node,
  539. "u-boot,i2c-transaction-bytes",
  540. &max);
  541. if (!ret && max > i2c->max_transaction_bytes)
  542. i2c->max_transaction_bytes = max;
  543. }
  544. debug("%s: I2C bus: %s max transaction bytes: %d\n", __func__,
  545. dev->name, i2c->max_transaction_bytes);
  546. #endif
  547. return 0;
  548. }
  549. static int i2c_post_probe(struct udevice *dev)
  550. {
  551. #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
  552. struct dm_i2c_bus *i2c = dev_get_uclass_priv(dev);
  553. i2c->speed_hz = dev_read_u32_default(dev, "clock-frequency",
  554. I2C_SPEED_STANDARD_RATE);
  555. return dm_i2c_set_bus_speed(dev, i2c->speed_hz);
  556. #else
  557. return 0;
  558. #endif
  559. }
  560. static int i2c_child_post_bind(struct udevice *dev)
  561. {
  562. #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
  563. struct dm_i2c_chip *plat = dev_get_parent_platdata(dev);
  564. if (!dev_of_valid(dev))
  565. return 0;
  566. return i2c_chip_ofdata_to_platdata(dev, plat);
  567. #else
  568. return 0;
  569. #endif
  570. }
  571. struct i2c_priv {
  572. int max_id;
  573. };
  574. static int i2c_post_bind(struct udevice *dev)
  575. {
  576. struct uclass *class = dev->uclass;
  577. struct i2c_priv *priv = class->priv;
  578. int ret = 0;
  579. /* Just for sure */
  580. if (!priv)
  581. return -ENOMEM;
  582. debug("%s: %s, req_seq=%d\n", __func__, dev->name, dev->req_seq);
  583. /* if there is no alias ID, use the first free */
  584. if (dev->req_seq == -1)
  585. dev->req_seq = ++priv->max_id;
  586. debug("%s: %s, new req_seq=%d\n", __func__, dev->name, dev->req_seq);
  587. #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
  588. ret = dm_scan_fdt_dev(dev);
  589. #endif
  590. return ret;
  591. }
  592. int i2c_uclass_init(struct uclass *class)
  593. {
  594. struct i2c_priv *priv = class->priv;
  595. /* Just for sure */
  596. if (!priv)
  597. return -ENOMEM;
  598. /* Get the last allocated alias. */
  599. if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA))
  600. priv->max_id = dev_read_alias_highest_id("i2c");
  601. else
  602. priv->max_id = -1;
  603. debug("%s: highest alias id is %d\n", __func__, priv->max_id);
  604. return 0;
  605. }
  606. UCLASS_DRIVER(i2c) = {
  607. .id = UCLASS_I2C,
  608. .name = "i2c",
  609. .flags = DM_UC_FLAG_SEQ_ALIAS,
  610. .post_bind = i2c_post_bind,
  611. .init = i2c_uclass_init,
  612. .priv_auto_alloc_size = sizeof(struct i2c_priv),
  613. .pre_probe = i2c_pre_probe,
  614. .post_probe = i2c_post_probe,
  615. .per_device_auto_alloc_size = sizeof(struct dm_i2c_bus),
  616. .per_child_platdata_auto_alloc_size = sizeof(struct dm_i2c_chip),
  617. .child_post_bind = i2c_child_post_bind,
  618. };
  619. UCLASS_DRIVER(i2c_generic) = {
  620. .id = UCLASS_I2C_GENERIC,
  621. .name = "i2c_generic",
  622. };
  623. U_BOOT_DRIVER(i2c_generic_chip_drv) = {
  624. .name = "i2c_generic_chip_drv",
  625. .id = UCLASS_I2C_GENERIC,
  626. };