1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2017 NXP
4 * Copyright (C) 2019 Boundary Devices
5 * Copyright (C) 2020 Amarula Solutions(India)
8 #include <linux/delay.h>
10 #include <linux/gpio/consumer.h>
11 #include <linux/i2c.h>
12 #include <linux/module.h>
13 #include <linux/regmap.h>
14 #include <linux/regulator/driver.h>
15 #include <linux/regulator/machine.h>
18 #define PF8X00_DEVICEID 0x00
19 #define PF8X00_REVID 0x01
20 #define PF8X00_EMREV 0x02
21 #define PF8X00_PROGID 0x03
22 #define PF8X00_IMS_INT 0x04
23 #define PF8X00_IMS_THERM 0x07
24 #define PF8X00_SW_MODE_INT 0x0a
25 #define PF8X00_SW_MODE_MASK 0x0b
26 #define PF8X00_IMS_SW_ILIM 0x12
27 #define PF8X00_IMS_LDO_ILIM 0x15
28 #define PF8X00_IMS_SW_UV 0x18
29 #define PF8X00_IMS_SW_OV 0x1b
30 #define PF8X00_IMS_LDO_UV 0x1e
31 #define PF8X00_IMS_LDO_OV 0x21
32 #define PF8X00_IMS_PWRON 0x24
33 #define PF8X00_SYS_INT 0x27
34 #define PF8X00_HARD_FAULT 0x29
35 #define PF8X00_FSOB_FLAGS 0x2a
36 #define PF8X00_FSOB_SELECT 0x2b
37 #define PF8X00_ABIST_OV1 0x2c
38 #define PF8X00_ABIST_OV2 0x2d
39 #define PF8X00_ABIST_UV1 0x2e
40 #define PF8X00_ABIST_UV2 0x2f
41 #define PF8X00_TEST_FLAGS 0x30
42 #define PF8X00_ABIST_RUN 0x31
43 #define PF8X00_RANDOM_GEN 0x33
44 #define PF8X00_RANDOM_CHK 0x34
45 #define PF8X00_VMONEN1 0x35
46 #define PF8X00_VMONEN2 0x36
47 #define PF8X00_CTRL1 0x37
48 #define PF8X00_CTRL2 0x38
49 #define PF8X00_CTRL3 0x39
50 #define PF8X00_PWRUP_CTRL 0x3a
51 #define PF8X00_RESETBMCU 0x3c
52 #define PF8X00_PGOOD 0x3d
53 #define PF8X00_PWRDN_DLY1 0x3e
54 #define PF8X00_PWRDN_DLY2 0x3f
55 #define PF8X00_FREQ_CTRL 0x40
56 #define PF8X00_COINCELL_CTRL 0x41
57 #define PF8X00_PWRON 0x42
58 #define PF8X00_WD_CONFIG 0x43
59 #define PF8X00_WD_CLEAR 0x44
60 #define PF8X00_WD_EXPIRE 0x45
61 #define PF8X00_WD_COUNTER 0x46
62 #define PF8X00_FAULT_COUNTER 0x47
63 #define PF8X00_FSAFE_COUNTER 0x48
64 #define PF8X00_FAULT_TIMER 0x49
65 #define PF8X00_AMUX 0x4a
66 #define PF8X00_SW1_CONFIG1 0x4d
67 #define PF8X00_LDO1_CONFIG1 0x85
68 #define PF8X00_VSNVS_CONFIG1 0x9d
69 #define PF8X00_PAGE_SELECT 0x9f
72 enum pf8x00_regulators {
86 PF8X00_MAX_REGULATORS,
89 enum pf8x00_buck_states {
97 #define PF8X00_SW_BASE(i) (8 * (i - PF8X00_BUCK1) + PF8X00_SW1_CONFIG1)
99 enum pf8x00_ldo_states {
106 #define PF8X00_LDO_BASE(i) (6 * (i - PF8X00_LDO1) + PF8X00_LDO1_CONFIG1)
114 #define PF8X00_SWXILIM_SHIFT 3
115 #define PF8X00_SWXILIM_MASK GENMASK(4, 3)
116 #define PF8X00_SWXPHASE_MASK GENMASK(2, 0)
117 #define PF8X00_SWXPHASE_SHIFT 7
124 #define PF8X00_FAM BIT(6)
125 #define PF8X00_DEVICE_FAM_MASK GENMASK(7, 4)
126 #define PF8X00_DEVICE_ID_MASK GENMASK(3, 0)
128 struct pf8x00_regulator_data {
129 struct regulator_desc desc;
130 unsigned int suspend_enable_reg;
131 unsigned int suspend_enable_mask;
132 unsigned int suspend_voltage_reg;
133 unsigned int suspend_voltage_cache;
137 struct regmap *regmap;
141 static const struct regmap_config pf8x00_regmap_config = {
144 .max_register = PF8X00_PAGE_SELECT,
145 .cache_type = REGCACHE_RBTREE,
148 /* VLDOx output: 1.5V to 5.0V */
149 static const int pf8x00_ldo_voltages[] = {
150 1500000, 1600000, 1800000, 1850000, 2150000, 2500000, 2800000, 3000000,
151 3100000, 3150000, 3200000, 3300000, 3350000, 1650000, 1700000, 5000000,
154 /* Output: 2.1A to 4.5A */
155 static const unsigned int pf8x00_sw_current_table[] = {
156 2100000, 2600000, 3000000, 4500000,
159 /* Output: 0.4V to 1.8V */
160 #define PF8XOO_SW1_6_VOLTAGE_NUM 0xB2
161 static const struct linear_range pf8x00_sw1_to_6_voltages[] = {
162 REGULATOR_LINEAR_RANGE(400000, 0x00, 0xB0, 6250),
163 REGULATOR_LINEAR_RANGE(1800000, 0xB1, 0xB1, 0),
166 /* Output: 1.0V to 4.1V */
167 static const int pf8x00_sw7_voltages[] = {
168 1000000, 1100000, 1200000, 1250000, 1300000, 1350000, 1500000, 1600000,
169 1800000, 1850000, 2000000, 2100000, 2150000, 2250000, 2300000, 2400000,
170 2500000, 2800000, 3150000, 3200000, 3250000, 3300000, 3350000, 3400000,
171 3500000, 3800000, 4000000, 4100000, 4100000, 4100000, 4100000, 4100000,
174 /* Output: 1.8V, 3.0V, or 3.3V */
175 static const int pf8x00_vsnvs_voltages[] = {
176 0, 1800000, 3000000, 3300000,
179 static void swxilim_select(struct pf8x00_chip *chip, int id, int ilim)
182 u8 reg = PF8X00_SW_BASE(id) + SW_CONFIG2;
186 ilim_sel = SWXILIM_2100_MA;
189 ilim_sel = SWXILIM_2600_MA;
192 ilim_sel = SWXILIM_3000_MA;
195 ilim_sel = SWXILIM_4500_MA;
198 ilim_sel = SWXILIM_2100_MA;
202 regmap_update_bits(chip->regmap, reg,
204 ilim_sel << PF8X00_SWXILIM_SHIFT);
207 static void handle_ilim_property(struct device_node *np,
208 const struct regulator_desc *desc,
209 struct regulator_config *config)
211 struct pf8x00_chip *chip = config->driver_data;
215 if ((desc->id >= PF8X00_BUCK1) && (desc->id <= PF8X00_BUCK7)) {
216 ret = of_property_read_u32(np, "nxp,ilim-ma", &val);
218 dev_dbg(chip->dev, "unspecified ilim for BUCK%d, use value stored in OTP\n",
219 desc->id - PF8X00_LDO4);
223 dev_warn(chip->dev, "nxp,ilim-ma is deprecated, please use regulator-max-microamp\n");
224 swxilim_select(chip, desc->id, val);
227 dev_warn(chip->dev, "nxp,ilim-ma used with incorrect regulator (%d)\n", desc->id);
230 static void handle_shift_property(struct device_node *np,
231 const struct regulator_desc *desc,
232 struct regulator_config *config)
234 unsigned char id = desc->id - PF8X00_LDO4;
235 unsigned char reg = PF8X00_SW_BASE(id) + SW_CONFIG2;
236 struct pf8x00_chip *chip = config->driver_data;
241 if ((desc->id >= PF8X00_BUCK1) && (desc->id <= PF8X00_BUCK7)) {
242 ret = of_property_read_u32(np, "nxp,phase-shift", &val);
245 "unspecified phase-shift for BUCK%d, using OTP configuration\n",
250 if (val < 0 || val > 315 || val % 45 != 0) {
251 dev_warn(config->dev,
252 "invalid phase_shift %d for BUCK%d, using OTP configuration\n",
262 phase = PF8X00_SWXPHASE_SHIFT;
264 regmap_update_bits(chip->regmap, reg,
265 PF8X00_SWXPHASE_MASK,
268 dev_warn(chip->dev, "nxp,phase-shift used with incorrect regulator (%d)\n", id);
272 static int pf8x00_of_parse_cb(struct device_node *np,
273 const struct regulator_desc *desc,
274 struct regulator_config *config)
277 handle_ilim_property(np, desc, config);
278 handle_shift_property(np, desc, config);
283 static int pf8x00_suspend_enable(struct regulator_dev *rdev)
285 struct pf8x00_regulator_data *regl = rdev_get_drvdata(rdev);
286 struct regmap *rmap = rdev_get_regmap(rdev);
288 return regmap_update_bits(rmap, regl->suspend_enable_reg,
289 regl->suspend_enable_mask,
290 regl->suspend_enable_mask);
293 static int pf8x00_suspend_disable(struct regulator_dev *rdev)
295 struct pf8x00_regulator_data *regl = rdev_get_drvdata(rdev);
296 struct regmap *rmap = rdev_get_regmap(rdev);
298 return regmap_update_bits(rmap, regl->suspend_enable_reg,
299 regl->suspend_enable_mask, 0);
302 static int pf8x00_set_suspend_voltage(struct regulator_dev *rdev, int uV)
304 struct pf8x00_regulator_data *regl = rdev_get_drvdata(rdev);
307 if (regl->suspend_voltage_cache == uV)
310 ret = regulator_map_voltage_iterate(rdev, uV, uV);
312 dev_err(rdev_get_dev(rdev), "failed to map %i uV\n", uV);
316 dev_dbg(rdev_get_dev(rdev), "uV: %i, reg: 0x%x, msk: 0x%x, val: 0x%x\n",
317 uV, regl->suspend_voltage_reg, regl->desc.vsel_mask, ret);
318 ret = regmap_update_bits(rdev->regmap, regl->suspend_voltage_reg,
319 regl->desc.vsel_mask, ret);
321 dev_err(rdev_get_dev(rdev), "failed to set %i uV\n", uV);
325 regl->suspend_voltage_cache = uV;
330 static const struct regulator_ops pf8x00_ldo_ops = {
331 .enable = regulator_enable_regmap,
332 .disable = regulator_disable_regmap,
333 .is_enabled = regulator_is_enabled_regmap,
334 .list_voltage = regulator_list_voltage_table,
335 .set_voltage_sel = regulator_set_voltage_sel_regmap,
336 .get_voltage_sel = regulator_get_voltage_sel_regmap,
337 .set_suspend_enable = pf8x00_suspend_enable,
338 .set_suspend_disable = pf8x00_suspend_disable,
339 .set_suspend_voltage = pf8x00_set_suspend_voltage,
343 static const struct regulator_ops pf8x00_buck1_6_ops = {
344 .enable = regulator_enable_regmap,
345 .disable = regulator_disable_regmap,
346 .is_enabled = regulator_is_enabled_regmap,
347 .list_voltage = regulator_list_voltage_linear_range,
348 .set_voltage_sel = regulator_set_voltage_sel_regmap,
349 .get_voltage_sel = regulator_get_voltage_sel_regmap,
350 .get_current_limit = regulator_get_current_limit_regmap,
351 .set_current_limit = regulator_set_current_limit_regmap,
352 .set_suspend_enable = pf8x00_suspend_enable,
353 .set_suspend_disable = pf8x00_suspend_disable,
354 .set_suspend_voltage = pf8x00_set_suspend_voltage,
357 static const struct regulator_ops pf8x00_buck7_ops = {
358 .enable = regulator_enable_regmap,
359 .disable = regulator_disable_regmap,
360 .is_enabled = regulator_is_enabled_regmap,
361 .list_voltage = regulator_list_voltage_table,
362 .map_voltage = regulator_map_voltage_ascend,
363 .set_voltage_sel = regulator_set_voltage_sel_regmap,
364 .get_voltage_sel = regulator_get_voltage_sel_regmap,
365 .get_current_limit = regulator_get_current_limit_regmap,
366 .set_current_limit = regulator_set_current_limit_regmap,
367 .set_suspend_enable = pf8x00_suspend_enable,
368 .set_suspend_disable = pf8x00_suspend_disable,
371 static const struct regulator_ops pf8x00_vsnvs_ops = {
372 .enable = regulator_enable_regmap,
373 .disable = regulator_disable_regmap,
374 .is_enabled = regulator_is_enabled_regmap,
375 .list_voltage = regulator_list_voltage_table,
376 .map_voltage = regulator_map_voltage_ascend,
377 .set_voltage_sel = regulator_set_voltage_sel_regmap,
378 .get_voltage_sel = regulator_get_voltage_sel_regmap,
381 #define PF8X00LDO(_id, _name, base, voltages) \
382 [PF8X00_LDO ## _id] = { \
386 .regulators_node = "regulators", \
387 .n_voltages = ARRAY_SIZE(voltages), \
388 .ops = &pf8x00_ldo_ops, \
389 .type = REGULATOR_VOLTAGE, \
390 .id = PF8X00_LDO ## _id, \
391 .owner = THIS_MODULE, \
392 .volt_table = voltages, \
393 .vsel_reg = (base) + LDO_RUN_VOLT, \
395 .enable_reg = (base) + LDO_CONFIG2, \
397 .disable_val = 0x0, \
400 .suspend_enable_reg = (base) + LDO_CONFIG2, \
401 .suspend_enable_mask = 1, \
402 .suspend_voltage_reg = (base) + LDO_STBY_VOLT, \
405 #define PF8X00BUCK(_id, _name, base, voltages) \
406 [PF8X00_BUCK ## _id] = { \
410 .regulators_node = "regulators", \
411 .of_parse_cb = pf8x00_of_parse_cb, \
412 .n_voltages = PF8XOO_SW1_6_VOLTAGE_NUM, \
413 .ops = &pf8x00_buck1_6_ops, \
414 .type = REGULATOR_VOLTAGE, \
415 .id = PF8X00_BUCK ## _id, \
416 .owner = THIS_MODULE, \
417 .ramp_delay = 19000, \
418 .linear_ranges = pf8x00_sw1_to_6_voltages, \
420 ARRAY_SIZE(pf8x00_sw1_to_6_voltages), \
421 .vsel_reg = (base) + SW_RUN_VOLT, \
423 .curr_table = pf8x00_sw_current_table, \
424 .n_current_limits = \
425 ARRAY_SIZE(pf8x00_sw_current_table), \
426 .csel_reg = (base) + SW_CONFIG2, \
427 .csel_mask = PF8X00_SWXILIM_MASK, \
428 .enable_reg = (base) + SW_MODE1, \
430 .disable_val = 0x0, \
431 .enable_mask = 0x3, \
432 .enable_time = 500, \
434 .suspend_enable_reg = (base) + SW_MODE1, \
435 .suspend_enable_mask = 0xc, \
436 .suspend_voltage_reg = (base) + SW_STBY_VOLT, \
439 #define PF8X00BUCK7(_name, base, voltages) \
444 .regulators_node = "regulators", \
445 .of_parse_cb = pf8x00_of_parse_cb, \
446 .n_voltages = ARRAY_SIZE(voltages), \
447 .ops = &pf8x00_buck7_ops, \
448 .type = REGULATOR_VOLTAGE, \
449 .id = PF8X00_BUCK7, \
450 .owner = THIS_MODULE, \
451 .ramp_delay = 19000, \
452 .volt_table = voltages, \
453 .vsel_reg = (base) + SW_RUN_VOLT, \
455 .curr_table = pf8x00_sw_current_table, \
456 .n_current_limits = \
457 ARRAY_SIZE(pf8x00_sw_current_table), \
458 .csel_reg = (base) + SW_CONFIG2, \
459 .csel_mask = PF8X00_SWXILIM_MASK, \
460 .enable_reg = (base) + SW_MODE1, \
462 .disable_val = 0x0, \
463 .enable_mask = 0x3, \
464 .enable_time = 500, \
469 #define PF8X00VSNVS(_name, base, voltages) \
474 .regulators_node = "regulators", \
475 .n_voltages = ARRAY_SIZE(voltages), \
476 .ops = &pf8x00_vsnvs_ops, \
477 .type = REGULATOR_VOLTAGE, \
478 .id = PF8X00_VSNVS, \
479 .owner = THIS_MODULE, \
480 .volt_table = voltages, \
481 .vsel_reg = (base), \
486 static struct pf8x00_regulator_data pf8x00_regs_data[PF8X00_MAX_REGULATORS] = {
487 PF8X00LDO(1, "ldo1", PF8X00_LDO_BASE(PF8X00_LDO1), pf8x00_ldo_voltages),
488 PF8X00LDO(2, "ldo2", PF8X00_LDO_BASE(PF8X00_LDO2), pf8x00_ldo_voltages),
489 PF8X00LDO(3, "ldo3", PF8X00_LDO_BASE(PF8X00_LDO3), pf8x00_ldo_voltages),
490 PF8X00LDO(4, "ldo4", PF8X00_LDO_BASE(PF8X00_LDO4), pf8x00_ldo_voltages),
491 PF8X00BUCK(1, "buck1", PF8X00_SW_BASE(PF8X00_BUCK1), pf8x00_sw1_to_6_voltages),
492 PF8X00BUCK(2, "buck2", PF8X00_SW_BASE(PF8X00_BUCK2), pf8x00_sw1_to_6_voltages),
493 PF8X00BUCK(3, "buck3", PF8X00_SW_BASE(PF8X00_BUCK3), pf8x00_sw1_to_6_voltages),
494 PF8X00BUCK(4, "buck4", PF8X00_SW_BASE(PF8X00_BUCK4), pf8x00_sw1_to_6_voltages),
495 PF8X00BUCK(5, "buck5", PF8X00_SW_BASE(PF8X00_BUCK5), pf8x00_sw1_to_6_voltages),
496 PF8X00BUCK(6, "buck6", PF8X00_SW_BASE(PF8X00_BUCK6), pf8x00_sw1_to_6_voltages),
497 PF8X00BUCK7("buck7", PF8X00_SW_BASE(PF8X00_BUCK7), pf8x00_sw7_voltages),
498 PF8X00VSNVS("vsnvs", PF8X00_VSNVS_CONFIG1, pf8x00_vsnvs_voltages),
501 static int pf8x00_identify(struct pf8x00_chip *chip)
505 const char *name = NULL;
508 ret = regmap_read(chip->regmap, PF8X00_DEVICEID, &value);
510 dev_err(chip->dev, "failed to read chip family\n");
514 dev_fam = value & PF8X00_DEVICE_FAM_MASK;
520 "Chip 0x%x is not from PF8X00 family\n", dev_fam);
524 dev_id = value & PF8X00_DEVICE_ID_MASK;
536 dev_err(chip->dev, "Unknown pf8x00 device id 0x%x\n", dev_id);
540 dev_info(chip->dev, "%s PMIC found.\n", name);
545 static int pf8x00_i2c_probe(struct i2c_client *client)
547 struct regulator_config config = { NULL, };
548 struct pf8x00_chip *chip;
552 chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
556 i2c_set_clientdata(client, chip);
557 chip->dev = &client->dev;
559 chip->regmap = devm_regmap_init_i2c(client, &pf8x00_regmap_config);
560 if (IS_ERR(chip->regmap)) {
561 ret = PTR_ERR(chip->regmap);
562 dev_err(&client->dev,
563 "regmap allocation failed with err %d\n", ret);
567 ret = pf8x00_identify(chip);
571 for (id = 0; id < ARRAY_SIZE(pf8x00_regs_data); id++) {
572 struct pf8x00_regulator_data *data = &pf8x00_regs_data[id];
573 struct regulator_dev *rdev;
575 config.dev = chip->dev;
576 config.driver_data = data;
577 config.regmap = chip->regmap;
579 rdev = devm_regulator_register(&client->dev, &data->desc, &config);
581 dev_err(&client->dev,
582 "failed to register %s regulator\n", data->desc.name);
583 return PTR_ERR(rdev);
590 static const struct of_device_id pf8x00_dt_ids[] = {
591 { .compatible = "nxp,pf8100",},
592 { .compatible = "nxp,pf8121a",},
593 { .compatible = "nxp,pf8200",},
596 MODULE_DEVICE_TABLE(of, pf8x00_dt_ids);
598 static const struct i2c_device_id pf8x00_i2c_id[] = {
604 MODULE_DEVICE_TABLE(i2c, pf8x00_i2c_id);
606 static struct i2c_driver pf8x00_regulator_driver = {
607 .id_table = pf8x00_i2c_id,
610 .of_match_table = pf8x00_dt_ids,
612 .probe_new = pf8x00_i2c_probe,
614 module_i2c_driver(pf8x00_regulator_driver);
616 MODULE_AUTHOR("Jagan Teki <jagan@amarulasolutions.com>");
617 MODULE_AUTHOR("Troy Kisky <troy.kisky@boundarydevices.com>");
618 MODULE_DESCRIPTION("Regulator Driver for NXP's PF8100/PF8121A/PF8200 PMIC");
619 MODULE_LICENSE("GPL v2");