gpio-regulator.c 3.2 KB

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