162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
262306a36Sopenharmony_ci//
362306a36Sopenharmony_ci// max77693.c - Regulator driver for the Maxim 77693 and 77843
462306a36Sopenharmony_ci//
562306a36Sopenharmony_ci// Copyright (C) 2013-2015 Samsung Electronics
662306a36Sopenharmony_ci// Jonghwa Lee <jonghwa3.lee@samsung.com>
762306a36Sopenharmony_ci// Krzysztof Kozlowski <krzk@kernel.org>
862306a36Sopenharmony_ci//
962306a36Sopenharmony_ci// This driver is based on max77686.c
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci#include <linux/err.h>
1262306a36Sopenharmony_ci#include <linux/slab.h>
1362306a36Sopenharmony_ci#include <linux/platform_device.h>
1462306a36Sopenharmony_ci#include <linux/module.h>
1562306a36Sopenharmony_ci#include <linux/export.h>
1662306a36Sopenharmony_ci#include <linux/regulator/driver.h>
1762306a36Sopenharmony_ci#include <linux/regulator/machine.h>
1862306a36Sopenharmony_ci#include <linux/mfd/max77693.h>
1962306a36Sopenharmony_ci#include <linux/mfd/max77693-common.h>
2062306a36Sopenharmony_ci#include <linux/mfd/max77693-private.h>
2162306a36Sopenharmony_ci#include <linux/mfd/max77843-private.h>
2262306a36Sopenharmony_ci#include <linux/regulator/of_regulator.h>
2362306a36Sopenharmony_ci#include <linux/regmap.h>
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci/*
2662306a36Sopenharmony_ci * ID for MAX77843 regulators.
2762306a36Sopenharmony_ci * There is no need for such for MAX77693.
2862306a36Sopenharmony_ci */
2962306a36Sopenharmony_cienum max77843_regulator_type {
3062306a36Sopenharmony_ci	MAX77843_SAFEOUT1 = 0,
3162306a36Sopenharmony_ci	MAX77843_SAFEOUT2,
3262306a36Sopenharmony_ci	MAX77843_CHARGER,
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ci	MAX77843_NUM,
3562306a36Sopenharmony_ci};
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_ci/* Register differences between chargers: MAX77693 and MAX77843 */
3862306a36Sopenharmony_cistruct chg_reg_data {
3962306a36Sopenharmony_ci	unsigned int linear_reg;
4062306a36Sopenharmony_ci	unsigned int linear_mask;
4162306a36Sopenharmony_ci	unsigned int uA_step;
4262306a36Sopenharmony_ci	unsigned int min_sel;
4362306a36Sopenharmony_ci};
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_ci/*
4662306a36Sopenharmony_ci * MAX77693 CHARGER regulator - Min : 20mA, Max : 2580mA, step : 20mA
4762306a36Sopenharmony_ci * 0x00, 0x01, 0x2, 0x03	= 60 mA
4862306a36Sopenharmony_ci * 0x04 ~ 0x7E			= (60 + (X - 3) * 20) mA
4962306a36Sopenharmony_ci * Actually for MAX77693 the driver manipulates the maximum input current,
5062306a36Sopenharmony_ci * not the fast charge current (output). This should be fixed.
5162306a36Sopenharmony_ci *
5262306a36Sopenharmony_ci * On MAX77843 the calculation formula is the same (except values).
5362306a36Sopenharmony_ci * Fortunately it properly manipulates the fast charge current.
5462306a36Sopenharmony_ci */
5562306a36Sopenharmony_cistatic int max77693_chg_get_current_limit(struct regulator_dev *rdev)
5662306a36Sopenharmony_ci{
5762306a36Sopenharmony_ci	const struct chg_reg_data *reg_data = rdev_get_drvdata(rdev);
5862306a36Sopenharmony_ci	unsigned int chg_min_uA = rdev->constraints->min_uA;
5962306a36Sopenharmony_ci	unsigned int chg_max_uA = rdev->constraints->max_uA;
6062306a36Sopenharmony_ci	unsigned int reg, sel;
6162306a36Sopenharmony_ci	unsigned int val;
6262306a36Sopenharmony_ci	int ret;
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_ci	ret = regmap_read(rdev->regmap, reg_data->linear_reg, &reg);
6562306a36Sopenharmony_ci	if (ret < 0)
6662306a36Sopenharmony_ci		return ret;
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ci	sel = reg & reg_data->linear_mask;
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ci	/* the first four codes for charger current are all 60mA */
7162306a36Sopenharmony_ci	if (sel <= reg_data->min_sel)
7262306a36Sopenharmony_ci		sel = 0;
7362306a36Sopenharmony_ci	else
7462306a36Sopenharmony_ci		sel -= reg_data->min_sel;
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_ci	val = chg_min_uA + reg_data->uA_step * sel;
7762306a36Sopenharmony_ci	if (val > chg_max_uA)
7862306a36Sopenharmony_ci		return -EINVAL;
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_ci	return val;
8162306a36Sopenharmony_ci}
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_cistatic int max77693_chg_set_current_limit(struct regulator_dev *rdev,
8462306a36Sopenharmony_ci						int min_uA, int max_uA)
8562306a36Sopenharmony_ci{
8662306a36Sopenharmony_ci	const struct chg_reg_data *reg_data = rdev_get_drvdata(rdev);
8762306a36Sopenharmony_ci	unsigned int chg_min_uA = rdev->constraints->min_uA;
8862306a36Sopenharmony_ci	int sel = 0;
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_ci	while (chg_min_uA + reg_data->uA_step * sel < min_uA)
9162306a36Sopenharmony_ci		sel++;
9262306a36Sopenharmony_ci
9362306a36Sopenharmony_ci	if (chg_min_uA + reg_data->uA_step * sel > max_uA)
9462306a36Sopenharmony_ci		return -EINVAL;
9562306a36Sopenharmony_ci
9662306a36Sopenharmony_ci	/* the first four codes for charger current are all 60mA */
9762306a36Sopenharmony_ci	sel += reg_data->min_sel;
9862306a36Sopenharmony_ci
9962306a36Sopenharmony_ci	return regmap_write(rdev->regmap, reg_data->linear_reg, sel);
10062306a36Sopenharmony_ci}
10162306a36Sopenharmony_ci/* end of CHARGER regulator ops */
10262306a36Sopenharmony_ci
10362306a36Sopenharmony_ci/* Returns regmap suitable for given regulator on chosen device */
10462306a36Sopenharmony_cistatic struct regmap *max77693_get_regmap(enum max77693_types type,
10562306a36Sopenharmony_ci					  struct max77693_dev *max77693,
10662306a36Sopenharmony_ci					  int reg_id)
10762306a36Sopenharmony_ci{
10862306a36Sopenharmony_ci	if (type == TYPE_MAX77693)
10962306a36Sopenharmony_ci		return max77693->regmap;
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_ci	/* Else: TYPE_MAX77843 */
11262306a36Sopenharmony_ci	switch (reg_id) {
11362306a36Sopenharmony_ci	case MAX77843_SAFEOUT1:
11462306a36Sopenharmony_ci	case MAX77843_SAFEOUT2:
11562306a36Sopenharmony_ci		return max77693->regmap;
11662306a36Sopenharmony_ci	case MAX77843_CHARGER:
11762306a36Sopenharmony_ci		return max77693->regmap_chg;
11862306a36Sopenharmony_ci	default:
11962306a36Sopenharmony_ci		return max77693->regmap;
12062306a36Sopenharmony_ci	}
12162306a36Sopenharmony_ci}
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_cistatic const unsigned int max77693_safeout_table[] = {
12462306a36Sopenharmony_ci	4850000,
12562306a36Sopenharmony_ci	4900000,
12662306a36Sopenharmony_ci	4950000,
12762306a36Sopenharmony_ci	3300000,
12862306a36Sopenharmony_ci};
12962306a36Sopenharmony_ci
13062306a36Sopenharmony_cistatic const struct regulator_ops max77693_safeout_ops = {
13162306a36Sopenharmony_ci	.list_voltage		= regulator_list_voltage_table,
13262306a36Sopenharmony_ci	.is_enabled		= regulator_is_enabled_regmap,
13362306a36Sopenharmony_ci	.enable			= regulator_enable_regmap,
13462306a36Sopenharmony_ci	.disable		= regulator_disable_regmap,
13562306a36Sopenharmony_ci	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
13662306a36Sopenharmony_ci	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
13762306a36Sopenharmony_ci};
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_cistatic const struct regulator_ops max77693_charger_ops = {
14062306a36Sopenharmony_ci	.is_enabled		= regulator_is_enabled_regmap,
14162306a36Sopenharmony_ci	.enable			= regulator_enable_regmap,
14262306a36Sopenharmony_ci	.disable		= regulator_disable_regmap,
14362306a36Sopenharmony_ci	.get_current_limit	= max77693_chg_get_current_limit,
14462306a36Sopenharmony_ci	.set_current_limit	= max77693_chg_set_current_limit,
14562306a36Sopenharmony_ci};
14662306a36Sopenharmony_ci
14762306a36Sopenharmony_ci#define max77693_regulator_desc_esafeout(_num)	{		\
14862306a36Sopenharmony_ci	.name		= "ESAFEOUT"#_num,			\
14962306a36Sopenharmony_ci	.id		= MAX77693_ESAFEOUT##_num,		\
15062306a36Sopenharmony_ci	.of_match	= of_match_ptr("ESAFEOUT"#_num),	\
15162306a36Sopenharmony_ci	.regulators_node	= of_match_ptr("regulators"),	\
15262306a36Sopenharmony_ci	.n_voltages	= 4,					\
15362306a36Sopenharmony_ci	.ops		= &max77693_safeout_ops,		\
15462306a36Sopenharmony_ci	.type		= REGULATOR_VOLTAGE,			\
15562306a36Sopenharmony_ci	.owner		= THIS_MODULE,				\
15662306a36Sopenharmony_ci	.volt_table	= max77693_safeout_table,		\
15762306a36Sopenharmony_ci	.vsel_reg	= MAX77693_CHG_REG_SAFEOUT_CTRL,	\
15862306a36Sopenharmony_ci	.vsel_mask	= SAFEOUT_CTRL_SAFEOUT##_num##_MASK,	\
15962306a36Sopenharmony_ci	.enable_reg	= MAX77693_CHG_REG_SAFEOUT_CTRL,	\
16062306a36Sopenharmony_ci	.enable_mask	= SAFEOUT_CTRL_ENSAFEOUT##_num##_MASK ,	\
16162306a36Sopenharmony_ci}
16262306a36Sopenharmony_ci
16362306a36Sopenharmony_cistatic const struct regulator_desc max77693_supported_regulators[] = {
16462306a36Sopenharmony_ci	max77693_regulator_desc_esafeout(1),
16562306a36Sopenharmony_ci	max77693_regulator_desc_esafeout(2),
16662306a36Sopenharmony_ci	{
16762306a36Sopenharmony_ci		.name = "CHARGER",
16862306a36Sopenharmony_ci		.id = MAX77693_CHARGER,
16962306a36Sopenharmony_ci		.of_match = of_match_ptr("CHARGER"),
17062306a36Sopenharmony_ci		.regulators_node = of_match_ptr("regulators"),
17162306a36Sopenharmony_ci		.ops = &max77693_charger_ops,
17262306a36Sopenharmony_ci		.type = REGULATOR_CURRENT,
17362306a36Sopenharmony_ci		.owner = THIS_MODULE,
17462306a36Sopenharmony_ci		.enable_reg = MAX77693_CHG_REG_CHG_CNFG_00,
17562306a36Sopenharmony_ci		.enable_mask = CHG_CNFG_00_CHG_MASK |
17662306a36Sopenharmony_ci				CHG_CNFG_00_BUCK_MASK,
17762306a36Sopenharmony_ci		.enable_val = CHG_CNFG_00_CHG_MASK | CHG_CNFG_00_BUCK_MASK,
17862306a36Sopenharmony_ci	},
17962306a36Sopenharmony_ci};
18062306a36Sopenharmony_ci
18162306a36Sopenharmony_cistatic const struct chg_reg_data max77693_chg_reg_data = {
18262306a36Sopenharmony_ci	.linear_reg	= MAX77693_CHG_REG_CHG_CNFG_09,
18362306a36Sopenharmony_ci	.linear_mask	= CHG_CNFG_09_CHGIN_ILIM_MASK,
18462306a36Sopenharmony_ci	.uA_step	= 20000,
18562306a36Sopenharmony_ci	.min_sel	= 3,
18662306a36Sopenharmony_ci};
18762306a36Sopenharmony_ci
18862306a36Sopenharmony_ci#define	max77843_regulator_desc_esafeout(num)	{			\
18962306a36Sopenharmony_ci	.name		= "SAFEOUT" # num,				\
19062306a36Sopenharmony_ci	.id		= MAX77843_SAFEOUT ## num,			\
19162306a36Sopenharmony_ci	.ops		= &max77693_safeout_ops,			\
19262306a36Sopenharmony_ci	.of_match	= of_match_ptr("SAFEOUT" # num),		\
19362306a36Sopenharmony_ci	.regulators_node = of_match_ptr("regulators"),			\
19462306a36Sopenharmony_ci	.type		= REGULATOR_VOLTAGE,				\
19562306a36Sopenharmony_ci	.owner		= THIS_MODULE,					\
19662306a36Sopenharmony_ci	.n_voltages	= ARRAY_SIZE(max77693_safeout_table),		\
19762306a36Sopenharmony_ci	.volt_table	= max77693_safeout_table,			\
19862306a36Sopenharmony_ci	.enable_reg	= MAX77843_SYS_REG_SAFEOUTCTRL,			\
19962306a36Sopenharmony_ci	.enable_mask	= MAX77843_REG_SAFEOUTCTRL_ENSAFEOUT ## num,	\
20062306a36Sopenharmony_ci	.vsel_reg	= MAX77843_SYS_REG_SAFEOUTCTRL,			\
20162306a36Sopenharmony_ci	.vsel_mask	= MAX77843_REG_SAFEOUTCTRL_SAFEOUT ## num ## _MASK, \
20262306a36Sopenharmony_ci}
20362306a36Sopenharmony_ci
20462306a36Sopenharmony_cistatic const struct regulator_desc max77843_supported_regulators[] = {
20562306a36Sopenharmony_ci	[MAX77843_SAFEOUT1] = max77843_regulator_desc_esafeout(1),
20662306a36Sopenharmony_ci	[MAX77843_SAFEOUT2] = max77843_regulator_desc_esafeout(2),
20762306a36Sopenharmony_ci	[MAX77843_CHARGER] = {
20862306a36Sopenharmony_ci		.name		= "CHARGER",
20962306a36Sopenharmony_ci		.id		= MAX77843_CHARGER,
21062306a36Sopenharmony_ci		.ops		= &max77693_charger_ops,
21162306a36Sopenharmony_ci		.of_match	= of_match_ptr("CHARGER"),
21262306a36Sopenharmony_ci		.regulators_node = of_match_ptr("regulators"),
21362306a36Sopenharmony_ci		.type		= REGULATOR_CURRENT,
21462306a36Sopenharmony_ci		.owner		= THIS_MODULE,
21562306a36Sopenharmony_ci		.enable_reg	= MAX77843_CHG_REG_CHG_CNFG_00,
21662306a36Sopenharmony_ci		.enable_mask	= MAX77843_CHG_MASK,
21762306a36Sopenharmony_ci		.enable_val	= MAX77843_CHG_MASK,
21862306a36Sopenharmony_ci	},
21962306a36Sopenharmony_ci};
22062306a36Sopenharmony_ci
22162306a36Sopenharmony_cistatic const struct chg_reg_data max77843_chg_reg_data = {
22262306a36Sopenharmony_ci	.linear_reg	= MAX77843_CHG_REG_CHG_CNFG_02,
22362306a36Sopenharmony_ci	.linear_mask	= MAX77843_CHG_FAST_CHG_CURRENT_MASK,
22462306a36Sopenharmony_ci	.uA_step	= MAX77843_CHG_FAST_CHG_CURRENT_STEP,
22562306a36Sopenharmony_ci	.min_sel	= 2,
22662306a36Sopenharmony_ci};
22762306a36Sopenharmony_ci
22862306a36Sopenharmony_cistatic int max77693_pmic_probe(struct platform_device *pdev)
22962306a36Sopenharmony_ci{
23062306a36Sopenharmony_ci	enum max77693_types type = platform_get_device_id(pdev)->driver_data;
23162306a36Sopenharmony_ci	struct max77693_dev *iodev = dev_get_drvdata(pdev->dev.parent);
23262306a36Sopenharmony_ci	const struct regulator_desc *regulators;
23362306a36Sopenharmony_ci	unsigned int regulators_size;
23462306a36Sopenharmony_ci	int i;
23562306a36Sopenharmony_ci	struct regulator_config config = { };
23662306a36Sopenharmony_ci
23762306a36Sopenharmony_ci	config.dev = iodev->dev;
23862306a36Sopenharmony_ci
23962306a36Sopenharmony_ci	switch (type) {
24062306a36Sopenharmony_ci	case TYPE_MAX77693:
24162306a36Sopenharmony_ci		regulators = max77693_supported_regulators;
24262306a36Sopenharmony_ci		regulators_size = ARRAY_SIZE(max77693_supported_regulators);
24362306a36Sopenharmony_ci		config.driver_data = (void *)&max77693_chg_reg_data;
24462306a36Sopenharmony_ci		break;
24562306a36Sopenharmony_ci	case TYPE_MAX77843:
24662306a36Sopenharmony_ci		regulators = max77843_supported_regulators;
24762306a36Sopenharmony_ci		regulators_size = ARRAY_SIZE(max77843_supported_regulators);
24862306a36Sopenharmony_ci		config.driver_data = (void *)&max77843_chg_reg_data;
24962306a36Sopenharmony_ci		break;
25062306a36Sopenharmony_ci	default:
25162306a36Sopenharmony_ci		dev_err(&pdev->dev, "Unsupported device type: %u\n", type);
25262306a36Sopenharmony_ci		return -ENODEV;
25362306a36Sopenharmony_ci	}
25462306a36Sopenharmony_ci
25562306a36Sopenharmony_ci	for (i = 0; i < regulators_size; i++) {
25662306a36Sopenharmony_ci		struct regulator_dev *rdev;
25762306a36Sopenharmony_ci
25862306a36Sopenharmony_ci		config.regmap = max77693_get_regmap(type, iodev,
25962306a36Sopenharmony_ci						    regulators[i].id);
26062306a36Sopenharmony_ci
26162306a36Sopenharmony_ci		rdev = devm_regulator_register(&pdev->dev,
26262306a36Sopenharmony_ci						&regulators[i], &config);
26362306a36Sopenharmony_ci		if (IS_ERR(rdev)) {
26462306a36Sopenharmony_ci			dev_err(&pdev->dev,
26562306a36Sopenharmony_ci				"Failed to initialize regulator-%d\n", i);
26662306a36Sopenharmony_ci			return PTR_ERR(rdev);
26762306a36Sopenharmony_ci		}
26862306a36Sopenharmony_ci	}
26962306a36Sopenharmony_ci
27062306a36Sopenharmony_ci	return 0;
27162306a36Sopenharmony_ci}
27262306a36Sopenharmony_ci
27362306a36Sopenharmony_cistatic const struct platform_device_id max77693_pmic_id[] = {
27462306a36Sopenharmony_ci	{ "max77693-pmic", TYPE_MAX77693 },
27562306a36Sopenharmony_ci	{ "max77843-regulator", TYPE_MAX77843 },
27662306a36Sopenharmony_ci	{},
27762306a36Sopenharmony_ci};
27862306a36Sopenharmony_ci
27962306a36Sopenharmony_ciMODULE_DEVICE_TABLE(platform, max77693_pmic_id);
28062306a36Sopenharmony_ci
28162306a36Sopenharmony_cistatic struct platform_driver max77693_pmic_driver = {
28262306a36Sopenharmony_ci	.driver = {
28362306a36Sopenharmony_ci		   .name = "max77693-pmic",
28462306a36Sopenharmony_ci		   .probe_type = PROBE_PREFER_ASYNCHRONOUS,
28562306a36Sopenharmony_ci		   },
28662306a36Sopenharmony_ci	.probe = max77693_pmic_probe,
28762306a36Sopenharmony_ci	.id_table = max77693_pmic_id,
28862306a36Sopenharmony_ci};
28962306a36Sopenharmony_ci
29062306a36Sopenharmony_cistatic int __init max77693_pmic_init(void)
29162306a36Sopenharmony_ci{
29262306a36Sopenharmony_ci	return platform_driver_register(&max77693_pmic_driver);
29362306a36Sopenharmony_ci}
29462306a36Sopenharmony_cisubsys_initcall(max77693_pmic_init);
29562306a36Sopenharmony_ci
29662306a36Sopenharmony_cistatic void __exit max77693_pmic_cleanup(void)
29762306a36Sopenharmony_ci{
29862306a36Sopenharmony_ci	platform_driver_unregister(&max77693_pmic_driver);
29962306a36Sopenharmony_ci}
30062306a36Sopenharmony_cimodule_exit(max77693_pmic_cleanup);
30162306a36Sopenharmony_ci
30262306a36Sopenharmony_ciMODULE_DESCRIPTION("MAXIM 77693/77843 regulator driver");
30362306a36Sopenharmony_ciMODULE_AUTHOR("Jonghwa Lee <jonghwa3.lee@samsung.com>");
30462306a36Sopenharmony_ciMODULE_AUTHOR("Krzysztof Kozlowski <krzk@kernel.org>");
30562306a36Sopenharmony_ciMODULE_LICENSE("GPL");
306