blob: f6d6a4ad9e8a28c8561b2ca807ccf2b11ed9aad3 [file] [log] [blame]
Mark Brown1910efa2012-06-23 13:07:57 +01001/*
2 * arizona-ldo1.c -- LDO1 supply for Arizona devices
3 *
4 * Copyright 2012 Wolfson Microelectronics PLC.
5 *
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
7 *
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.
12 */
13
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>
Arnd Bergmann7b22b9a2014-05-27 17:27:34 +010019#include <linux/of.h>
Linus Walleije1739e82018-05-14 10:06:24 +020020#include <linux/gpio/consumer.h>
Mark Brown1910efa2012-06-23 13:07:57 +010021#include <linux/platform_device.h>
22#include <linux/regulator/driver.h>
23#include <linux/regulator/machine.h>
Charles Keepax2cce4be2014-04-16 10:01:39 +010024#include <linux/regulator/of_regulator.h>
Mark Brown1910efa2012-06-23 13:07:57 +010025#include <linux/slab.h>
26
Richard Fitzgeraldaaa84e62017-04-18 11:43:52 +010027#include <linux/regulator/arizona-ldo1.h>
28
Mark Brown1910efa2012-06-23 13:07:57 +010029#include <linux/mfd/arizona/core.h>
30#include <linux/mfd/arizona/pdata.h>
31#include <linux/mfd/arizona/registers.h>
32
33struct arizona_ldo1 {
34 struct regulator_dev *regulator;
Richard Fitzgerald80a55f42017-04-18 11:43:53 +010035 struct regmap *regmap;
Mark Brown1910efa2012-06-23 13:07:57 +010036
37 struct regulator_consumer_supply supply;
38 struct regulator_init_data init_data;
39};
40
Mark Brown73ee2942012-11-27 18:48:33 +000041static int arizona_ldo1_hc_list_voltage(struct regulator_dev *rdev,
42 unsigned int selector)
43{
44 if (selector >= rdev->desc->n_voltages)
45 return -EINVAL;
46
47 if (selector == rdev->desc->n_voltages - 1)
48 return 1800000;
49 else
50 return rdev->desc->min_uV + (rdev->desc->uV_step * selector);
51}
52
53static int arizona_ldo1_hc_map_voltage(struct regulator_dev *rdev,
54 int min_uV, int max_uV)
55{
56 int sel;
57
58 sel = DIV_ROUND_UP(min_uV - rdev->desc->min_uV, rdev->desc->uV_step);
59 if (sel >= rdev->desc->n_voltages)
60 sel = rdev->desc->n_voltages - 1;
61
62 return sel;
63}
64
65static int arizona_ldo1_hc_set_voltage_sel(struct regulator_dev *rdev,
66 unsigned sel)
67{
68 struct arizona_ldo1 *ldo = rdev_get_drvdata(rdev);
Richard Fitzgerald80a55f42017-04-18 11:43:53 +010069 struct regmap *regmap = ldo->regmap;
Mark Brown73ee2942012-11-27 18:48:33 +000070 unsigned int val;
71 int ret;
72
73 if (sel == rdev->desc->n_voltages - 1)
74 val = ARIZONA_LDO1_HI_PWR;
75 else
76 val = 0;
77
78 ret = regmap_update_bits(regmap, ARIZONA_LDO1_CONTROL_2,
79 ARIZONA_LDO1_HI_PWR, val);
80 if (ret != 0)
81 return ret;
82
Mark Brown73ee2942012-11-27 18:48:33 +000083 if (val)
84 return 0;
85
86 val = sel << ARIZONA_LDO1_VSEL_SHIFT;
87
88 return regmap_update_bits(regmap, ARIZONA_LDO1_CONTROL_1,
89 ARIZONA_LDO1_VSEL_MASK, val);
90}
91
92static int arizona_ldo1_hc_get_voltage_sel(struct regulator_dev *rdev)
93{
94 struct arizona_ldo1 *ldo = rdev_get_drvdata(rdev);
Richard Fitzgerald80a55f42017-04-18 11:43:53 +010095 struct regmap *regmap = ldo->regmap;
Mark Brown73ee2942012-11-27 18:48:33 +000096 unsigned int val;
97 int ret;
98
99 ret = regmap_read(regmap, ARIZONA_LDO1_CONTROL_2, &val);
100 if (ret != 0)
101 return ret;
102
103 if (val & ARIZONA_LDO1_HI_PWR)
104 return rdev->desc->n_voltages - 1;
105
106 ret = regmap_read(regmap, ARIZONA_LDO1_CONTROL_1, &val);
107 if (ret != 0)
108 return ret;
109
110 return (val & ARIZONA_LDO1_VSEL_MASK) >> ARIZONA_LDO1_VSEL_SHIFT;
111}
112
Bhumika Goyal03d06102017-01-28 19:08:41 +0530113static const struct regulator_ops arizona_ldo1_hc_ops = {
Mark Brown73ee2942012-11-27 18:48:33 +0000114 .list_voltage = arizona_ldo1_hc_list_voltage,
115 .map_voltage = arizona_ldo1_hc_map_voltage,
116 .get_voltage_sel = arizona_ldo1_hc_get_voltage_sel,
117 .set_voltage_sel = arizona_ldo1_hc_set_voltage_sel,
118 .get_bypass = regulator_get_bypass_regmap,
119 .set_bypass = regulator_set_bypass_regmap,
120};
121
122static const struct regulator_desc arizona_ldo1_hc = {
123 .name = "LDO1",
124 .supply_name = "LDOVDD",
125 .type = REGULATOR_VOLTAGE,
126 .ops = &arizona_ldo1_hc_ops,
127
128 .bypass_reg = ARIZONA_LDO1_CONTROL_1,
129 .bypass_mask = ARIZONA_LDO1_BYPASS,
130 .min_uV = 900000,
131 .uV_step = 50000,
132 .n_voltages = 8,
Mark Brownce1bcb72013-03-26 12:02:32 +0000133 .enable_time = 1500,
Charles Keepax88f0e3a2016-10-21 14:47:42 +0100134 .ramp_delay = 24000,
Mark Brown73ee2942012-11-27 18:48:33 +0000135
136 .owner = THIS_MODULE,
137};
138
Bhumika Goyal03d06102017-01-28 19:08:41 +0530139static const struct regulator_ops arizona_ldo1_ops = {
Mark Brown1910efa2012-06-23 13:07:57 +0100140 .list_voltage = regulator_list_voltage_linear,
141 .map_voltage = regulator_map_voltage_linear,
142 .get_voltage_sel = regulator_get_voltage_sel_regmap,
143 .set_voltage_sel = regulator_set_voltage_sel_regmap,
144};
145
146static const struct regulator_desc arizona_ldo1 = {
147 .name = "LDO1",
148 .supply_name = "LDOVDD",
149 .type = REGULATOR_VOLTAGE,
150 .ops = &arizona_ldo1_ops,
151
152 .vsel_reg = ARIZONA_LDO1_CONTROL_1,
153 .vsel_mask = ARIZONA_LDO1_VSEL_MASK,
154 .min_uV = 900000,
Charles Keepaxa35ff282014-03-18 10:49:17 +0000155 .uV_step = 25000,
156 .n_voltages = 13,
Mark Brown86a14502012-11-27 17:02:29 +0000157 .enable_time = 500,
Charles Keepax88f0e3a2016-10-21 14:47:42 +0100158 .ramp_delay = 24000,
Mark Brown1910efa2012-06-23 13:07:57 +0100159
160 .owner = THIS_MODULE,
161};
162
Mark Brown55a18ae2012-11-27 17:51:46 +0000163static const struct regulator_init_data arizona_ldo1_dvfs = {
164 .constraints = {
165 .min_uV = 1200000,
166 .max_uV = 1800000,
167 .valid_ops_mask = REGULATOR_CHANGE_STATUS |
168 REGULATOR_CHANGE_VOLTAGE,
169 },
170 .num_consumer_supplies = 1,
171};
172
Mark Brown1910efa2012-06-23 13:07:57 +0100173static const struct regulator_init_data arizona_ldo1_default = {
Mark Browna9905b12012-06-27 14:32:06 +0100174 .constraints = {
175 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
176 },
Mark Brown1910efa2012-06-23 13:07:57 +0100177 .num_consumer_supplies = 1,
178};
179
Charles Keepax9a6c2fe2015-05-11 13:58:07 +0100180static const struct regulator_init_data arizona_ldo1_wm5110 = {
181 .constraints = {
182 .min_uV = 1175000,
183 .max_uV = 1200000,
184 .valid_ops_mask = REGULATOR_CHANGE_STATUS |
185 REGULATOR_CHANGE_VOLTAGE,
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
Linus Walleije1739e82018-05-14 10:06:24 +0200256 /* We assume that high output = regulator off */
257 config.ena_gpiod = devm_gpiod_get_optional(&pdev->dev, "wlf,ldoena",
258 GPIOD_OUT_HIGH);
259 if (IS_ERR(config.ena_gpiod))
260 return PTR_ERR(config.ena_gpiod);
Richard Fitzgeraldaf367af2017-04-18 11:43:54 +0100261
262 if (pdata->init_data)
263 config.init_data = pdata->init_data;
264 else
265 config.init_data = &ldo1->init_data;
266
267 /*
268 * LDO1 can only be used to supply DCVDD so if it has no
269 * consumers then DCVDD is supplied externally.
270 */
271 if (config.init_data->num_consumer_supplies == 0)
272 *external_dcvdd = true;
273
274 ldo1->regulator = devm_regulator_register(&pdev->dev, desc, &config);
275
276 of_node_put(config.of_node);
277
278 if (IS_ERR(ldo1->regulator)) {
279 ret = PTR_ERR(ldo1->regulator);
280 dev_err(&pdev->dev, "Failed to register LDO1 supply: %d\n",
281 ret);
282 return ret;
283 }
284
285 platform_set_drvdata(pdev, ldo1);
286
287 return 0;
288}
289
Bill Pembertona5023572012-11-19 13:22:22 -0500290static int arizona_ldo1_probe(struct platform_device *pdev)
Mark Brown1910efa2012-06-23 13:07:57 +0100291{
292 struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
Mark Brown1910efa2012-06-23 13:07:57 +0100293 struct arizona_ldo1 *ldo1;
Richard Fitzgeraldaf367af2017-04-18 11:43:54 +0100294 const struct regulator_desc *desc;
295 bool external_dcvdd;
Mark Brown1910efa2012-06-23 13:07:57 +0100296 int ret;
297
298 ldo1 = devm_kzalloc(&pdev->dev, sizeof(*ldo1), GFP_KERNEL);
Sachin Kamatf4a6c5b2014-02-18 16:11:09 +0530299 if (!ldo1)
Mark Brown1910efa2012-06-23 13:07:57 +0100300 return -ENOMEM;
Mark Brown1910efa2012-06-23 13:07:57 +0100301
Richard Fitzgerald80a55f42017-04-18 11:43:53 +0100302 ldo1->regmap = arizona->regmap;
Mark Brown1910efa2012-06-23 13:07:57 +0100303
304 /*
305 * Since the chip usually supplies itself we provide some
306 * default init_data for it. This will be overridden with
307 * platform data if provided.
308 */
Mark Brown55a18ae2012-11-27 17:51:46 +0000309 switch (arizona->type) {
310 case WM5102:
Charles Keepaxa35ff282014-03-18 10:49:17 +0000311 case WM8997:
Richard Fitzgerald33aa3802015-09-28 14:01:07 +0100312 case WM8998:
313 case WM1814:
Mark Brown73ee2942012-11-27 18:48:33 +0000314 desc = &arizona_ldo1_hc;
Mark Brown55a18ae2012-11-27 17:51:46 +0000315 ldo1->init_data = arizona_ldo1_dvfs;
316 break;
Charles Keepax9a6c2fe2015-05-11 13:58:07 +0100317 case WM5110:
318 case WM8280:
319 desc = &arizona_ldo1;
320 ldo1->init_data = arizona_ldo1_wm5110;
321 break;
Mark Brown55a18ae2012-11-27 17:51:46 +0000322 default:
Mark Brown73ee2942012-11-27 18:48:33 +0000323 desc = &arizona_ldo1;
Mark Brown55a18ae2012-11-27 17:51:46 +0000324 ldo1->init_data = arizona_ldo1_default;
325 break;
326 }
327
Richard Fitzgeraldaf367af2017-04-18 11:43:54 +0100328 ret = arizona_ldo1_common_init(pdev, ldo1, desc,
329 &arizona->pdata.ldo1,
330 &external_dcvdd);
331 if (ret == 0)
Richard Fitzgeraldaaa84e62017-04-18 11:43:52 +0100332 arizona->external_dcvdd = external_dcvdd;
Charles Keepax4a8c4752014-04-16 10:01:38 +0100333
Richard Fitzgeraldaf367af2017-04-18 11:43:54 +0100334 return ret;
Mark Brown1910efa2012-06-23 13:07:57 +0100335}
336
Mark Brown1910efa2012-06-23 13:07:57 +0100337static struct platform_driver arizona_ldo1_driver = {
338 .probe = arizona_ldo1_probe,
Mark Brown1910efa2012-06-23 13:07:57 +0100339 .driver = {
340 .name = "arizona-ldo1",
Mark Brown1910efa2012-06-23 13:07:57 +0100341 },
342};
343
344module_platform_driver(arizona_ldo1_driver);
345
346/* Module information */
347MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
348MODULE_DESCRIPTION("Arizona LDO1 driver");
349MODULE_LICENSE("GPL");
350MODULE_ALIAS("platform:arizona-ldo1");