1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) STMicroelectronics 2018
3 // Author: Pascal Paillet <p.paillet@st.com> for STMicroelectronics.
5 #include <linux/interrupt.h>
6 #include <linux/mfd/stpmic1.h>
7 #include <linux/module.h>
8 #include <linux/of_irq.h>
9 #include <linux/platform_device.h>
10 #include <linux/regmap.h>
11 #include <linux/regulator/driver.h>
12 #include <linux/regulator/machine.h>
13 #include <linux/regulator/of_regulator.h>
15 #include <dt-bindings/mfd/st,stpmic1.h>
18 * struct stpmic1 regulator description: this structure is used as driver data
19 * @desc: regulator framework description
20 * @mask_reset_reg: mask reset register address
21 * @mask_reset_mask: mask rank and mask reset register mask
22 * @icc_reg: icc register address
23 * @icc_mask: icc register mask
25 struct stpmic1_regulator_cfg {
26 struct regulator_desc desc;
33 static int stpmic1_set_mode(struct regulator_dev *rdev, unsigned int mode);
34 static unsigned int stpmic1_get_mode(struct regulator_dev *rdev);
35 static int stpmic1_set_icc(struct regulator_dev *rdev, int lim, int severity,
37 static unsigned int stpmic1_map_mode(unsigned int mode);
50 STPMIC1_VREF_DDR = 10,
52 STPMIC1_VBUS_OTG = 12,
56 /* Enable time worst case is 5000mV/(2250uV/uS) */
57 #define PMIC_ENABLE_TIME_US 2200
58 /* Ramp delay worst case is (2250uV/uS) */
59 #define PMIC_RAMP_DELAY 2200
61 static const struct linear_range buck1_ranges[] = {
62 REGULATOR_LINEAR_RANGE(725000, 0, 4, 0),
63 REGULATOR_LINEAR_RANGE(725000, 5, 36, 25000),
64 REGULATOR_LINEAR_RANGE(1500000, 37, 63, 0),
67 static const struct linear_range buck2_ranges[] = {
68 REGULATOR_LINEAR_RANGE(1000000, 0, 17, 0),
69 REGULATOR_LINEAR_RANGE(1050000, 18, 19, 0),
70 REGULATOR_LINEAR_RANGE(1100000, 20, 21, 0),
71 REGULATOR_LINEAR_RANGE(1150000, 22, 23, 0),
72 REGULATOR_LINEAR_RANGE(1200000, 24, 25, 0),
73 REGULATOR_LINEAR_RANGE(1250000, 26, 27, 0),
74 REGULATOR_LINEAR_RANGE(1300000, 28, 29, 0),
75 REGULATOR_LINEAR_RANGE(1350000, 30, 31, 0),
76 REGULATOR_LINEAR_RANGE(1400000, 32, 33, 0),
77 REGULATOR_LINEAR_RANGE(1450000, 34, 35, 0),
78 REGULATOR_LINEAR_RANGE(1500000, 36, 63, 0),
81 static const struct linear_range buck3_ranges[] = {
82 REGULATOR_LINEAR_RANGE(1000000, 0, 19, 0),
83 REGULATOR_LINEAR_RANGE(1100000, 20, 23, 0),
84 REGULATOR_LINEAR_RANGE(1200000, 24, 27, 0),
85 REGULATOR_LINEAR_RANGE(1300000, 28, 31, 0),
86 REGULATOR_LINEAR_RANGE(1400000, 32, 35, 0),
87 REGULATOR_LINEAR_RANGE(1500000, 36, 55, 100000),
88 REGULATOR_LINEAR_RANGE(3400000, 56, 63, 0),
91 static const struct linear_range buck4_ranges[] = {
92 REGULATOR_LINEAR_RANGE(600000, 0, 27, 25000),
93 REGULATOR_LINEAR_RANGE(1300000, 28, 29, 0),
94 REGULATOR_LINEAR_RANGE(1350000, 30, 31, 0),
95 REGULATOR_LINEAR_RANGE(1400000, 32, 33, 0),
96 REGULATOR_LINEAR_RANGE(1450000, 34, 35, 0),
97 REGULATOR_LINEAR_RANGE(1500000, 36, 60, 100000),
98 REGULATOR_LINEAR_RANGE(3900000, 61, 63, 0),
101 static const struct linear_range ldo1_ranges[] = {
102 REGULATOR_LINEAR_RANGE(1700000, 0, 7, 0),
103 REGULATOR_LINEAR_RANGE(1700000, 8, 24, 100000),
104 REGULATOR_LINEAR_RANGE(3300000, 25, 31, 0),
107 static const struct linear_range ldo2_ranges[] = {
108 REGULATOR_LINEAR_RANGE(1700000, 0, 7, 0),
109 REGULATOR_LINEAR_RANGE(1700000, 8, 24, 100000),
110 REGULATOR_LINEAR_RANGE(3300000, 25, 30, 0),
113 static const struct linear_range ldo3_ranges[] = {
114 REGULATOR_LINEAR_RANGE(1700000, 0, 7, 0),
115 REGULATOR_LINEAR_RANGE(1700000, 8, 24, 100000),
116 REGULATOR_LINEAR_RANGE(3300000, 25, 30, 0),
117 /* with index 31 LDO3 is in DDR mode */
118 REGULATOR_LINEAR_RANGE(500000, 31, 31, 0),
121 static const struct linear_range ldo5_ranges[] = {
122 REGULATOR_LINEAR_RANGE(1700000, 0, 7, 0),
123 REGULATOR_LINEAR_RANGE(1700000, 8, 30, 100000),
124 REGULATOR_LINEAR_RANGE(3900000, 31, 31, 0),
127 static const struct linear_range ldo6_ranges[] = {
128 REGULATOR_LINEAR_RANGE(900000, 0, 24, 100000),
129 REGULATOR_LINEAR_RANGE(3300000, 25, 31, 0),
132 static const struct regulator_ops stpmic1_ldo_ops = {
133 .list_voltage = regulator_list_voltage_linear_range,
134 .map_voltage = regulator_map_voltage_linear_range,
135 .is_enabled = regulator_is_enabled_regmap,
136 .enable = regulator_enable_regmap,
137 .disable = regulator_disable_regmap,
138 .get_voltage_sel = regulator_get_voltage_sel_regmap,
139 .set_voltage_sel = regulator_set_voltage_sel_regmap,
140 .set_over_current_protection = stpmic1_set_icc,
143 static const struct regulator_ops stpmic1_ldo3_ops = {
144 .list_voltage = regulator_list_voltage_linear_range,
145 .map_voltage = regulator_map_voltage_iterate,
146 .is_enabled = regulator_is_enabled_regmap,
147 .enable = regulator_enable_regmap,
148 .disable = regulator_disable_regmap,
149 .get_voltage_sel = regulator_get_voltage_sel_regmap,
150 .set_voltage_sel = regulator_set_voltage_sel_regmap,
151 .get_bypass = regulator_get_bypass_regmap,
152 .set_bypass = regulator_set_bypass_regmap,
153 .set_over_current_protection = stpmic1_set_icc,
156 static const struct regulator_ops stpmic1_ldo4_fixed_regul_ops = {
157 .is_enabled = regulator_is_enabled_regmap,
158 .enable = regulator_enable_regmap,
159 .disable = regulator_disable_regmap,
160 .set_over_current_protection = stpmic1_set_icc,
163 static const struct regulator_ops stpmic1_buck_ops = {
164 .list_voltage = regulator_list_voltage_linear_range,
165 .map_voltage = regulator_map_voltage_linear_range,
166 .is_enabled = regulator_is_enabled_regmap,
167 .enable = regulator_enable_regmap,
168 .disable = regulator_disable_regmap,
169 .get_voltage_sel = regulator_get_voltage_sel_regmap,
170 .set_voltage_sel = regulator_set_voltage_sel_regmap,
171 .set_pull_down = regulator_set_pull_down_regmap,
172 .set_mode = stpmic1_set_mode,
173 .get_mode = stpmic1_get_mode,
174 .set_over_current_protection = stpmic1_set_icc,
177 static const struct regulator_ops stpmic1_vref_ddr_ops = {
178 .is_enabled = regulator_is_enabled_regmap,
179 .enable = regulator_enable_regmap,
180 .disable = regulator_disable_regmap,
183 static const struct regulator_ops stpmic1_boost_regul_ops = {
184 .is_enabled = regulator_is_enabled_regmap,
185 .enable = regulator_enable_regmap,
186 .disable = regulator_disable_regmap,
187 .set_over_current_protection = stpmic1_set_icc,
190 static const struct regulator_ops stpmic1_switch_regul_ops = {
191 .is_enabled = regulator_is_enabled_regmap,
192 .enable = regulator_enable_regmap,
193 .disable = regulator_disable_regmap,
194 .set_over_current_protection = stpmic1_set_icc,
195 .set_active_discharge = regulator_set_active_discharge_regmap,
198 #define REG_LDO(ids, base) { \
200 .id = STPMIC1_##ids, \
202 .ops = &stpmic1_ldo_ops, \
203 .linear_ranges = base ## _ranges, \
204 .n_linear_ranges = ARRAY_SIZE(base ## _ranges), \
205 .type = REGULATOR_VOLTAGE, \
206 .owner = THIS_MODULE, \
207 .vsel_reg = ids##_ACTIVE_CR, \
208 .vsel_mask = LDO_VOLTAGE_MASK, \
209 .enable_reg = ids##_ACTIVE_CR, \
210 .enable_mask = LDO_ENABLE_MASK, \
213 .enable_time = PMIC_ENABLE_TIME_US, \
214 .ramp_delay = PMIC_RAMP_DELAY, \
215 .supply_name = #base, \
218 #define REG_LDO3(ids, base) { \
220 .id = STPMIC1_##ids, \
222 .ops = &stpmic1_ldo3_ops, \
223 .linear_ranges = ldo3_ranges, \
224 .n_linear_ranges = ARRAY_SIZE(ldo3_ranges), \
225 .type = REGULATOR_VOLTAGE, \
226 .owner = THIS_MODULE, \
227 .vsel_reg = LDO3_ACTIVE_CR, \
228 .vsel_mask = LDO_VOLTAGE_MASK, \
229 .enable_reg = LDO3_ACTIVE_CR, \
230 .enable_mask = LDO_ENABLE_MASK, \
233 .enable_time = PMIC_ENABLE_TIME_US, \
234 .ramp_delay = PMIC_RAMP_DELAY, \
235 .bypass_reg = LDO3_ACTIVE_CR, \
236 .bypass_mask = LDO_BYPASS_MASK, \
237 .bypass_val_on = LDO_BYPASS_MASK, \
238 .bypass_val_off = 0, \
239 .supply_name = #base, \
242 #define REG_LDO4(ids, base) { \
244 .id = STPMIC1_##ids, \
246 .ops = &stpmic1_ldo4_fixed_regul_ops, \
247 .type = REGULATOR_VOLTAGE, \
248 .owner = THIS_MODULE, \
250 .fixed_uV = 3300000, \
251 .enable_reg = LDO4_ACTIVE_CR, \
252 .enable_mask = LDO_ENABLE_MASK, \
255 .enable_time = PMIC_ENABLE_TIME_US, \
256 .ramp_delay = PMIC_RAMP_DELAY, \
257 .supply_name = #base, \
260 #define REG_BUCK(ids, base) { \
262 .id = STPMIC1_##ids, \
263 .ops = &stpmic1_buck_ops, \
265 .linear_ranges = base ## _ranges, \
266 .n_linear_ranges = ARRAY_SIZE(base ## _ranges), \
267 .type = REGULATOR_VOLTAGE, \
268 .owner = THIS_MODULE, \
269 .vsel_reg = ids##_ACTIVE_CR, \
270 .vsel_mask = BUCK_VOLTAGE_MASK, \
271 .enable_reg = ids##_ACTIVE_CR, \
272 .enable_mask = BUCK_ENABLE_MASK, \
275 .enable_time = PMIC_ENABLE_TIME_US, \
276 .ramp_delay = PMIC_RAMP_DELAY, \
277 .of_map_mode = stpmic1_map_mode, \
278 .pull_down_reg = ids##_PULL_DOWN_REG, \
279 .pull_down_mask = ids##_PULL_DOWN_MASK, \
280 .supply_name = #base, \
283 #define REG_VREF_DDR(ids, base) { \
285 .id = STPMIC1_##ids, \
287 .ops = &stpmic1_vref_ddr_ops, \
288 .type = REGULATOR_VOLTAGE, \
289 .owner = THIS_MODULE, \
291 .fixed_uV = 500000, \
292 .enable_reg = VREF_DDR_ACTIVE_CR, \
293 .enable_mask = BUCK_ENABLE_MASK, \
296 .enable_time = PMIC_ENABLE_TIME_US, \
297 .supply_name = #base, \
300 #define REG_BOOST(ids, base) { \
302 .id = STPMIC1_##ids, \
304 .ops = &stpmic1_boost_regul_ops, \
305 .type = REGULATOR_VOLTAGE, \
306 .owner = THIS_MODULE, \
308 .fixed_uV = 5000000, \
309 .enable_reg = BST_SW_CR, \
310 .enable_mask = BOOST_ENABLED, \
311 .enable_val = BOOST_ENABLED, \
313 .enable_time = PMIC_ENABLE_TIME_US, \
314 .supply_name = #base, \
317 #define REG_VBUS_OTG(ids, base) { \
319 .id = STPMIC1_##ids, \
321 .ops = &stpmic1_switch_regul_ops, \
322 .type = REGULATOR_VOLTAGE, \
323 .owner = THIS_MODULE, \
325 .fixed_uV = 5000000, \
326 .enable_reg = BST_SW_CR, \
327 .enable_mask = USBSW_OTG_SWITCH_ENABLED, \
328 .enable_val = USBSW_OTG_SWITCH_ENABLED, \
330 .enable_time = PMIC_ENABLE_TIME_US, \
331 .supply_name = #base, \
332 .active_discharge_reg = BST_SW_CR, \
333 .active_discharge_mask = VBUS_OTG_DISCHARGE, \
334 .active_discharge_on = VBUS_OTG_DISCHARGE, \
337 #define REG_SW_OUT(ids, base) { \
339 .id = STPMIC1_##ids, \
341 .ops = &stpmic1_switch_regul_ops, \
342 .type = REGULATOR_VOLTAGE, \
343 .owner = THIS_MODULE, \
345 .fixed_uV = 5000000, \
346 .enable_reg = BST_SW_CR, \
347 .enable_mask = SWIN_SWOUT_ENABLED, \
348 .enable_val = SWIN_SWOUT_ENABLED, \
350 .enable_time = PMIC_ENABLE_TIME_US, \
351 .supply_name = #base, \
352 .active_discharge_reg = BST_SW_CR, \
353 .active_discharge_mask = SW_OUT_DISCHARGE, \
354 .active_discharge_on = SW_OUT_DISCHARGE, \
357 static const struct stpmic1_regulator_cfg stpmic1_regulator_cfgs[] = {
359 .desc = REG_BUCK(BUCK1, buck1),
360 .icc_reg = BUCKS_ICCTO_CR,
362 .mask_reset_reg = BUCKS_MASK_RESET_CR,
363 .mask_reset_mask = BIT(0),
366 .desc = REG_BUCK(BUCK2, buck2),
367 .icc_reg = BUCKS_ICCTO_CR,
369 .mask_reset_reg = BUCKS_MASK_RESET_CR,
370 .mask_reset_mask = BIT(1),
373 .desc = REG_BUCK(BUCK3, buck3),
374 .icc_reg = BUCKS_ICCTO_CR,
376 .mask_reset_reg = BUCKS_MASK_RESET_CR,
377 .mask_reset_mask = BIT(2),
380 .desc = REG_BUCK(BUCK4, buck4),
381 .icc_reg = BUCKS_ICCTO_CR,
383 .mask_reset_reg = BUCKS_MASK_RESET_CR,
384 .mask_reset_mask = BIT(3),
387 .desc = REG_LDO(LDO1, ldo1),
388 .icc_reg = LDOS_ICCTO_CR,
390 .mask_reset_reg = LDOS_MASK_RESET_CR,
391 .mask_reset_mask = BIT(0),
394 .desc = REG_LDO(LDO2, ldo2),
395 .icc_reg = LDOS_ICCTO_CR,
397 .mask_reset_reg = LDOS_MASK_RESET_CR,
398 .mask_reset_mask = BIT(1),
401 .desc = REG_LDO3(LDO3, ldo3),
402 .icc_reg = LDOS_ICCTO_CR,
404 .mask_reset_reg = LDOS_MASK_RESET_CR,
405 .mask_reset_mask = BIT(2),
408 .desc = REG_LDO4(LDO4, ldo4),
409 .icc_reg = LDOS_ICCTO_CR,
411 .mask_reset_reg = LDOS_MASK_RESET_CR,
412 .mask_reset_mask = BIT(3),
415 .desc = REG_LDO(LDO5, ldo5),
416 .icc_reg = LDOS_ICCTO_CR,
418 .mask_reset_reg = LDOS_MASK_RESET_CR,
419 .mask_reset_mask = BIT(4),
422 .desc = REG_LDO(LDO6, ldo6),
423 .icc_reg = LDOS_ICCTO_CR,
425 .mask_reset_reg = LDOS_MASK_RESET_CR,
426 .mask_reset_mask = BIT(5),
428 [STPMIC1_VREF_DDR] = {
429 .desc = REG_VREF_DDR(VREF_DDR, vref_ddr),
430 .mask_reset_reg = LDOS_MASK_RESET_CR,
431 .mask_reset_mask = BIT(6),
434 .desc = REG_BOOST(BOOST, boost),
435 .icc_reg = BUCKS_ICCTO_CR,
438 [STPMIC1_VBUS_OTG] = {
439 .desc = REG_VBUS_OTG(VBUS_OTG, pwr_sw1),
440 .icc_reg = BUCKS_ICCTO_CR,
444 .desc = REG_SW_OUT(SW_OUT, pwr_sw2),
445 .icc_reg = BUCKS_ICCTO_CR,
450 static unsigned int stpmic1_map_mode(unsigned int mode)
453 case STPMIC1_BUCK_MODE_NORMAL:
454 return REGULATOR_MODE_NORMAL;
455 case STPMIC1_BUCK_MODE_LP:
456 return REGULATOR_MODE_STANDBY;
458 return REGULATOR_MODE_INVALID;
462 static unsigned int stpmic1_get_mode(struct regulator_dev *rdev)
465 struct regmap *regmap = rdev_get_regmap(rdev);
467 regmap_read(regmap, rdev->desc->enable_reg, &value);
469 if (value & STPMIC1_BUCK_MODE_LP)
470 return REGULATOR_MODE_STANDBY;
472 return REGULATOR_MODE_NORMAL;
475 static int stpmic1_set_mode(struct regulator_dev *rdev, unsigned int mode)
478 struct regmap *regmap = rdev_get_regmap(rdev);
481 case REGULATOR_MODE_NORMAL:
482 value = STPMIC1_BUCK_MODE_NORMAL;
484 case REGULATOR_MODE_STANDBY:
485 value = STPMIC1_BUCK_MODE_LP;
491 return regmap_update_bits(regmap, rdev->desc->enable_reg,
492 STPMIC1_BUCK_MODE_LP, value);
495 static int stpmic1_set_icc(struct regulator_dev *rdev, int lim, int severity,
498 struct stpmic1_regulator_cfg *cfg = rdev_get_drvdata(rdev);
499 struct regmap *regmap = rdev_get_regmap(rdev);
502 * The code seems like one bit in a register controls whether OCP is
503 * enabled. So we might be able to turn it off here is if that
504 * was requested. I won't support this because I don't have the HW.
505 * Feel free to try and implement if you have the HW and need kernel
508 * Also, I don't know if limit can be configured or if we support
509 * error/warning instead of protect. So I just keep existing logic
512 if (lim || severity != REGULATOR_SEVERITY_PROT || !enable)
515 /* enable switch off in case of over current */
516 return regmap_update_bits(regmap, cfg->icc_reg, cfg->icc_mask,
520 static irqreturn_t stpmic1_curlim_irq_handler(int irq, void *data)
522 struct regulator_dev *rdev = (struct regulator_dev *)data;
524 /* Send an overcurrent notification */
525 regulator_notifier_call_chain(rdev,
526 REGULATOR_EVENT_OVER_CURRENT,
532 #define MATCH(_name, _id) \
533 [STPMIC1_##_id] = { \
535 .desc = &stpmic1_regulator_cfgs[STPMIC1_##_id].desc, \
538 static struct of_regulator_match stpmic1_matches[] = {
549 MATCH(vref_ddr, VREF_DDR),
551 MATCH(pwr_sw1, VBUS_OTG),
552 MATCH(pwr_sw2, SW_OUT),
555 static int stpmic1_regulator_register(struct platform_device *pdev, int id,
556 struct of_regulator_match *match,
557 const struct stpmic1_regulator_cfg *cfg)
559 struct stpmic1 *pmic_dev = dev_get_drvdata(pdev->dev.parent);
560 struct regulator_dev *rdev;
561 struct regulator_config config = {};
565 config.dev = &pdev->dev;
566 config.init_data = match->init_data;
567 config.of_node = match->of_node;
568 config.regmap = pmic_dev->regmap;
569 config.driver_data = (void *)cfg;
571 rdev = devm_regulator_register(&pdev->dev, &cfg->desc, &config);
573 dev_err(&pdev->dev, "failed to register %s regulator\n",
575 return PTR_ERR(rdev);
579 if (of_get_property(config.of_node, "st,mask-reset", NULL) &&
580 cfg->mask_reset_reg != 0) {
581 ret = regmap_update_bits(pmic_dev->regmap,
583 cfg->mask_reset_mask,
584 cfg->mask_reset_mask);
586 dev_err(&pdev->dev, "set mask reset failed\n");
591 /* setup an irq handler for over-current detection */
592 irq = of_irq_get(config.of_node, 0);
594 ret = devm_request_threaded_irq(&pdev->dev,
596 stpmic1_curlim_irq_handler,
597 IRQF_ONESHOT | IRQF_SHARED,
600 dev_err(&pdev->dev, "Request IRQ failed\n");
607 static int stpmic1_regulator_probe(struct platform_device *pdev)
611 ret = of_regulator_match(&pdev->dev, pdev->dev.of_node, stpmic1_matches,
612 ARRAY_SIZE(stpmic1_matches));
615 "Error in PMIC regulator device tree node");
619 for (i = 0; i < ARRAY_SIZE(stpmic1_regulator_cfgs); i++) {
620 ret = stpmic1_regulator_register(pdev, i, &stpmic1_matches[i],
621 &stpmic1_regulator_cfgs[i]);
626 dev_dbg(&pdev->dev, "stpmic1_regulator driver probed\n");
631 static const struct of_device_id of_pmic_regulator_match[] = {
632 { .compatible = "st,stpmic1-regulators" },
636 MODULE_DEVICE_TABLE(of, of_pmic_regulator_match);
638 static struct platform_driver stpmic1_regulator_driver = {
640 .name = "stpmic1-regulator",
641 .of_match_table = of_match_ptr(of_pmic_regulator_match),
643 .probe = stpmic1_regulator_probe,
646 module_platform_driver(stpmic1_regulator_driver);
648 MODULE_DESCRIPTION("STPMIC1 PMIC voltage regulator driver");
649 MODULE_AUTHOR("Pascal Paillet <p.paillet@st.com>");
650 MODULE_LICENSE("GPL v2");