Heiko Stübner | 3f0292a | 2011-10-05 12:27:05 +0200 | [diff] [blame] | 1 | /* |
| 2 | * gpio-regulator.c |
| 3 | * |
| 4 | * Copyright 2011 Heiko Stuebner <heiko@sntech.de> |
| 5 | * |
| 6 | * based on fixed.c |
| 7 | * |
| 8 | * Copyright 2008 Wolfson Microelectronics PLC. |
| 9 | * |
| 10 | * Author: Mark Brown <broonie@opensource.wolfsonmicro.com> |
| 11 | * |
| 12 | * Copyright (c) 2009 Nokia Corporation |
| 13 | * Roger Quadros <ext-roger.quadros@nokia.com> |
| 14 | * |
| 15 | * This program is free software; you can redistribute it and/or |
| 16 | * modify it under the terms of the GNU General Public License as |
| 17 | * published by the Free Software Foundation; either version 2 of the |
| 18 | * License, or (at your option) any later version. |
| 19 | * |
| 20 | * This is useful for systems with mixed controllable and |
| 21 | * non-controllable regulators, as well as for allowing testing on |
| 22 | * systems with no controllable regulators. |
| 23 | */ |
| 24 | |
| 25 | #include <linux/err.h> |
| 26 | #include <linux/mutex.h> |
Mark Brown | ecc37ed | 2011-10-11 13:59:13 +0100 | [diff] [blame] | 27 | #include <linux/module.h> |
Heiko Stübner | 3f0292a | 2011-10-05 12:27:05 +0200 | [diff] [blame] | 28 | #include <linux/platform_device.h> |
| 29 | #include <linux/regulator/driver.h> |
| 30 | #include <linux/regulator/machine.h> |
Lee Jones | 006694d | 2012-10-15 14:16:59 +0100 | [diff] [blame] | 31 | #include <linux/regulator/of_regulator.h> |
Heiko Stübner | 3f0292a | 2011-10-05 12:27:05 +0200 | [diff] [blame] | 32 | #include <linux/regulator/gpio-regulator.h> |
Linus Walleij | d6cd33a | 2019-01-29 11:31:52 +0100 | [diff] [blame] | 33 | #include <linux/gpio/consumer.h> |
Heiko Stübner | 3f0292a | 2011-10-05 12:27:05 +0200 | [diff] [blame] | 34 | #include <linux/slab.h> |
Lee Jones | 006694d | 2012-10-15 14:16:59 +0100 | [diff] [blame] | 35 | #include <linux/of.h> |
Heiko Stübner | 3f0292a | 2011-10-05 12:27:05 +0200 | [diff] [blame] | 36 | |
| 37 | struct gpio_regulator_data { |
| 38 | struct regulator_desc desc; |
| 39 | struct regulator_dev *dev; |
| 40 | |
Linus Walleij | d6cd33a | 2019-01-29 11:31:52 +0100 | [diff] [blame] | 41 | struct gpio_desc **gpiods; |
Heiko Stübner | 3f0292a | 2011-10-05 12:27:05 +0200 | [diff] [blame] | 42 | int nr_gpios; |
| 43 | |
| 44 | struct gpio_regulator_state *states; |
| 45 | int nr_states; |
| 46 | |
| 47 | int state; |
| 48 | }; |
| 49 | |
Heiko Stübner | 3f0292a | 2011-10-05 12:27:05 +0200 | [diff] [blame] | 50 | static int gpio_regulator_get_value(struct regulator_dev *dev) |
| 51 | { |
| 52 | struct gpio_regulator_data *data = rdev_get_drvdata(dev); |
| 53 | int ptr; |
| 54 | |
| 55 | for (ptr = 0; ptr < data->nr_states; ptr++) |
| 56 | if (data->states[ptr].gpios == data->state) |
| 57 | return data->states[ptr].value; |
| 58 | |
| 59 | return -EINVAL; |
| 60 | } |
| 61 | |
Heiko Stübner | eb0c568 | 2012-08-08 00:50:19 +0200 | [diff] [blame] | 62 | static int gpio_regulator_set_voltage(struct regulator_dev *dev, |
| 63 | int min_uV, int max_uV, |
| 64 | unsigned *selector) |
Heiko Stübner | 3f0292a | 2011-10-05 12:27:05 +0200 | [diff] [blame] | 65 | { |
| 66 | struct gpio_regulator_data *data = rdev_get_drvdata(dev); |
Heiko Stübner | 0092636 | 2012-06-03 21:31:09 +0200 | [diff] [blame] | 67 | int ptr, target = 0, state, best_val = INT_MAX; |
Heiko Stübner | 3f0292a | 2011-10-05 12:27:05 +0200 | [diff] [blame] | 68 | |
Heiko Stübner | 3f0292a | 2011-10-05 12:27:05 +0200 | [diff] [blame] | 69 | for (ptr = 0; ptr < data->nr_states; ptr++) |
Axel Lin | 4dbd8f6 | 2012-03-22 14:08:04 +0800 | [diff] [blame] | 70 | if (data->states[ptr].value < best_val && |
Heiko Stübner | eb0c568 | 2012-08-08 00:50:19 +0200 | [diff] [blame] | 71 | data->states[ptr].value >= min_uV && |
| 72 | data->states[ptr].value <= max_uV) { |
Heiko Stübner | 3f0292a | 2011-10-05 12:27:05 +0200 | [diff] [blame] | 73 | target = data->states[ptr].gpios; |
Heiko Stübner | 0092636 | 2012-06-03 21:31:09 +0200 | [diff] [blame] | 74 | best_val = data->states[ptr].value; |
Heiko Stübner | b0e4d7b | 2012-06-03 21:32:05 +0200 | [diff] [blame] | 75 | if (selector) |
| 76 | *selector = ptr; |
Heiko Stübner | 0092636 | 2012-06-03 21:31:09 +0200 | [diff] [blame] | 77 | } |
Heiko Stübner | 3f0292a | 2011-10-05 12:27:05 +0200 | [diff] [blame] | 78 | |
Axel Lin | 4dbd8f6 | 2012-03-22 14:08:04 +0800 | [diff] [blame] | 79 | if (best_val == INT_MAX) |
Heiko Stübner | 3f0292a | 2011-10-05 12:27:05 +0200 | [diff] [blame] | 80 | return -EINVAL; |
| 81 | |
| 82 | for (ptr = 0; ptr < data->nr_gpios; ptr++) { |
| 83 | state = (target & (1 << ptr)) >> ptr; |
Linus Walleij | d6cd33a | 2019-01-29 11:31:52 +0100 | [diff] [blame] | 84 | gpiod_set_value_cansleep(data->gpiods[ptr], state); |
Heiko Stübner | 3f0292a | 2011-10-05 12:27:05 +0200 | [diff] [blame] | 85 | } |
| 86 | data->state = target; |
| 87 | |
| 88 | return 0; |
| 89 | } |
| 90 | |
Heiko Stübner | 3f0292a | 2011-10-05 12:27:05 +0200 | [diff] [blame] | 91 | static int gpio_regulator_list_voltage(struct regulator_dev *dev, |
| 92 | unsigned selector) |
| 93 | { |
| 94 | struct gpio_regulator_data *data = rdev_get_drvdata(dev); |
| 95 | |
| 96 | if (selector >= data->nr_states) |
| 97 | return -EINVAL; |
| 98 | |
| 99 | return data->states[selector].value; |
| 100 | } |
| 101 | |
| 102 | static int gpio_regulator_set_current_limit(struct regulator_dev *dev, |
| 103 | int min_uA, int max_uA) |
| 104 | { |
Heiko Stübner | eb0c568 | 2012-08-08 00:50:19 +0200 | [diff] [blame] | 105 | struct gpio_regulator_data *data = rdev_get_drvdata(dev); |
| 106 | int ptr, target = 0, state, best_val = 0; |
| 107 | |
| 108 | for (ptr = 0; ptr < data->nr_states; ptr++) |
| 109 | if (data->states[ptr].value > best_val && |
| 110 | data->states[ptr].value >= min_uA && |
| 111 | data->states[ptr].value <= max_uA) { |
| 112 | target = data->states[ptr].gpios; |
| 113 | best_val = data->states[ptr].value; |
| 114 | } |
| 115 | |
| 116 | if (best_val == 0) |
| 117 | return -EINVAL; |
| 118 | |
| 119 | for (ptr = 0; ptr < data->nr_gpios; ptr++) { |
| 120 | state = (target & (1 << ptr)) >> ptr; |
Linus Walleij | d6cd33a | 2019-01-29 11:31:52 +0100 | [diff] [blame] | 121 | gpiod_set_value_cansleep(data->gpiods[ptr], state); |
Heiko Stübner | eb0c568 | 2012-08-08 00:50:19 +0200 | [diff] [blame] | 122 | } |
| 123 | data->state = target; |
| 124 | |
| 125 | return 0; |
Heiko Stübner | 3f0292a | 2011-10-05 12:27:05 +0200 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | static struct regulator_ops gpio_regulator_voltage_ops = { |
Heiko Stübner | 3f0292a | 2011-10-05 12:27:05 +0200 | [diff] [blame] | 129 | .get_voltage = gpio_regulator_get_value, |
| 130 | .set_voltage = gpio_regulator_set_voltage, |
| 131 | .list_voltage = gpio_regulator_list_voltage, |
| 132 | }; |
| 133 | |
Axel Lin | a451405 | 2013-01-28 22:17:46 +0800 | [diff] [blame] | 134 | static struct gpio_regulator_config * |
Javier Martinez Canillas | 072e78b | 2014-11-10 14:43:53 +0100 | [diff] [blame] | 135 | of_get_gpio_regulator_config(struct device *dev, struct device_node *np, |
| 136 | const struct regulator_desc *desc) |
Lee Jones | 006694d | 2012-10-15 14:16:59 +0100 | [diff] [blame] | 137 | { |
| 138 | struct gpio_regulator_config *config; |
Lee Jones | 006694d | 2012-10-15 14:16:59 +0100 | [diff] [blame] | 139 | const char *regtype; |
Linus Walleij | d6cd33a | 2019-01-29 11:31:52 +0100 | [diff] [blame] | 140 | int proplen, i; |
| 141 | int ngpios; |
Laurent Pinchart | 251b9c2 | 2013-11-09 13:12:28 +0100 | [diff] [blame] | 142 | int ret; |
Lee Jones | 006694d | 2012-10-15 14:16:59 +0100 | [diff] [blame] | 143 | |
| 144 | config = devm_kzalloc(dev, |
| 145 | sizeof(struct gpio_regulator_config), |
| 146 | GFP_KERNEL); |
| 147 | if (!config) |
| 148 | return ERR_PTR(-ENOMEM); |
| 149 | |
Javier Martinez Canillas | 072e78b | 2014-11-10 14:43:53 +0100 | [diff] [blame] | 150 | config->init_data = of_get_regulator_init_data(dev, np, desc); |
Lee Jones | 006694d | 2012-10-15 14:16:59 +0100 | [diff] [blame] | 151 | if (!config->init_data) |
| 152 | return ERR_PTR(-EINVAL); |
| 153 | |
| 154 | config->supply_name = config->init_data->constraints.name; |
| 155 | |
Lee Jones | 006694d | 2012-10-15 14:16:59 +0100 | [diff] [blame] | 156 | if (of_property_read_bool(np, "enable-at-boot")) |
| 157 | config->enabled_at_boot = true; |
| 158 | |
| 159 | of_property_read_u32(np, "startup-delay-us", &config->startup_delay); |
| 160 | |
Linus Walleij | d6cd33a | 2019-01-29 11:31:52 +0100 | [diff] [blame] | 161 | /* Fetch GPIO init levels */ |
| 162 | ngpios = gpiod_count(dev, NULL); |
| 163 | if (ngpios > 0) { |
| 164 | config->gflags = devm_kzalloc(dev, |
| 165 | sizeof(enum gpiod_flags) |
| 166 | * ngpios, |
| 167 | GFP_KERNEL); |
| 168 | if (!config->gflags) |
Richard Fitzgerald | 9f94609 | 2014-11-19 14:13:06 +0000 | [diff] [blame] | 169 | return ERR_PTR(-ENOMEM); |
Lee Jones | 006694d | 2012-10-15 14:16:59 +0100 | [diff] [blame] | 170 | |
Linus Walleij | d6cd33a | 2019-01-29 11:31:52 +0100 | [diff] [blame] | 171 | for (i = 0; i < ngpios; i++) { |
| 172 | u32 val; |
Heiko Stuebner | 1f5a962 | 2014-02-13 16:34:32 +0100 | [diff] [blame] | 173 | |
Linus Walleij | d6cd33a | 2019-01-29 11:31:52 +0100 | [diff] [blame] | 174 | ret = of_property_read_u32_index(np, "gpios-states", i, |
| 175 | &val); |
Kuninori Morimoto | 0094050 | 2014-01-30 21:25:14 -0800 | [diff] [blame] | 176 | |
Linus Walleij | d6cd33a | 2019-01-29 11:31:52 +0100 | [diff] [blame] | 177 | /* Default to high per specification */ |
| 178 | if (ret) |
| 179 | config->gflags[i] = GPIOD_OUT_HIGH; |
| 180 | else |
| 181 | config->gflags[i] = |
| 182 | val ? GPIOD_OUT_HIGH : GPIOD_OUT_LOW; |
Heiko Stuebner | 1f5a962 | 2014-02-13 16:34:32 +0100 | [diff] [blame] | 183 | } |
Lee Jones | 006694d | 2012-10-15 14:16:59 +0100 | [diff] [blame] | 184 | } |
Linus Walleij | d6cd33a | 2019-01-29 11:31:52 +0100 | [diff] [blame] | 185 | config->ngpios = ngpios; |
Lee Jones | 006694d | 2012-10-15 14:16:59 +0100 | [diff] [blame] | 186 | |
| 187 | /* Fetch states. */ |
Heiko Stuebner | 934624d | 2014-02-12 01:01:08 +0100 | [diff] [blame] | 188 | proplen = of_property_count_u32_elems(np, "states"); |
| 189 | if (proplen < 0) { |
Lee Jones | 216f2b9 | 2012-11-14 11:51:36 +0000 | [diff] [blame] | 190 | dev_err(dev, "No 'states' property found\n"); |
| 191 | return ERR_PTR(-EINVAL); |
| 192 | } |
| 193 | |
Kees Cook | a86854d | 2018-06-12 14:07:58 -0700 | [diff] [blame] | 194 | config->states = devm_kcalloc(dev, |
| 195 | proplen / 2, |
| 196 | sizeof(struct gpio_regulator_state), |
Lee Jones | 006694d | 2012-10-15 14:16:59 +0100 | [diff] [blame] | 197 | GFP_KERNEL); |
| 198 | if (!config->states) |
| 199 | return ERR_PTR(-ENOMEM); |
| 200 | |
| 201 | for (i = 0; i < proplen / 2; i++) { |
Heiko Stuebner | 934624d | 2014-02-12 01:01:08 +0100 | [diff] [blame] | 202 | of_property_read_u32_index(np, "states", i * 2, |
| 203 | &config->states[i].value); |
| 204 | of_property_read_u32_index(np, "states", i * 2 + 1, |
| 205 | &config->states[i].gpios); |
Lee Jones | 006694d | 2012-10-15 14:16:59 +0100 | [diff] [blame] | 206 | } |
| 207 | config->nr_states = i; |
| 208 | |
Mark Brown | 5b1ada8 | 2013-12-05 00:29:57 +0000 | [diff] [blame] | 209 | config->type = REGULATOR_VOLTAGE; |
Laurent Pinchart | 251b9c2 | 2013-11-09 13:12:28 +0100 | [diff] [blame] | 210 | ret = of_property_read_string(np, "regulator-type", ®type); |
Mark Brown | 5b1ada8 | 2013-12-05 00:29:57 +0000 | [diff] [blame] | 211 | if (ret >= 0) { |
| 212 | if (!strncmp("voltage", regtype, 7)) |
| 213 | config->type = REGULATOR_VOLTAGE; |
| 214 | else if (!strncmp("current", regtype, 7)) |
| 215 | config->type = REGULATOR_CURRENT; |
Mark Brown | 9eb9d31 | 2013-12-05 19:09:55 +0000 | [diff] [blame] | 216 | else |
| 217 | dev_warn(dev, "Unknown regulator-type '%s'\n", |
| 218 | regtype); |
Laurent Pinchart | 251b9c2 | 2013-11-09 13:12:28 +0100 | [diff] [blame] | 219 | } |
Lee Jones | 006694d | 2012-10-15 14:16:59 +0100 | [diff] [blame] | 220 | |
Lee Jones | 006694d | 2012-10-15 14:16:59 +0100 | [diff] [blame] | 221 | return config; |
| 222 | } |
| 223 | |
Heiko Stübner | 3f0292a | 2011-10-05 12:27:05 +0200 | [diff] [blame] | 224 | static struct regulator_ops gpio_regulator_current_ops = { |
Heiko Stübner | 3f0292a | 2011-10-05 12:27:05 +0200 | [diff] [blame] | 225 | .get_current_limit = gpio_regulator_get_value, |
| 226 | .set_current_limit = gpio_regulator_set_current_limit, |
| 227 | }; |
| 228 | |
Bill Pemberton | a502357 | 2012-11-19 13:22:22 -0500 | [diff] [blame] | 229 | static int gpio_regulator_probe(struct platform_device *pdev) |
Heiko Stübner | 3f0292a | 2011-10-05 12:27:05 +0200 | [diff] [blame] | 230 | { |
Linus Walleij | d162d04 | 2019-01-29 11:31:55 +0100 | [diff] [blame^] | 231 | struct device *dev = &pdev->dev; |
| 232 | struct gpio_regulator_config *config = dev_get_platdata(dev); |
| 233 | struct device_node *np = dev->of_node; |
Heiko Stübner | 3f0292a | 2011-10-05 12:27:05 +0200 | [diff] [blame] | 234 | struct gpio_regulator_data *drvdata; |
Mark Brown | c172708d | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 235 | struct regulator_config cfg = { }; |
Linus Walleij | d6cd33a | 2019-01-29 11:31:52 +0100 | [diff] [blame] | 236 | enum gpiod_flags gflags; |
| 237 | int ptr, ret, state, i; |
Heiko Stübner | 3f0292a | 2011-10-05 12:27:05 +0200 | [diff] [blame] | 238 | |
Linus Walleij | d162d04 | 2019-01-29 11:31:55 +0100 | [diff] [blame^] | 239 | drvdata = devm_kzalloc(dev, sizeof(struct gpio_regulator_data), |
Mark Brown | 02b5521 | 2012-04-03 23:20:56 +0100 | [diff] [blame] | 240 | GFP_KERNEL); |
Fabio Estevam | 9c25960 | 2014-01-24 15:48:18 -0200 | [diff] [blame] | 241 | if (drvdata == NULL) |
Heiko Stübner | 3f0292a | 2011-10-05 12:27:05 +0200 | [diff] [blame] | 242 | return -ENOMEM; |
Heiko Stübner | 3f0292a | 2011-10-05 12:27:05 +0200 | [diff] [blame] | 243 | |
Javier Martinez Canillas | 072e78b | 2014-11-10 14:43:53 +0100 | [diff] [blame] | 244 | if (np) { |
Linus Walleij | d162d04 | 2019-01-29 11:31:55 +0100 | [diff] [blame^] | 245 | config = of_get_gpio_regulator_config(dev, np, |
Javier Martinez Canillas | 072e78b | 2014-11-10 14:43:53 +0100 | [diff] [blame] | 246 | &drvdata->desc); |
| 247 | if (IS_ERR(config)) |
| 248 | return PTR_ERR(config); |
| 249 | } |
| 250 | |
Linus Walleij | d162d04 | 2019-01-29 11:31:55 +0100 | [diff] [blame^] | 251 | drvdata->desc.name = devm_kstrdup(dev, config->supply_name, GFP_KERNEL); |
Heiko Stübner | 3f0292a | 2011-10-05 12:27:05 +0200 | [diff] [blame] | 252 | if (drvdata->desc.name == NULL) { |
Linus Walleij | d162d04 | 2019-01-29 11:31:55 +0100 | [diff] [blame^] | 253 | dev_err(dev, "Failed to allocate supply name\n"); |
Christophe Jaillet | ed8cffd | 2018-03-13 21:33:11 +0100 | [diff] [blame] | 254 | return -ENOMEM; |
Heiko Stübner | 3f0292a | 2011-10-05 12:27:05 +0200 | [diff] [blame] | 255 | } |
| 256 | |
Linus Walleij | d162d04 | 2019-01-29 11:31:55 +0100 | [diff] [blame^] | 257 | drvdata->gpiods = devm_kzalloc(dev, sizeof(struct gpio_desc *), |
Linus Walleij | d6cd33a | 2019-01-29 11:31:52 +0100 | [diff] [blame] | 258 | GFP_KERNEL); |
| 259 | if (!drvdata->gpiods) |
| 260 | return -ENOMEM; |
| 261 | for (i = 0; i < config->ngpios; i++) { |
Linus Walleij | d162d04 | 2019-01-29 11:31:55 +0100 | [diff] [blame^] | 262 | drvdata->gpiods[i] = devm_gpiod_get_index(dev, |
Linus Walleij | d6cd33a | 2019-01-29 11:31:52 +0100 | [diff] [blame] | 263 | NULL, |
| 264 | i, |
| 265 | config->gflags[i]); |
| 266 | if (IS_ERR(drvdata->gpiods[i])) |
| 267 | return PTR_ERR(drvdata->gpiods[i]); |
| 268 | /* This is good to know */ |
| 269 | gpiod_set_consumer_name(drvdata->gpiods[i], drvdata->desc.name); |
Heiko Stübner | 3f0292a | 2011-10-05 12:27:05 +0200 | [diff] [blame] | 270 | } |
Linus Walleij | d6cd33a | 2019-01-29 11:31:52 +0100 | [diff] [blame] | 271 | drvdata->nr_gpios = config->ngpios; |
Heiko Stübner | 3f0292a | 2011-10-05 12:27:05 +0200 | [diff] [blame] | 272 | |
Linus Walleij | d162d04 | 2019-01-29 11:31:55 +0100 | [diff] [blame^] | 273 | drvdata->states = devm_kmemdup(dev, |
| 274 | config->states, |
| 275 | config->nr_states * |
| 276 | sizeof(struct gpio_regulator_state), |
| 277 | GFP_KERNEL); |
Heiko Stübner | 3f0292a | 2011-10-05 12:27:05 +0200 | [diff] [blame] | 278 | if (drvdata->states == NULL) { |
Linus Walleij | d162d04 | 2019-01-29 11:31:55 +0100 | [diff] [blame^] | 279 | dev_err(dev, "Failed to allocate state data\n"); |
| 280 | return -ENOMEM; |
Heiko Stübner | 3f0292a | 2011-10-05 12:27:05 +0200 | [diff] [blame] | 281 | } |
| 282 | drvdata->nr_states = config->nr_states; |
| 283 | |
| 284 | drvdata->desc.owner = THIS_MODULE; |
Axel Lin | a2a8222 | 2012-07-04 10:19:46 +0800 | [diff] [blame] | 285 | drvdata->desc.enable_time = config->startup_delay; |
Heiko Stübner | 3f0292a | 2011-10-05 12:27:05 +0200 | [diff] [blame] | 286 | |
| 287 | /* handle regulator type*/ |
| 288 | switch (config->type) { |
| 289 | case REGULATOR_VOLTAGE: |
| 290 | drvdata->desc.type = REGULATOR_VOLTAGE; |
| 291 | drvdata->desc.ops = &gpio_regulator_voltage_ops; |
| 292 | drvdata->desc.n_voltages = config->nr_states; |
| 293 | break; |
| 294 | case REGULATOR_CURRENT: |
| 295 | drvdata->desc.type = REGULATOR_CURRENT; |
| 296 | drvdata->desc.ops = &gpio_regulator_current_ops; |
| 297 | break; |
| 298 | default: |
Linus Walleij | d162d04 | 2019-01-29 11:31:55 +0100 | [diff] [blame^] | 299 | dev_err(dev, "No regulator type set\n"); |
| 300 | return -EINVAL; |
Heiko Stübner | 3f0292a | 2011-10-05 12:27:05 +0200 | [diff] [blame] | 301 | } |
| 302 | |
Heiko Stübner | 3f0292a | 2011-10-05 12:27:05 +0200 | [diff] [blame] | 303 | /* build initial state from gpio init data. */ |
| 304 | state = 0; |
| 305 | for (ptr = 0; ptr < drvdata->nr_gpios; ptr++) { |
Linus Walleij | d6cd33a | 2019-01-29 11:31:52 +0100 | [diff] [blame] | 306 | if (config->gflags[ptr] == GPIOD_OUT_HIGH) |
Heiko Stübner | 3f0292a | 2011-10-05 12:27:05 +0200 | [diff] [blame] | 307 | state |= (1 << ptr); |
| 308 | } |
| 309 | drvdata->state = state; |
| 310 | |
Linus Walleij | d162d04 | 2019-01-29 11:31:55 +0100 | [diff] [blame^] | 311 | cfg.dev = dev; |
Mark Brown | c172708d | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 312 | cfg.init_data = config->init_data; |
Heiko Stübner | 7d4be2f | 2012-06-03 21:30:33 +0200 | [diff] [blame] | 313 | cfg.driver_data = drvdata; |
Frank Li | f8a9f75 | 2012-11-12 17:59:52 +0800 | [diff] [blame] | 314 | cfg.of_node = np; |
Mark Brown | c172708d | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 315 | |
Linus Walleij | d6cd33a | 2019-01-29 11:31:52 +0100 | [diff] [blame] | 316 | /* |
| 317 | * The signal will be inverted by the GPIO core if flagged so in the |
| 318 | * decriptor. |
| 319 | */ |
| 320 | if (config->enabled_at_boot) |
| 321 | gflags = GPIOD_OUT_HIGH | GPIOD_FLAGS_BIT_NONEXCLUSIVE; |
| 322 | else |
| 323 | gflags = GPIOD_OUT_LOW | GPIOD_FLAGS_BIT_NONEXCLUSIVE; |
| 324 | |
Linus Walleij | d162d04 | 2019-01-29 11:31:55 +0100 | [diff] [blame^] | 325 | cfg.ena_gpiod = gpiod_get_optional(dev, "enable", gflags); |
| 326 | if (IS_ERR(cfg.ena_gpiod)) |
| 327 | return PTR_ERR(cfg.ena_gpiod); |
Axel Lin | 4b7c948 | 2012-07-04 10:20:46 +0800 | [diff] [blame] | 328 | |
Mark Brown | c172708d | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 329 | drvdata->dev = regulator_register(&drvdata->desc, &cfg); |
Heiko Stübner | 3f0292a | 2011-10-05 12:27:05 +0200 | [diff] [blame] | 330 | if (IS_ERR(drvdata->dev)) { |
| 331 | ret = PTR_ERR(drvdata->dev); |
Linus Walleij | d162d04 | 2019-01-29 11:31:55 +0100 | [diff] [blame^] | 332 | dev_err(dev, "Failed to register regulator: %d\n", ret); |
| 333 | return ret; |
Heiko Stübner | 3f0292a | 2011-10-05 12:27:05 +0200 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | platform_set_drvdata(pdev, drvdata); |
| 337 | |
| 338 | return 0; |
Heiko Stübner | 3f0292a | 2011-10-05 12:27:05 +0200 | [diff] [blame] | 339 | } |
| 340 | |
Bill Pemberton | 8dc995f | 2012-11-19 13:26:10 -0500 | [diff] [blame] | 341 | static int gpio_regulator_remove(struct platform_device *pdev) |
Heiko Stübner | 3f0292a | 2011-10-05 12:27:05 +0200 | [diff] [blame] | 342 | { |
| 343 | struct gpio_regulator_data *drvdata = platform_get_drvdata(pdev); |
| 344 | |
| 345 | regulator_unregister(drvdata->dev); |
Heiko Stübner | 3f0292a | 2011-10-05 12:27:05 +0200 | [diff] [blame] | 346 | |
| 347 | return 0; |
| 348 | } |
| 349 | |
Axel Lin | ec4f7b8 | 2012-12-04 10:32:05 +0800 | [diff] [blame] | 350 | #if defined(CONFIG_OF) |
Greg Kroah-Hartman | 3d68dfe | 2012-12-21 13:26:06 -0800 | [diff] [blame] | 351 | static const struct of_device_id regulator_gpio_of_match[] = { |
Lee Jones | 006694d | 2012-10-15 14:16:59 +0100 | [diff] [blame] | 352 | { .compatible = "regulator-gpio", }, |
| 353 | {}, |
| 354 | }; |
Luis de Bethencourt | 2f9481e | 2015-09-18 19:09:24 +0200 | [diff] [blame] | 355 | MODULE_DEVICE_TABLE(of, regulator_gpio_of_match); |
Axel Lin | ec4f7b8 | 2012-12-04 10:32:05 +0800 | [diff] [blame] | 356 | #endif |
Lee Jones | 006694d | 2012-10-15 14:16:59 +0100 | [diff] [blame] | 357 | |
Heiko Stübner | 3f0292a | 2011-10-05 12:27:05 +0200 | [diff] [blame] | 358 | static struct platform_driver gpio_regulator_driver = { |
| 359 | .probe = gpio_regulator_probe, |
Bill Pemberton | 5eb9f2b | 2012-11-19 13:20:42 -0500 | [diff] [blame] | 360 | .remove = gpio_regulator_remove, |
Heiko Stübner | 3f0292a | 2011-10-05 12:27:05 +0200 | [diff] [blame] | 361 | .driver = { |
| 362 | .name = "gpio-regulator", |
Axel Lin | ec4f7b8 | 2012-12-04 10:32:05 +0800 | [diff] [blame] | 363 | .of_match_table = of_match_ptr(regulator_gpio_of_match), |
Heiko Stübner | 3f0292a | 2011-10-05 12:27:05 +0200 | [diff] [blame] | 364 | }, |
| 365 | }; |
| 366 | |
| 367 | static int __init gpio_regulator_init(void) |
| 368 | { |
| 369 | return platform_driver_register(&gpio_regulator_driver); |
| 370 | } |
| 371 | subsys_initcall(gpio_regulator_init); |
| 372 | |
| 373 | static void __exit gpio_regulator_exit(void) |
| 374 | { |
| 375 | platform_driver_unregister(&gpio_regulator_driver); |
| 376 | } |
| 377 | module_exit(gpio_regulator_exit); |
| 378 | |
| 379 | MODULE_AUTHOR("Heiko Stuebner <heiko@sntech.de>"); |
| 380 | MODULE_DESCRIPTION("gpio voltage regulator"); |
| 381 | MODULE_LICENSE("GPL"); |
| 382 | MODULE_ALIAS("platform:gpio-regulator"); |