2 * Broadcom BCM590xx regulator driver
4 * Copyright 2014 Linaro Limited
5 * Author: Matt Porter <mporter@linaro.org>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
13 #include <linux/err.h>
14 #include <linux/init.h>
15 #include <linux/kernel.h>
16 #include <linux/mfd/bcm590xx.h>
17 #include <linux/module.h>
19 #include <linux/platform_device.h>
20 #include <linux/regulator/driver.h>
21 #include <linux/regulator/machine.h>
22 #include <linux/regulator/of_regulator.h>
23 #include <linux/slab.h>
26 #define BCM590XX_RFLDOPMCTRL1 0x60
27 #define BCM590XX_IOSR1PMCTRL1 0x7a
28 #define BCM590XX_IOSR2PMCTRL1 0x7c
29 #define BCM590XX_CSRPMCTRL1 0x7e
30 #define BCM590XX_SDSR1PMCTRL1 0x82
31 #define BCM590XX_SDSR2PMCTRL1 0x86
32 #define BCM590XX_MSRPMCTRL1 0x8a
33 #define BCM590XX_VSRPMCTRL1 0x8e
34 #define BCM590XX_REG_ENABLE BIT(7)
36 #define BCM590XX_RFLDOCTRL 0x96
37 #define BCM590XX_CSRVOUT1 0xc0
38 #define BCM590XX_LDO_VSEL_MASK GENMASK(5, 3)
39 #define BCM590XX_SR_VSEL_MASK GENMASK(5, 0)
41 /* LDO regulator IDs */
42 #define BCM590XX_REG_RFLDO 0
43 #define BCM590XX_REG_CAMLDO1 1
44 #define BCM590XX_REG_CAMLDO2 2
45 #define BCM590XX_REG_SIMLDO1 3
46 #define BCM590XX_REG_SIMLDO2 4
47 #define BCM590XX_REG_SDLDO 5
48 #define BCM590XX_REG_SDXLDO 6
49 #define BCM590XX_REG_MMCLDO1 7
50 #define BCM590XX_REG_MMCLDO2 8
51 #define BCM590XX_REG_AUDLDO 9
52 #define BCM590XX_REG_MICLDO 10
53 #define BCM590XX_REG_USBLDO 11
54 #define BCM590XX_REG_VIBLDO 12
56 /* DCDC regulator IDs */
57 #define BCM590XX_REG_CSR 13
58 #define BCM590XX_REG_IOSR1 14
59 #define BCM590XX_REG_IOSR2 15
60 #define BCM590XX_REG_MSR 16
61 #define BCM590XX_REG_SDSR1 17
62 #define BCM590XX_REG_SDSR2 18
63 #define BCM590XX_REG_VSR 19
65 #define BCM590XX_NUM_REGS 20
67 #define BCM590XX_REG_IS_LDO(n) (n < BCM590XX_REG_CSR)
69 struct bcm590xx_board
{
70 struct regulator_init_data
*bcm590xx_pmu_init_data
[BCM590XX_NUM_REGS
];
73 /* LDO group A: supported voltages in microvolts */
74 static const unsigned int ldo_a_table
[] = {
75 1200000, 1800000, 2500000, 2700000, 2800000,
76 2900000, 3000000, 3300000,
79 /* LDO group C: supported voltages in microvolts */
80 static const unsigned int ldo_c_table
[] = {
81 3100000, 1800000, 2500000, 2700000, 2800000,
82 2900000, 3000000, 3300000,
85 /* DCDC group CSR: supported voltages in microvolts */
86 static const struct regulator_linear_range dcdc_csr_ranges
[] = {
87 REGULATOR_LINEAR_RANGE(860000, 2, 50, 10000),
88 REGULATOR_LINEAR_RANGE(1360000, 51, 55, 20000),
89 REGULATOR_LINEAR_RANGE(900000, 56, 63, 0),
92 /* DCDC group IOSR1: supported voltages in microvolts */
93 static const struct regulator_linear_range dcdc_iosr1_ranges
[] = {
94 REGULATOR_LINEAR_RANGE(860000, 2, 51, 10000),
95 REGULATOR_LINEAR_RANGE(1500000, 52, 52, 0),
96 REGULATOR_LINEAR_RANGE(1800000, 53, 53, 0),
97 REGULATOR_LINEAR_RANGE(900000, 54, 63, 0),
100 /* DCDC group SDSR1: supported voltages in microvolts */
101 static const struct regulator_linear_range dcdc_sdsr1_ranges
[] = {
102 REGULATOR_LINEAR_RANGE(860000, 2, 50, 10000),
103 REGULATOR_LINEAR_RANGE(1340000, 51, 51, 0),
104 REGULATOR_LINEAR_RANGE(900000, 52, 63, 0),
107 struct bcm590xx_info
{
109 const char *vin_name
;
111 const unsigned int *volt_table
;
113 const struct regulator_linear_range
*linear_ranges
;
116 #define BCM590XX_REG_TABLE(_name, _table) \
119 .n_voltages = ARRAY_SIZE(_table), \
120 .volt_table = _table, \
123 #define BCM590XX_REG_RANGES(_name, _ranges) \
127 .n_linear_ranges = ARRAY_SIZE(_ranges), \
128 .linear_ranges = _ranges, \
131 static struct bcm590xx_info bcm590xx_regs
[] = {
132 BCM590XX_REG_TABLE(rfldo
, ldo_a_table
),
133 BCM590XX_REG_TABLE(camldo1
, ldo_c_table
),
134 BCM590XX_REG_TABLE(camldo2
, ldo_c_table
),
135 BCM590XX_REG_TABLE(simldo1
, ldo_a_table
),
136 BCM590XX_REG_TABLE(simldo2
, ldo_a_table
),
137 BCM590XX_REG_TABLE(sdldo
, ldo_c_table
),
138 BCM590XX_REG_TABLE(sdxldo
, ldo_a_table
),
139 BCM590XX_REG_TABLE(mmcldo1
, ldo_a_table
),
140 BCM590XX_REG_TABLE(mmcldo2
, ldo_a_table
),
141 BCM590XX_REG_TABLE(audldo
, ldo_a_table
),
142 BCM590XX_REG_TABLE(micldo
, ldo_a_table
),
143 BCM590XX_REG_TABLE(usbldo
, ldo_a_table
),
144 BCM590XX_REG_TABLE(vibldo
, ldo_c_table
),
145 BCM590XX_REG_RANGES(csr
, dcdc_csr_ranges
),
146 BCM590XX_REG_RANGES(iosr1
, dcdc_iosr1_ranges
),
147 BCM590XX_REG_RANGES(iosr2
, dcdc_iosr1_ranges
),
148 BCM590XX_REG_RANGES(msr
, dcdc_iosr1_ranges
),
149 BCM590XX_REG_RANGES(sdsr1
, dcdc_sdsr1_ranges
),
150 BCM590XX_REG_RANGES(sdsr2
, dcdc_iosr1_ranges
),
151 BCM590XX_REG_RANGES(vsr
, dcdc_iosr1_ranges
),
154 struct bcm590xx_reg
{
155 struct regulator_desc
*desc
;
156 struct bcm590xx
*mfd
;
157 struct bcm590xx_info
**info
;
160 static int bcm590xx_get_vsel_register(int id
)
162 if (BCM590XX_REG_IS_LDO(id
))
163 return BCM590XX_RFLDOCTRL
+ id
;
165 return BCM590XX_CSRVOUT1
+ (id
- BCM590XX_REG_CSR
) * 3;
168 static int bcm590xx_get_enable_register(int id
)
172 if (BCM590XX_REG_IS_LDO(id
))
173 reg
= BCM590XX_RFLDOPMCTRL1
+ id
* 2;
176 case BCM590XX_REG_CSR
:
177 reg
= BCM590XX_CSRPMCTRL1
;
179 case BCM590XX_REG_IOSR1
:
180 reg
= BCM590XX_IOSR1PMCTRL1
;
182 case BCM590XX_REG_IOSR2
:
183 reg
= BCM590XX_IOSR2PMCTRL1
;
185 case BCM590XX_REG_MSR
:
186 reg
= BCM590XX_MSRPMCTRL1
;
188 case BCM590XX_REG_SDSR1
:
189 reg
= BCM590XX_SDSR1PMCTRL1
;
191 case BCM590XX_REG_SDSR2
:
192 reg
= BCM590XX_SDSR2PMCTRL1
;
199 static struct regulator_ops bcm590xx_ops_ldo
= {
200 .is_enabled
= regulator_is_enabled_regmap
,
201 .enable
= regulator_enable_regmap
,
202 .disable
= regulator_disable_regmap
,
203 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
204 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
205 .list_voltage
= regulator_list_voltage_table
,
206 .map_voltage
= regulator_map_voltage_iterate
,
209 static struct regulator_ops bcm590xx_ops_dcdc
= {
210 .is_enabled
= regulator_is_enabled_regmap
,
211 .enable
= regulator_enable_regmap
,
212 .disable
= regulator_disable_regmap
,
213 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
214 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
215 .list_voltage
= regulator_list_voltage_linear_range
,
216 .map_voltage
= regulator_map_voltage_linear_range
,
219 #define BCM590XX_MATCH(_name, _id) \
222 .driver_data = (void *)&bcm590xx_regs[BCM590XX_REG_##_id], \
225 static struct of_regulator_match bcm590xx_matches
[] = {
226 BCM590XX_MATCH(rfldo
, RFLDO
),
227 BCM590XX_MATCH(camldo1
, CAMLDO1
),
228 BCM590XX_MATCH(camldo2
, CAMLDO2
),
229 BCM590XX_MATCH(simldo1
, SIMLDO1
),
230 BCM590XX_MATCH(simldo2
, SIMLDO2
),
231 BCM590XX_MATCH(sdldo
, SDLDO
),
232 BCM590XX_MATCH(sdxldo
, SDXLDO
),
233 BCM590XX_MATCH(mmcldo1
, MMCLDO1
),
234 BCM590XX_MATCH(mmcldo2
, MMCLDO2
),
235 BCM590XX_MATCH(audldo
, AUDLDO
),
236 BCM590XX_MATCH(micldo
, MICLDO
),
237 BCM590XX_MATCH(usbldo
, USBLDO
),
238 BCM590XX_MATCH(vibldo
, VIBLDO
),
239 BCM590XX_MATCH(csr
, CSR
),
240 BCM590XX_MATCH(iosr1
, IOSR1
),
241 BCM590XX_MATCH(iosr2
, IOSR2
),
242 BCM590XX_MATCH(msr
, MSR
),
243 BCM590XX_MATCH(sdsr1
, SDSR1
),
244 BCM590XX_MATCH(sdsr2
, SDSR2
),
245 BCM590XX_MATCH(vsr
, VSR
),
248 static struct bcm590xx_board
*bcm590xx_parse_dt_reg_data(
249 struct platform_device
*pdev
,
250 struct of_regulator_match
**bcm590xx_reg_matches
)
252 struct bcm590xx_board
*data
;
253 struct device_node
*np
= pdev
->dev
.parent
->of_node
;
254 struct device_node
*regulators
;
255 struct of_regulator_match
*matches
= bcm590xx_matches
;
256 int count
= ARRAY_SIZE(bcm590xx_matches
);
261 dev_err(&pdev
->dev
, "of node not found\n");
265 data
= devm_kzalloc(&pdev
->dev
, sizeof(*data
), GFP_KERNEL
);
267 dev_err(&pdev
->dev
, "failed to allocate regulator board data\n");
271 np
= of_node_get(np
);
272 regulators
= of_get_child_by_name(np
, "regulators");
274 dev_warn(&pdev
->dev
, "regulator node not found\n");
278 ret
= of_regulator_match(&pdev
->dev
, regulators
, matches
, count
);
279 of_node_put(regulators
);
281 dev_err(&pdev
->dev
, "Error parsing regulator init data: %d\n",
286 *bcm590xx_reg_matches
= matches
;
288 for (idx
= 0; idx
< count
; idx
++) {
289 if (!matches
[idx
].init_data
|| !matches
[idx
].of_node
)
292 data
->bcm590xx_pmu_init_data
[idx
] = matches
[idx
].init_data
;
298 static int bcm590xx_probe(struct platform_device
*pdev
)
300 struct bcm590xx
*bcm590xx
= dev_get_drvdata(pdev
->dev
.parent
);
301 struct bcm590xx_board
*pmu_data
= NULL
;
302 struct bcm590xx_reg
*pmu
;
303 struct regulator_config config
= { };
304 struct bcm590xx_info
*info
;
305 struct regulator_init_data
*reg_data
;
306 struct regulator_dev
*rdev
;
307 struct of_regulator_match
*bcm590xx_reg_matches
= NULL
;
310 pmu_data
= bcm590xx_parse_dt_reg_data(pdev
,
311 &bcm590xx_reg_matches
);
313 pmu
= devm_kzalloc(&pdev
->dev
, sizeof(*pmu
), GFP_KERNEL
);
315 dev_err(&pdev
->dev
, "Memory allocation failed for pmu\n");
321 platform_set_drvdata(pdev
, pmu
);
323 pmu
->desc
= devm_kzalloc(&pdev
->dev
, BCM590XX_NUM_REGS
*
324 sizeof(struct regulator_desc
), GFP_KERNEL
);
326 dev_err(&pdev
->dev
, "Memory alloc fails for desc\n");
330 pmu
->info
= devm_kzalloc(&pdev
->dev
, BCM590XX_NUM_REGS
*
331 sizeof(struct bcm590xx_info
*), GFP_KERNEL
);
333 dev_err(&pdev
->dev
, "Memory alloc fails for info\n");
337 info
= bcm590xx_regs
;
339 for (i
= 0; i
< BCM590XX_NUM_REGS
; i
++, info
++) {
341 reg_data
= pmu_data
->bcm590xx_pmu_init_data
[i
];
345 /* Register the regulators */
348 pmu
->desc
[i
].name
= info
->name
;
349 pmu
->desc
[i
].supply_name
= info
->vin_name
;
351 pmu
->desc
[i
].volt_table
= info
->volt_table
;
352 pmu
->desc
[i
].n_voltages
= info
->n_voltages
;
353 pmu
->desc
[i
].linear_ranges
= info
->linear_ranges
;
354 pmu
->desc
[i
].n_linear_ranges
= info
->n_linear_ranges
;
356 if (BCM590XX_REG_IS_LDO(i
)) {
357 pmu
->desc
[i
].ops
= &bcm590xx_ops_ldo
;
358 pmu
->desc
[i
].vsel_mask
= BCM590XX_LDO_VSEL_MASK
;
360 pmu
->desc
[i
].ops
= &bcm590xx_ops_dcdc
;
361 pmu
->desc
[i
].vsel_mask
= BCM590XX_SR_VSEL_MASK
;
364 pmu
->desc
[i
].vsel_reg
= bcm590xx_get_vsel_register(i
);
365 pmu
->desc
[i
].enable_is_inverted
= true;
366 pmu
->desc
[i
].enable_mask
= BCM590XX_REG_ENABLE
;
367 pmu
->desc
[i
].enable_reg
= bcm590xx_get_enable_register(i
);
368 pmu
->desc
[i
].type
= REGULATOR_VOLTAGE
;
369 pmu
->desc
[i
].owner
= THIS_MODULE
;
371 config
.dev
= bcm590xx
->dev
;
372 config
.init_data
= reg_data
;
373 config
.driver_data
= pmu
;
374 config
.regmap
= bcm590xx
->regmap
;
376 if (bcm590xx_reg_matches
)
377 config
.of_node
= bcm590xx_reg_matches
[i
].of_node
;
379 rdev
= devm_regulator_register(&pdev
->dev
, &pmu
->desc
[i
],
382 dev_err(bcm590xx
->dev
,
383 "failed to register %s regulator\n",
385 return PTR_ERR(rdev
);
392 static struct platform_driver bcm590xx_regulator_driver
= {
394 .name
= "bcm590xx-vregs",
395 .owner
= THIS_MODULE
,
397 .probe
= bcm590xx_probe
,
399 module_platform_driver(bcm590xx_regulator_driver
);
401 MODULE_AUTHOR("Matt Porter <mporter@linaro.org>");
402 MODULE_DESCRIPTION("BCM590xx voltage regulator driver");
403 MODULE_LICENSE("GPL v2");
404 MODULE_ALIAS("platform:bcm590xx-vregs");