1 // SPDX-License-Identifier: GPL-2.0+
3 // arizona-ldo1.c -- LDO1 supply for Arizona devices
5 // Copyright 2012 Wolfson Microelectronics PLC.
7 // Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
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>
15 #include <linux/gpio/consumer.h>
16 #include <linux/platform_device.h>
17 #include <linux/regulator/driver.h>
18 #include <linux/regulator/machine.h>
19 #include <linux/regulator/of_regulator.h>
20 #include <linux/slab.h>
22 #include <linux/regulator/arizona-ldo1.h>
24 #include <linux/mfd/arizona/core.h>
25 #include <linux/mfd/arizona/pdata.h>
26 #include <linux/mfd/arizona/registers.h>
28 #include <linux/mfd/madera/core.h>
29 #include <linux/mfd/madera/pdata.h>
30 #include <linux/mfd/madera/registers.h>
33 struct regulator_dev *regulator;
34 struct regmap *regmap;
36 struct regulator_consumer_supply supply;
37 struct regulator_init_data init_data;
39 struct gpio_desc *ena_gpiod;
42 static int arizona_ldo1_hc_set_voltage_sel(struct regulator_dev *rdev,
45 struct regmap *regmap = rdev_get_regmap(rdev);
49 if (sel == rdev->desc->n_voltages - 1)
50 val = ARIZONA_LDO1_HI_PWR;
54 ret = regmap_update_bits(regmap, ARIZONA_LDO1_CONTROL_2,
55 ARIZONA_LDO1_HI_PWR, val);
62 return regulator_set_voltage_sel_regmap(rdev, sel);
65 static int arizona_ldo1_hc_get_voltage_sel(struct regulator_dev *rdev)
67 struct regmap *regmap = rdev_get_regmap(rdev);
71 ret = regmap_read(regmap, ARIZONA_LDO1_CONTROL_2, &val);
75 if (val & ARIZONA_LDO1_HI_PWR)
76 return rdev->desc->n_voltages - 1;
78 return regulator_get_voltage_sel_regmap(rdev);
81 static const struct regulator_ops arizona_ldo1_hc_ops = {
82 .list_voltage = regulator_list_voltage_linear_range,
83 .map_voltage = regulator_map_voltage_linear_range,
84 .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,
90 static const struct regulator_linear_range arizona_ldo1_hc_ranges[] = {
91 REGULATOR_LINEAR_RANGE(900000, 0, 0x6, 50000),
92 REGULATOR_LINEAR_RANGE(1800000, 0x7, 0x7, 0),
95 static const struct regulator_desc arizona_ldo1_hc = {
97 .supply_name = "LDOVDD",
98 .type = REGULATOR_VOLTAGE,
99 .ops = &arizona_ldo1_hc_ops,
101 .vsel_reg = ARIZONA_LDO1_CONTROL_1,
102 .vsel_mask = ARIZONA_LDO1_VSEL_MASK,
103 .bypass_reg = ARIZONA_LDO1_CONTROL_1,
104 .bypass_mask = ARIZONA_LDO1_BYPASS,
105 .linear_ranges = arizona_ldo1_hc_ranges,
106 .n_linear_ranges = ARRAY_SIZE(arizona_ldo1_hc_ranges),
111 .owner = THIS_MODULE,
114 static const struct regulator_ops arizona_ldo1_ops = {
115 .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,
121 static const struct regulator_desc arizona_ldo1 = {
123 .supply_name = "LDOVDD",
124 .type = REGULATOR_VOLTAGE,
125 .ops = &arizona_ldo1_ops,
127 .vsel_reg = ARIZONA_LDO1_CONTROL_1,
128 .vsel_mask = ARIZONA_LDO1_VSEL_MASK,
135 .owner = THIS_MODULE,
138 static const struct regulator_init_data arizona_ldo1_dvfs = {
142 .valid_ops_mask = REGULATOR_CHANGE_STATUS |
143 REGULATOR_CHANGE_VOLTAGE,
145 .num_consumer_supplies = 1,
148 static const struct regulator_init_data arizona_ldo1_default = {
150 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
152 .num_consumer_supplies = 1,
155 static const struct regulator_init_data arizona_ldo1_wm5110 = {
159 .valid_ops_mask = REGULATOR_CHANGE_STATUS |
160 REGULATOR_CHANGE_VOLTAGE,
162 .num_consumer_supplies = 1,
165 static const struct regulator_desc madera_ldo1 = {
167 .supply_name = "LDOVDD",
168 .type = REGULATOR_VOLTAGE,
169 .ops = &arizona_ldo1_ops,
171 .vsel_reg = MADERA_LDO1_CONTROL_1,
172 .vsel_mask = MADERA_LDO1_VSEL_MASK,
178 .owner = THIS_MODULE,
181 static const struct regulator_init_data madera_ldo1_default = {
185 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
187 .num_consumer_supplies = 1,
190 static int arizona_ldo1_of_get_pdata(struct arizona_ldo1_pdata *pdata,
191 struct regulator_config *config,
192 const struct regulator_desc *desc,
193 bool *external_dcvdd)
195 struct arizona_ldo1 *ldo1 = config->driver_data;
196 struct device_node *np = config->dev->of_node;
197 struct device_node *init_node, *dcvdd_node;
198 struct regulator_init_data *init_data;
200 init_node = of_get_child_by_name(np, "ldo1");
201 dcvdd_node = of_parse_phandle(np, "DCVDD-supply", 0);
204 config->of_node = init_node;
206 init_data = of_get_regulator_init_data(config->dev, init_node,
209 init_data->consumer_supplies = &ldo1->supply;
210 init_data->num_consumer_supplies = 1;
212 if (dcvdd_node && dcvdd_node != init_node)
213 *external_dcvdd = true;
215 pdata->init_data = init_data;
217 } else if (dcvdd_node) {
218 *external_dcvdd = true;
221 of_node_put(dcvdd_node);
226 static 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)
232 struct device *parent_dev = pdev->dev.parent;
233 struct regulator_config config = { };
236 *external_dcvdd = false;
238 ldo1->supply.supply = "DCVDD";
239 ldo1->init_data.consumer_supplies = &ldo1->supply;
240 ldo1->supply.dev_name = dev_name(parent_dev);
242 config.dev = parent_dev;
243 config.driver_data = ldo1;
244 config.regmap = ldo1->regmap;
246 if (IS_ENABLED(CONFIG_OF)) {
247 if (!dev_get_platdata(parent_dev)) {
248 ret = arizona_ldo1_of_get_pdata(pdata,
256 /* 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
260 config.ena_gpiod = gpiod_get_optional(parent_dev, "wlf,ldoena",
261 GPIOD_OUT_LOW | GPIOD_FLAGS_BIT_NONEXCLUSIVE);
262 if (IS_ERR(config.ena_gpiod))
263 return PTR_ERR(config.ena_gpiod);
265 ldo1->ena_gpiod = config.ena_gpiod;
267 if (pdata->init_data)
268 config.init_data = pdata->init_data;
270 config.init_data = &ldo1->init_data;
273 * LDO1 can only be used to supply DCVDD so if it has no
274 * consumers then DCVDD is supplied externally.
276 if (config.init_data->num_consumer_supplies == 0)
277 *external_dcvdd = true;
279 ldo1->regulator = devm_regulator_register(&pdev->dev, desc, &config);
281 of_node_put(config.of_node);
283 if (IS_ERR(ldo1->regulator)) {
284 ret = PTR_ERR(ldo1->regulator);
285 dev_err(&pdev->dev, "Failed to register LDO1 supply: %d\n",
290 platform_set_drvdata(pdev, ldo1);
295 static int arizona_ldo1_probe(struct platform_device *pdev)
297 struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
298 struct arizona_ldo1 *ldo1;
299 const struct regulator_desc *desc;
303 ldo1 = devm_kzalloc(&pdev->dev, sizeof(*ldo1), GFP_KERNEL);
307 ldo1->regmap = arizona->regmap;
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.
314 switch (arizona->type) {
319 desc = &arizona_ldo1_hc;
320 ldo1->init_data = arizona_ldo1_dvfs;
324 desc = &arizona_ldo1;
325 ldo1->init_data = arizona_ldo1_wm5110;
328 desc = &arizona_ldo1;
329 ldo1->init_data = arizona_ldo1_default;
333 ret = arizona_ldo1_common_init(pdev, ldo1, desc,
334 &arizona->pdata.ldo1,
337 arizona->external_dcvdd = external_dcvdd;
342 static int arizona_ldo1_remove(struct platform_device *pdev)
344 struct arizona_ldo1 *ldo1 = platform_get_drvdata(pdev);
347 gpiod_put(ldo1->ena_gpiod);
352 static int madera_ldo1_probe(struct platform_device *pdev)
354 struct madera *madera = dev_get_drvdata(pdev->dev.parent);
355 struct arizona_ldo1 *ldo1;
359 ldo1 = devm_kzalloc(&pdev->dev, sizeof(*ldo1), GFP_KERNEL);
363 ldo1->regmap = madera->regmap;
365 ldo1->init_data = madera_ldo1_default;
367 ret = arizona_ldo1_common_init(pdev, ldo1, &madera_ldo1,
373 madera->internal_dcvdd = !external_dcvdd;
378 static struct platform_driver arizona_ldo1_driver = {
379 .probe = arizona_ldo1_probe,
380 .remove = arizona_ldo1_remove,
382 .name = "arizona-ldo1",
386 static struct platform_driver madera_ldo1_driver = {
387 .probe = madera_ldo1_probe,
388 .remove = arizona_ldo1_remove,
390 .name = "madera-ldo1",
394 static struct platform_driver * const madera_ldo1_drivers[] = {
395 &arizona_ldo1_driver,
399 static int __init arizona_ldo1_init(void)
401 return platform_register_drivers(madera_ldo1_drivers,
402 ARRAY_SIZE(madera_ldo1_drivers));
404 module_init(arizona_ldo1_init);
406 static void __exit madera_ldo1_exit(void)
408 platform_unregister_drivers(madera_ldo1_drivers,
409 ARRAY_SIZE(madera_ldo1_drivers));
411 module_exit(madera_ldo1_exit);
413 /* Module information */
414 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
415 MODULE_DESCRIPTION("Arizona LDO1 driver");
416 MODULE_LICENSE("GPL");
417 MODULE_ALIAS("platform:arizona-ldo1");
418 MODULE_ALIAS("platform:madera-ldo1");