regulator: mcp16502: add support for ramp delay
[linux-2.6-microblaze.git] / drivers / regulator / mcp16502.c
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // MCP16502 PMIC driver
4 //
5 // Copyright (C) 2018 Microchip Technology Inc. and its subsidiaries
6 //
7 // Author: Andrei Stefanescu <andrei.stefanescu@microchip.com>
8 //
9 // Inspired from tps65086-regulator.c
10
11 #include <linux/gpio.h>
12 #include <linux/i2c.h>
13 #include <linux/init.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/of.h>
17 #include <linux/regmap.h>
18 #include <linux/regulator/driver.h>
19 #include <linux/suspend.h>
20 #include <linux/gpio/consumer.h>
21
22 #define VDD_LOW_SEL 0x0D
23 #define VDD_HIGH_SEL 0x3F
24
25 #define MCP16502_FLT            BIT(7)
26 #define MCP16502_DVSR           GENMASK(3, 2)
27 #define MCP16502_ENS            BIT(0)
28
29 /*
30  * The PMIC has four sets of registers corresponding to four power modes:
31  * Performance, Active, Low-power, Hibernate.
32  *
33  * Registers:
34  * Each regulator has a register for each power mode. To access a register
35  * for a specific regulator and mode BASE_* and OFFSET_* need to be added.
36  *
37  * Operating modes:
38  * In order for the PMIC to transition to operating modes it has to be
39  * controlled via GPIO lines called LPM and HPM.
40  *
41  * The registers are fully configurable such that you can put all regulators in
42  * a low-power state while the PMIC is in Active mode. They are supposed to be
43  * configured at startup and then simply transition to/from a global low-power
44  * state by setting the GPIO lpm pin high/low.
45  *
46  * This driver keeps the PMIC in Active mode, Low-power state is set for the
47  * regulators by enabling/disabling operating mode (FPWM or Auto PFM).
48  *
49  * The PMIC's Low-power and Hibernate modes are used during standby/suspend.
50  * To enter standby/suspend the PMIC will go to Low-power mode. From there, it
51  * will transition to Hibernate when the PWRHLD line is set to low by the MPU.
52  */
53
54 /*
55  * This function is useful for iterating over all regulators and accessing their
56  * registers in a generic way or accessing a regulator device by its id.
57  */
58 #define MCP16502_REG_BASE(i, r) ((((i) + 1) << 4) + MCP16502_REG_##r)
59 #define MCP16502_STAT_BASE(i) ((i) + 5)
60
61 #define MCP16502_OPMODE_ACTIVE REGULATOR_MODE_NORMAL
62 #define MCP16502_OPMODE_LPM REGULATOR_MODE_IDLE
63 #define MCP16502_OPMODE_HIB REGULATOR_MODE_STANDBY
64
65 #define MCP16502_MODE_AUTO_PFM 0
66 #define MCP16502_MODE_FPWM BIT(6)
67
68 #define MCP16502_VSEL 0x3F
69 #define MCP16502_EN BIT(7)
70 #define MCP16502_MODE BIT(6)
71
72 #define MCP16502_MIN_REG 0x0
73 #define MCP16502_MAX_REG 0x65
74
75 /**
76  * enum mcp16502_reg - MCP16502 regulators's registers
77  * @MCP16502_REG_A: active state register
78  * @MCP16502_REG_LPM: low power mode state register
79  * @MCP16502_REG_HIB: hibernate state register
80  * @MCP16502_REG_SEQ: startup sequence register
81  * @MCP16502_REG_CFG: configuration register
82  */
83 enum mcp16502_reg {
84         MCP16502_REG_A,
85         MCP16502_REG_LPM,
86         MCP16502_REG_HIB,
87         MCP16502_REG_HPM,
88         MCP16502_REG_SEQ,
89         MCP16502_REG_CFG,
90 };
91
92 /* Ramp delay (uV/us) for buck1, ldo1, ldo2. */
93 static const int mcp16502_ramp_b1l12[] = { 6250, 3125, 2083, 1563 };
94
95 /* Ramp delay (uV/us) for buck2, buck3, buck4. */
96 static const int mcp16502_ramp_b234[] = { 3125, 1563, 1042, 781 };
97
98 static unsigned int mcp16502_of_map_mode(unsigned int mode)
99 {
100         if (mode == REGULATOR_MODE_NORMAL || mode == REGULATOR_MODE_IDLE)
101                 return mode;
102
103         return REGULATOR_MODE_INVALID;
104 }
105
106 #define MCP16502_REGULATOR(_name, _id, _ranges, _ops)                   \
107         [_id] = {                                                       \
108                 .name                   = _name,                        \
109                 .regulators_node        = of_match_ptr("regulators"),   \
110                 .id                     = _id,                          \
111                 .ops                    = &(_ops),                      \
112                 .type                   = REGULATOR_VOLTAGE,            \
113                 .owner                  = THIS_MODULE,                  \
114                 .n_voltages             = MCP16502_VSEL + 1,            \
115                 .linear_ranges          = _ranges,                      \
116                 .linear_min_sel         = VDD_LOW_SEL,                  \
117                 .n_linear_ranges        = ARRAY_SIZE(_ranges),          \
118                 .of_match               = of_match_ptr(_name),          \
119                 .of_map_mode            = mcp16502_of_map_mode,         \
120                 .vsel_reg               = (((_id) + 1) << 4),           \
121                 .vsel_mask              = MCP16502_VSEL,                \
122                 .enable_reg             = (((_id) + 1) << 4),           \
123                 .enable_mask            = MCP16502_EN,                  \
124         }
125
126 enum {
127         BUCK1 = 0,
128         BUCK2,
129         BUCK3,
130         BUCK4,
131         LDO1,
132         LDO2,
133         NUM_REGULATORS
134 };
135
136 /*
137  * struct mcp16502 - PMIC representation
138  * @rdev: the regulators belonging to this chip
139  * @rmap: regmap to be used for I2C communication
140  * @lpm: LPM GPIO descriptor
141  */
142 struct mcp16502 {
143         struct gpio_desc *lpm;
144 };
145
146 /*
147  * mcp16502_gpio_set_mode() - set the GPIO corresponding value
148  *
149  * Used to prepare transitioning into hibernate or resuming from it.
150  */
151 static void mcp16502_gpio_set_mode(struct mcp16502 *mcp, int mode)
152 {
153         switch (mode) {
154         case MCP16502_OPMODE_ACTIVE:
155                 gpiod_set_value(mcp->lpm, 0);
156                 break;
157         case MCP16502_OPMODE_LPM:
158         case MCP16502_OPMODE_HIB:
159                 gpiod_set_value(mcp->lpm, 1);
160                 break;
161         default:
162                 pr_err("%s: %d invalid\n", __func__, mode);
163         }
164 }
165
166 /*
167  * mcp16502_get_reg() - get the PMIC's state configuration register for opmode
168  *
169  * @rdev: the regulator whose register we are searching
170  * @opmode: the PMIC's operating mode ACTIVE, Low-power, Hibernate
171  */
172 static int mcp16502_get_state_reg(struct regulator_dev *rdev, int opmode)
173 {
174         switch (opmode) {
175         case MCP16502_OPMODE_ACTIVE:
176                 return MCP16502_REG_BASE(rdev_get_id(rdev), A);
177         case MCP16502_OPMODE_LPM:
178                 return MCP16502_REG_BASE(rdev_get_id(rdev), LPM);
179         case MCP16502_OPMODE_HIB:
180                 return MCP16502_REG_BASE(rdev_get_id(rdev), HIB);
181         default:
182                 return -EINVAL;
183         }
184 }
185
186 /*
187  * mcp16502_get_mode() - return the current operating mode of a regulator
188  *
189  * Note: all functions that are not part of entering/exiting standby/suspend
190  *       use the Active mode registers.
191  *
192  * Note: this is different from the PMIC's operatig mode, it is the
193  *       MODE bit from the regulator's register.
194  */
195 static unsigned int mcp16502_get_mode(struct regulator_dev *rdev)
196 {
197         unsigned int val;
198         int ret, reg;
199
200         reg = mcp16502_get_state_reg(rdev, MCP16502_OPMODE_ACTIVE);
201         if (reg < 0)
202                 return reg;
203
204         ret = regmap_read(rdev->regmap, reg, &val);
205         if (ret)
206                 return ret;
207
208         switch (val & MCP16502_MODE) {
209         case MCP16502_MODE_FPWM:
210                 return REGULATOR_MODE_NORMAL;
211         case MCP16502_MODE_AUTO_PFM:
212                 return REGULATOR_MODE_IDLE;
213         default:
214                 return REGULATOR_MODE_INVALID;
215         }
216 }
217
218 /*
219  * _mcp16502_set_mode() - helper for set_mode and set_suspend_mode
220  *
221  * @rdev: the regulator for which we are setting the mode
222  * @mode: the regulator's mode (the one from MODE bit)
223  * @opmode: the PMIC's operating mode: Active/Low-power/Hibernate
224  */
225 static int _mcp16502_set_mode(struct regulator_dev *rdev, unsigned int mode,
226                               unsigned int op_mode)
227 {
228         int val;
229         int reg;
230
231         reg = mcp16502_get_state_reg(rdev, op_mode);
232         if (reg < 0)
233                 return reg;
234
235         switch (mode) {
236         case REGULATOR_MODE_NORMAL:
237                 val = MCP16502_MODE_FPWM;
238                 break;
239         case REGULATOR_MODE_IDLE:
240                 val = MCP16502_MODE_AUTO_PFM;
241                 break;
242         default:
243                 return -EINVAL;
244         }
245
246         reg = regmap_update_bits(rdev->regmap, reg, MCP16502_MODE, val);
247         return reg;
248 }
249
250 /*
251  * mcp16502_set_mode() - regulator_ops set_mode
252  */
253 static int mcp16502_set_mode(struct regulator_dev *rdev, unsigned int mode)
254 {
255         return _mcp16502_set_mode(rdev, mode, MCP16502_OPMODE_ACTIVE);
256 }
257
258 /*
259  * mcp16502_get_status() - regulator_ops get_status
260  */
261 static int mcp16502_get_status(struct regulator_dev *rdev)
262 {
263         int ret;
264         unsigned int val;
265
266         ret = regmap_read(rdev->regmap, MCP16502_STAT_BASE(rdev_get_id(rdev)),
267                           &val);
268         if (ret)
269                 return ret;
270
271         if (val & MCP16502_FLT)
272                 return REGULATOR_STATUS_ERROR;
273         else if (val & MCP16502_ENS)
274                 return REGULATOR_STATUS_ON;
275         else if (!(val & MCP16502_ENS))
276                 return REGULATOR_STATUS_OFF;
277
278         return REGULATOR_STATUS_UNDEFINED;
279 }
280
281 static int mcp16502_set_voltage_time_sel(struct regulator_dev *rdev,
282                                          unsigned int old_sel,
283                                          unsigned int new_sel)
284 {
285         static const u8 us_ramp[] = { 8, 16, 24, 32 };
286         int id = rdev_get_id(rdev);
287         unsigned int uV_delta, val;
288         int ret;
289
290         ret = regmap_read(rdev->regmap, MCP16502_REG_BASE(id, CFG), &val);
291         if (ret)
292                 return ret;
293
294         val = (val & MCP16502_DVSR) >> 2;
295         uV_delta = abs(new_sel * rdev->desc->linear_ranges->step -
296                        old_sel * rdev->desc->linear_ranges->step);
297         switch (id) {
298         case BUCK1:
299         case LDO1:
300         case LDO2:
301                 ret = DIV_ROUND_CLOSEST(uV_delta * us_ramp[val],
302                                         mcp16502_ramp_b1l12[val]);
303                 break;
304
305         case BUCK2:
306         case BUCK3:
307         case BUCK4:
308                 ret = DIV_ROUND_CLOSEST(uV_delta * us_ramp[val],
309                                         mcp16502_ramp_b234[val]);
310                 break;
311
312         default:
313                 return -EINVAL;
314         }
315
316         return ret;
317 }
318
319 static int mcp16502_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
320 {
321         const int *ramp;
322         int id = rdev_get_id(rdev);
323         unsigned int i, size;
324
325         switch (id) {
326         case BUCK1:
327         case LDO1:
328         case LDO2:
329                 ramp = mcp16502_ramp_b1l12;
330                 size = ARRAY_SIZE(mcp16502_ramp_b1l12);
331                 break;
332
333         case BUCK2:
334         case BUCK3:
335         case BUCK4:
336                 ramp = mcp16502_ramp_b234;
337                 size = ARRAY_SIZE(mcp16502_ramp_b234);
338                 break;
339
340         default:
341                 return -EINVAL;
342         }
343
344         for (i = 0; i < size; i++) {
345                 if (ramp[i] == ramp_delay)
346                         break;
347         }
348         if (i == size)
349                 return -EINVAL;
350
351         return regmap_update_bits(rdev->regmap, MCP16502_REG_BASE(id, CFG),
352                                   MCP16502_DVSR, (i << 2));
353 }
354
355 #ifdef CONFIG_SUSPEND
356 /*
357  * mcp16502_suspend_get_target_reg() - get the reg of the target suspend PMIC
358  *                                     mode
359  */
360 static int mcp16502_suspend_get_target_reg(struct regulator_dev *rdev)
361 {
362         switch (pm_suspend_target_state) {
363         case PM_SUSPEND_STANDBY:
364                 return mcp16502_get_state_reg(rdev, MCP16502_OPMODE_LPM);
365         case PM_SUSPEND_ON:
366         case PM_SUSPEND_MEM:
367                 return mcp16502_get_state_reg(rdev, MCP16502_OPMODE_HIB);
368         default:
369                 dev_err(&rdev->dev, "invalid suspend target: %d\n",
370                         pm_suspend_target_state);
371         }
372
373         return -EINVAL;
374 }
375
376 /*
377  * mcp16502_set_suspend_voltage() - regulator_ops set_suspend_voltage
378  */
379 static int mcp16502_set_suspend_voltage(struct regulator_dev *rdev, int uV)
380 {
381         int sel = regulator_map_voltage_linear_range(rdev, uV, uV);
382         int reg = mcp16502_suspend_get_target_reg(rdev);
383
384         if (sel < 0)
385                 return sel;
386
387         if (reg < 0)
388                 return reg;
389
390         return regmap_update_bits(rdev->regmap, reg, MCP16502_VSEL, sel);
391 }
392
393 /*
394  * mcp16502_set_suspend_mode() - regulator_ops set_suspend_mode
395  */
396 static int mcp16502_set_suspend_mode(struct regulator_dev *rdev,
397                                      unsigned int mode)
398 {
399         switch (pm_suspend_target_state) {
400         case PM_SUSPEND_STANDBY:
401                 return _mcp16502_set_mode(rdev, mode, MCP16502_OPMODE_LPM);
402         case PM_SUSPEND_ON:
403         case PM_SUSPEND_MEM:
404                 return _mcp16502_set_mode(rdev, mode, MCP16502_OPMODE_HIB);
405         default:
406                 dev_err(&rdev->dev, "invalid suspend target: %d\n",
407                         pm_suspend_target_state);
408         }
409
410         return -EINVAL;
411 }
412
413 /*
414  * mcp16502_set_suspend_enable() - regulator_ops set_suspend_enable
415  */
416 static int mcp16502_set_suspend_enable(struct regulator_dev *rdev)
417 {
418         int reg = mcp16502_suspend_get_target_reg(rdev);
419
420         if (reg < 0)
421                 return reg;
422
423         return regmap_update_bits(rdev->regmap, reg, MCP16502_EN, MCP16502_EN);
424 }
425
426 /*
427  * mcp16502_set_suspend_disable() - regulator_ops set_suspend_disable
428  */
429 static int mcp16502_set_suspend_disable(struct regulator_dev *rdev)
430 {
431         int reg = mcp16502_suspend_get_target_reg(rdev);
432
433         if (reg < 0)
434                 return reg;
435
436         return regmap_update_bits(rdev->regmap, reg, MCP16502_EN, 0);
437 }
438 #endif /* CONFIG_SUSPEND */
439
440 static const struct regulator_ops mcp16502_buck_ops = {
441         .list_voltage                   = regulator_list_voltage_linear_range,
442         .map_voltage                    = regulator_map_voltage_linear_range,
443         .get_voltage_sel                = regulator_get_voltage_sel_regmap,
444         .set_voltage_sel                = regulator_set_voltage_sel_regmap,
445         .enable                         = regulator_enable_regmap,
446         .disable                        = regulator_disable_regmap,
447         .is_enabled                     = regulator_is_enabled_regmap,
448         .get_status                     = mcp16502_get_status,
449         .set_voltage_time_sel           = mcp16502_set_voltage_time_sel,
450         .set_ramp_delay                 = mcp16502_set_ramp_delay,
451
452         .set_mode                       = mcp16502_set_mode,
453         .get_mode                       = mcp16502_get_mode,
454
455 #ifdef CONFIG_SUSPEND
456         .set_suspend_voltage            = mcp16502_set_suspend_voltage,
457         .set_suspend_mode               = mcp16502_set_suspend_mode,
458         .set_suspend_enable             = mcp16502_set_suspend_enable,
459         .set_suspend_disable            = mcp16502_set_suspend_disable,
460 #endif /* CONFIG_SUSPEND */
461 };
462
463 /*
464  * LDOs cannot change operating modes.
465  */
466 static const struct regulator_ops mcp16502_ldo_ops = {
467         .list_voltage                   = regulator_list_voltage_linear_range,
468         .map_voltage                    = regulator_map_voltage_linear_range,
469         .get_voltage_sel                = regulator_get_voltage_sel_regmap,
470         .set_voltage_sel                = regulator_set_voltage_sel_regmap,
471         .enable                         = regulator_enable_regmap,
472         .disable                        = regulator_disable_regmap,
473         .is_enabled                     = regulator_is_enabled_regmap,
474         .get_status                     = mcp16502_get_status,
475         .set_voltage_time_sel           = mcp16502_set_voltage_time_sel,
476         .set_ramp_delay                 = mcp16502_set_ramp_delay,
477
478 #ifdef CONFIG_SUSPEND
479         .set_suspend_voltage            = mcp16502_set_suspend_voltage,
480         .set_suspend_enable             = mcp16502_set_suspend_enable,
481         .set_suspend_disable            = mcp16502_set_suspend_disable,
482 #endif /* CONFIG_SUSPEND */
483 };
484
485 static const struct of_device_id mcp16502_ids[] = {
486         { .compatible = "microchip,mcp16502", },
487         {}
488 };
489 MODULE_DEVICE_TABLE(of, mcp16502_ids);
490
491 static const struct linear_range b1l12_ranges[] = {
492         REGULATOR_LINEAR_RANGE(1200000, VDD_LOW_SEL, VDD_HIGH_SEL, 50000),
493 };
494
495 static const struct linear_range b234_ranges[] = {
496         REGULATOR_LINEAR_RANGE(600000, VDD_LOW_SEL, VDD_HIGH_SEL, 25000),
497 };
498
499 static const struct regulator_desc mcp16502_desc[] = {
500         /* MCP16502_REGULATOR(_name, _id, ranges, regulator_ops) */
501         MCP16502_REGULATOR("VDD_IO", BUCK1, b1l12_ranges, mcp16502_buck_ops),
502         MCP16502_REGULATOR("VDD_DDR", BUCK2, b234_ranges, mcp16502_buck_ops),
503         MCP16502_REGULATOR("VDD_CORE", BUCK3, b234_ranges, mcp16502_buck_ops),
504         MCP16502_REGULATOR("VDD_OTHER", BUCK4, b234_ranges, mcp16502_buck_ops),
505         MCP16502_REGULATOR("LDO1", LDO1, b1l12_ranges, mcp16502_ldo_ops),
506         MCP16502_REGULATOR("LDO2", LDO2, b1l12_ranges, mcp16502_ldo_ops)
507 };
508
509 static const struct regmap_range mcp16502_ranges[] = {
510         regmap_reg_range(MCP16502_MIN_REG, MCP16502_MAX_REG)
511 };
512
513 static const struct regmap_access_table mcp16502_yes_reg_table = {
514         .yes_ranges = mcp16502_ranges,
515         .n_yes_ranges = ARRAY_SIZE(mcp16502_ranges),
516 };
517
518 static const struct regmap_config mcp16502_regmap_config = {
519         .reg_bits       = 8,
520         .val_bits       = 8,
521         .max_register   = MCP16502_MAX_REG,
522         .cache_type     = REGCACHE_NONE,
523         .rd_table       = &mcp16502_yes_reg_table,
524         .wr_table       = &mcp16502_yes_reg_table,
525 };
526
527 static int mcp16502_probe(struct i2c_client *client,
528                           const struct i2c_device_id *id)
529 {
530         struct regulator_config config = { };
531         struct regulator_dev *rdev;
532         struct device *dev;
533         struct mcp16502 *mcp;
534         struct regmap *rmap;
535         int i, ret;
536
537         dev = &client->dev;
538         config.dev = dev;
539
540         mcp = devm_kzalloc(dev, sizeof(*mcp), GFP_KERNEL);
541         if (!mcp)
542                 return -ENOMEM;
543
544         rmap = devm_regmap_init_i2c(client, &mcp16502_regmap_config);
545         if (IS_ERR(rmap)) {
546                 ret = PTR_ERR(rmap);
547                 dev_err(dev, "regmap init failed: %d\n", ret);
548                 return ret;
549         }
550
551         i2c_set_clientdata(client, mcp);
552         config.regmap = rmap;
553         config.driver_data = mcp;
554
555         mcp->lpm = devm_gpiod_get(dev, "lpm", GPIOD_OUT_LOW);
556         if (IS_ERR(mcp->lpm)) {
557                 dev_err(dev, "failed to get lpm pin: %ld\n", PTR_ERR(mcp->lpm));
558                 return PTR_ERR(mcp->lpm);
559         }
560
561         for (i = 0; i < NUM_REGULATORS; i++) {
562                 rdev = devm_regulator_register(dev, &mcp16502_desc[i], &config);
563                 if (IS_ERR(rdev)) {
564                         dev_err(dev,
565                                 "failed to register %s regulator %ld\n",
566                                 mcp16502_desc[i].name, PTR_ERR(rdev));
567                         return PTR_ERR(rdev);
568                 }
569         }
570
571         mcp16502_gpio_set_mode(mcp, MCP16502_OPMODE_ACTIVE);
572
573         return 0;
574 }
575
576 #ifdef CONFIG_PM_SLEEP
577 static int mcp16502_suspend_noirq(struct device *dev)
578 {
579         struct i2c_client *client = to_i2c_client(dev);
580         struct mcp16502 *mcp = i2c_get_clientdata(client);
581
582         mcp16502_gpio_set_mode(mcp, MCP16502_OPMODE_LPM);
583
584         return 0;
585 }
586
587 static int mcp16502_resume_noirq(struct device *dev)
588 {
589         struct i2c_client *client = to_i2c_client(dev);
590         struct mcp16502 *mcp = i2c_get_clientdata(client);
591
592         mcp16502_gpio_set_mode(mcp, MCP16502_OPMODE_ACTIVE);
593
594         return 0;
595 }
596 #endif
597
598 #ifdef CONFIG_PM
599 static const struct dev_pm_ops mcp16502_pm_ops = {
600         SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(mcp16502_suspend_noirq,
601                                       mcp16502_resume_noirq)
602 };
603 #endif
604 static const struct i2c_device_id mcp16502_i2c_id[] = {
605         { "mcp16502", 0 },
606         { }
607 };
608 MODULE_DEVICE_TABLE(i2c, mcp16502_i2c_id);
609
610 static struct i2c_driver mcp16502_drv = {
611         .probe          = mcp16502_probe,
612         .driver         = {
613                 .name   = "mcp16502-regulator",
614                 .of_match_table = of_match_ptr(mcp16502_ids),
615 #ifdef CONFIG_PM
616                 .pm = &mcp16502_pm_ops,
617 #endif
618         },
619         .id_table       = mcp16502_i2c_id,
620 };
621
622 module_i2c_driver(mcp16502_drv);
623
624 MODULE_LICENSE("GPL v2");
625 MODULE_DESCRIPTION("MCP16502 PMIC driver");
626 MODULE_AUTHOR("Andrei Stefanescu andrei.stefanescu@microchip.com");