blob: a1d07d347c20e30af065d204d94a314d18c408e3 [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>
Mark Brown1910efa2012-06-23 13:07:57 +010020#include <linux/platform_device.h>
21#include <linux/regulator/driver.h>
22#include <linux/regulator/machine.h>
Charles Keepax2cce4be2014-04-16 10:01:39 +010023#include <linux/regulator/of_regulator.h>
Mark Brown1910efa2012-06-23 13:07:57 +010024#include <linux/gpio.h>
25#include <linux/slab.h>
26
27#include <linux/mfd/arizona/core.h>
28#include <linux/mfd/arizona/pdata.h>
29#include <linux/mfd/arizona/registers.h>
30
31struct arizona_ldo1 {
32 struct regulator_dev *regulator;
33 struct arizona *arizona;
34
35 struct regulator_consumer_supply supply;
36 struct regulator_init_data init_data;
37};
38
Mark Brown73ee2942012-11-27 18:48:33 +000039static int arizona_ldo1_hc_list_voltage(struct regulator_dev *rdev,
40 unsigned int selector)
41{
42 if (selector >= rdev->desc->n_voltages)
43 return -EINVAL;
44
45 if (selector == rdev->desc->n_voltages - 1)
46 return 1800000;
47 else
48 return rdev->desc->min_uV + (rdev->desc->uV_step * selector);
49}
50
51static int arizona_ldo1_hc_map_voltage(struct regulator_dev *rdev,
52 int min_uV, int max_uV)
53{
54 int sel;
55
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;
59
60 return sel;
61}
62
63static int arizona_ldo1_hc_set_voltage_sel(struct regulator_dev *rdev,
64 unsigned sel)
65{
66 struct arizona_ldo1 *ldo = rdev_get_drvdata(rdev);
67 struct regmap *regmap = ldo->arizona->regmap;
68 unsigned int val;
69 int ret;
70
71 if (sel == rdev->desc->n_voltages - 1)
72 val = ARIZONA_LDO1_HI_PWR;
73 else
74 val = 0;
75
76 ret = regmap_update_bits(regmap, ARIZONA_LDO1_CONTROL_2,
77 ARIZONA_LDO1_HI_PWR, val);
78 if (ret != 0)
79 return ret;
80
81 ret = regmap_update_bits(regmap, ARIZONA_DYNAMIC_FREQUENCY_SCALING_1,
82 ARIZONA_SUBSYS_MAX_FREQ, val);
83 if (ret != 0)
84 return ret;
85
86 if (val)
87 return 0;
88
89 val = sel << ARIZONA_LDO1_VSEL_SHIFT;
90
91 return regmap_update_bits(regmap, ARIZONA_LDO1_CONTROL_1,
92 ARIZONA_LDO1_VSEL_MASK, val);
93}
94
95static int arizona_ldo1_hc_get_voltage_sel(struct regulator_dev *rdev)
96{
97 struct arizona_ldo1 *ldo = rdev_get_drvdata(rdev);
98 struct regmap *regmap = ldo->arizona->regmap;
99 unsigned int val;
100 int ret;
101
102 ret = regmap_read(regmap, ARIZONA_LDO1_CONTROL_2, &val);
103 if (ret != 0)
104 return ret;
105
106 if (val & ARIZONA_LDO1_HI_PWR)
107 return rdev->desc->n_voltages - 1;
108
109 ret = regmap_read(regmap, ARIZONA_LDO1_CONTROL_1, &val);
110 if (ret != 0)
111 return ret;
112
113 return (val & ARIZONA_LDO1_VSEL_MASK) >> ARIZONA_LDO1_VSEL_SHIFT;
114}
115
116static 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,
123};
124
125static const struct regulator_desc arizona_ldo1_hc = {
126 .name = "LDO1",
127 .supply_name = "LDOVDD",
128 .type = REGULATOR_VOLTAGE,
129 .ops = &arizona_ldo1_hc_ops,
130
131 .bypass_reg = ARIZONA_LDO1_CONTROL_1,
132 .bypass_mask = ARIZONA_LDO1_BYPASS,
133 .min_uV = 900000,
134 .uV_step = 50000,
135 .n_voltages = 8,
Mark Brownce1bcb72013-03-26 12:02:32 +0000136 .enable_time = 1500,
Mark Brown73ee2942012-11-27 18:48:33 +0000137
138 .owner = THIS_MODULE,
139};
140
Mark Brown1910efa2012-06-23 13:07:57 +0100141static 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,
146};
147
148static const struct regulator_desc arizona_ldo1 = {
149 .name = "LDO1",
150 .supply_name = "LDOVDD",
151 .type = REGULATOR_VOLTAGE,
152 .ops = &arizona_ldo1_ops,
153
154 .vsel_reg = ARIZONA_LDO1_CONTROL_1,
155 .vsel_mask = ARIZONA_LDO1_VSEL_MASK,
156 .min_uV = 900000,
Charles Keepaxa35ff282014-03-18 10:49:17 +0000157 .uV_step = 25000,
158 .n_voltages = 13,
Mark Brown86a14502012-11-27 17:02:29 +0000159 .enable_time = 500,
Mark Brown1910efa2012-06-23 13:07:57 +0100160
161 .owner = THIS_MODULE,
162};
163
Mark Brown55a18ae2012-11-27 17:51:46 +0000164static const struct regulator_init_data arizona_ldo1_dvfs = {
165 .constraints = {
166 .min_uV = 1200000,
167 .max_uV = 1800000,
168 .valid_ops_mask = REGULATOR_CHANGE_STATUS |
169 REGULATOR_CHANGE_VOLTAGE,
170 },
171 .num_consumer_supplies = 1,
172};
173
Mark Brown1910efa2012-06-23 13:07:57 +0100174static const struct regulator_init_data arizona_ldo1_default = {
Mark Browna9905b12012-06-27 14:32:06 +0100175 .constraints = {
176 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
177 },
Mark Brown1910efa2012-06-23 13:07:57 +0100178 .num_consumer_supplies = 1,
179};
180
Charles Keepax2cce4be2014-04-16 10:01:39 +0100181static int arizona_ldo1_of_get_pdata(struct arizona *arizona,
Javier Martinez Canillas072e78b2014-11-10 14:43:53 +0100182 struct regulator_config *config,
183 const struct regulator_desc *desc)
Charles Keepax4a8c4752014-04-16 10:01:38 +0100184{
185 struct arizona_pdata *pdata = &arizona->pdata;
Charles Keepax2cce4be2014-04-16 10:01:39 +0100186 struct arizona_ldo1 *ldo1 = config->driver_data;
187 struct device_node *init_node, *dcvdd_node;
188 struct regulator_init_data *init_data;
Charles Keepax4a8c4752014-04-16 10:01:38 +0100189
190 pdata->ldoena = arizona_of_get_named_gpio(arizona, "wlf,ldoena", true);
191
Charles Keepax2cce4be2014-04-16 10:01:39 +0100192 init_node = of_get_child_by_name(arizona->dev->of_node, "ldo1");
193 dcvdd_node = of_parse_phandle(arizona->dev->of_node, "DCVDD-supply", 0);
194
195 if (init_node) {
196 config->of_node = init_node;
197
Javier Martinez Canillas072e78b2014-11-10 14:43:53 +0100198 init_data = of_get_regulator_init_data(arizona->dev, init_node,
199 desc);
Charles Keepax2cce4be2014-04-16 10:01:39 +0100200
201 if (init_data) {
202 init_data->consumer_supplies = &ldo1->supply;
203 init_data->num_consumer_supplies = 1;
204
205 if (dcvdd_node && dcvdd_node != init_node)
206 arizona->external_dcvdd = true;
207
208 pdata->ldo1 = init_data;
209 }
210 } else if (dcvdd_node) {
211 arizona->external_dcvdd = true;
212 }
213
214 of_node_put(dcvdd_node);
215
Charles Keepax4a8c4752014-04-16 10:01:38 +0100216 return 0;
217}
218
Bill Pembertona5023572012-11-19 13:22:22 -0500219static int arizona_ldo1_probe(struct platform_device *pdev)
Mark Brown1910efa2012-06-23 13:07:57 +0100220{
221 struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
Mark Brown73ee2942012-11-27 18:48:33 +0000222 const struct regulator_desc *desc;
Mark Brown1910efa2012-06-23 13:07:57 +0100223 struct regulator_config config = { };
224 struct arizona_ldo1 *ldo1;
225 int ret;
226
Charles Keepax4a8c4752014-04-16 10:01:38 +0100227 arizona->external_dcvdd = false;
228
Mark Brown1910efa2012-06-23 13:07:57 +0100229 ldo1 = devm_kzalloc(&pdev->dev, sizeof(*ldo1), GFP_KERNEL);
Sachin Kamatf4a6c5b2014-02-18 16:11:09 +0530230 if (!ldo1)
Mark Brown1910efa2012-06-23 13:07:57 +0100231 return -ENOMEM;
Mark Brown1910efa2012-06-23 13:07:57 +0100232
233 ldo1->arizona = arizona;
234
235 /*
236 * Since the chip usually supplies itself we provide some
237 * default init_data for it. This will be overridden with
238 * platform data if provided.
239 */
Mark Brown55a18ae2012-11-27 17:51:46 +0000240 switch (arizona->type) {
241 case WM5102:
Charles Keepaxa35ff282014-03-18 10:49:17 +0000242 case WM8997:
Mark Brown73ee2942012-11-27 18:48:33 +0000243 desc = &arizona_ldo1_hc;
Mark Brown55a18ae2012-11-27 17:51:46 +0000244 ldo1->init_data = arizona_ldo1_dvfs;
245 break;
246 default:
Mark Brown73ee2942012-11-27 18:48:33 +0000247 desc = &arizona_ldo1;
Mark Brown55a18ae2012-11-27 17:51:46 +0000248 ldo1->init_data = arizona_ldo1_default;
249 break;
250 }
251
Mark Brown1910efa2012-06-23 13:07:57 +0100252 ldo1->init_data.consumer_supplies = &ldo1->supply;
253 ldo1->supply.supply = "DCVDD";
254 ldo1->supply.dev_name = dev_name(arizona->dev);
255
256 config.dev = arizona->dev;
257 config.driver_data = ldo1;
258 config.regmap = arizona->regmap;
Charles Keepax4a8c4752014-04-16 10:01:38 +0100259
260 if (IS_ENABLED(CONFIG_OF)) {
261 if (!dev_get_platdata(arizona->dev)) {
Javier Martinez Canillas072e78b2014-11-10 14:43:53 +0100262 ret = arizona_ldo1_of_get_pdata(arizona, &config, desc);
Charles Keepax4a8c4752014-04-16 10:01:38 +0100263 if (ret < 0)
264 return ret;
Markus Pargmann1de38212014-11-03 19:12:04 +0100265
266 config.ena_gpio_initialized = true;
Charles Keepax4a8c4752014-04-16 10:01:38 +0100267 }
268 }
269
Mark Browna9905b12012-06-27 14:32:06 +0100270 config.ena_gpio = arizona->pdata.ldoena;
Mark Brown1910efa2012-06-23 13:07:57 +0100271
272 if (arizona->pdata.ldo1)
273 config.init_data = arizona->pdata.ldo1;
274 else
275 config.init_data = &ldo1->init_data;
276
Charles Keepax4a8c4752014-04-16 10:01:38 +0100277 /*
278 * LDO1 can only be used to supply DCVDD so if it has no
279 * consumers then DCVDD is supplied externally.
280 */
281 if (config.init_data->num_consumer_supplies == 0)
282 arizona->external_dcvdd = true;
283
Mark Brown14ffa882013-08-31 11:56:47 +0100284 ldo1->regulator = devm_regulator_register(&pdev->dev, desc, &config);
Charles Keepaxec454d72015-02-20 16:08:43 +0000285
286 of_node_put(config.of_node);
287
Mark Brown1910efa2012-06-23 13:07:57 +0100288 if (IS_ERR(ldo1->regulator)) {
289 ret = PTR_ERR(ldo1->regulator);
290 dev_err(arizona->dev, "Failed to register LDO1 supply: %d\n",
291 ret);
292 return ret;
293 }
294
295 platform_set_drvdata(pdev, ldo1);
296
297 return 0;
298}
299
Mark Brown1910efa2012-06-23 13:07:57 +0100300static struct platform_driver arizona_ldo1_driver = {
301 .probe = arizona_ldo1_probe,
Mark Brown1910efa2012-06-23 13:07:57 +0100302 .driver = {
303 .name = "arizona-ldo1",
Mark Brown1910efa2012-06-23 13:07:57 +0100304 },
305};
306
307module_platform_driver(arizona_ldo1_driver);
308
309/* Module information */
310MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
311MODULE_DESCRIPTION("Arizona LDO1 driver");
312MODULE_LICENSE("GPL");
313MODULE_ALIAS("platform:arizona-ldo1");