4 * Copyright 2008 Wolfson Microelectronics PLC.
8 * Copyright (c) 2009 Nokia Corporation
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.
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.
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>
30 #include <linux/regulator/of_regulator.h>
31 #include <linux/regulator/machine.h>
33 struct fixed_voltage_data {
34 struct regulator_desc desc;
35 struct regulator_dev *dev;
40 * of_get_fixed_voltage_config - extract fixed_voltage_config structure info
41 * @dev: device requesting for fixed_voltage_config
42 * @desc: regulator description
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
48 static struct fixed_voltage_config *
49 of_get_fixed_voltage_config(struct device *dev,
50 const struct regulator_desc *desc)
52 struct fixed_voltage_config *config;
53 struct device_node *np = dev->of_node;
54 struct regulator_init_data *init_data;
56 config = devm_kzalloc(dev, sizeof(struct fixed_voltage_config),
59 return ERR_PTR(-ENOMEM);
61 config->init_data = of_get_regulator_init_data(dev, dev->of_node, desc);
62 if (!config->init_data)
63 return ERR_PTR(-EINVAL);
65 init_data = config->init_data;
66 init_data->constraints.apply_uV = 0;
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;
73 "Fixed regulator specified with variable voltages\n");
74 return ERR_PTR(-EINVAL);
77 if (init_data->constraints.boot_on)
78 config->enabled_at_boot = true;
80 of_property_read_u32(np, "startup-delay-us", &config->startup_delay);
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
88 config->enable_high = 1;
89 config->gpio_is_open_drain = 0;
91 if (of_find_property(np, "vin-supply", NULL))
92 config->input_supply = "vin";
97 static struct regulator_ops fixed_voltage_ops = {
100 static int reg_fixed_voltage_probe(struct platform_device *pdev)
102 struct fixed_voltage_config *config;
103 struct fixed_voltage_data *drvdata;
104 struct regulator_config cfg = { };
105 enum gpiod_flags gflags;
108 drvdata = devm_kzalloc(&pdev->dev, sizeof(struct fixed_voltage_data),
113 if (pdev->dev.of_node) {
114 config = of_get_fixed_voltage_config(&pdev->dev,
117 return PTR_ERR(config);
119 config = dev_get_platdata(&pdev->dev);
125 drvdata->desc.name = devm_kstrdup(&pdev->dev,
128 if (drvdata->desc.name == NULL) {
129 dev_err(&pdev->dev, "Failed to allocate supply name\n");
132 drvdata->desc.type = REGULATOR_VOLTAGE;
133 drvdata->desc.owner = THIS_MODULE;
134 drvdata->desc.ops = &fixed_voltage_ops;
136 drvdata->desc.enable_time = config->startup_delay;
138 if (config->input_supply) {
139 drvdata->desc.supply_name = devm_kstrdup(&pdev->dev,
140 config->input_supply,
142 if (!drvdata->desc.supply_name) {
144 "Failed to allocate input supply\n");
149 if (config->microvolts)
150 drvdata->desc.n_voltages = 1;
152 drvdata->desc.fixed_uV = config->microvolts;
154 cfg.ena_gpio_invert = !config->enable_high;
155 if (config->enabled_at_boot) {
156 if (config->enable_high)
157 gflags = GPIOD_OUT_HIGH;
159 gflags = GPIOD_OUT_LOW;
161 if (config->enable_high)
162 gflags = GPIOD_OUT_LOW;
164 gflags = GPIOD_OUT_HIGH;
166 if (config->gpio_is_open_drain) {
167 if (gflags == GPIOD_OUT_HIGH)
168 gflags = GPIOD_OUT_HIGH_OPEN_DRAIN;
170 gflags = GPIOD_OUT_LOW_OPEN_DRAIN;
174 * Some fixed regulators share the enable line between two
175 * regulators which makes it necessary to get a handle on the
176 * same descriptor for two different consumers. This will get
177 * the GPIO descriptor, but only the first call will initialize
178 * it so any flags such as inversion or open drain will only
179 * be set up by the first caller and assumed identical on the
182 * FIXME: find a better way to deal with this.
184 gflags |= GPIOD_FLAGS_BIT_NONEXCLUSIVE;
186 cfg.ena_gpiod = devm_gpiod_get_optional(&pdev->dev, NULL, gflags);
187 if (IS_ERR(cfg.ena_gpiod))
188 return PTR_ERR(cfg.ena_gpiod);
190 cfg.dev = &pdev->dev;
191 cfg.init_data = config->init_data;
192 cfg.driver_data = drvdata;
193 cfg.of_node = pdev->dev.of_node;
195 drvdata->dev = devm_regulator_register(&pdev->dev, &drvdata->desc,
197 if (IS_ERR(drvdata->dev)) {
198 ret = PTR_ERR(drvdata->dev);
199 dev_err(&pdev->dev, "Failed to register regulator: %d\n", ret);
203 platform_set_drvdata(pdev, drvdata);
205 dev_dbg(&pdev->dev, "%s supplying %duV\n", drvdata->desc.name,
206 drvdata->desc.fixed_uV);
211 #if defined(CONFIG_OF)
212 static const struct of_device_id fixed_of_match[] = {
213 { .compatible = "regulator-fixed", },
216 MODULE_DEVICE_TABLE(of, fixed_of_match);
219 static struct platform_driver regulator_fixed_voltage_driver = {
220 .probe = reg_fixed_voltage_probe,
222 .name = "reg-fixed-voltage",
223 .of_match_table = of_match_ptr(fixed_of_match),
227 static int __init regulator_fixed_voltage_init(void)
229 return platform_driver_register(®ulator_fixed_voltage_driver);
231 subsys_initcall(regulator_fixed_voltage_init);
233 static void __exit regulator_fixed_voltage_exit(void)
235 platform_driver_unregister(®ulator_fixed_voltage_driver);
237 module_exit(regulator_fixed_voltage_exit);
240 MODULE_DESCRIPTION("Fixed voltage regulator");
241 MODULE_LICENSE("GPL");
242 MODULE_ALIAS("platform:reg-fixed-voltage");