18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci//
38c2ecf20Sopenharmony_ci// Copyright (c) 2019 MediaTek Inc.
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci#include <linux/mfd/mt6358/registers.h>
68c2ecf20Sopenharmony_ci#include <linux/mfd/mt6397/core.h>
78c2ecf20Sopenharmony_ci#include <linux/module.h>
88c2ecf20Sopenharmony_ci#include <linux/of.h>
98c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
108c2ecf20Sopenharmony_ci#include <linux/regmap.h>
118c2ecf20Sopenharmony_ci#include <linux/regulator/driver.h>
128c2ecf20Sopenharmony_ci#include <linux/regulator/machine.h>
138c2ecf20Sopenharmony_ci#include <linux/regulator/mt6358-regulator.h>
148c2ecf20Sopenharmony_ci#include <linux/regulator/of_regulator.h>
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#define MT6358_BUCK_MODE_AUTO	0
178c2ecf20Sopenharmony_ci#define MT6358_BUCK_MODE_FORCE_PWM	1
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci/*
208c2ecf20Sopenharmony_ci * MT6358 regulators' information
218c2ecf20Sopenharmony_ci *
228c2ecf20Sopenharmony_ci * @desc: standard fields of regulator description.
238c2ecf20Sopenharmony_ci * @qi: Mask for query enable signal status of regulators
248c2ecf20Sopenharmony_ci */
258c2ecf20Sopenharmony_cistruct mt6358_regulator_info {
268c2ecf20Sopenharmony_ci	struct regulator_desc desc;
278c2ecf20Sopenharmony_ci	u32 status_reg;
288c2ecf20Sopenharmony_ci	u32 qi;
298c2ecf20Sopenharmony_ci	const u32 *index_table;
308c2ecf20Sopenharmony_ci	unsigned int n_table;
318c2ecf20Sopenharmony_ci	u32 vsel_shift;
328c2ecf20Sopenharmony_ci	u32 da_vsel_reg;
338c2ecf20Sopenharmony_ci	u32 da_vsel_mask;
348c2ecf20Sopenharmony_ci	u32 da_vsel_shift;
358c2ecf20Sopenharmony_ci	u32 modeset_reg;
368c2ecf20Sopenharmony_ci	u32 modeset_mask;
378c2ecf20Sopenharmony_ci	u32 modeset_shift;
388c2ecf20Sopenharmony_ci};
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci#define MT6358_BUCK(match, vreg, min, max, step,		\
418c2ecf20Sopenharmony_ci	volt_ranges, vosel_mask, _da_vsel_reg, _da_vsel_mask,	\
428c2ecf20Sopenharmony_ci	_da_vsel_shift, _modeset_reg, _modeset_shift)		\
438c2ecf20Sopenharmony_ci[MT6358_ID_##vreg] = {	\
448c2ecf20Sopenharmony_ci	.desc = {	\
458c2ecf20Sopenharmony_ci		.name = #vreg,	\
468c2ecf20Sopenharmony_ci		.of_match = of_match_ptr(match),	\
478c2ecf20Sopenharmony_ci		.ops = &mt6358_volt_range_ops,	\
488c2ecf20Sopenharmony_ci		.type = REGULATOR_VOLTAGE,	\
498c2ecf20Sopenharmony_ci		.id = MT6358_ID_##vreg,		\
508c2ecf20Sopenharmony_ci		.owner = THIS_MODULE,		\
518c2ecf20Sopenharmony_ci		.n_voltages = ((max) - (min)) / (step) + 1,	\
528c2ecf20Sopenharmony_ci		.linear_ranges = volt_ranges,		\
538c2ecf20Sopenharmony_ci		.n_linear_ranges = ARRAY_SIZE(volt_ranges),	\
548c2ecf20Sopenharmony_ci		.vsel_reg = MT6358_BUCK_##vreg##_ELR0,	\
558c2ecf20Sopenharmony_ci		.vsel_mask = vosel_mask,	\
568c2ecf20Sopenharmony_ci		.enable_reg = MT6358_BUCK_##vreg##_CON0,	\
578c2ecf20Sopenharmony_ci		.enable_mask = BIT(0),	\
588c2ecf20Sopenharmony_ci		.of_map_mode = mt6358_map_mode,	\
598c2ecf20Sopenharmony_ci	},	\
608c2ecf20Sopenharmony_ci	.status_reg = MT6358_BUCK_##vreg##_DBG1,	\
618c2ecf20Sopenharmony_ci	.qi = BIT(0),	\
628c2ecf20Sopenharmony_ci	.da_vsel_reg = _da_vsel_reg,	\
638c2ecf20Sopenharmony_ci	.da_vsel_mask = _da_vsel_mask,	\
648c2ecf20Sopenharmony_ci	.da_vsel_shift = _da_vsel_shift,	\
658c2ecf20Sopenharmony_ci	.modeset_reg = _modeset_reg,	\
668c2ecf20Sopenharmony_ci	.modeset_mask = BIT(_modeset_shift),	\
678c2ecf20Sopenharmony_ci	.modeset_shift = _modeset_shift	\
688c2ecf20Sopenharmony_ci}
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci#define MT6358_LDO(match, vreg, ldo_volt_table,	\
718c2ecf20Sopenharmony_ci	ldo_index_table, enreg, enbit, vosel,	\
728c2ecf20Sopenharmony_ci	vosel_mask, vosel_shift)	\
738c2ecf20Sopenharmony_ci[MT6358_ID_##vreg] = {	\
748c2ecf20Sopenharmony_ci	.desc = {	\
758c2ecf20Sopenharmony_ci		.name = #vreg,	\
768c2ecf20Sopenharmony_ci		.of_match = of_match_ptr(match),	\
778c2ecf20Sopenharmony_ci		.ops = &mt6358_volt_table_ops,	\
788c2ecf20Sopenharmony_ci		.type = REGULATOR_VOLTAGE,	\
798c2ecf20Sopenharmony_ci		.id = MT6358_ID_##vreg,	\
808c2ecf20Sopenharmony_ci		.owner = THIS_MODULE,	\
818c2ecf20Sopenharmony_ci		.n_voltages = ARRAY_SIZE(ldo_volt_table),	\
828c2ecf20Sopenharmony_ci		.volt_table = ldo_volt_table,	\
838c2ecf20Sopenharmony_ci		.vsel_reg = vosel,	\
848c2ecf20Sopenharmony_ci		.vsel_mask = vosel_mask,	\
858c2ecf20Sopenharmony_ci		.enable_reg = enreg,	\
868c2ecf20Sopenharmony_ci		.enable_mask = BIT(enbit),	\
878c2ecf20Sopenharmony_ci	},	\
888c2ecf20Sopenharmony_ci	.status_reg = MT6358_LDO_##vreg##_CON1,	\
898c2ecf20Sopenharmony_ci	.qi = BIT(15),	\
908c2ecf20Sopenharmony_ci	.index_table = ldo_index_table,	\
918c2ecf20Sopenharmony_ci	.n_table = ARRAY_SIZE(ldo_index_table),	\
928c2ecf20Sopenharmony_ci	.vsel_shift = vosel_shift,	\
938c2ecf20Sopenharmony_ci}
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci#define MT6358_LDO1(match, vreg, min, max, step,	\
968c2ecf20Sopenharmony_ci	volt_ranges, _da_vsel_reg, _da_vsel_mask,	\
978c2ecf20Sopenharmony_ci	_da_vsel_shift, vosel, vosel_mask)	\
988c2ecf20Sopenharmony_ci[MT6358_ID_##vreg] = {	\
998c2ecf20Sopenharmony_ci	.desc = {	\
1008c2ecf20Sopenharmony_ci		.name = #vreg,	\
1018c2ecf20Sopenharmony_ci		.of_match = of_match_ptr(match),	\
1028c2ecf20Sopenharmony_ci		.ops = &mt6358_volt_range_ops,	\
1038c2ecf20Sopenharmony_ci		.type = REGULATOR_VOLTAGE,	\
1048c2ecf20Sopenharmony_ci		.id = MT6358_ID_##vreg,	\
1058c2ecf20Sopenharmony_ci		.owner = THIS_MODULE,	\
1068c2ecf20Sopenharmony_ci		.n_voltages = ((max) - (min)) / (step) + 1,	\
1078c2ecf20Sopenharmony_ci		.linear_ranges = volt_ranges,	\
1088c2ecf20Sopenharmony_ci		.n_linear_ranges = ARRAY_SIZE(volt_ranges),	\
1098c2ecf20Sopenharmony_ci		.vsel_reg = vosel,	\
1108c2ecf20Sopenharmony_ci		.vsel_mask = vosel_mask,	\
1118c2ecf20Sopenharmony_ci		.enable_reg = MT6358_LDO_##vreg##_CON0,	\
1128c2ecf20Sopenharmony_ci		.enable_mask = BIT(0),	\
1138c2ecf20Sopenharmony_ci	},	\
1148c2ecf20Sopenharmony_ci	.da_vsel_reg = _da_vsel_reg,	\
1158c2ecf20Sopenharmony_ci	.da_vsel_mask = _da_vsel_mask,	\
1168c2ecf20Sopenharmony_ci	.da_vsel_shift = _da_vsel_shift,	\
1178c2ecf20Sopenharmony_ci	.status_reg = MT6358_LDO_##vreg##_DBG1,	\
1188c2ecf20Sopenharmony_ci	.qi = BIT(0),	\
1198c2ecf20Sopenharmony_ci}
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci#define MT6358_REG_FIXED(match, vreg,	\
1228c2ecf20Sopenharmony_ci	enreg, enbit, volt)	\
1238c2ecf20Sopenharmony_ci[MT6358_ID_##vreg] = {	\
1248c2ecf20Sopenharmony_ci	.desc = {	\
1258c2ecf20Sopenharmony_ci		.name = #vreg,	\
1268c2ecf20Sopenharmony_ci		.of_match = of_match_ptr(match),	\
1278c2ecf20Sopenharmony_ci		.ops = &mt6358_volt_fixed_ops,	\
1288c2ecf20Sopenharmony_ci		.type = REGULATOR_VOLTAGE,	\
1298c2ecf20Sopenharmony_ci		.id = MT6358_ID_##vreg,	\
1308c2ecf20Sopenharmony_ci		.owner = THIS_MODULE,	\
1318c2ecf20Sopenharmony_ci		.n_voltages = 1,	\
1328c2ecf20Sopenharmony_ci		.enable_reg = enreg,	\
1338c2ecf20Sopenharmony_ci		.enable_mask = BIT(enbit),	\
1348c2ecf20Sopenharmony_ci		.min_uV = volt,	\
1358c2ecf20Sopenharmony_ci	},	\
1368c2ecf20Sopenharmony_ci	.status_reg = MT6358_LDO_##vreg##_CON1,	\
1378c2ecf20Sopenharmony_ci	.qi = BIT(15),							\
1388c2ecf20Sopenharmony_ci}
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_cistatic const struct linear_range buck_volt_range1[] = {
1418c2ecf20Sopenharmony_ci	REGULATOR_LINEAR_RANGE(500000, 0, 0x7f, 6250),
1428c2ecf20Sopenharmony_ci};
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_cistatic const struct linear_range buck_volt_range2[] = {
1458c2ecf20Sopenharmony_ci	REGULATOR_LINEAR_RANGE(500000, 0, 0x7f, 12500),
1468c2ecf20Sopenharmony_ci};
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_cistatic const struct linear_range buck_volt_range3[] = {
1498c2ecf20Sopenharmony_ci	REGULATOR_LINEAR_RANGE(500000, 0, 0x3f, 50000),
1508c2ecf20Sopenharmony_ci};
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_cistatic const struct linear_range buck_volt_range4[] = {
1538c2ecf20Sopenharmony_ci	REGULATOR_LINEAR_RANGE(1000000, 0, 0x7f, 12500),
1548c2ecf20Sopenharmony_ci};
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_cistatic const u32 vdram2_voltages[] = {
1578c2ecf20Sopenharmony_ci	600000, 1800000,
1588c2ecf20Sopenharmony_ci};
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_cistatic const u32 vsim_voltages[] = {
1618c2ecf20Sopenharmony_ci	1700000, 1800000, 2700000, 3000000, 3100000,
1628c2ecf20Sopenharmony_ci};
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_cistatic const u32 vibr_voltages[] = {
1658c2ecf20Sopenharmony_ci	1200000, 1300000, 1500000, 1800000,
1668c2ecf20Sopenharmony_ci	2000000, 2800000, 3000000, 3300000,
1678c2ecf20Sopenharmony_ci};
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_cistatic const u32 vusb_voltages[] = {
1708c2ecf20Sopenharmony_ci	3000000, 3100000,
1718c2ecf20Sopenharmony_ci};
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_cistatic const u32 vcamd_voltages[] = {
1748c2ecf20Sopenharmony_ci	900000, 1000000, 1100000, 1200000,
1758c2ecf20Sopenharmony_ci	1300000, 1500000, 1800000,
1768c2ecf20Sopenharmony_ci};
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_cistatic const u32 vefuse_voltages[] = {
1798c2ecf20Sopenharmony_ci	1700000, 1800000, 1900000,
1808c2ecf20Sopenharmony_ci};
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_cistatic const u32 vmch_vemc_voltages[] = {
1838c2ecf20Sopenharmony_ci	2900000, 3000000, 3300000,
1848c2ecf20Sopenharmony_ci};
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_cistatic const u32 vcama_voltages[] = {
1878c2ecf20Sopenharmony_ci	1800000, 2500000, 2700000,
1888c2ecf20Sopenharmony_ci	2800000, 2900000, 3000000,
1898c2ecf20Sopenharmony_ci};
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_cistatic const u32 vcn33_bt_wifi_voltages[] = {
1928c2ecf20Sopenharmony_ci	3300000, 3400000, 3500000,
1938c2ecf20Sopenharmony_ci};
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_cistatic const u32 vmc_voltages[] = {
1968c2ecf20Sopenharmony_ci	1800000, 2900000, 3000000, 3300000,
1978c2ecf20Sopenharmony_ci};
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_cistatic const u32 vldo28_voltages[] = {
2008c2ecf20Sopenharmony_ci	2800000, 3000000,
2018c2ecf20Sopenharmony_ci};
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_cistatic const u32 vdram2_idx[] = {
2048c2ecf20Sopenharmony_ci	0, 12,
2058c2ecf20Sopenharmony_ci};
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_cistatic const u32 vsim_idx[] = {
2088c2ecf20Sopenharmony_ci	3, 4, 8, 11, 12,
2098c2ecf20Sopenharmony_ci};
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_cistatic const u32 vibr_idx[] = {
2128c2ecf20Sopenharmony_ci	0, 1, 2, 4, 5, 9, 11, 13,
2138c2ecf20Sopenharmony_ci};
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_cistatic const u32 vusb_idx[] = {
2168c2ecf20Sopenharmony_ci	3, 4,
2178c2ecf20Sopenharmony_ci};
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_cistatic const u32 vcamd_idx[] = {
2208c2ecf20Sopenharmony_ci	3, 4, 5, 6, 7, 9, 12,
2218c2ecf20Sopenharmony_ci};
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_cistatic const u32 vefuse_idx[] = {
2248c2ecf20Sopenharmony_ci	11, 12, 13,
2258c2ecf20Sopenharmony_ci};
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_cistatic const u32 vmch_vemc_idx[] = {
2288c2ecf20Sopenharmony_ci	2, 3, 5,
2298c2ecf20Sopenharmony_ci};
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_cistatic const u32 vcama_idx[] = {
2328c2ecf20Sopenharmony_ci	0, 7, 9, 10, 11, 12,
2338c2ecf20Sopenharmony_ci};
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_cistatic const u32 vcn33_bt_wifi_idx[] = {
2368c2ecf20Sopenharmony_ci	1, 2, 3,
2378c2ecf20Sopenharmony_ci};
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_cistatic const u32 vmc_idx[] = {
2408c2ecf20Sopenharmony_ci	4, 10, 11, 13,
2418c2ecf20Sopenharmony_ci};
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_cistatic const u32 vldo28_idx[] = {
2448c2ecf20Sopenharmony_ci	1, 3,
2458c2ecf20Sopenharmony_ci};
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_cistatic unsigned int mt6358_map_mode(unsigned int mode)
2488c2ecf20Sopenharmony_ci{
2498c2ecf20Sopenharmony_ci	return mode == MT6358_BUCK_MODE_AUTO ?
2508c2ecf20Sopenharmony_ci		REGULATOR_MODE_NORMAL : REGULATOR_MODE_FAST;
2518c2ecf20Sopenharmony_ci}
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_cistatic int mt6358_set_voltage_sel(struct regulator_dev *rdev,
2548c2ecf20Sopenharmony_ci				  unsigned int selector)
2558c2ecf20Sopenharmony_ci{
2568c2ecf20Sopenharmony_ci	int idx, ret;
2578c2ecf20Sopenharmony_ci	const u32 *pvol;
2588c2ecf20Sopenharmony_ci	struct mt6358_regulator_info *info = rdev_get_drvdata(rdev);
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_ci	pvol = info->index_table;
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_ci	idx = pvol[selector];
2638c2ecf20Sopenharmony_ci	ret = regmap_update_bits(rdev->regmap, info->desc.vsel_reg,
2648c2ecf20Sopenharmony_ci				 info->desc.vsel_mask,
2658c2ecf20Sopenharmony_ci				 idx << info->vsel_shift);
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ci	return ret;
2688c2ecf20Sopenharmony_ci}
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_cistatic int mt6358_get_voltage_sel(struct regulator_dev *rdev)
2718c2ecf20Sopenharmony_ci{
2728c2ecf20Sopenharmony_ci	int idx, ret;
2738c2ecf20Sopenharmony_ci	u32 selector;
2748c2ecf20Sopenharmony_ci	struct mt6358_regulator_info *info = rdev_get_drvdata(rdev);
2758c2ecf20Sopenharmony_ci	const u32 *pvol;
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ci	ret = regmap_read(rdev->regmap, info->desc.vsel_reg, &selector);
2788c2ecf20Sopenharmony_ci	if (ret != 0) {
2798c2ecf20Sopenharmony_ci		dev_info(&rdev->dev,
2808c2ecf20Sopenharmony_ci			 "Failed to get mt6358 %s vsel reg: %d\n",
2818c2ecf20Sopenharmony_ci			 info->desc.name, ret);
2828c2ecf20Sopenharmony_ci		return ret;
2838c2ecf20Sopenharmony_ci	}
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_ci	selector = (selector & info->desc.vsel_mask) >> info->vsel_shift;
2868c2ecf20Sopenharmony_ci	pvol = info->index_table;
2878c2ecf20Sopenharmony_ci	for (idx = 0; idx < info->desc.n_voltages; idx++) {
2888c2ecf20Sopenharmony_ci		if (pvol[idx] == selector)
2898c2ecf20Sopenharmony_ci			return idx;
2908c2ecf20Sopenharmony_ci	}
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci	return -EINVAL;
2938c2ecf20Sopenharmony_ci}
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_cistatic int mt6358_get_buck_voltage_sel(struct regulator_dev *rdev)
2968c2ecf20Sopenharmony_ci{
2978c2ecf20Sopenharmony_ci	int ret, regval;
2988c2ecf20Sopenharmony_ci	struct mt6358_regulator_info *info = rdev_get_drvdata(rdev);
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_ci	ret = regmap_read(rdev->regmap, info->da_vsel_reg, &regval);
3018c2ecf20Sopenharmony_ci	if (ret != 0) {
3028c2ecf20Sopenharmony_ci		dev_err(&rdev->dev,
3038c2ecf20Sopenharmony_ci			"Failed to get mt6358 Buck %s vsel reg: %d\n",
3048c2ecf20Sopenharmony_ci			info->desc.name, ret);
3058c2ecf20Sopenharmony_ci		return ret;
3068c2ecf20Sopenharmony_ci	}
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ci	ret = (regval >> info->da_vsel_shift) & info->da_vsel_mask;
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci	return ret;
3118c2ecf20Sopenharmony_ci}
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_cistatic int mt6358_get_status(struct regulator_dev *rdev)
3148c2ecf20Sopenharmony_ci{
3158c2ecf20Sopenharmony_ci	int ret;
3168c2ecf20Sopenharmony_ci	u32 regval;
3178c2ecf20Sopenharmony_ci	struct mt6358_regulator_info *info = rdev_get_drvdata(rdev);
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ci	ret = regmap_read(rdev->regmap, info->status_reg, &regval);
3208c2ecf20Sopenharmony_ci	if (ret != 0) {
3218c2ecf20Sopenharmony_ci		dev_info(&rdev->dev, "Failed to get enable reg: %d\n", ret);
3228c2ecf20Sopenharmony_ci		return ret;
3238c2ecf20Sopenharmony_ci	}
3248c2ecf20Sopenharmony_ci
3258c2ecf20Sopenharmony_ci	return (regval & info->qi) ? REGULATOR_STATUS_ON : REGULATOR_STATUS_OFF;
3268c2ecf20Sopenharmony_ci}
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_cistatic int mt6358_regulator_set_mode(struct regulator_dev *rdev,
3298c2ecf20Sopenharmony_ci				     unsigned int mode)
3308c2ecf20Sopenharmony_ci{
3318c2ecf20Sopenharmony_ci	struct mt6358_regulator_info *info = rdev_get_drvdata(rdev);
3328c2ecf20Sopenharmony_ci	int val;
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_ci	switch (mode) {
3358c2ecf20Sopenharmony_ci	case REGULATOR_MODE_FAST:
3368c2ecf20Sopenharmony_ci		val = MT6358_BUCK_MODE_FORCE_PWM;
3378c2ecf20Sopenharmony_ci		break;
3388c2ecf20Sopenharmony_ci	case REGULATOR_MODE_NORMAL:
3398c2ecf20Sopenharmony_ci		val = MT6358_BUCK_MODE_AUTO;
3408c2ecf20Sopenharmony_ci		break;
3418c2ecf20Sopenharmony_ci	default:
3428c2ecf20Sopenharmony_ci		return -EINVAL;
3438c2ecf20Sopenharmony_ci	}
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_ci	dev_dbg(&rdev->dev, "mt6358 buck set_mode %#x, %#x, %#x, %#x\n",
3468c2ecf20Sopenharmony_ci		info->modeset_reg, info->modeset_mask,
3478c2ecf20Sopenharmony_ci		info->modeset_shift, val);
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_ci	val <<= info->modeset_shift;
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_ci	return regmap_update_bits(rdev->regmap, info->modeset_reg,
3528c2ecf20Sopenharmony_ci				  info->modeset_mask, val);
3538c2ecf20Sopenharmony_ci}
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_cistatic unsigned int mt6358_regulator_get_mode(struct regulator_dev *rdev)
3568c2ecf20Sopenharmony_ci{
3578c2ecf20Sopenharmony_ci	struct mt6358_regulator_info *info = rdev_get_drvdata(rdev);
3588c2ecf20Sopenharmony_ci	int ret, regval;
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_ci	ret = regmap_read(rdev->regmap, info->modeset_reg, &regval);
3618c2ecf20Sopenharmony_ci	if (ret != 0) {
3628c2ecf20Sopenharmony_ci		dev_err(&rdev->dev,
3638c2ecf20Sopenharmony_ci			"Failed to get mt6358 buck mode: %d\n", ret);
3648c2ecf20Sopenharmony_ci		return ret;
3658c2ecf20Sopenharmony_ci	}
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_ci	switch ((regval & info->modeset_mask) >> info->modeset_shift) {
3688c2ecf20Sopenharmony_ci	case MT6358_BUCK_MODE_AUTO:
3698c2ecf20Sopenharmony_ci		return REGULATOR_MODE_NORMAL;
3708c2ecf20Sopenharmony_ci	case MT6358_BUCK_MODE_FORCE_PWM:
3718c2ecf20Sopenharmony_ci		return REGULATOR_MODE_FAST;
3728c2ecf20Sopenharmony_ci	default:
3738c2ecf20Sopenharmony_ci		return -EINVAL;
3748c2ecf20Sopenharmony_ci	}
3758c2ecf20Sopenharmony_ci}
3768c2ecf20Sopenharmony_ci
3778c2ecf20Sopenharmony_cistatic const struct regulator_ops mt6358_volt_range_ops = {
3788c2ecf20Sopenharmony_ci	.list_voltage = regulator_list_voltage_linear_range,
3798c2ecf20Sopenharmony_ci	.map_voltage = regulator_map_voltage_linear_range,
3808c2ecf20Sopenharmony_ci	.set_voltage_sel = regulator_set_voltage_sel_regmap,
3818c2ecf20Sopenharmony_ci	.get_voltage_sel = mt6358_get_buck_voltage_sel,
3828c2ecf20Sopenharmony_ci	.set_voltage_time_sel = regulator_set_voltage_time_sel,
3838c2ecf20Sopenharmony_ci	.enable = regulator_enable_regmap,
3848c2ecf20Sopenharmony_ci	.disable = regulator_disable_regmap,
3858c2ecf20Sopenharmony_ci	.is_enabled = regulator_is_enabled_regmap,
3868c2ecf20Sopenharmony_ci	.get_status = mt6358_get_status,
3878c2ecf20Sopenharmony_ci	.set_mode = mt6358_regulator_set_mode,
3888c2ecf20Sopenharmony_ci	.get_mode = mt6358_regulator_get_mode,
3898c2ecf20Sopenharmony_ci};
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_cistatic const struct regulator_ops mt6358_volt_table_ops = {
3928c2ecf20Sopenharmony_ci	.list_voltage = regulator_list_voltage_table,
3938c2ecf20Sopenharmony_ci	.map_voltage = regulator_map_voltage_iterate,
3948c2ecf20Sopenharmony_ci	.set_voltage_sel = mt6358_set_voltage_sel,
3958c2ecf20Sopenharmony_ci	.get_voltage_sel = mt6358_get_voltage_sel,
3968c2ecf20Sopenharmony_ci	.set_voltage_time_sel = regulator_set_voltage_time_sel,
3978c2ecf20Sopenharmony_ci	.enable = regulator_enable_regmap,
3988c2ecf20Sopenharmony_ci	.disable = regulator_disable_regmap,
3998c2ecf20Sopenharmony_ci	.is_enabled = regulator_is_enabled_regmap,
4008c2ecf20Sopenharmony_ci	.get_status = mt6358_get_status,
4018c2ecf20Sopenharmony_ci};
4028c2ecf20Sopenharmony_ci
4038c2ecf20Sopenharmony_cistatic const struct regulator_ops mt6358_volt_fixed_ops = {
4048c2ecf20Sopenharmony_ci	.list_voltage = regulator_list_voltage_linear,
4058c2ecf20Sopenharmony_ci	.enable = regulator_enable_regmap,
4068c2ecf20Sopenharmony_ci	.disable = regulator_disable_regmap,
4078c2ecf20Sopenharmony_ci	.is_enabled = regulator_is_enabled_regmap,
4088c2ecf20Sopenharmony_ci	.get_status = mt6358_get_status,
4098c2ecf20Sopenharmony_ci};
4108c2ecf20Sopenharmony_ci
4118c2ecf20Sopenharmony_ci/* The array is indexed by id(MT6358_ID_XXX) */
4128c2ecf20Sopenharmony_cistatic struct mt6358_regulator_info mt6358_regulators[] = {
4138c2ecf20Sopenharmony_ci	MT6358_BUCK("buck_vdram1", VDRAM1, 500000, 2087500, 12500,
4148c2ecf20Sopenharmony_ci		    buck_volt_range2, 0x7f, MT6358_BUCK_VDRAM1_DBG0, 0x7f,
4158c2ecf20Sopenharmony_ci		    0, MT6358_VDRAM1_ANA_CON0, 8),
4168c2ecf20Sopenharmony_ci	MT6358_BUCK("buck_vcore", VCORE, 500000, 1293750, 6250,
4178c2ecf20Sopenharmony_ci		    buck_volt_range1, 0x7f, MT6358_BUCK_VCORE_DBG0, 0x7f,
4188c2ecf20Sopenharmony_ci		    0, MT6358_VCORE_VGPU_ANA_CON0, 1),
4198c2ecf20Sopenharmony_ci	MT6358_BUCK("buck_vpa", VPA, 500000, 3650000, 50000,
4208c2ecf20Sopenharmony_ci		    buck_volt_range3, 0x3f, MT6358_BUCK_VPA_DBG0, 0x3f, 0,
4218c2ecf20Sopenharmony_ci		    MT6358_VPA_ANA_CON0, 3),
4228c2ecf20Sopenharmony_ci	MT6358_BUCK("buck_vproc11", VPROC11, 500000, 1293750, 6250,
4238c2ecf20Sopenharmony_ci		    buck_volt_range1, 0x7f, MT6358_BUCK_VPROC11_DBG0, 0x7f,
4248c2ecf20Sopenharmony_ci		    0, MT6358_VPROC_ANA_CON0, 1),
4258c2ecf20Sopenharmony_ci	MT6358_BUCK("buck_vproc12", VPROC12, 500000, 1293750, 6250,
4268c2ecf20Sopenharmony_ci		    buck_volt_range1, 0x7f, MT6358_BUCK_VPROC12_DBG0, 0x7f,
4278c2ecf20Sopenharmony_ci		    0, MT6358_VPROC_ANA_CON0, 2),
4288c2ecf20Sopenharmony_ci	MT6358_BUCK("buck_vgpu", VGPU, 500000, 1293750, 6250,
4298c2ecf20Sopenharmony_ci		    buck_volt_range1, 0x7f, MT6358_BUCK_VGPU_ELR0, 0x7f, 0,
4308c2ecf20Sopenharmony_ci		    MT6358_VCORE_VGPU_ANA_CON0, 2),
4318c2ecf20Sopenharmony_ci	MT6358_BUCK("buck_vs2", VS2, 500000, 2087500, 12500,
4328c2ecf20Sopenharmony_ci		    buck_volt_range2, 0x7f, MT6358_BUCK_VS2_DBG0, 0x7f, 0,
4338c2ecf20Sopenharmony_ci		    MT6358_VS2_ANA_CON0, 8),
4348c2ecf20Sopenharmony_ci	MT6358_BUCK("buck_vmodem", VMODEM, 500000, 1293750, 6250,
4358c2ecf20Sopenharmony_ci		    buck_volt_range1, 0x7f, MT6358_BUCK_VMODEM_DBG0, 0x7f,
4368c2ecf20Sopenharmony_ci		    0, MT6358_VMODEM_ANA_CON0, 8),
4378c2ecf20Sopenharmony_ci	MT6358_BUCK("buck_vs1", VS1, 1000000, 2587500, 12500,
4388c2ecf20Sopenharmony_ci		    buck_volt_range4, 0x7f, MT6358_BUCK_VS1_DBG0, 0x7f, 0,
4398c2ecf20Sopenharmony_ci		    MT6358_VS1_ANA_CON0, 8),
4408c2ecf20Sopenharmony_ci	MT6358_REG_FIXED("ldo_vrf12", VRF12,
4418c2ecf20Sopenharmony_ci			 MT6358_LDO_VRF12_CON0, 0, 1200000),
4428c2ecf20Sopenharmony_ci	MT6358_REG_FIXED("ldo_vio18", VIO18,
4438c2ecf20Sopenharmony_ci			 MT6358_LDO_VIO18_CON0, 0, 1800000),
4448c2ecf20Sopenharmony_ci	MT6358_REG_FIXED("ldo_vcamio", VCAMIO,
4458c2ecf20Sopenharmony_ci			 MT6358_LDO_VCAMIO_CON0, 0, 1800000),
4468c2ecf20Sopenharmony_ci	MT6358_REG_FIXED("ldo_vcn18", VCN18, MT6358_LDO_VCN18_CON0, 0, 1800000),
4478c2ecf20Sopenharmony_ci	MT6358_REG_FIXED("ldo_vfe28", VFE28, MT6358_LDO_VFE28_CON0, 0, 2800000),
4488c2ecf20Sopenharmony_ci	MT6358_REG_FIXED("ldo_vcn28", VCN28, MT6358_LDO_VCN28_CON0, 0, 2800000),
4498c2ecf20Sopenharmony_ci	MT6358_REG_FIXED("ldo_vxo22", VXO22, MT6358_LDO_VXO22_CON0, 0, 2200000),
4508c2ecf20Sopenharmony_ci	MT6358_REG_FIXED("ldo_vaux18", VAUX18,
4518c2ecf20Sopenharmony_ci			 MT6358_LDO_VAUX18_CON0, 0, 1800000),
4528c2ecf20Sopenharmony_ci	MT6358_REG_FIXED("ldo_vbif28", VBIF28,
4538c2ecf20Sopenharmony_ci			 MT6358_LDO_VBIF28_CON0, 0, 2800000),
4548c2ecf20Sopenharmony_ci	MT6358_REG_FIXED("ldo_vio28", VIO28, MT6358_LDO_VIO28_CON0, 0, 2800000),
4558c2ecf20Sopenharmony_ci	MT6358_REG_FIXED("ldo_va12", VA12, MT6358_LDO_VA12_CON0, 0, 1200000),
4568c2ecf20Sopenharmony_ci	MT6358_REG_FIXED("ldo_vrf18", VRF18, MT6358_LDO_VRF18_CON0, 0, 1800000),
4578c2ecf20Sopenharmony_ci	MT6358_REG_FIXED("ldo_vaud28", VAUD28,
4588c2ecf20Sopenharmony_ci			 MT6358_LDO_VAUD28_CON0, 0, 2800000),
4598c2ecf20Sopenharmony_ci	MT6358_LDO("ldo_vdram2", VDRAM2, vdram2_voltages, vdram2_idx,
4608c2ecf20Sopenharmony_ci		   MT6358_LDO_VDRAM2_CON0, 0, MT6358_LDO_VDRAM2_ELR0, 0xf, 0),
4618c2ecf20Sopenharmony_ci	MT6358_LDO("ldo_vsim1", VSIM1, vsim_voltages, vsim_idx,
4628c2ecf20Sopenharmony_ci		   MT6358_LDO_VSIM1_CON0, 0, MT6358_VSIM1_ANA_CON0, 0xf00, 8),
4638c2ecf20Sopenharmony_ci	MT6358_LDO("ldo_vibr", VIBR, vibr_voltages, vibr_idx,
4648c2ecf20Sopenharmony_ci		   MT6358_LDO_VIBR_CON0, 0, MT6358_VIBR_ANA_CON0, 0xf00, 8),
4658c2ecf20Sopenharmony_ci	MT6358_LDO("ldo_vusb", VUSB, vusb_voltages, vusb_idx,
4668c2ecf20Sopenharmony_ci		   MT6358_LDO_VUSB_CON0_0, 0, MT6358_VUSB_ANA_CON0, 0x700, 8),
4678c2ecf20Sopenharmony_ci	MT6358_LDO("ldo_vcamd", VCAMD, vcamd_voltages, vcamd_idx,
4688c2ecf20Sopenharmony_ci		   MT6358_LDO_VCAMD_CON0, 0, MT6358_VCAMD_ANA_CON0, 0xf00, 8),
4698c2ecf20Sopenharmony_ci	MT6358_LDO("ldo_vefuse", VEFUSE, vefuse_voltages, vefuse_idx,
4708c2ecf20Sopenharmony_ci		   MT6358_LDO_VEFUSE_CON0, 0, MT6358_VEFUSE_ANA_CON0, 0xf00, 8),
4718c2ecf20Sopenharmony_ci	MT6358_LDO("ldo_vmch", VMCH, vmch_vemc_voltages, vmch_vemc_idx,
4728c2ecf20Sopenharmony_ci		   MT6358_LDO_VMCH_CON0, 0, MT6358_VMCH_ANA_CON0, 0x700, 8),
4738c2ecf20Sopenharmony_ci	MT6358_LDO("ldo_vcama1", VCAMA1, vcama_voltages, vcama_idx,
4748c2ecf20Sopenharmony_ci		   MT6358_LDO_VCAMA1_CON0, 0, MT6358_VCAMA1_ANA_CON0, 0xf00, 8),
4758c2ecf20Sopenharmony_ci	MT6358_LDO("ldo_vemc", VEMC, vmch_vemc_voltages, vmch_vemc_idx,
4768c2ecf20Sopenharmony_ci		   MT6358_LDO_VEMC_CON0, 0, MT6358_VEMC_ANA_CON0, 0x700, 8),
4778c2ecf20Sopenharmony_ci	MT6358_LDO("ldo_vcn33_bt", VCN33_BT, vcn33_bt_wifi_voltages,
4788c2ecf20Sopenharmony_ci		   vcn33_bt_wifi_idx, MT6358_LDO_VCN33_CON0_0,
4798c2ecf20Sopenharmony_ci		   0, MT6358_VCN33_ANA_CON0, 0x300, 8),
4808c2ecf20Sopenharmony_ci	MT6358_LDO("ldo_vcn33_wifi", VCN33_WIFI, vcn33_bt_wifi_voltages,
4818c2ecf20Sopenharmony_ci		   vcn33_bt_wifi_idx, MT6358_LDO_VCN33_CON0_1,
4828c2ecf20Sopenharmony_ci		   0, MT6358_VCN33_ANA_CON0, 0x300, 8),
4838c2ecf20Sopenharmony_ci	MT6358_LDO("ldo_vcama2", VCAMA2, vcama_voltages, vcama_idx,
4848c2ecf20Sopenharmony_ci		   MT6358_LDO_VCAMA2_CON0, 0, MT6358_VCAMA2_ANA_CON0, 0xf00, 8),
4858c2ecf20Sopenharmony_ci	MT6358_LDO("ldo_vmc", VMC, vmc_voltages, vmc_idx,
4868c2ecf20Sopenharmony_ci		   MT6358_LDO_VMC_CON0, 0, MT6358_VMC_ANA_CON0, 0xf00, 8),
4878c2ecf20Sopenharmony_ci	MT6358_LDO("ldo_vldo28", VLDO28, vldo28_voltages, vldo28_idx,
4888c2ecf20Sopenharmony_ci		   MT6358_LDO_VLDO28_CON0_0, 0,
4898c2ecf20Sopenharmony_ci		   MT6358_VLDO28_ANA_CON0, 0x300, 8),
4908c2ecf20Sopenharmony_ci	MT6358_LDO("ldo_vsim2", VSIM2, vsim_voltages, vsim_idx,
4918c2ecf20Sopenharmony_ci		   MT6358_LDO_VSIM2_CON0, 0, MT6358_VSIM2_ANA_CON0, 0xf00, 8),
4928c2ecf20Sopenharmony_ci	MT6358_LDO1("ldo_vsram_proc11", VSRAM_PROC11, 500000, 1293750, 6250,
4938c2ecf20Sopenharmony_ci		    buck_volt_range1, MT6358_LDO_VSRAM_PROC11_DBG0, 0x7f, 8,
4948c2ecf20Sopenharmony_ci		    MT6358_LDO_VSRAM_CON0, 0x7f),
4958c2ecf20Sopenharmony_ci	MT6358_LDO1("ldo_vsram_others", VSRAM_OTHERS, 500000, 1293750, 6250,
4968c2ecf20Sopenharmony_ci		    buck_volt_range1, MT6358_LDO_VSRAM_OTHERS_DBG0, 0x7f, 8,
4978c2ecf20Sopenharmony_ci		    MT6358_LDO_VSRAM_CON2, 0x7f),
4988c2ecf20Sopenharmony_ci	MT6358_LDO1("ldo_vsram_gpu", VSRAM_GPU, 500000, 1293750, 6250,
4998c2ecf20Sopenharmony_ci		    buck_volt_range1, MT6358_LDO_VSRAM_GPU_DBG0, 0x7f, 8,
5008c2ecf20Sopenharmony_ci		    MT6358_LDO_VSRAM_CON3, 0x7f),
5018c2ecf20Sopenharmony_ci	MT6358_LDO1("ldo_vsram_proc12", VSRAM_PROC12, 500000, 1293750, 6250,
5028c2ecf20Sopenharmony_ci		    buck_volt_range1, MT6358_LDO_VSRAM_PROC12_DBG0, 0x7f, 8,
5038c2ecf20Sopenharmony_ci		    MT6358_LDO_VSRAM_CON1, 0x7f),
5048c2ecf20Sopenharmony_ci};
5058c2ecf20Sopenharmony_ci
5068c2ecf20Sopenharmony_cistatic int mt6358_regulator_probe(struct platform_device *pdev)
5078c2ecf20Sopenharmony_ci{
5088c2ecf20Sopenharmony_ci	struct mt6397_chip *mt6397 = dev_get_drvdata(pdev->dev.parent);
5098c2ecf20Sopenharmony_ci	struct regulator_config config = {};
5108c2ecf20Sopenharmony_ci	struct regulator_dev *rdev;
5118c2ecf20Sopenharmony_ci	int i;
5128c2ecf20Sopenharmony_ci
5138c2ecf20Sopenharmony_ci	for (i = 0; i < MT6358_MAX_REGULATOR; i++) {
5148c2ecf20Sopenharmony_ci		config.dev = &pdev->dev;
5158c2ecf20Sopenharmony_ci		config.driver_data = &mt6358_regulators[i];
5168c2ecf20Sopenharmony_ci		config.regmap = mt6397->regmap;
5178c2ecf20Sopenharmony_ci
5188c2ecf20Sopenharmony_ci		rdev = devm_regulator_register(&pdev->dev,
5198c2ecf20Sopenharmony_ci					       &mt6358_regulators[i].desc,
5208c2ecf20Sopenharmony_ci					       &config);
5218c2ecf20Sopenharmony_ci		if (IS_ERR(rdev)) {
5228c2ecf20Sopenharmony_ci			dev_err(&pdev->dev, "failed to register %s\n",
5238c2ecf20Sopenharmony_ci				mt6358_regulators[i].desc.name);
5248c2ecf20Sopenharmony_ci			return PTR_ERR(rdev);
5258c2ecf20Sopenharmony_ci		}
5268c2ecf20Sopenharmony_ci	}
5278c2ecf20Sopenharmony_ci
5288c2ecf20Sopenharmony_ci	return 0;
5298c2ecf20Sopenharmony_ci}
5308c2ecf20Sopenharmony_ci
5318c2ecf20Sopenharmony_cistatic const struct platform_device_id mt6358_platform_ids[] = {
5328c2ecf20Sopenharmony_ci	{"mt6358-regulator", 0},
5338c2ecf20Sopenharmony_ci	{ /* sentinel */ },
5348c2ecf20Sopenharmony_ci};
5358c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(platform, mt6358_platform_ids);
5368c2ecf20Sopenharmony_ci
5378c2ecf20Sopenharmony_cistatic struct platform_driver mt6358_regulator_driver = {
5388c2ecf20Sopenharmony_ci	.driver = {
5398c2ecf20Sopenharmony_ci		.name = "mt6358-regulator",
5408c2ecf20Sopenharmony_ci	},
5418c2ecf20Sopenharmony_ci	.probe = mt6358_regulator_probe,
5428c2ecf20Sopenharmony_ci	.id_table = mt6358_platform_ids,
5438c2ecf20Sopenharmony_ci};
5448c2ecf20Sopenharmony_ci
5458c2ecf20Sopenharmony_cimodule_platform_driver(mt6358_regulator_driver);
5468c2ecf20Sopenharmony_ci
5478c2ecf20Sopenharmony_ciMODULE_AUTHOR("Hsin-Hsiung Wang <hsin-hsiung.wang@mediatek.com>");
5488c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Regulator Driver for MediaTek MT6358 PMIC");
5498c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
550