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/platform_device.h>
21 #include <linux/regulator/driver.h>
22 #include <linux/regulator/machine.h>
23 #include <linux/regulator/of_regulator.h>
24 #include <linux/gpio.h>
25 #include <linux/slab.h>
27 #include <linux/mfd/arizona/core.h>
28 #include <linux/mfd/arizona/pdata.h>
29 #include <linux/mfd/arizona/registers.h>
32 struct regulator_dev
*regulator
;
33 struct arizona
*arizona
;
35 struct regulator_consumer_supply supply
;
36 struct regulator_init_data init_data
;
39 static int arizona_ldo1_hc_list_voltage(struct regulator_dev
*rdev
,
40 unsigned int selector
)
42 if (selector
>= rdev
->desc
->n_voltages
)
45 if (selector
== rdev
->desc
->n_voltages
- 1)
48 return rdev
->desc
->min_uV
+ (rdev
->desc
->uV_step
* selector
);
51 static int arizona_ldo1_hc_map_voltage(struct regulator_dev
*rdev
,
52 int min_uV
, int max_uV
)
56 sel
= DIV_ROUND_UP(min_uV
- rdev
->desc
->min_uV
, rdev
->desc
->uV_step
);
57 if (sel
>= rdev
->desc
->n_voltages
)
58 sel
= rdev
->desc
->n_voltages
- 1;
63 static int arizona_ldo1_hc_set_voltage_sel(struct regulator_dev
*rdev
,
66 struct arizona_ldo1
*ldo
= rdev_get_drvdata(rdev
);
67 struct regmap
*regmap
= ldo
->arizona
->regmap
;
71 if (sel
== rdev
->desc
->n_voltages
- 1)
72 val
= ARIZONA_LDO1_HI_PWR
;
76 ret
= regmap_update_bits(regmap
, ARIZONA_LDO1_CONTROL_2
,
77 ARIZONA_LDO1_HI_PWR
, val
);
81 ret
= regmap_update_bits(regmap
, ARIZONA_DYNAMIC_FREQUENCY_SCALING_1
,
82 ARIZONA_SUBSYS_MAX_FREQ
, val
);
89 val
= sel
<< ARIZONA_LDO1_VSEL_SHIFT
;
91 return regmap_update_bits(regmap
, ARIZONA_LDO1_CONTROL_1
,
92 ARIZONA_LDO1_VSEL_MASK
, val
);
95 static int arizona_ldo1_hc_get_voltage_sel(struct regulator_dev
*rdev
)
97 struct arizona_ldo1
*ldo
= rdev_get_drvdata(rdev
);
98 struct regmap
*regmap
= ldo
->arizona
->regmap
;
102 ret
= regmap_read(regmap
, ARIZONA_LDO1_CONTROL_2
, &val
);
106 if (val
& ARIZONA_LDO1_HI_PWR
)
107 return rdev
->desc
->n_voltages
- 1;
109 ret
= regmap_read(regmap
, ARIZONA_LDO1_CONTROL_1
, &val
);
113 return (val
& ARIZONA_LDO1_VSEL_MASK
) >> ARIZONA_LDO1_VSEL_SHIFT
;
116 static struct regulator_ops arizona_ldo1_hc_ops
= {
117 .list_voltage
= arizona_ldo1_hc_list_voltage
,
118 .map_voltage
= arizona_ldo1_hc_map_voltage
,
119 .get_voltage_sel
= arizona_ldo1_hc_get_voltage_sel
,
120 .set_voltage_sel
= arizona_ldo1_hc_set_voltage_sel
,
121 .get_bypass
= regulator_get_bypass_regmap
,
122 .set_bypass
= regulator_set_bypass_regmap
,
125 static const struct regulator_desc arizona_ldo1_hc
= {
127 .supply_name
= "LDOVDD",
128 .type
= REGULATOR_VOLTAGE
,
129 .ops
= &arizona_ldo1_hc_ops
,
131 .bypass_reg
= ARIZONA_LDO1_CONTROL_1
,
132 .bypass_mask
= ARIZONA_LDO1_BYPASS
,
138 .owner
= THIS_MODULE
,
141 static 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
,
161 .owner
= THIS_MODULE
,
164 static const struct regulator_init_data arizona_ldo1_dvfs
= {
168 .valid_ops_mask
= REGULATOR_CHANGE_STATUS
|
169 REGULATOR_CHANGE_VOLTAGE
,
171 .num_consumer_supplies
= 1,
174 static const struct regulator_init_data arizona_ldo1_default
= {
176 .valid_ops_mask
= REGULATOR_CHANGE_STATUS
,
178 .num_consumer_supplies
= 1,
181 static int arizona_ldo1_of_get_pdata(struct arizona
*arizona
,
182 struct regulator_config
*config
)
184 struct arizona_pdata
*pdata
= &arizona
->pdata
;
185 struct arizona_ldo1
*ldo1
= config
->driver_data
;
186 struct device_node
*init_node
, *dcvdd_node
;
187 struct regulator_init_data
*init_data
;
189 pdata
->ldoena
= arizona_of_get_named_gpio(arizona
, "wlf,ldoena", true);
191 init_node
= of_get_child_by_name(arizona
->dev
->of_node
, "ldo1");
192 dcvdd_node
= of_parse_phandle(arizona
->dev
->of_node
, "DCVDD-supply", 0);
195 config
->of_node
= init_node
;
197 init_data
= of_get_regulator_init_data(arizona
->dev
, init_node
);
200 init_data
->consumer_supplies
= &ldo1
->supply
;
201 init_data
->num_consumer_supplies
= 1;
203 if (dcvdd_node
&& dcvdd_node
!= init_node
)
204 arizona
->external_dcvdd
= true;
206 pdata
->ldo1
= init_data
;
208 } else if (dcvdd_node
) {
209 arizona
->external_dcvdd
= true;
212 of_node_put(dcvdd_node
);
217 static int arizona_ldo1_probe(struct platform_device
*pdev
)
219 struct arizona
*arizona
= dev_get_drvdata(pdev
->dev
.parent
);
220 const struct regulator_desc
*desc
;
221 struct regulator_config config
= { };
222 struct arizona_ldo1
*ldo1
;
225 arizona
->external_dcvdd
= false;
227 ldo1
= devm_kzalloc(&pdev
->dev
, sizeof(*ldo1
), GFP_KERNEL
);
231 ldo1
->arizona
= arizona
;
234 * Since the chip usually supplies itself we provide some
235 * default init_data for it. This will be overridden with
236 * platform data if provided.
238 switch (arizona
->type
) {
241 desc
= &arizona_ldo1_hc
;
242 ldo1
->init_data
= arizona_ldo1_dvfs
;
245 desc
= &arizona_ldo1
;
246 ldo1
->init_data
= arizona_ldo1_default
;
250 ldo1
->init_data
.consumer_supplies
= &ldo1
->supply
;
251 ldo1
->supply
.supply
= "DCVDD";
252 ldo1
->supply
.dev_name
= dev_name(arizona
->dev
);
254 config
.dev
= arizona
->dev
;
255 config
.driver_data
= ldo1
;
256 config
.regmap
= arizona
->regmap
;
258 if (IS_ENABLED(CONFIG_OF
)) {
259 if (!dev_get_platdata(arizona
->dev
)) {
260 ret
= arizona_ldo1_of_get_pdata(arizona
, &config
);
266 config
.ena_gpio
= arizona
->pdata
.ldoena
;
268 if (arizona
->pdata
.ldo1
)
269 config
.init_data
= arizona
->pdata
.ldo1
;
271 config
.init_data
= &ldo1
->init_data
;
274 * LDO1 can only be used to supply DCVDD so if it has no
275 * consumers then DCVDD is supplied externally.
277 if (config
.init_data
->num_consumer_supplies
== 0)
278 arizona
->external_dcvdd
= true;
280 ldo1
->regulator
= devm_regulator_register(&pdev
->dev
, desc
, &config
);
281 if (IS_ERR(ldo1
->regulator
)) {
282 ret
= PTR_ERR(ldo1
->regulator
);
283 dev_err(arizona
->dev
, "Failed to register LDO1 supply: %d\n",
288 of_node_put(config
.of_node
);
290 platform_set_drvdata(pdev
, ldo1
);
295 static struct platform_driver arizona_ldo1_driver
= {
296 .probe
= arizona_ldo1_probe
,
298 .name
= "arizona-ldo1",
299 .owner
= THIS_MODULE
,
303 module_platform_driver(arizona_ldo1_driver
);
305 /* Module information */
306 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
307 MODULE_DESCRIPTION("Arizona LDO1 driver");
308 MODULE_LICENSE("GPL");
309 MODULE_ALIAS("platform:arizona-ldo1");