i2c-uclass-compat.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. static int cur_busnum __attribute__((section(".data")));
  11. static int i2c_compat_get_device(uint chip_addr, int alen,
  12. struct udevice **devp)
  13. {
  14. struct dm_i2c_chip *chip;
  15. int ret;
  16. ret = i2c_get_chip_for_busnum(cur_busnum, chip_addr, alen, devp);
  17. if (ret)
  18. return ret;
  19. chip = dev_get_parent_platdata(*devp);
  20. if (chip->offset_len != alen) {
  21. printf("I2C chip %x: requested alen %d does not match chip offset_len %d\n",
  22. chip_addr, alen, chip->offset_len);
  23. return -EADDRNOTAVAIL;
  24. }
  25. return 0;
  26. }
  27. int i2c_probe(uint8_t chip_addr)
  28. {
  29. struct udevice *bus, *dev;
  30. int ret;
  31. ret = uclass_get_device_by_seq(UCLASS_I2C, cur_busnum, &bus);
  32. if (ret) {
  33. debug("Cannot find I2C bus %d: err=%d\n", cur_busnum, ret);
  34. return ret;
  35. }
  36. if (!bus)
  37. return -ENOENT;
  38. return dm_i2c_probe(bus, chip_addr, 0, &dev);
  39. }
  40. int i2c_read(uint8_t chip_addr, unsigned int addr, int alen, uint8_t *buffer,
  41. int len)
  42. {
  43. struct udevice *dev;
  44. int ret;
  45. ret = i2c_compat_get_device(chip_addr, alen, &dev);
  46. if (ret)
  47. return ret;
  48. return dm_i2c_read(dev, addr, buffer, len);
  49. }
  50. int i2c_write(uint8_t chip_addr, unsigned int addr, int alen, uint8_t *buffer,
  51. int len)
  52. {
  53. struct udevice *dev;
  54. int ret;
  55. ret = i2c_compat_get_device(chip_addr, alen, &dev);
  56. if (ret)
  57. return ret;
  58. return dm_i2c_write(dev, addr, buffer, len);
  59. }
  60. int i2c_get_bus_num_fdt(int node)
  61. {
  62. struct udevice *bus;
  63. int ret;
  64. ret = uclass_get_device_by_of_offset(UCLASS_I2C, node, &bus);
  65. if (ret)
  66. return ret;
  67. return bus->seq;
  68. }
  69. unsigned int i2c_get_bus_num(void)
  70. {
  71. return cur_busnum;
  72. }
  73. int i2c_set_bus_num(unsigned int bus)
  74. {
  75. cur_busnum = bus;
  76. return 0;
  77. }
  78. void i2c_init(int speed, int slaveaddr)
  79. {
  80. /* Nothing to do here - the init happens through driver model */
  81. }
  82. void board_i2c_init(const void *blob)
  83. {
  84. /* Nothing to do here - the init happens through driver model */
  85. }
  86. uint8_t i2c_reg_read(uint8_t chip_addr, uint8_t offset)
  87. {
  88. struct udevice *dev;
  89. int ret;
  90. ret = i2c_compat_get_device(chip_addr, 1, &dev);
  91. if (ret)
  92. return 0xff;
  93. return dm_i2c_reg_read(dev, offset);
  94. }
  95. void i2c_reg_write(uint8_t chip_addr, uint8_t offset, uint8_t val)
  96. {
  97. struct udevice *dev;
  98. int ret;
  99. ret = i2c_compat_get_device(chip_addr, 1, &dev);
  100. if (!ret)
  101. dm_i2c_reg_write(dev, offset, val);
  102. }