blob: cb11fb206899afad4a085043977b2001338d5df0 [file] [log] [blame]
Andrei.Stefanescu@microchip.com919261c2018-12-12 18:31:18 +00001// 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
77static 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 .linear_ranges = _ranges, \
94 .n_linear_ranges = ARRAY_SIZE(_ranges), \
95 .of_match = of_match_ptr(_name), \
96 .of_map_mode = mcp16502_of_map_mode, \
97 .vsel_reg = (((_id) + 1) << 4), \
98 .vsel_mask = MCP16502_VSEL, \
99 .enable_reg = (((_id) + 1) << 4), \
100 .enable_mask = MCP16502_EN, \
101 }
102
103enum {
104 BUCK1 = 0,
105 BUCK2,
106 BUCK3,
107 BUCK4,
108 LDO1,
109 LDO2,
110 NUM_REGULATORS
111};
112
113/*
114 * struct mcp16502 - PMIC representation
115 * @rdev: the regulators belonging to this chip
116 * @rmap: regmap to be used for I2C communication
117 * @lpm: LPM GPIO descriptor
118 */
119struct mcp16502 {
120 struct regulator_dev *rdev[NUM_REGULATORS];
121 struct regmap *rmap;
122 struct gpio_desc *lpm;
123};
124
125/*
126 * mcp16502_gpio_set_mode() - set the GPIO corresponding value
127 *
128 * Used to prepare transitioning into hibernate or resuming from it.
129 */
130static void mcp16502_gpio_set_mode(struct mcp16502 *mcp, int mode)
131{
132 switch (mode) {
133 case MCP16502_OPMODE_ACTIVE:
134 gpiod_set_value(mcp->lpm, 0);
135 break;
136 case MCP16502_OPMODE_LPM:
137 case MCP16502_OPMODE_HIB:
138 gpiod_set_value(mcp->lpm, 1);
139 break;
140 default:
141 pr_err("%s: %d invalid\n", __func__, mode);
142 }
143}
144
145/*
146 * mcp16502_get_reg() - get the PMIC's configuration register for opmode
147 *
148 * @rdev: the regulator whose register we are searching
149 * @opmode: the PMIC's operating mode ACTIVE, Low-power, Hibernate
150 */
151static int mcp16502_get_reg(struct regulator_dev *rdev, int opmode)
152{
153 int reg = MCP16502_BASE(rdev_get_id(rdev));
154
155 switch (opmode) {
156 case MCP16502_OPMODE_ACTIVE:
157 return reg + MCP16502_OFFSET_MODE_A;
158 case MCP16502_OPMODE_LPM:
159 return reg + MCP16502_OFFSET_MODE_LPM;
160 case MCP16502_OPMODE_HIB:
161 return reg + MCP16502_OFFSET_MODE_HIB;
162 default:
163 return -EINVAL;
164 }
165}
166
167/*
168 * mcp16502_get_mode() - return the current operating mode of a regulator
169 *
170 * Note: all functions that are not part of entering/exiting standby/suspend
171 * use the Active mode registers.
172 *
173 * Note: this is different from the PMIC's operatig mode, it is the
174 * MODE bit from the regulator's register.
175 */
176static unsigned int mcp16502_get_mode(struct regulator_dev *rdev)
177{
178 unsigned int val;
179 int ret, reg;
180 struct mcp16502 *mcp = rdev_get_drvdata(rdev);
181
182 reg = mcp16502_get_reg(rdev, MCP16502_OPMODE_ACTIVE);
183 if (reg < 0)
184 return reg;
185
186 ret = regmap_read(mcp->rmap, reg, &val);
187 if (ret)
188 return ret;
189
190 switch (val & MCP16502_MODE) {
191 case MCP16502_MODE_FPWM:
192 return REGULATOR_MODE_NORMAL;
193 case MCP16502_MODE_AUTO_PFM:
194 return REGULATOR_MODE_IDLE;
195 default:
196 return REGULATOR_MODE_INVALID;
197 }
198}
199
200/*
201 * _mcp16502_set_mode() - helper for set_mode and set_suspend_mode
202 *
203 * @rdev: the regulator for which we are setting the mode
204 * @mode: the regulator's mode (the one from MODE bit)
205 * @opmode: the PMIC's operating mode: Active/Low-power/Hibernate
206 */
207static int _mcp16502_set_mode(struct regulator_dev *rdev, unsigned int mode,
208 unsigned int op_mode)
209{
210 int val;
211 int reg;
212 struct mcp16502 *mcp = rdev_get_drvdata(rdev);
213
214 reg = mcp16502_get_reg(rdev, op_mode);
215 if (reg < 0)
216 return reg;
217
218 switch (mode) {
219 case REGULATOR_MODE_NORMAL:
220 val = MCP16502_MODE_FPWM;
221 break;
222 case REGULATOR_MODE_IDLE:
223 val = MCP16502_MODE_AUTO_PFM;
224 break;
225 default:
226 return -EINVAL;
227 }
228
229 reg = regmap_update_bits(mcp->rmap, reg, MCP16502_MODE, val);
230 return reg;
231}
232
233/*
234 * mcp16502_set_mode() - regulator_ops set_mode
235 */
236static int mcp16502_set_mode(struct regulator_dev *rdev, unsigned int mode)
237{
238 return _mcp16502_set_mode(rdev, mode, MCP16502_OPMODE_ACTIVE);
239}
240
241/*
242 * mcp16502_get_status() - regulator_ops get_status
243 */
244static int mcp16502_get_status(struct regulator_dev *rdev)
245{
246 int ret;
247 unsigned int val;
248 struct mcp16502 *mcp = rdev_get_drvdata(rdev);
249
250 ret = regmap_read(mcp->rmap, MCP16502_STAT_BASE(rdev_get_id(rdev)),
251 &val);
252 if (ret)
253 return ret;
254
255 if (val & MCP16502_FLT)
256 return REGULATOR_STATUS_ERROR;
257 else if (val & MCP16502_ENS)
258 return REGULATOR_STATUS_ON;
259 else if (!(val & MCP16502_ENS))
260 return REGULATOR_STATUS_OFF;
261
262 return REGULATOR_STATUS_UNDEFINED;
263}
264
265#ifdef CONFIG_SUSPEND
266/*
267 * mcp16502_suspend_get_target_reg() - get the reg of the target suspend PMIC
268 * mode
269 */
270static int mcp16502_suspend_get_target_reg(struct regulator_dev *rdev)
271{
272 switch (pm_suspend_target_state) {
273 case PM_SUSPEND_STANDBY:
274 return mcp16502_get_reg(rdev, MCP16502_OPMODE_LPM);
275 case PM_SUSPEND_ON:
276 case PM_SUSPEND_MEM:
277 return mcp16502_get_reg(rdev, MCP16502_OPMODE_HIB);
278 default:
279 dev_err(&rdev->dev, "invalid suspend target: %d\n",
280 pm_suspend_target_state);
281 }
282
283 return -EINVAL;
284}
285
286/*
287 * mcp16502_set_suspend_voltage() - regulator_ops set_suspend_voltage
288 */
289static int mcp16502_set_suspend_voltage(struct regulator_dev *rdev, int uV)
290{
291 struct mcp16502 *mcp = rdev_get_drvdata(rdev);
292 int sel = regulator_map_voltage_linear_range(rdev, uV, uV);
293 int reg = mcp16502_suspend_get_target_reg(rdev);
294
295 if (sel < 0)
296 return sel;
297
298 if (reg < 0)
299 return reg;
300
301 return regmap_update_bits(mcp->rmap, reg, MCP16502_VSEL, sel);
302}
303
304/*
305 * mcp16502_set_suspend_mode() - regulator_ops set_suspend_mode
306 */
307static int mcp16502_set_suspend_mode(struct regulator_dev *rdev,
308 unsigned int mode)
309{
310 switch (pm_suspend_target_state) {
311 case PM_SUSPEND_STANDBY:
312 return _mcp16502_set_mode(rdev, mode, MCP16502_OPMODE_LPM);
313 case PM_SUSPEND_ON:
314 case PM_SUSPEND_MEM:
315 return _mcp16502_set_mode(rdev, mode, MCP16502_OPMODE_HIB);
316 default:
317 dev_err(&rdev->dev, "invalid suspend target: %d\n",
318 pm_suspend_target_state);
319 }
320
321 return -EINVAL;
322}
323
324/*
325 * mcp16502_set_suspend_enable() - regulator_ops set_suspend_enable
326 */
327static int mcp16502_set_suspend_enable(struct regulator_dev *rdev)
328{
329 struct mcp16502 *mcp = rdev_get_drvdata(rdev);
330 int reg = mcp16502_suspend_get_target_reg(rdev);
331
332 if (reg < 0)
333 return reg;
334
335 return regmap_update_bits(mcp->rmap, reg, MCP16502_EN, MCP16502_EN);
336}
337
338/*
339 * mcp16502_set_suspend_disable() - regulator_ops set_suspend_disable
340 */
341static int mcp16502_set_suspend_disable(struct regulator_dev *rdev)
342{
343 struct mcp16502 *mcp = rdev_get_drvdata(rdev);
344 int reg = mcp16502_suspend_get_target_reg(rdev);
345
346 if (reg < 0)
347 return reg;
348
349 return regmap_update_bits(mcp->rmap, reg, MCP16502_EN, 0);
350}
351#endif /* CONFIG_SUSPEND */
352
353static const struct regulator_ops mcp16502_buck_ops = {
354 .list_voltage = regulator_list_voltage_linear_range,
355 .map_voltage = regulator_map_voltage_linear_range,
356 .get_voltage_sel = regulator_get_voltage_sel_regmap,
357 .set_voltage_sel = regulator_set_voltage_sel_regmap,
358 .enable = regulator_enable_regmap,
359 .disable = regulator_disable_regmap,
360 .is_enabled = regulator_is_enabled_regmap,
361 .get_status = mcp16502_get_status,
362
363 .set_mode = mcp16502_set_mode,
364 .get_mode = mcp16502_get_mode,
365
366#ifdef CONFIG_SUSPEND
367 .set_suspend_voltage = mcp16502_set_suspend_voltage,
368 .set_suspend_mode = mcp16502_set_suspend_mode,
369 .set_suspend_enable = mcp16502_set_suspend_enable,
370 .set_suspend_disable = mcp16502_set_suspend_disable,
371#endif /* CONFIG_SUSPEND */
372};
373
374/*
375 * LDOs cannot change operating modes.
376 */
377static const struct regulator_ops mcp16502_ldo_ops = {
378 .list_voltage = regulator_list_voltage_linear_range,
379 .map_voltage = regulator_map_voltage_linear_range,
380 .get_voltage_sel = regulator_get_voltage_sel_regmap,
381 .set_voltage_sel = regulator_set_voltage_sel_regmap,
382 .enable = regulator_enable_regmap,
383 .disable = regulator_disable_regmap,
384 .is_enabled = regulator_is_enabled_regmap,
385 .get_status = mcp16502_get_status,
386
387#ifdef CONFIG_SUSPEND
388 .set_suspend_voltage = mcp16502_set_suspend_voltage,
389 .set_suspend_enable = mcp16502_set_suspend_enable,
390 .set_suspend_disable = mcp16502_set_suspend_disable,
391#endif /* CONFIG_SUSPEND */
392};
393
394static const struct of_device_id mcp16502_ids[] = {
395 { .compatible = "microchip,mcp16502", },
396 {}
397};
398MODULE_DEVICE_TABLE(of, mcp16502_ids);
399
400static const struct regulator_linear_range b1l12_ranges[] = {
401 REGULATOR_LINEAR_RANGE(1200000, VDD_LOW_SEL, VDD_HIGH_SEL, 50000),
402};
403
404static const struct regulator_linear_range b234_ranges[] = {
405 REGULATOR_LINEAR_RANGE(600000, VDD_LOW_SEL, VDD_HIGH_SEL, 25000),
406};
407
408static const struct regulator_desc mcp16502_desc[] = {
409 /* MCP16502_REGULATOR(_name, _id, ranges, regulator_ops) */
410 MCP16502_REGULATOR("VDD_IO", BUCK1, b1l12_ranges, mcp16502_buck_ops),
411 MCP16502_REGULATOR("VDD_DDR", BUCK2, b234_ranges, mcp16502_buck_ops),
412 MCP16502_REGULATOR("VDD_CORE", BUCK3, b234_ranges, mcp16502_buck_ops),
413 MCP16502_REGULATOR("VDD_OTHER", BUCK4, b234_ranges, mcp16502_buck_ops),
414 MCP16502_REGULATOR("LDO1", LDO1, b1l12_ranges, mcp16502_ldo_ops),
415 MCP16502_REGULATOR("LDO2", LDO2, b1l12_ranges, mcp16502_ldo_ops)
416};
417
418static const struct regmap_range mcp16502_ranges[] = {
419 regmap_reg_range(MCP16502_MIN_REG, MCP16502_MAX_REG)
420};
421
422static const struct regmap_access_table mcp16502_yes_reg_table = {
423 .yes_ranges = mcp16502_ranges,
424 .n_yes_ranges = ARRAY_SIZE(mcp16502_ranges),
425};
426
427static const struct regmap_config mcp16502_regmap_config = {
428 .reg_bits = 8,
429 .val_bits = 8,
430 .max_register = MCP16502_MAX_REG,
431 .cache_type = REGCACHE_NONE,
432 .rd_table = &mcp16502_yes_reg_table,
433 .wr_table = &mcp16502_yes_reg_table,
434};
435
436/*
437 * set_up_regulators() - initialize all regulators
438 */
439static int setup_regulators(struct mcp16502 *mcp, struct device *dev,
440 struct regulator_config config)
441{
442 int i;
443
444 for (i = 0; i < NUM_REGULATORS; i++) {
445 mcp->rdev[i] = devm_regulator_register(dev,
446 &mcp16502_desc[i],
447 &config);
448 if (IS_ERR(mcp->rdev[i])) {
449 dev_err(dev,
450 "failed to register %s regulator %ld\n",
451 mcp16502_desc[i].name, PTR_ERR(mcp->rdev[i]));
452 return PTR_ERR(mcp->rdev[i]);
453 }
454 }
455
456 return 0;
457}
458
459static int mcp16502_probe(struct i2c_client *client,
460 const struct i2c_device_id *id)
461{
462 struct regulator_config config = { };
463 struct device *dev;
464 struct mcp16502 *mcp;
465 int ret = 0;
466
467 dev = &client->dev;
468 config.dev = dev;
469
470 mcp = devm_kzalloc(dev, sizeof(*mcp), GFP_KERNEL);
471 if (!mcp)
472 return -ENOMEM;
473
474 mcp->rmap = devm_regmap_init_i2c(client, &mcp16502_regmap_config);
475 if (IS_ERR(mcp->rmap)) {
476 ret = PTR_ERR(mcp->rmap);
477 dev_err(dev, "regmap init failed: %d\n", ret);
478 return ret;
479 }
480
481 i2c_set_clientdata(client, mcp);
482 config.regmap = mcp->rmap;
483 config.driver_data = mcp;
484
485 mcp->lpm = devm_gpiod_get(dev, "lpm", GPIOD_OUT_LOW);
486 if (IS_ERR(mcp->lpm)) {
487 dev_err(dev, "failed to get lpm pin: %ld\n", PTR_ERR(mcp->lpm));
488 return PTR_ERR(mcp->lpm);
489 }
490
491 ret = setup_regulators(mcp, dev, config);
492 if (ret != 0)
493 return ret;
494
495 mcp16502_gpio_set_mode(mcp, MCP16502_OPMODE_ACTIVE);
496
497 return 0;
498}
499
Axel Lin4906d092018-12-20 21:14:13 +0800500#ifdef CONFIG_PM_SLEEP
Andrei.Stefanescu@microchip.comfc401cb2018-12-14 08:09:31 +0000501static int mcp16502_suspend_noirq(struct device *dev)
502{
503 struct i2c_client *client = to_i2c_client(dev);
504 struct mcp16502 *mcp = i2c_get_clientdata(client);
505
506 mcp16502_gpio_set_mode(mcp, MCP16502_OPMODE_LPM);
507
508 return 0;
509}
510
511static int mcp16502_resume_noirq(struct device *dev)
512{
513 struct i2c_client *client = to_i2c_client(dev);
514 struct mcp16502 *mcp = i2c_get_clientdata(client);
515
516 mcp16502_gpio_set_mode(mcp, MCP16502_OPMODE_ACTIVE);
517
518 return 0;
519}
Axel Lin4906d092018-12-20 21:14:13 +0800520#endif
Andrei.Stefanescu@microchip.comfc401cb2018-12-14 08:09:31 +0000521
522#ifdef CONFIG_PM
523static const struct dev_pm_ops mcp16502_pm_ops = {
524 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(mcp16502_suspend_noirq,
Andrei.Stefanescu@microchip.com308144c2018-12-17 11:44:59 +0000525 mcp16502_resume_noirq)
Andrei.Stefanescu@microchip.comfc401cb2018-12-14 08:09:31 +0000526};
527#endif
Andrei.Stefanescu@microchip.com919261c2018-12-12 18:31:18 +0000528static const struct i2c_device_id mcp16502_i2c_id[] = {
529 { "mcp16502", 0 },
530 { }
531};
532MODULE_DEVICE_TABLE(i2c, mcp16502_i2c_id);
533
534static struct i2c_driver mcp16502_drv = {
535 .probe = mcp16502_probe,
536 .driver = {
537 .name = "mcp16502-regulator",
538 .of_match_table = of_match_ptr(mcp16502_ids),
Andrei.Stefanescu@microchip.comfc401cb2018-12-14 08:09:31 +0000539#ifdef CONFIG_PM
540 .pm = &mcp16502_pm_ops,
541#endif
Andrei.Stefanescu@microchip.com919261c2018-12-12 18:31:18 +0000542 },
543 .id_table = mcp16502_i2c_id,
544};
545
546module_i2c_driver(mcp16502_drv);
547
548MODULE_VERSION("1.0");
549MODULE_LICENSE("GPL v2");
550MODULE_DESCRIPTION("MCP16502 PMIC driver");
551MODULE_AUTHOR("Andrei Stefanescu andrei.stefanescu@microchip.com");