w1-eeprom-uclass.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. #include <common.h>
  13. #include <dm.h>
  14. #include <log.h>
  15. #include <w1.h>
  16. #include <w1-eeprom.h>
  17. #include <dm/device-internal.h>
  18. int w1_eeprom_read_buf(struct udevice *dev, unsigned int offset,
  19. u8 *buf, unsigned int count)
  20. {
  21. const struct w1_eeprom_ops *ops = device_get_ops(dev);
  22. u64 id = 0;
  23. int ret;
  24. if (!ops->read_buf)
  25. return -ENOSYS;
  26. ret = w1_eeprom_get_id(dev, &id);
  27. if (ret)
  28. return ret;
  29. if (!id)
  30. return -ENODEV;
  31. return ops->read_buf(dev, offset, buf, count);
  32. }
  33. int w1_eeprom_get_id(struct udevice *dev, u64 *id)
  34. {
  35. struct w1_device *w1 = dev_get_parent_plat(dev);
  36. if (!w1)
  37. return -ENODEV;
  38. *id = w1->id;
  39. return 0;
  40. }
  41. UCLASS_DRIVER(w1_eeprom) = {
  42. .name = "w1_eeprom",
  43. .id = UCLASS_W1_EEPROM,
  44. .flags = DM_UC_FLAG_SEQ_ALIAS,
  45. #if CONFIG_IS_ENABLED(OF_CONTROL)
  46. .post_bind = dm_scan_fdt_dev,
  47. #endif
  48. };
  49. int w1_eeprom_dm_init(void)
  50. {
  51. struct udevice *dev;
  52. struct uclass *uc;
  53. int ret;
  54. ret = uclass_get(UCLASS_W1_EEPROM, &uc);
  55. if (ret) {
  56. debug("W1_EEPROM uclass not available\n");
  57. return ret;
  58. }
  59. uclass_foreach_dev(dev, uc) {
  60. ret = device_probe(dev);
  61. if (ret == -ENODEV) { /* No such device. */
  62. debug("W1_EEPROM not available.\n");
  63. continue;
  64. }
  65. if (ret) { /* Other error. */
  66. printf("W1_EEPROM probe failed, error %d\n", ret);
  67. continue;
  68. }
  69. }
  70. return 0;
  71. }