blob: 85ccc93b2bb62d6f35143de096298f313e0598cf [file] [log] [blame]
Matt Porter462c9fc2014-03-11 11:46:13 -04001/*
2 * Broadcom BCM590xx regulator driver
3 *
4 * Copyright 2014 Linaro Limited
5 * Author: Matt Porter <mporter@linaro.org>
6 *
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.
11 */
12
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>
18#include <linux/of.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>
24
Matt Porterc6466952014-04-23 19:21:32 -040025/* I2C slave 0 registers */
Matt Porter462c9fc2014-03-11 11:46:13 -040026#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
Matt Porter462c9fc2014-03-11 11:46:13 -040034#define BCM590XX_RFLDOCTRL 0x96
35#define BCM590XX_CSRVOUT1 0xc0
Matt Porterc6466952014-04-23 19:21:32 -040036
37/* I2C slave 1 registers */
38#define BCM590XX_GPLDO5PMCTRL1 0x16
39#define BCM590XX_GPLDO6PMCTRL1 0x18
40#define BCM590XX_GPLDO1CTRL 0x1a
41#define BCM590XX_GPLDO2CTRL 0x1b
42#define BCM590XX_GPLDO3CTRL 0x1c
43#define BCM590XX_GPLDO4CTRL 0x1d
44#define BCM590XX_GPLDO5CTRL 0x1e
45#define BCM590XX_GPLDO6CTRL 0x1f
46#define BCM590XX_OTG_CTRL 0x40
47#define BCM590XX_GPLDO1PMCTRL1 0x57
48#define BCM590XX_GPLDO2PMCTRL1 0x59
49#define BCM590XX_GPLDO3PMCTRL1 0x5b
50#define BCM590XX_GPLDO4PMCTRL1 0x5d
51
52#define BCM590XX_REG_ENABLE BIT(7)
53#define BCM590XX_VBUS_ENABLE BIT(2)
Matt Porter462c9fc2014-03-11 11:46:13 -040054#define BCM590XX_LDO_VSEL_MASK GENMASK(5, 3)
55#define BCM590XX_SR_VSEL_MASK GENMASK(5, 0)
56
Matt Porterc6466952014-04-23 19:21:32 -040057/*
58 * RFLDO to VSR regulators are
59 * accessed via I2C slave 0
60 */
61
Matt Porter462c9fc2014-03-11 11:46:13 -040062/* LDO regulator IDs */
63#define BCM590XX_REG_RFLDO 0
64#define BCM590XX_REG_CAMLDO1 1
65#define BCM590XX_REG_CAMLDO2 2
66#define BCM590XX_REG_SIMLDO1 3
67#define BCM590XX_REG_SIMLDO2 4
68#define BCM590XX_REG_SDLDO 5
69#define BCM590XX_REG_SDXLDO 6
70#define BCM590XX_REG_MMCLDO1 7
71#define BCM590XX_REG_MMCLDO2 8
72#define BCM590XX_REG_AUDLDO 9
73#define BCM590XX_REG_MICLDO 10
74#define BCM590XX_REG_USBLDO 11
75#define BCM590XX_REG_VIBLDO 12
76
77/* DCDC regulator IDs */
78#define BCM590XX_REG_CSR 13
79#define BCM590XX_REG_IOSR1 14
80#define BCM590XX_REG_IOSR2 15
81#define BCM590XX_REG_MSR 16
82#define BCM590XX_REG_SDSR1 17
83#define BCM590XX_REG_SDSR2 18
84#define BCM590XX_REG_VSR 19
85
Matt Porterc6466952014-04-23 19:21:32 -040086/*
87 * GPLDO1 to VBUS regulators are
88 * accessed via I2C slave 1
89 */
90
91#define BCM590XX_REG_GPLDO1 20
92#define BCM590XX_REG_GPLDO2 21
93#define BCM590XX_REG_GPLDO3 22
94#define BCM590XX_REG_GPLDO4 23
95#define BCM590XX_REG_GPLDO5 24
96#define BCM590XX_REG_GPLDO6 25
97#define BCM590XX_REG_VBUS 26
98
99#define BCM590XX_NUM_REGS 27
Matt Porter462c9fc2014-03-11 11:46:13 -0400100
101#define BCM590XX_REG_IS_LDO(n) (n < BCM590XX_REG_CSR)
Matt Porterc6466952014-04-23 19:21:32 -0400102#define BCM590XX_REG_IS_GPLDO(n) \
103 ((n > BCM590XX_REG_VSR) && (n < BCM590XX_REG_VBUS))
104#define BCM590XX_REG_IS_VBUS(n) (n == BCM590XX_REG_VBUS)
Matt Porter462c9fc2014-03-11 11:46:13 -0400105
Matt Porter462c9fc2014-03-11 11:46:13 -0400106/* LDO group A: supported voltages in microvolts */
107static const unsigned int ldo_a_table[] = {
108 1200000, 1800000, 2500000, 2700000, 2800000,
109 2900000, 3000000, 3300000,
110};
111
112/* LDO group C: supported voltages in microvolts */
113static const unsigned int ldo_c_table[] = {
114 3100000, 1800000, 2500000, 2700000, 2800000,
115 2900000, 3000000, 3300000,
116};
117
Graham Williams636f4a32014-06-18 12:42:10 -0700118static const unsigned int ldo_vbus[] = {
119 5000000,
120};
121
Matt Porter462c9fc2014-03-11 11:46:13 -0400122/* DCDC group CSR: supported voltages in microvolts */
123static const struct regulator_linear_range dcdc_csr_ranges[] = {
124 REGULATOR_LINEAR_RANGE(860000, 2, 50, 10000),
125 REGULATOR_LINEAR_RANGE(1360000, 51, 55, 20000),
126 REGULATOR_LINEAR_RANGE(900000, 56, 63, 0),
127};
128
129/* DCDC group IOSR1: supported voltages in microvolts */
130static const struct regulator_linear_range dcdc_iosr1_ranges[] = {
131 REGULATOR_LINEAR_RANGE(860000, 2, 51, 10000),
132 REGULATOR_LINEAR_RANGE(1500000, 52, 52, 0),
133 REGULATOR_LINEAR_RANGE(1800000, 53, 53, 0),
134 REGULATOR_LINEAR_RANGE(900000, 54, 63, 0),
135};
136
137/* DCDC group SDSR1: supported voltages in microvolts */
138static const struct regulator_linear_range dcdc_sdsr1_ranges[] = {
139 REGULATOR_LINEAR_RANGE(860000, 2, 50, 10000),
140 REGULATOR_LINEAR_RANGE(1340000, 51, 51, 0),
141 REGULATOR_LINEAR_RANGE(900000, 52, 63, 0),
142};
143
144struct bcm590xx_info {
145 const char *name;
146 const char *vin_name;
147 u8 n_voltages;
148 const unsigned int *volt_table;
149 u8 n_linear_ranges;
150 const struct regulator_linear_range *linear_ranges;
151};
152
153#define BCM590XX_REG_TABLE(_name, _table) \
154 { \
155 .name = #_name, \
156 .n_voltages = ARRAY_SIZE(_table), \
157 .volt_table = _table, \
158 }
159
160#define BCM590XX_REG_RANGES(_name, _ranges) \
161 { \
162 .name = #_name, \
Tim Kryger1e791402014-04-10 13:04:00 -0700163 .n_voltages = 64, \
Matt Porter462c9fc2014-03-11 11:46:13 -0400164 .n_linear_ranges = ARRAY_SIZE(_ranges), \
165 .linear_ranges = _ranges, \
166 }
167
168static struct bcm590xx_info bcm590xx_regs[] = {
169 BCM590XX_REG_TABLE(rfldo, ldo_a_table),
170 BCM590XX_REG_TABLE(camldo1, ldo_c_table),
171 BCM590XX_REG_TABLE(camldo2, ldo_c_table),
172 BCM590XX_REG_TABLE(simldo1, ldo_a_table),
173 BCM590XX_REG_TABLE(simldo2, ldo_a_table),
174 BCM590XX_REG_TABLE(sdldo, ldo_c_table),
175 BCM590XX_REG_TABLE(sdxldo, ldo_a_table),
176 BCM590XX_REG_TABLE(mmcldo1, ldo_a_table),
177 BCM590XX_REG_TABLE(mmcldo2, ldo_a_table),
178 BCM590XX_REG_TABLE(audldo, ldo_a_table),
179 BCM590XX_REG_TABLE(micldo, ldo_a_table),
180 BCM590XX_REG_TABLE(usbldo, ldo_a_table),
181 BCM590XX_REG_TABLE(vibldo, ldo_c_table),
182 BCM590XX_REG_RANGES(csr, dcdc_csr_ranges),
183 BCM590XX_REG_RANGES(iosr1, dcdc_iosr1_ranges),
184 BCM590XX_REG_RANGES(iosr2, dcdc_iosr1_ranges),
185 BCM590XX_REG_RANGES(msr, dcdc_iosr1_ranges),
186 BCM590XX_REG_RANGES(sdsr1, dcdc_sdsr1_ranges),
187 BCM590XX_REG_RANGES(sdsr2, dcdc_iosr1_ranges),
188 BCM590XX_REG_RANGES(vsr, dcdc_iosr1_ranges),
Matt Porterc6466952014-04-23 19:21:32 -0400189 BCM590XX_REG_TABLE(gpldo1, ldo_a_table),
190 BCM590XX_REG_TABLE(gpldo2, ldo_a_table),
191 BCM590XX_REG_TABLE(gpldo3, ldo_a_table),
192 BCM590XX_REG_TABLE(gpldo4, ldo_a_table),
193 BCM590XX_REG_TABLE(gpldo5, ldo_a_table),
194 BCM590XX_REG_TABLE(gpldo6, ldo_a_table),
Graham Williams636f4a32014-06-18 12:42:10 -0700195 BCM590XX_REG_TABLE(vbus, ldo_vbus),
Matt Porter462c9fc2014-03-11 11:46:13 -0400196};
197
198struct bcm590xx_reg {
199 struct regulator_desc *desc;
200 struct bcm590xx *mfd;
Matt Porter462c9fc2014-03-11 11:46:13 -0400201};
202
203static int bcm590xx_get_vsel_register(int id)
204{
205 if (BCM590XX_REG_IS_LDO(id))
206 return BCM590XX_RFLDOCTRL + id;
Matt Porterc6466952014-04-23 19:21:32 -0400207 else if (BCM590XX_REG_IS_GPLDO(id))
208 return BCM590XX_GPLDO1CTRL + id;
Matt Porter462c9fc2014-03-11 11:46:13 -0400209 else
210 return BCM590XX_CSRVOUT1 + (id - BCM590XX_REG_CSR) * 3;
211}
212
213static int bcm590xx_get_enable_register(int id)
214{
215 int reg = 0;
216
217 if (BCM590XX_REG_IS_LDO(id))
218 reg = BCM590XX_RFLDOPMCTRL1 + id * 2;
Matt Porterc6466952014-04-23 19:21:32 -0400219 else if (BCM590XX_REG_IS_GPLDO(id))
220 reg = BCM590XX_GPLDO1PMCTRL1 + id * 2;
Matt Porter462c9fc2014-03-11 11:46:13 -0400221 else
222 switch (id) {
223 case BCM590XX_REG_CSR:
224 reg = BCM590XX_CSRPMCTRL1;
225 break;
226 case BCM590XX_REG_IOSR1:
227 reg = BCM590XX_IOSR1PMCTRL1;
228 break;
229 case BCM590XX_REG_IOSR2:
230 reg = BCM590XX_IOSR2PMCTRL1;
231 break;
232 case BCM590XX_REG_MSR:
233 reg = BCM590XX_MSRPMCTRL1;
234 break;
235 case BCM590XX_REG_SDSR1:
236 reg = BCM590XX_SDSR1PMCTRL1;
237 break;
238 case BCM590XX_REG_SDSR2:
239 reg = BCM590XX_SDSR2PMCTRL1;
240 break;
Axel Lind6afa2b2019-01-05 11:28:17 +0800241 case BCM590XX_REG_VSR:
242 reg = BCM590XX_VSRPMCTRL1;
243 break;
Matt Porterc6466952014-04-23 19:21:32 -0400244 case BCM590XX_REG_VBUS:
245 reg = BCM590XX_OTG_CTRL;
Axel Lind6afa2b2019-01-05 11:28:17 +0800246 break;
Javier Martinez Canillasf3f400e2015-09-11 04:26:56 +0200247 }
Matt Porter462c9fc2014-03-11 11:46:13 -0400248
Matt Porterc6466952014-04-23 19:21:32 -0400249
Matt Porter462c9fc2014-03-11 11:46:13 -0400250 return reg;
251}
252
Bhumika Goyal20f860c2017-01-28 19:30:24 +0530253static const struct regulator_ops bcm590xx_ops_ldo = {
Matt Porter462c9fc2014-03-11 11:46:13 -0400254 .is_enabled = regulator_is_enabled_regmap,
255 .enable = regulator_enable_regmap,
256 .disable = regulator_disable_regmap,
257 .get_voltage_sel = regulator_get_voltage_sel_regmap,
258 .set_voltage_sel = regulator_set_voltage_sel_regmap,
259 .list_voltage = regulator_list_voltage_table,
260 .map_voltage = regulator_map_voltage_iterate,
261};
262
Bhumika Goyal20f860c2017-01-28 19:30:24 +0530263static const struct regulator_ops bcm590xx_ops_dcdc = {
Matt Porter462c9fc2014-03-11 11:46:13 -0400264 .is_enabled = regulator_is_enabled_regmap,
265 .enable = regulator_enable_regmap,
266 .disable = regulator_disable_regmap,
267 .get_voltage_sel = regulator_get_voltage_sel_regmap,
268 .set_voltage_sel = regulator_set_voltage_sel_regmap,
269 .list_voltage = regulator_list_voltage_linear_range,
270 .map_voltage = regulator_map_voltage_linear_range,
271};
272
Bhumika Goyal20f860c2017-01-28 19:30:24 +0530273static const struct regulator_ops bcm590xx_ops_vbus = {
Matt Porterc6466952014-04-23 19:21:32 -0400274 .is_enabled = regulator_is_enabled_regmap,
275 .enable = regulator_enable_regmap,
276 .disable = regulator_disable_regmap,
277};
278
Matt Porter462c9fc2014-03-11 11:46:13 -0400279static int bcm590xx_probe(struct platform_device *pdev)
280{
281 struct bcm590xx *bcm590xx = dev_get_drvdata(pdev->dev.parent);
Matt Porter462c9fc2014-03-11 11:46:13 -0400282 struct bcm590xx_reg *pmu;
283 struct regulator_config config = { };
284 struct bcm590xx_info *info;
Matt Porter462c9fc2014-03-11 11:46:13 -0400285 struct regulator_dev *rdev;
Matt Porter462c9fc2014-03-11 11:46:13 -0400286 int i;
287
Matt Porter462c9fc2014-03-11 11:46:13 -0400288 pmu = devm_kzalloc(&pdev->dev, sizeof(*pmu), GFP_KERNEL);
Jingoo Hanc969faa2014-06-02 15:27:21 +0900289 if (!pmu)
Matt Porter462c9fc2014-03-11 11:46:13 -0400290 return -ENOMEM;
Matt Porter462c9fc2014-03-11 11:46:13 -0400291
292 pmu->mfd = bcm590xx;
293
294 platform_set_drvdata(pdev, pmu);
295
Kees Cooka86854d2018-06-12 14:07:58 -0700296 pmu->desc = devm_kcalloc(&pdev->dev,
297 BCM590XX_NUM_REGS,
298 sizeof(struct regulator_desc),
299 GFP_KERNEL);
Jingoo Hanc969faa2014-06-02 15:27:21 +0900300 if (!pmu->desc)
Matt Porter462c9fc2014-03-11 11:46:13 -0400301 return -ENOMEM;
Matt Porter462c9fc2014-03-11 11:46:13 -0400302
Matt Porter462c9fc2014-03-11 11:46:13 -0400303 info = bcm590xx_regs;
304
305 for (i = 0; i < BCM590XX_NUM_REGS; i++, info++) {
Matt Porter462c9fc2014-03-11 11:46:13 -0400306 /* Register the regulators */
Matt Porter462c9fc2014-03-11 11:46:13 -0400307 pmu->desc[i].name = info->name;
Axel Lina4e73622019-04-01 22:42:36 +0800308 pmu->desc[i].of_match = of_match_ptr(info->name);
309 pmu->desc[i].regulators_node = of_match_ptr("regulators");
Matt Porter462c9fc2014-03-11 11:46:13 -0400310 pmu->desc[i].supply_name = info->vin_name;
311 pmu->desc[i].id = i;
312 pmu->desc[i].volt_table = info->volt_table;
313 pmu->desc[i].n_voltages = info->n_voltages;
314 pmu->desc[i].linear_ranges = info->linear_ranges;
315 pmu->desc[i].n_linear_ranges = info->n_linear_ranges;
316
Matt Porterc6466952014-04-23 19:21:32 -0400317 if ((BCM590XX_REG_IS_LDO(i)) || (BCM590XX_REG_IS_GPLDO(i))) {
Matt Porter462c9fc2014-03-11 11:46:13 -0400318 pmu->desc[i].ops = &bcm590xx_ops_ldo;
319 pmu->desc[i].vsel_mask = BCM590XX_LDO_VSEL_MASK;
Matt Porterc6466952014-04-23 19:21:32 -0400320 } else if (BCM590XX_REG_IS_VBUS(i))
321 pmu->desc[i].ops = &bcm590xx_ops_vbus;
322 else {
Matt Porter462c9fc2014-03-11 11:46:13 -0400323 pmu->desc[i].ops = &bcm590xx_ops_dcdc;
324 pmu->desc[i].vsel_mask = BCM590XX_SR_VSEL_MASK;
325 }
326
Matt Porterc6466952014-04-23 19:21:32 -0400327 if (BCM590XX_REG_IS_VBUS(i))
328 pmu->desc[i].enable_mask = BCM590XX_VBUS_ENABLE;
329 else {
330 pmu->desc[i].vsel_reg = bcm590xx_get_vsel_register(i);
331 pmu->desc[i].enable_is_inverted = true;
332 pmu->desc[i].enable_mask = BCM590XX_REG_ENABLE;
333 }
Matt Porter462c9fc2014-03-11 11:46:13 -0400334 pmu->desc[i].enable_reg = bcm590xx_get_enable_register(i);
335 pmu->desc[i].type = REGULATOR_VOLTAGE;
336 pmu->desc[i].owner = THIS_MODULE;
337
338 config.dev = bcm590xx->dev;
Matt Porter462c9fc2014-03-11 11:46:13 -0400339 config.driver_data = pmu;
Matt Porterc6466952014-04-23 19:21:32 -0400340 if (BCM590XX_REG_IS_GPLDO(i) || BCM590XX_REG_IS_VBUS(i))
341 config.regmap = bcm590xx->regmap_sec;
342 else
343 config.regmap = bcm590xx->regmap_pri;
Matt Porter462c9fc2014-03-11 11:46:13 -0400344
Matt Porter462c9fc2014-03-11 11:46:13 -0400345 rdev = devm_regulator_register(&pdev->dev, &pmu->desc[i],
346 &config);
347 if (IS_ERR(rdev)) {
348 dev_err(bcm590xx->dev,
349 "failed to register %s regulator\n",
350 pdev->name);
351 return PTR_ERR(rdev);
352 }
Matt Porter462c9fc2014-03-11 11:46:13 -0400353 }
354
355 return 0;
356}
357
358static struct platform_driver bcm590xx_regulator_driver = {
359 .driver = {
360 .name = "bcm590xx-vregs",
Matt Porter462c9fc2014-03-11 11:46:13 -0400361 },
362 .probe = bcm590xx_probe,
363};
364module_platform_driver(bcm590xx_regulator_driver);
365
366MODULE_AUTHOR("Matt Porter <mporter@linaro.org>");
367MODULE_DESCRIPTION("BCM590xx voltage regulator driver");
368MODULE_LICENSE("GPL v2");
Axel Linc9347432014-03-14 09:57:54 +0800369MODULE_ALIAS("platform:bcm590xx-vregs");