1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Broadcom BCM590xx regulator driver
4 *
5 * Copyright 2014 Linaro Limited
6 * Author: Matt Porter <mporter@linaro.org>
7 */
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
21 /* I2C slave 0 registers */
22 #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
30 #define BCM590XX_RFLDOCTRL 0x96
31 #define BCM590XX_CSRVOUT1 0xc0
32
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)
50 #define BCM590XX_LDO_VSEL_MASK GENMASK(5, 3)
51 #define BCM590XX_SR_VSEL_MASK GENMASK(5, 0)
52
53 /*
54 * RFLDO to VSR regulators are
55 * accessed via I2C slave 0
56 */
57
58 /* 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
82 /*
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
96
97 #define BCM590XX_REG_IS_LDO(n) (n < BCM590XX_REG_CSR)
98 #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)
101
102 /* LDO group A: supported voltages in microvolts */
103 static 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 */
109 static const unsigned int ldo_c_table[] = {
110 3100000, 1800000, 2500000, 2700000, 2800000,
111 2900000, 3000000, 3300000,
112 };
113
114 static const unsigned int ldo_vbus[] = {
115 5000000,
116 };
117
118 /* DCDC group CSR: supported voltages in microvolts */
119 static const struct linear_range dcdc_csr_ranges[] = {
120 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 */
126 static const struct linear_range dcdc_iosr1_ranges[] = {
127 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 */
134 static const struct linear_range dcdc_sdsr1_ranges[] = {
135 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
140 struct 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;
146 const struct linear_range *linear_ranges;
147 };
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, \
159 .n_voltages = 64, \
160 .n_linear_ranges = ARRAY_SIZE(_ranges), \
161 .linear_ranges = _ranges, \
162 }
163
164 static 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),
185 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),
191 BCM590XX_REG_TABLE(vbus, ldo_vbus),
192 };
193
194 struct bcm590xx_reg {
195 struct regulator_desc *desc;
196 struct bcm590xx *mfd;
197 };
198
bcm590xx_get_vsel_register(int id)199 static int bcm590xx_get_vsel_register(int id)
200 {
201 if (BCM590XX_REG_IS_LDO(id))
202 return BCM590XX_RFLDOCTRL + id;
203 else if (BCM590XX_REG_IS_GPLDO(id))
204 return BCM590XX_GPLDO1CTRL + id;
205 else
206 return BCM590XX_CSRVOUT1 + (id - BCM590XX_REG_CSR) * 3;
207 }
208
bcm590xx_get_enable_register(int id)209 static 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;
215 else if (BCM590XX_REG_IS_GPLDO(id))
216 reg = BCM590XX_GPLDO1PMCTRL1 + id * 2;
217 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;
237 case BCM590XX_REG_VSR:
238 reg = BCM590XX_VSRPMCTRL1;
239 break;
240 case BCM590XX_REG_VBUS:
241 reg = BCM590XX_OTG_CTRL;
242 break;
243 }
244
245
246 return reg;
247 }
248
249 static const struct regulator_ops bcm590xx_ops_ldo = {
250 .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
259 static const struct regulator_ops bcm590xx_ops_dcdc = {
260 .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
269 static const struct regulator_ops bcm590xx_ops_vbus = {
270 .is_enabled = regulator_is_enabled_regmap,
271 .enable = regulator_enable_regmap,
272 .disable = regulator_disable_regmap,
273 };
274
bcm590xx_probe(struct platform_device * pdev)275 static int bcm590xx_probe(struct platform_device *pdev)
276 {
277 struct bcm590xx *bcm590xx = dev_get_drvdata(pdev->dev.parent);
278 struct bcm590xx_reg *pmu;
279 struct regulator_config config = { };
280 struct bcm590xx_info *info;
281 struct regulator_dev *rdev;
282 int i;
283
284 pmu = devm_kzalloc(&pdev->dev, sizeof(*pmu), GFP_KERNEL);
285 if (!pmu)
286 return -ENOMEM;
287
288 pmu->mfd = bcm590xx;
289
290 platform_set_drvdata(pdev, pmu);
291
292 pmu->desc = devm_kcalloc(&pdev->dev,
293 BCM590XX_NUM_REGS,
294 sizeof(struct regulator_desc),
295 GFP_KERNEL);
296 if (!pmu->desc)
297 return -ENOMEM;
298
299 info = bcm590xx_regs;
300
301 for (i = 0; i < BCM590XX_NUM_REGS; i++, info++) {
302 /* Register the regulators */
303 pmu->desc[i].name = info->name;
304 pmu->desc[i].of_match = of_match_ptr(info->name);
305 pmu->desc[i].regulators_node = of_match_ptr("regulators");
306 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
313 if ((BCM590XX_REG_IS_LDO(i)) || (BCM590XX_REG_IS_GPLDO(i))) {
314 pmu->desc[i].ops = &bcm590xx_ops_ldo;
315 pmu->desc[i].vsel_mask = BCM590XX_LDO_VSEL_MASK;
316 } else if (BCM590XX_REG_IS_VBUS(i))
317 pmu->desc[i].ops = &bcm590xx_ops_vbus;
318 else {
319 pmu->desc[i].ops = &bcm590xx_ops_dcdc;
320 pmu->desc[i].vsel_mask = BCM590XX_SR_VSEL_MASK;
321 }
322
323 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 }
330 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;
335 config.driver_data = pmu;
336 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;
340
341 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 }
349 }
350
351 return 0;
352 }
353
354 static struct platform_driver bcm590xx_regulator_driver = {
355 .driver = {
356 .name = "bcm590xx-vregs",
357 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
358 },
359 .probe = bcm590xx_probe,
360 };
361 module_platform_driver(bcm590xx_regulator_driver);
362
363 MODULE_AUTHOR("Matt Porter <mporter@linaro.org>");
364 MODULE_DESCRIPTION("BCM590xx voltage regulator driver");
365 MODULE_LICENSE("GPL v2");
366 MODULE_ALIAS("platform:bcm590xx-vregs");
367