18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci// 38c2ecf20Sopenharmony_ci// Copyright (c) 2014 MediaTek Inc. 48c2ecf20Sopenharmony_ci// Author: Flora Fu <flora.fu@mediatek.com> 58c2ecf20Sopenharmony_ci 68c2ecf20Sopenharmony_ci#include <linux/module.h> 78c2ecf20Sopenharmony_ci#include <linux/of.h> 88c2ecf20Sopenharmony_ci#include <linux/platform_device.h> 98c2ecf20Sopenharmony_ci#include <linux/regmap.h> 108c2ecf20Sopenharmony_ci#include <linux/mfd/mt6397/core.h> 118c2ecf20Sopenharmony_ci#include <linux/mfd/mt6397/registers.h> 128c2ecf20Sopenharmony_ci#include <linux/regulator/driver.h> 138c2ecf20Sopenharmony_ci#include <linux/regulator/machine.h> 148c2ecf20Sopenharmony_ci#include <linux/regulator/mt6397-regulator.h> 158c2ecf20Sopenharmony_ci#include <linux/regulator/of_regulator.h> 168c2ecf20Sopenharmony_ci#include <dt-bindings/regulator/mediatek,mt6397-regulator.h> 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_ci/* 198c2ecf20Sopenharmony_ci * MT6397 regulators' information 208c2ecf20Sopenharmony_ci * 218c2ecf20Sopenharmony_ci * @desc: standard fields of regulator description. 228c2ecf20Sopenharmony_ci * @qi: Mask for query enable signal status of regulators 238c2ecf20Sopenharmony_ci * @vselon_reg: Register sections for hardware control mode of bucks 248c2ecf20Sopenharmony_ci * @vselctrl_reg: Register for controlling the buck control mode. 258c2ecf20Sopenharmony_ci * @vselctrl_mask: Mask for query buck's voltage control mode. 268c2ecf20Sopenharmony_ci */ 278c2ecf20Sopenharmony_cistruct mt6397_regulator_info { 288c2ecf20Sopenharmony_ci struct regulator_desc desc; 298c2ecf20Sopenharmony_ci u32 qi; 308c2ecf20Sopenharmony_ci u32 vselon_reg; 318c2ecf20Sopenharmony_ci u32 vselctrl_reg; 328c2ecf20Sopenharmony_ci u32 vselctrl_mask; 338c2ecf20Sopenharmony_ci u32 modeset_reg; 348c2ecf20Sopenharmony_ci u32 modeset_mask; 358c2ecf20Sopenharmony_ci u32 modeset_shift; 368c2ecf20Sopenharmony_ci}; 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_ci#define MT6397_BUCK(match, vreg, min, max, step, volt_ranges, enreg, \ 398c2ecf20Sopenharmony_ci vosel, vosel_mask, voselon, vosel_ctrl, _modeset_reg, \ 408c2ecf20Sopenharmony_ci _modeset_shift) \ 418c2ecf20Sopenharmony_ci[MT6397_ID_##vreg] = { \ 428c2ecf20Sopenharmony_ci .desc = { \ 438c2ecf20Sopenharmony_ci .name = #vreg, \ 448c2ecf20Sopenharmony_ci .of_match = of_match_ptr(match), \ 458c2ecf20Sopenharmony_ci .ops = &mt6397_volt_range_ops, \ 468c2ecf20Sopenharmony_ci .type = REGULATOR_VOLTAGE, \ 478c2ecf20Sopenharmony_ci .id = MT6397_ID_##vreg, \ 488c2ecf20Sopenharmony_ci .owner = THIS_MODULE, \ 498c2ecf20Sopenharmony_ci .n_voltages = (max - min)/step + 1, \ 508c2ecf20Sopenharmony_ci .linear_ranges = volt_ranges, \ 518c2ecf20Sopenharmony_ci .n_linear_ranges = ARRAY_SIZE(volt_ranges), \ 528c2ecf20Sopenharmony_ci .vsel_reg = vosel, \ 538c2ecf20Sopenharmony_ci .vsel_mask = vosel_mask, \ 548c2ecf20Sopenharmony_ci .enable_reg = enreg, \ 558c2ecf20Sopenharmony_ci .enable_mask = BIT(0), \ 568c2ecf20Sopenharmony_ci .of_map_mode = mt6397_map_mode, \ 578c2ecf20Sopenharmony_ci }, \ 588c2ecf20Sopenharmony_ci .qi = BIT(13), \ 598c2ecf20Sopenharmony_ci .vselon_reg = voselon, \ 608c2ecf20Sopenharmony_ci .vselctrl_reg = vosel_ctrl, \ 618c2ecf20Sopenharmony_ci .vselctrl_mask = BIT(1), \ 628c2ecf20Sopenharmony_ci .modeset_reg = _modeset_reg, \ 638c2ecf20Sopenharmony_ci .modeset_mask = BIT(_modeset_shift), \ 648c2ecf20Sopenharmony_ci .modeset_shift = _modeset_shift \ 658c2ecf20Sopenharmony_ci} 668c2ecf20Sopenharmony_ci 678c2ecf20Sopenharmony_ci#define MT6397_LDO(match, vreg, ldo_volt_table, enreg, enbit, vosel, \ 688c2ecf20Sopenharmony_ci vosel_mask) \ 698c2ecf20Sopenharmony_ci[MT6397_ID_##vreg] = { \ 708c2ecf20Sopenharmony_ci .desc = { \ 718c2ecf20Sopenharmony_ci .name = #vreg, \ 728c2ecf20Sopenharmony_ci .of_match = of_match_ptr(match), \ 738c2ecf20Sopenharmony_ci .ops = &mt6397_volt_table_ops, \ 748c2ecf20Sopenharmony_ci .type = REGULATOR_VOLTAGE, \ 758c2ecf20Sopenharmony_ci .id = MT6397_ID_##vreg, \ 768c2ecf20Sopenharmony_ci .owner = THIS_MODULE, \ 778c2ecf20Sopenharmony_ci .n_voltages = ARRAY_SIZE(ldo_volt_table), \ 788c2ecf20Sopenharmony_ci .volt_table = ldo_volt_table, \ 798c2ecf20Sopenharmony_ci .vsel_reg = vosel, \ 808c2ecf20Sopenharmony_ci .vsel_mask = vosel_mask, \ 818c2ecf20Sopenharmony_ci .enable_reg = enreg, \ 828c2ecf20Sopenharmony_ci .enable_mask = BIT(enbit), \ 838c2ecf20Sopenharmony_ci }, \ 848c2ecf20Sopenharmony_ci .qi = BIT(15), \ 858c2ecf20Sopenharmony_ci} 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_ci#define MT6397_REG_FIXED(match, vreg, enreg, enbit, volt) \ 888c2ecf20Sopenharmony_ci[MT6397_ID_##vreg] = { \ 898c2ecf20Sopenharmony_ci .desc = { \ 908c2ecf20Sopenharmony_ci .name = #vreg, \ 918c2ecf20Sopenharmony_ci .of_match = of_match_ptr(match), \ 928c2ecf20Sopenharmony_ci .ops = &mt6397_volt_fixed_ops, \ 938c2ecf20Sopenharmony_ci .type = REGULATOR_VOLTAGE, \ 948c2ecf20Sopenharmony_ci .id = MT6397_ID_##vreg, \ 958c2ecf20Sopenharmony_ci .owner = THIS_MODULE, \ 968c2ecf20Sopenharmony_ci .n_voltages = 1, \ 978c2ecf20Sopenharmony_ci .enable_reg = enreg, \ 988c2ecf20Sopenharmony_ci .enable_mask = BIT(enbit), \ 998c2ecf20Sopenharmony_ci .min_uV = volt, \ 1008c2ecf20Sopenharmony_ci }, \ 1018c2ecf20Sopenharmony_ci .qi = BIT(15), \ 1028c2ecf20Sopenharmony_ci} 1038c2ecf20Sopenharmony_ci 1048c2ecf20Sopenharmony_cistatic const struct linear_range buck_volt_range1[] = { 1058c2ecf20Sopenharmony_ci REGULATOR_LINEAR_RANGE(700000, 0, 0x7f, 6250), 1068c2ecf20Sopenharmony_ci}; 1078c2ecf20Sopenharmony_ci 1088c2ecf20Sopenharmony_cistatic const struct linear_range buck_volt_range2[] = { 1098c2ecf20Sopenharmony_ci REGULATOR_LINEAR_RANGE(800000, 0, 0x7f, 6250), 1108c2ecf20Sopenharmony_ci}; 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_cistatic const struct linear_range buck_volt_range3[] = { 1138c2ecf20Sopenharmony_ci REGULATOR_LINEAR_RANGE(1500000, 0, 0x1f, 20000), 1148c2ecf20Sopenharmony_ci}; 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_cistatic const unsigned int ldo_volt_table1[] = { 1178c2ecf20Sopenharmony_ci 1500000, 1800000, 2500000, 2800000, 1188c2ecf20Sopenharmony_ci}; 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_cistatic const unsigned int ldo_volt_table2[] = { 1218c2ecf20Sopenharmony_ci 1800000, 3300000, 1228c2ecf20Sopenharmony_ci}; 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_cistatic const unsigned int ldo_volt_table3[] = { 1258c2ecf20Sopenharmony_ci 3000000, 3300000, 1268c2ecf20Sopenharmony_ci}; 1278c2ecf20Sopenharmony_ci 1288c2ecf20Sopenharmony_cistatic const unsigned int ldo_volt_table4[] = { 1298c2ecf20Sopenharmony_ci 1220000, 1300000, 1500000, 1800000, 2500000, 2800000, 3000000, 3300000, 1308c2ecf20Sopenharmony_ci}; 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_cistatic const unsigned int ldo_volt_table5[] = { 1338c2ecf20Sopenharmony_ci 1200000, 1300000, 1500000, 1800000, 2500000, 2800000, 3000000, 3300000, 1348c2ecf20Sopenharmony_ci}; 1358c2ecf20Sopenharmony_ci 1368c2ecf20Sopenharmony_cistatic const unsigned int ldo_volt_table5_v2[] = { 1378c2ecf20Sopenharmony_ci 1200000, 1000000, 1500000, 1800000, 2500000, 2800000, 3000000, 3300000, 1388c2ecf20Sopenharmony_ci}; 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_cistatic const unsigned int ldo_volt_table6[] = { 1418c2ecf20Sopenharmony_ci 1200000, 1300000, 1500000, 1800000, 2500000, 2800000, 3000000, 2000000, 1428c2ecf20Sopenharmony_ci}; 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_cistatic const unsigned int ldo_volt_table7[] = { 1458c2ecf20Sopenharmony_ci 1300000, 1500000, 1800000, 2000000, 2500000, 2800000, 3000000, 3300000, 1468c2ecf20Sopenharmony_ci}; 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_cistatic unsigned int mt6397_map_mode(unsigned int mode) 1498c2ecf20Sopenharmony_ci{ 1508c2ecf20Sopenharmony_ci switch (mode) { 1518c2ecf20Sopenharmony_ci case MT6397_BUCK_MODE_AUTO: 1528c2ecf20Sopenharmony_ci return REGULATOR_MODE_NORMAL; 1538c2ecf20Sopenharmony_ci case MT6397_BUCK_MODE_FORCE_PWM: 1548c2ecf20Sopenharmony_ci return REGULATOR_MODE_FAST; 1558c2ecf20Sopenharmony_ci default: 1568c2ecf20Sopenharmony_ci return REGULATOR_MODE_INVALID; 1578c2ecf20Sopenharmony_ci } 1588c2ecf20Sopenharmony_ci} 1598c2ecf20Sopenharmony_ci 1608c2ecf20Sopenharmony_cistatic int mt6397_regulator_set_mode(struct regulator_dev *rdev, 1618c2ecf20Sopenharmony_ci unsigned int mode) 1628c2ecf20Sopenharmony_ci{ 1638c2ecf20Sopenharmony_ci struct mt6397_regulator_info *info = rdev_get_drvdata(rdev); 1648c2ecf20Sopenharmony_ci int ret, val; 1658c2ecf20Sopenharmony_ci 1668c2ecf20Sopenharmony_ci switch (mode) { 1678c2ecf20Sopenharmony_ci case REGULATOR_MODE_FAST: 1688c2ecf20Sopenharmony_ci val = MT6397_BUCK_MODE_FORCE_PWM; 1698c2ecf20Sopenharmony_ci break; 1708c2ecf20Sopenharmony_ci case REGULATOR_MODE_NORMAL: 1718c2ecf20Sopenharmony_ci val = MT6397_BUCK_MODE_AUTO; 1728c2ecf20Sopenharmony_ci break; 1738c2ecf20Sopenharmony_ci default: 1748c2ecf20Sopenharmony_ci ret = -EINVAL; 1758c2ecf20Sopenharmony_ci goto err_mode; 1768c2ecf20Sopenharmony_ci } 1778c2ecf20Sopenharmony_ci 1788c2ecf20Sopenharmony_ci dev_dbg(&rdev->dev, "mt6397 buck set_mode %#x, %#x, %#x, %#x\n", 1798c2ecf20Sopenharmony_ci info->modeset_reg, info->modeset_mask, 1808c2ecf20Sopenharmony_ci info->modeset_shift, val); 1818c2ecf20Sopenharmony_ci 1828c2ecf20Sopenharmony_ci val <<= info->modeset_shift; 1838c2ecf20Sopenharmony_ci ret = regmap_update_bits(rdev->regmap, info->modeset_reg, 1848c2ecf20Sopenharmony_ci info->modeset_mask, val); 1858c2ecf20Sopenharmony_cierr_mode: 1868c2ecf20Sopenharmony_ci if (ret != 0) { 1878c2ecf20Sopenharmony_ci dev_err(&rdev->dev, 1888c2ecf20Sopenharmony_ci "Failed to set mt6397 buck mode: %d\n", ret); 1898c2ecf20Sopenharmony_ci return ret; 1908c2ecf20Sopenharmony_ci } 1918c2ecf20Sopenharmony_ci 1928c2ecf20Sopenharmony_ci return 0; 1938c2ecf20Sopenharmony_ci} 1948c2ecf20Sopenharmony_ci 1958c2ecf20Sopenharmony_cistatic unsigned int mt6397_regulator_get_mode(struct regulator_dev *rdev) 1968c2ecf20Sopenharmony_ci{ 1978c2ecf20Sopenharmony_ci struct mt6397_regulator_info *info = rdev_get_drvdata(rdev); 1988c2ecf20Sopenharmony_ci int ret, regval; 1998c2ecf20Sopenharmony_ci 2008c2ecf20Sopenharmony_ci ret = regmap_read(rdev->regmap, info->modeset_reg, ®val); 2018c2ecf20Sopenharmony_ci if (ret != 0) { 2028c2ecf20Sopenharmony_ci dev_err(&rdev->dev, 2038c2ecf20Sopenharmony_ci "Failed to get mt6397 buck mode: %d\n", ret); 2048c2ecf20Sopenharmony_ci return ret; 2058c2ecf20Sopenharmony_ci } 2068c2ecf20Sopenharmony_ci 2078c2ecf20Sopenharmony_ci switch ((regval & info->modeset_mask) >> info->modeset_shift) { 2088c2ecf20Sopenharmony_ci case MT6397_BUCK_MODE_AUTO: 2098c2ecf20Sopenharmony_ci return REGULATOR_MODE_NORMAL; 2108c2ecf20Sopenharmony_ci case MT6397_BUCK_MODE_FORCE_PWM: 2118c2ecf20Sopenharmony_ci return REGULATOR_MODE_FAST; 2128c2ecf20Sopenharmony_ci default: 2138c2ecf20Sopenharmony_ci return -EINVAL; 2148c2ecf20Sopenharmony_ci } 2158c2ecf20Sopenharmony_ci} 2168c2ecf20Sopenharmony_ci 2178c2ecf20Sopenharmony_cistatic int mt6397_get_status(struct regulator_dev *rdev) 2188c2ecf20Sopenharmony_ci{ 2198c2ecf20Sopenharmony_ci int ret; 2208c2ecf20Sopenharmony_ci u32 regval; 2218c2ecf20Sopenharmony_ci struct mt6397_regulator_info *info = rdev_get_drvdata(rdev); 2228c2ecf20Sopenharmony_ci 2238c2ecf20Sopenharmony_ci ret = regmap_read(rdev->regmap, info->desc.enable_reg, ®val); 2248c2ecf20Sopenharmony_ci if (ret != 0) { 2258c2ecf20Sopenharmony_ci dev_err(&rdev->dev, "Failed to get enable reg: %d\n", ret); 2268c2ecf20Sopenharmony_ci return ret; 2278c2ecf20Sopenharmony_ci } 2288c2ecf20Sopenharmony_ci 2298c2ecf20Sopenharmony_ci return (regval & info->qi) ? REGULATOR_STATUS_ON : REGULATOR_STATUS_OFF; 2308c2ecf20Sopenharmony_ci} 2318c2ecf20Sopenharmony_ci 2328c2ecf20Sopenharmony_cistatic const struct regulator_ops mt6397_volt_range_ops = { 2338c2ecf20Sopenharmony_ci .list_voltage = regulator_list_voltage_linear_range, 2348c2ecf20Sopenharmony_ci .map_voltage = regulator_map_voltage_linear_range, 2358c2ecf20Sopenharmony_ci .set_voltage_sel = regulator_set_voltage_sel_regmap, 2368c2ecf20Sopenharmony_ci .get_voltage_sel = regulator_get_voltage_sel_regmap, 2378c2ecf20Sopenharmony_ci .set_voltage_time_sel = regulator_set_voltage_time_sel, 2388c2ecf20Sopenharmony_ci .enable = regulator_enable_regmap, 2398c2ecf20Sopenharmony_ci .disable = regulator_disable_regmap, 2408c2ecf20Sopenharmony_ci .is_enabled = regulator_is_enabled_regmap, 2418c2ecf20Sopenharmony_ci .get_status = mt6397_get_status, 2428c2ecf20Sopenharmony_ci .set_mode = mt6397_regulator_set_mode, 2438c2ecf20Sopenharmony_ci .get_mode = mt6397_regulator_get_mode, 2448c2ecf20Sopenharmony_ci}; 2458c2ecf20Sopenharmony_ci 2468c2ecf20Sopenharmony_cistatic const struct regulator_ops mt6397_volt_table_ops = { 2478c2ecf20Sopenharmony_ci .list_voltage = regulator_list_voltage_table, 2488c2ecf20Sopenharmony_ci .map_voltage = regulator_map_voltage_iterate, 2498c2ecf20Sopenharmony_ci .set_voltage_sel = regulator_set_voltage_sel_regmap, 2508c2ecf20Sopenharmony_ci .get_voltage_sel = regulator_get_voltage_sel_regmap, 2518c2ecf20Sopenharmony_ci .set_voltage_time_sel = regulator_set_voltage_time_sel, 2528c2ecf20Sopenharmony_ci .enable = regulator_enable_regmap, 2538c2ecf20Sopenharmony_ci .disable = regulator_disable_regmap, 2548c2ecf20Sopenharmony_ci .is_enabled = regulator_is_enabled_regmap, 2558c2ecf20Sopenharmony_ci .get_status = mt6397_get_status, 2568c2ecf20Sopenharmony_ci}; 2578c2ecf20Sopenharmony_ci 2588c2ecf20Sopenharmony_cistatic const struct regulator_ops mt6397_volt_fixed_ops = { 2598c2ecf20Sopenharmony_ci .list_voltage = regulator_list_voltage_linear, 2608c2ecf20Sopenharmony_ci .enable = regulator_enable_regmap, 2618c2ecf20Sopenharmony_ci .disable = regulator_disable_regmap, 2628c2ecf20Sopenharmony_ci .is_enabled = regulator_is_enabled_regmap, 2638c2ecf20Sopenharmony_ci .get_status = mt6397_get_status, 2648c2ecf20Sopenharmony_ci}; 2658c2ecf20Sopenharmony_ci 2668c2ecf20Sopenharmony_ci/* The array is indexed by id(MT6397_ID_XXX) */ 2678c2ecf20Sopenharmony_cistatic struct mt6397_regulator_info mt6397_regulators[] = { 2688c2ecf20Sopenharmony_ci MT6397_BUCK("buck_vpca15", VPCA15, 700000, 1493750, 6250, 2698c2ecf20Sopenharmony_ci buck_volt_range1, MT6397_VCA15_CON7, MT6397_VCA15_CON9, 0x7f, 2708c2ecf20Sopenharmony_ci MT6397_VCA15_CON10, MT6397_VCA15_CON5, MT6397_VCA15_CON2, 11), 2718c2ecf20Sopenharmony_ci MT6397_BUCK("buck_vpca7", VPCA7, 700000, 1493750, 6250, 2728c2ecf20Sopenharmony_ci buck_volt_range1, MT6397_VPCA7_CON7, MT6397_VPCA7_CON9, 0x7f, 2738c2ecf20Sopenharmony_ci MT6397_VPCA7_CON10, MT6397_VPCA7_CON5, MT6397_VPCA7_CON2, 8), 2748c2ecf20Sopenharmony_ci MT6397_BUCK("buck_vsramca15", VSRAMCA15, 700000, 1493750, 6250, 2758c2ecf20Sopenharmony_ci buck_volt_range1, MT6397_VSRMCA15_CON7, MT6397_VSRMCA15_CON9, 2768c2ecf20Sopenharmony_ci 0x7f, MT6397_VSRMCA15_CON10, MT6397_VSRMCA15_CON5, 2778c2ecf20Sopenharmony_ci MT6397_VSRMCA15_CON2, 8), 2788c2ecf20Sopenharmony_ci MT6397_BUCK("buck_vsramca7", VSRAMCA7, 700000, 1493750, 6250, 2798c2ecf20Sopenharmony_ci buck_volt_range1, MT6397_VSRMCA7_CON7, MT6397_VSRMCA7_CON9, 2808c2ecf20Sopenharmony_ci 0x7f, MT6397_VSRMCA7_CON10, MT6397_VSRMCA7_CON5, 2818c2ecf20Sopenharmony_ci MT6397_VSRMCA7_CON2, 8), 2828c2ecf20Sopenharmony_ci MT6397_BUCK("buck_vcore", VCORE, 700000, 1493750, 6250, 2838c2ecf20Sopenharmony_ci buck_volt_range1, MT6397_VCORE_CON7, MT6397_VCORE_CON9, 0x7f, 2848c2ecf20Sopenharmony_ci MT6397_VCORE_CON10, MT6397_VCORE_CON5, MT6397_VCORE_CON2, 8), 2858c2ecf20Sopenharmony_ci MT6397_BUCK("buck_vgpu", VGPU, 700000, 1493750, 6250, buck_volt_range1, 2868c2ecf20Sopenharmony_ci MT6397_VGPU_CON7, MT6397_VGPU_CON9, 0x7f, 2878c2ecf20Sopenharmony_ci MT6397_VGPU_CON10, MT6397_VGPU_CON5, MT6397_VGPU_CON2, 8), 2888c2ecf20Sopenharmony_ci MT6397_BUCK("buck_vdrm", VDRM, 800000, 1593750, 6250, buck_volt_range2, 2898c2ecf20Sopenharmony_ci MT6397_VDRM_CON7, MT6397_VDRM_CON9, 0x7f, 2908c2ecf20Sopenharmony_ci MT6397_VDRM_CON10, MT6397_VDRM_CON5, MT6397_VDRM_CON2, 8), 2918c2ecf20Sopenharmony_ci MT6397_BUCK("buck_vio18", VIO18, 1500000, 2120000, 20000, 2928c2ecf20Sopenharmony_ci buck_volt_range3, MT6397_VIO18_CON7, MT6397_VIO18_CON9, 0x1f, 2938c2ecf20Sopenharmony_ci MT6397_VIO18_CON10, MT6397_VIO18_CON5, MT6397_VIO18_CON2, 8), 2948c2ecf20Sopenharmony_ci MT6397_REG_FIXED("ldo_vtcxo", VTCXO, MT6397_ANALDO_CON0, 10, 2800000), 2958c2ecf20Sopenharmony_ci MT6397_REG_FIXED("ldo_va28", VA28, MT6397_ANALDO_CON1, 14, 2800000), 2968c2ecf20Sopenharmony_ci MT6397_LDO("ldo_vcama", VCAMA, ldo_volt_table1, 2978c2ecf20Sopenharmony_ci MT6397_ANALDO_CON2, 15, MT6397_ANALDO_CON6, 0xC0), 2988c2ecf20Sopenharmony_ci MT6397_REG_FIXED("ldo_vio28", VIO28, MT6397_DIGLDO_CON0, 14, 2800000), 2998c2ecf20Sopenharmony_ci MT6397_REG_FIXED("ldo_vusb", VUSB, MT6397_DIGLDO_CON1, 14, 3300000), 3008c2ecf20Sopenharmony_ci MT6397_LDO("ldo_vmc", VMC, ldo_volt_table2, 3018c2ecf20Sopenharmony_ci MT6397_DIGLDO_CON2, 12, MT6397_DIGLDO_CON29, 0x10), 3028c2ecf20Sopenharmony_ci MT6397_LDO("ldo_vmch", VMCH, ldo_volt_table3, 3038c2ecf20Sopenharmony_ci MT6397_DIGLDO_CON3, 14, MT6397_DIGLDO_CON17, 0x80), 3048c2ecf20Sopenharmony_ci MT6397_LDO("ldo_vemc3v3", VEMC3V3, ldo_volt_table3, 3058c2ecf20Sopenharmony_ci MT6397_DIGLDO_CON4, 14, MT6397_DIGLDO_CON18, 0x10), 3068c2ecf20Sopenharmony_ci MT6397_LDO("ldo_vgp1", VGP1, ldo_volt_table4, 3078c2ecf20Sopenharmony_ci MT6397_DIGLDO_CON5, 15, MT6397_DIGLDO_CON19, 0xE0), 3088c2ecf20Sopenharmony_ci MT6397_LDO("ldo_vgp2", VGP2, ldo_volt_table5, 3098c2ecf20Sopenharmony_ci MT6397_DIGLDO_CON6, 15, MT6397_DIGLDO_CON20, 0xE0), 3108c2ecf20Sopenharmony_ci MT6397_LDO("ldo_vgp3", VGP3, ldo_volt_table5, 3118c2ecf20Sopenharmony_ci MT6397_DIGLDO_CON7, 15, MT6397_DIGLDO_CON21, 0xE0), 3128c2ecf20Sopenharmony_ci MT6397_LDO("ldo_vgp4", VGP4, ldo_volt_table5, 3138c2ecf20Sopenharmony_ci MT6397_DIGLDO_CON8, 15, MT6397_DIGLDO_CON22, 0xE0), 3148c2ecf20Sopenharmony_ci MT6397_LDO("ldo_vgp5", VGP5, ldo_volt_table6, 3158c2ecf20Sopenharmony_ci MT6397_DIGLDO_CON9, 15, MT6397_DIGLDO_CON23, 0xE0), 3168c2ecf20Sopenharmony_ci MT6397_LDO("ldo_vgp6", VGP6, ldo_volt_table5, 3178c2ecf20Sopenharmony_ci MT6397_DIGLDO_CON10, 15, MT6397_DIGLDO_CON33, 0xE0), 3188c2ecf20Sopenharmony_ci MT6397_LDO("ldo_vibr", VIBR, ldo_volt_table7, 3198c2ecf20Sopenharmony_ci MT6397_DIGLDO_CON24, 15, MT6397_DIGLDO_CON25, 0xE00), 3208c2ecf20Sopenharmony_ci}; 3218c2ecf20Sopenharmony_ci 3228c2ecf20Sopenharmony_cistatic int mt6397_set_buck_vosel_reg(struct platform_device *pdev) 3238c2ecf20Sopenharmony_ci{ 3248c2ecf20Sopenharmony_ci struct mt6397_chip *mt6397 = dev_get_drvdata(pdev->dev.parent); 3258c2ecf20Sopenharmony_ci int i; 3268c2ecf20Sopenharmony_ci u32 regval; 3278c2ecf20Sopenharmony_ci 3288c2ecf20Sopenharmony_ci for (i = 0; i < MT6397_MAX_REGULATOR; i++) { 3298c2ecf20Sopenharmony_ci if (mt6397_regulators[i].vselctrl_reg) { 3308c2ecf20Sopenharmony_ci if (regmap_read(mt6397->regmap, 3318c2ecf20Sopenharmony_ci mt6397_regulators[i].vselctrl_reg, 3328c2ecf20Sopenharmony_ci ®val) < 0) { 3338c2ecf20Sopenharmony_ci dev_err(&pdev->dev, 3348c2ecf20Sopenharmony_ci "Failed to read buck ctrl\n"); 3358c2ecf20Sopenharmony_ci return -EIO; 3368c2ecf20Sopenharmony_ci } 3378c2ecf20Sopenharmony_ci 3388c2ecf20Sopenharmony_ci if (regval & mt6397_regulators[i].vselctrl_mask) { 3398c2ecf20Sopenharmony_ci mt6397_regulators[i].desc.vsel_reg = 3408c2ecf20Sopenharmony_ci mt6397_regulators[i].vselon_reg; 3418c2ecf20Sopenharmony_ci } 3428c2ecf20Sopenharmony_ci } 3438c2ecf20Sopenharmony_ci } 3448c2ecf20Sopenharmony_ci 3458c2ecf20Sopenharmony_ci return 0; 3468c2ecf20Sopenharmony_ci} 3478c2ecf20Sopenharmony_ci 3488c2ecf20Sopenharmony_cistatic int mt6397_regulator_probe(struct platform_device *pdev) 3498c2ecf20Sopenharmony_ci{ 3508c2ecf20Sopenharmony_ci struct mt6397_chip *mt6397 = dev_get_drvdata(pdev->dev.parent); 3518c2ecf20Sopenharmony_ci struct regulator_config config = {}; 3528c2ecf20Sopenharmony_ci struct regulator_dev *rdev; 3538c2ecf20Sopenharmony_ci int i; 3548c2ecf20Sopenharmony_ci u32 reg_value, version; 3558c2ecf20Sopenharmony_ci 3568c2ecf20Sopenharmony_ci /* Query buck controller to select activated voltage register part */ 3578c2ecf20Sopenharmony_ci if (mt6397_set_buck_vosel_reg(pdev)) 3588c2ecf20Sopenharmony_ci return -EIO; 3598c2ecf20Sopenharmony_ci 3608c2ecf20Sopenharmony_ci /* Read PMIC chip revision to update constraints and voltage table */ 3618c2ecf20Sopenharmony_ci if (regmap_read(mt6397->regmap, MT6397_CID, ®_value) < 0) { 3628c2ecf20Sopenharmony_ci dev_err(&pdev->dev, "Failed to read Chip ID\n"); 3638c2ecf20Sopenharmony_ci return -EIO; 3648c2ecf20Sopenharmony_ci } 3658c2ecf20Sopenharmony_ci dev_info(&pdev->dev, "Chip ID = 0x%x\n", reg_value); 3668c2ecf20Sopenharmony_ci 3678c2ecf20Sopenharmony_ci version = (reg_value & 0xFF); 3688c2ecf20Sopenharmony_ci switch (version) { 3698c2ecf20Sopenharmony_ci case MT6397_REGULATOR_ID91: 3708c2ecf20Sopenharmony_ci mt6397_regulators[MT6397_ID_VGP2].desc.volt_table = 3718c2ecf20Sopenharmony_ci ldo_volt_table5_v2; 3728c2ecf20Sopenharmony_ci break; 3738c2ecf20Sopenharmony_ci default: 3748c2ecf20Sopenharmony_ci break; 3758c2ecf20Sopenharmony_ci } 3768c2ecf20Sopenharmony_ci 3778c2ecf20Sopenharmony_ci for (i = 0; i < MT6397_MAX_REGULATOR; i++) { 3788c2ecf20Sopenharmony_ci config.dev = &pdev->dev; 3798c2ecf20Sopenharmony_ci config.driver_data = &mt6397_regulators[i]; 3808c2ecf20Sopenharmony_ci config.regmap = mt6397->regmap; 3818c2ecf20Sopenharmony_ci rdev = devm_regulator_register(&pdev->dev, 3828c2ecf20Sopenharmony_ci &mt6397_regulators[i].desc, &config); 3838c2ecf20Sopenharmony_ci if (IS_ERR(rdev)) { 3848c2ecf20Sopenharmony_ci dev_err(&pdev->dev, "failed to register %s\n", 3858c2ecf20Sopenharmony_ci mt6397_regulators[i].desc.name); 3868c2ecf20Sopenharmony_ci return PTR_ERR(rdev); 3878c2ecf20Sopenharmony_ci } 3888c2ecf20Sopenharmony_ci } 3898c2ecf20Sopenharmony_ci 3908c2ecf20Sopenharmony_ci return 0; 3918c2ecf20Sopenharmony_ci} 3928c2ecf20Sopenharmony_ci 3938c2ecf20Sopenharmony_cistatic const struct platform_device_id mt6397_platform_ids[] = { 3948c2ecf20Sopenharmony_ci {"mt6397-regulator", 0}, 3958c2ecf20Sopenharmony_ci { /* sentinel */ }, 3968c2ecf20Sopenharmony_ci}; 3978c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(platform, mt6397_platform_ids); 3988c2ecf20Sopenharmony_ci 3998c2ecf20Sopenharmony_cistatic const struct of_device_id mt6397_of_match[] = { 4008c2ecf20Sopenharmony_ci { .compatible = "mediatek,mt6397-regulator", }, 4018c2ecf20Sopenharmony_ci { /* sentinel */ }, 4028c2ecf20Sopenharmony_ci}; 4038c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, mt6397_of_match); 4048c2ecf20Sopenharmony_ci 4058c2ecf20Sopenharmony_cistatic struct platform_driver mt6397_regulator_driver = { 4068c2ecf20Sopenharmony_ci .driver = { 4078c2ecf20Sopenharmony_ci .name = "mt6397-regulator", 4088c2ecf20Sopenharmony_ci .of_match_table = of_match_ptr(mt6397_of_match), 4098c2ecf20Sopenharmony_ci }, 4108c2ecf20Sopenharmony_ci .probe = mt6397_regulator_probe, 4118c2ecf20Sopenharmony_ci .id_table = mt6397_platform_ids, 4128c2ecf20Sopenharmony_ci}; 4138c2ecf20Sopenharmony_ci 4148c2ecf20Sopenharmony_cimodule_platform_driver(mt6397_regulator_driver); 4158c2ecf20Sopenharmony_ci 4168c2ecf20Sopenharmony_ciMODULE_AUTHOR("Flora Fu <flora.fu@mediatek.com>"); 4178c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Regulator Driver for MediaTek MT6397 PMIC"); 4188c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 419