w1-eeprom-uclass.c 2.2 KB

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