regulator: as3722: Fix incorrect parameter initialization
[cascardo/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 /* I2C slave 0 registers */
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_RFLDOCTRL      0x96
35 #define BCM590XX_CSRVOUT1       0xc0
36
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)
54 #define BCM590XX_LDO_VSEL_MASK  GENMASK(5, 3)
55 #define BCM590XX_SR_VSEL_MASK   GENMASK(5, 0)
56
57 /*
58  * RFLDO to VSR regulators are
59  * accessed via I2C slave 0
60  */
61
62 /* 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
86 /*
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
100
101 #define BCM590XX_REG_IS_LDO(n)  (n < BCM590XX_REG_CSR)
102 #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)
105
106 struct bcm590xx_board {
107         struct regulator_init_data *bcm590xx_pmu_init_data[BCM590XX_NUM_REGS];
108 };
109
110 /* LDO group A: supported voltages in microvolts */
111 static const unsigned int ldo_a_table[] = {
112         1200000, 1800000, 2500000, 2700000, 2800000,
113         2900000, 3000000, 3300000,
114 };
115
116 /* LDO group C: supported voltages in microvolts */
117 static const unsigned int ldo_c_table[] = {
118         3100000, 1800000, 2500000, 2700000, 2800000,
119         2900000, 3000000, 3300000,
120 };
121
122 /* DCDC group CSR: supported voltages in microvolts */
123 static 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 */
130 static 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 */
138 static 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
144 struct 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, \
163                 .n_voltages = 64, \
164                 .n_linear_ranges = ARRAY_SIZE(_ranges), \
165                 .linear_ranges = _ranges, \
166         }
167
168 static 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),
189         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),
195 };
196
197 struct bcm590xx_reg {
198         struct regulator_desc *desc;
199         struct bcm590xx *mfd;
200         struct bcm590xx_info **info;
201 };
202
203 static int bcm590xx_get_vsel_register(int id)
204 {
205         if (BCM590XX_REG_IS_LDO(id))
206                 return BCM590XX_RFLDOCTRL + id;
207         else if (BCM590XX_REG_IS_GPLDO(id))
208                 return BCM590XX_GPLDO1CTRL + id;
209         else
210                 return BCM590XX_CSRVOUT1 + (id - BCM590XX_REG_CSR) * 3;
211 }
212
213 static 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;
219         else if (BCM590XX_REG_IS_GPLDO(id))
220                 reg = BCM590XX_GPLDO1PMCTRL1 + id * 2;
221         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;
241                 case BCM590XX_REG_VBUS:
242                         reg = BCM590XX_OTG_CTRL;
243                 };
244
245
246         return reg;
247 }
248
249 static 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 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 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
275 #define BCM590XX_MATCH(_name, _id) \
276         { \
277                 .name = #_name, \
278                 .driver_data = (void *)&bcm590xx_regs[BCM590XX_REG_##_id], \
279         }
280
281 static struct of_regulator_match bcm590xx_matches[] = {
282         BCM590XX_MATCH(rfldo, RFLDO),
283         BCM590XX_MATCH(camldo1, CAMLDO1),
284         BCM590XX_MATCH(camldo2, CAMLDO2),
285         BCM590XX_MATCH(simldo1, SIMLDO1),
286         BCM590XX_MATCH(simldo2, SIMLDO2),
287         BCM590XX_MATCH(sdldo, SDLDO),
288         BCM590XX_MATCH(sdxldo, SDXLDO),
289         BCM590XX_MATCH(mmcldo1, MMCLDO1),
290         BCM590XX_MATCH(mmcldo2, MMCLDO2),
291         BCM590XX_MATCH(audldo, AUDLDO),
292         BCM590XX_MATCH(micldo, MICLDO),
293         BCM590XX_MATCH(usbldo, USBLDO),
294         BCM590XX_MATCH(vibldo, VIBLDO),
295         BCM590XX_MATCH(csr, CSR),
296         BCM590XX_MATCH(iosr1, IOSR1),
297         BCM590XX_MATCH(iosr2, IOSR2),
298         BCM590XX_MATCH(msr, MSR),
299         BCM590XX_MATCH(sdsr1, SDSR1),
300         BCM590XX_MATCH(sdsr2, SDSR2),
301         BCM590XX_MATCH(vsr, VSR),
302         BCM590XX_MATCH(gpldo1, GPLDO1),
303         BCM590XX_MATCH(gpldo2, GPLDO2),
304         BCM590XX_MATCH(gpldo3, GPLDO3),
305         BCM590XX_MATCH(gpldo4, GPLDO4),
306         BCM590XX_MATCH(gpldo5, GPLDO5),
307         BCM590XX_MATCH(gpldo6, GPLDO6),
308         BCM590XX_MATCH(vbus, VBUS),
309 };
310
311 static struct bcm590xx_board *bcm590xx_parse_dt_reg_data(
312                 struct platform_device *pdev,
313                 struct of_regulator_match **bcm590xx_reg_matches)
314 {
315         struct bcm590xx_board *data;
316         struct device_node *np = pdev->dev.parent->of_node;
317         struct device_node *regulators;
318         struct of_regulator_match *matches = bcm590xx_matches;
319         int count = ARRAY_SIZE(bcm590xx_matches);
320         int idx = 0;
321         int ret;
322
323         if (!np) {
324                 dev_err(&pdev->dev, "of node not found\n");
325                 return NULL;
326         }
327
328         data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
329         if (!data) {
330                 dev_err(&pdev->dev, "failed to allocate regulator board data\n");
331                 return NULL;
332         }
333
334         np = of_node_get(np);
335         regulators = of_get_child_by_name(np, "regulators");
336         if (!regulators) {
337                 dev_warn(&pdev->dev, "regulator node not found\n");
338                 return NULL;
339         }
340
341         ret = of_regulator_match(&pdev->dev, regulators, matches, count);
342         of_node_put(regulators);
343         if (ret < 0) {
344                 dev_err(&pdev->dev, "Error parsing regulator init data: %d\n",
345                         ret);
346                 return NULL;
347         }
348
349         *bcm590xx_reg_matches = matches;
350
351         for (idx = 0; idx < count; idx++) {
352                 if (!matches[idx].init_data || !matches[idx].of_node)
353                         continue;
354
355                 data->bcm590xx_pmu_init_data[idx] = matches[idx].init_data;
356         }
357
358         return data;
359 }
360
361 static int bcm590xx_probe(struct platform_device *pdev)
362 {
363         struct bcm590xx *bcm590xx = dev_get_drvdata(pdev->dev.parent);
364         struct bcm590xx_board *pmu_data = NULL;
365         struct bcm590xx_reg *pmu;
366         struct regulator_config config = { };
367         struct bcm590xx_info *info;
368         struct regulator_init_data *reg_data;
369         struct regulator_dev *rdev;
370         struct of_regulator_match *bcm590xx_reg_matches = NULL;
371         int i;
372
373         pmu_data = bcm590xx_parse_dt_reg_data(pdev,
374                                               &bcm590xx_reg_matches);
375
376         pmu = devm_kzalloc(&pdev->dev, sizeof(*pmu), GFP_KERNEL);
377         if (!pmu) {
378                 dev_err(&pdev->dev, "Memory allocation failed for pmu\n");
379                 return -ENOMEM;
380         }
381
382         pmu->mfd = bcm590xx;
383
384         platform_set_drvdata(pdev, pmu);
385
386         pmu->desc = devm_kzalloc(&pdev->dev, BCM590XX_NUM_REGS *
387                         sizeof(struct regulator_desc), GFP_KERNEL);
388         if (!pmu->desc) {
389                 dev_err(&pdev->dev, "Memory alloc fails for desc\n");
390                 return -ENOMEM;
391         }
392
393         pmu->info = devm_kzalloc(&pdev->dev, BCM590XX_NUM_REGS *
394                         sizeof(struct bcm590xx_info *), GFP_KERNEL);
395         if (!pmu->info) {
396                 dev_err(&pdev->dev, "Memory alloc fails for info\n");
397                 return -ENOMEM;
398         }
399
400         info = bcm590xx_regs;
401
402         for (i = 0; i < BCM590XX_NUM_REGS; i++, info++) {
403                 if (pmu_data)
404                         reg_data = pmu_data->bcm590xx_pmu_init_data[i];
405                 else
406                         reg_data = NULL;
407
408                 /* Register the regulators */
409                 pmu->info[i] = info;
410
411                 pmu->desc[i].name = info->name;
412                 pmu->desc[i].supply_name = info->vin_name;
413                 pmu->desc[i].id = i;
414                 pmu->desc[i].volt_table = info->volt_table;
415                 pmu->desc[i].n_voltages = info->n_voltages;
416                 pmu->desc[i].linear_ranges = info->linear_ranges;
417                 pmu->desc[i].n_linear_ranges = info->n_linear_ranges;
418
419                 if ((BCM590XX_REG_IS_LDO(i)) || (BCM590XX_REG_IS_GPLDO(i))) {
420                         pmu->desc[i].ops = &bcm590xx_ops_ldo;
421                         pmu->desc[i].vsel_mask = BCM590XX_LDO_VSEL_MASK;
422                 } else if (BCM590XX_REG_IS_VBUS(i))
423                         pmu->desc[i].ops = &bcm590xx_ops_vbus;
424                 else {
425                         pmu->desc[i].ops = &bcm590xx_ops_dcdc;
426                         pmu->desc[i].vsel_mask = BCM590XX_SR_VSEL_MASK;
427                 }
428
429                 if (BCM590XX_REG_IS_VBUS(i))
430                         pmu->desc[i].enable_mask = BCM590XX_VBUS_ENABLE;
431                 else {
432                         pmu->desc[i].vsel_reg = bcm590xx_get_vsel_register(i);
433                         pmu->desc[i].enable_is_inverted = true;
434                         pmu->desc[i].enable_mask = BCM590XX_REG_ENABLE;
435                 }
436                 pmu->desc[i].enable_reg = bcm590xx_get_enable_register(i);
437                 pmu->desc[i].type = REGULATOR_VOLTAGE;
438                 pmu->desc[i].owner = THIS_MODULE;
439
440                 config.dev = bcm590xx->dev;
441                 config.init_data = reg_data;
442                 config.driver_data = pmu;
443                 if (BCM590XX_REG_IS_GPLDO(i) || BCM590XX_REG_IS_VBUS(i))
444                         config.regmap = bcm590xx->regmap_sec;
445                 else
446                         config.regmap = bcm590xx->regmap_pri;
447
448                 if (bcm590xx_reg_matches)
449                         config.of_node = bcm590xx_reg_matches[i].of_node;
450
451                 rdev = devm_regulator_register(&pdev->dev, &pmu->desc[i],
452                                                &config);
453                 if (IS_ERR(rdev)) {
454                         dev_err(bcm590xx->dev,
455                                 "failed to register %s regulator\n",
456                                 pdev->name);
457                         return PTR_ERR(rdev);
458                 }
459         }
460
461         return 0;
462 }
463
464 static struct platform_driver bcm590xx_regulator_driver = {
465         .driver = {
466                 .name = "bcm590xx-vregs",
467                 .owner = THIS_MODULE,
468         },
469         .probe = bcm590xx_probe,
470 };
471 module_platform_driver(bcm590xx_regulator_driver);
472
473 MODULE_AUTHOR("Matt Porter <mporter@linaro.org>");
474 MODULE_DESCRIPTION("BCM590xx voltage regulator driver");
475 MODULE_LICENSE("GPL v2");
476 MODULE_ALIAS("platform:bcm590xx-vregs");