Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * fixed.c
0004  *
0005  * Copyright 2008 Wolfson Microelectronics PLC.
0006  *
0007  * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
0008  *
0009  * Copyright (c) 2009 Nokia Corporation
0010  * Roger Quadros <ext-roger.quadros@nokia.com>
0011  *
0012  * This is useful for systems with mixed controllable and
0013  * non-controllable regulators, as well as for allowing testing on
0014  * systems with no controllable regulators.
0015  */
0016 
0017 #include <linux/err.h>
0018 #include <linux/mutex.h>
0019 #include <linux/module.h>
0020 #include <linux/platform_device.h>
0021 #include <linux/pm_domain.h>
0022 #include <linux/pm_opp.h>
0023 #include <linux/regulator/driver.h>
0024 #include <linux/regulator/fixed.h>
0025 #include <linux/gpio/consumer.h>
0026 #include <linux/slab.h>
0027 #include <linux/of.h>
0028 #include <linux/of_device.h>
0029 #include <linux/regulator/of_regulator.h>
0030 #include <linux/regulator/machine.h>
0031 #include <linux/clk.h>
0032 
0033 
0034 struct fixed_voltage_data {
0035     struct regulator_desc desc;
0036     struct regulator_dev *dev;
0037 
0038     struct clk *enable_clock;
0039     unsigned int enable_counter;
0040     int performance_state;
0041 };
0042 
0043 struct fixed_dev_type {
0044     bool has_enable_clock;
0045     bool has_performance_state;
0046 };
0047 
0048 static int reg_clock_enable(struct regulator_dev *rdev)
0049 {
0050     struct fixed_voltage_data *priv = rdev_get_drvdata(rdev);
0051     int ret = 0;
0052 
0053     ret = clk_prepare_enable(priv->enable_clock);
0054     if (ret)
0055         return ret;
0056 
0057     priv->enable_counter++;
0058 
0059     return ret;
0060 }
0061 
0062 static int reg_clock_disable(struct regulator_dev *rdev)
0063 {
0064     struct fixed_voltage_data *priv = rdev_get_drvdata(rdev);
0065 
0066     clk_disable_unprepare(priv->enable_clock);
0067     priv->enable_counter--;
0068 
0069     return 0;
0070 }
0071 
0072 static int reg_domain_enable(struct regulator_dev *rdev)
0073 {
0074     struct fixed_voltage_data *priv = rdev_get_drvdata(rdev);
0075     struct device *dev = rdev->dev.parent;
0076     int ret;
0077 
0078     ret = dev_pm_genpd_set_performance_state(dev, priv->performance_state);
0079     if (ret)
0080         return ret;
0081 
0082     priv->enable_counter++;
0083 
0084     return ret;
0085 }
0086 
0087 static int reg_domain_disable(struct regulator_dev *rdev)
0088 {
0089     struct fixed_voltage_data *priv = rdev_get_drvdata(rdev);
0090     struct device *dev = rdev->dev.parent;
0091     int ret;
0092 
0093     ret = dev_pm_genpd_set_performance_state(dev, 0);
0094     if (ret)
0095         return ret;
0096 
0097     priv->enable_counter--;
0098 
0099     return 0;
0100 }
0101 
0102 static int reg_is_enabled(struct regulator_dev *rdev)
0103 {
0104     struct fixed_voltage_data *priv = rdev_get_drvdata(rdev);
0105 
0106     return priv->enable_counter > 0;
0107 }
0108 
0109 
0110 /**
0111  * of_get_fixed_voltage_config - extract fixed_voltage_config structure info
0112  * @dev: device requesting for fixed_voltage_config
0113  * @desc: regulator description
0114  *
0115  * Populates fixed_voltage_config structure by extracting data from device
0116  * tree node, returns a pointer to the populated structure of NULL if memory
0117  * alloc fails.
0118  */
0119 static struct fixed_voltage_config *
0120 of_get_fixed_voltage_config(struct device *dev,
0121                 const struct regulator_desc *desc)
0122 {
0123     struct fixed_voltage_config *config;
0124     struct device_node *np = dev->of_node;
0125     struct regulator_init_data *init_data;
0126 
0127     config = devm_kzalloc(dev, sizeof(struct fixed_voltage_config),
0128                                  GFP_KERNEL);
0129     if (!config)
0130         return ERR_PTR(-ENOMEM);
0131 
0132     config->init_data = of_get_regulator_init_data(dev, dev->of_node, desc);
0133     if (!config->init_data)
0134         return ERR_PTR(-EINVAL);
0135 
0136     init_data = config->init_data;
0137     init_data->constraints.apply_uV = 0;
0138 
0139     config->supply_name = init_data->constraints.name;
0140     if (init_data->constraints.min_uV == init_data->constraints.max_uV) {
0141         config->microvolts = init_data->constraints.min_uV;
0142     } else {
0143         dev_err(dev,
0144              "Fixed regulator specified with variable voltages\n");
0145         return ERR_PTR(-EINVAL);
0146     }
0147 
0148     if (init_data->constraints.boot_on)
0149         config->enabled_at_boot = true;
0150 
0151     of_property_read_u32(np, "startup-delay-us", &config->startup_delay);
0152     of_property_read_u32(np, "off-on-delay-us", &config->off_on_delay);
0153 
0154     if (of_find_property(np, "vin-supply", NULL))
0155         config->input_supply = "vin";
0156 
0157     return config;
0158 }
0159 
0160 static const struct regulator_ops fixed_voltage_ops = {
0161 };
0162 
0163 static const struct regulator_ops fixed_voltage_clkenabled_ops = {
0164     .enable = reg_clock_enable,
0165     .disable = reg_clock_disable,
0166     .is_enabled = reg_is_enabled,
0167 };
0168 
0169 static const struct regulator_ops fixed_voltage_domain_ops = {
0170     .enable = reg_domain_enable,
0171     .disable = reg_domain_disable,
0172     .is_enabled = reg_is_enabled,
0173 };
0174 
0175 static int reg_fixed_voltage_probe(struct platform_device *pdev)
0176 {
0177     struct device *dev = &pdev->dev;
0178     struct fixed_voltage_config *config;
0179     struct fixed_voltage_data *drvdata;
0180     const struct fixed_dev_type *drvtype = of_device_get_match_data(dev);
0181     struct regulator_config cfg = { };
0182     enum gpiod_flags gflags;
0183     int ret;
0184 
0185     drvdata = devm_kzalloc(&pdev->dev, sizeof(struct fixed_voltage_data),
0186                    GFP_KERNEL);
0187     if (!drvdata)
0188         return -ENOMEM;
0189 
0190     if (pdev->dev.of_node) {
0191         config = of_get_fixed_voltage_config(&pdev->dev,
0192                              &drvdata->desc);
0193         if (IS_ERR(config))
0194             return PTR_ERR(config);
0195     } else {
0196         config = dev_get_platdata(&pdev->dev);
0197     }
0198 
0199     if (!config)
0200         return -ENOMEM;
0201 
0202     drvdata->desc.name = devm_kstrdup(&pdev->dev,
0203                       config->supply_name,
0204                       GFP_KERNEL);
0205     if (drvdata->desc.name == NULL) {
0206         dev_err(&pdev->dev, "Failed to allocate supply name\n");
0207         return -ENOMEM;
0208     }
0209     drvdata->desc.type = REGULATOR_VOLTAGE;
0210     drvdata->desc.owner = THIS_MODULE;
0211 
0212     if (drvtype && drvtype->has_enable_clock) {
0213         drvdata->desc.ops = &fixed_voltage_clkenabled_ops;
0214 
0215         drvdata->enable_clock = devm_clk_get(dev, NULL);
0216         if (IS_ERR(drvdata->enable_clock)) {
0217             dev_err(dev, "Can't get enable-clock from devicetree\n");
0218             return -ENOENT;
0219         }
0220     } else if (drvtype && drvtype->has_performance_state) {
0221         drvdata->desc.ops = &fixed_voltage_domain_ops;
0222 
0223         drvdata->performance_state = of_get_required_opp_performance_state(dev->of_node, 0);
0224         if (drvdata->performance_state < 0) {
0225             dev_err(dev, "Can't get performance state from devicetree\n");
0226             return drvdata->performance_state;
0227         }
0228     } else {
0229         drvdata->desc.ops = &fixed_voltage_ops;
0230     }
0231 
0232     drvdata->desc.enable_time = config->startup_delay;
0233     drvdata->desc.off_on_delay = config->off_on_delay;
0234 
0235     if (config->input_supply) {
0236         drvdata->desc.supply_name = devm_kstrdup(&pdev->dev,
0237                         config->input_supply,
0238                         GFP_KERNEL);
0239         if (!drvdata->desc.supply_name)
0240             return -ENOMEM;
0241     }
0242 
0243     if (config->microvolts)
0244         drvdata->desc.n_voltages = 1;
0245 
0246     drvdata->desc.fixed_uV = config->microvolts;
0247 
0248     /*
0249      * The signal will be inverted by the GPIO core if flagged so in the
0250      * descriptor.
0251      */
0252     if (config->enabled_at_boot)
0253         gflags = GPIOD_OUT_HIGH;
0254     else
0255         gflags = GPIOD_OUT_LOW;
0256 
0257     /*
0258      * Some fixed regulators share the enable line between two
0259      * regulators which makes it necessary to get a handle on the
0260      * same descriptor for two different consumers. This will get
0261      * the GPIO descriptor, but only the first call will initialize
0262      * it so any flags such as inversion or open drain will only
0263      * be set up by the first caller and assumed identical on the
0264      * next caller.
0265      *
0266      * FIXME: find a better way to deal with this.
0267      */
0268     gflags |= GPIOD_FLAGS_BIT_NONEXCLUSIVE;
0269 
0270     /*
0271      * Do not use devm* here: the regulator core takes over the
0272      * lifecycle management of the GPIO descriptor.
0273      */
0274     cfg.ena_gpiod = gpiod_get_optional(&pdev->dev, NULL, gflags);
0275     if (IS_ERR(cfg.ena_gpiod))
0276         return dev_err_probe(&pdev->dev, PTR_ERR(cfg.ena_gpiod),
0277                      "can't get GPIO\n");
0278 
0279     cfg.dev = &pdev->dev;
0280     cfg.init_data = config->init_data;
0281     cfg.driver_data = drvdata;
0282     cfg.of_node = pdev->dev.of_node;
0283 
0284     drvdata->dev = devm_regulator_register(&pdev->dev, &drvdata->desc,
0285                            &cfg);
0286     if (IS_ERR(drvdata->dev)) {
0287         ret = dev_err_probe(&pdev->dev, PTR_ERR(drvdata->dev),
0288                     "Failed to register regulator: %ld\n",
0289                     PTR_ERR(drvdata->dev));
0290         return ret;
0291     }
0292 
0293     platform_set_drvdata(pdev, drvdata);
0294 
0295     dev_dbg(&pdev->dev, "%s supplying %duV\n", drvdata->desc.name,
0296         drvdata->desc.fixed_uV);
0297 
0298     return 0;
0299 }
0300 
0301 #if defined(CONFIG_OF)
0302 static const struct fixed_dev_type fixed_voltage_data = {
0303     .has_enable_clock = false,
0304 };
0305 
0306 static const struct fixed_dev_type fixed_clkenable_data = {
0307     .has_enable_clock = true,
0308 };
0309 
0310 static const struct fixed_dev_type fixed_domain_data = {
0311     .has_performance_state = true,
0312 };
0313 
0314 static const struct of_device_id fixed_of_match[] = {
0315     {
0316         .compatible = "regulator-fixed",
0317         .data = &fixed_voltage_data,
0318     },
0319     {
0320         .compatible = "regulator-fixed-clock",
0321         .data = &fixed_clkenable_data,
0322     },
0323     {
0324         .compatible = "regulator-fixed-domain",
0325         .data = &fixed_domain_data,
0326     },
0327     {
0328     },
0329 };
0330 MODULE_DEVICE_TABLE(of, fixed_of_match);
0331 #endif
0332 
0333 static struct platform_driver regulator_fixed_voltage_driver = {
0334     .probe      = reg_fixed_voltage_probe,
0335     .driver     = {
0336         .name       = "reg-fixed-voltage",
0337         .of_match_table = of_match_ptr(fixed_of_match),
0338     },
0339 };
0340 
0341 static int __init regulator_fixed_voltage_init(void)
0342 {
0343     return platform_driver_register(&regulator_fixed_voltage_driver);
0344 }
0345 subsys_initcall(regulator_fixed_voltage_init);
0346 
0347 static void __exit regulator_fixed_voltage_exit(void)
0348 {
0349     platform_driver_unregister(&regulator_fixed_voltage_driver);
0350 }
0351 module_exit(regulator_fixed_voltage_exit);
0352 
0353 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
0354 MODULE_DESCRIPTION("Fixed voltage regulator");
0355 MODULE_LICENSE("GPL");
0356 MODULE_ALIAS("platform:reg-fixed-voltage");