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