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/mt6331/registers.h>
1662306a36Sopenharmony_ci#include <linux/regulator/driver.h>
1762306a36Sopenharmony_ci#include <linux/regulator/machine.h>
1862306a36Sopenharmony_ci#include <linux/regulator/mt6331-regulator.h>
1962306a36Sopenharmony_ci#include <linux/regulator/of_regulator.h>
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_ci#define MT6331_LDO_MODE_NORMAL	0
2262306a36Sopenharmony_ci#define MT6331_LDO_MODE_LP	1
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ci/*
2562306a36Sopenharmony_ci * MT6331 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 mt6331_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 MT6331_BUCK(match, vreg, min, max, step, volt_ranges, enreg,	\
4862306a36Sopenharmony_ci		vosel, vosel_mask, voselon, vosel_ctrl)			\
4962306a36Sopenharmony_ci[MT6331_ID_##vreg] = {							\
5062306a36Sopenharmony_ci	.desc = {							\
5162306a36Sopenharmony_ci		.name = #vreg,						\
5262306a36Sopenharmony_ci		.of_match = of_match_ptr(match),			\
5362306a36Sopenharmony_ci		.ops = &mt6331_volt_range_ops,				\
5462306a36Sopenharmony_ci		.type = REGULATOR_VOLTAGE,				\
5562306a36Sopenharmony_ci		.id = MT6331_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 MT6331_LDO_AO(match, vreg, ldo_volt_table, vosel, vosel_mask)	\
7362306a36Sopenharmony_ci[MT6331_ID_##vreg] = {							\
7462306a36Sopenharmony_ci	.desc = {							\
7562306a36Sopenharmony_ci		.name = #vreg,						\
7662306a36Sopenharmony_ci		.of_match = of_match_ptr(match),			\
7762306a36Sopenharmony_ci		.ops = &mt6331_volt_table_ao_ops,			\
7862306a36Sopenharmony_ci		.type = REGULATOR_VOLTAGE,				\
7962306a36Sopenharmony_ci		.id = MT6331_ID_##vreg,					\
8062306a36Sopenharmony_ci		.owner = THIS_MODULE,					\
8162306a36Sopenharmony_ci		.n_voltages = ARRAY_SIZE(ldo_volt_table),		\
8262306a36Sopenharmony_ci		.volt_table = ldo_volt_table,				\
8362306a36Sopenharmony_ci		.vsel_reg = vosel,					\
8462306a36Sopenharmony_ci		.vsel_mask = vosel_mask,				\
8562306a36Sopenharmony_ci	},								\
8662306a36Sopenharmony_ci}
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ci#define MT6331_LDO_S(match, vreg, ldo_volt_table, enreg, enbit, vosel,	\
8962306a36Sopenharmony_ci		     vosel_mask, _modeset_reg, _modeset_mask,		\
9062306a36Sopenharmony_ci		     _status_reg, _status_mask)				\
9162306a36Sopenharmony_ci[MT6331_ID_##vreg] = {							\
9262306a36Sopenharmony_ci	.desc = {							\
9362306a36Sopenharmony_ci		.name = #vreg,						\
9462306a36Sopenharmony_ci		.of_match = of_match_ptr(match),			\
9562306a36Sopenharmony_ci		.ops = &mt6331_volt_table_no_qi_ops,			\
9662306a36Sopenharmony_ci		.type = REGULATOR_VOLTAGE,				\
9762306a36Sopenharmony_ci		.id = MT6331_ID_##vreg,					\
9862306a36Sopenharmony_ci		.owner = THIS_MODULE,					\
9962306a36Sopenharmony_ci		.n_voltages = ARRAY_SIZE(ldo_volt_table),		\
10062306a36Sopenharmony_ci		.volt_table = ldo_volt_table,				\
10162306a36Sopenharmony_ci		.vsel_reg = vosel,					\
10262306a36Sopenharmony_ci		.vsel_mask = vosel_mask,				\
10362306a36Sopenharmony_ci		.enable_reg = enreg,					\
10462306a36Sopenharmony_ci		.enable_mask = BIT(enbit),				\
10562306a36Sopenharmony_ci	},								\
10662306a36Sopenharmony_ci	.modeset_reg = _modeset_reg,					\
10762306a36Sopenharmony_ci	.modeset_mask = _modeset_mask,					\
10862306a36Sopenharmony_ci	.status_reg = _status_reg,					\
10962306a36Sopenharmony_ci	.status_mask = _status_mask,					\
11062306a36Sopenharmony_ci}
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_ci#define MT6331_LDO(match, vreg, ldo_volt_table, enreg, enbit, vosel,	\
11362306a36Sopenharmony_ci		   vosel_mask, _modeset_reg, _modeset_mask)		\
11462306a36Sopenharmony_ci[MT6331_ID_##vreg] = {							\
11562306a36Sopenharmony_ci	.desc = {							\
11662306a36Sopenharmony_ci		.name = #vreg,						\
11762306a36Sopenharmony_ci		.of_match = of_match_ptr(match),			\
11862306a36Sopenharmony_ci		.ops = (_modeset_reg ?					\
11962306a36Sopenharmony_ci			&mt6331_volt_table_ops :			\
12062306a36Sopenharmony_ci			&mt6331_volt_table_no_ms_ops),			\
12162306a36Sopenharmony_ci		.type = REGULATOR_VOLTAGE,				\
12262306a36Sopenharmony_ci		.id = MT6331_ID_##vreg,					\
12362306a36Sopenharmony_ci		.owner = THIS_MODULE,					\
12462306a36Sopenharmony_ci		.n_voltages = ARRAY_SIZE(ldo_volt_table),		\
12562306a36Sopenharmony_ci		.volt_table = ldo_volt_table,				\
12662306a36Sopenharmony_ci		.vsel_reg = vosel,					\
12762306a36Sopenharmony_ci		.vsel_mask = vosel_mask,				\
12862306a36Sopenharmony_ci		.enable_reg = enreg,					\
12962306a36Sopenharmony_ci		.enable_mask = BIT(enbit),				\
13062306a36Sopenharmony_ci	},								\
13162306a36Sopenharmony_ci	.qi = BIT(15),							\
13262306a36Sopenharmony_ci	.modeset_reg = _modeset_reg,					\
13362306a36Sopenharmony_ci	.modeset_mask = _modeset_mask,					\
13462306a36Sopenharmony_ci}
13562306a36Sopenharmony_ci
13662306a36Sopenharmony_ci#define MT6331_REG_FIXED(match, vreg, enreg, enbit, qibit, volt,	\
13762306a36Sopenharmony_ci			 _modeset_reg, _modeset_mask)			\
13862306a36Sopenharmony_ci[MT6331_ID_##vreg] = {							\
13962306a36Sopenharmony_ci	.desc = {							\
14062306a36Sopenharmony_ci		.name = #vreg,						\
14162306a36Sopenharmony_ci		.of_match = of_match_ptr(match),			\
14262306a36Sopenharmony_ci		.ops = (_modeset_reg ?					\
14362306a36Sopenharmony_ci			&mt6331_volt_fixed_ops :			\
14462306a36Sopenharmony_ci			&mt6331_volt_fixed_no_ms_ops),			\
14562306a36Sopenharmony_ci		.type = REGULATOR_VOLTAGE,				\
14662306a36Sopenharmony_ci		.id = MT6331_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	.modeset_reg = _modeset_reg,					\
15562306a36Sopenharmony_ci	.modeset_mask = _modeset_mask,					\
15662306a36Sopenharmony_ci}
15762306a36Sopenharmony_ci
15862306a36Sopenharmony_cistatic const struct linear_range buck_volt_range[] = {
15962306a36Sopenharmony_ci	REGULATOR_LINEAR_RANGE(700000, 0, 0x7f, 6250),
16062306a36Sopenharmony_ci};
16162306a36Sopenharmony_ci
16262306a36Sopenharmony_cistatic const unsigned int ldo_volt_table1[] = {
16362306a36Sopenharmony_ci	2800000, 3000000, 0, 3200000
16462306a36Sopenharmony_ci};
16562306a36Sopenharmony_ci
16662306a36Sopenharmony_cistatic const unsigned int ldo_volt_table2[] = {
16762306a36Sopenharmony_ci	1500000, 1800000, 2500000, 2800000,
16862306a36Sopenharmony_ci};
16962306a36Sopenharmony_ci
17062306a36Sopenharmony_cistatic const unsigned int ldo_volt_table3[] = {
17162306a36Sopenharmony_ci	1200000, 1300000, 1500000, 1800000, 2000000, 2800000, 3000000, 3300000,
17262306a36Sopenharmony_ci};
17362306a36Sopenharmony_ci
17462306a36Sopenharmony_cistatic const unsigned int ldo_volt_table4[] = {
17562306a36Sopenharmony_ci	0, 0, 1700000, 1800000, 1860000, 2760000, 3000000, 3100000,
17662306a36Sopenharmony_ci};
17762306a36Sopenharmony_ci
17862306a36Sopenharmony_cistatic const unsigned int ldo_volt_table5[] = {
17962306a36Sopenharmony_ci	1800000, 3300000, 1800000, 3300000,
18062306a36Sopenharmony_ci};
18162306a36Sopenharmony_ci
18262306a36Sopenharmony_cistatic const unsigned int ldo_volt_table6[] = {
18362306a36Sopenharmony_ci	3000000, 3300000,
18462306a36Sopenharmony_ci};
18562306a36Sopenharmony_ci
18662306a36Sopenharmony_cistatic const unsigned int ldo_volt_table7[] = {
18762306a36Sopenharmony_ci	1200000, 1600000, 1700000, 1800000, 1900000, 2000000, 2100000, 2200000,
18862306a36Sopenharmony_ci};
18962306a36Sopenharmony_ci
19062306a36Sopenharmony_cistatic const unsigned int ldo_volt_table8[] = {
19162306a36Sopenharmony_ci	900000, 1000000, 1100000, 1220000, 1300000, 1500000, 1500000, 1500000,
19262306a36Sopenharmony_ci};
19362306a36Sopenharmony_ci
19462306a36Sopenharmony_cistatic const unsigned int ldo_volt_table9[] = {
19562306a36Sopenharmony_ci	1000000, 1050000, 1100000, 1150000, 1200000, 1250000, 1300000, 1300000,
19662306a36Sopenharmony_ci};
19762306a36Sopenharmony_ci
19862306a36Sopenharmony_cistatic const unsigned int ldo_volt_table10[] = {
19962306a36Sopenharmony_ci	1200000, 1300000, 1500000, 1800000,
20062306a36Sopenharmony_ci};
20162306a36Sopenharmony_ci
20262306a36Sopenharmony_cistatic const unsigned int ldo_volt_table11[] = {
20362306a36Sopenharmony_ci	1200000, 1300000, 1400000, 1500000, 1600000, 1700000, 1800000, 1800000,
20462306a36Sopenharmony_ci};
20562306a36Sopenharmony_ci
20662306a36Sopenharmony_cistatic int mt6331_get_status(struct regulator_dev *rdev)
20762306a36Sopenharmony_ci{
20862306a36Sopenharmony_ci	struct mt6331_regulator_info *info = rdev_get_drvdata(rdev);
20962306a36Sopenharmony_ci	u32 regval;
21062306a36Sopenharmony_ci	int ret;
21162306a36Sopenharmony_ci
21262306a36Sopenharmony_ci	ret = regmap_read(rdev->regmap, info->desc.enable_reg, &regval);
21362306a36Sopenharmony_ci	if (ret != 0) {
21462306a36Sopenharmony_ci		dev_err(&rdev->dev, "Failed to get enable reg: %d\n", ret);
21562306a36Sopenharmony_ci		return ret;
21662306a36Sopenharmony_ci	}
21762306a36Sopenharmony_ci
21862306a36Sopenharmony_ci	return (regval & info->qi) ? REGULATOR_STATUS_ON : REGULATOR_STATUS_OFF;
21962306a36Sopenharmony_ci}
22062306a36Sopenharmony_ci
22162306a36Sopenharmony_cistatic int mt6331_ldo_set_mode(struct regulator_dev *rdev, unsigned int mode)
22262306a36Sopenharmony_ci{
22362306a36Sopenharmony_ci	struct mt6331_regulator_info *info = rdev_get_drvdata(rdev);
22462306a36Sopenharmony_ci	int val;
22562306a36Sopenharmony_ci
22662306a36Sopenharmony_ci	switch (mode) {
22762306a36Sopenharmony_ci	case REGULATOR_MODE_STANDBY:
22862306a36Sopenharmony_ci		val = MT6331_LDO_MODE_LP;
22962306a36Sopenharmony_ci		break;
23062306a36Sopenharmony_ci	case REGULATOR_MODE_NORMAL:
23162306a36Sopenharmony_ci		val = MT6331_LDO_MODE_NORMAL;
23262306a36Sopenharmony_ci		break;
23362306a36Sopenharmony_ci	default:
23462306a36Sopenharmony_ci		return -EINVAL;
23562306a36Sopenharmony_ci	}
23662306a36Sopenharmony_ci
23762306a36Sopenharmony_ci	val <<= ffs(info->modeset_mask) - 1;
23862306a36Sopenharmony_ci
23962306a36Sopenharmony_ci	return regmap_update_bits(rdev->regmap, info->modeset_reg,
24062306a36Sopenharmony_ci				  info->modeset_mask, val);
24162306a36Sopenharmony_ci}
24262306a36Sopenharmony_ci
24362306a36Sopenharmony_cistatic unsigned int mt6331_ldo_get_mode(struct regulator_dev *rdev)
24462306a36Sopenharmony_ci{
24562306a36Sopenharmony_ci	struct mt6331_regulator_info *info = rdev_get_drvdata(rdev);
24662306a36Sopenharmony_ci	unsigned int val;
24762306a36Sopenharmony_ci	int ret;
24862306a36Sopenharmony_ci
24962306a36Sopenharmony_ci	ret = regmap_read(rdev->regmap, info->modeset_reg, &val);
25062306a36Sopenharmony_ci	if (ret < 0)
25162306a36Sopenharmony_ci		return ret;
25262306a36Sopenharmony_ci
25362306a36Sopenharmony_ci	val &= info->modeset_mask;
25462306a36Sopenharmony_ci	val >>= ffs(info->modeset_mask) - 1;
25562306a36Sopenharmony_ci
25662306a36Sopenharmony_ci	return (val & BIT(0)) ? REGULATOR_MODE_STANDBY : REGULATOR_MODE_NORMAL;
25762306a36Sopenharmony_ci}
25862306a36Sopenharmony_ci
25962306a36Sopenharmony_cistatic const struct regulator_ops mt6331_volt_range_ops = {
26062306a36Sopenharmony_ci	.list_voltage = regulator_list_voltage_linear_range,
26162306a36Sopenharmony_ci	.map_voltage = regulator_map_voltage_linear_range,
26262306a36Sopenharmony_ci	.set_voltage_sel = regulator_set_voltage_sel_regmap,
26362306a36Sopenharmony_ci	.get_voltage_sel = regulator_get_voltage_sel_regmap,
26462306a36Sopenharmony_ci	.set_voltage_time_sel = regulator_set_voltage_time_sel,
26562306a36Sopenharmony_ci	.enable = regulator_enable_regmap,
26662306a36Sopenharmony_ci	.disable = regulator_disable_regmap,
26762306a36Sopenharmony_ci	.is_enabled = regulator_is_enabled_regmap,
26862306a36Sopenharmony_ci	.get_status = mt6331_get_status,
26962306a36Sopenharmony_ci};
27062306a36Sopenharmony_ci
27162306a36Sopenharmony_cistatic const struct regulator_ops mt6331_volt_table_no_ms_ops = {
27262306a36Sopenharmony_ci	.list_voltage = regulator_list_voltage_table,
27362306a36Sopenharmony_ci	.map_voltage = regulator_map_voltage_iterate,
27462306a36Sopenharmony_ci	.set_voltage_sel = regulator_set_voltage_sel_regmap,
27562306a36Sopenharmony_ci	.get_voltage_sel = regulator_get_voltage_sel_regmap,
27662306a36Sopenharmony_ci	.set_voltage_time_sel = regulator_set_voltage_time_sel,
27762306a36Sopenharmony_ci	.enable = regulator_enable_regmap,
27862306a36Sopenharmony_ci	.disable = regulator_disable_regmap,
27962306a36Sopenharmony_ci	.is_enabled = regulator_is_enabled_regmap,
28062306a36Sopenharmony_ci	.get_status = mt6331_get_status,
28162306a36Sopenharmony_ci};
28262306a36Sopenharmony_ci
28362306a36Sopenharmony_cistatic const struct regulator_ops mt6331_volt_table_no_qi_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	.enable = regulator_enable_regmap,
29062306a36Sopenharmony_ci	.disable = regulator_disable_regmap,
29162306a36Sopenharmony_ci	.is_enabled = regulator_is_enabled_regmap,
29262306a36Sopenharmony_ci	.set_mode = mt6331_ldo_set_mode,
29362306a36Sopenharmony_ci	.get_mode = mt6331_ldo_get_mode,
29462306a36Sopenharmony_ci};
29562306a36Sopenharmony_ci
29662306a36Sopenharmony_cistatic const struct regulator_ops mt6331_volt_table_ops = {
29762306a36Sopenharmony_ci	.list_voltage = regulator_list_voltage_table,
29862306a36Sopenharmony_ci	.map_voltage = regulator_map_voltage_iterate,
29962306a36Sopenharmony_ci	.set_voltage_sel = regulator_set_voltage_sel_regmap,
30062306a36Sopenharmony_ci	.get_voltage_sel = regulator_get_voltage_sel_regmap,
30162306a36Sopenharmony_ci	.set_voltage_time_sel = regulator_set_voltage_time_sel,
30262306a36Sopenharmony_ci	.enable = regulator_enable_regmap,
30362306a36Sopenharmony_ci	.disable = regulator_disable_regmap,
30462306a36Sopenharmony_ci	.is_enabled = regulator_is_enabled_regmap,
30562306a36Sopenharmony_ci	.get_status = mt6331_get_status,
30662306a36Sopenharmony_ci	.set_mode = mt6331_ldo_set_mode,
30762306a36Sopenharmony_ci	.get_mode = mt6331_ldo_get_mode,
30862306a36Sopenharmony_ci};
30962306a36Sopenharmony_ci
31062306a36Sopenharmony_cistatic const struct regulator_ops mt6331_volt_table_ao_ops = {
31162306a36Sopenharmony_ci	.list_voltage = regulator_list_voltage_table,
31262306a36Sopenharmony_ci	.map_voltage = regulator_map_voltage_iterate,
31362306a36Sopenharmony_ci	.set_voltage_sel = regulator_set_voltage_sel_regmap,
31462306a36Sopenharmony_ci	.get_voltage_sel = regulator_get_voltage_sel_regmap,
31562306a36Sopenharmony_ci	.set_voltage_time_sel = regulator_set_voltage_time_sel,
31662306a36Sopenharmony_ci};
31762306a36Sopenharmony_ci
31862306a36Sopenharmony_cistatic const struct regulator_ops mt6331_volt_fixed_no_ms_ops = {
31962306a36Sopenharmony_ci	.list_voltage = regulator_list_voltage_linear,
32062306a36Sopenharmony_ci	.enable = regulator_enable_regmap,
32162306a36Sopenharmony_ci	.disable = regulator_disable_regmap,
32262306a36Sopenharmony_ci	.is_enabled = regulator_is_enabled_regmap,
32362306a36Sopenharmony_ci	.get_status = mt6331_get_status,
32462306a36Sopenharmony_ci};
32562306a36Sopenharmony_ci
32662306a36Sopenharmony_cistatic const struct regulator_ops mt6331_volt_fixed_ops = {
32762306a36Sopenharmony_ci	.list_voltage = regulator_list_voltage_linear,
32862306a36Sopenharmony_ci	.enable = regulator_enable_regmap,
32962306a36Sopenharmony_ci	.disable = regulator_disable_regmap,
33062306a36Sopenharmony_ci	.is_enabled = regulator_is_enabled_regmap,
33162306a36Sopenharmony_ci	.get_status = mt6331_get_status,
33262306a36Sopenharmony_ci	.set_mode = mt6331_ldo_set_mode,
33362306a36Sopenharmony_ci	.get_mode = mt6331_ldo_get_mode,
33462306a36Sopenharmony_ci};
33562306a36Sopenharmony_ci
33662306a36Sopenharmony_ci/* The array is indexed by id(MT6331_ID_XXX) */
33762306a36Sopenharmony_cistatic struct mt6331_regulator_info mt6331_regulators[] = {
33862306a36Sopenharmony_ci	MT6331_BUCK("buck-vdvfs11", VDVFS11, 700000, 1493750, 6250,
33962306a36Sopenharmony_ci		    buck_volt_range, MT6331_VDVFS11_CON9,
34062306a36Sopenharmony_ci		    MT6331_VDVFS11_CON11, GENMASK(6, 0),
34162306a36Sopenharmony_ci		    MT6331_VDVFS11_CON12, MT6331_VDVFS11_CON7),
34262306a36Sopenharmony_ci	MT6331_BUCK("buck-vdvfs12", VDVFS12, 700000, 1493750, 6250,
34362306a36Sopenharmony_ci		    buck_volt_range, MT6331_VDVFS12_CON9,
34462306a36Sopenharmony_ci		    MT6331_VDVFS12_CON11, GENMASK(6, 0),
34562306a36Sopenharmony_ci		    MT6331_VDVFS12_CON12, MT6331_VDVFS12_CON7),
34662306a36Sopenharmony_ci	MT6331_BUCK("buck-vdvfs13", VDVFS13, 700000, 1493750, 6250,
34762306a36Sopenharmony_ci		    buck_volt_range, MT6331_VDVFS13_CON9,
34862306a36Sopenharmony_ci		    MT6331_VDVFS13_CON11, GENMASK(6, 0),
34962306a36Sopenharmony_ci		    MT6331_VDVFS13_CON12, MT6331_VDVFS13_CON7),
35062306a36Sopenharmony_ci	MT6331_BUCK("buck-vdvfs14", VDVFS14, 700000, 1493750, 6250,
35162306a36Sopenharmony_ci		    buck_volt_range, MT6331_VDVFS14_CON9,
35262306a36Sopenharmony_ci		    MT6331_VDVFS14_CON11, GENMASK(6, 0),
35362306a36Sopenharmony_ci		    MT6331_VDVFS14_CON12, MT6331_VDVFS14_CON7),
35462306a36Sopenharmony_ci	MT6331_BUCK("buck-vcore2", VCORE2, 700000, 1493750, 6250,
35562306a36Sopenharmony_ci		    buck_volt_range, MT6331_VCORE2_CON9,
35662306a36Sopenharmony_ci		    MT6331_VCORE2_CON11, GENMASK(6, 0),
35762306a36Sopenharmony_ci		    MT6331_VCORE2_CON12, MT6331_VCORE2_CON7),
35862306a36Sopenharmony_ci	MT6331_REG_FIXED("buck-vio18", VIO18, MT6331_VIO18_CON9, 0, 13, 1800000, 0, 0),
35962306a36Sopenharmony_ci	MT6331_REG_FIXED("ldo-vrtc", VRTC, MT6331_DIGLDO_CON11, 8, 15, 2800000, 0, 0),
36062306a36Sopenharmony_ci	MT6331_REG_FIXED("ldo-vtcxo1", VTCXO1, MT6331_ANALDO_CON1, 10, 15, 2800000,
36162306a36Sopenharmony_ci			 MT6331_ANALDO_CON1, GENMASK(1, 0)),
36262306a36Sopenharmony_ci	MT6331_REG_FIXED("ldo-vtcxo2", VTCXO2, MT6331_ANALDO_CON2, 10, 15, 2800000,
36362306a36Sopenharmony_ci			 MT6331_ANALDO_CON2, GENMASK(1, 0)),
36462306a36Sopenharmony_ci	MT6331_REG_FIXED("ldo-vsram", VSRAM_DVFS1, MT6331_SYSLDO_CON4, 10, 15, 1012500,
36562306a36Sopenharmony_ci			 MT6331_SYSLDO_CON4, GENMASK(1, 0)),
36662306a36Sopenharmony_ci	MT6331_REG_FIXED("ldo-vio28", VIO28, MT6331_DIGLDO_CON1, 10, 15, 2800000,
36762306a36Sopenharmony_ci			 MT6331_DIGLDO_CON1, GENMASK(1, 0)),
36862306a36Sopenharmony_ci	MT6331_LDO("ldo-avdd32aud", AVDD32_AUD, ldo_volt_table1, MT6331_ANALDO_CON3, 10,
36962306a36Sopenharmony_ci		   MT6331_ANALDO_CON10, GENMASK(6, 5), MT6331_ANALDO_CON3, GENMASK(1, 0)),
37062306a36Sopenharmony_ci	MT6331_LDO("ldo-vauxa32", VAUXA32, ldo_volt_table1, MT6331_ANALDO_CON4, 10,
37162306a36Sopenharmony_ci		   MT6331_ANALDO_CON6, GENMASK(6, 5), MT6331_ANALDO_CON4, GENMASK(1, 0)),
37262306a36Sopenharmony_ci	MT6331_LDO("ldo-vemc33", VEMC33, ldo_volt_table6, MT6331_DIGLDO_CON5, 10,
37362306a36Sopenharmony_ci		   MT6331_DIGLDO_CON17, BIT(6), MT6331_DIGLDO_CON5, GENMASK(1, 0)),
37462306a36Sopenharmony_ci	MT6331_LDO("ldo-vibr", VIBR, ldo_volt_table3, MT6331_DIGLDO_CON12, 10,
37562306a36Sopenharmony_ci		   MT6331_DIGLDO_CON20, GENMASK(6, 4), MT6331_DIGLDO_CON12, GENMASK(1, 0)),
37662306a36Sopenharmony_ci	MT6331_LDO("ldo-vmc", VMC, ldo_volt_table5, MT6331_DIGLDO_CON3, 10,
37762306a36Sopenharmony_ci		   MT6331_DIGLDO_CON15, GENMASK(5, 4), MT6331_DIGLDO_CON3, GENMASK(1, 0)),
37862306a36Sopenharmony_ci	MT6331_LDO("ldo-vmch", VMCH, ldo_volt_table6, MT6331_DIGLDO_CON4, 10,
37962306a36Sopenharmony_ci		   MT6331_DIGLDO_CON16, BIT(6), MT6331_DIGLDO_CON4, GENMASK(1, 0)),
38062306a36Sopenharmony_ci	MT6331_LDO("ldo-vmipi", VMIPI, ldo_volt_table3, MT6331_SYSLDO_CON5, 10,
38162306a36Sopenharmony_ci		   MT6331_SYSLDO_CON13, GENMASK(5, 3), MT6331_SYSLDO_CON5, GENMASK(1, 0)),
38262306a36Sopenharmony_ci	MT6331_LDO("ldo-vsim1", VSIM1, ldo_volt_table4, MT6331_DIGLDO_CON8, 10,
38362306a36Sopenharmony_ci		   MT6331_DIGLDO_CON21, GENMASK(6, 4), MT6331_DIGLDO_CON8, GENMASK(1, 0)),
38462306a36Sopenharmony_ci	MT6331_LDO("ldo-vsim2", VSIM2, ldo_volt_table4, MT6331_DIGLDO_CON9, 10,
38562306a36Sopenharmony_ci		   MT6331_DIGLDO_CON22, GENMASK(6, 4), MT6331_DIGLDO_CON9, GENMASK(1, 0)),
38662306a36Sopenharmony_ci	MT6331_LDO("ldo-vusb10", VUSB10, ldo_volt_table9, MT6331_SYSLDO_CON2, 10,
38762306a36Sopenharmony_ci		   MT6331_SYSLDO_CON10, GENMASK(5, 3), MT6331_SYSLDO_CON2, GENMASK(1, 0)),
38862306a36Sopenharmony_ci	MT6331_LDO("ldo-vcama", VCAMA, ldo_volt_table2, MT6331_ANALDO_CON5, 15,
38962306a36Sopenharmony_ci		   MT6331_ANALDO_CON9, GENMASK(5, 4), 0, 0),
39062306a36Sopenharmony_ci	MT6331_LDO_S("ldo-vcamaf", VCAM_AF, ldo_volt_table3, MT6331_DIGLDO_CON2, 10,
39162306a36Sopenharmony_ci		     MT6331_DIGLDO_CON14, GENMASK(6, 4), MT6331_DIGLDO_CON2, GENMASK(1, 0),
39262306a36Sopenharmony_ci		     MT6331_EN_STATUS1, BIT(0)),
39362306a36Sopenharmony_ci	MT6331_LDO_S("ldo-vcamd", VCAMD, ldo_volt_table8, MT6331_SYSLDO_CON1, 15,
39462306a36Sopenharmony_ci		     MT6331_SYSLDO_CON9, GENMASK(6, 4), MT6331_SYSLDO_CON1, GENMASK(1, 0),
39562306a36Sopenharmony_ci		     MT6331_EN_STATUS1, BIT(11)),
39662306a36Sopenharmony_ci	MT6331_LDO_S("ldo-vcamio", VCAM_IO,  ldo_volt_table10, MT6331_SYSLDO_CON3, 10,
39762306a36Sopenharmony_ci		     MT6331_SYSLDO_CON11, GENMASK(4, 3), MT6331_SYSLDO_CON3, GENMASK(1, 0),
39862306a36Sopenharmony_ci		     MT6331_EN_STATUS1, BIT(13)),
39962306a36Sopenharmony_ci	MT6331_LDO_S("ldo-vgp1", VGP1, ldo_volt_table3, MT6331_DIGLDO_CON6, 10,
40062306a36Sopenharmony_ci		     MT6331_DIGLDO_CON19, GENMASK(6, 4), MT6331_DIGLDO_CON6, GENMASK(1, 0),
40162306a36Sopenharmony_ci		     MT6331_EN_STATUS1, BIT(4)),
40262306a36Sopenharmony_ci	MT6331_LDO_S("ldo-vgp2", VGP2, ldo_volt_table10, MT6331_SYSLDO_CON6, 10,
40362306a36Sopenharmony_ci		     MT6331_SYSLDO_CON14, GENMASK(4, 3), MT6331_SYSLDO_CON6, GENMASK(1, 0),
40462306a36Sopenharmony_ci		     MT6331_EN_STATUS1, BIT(15)),
40562306a36Sopenharmony_ci	MT6331_LDO_S("ldo-vgp3", VGP3, ldo_volt_table10, MT6331_SYSLDO_CON7, 10,
40662306a36Sopenharmony_ci		     MT6331_SYSLDO_CON15, GENMASK(4, 3), MT6331_SYSLDO_CON7, GENMASK(1, 0),
40762306a36Sopenharmony_ci		     MT6331_EN_STATUS2, BIT(0)),
40862306a36Sopenharmony_ci	MT6331_LDO_S("ldo-vgp4", VGP4, ldo_volt_table7, MT6331_DIGLDO_CON7, 10,
40962306a36Sopenharmony_ci		     MT6331_DIGLDO_CON18, GENMASK(6, 4), MT6331_DIGLDO_CON7, GENMASK(1, 0),
41062306a36Sopenharmony_ci		     MT6331_EN_STATUS1, BIT(5)),
41162306a36Sopenharmony_ci	MT6331_LDO_AO("ldo-vdig18", VDIG18, ldo_volt_table11,
41262306a36Sopenharmony_ci		      MT6331_DIGLDO_CON28, GENMASK(14, 12)),
41362306a36Sopenharmony_ci};
41462306a36Sopenharmony_ci
41562306a36Sopenharmony_cistatic int mt6331_set_buck_vosel_reg(struct platform_device *pdev)
41662306a36Sopenharmony_ci{
41762306a36Sopenharmony_ci	struct mt6397_chip *mt6331 = dev_get_drvdata(pdev->dev.parent);
41862306a36Sopenharmony_ci	int i;
41962306a36Sopenharmony_ci	u32 regval;
42062306a36Sopenharmony_ci
42162306a36Sopenharmony_ci	for (i = 0; i < MT6331_ID_VREG_MAX; i++) {
42262306a36Sopenharmony_ci		if (mt6331_regulators[i].vselctrl_reg) {
42362306a36Sopenharmony_ci			if (regmap_read(mt6331->regmap,
42462306a36Sopenharmony_ci				mt6331_regulators[i].vselctrl_reg,
42562306a36Sopenharmony_ci				&regval) < 0) {
42662306a36Sopenharmony_ci				dev_err(&pdev->dev,
42762306a36Sopenharmony_ci					"Failed to read buck ctrl\n");
42862306a36Sopenharmony_ci				return -EIO;
42962306a36Sopenharmony_ci			}
43062306a36Sopenharmony_ci
43162306a36Sopenharmony_ci			if (regval & mt6331_regulators[i].vselctrl_mask) {
43262306a36Sopenharmony_ci				mt6331_regulators[i].desc.vsel_reg =
43362306a36Sopenharmony_ci				mt6331_regulators[i].vselon_reg;
43462306a36Sopenharmony_ci			}
43562306a36Sopenharmony_ci		}
43662306a36Sopenharmony_ci	}
43762306a36Sopenharmony_ci
43862306a36Sopenharmony_ci	return 0;
43962306a36Sopenharmony_ci}
44062306a36Sopenharmony_ci
44162306a36Sopenharmony_cistatic int mt6331_regulator_probe(struct platform_device *pdev)
44262306a36Sopenharmony_ci{
44362306a36Sopenharmony_ci	struct mt6397_chip *mt6331 = dev_get_drvdata(pdev->dev.parent);
44462306a36Sopenharmony_ci	struct regulator_config config = {};
44562306a36Sopenharmony_ci	struct regulator_dev *rdev;
44662306a36Sopenharmony_ci	int i;
44762306a36Sopenharmony_ci	u32 reg_value;
44862306a36Sopenharmony_ci
44962306a36Sopenharmony_ci	/* Query buck controller to select activated voltage register part */
45062306a36Sopenharmony_ci	if (mt6331_set_buck_vosel_reg(pdev))
45162306a36Sopenharmony_ci		return -EIO;
45262306a36Sopenharmony_ci
45362306a36Sopenharmony_ci	/* Read PMIC chip revision to update constraints and voltage table */
45462306a36Sopenharmony_ci	if (regmap_read(mt6331->regmap, MT6331_HWCID, &reg_value) < 0) {
45562306a36Sopenharmony_ci		dev_err(&pdev->dev, "Failed to read Chip ID\n");
45662306a36Sopenharmony_ci		return -EIO;
45762306a36Sopenharmony_ci	}
45862306a36Sopenharmony_ci	reg_value &= GENMASK(7, 0);
45962306a36Sopenharmony_ci
46062306a36Sopenharmony_ci	dev_info(&pdev->dev, "Chip ID = 0x%x\n", reg_value);
46162306a36Sopenharmony_ci
46262306a36Sopenharmony_ci	/*
46362306a36Sopenharmony_ci	 * ChipID 0x10 is "MT6331 E1", has a different voltage table and
46462306a36Sopenharmony_ci	 * it's currently not supported in this driver. Upon detection of
46562306a36Sopenharmony_ci	 * this ID, refuse to register the regulators, as we will wrongly
46662306a36Sopenharmony_ci	 * interpret the VSEL for this revision, potentially overvolting
46762306a36Sopenharmony_ci	 * some device.
46862306a36Sopenharmony_ci	 */
46962306a36Sopenharmony_ci	if (reg_value == 0x10) {
47062306a36Sopenharmony_ci		dev_err(&pdev->dev, "Chip version not supported. Bailing out.\n");
47162306a36Sopenharmony_ci		return -EINVAL;
47262306a36Sopenharmony_ci	}
47362306a36Sopenharmony_ci
47462306a36Sopenharmony_ci	for (i = 0; i < MT6331_ID_VREG_MAX; i++) {
47562306a36Sopenharmony_ci		config.dev = &pdev->dev;
47662306a36Sopenharmony_ci		config.driver_data = &mt6331_regulators[i];
47762306a36Sopenharmony_ci		config.regmap = mt6331->regmap;
47862306a36Sopenharmony_ci		rdev = devm_regulator_register(&pdev->dev,
47962306a36Sopenharmony_ci				&mt6331_regulators[i].desc, &config);
48062306a36Sopenharmony_ci		if (IS_ERR(rdev)) {
48162306a36Sopenharmony_ci			dev_err(&pdev->dev, "failed to register %s\n",
48262306a36Sopenharmony_ci				mt6331_regulators[i].desc.name);
48362306a36Sopenharmony_ci			return PTR_ERR(rdev);
48462306a36Sopenharmony_ci		}
48562306a36Sopenharmony_ci	}
48662306a36Sopenharmony_ci	return 0;
48762306a36Sopenharmony_ci}
48862306a36Sopenharmony_ci
48962306a36Sopenharmony_cistatic const struct platform_device_id mt6331_platform_ids[] = {
49062306a36Sopenharmony_ci	{"mt6331-regulator", 0},
49162306a36Sopenharmony_ci	{ /* sentinel */ },
49262306a36Sopenharmony_ci};
49362306a36Sopenharmony_ciMODULE_DEVICE_TABLE(platform, mt6331_platform_ids);
49462306a36Sopenharmony_ci
49562306a36Sopenharmony_cistatic struct platform_driver mt6331_regulator_driver = {
49662306a36Sopenharmony_ci	.driver = {
49762306a36Sopenharmony_ci		.name = "mt6331-regulator",
49862306a36Sopenharmony_ci		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
49962306a36Sopenharmony_ci	},
50062306a36Sopenharmony_ci	.probe = mt6331_regulator_probe,
50162306a36Sopenharmony_ci	.id_table = mt6331_platform_ids,
50262306a36Sopenharmony_ci};
50362306a36Sopenharmony_ci
50462306a36Sopenharmony_cimodule_platform_driver(mt6331_regulator_driver);
50562306a36Sopenharmony_ci
50662306a36Sopenharmony_ciMODULE_AUTHOR("AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com>");
50762306a36Sopenharmony_ciMODULE_DESCRIPTION("Regulator Driver for MediaTek MT6331 PMIC");
50862306a36Sopenharmony_ciMODULE_LICENSE("GPL");
509