Merge branch 'tunnels'
[linux.git] / drivers / regulator / bcm590xx-regulator.c
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_linear_ranges = ARRAY_SIZE(_ranges), \
127                 .linear_ranges = _ranges, \
128         }
129
130 static struct bcm590xx_info bcm590xx_regs[] = {
131         BCM590XX_REG_TABLE(rfldo, ldo_a_table),
132         BCM590XX_REG_TABLE(camldo1, ldo_c_table),
133         BCM590XX_REG_TABLE(camldo2, ldo_c_table),
134         BCM590XX_REG_TABLE(simldo1, ldo_a_table),
135         BCM590XX_REG_TABLE(simldo2, ldo_a_table),
136         BCM590XX_REG_TABLE(sdldo, ldo_c_table),
137         BCM590XX_REG_TABLE(sdxldo, ldo_a_table),
138         BCM590XX_REG_TABLE(mmcldo1, ldo_a_table),
139         BCM590XX_REG_TABLE(mmcldo2, ldo_a_table),
140         BCM590XX_REG_TABLE(audldo, ldo_a_table),
141         BCM590XX_REG_TABLE(micldo, ldo_a_table),
142         BCM590XX_REG_TABLE(usbldo, ldo_a_table),
143         BCM590XX_REG_TABLE(vibldo, ldo_c_table),
144         BCM590XX_REG_RANGES(csr, dcdc_csr_ranges),
145         BCM590XX_REG_RANGES(iosr1, dcdc_iosr1_ranges),
146         BCM590XX_REG_RANGES(iosr2, dcdc_iosr1_ranges),
147         BCM590XX_REG_RANGES(msr, dcdc_iosr1_ranges),
148         BCM590XX_REG_RANGES(sdsr1, dcdc_sdsr1_ranges),
149         BCM590XX_REG_RANGES(sdsr2, dcdc_iosr1_ranges),
150         BCM590XX_REG_RANGES(vsr, dcdc_iosr1_ranges),
151 };
152
153 struct bcm590xx_reg {
154         struct regulator_desc *desc;
155         struct bcm590xx *mfd;
156         struct bcm590xx_info **info;
157 };
158
159 static int bcm590xx_get_vsel_register(int id)
160 {
161         if (BCM590XX_REG_IS_LDO(id))
162                 return BCM590XX_RFLDOCTRL + id;
163         else
164                 return BCM590XX_CSRVOUT1 + (id - BCM590XX_REG_CSR) * 3;
165 }
166
167 static int bcm590xx_get_enable_register(int id)
168 {
169         int reg = 0;
170
171         if (BCM590XX_REG_IS_LDO(id))
172                 reg = BCM590XX_RFLDOPMCTRL1 + id * 2;
173         else
174                 switch (id) {
175                 case BCM590XX_REG_CSR:
176                         reg = BCM590XX_CSRPMCTRL1;
177                         break;
178                 case BCM590XX_REG_IOSR1:
179                         reg = BCM590XX_IOSR1PMCTRL1;
180                         break;
181                 case BCM590XX_REG_IOSR2:
182                         reg = BCM590XX_IOSR2PMCTRL1;
183                         break;
184                 case BCM590XX_REG_MSR:
185                         reg = BCM590XX_MSRPMCTRL1;
186                         break;
187                 case BCM590XX_REG_SDSR1:
188                         reg = BCM590XX_SDSR1PMCTRL1;
189                         break;
190                 case BCM590XX_REG_SDSR2:
191                         reg = BCM590XX_SDSR2PMCTRL1;
192                         break;
193                 };
194
195         return reg;
196 }
197
198 static struct regulator_ops bcm590xx_ops_ldo = {
199         .is_enabled             = regulator_is_enabled_regmap,
200         .enable                 = regulator_enable_regmap,
201         .disable                = regulator_disable_regmap,
202         .get_voltage_sel        = regulator_get_voltage_sel_regmap,
203         .set_voltage_sel        = regulator_set_voltage_sel_regmap,
204         .list_voltage           = regulator_list_voltage_table,
205         .map_voltage            = regulator_map_voltage_iterate,
206 };
207
208 static struct regulator_ops bcm590xx_ops_dcdc = {
209         .is_enabled             = regulator_is_enabled_regmap,
210         .enable                 = regulator_enable_regmap,
211         .disable                = regulator_disable_regmap,
212         .get_voltage_sel        = regulator_get_voltage_sel_regmap,
213         .set_voltage_sel        = regulator_set_voltage_sel_regmap,
214         .list_voltage           = regulator_list_voltage_linear_range,
215         .map_voltage            = regulator_map_voltage_linear_range,
216 };
217
218 #define BCM590XX_MATCH(_name, _id) \
219         { \
220                 .name = #_name, \
221                 .driver_data = (void *)&bcm590xx_regs[BCM590XX_REG_##_id], \
222         }
223
224 static struct of_regulator_match bcm590xx_matches[] = {
225         BCM590XX_MATCH(rfldo, RFLDO),
226         BCM590XX_MATCH(camldo1, CAMLDO1),
227         BCM590XX_MATCH(camldo2, CAMLDO2),
228         BCM590XX_MATCH(simldo1, SIMLDO1),
229         BCM590XX_MATCH(simldo2, SIMLDO2),
230         BCM590XX_MATCH(sdldo, SDLDO),
231         BCM590XX_MATCH(sdxldo, SDXLDO),
232         BCM590XX_MATCH(mmcldo1, MMCLDO1),
233         BCM590XX_MATCH(mmcldo2, MMCLDO2),
234         BCM590XX_MATCH(audldo, AUDLDO),
235         BCM590XX_MATCH(micldo, MICLDO),
236         BCM590XX_MATCH(usbldo, USBLDO),
237         BCM590XX_MATCH(vibldo, VIBLDO),
238         BCM590XX_MATCH(csr, CSR),
239         BCM590XX_MATCH(iosr1, IOSR1),
240         BCM590XX_MATCH(iosr2, IOSR2),
241         BCM590XX_MATCH(msr, MSR),
242         BCM590XX_MATCH(sdsr1, SDSR1),
243         BCM590XX_MATCH(sdsr2, SDSR2),
244         BCM590XX_MATCH(vsr, VSR),
245 };
246
247 static struct bcm590xx_board *bcm590xx_parse_dt_reg_data(
248                 struct platform_device *pdev,
249                 struct of_regulator_match **bcm590xx_reg_matches)
250 {
251         struct bcm590xx_board *data;
252         struct device_node *np = pdev->dev.parent->of_node;
253         struct device_node *regulators;
254         struct of_regulator_match *matches = bcm590xx_matches;
255         int count = ARRAY_SIZE(bcm590xx_matches);
256         int idx = 0;
257         int ret;
258
259         if (!np) {
260                 dev_err(&pdev->dev, "of node not found\n");
261                 return NULL;
262         }
263
264         data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
265         if (!data) {
266                 dev_err(&pdev->dev, "failed to allocate regulator board data\n");
267                 return NULL;
268         }
269
270         np = of_node_get(np);
271         regulators = of_get_child_by_name(np, "regulators");
272         if (!regulators) {
273                 dev_warn(&pdev->dev, "regulator node not found\n");
274                 return NULL;
275         }
276
277         ret = of_regulator_match(&pdev->dev, regulators, matches, count);
278         of_node_put(regulators);
279         if (ret < 0) {
280                 dev_err(&pdev->dev, "Error parsing regulator init data: %d\n",
281                         ret);
282                 return NULL;
283         }
284
285         *bcm590xx_reg_matches = matches;
286
287         for (idx = 0; idx < count; idx++) {
288                 if (!matches[idx].init_data || !matches[idx].of_node)
289                         continue;
290
291                 data->bcm590xx_pmu_init_data[idx] = matches[idx].init_data;
292         }
293
294         return data;
295 }
296
297 static int bcm590xx_probe(struct platform_device *pdev)
298 {
299         struct bcm590xx *bcm590xx = dev_get_drvdata(pdev->dev.parent);
300         struct bcm590xx_board *pmu_data = NULL;
301         struct bcm590xx_reg *pmu;
302         struct regulator_config config = { };
303         struct bcm590xx_info *info;
304         struct regulator_init_data *reg_data;
305         struct regulator_dev *rdev;
306         struct of_regulator_match *bcm590xx_reg_matches = NULL;
307         int i;
308
309         pmu_data = bcm590xx_parse_dt_reg_data(pdev,
310                                               &bcm590xx_reg_matches);
311
312         pmu = devm_kzalloc(&pdev->dev, sizeof(*pmu), GFP_KERNEL);
313         if (!pmu) {
314                 dev_err(&pdev->dev, "Memory allocation failed for pmu\n");
315                 return -ENOMEM;
316         }
317
318         pmu->mfd = bcm590xx;
319
320         platform_set_drvdata(pdev, pmu);
321
322         pmu->desc = devm_kzalloc(&pdev->dev, BCM590XX_NUM_REGS *
323                         sizeof(struct regulator_desc), GFP_KERNEL);
324         if (!pmu->desc) {
325                 dev_err(&pdev->dev, "Memory alloc fails for desc\n");
326                 return -ENOMEM;
327         }
328
329         pmu->info = devm_kzalloc(&pdev->dev, BCM590XX_NUM_REGS *
330                         sizeof(struct bcm590xx_info *), GFP_KERNEL);
331         if (!pmu->info) {
332                 dev_err(&pdev->dev, "Memory alloc fails for info\n");
333                 return -ENOMEM;
334         }
335
336         info = bcm590xx_regs;
337
338         for (i = 0; i < BCM590XX_NUM_REGS; i++, info++) {
339                 if (pmu_data)
340                         reg_data = pmu_data->bcm590xx_pmu_init_data[i];
341                 else
342                         reg_data = NULL;
343
344                 /* Register the regulators */
345                 pmu->info[i] = info;
346
347                 pmu->desc[i].name = info->name;
348                 pmu->desc[i].supply_name = info->vin_name;
349                 pmu->desc[i].id = i;
350                 pmu->desc[i].volt_table = info->volt_table;
351                 pmu->desc[i].n_voltages = info->n_voltages;
352                 pmu->desc[i].linear_ranges = info->linear_ranges;
353                 pmu->desc[i].n_linear_ranges = info->n_linear_ranges;
354
355                 if (BCM590XX_REG_IS_LDO(i)) {
356                         pmu->desc[i].ops = &bcm590xx_ops_ldo;
357                         pmu->desc[i].vsel_mask = BCM590XX_LDO_VSEL_MASK;
358                 } else {
359                         pmu->desc[i].ops = &bcm590xx_ops_dcdc;
360                         pmu->desc[i].vsel_mask = BCM590XX_SR_VSEL_MASK;
361                 }
362
363                 pmu->desc[i].vsel_reg = bcm590xx_get_vsel_register(i);
364                 pmu->desc[i].enable_is_inverted = true;
365                 pmu->desc[i].enable_mask = BCM590XX_REG_ENABLE;
366                 pmu->desc[i].enable_reg = bcm590xx_get_enable_register(i);
367                 pmu->desc[i].type = REGULATOR_VOLTAGE;
368                 pmu->desc[i].owner = THIS_MODULE;
369
370                 config.dev = bcm590xx->dev;
371                 config.init_data = reg_data;
372                 config.driver_data = pmu;
373                 config.regmap = bcm590xx->regmap;
374
375                 if (bcm590xx_reg_matches)
376                         config.of_node = bcm590xx_reg_matches[i].of_node;
377
378                 rdev = devm_regulator_register(&pdev->dev, &pmu->desc[i],
379                                                &config);
380                 if (IS_ERR(rdev)) {
381                         dev_err(bcm590xx->dev,
382                                 "failed to register %s regulator\n",
383                                 pdev->name);
384                         return PTR_ERR(rdev);
385                 }
386         }
387
388         return 0;
389 }
390
391 static struct platform_driver bcm590xx_regulator_driver = {
392         .driver = {
393                 .name = "bcm590xx-vregs",
394                 .owner = THIS_MODULE,
395         },
396         .probe = bcm590xx_probe,
397 };
398 module_platform_driver(bcm590xx_regulator_driver);
399
400 MODULE_AUTHOR("Matt Porter <mporter@linaro.org>");
401 MODULE_DESCRIPTION("BCM590xx voltage regulator driver");
402 MODULE_LICENSE("GPL v2");
403 MODULE_ALIAS("platform:bcm590xx-vregs");