162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci//
362306a36Sopenharmony_ci// Copyright (c) 2022 Collabora Ltd.
462306a36Sopenharmony_ci// Author: AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com>
562306a36Sopenharmony_ci//
662306a36Sopenharmony_ci// Based on mt6323-regulator.c,
762306a36Sopenharmony_ci//     Copyright (c) 2016 MediaTek Inc.
862306a36Sopenharmony_ci//
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci#include <linux/module.h>
1162306a36Sopenharmony_ci#include <linux/of.h>
1262306a36Sopenharmony_ci#include <linux/platform_device.h>
1362306a36Sopenharmony_ci#include <linux/regmap.h>
1462306a36Sopenharmony_ci#include <linux/mfd/mt6397/core.h>
1562306a36Sopenharmony_ci#include <linux/mfd/mt6332/registers.h>
1662306a36Sopenharmony_ci#include <linux/regulator/driver.h>
1762306a36Sopenharmony_ci#include <linux/regulator/machine.h>
1862306a36Sopenharmony_ci#include <linux/regulator/mt6332-regulator.h>
1962306a36Sopenharmony_ci#include <linux/regulator/of_regulator.h>
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_ci#define MT6332_LDO_MODE_NORMAL	0
2262306a36Sopenharmony_ci#define MT6332_LDO_MODE_LP	1
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ci/*
2562306a36Sopenharmony_ci * MT6332 regulators information
2662306a36Sopenharmony_ci *
2762306a36Sopenharmony_ci * @desc: standard fields of regulator description.
2862306a36Sopenharmony_ci * @qi: Mask for query enable signal status of regulators
2962306a36Sopenharmony_ci * @vselon_reg: Register sections for hardware control mode of bucks
3062306a36Sopenharmony_ci * @vselctrl_reg: Register for controlling the buck control mode.
3162306a36Sopenharmony_ci * @vselctrl_mask: Mask for query buck's voltage control mode.
3262306a36Sopenharmony_ci * @status_reg: Register for regulator enable status where qi unavailable
3362306a36Sopenharmony_ci * @status_mask: Mask for querying regulator enable status
3462306a36Sopenharmony_ci */
3562306a36Sopenharmony_cistruct mt6332_regulator_info {
3662306a36Sopenharmony_ci	struct regulator_desc desc;
3762306a36Sopenharmony_ci	u32 qi;
3862306a36Sopenharmony_ci	u32 vselon_reg;
3962306a36Sopenharmony_ci	u32 vselctrl_reg;
4062306a36Sopenharmony_ci	u32 vselctrl_mask;
4162306a36Sopenharmony_ci	u32 modeset_reg;
4262306a36Sopenharmony_ci	u32 modeset_mask;
4362306a36Sopenharmony_ci	u32 status_reg;
4462306a36Sopenharmony_ci	u32 status_mask;
4562306a36Sopenharmony_ci};
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ci#define MT6332_BUCK(match, vreg, min, max, step, volt_ranges, enreg,	\
4862306a36Sopenharmony_ci		vosel, vosel_mask, voselon, vosel_ctrl)			\
4962306a36Sopenharmony_ci[MT6332_ID_##vreg] = {							\
5062306a36Sopenharmony_ci	.desc = {							\
5162306a36Sopenharmony_ci		.name = #vreg,						\
5262306a36Sopenharmony_ci		.of_match = of_match_ptr(match),			\
5362306a36Sopenharmony_ci		.ops = &mt6332_buck_volt_range_ops,			\
5462306a36Sopenharmony_ci		.type = REGULATOR_VOLTAGE,				\
5562306a36Sopenharmony_ci		.id = MT6332_ID_##vreg,					\
5662306a36Sopenharmony_ci		.owner = THIS_MODULE,					\
5762306a36Sopenharmony_ci		.n_voltages = (max - min)/step + 1,			\
5862306a36Sopenharmony_ci		.linear_ranges = volt_ranges,				\
5962306a36Sopenharmony_ci		.n_linear_ranges = ARRAY_SIZE(volt_ranges),		\
6062306a36Sopenharmony_ci		.vsel_reg = vosel,					\
6162306a36Sopenharmony_ci		.vsel_mask = vosel_mask,				\
6262306a36Sopenharmony_ci		.enable_reg = enreg,					\
6362306a36Sopenharmony_ci		.enable_mask = BIT(0),					\
6462306a36Sopenharmony_ci	},								\
6562306a36Sopenharmony_ci	.qi = BIT(13),							\
6662306a36Sopenharmony_ci	.vselon_reg = voselon,						\
6762306a36Sopenharmony_ci	.vselctrl_reg = vosel_ctrl,					\
6862306a36Sopenharmony_ci	.vselctrl_mask = BIT(1),					\
6962306a36Sopenharmony_ci	.status_mask = 0,						\
7062306a36Sopenharmony_ci}
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ci#define MT6332_LDO_LINEAR(match, vreg, min, max, step, volt_ranges,	\
7362306a36Sopenharmony_ci			  enreg, vosel, vosel_mask, voselon,		\
7462306a36Sopenharmony_ci			  vosel_ctrl, _modeset_reg, _modeset_mask)	\
7562306a36Sopenharmony_ci[MT6332_ID_##vreg] = {							\
7662306a36Sopenharmony_ci	.desc = {							\
7762306a36Sopenharmony_ci		.name = #vreg,						\
7862306a36Sopenharmony_ci		.of_match = of_match_ptr(match),			\
7962306a36Sopenharmony_ci		.ops = &mt6332_ldo_volt_range_ops,			\
8062306a36Sopenharmony_ci		.type = REGULATOR_VOLTAGE,				\
8162306a36Sopenharmony_ci		.id = MT6332_ID_##vreg,					\
8262306a36Sopenharmony_ci		.owner = THIS_MODULE,					\
8362306a36Sopenharmony_ci		.n_voltages = (max - min)/step + 1,			\
8462306a36Sopenharmony_ci		.linear_ranges = volt_ranges,				\
8562306a36Sopenharmony_ci		.n_linear_ranges = ARRAY_SIZE(volt_ranges),		\
8662306a36Sopenharmony_ci		.vsel_reg = vosel,					\
8762306a36Sopenharmony_ci		.vsel_mask = vosel_mask,				\
8862306a36Sopenharmony_ci		.enable_reg = enreg,					\
8962306a36Sopenharmony_ci		.enable_mask = BIT(0),					\
9062306a36Sopenharmony_ci	},								\
9162306a36Sopenharmony_ci	.qi = BIT(15),							\
9262306a36Sopenharmony_ci	.vselon_reg = voselon,						\
9362306a36Sopenharmony_ci	.vselctrl_reg = vosel_ctrl,					\
9462306a36Sopenharmony_ci	.vselctrl_mask = BIT(1),					\
9562306a36Sopenharmony_ci	.modeset_reg = _modeset_reg,					\
9662306a36Sopenharmony_ci	.modeset_mask = _modeset_mask,					\
9762306a36Sopenharmony_ci	.status_mask = 0,						\
9862306a36Sopenharmony_ci}
9962306a36Sopenharmony_ci
10062306a36Sopenharmony_ci#define MT6332_LDO_AO(match, vreg, ldo_volt_table, vosel, vosel_mask)	\
10162306a36Sopenharmony_ci[MT6332_ID_##vreg] = {							\
10262306a36Sopenharmony_ci	.desc = {							\
10362306a36Sopenharmony_ci		.name = #vreg,						\
10462306a36Sopenharmony_ci		.of_match = of_match_ptr(match),			\
10562306a36Sopenharmony_ci		.ops = &mt6332_volt_table_ao_ops,			\
10662306a36Sopenharmony_ci		.type = REGULATOR_VOLTAGE,				\
10762306a36Sopenharmony_ci		.id = MT6332_ID_##vreg,					\
10862306a36Sopenharmony_ci		.owner = THIS_MODULE,					\
10962306a36Sopenharmony_ci		.n_voltages = ARRAY_SIZE(ldo_volt_table),		\
11062306a36Sopenharmony_ci		.volt_table = ldo_volt_table,				\
11162306a36Sopenharmony_ci		.vsel_reg = vosel,					\
11262306a36Sopenharmony_ci		.vsel_mask = vosel_mask,				\
11362306a36Sopenharmony_ci	},								\
11462306a36Sopenharmony_ci}
11562306a36Sopenharmony_ci
11662306a36Sopenharmony_ci#define MT6332_LDO(match, vreg, ldo_volt_table, enreg, enbit, vosel,	\
11762306a36Sopenharmony_ci		   vosel_mask, _modeset_reg, _modeset_mask)		\
11862306a36Sopenharmony_ci[MT6332_ID_##vreg] = {							\
11962306a36Sopenharmony_ci	.desc = {							\
12062306a36Sopenharmony_ci		.name = #vreg,						\
12162306a36Sopenharmony_ci		.of_match = of_match_ptr(match),			\
12262306a36Sopenharmony_ci		.ops = &mt6332_volt_table_ops,				\
12362306a36Sopenharmony_ci		.type = REGULATOR_VOLTAGE,				\
12462306a36Sopenharmony_ci		.id = MT6332_ID_##vreg,					\
12562306a36Sopenharmony_ci		.owner = THIS_MODULE,					\
12662306a36Sopenharmony_ci		.n_voltages = ARRAY_SIZE(ldo_volt_table),		\
12762306a36Sopenharmony_ci		.volt_table = ldo_volt_table,				\
12862306a36Sopenharmony_ci		.vsel_reg = vosel,					\
12962306a36Sopenharmony_ci		.vsel_mask = vosel_mask,				\
13062306a36Sopenharmony_ci		.enable_reg = enreg,					\
13162306a36Sopenharmony_ci		.enable_mask = BIT(enbit),				\
13262306a36Sopenharmony_ci	},								\
13362306a36Sopenharmony_ci	.qi = BIT(15),							\
13462306a36Sopenharmony_ci	.modeset_reg = _modeset_reg,					\
13562306a36Sopenharmony_ci	.modeset_mask = _modeset_mask,					\
13662306a36Sopenharmony_ci	.status_mask = 0,						\
13762306a36Sopenharmony_ci}
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_ci#define MT6332_REG_FIXED(match, vreg, enreg, enbit, qibit, volt, stbit)	\
14062306a36Sopenharmony_ci[MT6332_ID_##vreg] = {							\
14162306a36Sopenharmony_ci	.desc = {							\
14262306a36Sopenharmony_ci		.name = #vreg,						\
14362306a36Sopenharmony_ci		.of_match = of_match_ptr(match),			\
14462306a36Sopenharmony_ci		.ops = &mt6332_volt_fixed_ops,				\
14562306a36Sopenharmony_ci		.type = REGULATOR_VOLTAGE,				\
14662306a36Sopenharmony_ci		.id = MT6332_ID_##vreg,					\
14762306a36Sopenharmony_ci		.owner = THIS_MODULE,					\
14862306a36Sopenharmony_ci		.n_voltages = 1,					\
14962306a36Sopenharmony_ci		.enable_reg = enreg,					\
15062306a36Sopenharmony_ci		.enable_mask = BIT(enbit),				\
15162306a36Sopenharmony_ci		.min_uV = volt,						\
15262306a36Sopenharmony_ci	},								\
15362306a36Sopenharmony_ci	.qi = BIT(qibit),						\
15462306a36Sopenharmony_ci	.status_reg = MT6332_EN_STATUS0,				\
15562306a36Sopenharmony_ci	.status_mask = BIT(stbit),					\
15662306a36Sopenharmony_ci}
15762306a36Sopenharmony_ci
15862306a36Sopenharmony_cistatic const struct linear_range boost_volt_range[] = {
15962306a36Sopenharmony_ci	REGULATOR_LINEAR_RANGE(3500000, 0, 0x7f, 31250),
16062306a36Sopenharmony_ci};
16162306a36Sopenharmony_ci
16262306a36Sopenharmony_cistatic const struct linear_range buck_volt_range[] = {
16362306a36Sopenharmony_ci	REGULATOR_LINEAR_RANGE(700000, 0, 0x7f, 6250),
16462306a36Sopenharmony_ci};
16562306a36Sopenharmony_ci
16662306a36Sopenharmony_cistatic const struct linear_range buck_pa_volt_range[] = {
16762306a36Sopenharmony_ci	REGULATOR_LINEAR_RANGE(500000, 0, 0x3f, 50000),
16862306a36Sopenharmony_ci};
16962306a36Sopenharmony_ci
17062306a36Sopenharmony_cistatic const struct linear_range buck_rf_volt_range[] = {
17162306a36Sopenharmony_ci	REGULATOR_LINEAR_RANGE(1050000, 0, 0x7f, 9375),
17262306a36Sopenharmony_ci};
17362306a36Sopenharmony_ci
17462306a36Sopenharmony_cistatic const unsigned int ldo_volt_table1[] = {
17562306a36Sopenharmony_ci	2800000, 3000000, 0, 3200000
17662306a36Sopenharmony_ci};
17762306a36Sopenharmony_ci
17862306a36Sopenharmony_cistatic const unsigned int ldo_volt_table2[] = {
17962306a36Sopenharmony_ci	1200000, 1300000, 1400000, 1500000, 1600000, 1700000, 1800000, 1800000,
18062306a36Sopenharmony_ci};
18162306a36Sopenharmony_ci
18262306a36Sopenharmony_cistatic int mt6332_get_status(struct regulator_dev *rdev)
18362306a36Sopenharmony_ci{
18462306a36Sopenharmony_ci	struct mt6332_regulator_info *info = rdev_get_drvdata(rdev);
18562306a36Sopenharmony_ci	u32 reg, en_mask, regval;
18662306a36Sopenharmony_ci	int ret;
18762306a36Sopenharmony_ci
18862306a36Sopenharmony_ci	if (info->qi > 0) {
18962306a36Sopenharmony_ci		reg = info->desc.enable_reg;
19062306a36Sopenharmony_ci		en_mask = info->qi;
19162306a36Sopenharmony_ci	} else {
19262306a36Sopenharmony_ci		reg = info->status_reg;
19362306a36Sopenharmony_ci		en_mask = info->status_mask;
19462306a36Sopenharmony_ci	}
19562306a36Sopenharmony_ci
19662306a36Sopenharmony_ci	ret = regmap_read(rdev->regmap, reg, &regval);
19762306a36Sopenharmony_ci	if (ret != 0) {
19862306a36Sopenharmony_ci		dev_err(&rdev->dev, "Failed to get enable reg: %d\n", ret);
19962306a36Sopenharmony_ci		return ret;
20062306a36Sopenharmony_ci	}
20162306a36Sopenharmony_ci
20262306a36Sopenharmony_ci	return (regval & en_mask) ? REGULATOR_STATUS_ON : REGULATOR_STATUS_OFF;
20362306a36Sopenharmony_ci}
20462306a36Sopenharmony_ci
20562306a36Sopenharmony_cistatic int mt6332_ldo_set_mode(struct regulator_dev *rdev, unsigned int mode)
20662306a36Sopenharmony_ci{
20762306a36Sopenharmony_ci	struct mt6332_regulator_info *info = rdev_get_drvdata(rdev);
20862306a36Sopenharmony_ci	int val;
20962306a36Sopenharmony_ci
21062306a36Sopenharmony_ci	switch (mode) {
21162306a36Sopenharmony_ci	case REGULATOR_MODE_STANDBY:
21262306a36Sopenharmony_ci		val = MT6332_LDO_MODE_LP;
21362306a36Sopenharmony_ci		break;
21462306a36Sopenharmony_ci	case REGULATOR_MODE_NORMAL:
21562306a36Sopenharmony_ci		val = MT6332_LDO_MODE_NORMAL;
21662306a36Sopenharmony_ci		break;
21762306a36Sopenharmony_ci	default:
21862306a36Sopenharmony_ci		return -EINVAL;
21962306a36Sopenharmony_ci	}
22062306a36Sopenharmony_ci
22162306a36Sopenharmony_ci	val <<= ffs(info->modeset_mask) - 1;
22262306a36Sopenharmony_ci
22362306a36Sopenharmony_ci	return regmap_update_bits(rdev->regmap, info->modeset_reg,
22462306a36Sopenharmony_ci				  info->modeset_mask, val);
22562306a36Sopenharmony_ci}
22662306a36Sopenharmony_ci
22762306a36Sopenharmony_cistatic unsigned int mt6332_ldo_get_mode(struct regulator_dev *rdev)
22862306a36Sopenharmony_ci{
22962306a36Sopenharmony_ci	struct mt6332_regulator_info *info = rdev_get_drvdata(rdev);
23062306a36Sopenharmony_ci	unsigned int val;
23162306a36Sopenharmony_ci	int ret;
23262306a36Sopenharmony_ci
23362306a36Sopenharmony_ci	ret = regmap_read(rdev->regmap, info->modeset_reg, &val);
23462306a36Sopenharmony_ci	if (ret < 0)
23562306a36Sopenharmony_ci		return ret;
23662306a36Sopenharmony_ci
23762306a36Sopenharmony_ci	val &= info->modeset_mask;
23862306a36Sopenharmony_ci	val >>= ffs(info->modeset_mask) - 1;
23962306a36Sopenharmony_ci
24062306a36Sopenharmony_ci	return (val & BIT(0)) ? REGULATOR_MODE_STANDBY : REGULATOR_MODE_NORMAL;
24162306a36Sopenharmony_ci}
24262306a36Sopenharmony_ci
24362306a36Sopenharmony_cistatic const struct regulator_ops mt6332_buck_volt_range_ops = {
24462306a36Sopenharmony_ci	.list_voltage = regulator_list_voltage_linear_range,
24562306a36Sopenharmony_ci	.map_voltage = regulator_map_voltage_linear_range,
24662306a36Sopenharmony_ci	.set_voltage_sel = regulator_set_voltage_sel_regmap,
24762306a36Sopenharmony_ci	.get_voltage_sel = regulator_get_voltage_sel_regmap,
24862306a36Sopenharmony_ci	.set_voltage_time_sel = regulator_set_voltage_time_sel,
24962306a36Sopenharmony_ci	.enable = regulator_enable_regmap,
25062306a36Sopenharmony_ci	.disable = regulator_disable_regmap,
25162306a36Sopenharmony_ci	.is_enabled = regulator_is_enabled_regmap,
25262306a36Sopenharmony_ci	.get_status = mt6332_get_status,
25362306a36Sopenharmony_ci};
25462306a36Sopenharmony_ci
25562306a36Sopenharmony_cistatic const struct regulator_ops mt6332_ldo_volt_range_ops = {
25662306a36Sopenharmony_ci	.list_voltage = regulator_list_voltage_linear_range,
25762306a36Sopenharmony_ci	.map_voltage = regulator_map_voltage_linear_range,
25862306a36Sopenharmony_ci	.set_voltage_sel = regulator_set_voltage_sel_regmap,
25962306a36Sopenharmony_ci	.get_voltage_sel = regulator_get_voltage_sel_regmap,
26062306a36Sopenharmony_ci	.set_voltage_time_sel = regulator_set_voltage_time_sel,
26162306a36Sopenharmony_ci	.enable = regulator_enable_regmap,
26262306a36Sopenharmony_ci	.disable = regulator_disable_regmap,
26362306a36Sopenharmony_ci	.is_enabled = regulator_is_enabled_regmap,
26462306a36Sopenharmony_ci	.get_status = mt6332_get_status,
26562306a36Sopenharmony_ci	.set_mode = mt6332_ldo_set_mode,
26662306a36Sopenharmony_ci	.get_mode = mt6332_ldo_get_mode,
26762306a36Sopenharmony_ci};
26862306a36Sopenharmony_ci
26962306a36Sopenharmony_cistatic const struct regulator_ops mt6332_volt_table_ops = {
27062306a36Sopenharmony_ci	.list_voltage = regulator_list_voltage_table,
27162306a36Sopenharmony_ci	.map_voltage = regulator_map_voltage_iterate,
27262306a36Sopenharmony_ci	.set_voltage_sel = regulator_set_voltage_sel_regmap,
27362306a36Sopenharmony_ci	.get_voltage_sel = regulator_get_voltage_sel_regmap,
27462306a36Sopenharmony_ci	.set_voltage_time_sel = regulator_set_voltage_time_sel,
27562306a36Sopenharmony_ci	.enable = regulator_enable_regmap,
27662306a36Sopenharmony_ci	.disable = regulator_disable_regmap,
27762306a36Sopenharmony_ci	.is_enabled = regulator_is_enabled_regmap,
27862306a36Sopenharmony_ci	.get_status = mt6332_get_status,
27962306a36Sopenharmony_ci	.set_mode = mt6332_ldo_set_mode,
28062306a36Sopenharmony_ci	.get_mode = mt6332_ldo_get_mode,
28162306a36Sopenharmony_ci};
28262306a36Sopenharmony_ci
28362306a36Sopenharmony_cistatic const struct regulator_ops mt6332_volt_table_ao_ops = {
28462306a36Sopenharmony_ci	.list_voltage = regulator_list_voltage_table,
28562306a36Sopenharmony_ci	.map_voltage = regulator_map_voltage_iterate,
28662306a36Sopenharmony_ci	.set_voltage_sel = regulator_set_voltage_sel_regmap,
28762306a36Sopenharmony_ci	.get_voltage_sel = regulator_get_voltage_sel_regmap,
28862306a36Sopenharmony_ci	.set_voltage_time_sel = regulator_set_voltage_time_sel,
28962306a36Sopenharmony_ci};
29062306a36Sopenharmony_ci
29162306a36Sopenharmony_cistatic const struct regulator_ops mt6332_volt_fixed_ops = {
29262306a36Sopenharmony_ci	.list_voltage = regulator_list_voltage_linear,
29362306a36Sopenharmony_ci	.enable = regulator_enable_regmap,
29462306a36Sopenharmony_ci	.disable = regulator_disable_regmap,
29562306a36Sopenharmony_ci	.is_enabled = regulator_is_enabled_regmap,
29662306a36Sopenharmony_ci	.get_status = mt6332_get_status,
29762306a36Sopenharmony_ci};
29862306a36Sopenharmony_ci
29962306a36Sopenharmony_ci/* The array is indexed by id(MT6332_ID_XXX) */
30062306a36Sopenharmony_cistatic struct mt6332_regulator_info mt6332_regulators[] = {
30162306a36Sopenharmony_ci	MT6332_BUCK("buck-vdram", VDRAM, 700000, 1493750, 6250, buck_volt_range,
30262306a36Sopenharmony_ci		    MT6332_EN_STATUS0, MT6332_VDRAM_CON11, GENMASK(6, 0),
30362306a36Sopenharmony_ci		    MT6332_VDRAM_CON12, MT6332_VDRAM_CON7),
30462306a36Sopenharmony_ci	MT6332_BUCK("buck-vdvfs2", VDVFS2, 700000, 1312500, 6250, buck_volt_range,
30562306a36Sopenharmony_ci		    MT6332_VDVFS2_CON9, MT6332_VDVFS2_CON11, GENMASK(6, 0),
30662306a36Sopenharmony_ci		    MT6332_VDVFS2_CON12, MT6332_VDVFS2_CON7),
30762306a36Sopenharmony_ci	MT6332_BUCK("buck-vpa", VPA, 500000, 3400000, 50000, buck_pa_volt_range,
30862306a36Sopenharmony_ci		    MT6332_VPA_CON9, MT6332_VPA_CON11, GENMASK(5, 0),
30962306a36Sopenharmony_ci		    MT6332_VPA_CON12, MT6332_VPA_CON7),
31062306a36Sopenharmony_ci	MT6332_BUCK("buck-vrf18a", VRF1, 1050000, 2240625, 9375, buck_rf_volt_range,
31162306a36Sopenharmony_ci		    MT6332_VRF1_CON9, MT6332_VRF1_CON11, GENMASK(6, 0),
31262306a36Sopenharmony_ci		    MT6332_VRF1_CON12, MT6332_VRF1_CON7),
31362306a36Sopenharmony_ci	MT6332_BUCK("buck-vrf18b", VRF2, 1050000, 2240625, 9375, buck_rf_volt_range,
31462306a36Sopenharmony_ci		    MT6332_VRF2_CON9, MT6332_VRF2_CON11, GENMASK(6, 0),
31562306a36Sopenharmony_ci		    MT6332_VRF2_CON12, MT6332_VRF2_CON7),
31662306a36Sopenharmony_ci	MT6332_BUCK("buck-vsbst", VSBST, 3500000, 7468750, 31250, boost_volt_range,
31762306a36Sopenharmony_ci		    MT6332_VSBST_CON8, MT6332_VSBST_CON12, GENMASK(6, 0),
31862306a36Sopenharmony_ci		    MT6332_VSBST_CON13, MT6332_VSBST_CON8),
31962306a36Sopenharmony_ci	MT6332_LDO("ldo-vauxb32", VAUXB32, ldo_volt_table1, MT6332_LDO_CON1, 10,
32062306a36Sopenharmony_ci		   MT6332_LDO_CON9, GENMASK(6, 5), MT6332_LDO_CON1, GENMASK(1, 0)),
32162306a36Sopenharmony_ci	MT6332_REG_FIXED("ldo-vbif28", VBIF28, MT6332_LDO_CON2, 10, 0, 2800000, 1),
32262306a36Sopenharmony_ci	MT6332_REG_FIXED("ldo-vusb33", VUSB33, MT6332_LDO_CON3, 10, 0, 3300000, 2),
32362306a36Sopenharmony_ci	MT6332_LDO_LINEAR("ldo-vsram", VSRAM_DVFS2, 700000, 1493750, 6250, buck_volt_range,
32462306a36Sopenharmony_ci			  MT6332_EN_STATUS0, MT6332_LDO_CON8, GENMASK(15, 9),
32562306a36Sopenharmony_ci			  MT6332_VDVFS2_CON23, MT6332_VDVFS2_CON22,
32662306a36Sopenharmony_ci			  MT6332_LDO_CON5, GENMASK(1, 0)),
32762306a36Sopenharmony_ci	MT6332_LDO_AO("ldo-vdig18", VDIG18, ldo_volt_table2, MT6332_LDO_CON12, GENMASK(11, 9)),
32862306a36Sopenharmony_ci};
32962306a36Sopenharmony_ci
33062306a36Sopenharmony_cistatic int mt6332_set_buck_vosel_reg(struct platform_device *pdev)
33162306a36Sopenharmony_ci{
33262306a36Sopenharmony_ci	struct mt6397_chip *mt6332 = dev_get_drvdata(pdev->dev.parent);
33362306a36Sopenharmony_ci	int i;
33462306a36Sopenharmony_ci	u32 regval;
33562306a36Sopenharmony_ci
33662306a36Sopenharmony_ci	for (i = 0; i < MT6332_ID_VREG_MAX; i++) {
33762306a36Sopenharmony_ci		if (mt6332_regulators[i].vselctrl_reg) {
33862306a36Sopenharmony_ci			if (regmap_read(mt6332->regmap,
33962306a36Sopenharmony_ci				mt6332_regulators[i].vselctrl_reg,
34062306a36Sopenharmony_ci				&regval) < 0) {
34162306a36Sopenharmony_ci				dev_err(&pdev->dev,
34262306a36Sopenharmony_ci					"Failed to read buck ctrl\n");
34362306a36Sopenharmony_ci				return -EIO;
34462306a36Sopenharmony_ci			}
34562306a36Sopenharmony_ci
34662306a36Sopenharmony_ci			if (regval & mt6332_regulators[i].vselctrl_mask) {
34762306a36Sopenharmony_ci				mt6332_regulators[i].desc.vsel_reg =
34862306a36Sopenharmony_ci				mt6332_regulators[i].vselon_reg;
34962306a36Sopenharmony_ci			}
35062306a36Sopenharmony_ci		}
35162306a36Sopenharmony_ci	}
35262306a36Sopenharmony_ci
35362306a36Sopenharmony_ci	return 0;
35462306a36Sopenharmony_ci}
35562306a36Sopenharmony_ci
35662306a36Sopenharmony_cistatic int mt6332_regulator_probe(struct platform_device *pdev)
35762306a36Sopenharmony_ci{
35862306a36Sopenharmony_ci	struct mt6397_chip *mt6332 = dev_get_drvdata(pdev->dev.parent);
35962306a36Sopenharmony_ci	struct regulator_config config = {};
36062306a36Sopenharmony_ci	struct regulator_dev *rdev;
36162306a36Sopenharmony_ci	int i;
36262306a36Sopenharmony_ci	u32 reg_value;
36362306a36Sopenharmony_ci
36462306a36Sopenharmony_ci	/* Query buck controller to select activated voltage register part */
36562306a36Sopenharmony_ci	if (mt6332_set_buck_vosel_reg(pdev))
36662306a36Sopenharmony_ci		return -EIO;
36762306a36Sopenharmony_ci
36862306a36Sopenharmony_ci	/* Read PMIC chip revision to update constraints and voltage table */
36962306a36Sopenharmony_ci	if (regmap_read(mt6332->regmap, MT6332_HWCID, &reg_value) < 0) {
37062306a36Sopenharmony_ci		dev_err(&pdev->dev, "Failed to read Chip ID\n");
37162306a36Sopenharmony_ci		return -EIO;
37262306a36Sopenharmony_ci	}
37362306a36Sopenharmony_ci	reg_value &= GENMASK(7, 0);
37462306a36Sopenharmony_ci
37562306a36Sopenharmony_ci	dev_info(&pdev->dev, "Chip ID = 0x%x\n", reg_value);
37662306a36Sopenharmony_ci
37762306a36Sopenharmony_ci	/*
37862306a36Sopenharmony_ci	 * ChipID 0x10 is "MT6332 E1", has a different voltage table and
37962306a36Sopenharmony_ci	 * it's currently not supported in this driver. Upon detection of
38062306a36Sopenharmony_ci	 * this ID, refuse to register the regulators, as we will wrongly
38162306a36Sopenharmony_ci	 * interpret the VSEL for this revision, potentially overvolting
38262306a36Sopenharmony_ci	 * some device.
38362306a36Sopenharmony_ci	 */
38462306a36Sopenharmony_ci	if (reg_value == 0x10) {
38562306a36Sopenharmony_ci		dev_err(&pdev->dev, "Chip version not supported. Bailing out.\n");
38662306a36Sopenharmony_ci		return -EINVAL;
38762306a36Sopenharmony_ci	}
38862306a36Sopenharmony_ci
38962306a36Sopenharmony_ci	for (i = 0; i < MT6332_ID_VREG_MAX; i++) {
39062306a36Sopenharmony_ci		config.dev = &pdev->dev;
39162306a36Sopenharmony_ci		config.driver_data = &mt6332_regulators[i];
39262306a36Sopenharmony_ci		config.regmap = mt6332->regmap;
39362306a36Sopenharmony_ci		rdev = devm_regulator_register(&pdev->dev,
39462306a36Sopenharmony_ci				&mt6332_regulators[i].desc, &config);
39562306a36Sopenharmony_ci		if (IS_ERR(rdev)) {
39662306a36Sopenharmony_ci			dev_err(&pdev->dev, "failed to register %s\n",
39762306a36Sopenharmony_ci				mt6332_regulators[i].desc.name);
39862306a36Sopenharmony_ci			return PTR_ERR(rdev);
39962306a36Sopenharmony_ci		}
40062306a36Sopenharmony_ci	}
40162306a36Sopenharmony_ci	return 0;
40262306a36Sopenharmony_ci}
40362306a36Sopenharmony_ci
40462306a36Sopenharmony_cistatic const struct platform_device_id mt6332_platform_ids[] = {
40562306a36Sopenharmony_ci	{"mt6332-regulator", 0},
40662306a36Sopenharmony_ci	{ /* sentinel */ },
40762306a36Sopenharmony_ci};
40862306a36Sopenharmony_ciMODULE_DEVICE_TABLE(platform, mt6332_platform_ids);
40962306a36Sopenharmony_ci
41062306a36Sopenharmony_cistatic struct platform_driver mt6332_regulator_driver = {
41162306a36Sopenharmony_ci	.driver = {
41262306a36Sopenharmony_ci		.name = "mt6332-regulator",
41362306a36Sopenharmony_ci		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
41462306a36Sopenharmony_ci	},
41562306a36Sopenharmony_ci	.probe = mt6332_regulator_probe,
41662306a36Sopenharmony_ci	.id_table = mt6332_platform_ids,
41762306a36Sopenharmony_ci};
41862306a36Sopenharmony_ci
41962306a36Sopenharmony_cimodule_platform_driver(mt6332_regulator_driver);
42062306a36Sopenharmony_ci
42162306a36Sopenharmony_ciMODULE_AUTHOR("AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com>");
42262306a36Sopenharmony_ciMODULE_DESCRIPTION("Regulator Driver for MediaTek MT6332 PMIC");
42362306a36Sopenharmony_ciMODULE_LICENSE("GPL");
424