1 // SPDX-License-Identifier: GPL-2.0+
5 * Peng Fan <peng.fan@nxp.com>
13 #include <power/pmic.h>
14 #include <power/regulator.h>
15 #include <power/pfuze100_pmic.h>
18 * struct pfuze100_regulator_desc - regulator descriptor
20 * @name: Identify name for the regulator.
21 * @type: Indicates the regulator type.
22 * @uV_step: Voltage increase for each selector.
23 * @vsel_reg: Register for adjust regulator voltage for normal.
24 * @vsel_mask: Mask bit for setting regulator voltage for normal.
25 * @stby_reg: Register for adjust regulator voltage for standby.
26 * @stby_mask: Mask bit for setting regulator voltage for standby.
27 * @volt_table: Voltage mapping table (if table based mapping).
28 * @voltage: Current voltage for REGULATOR_TYPE_FIXED type regulator.
30 struct pfuze100_regulator_desc {
32 enum regulator_type type;
34 unsigned int vsel_reg;
35 unsigned int vsel_mask;
36 unsigned int stby_reg;
37 unsigned int stby_mask;
38 unsigned int *volt_table;
43 * struct pfuze100_regulator_platdata - platform data for pfuze100
45 * @desc: Points the description entry of one regulator of pfuze100
47 struct pfuze100_regulator_platdata {
48 struct pfuze100_regulator_desc *desc;
51 #define PFUZE100_FIXED_REG(_name, base, vol) \
54 .type = REGULATOR_TYPE_FIXED, \
58 #define PFUZE100_SW_REG(_name, base, step) \
61 .type = REGULATOR_TYPE_BUCK, \
63 .vsel_reg = (base) + PFUZE100_VOL_OFFSET, \
65 .stby_reg = (base) + PFUZE100_STBY_OFFSET, \
69 #define PFUZE100_SWB_REG(_name, base, mask, step, voltages) \
72 .type = REGULATOR_TYPE_BUCK, \
75 .vsel_mask = (mask), \
76 .volt_table = (voltages), \
79 #define PFUZE100_SNVS_REG(_name, base, mask, voltages) \
82 .type = REGULATOR_TYPE_OTHER, \
84 .vsel_mask = (mask), \
85 .volt_table = (voltages), \
88 #define PFUZE100_VGEN_REG(_name, base, step) \
91 .type = REGULATOR_TYPE_LDO, \
99 #define PFUZE3000_VCC_REG(_name, base, step) \
102 .type = REGULATOR_TYPE_LDO, \
104 .vsel_reg = (base), \
106 .stby_reg = (base), \
110 #define PFUZE3000_SW1_REG(_name, base, step) \
113 .type = REGULATOR_TYPE_BUCK, \
115 .vsel_reg = (base) + PFUZE100_VOL_OFFSET, \
117 .stby_reg = (base) + PFUZE100_STBY_OFFSET, \
121 #define PFUZE3000_SW2_REG(_name, base, step) \
124 .type = REGULATOR_TYPE_BUCK, \
126 .vsel_reg = (base) + PFUZE100_VOL_OFFSET, \
128 .stby_reg = (base) + PFUZE100_STBY_OFFSET, \
132 #define PFUZE3000_SW3_REG(_name, base, step) \
135 .type = REGULATOR_TYPE_BUCK, \
137 .vsel_reg = (base) + PFUZE100_VOL_OFFSET, \
139 .stby_reg = (base) + PFUZE100_STBY_OFFSET, \
143 static unsigned int pfuze100_swbst[] = {
144 5000000, 5050000, 5100000, 5150000
147 static unsigned int pfuze100_vsnvs[] = {
148 1000000, 1100000, 1200000, 1300000, 1500000, 1800000, 3000000, -1
151 static unsigned int pfuze3000_vsnvs[] = {
152 -1, -1, -1, -1, -1, -1, 3000000, -1
155 static unsigned int pfuze3000_sw2lo[] = {
156 1500000, 1550000, 1600000, 1650000, 1700000, 1750000, 1800000, 1850000
160 static struct pfuze100_regulator_desc pfuze100_regulators[] = {
161 PFUZE100_SW_REG(sw1ab, PFUZE100_SW1ABVOL, 25000),
162 PFUZE100_SW_REG(sw1c, PFUZE100_SW1CVOL, 25000),
163 PFUZE100_SW_REG(sw2, PFUZE100_SW2VOL, 25000),
164 PFUZE100_SW_REG(sw3a, PFUZE100_SW3AVOL, 25000),
165 PFUZE100_SW_REG(sw3b, PFUZE100_SW3BVOL, 25000),
166 PFUZE100_SW_REG(sw4, PFUZE100_SW4VOL, 25000),
167 PFUZE100_SWB_REG(swbst, PFUZE100_SWBSTCON1, 0x3, 50000, pfuze100_swbst),
168 PFUZE100_SNVS_REG(vsnvs, PFUZE100_VSNVSVOL, 0x7, pfuze100_vsnvs),
169 PFUZE100_FIXED_REG(vrefddr, PFUZE100_VREFDDRCON, 750000),
170 PFUZE100_VGEN_REG(vgen1, PFUZE100_VGEN1VOL, 50000),
171 PFUZE100_VGEN_REG(vgen2, PFUZE100_VGEN2VOL, 50000),
172 PFUZE100_VGEN_REG(vgen3, PFUZE100_VGEN3VOL, 100000),
173 PFUZE100_VGEN_REG(vgen4, PFUZE100_VGEN4VOL, 100000),
174 PFUZE100_VGEN_REG(vgen5, PFUZE100_VGEN5VOL, 100000),
175 PFUZE100_VGEN_REG(vgen6, PFUZE100_VGEN6VOL, 100000),
179 static struct pfuze100_regulator_desc pfuze200_regulators[] = {
180 PFUZE100_SW_REG(sw1ab, PFUZE100_SW1ABVOL, 25000),
181 PFUZE100_SW_REG(sw2, PFUZE100_SW2VOL, 25000),
182 PFUZE100_SW_REG(sw3a, PFUZE100_SW3AVOL, 25000),
183 PFUZE100_SW_REG(sw3b, PFUZE100_SW3BVOL, 25000),
184 PFUZE100_SWB_REG(swbst, PFUZE100_SWBSTCON1, 0x3, 50000, pfuze100_swbst),
185 PFUZE100_SNVS_REG(vsnvs, PFUZE100_VSNVSVOL, 0x7, pfuze100_vsnvs),
186 PFUZE100_FIXED_REG(vrefddr, PFUZE100_VREFDDRCON, 750000),
187 PFUZE100_VGEN_REG(vgen1, PFUZE100_VGEN1VOL, 50000),
188 PFUZE100_VGEN_REG(vgen2, PFUZE100_VGEN2VOL, 50000),
189 PFUZE100_VGEN_REG(vgen3, PFUZE100_VGEN3VOL, 100000),
190 PFUZE100_VGEN_REG(vgen4, PFUZE100_VGEN4VOL, 100000),
191 PFUZE100_VGEN_REG(vgen5, PFUZE100_VGEN5VOL, 100000),
192 PFUZE100_VGEN_REG(vgen6, PFUZE100_VGEN6VOL, 100000),
196 static struct pfuze100_regulator_desc pfuze3000_regulators[] = {
197 PFUZE3000_SW1_REG(sw1a, PFUZE100_SW1ABVOL, 25000),
198 PFUZE3000_SW1_REG(sw1b, PFUZE100_SW1CVOL, 25000),
199 PFUZE100_SWB_REG(sw2, PFUZE100_SW2VOL, 0x7, 50000, pfuze3000_sw2lo),
200 PFUZE3000_SW3_REG(sw3, PFUZE100_SW3AVOL, 50000),
201 PFUZE100_SWB_REG(swbst, PFUZE100_SWBSTCON1, 0x3, 50000, pfuze100_swbst),
202 PFUZE100_SNVS_REG(vsnvs, PFUZE100_VSNVSVOL, 0x7, pfuze3000_vsnvs),
203 PFUZE100_FIXED_REG(vrefddr, PFUZE100_VREFDDRCON, 750000),
204 PFUZE100_VGEN_REG(vldo1, PFUZE100_VGEN1VOL, 100000),
205 PFUZE100_VGEN_REG(vldo2, PFUZE100_VGEN2VOL, 50000),
206 PFUZE3000_VCC_REG(vccsd, PFUZE100_VGEN3VOL, 150000),
207 PFUZE3000_VCC_REG(v33, PFUZE100_VGEN4VOL, 150000),
208 PFUZE100_VGEN_REG(vldo3, PFUZE100_VGEN5VOL, 100000),
209 PFUZE100_VGEN_REG(vldo4, PFUZE100_VGEN6VOL, 100000),
212 #define MODE(_id, _val, _name) { \
214 .register_value = _val, \
218 /* SWx Buck regulator mode */
219 static struct dm_regulator_mode pfuze_sw_modes[] = {
220 MODE(OFF_OFF, OFF_OFF, "OFF_OFF"),
221 MODE(PWM_OFF, PWM_OFF, "PWM_OFF"),
222 MODE(PFM_OFF, PFM_OFF, "PFM_OFF"),
223 MODE(APS_OFF, APS_OFF, "APS_OFF"),
224 MODE(PWM_PWM, PWM_PWM, "PWM_PWM"),
225 MODE(PWM_APS, PWM_APS, "PWM_APS"),
226 MODE(APS_APS, APS_APS, "APS_APS"),
227 MODE(APS_PFM, APS_PFM, "APS_PFM"),
228 MODE(PWM_PFM, PWM_PFM, "PWM_PFM"),
231 /* Boost Buck regulator mode for normal operation */
232 static struct dm_regulator_mode pfuze_swbst_modes[] = {
233 MODE(SWBST_MODE_OFF, SWBST_MODE_OFF , "SWBST_MODE_OFF"),
234 MODE(SWBST_MODE_PFM, SWBST_MODE_PFM, "SWBST_MODE_PFM"),
235 MODE(SWBST_MODE_AUTO, SWBST_MODE_AUTO, "SWBST_MODE_AUTO"),
236 MODE(SWBST_MODE_APS, SWBST_MODE_APS, "SWBST_MODE_APS"),
239 /* VGENx LDO regulator mode for normal operation */
240 static struct dm_regulator_mode pfuze_ldo_modes[] = {
241 MODE(LDO_MODE_OFF, LDO_MODE_OFF, "LDO_MODE_OFF"),
242 MODE(LDO_MODE_ON, LDO_MODE_ON, "LDO_MODE_ON"),
245 static struct pfuze100_regulator_desc *se_desc(struct pfuze100_regulator_desc *desc,
251 for (i = 0; i < size; desc++) {
252 if (!strcmp(desc->name, name))
260 static int pfuze100_regulator_probe(struct udevice *dev)
262 struct dm_regulator_uclass_platdata *uc_pdata;
263 struct pfuze100_regulator_platdata *plat = dev_get_platdata(dev);
264 struct pfuze100_regulator_desc *desc;
266 switch (dev_get_driver_data(dev_get_parent(dev))) {
268 desc = se_desc(pfuze100_regulators,
269 ARRAY_SIZE(pfuze100_regulators),
273 desc = se_desc(pfuze200_regulators,
274 ARRAY_SIZE(pfuze200_regulators),
278 desc = se_desc(pfuze3000_regulators,
279 ARRAY_SIZE(pfuze3000_regulators),
283 debug("Unsupported PFUZE\n");
287 debug("Do not support regulator %s\n", dev->name);
292 uc_pdata = dev_get_uclass_platdata(dev);
294 uc_pdata->type = desc->type;
295 if (uc_pdata->type == REGULATOR_TYPE_BUCK) {
296 if (!strcmp(dev->name, "swbst")) {
297 uc_pdata->mode = pfuze_swbst_modes;
298 uc_pdata->mode_count = ARRAY_SIZE(pfuze_swbst_modes);
300 uc_pdata->mode = pfuze_sw_modes;
301 uc_pdata->mode_count = ARRAY_SIZE(pfuze_sw_modes);
303 } else if (uc_pdata->type == REGULATOR_TYPE_LDO) {
304 uc_pdata->mode = pfuze_ldo_modes;
305 uc_pdata->mode_count = ARRAY_SIZE(pfuze_ldo_modes);
307 uc_pdata->mode = NULL;
308 uc_pdata->mode_count = 0;
314 static int pfuze100_regulator_mode(struct udevice *dev, int op, int *opmode)
317 struct pfuze100_regulator_platdata *plat = dev_get_platdata(dev);
318 struct pfuze100_regulator_desc *desc = plat->desc;
320 if (op == PMIC_OP_GET) {
321 if (desc->type == REGULATOR_TYPE_BUCK) {
322 if (!strcmp(dev->name, "swbst")) {
323 val = pmic_reg_read(dev->parent,
328 val &= SWBST_MODE_MASK;
329 val >>= SWBST_MODE_SHIFT;
334 val = pmic_reg_read(dev->parent,
336 PFUZE100_MODE_OFFSET);
341 val >>= SW_MODE_SHIFT;
346 } else if (desc->type == REGULATOR_TYPE_LDO) {
347 val = pmic_reg_read(dev->parent, desc->vsel_reg);
351 val &= LDO_MODE_MASK;
352 val >>= LDO_MODE_SHIFT;
361 if (desc->type == REGULATOR_TYPE_BUCK) {
362 if (!strcmp(dev->name, "swbst"))
363 return pmic_clrsetbits(dev->parent, desc->vsel_reg,
365 *opmode << SWBST_MODE_SHIFT);
367 val = pmic_clrsetbits(dev->parent,
368 desc->vsel_reg + PFUZE100_MODE_OFFSET,
370 *opmode << SW_MODE_SHIFT);
372 } else if (desc->type == REGULATOR_TYPE_LDO) {
373 val = pmic_clrsetbits(dev->parent, desc->vsel_reg,
375 *opmode << LDO_MODE_SHIFT);
384 static int pfuze100_regulator_enable(struct udevice *dev, int op, bool *enable)
388 struct dm_regulator_uclass_platdata *uc_pdata =
389 dev_get_uclass_platdata(dev);
391 if (op == PMIC_OP_GET) {
392 if (!strcmp(dev->name, "vrefddr")) {
393 val = pmic_reg_read(dev->parent, PFUZE100_VREFDDRCON);
397 if (val & VREFDDRCON_EN)
403 ret = pfuze100_regulator_mode(dev, op, &on_off);
407 /* OFF_OFF, SWBST_MODE_OFF, LDO_MODE_OFF have same value */
415 } else if (op == PMIC_OP_SET) {
416 if (!strcmp(dev->name, "vrefddr")) {
417 val = pmic_reg_read(dev->parent, PFUZE100_VREFDDRCON);
421 if (val & VREFDDRCON_EN)
423 val |= VREFDDRCON_EN;
425 return pmic_reg_write(dev->parent, PFUZE100_VREFDDRCON,
429 if (uc_pdata->type == REGULATOR_TYPE_LDO) {
430 on_off = *enable ? LDO_MODE_ON : LDO_MODE_OFF;
431 } else if (uc_pdata->type == REGULATOR_TYPE_BUCK) {
432 if (!strcmp(dev->name, "swbst"))
433 on_off = *enable ? SWBST_MODE_AUTO :
436 on_off = *enable ? APS_PFM : OFF_OFF;
441 return pfuze100_regulator_mode(dev, op, &on_off);
447 static int pfuze100_regulator_val(struct udevice *dev, int op, int *uV)
451 struct pfuze100_regulator_platdata *plat = dev_get_platdata(dev);
452 struct pfuze100_regulator_desc *desc = plat->desc;
453 struct dm_regulator_uclass_platdata *uc_pdata =
454 dev_get_uclass_platdata(dev);
456 if (op == PMIC_OP_GET) {
458 if (uc_pdata->type == REGULATOR_TYPE_FIXED) {
460 } else if (desc->volt_table) {
461 val = pmic_reg_read(dev->parent, desc->vsel_reg);
464 val &= desc->vsel_mask;
465 *uV = desc->volt_table[val];
467 if (uc_pdata->min_uV < 0) {
468 debug("Need to provide min_uV in dts.\n");
471 val = pmic_reg_read(dev->parent, desc->vsel_reg);
474 val &= desc->vsel_mask;
475 *uV = uc_pdata->min_uV + (int)val * desc->uV_step;
481 if (uc_pdata->type == REGULATOR_TYPE_FIXED) {
482 debug("Set voltage for REGULATOR_TYPE_FIXED regulator\n");
484 } else if (desc->volt_table) {
485 for (i = 0; i < desc->vsel_mask; i++) {
486 if (*uV == desc->volt_table[i])
489 if (i == desc->vsel_mask) {
490 debug("Unsupported voltage %u\n", *uV);
494 return pmic_clrsetbits(dev->parent, desc->vsel_reg,
497 if (uc_pdata->min_uV < 0) {
498 debug("Need to provide min_uV in dts.\n");
501 return pmic_clrsetbits(dev->parent, desc->vsel_reg,
503 (*uV - uc_pdata->min_uV) / desc->uV_step);
509 static int pfuze100_regulator_get_value(struct udevice *dev)
514 ret = pfuze100_regulator_val(dev, PMIC_OP_GET, &uV);
521 static int pfuze100_regulator_set_value(struct udevice *dev, int uV)
523 return pfuze100_regulator_val(dev, PMIC_OP_SET, &uV);
526 static int pfuze100_regulator_get_enable(struct udevice *dev)
531 ret = pfuze100_regulator_enable(dev, PMIC_OP_GET, &enable);
538 static int pfuze100_regulator_set_enable(struct udevice *dev, bool enable)
540 return pfuze100_regulator_enable(dev, PMIC_OP_SET, &enable);
543 static int pfuze100_regulator_get_mode(struct udevice *dev)
548 ret = pfuze100_regulator_mode(dev, PMIC_OP_GET, &mode);
555 static int pfuze100_regulator_set_mode(struct udevice *dev, int mode)
557 return pfuze100_regulator_mode(dev, PMIC_OP_SET, &mode);
560 static const struct dm_regulator_ops pfuze100_regulator_ops = {
561 .get_value = pfuze100_regulator_get_value,
562 .set_value = pfuze100_regulator_set_value,
563 .get_enable = pfuze100_regulator_get_enable,
564 .set_enable = pfuze100_regulator_set_enable,
565 .get_mode = pfuze100_regulator_get_mode,
566 .set_mode = pfuze100_regulator_set_mode,
569 U_BOOT_DRIVER(pfuze100_regulator) = {
570 .name = "pfuze100_regulator",
571 .id = UCLASS_REGULATOR,
572 .ops = &pfuze100_regulator_ops,
573 .probe = pfuze100_regulator_probe,
574 .platdata_auto_alloc_size = sizeof(struct pfuze100_regulator_platdata),