1 // SPDX-License-Identifier: GPL-2.0
3 // MCP16502 PMIC driver
5 // Copyright (C) 2018 Microchip Technology Inc. and its subsidiaries
7 // Author: Andrei Stefanescu <andrei.stefanescu@microchip.com>
9 // Inspired from tps65086-regulator.c
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>
17 #include <linux/regmap.h>
18 #include <linux/regulator/driver.h>
19 #include <linux/suspend.h>
20 #include <linux/gpio/consumer.h>
22 #define VDD_LOW_SEL 0x0D
23 #define VDD_HIGH_SEL 0x3F
25 #define MCP16502_FLT BIT(7)
26 #define MCP16502_DVSR GENMASK(3, 2)
27 #define MCP16502_ENS BIT(0)
30 * The PMIC has four sets of registers corresponding to four power modes:
31 * Performance, Active, Low-power, Hibernate.
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.
38 * In order for the PMIC to transition to operating modes it has to be
39 * controlled via GPIO lines called LPM and HPM.
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.
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).
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.
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.
58 #define MCP16502_REG_BASE(i, r) ((((i) + 1) << 4) + MCP16502_REG_##r)
59 #define MCP16502_STAT_BASE(i) ((i) + 5)
61 #define MCP16502_OPMODE_ACTIVE REGULATOR_MODE_NORMAL
62 #define MCP16502_OPMODE_LPM REGULATOR_MODE_IDLE
63 #define MCP16502_OPMODE_HIB REGULATOR_MODE_STANDBY
65 #define MCP16502_MODE_AUTO_PFM 0
66 #define MCP16502_MODE_FPWM BIT(6)
68 #define MCP16502_VSEL 0x3F
69 #define MCP16502_EN BIT(7)
70 #define MCP16502_MODE BIT(6)
72 #define MCP16502_MIN_REG 0x0
73 #define MCP16502_MAX_REG 0x65
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
92 /* Ramp delay (uV/us) for buck1, ldo1, ldo2. */
93 static const int mcp16502_ramp_b1l12[] = { 6250, 3125, 2083, 1563 };
95 /* Ramp delay (uV/us) for buck2, buck3, buck4. */
96 static const int mcp16502_ramp_b234[] = { 3125, 1563, 1042, 781 };
98 static unsigned int mcp16502_of_map_mode(unsigned int mode)
100 if (mode == REGULATOR_MODE_NORMAL || mode == REGULATOR_MODE_IDLE)
103 return REGULATOR_MODE_INVALID;
106 #define MCP16502_REGULATOR(_name, _id, _ranges, _ops) \
109 .regulators_node = of_match_ptr("regulators"), \
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, \
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
143 struct gpio_desc *lpm;
147 * mcp16502_gpio_set_mode() - set the GPIO corresponding value
149 * Used to prepare transitioning into hibernate or resuming from it.
151 static void mcp16502_gpio_set_mode(struct mcp16502 *mcp, int mode)
154 case MCP16502_OPMODE_ACTIVE:
155 gpiod_set_value(mcp->lpm, 0);
157 case MCP16502_OPMODE_LPM:
158 case MCP16502_OPMODE_HIB:
159 gpiod_set_value(mcp->lpm, 1);
162 pr_err("%s: %d invalid\n", __func__, mode);
167 * mcp16502_get_reg() - get the PMIC's state configuration register for opmode
169 * @rdev: the regulator whose register we are searching
170 * @opmode: the PMIC's operating mode ACTIVE, Low-power, Hibernate
172 static int mcp16502_get_state_reg(struct regulator_dev *rdev, int 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);
187 * mcp16502_get_mode() - return the current operating mode of a regulator
189 * Note: all functions that are not part of entering/exiting standby/suspend
190 * use the Active mode registers.
192 * Note: this is different from the PMIC's operatig mode, it is the
193 * MODE bit from the regulator's register.
195 static unsigned int mcp16502_get_mode(struct regulator_dev *rdev)
200 reg = mcp16502_get_state_reg(rdev, MCP16502_OPMODE_ACTIVE);
204 ret = regmap_read(rdev->regmap, reg, &val);
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;
214 return REGULATOR_MODE_INVALID;
219 * _mcp16502_set_mode() - helper for set_mode and set_suspend_mode
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
225 static int _mcp16502_set_mode(struct regulator_dev *rdev, unsigned int mode,
226 unsigned int op_mode)
231 reg = mcp16502_get_state_reg(rdev, op_mode);
236 case REGULATOR_MODE_NORMAL:
237 val = MCP16502_MODE_FPWM;
239 case REGULATOR_MODE_IDLE:
240 val = MCP16502_MODE_AUTO_PFM;
246 reg = regmap_update_bits(rdev->regmap, reg, MCP16502_MODE, val);
251 * mcp16502_set_mode() - regulator_ops set_mode
253 static int mcp16502_set_mode(struct regulator_dev *rdev, unsigned int mode)
255 return _mcp16502_set_mode(rdev, mode, MCP16502_OPMODE_ACTIVE);
259 * mcp16502_get_status() - regulator_ops get_status
261 static int mcp16502_get_status(struct regulator_dev *rdev)
266 ret = regmap_read(rdev->regmap, MCP16502_STAT_BASE(rdev_get_id(rdev)),
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;
278 return REGULATOR_STATUS_UNDEFINED;
281 static int mcp16502_set_voltage_time_sel(struct regulator_dev *rdev,
282 unsigned int old_sel,
283 unsigned int new_sel)
285 static const u8 us_ramp[] = { 8, 16, 24, 32 };
286 int id = rdev_get_id(rdev);
287 unsigned int uV_delta, val;
290 ret = regmap_read(rdev->regmap, MCP16502_REG_BASE(id, CFG), &val);
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);
301 ret = DIV_ROUND_CLOSEST(uV_delta * us_ramp[val],
302 mcp16502_ramp_b1l12[val]);
308 ret = DIV_ROUND_CLOSEST(uV_delta * us_ramp[val],
309 mcp16502_ramp_b234[val]);
319 static int mcp16502_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
322 int id = rdev_get_id(rdev);
323 unsigned int i, size;
329 ramp = mcp16502_ramp_b1l12;
330 size = ARRAY_SIZE(mcp16502_ramp_b1l12);
336 ramp = mcp16502_ramp_b234;
337 size = ARRAY_SIZE(mcp16502_ramp_b234);
344 for (i = 0; i < size; i++) {
345 if (ramp[i] == ramp_delay)
351 return regmap_update_bits(rdev->regmap, MCP16502_REG_BASE(id, CFG),
352 MCP16502_DVSR, (i << 2));
355 #ifdef CONFIG_SUSPEND
357 * mcp16502_suspend_get_target_reg() - get the reg of the target suspend PMIC
360 static int mcp16502_suspend_get_target_reg(struct regulator_dev *rdev)
362 switch (pm_suspend_target_state) {
363 case PM_SUSPEND_STANDBY:
364 return mcp16502_get_state_reg(rdev, MCP16502_OPMODE_LPM);
367 return mcp16502_get_state_reg(rdev, MCP16502_OPMODE_HIB);
369 dev_err(&rdev->dev, "invalid suspend target: %d\n",
370 pm_suspend_target_state);
377 * mcp16502_set_suspend_voltage() - regulator_ops set_suspend_voltage
379 static int mcp16502_set_suspend_voltage(struct regulator_dev *rdev, int uV)
381 int sel = regulator_map_voltage_linear_range(rdev, uV, uV);
382 int reg = mcp16502_suspend_get_target_reg(rdev);
390 return regmap_update_bits(rdev->regmap, reg, MCP16502_VSEL, sel);
394 * mcp16502_set_suspend_mode() - regulator_ops set_suspend_mode
396 static int mcp16502_set_suspend_mode(struct regulator_dev *rdev,
399 switch (pm_suspend_target_state) {
400 case PM_SUSPEND_STANDBY:
401 return _mcp16502_set_mode(rdev, mode, MCP16502_OPMODE_LPM);
404 return _mcp16502_set_mode(rdev, mode, MCP16502_OPMODE_HIB);
406 dev_err(&rdev->dev, "invalid suspend target: %d\n",
407 pm_suspend_target_state);
414 * mcp16502_set_suspend_enable() - regulator_ops set_suspend_enable
416 static int mcp16502_set_suspend_enable(struct regulator_dev *rdev)
418 int reg = mcp16502_suspend_get_target_reg(rdev);
423 return regmap_update_bits(rdev->regmap, reg, MCP16502_EN, MCP16502_EN);
427 * mcp16502_set_suspend_disable() - regulator_ops set_suspend_disable
429 static int mcp16502_set_suspend_disable(struct regulator_dev *rdev)
431 int reg = mcp16502_suspend_get_target_reg(rdev);
436 return regmap_update_bits(rdev->regmap, reg, MCP16502_EN, 0);
438 #endif /* CONFIG_SUSPEND */
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,
452 .set_mode = mcp16502_set_mode,
453 .get_mode = mcp16502_get_mode,
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 */
464 * LDOs cannot change operating modes.
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,
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 */
485 static const struct of_device_id mcp16502_ids[] = {
486 { .compatible = "microchip,mcp16502", },
489 MODULE_DEVICE_TABLE(of, mcp16502_ids);
491 static const struct linear_range b1l12_ranges[] = {
492 REGULATOR_LINEAR_RANGE(1200000, VDD_LOW_SEL, VDD_HIGH_SEL, 50000),
495 static const struct linear_range b234_ranges[] = {
496 REGULATOR_LINEAR_RANGE(600000, VDD_LOW_SEL, VDD_HIGH_SEL, 25000),
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)
509 static const struct regmap_range mcp16502_ranges[] = {
510 regmap_reg_range(MCP16502_MIN_REG, MCP16502_MAX_REG)
513 static const struct regmap_access_table mcp16502_yes_reg_table = {
514 .yes_ranges = mcp16502_ranges,
515 .n_yes_ranges = ARRAY_SIZE(mcp16502_ranges),
518 static const struct regmap_config mcp16502_regmap_config = {
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,
527 static int mcp16502_probe(struct i2c_client *client,
528 const struct i2c_device_id *id)
530 struct regulator_config config = { };
531 struct regulator_dev *rdev;
533 struct mcp16502 *mcp;
540 mcp = devm_kzalloc(dev, sizeof(*mcp), GFP_KERNEL);
544 rmap = devm_regmap_init_i2c(client, &mcp16502_regmap_config);
547 dev_err(dev, "regmap init failed: %d\n", ret);
551 i2c_set_clientdata(client, mcp);
552 config.regmap = rmap;
553 config.driver_data = mcp;
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);
561 for (i = 0; i < NUM_REGULATORS; i++) {
562 rdev = devm_regulator_register(dev, &mcp16502_desc[i], &config);
565 "failed to register %s regulator %ld\n",
566 mcp16502_desc[i].name, PTR_ERR(rdev));
567 return PTR_ERR(rdev);
571 mcp16502_gpio_set_mode(mcp, MCP16502_OPMODE_ACTIVE);
576 #ifdef CONFIG_PM_SLEEP
577 static int mcp16502_suspend_noirq(struct device *dev)
579 struct i2c_client *client = to_i2c_client(dev);
580 struct mcp16502 *mcp = i2c_get_clientdata(client);
582 mcp16502_gpio_set_mode(mcp, MCP16502_OPMODE_LPM);
587 static int mcp16502_resume_noirq(struct device *dev)
589 struct i2c_client *client = to_i2c_client(dev);
590 struct mcp16502 *mcp = i2c_get_clientdata(client);
592 mcp16502_gpio_set_mode(mcp, MCP16502_OPMODE_ACTIVE);
599 static const struct dev_pm_ops mcp16502_pm_ops = {
600 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(mcp16502_suspend_noirq,
601 mcp16502_resume_noirq)
604 static const struct i2c_device_id mcp16502_i2c_id[] = {
608 MODULE_DEVICE_TABLE(i2c, mcp16502_i2c_id);
610 static struct i2c_driver mcp16502_drv = {
611 .probe = mcp16502_probe,
613 .name = "mcp16502-regulator",
614 .of_match_table = of_match_ptr(mcp16502_ids),
616 .pm = &mcp16502_pm_ops,
619 .id_table = mcp16502_i2c_id,
622 module_i2c_driver(mcp16502_drv);
624 MODULE_LICENSE("GPL v2");
625 MODULE_DESCRIPTION("MCP16502 PMIC driver");
626 MODULE_AUTHOR("Andrei Stefanescu andrei.stefanescu@microchip.com");