27 #include <linux/slab.h>
28 #include <linux/module.h>
55 static inline void max8997_set_gpio(
struct max8997_data *max8997)
74 .min = 800, .max = 3950, .step = 50,
78 .min = 650, .max = 2225, .step = 25,
82 .min = 750, .max = 3900, .step = 50,
87 .min = 200, .max = 950, .step = 50,
91 .min = 50, .max = 200, .step = 10,
154 static int max8997_list_voltage_charger_cv(
struct regulator_dev *rdev,
155 unsigned int selector)
166 return 4000000 + 20000 * (selector - 0x01);
177 unsigned int selector)
187 desc = reg_voltage_map[rid];
191 val = desc->
min + desc->
step * selector;
198 static int max8997_get_enable_register(
struct regulator_dev *rdev,
272 static int max8997_reg_is_enabled(
struct regulator_dev *rdev)
279 ret = max8997_get_enable_register(rdev, ®, &mask, &pattern);
287 return (val & mask) ==
pattern;
296 ret = max8997_get_enable_register(rdev, ®, &mask, &pattern);
309 ret = max8997_get_enable_register(rdev, ®, &mask, &pattern);
316 static int max8997_get_voltage_register(
struct regulator_dev *rdev,
317 int *_reg,
int *_shift,
int *_mask)
321 int reg, shift = 0, mask = 0x3f;
382 static int max8997_get_voltage_sel(
struct regulator_dev *rdev)
389 ret = max8997_get_voltage_register(rdev, ®, &shift, &mask);
403 static inline int max8997_get_voltage_proper_val(
405 int min_vol,
int max_vol)
412 if (max_vol < desc->
min || min_vol > desc->
max)
415 if (min_vol < desc->
min)
420 if (desc->
min + desc->
step * i > max_vol)
426 static int max8997_set_voltage_charger_cv(
struct regulator_dev *rdev,
427 int min_uV,
int max_uV,
unsigned *selector)
433 int reg, shift = 0,
mask, ret = 0;
439 ret = max8997_get_voltage_register(rdev, ®, &shift, &mask);
443 if (max_uV < 4000000 || min_uV > 4350000)
446 if (min_uV <= 4000000) {
447 if (max_uV >= 4000000)
451 }
else if (min_uV <= 4200000 && max_uV >= 4200000)
454 lb = (min_uV - 4000001) / 20000 + 2;
455 ub = (max_uV - 4000000) / 20000 + 1;
481 static int max8997_set_voltage_ldobuck(
struct regulator_dev *rdev,
482 int min_uV,
int max_uV,
unsigned *selector)
486 int min_vol = min_uV / 1000, max_vol = max_uV / 1000;
508 desc = reg_voltage_map[rid];
510 i = max8997_get_voltage_proper_val(desc, min_vol, max_vol);
514 ret = max8997_get_voltage_register(rdev, ®, &shift, &mask);
524 static int max8997_set_voltage_ldobuck_time_sel(
struct regulator_dev *rdev,
525 unsigned int old_selector,
526 unsigned int new_selector)
533 if (old_selector >= new_selector)
572 static int max8997_assess_side_effect(
struct regulator_dev *rdev,
573 u8 new_val,
int *best)
578 bool buckx_gpiodvs[3];
606 for (i = 0; i < 8; i++) {
609 if (new_val != (buckx_val[rid])[i]) {
615 for (others = 0; others < 3; others++) {
620 if (buckx_gpiodvs[others] ==
false)
622 diff = (buckx_val[others])[i] -
625 side_effect[
i] += diff;
627 side_effect[
i] -= diff;
629 if (side_effect[i] == 0) {
633 if (side_effect[i] < min_side_effect) {
634 min_side_effect = side_effect[
i];
642 return side_effect[*best];
649 static int max8997_set_voltage_buck(
struct regulator_dev *rdev,
650 int min_uV,
int max_uV,
unsigned *selector)
655 int new_val, new_idx, damage, tmp_val, tmp_idx, tmp_dmg;
656 bool gpio_dvs_mode =
false;
657 int min_vol = min_uV / 1000, max_vol = max_uV / 1000;
665 gpio_dvs_mode =
true;
669 gpio_dvs_mode =
true;
673 gpio_dvs_mode =
true;
678 return max8997_set_voltage_ldobuck(rdev, min_uV, max_uV,
681 desc = reg_voltage_map[rid];
682 new_val = max8997_get_voltage_proper_val(desc, min_vol, max_vol);
690 damage = max8997_assess_side_effect(rdev, new_val, &new_idx);
694 if (tmp_dmg > damage) {
701 }
while (desc->
min + desc->
step * new_val <= desc->
max);
709 dev_warn(&rdev->
dev,
"MAX8997 GPIO-DVS Side Effect Warning: GPIO SET:"
713 if (new_idx < 0 || new_val < 0)
717 max8997_set_gpio(max8997);
723 static const int safeoutvolt[] = {
731 static int max8997_set_voltage_safeout(
struct regulator_dev *rdev,
732 int min_uV,
int max_uV,
unsigned *selector)
744 for (i = 0; i <
ARRAY_SIZE(safeoutvolt); i++) {
745 if (min_uV <= safeoutvolt[i] &&
746 max_uV >= safeoutvolt[i])
758 ret = max8997_get_voltage_register(rdev, ®, &shift, &mask);
768 static int max8997_reg_disable_suspend(
struct regulator_dev *rdev)
775 ret = max8997_get_enable_register(rdev, ®, &mask, &pattern);
784 dev_dbg(&rdev->
dev,
"Conditional Power-Off for %s\n",
789 dev_dbg(&rdev->
dev,
"Full Power-Off for %s (%xh -> %xh)\n",
796 .list_voltage = max8997_list_voltage,
797 .is_enabled = max8997_reg_is_enabled,
798 .enable = max8997_reg_enable,
799 .disable = max8997_reg_disable,
800 .get_voltage_sel = max8997_get_voltage_sel,
801 .set_voltage = max8997_set_voltage_ldobuck,
802 .set_voltage_time_sel = max8997_set_voltage_ldobuck_time_sel,
803 .set_suspend_disable = max8997_reg_disable_suspend,
807 .list_voltage = max8997_list_voltage,
808 .is_enabled = max8997_reg_is_enabled,
809 .enable = max8997_reg_enable,
810 .disable = max8997_reg_disable,
811 .get_voltage_sel = max8997_get_voltage_sel,
812 .set_voltage = max8997_set_voltage_buck,
813 .set_voltage_time_sel = max8997_set_voltage_ldobuck_time_sel,
814 .set_suspend_disable = max8997_reg_disable_suspend,
818 .list_voltage = max8997_list_voltage,
819 .is_enabled = max8997_reg_is_enabled,
820 .enable = max8997_reg_enable,
821 .disable = max8997_reg_disable,
822 .set_suspend_disable = max8997_reg_disable_suspend,
826 .list_voltage = max8997_list_voltage_safeout,
827 .is_enabled = max8997_reg_is_enabled,
828 .enable = max8997_reg_enable,
829 .disable = max8997_reg_disable,
830 .get_voltage_sel = max8997_get_voltage_sel,
831 .set_voltage = max8997_set_voltage_safeout,
832 .set_suspend_disable = max8997_reg_disable_suspend,
836 .list_voltage = max8997_list_voltage_charger_cv,
837 .get_voltage_sel = max8997_get_voltage_sel,
838 .set_voltage = max8997_set_voltage_charger_cv,
841 static int max8997_set_current_limit(
struct regulator_dev *rdev,
842 int min_uA,
int max_uA)
851 return max8997_set_voltage_ldobuck(rdev, min_uA, max_uA, &dummy);
854 static int max8997_get_current_limit(
struct regulator_dev *rdev)
861 sel = max8997_get_voltage_sel(rdev);
866 return max8997_list_voltage(rdev, sel);
870 .is_enabled = max8997_reg_is_enabled,
871 .enable = max8997_reg_enable,
872 .disable = max8997_reg_disable,
873 .get_current_limit = max8997_get_current_limit,
874 .set_current_limit = max8997_set_current_limit,
877 static struct regulator_ops max8997_charger_fixedstate_ops = {
878 .get_current_limit = max8997_get_current_limit,
879 .set_current_limit = max8997_set_current_limit,
882 #define MAX8997_VOLTAGE_REGULATOR(_name, _ops) {\
884 .id = MAX8997_##_name, \
886 .type = REGULATOR_VOLTAGE, \
887 .owner = THIS_MODULE, \
890 #define MAX8997_CURRENT_REGULATOR(_name, _ops) {\
892 .id = MAX8997_##_name, \
894 .type = REGULATOR_CURRENT, \
895 .owner = THIS_MODULE, \
933 max8997_charger_fixedstate_ops),
945 u8 max_buck1 = 0, max_buck2 = 0, max_buck5 = 0;
948 dev_err(pdev->
dev.parent,
"No platform init data supplied.\n");
962 rdev = max8997->
rdev;
963 max8997->
dev = &pdev->
dev;
964 max8997->
iodev = iodev;
966 platform_set_drvdata(pdev, max8997);
967 i2c = max8997->
iodev->i2c;
976 for (i = 0; i < 8; i++) {
978 max8997_get_voltage_proper_val(
979 &buck1245_voltage_map_desc,
982 buck1245_voltage_map_desc.
step);
987 max8997_get_voltage_proper_val(
988 &buck1245_voltage_map_desc,
991 buck1245_voltage_map_desc.
step);
996 max8997_get_voltage_proper_val(
997 &buck1245_voltage_map_desc,
1000 buck1245_voltage_map_desc.
step);
1004 if (max_buck1 < max8997->buck1_vol[i])
1006 if (max_buck2 < max8997->buck2_vol[i])
1008 if (max_buck5 < max8997->buck5_vol[i])
1013 for (i = 0; i < 8; i++) {
1065 (1 << 1) : (0 << 1), 1 << 1);
1067 (1 << 1) : (0 << 1), 1 << 1);
1069 (1 << 1) : (0 << 1), 1 << 1);
1072 for (i = 0; i < 8; i++) {
1092 desc = reg_voltage_map[
id];
1101 config.
dev = max8997->
dev;
1106 if (IS_ERR(rdev[i])) {
1107 ret = PTR_ERR(rdev[i]);
1108 dev_err(max8997->
dev,
"regulator init failed for %d\n",
1125 struct max8997_data *max8997 = platform_get_drvdata(pdev);
1135 {
"max8997-pmic", 0},
1142 .name =
"max8997-pmic",
1145 .probe = max8997_pmic_probe,
1147 .id_table = max8997_pmic_id,
1150 static int __init max8997_pmic_init(
void)
1156 static void __exit max8997_pmic_cleanup(
void)