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 | |
Keerthy | 17b3fe7 | 2016-09-15 17:04:06 +0530 | [diff] [blame] | 7 | #include <fdtdec.h> |
| 8 | #include <errno.h> |
| 9 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 10 | #include <log.h> |
Keerthy | 17b3fe7 | 2016-09-15 17:04:06 +0530 | [diff] [blame] | 11 | #include <asm/gpio.h> |
Simon Glass | bdd5f81 | 2023-09-14 18:21:46 -0600 | [diff] [blame] | 12 | #include <linux/printk.h> |
Keerthy | 17b3fe7 | 2016-09-15 17:04:06 +0530 | [diff] [blame] | 13 | #include <power/pmic.h> |
| 14 | #include <power/regulator.h> |
Simon Glass | bdd5f81 | 2023-09-14 18:21:46 -0600 | [diff] [blame] | 15 | #include "regulator_common.h" |
Keerthy | 17b3fe7 | 2016-09-15 17:04:06 +0530 | [diff] [blame] | 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 | |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 21 | struct gpio_regulator_plat { |
| 22 | struct regulator_common_plat common; |
Keerthy | 17b3fe7 | 2016-09-15 17:04:06 +0530 | [diff] [blame] | 23 | struct gpio_desc gpio; /* GPIO for regulator voltage control */ |
| 24 | int states[GPIO_REGULATOR_MAX_STATES]; |
| 25 | int voltages[GPIO_REGULATOR_MAX_STATES]; |
| 26 | }; |
| 27 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 28 | static int gpio_regulator_of_to_plat(struct udevice *dev) |
Keerthy | 17b3fe7 | 2016-09-15 17:04:06 +0530 | [diff] [blame] | 29 | { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 30 | struct dm_regulator_uclass_plat *uc_pdata; |
Eugen Hristev | 81aa192 | 2023-04-19 16:45:25 +0300 | [diff] [blame] | 31 | struct gpio_regulator_plat *plat; |
Keerthy | 17b3fe7 | 2016-09-15 17:04:06 +0530 | [diff] [blame] | 32 | struct gpio_desc *gpio; |
Keerthy | 17b3fe7 | 2016-09-15 17:04:06 +0530 | [diff] [blame] | 33 | int ret, count, i, j; |
Patrick Delaunay | 23f988a | 2020-09-10 18:18:16 +0200 | [diff] [blame] | 34 | u32 states_array[GPIO_REGULATOR_MAX_STATES * 2]; |
Keerthy | 17b3fe7 | 2016-09-15 17:04:06 +0530 | [diff] [blame] | 35 | |
Eugen Hristev | 81aa192 | 2023-04-19 16:45:25 +0300 | [diff] [blame] | 36 | plat = dev_get_plat(dev); |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 37 | uc_pdata = dev_get_uclass_plat(dev); |
Keerthy | 17b3fe7 | 2016-09-15 17:04:06 +0530 | [diff] [blame] | 38 | if (!uc_pdata) |
| 39 | return -ENXIO; |
| 40 | |
| 41 | /* Set type to gpio */ |
| 42 | uc_pdata->type = REGULATOR_TYPE_GPIO; |
| 43 | |
| 44 | /* |
| 45 | * Get gpio regulator gpio desc |
| 46 | * Assuming one GPIO per regulator. |
| 47 | * Can be extended later to multiple GPIOs |
| 48 | * per gpio-regulator. As of now no instance with multiple |
| 49 | * gpios is presnt |
| 50 | */ |
Eugen Hristev | 81aa192 | 2023-04-19 16:45:25 +0300 | [diff] [blame] | 51 | gpio = &plat->gpio; |
Keerthy | 17b3fe7 | 2016-09-15 17:04:06 +0530 | [diff] [blame] | 52 | ret = gpio_request_by_name(dev, "gpios", 0, gpio, GPIOD_IS_OUT); |
| 53 | if (ret) |
| 54 | debug("regulator gpio - not found! Error: %d", ret); |
| 55 | |
Patrick Delaunay | deb5b48 | 2020-09-10 18:18:17 +0200 | [diff] [blame] | 56 | ret = dev_read_size(dev, "states"); |
| 57 | if (ret < 0) |
| 58 | return ret; |
| 59 | |
| 60 | count = ret / sizeof(states_array[0]); |
| 61 | if (count > ARRAY_SIZE(states_array)) { |
| 62 | debug("regulator gpio - to many states (%d > %d)", |
| 63 | count / 2, GPIO_REGULATOR_MAX_STATES); |
| 64 | count = ARRAY_SIZE(states_array); |
| 65 | } |
Keerthy | 17b3fe7 | 2016-09-15 17:04:06 +0530 | [diff] [blame] | 66 | |
Patrick Delaunay | deb5b48 | 2020-09-10 18:18:17 +0200 | [diff] [blame] | 67 | ret = dev_read_u32_array(dev, "states", states_array, count); |
| 68 | if (ret < 0) |
| 69 | return ret; |
Keerthy | 17b3fe7 | 2016-09-15 17:04:06 +0530 | [diff] [blame] | 70 | |
| 71 | for (i = 0, j = 0; i < count; i += 2) { |
Eugen Hristev | 81aa192 | 2023-04-19 16:45:25 +0300 | [diff] [blame] | 72 | plat->voltages[j] = states_array[i]; |
| 73 | plat->states[j] = states_array[i + 1]; |
Keerthy | 17b3fe7 | 2016-09-15 17:04:06 +0530 | [diff] [blame] | 74 | j++; |
| 75 | } |
| 76 | |
Eugen Hristev | 81aa192 | 2023-04-19 16:45:25 +0300 | [diff] [blame] | 77 | return regulator_common_of_to_plat(dev, &plat->common, "enable-gpios"); |
Keerthy | 17b3fe7 | 2016-09-15 17:04:06 +0530 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | static int gpio_regulator_get_value(struct udevice *dev) |
| 81 | { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 82 | struct dm_regulator_uclass_plat *uc_pdata; |
Eugen Hristev | 81aa192 | 2023-04-19 16:45:25 +0300 | [diff] [blame] | 83 | struct gpio_regulator_plat *plat = dev_get_plat(dev); |
Keerthy | 17b3fe7 | 2016-09-15 17:04:06 +0530 | [diff] [blame] | 84 | int enable; |
| 85 | |
Eugen Hristev | 81aa192 | 2023-04-19 16:45:25 +0300 | [diff] [blame] | 86 | if (!plat->gpio.dev) |
Keerthy | 17b3fe7 | 2016-09-15 17:04:06 +0530 | [diff] [blame] | 87 | return -ENOSYS; |
| 88 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 89 | uc_pdata = dev_get_uclass_plat(dev); |
Keerthy | 17b3fe7 | 2016-09-15 17:04:06 +0530 | [diff] [blame] | 90 | if (uc_pdata->min_uV > uc_pdata->max_uV) { |
| 91 | debug("Invalid constraints for: %s\n", uc_pdata->name); |
| 92 | return -EINVAL; |
| 93 | } |
| 94 | |
Eugen Hristev | 81aa192 | 2023-04-19 16:45:25 +0300 | [diff] [blame] | 95 | enable = dm_gpio_get_value(&plat->gpio); |
| 96 | if (enable == plat->states[0]) |
| 97 | return plat->voltages[0]; |
Keerthy | 17b3fe7 | 2016-09-15 17:04:06 +0530 | [diff] [blame] | 98 | else |
Eugen Hristev | 81aa192 | 2023-04-19 16:45:25 +0300 | [diff] [blame] | 99 | return plat->voltages[1]; |
Keerthy | 17b3fe7 | 2016-09-15 17:04:06 +0530 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | static int gpio_regulator_set_value(struct udevice *dev, int uV) |
| 103 | { |
Eugen Hristev | 81aa192 | 2023-04-19 16:45:25 +0300 | [diff] [blame] | 104 | struct gpio_regulator_plat *plat = dev_get_plat(dev); |
Keerthy | 17b3fe7 | 2016-09-15 17:04:06 +0530 | [diff] [blame] | 105 | int ret; |
| 106 | bool enable; |
| 107 | |
Eugen Hristev | 81aa192 | 2023-04-19 16:45:25 +0300 | [diff] [blame] | 108 | if (!plat->gpio.dev) |
Keerthy | 17b3fe7 | 2016-09-15 17:04:06 +0530 | [diff] [blame] | 109 | return -ENOSYS; |
| 110 | |
Eugen Hristev | 81aa192 | 2023-04-19 16:45:25 +0300 | [diff] [blame] | 111 | if (uV == plat->voltages[0]) |
| 112 | enable = plat->states[0]; |
| 113 | else if (uV == plat->voltages[1]) |
| 114 | enable = plat->states[1]; |
Keerthy | 17b3fe7 | 2016-09-15 17:04:06 +0530 | [diff] [blame] | 115 | else |
| 116 | return -EINVAL; |
| 117 | |
Eugen Hristev | 81aa192 | 2023-04-19 16:45:25 +0300 | [diff] [blame] | 118 | ret = dm_gpio_set_value(&plat->gpio, enable); |
Keerthy | 17b3fe7 | 2016-09-15 17:04:06 +0530 | [diff] [blame] | 119 | if (ret) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 120 | pr_err("Can't set regulator : %s gpio to: %d\n", dev->name, |
Keerthy | 17b3fe7 | 2016-09-15 17:04:06 +0530 | [diff] [blame] | 121 | enable); |
| 122 | return ret; |
| 123 | } |
| 124 | |
| 125 | return 0; |
| 126 | } |
| 127 | |
Sven Schwermer | db58d9e | 2019-06-24 13:03:34 +0200 | [diff] [blame] | 128 | static int gpio_regulator_get_enable(struct udevice *dev) |
| 129 | { |
Eugen Hristev | 81aa192 | 2023-04-19 16:45:25 +0300 | [diff] [blame] | 130 | struct gpio_regulator_plat *plat = dev_get_plat(dev); |
| 131 | return regulator_common_get_enable(dev, &plat->common); |
Sven Schwermer | db58d9e | 2019-06-24 13:03:34 +0200 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | static int gpio_regulator_set_enable(struct udevice *dev, bool enable) |
| 135 | { |
Eugen Hristev | 81aa192 | 2023-04-19 16:45:25 +0300 | [diff] [blame] | 136 | struct gpio_regulator_plat *plat = dev_get_plat(dev); |
| 137 | return regulator_common_set_enable(dev, &plat->common, enable); |
Sven Schwermer | db58d9e | 2019-06-24 13:03:34 +0200 | [diff] [blame] | 138 | } |
| 139 | |
Keerthy | 17b3fe7 | 2016-09-15 17:04:06 +0530 | [diff] [blame] | 140 | static const struct dm_regulator_ops gpio_regulator_ops = { |
| 141 | .get_value = gpio_regulator_get_value, |
| 142 | .set_value = gpio_regulator_set_value, |
Sven Schwermer | db58d9e | 2019-06-24 13:03:34 +0200 | [diff] [blame] | 143 | .get_enable = gpio_regulator_get_enable, |
| 144 | .set_enable = gpio_regulator_set_enable, |
Keerthy | 17b3fe7 | 2016-09-15 17:04:06 +0530 | [diff] [blame] | 145 | }; |
| 146 | |
| 147 | static const struct udevice_id gpio_regulator_ids[] = { |
| 148 | { .compatible = "regulator-gpio" }, |
| 149 | { }, |
| 150 | }; |
| 151 | |
| 152 | U_BOOT_DRIVER(gpio_regulator) = { |
| 153 | .name = "gpio regulator", |
| 154 | .id = UCLASS_REGULATOR, |
| 155 | .ops = &gpio_regulator_ops, |
| 156 | .of_match = gpio_regulator_ids, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 157 | .of_to_plat = gpio_regulator_of_to_plat, |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 158 | .plat_auto = sizeof(struct gpio_regulator_plat), |
Keerthy | 17b3fe7 | 2016-09-15 17:04:06 +0530 | [diff] [blame] | 159 | }; |