regulator_fixed.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Copyright (c) 2021-2023, STMicroelectronics - All Rights Reserved
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <errno.h>
  8. #include <common/debug.h>
  9. #include <common/fdt_wrappers.h>
  10. #include <drivers/st/regulator.h>
  11. #include <drivers/st/regulator_fixed.h>
  12. #include <libfdt.h>
  13. #ifndef PLAT_NB_FIXED_REGUS
  14. #error "Missing PLAT_NB_FIXED_REGUS"
  15. #endif
  16. #define FIXED_NAME_LEN 32
  17. struct fixed_data {
  18. char name[FIXED_NAME_LEN];
  19. uint16_t volt;
  20. struct regul_description desc;
  21. };
  22. static struct fixed_data data[PLAT_NB_FIXED_REGUS];
  23. static int fixed_set_state(const struct regul_description *desc, bool state)
  24. {
  25. return 0;
  26. }
  27. static int fixed_get_state(const struct regul_description *desc)
  28. {
  29. return 1;
  30. }
  31. static struct regul_ops fixed_ops = {
  32. .set_state = fixed_set_state,
  33. .get_state = fixed_get_state,
  34. };
  35. int fixed_regulator_register(void)
  36. {
  37. uint32_t count = 0;
  38. void *fdt;
  39. int node;
  40. VERBOSE("fixed reg init!\n");
  41. if (fdt_get_address(&fdt) == 0) {
  42. return -FDT_ERR_NOTFOUND;
  43. }
  44. fdt_for_each_compatible_node(fdt, node, "regulator-fixed") {
  45. int len __unused;
  46. int ret;
  47. struct fixed_data *d = &data[count];
  48. const char *reg_name;
  49. reg_name = fdt_get_name(fdt, node, NULL);
  50. VERBOSE("register fixed reg %s!\n", reg_name);
  51. len = snprintf(d->name, FIXED_NAME_LEN - 1, "%s", reg_name);
  52. assert((len > 0) && (len < (FIXED_NAME_LEN - 1)));
  53. d->desc.node_name = d->name;
  54. d->desc.driver_data = d;
  55. d->desc.ops = &fixed_ops;
  56. ret = regulator_register(&d->desc, node);
  57. if (ret != 0) {
  58. WARN("%s:%d failed to register %s\n", __func__,
  59. __LINE__, reg_name);
  60. return ret;
  61. }
  62. count++;
  63. assert(count <= PLAT_NB_FIXED_REGUS);
  64. }
  65. return 0;
  66. }