root/drivers/regulator/max14577-regulator.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. max14577_reg_is_enabled
  2. max14577_reg_get_current_limit
  3. max14577_reg_set_current_limit
  4. max14577_get_regmap
  5. max14577_regulator_probe
  6. max14577_regulator_init
  7. max14577_regulator_exit

   1 // SPDX-License-Identifier: GPL-2.0+
   2 //
   3 // max14577.c - Regulator driver for the Maxim 14577/77836
   4 //
   5 // Copyright (C) 2013,2014 Samsung Electronics
   6 // Krzysztof Kozlowski <krzk@kernel.org>
   7 
   8 #include <linux/module.h>
   9 #include <linux/platform_device.h>
  10 #include <linux/regulator/driver.h>
  11 #include <linux/mfd/max14577.h>
  12 #include <linux/mfd/max14577-private.h>
  13 #include <linux/regulator/of_regulator.h>
  14 
  15 static int max14577_reg_is_enabled(struct regulator_dev *rdev)
  16 {
  17         int rid = rdev_get_id(rdev);
  18         struct regmap *rmap = rdev->regmap;
  19         u8 reg_data;
  20 
  21         switch (rid) {
  22         case MAX14577_CHARGER:
  23                 max14577_read_reg(rmap, MAX14577_CHG_REG_CHG_CTRL2, &reg_data);
  24                 if ((reg_data & CHGCTRL2_MBCHOSTEN_MASK) == 0)
  25                         return 0;
  26                 max14577_read_reg(rmap, MAX14577_CHG_REG_STATUS3, &reg_data);
  27                 if ((reg_data & STATUS3_CGMBC_MASK) == 0)
  28                         return 0;
  29                 /* MBCHOSTEN and CGMBC are on */
  30                 return 1;
  31         default:
  32                 return -EINVAL;
  33         }
  34 }
  35 
  36 static int max14577_reg_get_current_limit(struct regulator_dev *rdev)
  37 {
  38         u8 reg_data;
  39         struct regmap *rmap = rdev->regmap;
  40         struct max14577 *max14577 = rdev_get_drvdata(rdev);
  41         const struct maxim_charger_current *limits =
  42                 &maxim_charger_currents[max14577->dev_type];
  43 
  44         if (rdev_get_id(rdev) != MAX14577_CHARGER)
  45                 return -EINVAL;
  46 
  47         max14577_read_reg(rmap, MAX14577_CHG_REG_CHG_CTRL4, &reg_data);
  48 
  49         if ((reg_data & CHGCTRL4_MBCICHWRCL_MASK) == 0)
  50                 return limits->min;
  51 
  52         reg_data = ((reg_data & CHGCTRL4_MBCICHWRCH_MASK) >>
  53                         CHGCTRL4_MBCICHWRCH_SHIFT);
  54         return limits->high_start + reg_data * limits->high_step;
  55 }
  56 
  57 static int max14577_reg_set_current_limit(struct regulator_dev *rdev,
  58                 int min_uA, int max_uA)
  59 {
  60         u8 reg_data;
  61         int ret;
  62         struct max14577 *max14577 = rdev_get_drvdata(rdev);
  63         const struct maxim_charger_current *limits =
  64                 &maxim_charger_currents[max14577->dev_type];
  65 
  66         if (rdev_get_id(rdev) != MAX14577_CHARGER)
  67                 return -EINVAL;
  68 
  69         ret = maxim_charger_calc_reg_current(limits, min_uA, max_uA, &reg_data);
  70         if (ret)
  71                 return ret;
  72 
  73         return max14577_update_reg(rdev->regmap, MAX14577_CHG_REG_CHG_CTRL4,
  74                         CHGCTRL4_MBCICHWRCL_MASK | CHGCTRL4_MBCICHWRCH_MASK,
  75                         reg_data);
  76 }
  77 
  78 static const struct regulator_ops max14577_safeout_ops = {
  79         .is_enabled             = regulator_is_enabled_regmap,
  80         .enable                 = regulator_enable_regmap,
  81         .disable                = regulator_disable_regmap,
  82         .list_voltage           = regulator_list_voltage_linear,
  83 };
  84 
  85 static const struct regulator_ops max14577_charger_ops = {
  86         .is_enabled             = max14577_reg_is_enabled,
  87         .enable                 = regulator_enable_regmap,
  88         .disable                = regulator_disable_regmap,
  89         .get_current_limit      = max14577_reg_get_current_limit,
  90         .set_current_limit      = max14577_reg_set_current_limit,
  91 };
  92 
  93 #define MAX14577_SAFEOUT_REG    { \
  94         .name           = "SAFEOUT", \
  95         .of_match       = of_match_ptr("SAFEOUT"), \
  96         .regulators_node = of_match_ptr("regulators"), \
  97         .id             = MAX14577_SAFEOUT, \
  98         .ops            = &max14577_safeout_ops, \
  99         .type           = REGULATOR_VOLTAGE, \
 100         .owner          = THIS_MODULE, \
 101         .n_voltages     = 1, \
 102         .min_uV         = MAX14577_REGULATOR_SAFEOUT_VOLTAGE, \
 103         .enable_reg     = MAX14577_REG_CONTROL2, \
 104         .enable_mask    = CTRL2_SFOUTORD_MASK, \
 105 }
 106 #define MAX14577_CHARGER_REG    { \
 107         .name           = "CHARGER", \
 108         .of_match       = of_match_ptr("CHARGER"), \
 109         .regulators_node = of_match_ptr("regulators"), \
 110         .id             = MAX14577_CHARGER, \
 111         .ops            = &max14577_charger_ops, \
 112         .type           = REGULATOR_CURRENT, \
 113         .owner          = THIS_MODULE, \
 114         .enable_reg     = MAX14577_CHG_REG_CHG_CTRL2, \
 115         .enable_mask    = CHGCTRL2_MBCHOSTEN_MASK, \
 116 }
 117 
 118 static const struct regulator_desc max14577_supported_regulators[] = {
 119         [MAX14577_SAFEOUT] = MAX14577_SAFEOUT_REG,
 120         [MAX14577_CHARGER] = MAX14577_CHARGER_REG,
 121 };
 122 
 123 static const struct regulator_ops max77836_ldo_ops = {
 124         .is_enabled             = regulator_is_enabled_regmap,
 125         .enable                 = regulator_enable_regmap,
 126         .disable                = regulator_disable_regmap,
 127         .list_voltage           = regulator_list_voltage_linear,
 128         .map_voltage            = regulator_map_voltage_linear,
 129         .get_voltage_sel        = regulator_get_voltage_sel_regmap,
 130         .set_voltage_sel        = regulator_set_voltage_sel_regmap,
 131         /* TODO: add .set_suspend_mode */
 132 };
 133 
 134 #define MAX77836_LDO_REG(num)   { \
 135         .name           = "LDO" # num, \
 136         .of_match       = of_match_ptr("LDO" # num), \
 137         .regulators_node = of_match_ptr("regulators"), \
 138         .id             = MAX77836_LDO ## num, \
 139         .ops            = &max77836_ldo_ops, \
 140         .type           = REGULATOR_VOLTAGE, \
 141         .owner          = THIS_MODULE, \
 142         .n_voltages     = MAX77836_REGULATOR_LDO_VOLTAGE_STEPS_NUM, \
 143         .min_uV         = MAX77836_REGULATOR_LDO_VOLTAGE_MIN, \
 144         .uV_step        = MAX77836_REGULATOR_LDO_VOLTAGE_STEP, \
 145         .enable_reg     = MAX77836_LDO_REG_CNFG1_LDO ## num, \
 146         .enable_mask    = MAX77836_CNFG1_LDO_PWRMD_MASK, \
 147         .vsel_reg       = MAX77836_LDO_REG_CNFG1_LDO ## num, \
 148         .vsel_mask      = MAX77836_CNFG1_LDO_TV_MASK, \
 149 }
 150 
 151 static const struct regulator_desc max77836_supported_regulators[] = {
 152         [MAX14577_SAFEOUT] = MAX14577_SAFEOUT_REG,
 153         [MAX14577_CHARGER] = MAX14577_CHARGER_REG,
 154         [MAX77836_LDO1] = MAX77836_LDO_REG(1),
 155         [MAX77836_LDO2] = MAX77836_LDO_REG(2),
 156 };
 157 
 158 /**
 159  * Registers for regulators of max77836 use different I2C slave addresses so
 160  * different regmaps must be used for them.
 161  *
 162  * Returns proper regmap for accessing regulator passed by id.
 163  */
 164 static struct regmap *max14577_get_regmap(struct max14577 *max14577,
 165                 int reg_id)
 166 {
 167         switch (max14577->dev_type) {
 168         case MAXIM_DEVICE_TYPE_MAX77836:
 169                 switch (reg_id) {
 170                 case MAX77836_SAFEOUT ... MAX77836_CHARGER:
 171                         return max14577->regmap;
 172                 default:
 173                         /* MAX77836_LDO1 ... MAX77836_LDO2 */
 174                         return max14577->regmap_pmic;
 175                 }
 176 
 177         case MAXIM_DEVICE_TYPE_MAX14577:
 178         default:
 179                 return max14577->regmap;
 180         }
 181 }
 182 
 183 static int max14577_regulator_probe(struct platform_device *pdev)
 184 {
 185         struct max14577 *max14577 = dev_get_drvdata(pdev->dev.parent);
 186         struct max14577_platform_data *pdata = dev_get_platdata(max14577->dev);
 187         int i, ret = 0;
 188         struct regulator_config config = {};
 189         const struct regulator_desc *supported_regulators;
 190         unsigned int supported_regulators_size;
 191         enum maxim_device_type dev_type = max14577->dev_type;
 192 
 193         switch (dev_type) {
 194         case MAXIM_DEVICE_TYPE_MAX77836:
 195                 supported_regulators = max77836_supported_regulators;
 196                 supported_regulators_size = ARRAY_SIZE(max77836_supported_regulators);
 197                 break;
 198         case MAXIM_DEVICE_TYPE_MAX14577:
 199         default:
 200                 supported_regulators = max14577_supported_regulators;
 201                 supported_regulators_size = ARRAY_SIZE(max14577_supported_regulators);
 202         }
 203 
 204         config.dev = max14577->dev;
 205         config.driver_data = max14577;
 206 
 207         for (i = 0; i < supported_regulators_size; i++) {
 208                 struct regulator_dev *regulator;
 209                 /*
 210                  * Index of supported_regulators[] is also the id and must
 211                  * match index of pdata->regulators[].
 212                  */
 213                 if (pdata && pdata->regulators) {
 214                         config.init_data = pdata->regulators[i].initdata;
 215                         config.of_node = pdata->regulators[i].of_node;
 216                 }
 217                 config.regmap = max14577_get_regmap(max14577,
 218                                 supported_regulators[i].id);
 219 
 220                 regulator = devm_regulator_register(&pdev->dev,
 221                                 &supported_regulators[i], &config);
 222                 if (IS_ERR(regulator)) {
 223                         ret = PTR_ERR(regulator);
 224                         dev_err(&pdev->dev,
 225                                         "Regulator init failed for %d/%s with error: %d\n",
 226                                         i, supported_regulators[i].name, ret);
 227                         return ret;
 228                 }
 229         }
 230 
 231         return ret;
 232 }
 233 
 234 static const struct platform_device_id max14577_regulator_id[] = {
 235         { "max14577-regulator", MAXIM_DEVICE_TYPE_MAX14577, },
 236         { "max77836-regulator", MAXIM_DEVICE_TYPE_MAX77836, },
 237         { }
 238 };
 239 MODULE_DEVICE_TABLE(platform, max14577_regulator_id);
 240 
 241 static struct platform_driver max14577_regulator_driver = {
 242         .driver = {
 243                    .name = "max14577-regulator",
 244                    },
 245         .probe          = max14577_regulator_probe,
 246         .id_table       = max14577_regulator_id,
 247 };
 248 
 249 static int __init max14577_regulator_init(void)
 250 {
 251         BUILD_BUG_ON(ARRAY_SIZE(max14577_supported_regulators) != MAX14577_REGULATOR_NUM);
 252         BUILD_BUG_ON(ARRAY_SIZE(max77836_supported_regulators) != MAX77836_REGULATOR_NUM);
 253 
 254         BUILD_BUG_ON(MAX77836_REGULATOR_LDO_VOLTAGE_MIN +
 255                         (MAX77836_REGULATOR_LDO_VOLTAGE_STEP *
 256                           (MAX77836_REGULATOR_LDO_VOLTAGE_STEPS_NUM - 1)) !=
 257                         MAX77836_REGULATOR_LDO_VOLTAGE_MAX);
 258 
 259         return platform_driver_register(&max14577_regulator_driver);
 260 }
 261 subsys_initcall(max14577_regulator_init);
 262 
 263 static void __exit max14577_regulator_exit(void)
 264 {
 265         platform_driver_unregister(&max14577_regulator_driver);
 266 }
 267 module_exit(max14577_regulator_exit);
 268 
 269 MODULE_AUTHOR("Krzysztof Kozlowski <krzk@kernel.org>");
 270 MODULE_DESCRIPTION("Maxim 14577/77836 regulator driver");
 271 MODULE_LICENSE("GPL");

/* [<][>][^][v][top][bottom][index][help] */