xref: /openbmc/linux/drivers/regulator/fixed.c (revision 28be5f15)
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 	/*
83 	 * FIXME: we pulled active low/high and open drain handling into
84 	 * gpiolib so it will be handled there. Delete this in the second
85 	 * step when we also remove the custom inversion handling for all
86 	 * legacy boardfiles.
87 	 */
88 	config->enable_high = 1;
89 	config->gpio_is_open_drain = 0;
90 
91 	if (of_find_property(np, "vin-supply", NULL))
92 		config->input_supply = "vin";
93 
94 	return config;
95 }
96 
97 static struct regulator_ops fixed_voltage_ops = {
98 };
99 
100 static int reg_fixed_voltage_probe(struct platform_device *pdev)
101 {
102 	struct fixed_voltage_config *config;
103 	struct fixed_voltage_data *drvdata;
104 	struct regulator_config cfg = { };
105 	enum gpiod_flags gflags;
106 	int ret;
107 
108 	drvdata = devm_kzalloc(&pdev->dev, sizeof(struct fixed_voltage_data),
109 			       GFP_KERNEL);
110 	if (!drvdata)
111 		return -ENOMEM;
112 
113 	if (pdev->dev.of_node) {
114 		config = of_get_fixed_voltage_config(&pdev->dev,
115 						     &drvdata->desc);
116 		if (IS_ERR(config))
117 			return PTR_ERR(config);
118 	} else {
119 		config = dev_get_platdata(&pdev->dev);
120 	}
121 
122 	if (!config)
123 		return -ENOMEM;
124 
125 	drvdata->desc.name = devm_kstrdup(&pdev->dev,
126 					  config->supply_name,
127 					  GFP_KERNEL);
128 	if (drvdata->desc.name == NULL) {
129 		dev_err(&pdev->dev, "Failed to allocate supply name\n");
130 		return -ENOMEM;
131 	}
132 	drvdata->desc.type = REGULATOR_VOLTAGE;
133 	drvdata->desc.owner = THIS_MODULE;
134 	drvdata->desc.ops = &fixed_voltage_ops;
135 
136 	drvdata->desc.enable_time = config->startup_delay;
137 
138 	if (config->input_supply) {
139 		drvdata->desc.supply_name = devm_kstrdup(&pdev->dev,
140 					    config->input_supply,
141 					    GFP_KERNEL);
142 		if (!drvdata->desc.supply_name) {
143 			dev_err(&pdev->dev,
144 				"Failed to allocate input supply\n");
145 			return -ENOMEM;
146 		}
147 	}
148 
149 	if (config->microvolts)
150 		drvdata->desc.n_voltages = 1;
151 
152 	drvdata->desc.fixed_uV = config->microvolts;
153 
154 	cfg.ena_gpio_invert = !config->enable_high;
155 	if (config->enabled_at_boot) {
156 		if (config->enable_high)
157 			gflags = GPIOD_OUT_HIGH;
158 		else
159 			gflags = GPIOD_OUT_LOW;
160 	} else {
161 		if (config->enable_high)
162 			gflags = GPIOD_OUT_LOW;
163 		else
164 			gflags = GPIOD_OUT_HIGH;
165 	}
166 	if (config->gpio_is_open_drain) {
167 		if (gflags == GPIOD_OUT_HIGH)
168 			gflags = GPIOD_OUT_HIGH_OPEN_DRAIN;
169 		else
170 			gflags = GPIOD_OUT_LOW_OPEN_DRAIN;
171 	}
172 
173 	cfg.ena_gpiod = devm_gpiod_get_optional(&pdev->dev, NULL, gflags);
174 	if (IS_ERR(cfg.ena_gpiod))
175 		return PTR_ERR(cfg.ena_gpiod);
176 
177 	cfg.dev = &pdev->dev;
178 	cfg.init_data = config->init_data;
179 	cfg.driver_data = drvdata;
180 	cfg.of_node = pdev->dev.of_node;
181 
182 	drvdata->dev = devm_regulator_register(&pdev->dev, &drvdata->desc,
183 					       &cfg);
184 	if (IS_ERR(drvdata->dev)) {
185 		ret = PTR_ERR(drvdata->dev);
186 		dev_err(&pdev->dev, "Failed to register regulator: %d\n", ret);
187 		return ret;
188 	}
189 
190 	platform_set_drvdata(pdev, drvdata);
191 
192 	dev_dbg(&pdev->dev, "%s supplying %duV\n", drvdata->desc.name,
193 		drvdata->desc.fixed_uV);
194 
195 	return 0;
196 }
197 
198 #if defined(CONFIG_OF)
199 static const struct of_device_id fixed_of_match[] = {
200 	{ .compatible = "regulator-fixed", },
201 	{},
202 };
203 MODULE_DEVICE_TABLE(of, fixed_of_match);
204 #endif
205 
206 static struct platform_driver regulator_fixed_voltage_driver = {
207 	.probe		= reg_fixed_voltage_probe,
208 	.driver		= {
209 		.name		= "reg-fixed-voltage",
210 		.of_match_table = of_match_ptr(fixed_of_match),
211 	},
212 };
213 
214 static int __init regulator_fixed_voltage_init(void)
215 {
216 	return platform_driver_register(&regulator_fixed_voltage_driver);
217 }
218 subsys_initcall(regulator_fixed_voltage_init);
219 
220 static void __exit regulator_fixed_voltage_exit(void)
221 {
222 	platform_driver_unregister(&regulator_fixed_voltage_driver);
223 }
224 module_exit(regulator_fixed_voltage_exit);
225 
226 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
227 MODULE_DESCRIPTION("Fixed voltage regulator");
228 MODULE_LICENSE("GPL");
229 MODULE_ALIAS("platform:reg-fixed-voltage");
230