blob: ade0bef4569d977d3fb235c9d8ab688dd4cf27ad [file] [log] [blame]
Axel Lindfc0c822019-04-17 22:16:29 +08001// SPDX-License-Identifier: GPL-2.0+
2//
3// arizona-ldo1.c -- LDO1 supply for Arizona devices
4//
5// Copyright 2012 Wolfson Microelectronics PLC.
6//
7// Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
Mark Brown1910efa2012-06-23 13:07:57 +01008
9#include <linux/module.h>
10#include <linux/moduleparam.h>
11#include <linux/init.h>
12#include <linux/bitops.h>
13#include <linux/err.h>
Arnd Bergmann7b22b9a2014-05-27 17:27:34 +010014#include <linux/of.h>
Linus Walleije1739e82018-05-14 10:06:24 +020015#include <linux/gpio/consumer.h>
Mark Brown1910efa2012-06-23 13:07:57 +010016#include <linux/platform_device.h>
17#include <linux/regulator/driver.h>
18#include <linux/regulator/machine.h>
Charles Keepax2cce4be2014-04-16 10:01:39 +010019#include <linux/regulator/of_regulator.h>
Mark Brown1910efa2012-06-23 13:07:57 +010020#include <linux/slab.h>
21
Richard Fitzgeraldaaa84e62017-04-18 11:43:52 +010022#include <linux/regulator/arizona-ldo1.h>
23
Mark Brown1910efa2012-06-23 13:07:57 +010024#include <linux/mfd/arizona/core.h>
25#include <linux/mfd/arizona/pdata.h>
26#include <linux/mfd/arizona/registers.h>
27
Richard Fitzgerald1f5f11e2019-05-21 11:04:38 +010028#include <linux/mfd/madera/core.h>
29#include <linux/mfd/madera/pdata.h>
30#include <linux/mfd/madera/registers.h>
31
Mark Brown1910efa2012-06-23 13:07:57 +010032struct arizona_ldo1 {
33 struct regulator_dev *regulator;
Richard Fitzgerald80a55f42017-04-18 11:43:53 +010034 struct regmap *regmap;
Mark Brown1910efa2012-06-23 13:07:57 +010035
36 struct regulator_consumer_supply supply;
37 struct regulator_init_data init_data;
Charles Keepaxa9191572018-06-19 16:10:00 +010038
39 struct gpio_desc *ena_gpiod;
Mark Brown1910efa2012-06-23 13:07:57 +010040};
41
Mark Brown73ee2942012-11-27 18:48:33 +000042static int arizona_ldo1_hc_set_voltage_sel(struct regulator_dev *rdev,
43 unsigned sel)
44{
Axel Lin5db2efb2019-02-25 17:13:49 +080045 struct regmap *regmap = rdev_get_regmap(rdev);
Mark Brown73ee2942012-11-27 18:48:33 +000046 unsigned int val;
47 int ret;
48
49 if (sel == rdev->desc->n_voltages - 1)
50 val = ARIZONA_LDO1_HI_PWR;
51 else
52 val = 0;
53
54 ret = regmap_update_bits(regmap, ARIZONA_LDO1_CONTROL_2,
55 ARIZONA_LDO1_HI_PWR, val);
56 if (ret != 0)
57 return ret;
58
Mark Brown73ee2942012-11-27 18:48:33 +000059 if (val)
60 return 0;
61
Axel Lin5db2efb2019-02-25 17:13:49 +080062 return regulator_set_voltage_sel_regmap(rdev, sel);
Mark Brown73ee2942012-11-27 18:48:33 +000063}
64
65static int arizona_ldo1_hc_get_voltage_sel(struct regulator_dev *rdev)
66{
Axel Lin5db2efb2019-02-25 17:13:49 +080067 struct regmap *regmap = rdev_get_regmap(rdev);
Mark Brown73ee2942012-11-27 18:48:33 +000068 unsigned int val;
69 int ret;
70
71 ret = regmap_read(regmap, ARIZONA_LDO1_CONTROL_2, &val);
72 if (ret != 0)
73 return ret;
74
75 if (val & ARIZONA_LDO1_HI_PWR)
76 return rdev->desc->n_voltages - 1;
77
Axel Lin5db2efb2019-02-25 17:13:49 +080078 return regulator_get_voltage_sel_regmap(rdev);
Mark Brown73ee2942012-11-27 18:48:33 +000079}
80
Bhumika Goyal03d06102017-01-28 19:08:41 +053081static const struct regulator_ops arizona_ldo1_hc_ops = {
Axel Lin37b9ef92019-01-19 20:36:36 +080082 .list_voltage = regulator_list_voltage_linear_range,
83 .map_voltage = regulator_map_voltage_linear_range,
Mark Brown73ee2942012-11-27 18:48:33 +000084 .get_voltage_sel = arizona_ldo1_hc_get_voltage_sel,
85 .set_voltage_sel = arizona_ldo1_hc_set_voltage_sel,
86 .get_bypass = regulator_get_bypass_regmap,
87 .set_bypass = regulator_set_bypass_regmap,
88};
89
Matti Vaittinen60ab7f42020-05-08 18:43:36 +030090static const struct linear_range arizona_ldo1_hc_ranges[] = {
Axel Lin37b9ef92019-01-19 20:36:36 +080091 REGULATOR_LINEAR_RANGE(900000, 0, 0x6, 50000),
92 REGULATOR_LINEAR_RANGE(1800000, 0x7, 0x7, 0),
93};
94
Mark Brown73ee2942012-11-27 18:48:33 +000095static const struct regulator_desc arizona_ldo1_hc = {
96 .name = "LDO1",
97 .supply_name = "LDOVDD",
98 .type = REGULATOR_VOLTAGE,
99 .ops = &arizona_ldo1_hc_ops,
100
Axel Lin5db2efb2019-02-25 17:13:49 +0800101 .vsel_reg = ARIZONA_LDO1_CONTROL_1,
102 .vsel_mask = ARIZONA_LDO1_VSEL_MASK,
Mark Brown73ee2942012-11-27 18:48:33 +0000103 .bypass_reg = ARIZONA_LDO1_CONTROL_1,
104 .bypass_mask = ARIZONA_LDO1_BYPASS,
Axel Lin37b9ef92019-01-19 20:36:36 +0800105 .linear_ranges = arizona_ldo1_hc_ranges,
106 .n_linear_ranges = ARRAY_SIZE(arizona_ldo1_hc_ranges),
Mark Brown73ee2942012-11-27 18:48:33 +0000107 .n_voltages = 8,
Mark Brownce1bcb72013-03-26 12:02:32 +0000108 .enable_time = 1500,
Charles Keepax88f0e3a2016-10-21 14:47:42 +0100109 .ramp_delay = 24000,
Mark Brown73ee2942012-11-27 18:48:33 +0000110
111 .owner = THIS_MODULE,
112};
113
Bhumika Goyal03d06102017-01-28 19:08:41 +0530114static const struct regulator_ops arizona_ldo1_ops = {
Mark Brown1910efa2012-06-23 13:07:57 +0100115 .list_voltage = regulator_list_voltage_linear,
116 .map_voltage = regulator_map_voltage_linear,
117 .get_voltage_sel = regulator_get_voltage_sel_regmap,
118 .set_voltage_sel = regulator_set_voltage_sel_regmap,
119};
120
121static const struct regulator_desc arizona_ldo1 = {
122 .name = "LDO1",
123 .supply_name = "LDOVDD",
124 .type = REGULATOR_VOLTAGE,
125 .ops = &arizona_ldo1_ops,
126
127 .vsel_reg = ARIZONA_LDO1_CONTROL_1,
128 .vsel_mask = ARIZONA_LDO1_VSEL_MASK,
129 .min_uV = 900000,
Charles Keepaxa35ff282014-03-18 10:49:17 +0000130 .uV_step = 25000,
131 .n_voltages = 13,
Mark Brown86a14502012-11-27 17:02:29 +0000132 .enable_time = 500,
Charles Keepax88f0e3a2016-10-21 14:47:42 +0100133 .ramp_delay = 24000,
Mark Brown1910efa2012-06-23 13:07:57 +0100134
135 .owner = THIS_MODULE,
136};
137
Mark Brown55a18ae2012-11-27 17:51:46 +0000138static const struct regulator_init_data arizona_ldo1_dvfs = {
139 .constraints = {
140 .min_uV = 1200000,
141 .max_uV = 1800000,
142 .valid_ops_mask = REGULATOR_CHANGE_STATUS |
143 REGULATOR_CHANGE_VOLTAGE,
144 },
145 .num_consumer_supplies = 1,
146};
147
Mark Brown1910efa2012-06-23 13:07:57 +0100148static const struct regulator_init_data arizona_ldo1_default = {
Mark Browna9905b12012-06-27 14:32:06 +0100149 .constraints = {
150 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
151 },
Mark Brown1910efa2012-06-23 13:07:57 +0100152 .num_consumer_supplies = 1,
153};
154
Charles Keepax9a6c2fe2015-05-11 13:58:07 +0100155static const struct regulator_init_data arizona_ldo1_wm5110 = {
156 .constraints = {
157 .min_uV = 1175000,
158 .max_uV = 1200000,
159 .valid_ops_mask = REGULATOR_CHANGE_STATUS |
160 REGULATOR_CHANGE_VOLTAGE,
161 },
162 .num_consumer_supplies = 1,
163};
164
Richard Fitzgerald1f5f11e2019-05-21 11:04:38 +0100165static const struct regulator_desc madera_ldo1 = {
166 .name = "LDO1",
167 .supply_name = "LDOVDD",
168 .type = REGULATOR_VOLTAGE,
169 .ops = &arizona_ldo1_ops,
170
171 .vsel_reg = MADERA_LDO1_CONTROL_1,
172 .vsel_mask = MADERA_LDO1_VSEL_MASK,
173 .min_uV = 900000,
174 .uV_step = 25000,
175 .n_voltages = 13,
176 .enable_time = 3000,
177
178 .owner = THIS_MODULE,
179};
180
181static const struct regulator_init_data madera_ldo1_default = {
182 .constraints = {
183 .min_uV = 1200000,
184 .max_uV = 1200000,
185 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
186 },
187 .num_consumer_supplies = 1,
188};
189
Richard Fitzgeraldaaa84e62017-04-18 11:43:52 +0100190static int arizona_ldo1_of_get_pdata(struct arizona_ldo1_pdata *pdata,
Javier Martinez Canillas072e78b2014-11-10 14:43:53 +0100191 struct regulator_config *config,
Richard Fitzgeraldaaa84e62017-04-18 11:43:52 +0100192 const struct regulator_desc *desc,
193 bool *external_dcvdd)
Charles Keepax4a8c4752014-04-16 10:01:38 +0100194{
Charles Keepax2cce4be2014-04-16 10:01:39 +0100195 struct arizona_ldo1 *ldo1 = config->driver_data;
Richard Fitzgeraldaaa84e62017-04-18 11:43:52 +0100196 struct device_node *np = config->dev->of_node;
Charles Keepax2cce4be2014-04-16 10:01:39 +0100197 struct device_node *init_node, *dcvdd_node;
198 struct regulator_init_data *init_data;
Charles Keepax4a8c4752014-04-16 10:01:38 +0100199
Charles Keepaxce938002015-10-15 10:37:12 +0100200 init_node = of_get_child_by_name(np, "ldo1");
201 dcvdd_node = of_parse_phandle(np, "DCVDD-supply", 0);
Charles Keepax2cce4be2014-04-16 10:01:39 +0100202
203 if (init_node) {
204 config->of_node = init_node;
205
Richard Fitzgeraldaaa84e62017-04-18 11:43:52 +0100206 init_data = of_get_regulator_init_data(config->dev, init_node,
207 desc);
Charles Keepax2cce4be2014-04-16 10:01:39 +0100208 if (init_data) {
209 init_data->consumer_supplies = &ldo1->supply;
210 init_data->num_consumer_supplies = 1;
211
212 if (dcvdd_node && dcvdd_node != init_node)
Richard Fitzgeraldaaa84e62017-04-18 11:43:52 +0100213 *external_dcvdd = true;
Charles Keepax2cce4be2014-04-16 10:01:39 +0100214
Richard Fitzgeraldaaa84e62017-04-18 11:43:52 +0100215 pdata->init_data = init_data;
Charles Keepax2cce4be2014-04-16 10:01:39 +0100216 }
217 } else if (dcvdd_node) {
Richard Fitzgeraldaaa84e62017-04-18 11:43:52 +0100218 *external_dcvdd = true;
Charles Keepax2cce4be2014-04-16 10:01:39 +0100219 }
220
221 of_node_put(dcvdd_node);
222
Charles Keepax4a8c4752014-04-16 10:01:38 +0100223 return 0;
224}
225
Richard Fitzgeraldaf367af2017-04-18 11:43:54 +0100226static int arizona_ldo1_common_init(struct platform_device *pdev,
227 struct arizona_ldo1 *ldo1,
228 const struct regulator_desc *desc,
229 struct arizona_ldo1_pdata *pdata,
230 bool *external_dcvdd)
231{
232 struct device *parent_dev = pdev->dev.parent;
233 struct regulator_config config = { };
234 int ret;
235
236 *external_dcvdd = false;
237
238 ldo1->supply.supply = "DCVDD";
239 ldo1->init_data.consumer_supplies = &ldo1->supply;
240 ldo1->supply.dev_name = dev_name(parent_dev);
241
242 config.dev = parent_dev;
243 config.driver_data = ldo1;
244 config.regmap = ldo1->regmap;
245
246 if (IS_ENABLED(CONFIG_OF)) {
247 if (!dev_get_platdata(parent_dev)) {
248 ret = arizona_ldo1_of_get_pdata(pdata,
249 &config, desc,
250 external_dcvdd);
251 if (ret < 0)
252 return ret;
253 }
254 }
255
Charles Keepaxa9191572018-06-19 16:10:00 +0100256 /* We assume that high output = regulator off
257 * Don't use devm, since we need to get against the parent device
258 * so clean up would happen at the wrong time
259 */
260 config.ena_gpiod = gpiod_get_optional(parent_dev, "wlf,ldoena",
Linus Walleij63239e42018-10-15 11:02:40 +0200261 GPIOD_OUT_LOW | GPIOD_FLAGS_BIT_NONEXCLUSIVE);
Linus Walleije1739e82018-05-14 10:06:24 +0200262 if (IS_ERR(config.ena_gpiod))
263 return PTR_ERR(config.ena_gpiod);
Richard Fitzgeraldaf367af2017-04-18 11:43:54 +0100264
Charles Keepaxa9191572018-06-19 16:10:00 +0100265 ldo1->ena_gpiod = config.ena_gpiod;
266
Richard Fitzgeraldaf367af2017-04-18 11:43:54 +0100267 if (pdata->init_data)
268 config.init_data = pdata->init_data;
269 else
270 config.init_data = &ldo1->init_data;
271
272 /*
273 * LDO1 can only be used to supply DCVDD so if it has no
274 * consumers then DCVDD is supplied externally.
275 */
276 if (config.init_data->num_consumer_supplies == 0)
277 *external_dcvdd = true;
278
279 ldo1->regulator = devm_regulator_register(&pdev->dev, desc, &config);
280
281 of_node_put(config.of_node);
282
283 if (IS_ERR(ldo1->regulator)) {
284 ret = PTR_ERR(ldo1->regulator);
285 dev_err(&pdev->dev, "Failed to register LDO1 supply: %d\n",
286 ret);
287 return ret;
288 }
289
290 platform_set_drvdata(pdev, ldo1);
291
292 return 0;
293}
294
Bill Pembertona5023572012-11-19 13:22:22 -0500295static int arizona_ldo1_probe(struct platform_device *pdev)
Mark Brown1910efa2012-06-23 13:07:57 +0100296{
297 struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
Mark Brown1910efa2012-06-23 13:07:57 +0100298 struct arizona_ldo1 *ldo1;
Richard Fitzgeraldaf367af2017-04-18 11:43:54 +0100299 const struct regulator_desc *desc;
300 bool external_dcvdd;
Mark Brown1910efa2012-06-23 13:07:57 +0100301 int ret;
302
303 ldo1 = devm_kzalloc(&pdev->dev, sizeof(*ldo1), GFP_KERNEL);
Sachin Kamatf4a6c5b2014-02-18 16:11:09 +0530304 if (!ldo1)
Mark Brown1910efa2012-06-23 13:07:57 +0100305 return -ENOMEM;
Mark Brown1910efa2012-06-23 13:07:57 +0100306
Richard Fitzgerald80a55f42017-04-18 11:43:53 +0100307 ldo1->regmap = arizona->regmap;
Mark Brown1910efa2012-06-23 13:07:57 +0100308
309 /*
310 * Since the chip usually supplies itself we provide some
311 * default init_data for it. This will be overridden with
312 * platform data if provided.
313 */
Mark Brown55a18ae2012-11-27 17:51:46 +0000314 switch (arizona->type) {
315 case WM5102:
Charles Keepaxa35ff282014-03-18 10:49:17 +0000316 case WM8997:
Richard Fitzgerald33aa3802015-09-28 14:01:07 +0100317 case WM8998:
318 case WM1814:
Mark Brown73ee2942012-11-27 18:48:33 +0000319 desc = &arizona_ldo1_hc;
Mark Brown55a18ae2012-11-27 17:51:46 +0000320 ldo1->init_data = arizona_ldo1_dvfs;
321 break;
Charles Keepax9a6c2fe2015-05-11 13:58:07 +0100322 case WM5110:
323 case WM8280:
324 desc = &arizona_ldo1;
325 ldo1->init_data = arizona_ldo1_wm5110;
326 break;
Mark Brown55a18ae2012-11-27 17:51:46 +0000327 default:
Mark Brown73ee2942012-11-27 18:48:33 +0000328 desc = &arizona_ldo1;
Mark Brown55a18ae2012-11-27 17:51:46 +0000329 ldo1->init_data = arizona_ldo1_default;
330 break;
331 }
332
Richard Fitzgeraldaf367af2017-04-18 11:43:54 +0100333 ret = arizona_ldo1_common_init(pdev, ldo1, desc,
334 &arizona->pdata.ldo1,
335 &external_dcvdd);
336 if (ret == 0)
Richard Fitzgeraldaaa84e62017-04-18 11:43:52 +0100337 arizona->external_dcvdd = external_dcvdd;
Charles Keepax4a8c4752014-04-16 10:01:38 +0100338
Richard Fitzgeraldaf367af2017-04-18 11:43:54 +0100339 return ret;
Mark Brown1910efa2012-06-23 13:07:57 +0100340}
341
Charles Keepaxa9191572018-06-19 16:10:00 +0100342static int arizona_ldo1_remove(struct platform_device *pdev)
343{
344 struct arizona_ldo1 *ldo1 = platform_get_drvdata(pdev);
345
346 if (ldo1->ena_gpiod)
347 gpiod_put(ldo1->ena_gpiod);
348
349 return 0;
350}
351
Richard Fitzgerald1f5f11e2019-05-21 11:04:38 +0100352static int madera_ldo1_probe(struct platform_device *pdev)
353{
354 struct madera *madera = dev_get_drvdata(pdev->dev.parent);
355 struct arizona_ldo1 *ldo1;
356 bool external_dcvdd;
357 int ret;
358
359 ldo1 = devm_kzalloc(&pdev->dev, sizeof(*ldo1), GFP_KERNEL);
360 if (!ldo1)
361 return -ENOMEM;
362
363 ldo1->regmap = madera->regmap;
364
365 ldo1->init_data = madera_ldo1_default;
366
367 ret = arizona_ldo1_common_init(pdev, ldo1, &madera_ldo1,
368 &madera->pdata.ldo1,
369 &external_dcvdd);
370 if (ret)
371 return ret;
372
373 madera->internal_dcvdd = !external_dcvdd;
374
375 return 0;
376}
377
Mark Brown1910efa2012-06-23 13:07:57 +0100378static struct platform_driver arizona_ldo1_driver = {
379 .probe = arizona_ldo1_probe,
Charles Keepaxa9191572018-06-19 16:10:00 +0100380 .remove = arizona_ldo1_remove,
Mark Brown1910efa2012-06-23 13:07:57 +0100381 .driver = {
382 .name = "arizona-ldo1",
Mark Brown1910efa2012-06-23 13:07:57 +0100383 },
384};
385
Richard Fitzgerald1f5f11e2019-05-21 11:04:38 +0100386static struct platform_driver madera_ldo1_driver = {
387 .probe = madera_ldo1_probe,
388 .remove = arizona_ldo1_remove,
389 .driver = {
390 .name = "madera-ldo1",
391 },
392};
393
394static struct platform_driver * const madera_ldo1_drivers[] = {
395 &arizona_ldo1_driver,
396 &madera_ldo1_driver,
397};
398
399static int __init arizona_ldo1_init(void)
400{
401 return platform_register_drivers(madera_ldo1_drivers,
402 ARRAY_SIZE(madera_ldo1_drivers));
403}
404module_init(arizona_ldo1_init);
405
406static void __exit madera_ldo1_exit(void)
407{
408 platform_unregister_drivers(madera_ldo1_drivers,
409 ARRAY_SIZE(madera_ldo1_drivers));
410}
411module_exit(madera_ldo1_exit);
Mark Brown1910efa2012-06-23 13:07:57 +0100412
413/* Module information */
414MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
415MODULE_DESCRIPTION("Arizona LDO1 driver");
416MODULE_LICENSE("GPL");
417MODULE_ALIAS("platform:arizona-ldo1");
Richard Fitzgerald1f5f11e2019-05-21 11:04:38 +0100418MODULE_ALIAS("platform:madera-ldo1");