1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Regulator driver for LP873X PMIC 4 * 5 * Copyright (C) 2016 Texas Instruments Incorporated - https://www.ti.com/ 6 */ 7 8 #include <linux/module.h> 9 #include <linux/platform_device.h> 10 #include <linux/regmap.h> 11 12 #include <linux/mfd/lp873x.h> 13 14 #define LP873X_REGULATOR(_name, _id, _of, _ops, _n, _vr, _vm, _er, _em, \ 15 _delay, _lr, _cr) \ 16 [_id] = { \ 17 .desc = { \ 18 .name = _name, \ 19 .supply_name = _of "-in", \ 20 .id = _id, \ 21 .of_match = of_match_ptr(_of), \ 22 .regulators_node = of_match_ptr("regulators"),\ 23 .ops = &_ops, \ 24 .n_voltages = _n, \ 25 .type = REGULATOR_VOLTAGE, \ 26 .owner = THIS_MODULE, \ 27 .vsel_reg = _vr, \ 28 .vsel_mask = _vm, \ 29 .enable_reg = _er, \ 30 .enable_mask = _em, \ 31 .ramp_delay = _delay, \ 32 .linear_ranges = _lr, \ 33 .n_linear_ranges = ARRAY_SIZE(_lr), \ 34 .curr_table = lp873x_buck_uA, \ 35 .n_current_limits = ARRAY_SIZE(lp873x_buck_uA), \ 36 .csel_reg = (_cr), \ 37 .csel_mask = LP873X_BUCK0_CTRL_2_BUCK0_ILIM,\ 38 }, \ 39 .ctrl2_reg = _cr, \ 40 } 41 42 struct lp873x_regulator { 43 struct regulator_desc desc; 44 unsigned int ctrl2_reg; 45 }; 46 47 static const struct lp873x_regulator regulators[]; 48 49 static const struct linear_range buck0_buck1_ranges[] = { 50 REGULATOR_LINEAR_RANGE(0, 0x0, 0x13, 0), 51 REGULATOR_LINEAR_RANGE(700000, 0x14, 0x17, 10000), 52 REGULATOR_LINEAR_RANGE(735000, 0x18, 0x9d, 5000), 53 REGULATOR_LINEAR_RANGE(1420000, 0x9e, 0xff, 20000), 54 }; 55 56 static const struct linear_range ldo0_ldo1_ranges[] = { 57 REGULATOR_LINEAR_RANGE(800000, 0x0, 0x19, 100000), 58 }; 59 60 static const unsigned int lp873x_buck_ramp_delay[] = { 61 30000, 15000, 10000, 7500, 3800, 1900, 940, 470 62 }; 63 64 /* LP873X BUCK current limit */ 65 static const unsigned int lp873x_buck_uA[] = { 66 1500000, 2000000, 2500000, 3000000, 3500000, 4000000, 67 }; 68 69 static int lp873x_buck_set_ramp_delay(struct regulator_dev *rdev, 70 int ramp_delay) 71 { 72 int id = rdev_get_id(rdev); 73 struct lp873x *lp873 = rdev_get_drvdata(rdev); 74 unsigned int reg; 75 int ret; 76 77 if (ramp_delay <= 470) 78 reg = 7; 79 else if (ramp_delay <= 940) 80 reg = 6; 81 else if (ramp_delay <= 1900) 82 reg = 5; 83 else if (ramp_delay <= 3800) 84 reg = 4; 85 else if (ramp_delay <= 7500) 86 reg = 3; 87 else if (ramp_delay <= 10000) 88 reg = 2; 89 else if (ramp_delay <= 15000) 90 reg = 1; 91 else 92 reg = 0; 93 94 ret = regmap_update_bits(lp873->regmap, regulators[id].ctrl2_reg, 95 LP873X_BUCK0_CTRL_2_BUCK0_SLEW_RATE, 96 reg << __ffs(LP873X_BUCK0_CTRL_2_BUCK0_SLEW_RATE)); 97 if (ret) { 98 dev_err(lp873->dev, "SLEW RATE write failed: %d\n", ret); 99 return ret; 100 } 101 102 rdev->constraints->ramp_delay = lp873x_buck_ramp_delay[reg]; 103 104 return 0; 105 } 106 107 /* Operations permitted on BUCK0, BUCK1 */ 108 static const struct regulator_ops lp873x_buck01_ops = { 109 .is_enabled = regulator_is_enabled_regmap, 110 .enable = regulator_enable_regmap, 111 .disable = regulator_disable_regmap, 112 .get_voltage_sel = regulator_get_voltage_sel_regmap, 113 .set_voltage_sel = regulator_set_voltage_sel_regmap, 114 .list_voltage = regulator_list_voltage_linear_range, 115 .map_voltage = regulator_map_voltage_linear_range, 116 .set_voltage_time_sel = regulator_set_voltage_time_sel, 117 .set_ramp_delay = lp873x_buck_set_ramp_delay, 118 .set_current_limit = regulator_set_current_limit_regmap, 119 .get_current_limit = regulator_get_current_limit_regmap, 120 }; 121 122 /* Operations permitted on LDO0 and LDO1 */ 123 static const struct regulator_ops lp873x_ldo01_ops = { 124 .is_enabled = regulator_is_enabled_regmap, 125 .enable = regulator_enable_regmap, 126 .disable = regulator_disable_regmap, 127 .get_voltage_sel = regulator_get_voltage_sel_regmap, 128 .set_voltage_sel = regulator_set_voltage_sel_regmap, 129 .list_voltage = regulator_list_voltage_linear_range, 130 .map_voltage = regulator_map_voltage_linear_range, 131 }; 132 133 static const struct lp873x_regulator regulators[] = { 134 LP873X_REGULATOR("BUCK0", LP873X_BUCK_0, "buck0", lp873x_buck01_ops, 135 256, LP873X_REG_BUCK0_VOUT, 136 LP873X_BUCK0_VOUT_BUCK0_VSET, LP873X_REG_BUCK0_CTRL_1, 137 LP873X_BUCK0_CTRL_1_BUCK0_EN, 10000, 138 buck0_buck1_ranges, LP873X_REG_BUCK0_CTRL_2), 139 LP873X_REGULATOR("BUCK1", LP873X_BUCK_1, "buck1", lp873x_buck01_ops, 140 256, LP873X_REG_BUCK1_VOUT, 141 LP873X_BUCK1_VOUT_BUCK1_VSET, LP873X_REG_BUCK1_CTRL_1, 142 LP873X_BUCK1_CTRL_1_BUCK1_EN, 10000, 143 buck0_buck1_ranges, LP873X_REG_BUCK1_CTRL_2), 144 LP873X_REGULATOR("LDO0", LP873X_LDO_0, "ldo0", lp873x_ldo01_ops, 26, 145 LP873X_REG_LDO0_VOUT, LP873X_LDO0_VOUT_LDO0_VSET, 146 LP873X_REG_LDO0_CTRL, 147 LP873X_LDO0_CTRL_LDO0_EN, 0, ldo0_ldo1_ranges, 0xFF), 148 LP873X_REGULATOR("LDO1", LP873X_LDO_1, "ldo1", lp873x_ldo01_ops, 26, 149 LP873X_REG_LDO1_VOUT, LP873X_LDO1_VOUT_LDO1_VSET, 150 LP873X_REG_LDO1_CTRL, 151 LP873X_LDO1_CTRL_LDO1_EN, 0, ldo0_ldo1_ranges, 0xFF), 152 }; 153 154 static int lp873x_regulator_probe(struct platform_device *pdev) 155 { 156 struct lp873x *lp873 = dev_get_drvdata(pdev->dev.parent); 157 struct regulator_config config = { }; 158 struct regulator_dev *rdev; 159 int i; 160 161 platform_set_drvdata(pdev, lp873); 162 163 config.dev = &pdev->dev; 164 config.dev->of_node = lp873->dev->of_node; 165 config.driver_data = lp873; 166 config.regmap = lp873->regmap; 167 168 for (i = 0; i < ARRAY_SIZE(regulators); i++) { 169 rdev = devm_regulator_register(&pdev->dev, ®ulators[i].desc, 170 &config); 171 if (IS_ERR(rdev)) { 172 dev_err(lp873->dev, "failed to register %s regulator\n", 173 pdev->name); 174 return PTR_ERR(rdev); 175 } 176 } 177 178 return 0; 179 } 180 181 static const struct platform_device_id lp873x_regulator_id_table[] = { 182 { "lp873x-regulator", }, 183 { /* sentinel */ } 184 }; 185 MODULE_DEVICE_TABLE(platform, lp873x_regulator_id_table); 186 187 static struct platform_driver lp873x_regulator_driver = { 188 .driver = { 189 .name = "lp873x-pmic", 190 .probe_type = PROBE_PREFER_ASYNCHRONOUS, 191 }, 192 .probe = lp873x_regulator_probe, 193 .id_table = lp873x_regulator_id_table, 194 }; 195 module_platform_driver(lp873x_regulator_driver); 196 197 MODULE_AUTHOR("J Keerthy <j-keerthy@ti.com>"); 198 MODULE_DESCRIPTION("LP873X voltage regulator driver"); 199 MODULE_ALIAS("platform:lp873x-pmic"); 200 MODULE_LICENSE("GPL v2"); 201