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