xref: /OK3568_Linux_fs/kernel/drivers/regulator/fixed.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * fixed.c
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright 2008 Wolfson Microelectronics PLC.
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * Copyright (c) 2009 Nokia Corporation
10*4882a593Smuzhiyun  * Roger Quadros <ext-roger.quadros@nokia.com>
11*4882a593Smuzhiyun  *
12*4882a593Smuzhiyun  * This is useful for systems with mixed controllable and
13*4882a593Smuzhiyun  * non-controllable regulators, as well as for allowing testing on
14*4882a593Smuzhiyun  * systems with no controllable regulators.
15*4882a593Smuzhiyun  */
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun #include <linux/err.h>
18*4882a593Smuzhiyun #include <linux/mutex.h>
19*4882a593Smuzhiyun #include <linux/module.h>
20*4882a593Smuzhiyun #include <linux/platform_device.h>
21*4882a593Smuzhiyun #include <linux/regulator/driver.h>
22*4882a593Smuzhiyun #include <linux/regulator/fixed.h>
23*4882a593Smuzhiyun #include <linux/gpio/consumer.h>
24*4882a593Smuzhiyun #include <linux/slab.h>
25*4882a593Smuzhiyun #include <linux/of.h>
26*4882a593Smuzhiyun #include <linux/of_device.h>
27*4882a593Smuzhiyun #include <linux/regulator/of_regulator.h>
28*4882a593Smuzhiyun #include <linux/regulator/machine.h>
29*4882a593Smuzhiyun #include <linux/clk.h>
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun struct fixed_voltage_data {
33*4882a593Smuzhiyun 	struct regulator_desc desc;
34*4882a593Smuzhiyun 	struct regulator_dev *dev;
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun 	struct clk *enable_clock;
37*4882a593Smuzhiyun 	unsigned int clk_enable_counter;
38*4882a593Smuzhiyun };
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun struct fixed_dev_type {
41*4882a593Smuzhiyun 	bool has_enable_clock;
42*4882a593Smuzhiyun };
43*4882a593Smuzhiyun 
reg_clock_enable(struct regulator_dev * rdev)44*4882a593Smuzhiyun static int reg_clock_enable(struct regulator_dev *rdev)
45*4882a593Smuzhiyun {
46*4882a593Smuzhiyun 	struct fixed_voltage_data *priv = rdev_get_drvdata(rdev);
47*4882a593Smuzhiyun 	int ret = 0;
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun 	ret = clk_prepare_enable(priv->enable_clock);
50*4882a593Smuzhiyun 	if (ret)
51*4882a593Smuzhiyun 		return ret;
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun 	priv->clk_enable_counter++;
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun 	return ret;
56*4882a593Smuzhiyun }
57*4882a593Smuzhiyun 
reg_clock_disable(struct regulator_dev * rdev)58*4882a593Smuzhiyun static int reg_clock_disable(struct regulator_dev *rdev)
59*4882a593Smuzhiyun {
60*4882a593Smuzhiyun 	struct fixed_voltage_data *priv = rdev_get_drvdata(rdev);
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun 	clk_disable_unprepare(priv->enable_clock);
63*4882a593Smuzhiyun 	priv->clk_enable_counter--;
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun 	return 0;
66*4882a593Smuzhiyun }
67*4882a593Smuzhiyun 
reg_clock_is_enabled(struct regulator_dev * rdev)68*4882a593Smuzhiyun static int reg_clock_is_enabled(struct regulator_dev *rdev)
69*4882a593Smuzhiyun {
70*4882a593Smuzhiyun 	struct fixed_voltage_data *priv = rdev_get_drvdata(rdev);
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun 	return priv->clk_enable_counter > 0;
73*4882a593Smuzhiyun }
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun /**
77*4882a593Smuzhiyun  * of_get_fixed_voltage_config - extract fixed_voltage_config structure info
78*4882a593Smuzhiyun  * @dev: device requesting for fixed_voltage_config
79*4882a593Smuzhiyun  * @desc: regulator description
80*4882a593Smuzhiyun  *
81*4882a593Smuzhiyun  * Populates fixed_voltage_config structure by extracting data from device
82*4882a593Smuzhiyun  * tree node, returns a pointer to the populated structure of NULL if memory
83*4882a593Smuzhiyun  * alloc fails.
84*4882a593Smuzhiyun  */
85*4882a593Smuzhiyun static struct fixed_voltage_config *
of_get_fixed_voltage_config(struct device * dev,const struct regulator_desc * desc)86*4882a593Smuzhiyun of_get_fixed_voltage_config(struct device *dev,
87*4882a593Smuzhiyun 			    const struct regulator_desc *desc)
88*4882a593Smuzhiyun {
89*4882a593Smuzhiyun 	struct fixed_voltage_config *config;
90*4882a593Smuzhiyun 	struct device_node *np = dev->of_node;
91*4882a593Smuzhiyun 	struct regulator_init_data *init_data;
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun 	config = devm_kzalloc(dev, sizeof(struct fixed_voltage_config),
94*4882a593Smuzhiyun 								 GFP_KERNEL);
95*4882a593Smuzhiyun 	if (!config)
96*4882a593Smuzhiyun 		return ERR_PTR(-ENOMEM);
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun 	config->init_data = of_get_regulator_init_data(dev, dev->of_node, desc);
99*4882a593Smuzhiyun 	if (!config->init_data)
100*4882a593Smuzhiyun 		return ERR_PTR(-EINVAL);
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun 	init_data = config->init_data;
103*4882a593Smuzhiyun 	init_data->constraints.apply_uV = 0;
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun 	config->supply_name = init_data->constraints.name;
106*4882a593Smuzhiyun 	if (init_data->constraints.min_uV == init_data->constraints.max_uV) {
107*4882a593Smuzhiyun 		config->microvolts = init_data->constraints.min_uV;
108*4882a593Smuzhiyun 	} else {
109*4882a593Smuzhiyun 		dev_err(dev,
110*4882a593Smuzhiyun 			 "Fixed regulator specified with variable voltages\n");
111*4882a593Smuzhiyun 		return ERR_PTR(-EINVAL);
112*4882a593Smuzhiyun 	}
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun 	if (init_data->constraints.boot_on)
115*4882a593Smuzhiyun 		config->enabled_at_boot = true;
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun 	of_property_read_u32(np, "startup-delay-us", &config->startup_delay);
118*4882a593Smuzhiyun 	of_property_read_u32(np, "off-on-delay-us", &config->off_on_delay);
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun 	if (of_find_property(np, "vin-supply", NULL))
121*4882a593Smuzhiyun 		config->input_supply = "vin";
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun 	return config;
124*4882a593Smuzhiyun }
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun static const struct regulator_ops fixed_voltage_ops = {
127*4882a593Smuzhiyun };
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun static const struct regulator_ops fixed_voltage_clkenabled_ops = {
130*4882a593Smuzhiyun 	.enable = reg_clock_enable,
131*4882a593Smuzhiyun 	.disable = reg_clock_disable,
132*4882a593Smuzhiyun 	.is_enabled = reg_clock_is_enabled,
133*4882a593Smuzhiyun };
134*4882a593Smuzhiyun 
reg_fixed_voltage_probe(struct platform_device * pdev)135*4882a593Smuzhiyun static int reg_fixed_voltage_probe(struct platform_device *pdev)
136*4882a593Smuzhiyun {
137*4882a593Smuzhiyun 	struct device *dev = &pdev->dev;
138*4882a593Smuzhiyun 	struct fixed_voltage_config *config;
139*4882a593Smuzhiyun 	struct fixed_voltage_data *drvdata;
140*4882a593Smuzhiyun 	const struct fixed_dev_type *drvtype = of_device_get_match_data(dev);
141*4882a593Smuzhiyun 	struct regulator_config cfg = { };
142*4882a593Smuzhiyun 	enum gpiod_flags gflags;
143*4882a593Smuzhiyun 	int ret;
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun 	drvdata = devm_kzalloc(&pdev->dev, sizeof(struct fixed_voltage_data),
146*4882a593Smuzhiyun 			       GFP_KERNEL);
147*4882a593Smuzhiyun 	if (!drvdata)
148*4882a593Smuzhiyun 		return -ENOMEM;
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun 	if (pdev->dev.of_node) {
151*4882a593Smuzhiyun 		config = of_get_fixed_voltage_config(&pdev->dev,
152*4882a593Smuzhiyun 						     &drvdata->desc);
153*4882a593Smuzhiyun 		if (IS_ERR(config))
154*4882a593Smuzhiyun 			return PTR_ERR(config);
155*4882a593Smuzhiyun 	} else {
156*4882a593Smuzhiyun 		config = dev_get_platdata(&pdev->dev);
157*4882a593Smuzhiyun 	}
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun 	if (!config)
160*4882a593Smuzhiyun 		return -ENOMEM;
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun 	drvdata->desc.name = devm_kstrdup(&pdev->dev,
163*4882a593Smuzhiyun 					  config->supply_name,
164*4882a593Smuzhiyun 					  GFP_KERNEL);
165*4882a593Smuzhiyun 	if (drvdata->desc.name == NULL) {
166*4882a593Smuzhiyun 		dev_err(&pdev->dev, "Failed to allocate supply name\n");
167*4882a593Smuzhiyun 		return -ENOMEM;
168*4882a593Smuzhiyun 	}
169*4882a593Smuzhiyun 	drvdata->desc.type = REGULATOR_VOLTAGE;
170*4882a593Smuzhiyun 	drvdata->desc.owner = THIS_MODULE;
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun 	if (drvtype && drvtype->has_enable_clock) {
173*4882a593Smuzhiyun 		drvdata->desc.ops = &fixed_voltage_clkenabled_ops;
174*4882a593Smuzhiyun 
175*4882a593Smuzhiyun 		drvdata->enable_clock = devm_clk_get(dev, NULL);
176*4882a593Smuzhiyun 		if (IS_ERR(drvdata->enable_clock)) {
177*4882a593Smuzhiyun 			dev_err(dev, "Can't get enable-clock from devicetree\n");
178*4882a593Smuzhiyun 			return -ENOENT;
179*4882a593Smuzhiyun 		}
180*4882a593Smuzhiyun 	} else {
181*4882a593Smuzhiyun 		drvdata->desc.ops = &fixed_voltage_ops;
182*4882a593Smuzhiyun 	}
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun 	drvdata->desc.enable_time = config->startup_delay;
185*4882a593Smuzhiyun 	drvdata->desc.off_on_delay = config->off_on_delay;
186*4882a593Smuzhiyun 
187*4882a593Smuzhiyun 	if (config->input_supply) {
188*4882a593Smuzhiyun 		drvdata->desc.supply_name = devm_kstrdup(&pdev->dev,
189*4882a593Smuzhiyun 					    config->input_supply,
190*4882a593Smuzhiyun 					    GFP_KERNEL);
191*4882a593Smuzhiyun 		if (!drvdata->desc.supply_name) {
192*4882a593Smuzhiyun 			dev_err(&pdev->dev,
193*4882a593Smuzhiyun 				"Failed to allocate input supply\n");
194*4882a593Smuzhiyun 			return -ENOMEM;
195*4882a593Smuzhiyun 		}
196*4882a593Smuzhiyun 	}
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun 	if (config->microvolts)
199*4882a593Smuzhiyun 		drvdata->desc.n_voltages = 1;
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun 	drvdata->desc.fixed_uV = config->microvolts;
202*4882a593Smuzhiyun 
203*4882a593Smuzhiyun 	/*
204*4882a593Smuzhiyun 	 * The signal will be inverted by the GPIO core if flagged so in the
205*4882a593Smuzhiyun 	 * descriptor.
206*4882a593Smuzhiyun 	 */
207*4882a593Smuzhiyun 	if (config->enabled_at_boot)
208*4882a593Smuzhiyun 		gflags = GPIOD_OUT_HIGH;
209*4882a593Smuzhiyun 	else
210*4882a593Smuzhiyun 		gflags = GPIOD_OUT_LOW;
211*4882a593Smuzhiyun 
212*4882a593Smuzhiyun 	/*
213*4882a593Smuzhiyun 	 * Some fixed regulators share the enable line between two
214*4882a593Smuzhiyun 	 * regulators which makes it necessary to get a handle on the
215*4882a593Smuzhiyun 	 * same descriptor for two different consumers. This will get
216*4882a593Smuzhiyun 	 * the GPIO descriptor, but only the first call will initialize
217*4882a593Smuzhiyun 	 * it so any flags such as inversion or open drain will only
218*4882a593Smuzhiyun 	 * be set up by the first caller and assumed identical on the
219*4882a593Smuzhiyun 	 * next caller.
220*4882a593Smuzhiyun 	 *
221*4882a593Smuzhiyun 	 * FIXME: find a better way to deal with this.
222*4882a593Smuzhiyun 	 */
223*4882a593Smuzhiyun 	gflags |= GPIOD_FLAGS_BIT_NONEXCLUSIVE;
224*4882a593Smuzhiyun 
225*4882a593Smuzhiyun 	/*
226*4882a593Smuzhiyun 	 * Do not use devm* here: the regulator core takes over the
227*4882a593Smuzhiyun 	 * lifecycle management of the GPIO descriptor.
228*4882a593Smuzhiyun 	 */
229*4882a593Smuzhiyun 	cfg.ena_gpiod = gpiod_get_optional(&pdev->dev, NULL, gflags);
230*4882a593Smuzhiyun 	if (IS_ERR(cfg.ena_gpiod))
231*4882a593Smuzhiyun 		return PTR_ERR(cfg.ena_gpiod);
232*4882a593Smuzhiyun 
233*4882a593Smuzhiyun 	cfg.dev = &pdev->dev;
234*4882a593Smuzhiyun 	cfg.init_data = config->init_data;
235*4882a593Smuzhiyun 	cfg.driver_data = drvdata;
236*4882a593Smuzhiyun 	cfg.of_node = pdev->dev.of_node;
237*4882a593Smuzhiyun 
238*4882a593Smuzhiyun 	drvdata->dev = devm_regulator_register(&pdev->dev, &drvdata->desc,
239*4882a593Smuzhiyun 					       &cfg);
240*4882a593Smuzhiyun 	if (IS_ERR(drvdata->dev)) {
241*4882a593Smuzhiyun 		ret = PTR_ERR(drvdata->dev);
242*4882a593Smuzhiyun 		dev_err(&pdev->dev, "Failed to register regulator: %d\n", ret);
243*4882a593Smuzhiyun 		return ret;
244*4882a593Smuzhiyun 	}
245*4882a593Smuzhiyun 
246*4882a593Smuzhiyun 	platform_set_drvdata(pdev, drvdata);
247*4882a593Smuzhiyun 
248*4882a593Smuzhiyun 	dev_dbg(&pdev->dev, "%s supplying %duV\n", drvdata->desc.name,
249*4882a593Smuzhiyun 		drvdata->desc.fixed_uV);
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun 	return 0;
252*4882a593Smuzhiyun }
253*4882a593Smuzhiyun 
254*4882a593Smuzhiyun #if defined(CONFIG_OF)
255*4882a593Smuzhiyun static const struct fixed_dev_type fixed_voltage_data = {
256*4882a593Smuzhiyun 	.has_enable_clock = false,
257*4882a593Smuzhiyun };
258*4882a593Smuzhiyun 
259*4882a593Smuzhiyun static const struct fixed_dev_type fixed_clkenable_data = {
260*4882a593Smuzhiyun 	.has_enable_clock = true,
261*4882a593Smuzhiyun };
262*4882a593Smuzhiyun 
263*4882a593Smuzhiyun static const struct of_device_id fixed_of_match[] = {
264*4882a593Smuzhiyun 	{
265*4882a593Smuzhiyun 		.compatible = "regulator-fixed",
266*4882a593Smuzhiyun 		.data = &fixed_voltage_data,
267*4882a593Smuzhiyun 	},
268*4882a593Smuzhiyun 	{
269*4882a593Smuzhiyun 		.compatible = "regulator-fixed-clock",
270*4882a593Smuzhiyun 		.data = &fixed_clkenable_data,
271*4882a593Smuzhiyun 	},
272*4882a593Smuzhiyun 	{
273*4882a593Smuzhiyun 	},
274*4882a593Smuzhiyun };
275*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, fixed_of_match);
276*4882a593Smuzhiyun #endif
277*4882a593Smuzhiyun 
278*4882a593Smuzhiyun static struct platform_driver regulator_fixed_voltage_driver = {
279*4882a593Smuzhiyun 	.probe		= reg_fixed_voltage_probe,
280*4882a593Smuzhiyun 	.driver		= {
281*4882a593Smuzhiyun 		.name		= "reg-fixed-voltage",
282*4882a593Smuzhiyun 		.of_match_table = of_match_ptr(fixed_of_match),
283*4882a593Smuzhiyun 	},
284*4882a593Smuzhiyun };
285*4882a593Smuzhiyun 
regulator_fixed_voltage_init(void)286*4882a593Smuzhiyun static int __init regulator_fixed_voltage_init(void)
287*4882a593Smuzhiyun {
288*4882a593Smuzhiyun 	return platform_driver_register(&regulator_fixed_voltage_driver);
289*4882a593Smuzhiyun }
290*4882a593Smuzhiyun subsys_initcall(regulator_fixed_voltage_init);
291*4882a593Smuzhiyun 
regulator_fixed_voltage_exit(void)292*4882a593Smuzhiyun static void __exit regulator_fixed_voltage_exit(void)
293*4882a593Smuzhiyun {
294*4882a593Smuzhiyun 	platform_driver_unregister(&regulator_fixed_voltage_driver);
295*4882a593Smuzhiyun }
296*4882a593Smuzhiyun module_exit(regulator_fixed_voltage_exit);
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
299*4882a593Smuzhiyun MODULE_DESCRIPTION("Fixed voltage regulator");
300*4882a593Smuzhiyun MODULE_LICENSE("GPL");
301*4882a593Smuzhiyun MODULE_ALIAS("platform:reg-fixed-voltage");
302