15e9384c7SKrzysztof Kozlowski // SPDX-License-Identifier: GPL-2.0+
25e9384c7SKrzysztof Kozlowski //
35e9384c7SKrzysztof Kozlowski // max77693.c - Regulator driver for the Maxim 77693 and 77843
45e9384c7SKrzysztof Kozlowski //
55e9384c7SKrzysztof Kozlowski // Copyright (C) 2013-2015 Samsung Electronics
65e9384c7SKrzysztof Kozlowski // Jonghwa Lee <jonghwa3.lee@samsung.com>
75e9384c7SKrzysztof Kozlowski // Krzysztof Kozlowski <krzk@kernel.org>
85e9384c7SKrzysztof Kozlowski //
95e9384c7SKrzysztof Kozlowski // This driver is based on max77686.c
1086cf635aSJavier Martinez Canillas
1186cf635aSJavier Martinez Canillas #include <linux/err.h>
1286cf635aSJavier Martinez Canillas #include <linux/slab.h>
1386cf635aSJavier Martinez Canillas #include <linux/platform_device.h>
1486cf635aSJavier Martinez Canillas #include <linux/module.h>
1586cf635aSJavier Martinez Canillas #include <linux/export.h>
1686cf635aSJavier Martinez Canillas #include <linux/regulator/driver.h>
1786cf635aSJavier Martinez Canillas #include <linux/regulator/machine.h>
1886cf635aSJavier Martinez Canillas #include <linux/mfd/max77693.h>
1986cf635aSJavier Martinez Canillas #include <linux/mfd/max77693-common.h>
2086cf635aSJavier Martinez Canillas #include <linux/mfd/max77693-private.h>
2186cf635aSJavier Martinez Canillas #include <linux/mfd/max77843-private.h>
2286cf635aSJavier Martinez Canillas #include <linux/regulator/of_regulator.h>
2386cf635aSJavier Martinez Canillas #include <linux/regmap.h>
2486cf635aSJavier Martinez Canillas
2586cf635aSJavier Martinez Canillas /*
2686cf635aSJavier Martinez Canillas * ID for MAX77843 regulators.
2786cf635aSJavier Martinez Canillas * There is no need for such for MAX77693.
2886cf635aSJavier Martinez Canillas */
2986cf635aSJavier Martinez Canillas enum max77843_regulator_type {
3086cf635aSJavier Martinez Canillas MAX77843_SAFEOUT1 = 0,
3186cf635aSJavier Martinez Canillas MAX77843_SAFEOUT2,
3286cf635aSJavier Martinez Canillas MAX77843_CHARGER,
3386cf635aSJavier Martinez Canillas
3486cf635aSJavier Martinez Canillas MAX77843_NUM,
3586cf635aSJavier Martinez Canillas };
3686cf635aSJavier Martinez Canillas
3786cf635aSJavier Martinez Canillas /* Register differences between chargers: MAX77693 and MAX77843 */
3886cf635aSJavier Martinez Canillas struct chg_reg_data {
3986cf635aSJavier Martinez Canillas unsigned int linear_reg;
4086cf635aSJavier Martinez Canillas unsigned int linear_mask;
4186cf635aSJavier Martinez Canillas unsigned int uA_step;
4286cf635aSJavier Martinez Canillas unsigned int min_sel;
4386cf635aSJavier Martinez Canillas };
4486cf635aSJavier Martinez Canillas
4586cf635aSJavier Martinez Canillas /*
4686cf635aSJavier Martinez Canillas * MAX77693 CHARGER regulator - Min : 20mA, Max : 2580mA, step : 20mA
4786cf635aSJavier Martinez Canillas * 0x00, 0x01, 0x2, 0x03 = 60 mA
4886cf635aSJavier Martinez Canillas * 0x04 ~ 0x7E = (60 + (X - 3) * 20) mA
4986cf635aSJavier Martinez Canillas * Actually for MAX77693 the driver manipulates the maximum input current,
5086cf635aSJavier Martinez Canillas * not the fast charge current (output). This should be fixed.
5186cf635aSJavier Martinez Canillas *
5286cf635aSJavier Martinez Canillas * On MAX77843 the calculation formula is the same (except values).
5386cf635aSJavier Martinez Canillas * Fortunately it properly manipulates the fast charge current.
5486cf635aSJavier Martinez Canillas */
max77693_chg_get_current_limit(struct regulator_dev * rdev)5586cf635aSJavier Martinez Canillas static int max77693_chg_get_current_limit(struct regulator_dev *rdev)
5686cf635aSJavier Martinez Canillas {
5786cf635aSJavier Martinez Canillas const struct chg_reg_data *reg_data = rdev_get_drvdata(rdev);
5886cf635aSJavier Martinez Canillas unsigned int chg_min_uA = rdev->constraints->min_uA;
5986cf635aSJavier Martinez Canillas unsigned int chg_max_uA = rdev->constraints->max_uA;
6086cf635aSJavier Martinez Canillas unsigned int reg, sel;
6186cf635aSJavier Martinez Canillas unsigned int val;
6286cf635aSJavier Martinez Canillas int ret;
6386cf635aSJavier Martinez Canillas
6486cf635aSJavier Martinez Canillas ret = regmap_read(rdev->regmap, reg_data->linear_reg, ®);
6586cf635aSJavier Martinez Canillas if (ret < 0)
6686cf635aSJavier Martinez Canillas return ret;
6786cf635aSJavier Martinez Canillas
6886cf635aSJavier Martinez Canillas sel = reg & reg_data->linear_mask;
6986cf635aSJavier Martinez Canillas
7086cf635aSJavier Martinez Canillas /* the first four codes for charger current are all 60mA */
7186cf635aSJavier Martinez Canillas if (sel <= reg_data->min_sel)
7286cf635aSJavier Martinez Canillas sel = 0;
7386cf635aSJavier Martinez Canillas else
7486cf635aSJavier Martinez Canillas sel -= reg_data->min_sel;
7586cf635aSJavier Martinez Canillas
7686cf635aSJavier Martinez Canillas val = chg_min_uA + reg_data->uA_step * sel;
7786cf635aSJavier Martinez Canillas if (val > chg_max_uA)
7886cf635aSJavier Martinez Canillas return -EINVAL;
7986cf635aSJavier Martinez Canillas
8086cf635aSJavier Martinez Canillas return val;
8186cf635aSJavier Martinez Canillas }
8286cf635aSJavier Martinez Canillas
max77693_chg_set_current_limit(struct regulator_dev * rdev,int min_uA,int max_uA)8386cf635aSJavier Martinez Canillas static int max77693_chg_set_current_limit(struct regulator_dev *rdev,
8486cf635aSJavier Martinez Canillas int min_uA, int max_uA)
8586cf635aSJavier Martinez Canillas {
8686cf635aSJavier Martinez Canillas const struct chg_reg_data *reg_data = rdev_get_drvdata(rdev);
8786cf635aSJavier Martinez Canillas unsigned int chg_min_uA = rdev->constraints->min_uA;
8886cf635aSJavier Martinez Canillas int sel = 0;
8986cf635aSJavier Martinez Canillas
9086cf635aSJavier Martinez Canillas while (chg_min_uA + reg_data->uA_step * sel < min_uA)
9186cf635aSJavier Martinez Canillas sel++;
9286cf635aSJavier Martinez Canillas
9386cf635aSJavier Martinez Canillas if (chg_min_uA + reg_data->uA_step * sel > max_uA)
9486cf635aSJavier Martinez Canillas return -EINVAL;
9586cf635aSJavier Martinez Canillas
9686cf635aSJavier Martinez Canillas /* the first four codes for charger current are all 60mA */
9786cf635aSJavier Martinez Canillas sel += reg_data->min_sel;
9886cf635aSJavier Martinez Canillas
9986cf635aSJavier Martinez Canillas return regmap_write(rdev->regmap, reg_data->linear_reg, sel);
10086cf635aSJavier Martinez Canillas }
10186cf635aSJavier Martinez Canillas /* end of CHARGER regulator ops */
10286cf635aSJavier Martinez Canillas
10386cf635aSJavier Martinez Canillas /* Returns regmap suitable for given regulator on chosen device */
max77693_get_regmap(enum max77693_types type,struct max77693_dev * max77693,int reg_id)10486cf635aSJavier Martinez Canillas static struct regmap *max77693_get_regmap(enum max77693_types type,
10586cf635aSJavier Martinez Canillas struct max77693_dev *max77693,
10686cf635aSJavier Martinez Canillas int reg_id)
10786cf635aSJavier Martinez Canillas {
10886cf635aSJavier Martinez Canillas if (type == TYPE_MAX77693)
10986cf635aSJavier Martinez Canillas return max77693->regmap;
11086cf635aSJavier Martinez Canillas
11186cf635aSJavier Martinez Canillas /* Else: TYPE_MAX77843 */
11286cf635aSJavier Martinez Canillas switch (reg_id) {
11386cf635aSJavier Martinez Canillas case MAX77843_SAFEOUT1:
11486cf635aSJavier Martinez Canillas case MAX77843_SAFEOUT2:
11586cf635aSJavier Martinez Canillas return max77693->regmap;
11686cf635aSJavier Martinez Canillas case MAX77843_CHARGER:
11786cf635aSJavier Martinez Canillas return max77693->regmap_chg;
11886cf635aSJavier Martinez Canillas default:
11986cf635aSJavier Martinez Canillas return max77693->regmap;
12086cf635aSJavier Martinez Canillas }
12186cf635aSJavier Martinez Canillas }
12286cf635aSJavier Martinez Canillas
12386cf635aSJavier Martinez Canillas static const unsigned int max77693_safeout_table[] = {
12486cf635aSJavier Martinez Canillas 4850000,
12586cf635aSJavier Martinez Canillas 4900000,
12686cf635aSJavier Martinez Canillas 4950000,
12786cf635aSJavier Martinez Canillas 3300000,
12886cf635aSJavier Martinez Canillas };
12986cf635aSJavier Martinez Canillas
130b68f9078SBhumika Goyal static const struct regulator_ops max77693_safeout_ops = {
13186cf635aSJavier Martinez Canillas .list_voltage = regulator_list_voltage_table,
13286cf635aSJavier Martinez Canillas .is_enabled = regulator_is_enabled_regmap,
13386cf635aSJavier Martinez Canillas .enable = regulator_enable_regmap,
13486cf635aSJavier Martinez Canillas .disable = regulator_disable_regmap,
13586cf635aSJavier Martinez Canillas .get_voltage_sel = regulator_get_voltage_sel_regmap,
13686cf635aSJavier Martinez Canillas .set_voltage_sel = regulator_set_voltage_sel_regmap,
13786cf635aSJavier Martinez Canillas };
13886cf635aSJavier Martinez Canillas
139a08904fdSKrzysztof Kozlowski static const struct regulator_ops max77693_charger_ops = {
14086cf635aSJavier Martinez Canillas .is_enabled = regulator_is_enabled_regmap,
14186cf635aSJavier Martinez Canillas .enable = regulator_enable_regmap,
14286cf635aSJavier Martinez Canillas .disable = regulator_disable_regmap,
14386cf635aSJavier Martinez Canillas .get_current_limit = max77693_chg_get_current_limit,
14486cf635aSJavier Martinez Canillas .set_current_limit = max77693_chg_set_current_limit,
14586cf635aSJavier Martinez Canillas };
14686cf635aSJavier Martinez Canillas
14786cf635aSJavier Martinez Canillas #define max77693_regulator_desc_esafeout(_num) { \
14886cf635aSJavier Martinez Canillas .name = "ESAFEOUT"#_num, \
14986cf635aSJavier Martinez Canillas .id = MAX77693_ESAFEOUT##_num, \
15086cf635aSJavier Martinez Canillas .of_match = of_match_ptr("ESAFEOUT"#_num), \
15186cf635aSJavier Martinez Canillas .regulators_node = of_match_ptr("regulators"), \
15286cf635aSJavier Martinez Canillas .n_voltages = 4, \
15386cf635aSJavier Martinez Canillas .ops = &max77693_safeout_ops, \
15486cf635aSJavier Martinez Canillas .type = REGULATOR_VOLTAGE, \
15586cf635aSJavier Martinez Canillas .owner = THIS_MODULE, \
15686cf635aSJavier Martinez Canillas .volt_table = max77693_safeout_table, \
15786cf635aSJavier Martinez Canillas .vsel_reg = MAX77693_CHG_REG_SAFEOUT_CTRL, \
15886cf635aSJavier Martinez Canillas .vsel_mask = SAFEOUT_CTRL_SAFEOUT##_num##_MASK, \
15986cf635aSJavier Martinez Canillas .enable_reg = MAX77693_CHG_REG_SAFEOUT_CTRL, \
16086cf635aSJavier Martinez Canillas .enable_mask = SAFEOUT_CTRL_ENSAFEOUT##_num##_MASK , \
16186cf635aSJavier Martinez Canillas }
16286cf635aSJavier Martinez Canillas
16386cf635aSJavier Martinez Canillas static const struct regulator_desc max77693_supported_regulators[] = {
16486cf635aSJavier Martinez Canillas max77693_regulator_desc_esafeout(1),
16586cf635aSJavier Martinez Canillas max77693_regulator_desc_esafeout(2),
16686cf635aSJavier Martinez Canillas {
16786cf635aSJavier Martinez Canillas .name = "CHARGER",
16886cf635aSJavier Martinez Canillas .id = MAX77693_CHARGER,
16986cf635aSJavier Martinez Canillas .of_match = of_match_ptr("CHARGER"),
17086cf635aSJavier Martinez Canillas .regulators_node = of_match_ptr("regulators"),
17186cf635aSJavier Martinez Canillas .ops = &max77693_charger_ops,
17286cf635aSJavier Martinez Canillas .type = REGULATOR_CURRENT,
17386cf635aSJavier Martinez Canillas .owner = THIS_MODULE,
17486cf635aSJavier Martinez Canillas .enable_reg = MAX77693_CHG_REG_CHG_CNFG_00,
17586cf635aSJavier Martinez Canillas .enable_mask = CHG_CNFG_00_CHG_MASK |
17686cf635aSJavier Martinez Canillas CHG_CNFG_00_BUCK_MASK,
17786cf635aSJavier Martinez Canillas .enable_val = CHG_CNFG_00_CHG_MASK | CHG_CNFG_00_BUCK_MASK,
17886cf635aSJavier Martinez Canillas },
17986cf635aSJavier Martinez Canillas };
18086cf635aSJavier Martinez Canillas
18186cf635aSJavier Martinez Canillas static const struct chg_reg_data max77693_chg_reg_data = {
18286cf635aSJavier Martinez Canillas .linear_reg = MAX77693_CHG_REG_CHG_CNFG_09,
18386cf635aSJavier Martinez Canillas .linear_mask = CHG_CNFG_09_CHGIN_ILIM_MASK,
18486cf635aSJavier Martinez Canillas .uA_step = 20000,
18586cf635aSJavier Martinez Canillas .min_sel = 3,
18686cf635aSJavier Martinez Canillas };
18786cf635aSJavier Martinez Canillas
18886cf635aSJavier Martinez Canillas #define max77843_regulator_desc_esafeout(num) { \
18986cf635aSJavier Martinez Canillas .name = "SAFEOUT" # num, \
19086cf635aSJavier Martinez Canillas .id = MAX77843_SAFEOUT ## num, \
19186cf635aSJavier Martinez Canillas .ops = &max77693_safeout_ops, \
19286cf635aSJavier Martinez Canillas .of_match = of_match_ptr("SAFEOUT" # num), \
19386cf635aSJavier Martinez Canillas .regulators_node = of_match_ptr("regulators"), \
19486cf635aSJavier Martinez Canillas .type = REGULATOR_VOLTAGE, \
19586cf635aSJavier Martinez Canillas .owner = THIS_MODULE, \
19686cf635aSJavier Martinez Canillas .n_voltages = ARRAY_SIZE(max77693_safeout_table), \
19786cf635aSJavier Martinez Canillas .volt_table = max77693_safeout_table, \
19886cf635aSJavier Martinez Canillas .enable_reg = MAX77843_SYS_REG_SAFEOUTCTRL, \
19986cf635aSJavier Martinez Canillas .enable_mask = MAX77843_REG_SAFEOUTCTRL_ENSAFEOUT ## num, \
20086cf635aSJavier Martinez Canillas .vsel_reg = MAX77843_SYS_REG_SAFEOUTCTRL, \
20186cf635aSJavier Martinez Canillas .vsel_mask = MAX77843_REG_SAFEOUTCTRL_SAFEOUT ## num ## _MASK, \
20286cf635aSJavier Martinez Canillas }
20386cf635aSJavier Martinez Canillas
20486cf635aSJavier Martinez Canillas static const struct regulator_desc max77843_supported_regulators[] = {
20586cf635aSJavier Martinez Canillas [MAX77843_SAFEOUT1] = max77843_regulator_desc_esafeout(1),
20686cf635aSJavier Martinez Canillas [MAX77843_SAFEOUT2] = max77843_regulator_desc_esafeout(2),
20786cf635aSJavier Martinez Canillas [MAX77843_CHARGER] = {
20886cf635aSJavier Martinez Canillas .name = "CHARGER",
20986cf635aSJavier Martinez Canillas .id = MAX77843_CHARGER,
21086cf635aSJavier Martinez Canillas .ops = &max77693_charger_ops,
21186cf635aSJavier Martinez Canillas .of_match = of_match_ptr("CHARGER"),
21286cf635aSJavier Martinez Canillas .regulators_node = of_match_ptr("regulators"),
21386cf635aSJavier Martinez Canillas .type = REGULATOR_CURRENT,
21486cf635aSJavier Martinez Canillas .owner = THIS_MODULE,
21586cf635aSJavier Martinez Canillas .enable_reg = MAX77843_CHG_REG_CHG_CNFG_00,
21686cf635aSJavier Martinez Canillas .enable_mask = MAX77843_CHG_MASK,
21786cf635aSJavier Martinez Canillas .enable_val = MAX77843_CHG_MASK,
21886cf635aSJavier Martinez Canillas },
21986cf635aSJavier Martinez Canillas };
22086cf635aSJavier Martinez Canillas
22186cf635aSJavier Martinez Canillas static const struct chg_reg_data max77843_chg_reg_data = {
22286cf635aSJavier Martinez Canillas .linear_reg = MAX77843_CHG_REG_CHG_CNFG_02,
22386cf635aSJavier Martinez Canillas .linear_mask = MAX77843_CHG_FAST_CHG_CURRENT_MASK,
22486cf635aSJavier Martinez Canillas .uA_step = MAX77843_CHG_FAST_CHG_CURRENT_STEP,
22586cf635aSJavier Martinez Canillas .min_sel = 2,
22686cf635aSJavier Martinez Canillas };
22786cf635aSJavier Martinez Canillas
max77693_pmic_probe(struct platform_device * pdev)22886cf635aSJavier Martinez Canillas static int max77693_pmic_probe(struct platform_device *pdev)
22986cf635aSJavier Martinez Canillas {
23086cf635aSJavier Martinez Canillas enum max77693_types type = platform_get_device_id(pdev)->driver_data;
23186cf635aSJavier Martinez Canillas struct max77693_dev *iodev = dev_get_drvdata(pdev->dev.parent);
23286cf635aSJavier Martinez Canillas const struct regulator_desc *regulators;
23386cf635aSJavier Martinez Canillas unsigned int regulators_size;
23486cf635aSJavier Martinez Canillas int i;
23586cf635aSJavier Martinez Canillas struct regulator_config config = { };
23686cf635aSJavier Martinez Canillas
23786cf635aSJavier Martinez Canillas config.dev = iodev->dev;
23886cf635aSJavier Martinez Canillas
23986cf635aSJavier Martinez Canillas switch (type) {
24086cf635aSJavier Martinez Canillas case TYPE_MAX77693:
24186cf635aSJavier Martinez Canillas regulators = max77693_supported_regulators;
24286cf635aSJavier Martinez Canillas regulators_size = ARRAY_SIZE(max77693_supported_regulators);
24386cf635aSJavier Martinez Canillas config.driver_data = (void *)&max77693_chg_reg_data;
24486cf635aSJavier Martinez Canillas break;
24586cf635aSJavier Martinez Canillas case TYPE_MAX77843:
24686cf635aSJavier Martinez Canillas regulators = max77843_supported_regulators;
24786cf635aSJavier Martinez Canillas regulators_size = ARRAY_SIZE(max77843_supported_regulators);
24886cf635aSJavier Martinez Canillas config.driver_data = (void *)&max77843_chg_reg_data;
24986cf635aSJavier Martinez Canillas break;
25086cf635aSJavier Martinez Canillas default:
25186cf635aSJavier Martinez Canillas dev_err(&pdev->dev, "Unsupported device type: %u\n", type);
25286cf635aSJavier Martinez Canillas return -ENODEV;
25386cf635aSJavier Martinez Canillas }
25486cf635aSJavier Martinez Canillas
25586cf635aSJavier Martinez Canillas for (i = 0; i < regulators_size; i++) {
25686cf635aSJavier Martinez Canillas struct regulator_dev *rdev;
25786cf635aSJavier Martinez Canillas
25886cf635aSJavier Martinez Canillas config.regmap = max77693_get_regmap(type, iodev,
25986cf635aSJavier Martinez Canillas regulators[i].id);
26086cf635aSJavier Martinez Canillas
26186cf635aSJavier Martinez Canillas rdev = devm_regulator_register(&pdev->dev,
26286cf635aSJavier Martinez Canillas ®ulators[i], &config);
26386cf635aSJavier Martinez Canillas if (IS_ERR(rdev)) {
26486cf635aSJavier Martinez Canillas dev_err(&pdev->dev,
26586cf635aSJavier Martinez Canillas "Failed to initialize regulator-%d\n", i);
26686cf635aSJavier Martinez Canillas return PTR_ERR(rdev);
26786cf635aSJavier Martinez Canillas }
26886cf635aSJavier Martinez Canillas }
26986cf635aSJavier Martinez Canillas
27086cf635aSJavier Martinez Canillas return 0;
27186cf635aSJavier Martinez Canillas }
27286cf635aSJavier Martinez Canillas
27386cf635aSJavier Martinez Canillas static const struct platform_device_id max77693_pmic_id[] = {
27486cf635aSJavier Martinez Canillas { "max77693-pmic", TYPE_MAX77693 },
27586cf635aSJavier Martinez Canillas { "max77843-regulator", TYPE_MAX77843 },
27686cf635aSJavier Martinez Canillas {},
27786cf635aSJavier Martinez Canillas };
27886cf635aSJavier Martinez Canillas
27986cf635aSJavier Martinez Canillas MODULE_DEVICE_TABLE(platform, max77693_pmic_id);
28086cf635aSJavier Martinez Canillas
28186cf635aSJavier Martinez Canillas static struct platform_driver max77693_pmic_driver = {
28286cf635aSJavier Martinez Canillas .driver = {
28386cf635aSJavier Martinez Canillas .name = "max77693-pmic",
284*259b93b2SDouglas Anderson .probe_type = PROBE_PREFER_ASYNCHRONOUS,
28586cf635aSJavier Martinez Canillas },
28686cf635aSJavier Martinez Canillas .probe = max77693_pmic_probe,
28786cf635aSJavier Martinez Canillas .id_table = max77693_pmic_id,
28886cf635aSJavier Martinez Canillas };
28986cf635aSJavier Martinez Canillas
max77693_pmic_init(void)29086cf635aSJavier Martinez Canillas static int __init max77693_pmic_init(void)
29186cf635aSJavier Martinez Canillas {
29286cf635aSJavier Martinez Canillas return platform_driver_register(&max77693_pmic_driver);
29386cf635aSJavier Martinez Canillas }
29486cf635aSJavier Martinez Canillas subsys_initcall(max77693_pmic_init);
29586cf635aSJavier Martinez Canillas
max77693_pmic_cleanup(void)29686cf635aSJavier Martinez Canillas static void __exit max77693_pmic_cleanup(void)
29786cf635aSJavier Martinez Canillas {
29886cf635aSJavier Martinez Canillas platform_driver_unregister(&max77693_pmic_driver);
29986cf635aSJavier Martinez Canillas }
30086cf635aSJavier Martinez Canillas module_exit(max77693_pmic_cleanup);
30186cf635aSJavier Martinez Canillas
30286cf635aSJavier Martinez Canillas MODULE_DESCRIPTION("MAXIM 77693/77843 regulator driver");
30386cf635aSJavier Martinez Canillas MODULE_AUTHOR("Jonghwa Lee <jonghwa3.lee@samsung.com>");
304cea8aa3aSKrzysztof Kozlowski MODULE_AUTHOR("Krzysztof Kozlowski <krzk@kernel.org>");
30586cf635aSJavier Martinez Canillas MODULE_LICENSE("GPL");
306