162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci//
362306a36Sopenharmony_ci// Copyright (c) 2014 MediaTek Inc.
462306a36Sopenharmony_ci// Author: Flora Fu <flora.fu@mediatek.com>
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci#include <linux/module.h>
762306a36Sopenharmony_ci#include <linux/of.h>
862306a36Sopenharmony_ci#include <linux/platform_device.h>
962306a36Sopenharmony_ci#include <linux/regmap.h>
1062306a36Sopenharmony_ci#include <linux/mfd/mt6397/core.h>
1162306a36Sopenharmony_ci#include <linux/mfd/mt6397/registers.h>
1262306a36Sopenharmony_ci#include <linux/regulator/driver.h>
1362306a36Sopenharmony_ci#include <linux/regulator/machine.h>
1462306a36Sopenharmony_ci#include <linux/regulator/mt6397-regulator.h>
1562306a36Sopenharmony_ci#include <linux/regulator/of_regulator.h>
1662306a36Sopenharmony_ci#include <dt-bindings/regulator/mediatek,mt6397-regulator.h>
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_ci/*
1962306a36Sopenharmony_ci * MT6397 regulators' information
2062306a36Sopenharmony_ci *
2162306a36Sopenharmony_ci * @desc: standard fields of regulator description.
2262306a36Sopenharmony_ci * @qi: Mask for query enable signal status of regulators
2362306a36Sopenharmony_ci * @vselon_reg: Register sections for hardware control mode of bucks
2462306a36Sopenharmony_ci * @vselctrl_reg: Register for controlling the buck control mode.
2562306a36Sopenharmony_ci * @vselctrl_mask: Mask for query buck's voltage control mode.
2662306a36Sopenharmony_ci */
2762306a36Sopenharmony_cistruct mt6397_regulator_info {
2862306a36Sopenharmony_ci	struct regulator_desc desc;
2962306a36Sopenharmony_ci	u32 qi;
3062306a36Sopenharmony_ci	u32 vselon_reg;
3162306a36Sopenharmony_ci	u32 vselctrl_reg;
3262306a36Sopenharmony_ci	u32 vselctrl_mask;
3362306a36Sopenharmony_ci	u32 modeset_reg;
3462306a36Sopenharmony_ci	u32 modeset_mask;
3562306a36Sopenharmony_ci};
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_ci#define MT6397_BUCK(match, vreg, min, max, step, volt_ranges, enreg,	\
3862306a36Sopenharmony_ci		vosel, vosel_mask, voselon, vosel_ctrl, _modeset_reg,	\
3962306a36Sopenharmony_ci		_modeset_shift)					\
4062306a36Sopenharmony_ci[MT6397_ID_##vreg] = {							\
4162306a36Sopenharmony_ci	.desc = {							\
4262306a36Sopenharmony_ci		.name = #vreg,						\
4362306a36Sopenharmony_ci		.of_match = of_match_ptr(match),			\
4462306a36Sopenharmony_ci		.ops = &mt6397_volt_range_ops,				\
4562306a36Sopenharmony_ci		.type = REGULATOR_VOLTAGE,				\
4662306a36Sopenharmony_ci		.id = MT6397_ID_##vreg,					\
4762306a36Sopenharmony_ci		.owner = THIS_MODULE,					\
4862306a36Sopenharmony_ci		.n_voltages = (max - min)/step + 1,			\
4962306a36Sopenharmony_ci		.linear_ranges = volt_ranges,				\
5062306a36Sopenharmony_ci		.n_linear_ranges = ARRAY_SIZE(volt_ranges),		\
5162306a36Sopenharmony_ci		.vsel_reg = vosel,					\
5262306a36Sopenharmony_ci		.vsel_mask = vosel_mask,				\
5362306a36Sopenharmony_ci		.enable_reg = enreg,					\
5462306a36Sopenharmony_ci		.enable_mask = BIT(0),					\
5562306a36Sopenharmony_ci		.of_map_mode = mt6397_map_mode,				\
5662306a36Sopenharmony_ci	},								\
5762306a36Sopenharmony_ci	.qi = BIT(13),							\
5862306a36Sopenharmony_ci	.vselon_reg = voselon,						\
5962306a36Sopenharmony_ci	.vselctrl_reg = vosel_ctrl,					\
6062306a36Sopenharmony_ci	.vselctrl_mask = BIT(1),					\
6162306a36Sopenharmony_ci	.modeset_reg = _modeset_reg,					\
6262306a36Sopenharmony_ci	.modeset_mask = BIT(_modeset_shift),				\
6362306a36Sopenharmony_ci}
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ci#define MT6397_LDO(match, vreg, ldo_volt_table, enreg, enbit, vosel,	\
6662306a36Sopenharmony_ci		vosel_mask)						\
6762306a36Sopenharmony_ci[MT6397_ID_##vreg] = {							\
6862306a36Sopenharmony_ci	.desc = {							\
6962306a36Sopenharmony_ci		.name = #vreg,						\
7062306a36Sopenharmony_ci		.of_match = of_match_ptr(match),			\
7162306a36Sopenharmony_ci		.ops = &mt6397_volt_table_ops,				\
7262306a36Sopenharmony_ci		.type = REGULATOR_VOLTAGE,				\
7362306a36Sopenharmony_ci		.id = MT6397_ID_##vreg,					\
7462306a36Sopenharmony_ci		.owner = THIS_MODULE,					\
7562306a36Sopenharmony_ci		.n_voltages = ARRAY_SIZE(ldo_volt_table),		\
7662306a36Sopenharmony_ci		.volt_table = ldo_volt_table,				\
7762306a36Sopenharmony_ci		.vsel_reg = vosel,					\
7862306a36Sopenharmony_ci		.vsel_mask = vosel_mask,				\
7962306a36Sopenharmony_ci		.enable_reg = enreg,					\
8062306a36Sopenharmony_ci		.enable_mask = BIT(enbit),				\
8162306a36Sopenharmony_ci	},								\
8262306a36Sopenharmony_ci	.qi = BIT(15),							\
8362306a36Sopenharmony_ci}
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_ci#define MT6397_REG_FIXED(match, vreg, enreg, enbit, volt)		\
8662306a36Sopenharmony_ci[MT6397_ID_##vreg] = {							\
8762306a36Sopenharmony_ci	.desc = {							\
8862306a36Sopenharmony_ci		.name = #vreg,						\
8962306a36Sopenharmony_ci		.of_match = of_match_ptr(match),			\
9062306a36Sopenharmony_ci		.ops = &mt6397_volt_fixed_ops,				\
9162306a36Sopenharmony_ci		.type = REGULATOR_VOLTAGE,				\
9262306a36Sopenharmony_ci		.id = MT6397_ID_##vreg,					\
9362306a36Sopenharmony_ci		.owner = THIS_MODULE,					\
9462306a36Sopenharmony_ci		.n_voltages = 1,					\
9562306a36Sopenharmony_ci		.enable_reg = enreg,					\
9662306a36Sopenharmony_ci		.enable_mask = BIT(enbit),				\
9762306a36Sopenharmony_ci		.min_uV = volt,						\
9862306a36Sopenharmony_ci	},								\
9962306a36Sopenharmony_ci	.qi = BIT(15),							\
10062306a36Sopenharmony_ci}
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_cistatic const struct linear_range buck_volt_range1[] = {
10362306a36Sopenharmony_ci	REGULATOR_LINEAR_RANGE(700000, 0, 0x7f, 6250),
10462306a36Sopenharmony_ci};
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_cistatic const struct linear_range buck_volt_range2[] = {
10762306a36Sopenharmony_ci	REGULATOR_LINEAR_RANGE(800000, 0, 0x7f, 6250),
10862306a36Sopenharmony_ci};
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_cistatic const struct linear_range buck_volt_range3[] = {
11162306a36Sopenharmony_ci	REGULATOR_LINEAR_RANGE(1500000, 0, 0x1f, 20000),
11262306a36Sopenharmony_ci};
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_cistatic const unsigned int ldo_volt_table1[] = {
11562306a36Sopenharmony_ci	1500000, 1800000, 2500000, 2800000,
11662306a36Sopenharmony_ci};
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_cistatic const unsigned int ldo_volt_table2[] = {
11962306a36Sopenharmony_ci	1800000, 3300000,
12062306a36Sopenharmony_ci};
12162306a36Sopenharmony_ci
12262306a36Sopenharmony_cistatic const unsigned int ldo_volt_table3[] = {
12362306a36Sopenharmony_ci	3000000, 3300000,
12462306a36Sopenharmony_ci};
12562306a36Sopenharmony_ci
12662306a36Sopenharmony_cistatic const unsigned int ldo_volt_table4[] = {
12762306a36Sopenharmony_ci	1220000, 1300000, 1500000, 1800000, 2500000, 2800000, 3000000, 3300000,
12862306a36Sopenharmony_ci};
12962306a36Sopenharmony_ci
13062306a36Sopenharmony_cistatic const unsigned int ldo_volt_table5[] = {
13162306a36Sopenharmony_ci	1200000, 1300000, 1500000, 1800000, 2500000, 2800000, 3000000, 3300000,
13262306a36Sopenharmony_ci};
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_cistatic const unsigned int ldo_volt_table5_v2[] = {
13562306a36Sopenharmony_ci	1200000, 1000000, 1500000, 1800000, 2500000, 2800000, 3000000, 3300000,
13662306a36Sopenharmony_ci};
13762306a36Sopenharmony_ci
13862306a36Sopenharmony_cistatic const unsigned int ldo_volt_table6[] = {
13962306a36Sopenharmony_ci	1200000, 1300000, 1500000, 1800000, 2500000, 2800000, 3000000, 2000000,
14062306a36Sopenharmony_ci};
14162306a36Sopenharmony_ci
14262306a36Sopenharmony_cistatic const unsigned int ldo_volt_table7[] = {
14362306a36Sopenharmony_ci	1300000, 1500000, 1800000, 2000000, 2500000, 2800000, 3000000, 3300000,
14462306a36Sopenharmony_ci};
14562306a36Sopenharmony_ci
14662306a36Sopenharmony_cistatic unsigned int mt6397_map_mode(unsigned int mode)
14762306a36Sopenharmony_ci{
14862306a36Sopenharmony_ci	switch (mode) {
14962306a36Sopenharmony_ci	case MT6397_BUCK_MODE_AUTO:
15062306a36Sopenharmony_ci		return REGULATOR_MODE_NORMAL;
15162306a36Sopenharmony_ci	case MT6397_BUCK_MODE_FORCE_PWM:
15262306a36Sopenharmony_ci		return REGULATOR_MODE_FAST;
15362306a36Sopenharmony_ci	default:
15462306a36Sopenharmony_ci		return REGULATOR_MODE_INVALID;
15562306a36Sopenharmony_ci	}
15662306a36Sopenharmony_ci}
15762306a36Sopenharmony_ci
15862306a36Sopenharmony_cistatic int mt6397_regulator_set_mode(struct regulator_dev *rdev,
15962306a36Sopenharmony_ci				     unsigned int mode)
16062306a36Sopenharmony_ci{
16162306a36Sopenharmony_ci	struct mt6397_regulator_info *info = rdev_get_drvdata(rdev);
16262306a36Sopenharmony_ci	int ret, val;
16362306a36Sopenharmony_ci
16462306a36Sopenharmony_ci	switch (mode) {
16562306a36Sopenharmony_ci	case REGULATOR_MODE_FAST:
16662306a36Sopenharmony_ci		val = MT6397_BUCK_MODE_FORCE_PWM;
16762306a36Sopenharmony_ci		break;
16862306a36Sopenharmony_ci	case REGULATOR_MODE_NORMAL:
16962306a36Sopenharmony_ci		val = MT6397_BUCK_MODE_AUTO;
17062306a36Sopenharmony_ci		break;
17162306a36Sopenharmony_ci	default:
17262306a36Sopenharmony_ci		ret = -EINVAL;
17362306a36Sopenharmony_ci		goto err_mode;
17462306a36Sopenharmony_ci	}
17562306a36Sopenharmony_ci
17662306a36Sopenharmony_ci	dev_dbg(&rdev->dev, "mt6397 buck set_mode %#x, %#x, %#x\n",
17762306a36Sopenharmony_ci		info->modeset_reg, info->modeset_mask, val);
17862306a36Sopenharmony_ci
17962306a36Sopenharmony_ci	val <<= ffs(info->modeset_mask) - 1;
18062306a36Sopenharmony_ci
18162306a36Sopenharmony_ci	ret = regmap_update_bits(rdev->regmap, info->modeset_reg,
18262306a36Sopenharmony_ci				 info->modeset_mask, val);
18362306a36Sopenharmony_cierr_mode:
18462306a36Sopenharmony_ci	if (ret != 0) {
18562306a36Sopenharmony_ci		dev_err(&rdev->dev,
18662306a36Sopenharmony_ci			"Failed to set mt6397 buck mode: %d\n", ret);
18762306a36Sopenharmony_ci		return ret;
18862306a36Sopenharmony_ci	}
18962306a36Sopenharmony_ci
19062306a36Sopenharmony_ci	return 0;
19162306a36Sopenharmony_ci}
19262306a36Sopenharmony_ci
19362306a36Sopenharmony_cistatic unsigned int mt6397_regulator_get_mode(struct regulator_dev *rdev)
19462306a36Sopenharmony_ci{
19562306a36Sopenharmony_ci	struct mt6397_regulator_info *info = rdev_get_drvdata(rdev);
19662306a36Sopenharmony_ci	int ret, regval;
19762306a36Sopenharmony_ci
19862306a36Sopenharmony_ci	ret = regmap_read(rdev->regmap, info->modeset_reg, &regval);
19962306a36Sopenharmony_ci	if (ret != 0) {
20062306a36Sopenharmony_ci		dev_err(&rdev->dev,
20162306a36Sopenharmony_ci			"Failed to get mt6397 buck mode: %d\n", ret);
20262306a36Sopenharmony_ci		return ret;
20362306a36Sopenharmony_ci	}
20462306a36Sopenharmony_ci
20562306a36Sopenharmony_ci	regval &= info->modeset_mask;
20662306a36Sopenharmony_ci	regval >>= ffs(info->modeset_mask) - 1;
20762306a36Sopenharmony_ci
20862306a36Sopenharmony_ci	switch (regval) {
20962306a36Sopenharmony_ci	case MT6397_BUCK_MODE_AUTO:
21062306a36Sopenharmony_ci		return REGULATOR_MODE_NORMAL;
21162306a36Sopenharmony_ci	case MT6397_BUCK_MODE_FORCE_PWM:
21262306a36Sopenharmony_ci		return REGULATOR_MODE_FAST;
21362306a36Sopenharmony_ci	default:
21462306a36Sopenharmony_ci		return -EINVAL;
21562306a36Sopenharmony_ci	}
21662306a36Sopenharmony_ci}
21762306a36Sopenharmony_ci
21862306a36Sopenharmony_cistatic int mt6397_get_status(struct regulator_dev *rdev)
21962306a36Sopenharmony_ci{
22062306a36Sopenharmony_ci	int ret;
22162306a36Sopenharmony_ci	u32 regval;
22262306a36Sopenharmony_ci	struct mt6397_regulator_info *info = rdev_get_drvdata(rdev);
22362306a36Sopenharmony_ci
22462306a36Sopenharmony_ci	ret = regmap_read(rdev->regmap, info->desc.enable_reg, &regval);
22562306a36Sopenharmony_ci	if (ret != 0) {
22662306a36Sopenharmony_ci		dev_err(&rdev->dev, "Failed to get enable reg: %d\n", ret);
22762306a36Sopenharmony_ci		return ret;
22862306a36Sopenharmony_ci	}
22962306a36Sopenharmony_ci
23062306a36Sopenharmony_ci	return (regval & info->qi) ? REGULATOR_STATUS_ON : REGULATOR_STATUS_OFF;
23162306a36Sopenharmony_ci}
23262306a36Sopenharmony_ci
23362306a36Sopenharmony_cistatic const struct regulator_ops mt6397_volt_range_ops = {
23462306a36Sopenharmony_ci	.list_voltage = regulator_list_voltage_linear_range,
23562306a36Sopenharmony_ci	.map_voltage = regulator_map_voltage_linear_range,
23662306a36Sopenharmony_ci	.set_voltage_sel = regulator_set_voltage_sel_regmap,
23762306a36Sopenharmony_ci	.get_voltage_sel = regulator_get_voltage_sel_regmap,
23862306a36Sopenharmony_ci	.set_voltage_time_sel = regulator_set_voltage_time_sel,
23962306a36Sopenharmony_ci	.enable = regulator_enable_regmap,
24062306a36Sopenharmony_ci	.disable = regulator_disable_regmap,
24162306a36Sopenharmony_ci	.is_enabled = regulator_is_enabled_regmap,
24262306a36Sopenharmony_ci	.get_status = mt6397_get_status,
24362306a36Sopenharmony_ci	.set_mode = mt6397_regulator_set_mode,
24462306a36Sopenharmony_ci	.get_mode = mt6397_regulator_get_mode,
24562306a36Sopenharmony_ci};
24662306a36Sopenharmony_ci
24762306a36Sopenharmony_cistatic const struct regulator_ops mt6397_volt_table_ops = {
24862306a36Sopenharmony_ci	.list_voltage = regulator_list_voltage_table,
24962306a36Sopenharmony_ci	.map_voltage = regulator_map_voltage_iterate,
25062306a36Sopenharmony_ci	.set_voltage_sel = regulator_set_voltage_sel_regmap,
25162306a36Sopenharmony_ci	.get_voltage_sel = regulator_get_voltage_sel_regmap,
25262306a36Sopenharmony_ci	.set_voltage_time_sel = regulator_set_voltage_time_sel,
25362306a36Sopenharmony_ci	.enable = regulator_enable_regmap,
25462306a36Sopenharmony_ci	.disable = regulator_disable_regmap,
25562306a36Sopenharmony_ci	.is_enabled = regulator_is_enabled_regmap,
25662306a36Sopenharmony_ci	.get_status = mt6397_get_status,
25762306a36Sopenharmony_ci};
25862306a36Sopenharmony_ci
25962306a36Sopenharmony_cistatic const struct regulator_ops mt6397_volt_fixed_ops = {
26062306a36Sopenharmony_ci	.list_voltage = regulator_list_voltage_linear,
26162306a36Sopenharmony_ci	.enable = regulator_enable_regmap,
26262306a36Sopenharmony_ci	.disable = regulator_disable_regmap,
26362306a36Sopenharmony_ci	.is_enabled = regulator_is_enabled_regmap,
26462306a36Sopenharmony_ci	.get_status = mt6397_get_status,
26562306a36Sopenharmony_ci};
26662306a36Sopenharmony_ci
26762306a36Sopenharmony_ci/* The array is indexed by id(MT6397_ID_XXX) */
26862306a36Sopenharmony_cistatic struct mt6397_regulator_info mt6397_regulators[] = {
26962306a36Sopenharmony_ci	MT6397_BUCK("buck_vpca15", VPCA15, 700000, 1493750, 6250,
27062306a36Sopenharmony_ci		buck_volt_range1, MT6397_VCA15_CON7, MT6397_VCA15_CON9, 0x7f,
27162306a36Sopenharmony_ci		MT6397_VCA15_CON10, MT6397_VCA15_CON5, MT6397_VCA15_CON2, 11),
27262306a36Sopenharmony_ci	MT6397_BUCK("buck_vpca7", VPCA7, 700000, 1493750, 6250,
27362306a36Sopenharmony_ci		buck_volt_range1, MT6397_VPCA7_CON7, MT6397_VPCA7_CON9, 0x7f,
27462306a36Sopenharmony_ci		MT6397_VPCA7_CON10, MT6397_VPCA7_CON5, MT6397_VPCA7_CON2, 8),
27562306a36Sopenharmony_ci	MT6397_BUCK("buck_vsramca15", VSRAMCA15, 700000, 1493750, 6250,
27662306a36Sopenharmony_ci		buck_volt_range1, MT6397_VSRMCA15_CON7, MT6397_VSRMCA15_CON9,
27762306a36Sopenharmony_ci		0x7f, MT6397_VSRMCA15_CON10, MT6397_VSRMCA15_CON5,
27862306a36Sopenharmony_ci		MT6397_VSRMCA15_CON2, 8),
27962306a36Sopenharmony_ci	MT6397_BUCK("buck_vsramca7", VSRAMCA7, 700000, 1493750, 6250,
28062306a36Sopenharmony_ci		buck_volt_range1, MT6397_VSRMCA7_CON7, MT6397_VSRMCA7_CON9,
28162306a36Sopenharmony_ci		0x7f, MT6397_VSRMCA7_CON10, MT6397_VSRMCA7_CON5,
28262306a36Sopenharmony_ci		MT6397_VSRMCA7_CON2, 8),
28362306a36Sopenharmony_ci	MT6397_BUCK("buck_vcore", VCORE, 700000, 1493750, 6250,
28462306a36Sopenharmony_ci		buck_volt_range1, MT6397_VCORE_CON7, MT6397_VCORE_CON9, 0x7f,
28562306a36Sopenharmony_ci		MT6397_VCORE_CON10, MT6397_VCORE_CON5, MT6397_VCORE_CON2, 8),
28662306a36Sopenharmony_ci	MT6397_BUCK("buck_vgpu", VGPU, 700000, 1493750, 6250, buck_volt_range1,
28762306a36Sopenharmony_ci		MT6397_VGPU_CON7, MT6397_VGPU_CON9, 0x7f,
28862306a36Sopenharmony_ci		MT6397_VGPU_CON10, MT6397_VGPU_CON5, MT6397_VGPU_CON2, 8),
28962306a36Sopenharmony_ci	MT6397_BUCK("buck_vdrm", VDRM, 800000, 1593750, 6250, buck_volt_range2,
29062306a36Sopenharmony_ci		MT6397_VDRM_CON7, MT6397_VDRM_CON9, 0x7f,
29162306a36Sopenharmony_ci		MT6397_VDRM_CON10, MT6397_VDRM_CON5, MT6397_VDRM_CON2, 8),
29262306a36Sopenharmony_ci	MT6397_BUCK("buck_vio18", VIO18, 1500000, 2120000, 20000,
29362306a36Sopenharmony_ci		buck_volt_range3, MT6397_VIO18_CON7, MT6397_VIO18_CON9, 0x1f,
29462306a36Sopenharmony_ci		MT6397_VIO18_CON10, MT6397_VIO18_CON5, MT6397_VIO18_CON2, 8),
29562306a36Sopenharmony_ci	MT6397_REG_FIXED("ldo_vtcxo", VTCXO, MT6397_ANALDO_CON0, 10, 2800000),
29662306a36Sopenharmony_ci	MT6397_REG_FIXED("ldo_va28", VA28, MT6397_ANALDO_CON1, 14, 2800000),
29762306a36Sopenharmony_ci	MT6397_LDO("ldo_vcama", VCAMA, ldo_volt_table1,
29862306a36Sopenharmony_ci		MT6397_ANALDO_CON2, 15, MT6397_ANALDO_CON6, 0xC0),
29962306a36Sopenharmony_ci	MT6397_REG_FIXED("ldo_vio28", VIO28, MT6397_DIGLDO_CON0, 14, 2800000),
30062306a36Sopenharmony_ci	MT6397_REG_FIXED("ldo_vusb", VUSB, MT6397_DIGLDO_CON1, 14, 3300000),
30162306a36Sopenharmony_ci	MT6397_LDO("ldo_vmc", VMC, ldo_volt_table2,
30262306a36Sopenharmony_ci		MT6397_DIGLDO_CON2, 12, MT6397_DIGLDO_CON29, 0x10),
30362306a36Sopenharmony_ci	MT6397_LDO("ldo_vmch", VMCH, ldo_volt_table3,
30462306a36Sopenharmony_ci		MT6397_DIGLDO_CON3, 14, MT6397_DIGLDO_CON17, 0x80),
30562306a36Sopenharmony_ci	MT6397_LDO("ldo_vemc3v3", VEMC3V3, ldo_volt_table3,
30662306a36Sopenharmony_ci		MT6397_DIGLDO_CON4, 14, MT6397_DIGLDO_CON18, 0x10),
30762306a36Sopenharmony_ci	MT6397_LDO("ldo_vgp1", VGP1, ldo_volt_table4,
30862306a36Sopenharmony_ci		MT6397_DIGLDO_CON5, 15, MT6397_DIGLDO_CON19, 0xE0),
30962306a36Sopenharmony_ci	MT6397_LDO("ldo_vgp2", VGP2, ldo_volt_table5,
31062306a36Sopenharmony_ci		MT6397_DIGLDO_CON6, 15, MT6397_DIGLDO_CON20, 0xE0),
31162306a36Sopenharmony_ci	MT6397_LDO("ldo_vgp3", VGP3, ldo_volt_table5,
31262306a36Sopenharmony_ci		MT6397_DIGLDO_CON7, 15, MT6397_DIGLDO_CON21, 0xE0),
31362306a36Sopenharmony_ci	MT6397_LDO("ldo_vgp4", VGP4, ldo_volt_table5,
31462306a36Sopenharmony_ci		MT6397_DIGLDO_CON8, 15, MT6397_DIGLDO_CON22, 0xE0),
31562306a36Sopenharmony_ci	MT6397_LDO("ldo_vgp5", VGP5, ldo_volt_table6,
31662306a36Sopenharmony_ci		MT6397_DIGLDO_CON9, 15, MT6397_DIGLDO_CON23, 0xE0),
31762306a36Sopenharmony_ci	MT6397_LDO("ldo_vgp6", VGP6, ldo_volt_table5,
31862306a36Sopenharmony_ci		MT6397_DIGLDO_CON10, 15, MT6397_DIGLDO_CON33, 0xE0),
31962306a36Sopenharmony_ci	MT6397_LDO("ldo_vibr", VIBR, ldo_volt_table7,
32062306a36Sopenharmony_ci		MT6397_DIGLDO_CON24, 15, MT6397_DIGLDO_CON25, 0xE00),
32162306a36Sopenharmony_ci};
32262306a36Sopenharmony_ci
32362306a36Sopenharmony_cistatic int mt6397_set_buck_vosel_reg(struct platform_device *pdev)
32462306a36Sopenharmony_ci{
32562306a36Sopenharmony_ci	struct mt6397_chip *mt6397 = dev_get_drvdata(pdev->dev.parent);
32662306a36Sopenharmony_ci	int i;
32762306a36Sopenharmony_ci	u32 regval;
32862306a36Sopenharmony_ci
32962306a36Sopenharmony_ci	for (i = 0; i < MT6397_MAX_REGULATOR; i++) {
33062306a36Sopenharmony_ci		if (mt6397_regulators[i].vselctrl_reg) {
33162306a36Sopenharmony_ci			if (regmap_read(mt6397->regmap,
33262306a36Sopenharmony_ci				mt6397_regulators[i].vselctrl_reg,
33362306a36Sopenharmony_ci				&regval) < 0) {
33462306a36Sopenharmony_ci				dev_err(&pdev->dev,
33562306a36Sopenharmony_ci					"Failed to read buck ctrl\n");
33662306a36Sopenharmony_ci				return -EIO;
33762306a36Sopenharmony_ci			}
33862306a36Sopenharmony_ci
33962306a36Sopenharmony_ci			if (regval & mt6397_regulators[i].vselctrl_mask) {
34062306a36Sopenharmony_ci				mt6397_regulators[i].desc.vsel_reg =
34162306a36Sopenharmony_ci				mt6397_regulators[i].vselon_reg;
34262306a36Sopenharmony_ci			}
34362306a36Sopenharmony_ci		}
34462306a36Sopenharmony_ci	}
34562306a36Sopenharmony_ci
34662306a36Sopenharmony_ci	return 0;
34762306a36Sopenharmony_ci}
34862306a36Sopenharmony_ci
34962306a36Sopenharmony_cistatic int mt6397_regulator_probe(struct platform_device *pdev)
35062306a36Sopenharmony_ci{
35162306a36Sopenharmony_ci	struct mt6397_chip *mt6397 = dev_get_drvdata(pdev->dev.parent);
35262306a36Sopenharmony_ci	struct regulator_config config = {};
35362306a36Sopenharmony_ci	struct regulator_dev *rdev;
35462306a36Sopenharmony_ci	int i;
35562306a36Sopenharmony_ci	u32 reg_value, version;
35662306a36Sopenharmony_ci
35762306a36Sopenharmony_ci	/* Query buck controller to select activated voltage register part */
35862306a36Sopenharmony_ci	if (mt6397_set_buck_vosel_reg(pdev))
35962306a36Sopenharmony_ci		return -EIO;
36062306a36Sopenharmony_ci
36162306a36Sopenharmony_ci	/* Read PMIC chip revision to update constraints and voltage table */
36262306a36Sopenharmony_ci	if (regmap_read(mt6397->regmap, MT6397_CID, &reg_value) < 0) {
36362306a36Sopenharmony_ci		dev_err(&pdev->dev, "Failed to read Chip ID\n");
36462306a36Sopenharmony_ci		return -EIO;
36562306a36Sopenharmony_ci	}
36662306a36Sopenharmony_ci	dev_info(&pdev->dev, "Chip ID = 0x%x\n", reg_value);
36762306a36Sopenharmony_ci
36862306a36Sopenharmony_ci	version = (reg_value & 0xFF);
36962306a36Sopenharmony_ci	switch (version) {
37062306a36Sopenharmony_ci	case MT6397_REGULATOR_ID91:
37162306a36Sopenharmony_ci		mt6397_regulators[MT6397_ID_VGP2].desc.volt_table =
37262306a36Sopenharmony_ci		ldo_volt_table5_v2;
37362306a36Sopenharmony_ci		break;
37462306a36Sopenharmony_ci	default:
37562306a36Sopenharmony_ci		break;
37662306a36Sopenharmony_ci	}
37762306a36Sopenharmony_ci
37862306a36Sopenharmony_ci	for (i = 0; i < MT6397_MAX_REGULATOR; i++) {
37962306a36Sopenharmony_ci		config.dev = &pdev->dev;
38062306a36Sopenharmony_ci		config.driver_data = &mt6397_regulators[i];
38162306a36Sopenharmony_ci		config.regmap = mt6397->regmap;
38262306a36Sopenharmony_ci		rdev = devm_regulator_register(&pdev->dev,
38362306a36Sopenharmony_ci				&mt6397_regulators[i].desc, &config);
38462306a36Sopenharmony_ci		if (IS_ERR(rdev)) {
38562306a36Sopenharmony_ci			dev_err(&pdev->dev, "failed to register %s\n",
38662306a36Sopenharmony_ci				mt6397_regulators[i].desc.name);
38762306a36Sopenharmony_ci			return PTR_ERR(rdev);
38862306a36Sopenharmony_ci		}
38962306a36Sopenharmony_ci	}
39062306a36Sopenharmony_ci
39162306a36Sopenharmony_ci	return 0;
39262306a36Sopenharmony_ci}
39362306a36Sopenharmony_ci
39462306a36Sopenharmony_cistatic const struct platform_device_id mt6397_platform_ids[] = {
39562306a36Sopenharmony_ci	{"mt6397-regulator", 0},
39662306a36Sopenharmony_ci	{ /* sentinel */ },
39762306a36Sopenharmony_ci};
39862306a36Sopenharmony_ciMODULE_DEVICE_TABLE(platform, mt6397_platform_ids);
39962306a36Sopenharmony_ci
40062306a36Sopenharmony_cistatic const struct of_device_id mt6397_of_match[] __maybe_unused = {
40162306a36Sopenharmony_ci	{ .compatible = "mediatek,mt6397-regulator", },
40262306a36Sopenharmony_ci	{ /* sentinel */ },
40362306a36Sopenharmony_ci};
40462306a36Sopenharmony_ciMODULE_DEVICE_TABLE(of, mt6397_of_match);
40562306a36Sopenharmony_ci
40662306a36Sopenharmony_cistatic struct platform_driver mt6397_regulator_driver = {
40762306a36Sopenharmony_ci	.driver = {
40862306a36Sopenharmony_ci		.name = "mt6397-regulator",
40962306a36Sopenharmony_ci		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
41062306a36Sopenharmony_ci		.of_match_table = of_match_ptr(mt6397_of_match),
41162306a36Sopenharmony_ci	},
41262306a36Sopenharmony_ci	.probe = mt6397_regulator_probe,
41362306a36Sopenharmony_ci	.id_table = mt6397_platform_ids,
41462306a36Sopenharmony_ci};
41562306a36Sopenharmony_ci
41662306a36Sopenharmony_cimodule_platform_driver(mt6397_regulator_driver);
41762306a36Sopenharmony_ci
41862306a36Sopenharmony_ciMODULE_AUTHOR("Flora Fu <flora.fu@mediatek.com>");
41962306a36Sopenharmony_ciMODULE_DESCRIPTION("Regulator Driver for MediaTek MT6397 PMIC");
42062306a36Sopenharmony_ciMODULE_LICENSE("GPL");
421