1 /*
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 
25 /* Register defs */
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)
35 
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)
40 
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
55 
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
64 
65 #define BCM590XX_NUM_REGS	20
66 
67 #define BCM590XX_REG_IS_LDO(n)	(n < BCM590XX_REG_CSR)
68 
69 struct bcm590xx_board {
70 	struct regulator_init_data *bcm590xx_pmu_init_data[BCM590XX_NUM_REGS];
71 };
72 
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,
77 };
78 
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,
83 };
84 
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),
90 };
91 
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),
98 };
99 
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),
105 };
106 
107 struct bcm590xx_info {
108 	const char *name;
109 	const char *vin_name;
110 	u8 n_voltages;
111 	const unsigned int *volt_table;
112 	u8 n_linear_ranges;
113 	const struct regulator_linear_range *linear_ranges;
114 };
115 
116 #define BCM590XX_REG_TABLE(_name, _table) \
117 	{ \
118 		.name = #_name, \
119 		.n_voltages = ARRAY_SIZE(_table), \
120 		.volt_table = _table, \
121 	}
122 
123 #define BCM590XX_REG_RANGES(_name, _ranges) \
124 	{ \
125 		.name = #_name, \
126 		.n_voltages = 64, \
127 		.n_linear_ranges = ARRAY_SIZE(_ranges), \
128 		.linear_ranges = _ranges, \
129 	}
130 
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),
152 };
153 
154 struct bcm590xx_reg {
155 	struct regulator_desc *desc;
156 	struct bcm590xx *mfd;
157 	struct bcm590xx_info **info;
158 };
159 
160 static int bcm590xx_get_vsel_register(int id)
161 {
162 	if (BCM590XX_REG_IS_LDO(id))
163 		return BCM590XX_RFLDOCTRL + id;
164 	else
165 		return BCM590XX_CSRVOUT1 + (id - BCM590XX_REG_CSR) * 3;
166 }
167 
168 static int bcm590xx_get_enable_register(int id)
169 {
170 	int reg = 0;
171 
172 	if (BCM590XX_REG_IS_LDO(id))
173 		reg = BCM590XX_RFLDOPMCTRL1 + id * 2;
174 	else
175 		switch (id) {
176 		case BCM590XX_REG_CSR:
177 			reg = BCM590XX_CSRPMCTRL1;
178 			break;
179 		case BCM590XX_REG_IOSR1:
180 			reg = BCM590XX_IOSR1PMCTRL1;
181 			break;
182 		case BCM590XX_REG_IOSR2:
183 			reg = BCM590XX_IOSR2PMCTRL1;
184 			break;
185 		case BCM590XX_REG_MSR:
186 			reg = BCM590XX_MSRPMCTRL1;
187 			break;
188 		case BCM590XX_REG_SDSR1:
189 			reg = BCM590XX_SDSR1PMCTRL1;
190 			break;
191 		case BCM590XX_REG_SDSR2:
192 			reg = BCM590XX_SDSR2PMCTRL1;
193 			break;
194 		};
195 
196 	return reg;
197 }
198 
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,
207 };
208 
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,
217 };
218 
219 #define BCM590XX_MATCH(_name, _id) \
220 	{ \
221 		.name = #_name, \
222 		.driver_data = (void *)&bcm590xx_regs[BCM590XX_REG_##_id], \
223 	}
224 
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),
246 };
247 
248 static struct bcm590xx_board *bcm590xx_parse_dt_reg_data(
249 		struct platform_device *pdev,
250 		struct of_regulator_match **bcm590xx_reg_matches)
251 {
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);
257 	int idx = 0;
258 	int ret;
259 
260 	if (!np) {
261 		dev_err(&pdev->dev, "of node not found\n");
262 		return NULL;
263 	}
264 
265 	data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
266 	if (!data) {
267 		dev_err(&pdev->dev, "failed to allocate regulator board data\n");
268 		return NULL;
269 	}
270 
271 	np = of_node_get(np);
272 	regulators = of_get_child_by_name(np, "regulators");
273 	if (!regulators) {
274 		dev_warn(&pdev->dev, "regulator node not found\n");
275 		return NULL;
276 	}
277 
278 	ret = of_regulator_match(&pdev->dev, regulators, matches, count);
279 	of_node_put(regulators);
280 	if (ret < 0) {
281 		dev_err(&pdev->dev, "Error parsing regulator init data: %d\n",
282 			ret);
283 		return NULL;
284 	}
285 
286 	*bcm590xx_reg_matches = matches;
287 
288 	for (idx = 0; idx < count; idx++) {
289 		if (!matches[idx].init_data || !matches[idx].of_node)
290 			continue;
291 
292 		data->bcm590xx_pmu_init_data[idx] = matches[idx].init_data;
293 	}
294 
295 	return data;
296 }
297 
298 static int bcm590xx_probe(struct platform_device *pdev)
299 {
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;
308 	int i;
309 
310 	pmu_data = bcm590xx_parse_dt_reg_data(pdev,
311 					      &bcm590xx_reg_matches);
312 
313 	pmu = devm_kzalloc(&pdev->dev, sizeof(*pmu), GFP_KERNEL);
314 	if (!pmu) {
315 		dev_err(&pdev->dev, "Memory allocation failed for pmu\n");
316 		return -ENOMEM;
317 	}
318 
319 	pmu->mfd = bcm590xx;
320 
321 	platform_set_drvdata(pdev, pmu);
322 
323 	pmu->desc = devm_kzalloc(&pdev->dev, BCM590XX_NUM_REGS *
324 			sizeof(struct regulator_desc), GFP_KERNEL);
325 	if (!pmu->desc) {
326 		dev_err(&pdev->dev, "Memory alloc fails for desc\n");
327 		return -ENOMEM;
328 	}
329 
330 	pmu->info = devm_kzalloc(&pdev->dev, BCM590XX_NUM_REGS *
331 			sizeof(struct bcm590xx_info *), GFP_KERNEL);
332 	if (!pmu->info) {
333 		dev_err(&pdev->dev, "Memory alloc fails for info\n");
334 		return -ENOMEM;
335 	}
336 
337 	info = bcm590xx_regs;
338 
339 	for (i = 0; i < BCM590XX_NUM_REGS; i++, info++) {
340 		if (pmu_data)
341 			reg_data = pmu_data->bcm590xx_pmu_init_data[i];
342 		else
343 			reg_data = NULL;
344 
345 		/* Register the regulators */
346 		pmu->info[i] = info;
347 
348 		pmu->desc[i].name = info->name;
349 		pmu->desc[i].supply_name = info->vin_name;
350 		pmu->desc[i].id = i;
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;
355 
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;
359 		} else {
360 			pmu->desc[i].ops = &bcm590xx_ops_dcdc;
361 			pmu->desc[i].vsel_mask = BCM590XX_SR_VSEL_MASK;
362 		}
363 
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;
370 
371 		config.dev = bcm590xx->dev;
372 		config.init_data = reg_data;
373 		config.driver_data = pmu;
374 		config.regmap = bcm590xx->regmap;
375 
376 		if (bcm590xx_reg_matches)
377 			config.of_node = bcm590xx_reg_matches[i].of_node;
378 
379 		rdev = devm_regulator_register(&pdev->dev, &pmu->desc[i],
380 					       &config);
381 		if (IS_ERR(rdev)) {
382 			dev_err(bcm590xx->dev,
383 				"failed to register %s regulator\n",
384 				pdev->name);
385 			return PTR_ERR(rdev);
386 		}
387 	}
388 
389 	return 0;
390 }
391 
392 static struct platform_driver bcm590xx_regulator_driver = {
393 	.driver = {
394 		.name = "bcm590xx-vregs",
395 		.owner = THIS_MODULE,
396 	},
397 	.probe = bcm590xx_probe,
398 };
399 module_platform_driver(bcm590xx_regulator_driver);
400 
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");
405