xref: /openbmc/linux/drivers/regulator/mcp16502.c (revision 565485b8)
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 
21 #define VDD_LOW_SEL 0x0D
22 #define VDD_HIGH_SEL 0x3F
23 
24 #define MCP16502_FLT BIT(7)
25 #define MCP16502_ENS BIT(0)
26 
27 /*
28  * The PMIC has four sets of registers corresponding to four power modes:
29  * Performance, Active, Low-power, Hibernate.
30  *
31  * Registers:
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.
34  *
35  * Operating modes:
36  * In order for the PMIC to transition to operating modes it has to be
37  * controlled via GPIO lines called LPM and HPM.
38  *
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.
43  *
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).
46  *
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.
50  */
51 
52 /*
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.
55  */
56 #define MCP16502_BASE(i) (((i) + 1) << 4)
57 #define MCP16502_STAT_BASE(i) ((i) + 5)
58 
59 #define MCP16502_OFFSET_MODE_A 0
60 #define MCP16502_OFFSET_MODE_LPM 1
61 #define MCP16502_OFFSET_MODE_HIB 2
62 
63 #define MCP16502_OPMODE_ACTIVE REGULATOR_MODE_NORMAL
64 #define MCP16502_OPMODE_LPM REGULATOR_MODE_IDLE
65 #define MCP16502_OPMODE_HIB REGULATOR_MODE_STANDBY
66 
67 #define MCP16502_MODE_AUTO_PFM 0
68 #define MCP16502_MODE_FPWM BIT(6)
69 
70 #define MCP16502_VSEL 0x3F
71 #define MCP16502_EN BIT(7)
72 #define MCP16502_MODE BIT(6)
73 
74 #define MCP16502_MIN_REG 0x0
75 #define MCP16502_MAX_REG 0x65
76 
77 static unsigned int mcp16502_of_map_mode(unsigned int mode)
78 {
79 	if (mode == REGULATOR_MODE_NORMAL || mode == REGULATOR_MODE_IDLE)
80 		return mode;
81 
82 	return REGULATOR_MODE_INVALID;
83 }
84 
85 #define MCP16502_REGULATOR(_name, _id, _ranges, _ops)			\
86 	[_id] = {							\
87 		.name			= _name,			\
88 		.regulators_node	= of_match_ptr("regulators"),	\
89 		.id			= _id,				\
90 		.ops			= &(_ops),			\
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,			\
102 	}
103 
104 enum {
105 	BUCK1 = 0,
106 	BUCK2,
107 	BUCK3,
108 	BUCK4,
109 	LDO1,
110 	LDO2,
111 	NUM_REGULATORS
112 };
113 
114 /*
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
119  */
120 struct mcp16502 {
121 	struct regulator_dev *rdev[NUM_REGULATORS];
122 	struct regmap *rmap;
123 	struct gpio_desc *lpm;
124 };
125 
126 /*
127  * mcp16502_gpio_set_mode() - set the GPIO corresponding value
128  *
129  * Used to prepare transitioning into hibernate or resuming from it.
130  */
131 static void mcp16502_gpio_set_mode(struct mcp16502 *mcp, int mode)
132 {
133 	switch (mode) {
134 	case MCP16502_OPMODE_ACTIVE:
135 		gpiod_set_value(mcp->lpm, 0);
136 		break;
137 	case MCP16502_OPMODE_LPM:
138 	case MCP16502_OPMODE_HIB:
139 		gpiod_set_value(mcp->lpm, 1);
140 		break;
141 	default:
142 		pr_err("%s: %d invalid\n", __func__, mode);
143 	}
144 }
145 
146 /*
147  * mcp16502_get_reg() - get the PMIC's configuration register for opmode
148  *
149  * @rdev: the regulator whose register we are searching
150  * @opmode: the PMIC's operating mode ACTIVE, Low-power, Hibernate
151  */
152 static int mcp16502_get_reg(struct regulator_dev *rdev, int opmode)
153 {
154 	int reg = MCP16502_BASE(rdev_get_id(rdev));
155 
156 	switch (opmode) {
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;
163 	default:
164 		return -EINVAL;
165 	}
166 }
167 
168 /*
169  * mcp16502_get_mode() - return the current operating mode of a regulator
170  *
171  * Note: all functions that are not part of entering/exiting standby/suspend
172  *	 use the Active mode registers.
173  *
174  * Note: this is different from the PMIC's operatig mode, it is the
175  *	 MODE bit from the regulator's register.
176  */
177 static unsigned int mcp16502_get_mode(struct regulator_dev *rdev)
178 {
179 	unsigned int val;
180 	int ret, reg;
181 	struct mcp16502 *mcp = rdev_get_drvdata(rdev);
182 
183 	reg = mcp16502_get_reg(rdev, MCP16502_OPMODE_ACTIVE);
184 	if (reg < 0)
185 		return reg;
186 
187 	ret = regmap_read(mcp->rmap, reg, &val);
188 	if (ret)
189 		return ret;
190 
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;
196 	default:
197 		return REGULATOR_MODE_INVALID;
198 	}
199 }
200 
201 /*
202  * _mcp16502_set_mode() - helper for set_mode and set_suspend_mode
203  *
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
207  */
208 static int _mcp16502_set_mode(struct regulator_dev *rdev, unsigned int mode,
209 			      unsigned int op_mode)
210 {
211 	int val;
212 	int reg;
213 	struct mcp16502 *mcp = rdev_get_drvdata(rdev);
214 
215 	reg = mcp16502_get_reg(rdev, op_mode);
216 	if (reg < 0)
217 		return reg;
218 
219 	switch (mode) {
220 	case REGULATOR_MODE_NORMAL:
221 		val = MCP16502_MODE_FPWM;
222 		break;
223 	case REGULATOR_MODE_IDLE:
224 		val = MCP16502_MODE_AUTO_PFM;
225 		break;
226 	default:
227 		return -EINVAL;
228 	}
229 
230 	reg = regmap_update_bits(mcp->rmap, reg, MCP16502_MODE, val);
231 	return reg;
232 }
233 
234 /*
235  * mcp16502_set_mode() - regulator_ops set_mode
236  */
237 static int mcp16502_set_mode(struct regulator_dev *rdev, unsigned int mode)
238 {
239 	return _mcp16502_set_mode(rdev, mode, MCP16502_OPMODE_ACTIVE);
240 }
241 
242 /*
243  * mcp16502_get_status() - regulator_ops get_status
244  */
245 static int mcp16502_get_status(struct regulator_dev *rdev)
246 {
247 	int ret;
248 	unsigned int val;
249 	struct mcp16502 *mcp = rdev_get_drvdata(rdev);
250 
251 	ret = regmap_read(mcp->rmap, MCP16502_STAT_BASE(rdev_get_id(rdev)),
252 			  &val);
253 	if (ret)
254 		return ret;
255 
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;
262 
263 	return REGULATOR_STATUS_UNDEFINED;
264 }
265 
266 #ifdef CONFIG_SUSPEND
267 /*
268  * mcp16502_suspend_get_target_reg() - get the reg of the target suspend PMIC
269  *				       mode
270  */
271 static int mcp16502_suspend_get_target_reg(struct regulator_dev *rdev)
272 {
273 	switch (pm_suspend_target_state) {
274 	case PM_SUSPEND_STANDBY:
275 		return mcp16502_get_reg(rdev, MCP16502_OPMODE_LPM);
276 	case PM_SUSPEND_ON:
277 	case PM_SUSPEND_MEM:
278 		return mcp16502_get_reg(rdev, MCP16502_OPMODE_HIB);
279 	default:
280 		dev_err(&rdev->dev, "invalid suspend target: %d\n",
281 			pm_suspend_target_state);
282 	}
283 
284 	return -EINVAL;
285 }
286 
287 /*
288  * mcp16502_set_suspend_voltage() - regulator_ops set_suspend_voltage
289  */
290 static int mcp16502_set_suspend_voltage(struct regulator_dev *rdev, int uV)
291 {
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);
295 
296 	if (sel < 0)
297 		return sel;
298 
299 	if (reg < 0)
300 		return reg;
301 
302 	return regmap_update_bits(mcp->rmap, reg, MCP16502_VSEL, sel);
303 }
304 
305 /*
306  * mcp16502_set_suspend_mode() - regulator_ops set_suspend_mode
307  */
308 static int mcp16502_set_suspend_mode(struct regulator_dev *rdev,
309 				     unsigned int mode)
310 {
311 	switch (pm_suspend_target_state) {
312 	case PM_SUSPEND_STANDBY:
313 		return _mcp16502_set_mode(rdev, mode, MCP16502_OPMODE_LPM);
314 	case PM_SUSPEND_ON:
315 	case PM_SUSPEND_MEM:
316 		return _mcp16502_set_mode(rdev, mode, MCP16502_OPMODE_HIB);
317 	default:
318 		dev_err(&rdev->dev, "invalid suspend target: %d\n",
319 			pm_suspend_target_state);
320 	}
321 
322 	return -EINVAL;
323 }
324 
325 /*
326  * mcp16502_set_suspend_enable() - regulator_ops set_suspend_enable
327  */
328 static int mcp16502_set_suspend_enable(struct regulator_dev *rdev)
329 {
330 	struct mcp16502 *mcp = rdev_get_drvdata(rdev);
331 	int reg = mcp16502_suspend_get_target_reg(rdev);
332 
333 	if (reg < 0)
334 		return reg;
335 
336 	return regmap_update_bits(mcp->rmap, reg, MCP16502_EN, MCP16502_EN);
337 }
338 
339 /*
340  * mcp16502_set_suspend_disable() - regulator_ops set_suspend_disable
341  */
342 static int mcp16502_set_suspend_disable(struct regulator_dev *rdev)
343 {
344 	struct mcp16502 *mcp = rdev_get_drvdata(rdev);
345 	int reg = mcp16502_suspend_get_target_reg(rdev);
346 
347 	if (reg < 0)
348 		return reg;
349 
350 	return regmap_update_bits(mcp->rmap, reg, MCP16502_EN, 0);
351 }
352 #endif /* CONFIG_SUSPEND */
353 
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,
363 
364 	.set_mode			= mcp16502_set_mode,
365 	.get_mode			= mcp16502_get_mode,
366 
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 */
373 };
374 
375 /*
376  * LDOs cannot change operating modes.
377  */
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,
387 
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 */
393 };
394 
395 static const struct of_device_id mcp16502_ids[] = {
396 	{ .compatible = "microchip,mcp16502", },
397 	{}
398 };
399 MODULE_DEVICE_TABLE(of, mcp16502_ids);
400 
401 static const struct regulator_linear_range b1l12_ranges[] = {
402 	REGULATOR_LINEAR_RANGE(1200000, VDD_LOW_SEL, VDD_HIGH_SEL, 50000),
403 };
404 
405 static const struct regulator_linear_range b234_ranges[] = {
406 	REGULATOR_LINEAR_RANGE(600000, VDD_LOW_SEL, VDD_HIGH_SEL, 25000),
407 };
408 
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)
417 };
418 
419 static const struct regmap_range mcp16502_ranges[] = {
420 	regmap_reg_range(MCP16502_MIN_REG, MCP16502_MAX_REG)
421 };
422 
423 static const struct regmap_access_table mcp16502_yes_reg_table = {
424 	.yes_ranges = mcp16502_ranges,
425 	.n_yes_ranges = ARRAY_SIZE(mcp16502_ranges),
426 };
427 
428 static const struct regmap_config mcp16502_regmap_config = {
429 	.reg_bits	= 8,
430 	.val_bits	= 8,
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,
435 };
436 
437 /*
438  * set_up_regulators() - initialize all regulators
439  */
440 static int setup_regulators(struct mcp16502 *mcp, struct device *dev,
441 			    struct regulator_config config)
442 {
443 	int i;
444 
445 	for (i = 0; i < NUM_REGULATORS; i++) {
446 		mcp->rdev[i] = devm_regulator_register(dev,
447 						       &mcp16502_desc[i],
448 						       &config);
449 		if (IS_ERR(mcp->rdev[i])) {
450 			dev_err(dev,
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]);
454 		}
455 	}
456 
457 	return 0;
458 }
459 
460 static int mcp16502_probe(struct i2c_client *client,
461 			  const struct i2c_device_id *id)
462 {
463 	struct regulator_config config = { };
464 	struct device *dev;
465 	struct mcp16502 *mcp;
466 	int ret = 0;
467 
468 	dev = &client->dev;
469 	config.dev = dev;
470 
471 	mcp = devm_kzalloc(dev, sizeof(*mcp), GFP_KERNEL);
472 	if (!mcp)
473 		return -ENOMEM;
474 
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);
479 		return ret;
480 	}
481 
482 	i2c_set_clientdata(client, mcp);
483 	config.regmap = mcp->rmap;
484 	config.driver_data = mcp;
485 
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);
490 	}
491 
492 	ret = setup_regulators(mcp, dev, config);
493 	if (ret != 0)
494 		return ret;
495 
496 	mcp16502_gpio_set_mode(mcp, MCP16502_OPMODE_ACTIVE);
497 
498 	return 0;
499 }
500 
501 #ifdef CONFIG_PM_SLEEP
502 static int mcp16502_suspend_noirq(struct device *dev)
503 {
504 	struct i2c_client *client = to_i2c_client(dev);
505 	struct mcp16502 *mcp = i2c_get_clientdata(client);
506 
507 	mcp16502_gpio_set_mode(mcp, MCP16502_OPMODE_LPM);
508 
509 	return 0;
510 }
511 
512 static int mcp16502_resume_noirq(struct device *dev)
513 {
514 	struct i2c_client *client = to_i2c_client(dev);
515 	struct mcp16502 *mcp = i2c_get_clientdata(client);
516 
517 	mcp16502_gpio_set_mode(mcp, MCP16502_OPMODE_ACTIVE);
518 
519 	return 0;
520 }
521 #endif
522 
523 #ifdef CONFIG_PM
524 static const struct dev_pm_ops mcp16502_pm_ops = {
525 	SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(mcp16502_suspend_noirq,
526 				      mcp16502_resume_noirq)
527 };
528 #endif
529 static const struct i2c_device_id mcp16502_i2c_id[] = {
530 	{ "mcp16502", 0 },
531 	{ }
532 };
533 MODULE_DEVICE_TABLE(i2c, mcp16502_i2c_id);
534 
535 static struct i2c_driver mcp16502_drv = {
536 	.probe		= mcp16502_probe,
537 	.driver		= {
538 		.name	= "mcp16502-regulator",
539 		.of_match_table	= of_match_ptr(mcp16502_ids),
540 #ifdef CONFIG_PM
541 		.pm = &mcp16502_pm_ops,
542 #endif
543 	},
544 	.id_table	= mcp16502_i2c_id,
545 };
546 
547 module_i2c_driver(mcp16502_drv);
548 
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");
553