aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/regulator/mc13892-regulator.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/regulator/mc13892-regulator.c')
-rw-r--r--drivers/regulator/mc13892-regulator.c111
1 files changed, 93 insertions, 18 deletions
diff --git a/drivers/regulator/mc13892-regulator.c b/drivers/regulator/mc13892-regulator.c
index 0d84b1f33199..9891aec47b57 100644
--- a/drivers/regulator/mc13892-regulator.c
+++ b/drivers/regulator/mc13892-regulator.c
@@ -164,6 +164,14 @@ static const unsigned int mc13892_sw1[] = {
164 1350000, 1375000 164 1350000, 1375000
165}; 165};
166 166
167/*
168 * Note: this table is used to derive SWxVSEL by index into
169 * the array. Offset the values by the index of 1100000uV
170 * to get the actual register value for that voltage selector
171 * if the HI bit is to be set as well.
172 */
173#define MC13892_SWxHI_SEL_OFFSET 20
174
167static const unsigned int mc13892_sw[] = { 175static const unsigned int mc13892_sw[] = {
168 600000, 625000, 650000, 675000, 700000, 725000, 176 600000, 625000, 650000, 675000, 700000, 725000,
169 750000, 775000, 800000, 825000, 850000, 875000, 177 750000, 775000, 800000, 825000, 850000, 875000,
@@ -239,7 +247,6 @@ static const unsigned int mc13892_pwgtdrv[] = {
239}; 247};
240 248
241static struct regulator_ops mc13892_gpo_regulator_ops; 249static struct regulator_ops mc13892_gpo_regulator_ops;
242/* sw regulators need special care due to the "hi bit" */
243static struct regulator_ops mc13892_sw_regulator_ops; 250static struct regulator_ops mc13892_sw_regulator_ops;
244 251
245 252
@@ -396,7 +403,7 @@ static int mc13892_sw_regulator_get_voltage_sel(struct regulator_dev *rdev)
396{ 403{
397 struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev); 404 struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
398 int ret, id = rdev_get_id(rdev); 405 int ret, id = rdev_get_id(rdev);
399 unsigned int val; 406 unsigned int val, selector;
400 407
401 dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id); 408 dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
402 409
@@ -407,12 +414,28 @@ static int mc13892_sw_regulator_get_voltage_sel(struct regulator_dev *rdev)
407 if (ret) 414 if (ret)
408 return ret; 415 return ret;
409 416
410 val = (val & mc13892_regulators[id].vsel_mask) 417 /*
411 >> mc13892_regulators[id].vsel_shift; 418 * Figure out if the HI bit is set inside the switcher mode register
419 * since this means the selector value we return is at a different
420 * offset into the selector table.
421 *
422 * According to the MC13892 documentation note 59 (Table 47) the SW1
423 * buck switcher does not support output range programming therefore
424 * the HI bit must always remain 0. So do not do anything strange if
425 * our register is MC13892_SWITCHERS0.
426 */
427
428 selector = val & mc13892_regulators[id].vsel_mask;
429
430 if ((mc13892_regulators[id].vsel_reg != MC13892_SWITCHERS0) &&
431 (val & MC13892_SWITCHERS0_SWxHI)) {
432 selector += MC13892_SWxHI_SEL_OFFSET;
433 }
412 434
413 dev_dbg(rdev_get_dev(rdev), "%s id: %d val: %d\n", __func__, id, val); 435 dev_dbg(rdev_get_dev(rdev), "%s id: %d val: 0x%08x selector: %d\n",
436 __func__, id, val, selector);
414 437
415 return val; 438 return selector;
416} 439}
417 440
418static int mc13892_sw_regulator_set_voltage_sel(struct regulator_dev *rdev, 441static int mc13892_sw_regulator_set_voltage_sel(struct regulator_dev *rdev,
@@ -425,18 +448,35 @@ static int mc13892_sw_regulator_set_voltage_sel(struct regulator_dev *rdev,
425 448
426 volt = rdev->desc->volt_table[selector]; 449 volt = rdev->desc->volt_table[selector];
427 mask = mc13892_regulators[id].vsel_mask; 450 mask = mc13892_regulators[id].vsel_mask;
428 reg_value = selector << mc13892_regulators[id].vsel_shift; 451 reg_value = selector;
429 452
430 if (volt > 1375000) { 453 /*
431 mask |= MC13892_SWITCHERS0_SWxHI; 454 * Don't mess with the HI bit or support HI voltage offsets for SW1.
432 reg_value |= MC13892_SWITCHERS0_SWxHI; 455 *
433 } else if (volt < 1100000) { 456 * Since the get_voltage_sel callback has given a fudged value for
434 mask |= MC13892_SWITCHERS0_SWxHI; 457 * the selector offset, we need to back out that offset if HI is
435 reg_value &= ~MC13892_SWITCHERS0_SWxHI; 458 * to be set so we write the correct value to the register.
459 *
460 * The HI bit addition and selector offset handling COULD be more
461 * complicated by shifting and masking off the voltage selector part
462 * of the register then logical OR it back in, but since the selector
463 * is at bits 4:0 there is very little point. This makes the whole
464 * thing more readable and we do far less work.
465 */
466
467 if (mc13892_regulators[id].vsel_reg != MC13892_SWITCHERS0) {
468 if (volt > 1375000) {
469 reg_value -= MC13892_SWxHI_SEL_OFFSET;
470 reg_value |= MC13892_SWITCHERS0_SWxHI;
471 mask |= MC13892_SWITCHERS0_SWxHI;
472 } else if (volt < 1100000) {
473 reg_value &= ~MC13892_SWITCHERS0_SWxHI;
474 mask |= MC13892_SWITCHERS0_SWxHI;
475 }
436 } 476 }
437 477
438 mc13xxx_lock(priv->mc13xxx); 478 mc13xxx_lock(priv->mc13xxx);
439 ret = mc13xxx_reg_rmw(priv->mc13xxx, mc13892_regulators[id].reg, mask, 479 ret = mc13xxx_reg_rmw(priv->mc13xxx, mc13892_regulators[id].vsel_reg, mask,
440 reg_value); 480 reg_value);
441 mc13xxx_unlock(priv->mc13xxx); 481 mc13xxx_unlock(priv->mc13xxx);
442 482
@@ -495,15 +535,18 @@ static int mc13892_regulator_probe(struct platform_device *pdev)
495 struct mc13xxx_regulator_init_data *mc13xxx_data; 535 struct mc13xxx_regulator_init_data *mc13xxx_data;
496 struct regulator_config config = { }; 536 struct regulator_config config = { };
497 int i, ret; 537 int i, ret;
498 int num_regulators = 0; 538 int num_regulators = 0, num_parsed;
499 u32 val; 539 u32 val;
500 540
501 num_regulators = mc13xxx_get_num_regulators_dt(pdev); 541 num_regulators = mc13xxx_get_num_regulators_dt(pdev);
542
502 if (num_regulators <= 0 && pdata) 543 if (num_regulators <= 0 && pdata)
503 num_regulators = pdata->num_regulators; 544 num_regulators = pdata->num_regulators;
504 if (num_regulators <= 0) 545 if (num_regulators <= 0)
505 return -EINVAL; 546 return -EINVAL;
506 547
548 num_parsed = num_regulators;
549
507 priv = devm_kzalloc(&pdev->dev, sizeof(*priv) + 550 priv = devm_kzalloc(&pdev->dev, sizeof(*priv) +
508 num_regulators * sizeof(priv->regulators[0]), 551 num_regulators * sizeof(priv->regulators[0]),
509 GFP_KERNEL); 552 GFP_KERNEL);
@@ -520,7 +563,7 @@ static int mc13892_regulator_probe(struct platform_device *pdev)
520 if (ret) 563 if (ret)
521 goto err_unlock; 564 goto err_unlock;
522 565
523 /* enable switch auto mode */ 566 /* enable switch auto mode (on 2.0A silicon only) */
524 if ((val & 0x0000FFFF) == 0x45d0) { 567 if ((val & 0x0000FFFF) == 0x45d0) {
525 ret = mc13xxx_reg_rmw(mc13892, MC13892_SWITCHERS4, 568 ret = mc13xxx_reg_rmw(mc13892, MC13892_SWITCHERS4,
526 MC13892_SWITCHERS4_SW1MODE_M | 569 MC13892_SWITCHERS4_SW1MODE_M |
@@ -546,7 +589,39 @@ static int mc13892_regulator_probe(struct platform_device *pdev)
546 = mc13892_vcam_get_mode; 589 = mc13892_vcam_get_mode;
547 590
548 mc13xxx_data = mc13xxx_parse_regulators_dt(pdev, mc13892_regulators, 591 mc13xxx_data = mc13xxx_parse_regulators_dt(pdev, mc13892_regulators,
549 ARRAY_SIZE(mc13892_regulators)); 592 ARRAY_SIZE(mc13892_regulators),
593 &num_parsed);
594
595 /*
596 * Perform a little sanity check on the regulator tree - if we found
597 * a number of regulators from mc13xxx_get_num_regulators_dt and
598 * then parsed a smaller number in mc13xxx_parse_regulators_dt then
599 * there is a regulator defined in the regulators node which has
600 * not matched any usable regulator in the driver. In this case,
601 * there is one missing and what will happen is the first regulator
602 * will get registered again.
603 *
604 * Fix this by basically making our number of registerable regulators
605 * equal to the number of regulators we parsed. We are allocating
606 * too much memory for priv, but this is unavoidable at this point.
607 *
608 * As an example of how this can happen, try making a typo in your
609 * regulators node (vviohi {} instead of viohi {}) so that the name
610 * does not match..
611 *
612 * The check will basically pass for platform data (non-DT) because
613 * mc13xxx_parse_regulators_dt for !CONFIG_OF will not touch num_parsed.
614 *
615 */
616 if (num_parsed != num_regulators) {
617 dev_warn(&pdev->dev,
618 "parsed %d != regulators %d - check your device tree!\n",
619 num_parsed, num_regulators);
620
621 num_regulators = num_parsed;
622 priv->num_regulators = num_regulators;
623 }
624
550 for (i = 0; i < num_regulators; i++) { 625 for (i = 0; i < num_regulators; i++) {
551 struct regulator_init_data *init_data; 626 struct regulator_init_data *init_data;
552 struct regulator_desc *desc; 627 struct regulator_desc *desc;