162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
262306a36Sopenharmony_ci//
362306a36Sopenharmony_ci// max14577.c - Regulator driver for the Maxim 14577/77836
462306a36Sopenharmony_ci//
562306a36Sopenharmony_ci// Copyright (C) 2013,2014 Samsung Electronics
662306a36Sopenharmony_ci// Krzysztof Kozlowski <krzk@kernel.org>
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#include <linux/module.h>
962306a36Sopenharmony_ci#include <linux/platform_device.h>
1062306a36Sopenharmony_ci#include <linux/regulator/driver.h>
1162306a36Sopenharmony_ci#include <linux/mfd/max14577.h>
1262306a36Sopenharmony_ci#include <linux/mfd/max14577-private.h>
1362306a36Sopenharmony_ci#include <linux/regulator/of_regulator.h>
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_cistatic int max14577_reg_is_enabled(struct regulator_dev *rdev)
1662306a36Sopenharmony_ci{
1762306a36Sopenharmony_ci	int rid = rdev_get_id(rdev);
1862306a36Sopenharmony_ci	struct regmap *rmap = rdev->regmap;
1962306a36Sopenharmony_ci	u8 reg_data;
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_ci	switch (rid) {
2262306a36Sopenharmony_ci	case MAX14577_CHARGER:
2362306a36Sopenharmony_ci		max14577_read_reg(rmap, MAX14577_CHG_REG_CHG_CTRL2, &reg_data);
2462306a36Sopenharmony_ci		if ((reg_data & CHGCTRL2_MBCHOSTEN_MASK) == 0)
2562306a36Sopenharmony_ci			return 0;
2662306a36Sopenharmony_ci		max14577_read_reg(rmap, MAX14577_CHG_REG_STATUS3, &reg_data);
2762306a36Sopenharmony_ci		if ((reg_data & STATUS3_CGMBC_MASK) == 0)
2862306a36Sopenharmony_ci			return 0;
2962306a36Sopenharmony_ci		/* MBCHOSTEN and CGMBC are on */
3062306a36Sopenharmony_ci		return 1;
3162306a36Sopenharmony_ci	default:
3262306a36Sopenharmony_ci		return -EINVAL;
3362306a36Sopenharmony_ci	}
3462306a36Sopenharmony_ci}
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_cistatic int max14577_reg_get_current_limit(struct regulator_dev *rdev)
3762306a36Sopenharmony_ci{
3862306a36Sopenharmony_ci	u8 reg_data;
3962306a36Sopenharmony_ci	struct regmap *rmap = rdev->regmap;
4062306a36Sopenharmony_ci	struct max14577 *max14577 = rdev_get_drvdata(rdev);
4162306a36Sopenharmony_ci	const struct maxim_charger_current *limits =
4262306a36Sopenharmony_ci		&maxim_charger_currents[max14577->dev_type];
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_ci	if (rdev_get_id(rdev) != MAX14577_CHARGER)
4562306a36Sopenharmony_ci		return -EINVAL;
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ci	max14577_read_reg(rmap, MAX14577_CHG_REG_CHG_CTRL4, &reg_data);
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_ci	if ((reg_data & CHGCTRL4_MBCICHWRCL_MASK) == 0)
5062306a36Sopenharmony_ci		return limits->min;
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_ci	reg_data = ((reg_data & CHGCTRL4_MBCICHWRCH_MASK) >>
5362306a36Sopenharmony_ci			CHGCTRL4_MBCICHWRCH_SHIFT);
5462306a36Sopenharmony_ci	return limits->high_start + reg_data * limits->high_step;
5562306a36Sopenharmony_ci}
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_cistatic int max14577_reg_set_current_limit(struct regulator_dev *rdev,
5862306a36Sopenharmony_ci		int min_uA, int max_uA)
5962306a36Sopenharmony_ci{
6062306a36Sopenharmony_ci	u8 reg_data;
6162306a36Sopenharmony_ci	int ret;
6262306a36Sopenharmony_ci	struct max14577 *max14577 = rdev_get_drvdata(rdev);
6362306a36Sopenharmony_ci	const struct maxim_charger_current *limits =
6462306a36Sopenharmony_ci		&maxim_charger_currents[max14577->dev_type];
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ci	if (rdev_get_id(rdev) != MAX14577_CHARGER)
6762306a36Sopenharmony_ci		return -EINVAL;
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_ci	ret = maxim_charger_calc_reg_current(limits, min_uA, max_uA, &reg_data);
7062306a36Sopenharmony_ci	if (ret)
7162306a36Sopenharmony_ci		return ret;
7262306a36Sopenharmony_ci
7362306a36Sopenharmony_ci	return max14577_update_reg(rdev->regmap, MAX14577_CHG_REG_CHG_CTRL4,
7462306a36Sopenharmony_ci			CHGCTRL4_MBCICHWRCL_MASK | CHGCTRL4_MBCICHWRCH_MASK,
7562306a36Sopenharmony_ci			reg_data);
7662306a36Sopenharmony_ci}
7762306a36Sopenharmony_ci
7862306a36Sopenharmony_cistatic const struct regulator_ops max14577_safeout_ops = {
7962306a36Sopenharmony_ci	.is_enabled		= regulator_is_enabled_regmap,
8062306a36Sopenharmony_ci	.enable			= regulator_enable_regmap,
8162306a36Sopenharmony_ci	.disable		= regulator_disable_regmap,
8262306a36Sopenharmony_ci	.list_voltage		= regulator_list_voltage_linear,
8362306a36Sopenharmony_ci};
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_cistatic const struct regulator_ops max14577_charger_ops = {
8662306a36Sopenharmony_ci	.is_enabled		= max14577_reg_is_enabled,
8762306a36Sopenharmony_ci	.enable			= regulator_enable_regmap,
8862306a36Sopenharmony_ci	.disable		= regulator_disable_regmap,
8962306a36Sopenharmony_ci	.get_current_limit	= max14577_reg_get_current_limit,
9062306a36Sopenharmony_ci	.set_current_limit	= max14577_reg_set_current_limit,
9162306a36Sopenharmony_ci};
9262306a36Sopenharmony_ci
9362306a36Sopenharmony_ci#define MAX14577_SAFEOUT_REG	{ \
9462306a36Sopenharmony_ci	.name		= "SAFEOUT", \
9562306a36Sopenharmony_ci	.of_match	= of_match_ptr("SAFEOUT"), \
9662306a36Sopenharmony_ci	.regulators_node = of_match_ptr("regulators"), \
9762306a36Sopenharmony_ci	.id		= MAX14577_SAFEOUT, \
9862306a36Sopenharmony_ci	.ops		= &max14577_safeout_ops, \
9962306a36Sopenharmony_ci	.type		= REGULATOR_VOLTAGE, \
10062306a36Sopenharmony_ci	.owner		= THIS_MODULE, \
10162306a36Sopenharmony_ci	.n_voltages	= 1, \
10262306a36Sopenharmony_ci	.min_uV		= MAX14577_REGULATOR_SAFEOUT_VOLTAGE, \
10362306a36Sopenharmony_ci	.enable_reg	= MAX14577_REG_CONTROL2, \
10462306a36Sopenharmony_ci	.enable_mask	= CTRL2_SFOUTORD_MASK, \
10562306a36Sopenharmony_ci}
10662306a36Sopenharmony_ci#define MAX14577_CHARGER_REG	{ \
10762306a36Sopenharmony_ci	.name		= "CHARGER", \
10862306a36Sopenharmony_ci	.of_match	= of_match_ptr("CHARGER"), \
10962306a36Sopenharmony_ci	.regulators_node = of_match_ptr("regulators"), \
11062306a36Sopenharmony_ci	.id		= MAX14577_CHARGER, \
11162306a36Sopenharmony_ci	.ops		= &max14577_charger_ops, \
11262306a36Sopenharmony_ci	.type		= REGULATOR_CURRENT, \
11362306a36Sopenharmony_ci	.owner		= THIS_MODULE, \
11462306a36Sopenharmony_ci	.enable_reg	= MAX14577_CHG_REG_CHG_CTRL2, \
11562306a36Sopenharmony_ci	.enable_mask	= CHGCTRL2_MBCHOSTEN_MASK, \
11662306a36Sopenharmony_ci}
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_cistatic const struct regulator_desc max14577_supported_regulators[] = {
11962306a36Sopenharmony_ci	[MAX14577_SAFEOUT] = MAX14577_SAFEOUT_REG,
12062306a36Sopenharmony_ci	[MAX14577_CHARGER] = MAX14577_CHARGER_REG,
12162306a36Sopenharmony_ci};
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_cistatic const struct regulator_ops max77836_ldo_ops = {
12462306a36Sopenharmony_ci	.is_enabled		= regulator_is_enabled_regmap,
12562306a36Sopenharmony_ci	.enable			= regulator_enable_regmap,
12662306a36Sopenharmony_ci	.disable		= regulator_disable_regmap,
12762306a36Sopenharmony_ci	.list_voltage		= regulator_list_voltage_linear,
12862306a36Sopenharmony_ci	.map_voltage		= regulator_map_voltage_linear,
12962306a36Sopenharmony_ci	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
13062306a36Sopenharmony_ci	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
13162306a36Sopenharmony_ci	/* TODO: add .set_suspend_mode */
13262306a36Sopenharmony_ci};
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_ci#define MAX77836_LDO_REG(num)	{ \
13562306a36Sopenharmony_ci	.name		= "LDO" # num, \
13662306a36Sopenharmony_ci	.of_match	= of_match_ptr("LDO" # num), \
13762306a36Sopenharmony_ci	.regulators_node = of_match_ptr("regulators"), \
13862306a36Sopenharmony_ci	.id		= MAX77836_LDO ## num, \
13962306a36Sopenharmony_ci	.ops		= &max77836_ldo_ops, \
14062306a36Sopenharmony_ci	.type		= REGULATOR_VOLTAGE, \
14162306a36Sopenharmony_ci	.owner		= THIS_MODULE, \
14262306a36Sopenharmony_ci	.n_voltages	= MAX77836_REGULATOR_LDO_VOLTAGE_STEPS_NUM, \
14362306a36Sopenharmony_ci	.min_uV		= MAX77836_REGULATOR_LDO_VOLTAGE_MIN, \
14462306a36Sopenharmony_ci	.uV_step	= MAX77836_REGULATOR_LDO_VOLTAGE_STEP, \
14562306a36Sopenharmony_ci	.enable_reg	= MAX77836_LDO_REG_CNFG1_LDO ## num, \
14662306a36Sopenharmony_ci	.enable_mask	= MAX77836_CNFG1_LDO_PWRMD_MASK, \
14762306a36Sopenharmony_ci	.vsel_reg	= MAX77836_LDO_REG_CNFG1_LDO ## num, \
14862306a36Sopenharmony_ci	.vsel_mask	= MAX77836_CNFG1_LDO_TV_MASK, \
14962306a36Sopenharmony_ci}
15062306a36Sopenharmony_ci
15162306a36Sopenharmony_cistatic const struct regulator_desc max77836_supported_regulators[] = {
15262306a36Sopenharmony_ci	[MAX14577_SAFEOUT] = MAX14577_SAFEOUT_REG,
15362306a36Sopenharmony_ci	[MAX14577_CHARGER] = MAX14577_CHARGER_REG,
15462306a36Sopenharmony_ci	[MAX77836_LDO1] = MAX77836_LDO_REG(1),
15562306a36Sopenharmony_ci	[MAX77836_LDO2] = MAX77836_LDO_REG(2),
15662306a36Sopenharmony_ci};
15762306a36Sopenharmony_ci
15862306a36Sopenharmony_ci/*
15962306a36Sopenharmony_ci * Registers for regulators of max77836 use different I2C slave addresses so
16062306a36Sopenharmony_ci * different regmaps must be used for them.
16162306a36Sopenharmony_ci *
16262306a36Sopenharmony_ci * Returns proper regmap for accessing regulator passed by id.
16362306a36Sopenharmony_ci */
16462306a36Sopenharmony_cistatic struct regmap *max14577_get_regmap(struct max14577 *max14577,
16562306a36Sopenharmony_ci		int reg_id)
16662306a36Sopenharmony_ci{
16762306a36Sopenharmony_ci	switch (max14577->dev_type) {
16862306a36Sopenharmony_ci	case MAXIM_DEVICE_TYPE_MAX77836:
16962306a36Sopenharmony_ci		switch (reg_id) {
17062306a36Sopenharmony_ci		case MAX77836_SAFEOUT ... MAX77836_CHARGER:
17162306a36Sopenharmony_ci			return max14577->regmap;
17262306a36Sopenharmony_ci		default:
17362306a36Sopenharmony_ci			/* MAX77836_LDO1 ... MAX77836_LDO2 */
17462306a36Sopenharmony_ci			return max14577->regmap_pmic;
17562306a36Sopenharmony_ci		}
17662306a36Sopenharmony_ci
17762306a36Sopenharmony_ci	case MAXIM_DEVICE_TYPE_MAX14577:
17862306a36Sopenharmony_ci	default:
17962306a36Sopenharmony_ci		return max14577->regmap;
18062306a36Sopenharmony_ci	}
18162306a36Sopenharmony_ci}
18262306a36Sopenharmony_ci
18362306a36Sopenharmony_cistatic int max14577_regulator_probe(struct platform_device *pdev)
18462306a36Sopenharmony_ci{
18562306a36Sopenharmony_ci	struct max14577 *max14577 = dev_get_drvdata(pdev->dev.parent);
18662306a36Sopenharmony_ci	struct max14577_platform_data *pdata = dev_get_platdata(max14577->dev);
18762306a36Sopenharmony_ci	int i, ret = 0;
18862306a36Sopenharmony_ci	struct regulator_config config = {};
18962306a36Sopenharmony_ci	const struct regulator_desc *supported_regulators;
19062306a36Sopenharmony_ci	unsigned int supported_regulators_size;
19162306a36Sopenharmony_ci	enum maxim_device_type dev_type = max14577->dev_type;
19262306a36Sopenharmony_ci
19362306a36Sopenharmony_ci	switch (dev_type) {
19462306a36Sopenharmony_ci	case MAXIM_DEVICE_TYPE_MAX77836:
19562306a36Sopenharmony_ci		supported_regulators = max77836_supported_regulators;
19662306a36Sopenharmony_ci		supported_regulators_size = ARRAY_SIZE(max77836_supported_regulators);
19762306a36Sopenharmony_ci		break;
19862306a36Sopenharmony_ci	case MAXIM_DEVICE_TYPE_MAX14577:
19962306a36Sopenharmony_ci	default:
20062306a36Sopenharmony_ci		supported_regulators = max14577_supported_regulators;
20162306a36Sopenharmony_ci		supported_regulators_size = ARRAY_SIZE(max14577_supported_regulators);
20262306a36Sopenharmony_ci	}
20362306a36Sopenharmony_ci
20462306a36Sopenharmony_ci	config.dev = max14577->dev;
20562306a36Sopenharmony_ci	config.driver_data = max14577;
20662306a36Sopenharmony_ci
20762306a36Sopenharmony_ci	for (i = 0; i < supported_regulators_size; i++) {
20862306a36Sopenharmony_ci		struct regulator_dev *regulator;
20962306a36Sopenharmony_ci		/*
21062306a36Sopenharmony_ci		 * Index of supported_regulators[] is also the id and must
21162306a36Sopenharmony_ci		 * match index of pdata->regulators[].
21262306a36Sopenharmony_ci		 */
21362306a36Sopenharmony_ci		if (pdata && pdata->regulators) {
21462306a36Sopenharmony_ci			config.init_data = pdata->regulators[i].initdata;
21562306a36Sopenharmony_ci			config.of_node = pdata->regulators[i].of_node;
21662306a36Sopenharmony_ci		}
21762306a36Sopenharmony_ci		config.regmap = max14577_get_regmap(max14577,
21862306a36Sopenharmony_ci				supported_regulators[i].id);
21962306a36Sopenharmony_ci
22062306a36Sopenharmony_ci		regulator = devm_regulator_register(&pdev->dev,
22162306a36Sopenharmony_ci				&supported_regulators[i], &config);
22262306a36Sopenharmony_ci		if (IS_ERR(regulator)) {
22362306a36Sopenharmony_ci			ret = PTR_ERR(regulator);
22462306a36Sopenharmony_ci			dev_err(&pdev->dev,
22562306a36Sopenharmony_ci					"Regulator init failed for %d/%s with error: %d\n",
22662306a36Sopenharmony_ci					i, supported_regulators[i].name, ret);
22762306a36Sopenharmony_ci			return ret;
22862306a36Sopenharmony_ci		}
22962306a36Sopenharmony_ci	}
23062306a36Sopenharmony_ci
23162306a36Sopenharmony_ci	return ret;
23262306a36Sopenharmony_ci}
23362306a36Sopenharmony_ci
23462306a36Sopenharmony_cistatic const struct platform_device_id max14577_regulator_id[] = {
23562306a36Sopenharmony_ci	{ "max14577-regulator", MAXIM_DEVICE_TYPE_MAX14577, },
23662306a36Sopenharmony_ci	{ "max77836-regulator", MAXIM_DEVICE_TYPE_MAX77836, },
23762306a36Sopenharmony_ci	{ }
23862306a36Sopenharmony_ci};
23962306a36Sopenharmony_ciMODULE_DEVICE_TABLE(platform, max14577_regulator_id);
24062306a36Sopenharmony_ci
24162306a36Sopenharmony_cistatic struct platform_driver max14577_regulator_driver = {
24262306a36Sopenharmony_ci	.driver = {
24362306a36Sopenharmony_ci		   .name = "max14577-regulator",
24462306a36Sopenharmony_ci		   .probe_type = PROBE_PREFER_ASYNCHRONOUS,
24562306a36Sopenharmony_ci		   },
24662306a36Sopenharmony_ci	.probe		= max14577_regulator_probe,
24762306a36Sopenharmony_ci	.id_table	= max14577_regulator_id,
24862306a36Sopenharmony_ci};
24962306a36Sopenharmony_ci
25062306a36Sopenharmony_cistatic int __init max14577_regulator_init(void)
25162306a36Sopenharmony_ci{
25262306a36Sopenharmony_ci	BUILD_BUG_ON(ARRAY_SIZE(max14577_supported_regulators) != MAX14577_REGULATOR_NUM);
25362306a36Sopenharmony_ci	BUILD_BUG_ON(ARRAY_SIZE(max77836_supported_regulators) != MAX77836_REGULATOR_NUM);
25462306a36Sopenharmony_ci
25562306a36Sopenharmony_ci	BUILD_BUG_ON(MAX77836_REGULATOR_LDO_VOLTAGE_MIN +
25662306a36Sopenharmony_ci			(MAX77836_REGULATOR_LDO_VOLTAGE_STEP *
25762306a36Sopenharmony_ci			  (MAX77836_REGULATOR_LDO_VOLTAGE_STEPS_NUM - 1)) !=
25862306a36Sopenharmony_ci			MAX77836_REGULATOR_LDO_VOLTAGE_MAX);
25962306a36Sopenharmony_ci
26062306a36Sopenharmony_ci	return platform_driver_register(&max14577_regulator_driver);
26162306a36Sopenharmony_ci}
26262306a36Sopenharmony_cisubsys_initcall(max14577_regulator_init);
26362306a36Sopenharmony_ci
26462306a36Sopenharmony_cistatic void __exit max14577_regulator_exit(void)
26562306a36Sopenharmony_ci{
26662306a36Sopenharmony_ci	platform_driver_unregister(&max14577_regulator_driver);
26762306a36Sopenharmony_ci}
26862306a36Sopenharmony_cimodule_exit(max14577_regulator_exit);
26962306a36Sopenharmony_ci
27062306a36Sopenharmony_ciMODULE_AUTHOR("Krzysztof Kozlowski <krzk@kernel.org>");
27162306a36Sopenharmony_ciMODULE_DESCRIPTION("Maxim 14577/77836 regulator driver");
27262306a36Sopenharmony_ciMODULE_LICENSE("GPL");
273