blob: 3de7709bdcd4cb786c8483be5be7860568a56d15 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-or-later
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * fixed.c
4 *
5 * Copyright 2008 Wolfson Microelectronics PLC.
6 *
7 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
8 *
9 * Copyright (c) 2009 Nokia Corporation
10 * Roger Quadros <ext-roger.quadros@nokia.com>
11 *
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000012 * 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.
15 */
16
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>
David Brazdil0f672f62019-12-10 10:32:29 +000023#include <linux/gpio/consumer.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000024#include <linux/slab.h>
25#include <linux/of.h>
David Brazdil0f672f62019-12-10 10:32:29 +000026#include <linux/of_device.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000027#include <linux/regulator/of_regulator.h>
28#include <linux/regulator/machine.h>
David Brazdil0f672f62019-12-10 10:32:29 +000029#include <linux/clk.h>
30
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000031
32struct fixed_voltage_data {
33 struct regulator_desc desc;
34 struct regulator_dev *dev;
David Brazdil0f672f62019-12-10 10:32:29 +000035
36 struct clk *enable_clock;
37 unsigned int clk_enable_counter;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000038};
39
David Brazdil0f672f62019-12-10 10:32:29 +000040struct fixed_dev_type {
41 bool has_enable_clock;
42};
43
David Brazdil0f672f62019-12-10 10:32:29 +000044static int reg_clock_enable(struct regulator_dev *rdev)
45{
46 struct fixed_voltage_data *priv = rdev_get_drvdata(rdev);
47 int ret = 0;
48
49 ret = clk_prepare_enable(priv->enable_clock);
50 if (ret)
51 return ret;
52
53 priv->clk_enable_counter++;
54
55 return ret;
56}
57
58static int reg_clock_disable(struct regulator_dev *rdev)
59{
60 struct fixed_voltage_data *priv = rdev_get_drvdata(rdev);
61
62 clk_disable_unprepare(priv->enable_clock);
63 priv->clk_enable_counter--;
64
65 return 0;
66}
67
68static int reg_clock_is_enabled(struct regulator_dev *rdev)
69{
70 struct fixed_voltage_data *priv = rdev_get_drvdata(rdev);
71
72 return priv->clk_enable_counter > 0;
73}
74
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000075
76/**
77 * of_get_fixed_voltage_config - extract fixed_voltage_config structure info
78 * @dev: device requesting for fixed_voltage_config
79 * @desc: regulator description
80 *
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
83 * alloc fails.
84 */
85static struct fixed_voltage_config *
86of_get_fixed_voltage_config(struct device *dev,
87 const struct regulator_desc *desc)
88{
89 struct fixed_voltage_config *config;
90 struct device_node *np = dev->of_node;
91 struct regulator_init_data *init_data;
92
93 config = devm_kzalloc(dev, sizeof(struct fixed_voltage_config),
94 GFP_KERNEL);
95 if (!config)
96 return ERR_PTR(-ENOMEM);
97
98 config->init_data = of_get_regulator_init_data(dev, dev->of_node, desc);
99 if (!config->init_data)
100 return ERR_PTR(-EINVAL);
101
102 init_data = config->init_data;
103 init_data->constraints.apply_uV = 0;
104
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;
108 } else {
109 dev_err(dev,
110 "Fixed regulator specified with variable voltages\n");
111 return ERR_PTR(-EINVAL);
112 }
113
114 if (init_data->constraints.boot_on)
115 config->enabled_at_boot = true;
116
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000117 of_property_read_u32(np, "startup-delay-us", &config->startup_delay);
Olivier Deprez157378f2022-04-04 15:47:50 +0200118 of_property_read_u32(np, "off-on-delay-us", &config->off_on_delay);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000119
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000120 if (of_find_property(np, "vin-supply", NULL))
121 config->input_supply = "vin";
122
123 return config;
124}
125
Olivier Deprez157378f2022-04-04 15:47:50 +0200126static const struct regulator_ops fixed_voltage_ops = {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000127};
128
Olivier Deprez157378f2022-04-04 15:47:50 +0200129static const struct regulator_ops fixed_voltage_clkenabled_ops = {
David Brazdil0f672f62019-12-10 10:32:29 +0000130 .enable = reg_clock_enable,
131 .disable = reg_clock_disable,
132 .is_enabled = reg_clock_is_enabled,
133};
134
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000135static int reg_fixed_voltage_probe(struct platform_device *pdev)
136{
David Brazdil0f672f62019-12-10 10:32:29 +0000137 struct device *dev = &pdev->dev;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000138 struct fixed_voltage_config *config;
139 struct fixed_voltage_data *drvdata;
David Brazdil0f672f62019-12-10 10:32:29 +0000140 const struct fixed_dev_type *drvtype = of_device_get_match_data(dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000141 struct regulator_config cfg = { };
David Brazdil0f672f62019-12-10 10:32:29 +0000142 enum gpiod_flags gflags;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000143 int ret;
144
145 drvdata = devm_kzalloc(&pdev->dev, sizeof(struct fixed_voltage_data),
146 GFP_KERNEL);
147 if (!drvdata)
148 return -ENOMEM;
149
150 if (pdev->dev.of_node) {
151 config = of_get_fixed_voltage_config(&pdev->dev,
152 &drvdata->desc);
153 if (IS_ERR(config))
154 return PTR_ERR(config);
155 } else {
156 config = dev_get_platdata(&pdev->dev);
157 }
158
159 if (!config)
160 return -ENOMEM;
161
162 drvdata->desc.name = devm_kstrdup(&pdev->dev,
163 config->supply_name,
164 GFP_KERNEL);
165 if (drvdata->desc.name == NULL) {
166 dev_err(&pdev->dev, "Failed to allocate supply name\n");
167 return -ENOMEM;
168 }
169 drvdata->desc.type = REGULATOR_VOLTAGE;
170 drvdata->desc.owner = THIS_MODULE;
David Brazdil0f672f62019-12-10 10:32:29 +0000171
172 if (drvtype && drvtype->has_enable_clock) {
173 drvdata->desc.ops = &fixed_voltage_clkenabled_ops;
174
175 drvdata->enable_clock = devm_clk_get(dev, NULL);
176 if (IS_ERR(drvdata->enable_clock)) {
Olivier Deprez157378f2022-04-04 15:47:50 +0200177 dev_err(dev, "Can't get enable-clock from devicetree\n");
David Brazdil0f672f62019-12-10 10:32:29 +0000178 return -ENOENT;
179 }
180 } else {
181 drvdata->desc.ops = &fixed_voltage_ops;
182 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000183
184 drvdata->desc.enable_time = config->startup_delay;
Olivier Deprez157378f2022-04-04 15:47:50 +0200185 drvdata->desc.off_on_delay = config->off_on_delay;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000186
187 if (config->input_supply) {
188 drvdata->desc.supply_name = devm_kstrdup(&pdev->dev,
189 config->input_supply,
190 GFP_KERNEL);
191 if (!drvdata->desc.supply_name) {
192 dev_err(&pdev->dev,
193 "Failed to allocate input supply\n");
194 return -ENOMEM;
195 }
196 }
197
198 if (config->microvolts)
199 drvdata->desc.n_voltages = 1;
200
201 drvdata->desc.fixed_uV = config->microvolts;
202
David Brazdil0f672f62019-12-10 10:32:29 +0000203 /*
204 * The signal will be inverted by the GPIO core if flagged so in the
Olivier Deprez157378f2022-04-04 15:47:50 +0200205 * descriptor.
David Brazdil0f672f62019-12-10 10:32:29 +0000206 */
207 if (config->enabled_at_boot)
208 gflags = GPIOD_OUT_HIGH;
209 else
210 gflags = GPIOD_OUT_LOW;
211
212 /*
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
219 * next caller.
220 *
221 * FIXME: find a better way to deal with this.
222 */
223 gflags |= GPIOD_FLAGS_BIT_NONEXCLUSIVE;
224
225 /*
226 * Do not use devm* here: the regulator core takes over the
227 * lifecycle management of the GPIO descriptor.
228 */
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);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000232
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;
237
238 drvdata->dev = devm_regulator_register(&pdev->dev, &drvdata->desc,
239 &cfg);
240 if (IS_ERR(drvdata->dev)) {
241 ret = PTR_ERR(drvdata->dev);
242 dev_err(&pdev->dev, "Failed to register regulator: %d\n", ret);
243 return ret;
244 }
245
246 platform_set_drvdata(pdev, drvdata);
247
248 dev_dbg(&pdev->dev, "%s supplying %duV\n", drvdata->desc.name,
249 drvdata->desc.fixed_uV);
250
251 return 0;
252}
253
254#if defined(CONFIG_OF)
Olivier Deprez157378f2022-04-04 15:47:50 +0200255static const struct fixed_dev_type fixed_voltage_data = {
256 .has_enable_clock = false,
257};
258
259static const struct fixed_dev_type fixed_clkenable_data = {
260 .has_enable_clock = true,
261};
262
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000263static const struct of_device_id fixed_of_match[] = {
David Brazdil0f672f62019-12-10 10:32:29 +0000264 {
265 .compatible = "regulator-fixed",
266 .data = &fixed_voltage_data,
267 },
268 {
269 .compatible = "regulator-fixed-clock",
270 .data = &fixed_clkenable_data,
271 },
272 {
273 },
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000274};
275MODULE_DEVICE_TABLE(of, fixed_of_match);
276#endif
277
278static struct platform_driver regulator_fixed_voltage_driver = {
279 .probe = reg_fixed_voltage_probe,
280 .driver = {
281 .name = "reg-fixed-voltage",
282 .of_match_table = of_match_ptr(fixed_of_match),
283 },
284};
285
286static int __init regulator_fixed_voltage_init(void)
287{
288 return platform_driver_register(&regulator_fixed_voltage_driver);
289}
290subsys_initcall(regulator_fixed_voltage_init);
291
292static void __exit regulator_fixed_voltage_exit(void)
293{
294 platform_driver_unregister(&regulator_fixed_voltage_driver);
295}
296module_exit(regulator_fixed_voltage_exit);
297
298MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
299MODULE_DESCRIPTION("Fixed voltage regulator");
300MODULE_LICENSE("GPL");
301MODULE_ALIAS("platform:reg-fixed-voltage");