xref: /openbmc/linux/drivers/regulator/mcp16502.c (revision c4f461a1)
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_HPM: high-performance mode register
81  * @MCP16502_REG_SEQ: startup sequence register
82  * @MCP16502_REG_CFG: configuration register
83  */
84 enum mcp16502_reg {
85 	MCP16502_REG_A,
86 	MCP16502_REG_LPM,
87 	MCP16502_REG_HIB,
88 	MCP16502_REG_HPM,
89 	MCP16502_REG_SEQ,
90 	MCP16502_REG_CFG,
91 };
92 
93 /* Ramp delay (uV/us) for buck1, ldo1, ldo2. */
94 static const unsigned int mcp16502_ramp_b1l12[] = {
95 	6250, 3125, 2083, 1563
96 };
97 
98 /* Ramp delay (uV/us) for buck2, buck3, buck4. */
99 static const unsigned int mcp16502_ramp_b234[] = {
100 	3125, 1563, 1042, 781
101 };
102 
103 static unsigned int mcp16502_of_map_mode(unsigned int mode)
104 {
105 	if (mode == REGULATOR_MODE_NORMAL || mode == REGULATOR_MODE_IDLE)
106 		return mode;
107 
108 	return REGULATOR_MODE_INVALID;
109 }
110 
111 #define MCP16502_REGULATOR(_name, _id, _ranges, _ops, _ramp_table)	\
112 	[_id] = {							\
113 		.name			= _name,			\
114 		.regulators_node	= of_match_ptr("regulators"),	\
115 		.id			= _id,				\
116 		.ops			= &(_ops),			\
117 		.type			= REGULATOR_VOLTAGE,		\
118 		.owner			= THIS_MODULE,			\
119 		.n_voltages		= MCP16502_VSEL + 1,		\
120 		.linear_ranges		= _ranges,			\
121 		.linear_min_sel		= VDD_LOW_SEL,			\
122 		.n_linear_ranges	= ARRAY_SIZE(_ranges),		\
123 		.of_match		= of_match_ptr(_name),		\
124 		.of_map_mode		= mcp16502_of_map_mode,		\
125 		.vsel_reg		= (((_id) + 1) << 4),		\
126 		.vsel_mask		= MCP16502_VSEL,		\
127 		.enable_reg		= (((_id) + 1) << 4),		\
128 		.enable_mask		= MCP16502_EN,			\
129 		.ramp_reg		= MCP16502_REG_BASE(_id, CFG),	\
130 		.ramp_mask		= MCP16502_DVSR,		\
131 		.ramp_delay_table	= _ramp_table,			\
132 		.n_ramp_values		= ARRAY_SIZE(_ramp_table),	\
133 	}
134 
135 enum {
136 	BUCK1 = 0,
137 	BUCK2,
138 	BUCK3,
139 	BUCK4,
140 	LDO1,
141 	LDO2,
142 	NUM_REGULATORS
143 };
144 
145 /*
146  * struct mcp16502 - PMIC representation
147  * @lpm: LPM GPIO descriptor
148  */
149 struct mcp16502 {
150 	struct gpio_desc *lpm;
151 };
152 
153 /*
154  * mcp16502_gpio_set_mode() - set the GPIO corresponding value
155  *
156  * Used to prepare transitioning into hibernate or resuming from it.
157  */
158 static void mcp16502_gpio_set_mode(struct mcp16502 *mcp, int mode)
159 {
160 	switch (mode) {
161 	case MCP16502_OPMODE_ACTIVE:
162 		gpiod_set_value(mcp->lpm, 0);
163 		break;
164 	case MCP16502_OPMODE_LPM:
165 	case MCP16502_OPMODE_HIB:
166 		gpiod_set_value(mcp->lpm, 1);
167 		break;
168 	default:
169 		pr_err("%s: %d invalid\n", __func__, mode);
170 	}
171 }
172 
173 /*
174  * mcp16502_get_reg() - get the PMIC's state configuration register for opmode
175  *
176  * @rdev: the regulator whose register we are searching
177  * @opmode: the PMIC's operating mode ACTIVE, Low-power, Hibernate
178  */
179 static int mcp16502_get_state_reg(struct regulator_dev *rdev, int opmode)
180 {
181 	switch (opmode) {
182 	case MCP16502_OPMODE_ACTIVE:
183 		return MCP16502_REG_BASE(rdev_get_id(rdev), A);
184 	case MCP16502_OPMODE_LPM:
185 		return MCP16502_REG_BASE(rdev_get_id(rdev), LPM);
186 	case MCP16502_OPMODE_HIB:
187 		return MCP16502_REG_BASE(rdev_get_id(rdev), HIB);
188 	default:
189 		return -EINVAL;
190 	}
191 }
192 
193 /*
194  * mcp16502_get_mode() - return the current operating mode of a regulator
195  *
196  * Note: all functions that are not part of entering/exiting standby/suspend
197  *	 use the Active mode registers.
198  *
199  * Note: this is different from the PMIC's operatig mode, it is the
200  *	 MODE bit from the regulator's register.
201  */
202 static unsigned int mcp16502_get_mode(struct regulator_dev *rdev)
203 {
204 	unsigned int val;
205 	int ret, reg;
206 
207 	reg = mcp16502_get_state_reg(rdev, MCP16502_OPMODE_ACTIVE);
208 	if (reg < 0)
209 		return reg;
210 
211 	ret = regmap_read(rdev->regmap, reg, &val);
212 	if (ret)
213 		return ret;
214 
215 	switch (val & MCP16502_MODE) {
216 	case MCP16502_MODE_FPWM:
217 		return REGULATOR_MODE_NORMAL;
218 	case MCP16502_MODE_AUTO_PFM:
219 		return REGULATOR_MODE_IDLE;
220 	default:
221 		return REGULATOR_MODE_INVALID;
222 	}
223 }
224 
225 /*
226  * _mcp16502_set_mode() - helper for set_mode and set_suspend_mode
227  *
228  * @rdev: the regulator for which we are setting the mode
229  * @mode: the regulator's mode (the one from MODE bit)
230  * @opmode: the PMIC's operating mode: Active/Low-power/Hibernate
231  */
232 static int _mcp16502_set_mode(struct regulator_dev *rdev, unsigned int mode,
233 			      unsigned int op_mode)
234 {
235 	int val;
236 	int reg;
237 
238 	reg = mcp16502_get_state_reg(rdev, op_mode);
239 	if (reg < 0)
240 		return reg;
241 
242 	switch (mode) {
243 	case REGULATOR_MODE_NORMAL:
244 		val = MCP16502_MODE_FPWM;
245 		break;
246 	case REGULATOR_MODE_IDLE:
247 		val = MCP16502_MODE_AUTO_PFM;
248 		break;
249 	default:
250 		return -EINVAL;
251 	}
252 
253 	reg = regmap_update_bits(rdev->regmap, reg, MCP16502_MODE, val);
254 	return reg;
255 }
256 
257 /*
258  * mcp16502_set_mode() - regulator_ops set_mode
259  */
260 static int mcp16502_set_mode(struct regulator_dev *rdev, unsigned int mode)
261 {
262 	return _mcp16502_set_mode(rdev, mode, MCP16502_OPMODE_ACTIVE);
263 }
264 
265 /*
266  * mcp16502_get_status() - regulator_ops get_status
267  */
268 static int mcp16502_get_status(struct regulator_dev *rdev)
269 {
270 	int ret;
271 	unsigned int val;
272 
273 	ret = regmap_read(rdev->regmap, MCP16502_STAT_BASE(rdev_get_id(rdev)),
274 			  &val);
275 	if (ret)
276 		return ret;
277 
278 	if (val & MCP16502_FLT)
279 		return REGULATOR_STATUS_ERROR;
280 	else if (val & MCP16502_ENS)
281 		return REGULATOR_STATUS_ON;
282 	else if (!(val & MCP16502_ENS))
283 		return REGULATOR_STATUS_OFF;
284 
285 	return REGULATOR_STATUS_UNDEFINED;
286 }
287 
288 static int mcp16502_set_voltage_time_sel(struct regulator_dev *rdev,
289 					 unsigned int old_sel,
290 					 unsigned int new_sel)
291 {
292 	static const u8 us_ramp[] = { 8, 16, 24, 32 };
293 	int id = rdev_get_id(rdev);
294 	unsigned int uV_delta, val;
295 	int ret;
296 
297 	ret = regmap_read(rdev->regmap, MCP16502_REG_BASE(id, CFG), &val);
298 	if (ret)
299 		return ret;
300 
301 	val = (val & MCP16502_DVSR) >> 2;
302 	uV_delta = abs(new_sel * rdev->desc->linear_ranges->step -
303 		       old_sel * rdev->desc->linear_ranges->step);
304 	switch (id) {
305 	case BUCK1:
306 	case LDO1:
307 	case LDO2:
308 		ret = DIV_ROUND_CLOSEST(uV_delta * us_ramp[val],
309 					mcp16502_ramp_b1l12[val]);
310 		break;
311 
312 	case BUCK2:
313 	case BUCK3:
314 	case BUCK4:
315 		ret = DIV_ROUND_CLOSEST(uV_delta * us_ramp[val],
316 					mcp16502_ramp_b234[val]);
317 		break;
318 
319 	default:
320 		return -EINVAL;
321 	}
322 
323 	return ret;
324 }
325 
326 #ifdef CONFIG_SUSPEND
327 /*
328  * mcp16502_suspend_get_target_reg() - get the reg of the target suspend PMIC
329  *				       mode
330  */
331 static int mcp16502_suspend_get_target_reg(struct regulator_dev *rdev)
332 {
333 	switch (pm_suspend_target_state) {
334 	case PM_SUSPEND_STANDBY:
335 		return mcp16502_get_state_reg(rdev, MCP16502_OPMODE_LPM);
336 	case PM_SUSPEND_ON:
337 	case PM_SUSPEND_MEM:
338 		return mcp16502_get_state_reg(rdev, MCP16502_OPMODE_HIB);
339 	default:
340 		dev_err(&rdev->dev, "invalid suspend target: %d\n",
341 			pm_suspend_target_state);
342 	}
343 
344 	return -EINVAL;
345 }
346 
347 /*
348  * mcp16502_set_suspend_voltage() - regulator_ops set_suspend_voltage
349  */
350 static int mcp16502_set_suspend_voltage(struct regulator_dev *rdev, int uV)
351 {
352 	int sel = regulator_map_voltage_linear_range(rdev, uV, uV);
353 	int reg = mcp16502_suspend_get_target_reg(rdev);
354 
355 	if (sel < 0)
356 		return sel;
357 
358 	if (reg < 0)
359 		return reg;
360 
361 	return regmap_update_bits(rdev->regmap, reg, MCP16502_VSEL, sel);
362 }
363 
364 /*
365  * mcp16502_set_suspend_mode() - regulator_ops set_suspend_mode
366  */
367 static int mcp16502_set_suspend_mode(struct regulator_dev *rdev,
368 				     unsigned int mode)
369 {
370 	switch (pm_suspend_target_state) {
371 	case PM_SUSPEND_STANDBY:
372 		return _mcp16502_set_mode(rdev, mode, MCP16502_OPMODE_LPM);
373 	case PM_SUSPEND_ON:
374 	case PM_SUSPEND_MEM:
375 		return _mcp16502_set_mode(rdev, mode, MCP16502_OPMODE_HIB);
376 	default:
377 		dev_err(&rdev->dev, "invalid suspend target: %d\n",
378 			pm_suspend_target_state);
379 	}
380 
381 	return -EINVAL;
382 }
383 
384 /*
385  * mcp16502_set_suspend_enable() - regulator_ops set_suspend_enable
386  */
387 static int mcp16502_set_suspend_enable(struct regulator_dev *rdev)
388 {
389 	int reg = mcp16502_suspend_get_target_reg(rdev);
390 
391 	if (reg < 0)
392 		return reg;
393 
394 	return regmap_update_bits(rdev->regmap, reg, MCP16502_EN, MCP16502_EN);
395 }
396 
397 /*
398  * mcp16502_set_suspend_disable() - regulator_ops set_suspend_disable
399  */
400 static int mcp16502_set_suspend_disable(struct regulator_dev *rdev)
401 {
402 	int reg = mcp16502_suspend_get_target_reg(rdev);
403 
404 	if (reg < 0)
405 		return reg;
406 
407 	return regmap_update_bits(rdev->regmap, reg, MCP16502_EN, 0);
408 }
409 #endif /* CONFIG_SUSPEND */
410 
411 static const struct regulator_ops mcp16502_buck_ops = {
412 	.list_voltage			= regulator_list_voltage_linear_range,
413 	.map_voltage			= regulator_map_voltage_linear_range,
414 	.get_voltage_sel		= regulator_get_voltage_sel_regmap,
415 	.set_voltage_sel		= regulator_set_voltage_sel_regmap,
416 	.enable				= regulator_enable_regmap,
417 	.disable			= regulator_disable_regmap,
418 	.is_enabled			= regulator_is_enabled_regmap,
419 	.get_status			= mcp16502_get_status,
420 	.set_voltage_time_sel		= mcp16502_set_voltage_time_sel,
421 	.set_ramp_delay			= regulator_set_ramp_delay_regmap,
422 
423 	.set_mode			= mcp16502_set_mode,
424 	.get_mode			= mcp16502_get_mode,
425 
426 #ifdef CONFIG_SUSPEND
427 	.set_suspend_voltage		= mcp16502_set_suspend_voltage,
428 	.set_suspend_mode		= mcp16502_set_suspend_mode,
429 	.set_suspend_enable		= mcp16502_set_suspend_enable,
430 	.set_suspend_disable		= mcp16502_set_suspend_disable,
431 #endif /* CONFIG_SUSPEND */
432 };
433 
434 /*
435  * LDOs cannot change operating modes.
436  */
437 static const struct regulator_ops mcp16502_ldo_ops = {
438 	.list_voltage			= regulator_list_voltage_linear_range,
439 	.map_voltage			= regulator_map_voltage_linear_range,
440 	.get_voltage_sel		= regulator_get_voltage_sel_regmap,
441 	.set_voltage_sel		= regulator_set_voltage_sel_regmap,
442 	.enable				= regulator_enable_regmap,
443 	.disable			= regulator_disable_regmap,
444 	.is_enabled			= regulator_is_enabled_regmap,
445 	.get_status			= mcp16502_get_status,
446 	.set_voltage_time_sel		= mcp16502_set_voltage_time_sel,
447 	.set_ramp_delay			= regulator_set_ramp_delay_regmap,
448 
449 #ifdef CONFIG_SUSPEND
450 	.set_suspend_voltage		= mcp16502_set_suspend_voltage,
451 	.set_suspend_enable		= mcp16502_set_suspend_enable,
452 	.set_suspend_disable		= mcp16502_set_suspend_disable,
453 #endif /* CONFIG_SUSPEND */
454 };
455 
456 static const struct of_device_id mcp16502_ids[] = {
457 	{ .compatible = "microchip,mcp16502", },
458 	{}
459 };
460 MODULE_DEVICE_TABLE(of, mcp16502_ids);
461 
462 static const struct linear_range b1l12_ranges[] = {
463 	REGULATOR_LINEAR_RANGE(1200000, VDD_LOW_SEL, VDD_HIGH_SEL, 50000),
464 };
465 
466 static const struct linear_range b234_ranges[] = {
467 	REGULATOR_LINEAR_RANGE(600000, VDD_LOW_SEL, VDD_HIGH_SEL, 25000),
468 };
469 
470 static const struct regulator_desc mcp16502_desc[] = {
471 	/* MCP16502_REGULATOR(_name, _id, ranges, regulator_ops, ramp_table) */
472 	MCP16502_REGULATOR("VDD_IO", BUCK1, b1l12_ranges, mcp16502_buck_ops,
473 			   mcp16502_ramp_b1l12),
474 	MCP16502_REGULATOR("VDD_DDR", BUCK2, b234_ranges, mcp16502_buck_ops,
475 			   mcp16502_ramp_b234),
476 	MCP16502_REGULATOR("VDD_CORE", BUCK3, b234_ranges, mcp16502_buck_ops,
477 			   mcp16502_ramp_b234),
478 	MCP16502_REGULATOR("VDD_OTHER", BUCK4, b234_ranges, mcp16502_buck_ops,
479 			   mcp16502_ramp_b234),
480 	MCP16502_REGULATOR("LDO1", LDO1, b1l12_ranges, mcp16502_ldo_ops,
481 			   mcp16502_ramp_b1l12),
482 	MCP16502_REGULATOR("LDO2", LDO2, b1l12_ranges, mcp16502_ldo_ops,
483 			   mcp16502_ramp_b1l12)
484 };
485 
486 static const struct regmap_range mcp16502_ranges[] = {
487 	regmap_reg_range(MCP16502_MIN_REG, MCP16502_MAX_REG)
488 };
489 
490 static const struct regmap_access_table mcp16502_yes_reg_table = {
491 	.yes_ranges = mcp16502_ranges,
492 	.n_yes_ranges = ARRAY_SIZE(mcp16502_ranges),
493 };
494 
495 static const struct regmap_config mcp16502_regmap_config = {
496 	.reg_bits	= 8,
497 	.val_bits	= 8,
498 	.max_register	= MCP16502_MAX_REG,
499 	.cache_type	= REGCACHE_NONE,
500 	.rd_table	= &mcp16502_yes_reg_table,
501 	.wr_table	= &mcp16502_yes_reg_table,
502 };
503 
504 static int mcp16502_probe(struct i2c_client *client)
505 {
506 	struct regulator_config config = { };
507 	struct regulator_dev *rdev;
508 	struct device *dev;
509 	struct mcp16502 *mcp;
510 	struct regmap *rmap;
511 	int i, ret;
512 
513 	dev = &client->dev;
514 	config.dev = dev;
515 
516 	mcp = devm_kzalloc(dev, sizeof(*mcp), GFP_KERNEL);
517 	if (!mcp)
518 		return -ENOMEM;
519 
520 	rmap = devm_regmap_init_i2c(client, &mcp16502_regmap_config);
521 	if (IS_ERR(rmap)) {
522 		ret = PTR_ERR(rmap);
523 		dev_err(dev, "regmap init failed: %d\n", ret);
524 		return ret;
525 	}
526 
527 	i2c_set_clientdata(client, mcp);
528 	config.regmap = rmap;
529 	config.driver_data = mcp;
530 
531 	mcp->lpm = devm_gpiod_get_optional(dev, "lpm", GPIOD_OUT_LOW);
532 	if (IS_ERR(mcp->lpm)) {
533 		dev_err(dev, "failed to get lpm pin: %ld\n", PTR_ERR(mcp->lpm));
534 		return PTR_ERR(mcp->lpm);
535 	}
536 
537 	for (i = 0; i < NUM_REGULATORS; i++) {
538 		rdev = devm_regulator_register(dev, &mcp16502_desc[i], &config);
539 		if (IS_ERR(rdev)) {
540 			dev_err(dev,
541 				"failed to register %s regulator %ld\n",
542 				mcp16502_desc[i].name, PTR_ERR(rdev));
543 			return PTR_ERR(rdev);
544 		}
545 	}
546 
547 	mcp16502_gpio_set_mode(mcp, MCP16502_OPMODE_ACTIVE);
548 
549 	return 0;
550 }
551 
552 #ifdef CONFIG_PM_SLEEP
553 static int mcp16502_suspend_noirq(struct device *dev)
554 {
555 	struct i2c_client *client = to_i2c_client(dev);
556 	struct mcp16502 *mcp = i2c_get_clientdata(client);
557 
558 	mcp16502_gpio_set_mode(mcp, MCP16502_OPMODE_LPM);
559 
560 	return 0;
561 }
562 
563 static int mcp16502_resume_noirq(struct device *dev)
564 {
565 	struct i2c_client *client = to_i2c_client(dev);
566 	struct mcp16502 *mcp = i2c_get_clientdata(client);
567 
568 	mcp16502_gpio_set_mode(mcp, MCP16502_OPMODE_ACTIVE);
569 
570 	return 0;
571 }
572 #endif
573 
574 #ifdef CONFIG_PM
575 static const struct dev_pm_ops mcp16502_pm_ops = {
576 	SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(mcp16502_suspend_noirq,
577 				      mcp16502_resume_noirq)
578 };
579 #endif
580 static const struct i2c_device_id mcp16502_i2c_id[] = {
581 	{ "mcp16502", 0 },
582 	{ }
583 };
584 MODULE_DEVICE_TABLE(i2c, mcp16502_i2c_id);
585 
586 static struct i2c_driver mcp16502_drv = {
587 	.probe_new	= mcp16502_probe,
588 	.driver		= {
589 		.name	= "mcp16502-regulator",
590 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
591 		.of_match_table	= of_match_ptr(mcp16502_ids),
592 #ifdef CONFIG_PM
593 		.pm = &mcp16502_pm_ops,
594 #endif
595 	},
596 	.id_table	= mcp16502_i2c_id,
597 };
598 
599 module_i2c_driver(mcp16502_drv);
600 
601 MODULE_LICENSE("GPL v2");
602 MODULE_DESCRIPTION("MCP16502 PMIC driver");
603 MODULE_AUTHOR("Andrei Stefanescu andrei.stefanescu@microchip.com");
604