i2c-uclass.c 18 KB

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