w1-eeprom-uclass.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. *
  4. * Copyright (c) 2015 Free Electrons
  5. * Copyright (c) 2015 NextThing Co.
  6. * Copyright (c) 2018 Microchip Technology, Inc.
  7. *
  8. * Maxime Ripard <maxime.ripard@free-electrons.com>
  9. * Eugen Hristev <eugen.hristev@microchip.com>
  10. *
  11. */
  12. #define LOG_CATEGORY UCLASS_W1_EEPROM
  13. #include <common.h>
  14. #include <dm.h>
  15. #include <log.h>
  16. #include <w1.h>
  17. #include <w1-eeprom.h>
  18. #include <dm/device-internal.h>
  19. int w1_eeprom_read_buf(struct udevice *dev, unsigned int offset,
  20. u8 *buf, unsigned int count)
  21. {
  22. const struct w1_eeprom_ops *ops = device_get_ops(dev);
  23. u64 id = 0;
  24. int ret;
  25. if (!ops->read_buf)
  26. return -ENOSYS;
  27. ret = w1_eeprom_get_id(dev, &id);
  28. if (ret)
  29. return ret;
  30. if (!id)
  31. return -ENODEV;
  32. return ops->read_buf(dev, offset, buf, count);
  33. }
  34. int w1_eeprom_get_id(struct udevice *dev, u64 *id)
  35. {
  36. struct w1_device *w1 = dev_get_parent_plat(dev);
  37. if (!w1)
  38. return -ENODEV;
  39. *id = w1->id;
  40. return 0;
  41. }
  42. UCLASS_DRIVER(w1_eeprom) = {
  43. .name = "w1_eeprom",
  44. .id = UCLASS_W1_EEPROM,
  45. .flags = DM_UC_FLAG_SEQ_ALIAS,
  46. #if CONFIG_IS_ENABLED(OF_CONTROL)
  47. .post_bind = dm_scan_fdt_dev,
  48. #endif
  49. };
  50. int w1_eeprom_dm_init(void)
  51. {
  52. struct udevice *dev;
  53. struct uclass *uc;
  54. int ret;
  55. ret = uclass_get(UCLASS_W1_EEPROM, &uc);
  56. if (ret) {
  57. debug("W1_EEPROM uclass not available\n");
  58. return ret;
  59. }
  60. uclass_foreach_dev(dev, uc) {
  61. ret = device_probe(dev);
  62. if (ret == -ENODEV) { /* No such device. */
  63. debug("W1_EEPROM not available.\n");
  64. continue;
  65. }
  66. if (ret) { /* Other error. */
  67. printf("W1_EEPROM probe failed, error %d\n", ret);
  68. continue;
  69. }
  70. }
  71. return 0;
  72. }