i2c-uclass-compat.c 2.4 KB

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