w1-eeprom-uclass.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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_register_new_device(u64 id)
  34. {
  35. u8 family = id & 0xff;
  36. int ret;
  37. struct udevice *dev;
  38. for (ret = uclass_first_device(UCLASS_W1_EEPROM, &dev);
  39. !ret && dev;
  40. uclass_next_device(&dev)) {
  41. if (ret || !dev) {
  42. debug("cannot find w1 eeprom dev\n");
  43. return ret;
  44. }
  45. if (dev_get_driver_data(dev) == family) {
  46. struct w1_device *w1;
  47. w1 = dev_get_parent_platdata(dev);
  48. if (w1->id) /* device already in use */
  49. continue;
  50. w1->id = id;
  51. debug("%s: Match found: %s:%s %llx\n", __func__,
  52. dev->name, dev->driver->name, id);
  53. return 0;
  54. }
  55. }
  56. debug("%s: No matches found: error %d\n", __func__, ret);
  57. return ret;
  58. }
  59. int w1_eeprom_get_id(struct udevice *dev, u64 *id)
  60. {
  61. struct w1_device *w1 = dev_get_parent_platdata(dev);
  62. if (!w1)
  63. return -ENODEV;
  64. *id = w1->id;
  65. return 0;
  66. }
  67. UCLASS_DRIVER(w1_eeprom) = {
  68. .name = "w1_eeprom",
  69. .id = UCLASS_W1_EEPROM,
  70. .flags = DM_UC_FLAG_SEQ_ALIAS,
  71. #if CONFIG_IS_ENABLED(OF_CONTROL)
  72. .post_bind = dm_scan_fdt_dev,
  73. #endif
  74. };
  75. int w1_eeprom_dm_init(void)
  76. {
  77. struct udevice *dev;
  78. struct uclass *uc;
  79. int ret;
  80. ret = uclass_get(UCLASS_W1_EEPROM, &uc);
  81. if (ret) {
  82. debug("W1_EEPROM uclass not available\n");
  83. return ret;
  84. }
  85. uclass_foreach_dev(dev, uc) {
  86. ret = device_probe(dev);
  87. if (ret == -ENODEV) { /* No such device. */
  88. debug("W1_EEPROM not available.\n");
  89. continue;
  90. }
  91. if (ret) { /* Other error. */
  92. printf("W1_EEPROM probe failed, error %d\n", ret);
  93. continue;
  94. }
  95. }
  96. return 0;
  97. }