xref: /openbmc/linux/drivers/regulator/fixed.c (revision ed1666f6)
1 /*
2  * fixed.c
3  *
4  * Copyright 2008 Wolfson Microelectronics PLC.
5  *
6  * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
7  *
8  * Copyright (c) 2009 Nokia Corporation
9  * Roger Quadros <ext-roger.quadros@nokia.com>
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License as
13  * published by the Free Software Foundation; either version 2 of the
14  * License, or (at your option) any later version.
15  *
16  * This is useful for systems with mixed controllable and
17  * non-controllable regulators, as well as for allowing testing on
18  * systems with no controllable regulators.
19  */
20 
21 #include <linux/err.h>
22 #include <linux/mutex.h>
23 #include <linux/module.h>
24 #include <linux/platform_device.h>
25 #include <linux/regulator/driver.h>
26 #include <linux/regulator/fixed.h>
27 #include <linux/gpio/consumer.h>
28 #include <linux/slab.h>
29 #include <linux/of.h>
30 #include <linux/regulator/of_regulator.h>
31 #include <linux/regulator/machine.h>
32 
33 struct fixed_voltage_data {
34 	struct regulator_desc desc;
35 	struct regulator_dev *dev;
36 };
37 
38 
39 /**
40  * of_get_fixed_voltage_config - extract fixed_voltage_config structure info
41  * @dev: device requesting for fixed_voltage_config
42  * @desc: regulator description
43  *
44  * Populates fixed_voltage_config structure by extracting data from device
45  * tree node, returns a pointer to the populated structure of NULL if memory
46  * alloc fails.
47  */
48 static struct fixed_voltage_config *
49 of_get_fixed_voltage_config(struct device *dev,
50 			    const struct regulator_desc *desc)
51 {
52 	struct fixed_voltage_config *config;
53 	struct device_node *np = dev->of_node;
54 	struct regulator_init_data *init_data;
55 
56 	config = devm_kzalloc(dev, sizeof(struct fixed_voltage_config),
57 								 GFP_KERNEL);
58 	if (!config)
59 		return ERR_PTR(-ENOMEM);
60 
61 	config->init_data = of_get_regulator_init_data(dev, dev->of_node, desc);
62 	if (!config->init_data)
63 		return ERR_PTR(-EINVAL);
64 
65 	init_data = config->init_data;
66 	init_data->constraints.apply_uV = 0;
67 
68 	config->supply_name = init_data->constraints.name;
69 	if (init_data->constraints.min_uV == init_data->constraints.max_uV) {
70 		config->microvolts = init_data->constraints.min_uV;
71 	} else {
72 		dev_err(dev,
73 			 "Fixed regulator specified with variable voltages\n");
74 		return ERR_PTR(-EINVAL);
75 	}
76 
77 	if (init_data->constraints.boot_on)
78 		config->enabled_at_boot = true;
79 
80 	of_property_read_u32(np, "startup-delay-us", &config->startup_delay);
81 
82 	if (of_find_property(np, "vin-supply", NULL))
83 		config->input_supply = "vin";
84 
85 	return config;
86 }
87 
88 static struct regulator_ops fixed_voltage_ops = {
89 };
90 
91 static int reg_fixed_voltage_probe(struct platform_device *pdev)
92 {
93 	struct fixed_voltage_config *config;
94 	struct fixed_voltage_data *drvdata;
95 	struct regulator_config cfg = { };
96 	enum gpiod_flags gflags;
97 	int ret;
98 
99 	drvdata = devm_kzalloc(&pdev->dev, sizeof(struct fixed_voltage_data),
100 			       GFP_KERNEL);
101 	if (!drvdata)
102 		return -ENOMEM;
103 
104 	if (pdev->dev.of_node) {
105 		config = of_get_fixed_voltage_config(&pdev->dev,
106 						     &drvdata->desc);
107 		if (IS_ERR(config))
108 			return PTR_ERR(config);
109 	} else {
110 		config = dev_get_platdata(&pdev->dev);
111 	}
112 
113 	if (!config)
114 		return -ENOMEM;
115 
116 	drvdata->desc.name = devm_kstrdup(&pdev->dev,
117 					  config->supply_name,
118 					  GFP_KERNEL);
119 	if (drvdata->desc.name == NULL) {
120 		dev_err(&pdev->dev, "Failed to allocate supply name\n");
121 		return -ENOMEM;
122 	}
123 	drvdata->desc.type = REGULATOR_VOLTAGE;
124 	drvdata->desc.owner = THIS_MODULE;
125 	drvdata->desc.ops = &fixed_voltage_ops;
126 
127 	drvdata->desc.enable_time = config->startup_delay;
128 
129 	if (config->input_supply) {
130 		drvdata->desc.supply_name = devm_kstrdup(&pdev->dev,
131 					    config->input_supply,
132 					    GFP_KERNEL);
133 		if (!drvdata->desc.supply_name) {
134 			dev_err(&pdev->dev,
135 				"Failed to allocate input supply\n");
136 			return -ENOMEM;
137 		}
138 	}
139 
140 	if (config->microvolts)
141 		drvdata->desc.n_voltages = 1;
142 
143 	drvdata->desc.fixed_uV = config->microvolts;
144 
145 	/*
146 	 * The signal will be inverted by the GPIO core if flagged so in the
147 	 * decriptor.
148 	 */
149 	if (config->enabled_at_boot)
150 		gflags = GPIOD_OUT_HIGH;
151 	else
152 		gflags = GPIOD_OUT_LOW;
153 
154 	/*
155 	 * Some fixed regulators share the enable line between two
156 	 * regulators which makes it necessary to get a handle on the
157 	 * same descriptor for two different consumers. This will get
158 	 * the GPIO descriptor, but only the first call will initialize
159 	 * it so any flags such as inversion or open drain will only
160 	 * be set up by the first caller and assumed identical on the
161 	 * next caller.
162 	 *
163 	 * FIXME: find a better way to deal with this.
164 	 */
165 	gflags |= GPIOD_FLAGS_BIT_NONEXCLUSIVE;
166 
167 	/*
168 	 * Do not use devm* here: the regulator core takes over the
169 	 * lifecycle management of the GPIO descriptor.
170 	 */
171 	cfg.ena_gpiod = gpiod_get_optional(&pdev->dev, NULL, gflags);
172 	if (IS_ERR(cfg.ena_gpiod))
173 		return PTR_ERR(cfg.ena_gpiod);
174 
175 	cfg.dev = &pdev->dev;
176 	cfg.init_data = config->init_data;
177 	cfg.driver_data = drvdata;
178 	cfg.of_node = pdev->dev.of_node;
179 
180 	drvdata->dev = devm_regulator_register(&pdev->dev, &drvdata->desc,
181 					       &cfg);
182 	if (IS_ERR(drvdata->dev)) {
183 		ret = PTR_ERR(drvdata->dev);
184 		dev_err(&pdev->dev, "Failed to register regulator: %d\n", ret);
185 		return ret;
186 	}
187 
188 	platform_set_drvdata(pdev, drvdata);
189 
190 	dev_dbg(&pdev->dev, "%s supplying %duV\n", drvdata->desc.name,
191 		drvdata->desc.fixed_uV);
192 
193 	return 0;
194 }
195 
196 #if defined(CONFIG_OF)
197 static const struct of_device_id fixed_of_match[] = {
198 	{ .compatible = "regulator-fixed", },
199 	{},
200 };
201 MODULE_DEVICE_TABLE(of, fixed_of_match);
202 #endif
203 
204 static struct platform_driver regulator_fixed_voltage_driver = {
205 	.probe		= reg_fixed_voltage_probe,
206 	.driver		= {
207 		.name		= "reg-fixed-voltage",
208 		.of_match_table = of_match_ptr(fixed_of_match),
209 	},
210 };
211 
212 static int __init regulator_fixed_voltage_init(void)
213 {
214 	return platform_driver_register(&regulator_fixed_voltage_driver);
215 }
216 subsys_initcall(regulator_fixed_voltage_init);
217 
218 static void __exit regulator_fixed_voltage_exit(void)
219 {
220 	platform_driver_unregister(&regulator_fixed_voltage_driver);
221 }
222 module_exit(regulator_fixed_voltage_exit);
223 
224 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
225 MODULE_DESCRIPTION("Fixed voltage regulator");
226 MODULE_LICENSE("GPL");
227 MODULE_ALIAS("platform:reg-fixed-voltage");
228