162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 262306a36Sopenharmony_ci 362306a36Sopenharmony_ci#include <linux/bitops.h> 462306a36Sopenharmony_ci#include <linux/kernel.h> 562306a36Sopenharmony_ci#include <linux/module.h> 662306a36Sopenharmony_ci#include <linux/of.h> 762306a36Sopenharmony_ci#include <linux/platform_device.h> 862306a36Sopenharmony_ci#include <linux/regmap.h> 962306a36Sopenharmony_ci#include <linux/regulator/consumer.h> 1062306a36Sopenharmony_ci#include <linux/regulator/driver.h> 1162306a36Sopenharmony_ci 1262306a36Sopenharmony_cienum { 1362306a36Sopenharmony_ci DSV_OUT_VLCM = 0, 1462306a36Sopenharmony_ci DSV_OUT_VPOS, 1562306a36Sopenharmony_ci DSV_OUT_VNEG, 1662306a36Sopenharmony_ci DSV_OUT_MAX 1762306a36Sopenharmony_ci}; 1862306a36Sopenharmony_ci 1962306a36Sopenharmony_ci#define RT4831_REG_DSVEN 0x09 2062306a36Sopenharmony_ci#define RT4831_REG_VLCM 0x0c 2162306a36Sopenharmony_ci#define RT4831_REG_VPOS 0x0d 2262306a36Sopenharmony_ci#define RT4831_REG_VNEG 0x0e 2362306a36Sopenharmony_ci#define RT4831_REG_FLAGS 0x0f 2462306a36Sopenharmony_ci 2562306a36Sopenharmony_ci#define RT4831_VOLT_MASK GENMASK(5, 0) 2662306a36Sopenharmony_ci#define RT4831_DSVMODE_SHIFT 5 2762306a36Sopenharmony_ci#define RT4831_DSVMODE_MASK GENMASK(7, 5) 2862306a36Sopenharmony_ci#define RT4831_POSADEN_MASK BIT(4) 2962306a36Sopenharmony_ci#define RT4831_NEGADEN_MASK BIT(3) 3062306a36Sopenharmony_ci#define RT4831_POSEN_MASK BIT(2) 3162306a36Sopenharmony_ci#define RT4831_NEGEN_MASK BIT(1) 3262306a36Sopenharmony_ci 3362306a36Sopenharmony_ci#define RT4831_OTP_MASK BIT(6) 3462306a36Sopenharmony_ci#define RT4831_LCMOVP_MASK BIT(5) 3562306a36Sopenharmony_ci#define RT4831_VPOSSCP_MASK BIT(3) 3662306a36Sopenharmony_ci#define RT4831_VNEGSCP_MASK BIT(2) 3762306a36Sopenharmony_ci 3862306a36Sopenharmony_ci#define DSV_MODE_NORMAL (0x4 << RT4831_DSVMODE_SHIFT) 3962306a36Sopenharmony_ci#define DSV_MODE_BYPASS (0x6 << RT4831_DSVMODE_SHIFT) 4062306a36Sopenharmony_ci#define STEP_UV 50000 4162306a36Sopenharmony_ci#define VLCM_MIN_UV 4000000 4262306a36Sopenharmony_ci#define VLCM_MAX_UV 7150000 4362306a36Sopenharmony_ci#define VLCM_N_VOLTAGES ((VLCM_MAX_UV - VLCM_MIN_UV) / STEP_UV + 1) 4462306a36Sopenharmony_ci#define VPN_MIN_UV 4000000 4562306a36Sopenharmony_ci#define VPN_MAX_UV 6500000 4662306a36Sopenharmony_ci#define VPN_N_VOLTAGES ((VPN_MAX_UV - VPN_MIN_UV) / STEP_UV + 1) 4762306a36Sopenharmony_ci 4862306a36Sopenharmony_cistatic int rt4831_get_error_flags(struct regulator_dev *rdev, unsigned int *flags) 4962306a36Sopenharmony_ci{ 5062306a36Sopenharmony_ci struct regmap *regmap = rdev_get_regmap(rdev); 5162306a36Sopenharmony_ci int rid = rdev_get_id(rdev); 5262306a36Sopenharmony_ci unsigned int val, events = 0; 5362306a36Sopenharmony_ci int ret; 5462306a36Sopenharmony_ci 5562306a36Sopenharmony_ci ret = regmap_read(regmap, RT4831_REG_FLAGS, &val); 5662306a36Sopenharmony_ci if (ret) 5762306a36Sopenharmony_ci return ret; 5862306a36Sopenharmony_ci 5962306a36Sopenharmony_ci if (val & RT4831_OTP_MASK) 6062306a36Sopenharmony_ci events |= REGULATOR_ERROR_OVER_TEMP; 6162306a36Sopenharmony_ci 6262306a36Sopenharmony_ci if (rid == DSV_OUT_VLCM && (val & RT4831_LCMOVP_MASK)) 6362306a36Sopenharmony_ci events |= REGULATOR_ERROR_OVER_CURRENT; 6462306a36Sopenharmony_ci 6562306a36Sopenharmony_ci if (rid == DSV_OUT_VPOS && (val & RT4831_VPOSSCP_MASK)) 6662306a36Sopenharmony_ci events |= REGULATOR_ERROR_OVER_CURRENT; 6762306a36Sopenharmony_ci 6862306a36Sopenharmony_ci if (rid == DSV_OUT_VNEG && (val & RT4831_VNEGSCP_MASK)) 6962306a36Sopenharmony_ci events |= REGULATOR_ERROR_OVER_CURRENT; 7062306a36Sopenharmony_ci 7162306a36Sopenharmony_ci *flags = events; 7262306a36Sopenharmony_ci return 0; 7362306a36Sopenharmony_ci} 7462306a36Sopenharmony_ci 7562306a36Sopenharmony_cistatic const struct regulator_ops rt4831_dsvlcm_ops = { 7662306a36Sopenharmony_ci .list_voltage = regulator_list_voltage_linear, 7762306a36Sopenharmony_ci .set_voltage_sel = regulator_set_voltage_sel_regmap, 7862306a36Sopenharmony_ci .get_voltage_sel = regulator_get_voltage_sel_regmap, 7962306a36Sopenharmony_ci .set_bypass = regulator_set_bypass_regmap, 8062306a36Sopenharmony_ci .get_bypass = regulator_get_bypass_regmap, 8162306a36Sopenharmony_ci .get_error_flags = rt4831_get_error_flags, 8262306a36Sopenharmony_ci}; 8362306a36Sopenharmony_ci 8462306a36Sopenharmony_cistatic const struct regulator_ops rt4831_dsvpn_ops = { 8562306a36Sopenharmony_ci .list_voltage = regulator_list_voltage_linear, 8662306a36Sopenharmony_ci .set_voltage_sel = regulator_set_voltage_sel_regmap, 8762306a36Sopenharmony_ci .get_voltage_sel = regulator_get_voltage_sel_regmap, 8862306a36Sopenharmony_ci .enable = regulator_enable_regmap, 8962306a36Sopenharmony_ci .disable = regulator_disable_regmap, 9062306a36Sopenharmony_ci .is_enabled = regulator_is_enabled_regmap, 9162306a36Sopenharmony_ci .set_active_discharge = regulator_set_active_discharge_regmap, 9262306a36Sopenharmony_ci .get_error_flags = rt4831_get_error_flags, 9362306a36Sopenharmony_ci}; 9462306a36Sopenharmony_ci 9562306a36Sopenharmony_cistatic const struct regulator_desc rt4831_regulator_descs[] = { 9662306a36Sopenharmony_ci { 9762306a36Sopenharmony_ci .name = "DSVLCM", 9862306a36Sopenharmony_ci .ops = &rt4831_dsvlcm_ops, 9962306a36Sopenharmony_ci .of_match = of_match_ptr("DSVLCM"), 10062306a36Sopenharmony_ci .regulators_node = of_match_ptr("regulators"), 10162306a36Sopenharmony_ci .type = REGULATOR_VOLTAGE, 10262306a36Sopenharmony_ci .id = DSV_OUT_VLCM, 10362306a36Sopenharmony_ci .n_voltages = VLCM_N_VOLTAGES, 10462306a36Sopenharmony_ci .min_uV = VLCM_MIN_UV, 10562306a36Sopenharmony_ci .uV_step = STEP_UV, 10662306a36Sopenharmony_ci .vsel_reg = RT4831_REG_VLCM, 10762306a36Sopenharmony_ci .vsel_mask = RT4831_VOLT_MASK, 10862306a36Sopenharmony_ci .bypass_reg = RT4831_REG_DSVEN, 10962306a36Sopenharmony_ci .bypass_mask = RT4831_DSVMODE_MASK, 11062306a36Sopenharmony_ci .bypass_val_on = DSV_MODE_BYPASS, 11162306a36Sopenharmony_ci .bypass_val_off = DSV_MODE_NORMAL, 11262306a36Sopenharmony_ci .owner = THIS_MODULE, 11362306a36Sopenharmony_ci }, 11462306a36Sopenharmony_ci { 11562306a36Sopenharmony_ci .name = "DSVP", 11662306a36Sopenharmony_ci .ops = &rt4831_dsvpn_ops, 11762306a36Sopenharmony_ci .of_match = of_match_ptr("DSVP"), 11862306a36Sopenharmony_ci .regulators_node = of_match_ptr("regulators"), 11962306a36Sopenharmony_ci .type = REGULATOR_VOLTAGE, 12062306a36Sopenharmony_ci .id = DSV_OUT_VPOS, 12162306a36Sopenharmony_ci .n_voltages = VPN_N_VOLTAGES, 12262306a36Sopenharmony_ci .min_uV = VPN_MIN_UV, 12362306a36Sopenharmony_ci .uV_step = STEP_UV, 12462306a36Sopenharmony_ci .vsel_reg = RT4831_REG_VPOS, 12562306a36Sopenharmony_ci .vsel_mask = RT4831_VOLT_MASK, 12662306a36Sopenharmony_ci .enable_reg = RT4831_REG_DSVEN, 12762306a36Sopenharmony_ci .enable_mask = RT4831_POSEN_MASK, 12862306a36Sopenharmony_ci .active_discharge_reg = RT4831_REG_DSVEN, 12962306a36Sopenharmony_ci .active_discharge_mask = RT4831_POSADEN_MASK, 13062306a36Sopenharmony_ci .active_discharge_on = RT4831_POSADEN_MASK, 13162306a36Sopenharmony_ci .owner = THIS_MODULE, 13262306a36Sopenharmony_ci }, 13362306a36Sopenharmony_ci { 13462306a36Sopenharmony_ci .name = "DSVN", 13562306a36Sopenharmony_ci .ops = &rt4831_dsvpn_ops, 13662306a36Sopenharmony_ci .of_match = of_match_ptr("DSVN"), 13762306a36Sopenharmony_ci .regulators_node = of_match_ptr("regulators"), 13862306a36Sopenharmony_ci .type = REGULATOR_VOLTAGE, 13962306a36Sopenharmony_ci .id = DSV_OUT_VNEG, 14062306a36Sopenharmony_ci .n_voltages = VPN_N_VOLTAGES, 14162306a36Sopenharmony_ci .min_uV = VPN_MIN_UV, 14262306a36Sopenharmony_ci .uV_step = STEP_UV, 14362306a36Sopenharmony_ci .vsel_reg = RT4831_REG_VNEG, 14462306a36Sopenharmony_ci .vsel_mask = RT4831_VOLT_MASK, 14562306a36Sopenharmony_ci .enable_reg = RT4831_REG_DSVEN, 14662306a36Sopenharmony_ci .enable_mask = RT4831_NEGEN_MASK, 14762306a36Sopenharmony_ci .active_discharge_reg = RT4831_REG_DSVEN, 14862306a36Sopenharmony_ci .active_discharge_mask = RT4831_NEGADEN_MASK, 14962306a36Sopenharmony_ci .active_discharge_on = RT4831_NEGADEN_MASK, 15062306a36Sopenharmony_ci .owner = THIS_MODULE, 15162306a36Sopenharmony_ci } 15262306a36Sopenharmony_ci}; 15362306a36Sopenharmony_ci 15462306a36Sopenharmony_cistatic int rt4831_regulator_probe(struct platform_device *pdev) 15562306a36Sopenharmony_ci{ 15662306a36Sopenharmony_ci struct regmap *regmap; 15762306a36Sopenharmony_ci struct regulator_dev *rdev; 15862306a36Sopenharmony_ci struct regulator_config config = {}; 15962306a36Sopenharmony_ci int i, ret; 16062306a36Sopenharmony_ci 16162306a36Sopenharmony_ci regmap = dev_get_regmap(pdev->dev.parent, NULL); 16262306a36Sopenharmony_ci if (!regmap) { 16362306a36Sopenharmony_ci dev_err(&pdev->dev, "Failed to init regmap\n"); 16462306a36Sopenharmony_ci return -ENODEV; 16562306a36Sopenharmony_ci } 16662306a36Sopenharmony_ci 16762306a36Sopenharmony_ci /* Configure DSV mode to normal by default */ 16862306a36Sopenharmony_ci ret = regmap_update_bits(regmap, RT4831_REG_DSVEN, RT4831_DSVMODE_MASK, DSV_MODE_NORMAL); 16962306a36Sopenharmony_ci if (ret) { 17062306a36Sopenharmony_ci dev_err(&pdev->dev, "Failed to configure dsv mode to normal\n"); 17162306a36Sopenharmony_ci return ret; 17262306a36Sopenharmony_ci } 17362306a36Sopenharmony_ci 17462306a36Sopenharmony_ci config.dev = pdev->dev.parent; 17562306a36Sopenharmony_ci config.regmap = regmap; 17662306a36Sopenharmony_ci 17762306a36Sopenharmony_ci for (i = 0; i < DSV_OUT_MAX; i++) { 17862306a36Sopenharmony_ci rdev = devm_regulator_register(&pdev->dev, rt4831_regulator_descs + i, &config); 17962306a36Sopenharmony_ci if (IS_ERR(rdev)) { 18062306a36Sopenharmony_ci dev_err(&pdev->dev, "Failed to register %d regulator\n", i); 18162306a36Sopenharmony_ci return PTR_ERR(rdev); 18262306a36Sopenharmony_ci } 18362306a36Sopenharmony_ci } 18462306a36Sopenharmony_ci 18562306a36Sopenharmony_ci return 0; 18662306a36Sopenharmony_ci} 18762306a36Sopenharmony_ci 18862306a36Sopenharmony_cistatic const struct platform_device_id rt4831_regulator_match[] = { 18962306a36Sopenharmony_ci { "rt4831-regulator", 0 }, 19062306a36Sopenharmony_ci {} 19162306a36Sopenharmony_ci}; 19262306a36Sopenharmony_ciMODULE_DEVICE_TABLE(platform, rt4831_regulator_match); 19362306a36Sopenharmony_ci 19462306a36Sopenharmony_cistatic struct platform_driver rt4831_regulator_driver = { 19562306a36Sopenharmony_ci .driver = { 19662306a36Sopenharmony_ci .name = "rt4831-regulator", 19762306a36Sopenharmony_ci .probe_type = PROBE_PREFER_ASYNCHRONOUS, 19862306a36Sopenharmony_ci }, 19962306a36Sopenharmony_ci .id_table = rt4831_regulator_match, 20062306a36Sopenharmony_ci .probe = rt4831_regulator_probe, 20162306a36Sopenharmony_ci}; 20262306a36Sopenharmony_cimodule_platform_driver(rt4831_regulator_driver); 20362306a36Sopenharmony_ci 20462306a36Sopenharmony_ciMODULE_AUTHOR("ChiYuan Huang <cy_huang@richtek.com>"); 20562306a36Sopenharmony_ciMODULE_LICENSE("GPL v2"); 206