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