regulator: max77650: Fix include files
[linux-2.6-microblaze.git] / drivers / regulator / stpmic1_regulator.c
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) STMicroelectronics 2018
3 // Author: Pascal Paillet <p.paillet@st.com> for STMicroelectronics.
4
5 #include <linux/interrupt.h>
6 #include <linux/mfd/stpmic1.h>
7 #include <linux/module.h>
8 #include <linux/of_irq.h>
9 #include <linux/platform_device.h>
10 #include <linux/regmap.h>
11 #include <linux/regulator/driver.h>
12 #include <linux/regulator/machine.h>
13 #include <linux/regulator/of_regulator.h>
14
15 /**
16  * stpmic1 regulator description
17  * @desc: regulator framework description
18  * @mask_reset_reg: mask reset register address
19  * @mask_reset_mask: mask rank and mask reset register mask
20  * @icc_reg: icc register address
21  * @icc_mask: icc register mask
22  */
23 struct stpmic1_regulator_cfg {
24         struct regulator_desc desc;
25         u8 mask_reset_reg;
26         u8 mask_reset_mask;
27         u8 icc_reg;
28         u8 icc_mask;
29 };
30
31 /**
32  * stpmic1 regulator data: this structure is used as driver data
33  * @regul_id: regulator id
34  * @reg_node: DT node of regulator (unused on non-DT platforms)
35  * @cfg: stpmic specific regulator description
36  * @mask_reset: mask_reset bit value
37  * @irq_curlim: current limit interrupt number
38  * @regmap: point to parent regmap structure
39  */
40 struct stpmic1_regulator {
41         unsigned int regul_id;
42         struct device_node *reg_node;
43         const struct stpmic1_regulator_cfg *cfg;
44         u8 mask_reset;
45         int irq_curlim;
46         struct regmap *regmap;
47 };
48
49 static int stpmic1_set_mode(struct regulator_dev *rdev, unsigned int mode);
50 static unsigned int stpmic1_get_mode(struct regulator_dev *rdev);
51 static int stpmic1_set_icc(struct regulator_dev *rdev);
52 static int stpmic1_regulator_parse_dt(void *driver_data);
53 static unsigned int stpmic1_map_mode(unsigned int mode);
54
55 enum {
56         STPMIC1_BUCK1 = 0,
57         STPMIC1_BUCK2 = 1,
58         STPMIC1_BUCK3 = 2,
59         STPMIC1_BUCK4 = 3,
60         STPMIC1_LDO1 = 4,
61         STPMIC1_LDO2 = 5,
62         STPMIC1_LDO3 = 6,
63         STPMIC1_LDO4 = 7,
64         STPMIC1_LDO5 = 8,
65         STPMIC1_LDO6 = 9,
66         STPMIC1_VREF_DDR = 10,
67         STPMIC1_BOOST = 11,
68         STPMIC1_VBUS_OTG = 12,
69         STPMIC1_SW_OUT = 13,
70 };
71
72 /* Enable time worst case is 5000mV/(2250uV/uS) */
73 #define PMIC_ENABLE_TIME_US 2200
74
75 #define STPMIC1_BUCK_MODE_NORMAL 0
76 #define STPMIC1_BUCK_MODE_LP BUCK_HPLP_ENABLE_MASK
77
78 static const struct regulator_linear_range buck1_ranges[] = {
79         REGULATOR_LINEAR_RANGE(600000, 0, 30, 25000),
80         REGULATOR_LINEAR_RANGE(1350000, 31, 63, 0),
81 };
82
83 static const struct regulator_linear_range buck2_ranges[] = {
84         REGULATOR_LINEAR_RANGE(1000000, 0, 17, 0),
85         REGULATOR_LINEAR_RANGE(1050000, 18, 19, 0),
86         REGULATOR_LINEAR_RANGE(1100000, 20, 21, 0),
87         REGULATOR_LINEAR_RANGE(1150000, 22, 23, 0),
88         REGULATOR_LINEAR_RANGE(1200000, 24, 25, 0),
89         REGULATOR_LINEAR_RANGE(1250000, 26, 27, 0),
90         REGULATOR_LINEAR_RANGE(1300000, 28, 29, 0),
91         REGULATOR_LINEAR_RANGE(1350000, 30, 31, 0),
92         REGULATOR_LINEAR_RANGE(1400000, 32, 33, 0),
93         REGULATOR_LINEAR_RANGE(1450000, 34, 35, 0),
94         REGULATOR_LINEAR_RANGE(1500000, 36, 63, 0),
95 };
96
97 static const struct regulator_linear_range buck3_ranges[] = {
98         REGULATOR_LINEAR_RANGE(1000000, 0, 19, 0),
99         REGULATOR_LINEAR_RANGE(1100000, 20, 23, 0),
100         REGULATOR_LINEAR_RANGE(1200000, 24, 27, 0),
101         REGULATOR_LINEAR_RANGE(1300000, 28, 31, 0),
102         REGULATOR_LINEAR_RANGE(1400000, 32, 35, 0),
103         REGULATOR_LINEAR_RANGE(1500000, 36, 55, 100000),
104         REGULATOR_LINEAR_RANGE(3400000, 56, 63, 0),
105 };
106
107 static const struct regulator_linear_range buck4_ranges[] = {
108         REGULATOR_LINEAR_RANGE(600000, 0, 27, 25000),
109         REGULATOR_LINEAR_RANGE(1300000, 28, 29, 0),
110         REGULATOR_LINEAR_RANGE(1350000, 30, 31, 0),
111         REGULATOR_LINEAR_RANGE(1400000, 32, 33, 0),
112         REGULATOR_LINEAR_RANGE(1450000, 34, 35, 0),
113         REGULATOR_LINEAR_RANGE(1500000, 36, 60, 100000),
114         REGULATOR_LINEAR_RANGE(3900000, 61, 63, 0),
115 };
116
117 static const struct regulator_linear_range ldo1_ranges[] = {
118         REGULATOR_LINEAR_RANGE(1700000, 0, 7, 0),
119         REGULATOR_LINEAR_RANGE(1700000, 8, 24, 100000),
120         REGULATOR_LINEAR_RANGE(3300000, 25, 31, 0),
121 };
122
123 static const struct regulator_linear_range ldo2_ranges[] = {
124         REGULATOR_LINEAR_RANGE(1700000, 0, 7, 0),
125         REGULATOR_LINEAR_RANGE(1700000, 8, 24, 100000),
126         REGULATOR_LINEAR_RANGE(3300000, 25, 30, 0),
127 };
128
129 static const struct regulator_linear_range ldo3_ranges[] = {
130         REGULATOR_LINEAR_RANGE(1700000, 0, 7, 0),
131         REGULATOR_LINEAR_RANGE(1700000, 8, 24, 100000),
132         REGULATOR_LINEAR_RANGE(3300000, 25, 30, 0),
133         /* with index 31 LDO3 is in DDR mode */
134         REGULATOR_LINEAR_RANGE(500000, 31, 31, 0),
135 };
136
137 static const struct regulator_linear_range ldo5_ranges[] = {
138         REGULATOR_LINEAR_RANGE(1700000, 0, 7, 0),
139         REGULATOR_LINEAR_RANGE(1700000, 8, 30, 100000),
140         REGULATOR_LINEAR_RANGE(3900000, 31, 31, 0),
141 };
142
143 static const struct regulator_linear_range ldo6_ranges[] = {
144         REGULATOR_LINEAR_RANGE(900000, 0, 24, 100000),
145         REGULATOR_LINEAR_RANGE(3300000, 25, 31, 0),
146 };
147
148 static const struct regulator_ops stpmic1_ldo_ops = {
149         .list_voltage = regulator_list_voltage_linear_range,
150         .map_voltage = regulator_map_voltage_linear_range,
151         .is_enabled = regulator_is_enabled_regmap,
152         .enable = regulator_enable_regmap,
153         .disable = regulator_disable_regmap,
154         .get_voltage_sel = regulator_get_voltage_sel_regmap,
155         .set_voltage_sel = regulator_set_voltage_sel_regmap,
156         .set_pull_down = regulator_set_pull_down_regmap,
157         .set_over_current_protection = stpmic1_set_icc,
158 };
159
160 static const struct regulator_ops stpmic1_ldo3_ops = {
161         .list_voltage = regulator_list_voltage_linear_range,
162         .map_voltage = regulator_map_voltage_iterate,
163         .is_enabled = regulator_is_enabled_regmap,
164         .enable = regulator_enable_regmap,
165         .disable = regulator_disable_regmap,
166         .get_voltage_sel = regulator_get_voltage_sel_regmap,
167         .set_voltage_sel = regulator_set_voltage_sel_regmap,
168         .set_pull_down = regulator_set_pull_down_regmap,
169         .get_bypass = regulator_get_bypass_regmap,
170         .set_bypass = regulator_set_bypass_regmap,
171         .set_over_current_protection = stpmic1_set_icc,
172 };
173
174 static const struct regulator_ops stpmic1_ldo4_fixed_regul_ops = {
175         .is_enabled = regulator_is_enabled_regmap,
176         .enable = regulator_enable_regmap,
177         .disable = regulator_disable_regmap,
178         .set_pull_down = regulator_set_pull_down_regmap,
179         .set_over_current_protection = stpmic1_set_icc,
180 };
181
182 static const struct regulator_ops stpmic1_buck_ops = {
183         .list_voltage = regulator_list_voltage_linear_range,
184         .map_voltage = regulator_map_voltage_linear_range,
185         .is_enabled = regulator_is_enabled_regmap,
186         .enable = regulator_enable_regmap,
187         .disable = regulator_disable_regmap,
188         .get_voltage_sel = regulator_get_voltage_sel_regmap,
189         .set_voltage_sel = regulator_set_voltage_sel_regmap,
190         .set_pull_down = regulator_set_pull_down_regmap,
191         .set_mode = stpmic1_set_mode,
192         .get_mode = stpmic1_get_mode,
193         .set_over_current_protection = stpmic1_set_icc,
194 };
195
196 static const struct regulator_ops stpmic1_vref_ddr_ops = {
197         .is_enabled = regulator_is_enabled_regmap,
198         .enable = regulator_enable_regmap,
199         .disable = regulator_disable_regmap,
200         .set_pull_down = regulator_set_pull_down_regmap,
201 };
202
203 static const struct regulator_ops stpmic1_switch_regul_ops = {
204         .is_enabled = regulator_is_enabled_regmap,
205         .enable = regulator_enable_regmap,
206         .disable = regulator_disable_regmap,
207         .set_over_current_protection = stpmic1_set_icc,
208 };
209
210 #define REG_LDO(ids, base) { \
211         .name = #ids, \
212         .id = STPMIC1_##ids, \
213         .n_voltages = 32, \
214         .ops = &stpmic1_ldo_ops, \
215         .linear_ranges = base ## _ranges, \
216         .n_linear_ranges = ARRAY_SIZE(base ## _ranges), \
217         .type = REGULATOR_VOLTAGE, \
218         .owner = THIS_MODULE, \
219         .vsel_reg = ids##_ACTIVE_CR, \
220         .vsel_mask = LDO_VOLTAGE_MASK, \
221         .enable_reg = ids##_ACTIVE_CR, \
222         .enable_mask = LDO_ENABLE_MASK, \
223         .enable_val = 1, \
224         .disable_val = 0, \
225         .enable_time = PMIC_ENABLE_TIME_US, \
226         .pull_down_reg = ids##_PULL_DOWN_REG, \
227         .pull_down_mask = ids##_PULL_DOWN_MASK, \
228         .supply_name = #base, \
229 }
230
231 #define REG_LDO3(ids, base) { \
232         .name = #ids, \
233         .id = STPMIC1_##ids, \
234         .n_voltages = 32, \
235         .ops = &stpmic1_ldo3_ops, \
236         .linear_ranges = ldo3_ranges, \
237         .n_linear_ranges = ARRAY_SIZE(ldo3_ranges), \
238         .type = REGULATOR_VOLTAGE, \
239         .owner = THIS_MODULE, \
240         .vsel_reg = LDO3_ACTIVE_CR, \
241         .vsel_mask = LDO_VOLTAGE_MASK, \
242         .enable_reg = LDO3_ACTIVE_CR, \
243         .enable_mask = LDO_ENABLE_MASK, \
244         .enable_val = 1, \
245         .disable_val = 0, \
246         .enable_time = PMIC_ENABLE_TIME_US, \
247         .bypass_reg = LDO3_ACTIVE_CR, \
248         .bypass_mask = LDO_BYPASS_MASK, \
249         .bypass_val_on = LDO_BYPASS_MASK, \
250         .bypass_val_off = 0, \
251         .pull_down_reg = ids##_PULL_DOWN_REG, \
252         .pull_down_mask = ids##_PULL_DOWN_MASK, \
253         .supply_name = #base, \
254 }
255
256 #define REG_LDO4(ids, base) { \
257         .name = #ids, \
258         .id = STPMIC1_##ids, \
259         .n_voltages = 1, \
260         .ops = &stpmic1_ldo4_fixed_regul_ops, \
261         .type = REGULATOR_VOLTAGE, \
262         .owner = THIS_MODULE, \
263         .min_uV = 3300000, \
264         .fixed_uV = 3300000, \
265         .enable_reg = LDO4_ACTIVE_CR, \
266         .enable_mask = LDO_ENABLE_MASK, \
267         .enable_val = 1, \
268         .disable_val = 0, \
269         .enable_time = PMIC_ENABLE_TIME_US, \
270         .pull_down_reg = ids##_PULL_DOWN_REG, \
271         .pull_down_mask = ids##_PULL_DOWN_MASK, \
272         .supply_name = #base, \
273 }
274
275 #define REG_BUCK(ids, base) { \
276         .name = #ids, \
277         .id = STPMIC1_##ids, \
278         .ops = &stpmic1_buck_ops, \
279         .n_voltages = 64, \
280         .linear_ranges = base ## _ranges, \
281         .n_linear_ranges = ARRAY_SIZE(base ## _ranges), \
282         .type = REGULATOR_VOLTAGE, \
283         .owner = THIS_MODULE, \
284         .vsel_reg = ids##_ACTIVE_CR, \
285         .vsel_mask = BUCK_VOLTAGE_MASK, \
286         .enable_reg = ids##_ACTIVE_CR, \
287         .enable_mask = BUCK_ENABLE_MASK, \
288         .enable_val = 1, \
289         .disable_val = 0, \
290         .enable_time = PMIC_ENABLE_TIME_US, \
291         .of_map_mode = stpmic1_map_mode, \
292         .pull_down_reg = ids##_PULL_DOWN_REG, \
293         .pull_down_mask = ids##_PULL_DOWN_MASK, \
294         .supply_name = #base, \
295 }
296
297 #define REG_VREF_DDR(ids, base) { \
298         .name = #ids, \
299         .id = STPMIC1_##ids, \
300         .n_voltages = 1, \
301         .ops = &stpmic1_vref_ddr_ops, \
302         .type = REGULATOR_VOLTAGE, \
303         .owner = THIS_MODULE, \
304         .min_uV = 500000, \
305         .fixed_uV = 500000, \
306         .enable_reg = VREF_DDR_ACTIVE_CR, \
307         .enable_mask = BUCK_ENABLE_MASK, \
308         .enable_val = 1, \
309         .disable_val = 0, \
310         .enable_time = PMIC_ENABLE_TIME_US, \
311         .pull_down_reg = ids##_PULL_DOWN_REG, \
312         .pull_down_mask = ids##_PULL_DOWN_MASK, \
313         .supply_name = #base, \
314 }
315
316 #define REG_SWITCH(ids, base, reg, mask, val) { \
317         .name = #ids, \
318         .id = STPMIC1_##ids, \
319         .n_voltages = 1, \
320         .ops = &stpmic1_switch_regul_ops, \
321         .type = REGULATOR_VOLTAGE, \
322         .owner = THIS_MODULE, \
323         .min_uV = 0, \
324         .fixed_uV = 5000000, \
325         .enable_reg = (reg), \
326         .enable_mask = (mask), \
327         .enable_val = (val), \
328         .disable_val = 0, \
329         .enable_time = PMIC_ENABLE_TIME_US, \
330         .supply_name = #base, \
331 }
332
333 static const struct stpmic1_regulator_cfg stpmic1_regulator_cfgs[] = {
334         [STPMIC1_BUCK1] = {
335                 .desc = REG_BUCK(BUCK1, buck1),
336                 .icc_reg = BUCKS_ICCTO_CR,
337                 .icc_mask = BIT(0),
338                 .mask_reset_reg = BUCKS_MASK_RESET_CR,
339                 .mask_reset_mask = BIT(0),
340         },
341         [STPMIC1_BUCK2] = {
342                 .desc = REG_BUCK(BUCK2, buck2),
343                 .icc_reg = BUCKS_ICCTO_CR,
344                 .icc_mask = BIT(1),
345                 .mask_reset_reg = BUCKS_MASK_RESET_CR,
346                 .mask_reset_mask = BIT(1),
347         },
348         [STPMIC1_BUCK3] = {
349                 .desc = REG_BUCK(BUCK3, buck3),
350                 .icc_reg = BUCKS_ICCTO_CR,
351                 .icc_mask = BIT(2),
352                 .mask_reset_reg = BUCKS_MASK_RESET_CR,
353                 .mask_reset_mask = BIT(2),
354         },
355         [STPMIC1_BUCK4] = {
356                 .desc = REG_BUCK(BUCK4, buck4),
357                 .icc_reg = BUCKS_ICCTO_CR,
358                 .icc_mask = BIT(3),
359                 .mask_reset_reg = BUCKS_MASK_RESET_CR,
360                 .mask_reset_mask = BIT(3),
361         },
362         [STPMIC1_LDO1] = {
363                 .desc = REG_LDO(LDO1, ldo1),
364                 .icc_reg = LDOS_ICCTO_CR,
365                 .icc_mask = BIT(0),
366                 .mask_reset_reg = LDOS_MASK_RESET_CR,
367                 .mask_reset_mask = BIT(0),
368         },
369         [STPMIC1_LDO2] = {
370                 .desc = REG_LDO(LDO2, ldo2),
371                 .icc_reg = LDOS_ICCTO_CR,
372                 .icc_mask = BIT(1),
373                 .mask_reset_reg = LDOS_MASK_RESET_CR,
374                 .mask_reset_mask = BIT(1),
375         },
376         [STPMIC1_LDO3] = {
377                 .desc = REG_LDO3(LDO3, ldo3),
378                 .icc_reg = LDOS_ICCTO_CR,
379                 .icc_mask = BIT(2),
380                 .mask_reset_reg = LDOS_MASK_RESET_CR,
381                 .mask_reset_mask = BIT(2),
382         },
383         [STPMIC1_LDO4] = {
384                 .desc = REG_LDO4(LDO4, ldo4),
385                 .icc_reg = LDOS_ICCTO_CR,
386                 .icc_mask = BIT(3),
387                 .mask_reset_reg = LDOS_MASK_RESET_CR,
388                 .mask_reset_mask = BIT(3),
389         },
390         [STPMIC1_LDO5] = {
391                 .desc = REG_LDO(LDO5, ldo5),
392                 .icc_reg = LDOS_ICCTO_CR,
393                 .icc_mask = BIT(4),
394                 .mask_reset_reg = LDOS_MASK_RESET_CR,
395                 .mask_reset_mask = BIT(4),
396         },
397         [STPMIC1_LDO6] = {
398                 .desc = REG_LDO(LDO6, ldo6),
399                 .icc_reg = LDOS_ICCTO_CR,
400                 .icc_mask = BIT(5),
401                 .mask_reset_reg = LDOS_MASK_RESET_CR,
402                 .mask_reset_mask = BIT(5),
403         },
404         [STPMIC1_VREF_DDR] = {
405                 .desc = REG_VREF_DDR(VREF_DDR, vref_ddr),
406                 .mask_reset_reg = LDOS_MASK_RESET_CR,
407                 .mask_reset_mask = BIT(6),
408         },
409         [STPMIC1_BOOST] = {
410                 .desc = REG_SWITCH(BOOST, boost, BST_SW_CR,
411                                    BOOST_ENABLED,
412                                    BOOST_ENABLED),
413                 .icc_reg = BUCKS_ICCTO_CR,
414                 .icc_mask = BIT(6),
415         },
416         [STPMIC1_VBUS_OTG] = {
417                 .desc = REG_SWITCH(VBUS_OTG, pwr_sw1, BST_SW_CR,
418                                    USBSW_OTG_SWITCH_ENABLED,
419                                    USBSW_OTG_SWITCH_ENABLED),
420                 .icc_reg = BUCKS_ICCTO_CR,
421                 .icc_mask = BIT(4),
422         },
423         [STPMIC1_SW_OUT] = {
424                 .desc = REG_SWITCH(SW_OUT, pwr_sw2, BST_SW_CR,
425                                    SWIN_SWOUT_ENABLED,
426                                    SWIN_SWOUT_ENABLED),
427                 .icc_reg = BUCKS_ICCTO_CR,
428                 .icc_mask = BIT(5),
429         },
430 };
431
432 static unsigned int stpmic1_map_mode(unsigned int mode)
433 {
434         switch (mode) {
435         case STPMIC1_BUCK_MODE_NORMAL:
436                 return REGULATOR_MODE_NORMAL;
437         case STPMIC1_BUCK_MODE_LP:
438                 return REGULATOR_MODE_STANDBY;
439         default:
440                 return REGULATOR_MODE_INVALID;
441         }
442 }
443
444 static unsigned int stpmic1_get_mode(struct regulator_dev *rdev)
445 {
446         int value;
447
448         regmap_read(rdev->regmap, rdev->desc->enable_reg, &value);
449
450         if (value & STPMIC1_BUCK_MODE_LP)
451                 return REGULATOR_MODE_STANDBY;
452
453         return REGULATOR_MODE_NORMAL;
454 }
455
456 static int stpmic1_set_mode(struct regulator_dev *rdev, unsigned int mode)
457 {
458         int value;
459
460         switch (mode) {
461         case REGULATOR_MODE_NORMAL:
462                 value = STPMIC1_BUCK_MODE_NORMAL;
463                 break;
464         case REGULATOR_MODE_STANDBY:
465                 value = STPMIC1_BUCK_MODE_LP;
466                 break;
467         default:
468                 return -EINVAL;
469         }
470
471         return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
472                                   STPMIC1_BUCK_MODE_LP, value);
473 }
474
475 static int stpmic1_set_icc(struct regulator_dev *rdev)
476 {
477         struct stpmic1_regulator *regul = rdev_get_drvdata(rdev);
478
479         /* enable switch off in case of over current */
480         return regmap_update_bits(regul->regmap, regul->cfg->icc_reg,
481                                   regul->cfg->icc_mask, regul->cfg->icc_mask);
482 }
483
484 static irqreturn_t stpmic1_curlim_irq_handler(int irq, void *data)
485 {
486         struct regulator_dev *rdev = (struct regulator_dev *)data;
487
488         regulator_lock(rdev);
489
490         /* Send an overcurrent notification */
491         regulator_notifier_call_chain(rdev,
492                                       REGULATOR_EVENT_OVER_CURRENT,
493                                       NULL);
494
495         regulator_unlock(rdev);
496
497         return IRQ_HANDLED;
498 }
499
500 static int stpmic1_regulator_init(struct platform_device *pdev,
501                                   struct regulator_dev *rdev)
502 {
503         struct stpmic1_regulator *regul = rdev_get_drvdata(rdev);
504         int ret = 0;
505
506         /* set mask reset */
507         if (regul->mask_reset && regul->cfg->mask_reset_reg != 0) {
508                 ret = regmap_update_bits(regul->regmap,
509                                          regul->cfg->mask_reset_reg,
510                                          regul->cfg->mask_reset_mask,
511                                          regul->cfg->mask_reset_mask);
512                 if (ret) {
513                         dev_err(&pdev->dev, "set mask reset failed\n");
514                         return ret;
515                 }
516         }
517
518         /* setup an irq handler for over-current detection */
519         if (regul->irq_curlim > 0) {
520                 ret = devm_request_threaded_irq(&pdev->dev,
521                                                 regul->irq_curlim, NULL,
522                                                 stpmic1_curlim_irq_handler,
523                                                 IRQF_ONESHOT | IRQF_SHARED,
524                                                 pdev->name, rdev);
525                 if (ret) {
526                         dev_err(&pdev->dev, "Request IRQ failed\n");
527                         return ret;
528                 }
529         }
530         return 0;
531 }
532
533 #define MATCH(_name, _id) \
534         [STPMIC1_##_id] = { \
535                 .name = #_name, \
536                 .desc = &stpmic1_regulator_cfgs[STPMIC1_##_id].desc, \
537         }
538
539 static struct of_regulator_match stpmic1_regulators_matches[] = {
540         MATCH(buck1, BUCK1),
541         MATCH(buck2, BUCK2),
542         MATCH(buck3, BUCK3),
543         MATCH(buck4, BUCK4),
544         MATCH(ldo1, LDO1),
545         MATCH(ldo2, LDO2),
546         MATCH(ldo3, LDO3),
547         MATCH(ldo4, LDO4),
548         MATCH(ldo5, LDO5),
549         MATCH(ldo6, LDO6),
550         MATCH(vref_ddr, VREF_DDR),
551         MATCH(boost, BOOST),
552         MATCH(pwr_sw1, VBUS_OTG),
553         MATCH(pwr_sw2, SW_OUT),
554 };
555
556 static int stpmic1_regulator_parse_dt(void *driver_data)
557 {
558         struct stpmic1_regulator *regul =
559                 (struct stpmic1_regulator *)driver_data;
560
561         if (!regul)
562                 return -EINVAL;
563
564         if (of_get_property(regul->reg_node, "st,mask-reset", NULL))
565                 regul->mask_reset = 1;
566
567         regul->irq_curlim = of_irq_get(regul->reg_node, 0);
568
569         return 0;
570 }
571
572 static struct
573 regulator_dev *stpmic1_regulator_register(struct platform_device *pdev, int id,
574                                           struct regulator_init_data *init_data,
575                                           struct stpmic1_regulator *regul)
576 {
577         struct stpmic1 *pmic_dev = dev_get_drvdata(pdev->dev.parent);
578         struct regulator_dev *rdev;
579         struct regulator_config config = {};
580
581         config.dev = &pdev->dev;
582         config.init_data = init_data;
583         config.of_node = stpmic1_regulators_matches[id].of_node;
584         config.regmap = pmic_dev->regmap;
585         config.driver_data = regul;
586
587         regul->regul_id = id;
588         regul->reg_node = config.of_node;
589         regul->cfg = &stpmic1_regulator_cfgs[id];
590         regul->regmap = pmic_dev->regmap;
591
592         rdev = devm_regulator_register(&pdev->dev, &regul->cfg->desc, &config);
593         if (IS_ERR(rdev)) {
594                 dev_err(&pdev->dev, "failed to register %s regulator\n",
595                         regul->cfg->desc.name);
596         }
597
598         return rdev;
599 }
600
601 static int stpmic1_regulator_probe(struct platform_device *pdev)
602 {
603         struct regulator_dev *rdev;
604         struct stpmic1_regulator *regul;
605         struct regulator_init_data *init_data;
606         struct device_node *np;
607         int i, ret;
608
609         np = pdev->dev.of_node;
610
611         ret = of_regulator_match(&pdev->dev, np,
612                                  stpmic1_regulators_matches,
613                                  ARRAY_SIZE(stpmic1_regulators_matches));
614         if (ret < 0) {
615                 dev_err(&pdev->dev,
616                         "Error in PMIC regulator device tree node");
617                 return ret;
618         }
619
620         regul = devm_kzalloc(&pdev->dev, ARRAY_SIZE(stpmic1_regulator_cfgs) *
621                              sizeof(struct stpmic1_regulator),
622                              GFP_KERNEL);
623         if (!regul)
624                 return -ENOMEM;
625
626         for (i = 0; i < ARRAY_SIZE(stpmic1_regulator_cfgs); i++) {
627                 /* Parse DT & find regulators to register */
628                 init_data = stpmic1_regulators_matches[i].init_data;
629                 if (init_data)
630                         init_data->regulator_init = &stpmic1_regulator_parse_dt;
631
632                 rdev = stpmic1_regulator_register(pdev, i, init_data, regul);
633                 if (IS_ERR(rdev))
634                         return PTR_ERR(rdev);
635
636                 ret = stpmic1_regulator_init(pdev, rdev);
637                 if (ret) {
638                         dev_err(&pdev->dev,
639                                 "failed to initialize regulator %d\n", ret);
640                         return ret;
641                 }
642
643                 regul++;
644         }
645
646         dev_dbg(&pdev->dev, "stpmic1_regulator driver probed\n");
647
648         return 0;
649 }
650
651 static const struct of_device_id of_pmic_regulator_match[] = {
652         { .compatible = "st,stpmic1-regulators" },
653         { },
654 };
655
656 MODULE_DEVICE_TABLE(of, of_pmic_regulator_match);
657
658 static struct platform_driver stpmic1_regulator_driver = {
659         .driver = {
660                 .name = "stpmic1-regulator",
661                 .of_match_table = of_match_ptr(of_pmic_regulator_match),
662         },
663         .probe = stpmic1_regulator_probe,
664 };
665
666 module_platform_driver(stpmic1_regulator_driver);
667
668 MODULE_DESCRIPTION("STPMIC1 PMIC voltage regulator driver");
669 MODULE_AUTHOR("Pascal Paillet <p.paillet@st.com>");
670 MODULE_LICENSE("GPL v2");