Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Keerthy | 17b3fe7 | 2016-09-15 17:04:06 +0530 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2016 Texas Instruments Incorporated, <www.ti.com> |
| 4 | * Keerthy <j-keerthy@ti.com> |
Keerthy | 17b3fe7 | 2016-09-15 17:04:06 +0530 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <fdtdec.h> |
| 9 | #include <errno.h> |
| 10 | #include <dm.h> |
| 11 | #include <i2c.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Keerthy | 17b3fe7 | 2016-09-15 17:04:06 +0530 | [diff] [blame] | 13 | #include <asm/gpio.h> |
| 14 | #include <power/pmic.h> |
| 15 | #include <power/regulator.h> |
| 16 | |
Simon Glass | 1c1ddf6 | 2020-07-19 10:15:44 -0600 | [diff] [blame] | 17 | #include "regulator_common.h" |
| 18 | |
Keerthy | 17b3fe7 | 2016-09-15 17:04:06 +0530 | [diff] [blame] | 19 | #define GPIO_REGULATOR_MAX_STATES 2 |
| 20 | |
| 21 | DECLARE_GLOBAL_DATA_PTR; |
| 22 | |
| 23 | struct gpio_regulator_platdata { |
Sven Schwermer | db58d9e | 2019-06-24 13:03:34 +0200 | [diff] [blame] | 24 | struct regulator_common_platdata common; |
Keerthy | 17b3fe7 | 2016-09-15 17:04:06 +0530 | [diff] [blame] | 25 | struct gpio_desc gpio; /* GPIO for regulator voltage control */ |
| 26 | int states[GPIO_REGULATOR_MAX_STATES]; |
| 27 | int voltages[GPIO_REGULATOR_MAX_STATES]; |
| 28 | }; |
| 29 | |
| 30 | static int gpio_regulator_ofdata_to_platdata(struct udevice *dev) |
| 31 | { |
| 32 | struct dm_regulator_uclass_platdata *uc_pdata; |
| 33 | struct gpio_regulator_platdata *dev_pdata; |
| 34 | struct gpio_desc *gpio; |
| 35 | const void *blob = gd->fdt_blob; |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 36 | int node = dev_of_offset(dev); |
Keerthy | 17b3fe7 | 2016-09-15 17:04:06 +0530 | [diff] [blame] | 37 | int ret, count, i, j; |
| 38 | u32 states_array[8]; |
| 39 | |
| 40 | dev_pdata = dev_get_platdata(dev); |
| 41 | uc_pdata = dev_get_uclass_platdata(dev); |
| 42 | if (!uc_pdata) |
| 43 | return -ENXIO; |
| 44 | |
| 45 | /* Set type to gpio */ |
| 46 | uc_pdata->type = REGULATOR_TYPE_GPIO; |
| 47 | |
| 48 | /* |
| 49 | * Get gpio regulator gpio desc |
| 50 | * Assuming one GPIO per regulator. |
| 51 | * Can be extended later to multiple GPIOs |
| 52 | * per gpio-regulator. As of now no instance with multiple |
| 53 | * gpios is presnt |
| 54 | */ |
| 55 | gpio = &dev_pdata->gpio; |
| 56 | ret = gpio_request_by_name(dev, "gpios", 0, gpio, GPIOD_IS_OUT); |
| 57 | if (ret) |
| 58 | debug("regulator gpio - not found! Error: %d", ret); |
| 59 | |
| 60 | count = fdtdec_get_int_array_count(blob, node, "states", |
| 61 | states_array, 8); |
| 62 | |
| 63 | if (!count) |
| 64 | return -EINVAL; |
| 65 | |
| 66 | for (i = 0, j = 0; i < count; i += 2) { |
| 67 | dev_pdata->voltages[j] = states_array[i]; |
| 68 | dev_pdata->states[j] = states_array[i + 1]; |
| 69 | j++; |
| 70 | } |
| 71 | |
Sven Schwermer | db58d9e | 2019-06-24 13:03:34 +0200 | [diff] [blame] | 72 | return regulator_common_ofdata_to_platdata(dev, &dev_pdata->common, "enable-gpios"); |
Keerthy | 17b3fe7 | 2016-09-15 17:04:06 +0530 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | static int gpio_regulator_get_value(struct udevice *dev) |
| 76 | { |
| 77 | struct dm_regulator_uclass_platdata *uc_pdata; |
| 78 | struct gpio_regulator_platdata *dev_pdata = dev_get_platdata(dev); |
| 79 | int enable; |
| 80 | |
| 81 | if (!dev_pdata->gpio.dev) |
| 82 | return -ENOSYS; |
| 83 | |
| 84 | uc_pdata = dev_get_uclass_platdata(dev); |
| 85 | if (uc_pdata->min_uV > uc_pdata->max_uV) { |
| 86 | debug("Invalid constraints for: %s\n", uc_pdata->name); |
| 87 | return -EINVAL; |
| 88 | } |
| 89 | |
| 90 | enable = dm_gpio_get_value(&dev_pdata->gpio); |
| 91 | if (enable == dev_pdata->states[0]) |
| 92 | return dev_pdata->voltages[0]; |
| 93 | else |
| 94 | return dev_pdata->voltages[1]; |
| 95 | } |
| 96 | |
| 97 | static int gpio_regulator_set_value(struct udevice *dev, int uV) |
| 98 | { |
| 99 | struct gpio_regulator_platdata *dev_pdata = dev_get_platdata(dev); |
| 100 | int ret; |
| 101 | bool enable; |
| 102 | |
| 103 | if (!dev_pdata->gpio.dev) |
| 104 | return -ENOSYS; |
| 105 | |
| 106 | if (uV == dev_pdata->voltages[0]) |
| 107 | enable = dev_pdata->states[0]; |
| 108 | else if (uV == dev_pdata->voltages[1]) |
| 109 | enable = dev_pdata->states[1]; |
| 110 | else |
| 111 | return -EINVAL; |
| 112 | |
| 113 | ret = dm_gpio_set_value(&dev_pdata->gpio, enable); |
| 114 | if (ret) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 115 | pr_err("Can't set regulator : %s gpio to: %d\n", dev->name, |
Keerthy | 17b3fe7 | 2016-09-15 17:04:06 +0530 | [diff] [blame] | 116 | enable); |
| 117 | return ret; |
| 118 | } |
| 119 | |
| 120 | return 0; |
| 121 | } |
| 122 | |
Sven Schwermer | db58d9e | 2019-06-24 13:03:34 +0200 | [diff] [blame] | 123 | static int gpio_regulator_get_enable(struct udevice *dev) |
| 124 | { |
| 125 | struct gpio_regulator_platdata *dev_pdata = dev_get_platdata(dev); |
| 126 | return regulator_common_get_enable(dev, &dev_pdata->common); |
| 127 | } |
| 128 | |
| 129 | static int gpio_regulator_set_enable(struct udevice *dev, bool enable) |
| 130 | { |
| 131 | struct gpio_regulator_platdata *dev_pdata = dev_get_platdata(dev); |
| 132 | return regulator_common_set_enable(dev, &dev_pdata->common, enable); |
| 133 | } |
| 134 | |
Keerthy | 17b3fe7 | 2016-09-15 17:04:06 +0530 | [diff] [blame] | 135 | static const struct dm_regulator_ops gpio_regulator_ops = { |
| 136 | .get_value = gpio_regulator_get_value, |
| 137 | .set_value = gpio_regulator_set_value, |
Sven Schwermer | db58d9e | 2019-06-24 13:03:34 +0200 | [diff] [blame] | 138 | .get_enable = gpio_regulator_get_enable, |
| 139 | .set_enable = gpio_regulator_set_enable, |
Keerthy | 17b3fe7 | 2016-09-15 17:04:06 +0530 | [diff] [blame] | 140 | }; |
| 141 | |
| 142 | static const struct udevice_id gpio_regulator_ids[] = { |
| 143 | { .compatible = "regulator-gpio" }, |
| 144 | { }, |
| 145 | }; |
| 146 | |
| 147 | U_BOOT_DRIVER(gpio_regulator) = { |
| 148 | .name = "gpio regulator", |
| 149 | .id = UCLASS_REGULATOR, |
| 150 | .ops = &gpio_regulator_ops, |
| 151 | .of_match = gpio_regulator_ids, |
| 152 | .ofdata_to_platdata = gpio_regulator_ofdata_to_platdata, |
| 153 | .platdata_auto_alloc_size = sizeof(struct gpio_regulator_platdata), |
| 154 | }; |