1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * Copyright 2008 Wolfson Microelectronics PLC.
7 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
9 * Copyright (c) 2009 Nokia Corporation
10 * Roger Quadros <ext-roger.quadros@nokia.com>
12 * This is useful for systems with mixed controllable and
13 * non-controllable regulators, as well as for allowing testing on
14 * systems with no controllable regulators.
17 #include <linux/err.h>
18 #include <linux/mutex.h>
19 #include <linux/module.h>
20 #include <linux/platform_device.h>
21 #include <linux/regulator/driver.h>
22 #include <linux/regulator/fixed.h>
23 #include <linux/gpio/consumer.h>
24 #include <linux/slab.h>
26 #include <linux/of_device.h>
27 #include <linux/regulator/of_regulator.h>
28 #include <linux/regulator/machine.h>
29 #include <linux/clk.h>
32 struct fixed_voltage_data {
33 struct regulator_desc desc;
34 struct regulator_dev *dev;
36 struct clk *enable_clock;
37 unsigned int clk_enable_counter;
40 struct fixed_dev_type {
41 bool has_enable_clock;
44 static int reg_clock_enable(struct regulator_dev *rdev)
46 struct fixed_voltage_data *priv = rdev_get_drvdata(rdev);
49 ret = clk_prepare_enable(priv->enable_clock);
53 priv->clk_enable_counter++;
58 static int reg_clock_disable(struct regulator_dev *rdev)
60 struct fixed_voltage_data *priv = rdev_get_drvdata(rdev);
62 clk_disable_unprepare(priv->enable_clock);
63 priv->clk_enable_counter--;
68 static int reg_clock_is_enabled(struct regulator_dev *rdev)
70 struct fixed_voltage_data *priv = rdev_get_drvdata(rdev);
72 return priv->clk_enable_counter > 0;
77 * of_get_fixed_voltage_config - extract fixed_voltage_config structure info
78 * @dev: device requesting for fixed_voltage_config
79 * @desc: regulator description
81 * Populates fixed_voltage_config structure by extracting data from device
82 * tree node, returns a pointer to the populated structure of NULL if memory
85 static struct fixed_voltage_config *
86 of_get_fixed_voltage_config(struct device *dev,
87 const struct regulator_desc *desc)
89 struct fixed_voltage_config *config;
90 struct device_node *np = dev->of_node;
91 struct regulator_init_data *init_data;
93 config = devm_kzalloc(dev, sizeof(struct fixed_voltage_config),
96 return ERR_PTR(-ENOMEM);
98 config->init_data = of_get_regulator_init_data(dev, dev->of_node, desc);
99 if (!config->init_data)
100 return ERR_PTR(-EINVAL);
102 init_data = config->init_data;
103 init_data->constraints.apply_uV = 0;
105 config->supply_name = init_data->constraints.name;
106 if (init_data->constraints.min_uV == init_data->constraints.max_uV) {
107 config->microvolts = init_data->constraints.min_uV;
110 "Fixed regulator specified with variable voltages\n");
111 return ERR_PTR(-EINVAL);
114 if (init_data->constraints.boot_on)
115 config->enabled_at_boot = true;
117 of_property_read_u32(np, "startup-delay-us", &config->startup_delay);
118 of_property_read_u32(np, "off-on-delay-us", &config->off_on_delay);
120 if (of_find_property(np, "vin-supply", NULL))
121 config->input_supply = "vin";
126 static const struct regulator_ops fixed_voltage_ops = {
129 static const struct regulator_ops fixed_voltage_clkenabled_ops = {
130 .enable = reg_clock_enable,
131 .disable = reg_clock_disable,
132 .is_enabled = reg_clock_is_enabled,
135 static int reg_fixed_voltage_probe(struct platform_device *pdev)
137 struct device *dev = &pdev->dev;
138 struct fixed_voltage_config *config;
139 struct fixed_voltage_data *drvdata;
140 const struct fixed_dev_type *drvtype = of_device_get_match_data(dev);
141 struct regulator_config cfg = { };
142 enum gpiod_flags gflags;
145 drvdata = devm_kzalloc(&pdev->dev, sizeof(struct fixed_voltage_data),
150 if (pdev->dev.of_node) {
151 config = of_get_fixed_voltage_config(&pdev->dev,
154 return PTR_ERR(config);
156 config = dev_get_platdata(&pdev->dev);
162 drvdata->desc.name = devm_kstrdup(&pdev->dev,
165 if (drvdata->desc.name == NULL) {
166 dev_err(&pdev->dev, "Failed to allocate supply name\n");
169 drvdata->desc.type = REGULATOR_VOLTAGE;
170 drvdata->desc.owner = THIS_MODULE;
172 if (drvtype && drvtype->has_enable_clock) {
173 drvdata->desc.ops = &fixed_voltage_clkenabled_ops;
175 drvdata->enable_clock = devm_clk_get(dev, NULL);
176 if (IS_ERR(drvdata->enable_clock)) {
177 dev_err(dev, "Can't get enable-clock from devicetree\n");
178 return PTR_ERR(drvdata->enable_clock);
181 drvdata->desc.ops = &fixed_voltage_ops;
184 drvdata->desc.enable_time = config->startup_delay;
185 drvdata->desc.off_on_delay = config->off_on_delay;
187 if (config->input_supply) {
188 drvdata->desc.supply_name = devm_kstrdup(&pdev->dev,
189 config->input_supply,
191 if (!drvdata->desc.supply_name) {
193 "Failed to allocate input supply\n");
198 if (config->microvolts)
199 drvdata->desc.n_voltages = 1;
201 drvdata->desc.fixed_uV = config->microvolts;
204 * The signal will be inverted by the GPIO core if flagged so in the
207 if (config->enabled_at_boot)
208 gflags = GPIOD_OUT_HIGH;
210 gflags = GPIOD_OUT_LOW;
213 * Some fixed regulators share the enable line between two
214 * regulators which makes it necessary to get a handle on the
215 * same descriptor for two different consumers. This will get
216 * the GPIO descriptor, but only the first call will initialize
217 * it so any flags such as inversion or open drain will only
218 * be set up by the first caller and assumed identical on the
221 * FIXME: find a better way to deal with this.
223 gflags |= GPIOD_FLAGS_BIT_NONEXCLUSIVE;
226 * Do not use devm* here: the regulator core takes over the
227 * lifecycle management of the GPIO descriptor.
229 cfg.ena_gpiod = gpiod_get_optional(&pdev->dev, NULL, gflags);
230 if (IS_ERR(cfg.ena_gpiod))
231 return PTR_ERR(cfg.ena_gpiod);
233 cfg.dev = &pdev->dev;
234 cfg.init_data = config->init_data;
235 cfg.driver_data = drvdata;
236 cfg.of_node = pdev->dev.of_node;
238 drvdata->dev = devm_regulator_register(&pdev->dev, &drvdata->desc,
240 if (IS_ERR(drvdata->dev)) {
241 ret = PTR_ERR(drvdata->dev);
242 dev_err(&pdev->dev, "Failed to register regulator: %d\n", ret);
246 platform_set_drvdata(pdev, drvdata);
248 dev_dbg(&pdev->dev, "%s supplying %duV\n", drvdata->desc.name,
249 drvdata->desc.fixed_uV);
254 #if defined(CONFIG_OF)
255 static const struct fixed_dev_type fixed_voltage_data = {
256 .has_enable_clock = false,
259 static const struct fixed_dev_type fixed_clkenable_data = {
260 .has_enable_clock = true,
263 static const struct of_device_id fixed_of_match[] = {
265 .compatible = "regulator-fixed",
266 .data = &fixed_voltage_data,
269 .compatible = "regulator-fixed-clock",
270 .data = &fixed_clkenable_data,
275 MODULE_DEVICE_TABLE(of, fixed_of_match);
278 static struct platform_driver regulator_fixed_voltage_driver = {
279 .probe = reg_fixed_voltage_probe,
281 .name = "reg-fixed-voltage",
282 .of_match_table = of_match_ptr(fixed_of_match),
286 static int __init regulator_fixed_voltage_init(void)
288 return platform_driver_register(®ulator_fixed_voltage_driver);
290 subsys_initcall(regulator_fixed_voltage_init);
292 static void __exit regulator_fixed_voltage_exit(void)
294 platform_driver_unregister(®ulator_fixed_voltage_driver);
296 module_exit(regulator_fixed_voltage_exit);
298 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
299 MODULE_DESCRIPTION("Fixed voltage regulator");
300 MODULE_LICENSE("GPL");
301 MODULE_ALIAS("platform:reg-fixed-voltage");