gpio-regulator.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * (C) Copyright 2016 Texas Instruments Incorporated, <www.ti.com>
  3. * Keerthy <j-keerthy@ti.com>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <fdtdec.h>
  9. #include <errno.h>
  10. #include <dm.h>
  11. #include <i2c.h>
  12. #include <asm/gpio.h>
  13. #include <power/pmic.h>
  14. #include <power/regulator.h>
  15. #define GPIO_REGULATOR_MAX_STATES 2
  16. DECLARE_GLOBAL_DATA_PTR;
  17. struct gpio_regulator_platdata {
  18. struct gpio_desc gpio; /* GPIO for regulator voltage control */
  19. int states[GPIO_REGULATOR_MAX_STATES];
  20. int voltages[GPIO_REGULATOR_MAX_STATES];
  21. };
  22. static int gpio_regulator_ofdata_to_platdata(struct udevice *dev)
  23. {
  24. struct dm_regulator_uclass_platdata *uc_pdata;
  25. struct gpio_regulator_platdata *dev_pdata;
  26. struct gpio_desc *gpio;
  27. const void *blob = gd->fdt_blob;
  28. int node = dev_of_offset(dev);
  29. int ret, count, i, j;
  30. u32 states_array[8];
  31. dev_pdata = dev_get_platdata(dev);
  32. uc_pdata = dev_get_uclass_platdata(dev);
  33. if (!uc_pdata)
  34. return -ENXIO;
  35. /* Set type to gpio */
  36. uc_pdata->type = REGULATOR_TYPE_GPIO;
  37. /*
  38. * Get gpio regulator gpio desc
  39. * Assuming one GPIO per regulator.
  40. * Can be extended later to multiple GPIOs
  41. * per gpio-regulator. As of now no instance with multiple
  42. * gpios is presnt
  43. */
  44. gpio = &dev_pdata->gpio;
  45. ret = gpio_request_by_name(dev, "gpios", 0, gpio, GPIOD_IS_OUT);
  46. if (ret)
  47. debug("regulator gpio - not found! Error: %d", ret);
  48. count = fdtdec_get_int_array_count(blob, node, "states",
  49. states_array, 8);
  50. if (!count)
  51. return -EINVAL;
  52. for (i = 0, j = 0; i < count; i += 2) {
  53. dev_pdata->voltages[j] = states_array[i];
  54. dev_pdata->states[j] = states_array[i + 1];
  55. j++;
  56. }
  57. return 0;
  58. }
  59. static int gpio_regulator_get_value(struct udevice *dev)
  60. {
  61. struct dm_regulator_uclass_platdata *uc_pdata;
  62. struct gpio_regulator_platdata *dev_pdata = dev_get_platdata(dev);
  63. int enable;
  64. if (!dev_pdata->gpio.dev)
  65. return -ENOSYS;
  66. uc_pdata = dev_get_uclass_platdata(dev);
  67. if (uc_pdata->min_uV > uc_pdata->max_uV) {
  68. debug("Invalid constraints for: %s\n", uc_pdata->name);
  69. return -EINVAL;
  70. }
  71. enable = dm_gpio_get_value(&dev_pdata->gpio);
  72. if (enable == dev_pdata->states[0])
  73. return dev_pdata->voltages[0];
  74. else
  75. return dev_pdata->voltages[1];
  76. }
  77. static int gpio_regulator_set_value(struct udevice *dev, int uV)
  78. {
  79. struct gpio_regulator_platdata *dev_pdata = dev_get_platdata(dev);
  80. int ret;
  81. bool enable;
  82. if (!dev_pdata->gpio.dev)
  83. return -ENOSYS;
  84. if (uV == dev_pdata->voltages[0])
  85. enable = dev_pdata->states[0];
  86. else if (uV == dev_pdata->voltages[1])
  87. enable = dev_pdata->states[1];
  88. else
  89. return -EINVAL;
  90. ret = dm_gpio_set_value(&dev_pdata->gpio, enable);
  91. if (ret) {
  92. error("Can't set regulator : %s gpio to: %d\n", dev->name,
  93. enable);
  94. return ret;
  95. }
  96. return 0;
  97. }
  98. static const struct dm_regulator_ops gpio_regulator_ops = {
  99. .get_value = gpio_regulator_get_value,
  100. .set_value = gpio_regulator_set_value,
  101. };
  102. static const struct udevice_id gpio_regulator_ids[] = {
  103. { .compatible = "regulator-gpio" },
  104. { },
  105. };
  106. U_BOOT_DRIVER(gpio_regulator) = {
  107. .name = "gpio regulator",
  108. .id = UCLASS_REGULATOR,
  109. .ops = &gpio_regulator_ops,
  110. .of_match = gpio_regulator_ids,
  111. .ofdata_to_platdata = gpio_regulator_ofdata_to_platdata,
  112. .platdata_auto_alloc_size = sizeof(struct gpio_regulator_platdata),
  113. };