Merge tag 'usb-6.6-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/usb
[platform/kernel/linux-starfive.git] / drivers / regulator / da9063-regulator.c
1 // SPDX-License-Identifier: GPL-2.0+
2 //
3 // Regulator driver for DA9063 PMIC series
4 //
5 // Copyright 2012 Dialog Semiconductors Ltd.
6 // Copyright 2013 Philipp Zabel, Pengutronix
7 //
8 // Author: Krystian Garbaciak <krystian.garbaciak@diasemi.com>
9
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/err.h>
14 #include <linux/slab.h>
15 #include <linux/of.h>
16 #include <linux/platform_device.h>
17 #include <linux/regmap.h>
18 #include <linux/regulator/driver.h>
19 #include <linux/regulator/machine.h>
20 #include <linux/regulator/of_regulator.h>
21 #include <linux/mfd/da9063/core.h>
22 #include <linux/mfd/da9063/registers.h>
23
24
25 /* Definition for registering regmap bit fields using a mask */
26 #define BFIELD(_reg, _mask) \
27         REG_FIELD(_reg, __builtin_ffs((int)_mask) - 1, \
28                 sizeof(unsigned int) * 8 - __builtin_clz((_mask)) - 1)
29
30 /* DA9063 and DA9063L regulator IDs */
31 enum {
32         /* BUCKs */
33         DA9063_ID_BCORE1,
34         DA9063_ID_BCORE2,
35         DA9063_ID_BPRO,
36         DA9063_ID_BMEM,
37         DA9063_ID_BIO,
38         DA9063_ID_BPERI,
39
40         /* BCORE1 and BCORE2 in merged mode */
41         DA9063_ID_BCORES_MERGED,
42         /* BMEM and BIO in merged mode */
43         DA9063_ID_BMEM_BIO_MERGED,
44         /* When two BUCKs are merged, they cannot be reused separately */
45
46         /* LDOs on both DA9063 and DA9063L */
47         DA9063_ID_LDO3,
48         DA9063_ID_LDO7,
49         DA9063_ID_LDO8,
50         DA9063_ID_LDO9,
51         DA9063_ID_LDO11,
52
53         /* DA9063-only LDOs */
54         DA9063_ID_LDO1,
55         DA9063_ID_LDO2,
56         DA9063_ID_LDO4,
57         DA9063_ID_LDO5,
58         DA9063_ID_LDO6,
59         DA9063_ID_LDO10,
60 };
61
62 /* Old regulator platform data */
63 struct da9063_regulator_data {
64         int                             id;
65         struct regulator_init_data      *initdata;
66 };
67
68 struct da9063_regulators_pdata {
69         unsigned int                    n_regulators;
70         struct da9063_regulator_data    *regulator_data;
71 };
72
73 /* Regulator capabilities and registers description */
74 struct da9063_regulator_info {
75         struct regulator_desc desc;
76
77         /* DA9063 main register fields */
78         struct reg_field mode;          /* buck mode of operation */
79         struct reg_field suspend;
80         struct reg_field sleep;
81         struct reg_field suspend_sleep;
82         unsigned int suspend_vsel_reg;
83
84         /* DA9063 event detection bit */
85         struct reg_field oc_event;
86
87         /* DA9063 voltage monitor bit */
88         struct reg_field vmon;
89 };
90
91 /* Macros for LDO */
92 #define DA9063_LDO(chip, regl_name, min_mV, step_mV, max_mV) \
93         .desc.id = chip##_ID_##regl_name, \
94         .desc.name = __stringify(chip##_##regl_name), \
95         .desc.ops = &da9063_ldo_ops, \
96         .desc.min_uV = (min_mV) * 1000, \
97         .desc.uV_step = (step_mV) * 1000, \
98         .desc.n_voltages = (((max_mV) - (min_mV))/(step_mV) + 1 \
99                 + (DA9063_V##regl_name##_BIAS)), \
100         .desc.enable_reg = DA9063_REG_##regl_name##_CONT, \
101         .desc.enable_mask = DA9063_LDO_EN, \
102         .desc.vsel_reg = DA9063_REG_V##regl_name##_A, \
103         .desc.vsel_mask = DA9063_V##regl_name##_MASK, \
104         .desc.linear_min_sel = DA9063_V##regl_name##_BIAS, \
105         .sleep = BFIELD(DA9063_REG_V##regl_name##_A, DA9063_LDO_SL), \
106         .suspend = BFIELD(DA9063_REG_##regl_name##_CONT, DA9063_LDO_CONF), \
107         .suspend_sleep = BFIELD(DA9063_REG_V##regl_name##_B, DA9063_LDO_SL), \
108         .suspend_vsel_reg = DA9063_REG_V##regl_name##_B
109
110 /* Macros for voltage DC/DC converters (BUCKs) */
111 #define DA9063_BUCK(chip, regl_name, min_mV, step_mV, max_mV, limits_array, \
112                     creg, cmask) \
113         .desc.id = chip##_ID_##regl_name, \
114         .desc.name = __stringify(chip##_##regl_name), \
115         .desc.ops = &da9063_buck_ops, \
116         .desc.min_uV = (min_mV) * 1000, \
117         .desc.uV_step = (step_mV) * 1000, \
118         .desc.n_voltages = ((max_mV) - (min_mV))/(step_mV) + 1, \
119         .desc.csel_reg = (creg), \
120         .desc.csel_mask = (cmask), \
121         .desc.curr_table = limits_array, \
122         .desc.n_current_limits = ARRAY_SIZE(limits_array)
123
124 #define DA9063_BUCK_COMMON_FIELDS(regl_name) \
125         .desc.enable_reg = DA9063_REG_##regl_name##_CONT, \
126         .desc.enable_mask = DA9063_BUCK_EN, \
127         .desc.vsel_reg = DA9063_REG_V##regl_name##_A, \
128         .desc.vsel_mask = DA9063_VBUCK_MASK, \
129         .desc.linear_min_sel = DA9063_VBUCK_BIAS, \
130         .sleep = BFIELD(DA9063_REG_V##regl_name##_A, DA9063_BUCK_SL), \
131         .suspend = BFIELD(DA9063_REG_##regl_name##_CONT, DA9063_BUCK_CONF), \
132         .suspend_sleep = BFIELD(DA9063_REG_V##regl_name##_B, DA9063_BUCK_SL), \
133         .suspend_vsel_reg = DA9063_REG_V##regl_name##_B, \
134         .mode = BFIELD(DA9063_REG_##regl_name##_CFG, DA9063_BUCK_MODE_MASK)
135
136 /* Defines asignment of regulators info table to chip model */
137 struct da9063_dev_model {
138         const struct da9063_regulator_info      *regulator_info;
139         unsigned int                            n_regulators;
140         enum da9063_type                        type;
141 };
142
143 /* Single regulator settings */
144 struct da9063_regulator {
145         struct regulator_desc                   desc;
146         struct regulator_dev                    *rdev;
147         struct da9063                           *hw;
148         const struct da9063_regulator_info      *info;
149
150         struct regmap_field                     *mode;
151         struct regmap_field                     *suspend;
152         struct regmap_field                     *sleep;
153         struct regmap_field                     *suspend_sleep;
154         struct regmap_field                     *vmon;
155 };
156
157 /* Encapsulates all information for the regulators driver */
158 struct da9063_regulators {
159         unsigned int                            n_regulators;
160         /* Array size to be defined during init. Keep at end. */
161         struct da9063_regulator                 regulator[];
162 };
163
164 /* BUCK modes for DA9063 */
165 enum {
166         BUCK_MODE_MANUAL,       /* 0 */
167         BUCK_MODE_SLEEP,        /* 1 */
168         BUCK_MODE_SYNC,         /* 2 */
169         BUCK_MODE_AUTO          /* 3 */
170 };
171
172 /* Regulator operations */
173
174 /*
175  * Current limits array (in uA) for BCORE1, BCORE2, BPRO.
176  * Entry indexes corresponds to register values.
177  */
178 static const unsigned int da9063_buck_a_limits[] = {
179          500000,  600000,  700000,  800000,  900000, 1000000, 1100000, 1200000,
180         1300000, 1400000, 1500000, 1600000, 1700000, 1800000, 1900000, 2000000
181 };
182
183 /*
184  * Current limits array (in uA) for BMEM, BIO, BPERI.
185  * Entry indexes corresponds to register values.
186  */
187 static const unsigned int da9063_buck_b_limits[] = {
188         1500000, 1600000, 1700000, 1800000, 1900000, 2000000, 2100000, 2200000,
189         2300000, 2400000, 2500000, 2600000, 2700000, 2800000, 2900000, 3000000
190 };
191
192 /*
193  * Current limits array (in uA) for merged BCORE1 and BCORE2.
194  * Entry indexes corresponds to register values.
195  */
196 static const unsigned int da9063_bcores_merged_limits[] = {
197         1000000, 1200000, 1400000, 1600000, 1800000, 2000000, 2200000, 2400000,
198         2600000, 2800000, 3000000, 3200000, 3400000, 3600000, 3800000, 4000000
199 };
200
201 /*
202  * Current limits array (in uA) for merged BMEM and BIO.
203  * Entry indexes corresponds to register values.
204  */
205 static const unsigned int da9063_bmem_bio_merged_limits[] = {
206         3000000, 3200000, 3400000, 3600000, 3800000, 4000000, 4200000, 4400000,
207         4600000, 4800000, 5000000, 5200000, 5400000, 5600000, 5800000, 6000000
208 };
209
210 static int da9063_set_xvp(struct regulator_dev *rdev, int lim_uV, int severity, bool enable)
211 {
212         struct da9063_regulator *regl = rdev_get_drvdata(rdev);
213         struct device *dev = regl->hw->dev;
214
215         dev_dbg(dev, "%s: lim: %d, sev: %d, en: %d\n", regl->desc.name, lim_uV, severity, enable);
216
217         /*
218          * only support enable and disable.
219          * the da9063 offers a GPIO (GP_FB2) which is unasserted if an XV happens.
220          * therefore ignore severity here, as there might be handlers in hardware.
221          */
222         if (lim_uV)
223                 return -EINVAL;
224
225         return regmap_field_write(regl->vmon, enable ? 1 : 0);
226 }
227
228 static int da9063_buck_set_mode(struct regulator_dev *rdev, unsigned int mode)
229 {
230         struct da9063_regulator *regl = rdev_get_drvdata(rdev);
231         unsigned int val;
232
233         switch (mode) {
234         case REGULATOR_MODE_FAST:
235                 val = BUCK_MODE_SYNC;
236                 break;
237         case REGULATOR_MODE_NORMAL:
238                 val = BUCK_MODE_AUTO;
239                 break;
240         case REGULATOR_MODE_STANDBY:
241                 val = BUCK_MODE_SLEEP;
242                 break;
243         default:
244                 return -EINVAL;
245         }
246
247         return regmap_field_write(regl->mode, val);
248 }
249
250 /*
251  * Bucks use single mode register field for normal operation
252  * and suspend state.
253  * There are 3 modes to map to: FAST, NORMAL, and STANDBY.
254  */
255
256 static unsigned int da9063_buck_get_mode(struct regulator_dev *rdev)
257 {
258         struct da9063_regulator *regl = rdev_get_drvdata(rdev);
259         unsigned int val;
260         int ret;
261
262         ret = regmap_field_read(regl->mode, &val);
263         if (ret < 0)
264                 return ret;
265
266         switch (val) {
267         default:
268         case BUCK_MODE_MANUAL:
269                 /* Sleep flag bit decides the mode */
270                 break;
271         case BUCK_MODE_SLEEP:
272                 return REGULATOR_MODE_STANDBY;
273         case BUCK_MODE_SYNC:
274                 return REGULATOR_MODE_FAST;
275         case BUCK_MODE_AUTO:
276                 return REGULATOR_MODE_NORMAL;
277         }
278
279         ret = regmap_field_read(regl->sleep, &val);
280         if (ret < 0)
281                 return 0;
282
283         if (val)
284                 return REGULATOR_MODE_STANDBY;
285         else
286                 return REGULATOR_MODE_FAST;
287 }
288
289 /*
290  * LDOs use sleep flags - one for normal and one for suspend state.
291  * There are 2 modes to map to: NORMAL and STANDBY (sleep) for each state.
292  */
293
294 static int da9063_ldo_set_mode(struct regulator_dev *rdev, unsigned int mode)
295 {
296         struct da9063_regulator *regl = rdev_get_drvdata(rdev);
297         unsigned int val;
298
299         switch (mode) {
300         case REGULATOR_MODE_NORMAL:
301                 val = 0;
302                 break;
303         case REGULATOR_MODE_STANDBY:
304                 val = 1;
305                 break;
306         default:
307                 return -EINVAL;
308         }
309
310         return regmap_field_write(regl->sleep, val);
311 }
312
313 static unsigned int da9063_ldo_get_mode(struct regulator_dev *rdev)
314 {
315         struct da9063_regulator *regl = rdev_get_drvdata(rdev);
316         int ret, val;
317
318         ret = regmap_field_read(regl->sleep, &val);
319         if (ret < 0)
320                 return 0;
321
322         if (val)
323                 return REGULATOR_MODE_STANDBY;
324         else
325                 return REGULATOR_MODE_NORMAL;
326 }
327
328 static int da9063_buck_get_status(struct regulator_dev *rdev)
329 {
330         int ret = regulator_is_enabled_regmap(rdev);
331
332         if (ret == 0) {
333                 ret = REGULATOR_STATUS_OFF;
334         } else if (ret > 0) {
335                 ret = da9063_buck_get_mode(rdev);
336                 if (ret > 0)
337                         ret = regulator_mode_to_status(ret);
338                 else if (ret == 0)
339                         ret = -EIO;
340         }
341
342         return ret;
343 }
344
345 static int da9063_ldo_get_status(struct regulator_dev *rdev)
346 {
347         int ret = regulator_is_enabled_regmap(rdev);
348
349         if (ret == 0) {
350                 ret = REGULATOR_STATUS_OFF;
351         } else if (ret > 0) {
352                 ret = da9063_ldo_get_mode(rdev);
353                 if (ret > 0)
354                         ret = regulator_mode_to_status(ret);
355                 else if (ret == 0)
356                         ret = -EIO;
357         }
358
359         return ret;
360 }
361
362 static int da9063_set_suspend_voltage(struct regulator_dev *rdev, int uV)
363 {
364         struct da9063_regulator *regl = rdev_get_drvdata(rdev);
365         const struct da9063_regulator_info *rinfo = regl->info;
366         int ret, sel;
367
368         sel = regulator_map_voltage_linear(rdev, uV, uV);
369         if (sel < 0)
370                 return sel;
371
372         sel <<= ffs(rdev->desc->vsel_mask) - 1;
373
374         ret = regmap_update_bits(regl->hw->regmap, rinfo->suspend_vsel_reg,
375                                  rdev->desc->vsel_mask, sel);
376
377         return ret;
378 }
379
380 static int da9063_suspend_enable(struct regulator_dev *rdev)
381 {
382         struct da9063_regulator *regl = rdev_get_drvdata(rdev);
383
384         return regmap_field_write(regl->suspend, 1);
385 }
386
387 static int da9063_suspend_disable(struct regulator_dev *rdev)
388 {
389         struct da9063_regulator *regl = rdev_get_drvdata(rdev);
390
391         return regmap_field_write(regl->suspend, 0);
392 }
393
394 static int da9063_buck_set_suspend_mode(struct regulator_dev *rdev,
395                                 unsigned int mode)
396 {
397         struct da9063_regulator *regl = rdev_get_drvdata(rdev);
398         int val;
399
400         switch (mode) {
401         case REGULATOR_MODE_FAST:
402                 val = BUCK_MODE_SYNC;
403                 break;
404         case REGULATOR_MODE_NORMAL:
405                 val = BUCK_MODE_AUTO;
406                 break;
407         case REGULATOR_MODE_STANDBY:
408                 val = BUCK_MODE_SLEEP;
409                 break;
410         default:
411                 return -EINVAL;
412         }
413
414         return regmap_field_write(regl->mode, val);
415 }
416
417 static int da9063_ldo_set_suspend_mode(struct regulator_dev *rdev,
418                                 unsigned int mode)
419 {
420         struct da9063_regulator *regl = rdev_get_drvdata(rdev);
421         unsigned int val;
422
423         switch (mode) {
424         case REGULATOR_MODE_NORMAL:
425                 val = 0;
426                 break;
427         case REGULATOR_MODE_STANDBY:
428                 val = 1;
429                 break;
430         default:
431                 return -EINVAL;
432         }
433
434         return regmap_field_write(regl->suspend_sleep, val);
435 }
436
437 static unsigned int da9063_get_overdrive_mask(const struct regulator_desc *desc)
438 {
439         switch (desc->id) {
440         case DA9063_ID_BCORES_MERGED:
441         case DA9063_ID_BCORE1:
442                 return DA9063_BCORE1_OD;
443         case DA9063_ID_BCORE2:
444                 return DA9063_BCORE2_OD;
445         case DA9063_ID_BPRO:
446                 return DA9063_BPRO_OD;
447         default:
448                 return 0;
449         }
450 }
451
452 static int da9063_buck_set_limit_set_overdrive(struct regulator_dev *rdev,
453                                                int min_uA, int max_uA,
454                                                unsigned int overdrive_mask)
455 {
456         /*
457          * When enabling overdrive, do it before changing the current limit to
458          * ensure sufficient supply throughout the switch.
459          */
460         struct da9063_regulator *regl = rdev_get_drvdata(rdev);
461         int ret;
462         unsigned int orig_overdrive;
463
464         ret = regmap_read(regl->hw->regmap, DA9063_REG_CONFIG_H,
465                           &orig_overdrive);
466         if (ret < 0)
467                 return ret;
468         orig_overdrive &= overdrive_mask;
469
470         if (orig_overdrive == 0) {
471                 ret = regmap_set_bits(regl->hw->regmap, DA9063_REG_CONFIG_H,
472                                 overdrive_mask);
473                 if (ret < 0)
474                         return ret;
475         }
476
477         ret = regulator_set_current_limit_regmap(rdev, min_uA / 2, max_uA / 2);
478         if (ret < 0 && orig_overdrive == 0)
479                 /*
480                  * regulator_set_current_limit_regmap may have rejected the
481                  * change because of unusable min_uA and/or max_uA inputs.
482                  * Attempt to restore original overdrive state, ignore failure-
483                  * on-failure.
484                  */
485                 regmap_clear_bits(regl->hw->regmap, DA9063_REG_CONFIG_H,
486                                   overdrive_mask);
487
488         return ret;
489 }
490
491 static int da9063_buck_set_limit_clear_overdrive(struct regulator_dev *rdev,
492                                                  int min_uA, int max_uA,
493                                                  unsigned int overdrive_mask)
494 {
495         /*
496          * When disabling overdrive, do it after changing the current limit to
497          * ensure sufficient supply throughout the switch.
498          */
499         struct da9063_regulator *regl = rdev_get_drvdata(rdev);
500         int ret, orig_limit;
501
502         ret = regmap_read(rdev->regmap, rdev->desc->csel_reg, &orig_limit);
503         if (ret < 0)
504                 return ret;
505
506         ret = regulator_set_current_limit_regmap(rdev, min_uA, max_uA);
507         if (ret < 0)
508                 return ret;
509
510         ret = regmap_clear_bits(regl->hw->regmap, DA9063_REG_CONFIG_H,
511                                 overdrive_mask);
512         if (ret < 0)
513                 /*
514                  * Attempt to restore original current limit, ignore failure-
515                  * on-failure.
516                  */
517                 regmap_write(rdev->regmap, rdev->desc->csel_reg, orig_limit);
518
519         return ret;
520 }
521
522 static int da9063_buck_set_current_limit(struct regulator_dev *rdev,
523                                          int min_uA, int max_uA)
524 {
525         unsigned int overdrive_mask, n_currents;
526
527         overdrive_mask = da9063_get_overdrive_mask(rdev->desc);
528         if (overdrive_mask) {
529                 n_currents = rdev->desc->n_current_limits;
530                 if (n_currents == 0)
531                         return -EINVAL;
532
533                 if (max_uA > rdev->desc->curr_table[n_currents - 1])
534                         return da9063_buck_set_limit_set_overdrive(rdev, min_uA,
535                                                                    max_uA,
536                                                                    overdrive_mask);
537
538                 return da9063_buck_set_limit_clear_overdrive(rdev, min_uA,
539                                                              max_uA,
540                                                              overdrive_mask);
541         }
542         return regulator_set_current_limit_regmap(rdev, min_uA, max_uA);
543 }
544
545 static int da9063_buck_get_current_limit(struct regulator_dev *rdev)
546 {
547         struct da9063_regulator *regl = rdev_get_drvdata(rdev);
548         int val, ret, limit;
549         unsigned int mask;
550
551         limit = regulator_get_current_limit_regmap(rdev);
552         if (limit < 0)
553                 return limit;
554         mask = da9063_get_overdrive_mask(rdev->desc);
555         if (mask) {
556                 ret = regmap_read(regl->hw->regmap, DA9063_REG_CONFIG_H, &val);
557                 if (ret < 0)
558                         return ret;
559                 if (val & mask)
560                         limit *= 2;
561         }
562         return limit;
563 }
564
565 static const struct regulator_ops da9063_buck_ops = {
566         .enable                         = regulator_enable_regmap,
567         .disable                        = regulator_disable_regmap,
568         .is_enabled                     = regulator_is_enabled_regmap,
569         .get_voltage_sel                = regulator_get_voltage_sel_regmap,
570         .set_voltage_sel                = regulator_set_voltage_sel_regmap,
571         .list_voltage                   = regulator_list_voltage_linear,
572         .set_current_limit              = da9063_buck_set_current_limit,
573         .get_current_limit              = da9063_buck_get_current_limit,
574         .set_mode                       = da9063_buck_set_mode,
575         .get_mode                       = da9063_buck_get_mode,
576         .get_status                     = da9063_buck_get_status,
577         .set_suspend_voltage            = da9063_set_suspend_voltage,
578         .set_suspend_enable             = da9063_suspend_enable,
579         .set_suspend_disable            = da9063_suspend_disable,
580         .set_suspend_mode               = da9063_buck_set_suspend_mode,
581         .set_over_voltage_protection    = da9063_set_xvp,
582         .set_under_voltage_protection   = da9063_set_xvp,
583 };
584
585 static const struct regulator_ops da9063_ldo_ops = {
586         .enable                         = regulator_enable_regmap,
587         .disable                        = regulator_disable_regmap,
588         .is_enabled                     = regulator_is_enabled_regmap,
589         .get_voltage_sel                = regulator_get_voltage_sel_regmap,
590         .set_voltage_sel                = regulator_set_voltage_sel_regmap,
591         .list_voltage                   = regulator_list_voltage_linear,
592         .set_mode                       = da9063_ldo_set_mode,
593         .get_mode                       = da9063_ldo_get_mode,
594         .get_status                     = da9063_ldo_get_status,
595         .set_suspend_voltage            = da9063_set_suspend_voltage,
596         .set_suspend_enable             = da9063_suspend_enable,
597         .set_suspend_disable            = da9063_suspend_disable,
598         .set_suspend_mode               = da9063_ldo_set_suspend_mode,
599         .set_over_voltage_protection    = da9063_set_xvp,
600         .set_under_voltage_protection   = da9063_set_xvp,
601 };
602
603 /* Info of regulators for DA9063 */
604 static const struct da9063_regulator_info da9063_regulator_info[] = {
605         {
606                 DA9063_BUCK(DA9063, BCORE1, 300, 10, 1570,
607                             da9063_buck_a_limits,
608                             DA9063_REG_BUCK_ILIM_C, DA9063_BCORE1_ILIM_MASK),
609                 DA9063_BUCK_COMMON_FIELDS(BCORE1),
610                 .vmon = BFIELD(DA9063_BB_REG_MON_REG_4, DA9063_BCORE1_MON_EN),
611         },
612         {
613                 DA9063_BUCK(DA9063, BCORE2, 300, 10, 1570,
614                             da9063_buck_a_limits,
615                             DA9063_REG_BUCK_ILIM_C, DA9063_BCORE2_ILIM_MASK),
616                 DA9063_BUCK_COMMON_FIELDS(BCORE2),
617                 .vmon = BFIELD(DA9063_BB_REG_MON_REG_4, DA9063_BCORE2_MON_EN),
618         },
619         {
620                 DA9063_BUCK(DA9063, BPRO, 530, 10, 1800,
621                             da9063_buck_a_limits,
622                             DA9063_REG_BUCK_ILIM_B, DA9063_BPRO_ILIM_MASK),
623                 DA9063_BUCK_COMMON_FIELDS(BPRO),
624                 .vmon = BFIELD(DA9063_BB_REG_MON_REG_4, DA9063_BPRO_MON_EN),
625         },
626         {
627                 DA9063_BUCK(DA9063, BMEM, 800, 20, 3340,
628                             da9063_buck_b_limits,
629                             DA9063_REG_BUCK_ILIM_A, DA9063_BMEM_ILIM_MASK),
630                 DA9063_BUCK_COMMON_FIELDS(BMEM),
631                 .vmon = BFIELD(DA9063_BB_REG_MON_REG_4, DA9063_BMEM_MON_EN),
632         },
633         {
634                 DA9063_BUCK(DA9063, BIO, 800, 20, 3340,
635                             da9063_buck_b_limits,
636                             DA9063_REG_BUCK_ILIM_A, DA9063_BIO_ILIM_MASK),
637                 DA9063_BUCK_COMMON_FIELDS(BIO),
638                 .vmon = BFIELD(DA9063_BB_REG_MON_REG_4, DA9063_BIO_MON_EN),
639         },
640         {
641                 DA9063_BUCK(DA9063, BPERI, 800, 20, 3340,
642                             da9063_buck_b_limits,
643                             DA9063_REG_BUCK_ILIM_B, DA9063_BPERI_ILIM_MASK),
644                 DA9063_BUCK_COMMON_FIELDS(BPERI),
645                 .vmon = BFIELD(DA9063_BB_REG_MON_REG_4, DA9063_BPERI_MON_EN),
646         },
647         {
648                 DA9063_BUCK(DA9063, BCORES_MERGED, 300, 10, 1570,
649                             da9063_bcores_merged_limits,
650                             DA9063_REG_BUCK_ILIM_C, DA9063_BCORE1_ILIM_MASK),
651                 /* BCORES_MERGED uses the same register fields as BCORE1 */
652                 DA9063_BUCK_COMMON_FIELDS(BCORE1),
653                 .vmon = BFIELD(DA9063_BB_REG_MON_REG_4, DA9063_BCORE1_MON_EN),
654         },
655         {
656                 DA9063_BUCK(DA9063, BMEM_BIO_MERGED, 800, 20, 3340,
657                             da9063_bmem_bio_merged_limits,
658                             DA9063_REG_BUCK_ILIM_A, DA9063_BMEM_ILIM_MASK),
659                 /* BMEM_BIO_MERGED uses the same register fields as BMEM */
660                 DA9063_BUCK_COMMON_FIELDS(BMEM),
661                 .vmon = BFIELD(DA9063_BB_REG_MON_REG_4, DA9063_BMEM_MON_EN),
662         },
663         {
664                 DA9063_LDO(DA9063, LDO3, 900, 20, 3440),
665                 .oc_event = BFIELD(DA9063_REG_STATUS_D, DA9063_LDO3_LIM),
666                 .vmon = BFIELD(DA9063_BB_REG_MON_REG_2, DA9063_LDO3_MON_EN),
667         },
668         {
669                 DA9063_LDO(DA9063, LDO7, 900, 50, 3600),
670                 .oc_event = BFIELD(DA9063_REG_STATUS_D, DA9063_LDO7_LIM),
671                 .vmon = BFIELD(DA9063_BB_REG_MON_REG_2, DA9063_LDO7_MON_EN),
672         },
673         {
674                 DA9063_LDO(DA9063, LDO8, 900, 50, 3600),
675                 .oc_event = BFIELD(DA9063_REG_STATUS_D, DA9063_LDO8_LIM),
676                 .vmon = BFIELD(DA9063_BB_REG_MON_REG_2, DA9063_LDO8_MON_EN),
677         },
678         {
679                 DA9063_LDO(DA9063, LDO9, 950, 50, 3600),
680                 .vmon = BFIELD(DA9063_BB_REG_MON_REG_3, DA9063_LDO9_MON_EN),
681         },
682         {
683                 DA9063_LDO(DA9063, LDO11, 900, 50, 3600),
684                 .oc_event = BFIELD(DA9063_REG_STATUS_D, DA9063_LDO11_LIM),
685                 .vmon = BFIELD(DA9063_BB_REG_MON_REG_3, DA9063_LDO11_MON_EN),
686         },
687
688         /* The following LDOs are present only on DA9063, not on DA9063L */
689         {
690                 DA9063_LDO(DA9063, LDO1, 600, 20, 1860),
691                 .vmon = BFIELD(DA9063_BB_REG_MON_REG_2, DA9063_LDO1_MON_EN),
692         },
693         {
694                 DA9063_LDO(DA9063, LDO2, 600, 20, 1860),
695                 .vmon = BFIELD(DA9063_BB_REG_MON_REG_2, DA9063_LDO2_MON_EN),
696         },
697         {
698                 DA9063_LDO(DA9063, LDO4, 900, 20, 3440),
699                 .oc_event = BFIELD(DA9063_REG_STATUS_D, DA9063_LDO4_LIM),
700                 .vmon = BFIELD(DA9063_BB_REG_MON_REG_2, DA9063_LDO4_MON_EN),
701         },
702         {
703                 DA9063_LDO(DA9063, LDO5, 900, 50, 3600),
704                 .vmon = BFIELD(DA9063_BB_REG_MON_REG_2, DA9063_LDO5_MON_EN),
705         },
706         {
707                 DA9063_LDO(DA9063, LDO6, 900, 50, 3600),
708                 .vmon = BFIELD(DA9063_BB_REG_MON_REG_2, DA9063_LDO6_MON_EN),
709         },
710
711         {
712                 DA9063_LDO(DA9063, LDO10, 900, 50, 3600),
713                 .vmon = BFIELD(DA9063_BB_REG_MON_REG_3, DA9063_LDO10_MON_EN),
714         },
715 };
716
717 /* Link chip model with regulators info table */
718 static struct da9063_dev_model regulators_models[] = {
719         {
720                 .regulator_info = da9063_regulator_info,
721                 .n_regulators = ARRAY_SIZE(da9063_regulator_info),
722                 .type = PMIC_TYPE_DA9063,
723         },
724         {
725                 .regulator_info = da9063_regulator_info,
726                 .n_regulators = ARRAY_SIZE(da9063_regulator_info) - 6,
727                 .type = PMIC_TYPE_DA9063L,
728         },
729         { }
730 };
731
732 /* Regulator interrupt handlers */
733 static irqreturn_t da9063_ldo_lim_event(int irq, void *data)
734 {
735         struct da9063_regulators *regulators = data;
736         struct da9063 *hw = regulators->regulator[0].hw;
737         struct da9063_regulator *regl;
738         int bits, i, ret;
739
740         ret = regmap_read(hw->regmap, DA9063_REG_STATUS_D, &bits);
741         if (ret < 0)
742                 return IRQ_NONE;
743
744         for (i = regulators->n_regulators - 1; i >= 0; i--) {
745                 regl = &regulators->regulator[i];
746                 if (regl->info->oc_event.reg != DA9063_REG_STATUS_D)
747                         continue;
748
749                 if (BIT(regl->info->oc_event.lsb) & bits) {
750                         regulator_notifier_call_chain(regl->rdev,
751                                         REGULATOR_EVENT_OVER_CURRENT, NULL);
752                 }
753         }
754
755         return IRQ_HANDLED;
756 }
757
758 /*
759  * Probing and Initialisation functions
760  */
761 static const struct regulator_init_data *da9063_get_regulator_initdata(
762                 const struct da9063_regulators_pdata *regl_pdata, int id)
763 {
764         int i;
765
766         for (i = 0; i < regl_pdata->n_regulators; i++) {
767                 if (id == regl_pdata->regulator_data[i].id)
768                         return regl_pdata->regulator_data[i].initdata;
769         }
770
771         return NULL;
772 }
773
774 static int da9063_check_xvp_constraints(struct regulator_config *config)
775 {
776         struct da9063_regulator *regl = config->driver_data;
777         const struct regulation_constraints *constr = &config->init_data->constraints;
778         const struct notification_limit *uv_l = &constr->under_voltage_limits;
779         const struct notification_limit *ov_l = &constr->over_voltage_limits;
780
781         /* make sure that only one severity is used to clarify if unchanged, enabled or disabled */
782         if ((!!uv_l->prot + !!uv_l->err + !!uv_l->warn) > 1) {
783                 dev_err(config->dev, "%s: at most one voltage monitoring severity allowed!\n",
784                         regl->desc.name);
785                 return -EINVAL;
786         }
787
788         /* make sure that UV and OV monitoring is set to the same severity and value */
789         if (uv_l->prot != ov_l->prot) {
790                 dev_err(config->dev,
791                         "%s: protection-microvolt: value must be equal for uv and ov!\n",
792                         regl->desc.name);
793                 return -EINVAL;
794         }
795         if (uv_l->err != ov_l->err) {
796                 dev_err(config->dev, "%s: error-microvolt: value must be equal for uv and ov!\n",
797                         regl->desc.name);
798                 return -EINVAL;
799         }
800         if (uv_l->warn != ov_l->warn) {
801                 dev_err(config->dev, "%s: warn-microvolt: value must be equal for uv and ov!\n",
802                         regl->desc.name);
803                 return -EINVAL;
804         }
805
806         return 0;
807 }
808
809 static struct of_regulator_match da9063_matches[] = {
810         [DA9063_ID_BCORE1]           = { .name = "bcore1"           },
811         [DA9063_ID_BCORE2]           = { .name = "bcore2"           },
812         [DA9063_ID_BPRO]             = { .name = "bpro",            },
813         [DA9063_ID_BMEM]             = { .name = "bmem",            },
814         [DA9063_ID_BIO]              = { .name = "bio",             },
815         [DA9063_ID_BPERI]            = { .name = "bperi",           },
816         [DA9063_ID_BCORES_MERGED]    = { .name = "bcores-merged"    },
817         [DA9063_ID_BMEM_BIO_MERGED]  = { .name = "bmem-bio-merged", },
818         [DA9063_ID_LDO3]             = { .name = "ldo3",            },
819         [DA9063_ID_LDO7]             = { .name = "ldo7",            },
820         [DA9063_ID_LDO8]             = { .name = "ldo8",            },
821         [DA9063_ID_LDO9]             = { .name = "ldo9",            },
822         [DA9063_ID_LDO11]            = { .name = "ldo11",           },
823         /* The following LDOs are present only on DA9063, not on DA9063L */
824         [DA9063_ID_LDO1]             = { .name = "ldo1",            },
825         [DA9063_ID_LDO2]             = { .name = "ldo2",            },
826         [DA9063_ID_LDO4]             = { .name = "ldo4",            },
827         [DA9063_ID_LDO5]             = { .name = "ldo5",            },
828         [DA9063_ID_LDO6]             = { .name = "ldo6",            },
829         [DA9063_ID_LDO10]            = { .name = "ldo10",           },
830 };
831
832 static struct da9063_regulators_pdata *da9063_parse_regulators_dt(
833                 struct platform_device *pdev,
834                 struct of_regulator_match **da9063_reg_matches)
835 {
836         struct da9063 *da9063 = dev_get_drvdata(pdev->dev.parent);
837         struct da9063_regulators_pdata *pdata;
838         struct da9063_regulator_data *rdata;
839         struct device_node *node;
840         int da9063_matches_len = ARRAY_SIZE(da9063_matches);
841         int i, n, num;
842
843         if (da9063->type == PMIC_TYPE_DA9063L)
844                 da9063_matches_len -= 6;
845
846         node = of_get_child_by_name(pdev->dev.parent->of_node, "regulators");
847         if (!node) {
848                 dev_err(&pdev->dev, "Regulators device node not found\n");
849                 return ERR_PTR(-ENODEV);
850         }
851
852         num = of_regulator_match(&pdev->dev, node, da9063_matches,
853                                  da9063_matches_len);
854         of_node_put(node);
855         if (num < 0) {
856                 dev_err(&pdev->dev, "Failed to match regulators\n");
857                 return ERR_PTR(-EINVAL);
858         }
859
860         pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
861         if (!pdata)
862                 return ERR_PTR(-ENOMEM);
863
864         pdata->regulator_data = devm_kcalloc(&pdev->dev,
865                                         num, sizeof(*pdata->regulator_data),
866                                         GFP_KERNEL);
867         if (!pdata->regulator_data)
868                 return ERR_PTR(-ENOMEM);
869         pdata->n_regulators = num;
870
871         n = 0;
872         for (i = 0; i < da9063_matches_len; i++) {
873                 if (!da9063_matches[i].init_data)
874                         continue;
875
876                 rdata = &pdata->regulator_data[n];
877                 rdata->id = i;
878                 rdata->initdata = da9063_matches[i].init_data;
879
880                 n++;
881         }
882
883         *da9063_reg_matches = da9063_matches;
884         return pdata;
885 }
886
887 static int da9063_regulator_probe(struct platform_device *pdev)
888 {
889         struct da9063 *da9063 = dev_get_drvdata(pdev->dev.parent);
890         struct of_regulator_match *da9063_reg_matches = NULL;
891         struct da9063_regulators_pdata *regl_pdata;
892         const struct da9063_dev_model *model;
893         struct da9063_regulators *regulators;
894         struct da9063_regulator *regl;
895         struct regulator_config config;
896         bool bcores_merged, bmem_bio_merged;
897         int id, irq, n, n_regulators, ret, val;
898
899         regl_pdata = da9063_parse_regulators_dt(pdev, &da9063_reg_matches);
900
901         if (IS_ERR(regl_pdata) || regl_pdata->n_regulators == 0) {
902                 dev_err(&pdev->dev,
903                         "No regulators defined for the platform\n");
904                 return -ENODEV;
905         }
906
907         /* Find regulators set for particular device model */
908         for (model = regulators_models; model->regulator_info; model++) {
909                 if (model->type == da9063->type)
910                         break;
911         }
912         if (!model->regulator_info) {
913                 dev_err(&pdev->dev, "Chip model not recognised (%u)\n",
914                         da9063->type);
915                 return -ENODEV;
916         }
917
918         ret = regmap_read(da9063->regmap, DA9063_REG_CONFIG_H, &val);
919         if (ret < 0) {
920                 dev_err(&pdev->dev,
921                         "Error while reading BUCKs configuration\n");
922                 return ret;
923         }
924         bcores_merged = val & DA9063_BCORE_MERGE;
925         bmem_bio_merged = val & DA9063_BUCK_MERGE;
926
927         n_regulators = model->n_regulators;
928         if (bcores_merged)
929                 n_regulators -= 2; /* remove BCORE1, BCORE2 */
930         else
931                 n_regulators--;    /* remove BCORES_MERGED */
932         if (bmem_bio_merged)
933                 n_regulators -= 2; /* remove BMEM, BIO */
934         else
935                 n_regulators--;    /* remove BMEM_BIO_MERGED */
936
937         /* Allocate memory required by usable regulators */
938         regulators = devm_kzalloc(&pdev->dev, struct_size(regulators,
939                                   regulator, n_regulators), GFP_KERNEL);
940         if (!regulators)
941                 return -ENOMEM;
942
943         regulators->n_regulators = n_regulators;
944         platform_set_drvdata(pdev, regulators);
945
946         /* Register all regulators declared in platform information */
947         n = 0;
948         id = 0;
949         while (n < regulators->n_regulators) {
950                 /* Skip regulator IDs depending on merge mode configuration */
951                 switch (id) {
952                 case DA9063_ID_BCORE1:
953                 case DA9063_ID_BCORE2:
954                         if (bcores_merged) {
955                                 id++;
956                                 continue;
957                         }
958                         break;
959                 case DA9063_ID_BMEM:
960                 case DA9063_ID_BIO:
961                         if (bmem_bio_merged) {
962                                 id++;
963                                 continue;
964                         }
965                         break;
966                 case DA9063_ID_BCORES_MERGED:
967                         if (!bcores_merged) {
968                                 id++;
969                                 continue;
970                         }
971                         break;
972                 case DA9063_ID_BMEM_BIO_MERGED:
973                         if (!bmem_bio_merged) {
974                                 id++;
975                                 continue;
976                         }
977                         break;
978                 }
979
980                 /* Initialise regulator structure */
981                 regl = &regulators->regulator[n];
982                 regl->hw = da9063;
983                 regl->info = &model->regulator_info[id];
984                 regl->desc = regl->info->desc;
985                 regl->desc.type = REGULATOR_VOLTAGE;
986                 regl->desc.owner = THIS_MODULE;
987
988                 if (regl->info->mode.reg) {
989                         regl->mode = devm_regmap_field_alloc(&pdev->dev,
990                                         da9063->regmap, regl->info->mode);
991                         if (IS_ERR(regl->mode))
992                                 return PTR_ERR(regl->mode);
993                 }
994
995                 if (regl->info->suspend.reg) {
996                         regl->suspend = devm_regmap_field_alloc(&pdev->dev,
997                                         da9063->regmap, regl->info->suspend);
998                         if (IS_ERR(regl->suspend))
999                                 return PTR_ERR(regl->suspend);
1000                 }
1001
1002                 if (regl->info->sleep.reg) {
1003                         regl->sleep = devm_regmap_field_alloc(&pdev->dev,
1004                                         da9063->regmap, regl->info->sleep);
1005                         if (IS_ERR(regl->sleep))
1006                                 return PTR_ERR(regl->sleep);
1007                 }
1008
1009                 if (regl->info->suspend_sleep.reg) {
1010                         regl->suspend_sleep = devm_regmap_field_alloc(&pdev->dev,
1011                                 da9063->regmap, regl->info->suspend_sleep);
1012                         if (IS_ERR(regl->suspend_sleep))
1013                                 return PTR_ERR(regl->suspend_sleep);
1014                 }
1015                 if (regl->info->vmon.reg) {
1016                         regl->vmon = devm_regmap_field_alloc(&pdev->dev,
1017                                 da9063->regmap, regl->info->vmon);
1018                         if (IS_ERR(regl->vmon))
1019                                 return PTR_ERR(regl->vmon);
1020                 }
1021
1022                 /* Register regulator */
1023                 memset(&config, 0, sizeof(config));
1024                 config.dev = &pdev->dev;
1025                 config.init_data = da9063_get_regulator_initdata(regl_pdata, id);
1026                 config.driver_data = regl;
1027                 if (da9063_reg_matches)
1028                         config.of_node = da9063_reg_matches[id].of_node;
1029                 config.regmap = da9063->regmap;
1030
1031                 /* Checking constraints requires init_data from DT. */
1032                 if (config.init_data) {
1033                         ret = da9063_check_xvp_constraints(&config);
1034                         if (ret)
1035                                 return ret;
1036                 }
1037
1038                 regl->rdev = devm_regulator_register(&pdev->dev, &regl->desc,
1039                                                      &config);
1040                 if (IS_ERR(regl->rdev)) {
1041                         dev_err(&pdev->dev,
1042                                 "Failed to register %s regulator\n",
1043                                 regl->desc.name);
1044                         return PTR_ERR(regl->rdev);
1045                 }
1046                 id++;
1047                 n++;
1048         }
1049
1050         /* LDOs overcurrent event support */
1051         irq = platform_get_irq_byname(pdev, "LDO_LIM");
1052         if (irq < 0)
1053                 return irq;
1054
1055         ret = devm_request_threaded_irq(&pdev->dev, irq,
1056                                 NULL, da9063_ldo_lim_event,
1057                                 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
1058                                 "LDO_LIM", regulators);
1059         if (ret)
1060                 dev_err(&pdev->dev, "Failed to request LDO_LIM IRQ.\n");
1061
1062         return ret;
1063 }
1064
1065 static struct platform_driver da9063_regulator_driver = {
1066         .driver = {
1067                 .name = DA9063_DRVNAME_REGULATORS,
1068                 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
1069         },
1070         .probe = da9063_regulator_probe,
1071 };
1072
1073 static int __init da9063_regulator_init(void)
1074 {
1075         return platform_driver_register(&da9063_regulator_driver);
1076 }
1077 subsys_initcall(da9063_regulator_init);
1078
1079 static void __exit da9063_regulator_cleanup(void)
1080 {
1081         platform_driver_unregister(&da9063_regulator_driver);
1082 }
1083 module_exit(da9063_regulator_cleanup);
1084
1085
1086 /* Module information */
1087 MODULE_AUTHOR("Krystian Garbaciak <krystian.garbaciak@diasemi.com>");
1088 MODULE_DESCRIPTION("DA9063 regulators driver");
1089 MODULE_LICENSE("GPL");
1090 MODULE_ALIAS("platform:" DA9063_DRVNAME_REGULATORS);