Skip to content

Commit 65f7350

Browse files
committed
regulator: core: Add core support for GPIO controlled enable lines
It is very common for regulators to support having their enable signal controlled by a GPIO. Since there are a bunch of fiddly things to get right like handling the operations when the enable signal is tied to a rail and it's just replicated code add support for this to the core. Drivers should set ena_gpio in their config if they have a GPIO control, using ena_gpio_flags to specify any flags (including GPIOF_OUT_INIT_ for the initial state) and ena_gpio_invert if the GPIO is active low. The core will then override any enable and disable operations the driver has and instead control the specified GPIO. This will in the future also allow us to further extend the core by identifying when several enable signals have been tied together and handling this properly. Signed-off-by: Mark Brown <broonie@opensource.wolfsonmicro.com>
1 parent 5c5659d commit 65f7350

2 files changed

Lines changed: 45 additions & 1 deletion

File tree

‎drivers/regulator/core.c‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <linux/mutex.h>
2424
#include <linux/suspend.h>
2525
#include <linux/delay.h>
26+
#include <linux/gpio.h>
2627
#include <linux/of.h>
2728
#include <linux/regmap.h>
2829
#include <linux/regulator/of_regulator.h>
@@ -1491,7 +1492,11 @@ static int _regulator_do_enable(struct regulator_dev *rdev)
14911492

14921493
trace_regulator_enable(rdev_get_name(rdev));
14931494

1494-
if (rdev->desc->ops->enable) {
1495+
if (rdev->ena_gpio) {
1496+
gpio_set_value_cansleep(rdev->ena_gpio,
1497+
!rdev->ena_gpio_invert);
1498+
rdev->ena_gpio_state = 1;
1499+
} else if (rdev->desc->ops->enable) {
14951500
ret = rdev->desc->ops->enable(rdev);
14961501
if (ret < 0)
14971502
return ret;
@@ -1846,6 +1851,10 @@ EXPORT_SYMBOL_GPL(regulator_disable_regmap);
18461851

18471852
static int _regulator_is_enabled(struct regulator_dev *rdev)
18481853
{
1854+
/* A GPIO control always takes precedence */
1855+
if (rdev->ena_gpio)
1856+
return rdev->ena_gpio_state;
1857+
18491858
/* If we don't know then assume that the regulator is always on */
18501859
if (!rdev->desc->ops->is_enabled)
18511860
return 1;
@@ -3243,6 +3252,26 @@ regulator_register(const struct regulator_desc *regulator_desc,
32433252

32443253
dev_set_drvdata(&rdev->dev, rdev);
32453254

3255+
if (config->ena_gpio) {
3256+
ret = gpio_request_one(config->ena_gpio,
3257+
GPIOF_DIR_OUT | config->ena_gpio_flags,
3258+
rdev_get_name(rdev));
3259+
if (ret != 0) {
3260+
rdev_err(rdev, "Failed to request enable GPIO%d: %d\n",
3261+
config->ena_gpio, ret);
3262+
goto clean;
3263+
}
3264+
3265+
rdev->ena_gpio = config->ena_gpio;
3266+
rdev->ena_gpio_invert = config->ena_gpio_invert;
3267+
3268+
if (config->ena_gpio_flags & GPIOF_OUT_INIT_HIGH)
3269+
rdev->ena_gpio_state = 1;
3270+
3271+
if (rdev->ena_gpio_invert)
3272+
rdev->ena_gpio_state = !rdev->ena_gpio_state;
3273+
}
3274+
32463275
/* set regulator constraints */
32473276
if (init_data)
32483277
constraints = &init_data->constraints;
@@ -3311,6 +3340,8 @@ regulator_register(const struct regulator_desc *regulator_desc,
33113340
scrub:
33123341
if (rdev->supply)
33133342
regulator_put(rdev->supply);
3343+
if (rdev->ena_gpio)
3344+
gpio_free(rdev->ena_gpio);
33143345
kfree(rdev->constraints);
33153346
device_unregister(&rdev->dev);
33163347
/* device core frees rdev */
@@ -3344,6 +3375,8 @@ void regulator_unregister(struct regulator_dev *rdev)
33443375
unset_regulator_supplies(rdev);
33453376
list_del(&rdev->list);
33463377
kfree(rdev->constraints);
3378+
if (rdev->ena_gpio)
3379+
gpio_free(rdev->ena_gpio);
33473380
device_unregister(&rdev->dev);
33483381
mutex_unlock(&regulator_list_mutex);
33493382
}

‎include/linux/regulator/driver.h‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,13 +220,20 @@ struct regulator_desc {
220220
* @of_node: OpenFirmware node to parse for device tree bindings (may be
221221
* NULL).
222222
* @regmap: regmap to use for core regmap helpers
223+
* @ena_gpio: GPIO controlling regulator enable.
224+
* @ena_gpio_invert: Sense for GPIO enable control.
225+
* @ena_gpio_flags: Flags to use when calling gpio_request_one()
223226
*/
224227
struct regulator_config {
225228
struct device *dev;
226229
const struct regulator_init_data *init_data;
227230
void *driver_data;
228231
struct device_node *of_node;
229232
struct regmap *regmap;
233+
234+
int ena_gpio;
235+
unsigned int ena_gpio_invert:1;
236+
unsigned int ena_gpio_flags;
230237
};
231238

232239
/*
@@ -265,6 +272,10 @@ struct regulator_dev {
265272
void *reg_data; /* regulator_dev data */
266273

267274
struct dentry *debugfs;
275+
276+
int ena_gpio;
277+
unsigned int ena_gpio_invert:1;
278+
unsigned int ena_gpio_state:1;
268279
};
269280

270281
struct regulator_dev *

0 commit comments

Comments
 (0)