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>
21 #define VDD_LOW_SEL 0x0D
22 #define VDD_HIGH_SEL 0x3F
24 #define MCP16502_FLT BIT(7)
25 #define MCP16502_ENS BIT(0)
28 * The PMIC has four sets of registers corresponding to four power modes:
29 * Performance, Active, Low-power, Hibernate.
32 * Each regulator has a register for each power mode. To access a register
33 * for a specific regulator and mode BASE_* and OFFSET_* need to be added.
36 * In order for the PMIC to transition to operating modes it has to be
37 * controlled via GPIO lines called LPM and HPM.
39 * The registers are fully configurable such that you can put all regulators in
40 * a low-power state while the PMIC is in Active mode. They are supposed to be
41 * configured at startup and then simply transition to/from a global low-power
42 * state by setting the GPIO lpm pin high/low.
44 * This driver keeps the PMIC in Active mode, Low-power state is set for the
45 * regulators by enabling/disabling operating mode (FPWM or Auto PFM).
47 * The PMIC's Low-power and Hibernate modes are used during standby/suspend.
48 * To enter standby/suspend the PMIC will go to Low-power mode. From there, it
49 * will transition to Hibernate when the PWRHLD line is set to low by the MPU.
53 * This function is useful for iterating over all regulators and accessing their
54 * registers in a generic way or accessing a regulator device by its id.
56 #define MCP16502_BASE(i) (((i) + 1) << 4)
57 #define MCP16502_STAT_BASE(i) ((i) + 5)
59 #define MCP16502_OFFSET_MODE_A 0
60 #define MCP16502_OFFSET_MODE_LPM 1
61 #define MCP16502_OFFSET_MODE_HIB 2
63 #define MCP16502_OPMODE_ACTIVE REGULATOR_MODE_NORMAL
64 #define MCP16502_OPMODE_LPM REGULATOR_MODE_IDLE
65 #define MCP16502_OPMODE_HIB REGULATOR_MODE_STANDBY
67 #define MCP16502_MODE_AUTO_PFM 0
68 #define MCP16502_MODE_FPWM BIT(6)
70 #define MCP16502_VSEL 0x3F
71 #define MCP16502_EN BIT(7)
72 #define MCP16502_MODE BIT(6)
74 #define MCP16502_MIN_REG 0x0
75 #define MCP16502_MAX_REG 0x65
77 static unsigned int mcp16502_of_map_mode(unsigned int mode
)
79 if (mode
== REGULATOR_MODE_NORMAL
|| mode
== REGULATOR_MODE_IDLE
)
82 return REGULATOR_MODE_INVALID
;
85 #define MCP16502_REGULATOR(_name, _id, _ranges, _ops) \
88 .regulators_node = of_match_ptr("regulators"), \
91 .type = REGULATOR_VOLTAGE, \
92 .owner = THIS_MODULE, \
93 .n_voltages = MCP16502_VSEL + 1, \
94 .linear_ranges = _ranges, \
95 .n_linear_ranges = ARRAY_SIZE(_ranges), \
96 .of_match = of_match_ptr(_name), \
97 .of_map_mode = mcp16502_of_map_mode, \
98 .vsel_reg = (((_id) + 1) << 4), \
99 .vsel_mask = MCP16502_VSEL, \
100 .enable_reg = (((_id) + 1) << 4), \
101 .enable_mask = MCP16502_EN, \
115 * struct mcp16502 - PMIC representation
116 * @rdev: the regulators belonging to this chip
117 * @rmap: regmap to be used for I2C communication
118 * @lpm: LPM GPIO descriptor
121 struct regulator_dev
*rdev
[NUM_REGULATORS
];
123 struct gpio_desc
*lpm
;
127 * mcp16502_gpio_set_mode() - set the GPIO corresponding value
129 * Used to prepare transitioning into hibernate or resuming from it.
131 static void mcp16502_gpio_set_mode(struct mcp16502
*mcp
, int mode
)
134 case MCP16502_OPMODE_ACTIVE
:
135 gpiod_set_value(mcp
->lpm
, 0);
137 case MCP16502_OPMODE_LPM
:
138 case MCP16502_OPMODE_HIB
:
139 gpiod_set_value(mcp
->lpm
, 1);
142 pr_err("%s: %d invalid\n", __func__
, mode
);
147 * mcp16502_get_reg() - get the PMIC's configuration register for opmode
149 * @rdev: the regulator whose register we are searching
150 * @opmode: the PMIC's operating mode ACTIVE, Low-power, Hibernate
152 static int mcp16502_get_reg(struct regulator_dev
*rdev
, int opmode
)
154 int reg
= MCP16502_BASE(rdev_get_id(rdev
));
157 case MCP16502_OPMODE_ACTIVE
:
158 return reg
+ MCP16502_OFFSET_MODE_A
;
159 case MCP16502_OPMODE_LPM
:
160 return reg
+ MCP16502_OFFSET_MODE_LPM
;
161 case MCP16502_OPMODE_HIB
:
162 return reg
+ MCP16502_OFFSET_MODE_HIB
;
169 * mcp16502_get_mode() - return the current operating mode of a regulator
171 * Note: all functions that are not part of entering/exiting standby/suspend
172 * use the Active mode registers.
174 * Note: this is different from the PMIC's operatig mode, it is the
175 * MODE bit from the regulator's register.
177 static unsigned int mcp16502_get_mode(struct regulator_dev
*rdev
)
181 struct mcp16502
*mcp
= rdev_get_drvdata(rdev
);
183 reg
= mcp16502_get_reg(rdev
, MCP16502_OPMODE_ACTIVE
);
187 ret
= regmap_read(mcp
->rmap
, reg
, &val
);
191 switch (val
& MCP16502_MODE
) {
192 case MCP16502_MODE_FPWM
:
193 return REGULATOR_MODE_NORMAL
;
194 case MCP16502_MODE_AUTO_PFM
:
195 return REGULATOR_MODE_IDLE
;
197 return REGULATOR_MODE_INVALID
;
202 * _mcp16502_set_mode() - helper for set_mode and set_suspend_mode
204 * @rdev: the regulator for which we are setting the mode
205 * @mode: the regulator's mode (the one from MODE bit)
206 * @opmode: the PMIC's operating mode: Active/Low-power/Hibernate
208 static int _mcp16502_set_mode(struct regulator_dev
*rdev
, unsigned int mode
,
209 unsigned int op_mode
)
213 struct mcp16502
*mcp
= rdev_get_drvdata(rdev
);
215 reg
= mcp16502_get_reg(rdev
, op_mode
);
220 case REGULATOR_MODE_NORMAL
:
221 val
= MCP16502_MODE_FPWM
;
223 case REGULATOR_MODE_IDLE
:
224 val
= MCP16502_MODE_AUTO_PFM
;
230 reg
= regmap_update_bits(mcp
->rmap
, reg
, MCP16502_MODE
, val
);
235 * mcp16502_set_mode() - regulator_ops set_mode
237 static int mcp16502_set_mode(struct regulator_dev
*rdev
, unsigned int mode
)
239 return _mcp16502_set_mode(rdev
, mode
, MCP16502_OPMODE_ACTIVE
);
243 * mcp16502_get_status() - regulator_ops get_status
245 static int mcp16502_get_status(struct regulator_dev
*rdev
)
249 struct mcp16502
*mcp
= rdev_get_drvdata(rdev
);
251 ret
= regmap_read(mcp
->rmap
, MCP16502_STAT_BASE(rdev_get_id(rdev
)),
256 if (val
& MCP16502_FLT
)
257 return REGULATOR_STATUS_ERROR
;
258 else if (val
& MCP16502_ENS
)
259 return REGULATOR_STATUS_ON
;
260 else if (!(val
& MCP16502_ENS
))
261 return REGULATOR_STATUS_OFF
;
263 return REGULATOR_STATUS_UNDEFINED
;
266 #ifdef CONFIG_SUSPEND
268 * mcp16502_suspend_get_target_reg() - get the reg of the target suspend PMIC
271 static int mcp16502_suspend_get_target_reg(struct regulator_dev
*rdev
)
273 switch (pm_suspend_target_state
) {
274 case PM_SUSPEND_STANDBY
:
275 return mcp16502_get_reg(rdev
, MCP16502_OPMODE_LPM
);
278 return mcp16502_get_reg(rdev
, MCP16502_OPMODE_HIB
);
280 dev_err(&rdev
->dev
, "invalid suspend target: %d\n",
281 pm_suspend_target_state
);
288 * mcp16502_set_suspend_voltage() - regulator_ops set_suspend_voltage
290 static int mcp16502_set_suspend_voltage(struct regulator_dev
*rdev
, int uV
)
292 struct mcp16502
*mcp
= rdev_get_drvdata(rdev
);
293 int sel
= regulator_map_voltage_linear_range(rdev
, uV
, uV
);
294 int reg
= mcp16502_suspend_get_target_reg(rdev
);
302 return regmap_update_bits(mcp
->rmap
, reg
, MCP16502_VSEL
, sel
);
306 * mcp16502_set_suspend_mode() - regulator_ops set_suspend_mode
308 static int mcp16502_set_suspend_mode(struct regulator_dev
*rdev
,
311 switch (pm_suspend_target_state
) {
312 case PM_SUSPEND_STANDBY
:
313 return _mcp16502_set_mode(rdev
, mode
, MCP16502_OPMODE_LPM
);
316 return _mcp16502_set_mode(rdev
, mode
, MCP16502_OPMODE_HIB
);
318 dev_err(&rdev
->dev
, "invalid suspend target: %d\n",
319 pm_suspend_target_state
);
326 * mcp16502_set_suspend_enable() - regulator_ops set_suspend_enable
328 static int mcp16502_set_suspend_enable(struct regulator_dev
*rdev
)
330 struct mcp16502
*mcp
= rdev_get_drvdata(rdev
);
331 int reg
= mcp16502_suspend_get_target_reg(rdev
);
336 return regmap_update_bits(mcp
->rmap
, reg
, MCP16502_EN
, MCP16502_EN
);
340 * mcp16502_set_suspend_disable() - regulator_ops set_suspend_disable
342 static int mcp16502_set_suspend_disable(struct regulator_dev
*rdev
)
344 struct mcp16502
*mcp
= rdev_get_drvdata(rdev
);
345 int reg
= mcp16502_suspend_get_target_reg(rdev
);
350 return regmap_update_bits(mcp
->rmap
, reg
, MCP16502_EN
, 0);
352 #endif /* CONFIG_SUSPEND */
354 static const struct regulator_ops mcp16502_buck_ops
= {
355 .list_voltage
= regulator_list_voltage_linear_range
,
356 .map_voltage
= regulator_map_voltage_linear_range
,
357 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
358 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
359 .enable
= regulator_enable_regmap
,
360 .disable
= regulator_disable_regmap
,
361 .is_enabled
= regulator_is_enabled_regmap
,
362 .get_status
= mcp16502_get_status
,
364 .set_mode
= mcp16502_set_mode
,
365 .get_mode
= mcp16502_get_mode
,
367 #ifdef CONFIG_SUSPEND
368 .set_suspend_voltage
= mcp16502_set_suspend_voltage
,
369 .set_suspend_mode
= mcp16502_set_suspend_mode
,
370 .set_suspend_enable
= mcp16502_set_suspend_enable
,
371 .set_suspend_disable
= mcp16502_set_suspend_disable
,
372 #endif /* CONFIG_SUSPEND */
376 * LDOs cannot change operating modes.
378 static const struct regulator_ops mcp16502_ldo_ops
= {
379 .list_voltage
= regulator_list_voltage_linear_range
,
380 .map_voltage
= regulator_map_voltage_linear_range
,
381 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
382 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
383 .enable
= regulator_enable_regmap
,
384 .disable
= regulator_disable_regmap
,
385 .is_enabled
= regulator_is_enabled_regmap
,
386 .get_status
= mcp16502_get_status
,
388 #ifdef CONFIG_SUSPEND
389 .set_suspend_voltage
= mcp16502_set_suspend_voltage
,
390 .set_suspend_enable
= mcp16502_set_suspend_enable
,
391 .set_suspend_disable
= mcp16502_set_suspend_disable
,
392 #endif /* CONFIG_SUSPEND */
395 static const struct of_device_id mcp16502_ids
[] = {
396 { .compatible
= "microchip,mcp16502", },
399 MODULE_DEVICE_TABLE(of
, mcp16502_ids
);
401 static const struct regulator_linear_range b1l12_ranges
[] = {
402 REGULATOR_LINEAR_RANGE(1200000, VDD_LOW_SEL
, VDD_HIGH_SEL
, 50000),
405 static const struct regulator_linear_range b234_ranges
[] = {
406 REGULATOR_LINEAR_RANGE(600000, VDD_LOW_SEL
, VDD_HIGH_SEL
, 25000),
409 static const struct regulator_desc mcp16502_desc
[] = {
410 /* MCP16502_REGULATOR(_name, _id, ranges, regulator_ops) */
411 MCP16502_REGULATOR("VDD_IO", BUCK1
, b1l12_ranges
, mcp16502_buck_ops
),
412 MCP16502_REGULATOR("VDD_DDR", BUCK2
, b234_ranges
, mcp16502_buck_ops
),
413 MCP16502_REGULATOR("VDD_CORE", BUCK3
, b234_ranges
, mcp16502_buck_ops
),
414 MCP16502_REGULATOR("VDD_OTHER", BUCK4
, b234_ranges
, mcp16502_buck_ops
),
415 MCP16502_REGULATOR("LDO1", LDO1
, b1l12_ranges
, mcp16502_ldo_ops
),
416 MCP16502_REGULATOR("LDO2", LDO2
, b1l12_ranges
, mcp16502_ldo_ops
)
419 static const struct regmap_range mcp16502_ranges
[] = {
420 regmap_reg_range(MCP16502_MIN_REG
, MCP16502_MAX_REG
)
423 static const struct regmap_access_table mcp16502_yes_reg_table
= {
424 .yes_ranges
= mcp16502_ranges
,
425 .n_yes_ranges
= ARRAY_SIZE(mcp16502_ranges
),
428 static const struct regmap_config mcp16502_regmap_config
= {
431 .max_register
= MCP16502_MAX_REG
,
432 .cache_type
= REGCACHE_NONE
,
433 .rd_table
= &mcp16502_yes_reg_table
,
434 .wr_table
= &mcp16502_yes_reg_table
,
438 * set_up_regulators() - initialize all regulators
440 static int setup_regulators(struct mcp16502
*mcp
, struct device
*dev
,
441 struct regulator_config config
)
445 for (i
= 0; i
< NUM_REGULATORS
; i
++) {
446 mcp
->rdev
[i
] = devm_regulator_register(dev
,
449 if (IS_ERR(mcp
->rdev
[i
])) {
451 "failed to register %s regulator %ld\n",
452 mcp16502_desc
[i
].name
, PTR_ERR(mcp
->rdev
[i
]));
453 return PTR_ERR(mcp
->rdev
[i
]);
460 static int mcp16502_probe(struct i2c_client
*client
,
461 const struct i2c_device_id
*id
)
463 struct regulator_config config
= { };
465 struct mcp16502
*mcp
;
471 mcp
= devm_kzalloc(dev
, sizeof(*mcp
), GFP_KERNEL
);
475 mcp
->rmap
= devm_regmap_init_i2c(client
, &mcp16502_regmap_config
);
476 if (IS_ERR(mcp
->rmap
)) {
477 ret
= PTR_ERR(mcp
->rmap
);
478 dev_err(dev
, "regmap init failed: %d\n", ret
);
482 i2c_set_clientdata(client
, mcp
);
483 config
.regmap
= mcp
->rmap
;
484 config
.driver_data
= mcp
;
486 mcp
->lpm
= devm_gpiod_get(dev
, "lpm", GPIOD_OUT_LOW
);
487 if (IS_ERR(mcp
->lpm
)) {
488 dev_err(dev
, "failed to get lpm pin: %ld\n", PTR_ERR(mcp
->lpm
));
489 return PTR_ERR(mcp
->lpm
);
492 ret
= setup_regulators(mcp
, dev
, config
);
496 mcp16502_gpio_set_mode(mcp
, MCP16502_OPMODE_ACTIVE
);
501 #ifdef CONFIG_PM_SLEEP
502 static int mcp16502_suspend_noirq(struct device
*dev
)
504 struct i2c_client
*client
= to_i2c_client(dev
);
505 struct mcp16502
*mcp
= i2c_get_clientdata(client
);
507 mcp16502_gpio_set_mode(mcp
, MCP16502_OPMODE_LPM
);
512 static int mcp16502_resume_noirq(struct device
*dev
)
514 struct i2c_client
*client
= to_i2c_client(dev
);
515 struct mcp16502
*mcp
= i2c_get_clientdata(client
);
517 mcp16502_gpio_set_mode(mcp
, MCP16502_OPMODE_ACTIVE
);
524 static const struct dev_pm_ops mcp16502_pm_ops
= {
525 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(mcp16502_suspend_noirq
,
526 mcp16502_resume_noirq
)
529 static const struct i2c_device_id mcp16502_i2c_id
[] = {
533 MODULE_DEVICE_TABLE(i2c
, mcp16502_i2c_id
);
535 static struct i2c_driver mcp16502_drv
= {
536 .probe
= mcp16502_probe
,
538 .name
= "mcp16502-regulator",
539 .of_match_table
= of_match_ptr(mcp16502_ids
),
541 .pm
= &mcp16502_pm_ops
,
544 .id_table
= mcp16502_i2c_id
,
547 module_i2c_driver(mcp16502_drv
);
549 MODULE_VERSION("1.0");
550 MODULE_LICENSE("GPL v2");
551 MODULE_DESCRIPTION("MCP16502 PMIC driver");
552 MODULE_AUTHOR("Andrei Stefanescu andrei.stefanescu@microchip.com");