]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/regulator/fixed.c
ASoC: codecs: jz4725b: Remove unnecessary const qualifier
[linux.git] / drivers / regulator / fixed.c
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         /*
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
180          * next caller.
181          *
182          * FIXME: find a better way to deal with this.
183          */
184         gflags |= GPIOD_FLAGS_BIT_NONEXCLUSIVE;
185
186         /*
187          * Do not use devm* here: the regulator core takes over the
188          * lifecycle management of the GPIO descriptor.
189          */
190         cfg.ena_gpiod = gpiod_get_optional(&pdev->dev, NULL, gflags);
191         if (IS_ERR(cfg.ena_gpiod))
192                 return PTR_ERR(cfg.ena_gpiod);
193
194         cfg.dev = &pdev->dev;
195         cfg.init_data = config->init_data;
196         cfg.driver_data = drvdata;
197         cfg.of_node = pdev->dev.of_node;
198
199         drvdata->dev = devm_regulator_register(&pdev->dev, &drvdata->desc,
200                                                &cfg);
201         if (IS_ERR(drvdata->dev)) {
202                 ret = PTR_ERR(drvdata->dev);
203                 dev_err(&pdev->dev, "Failed to register regulator: %d\n", ret);
204                 return ret;
205         }
206
207         platform_set_drvdata(pdev, drvdata);
208
209         dev_dbg(&pdev->dev, "%s supplying %duV\n", drvdata->desc.name,
210                 drvdata->desc.fixed_uV);
211
212         return 0;
213 }
214
215 #if defined(CONFIG_OF)
216 static const struct of_device_id fixed_of_match[] = {
217         { .compatible = "regulator-fixed", },
218         {},
219 };
220 MODULE_DEVICE_TABLE(of, fixed_of_match);
221 #endif
222
223 static struct platform_driver regulator_fixed_voltage_driver = {
224         .probe          = reg_fixed_voltage_probe,
225         .driver         = {
226                 .name           = "reg-fixed-voltage",
227                 .of_match_table = of_match_ptr(fixed_of_match),
228         },
229 };
230
231 static int __init regulator_fixed_voltage_init(void)
232 {
233         return platform_driver_register(&regulator_fixed_voltage_driver);
234 }
235 subsys_initcall(regulator_fixed_voltage_init);
236
237 static void __exit regulator_fixed_voltage_exit(void)
238 {
239         platform_driver_unregister(&regulator_fixed_voltage_driver);
240 }
241 module_exit(regulator_fixed_voltage_exit);
242
243 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
244 MODULE_DESCRIPTION("Fixed voltage regulator");
245 MODULE_LICENSE("GPL");
246 MODULE_ALIAS("platform:reg-fixed-voltage");