blob: 65e23fc5f9c399fa15145c5534f57163decbeef4 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Matt Porter462c9fc2014-03-11 11:46:13 -04002/*
3 * Broadcom BCM590xx regulator driver
4 *
5 * Copyright 2014 Linaro Limited
6 * Author: Matt Porter <mporter@linaro.org>
Matt Porter462c9fc2014-03-11 11:46:13 -04007 */
8
9#include <linux/err.h>
10#include <linux/init.h>
11#include <linux/kernel.h>
12#include <linux/mfd/bcm590xx.h>
13#include <linux/module.h>
14#include <linux/of.h>
15#include <linux/platform_device.h>
16#include <linux/regulator/driver.h>
17#include <linux/regulator/machine.h>
18#include <linux/regulator/of_regulator.h>
19#include <linux/slab.h>
20
Matt Porterc6466952014-04-23 19:21:32 -040021/* I2C slave 0 registers */
Matt Porter462c9fc2014-03-11 11:46:13 -040022#define BCM590XX_RFLDOPMCTRL1 0x60
23#define BCM590XX_IOSR1PMCTRL1 0x7a
24#define BCM590XX_IOSR2PMCTRL1 0x7c
25#define BCM590XX_CSRPMCTRL1 0x7e
26#define BCM590XX_SDSR1PMCTRL1 0x82
27#define BCM590XX_SDSR2PMCTRL1 0x86
28#define BCM590XX_MSRPMCTRL1 0x8a
29#define BCM590XX_VSRPMCTRL1 0x8e
Matt Porter462c9fc2014-03-11 11:46:13 -040030#define BCM590XX_RFLDOCTRL 0x96
31#define BCM590XX_CSRVOUT1 0xc0
Matt Porterc6466952014-04-23 19:21:32 -040032
33/* I2C slave 1 registers */
34#define BCM590XX_GPLDO5PMCTRL1 0x16
35#define BCM590XX_GPLDO6PMCTRL1 0x18
36#define BCM590XX_GPLDO1CTRL 0x1a
37#define BCM590XX_GPLDO2CTRL 0x1b
38#define BCM590XX_GPLDO3CTRL 0x1c
39#define BCM590XX_GPLDO4CTRL 0x1d
40#define BCM590XX_GPLDO5CTRL 0x1e
41#define BCM590XX_GPLDO6CTRL 0x1f
42#define BCM590XX_OTG_CTRL 0x40
43#define BCM590XX_GPLDO1PMCTRL1 0x57
44#define BCM590XX_GPLDO2PMCTRL1 0x59
45#define BCM590XX_GPLDO3PMCTRL1 0x5b
46#define BCM590XX_GPLDO4PMCTRL1 0x5d
47
48#define BCM590XX_REG_ENABLE BIT(7)
49#define BCM590XX_VBUS_ENABLE BIT(2)
Matt Porter462c9fc2014-03-11 11:46:13 -040050#define BCM590XX_LDO_VSEL_MASK GENMASK(5, 3)
51#define BCM590XX_SR_VSEL_MASK GENMASK(5, 0)
52
Matt Porterc6466952014-04-23 19:21:32 -040053/*
54 * RFLDO to VSR regulators are
55 * accessed via I2C slave 0
56 */
57
Matt Porter462c9fc2014-03-11 11:46:13 -040058/* LDO regulator IDs */
59#define BCM590XX_REG_RFLDO 0
60#define BCM590XX_REG_CAMLDO1 1
61#define BCM590XX_REG_CAMLDO2 2
62#define BCM590XX_REG_SIMLDO1 3
63#define BCM590XX_REG_SIMLDO2 4
64#define BCM590XX_REG_SDLDO 5
65#define BCM590XX_REG_SDXLDO 6
66#define BCM590XX_REG_MMCLDO1 7
67#define BCM590XX_REG_MMCLDO2 8
68#define BCM590XX_REG_AUDLDO 9
69#define BCM590XX_REG_MICLDO 10
70#define BCM590XX_REG_USBLDO 11
71#define BCM590XX_REG_VIBLDO 12
72
73/* DCDC regulator IDs */
74#define BCM590XX_REG_CSR 13
75#define BCM590XX_REG_IOSR1 14
76#define BCM590XX_REG_IOSR2 15
77#define BCM590XX_REG_MSR 16
78#define BCM590XX_REG_SDSR1 17
79#define BCM590XX_REG_SDSR2 18
80#define BCM590XX_REG_VSR 19
81
Matt Porterc6466952014-04-23 19:21:32 -040082/*
83 * GPLDO1 to VBUS regulators are
84 * accessed via I2C slave 1
85 */
86
87#define BCM590XX_REG_GPLDO1 20
88#define BCM590XX_REG_GPLDO2 21
89#define BCM590XX_REG_GPLDO3 22
90#define BCM590XX_REG_GPLDO4 23
91#define BCM590XX_REG_GPLDO5 24
92#define BCM590XX_REG_GPLDO6 25
93#define BCM590XX_REG_VBUS 26
94
95#define BCM590XX_NUM_REGS 27
Matt Porter462c9fc2014-03-11 11:46:13 -040096
97#define BCM590XX_REG_IS_LDO(n) (n < BCM590XX_REG_CSR)
Matt Porterc6466952014-04-23 19:21:32 -040098#define BCM590XX_REG_IS_GPLDO(n) \
99 ((n > BCM590XX_REG_VSR) && (n < BCM590XX_REG_VBUS))
100#define BCM590XX_REG_IS_VBUS(n) (n == BCM590XX_REG_VBUS)
Matt Porter462c9fc2014-03-11 11:46:13 -0400101
Matt Porter462c9fc2014-03-11 11:46:13 -0400102/* LDO group A: supported voltages in microvolts */
103static const unsigned int ldo_a_table[] = {
104 1200000, 1800000, 2500000, 2700000, 2800000,
105 2900000, 3000000, 3300000,
106};
107
108/* LDO group C: supported voltages in microvolts */
109static const unsigned int ldo_c_table[] = {
110 3100000, 1800000, 2500000, 2700000, 2800000,
111 2900000, 3000000, 3300000,
112};
113
Graham Williams636f4a32014-06-18 12:42:10 -0700114static const unsigned int ldo_vbus[] = {
115 5000000,
116};
117
Matt Porter462c9fc2014-03-11 11:46:13 -0400118/* DCDC group CSR: supported voltages in microvolts */
Matti Vaittinen60ab7f42020-05-08 18:43:36 +0300119static const struct linear_range dcdc_csr_ranges[] = {
Matt Porter462c9fc2014-03-11 11:46:13 -0400120 REGULATOR_LINEAR_RANGE(860000, 2, 50, 10000),
121 REGULATOR_LINEAR_RANGE(1360000, 51, 55, 20000),
122 REGULATOR_LINEAR_RANGE(900000, 56, 63, 0),
123};
124
125/* DCDC group IOSR1: supported voltages in microvolts */
Matti Vaittinen60ab7f42020-05-08 18:43:36 +0300126static const struct linear_range dcdc_iosr1_ranges[] = {
Matt Porter462c9fc2014-03-11 11:46:13 -0400127 REGULATOR_LINEAR_RANGE(860000, 2, 51, 10000),
128 REGULATOR_LINEAR_RANGE(1500000, 52, 52, 0),
129 REGULATOR_LINEAR_RANGE(1800000, 53, 53, 0),
130 REGULATOR_LINEAR_RANGE(900000, 54, 63, 0),
131};
132
133/* DCDC group SDSR1: supported voltages in microvolts */
Matti Vaittinen60ab7f42020-05-08 18:43:36 +0300134static const struct linear_range dcdc_sdsr1_ranges[] = {
Matt Porter462c9fc2014-03-11 11:46:13 -0400135 REGULATOR_LINEAR_RANGE(860000, 2, 50, 10000),
136 REGULATOR_LINEAR_RANGE(1340000, 51, 51, 0),
137 REGULATOR_LINEAR_RANGE(900000, 52, 63, 0),
138};
139
140struct bcm590xx_info {
141 const char *name;
142 const char *vin_name;
143 u8 n_voltages;
144 const unsigned int *volt_table;
145 u8 n_linear_ranges;
Matti Vaittinen60ab7f42020-05-08 18:43:36 +0300146 const struct linear_range *linear_ranges;
Matt Porter462c9fc2014-03-11 11:46:13 -0400147};
148
149#define BCM590XX_REG_TABLE(_name, _table) \
150 { \
151 .name = #_name, \
152 .n_voltages = ARRAY_SIZE(_table), \
153 .volt_table = _table, \
154 }
155
156#define BCM590XX_REG_RANGES(_name, _ranges) \
157 { \
158 .name = #_name, \
Tim Kryger1e791402014-04-10 13:04:00 -0700159 .n_voltages = 64, \
Matt Porter462c9fc2014-03-11 11:46:13 -0400160 .n_linear_ranges = ARRAY_SIZE(_ranges), \
161 .linear_ranges = _ranges, \
162 }
163
164static struct bcm590xx_info bcm590xx_regs[] = {
165 BCM590XX_REG_TABLE(rfldo, ldo_a_table),
166 BCM590XX_REG_TABLE(camldo1, ldo_c_table),
167 BCM590XX_REG_TABLE(camldo2, ldo_c_table),
168 BCM590XX_REG_TABLE(simldo1, ldo_a_table),
169 BCM590XX_REG_TABLE(simldo2, ldo_a_table),
170 BCM590XX_REG_TABLE(sdldo, ldo_c_table),
171 BCM590XX_REG_TABLE(sdxldo, ldo_a_table),
172 BCM590XX_REG_TABLE(mmcldo1, ldo_a_table),
173 BCM590XX_REG_TABLE(mmcldo2, ldo_a_table),
174 BCM590XX_REG_TABLE(audldo, ldo_a_table),
175 BCM590XX_REG_TABLE(micldo, ldo_a_table),
176 BCM590XX_REG_TABLE(usbldo, ldo_a_table),
177 BCM590XX_REG_TABLE(vibldo, ldo_c_table),
178 BCM590XX_REG_RANGES(csr, dcdc_csr_ranges),
179 BCM590XX_REG_RANGES(iosr1, dcdc_iosr1_ranges),
180 BCM590XX_REG_RANGES(iosr2, dcdc_iosr1_ranges),
181 BCM590XX_REG_RANGES(msr, dcdc_iosr1_ranges),
182 BCM590XX_REG_RANGES(sdsr1, dcdc_sdsr1_ranges),
183 BCM590XX_REG_RANGES(sdsr2, dcdc_iosr1_ranges),
184 BCM590XX_REG_RANGES(vsr, dcdc_iosr1_ranges),
Matt Porterc6466952014-04-23 19:21:32 -0400185 BCM590XX_REG_TABLE(gpldo1, ldo_a_table),
186 BCM590XX_REG_TABLE(gpldo2, ldo_a_table),
187 BCM590XX_REG_TABLE(gpldo3, ldo_a_table),
188 BCM590XX_REG_TABLE(gpldo4, ldo_a_table),
189 BCM590XX_REG_TABLE(gpldo5, ldo_a_table),
190 BCM590XX_REG_TABLE(gpldo6, ldo_a_table),
Graham Williams636f4a32014-06-18 12:42:10 -0700191 BCM590XX_REG_TABLE(vbus, ldo_vbus),
Matt Porter462c9fc2014-03-11 11:46:13 -0400192};
193
194struct bcm590xx_reg {
195 struct regulator_desc *desc;
196 struct bcm590xx *mfd;
Matt Porter462c9fc2014-03-11 11:46:13 -0400197};
198
199static int bcm590xx_get_vsel_register(int id)
200{
201 if (BCM590XX_REG_IS_LDO(id))
202 return BCM590XX_RFLDOCTRL + id;
Matt Porterc6466952014-04-23 19:21:32 -0400203 else if (BCM590XX_REG_IS_GPLDO(id))
204 return BCM590XX_GPLDO1CTRL + id;
Matt Porter462c9fc2014-03-11 11:46:13 -0400205 else
206 return BCM590XX_CSRVOUT1 + (id - BCM590XX_REG_CSR) * 3;
207}
208
209static int bcm590xx_get_enable_register(int id)
210{
211 int reg = 0;
212
213 if (BCM590XX_REG_IS_LDO(id))
214 reg = BCM590XX_RFLDOPMCTRL1 + id * 2;
Matt Porterc6466952014-04-23 19:21:32 -0400215 else if (BCM590XX_REG_IS_GPLDO(id))
216 reg = BCM590XX_GPLDO1PMCTRL1 + id * 2;
Matt Porter462c9fc2014-03-11 11:46:13 -0400217 else
218 switch (id) {
219 case BCM590XX_REG_CSR:
220 reg = BCM590XX_CSRPMCTRL1;
221 break;
222 case BCM590XX_REG_IOSR1:
223 reg = BCM590XX_IOSR1PMCTRL1;
224 break;
225 case BCM590XX_REG_IOSR2:
226 reg = BCM590XX_IOSR2PMCTRL1;
227 break;
228 case BCM590XX_REG_MSR:
229 reg = BCM590XX_MSRPMCTRL1;
230 break;
231 case BCM590XX_REG_SDSR1:
232 reg = BCM590XX_SDSR1PMCTRL1;
233 break;
234 case BCM590XX_REG_SDSR2:
235 reg = BCM590XX_SDSR2PMCTRL1;
236 break;
Axel Lind6afa2b2019-01-05 11:28:17 +0800237 case BCM590XX_REG_VSR:
238 reg = BCM590XX_VSRPMCTRL1;
239 break;
Matt Porterc6466952014-04-23 19:21:32 -0400240 case BCM590XX_REG_VBUS:
241 reg = BCM590XX_OTG_CTRL;
Axel Lind6afa2b2019-01-05 11:28:17 +0800242 break;
Javier Martinez Canillasf3f400e2015-09-11 04:26:56 +0200243 }
Matt Porter462c9fc2014-03-11 11:46:13 -0400244
Matt Porterc6466952014-04-23 19:21:32 -0400245
Matt Porter462c9fc2014-03-11 11:46:13 -0400246 return reg;
247}
248
Bhumika Goyal20f860c2017-01-28 19:30:24 +0530249static const struct regulator_ops bcm590xx_ops_ldo = {
Matt Porter462c9fc2014-03-11 11:46:13 -0400250 .is_enabled = regulator_is_enabled_regmap,
251 .enable = regulator_enable_regmap,
252 .disable = regulator_disable_regmap,
253 .get_voltage_sel = regulator_get_voltage_sel_regmap,
254 .set_voltage_sel = regulator_set_voltage_sel_regmap,
255 .list_voltage = regulator_list_voltage_table,
256 .map_voltage = regulator_map_voltage_iterate,
257};
258
Bhumika Goyal20f860c2017-01-28 19:30:24 +0530259static const struct regulator_ops bcm590xx_ops_dcdc = {
Matt Porter462c9fc2014-03-11 11:46:13 -0400260 .is_enabled = regulator_is_enabled_regmap,
261 .enable = regulator_enable_regmap,
262 .disable = regulator_disable_regmap,
263 .get_voltage_sel = regulator_get_voltage_sel_regmap,
264 .set_voltage_sel = regulator_set_voltage_sel_regmap,
265 .list_voltage = regulator_list_voltage_linear_range,
266 .map_voltage = regulator_map_voltage_linear_range,
267};
268
Bhumika Goyal20f860c2017-01-28 19:30:24 +0530269static const struct regulator_ops bcm590xx_ops_vbus = {
Matt Porterc6466952014-04-23 19:21:32 -0400270 .is_enabled = regulator_is_enabled_regmap,
271 .enable = regulator_enable_regmap,
272 .disable = regulator_disable_regmap,
273};
274
Matt Porter462c9fc2014-03-11 11:46:13 -0400275static int bcm590xx_probe(struct platform_device *pdev)
276{
277 struct bcm590xx *bcm590xx = dev_get_drvdata(pdev->dev.parent);
Matt Porter462c9fc2014-03-11 11:46:13 -0400278 struct bcm590xx_reg *pmu;
279 struct regulator_config config = { };
280 struct bcm590xx_info *info;
Matt Porter462c9fc2014-03-11 11:46:13 -0400281 struct regulator_dev *rdev;
Matt Porter462c9fc2014-03-11 11:46:13 -0400282 int i;
283
Matt Porter462c9fc2014-03-11 11:46:13 -0400284 pmu = devm_kzalloc(&pdev->dev, sizeof(*pmu), GFP_KERNEL);
Jingoo Hanc969faa2014-06-02 15:27:21 +0900285 if (!pmu)
Matt Porter462c9fc2014-03-11 11:46:13 -0400286 return -ENOMEM;
Matt Porter462c9fc2014-03-11 11:46:13 -0400287
288 pmu->mfd = bcm590xx;
289
290 platform_set_drvdata(pdev, pmu);
291
Kees Cooka86854d2018-06-12 14:07:58 -0700292 pmu->desc = devm_kcalloc(&pdev->dev,
293 BCM590XX_NUM_REGS,
294 sizeof(struct regulator_desc),
295 GFP_KERNEL);
Jingoo Hanc969faa2014-06-02 15:27:21 +0900296 if (!pmu->desc)
Matt Porter462c9fc2014-03-11 11:46:13 -0400297 return -ENOMEM;
Matt Porter462c9fc2014-03-11 11:46:13 -0400298
Matt Porter462c9fc2014-03-11 11:46:13 -0400299 info = bcm590xx_regs;
300
301 for (i = 0; i < BCM590XX_NUM_REGS; i++, info++) {
Matt Porter462c9fc2014-03-11 11:46:13 -0400302 /* Register the regulators */
Matt Porter462c9fc2014-03-11 11:46:13 -0400303 pmu->desc[i].name = info->name;
Axel Lina4e73622019-04-01 22:42:36 +0800304 pmu->desc[i].of_match = of_match_ptr(info->name);
305 pmu->desc[i].regulators_node = of_match_ptr("regulators");
Matt Porter462c9fc2014-03-11 11:46:13 -0400306 pmu->desc[i].supply_name = info->vin_name;
307 pmu->desc[i].id = i;
308 pmu->desc[i].volt_table = info->volt_table;
309 pmu->desc[i].n_voltages = info->n_voltages;
310 pmu->desc[i].linear_ranges = info->linear_ranges;
311 pmu->desc[i].n_linear_ranges = info->n_linear_ranges;
312
Matt Porterc6466952014-04-23 19:21:32 -0400313 if ((BCM590XX_REG_IS_LDO(i)) || (BCM590XX_REG_IS_GPLDO(i))) {
Matt Porter462c9fc2014-03-11 11:46:13 -0400314 pmu->desc[i].ops = &bcm590xx_ops_ldo;
315 pmu->desc[i].vsel_mask = BCM590XX_LDO_VSEL_MASK;
Matt Porterc6466952014-04-23 19:21:32 -0400316 } else if (BCM590XX_REG_IS_VBUS(i))
317 pmu->desc[i].ops = &bcm590xx_ops_vbus;
318 else {
Matt Porter462c9fc2014-03-11 11:46:13 -0400319 pmu->desc[i].ops = &bcm590xx_ops_dcdc;
320 pmu->desc[i].vsel_mask = BCM590XX_SR_VSEL_MASK;
321 }
322
Matt Porterc6466952014-04-23 19:21:32 -0400323 if (BCM590XX_REG_IS_VBUS(i))
324 pmu->desc[i].enable_mask = BCM590XX_VBUS_ENABLE;
325 else {
326 pmu->desc[i].vsel_reg = bcm590xx_get_vsel_register(i);
327 pmu->desc[i].enable_is_inverted = true;
328 pmu->desc[i].enable_mask = BCM590XX_REG_ENABLE;
329 }
Matt Porter462c9fc2014-03-11 11:46:13 -0400330 pmu->desc[i].enable_reg = bcm590xx_get_enable_register(i);
331 pmu->desc[i].type = REGULATOR_VOLTAGE;
332 pmu->desc[i].owner = THIS_MODULE;
333
334 config.dev = bcm590xx->dev;
Matt Porter462c9fc2014-03-11 11:46:13 -0400335 config.driver_data = pmu;
Matt Porterc6466952014-04-23 19:21:32 -0400336 if (BCM590XX_REG_IS_GPLDO(i) || BCM590XX_REG_IS_VBUS(i))
337 config.regmap = bcm590xx->regmap_sec;
338 else
339 config.regmap = bcm590xx->regmap_pri;
Matt Porter462c9fc2014-03-11 11:46:13 -0400340
Matt Porter462c9fc2014-03-11 11:46:13 -0400341 rdev = devm_regulator_register(&pdev->dev, &pmu->desc[i],
342 &config);
343 if (IS_ERR(rdev)) {
344 dev_err(bcm590xx->dev,
345 "failed to register %s regulator\n",
346 pdev->name);
347 return PTR_ERR(rdev);
348 }
Matt Porter462c9fc2014-03-11 11:46:13 -0400349 }
350
351 return 0;
352}
353
354static struct platform_driver bcm590xx_regulator_driver = {
355 .driver = {
356 .name = "bcm590xx-vregs",
Matt Porter462c9fc2014-03-11 11:46:13 -0400357 },
358 .probe = bcm590xx_probe,
359};
360module_platform_driver(bcm590xx_regulator_driver);
361
362MODULE_AUTHOR("Matt Porter <mporter@linaro.org>");
363MODULE_DESCRIPTION("BCM590xx voltage regulator driver");
364MODULE_LICENSE("GPL v2");
Axel Linc9347432014-03-14 09:57:54 +0800365MODULE_ALIAS("platform:bcm590xx-vregs");