regulator-fixed.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2023-09-23 GuEe-GUI first version
  9. */
  10. #include "regulator_dm.h"
  11. struct regulator_fixed
  12. {
  13. struct rt_regulator_node parent;
  14. struct rt_regulator_param param;
  15. rt_base_t enable_pin;
  16. const char *input_supply;
  17. };
  18. #define raw_to_regulator_fixed(raw) rt_container_of(raw, struct regulator_fixed, parent)
  19. static rt_err_t regulator_fixed_enable(struct rt_regulator_node *reg_np)
  20. {
  21. struct regulator_fixed *rf = raw_to_regulator_fixed(reg_np);
  22. struct rt_regulator_param *param = &rf->param;
  23. if (rf->enable_pin < 0 || param->always_on)
  24. {
  25. return RT_EOK;
  26. }
  27. rt_pin_mode(rf->enable_pin, PIN_MODE_OUTPUT);
  28. rt_pin_write(rf->enable_pin, param->enable_active_high ? PIN_HIGH : PIN_LOW);
  29. return RT_EOK;
  30. }
  31. static rt_err_t regulator_fixed_disable(struct rt_regulator_node *reg_np)
  32. {
  33. struct regulator_fixed *rf = raw_to_regulator_fixed(reg_np);
  34. struct rt_regulator_param *param = &rf->param;
  35. if (rf->enable_pin < 0 || param->always_on)
  36. {
  37. return RT_EOK;
  38. }
  39. rt_pin_mode(rf->enable_pin, PIN_MODE_OUTPUT);
  40. rt_pin_write(rf->enable_pin, param->enable_active_high ? PIN_LOW: PIN_HIGH);
  41. return RT_EOK;
  42. }
  43. static rt_bool_t regulator_fixed_is_enabled(struct rt_regulator_node *reg_np)
  44. {
  45. rt_uint8_t active;
  46. struct regulator_fixed *rf = raw_to_regulator_fixed(reg_np);
  47. struct rt_regulator_param *param = &rf->param;
  48. if (rf->enable_pin < 0 || param->always_on)
  49. {
  50. return RT_TRUE;
  51. }
  52. rt_pin_mode(rf->enable_pin, PIN_MODE_INPUT);
  53. active = rt_pin_read(rf->enable_pin);
  54. if (param->enable_active_high)
  55. {
  56. return active == PIN_HIGH;
  57. }
  58. return active == PIN_LOW;
  59. }
  60. static int regulator_fixed_get_voltage(struct rt_regulator_node *reg_np)
  61. {
  62. struct regulator_fixed *rf = raw_to_regulator_fixed(reg_np);
  63. return rf->param.min_uvolt + (rf->param.max_uvolt - rf->param.min_uvolt) / 2;
  64. }
  65. static const struct rt_regulator_ops regulator_fixed_ops =
  66. {
  67. .enable = regulator_fixed_enable,
  68. .disable = regulator_fixed_disable,
  69. .is_enabled = regulator_fixed_is_enabled,
  70. .get_voltage = regulator_fixed_get_voltage,
  71. };
  72. static rt_err_t regulator_fixed_probe(struct rt_platform_device *pdev)
  73. {
  74. rt_err_t err;
  75. rt_uint32_t val;
  76. struct rt_device *dev = &pdev->parent;
  77. struct regulator_fixed *rf = rt_calloc(1, sizeof(*rf));
  78. struct rt_regulator_node *rnp;
  79. if (!rf)
  80. {
  81. return -RT_ENOMEM;
  82. }
  83. regulator_ofw_parse(dev->ofw_node, &rf->param);
  84. rnp = &rf->parent;
  85. rnp->supply_name = rf->param.name;
  86. rnp->ops = &regulator_fixed_ops;
  87. rnp->param = &rf->param;
  88. rnp->dev = &pdev->parent;
  89. rf->enable_pin = rt_pin_get_named_pin(dev, "enable", 0, RT_NULL, RT_NULL);
  90. if (rf->enable_pin < 0)
  91. {
  92. rf->enable_pin = rt_pin_get_named_pin(dev, RT_NULL, 0, RT_NULL, RT_NULL);
  93. }
  94. if (rf->enable_pin < 0)
  95. {
  96. rf->enable_pin = -1;
  97. }
  98. rt_pin_ctrl_confs_apply(dev, 0);
  99. if (!rt_dm_dev_prop_read_u32(dev, "startup-delay-us", &val))
  100. {
  101. rf->param.enable_delay = val;
  102. }
  103. if (!rt_dm_dev_prop_read_u32(dev, "off-on-delay-us", &val))
  104. {
  105. rf->param.off_on_delay = val;
  106. }
  107. if ((err = rt_regulator_register(rnp)))
  108. {
  109. goto _fail;
  110. }
  111. return RT_EOK;
  112. _fail:
  113. rt_free(rf);
  114. return err;
  115. }
  116. static const struct rt_ofw_node_id regulator_fixed_ofw_ids[] =
  117. {
  118. { .compatible = "regulator-fixed" },
  119. { /* sentinel */ }
  120. };
  121. static struct rt_platform_driver regulator_fixed_driver =
  122. {
  123. .name = "reg-fixed-voltage",
  124. .ids = regulator_fixed_ofw_ids,
  125. .probe = regulator_fixed_probe,
  126. };
  127. static int regulator_fixed_register(void)
  128. {
  129. rt_platform_driver_register(&regulator_fixed_driver);
  130. return 0;
  131. }
  132. INIT_SUBSYS_EXPORT(regulator_fixed_register);