i2c-uclass.c 17 KB

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