Commit | Line | Data |
---|---|---|
4b74ff65 MB |
1 | /* |
2 | * fixed.c | |
3 | * | |
4 | * Copyright 2008 Wolfson Microelectronics PLC. | |
5 | * | |
6 | * Author: Mark Brown <broonie@opensource.wolfsonmicro.com> | |
7 | * | |
86d9884b RQ |
8 | * Copyright (c) 2009 Nokia Corporation |
9 | * Roger Quadros <ext-roger.quadros@nokia.com> | |
10 | * | |
4b74ff65 MB |
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> | |
65602c32 | 23 | #include <linux/module.h> |
4b74ff65 MB |
24 | #include <linux/platform_device.h> |
25 | #include <linux/regulator/driver.h> | |
26 | #include <linux/regulator/fixed.h> | |
efdfeb07 | 27 | #include <linux/gpio/consumer.h> |
5a0e3ad6 | 28 | #include <linux/slab.h> |
cef49102 | 29 | #include <linux/of.h> |
cef49102 RN |
30 | #include <linux/regulator/of_regulator.h> |
31 | #include <linux/regulator/machine.h> | |
4b74ff65 MB |
32 | |
33 | struct fixed_voltage_data { | |
34 | struct regulator_desc desc; | |
35 | struct regulator_dev *dev; | |
4b74ff65 MB |
36 | }; |
37 | ||
cef49102 RN |
38 | |
39 | /** | |
40 | * of_get_fixed_voltage_config - extract fixed_voltage_config structure info | |
41 | * @dev: device requesting for fixed_voltage_config | |
072e78b1 | 42 | * @desc: regulator description |
cef49102 RN |
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 | */ | |
bc91396b | 48 | static struct fixed_voltage_config * |
072e78b1 JMC |
49 | of_get_fixed_voltage_config(struct device *dev, |
50 | const struct regulator_desc *desc) | |
cef49102 RN |
51 | { |
52 | struct fixed_voltage_config *config; | |
53 | struct device_node *np = dev->of_node; | |
cef49102 RN |
54 | struct regulator_init_data *init_data; |
55 | ||
56 | config = devm_kzalloc(dev, sizeof(struct fixed_voltage_config), | |
57 | GFP_KERNEL); | |
58 | if (!config) | |
f141822b | 59 | return ERR_PTR(-ENOMEM); |
cef49102 | 60 | |
072e78b1 | 61 | config->init_data = of_get_regulator_init_data(dev, dev->of_node, desc); |
4b864af1 | 62 | if (!config->init_data) |
f141822b | 63 | return ERR_PTR(-EINVAL); |
4b864af1 | 64 | |
cef49102 | 65 | init_data = config->init_data; |
0c437c4a | 66 | init_data->constraints.apply_uV = 0; |
cef49102 RN |
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"); | |
f141822b | 74 | return ERR_PTR(-EINVAL); |
cef49102 RN |
75 | } |
76 | ||
77 | if (init_data->constraints.boot_on) | |
78 | config->enabled_at_boot = true; | |
79 | ||
4127f696 | 80 | of_property_read_u32(np, "startup-delay-us", &config->startup_delay); |
cef49102 | 81 | |
28be5f15 LW |
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; | |
9a50dba5 | 90 | |
6be5bfc3 LD |
91 | if (of_find_property(np, "vin-supply", NULL)) |
92 | config->input_supply = "vin"; | |
93 | ||
cef49102 RN |
94 | return config; |
95 | } | |
96 | ||
9d442061 | 97 | static struct regulator_ops fixed_voltage_ops = { |
9d442061 MB |
98 | }; |
99 | ||
a5023574 | 100 | static int reg_fixed_voltage_probe(struct platform_device *pdev) |
4b74ff65 | 101 | { |
22d881c0 | 102 | struct fixed_voltage_config *config; |
4b74ff65 | 103 | struct fixed_voltage_data *drvdata; |
c172708d | 104 | struct regulator_config cfg = { }; |
efdfeb07 | 105 | enum gpiod_flags gflags; |
4b74ff65 MB |
106 | int ret; |
107 | ||
072e78b1 JMC |
108 | drvdata = devm_kzalloc(&pdev->dev, sizeof(struct fixed_voltage_data), |
109 | GFP_KERNEL); | |
110 | if (!drvdata) | |
111 | return -ENOMEM; | |
112 | ||
f141822b | 113 | if (pdev->dev.of_node) { |
072e78b1 JMC |
114 | config = of_get_fixed_voltage_config(&pdev->dev, |
115 | &drvdata->desc); | |
f141822b SW |
116 | if (IS_ERR(config)) |
117 | return PTR_ERR(config); | |
118 | } else { | |
dff91d0b | 119 | config = dev_get_platdata(&pdev->dev); |
f141822b | 120 | } |
22d881c0 AL |
121 | |
122 | if (!config) | |
123 | return -ENOMEM; | |
cef49102 | 124 | |
84d0ffbe MB |
125 | drvdata->desc.name = devm_kstrdup(&pdev->dev, |
126 | config->supply_name, | |
127 | GFP_KERNEL); | |
4b74ff65 | 128 | if (drvdata->desc.name == NULL) { |
c53ad7fe | 129 | dev_err(&pdev->dev, "Failed to allocate supply name\n"); |
84d0ffbe | 130 | return -ENOMEM; |
4b74ff65 MB |
131 | } |
132 | drvdata->desc.type = REGULATOR_VOLTAGE; | |
133 | drvdata->desc.owner = THIS_MODULE; | |
25a53dfb | 134 | drvdata->desc.ops = &fixed_voltage_ops; |
1c37f8a8 | 135 | |
3d0f267f MB |
136 | drvdata->desc.enable_time = config->startup_delay; |
137 | ||
6be5bfc3 | 138 | if (config->input_supply) { |
84d0ffbe MB |
139 | drvdata->desc.supply_name = devm_kstrdup(&pdev->dev, |
140 | config->input_supply, | |
141 | GFP_KERNEL); | |
6be5bfc3 LD |
142 | if (!drvdata->desc.supply_name) { |
143 | dev_err(&pdev->dev, | |
144 | "Failed to allocate input supply\n"); | |
84d0ffbe | 145 | return -ENOMEM; |
6be5bfc3 LD |
146 | } |
147 | } | |
148 | ||
1c37f8a8 SH |
149 | if (config->microvolts) |
150 | drvdata->desc.n_voltages = 1; | |
4b74ff65 | 151 | |
c368e5fc | 152 | drvdata->desc.fixed_uV = config->microvolts; |
9d442061 | 153 | |
25a53dfb MB |
154 | cfg.ena_gpio_invert = !config->enable_high; |
155 | if (config->enabled_at_boot) { | |
609d5f6d | 156 | if (config->enable_high) |
efdfeb07 | 157 | gflags = GPIOD_OUT_HIGH; |
609d5f6d | 158 | else |
efdfeb07 | 159 | gflags = GPIOD_OUT_LOW; |
86d9884b | 160 | } else { |
609d5f6d | 161 | if (config->enable_high) |
efdfeb07 | 162 | gflags = GPIOD_OUT_LOW; |
609d5f6d | 163 | else |
efdfeb07 | 164 | gflags = GPIOD_OUT_HIGH; |
86d9884b | 165 | } |
efdfeb07 LW |
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 | ||
b0ce7b29 LW |
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 | ||
5e6f3ae5 LW |
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); | |
efdfeb07 LW |
191 | if (IS_ERR(cfg.ena_gpiod)) |
192 | return PTR_ERR(cfg.ena_gpiod); | |
4b74ff65 | 193 | |
c172708d MB |
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 | ||
84d0ffbe MB |
199 | drvdata->dev = devm_regulator_register(&pdev->dev, &drvdata->desc, |
200 | &cfg); | |
4b74ff65 MB |
201 | if (IS_ERR(drvdata->dev)) { |
202 | ret = PTR_ERR(drvdata->dev); | |
c53ad7fe | 203 | dev_err(&pdev->dev, "Failed to register regulator: %d\n", ret); |
84d0ffbe | 204 | return ret; |
4b74ff65 MB |
205 | } |
206 | ||
207 | platform_set_drvdata(pdev, drvdata); | |
208 | ||
209 | dev_dbg(&pdev->dev, "%s supplying %duV\n", drvdata->desc.name, | |
c368e5fc | 210 | drvdata->desc.fixed_uV); |
4b74ff65 MB |
211 | |
212 | return 0; | |
4b74ff65 MB |
213 | } |
214 | ||
cef49102 | 215 | #if defined(CONFIG_OF) |
3d68dfe3 | 216 | static const struct of_device_id fixed_of_match[] = { |
cef49102 RN |
217 | { .compatible = "regulator-fixed", }, |
218 | {}, | |
219 | }; | |
220 | MODULE_DEVICE_TABLE(of, fixed_of_match); | |
cef49102 RN |
221 | #endif |
222 | ||
4b74ff65 | 223 | static struct platform_driver regulator_fixed_voltage_driver = { |
8ab3343d | 224 | .probe = reg_fixed_voltage_probe, |
4b74ff65 MB |
225 | .driver = { |
226 | .name = "reg-fixed-voltage", | |
abcfaf23 | 227 | .of_match_table = of_match_ptr(fixed_of_match), |
4b74ff65 MB |
228 | }, |
229 | }; | |
230 | ||
231 | static int __init regulator_fixed_voltage_init(void) | |
232 | { | |
233 | return platform_driver_register(®ulator_fixed_voltage_driver); | |
234 | } | |
5a1b22be | 235 | subsys_initcall(regulator_fixed_voltage_init); |
4b74ff65 MB |
236 | |
237 | static void __exit regulator_fixed_voltage_exit(void) | |
238 | { | |
239 | platform_driver_unregister(®ulator_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"); | |
38c53c89 | 246 | MODULE_ALIAS("platform:reg-fixed-voltage"); |