Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 //
0003 // max77693.c - Regulator driver for the Maxim 77693 and 77843
0004 //
0005 // Copyright (C) 2013-2015 Samsung Electronics
0006 // Jonghwa Lee <jonghwa3.lee@samsung.com>
0007 // Krzysztof Kozlowski <krzk@kernel.org>
0008 //
0009 // This driver is based on max77686.c
0010 
0011 #include <linux/err.h>
0012 #include <linux/slab.h>
0013 #include <linux/platform_device.h>
0014 #include <linux/module.h>
0015 #include <linux/export.h>
0016 #include <linux/regulator/driver.h>
0017 #include <linux/regulator/machine.h>
0018 #include <linux/mfd/max77693.h>
0019 #include <linux/mfd/max77693-common.h>
0020 #include <linux/mfd/max77693-private.h>
0021 #include <linux/mfd/max77843-private.h>
0022 #include <linux/regulator/of_regulator.h>
0023 #include <linux/regmap.h>
0024 
0025 /*
0026  * ID for MAX77843 regulators.
0027  * There is no need for such for MAX77693.
0028  */
0029 enum max77843_regulator_type {
0030     MAX77843_SAFEOUT1 = 0,
0031     MAX77843_SAFEOUT2,
0032     MAX77843_CHARGER,
0033 
0034     MAX77843_NUM,
0035 };
0036 
0037 /* Register differences between chargers: MAX77693 and MAX77843 */
0038 struct chg_reg_data {
0039     unsigned int linear_reg;
0040     unsigned int linear_mask;
0041     unsigned int uA_step;
0042     unsigned int min_sel;
0043 };
0044 
0045 /*
0046  * MAX77693 CHARGER regulator - Min : 20mA, Max : 2580mA, step : 20mA
0047  * 0x00, 0x01, 0x2, 0x03    = 60 mA
0048  * 0x04 ~ 0x7E          = (60 + (X - 3) * 20) mA
0049  * Actually for MAX77693 the driver manipulates the maximum input current,
0050  * not the fast charge current (output). This should be fixed.
0051  *
0052  * On MAX77843 the calculation formula is the same (except values).
0053  * Fortunately it properly manipulates the fast charge current.
0054  */
0055 static int max77693_chg_get_current_limit(struct regulator_dev *rdev)
0056 {
0057     const struct chg_reg_data *reg_data = rdev_get_drvdata(rdev);
0058     unsigned int chg_min_uA = rdev->constraints->min_uA;
0059     unsigned int chg_max_uA = rdev->constraints->max_uA;
0060     unsigned int reg, sel;
0061     unsigned int val;
0062     int ret;
0063 
0064     ret = regmap_read(rdev->regmap, reg_data->linear_reg, &reg);
0065     if (ret < 0)
0066         return ret;
0067 
0068     sel = reg & reg_data->linear_mask;
0069 
0070     /* the first four codes for charger current are all 60mA */
0071     if (sel <= reg_data->min_sel)
0072         sel = 0;
0073     else
0074         sel -= reg_data->min_sel;
0075 
0076     val = chg_min_uA + reg_data->uA_step * sel;
0077     if (val > chg_max_uA)
0078         return -EINVAL;
0079 
0080     return val;
0081 }
0082 
0083 static int max77693_chg_set_current_limit(struct regulator_dev *rdev,
0084                         int min_uA, int max_uA)
0085 {
0086     const struct chg_reg_data *reg_data = rdev_get_drvdata(rdev);
0087     unsigned int chg_min_uA = rdev->constraints->min_uA;
0088     int sel = 0;
0089 
0090     while (chg_min_uA + reg_data->uA_step * sel < min_uA)
0091         sel++;
0092 
0093     if (chg_min_uA + reg_data->uA_step * sel > max_uA)
0094         return -EINVAL;
0095 
0096     /* the first four codes for charger current are all 60mA */
0097     sel += reg_data->min_sel;
0098 
0099     return regmap_write(rdev->regmap, reg_data->linear_reg, sel);
0100 }
0101 /* end of CHARGER regulator ops */
0102 
0103 /* Returns regmap suitable for given regulator on chosen device */
0104 static struct regmap *max77693_get_regmap(enum max77693_types type,
0105                       struct max77693_dev *max77693,
0106                       int reg_id)
0107 {
0108     if (type == TYPE_MAX77693)
0109         return max77693->regmap;
0110 
0111     /* Else: TYPE_MAX77843 */
0112     switch (reg_id) {
0113     case MAX77843_SAFEOUT1:
0114     case MAX77843_SAFEOUT2:
0115         return max77693->regmap;
0116     case MAX77843_CHARGER:
0117         return max77693->regmap_chg;
0118     default:
0119         return max77693->regmap;
0120     }
0121 }
0122 
0123 static const unsigned int max77693_safeout_table[] = {
0124     4850000,
0125     4900000,
0126     4950000,
0127     3300000,
0128 };
0129 
0130 static const struct regulator_ops max77693_safeout_ops = {
0131     .list_voltage       = regulator_list_voltage_table,
0132     .is_enabled     = regulator_is_enabled_regmap,
0133     .enable         = regulator_enable_regmap,
0134     .disable        = regulator_disable_regmap,
0135     .get_voltage_sel    = regulator_get_voltage_sel_regmap,
0136     .set_voltage_sel    = regulator_set_voltage_sel_regmap,
0137 };
0138 
0139 static const struct regulator_ops max77693_charger_ops = {
0140     .is_enabled     = regulator_is_enabled_regmap,
0141     .enable         = regulator_enable_regmap,
0142     .disable        = regulator_disable_regmap,
0143     .get_current_limit  = max77693_chg_get_current_limit,
0144     .set_current_limit  = max77693_chg_set_current_limit,
0145 };
0146 
0147 #define max77693_regulator_desc_esafeout(_num)  {       \
0148     .name       = "ESAFEOUT"#_num,          \
0149     .id     = MAX77693_ESAFEOUT##_num,      \
0150     .of_match   = of_match_ptr("ESAFEOUT"#_num),    \
0151     .regulators_node    = of_match_ptr("regulators"),   \
0152     .n_voltages = 4,                    \
0153     .ops        = &max77693_safeout_ops,        \
0154     .type       = REGULATOR_VOLTAGE,            \
0155     .owner      = THIS_MODULE,              \
0156     .volt_table = max77693_safeout_table,       \
0157     .vsel_reg   = MAX77693_CHG_REG_SAFEOUT_CTRL,    \
0158     .vsel_mask  = SAFEOUT_CTRL_SAFEOUT##_num##_MASK,    \
0159     .enable_reg = MAX77693_CHG_REG_SAFEOUT_CTRL,    \
0160     .enable_mask    = SAFEOUT_CTRL_ENSAFEOUT##_num##_MASK , \
0161 }
0162 
0163 static const struct regulator_desc max77693_supported_regulators[] = {
0164     max77693_regulator_desc_esafeout(1),
0165     max77693_regulator_desc_esafeout(2),
0166     {
0167         .name = "CHARGER",
0168         .id = MAX77693_CHARGER,
0169         .of_match = of_match_ptr("CHARGER"),
0170         .regulators_node = of_match_ptr("regulators"),
0171         .ops = &max77693_charger_ops,
0172         .type = REGULATOR_CURRENT,
0173         .owner = THIS_MODULE,
0174         .enable_reg = MAX77693_CHG_REG_CHG_CNFG_00,
0175         .enable_mask = CHG_CNFG_00_CHG_MASK |
0176                 CHG_CNFG_00_BUCK_MASK,
0177         .enable_val = CHG_CNFG_00_CHG_MASK | CHG_CNFG_00_BUCK_MASK,
0178     },
0179 };
0180 
0181 static const struct chg_reg_data max77693_chg_reg_data = {
0182     .linear_reg = MAX77693_CHG_REG_CHG_CNFG_09,
0183     .linear_mask    = CHG_CNFG_09_CHGIN_ILIM_MASK,
0184     .uA_step    = 20000,
0185     .min_sel    = 3,
0186 };
0187 
0188 #define max77843_regulator_desc_esafeout(num)   {           \
0189     .name       = "SAFEOUT" # num,              \
0190     .id     = MAX77843_SAFEOUT ## num,          \
0191     .ops        = &max77693_safeout_ops,            \
0192     .of_match   = of_match_ptr("SAFEOUT" # num),        \
0193     .regulators_node = of_match_ptr("regulators"),          \
0194     .type       = REGULATOR_VOLTAGE,                \
0195     .owner      = THIS_MODULE,                  \
0196     .n_voltages = ARRAY_SIZE(max77693_safeout_table),       \
0197     .volt_table = max77693_safeout_table,           \
0198     .enable_reg = MAX77843_SYS_REG_SAFEOUTCTRL,         \
0199     .enable_mask    = MAX77843_REG_SAFEOUTCTRL_ENSAFEOUT ## num,    \
0200     .vsel_reg   = MAX77843_SYS_REG_SAFEOUTCTRL,         \
0201     .vsel_mask  = MAX77843_REG_SAFEOUTCTRL_SAFEOUT ## num ## _MASK, \
0202 }
0203 
0204 static const struct regulator_desc max77843_supported_regulators[] = {
0205     [MAX77843_SAFEOUT1] = max77843_regulator_desc_esafeout(1),
0206     [MAX77843_SAFEOUT2] = max77843_regulator_desc_esafeout(2),
0207     [MAX77843_CHARGER] = {
0208         .name       = "CHARGER",
0209         .id     = MAX77843_CHARGER,
0210         .ops        = &max77693_charger_ops,
0211         .of_match   = of_match_ptr("CHARGER"),
0212         .regulators_node = of_match_ptr("regulators"),
0213         .type       = REGULATOR_CURRENT,
0214         .owner      = THIS_MODULE,
0215         .enable_reg = MAX77843_CHG_REG_CHG_CNFG_00,
0216         .enable_mask    = MAX77843_CHG_MASK,
0217         .enable_val = MAX77843_CHG_MASK,
0218     },
0219 };
0220 
0221 static const struct chg_reg_data max77843_chg_reg_data = {
0222     .linear_reg = MAX77843_CHG_REG_CHG_CNFG_02,
0223     .linear_mask    = MAX77843_CHG_FAST_CHG_CURRENT_MASK,
0224     .uA_step    = MAX77843_CHG_FAST_CHG_CURRENT_STEP,
0225     .min_sel    = 2,
0226 };
0227 
0228 static int max77693_pmic_probe(struct platform_device *pdev)
0229 {
0230     enum max77693_types type = platform_get_device_id(pdev)->driver_data;
0231     struct max77693_dev *iodev = dev_get_drvdata(pdev->dev.parent);
0232     const struct regulator_desc *regulators;
0233     unsigned int regulators_size;
0234     int i;
0235     struct regulator_config config = { };
0236 
0237     config.dev = iodev->dev;
0238 
0239     switch (type) {
0240     case TYPE_MAX77693:
0241         regulators = max77693_supported_regulators;
0242         regulators_size = ARRAY_SIZE(max77693_supported_regulators);
0243         config.driver_data = (void *)&max77693_chg_reg_data;
0244         break;
0245     case TYPE_MAX77843:
0246         regulators = max77843_supported_regulators;
0247         regulators_size = ARRAY_SIZE(max77843_supported_regulators);
0248         config.driver_data = (void *)&max77843_chg_reg_data;
0249         break;
0250     default:
0251         dev_err(&pdev->dev, "Unsupported device type: %u\n", type);
0252         return -ENODEV;
0253     }
0254 
0255     for (i = 0; i < regulators_size; i++) {
0256         struct regulator_dev *rdev;
0257 
0258         config.regmap = max77693_get_regmap(type, iodev,
0259                             regulators[i].id);
0260 
0261         rdev = devm_regulator_register(&pdev->dev,
0262                         &regulators[i], &config);
0263         if (IS_ERR(rdev)) {
0264             dev_err(&pdev->dev,
0265                 "Failed to initialize regulator-%d\n", i);
0266             return PTR_ERR(rdev);
0267         }
0268     }
0269 
0270     return 0;
0271 }
0272 
0273 static const struct platform_device_id max77693_pmic_id[] = {
0274     { "max77693-pmic", TYPE_MAX77693 },
0275     { "max77843-regulator", TYPE_MAX77843 },
0276     {},
0277 };
0278 
0279 MODULE_DEVICE_TABLE(platform, max77693_pmic_id);
0280 
0281 static struct platform_driver max77693_pmic_driver = {
0282     .driver = {
0283            .name = "max77693-pmic",
0284            },
0285     .probe = max77693_pmic_probe,
0286     .id_table = max77693_pmic_id,
0287 };
0288 
0289 static int __init max77693_pmic_init(void)
0290 {
0291     return platform_driver_register(&max77693_pmic_driver);
0292 }
0293 subsys_initcall(max77693_pmic_init);
0294 
0295 static void __exit max77693_pmic_cleanup(void)
0296 {
0297     platform_driver_unregister(&max77693_pmic_driver);
0298 }
0299 module_exit(max77693_pmic_cleanup);
0300 
0301 MODULE_DESCRIPTION("MAXIM 77693/77843 regulator driver");
0302 MODULE_AUTHOR("Jonghwa Lee <jonghwa3.lee@samsung.com>");
0303 MODULE_AUTHOR("Krzysztof Kozlowski <krzk@kernel.org>");
0304 MODULE_LICENSE("GPL");