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/of_gpio.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/gpio.h>
26 #include <linux/slab.h>
28 #include <linux/mfd/arizona/core.h>
29 #include <linux/mfd/arizona/pdata.h>
30 #include <linux/mfd/arizona/registers.h>
33 struct regulator_dev
*regulator
;
34 struct arizona
*arizona
;
36 struct regulator_consumer_supply supply
;
37 struct regulator_init_data init_data
;
40 static int arizona_ldo1_hc_list_voltage(struct regulator_dev
*rdev
,
41 unsigned int selector
)
43 if (selector
>= rdev
->desc
->n_voltages
)
46 if (selector
== rdev
->desc
->n_voltages
- 1)
49 return rdev
->desc
->min_uV
+ (rdev
->desc
->uV_step
* selector
);
52 static int arizona_ldo1_hc_map_voltage(struct regulator_dev
*rdev
,
53 int min_uV
, int max_uV
)
57 sel
= DIV_ROUND_UP(min_uV
- rdev
->desc
->min_uV
, rdev
->desc
->uV_step
);
58 if (sel
>= rdev
->desc
->n_voltages
)
59 sel
= rdev
->desc
->n_voltages
- 1;
64 static int arizona_ldo1_hc_set_voltage_sel(struct regulator_dev
*rdev
,
67 struct arizona_ldo1
*ldo
= rdev_get_drvdata(rdev
);
68 struct regmap
*regmap
= ldo
->arizona
->regmap
;
72 if (sel
== rdev
->desc
->n_voltages
- 1)
73 val
= ARIZONA_LDO1_HI_PWR
;
77 ret
= regmap_update_bits(regmap
, ARIZONA_LDO1_CONTROL_2
,
78 ARIZONA_LDO1_HI_PWR
, val
);
85 val
= sel
<< ARIZONA_LDO1_VSEL_SHIFT
;
87 return regmap_update_bits(regmap
, ARIZONA_LDO1_CONTROL_1
,
88 ARIZONA_LDO1_VSEL_MASK
, val
);
91 static int arizona_ldo1_hc_get_voltage_sel(struct regulator_dev
*rdev
)
93 struct arizona_ldo1
*ldo
= rdev_get_drvdata(rdev
);
94 struct regmap
*regmap
= ldo
->arizona
->regmap
;
98 ret
= regmap_read(regmap
, ARIZONA_LDO1_CONTROL_2
, &val
);
102 if (val
& ARIZONA_LDO1_HI_PWR
)
103 return rdev
->desc
->n_voltages
- 1;
105 ret
= regmap_read(regmap
, ARIZONA_LDO1_CONTROL_1
, &val
);
109 return (val
& ARIZONA_LDO1_VSEL_MASK
) >> ARIZONA_LDO1_VSEL_SHIFT
;
112 static const struct regulator_ops arizona_ldo1_hc_ops
= {
113 .list_voltage
= arizona_ldo1_hc_list_voltage
,
114 .map_voltage
= arizona_ldo1_hc_map_voltage
,
115 .get_voltage_sel
= arizona_ldo1_hc_get_voltage_sel
,
116 .set_voltage_sel
= arizona_ldo1_hc_set_voltage_sel
,
117 .get_bypass
= regulator_get_bypass_regmap
,
118 .set_bypass
= regulator_set_bypass_regmap
,
121 static const struct regulator_desc arizona_ldo1_hc
= {
123 .supply_name
= "LDOVDD",
124 .type
= REGULATOR_VOLTAGE
,
125 .ops
= &arizona_ldo1_hc_ops
,
127 .bypass_reg
= ARIZONA_LDO1_CONTROL_1
,
128 .bypass_mask
= ARIZONA_LDO1_BYPASS
,
135 .owner
= THIS_MODULE
,
138 static const struct regulator_ops arizona_ldo1_ops
= {
139 .list_voltage
= regulator_list_voltage_linear
,
140 .map_voltage
= regulator_map_voltage_linear
,
141 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
142 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
145 static const struct regulator_desc arizona_ldo1
= {
147 .supply_name
= "LDOVDD",
148 .type
= REGULATOR_VOLTAGE
,
149 .ops
= &arizona_ldo1_ops
,
151 .vsel_reg
= ARIZONA_LDO1_CONTROL_1
,
152 .vsel_mask
= ARIZONA_LDO1_VSEL_MASK
,
159 .owner
= THIS_MODULE
,
162 static const struct regulator_init_data arizona_ldo1_dvfs
= {
166 .valid_ops_mask
= REGULATOR_CHANGE_STATUS
|
167 REGULATOR_CHANGE_VOLTAGE
,
169 .num_consumer_supplies
= 1,
172 static const struct regulator_init_data arizona_ldo1_default
= {
174 .valid_ops_mask
= REGULATOR_CHANGE_STATUS
,
176 .num_consumer_supplies
= 1,
179 static const struct regulator_init_data arizona_ldo1_wm5110
= {
183 .valid_ops_mask
= REGULATOR_CHANGE_STATUS
|
184 REGULATOR_CHANGE_VOLTAGE
,
186 .num_consumer_supplies
= 1,
189 static int arizona_ldo1_of_get_pdata(struct arizona
*arizona
,
190 struct regulator_config
*config
,
191 const struct regulator_desc
*desc
)
193 struct arizona_pdata
*pdata
= &arizona
->pdata
;
194 struct arizona_ldo1
*ldo1
= config
->driver_data
;
195 struct device_node
*np
= arizona
->dev
->of_node
;
196 struct device_node
*init_node
, *dcvdd_node
;
197 struct regulator_init_data
*init_data
;
199 pdata
->ldoena
= of_get_named_gpio(np
, "wlf,ldoena", 0);
200 if (pdata
->ldoena
< 0) {
201 dev_warn(arizona
->dev
,
202 "LDOENA GPIO property missing/malformed: %d\n",
206 config
->ena_gpio_initialized
= true;
209 init_node
= of_get_child_by_name(np
, "ldo1");
210 dcvdd_node
= of_parse_phandle(np
, "DCVDD-supply", 0);
213 config
->of_node
= init_node
;
215 init_data
= of_get_regulator_init_data(arizona
->dev
, init_node
,
219 init_data
->consumer_supplies
= &ldo1
->supply
;
220 init_data
->num_consumer_supplies
= 1;
222 if (dcvdd_node
&& dcvdd_node
!= init_node
)
223 arizona
->external_dcvdd
= true;
225 pdata
->ldo1
= init_data
;
227 } else if (dcvdd_node
) {
228 arizona
->external_dcvdd
= true;
231 of_node_put(dcvdd_node
);
236 static int arizona_ldo1_probe(struct platform_device
*pdev
)
238 struct arizona
*arizona
= dev_get_drvdata(pdev
->dev
.parent
);
239 const struct regulator_desc
*desc
;
240 struct regulator_config config
= { };
241 struct arizona_ldo1
*ldo1
;
244 arizona
->external_dcvdd
= false;
246 ldo1
= devm_kzalloc(&pdev
->dev
, sizeof(*ldo1
), GFP_KERNEL
);
250 ldo1
->arizona
= arizona
;
253 * Since the chip usually supplies itself we provide some
254 * default init_data for it. This will be overridden with
255 * platform data if provided.
257 switch (arizona
->type
) {
262 desc
= &arizona_ldo1_hc
;
263 ldo1
->init_data
= arizona_ldo1_dvfs
;
267 desc
= &arizona_ldo1
;
268 ldo1
->init_data
= arizona_ldo1_wm5110
;
271 desc
= &arizona_ldo1
;
272 ldo1
->init_data
= arizona_ldo1_default
;
276 ldo1
->init_data
.consumer_supplies
= &ldo1
->supply
;
277 ldo1
->supply
.supply
= "DCVDD";
278 ldo1
->supply
.dev_name
= dev_name(arizona
->dev
);
280 config
.dev
= arizona
->dev
;
281 config
.driver_data
= ldo1
;
282 config
.regmap
= arizona
->regmap
;
284 if (IS_ENABLED(CONFIG_OF
)) {
285 if (!dev_get_platdata(arizona
->dev
)) {
286 ret
= arizona_ldo1_of_get_pdata(arizona
, &config
, desc
);
292 config
.ena_gpio
= arizona
->pdata
.ldoena
;
294 if (arizona
->pdata
.ldo1
)
295 config
.init_data
= arizona
->pdata
.ldo1
;
297 config
.init_data
= &ldo1
->init_data
;
300 * LDO1 can only be used to supply DCVDD so if it has no
301 * consumers then DCVDD is supplied externally.
303 if (config
.init_data
->num_consumer_supplies
== 0)
304 arizona
->external_dcvdd
= true;
306 ldo1
->regulator
= devm_regulator_register(&pdev
->dev
, desc
, &config
);
308 of_node_put(config
.of_node
);
310 if (IS_ERR(ldo1
->regulator
)) {
311 ret
= PTR_ERR(ldo1
->regulator
);
312 dev_err(arizona
->dev
, "Failed to register LDO1 supply: %d\n",
317 platform_set_drvdata(pdev
, ldo1
);
322 static struct platform_driver arizona_ldo1_driver
= {
323 .probe
= arizona_ldo1_probe
,
325 .name
= "arizona-ldo1",
329 module_platform_driver(arizona_ldo1_driver
);
331 /* Module information */
332 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
333 MODULE_DESCRIPTION("Arizona LDO1 driver");
334 MODULE_LICENSE("GPL");
335 MODULE_ALIAS("platform:arizona-ldo1");