Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * gpio-regulator.c
0004  *
0005  * Copyright 2011 Heiko Stuebner <heiko@sntech.de>
0006  *
0007  * based on fixed.c
0008  *
0009  * Copyright 2008 Wolfson Microelectronics PLC.
0010  *
0011  * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
0012  *
0013  * Copyright (c) 2009 Nokia Corporation
0014  * Roger Quadros <ext-roger.quadros@nokia.com>
0015  *
0016  * This is useful for systems with mixed controllable and
0017  * non-controllable regulators, as well as for allowing testing on
0018  * systems with no controllable regulators.
0019  */
0020 
0021 #include <linux/err.h>
0022 #include <linux/mutex.h>
0023 #include <linux/module.h>
0024 #include <linux/platform_device.h>
0025 #include <linux/regulator/driver.h>
0026 #include <linux/regulator/machine.h>
0027 #include <linux/regulator/of_regulator.h>
0028 #include <linux/regulator/gpio-regulator.h>
0029 #include <linux/gpio/consumer.h>
0030 #include <linux/slab.h>
0031 #include <linux/of.h>
0032 
0033 struct gpio_regulator_data {
0034     struct regulator_desc desc;
0035 
0036     struct gpio_desc **gpiods;
0037     int nr_gpios;
0038 
0039     struct gpio_regulator_state *states;
0040     int nr_states;
0041 
0042     int state;
0043 };
0044 
0045 static int gpio_regulator_get_value(struct regulator_dev *dev)
0046 {
0047     struct gpio_regulator_data *data = rdev_get_drvdata(dev);
0048     int ptr;
0049 
0050     for (ptr = 0; ptr < data->nr_states; ptr++)
0051         if (data->states[ptr].gpios == data->state)
0052             return data->states[ptr].value;
0053 
0054     return -EINVAL;
0055 }
0056 
0057 static int gpio_regulator_set_voltage(struct regulator_dev *dev,
0058                     int min_uV, int max_uV,
0059                     unsigned *selector)
0060 {
0061     struct gpio_regulator_data *data = rdev_get_drvdata(dev);
0062     int ptr, target = 0, state, best_val = INT_MAX;
0063 
0064     for (ptr = 0; ptr < data->nr_states; ptr++)
0065         if (data->states[ptr].value < best_val &&
0066             data->states[ptr].value >= min_uV &&
0067             data->states[ptr].value <= max_uV) {
0068             target = data->states[ptr].gpios;
0069             best_val = data->states[ptr].value;
0070             if (selector)
0071                 *selector = ptr;
0072         }
0073 
0074     if (best_val == INT_MAX)
0075         return -EINVAL;
0076 
0077     for (ptr = 0; ptr < data->nr_gpios; ptr++) {
0078         state = (target & (1 << ptr)) >> ptr;
0079         gpiod_set_value_cansleep(data->gpiods[ptr], state);
0080     }
0081     data->state = target;
0082 
0083     return 0;
0084 }
0085 
0086 static int gpio_regulator_list_voltage(struct regulator_dev *dev,
0087                       unsigned selector)
0088 {
0089     struct gpio_regulator_data *data = rdev_get_drvdata(dev);
0090 
0091     if (selector >= data->nr_states)
0092         return -EINVAL;
0093 
0094     return data->states[selector].value;
0095 }
0096 
0097 static int gpio_regulator_set_current_limit(struct regulator_dev *dev,
0098                     int min_uA, int max_uA)
0099 {
0100     struct gpio_regulator_data *data = rdev_get_drvdata(dev);
0101     int ptr, target = 0, state, best_val = 0;
0102 
0103     for (ptr = 0; ptr < data->nr_states; ptr++)
0104         if (data->states[ptr].value > best_val &&
0105             data->states[ptr].value >= min_uA &&
0106             data->states[ptr].value <= max_uA) {
0107             target = data->states[ptr].gpios;
0108             best_val = data->states[ptr].value;
0109         }
0110 
0111     if (best_val == 0)
0112         return -EINVAL;
0113 
0114     for (ptr = 0; ptr < data->nr_gpios; ptr++) {
0115         state = (target & (1 << ptr)) >> ptr;
0116         gpiod_set_value_cansleep(data->gpiods[ptr], state);
0117     }
0118     data->state = target;
0119 
0120     return 0;
0121 }
0122 
0123 static const struct regulator_ops gpio_regulator_voltage_ops = {
0124     .get_voltage = gpio_regulator_get_value,
0125     .set_voltage = gpio_regulator_set_voltage,
0126     .list_voltage = gpio_regulator_list_voltage,
0127 };
0128 
0129 static struct gpio_regulator_config *
0130 of_get_gpio_regulator_config(struct device *dev, struct device_node *np,
0131                  const struct regulator_desc *desc)
0132 {
0133     struct gpio_regulator_config *config;
0134     const char *regtype;
0135     int proplen, i;
0136     int ngpios;
0137     int ret;
0138 
0139     config = devm_kzalloc(dev,
0140             sizeof(struct gpio_regulator_config),
0141             GFP_KERNEL);
0142     if (!config)
0143         return ERR_PTR(-ENOMEM);
0144 
0145     config->init_data = of_get_regulator_init_data(dev, np, desc);
0146     if (!config->init_data)
0147         return ERR_PTR(-EINVAL);
0148 
0149     config->supply_name = config->init_data->constraints.name;
0150 
0151     if (config->init_data->constraints.boot_on)
0152         config->enabled_at_boot = true;
0153 
0154     /*
0155      * Do not use: undocumented device tree property.
0156      * This is kept around solely for device tree ABI stability.
0157      */
0158     if (of_property_read_bool(np, "enable-at-boot"))
0159         config->enabled_at_boot = true;
0160 
0161     of_property_read_u32(np, "startup-delay-us", &config->startup_delay);
0162 
0163     /* Fetch GPIO init levels */
0164     ngpios = gpiod_count(dev, NULL);
0165     if (ngpios > 0) {
0166         config->gflags = devm_kzalloc(dev,
0167                           sizeof(enum gpiod_flags)
0168                           * ngpios,
0169                           GFP_KERNEL);
0170         if (!config->gflags)
0171             return ERR_PTR(-ENOMEM);
0172 
0173         for (i = 0; i < ngpios; i++) {
0174             u32 val;
0175 
0176             ret = of_property_read_u32_index(np, "gpios-states", i,
0177                              &val);
0178 
0179             /* Default to high per specification */
0180             if (ret)
0181                 config->gflags[i] = GPIOD_OUT_HIGH;
0182             else
0183                 config->gflags[i] =
0184                     val ? GPIOD_OUT_HIGH : GPIOD_OUT_LOW;
0185         }
0186     }
0187     config->ngpios = ngpios;
0188 
0189     /* Fetch states. */
0190     proplen = of_property_count_u32_elems(np, "states");
0191     if (proplen < 0) {
0192         dev_err(dev, "No 'states' property found\n");
0193         return ERR_PTR(-EINVAL);
0194     }
0195 
0196     config->states = devm_kcalloc(dev,
0197                 proplen / 2,
0198                 sizeof(struct gpio_regulator_state),
0199                 GFP_KERNEL);
0200     if (!config->states)
0201         return ERR_PTR(-ENOMEM);
0202 
0203     for (i = 0; i < proplen / 2; i++) {
0204         of_property_read_u32_index(np, "states", i * 2,
0205                        &config->states[i].value);
0206         of_property_read_u32_index(np, "states", i * 2 + 1,
0207                        &config->states[i].gpios);
0208     }
0209     config->nr_states = i;
0210 
0211     config->type = REGULATOR_VOLTAGE;
0212     ret = of_property_read_string(np, "regulator-type", &regtype);
0213     if (ret >= 0) {
0214         if (!strncmp("voltage", regtype, 7))
0215             config->type = REGULATOR_VOLTAGE;
0216         else if (!strncmp("current", regtype, 7))
0217             config->type = REGULATOR_CURRENT;
0218         else
0219             dev_warn(dev, "Unknown regulator-type '%s'\n",
0220                  regtype);
0221     }
0222 
0223     return config;
0224 }
0225 
0226 static const struct regulator_ops gpio_regulator_current_ops = {
0227     .get_current_limit = gpio_regulator_get_value,
0228     .set_current_limit = gpio_regulator_set_current_limit,
0229 };
0230 
0231 static int gpio_regulator_probe(struct platform_device *pdev)
0232 {
0233     struct device *dev = &pdev->dev;
0234     struct gpio_regulator_config *config = dev_get_platdata(dev);
0235     struct device_node *np = dev->of_node;
0236     struct gpio_regulator_data *drvdata;
0237     struct regulator_config cfg = { };
0238     struct regulator_dev *rdev;
0239     enum gpiod_flags gflags;
0240     int ptr, ret, state, i;
0241 
0242     drvdata = devm_kzalloc(dev, sizeof(struct gpio_regulator_data),
0243                    GFP_KERNEL);
0244     if (drvdata == NULL)
0245         return -ENOMEM;
0246 
0247     if (np) {
0248         config = of_get_gpio_regulator_config(dev, np,
0249                               &drvdata->desc);
0250         if (IS_ERR(config))
0251             return PTR_ERR(config);
0252     }
0253 
0254     drvdata->desc.name = devm_kstrdup(dev, config->supply_name, GFP_KERNEL);
0255     if (drvdata->desc.name == NULL) {
0256         dev_err(dev, "Failed to allocate supply name\n");
0257         return -ENOMEM;
0258     }
0259 
0260     drvdata->gpiods = devm_kzalloc(dev, sizeof(struct gpio_desc *),
0261                        GFP_KERNEL);
0262     if (!drvdata->gpiods)
0263         return -ENOMEM;
0264     for (i = 0; i < config->ngpios; i++) {
0265         drvdata->gpiods[i] = devm_gpiod_get_index(dev,
0266                               NULL,
0267                               i,
0268                               config->gflags[i]);
0269         if (IS_ERR(drvdata->gpiods[i]))
0270             return PTR_ERR(drvdata->gpiods[i]);
0271         /* This is good to know */
0272         gpiod_set_consumer_name(drvdata->gpiods[i], drvdata->desc.name);
0273     }
0274     drvdata->nr_gpios = config->ngpios;
0275 
0276     drvdata->states = devm_kmemdup(dev,
0277                        config->states,
0278                        config->nr_states *
0279                        sizeof(struct gpio_regulator_state),
0280                        GFP_KERNEL);
0281     if (drvdata->states == NULL) {
0282         dev_err(dev, "Failed to allocate state data\n");
0283         return -ENOMEM;
0284     }
0285     drvdata->nr_states = config->nr_states;
0286 
0287     drvdata->desc.owner = THIS_MODULE;
0288     drvdata->desc.enable_time = config->startup_delay;
0289 
0290     /* handle regulator type*/
0291     switch (config->type) {
0292     case REGULATOR_VOLTAGE:
0293         drvdata->desc.type = REGULATOR_VOLTAGE;
0294         drvdata->desc.ops = &gpio_regulator_voltage_ops;
0295         drvdata->desc.n_voltages = config->nr_states;
0296         break;
0297     case REGULATOR_CURRENT:
0298         drvdata->desc.type = REGULATOR_CURRENT;
0299         drvdata->desc.ops = &gpio_regulator_current_ops;
0300         break;
0301     default:
0302         dev_err(dev, "No regulator type set\n");
0303         return -EINVAL;
0304     }
0305 
0306     /* build initial state from gpio init data. */
0307     state = 0;
0308     for (ptr = 0; ptr < drvdata->nr_gpios; ptr++) {
0309         if (config->gflags[ptr] == GPIOD_OUT_HIGH)
0310             state |= (1 << ptr);
0311     }
0312     drvdata->state = state;
0313 
0314     cfg.dev = dev;
0315     cfg.init_data = config->init_data;
0316     cfg.driver_data = drvdata;
0317     cfg.of_node = np;
0318 
0319     /*
0320      * The signal will be inverted by the GPIO core if flagged so in the
0321      * descriptor.
0322      */
0323     if (config->enabled_at_boot)
0324         gflags = GPIOD_OUT_HIGH | GPIOD_FLAGS_BIT_NONEXCLUSIVE;
0325     else
0326         gflags = GPIOD_OUT_LOW | GPIOD_FLAGS_BIT_NONEXCLUSIVE;
0327 
0328     cfg.ena_gpiod = gpiod_get_optional(dev, "enable", gflags);
0329     if (IS_ERR(cfg.ena_gpiod))
0330         return PTR_ERR(cfg.ena_gpiod);
0331 
0332     rdev = devm_regulator_register(dev, &drvdata->desc, &cfg);
0333     if (IS_ERR(rdev)) {
0334         ret = PTR_ERR(rdev);
0335         dev_err(dev, "Failed to register regulator: %d\n", ret);
0336         return ret;
0337     }
0338 
0339     platform_set_drvdata(pdev, drvdata);
0340 
0341     return 0;
0342 }
0343 
0344 #if defined(CONFIG_OF)
0345 static const struct of_device_id regulator_gpio_of_match[] = {
0346     { .compatible = "regulator-gpio", },
0347     {},
0348 };
0349 MODULE_DEVICE_TABLE(of, regulator_gpio_of_match);
0350 #endif
0351 
0352 static struct platform_driver gpio_regulator_driver = {
0353     .probe      = gpio_regulator_probe,
0354     .driver     = {
0355         .name       = "gpio-regulator",
0356         .of_match_table = of_match_ptr(regulator_gpio_of_match),
0357     },
0358 };
0359 
0360 static int __init gpio_regulator_init(void)
0361 {
0362     return platform_driver_register(&gpio_regulator_driver);
0363 }
0364 subsys_initcall(gpio_regulator_init);
0365 
0366 static void __exit gpio_regulator_exit(void)
0367 {
0368     platform_driver_unregister(&gpio_regulator_driver);
0369 }
0370 module_exit(gpio_regulator_exit);
0371 
0372 MODULE_AUTHOR("Heiko Stuebner <heiko@sntech.de>");
0373 MODULE_DESCRIPTION("gpio voltage regulator");
0374 MODULE_LICENSE("GPL");
0375 MODULE_ALIAS("platform:gpio-regulator");