2 * arizona-ldo1.c -- LDO1 supply for Arizona devices
4 * Copyright 2012 Wolfson Microelectronics PLC.
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
14 #include <linux/module.h>
15 #include <linux/moduleparam.h>
16 #include <linux/init.h>
17 #include <linux/bitops.h>
18 #include <linux/err.h>
20 #include <linux/gpio/consumer.h>
21 #include <linux/platform_device.h>
22 #include <linux/regulator/driver.h>
23 #include <linux/regulator/machine.h>
24 #include <linux/regulator/of_regulator.h>
25 #include <linux/slab.h>
27 #include <linux/regulator/arizona-ldo1.h>
29 #include <linux/mfd/arizona/core.h>
30 #include <linux/mfd/arizona/pdata.h>
31 #include <linux/mfd/arizona/registers.h>
34 struct regulator_dev
*regulator
;
35 struct regmap
*regmap
;
37 struct regulator_consumer_supply supply
;
38 struct regulator_init_data init_data
;
40 struct gpio_desc
*ena_gpiod
;
43 static int arizona_ldo1_hc_list_voltage(struct regulator_dev
*rdev
,
44 unsigned int selector
)
46 if (selector
>= rdev
->desc
->n_voltages
)
49 if (selector
== rdev
->desc
->n_voltages
- 1)
52 return rdev
->desc
->min_uV
+ (rdev
->desc
->uV_step
* selector
);
55 static int arizona_ldo1_hc_map_voltage(struct regulator_dev
*rdev
,
56 int min_uV
, int max_uV
)
60 sel
= DIV_ROUND_UP(min_uV
- rdev
->desc
->min_uV
, rdev
->desc
->uV_step
);
61 if (sel
>= rdev
->desc
->n_voltages
)
62 sel
= rdev
->desc
->n_voltages
- 1;
67 static int arizona_ldo1_hc_set_voltage_sel(struct regulator_dev
*rdev
,
70 struct arizona_ldo1
*ldo
= rdev_get_drvdata(rdev
);
71 struct regmap
*regmap
= ldo
->regmap
;
75 if (sel
== rdev
->desc
->n_voltages
- 1)
76 val
= ARIZONA_LDO1_HI_PWR
;
80 ret
= regmap_update_bits(regmap
, ARIZONA_LDO1_CONTROL_2
,
81 ARIZONA_LDO1_HI_PWR
, val
);
88 val
= sel
<< ARIZONA_LDO1_VSEL_SHIFT
;
90 return regmap_update_bits(regmap
, ARIZONA_LDO1_CONTROL_1
,
91 ARIZONA_LDO1_VSEL_MASK
, val
);
94 static int arizona_ldo1_hc_get_voltage_sel(struct regulator_dev
*rdev
)
96 struct arizona_ldo1
*ldo
= rdev_get_drvdata(rdev
);
97 struct regmap
*regmap
= ldo
->regmap
;
101 ret
= regmap_read(regmap
, ARIZONA_LDO1_CONTROL_2
, &val
);
105 if (val
& ARIZONA_LDO1_HI_PWR
)
106 return rdev
->desc
->n_voltages
- 1;
108 ret
= regmap_read(regmap
, ARIZONA_LDO1_CONTROL_1
, &val
);
112 return (val
& ARIZONA_LDO1_VSEL_MASK
) >> ARIZONA_LDO1_VSEL_SHIFT
;
115 static const struct regulator_ops arizona_ldo1_hc_ops
= {
116 .list_voltage
= arizona_ldo1_hc_list_voltage
,
117 .map_voltage
= arizona_ldo1_hc_map_voltage
,
118 .get_voltage_sel
= arizona_ldo1_hc_get_voltage_sel
,
119 .set_voltage_sel
= arizona_ldo1_hc_set_voltage_sel
,
120 .get_bypass
= regulator_get_bypass_regmap
,
121 .set_bypass
= regulator_set_bypass_regmap
,
124 static const struct regulator_desc arizona_ldo1_hc
= {
126 .supply_name
= "LDOVDD",
127 .type
= REGULATOR_VOLTAGE
,
128 .ops
= &arizona_ldo1_hc_ops
,
130 .bypass_reg
= ARIZONA_LDO1_CONTROL_1
,
131 .bypass_mask
= ARIZONA_LDO1_BYPASS
,
138 .owner
= THIS_MODULE
,
141 static const struct regulator_ops arizona_ldo1_ops
= {
142 .list_voltage
= regulator_list_voltage_linear
,
143 .map_voltage
= regulator_map_voltage_linear
,
144 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
145 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
148 static const struct regulator_desc arizona_ldo1
= {
150 .supply_name
= "LDOVDD",
151 .type
= REGULATOR_VOLTAGE
,
152 .ops
= &arizona_ldo1_ops
,
154 .vsel_reg
= ARIZONA_LDO1_CONTROL_1
,
155 .vsel_mask
= ARIZONA_LDO1_VSEL_MASK
,
162 .owner
= THIS_MODULE
,
165 static const struct regulator_init_data arizona_ldo1_dvfs
= {
169 .valid_ops_mask
= REGULATOR_CHANGE_STATUS
|
170 REGULATOR_CHANGE_VOLTAGE
,
172 .num_consumer_supplies
= 1,
175 static const struct regulator_init_data arizona_ldo1_default
= {
177 .valid_ops_mask
= REGULATOR_CHANGE_STATUS
,
179 .num_consumer_supplies
= 1,
182 static const struct regulator_init_data arizona_ldo1_wm5110
= {
186 .valid_ops_mask
= REGULATOR_CHANGE_STATUS
|
187 REGULATOR_CHANGE_VOLTAGE
,
189 .num_consumer_supplies
= 1,
192 static int arizona_ldo1_of_get_pdata(struct arizona_ldo1_pdata
*pdata
,
193 struct regulator_config
*config
,
194 const struct regulator_desc
*desc
,
195 bool *external_dcvdd
)
197 struct arizona_ldo1
*ldo1
= config
->driver_data
;
198 struct device_node
*np
= config
->dev
->of_node
;
199 struct device_node
*init_node
, *dcvdd_node
;
200 struct regulator_init_data
*init_data
;
202 init_node
= of_get_child_by_name(np
, "ldo1");
203 dcvdd_node
= of_parse_phandle(np
, "DCVDD-supply", 0);
206 config
->of_node
= init_node
;
208 init_data
= of_get_regulator_init_data(config
->dev
, init_node
,
211 init_data
->consumer_supplies
= &ldo1
->supply
;
212 init_data
->num_consumer_supplies
= 1;
214 if (dcvdd_node
&& dcvdd_node
!= init_node
)
215 *external_dcvdd
= true;
217 pdata
->init_data
= init_data
;
219 } else if (dcvdd_node
) {
220 *external_dcvdd
= true;
223 of_node_put(dcvdd_node
);
228 static int arizona_ldo1_common_init(struct platform_device
*pdev
,
229 struct arizona_ldo1
*ldo1
,
230 const struct regulator_desc
*desc
,
231 struct arizona_ldo1_pdata
*pdata
,
232 bool *external_dcvdd
)
234 struct device
*parent_dev
= pdev
->dev
.parent
;
235 struct regulator_config config
= { };
238 *external_dcvdd
= false;
240 ldo1
->supply
.supply
= "DCVDD";
241 ldo1
->init_data
.consumer_supplies
= &ldo1
->supply
;
242 ldo1
->supply
.dev_name
= dev_name(parent_dev
);
244 config
.dev
= parent_dev
;
245 config
.driver_data
= ldo1
;
246 config
.regmap
= ldo1
->regmap
;
248 if (IS_ENABLED(CONFIG_OF
)) {
249 if (!dev_get_platdata(parent_dev
)) {
250 ret
= arizona_ldo1_of_get_pdata(pdata
,
258 /* We assume that high output = regulator off
259 * Don't use devm, since we need to get against the parent device
260 * so clean up would happen at the wrong time
262 config
.ena_gpiod
= gpiod_get_optional(parent_dev
, "wlf,ldoena",
264 if (IS_ERR(config
.ena_gpiod
))
265 return PTR_ERR(config
.ena_gpiod
);
267 ldo1
->ena_gpiod
= config
.ena_gpiod
;
269 if (pdata
->init_data
)
270 config
.init_data
= pdata
->init_data
;
272 config
.init_data
= &ldo1
->init_data
;
275 * LDO1 can only be used to supply DCVDD so if it has no
276 * consumers then DCVDD is supplied externally.
278 if (config
.init_data
->num_consumer_supplies
== 0)
279 *external_dcvdd
= true;
281 ldo1
->regulator
= devm_regulator_register(&pdev
->dev
, desc
, &config
);
283 of_node_put(config
.of_node
);
285 if (IS_ERR(ldo1
->regulator
)) {
286 if (config
.ena_gpiod
)
287 gpiod_put(config
.ena_gpiod
);
289 ret
= PTR_ERR(ldo1
->regulator
);
290 dev_err(&pdev
->dev
, "Failed to register LDO1 supply: %d\n",
295 platform_set_drvdata(pdev
, ldo1
);
300 static int arizona_ldo1_probe(struct platform_device
*pdev
)
302 struct arizona
*arizona
= dev_get_drvdata(pdev
->dev
.parent
);
303 struct arizona_ldo1
*ldo1
;
304 const struct regulator_desc
*desc
;
308 ldo1
= devm_kzalloc(&pdev
->dev
, sizeof(*ldo1
), GFP_KERNEL
);
312 ldo1
->regmap
= arizona
->regmap
;
315 * Since the chip usually supplies itself we provide some
316 * default init_data for it. This will be overridden with
317 * platform data if provided.
319 switch (arizona
->type
) {
324 desc
= &arizona_ldo1_hc
;
325 ldo1
->init_data
= arizona_ldo1_dvfs
;
329 desc
= &arizona_ldo1
;
330 ldo1
->init_data
= arizona_ldo1_wm5110
;
333 desc
= &arizona_ldo1
;
334 ldo1
->init_data
= arizona_ldo1_default
;
338 ret
= arizona_ldo1_common_init(pdev
, ldo1
, desc
,
339 &arizona
->pdata
.ldo1
,
342 arizona
->external_dcvdd
= external_dcvdd
;
347 static int arizona_ldo1_remove(struct platform_device
*pdev
)
349 struct arizona_ldo1
*ldo1
= platform_get_drvdata(pdev
);
352 gpiod_put(ldo1
->ena_gpiod
);
357 static struct platform_driver arizona_ldo1_driver
= {
358 .probe
= arizona_ldo1_probe
,
359 .remove
= arizona_ldo1_remove
,
361 .name
= "arizona-ldo1",
365 module_platform_driver(arizona_ldo1_driver
);
367 /* Module information */
368 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
369 MODULE_DESCRIPTION("Arizona LDO1 driver");
370 MODULE_LICENSE("GPL");
371 MODULE_ALIAS("platform:arizona-ldo1");