162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Regulator driver for LP873X PMIC 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * Copyright (C) 2016 Texas Instruments Incorporated - https://www.ti.com/ 662306a36Sopenharmony_ci */ 762306a36Sopenharmony_ci 862306a36Sopenharmony_ci#include <linux/module.h> 962306a36Sopenharmony_ci#include <linux/platform_device.h> 1062306a36Sopenharmony_ci#include <linux/regmap.h> 1162306a36Sopenharmony_ci 1262306a36Sopenharmony_ci#include <linux/mfd/lp873x.h> 1362306a36Sopenharmony_ci 1462306a36Sopenharmony_ci#define LP873X_REGULATOR(_name, _id, _of, _ops, _n, _vr, _vm, _er, _em, \ 1562306a36Sopenharmony_ci _delay, _lr, _cr) \ 1662306a36Sopenharmony_ci [_id] = { \ 1762306a36Sopenharmony_ci .desc = { \ 1862306a36Sopenharmony_ci .name = _name, \ 1962306a36Sopenharmony_ci .supply_name = _of "-in", \ 2062306a36Sopenharmony_ci .id = _id, \ 2162306a36Sopenharmony_ci .of_match = of_match_ptr(_of), \ 2262306a36Sopenharmony_ci .regulators_node = of_match_ptr("regulators"),\ 2362306a36Sopenharmony_ci .ops = &_ops, \ 2462306a36Sopenharmony_ci .n_voltages = _n, \ 2562306a36Sopenharmony_ci .type = REGULATOR_VOLTAGE, \ 2662306a36Sopenharmony_ci .owner = THIS_MODULE, \ 2762306a36Sopenharmony_ci .vsel_reg = _vr, \ 2862306a36Sopenharmony_ci .vsel_mask = _vm, \ 2962306a36Sopenharmony_ci .enable_reg = _er, \ 3062306a36Sopenharmony_ci .enable_mask = _em, \ 3162306a36Sopenharmony_ci .ramp_delay = _delay, \ 3262306a36Sopenharmony_ci .linear_ranges = _lr, \ 3362306a36Sopenharmony_ci .n_linear_ranges = ARRAY_SIZE(_lr), \ 3462306a36Sopenharmony_ci .curr_table = lp873x_buck_uA, \ 3562306a36Sopenharmony_ci .n_current_limits = ARRAY_SIZE(lp873x_buck_uA), \ 3662306a36Sopenharmony_ci .csel_reg = (_cr), \ 3762306a36Sopenharmony_ci .csel_mask = LP873X_BUCK0_CTRL_2_BUCK0_ILIM,\ 3862306a36Sopenharmony_ci }, \ 3962306a36Sopenharmony_ci .ctrl2_reg = _cr, \ 4062306a36Sopenharmony_ci } 4162306a36Sopenharmony_ci 4262306a36Sopenharmony_cistruct lp873x_regulator { 4362306a36Sopenharmony_ci struct regulator_desc desc; 4462306a36Sopenharmony_ci unsigned int ctrl2_reg; 4562306a36Sopenharmony_ci}; 4662306a36Sopenharmony_ci 4762306a36Sopenharmony_cistatic const struct lp873x_regulator regulators[]; 4862306a36Sopenharmony_ci 4962306a36Sopenharmony_cistatic const struct linear_range buck0_buck1_ranges[] = { 5062306a36Sopenharmony_ci REGULATOR_LINEAR_RANGE(0, 0x0, 0x13, 0), 5162306a36Sopenharmony_ci REGULATOR_LINEAR_RANGE(700000, 0x14, 0x17, 10000), 5262306a36Sopenharmony_ci REGULATOR_LINEAR_RANGE(735000, 0x18, 0x9d, 5000), 5362306a36Sopenharmony_ci REGULATOR_LINEAR_RANGE(1420000, 0x9e, 0xff, 20000), 5462306a36Sopenharmony_ci}; 5562306a36Sopenharmony_ci 5662306a36Sopenharmony_cistatic const struct linear_range ldo0_ldo1_ranges[] = { 5762306a36Sopenharmony_ci REGULATOR_LINEAR_RANGE(800000, 0x0, 0x19, 100000), 5862306a36Sopenharmony_ci}; 5962306a36Sopenharmony_ci 6062306a36Sopenharmony_cistatic const unsigned int lp873x_buck_ramp_delay[] = { 6162306a36Sopenharmony_ci 30000, 15000, 10000, 7500, 3800, 1900, 940, 470 6262306a36Sopenharmony_ci}; 6362306a36Sopenharmony_ci 6462306a36Sopenharmony_ci/* LP873X BUCK current limit */ 6562306a36Sopenharmony_cistatic const unsigned int lp873x_buck_uA[] = { 6662306a36Sopenharmony_ci 1500000, 2000000, 2500000, 3000000, 3500000, 4000000, 6762306a36Sopenharmony_ci}; 6862306a36Sopenharmony_ci 6962306a36Sopenharmony_cistatic int lp873x_buck_set_ramp_delay(struct regulator_dev *rdev, 7062306a36Sopenharmony_ci int ramp_delay) 7162306a36Sopenharmony_ci{ 7262306a36Sopenharmony_ci int id = rdev_get_id(rdev); 7362306a36Sopenharmony_ci struct lp873x *lp873 = rdev_get_drvdata(rdev); 7462306a36Sopenharmony_ci unsigned int reg; 7562306a36Sopenharmony_ci int ret; 7662306a36Sopenharmony_ci 7762306a36Sopenharmony_ci if (ramp_delay <= 470) 7862306a36Sopenharmony_ci reg = 7; 7962306a36Sopenharmony_ci else if (ramp_delay <= 940) 8062306a36Sopenharmony_ci reg = 6; 8162306a36Sopenharmony_ci else if (ramp_delay <= 1900) 8262306a36Sopenharmony_ci reg = 5; 8362306a36Sopenharmony_ci else if (ramp_delay <= 3800) 8462306a36Sopenharmony_ci reg = 4; 8562306a36Sopenharmony_ci else if (ramp_delay <= 7500) 8662306a36Sopenharmony_ci reg = 3; 8762306a36Sopenharmony_ci else if (ramp_delay <= 10000) 8862306a36Sopenharmony_ci reg = 2; 8962306a36Sopenharmony_ci else if (ramp_delay <= 15000) 9062306a36Sopenharmony_ci reg = 1; 9162306a36Sopenharmony_ci else 9262306a36Sopenharmony_ci reg = 0; 9362306a36Sopenharmony_ci 9462306a36Sopenharmony_ci ret = regmap_update_bits(lp873->regmap, regulators[id].ctrl2_reg, 9562306a36Sopenharmony_ci LP873X_BUCK0_CTRL_2_BUCK0_SLEW_RATE, 9662306a36Sopenharmony_ci reg << __ffs(LP873X_BUCK0_CTRL_2_BUCK0_SLEW_RATE)); 9762306a36Sopenharmony_ci if (ret) { 9862306a36Sopenharmony_ci dev_err(lp873->dev, "SLEW RATE write failed: %d\n", ret); 9962306a36Sopenharmony_ci return ret; 10062306a36Sopenharmony_ci } 10162306a36Sopenharmony_ci 10262306a36Sopenharmony_ci rdev->constraints->ramp_delay = lp873x_buck_ramp_delay[reg]; 10362306a36Sopenharmony_ci 10462306a36Sopenharmony_ci return 0; 10562306a36Sopenharmony_ci} 10662306a36Sopenharmony_ci 10762306a36Sopenharmony_ci/* Operations permitted on BUCK0, BUCK1 */ 10862306a36Sopenharmony_cistatic const struct regulator_ops lp873x_buck01_ops = { 10962306a36Sopenharmony_ci .is_enabled = regulator_is_enabled_regmap, 11062306a36Sopenharmony_ci .enable = regulator_enable_regmap, 11162306a36Sopenharmony_ci .disable = regulator_disable_regmap, 11262306a36Sopenharmony_ci .get_voltage_sel = regulator_get_voltage_sel_regmap, 11362306a36Sopenharmony_ci .set_voltage_sel = regulator_set_voltage_sel_regmap, 11462306a36Sopenharmony_ci .list_voltage = regulator_list_voltage_linear_range, 11562306a36Sopenharmony_ci .map_voltage = regulator_map_voltage_linear_range, 11662306a36Sopenharmony_ci .set_voltage_time_sel = regulator_set_voltage_time_sel, 11762306a36Sopenharmony_ci .set_ramp_delay = lp873x_buck_set_ramp_delay, 11862306a36Sopenharmony_ci .set_current_limit = regulator_set_current_limit_regmap, 11962306a36Sopenharmony_ci .get_current_limit = regulator_get_current_limit_regmap, 12062306a36Sopenharmony_ci}; 12162306a36Sopenharmony_ci 12262306a36Sopenharmony_ci/* Operations permitted on LDO0 and LDO1 */ 12362306a36Sopenharmony_cistatic const struct regulator_ops lp873x_ldo01_ops = { 12462306a36Sopenharmony_ci .is_enabled = regulator_is_enabled_regmap, 12562306a36Sopenharmony_ci .enable = regulator_enable_regmap, 12662306a36Sopenharmony_ci .disable = regulator_disable_regmap, 12762306a36Sopenharmony_ci .get_voltage_sel = regulator_get_voltage_sel_regmap, 12862306a36Sopenharmony_ci .set_voltage_sel = regulator_set_voltage_sel_regmap, 12962306a36Sopenharmony_ci .list_voltage = regulator_list_voltage_linear_range, 13062306a36Sopenharmony_ci .map_voltage = regulator_map_voltage_linear_range, 13162306a36Sopenharmony_ci}; 13262306a36Sopenharmony_ci 13362306a36Sopenharmony_cistatic const struct lp873x_regulator regulators[] = { 13462306a36Sopenharmony_ci LP873X_REGULATOR("BUCK0", LP873X_BUCK_0, "buck0", lp873x_buck01_ops, 13562306a36Sopenharmony_ci 256, LP873X_REG_BUCK0_VOUT, 13662306a36Sopenharmony_ci LP873X_BUCK0_VOUT_BUCK0_VSET, LP873X_REG_BUCK0_CTRL_1, 13762306a36Sopenharmony_ci LP873X_BUCK0_CTRL_1_BUCK0_EN, 10000, 13862306a36Sopenharmony_ci buck0_buck1_ranges, LP873X_REG_BUCK0_CTRL_2), 13962306a36Sopenharmony_ci LP873X_REGULATOR("BUCK1", LP873X_BUCK_1, "buck1", lp873x_buck01_ops, 14062306a36Sopenharmony_ci 256, LP873X_REG_BUCK1_VOUT, 14162306a36Sopenharmony_ci LP873X_BUCK1_VOUT_BUCK1_VSET, LP873X_REG_BUCK1_CTRL_1, 14262306a36Sopenharmony_ci LP873X_BUCK1_CTRL_1_BUCK1_EN, 10000, 14362306a36Sopenharmony_ci buck0_buck1_ranges, LP873X_REG_BUCK1_CTRL_2), 14462306a36Sopenharmony_ci LP873X_REGULATOR("LDO0", LP873X_LDO_0, "ldo0", lp873x_ldo01_ops, 26, 14562306a36Sopenharmony_ci LP873X_REG_LDO0_VOUT, LP873X_LDO0_VOUT_LDO0_VSET, 14662306a36Sopenharmony_ci LP873X_REG_LDO0_CTRL, 14762306a36Sopenharmony_ci LP873X_LDO0_CTRL_LDO0_EN, 0, ldo0_ldo1_ranges, 0xFF), 14862306a36Sopenharmony_ci LP873X_REGULATOR("LDO1", LP873X_LDO_1, "ldo1", lp873x_ldo01_ops, 26, 14962306a36Sopenharmony_ci LP873X_REG_LDO1_VOUT, LP873X_LDO1_VOUT_LDO1_VSET, 15062306a36Sopenharmony_ci LP873X_REG_LDO1_CTRL, 15162306a36Sopenharmony_ci LP873X_LDO1_CTRL_LDO1_EN, 0, ldo0_ldo1_ranges, 0xFF), 15262306a36Sopenharmony_ci}; 15362306a36Sopenharmony_ci 15462306a36Sopenharmony_cistatic int lp873x_regulator_probe(struct platform_device *pdev) 15562306a36Sopenharmony_ci{ 15662306a36Sopenharmony_ci struct lp873x *lp873 = dev_get_drvdata(pdev->dev.parent); 15762306a36Sopenharmony_ci struct regulator_config config = { }; 15862306a36Sopenharmony_ci struct regulator_dev *rdev; 15962306a36Sopenharmony_ci int i; 16062306a36Sopenharmony_ci 16162306a36Sopenharmony_ci platform_set_drvdata(pdev, lp873); 16262306a36Sopenharmony_ci 16362306a36Sopenharmony_ci config.dev = &pdev->dev; 16462306a36Sopenharmony_ci config.dev->of_node = lp873->dev->of_node; 16562306a36Sopenharmony_ci config.driver_data = lp873; 16662306a36Sopenharmony_ci config.regmap = lp873->regmap; 16762306a36Sopenharmony_ci 16862306a36Sopenharmony_ci for (i = 0; i < ARRAY_SIZE(regulators); i++) { 16962306a36Sopenharmony_ci rdev = devm_regulator_register(&pdev->dev, ®ulators[i].desc, 17062306a36Sopenharmony_ci &config); 17162306a36Sopenharmony_ci if (IS_ERR(rdev)) { 17262306a36Sopenharmony_ci dev_err(lp873->dev, "failed to register %s regulator\n", 17362306a36Sopenharmony_ci pdev->name); 17462306a36Sopenharmony_ci return PTR_ERR(rdev); 17562306a36Sopenharmony_ci } 17662306a36Sopenharmony_ci } 17762306a36Sopenharmony_ci 17862306a36Sopenharmony_ci return 0; 17962306a36Sopenharmony_ci} 18062306a36Sopenharmony_ci 18162306a36Sopenharmony_cistatic const struct platform_device_id lp873x_regulator_id_table[] = { 18262306a36Sopenharmony_ci { "lp873x-regulator", }, 18362306a36Sopenharmony_ci { /* sentinel */ } 18462306a36Sopenharmony_ci}; 18562306a36Sopenharmony_ciMODULE_DEVICE_TABLE(platform, lp873x_regulator_id_table); 18662306a36Sopenharmony_ci 18762306a36Sopenharmony_cistatic struct platform_driver lp873x_regulator_driver = { 18862306a36Sopenharmony_ci .driver = { 18962306a36Sopenharmony_ci .name = "lp873x-pmic", 19062306a36Sopenharmony_ci .probe_type = PROBE_PREFER_ASYNCHRONOUS, 19162306a36Sopenharmony_ci }, 19262306a36Sopenharmony_ci .probe = lp873x_regulator_probe, 19362306a36Sopenharmony_ci .id_table = lp873x_regulator_id_table, 19462306a36Sopenharmony_ci}; 19562306a36Sopenharmony_cimodule_platform_driver(lp873x_regulator_driver); 19662306a36Sopenharmony_ci 19762306a36Sopenharmony_ciMODULE_AUTHOR("J Keerthy <j-keerthy@ti.com>"); 19862306a36Sopenharmony_ciMODULE_DESCRIPTION("LP873X voltage regulator driver"); 19962306a36Sopenharmony_ciMODULE_ALIAS("platform:lp873x-pmic"); 20062306a36Sopenharmony_ciMODULE_LICENSE("GPL v2"); 201