1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * tps65218-regulator.c 4 * 5 * Regulator driver for TPS65218 PMIC 6 * 7 * Copyright (C) 2014 Texas Instruments Incorporated - https://www.ti.com/ 8 */ 9 10 #include <linux/kernel.h> 11 #include <linux/module.h> 12 #include <linux/device.h> 13 #include <linux/init.h> 14 #include <linux/err.h> 15 #include <linux/platform_device.h> 16 #include <linux/regmap.h> 17 #include <linux/regulator/of_regulator.h> 18 #include <linux/regulator/driver.h> 19 #include <linux/regulator/machine.h> 20 #include <linux/mfd/tps65218.h> 21 22 #define TPS65218_REGULATOR(_name, _of, _id, _type, _ops, _n, _vr, _vm, _er, \ 23 _em, _cr, _cm, _lr, _nlr, _delay, _fuv, _sr, _sm, \ 24 _ct, _ncl) \ 25 { \ 26 .name = _name, \ 27 .of_match = _of, \ 28 .id = _id, \ 29 .ops = &_ops, \ 30 .n_voltages = _n, \ 31 .type = _type, \ 32 .owner = THIS_MODULE, \ 33 .vsel_reg = _vr, \ 34 .vsel_mask = _vm, \ 35 .csel_reg = _cr, \ 36 .csel_mask = _cm, \ 37 .curr_table = _ct, \ 38 .n_current_limits = _ncl, \ 39 .enable_reg = _er, \ 40 .enable_mask = _em, \ 41 .volt_table = NULL, \ 42 .linear_ranges = _lr, \ 43 .n_linear_ranges = _nlr, \ 44 .ramp_delay = _delay, \ 45 .fixed_uV = _fuv, \ 46 .bypass_reg = _sr, \ 47 .bypass_mask = _sm, \ 48 } \ 49 50 static const struct linear_range dcdc1_dcdc2_ranges[] = { 51 REGULATOR_LINEAR_RANGE(850000, 0x0, 0x32, 10000), 52 REGULATOR_LINEAR_RANGE(1375000, 0x33, 0x3f, 25000), 53 }; 54 55 static const struct linear_range ldo1_dcdc3_ranges[] = { 56 REGULATOR_LINEAR_RANGE(900000, 0x0, 0x1a, 25000), 57 REGULATOR_LINEAR_RANGE(1600000, 0x1b, 0x3f, 50000), 58 }; 59 60 static const struct linear_range dcdc4_ranges[] = { 61 REGULATOR_LINEAR_RANGE(1175000, 0x0, 0xf, 25000), 62 REGULATOR_LINEAR_RANGE(1600000, 0x10, 0x34, 50000), 63 }; 64 65 static int tps65218_pmic_set_voltage_sel(struct regulator_dev *dev, 66 unsigned selector) 67 { 68 int ret; 69 struct tps65218 *tps = rdev_get_drvdata(dev); 70 unsigned int rid = rdev_get_id(dev); 71 72 /* Set the voltage based on vsel value and write protect level is 2 */ 73 ret = tps65218_set_bits(tps, dev->desc->vsel_reg, dev->desc->vsel_mask, 74 selector, TPS65218_PROTECT_L1); 75 76 /* Set GO bit for DCDC1/2 to initiate voltage transistion */ 77 switch (rid) { 78 case TPS65218_DCDC_1: 79 case TPS65218_DCDC_2: 80 ret = tps65218_set_bits(tps, TPS65218_REG_CONTRL_SLEW_RATE, 81 TPS65218_SLEW_RATE_GO, 82 TPS65218_SLEW_RATE_GO, 83 TPS65218_PROTECT_L1); 84 break; 85 } 86 87 return ret; 88 } 89 90 static int tps65218_pmic_enable(struct regulator_dev *dev) 91 { 92 struct tps65218 *tps = rdev_get_drvdata(dev); 93 int rid = rdev_get_id(dev); 94 95 if (rid < TPS65218_DCDC_1 || rid > TPS65218_LDO_1) 96 return -EINVAL; 97 98 /* Enable the regulator and password protection is level 1 */ 99 return tps65218_set_bits(tps, dev->desc->enable_reg, 100 dev->desc->enable_mask, dev->desc->enable_mask, 101 TPS65218_PROTECT_L1); 102 } 103 104 static int tps65218_pmic_disable(struct regulator_dev *dev) 105 { 106 struct tps65218 *tps = rdev_get_drvdata(dev); 107 int rid = rdev_get_id(dev); 108 109 if (rid < TPS65218_DCDC_1 || rid > TPS65218_LDO_1) 110 return -EINVAL; 111 112 /* Disable the regulator and password protection is level 1 */ 113 return tps65218_clear_bits(tps, dev->desc->enable_reg, 114 dev->desc->enable_mask, TPS65218_PROTECT_L1); 115 } 116 117 static int tps65218_pmic_set_suspend_enable(struct regulator_dev *dev) 118 { 119 struct tps65218 *tps = rdev_get_drvdata(dev); 120 unsigned int rid = rdev_get_id(dev); 121 122 if (rid > TPS65218_LDO_1) 123 return -EINVAL; 124 125 return tps65218_clear_bits(tps, dev->desc->bypass_reg, 126 dev->desc->bypass_mask, 127 TPS65218_PROTECT_L1); 128 } 129 130 static int tps65218_pmic_set_suspend_disable(struct regulator_dev *dev) 131 { 132 struct tps65218 *tps = rdev_get_drvdata(dev); 133 unsigned int rid = rdev_get_id(dev); 134 135 if (rid > TPS65218_LDO_1) 136 return -EINVAL; 137 138 /* 139 * Certain revisions of TPS65218 will need to have DCDC3 regulator 140 * enabled always, otherwise an immediate system reboot will occur 141 * during poweroff. 142 */ 143 if (rid == TPS65218_DCDC_3 && tps->rev == TPS65218_REV_2_1) 144 return 0; 145 146 if (!tps->strobes[rid]) { 147 if (rid == TPS65218_DCDC_3) 148 tps->strobes[rid] = 3; 149 else 150 return -EINVAL; 151 } 152 153 return tps65218_set_bits(tps, dev->desc->bypass_reg, 154 dev->desc->bypass_mask, 155 tps->strobes[rid], TPS65218_PROTECT_L1); 156 } 157 158 /* Operations permitted on DCDC1, DCDC2 */ 159 static const struct regulator_ops tps65218_dcdc12_ops = { 160 .is_enabled = regulator_is_enabled_regmap, 161 .enable = tps65218_pmic_enable, 162 .disable = tps65218_pmic_disable, 163 .get_voltage_sel = regulator_get_voltage_sel_regmap, 164 .set_voltage_sel = tps65218_pmic_set_voltage_sel, 165 .list_voltage = regulator_list_voltage_linear_range, 166 .map_voltage = regulator_map_voltage_linear_range, 167 .set_voltage_time_sel = regulator_set_voltage_time_sel, 168 .set_suspend_enable = tps65218_pmic_set_suspend_enable, 169 .set_suspend_disable = tps65218_pmic_set_suspend_disable, 170 }; 171 172 /* Operations permitted on DCDC3, DCDC4 and LDO1 */ 173 static const struct regulator_ops tps65218_ldo1_dcdc34_ops = { 174 .is_enabled = regulator_is_enabled_regmap, 175 .enable = tps65218_pmic_enable, 176 .disable = tps65218_pmic_disable, 177 .get_voltage_sel = regulator_get_voltage_sel_regmap, 178 .set_voltage_sel = tps65218_pmic_set_voltage_sel, 179 .list_voltage = regulator_list_voltage_linear_range, 180 .map_voltage = regulator_map_voltage_linear_range, 181 .set_suspend_enable = tps65218_pmic_set_suspend_enable, 182 .set_suspend_disable = tps65218_pmic_set_suspend_disable, 183 }; 184 185 static const unsigned int ls3_currents[] = { 100000, 200000, 500000, 1000000 }; 186 187 static int tps65218_pmic_set_input_current_lim(struct regulator_dev *dev, 188 int lim_uA) 189 { 190 unsigned int index = 0; 191 unsigned int num_currents = ARRAY_SIZE(ls3_currents); 192 struct tps65218 *tps = rdev_get_drvdata(dev); 193 194 while (index < num_currents && ls3_currents[index] != lim_uA) 195 index++; 196 197 if (index == num_currents) 198 return -EINVAL; 199 200 return tps65218_set_bits(tps, dev->desc->csel_reg, dev->desc->csel_mask, 201 index << __builtin_ctz(dev->desc->csel_mask), 202 TPS65218_PROTECT_L1); 203 } 204 205 static int tps65218_pmic_set_current_limit(struct regulator_dev *dev, 206 int min_uA, int max_uA) 207 { 208 int index = 0; 209 unsigned int num_currents = ARRAY_SIZE(ls3_currents); 210 struct tps65218 *tps = rdev_get_drvdata(dev); 211 212 while (index < num_currents && ls3_currents[index] <= max_uA) 213 index++; 214 215 index--; 216 217 if (index < 0 || ls3_currents[index] < min_uA) 218 return -EINVAL; 219 220 return tps65218_set_bits(tps, dev->desc->csel_reg, dev->desc->csel_mask, 221 index << __builtin_ctz(dev->desc->csel_mask), 222 TPS65218_PROTECT_L1); 223 } 224 225 static const struct regulator_ops tps65218_ls23_ops = { 226 .is_enabled = regulator_is_enabled_regmap, 227 .enable = tps65218_pmic_enable, 228 .disable = tps65218_pmic_disable, 229 .set_input_current_limit = tps65218_pmic_set_input_current_lim, 230 .set_current_limit = tps65218_pmic_set_current_limit, 231 .get_current_limit = regulator_get_current_limit_regmap, 232 }; 233 234 /* Operations permitted on DCDC5, DCDC6 */ 235 static const struct regulator_ops tps65218_dcdc56_pmic_ops = { 236 .is_enabled = regulator_is_enabled_regmap, 237 .enable = tps65218_pmic_enable, 238 .disable = tps65218_pmic_disable, 239 .set_suspend_enable = tps65218_pmic_set_suspend_enable, 240 .set_suspend_disable = tps65218_pmic_set_suspend_disable, 241 }; 242 243 static const struct regulator_desc regulators[] = { 244 TPS65218_REGULATOR("DCDC1", "regulator-dcdc1", TPS65218_DCDC_1, 245 REGULATOR_VOLTAGE, tps65218_dcdc12_ops, 64, 246 TPS65218_REG_CONTROL_DCDC1, 247 TPS65218_CONTROL_DCDC1_MASK, TPS65218_REG_ENABLE1, 248 TPS65218_ENABLE1_DC1_EN, 0, 0, dcdc1_dcdc2_ranges, 249 2, 4000, 0, TPS65218_REG_SEQ3, 250 TPS65218_SEQ3_DC1_SEQ_MASK, NULL, 0), 251 TPS65218_REGULATOR("DCDC2", "regulator-dcdc2", TPS65218_DCDC_2, 252 REGULATOR_VOLTAGE, tps65218_dcdc12_ops, 64, 253 TPS65218_REG_CONTROL_DCDC2, 254 TPS65218_CONTROL_DCDC2_MASK, TPS65218_REG_ENABLE1, 255 TPS65218_ENABLE1_DC2_EN, 0, 0, dcdc1_dcdc2_ranges, 256 2, 4000, 0, TPS65218_REG_SEQ3, 257 TPS65218_SEQ3_DC2_SEQ_MASK, NULL, 0), 258 TPS65218_REGULATOR("DCDC3", "regulator-dcdc3", TPS65218_DCDC_3, 259 REGULATOR_VOLTAGE, tps65218_ldo1_dcdc34_ops, 64, 260 TPS65218_REG_CONTROL_DCDC3, 261 TPS65218_CONTROL_DCDC3_MASK, TPS65218_REG_ENABLE1, 262 TPS65218_ENABLE1_DC3_EN, 0, 0, ldo1_dcdc3_ranges, 2, 263 0, 0, TPS65218_REG_SEQ4, TPS65218_SEQ4_DC3_SEQ_MASK, 264 NULL, 0), 265 TPS65218_REGULATOR("DCDC4", "regulator-dcdc4", TPS65218_DCDC_4, 266 REGULATOR_VOLTAGE, tps65218_ldo1_dcdc34_ops, 53, 267 TPS65218_REG_CONTROL_DCDC4, 268 TPS65218_CONTROL_DCDC4_MASK, TPS65218_REG_ENABLE1, 269 TPS65218_ENABLE1_DC4_EN, 0, 0, dcdc4_ranges, 2, 270 0, 0, TPS65218_REG_SEQ4, TPS65218_SEQ4_DC4_SEQ_MASK, 271 NULL, 0), 272 TPS65218_REGULATOR("DCDC5", "regulator-dcdc5", TPS65218_DCDC_5, 273 REGULATOR_VOLTAGE, tps65218_dcdc56_pmic_ops, 1, -1, 274 -1, TPS65218_REG_ENABLE1, TPS65218_ENABLE1_DC5_EN, 0, 275 0, NULL, 0, 0, 1000000, TPS65218_REG_SEQ5, 276 TPS65218_SEQ5_DC5_SEQ_MASK, NULL, 0), 277 TPS65218_REGULATOR("DCDC6", "regulator-dcdc6", TPS65218_DCDC_6, 278 REGULATOR_VOLTAGE, tps65218_dcdc56_pmic_ops, 1, -1, 279 -1, TPS65218_REG_ENABLE1, TPS65218_ENABLE1_DC6_EN, 0, 280 0, NULL, 0, 0, 1800000, TPS65218_REG_SEQ5, 281 TPS65218_SEQ5_DC6_SEQ_MASK, NULL, 0), 282 TPS65218_REGULATOR("LDO1", "regulator-ldo1", TPS65218_LDO_1, 283 REGULATOR_VOLTAGE, tps65218_ldo1_dcdc34_ops, 64, 284 TPS65218_REG_CONTROL_LDO1, 285 TPS65218_CONTROL_LDO1_MASK, TPS65218_REG_ENABLE2, 286 TPS65218_ENABLE2_LDO1_EN, 0, 0, ldo1_dcdc3_ranges, 287 2, 0, 0, TPS65218_REG_SEQ6, 288 TPS65218_SEQ6_LDO1_SEQ_MASK, NULL, 0), 289 TPS65218_REGULATOR("LS2", "regulator-ls2", TPS65218_LS_2, 290 REGULATOR_CURRENT, tps65218_ls23_ops, 0, 0, 0, 291 TPS65218_REG_ENABLE2, TPS65218_ENABLE2_LS2_EN, 292 TPS65218_REG_CONFIG2, TPS65218_CONFIG2_LS2ILIM_MASK, 293 NULL, 0, 0, 0, 0, 0, ls3_currents, 294 ARRAY_SIZE(ls3_currents)), 295 TPS65218_REGULATOR("LS3", "regulator-ls3", TPS65218_LS_3, 296 REGULATOR_CURRENT, tps65218_ls23_ops, 0, 0, 0, 297 TPS65218_REG_ENABLE2, TPS65218_ENABLE2_LS3_EN, 298 TPS65218_REG_CONFIG2, TPS65218_CONFIG2_LS3ILIM_MASK, 299 NULL, 0, 0, 0, 0, 0, ls3_currents, 300 ARRAY_SIZE(ls3_currents)), 301 }; 302 303 static int tps65218_regulator_probe(struct platform_device *pdev) 304 { 305 struct tps65218 *tps = dev_get_drvdata(pdev->dev.parent); 306 struct regulator_dev *rdev; 307 struct regulator_config config = { }; 308 int i, ret; 309 unsigned int val; 310 311 config.dev = &pdev->dev; 312 config.dev->of_node = tps->dev->of_node; 313 config.driver_data = tps; 314 config.regmap = tps->regmap; 315 316 /* Allocate memory for strobes */ 317 tps->strobes = devm_kcalloc(&pdev->dev, 318 TPS65218_NUM_REGULATOR, sizeof(u8), 319 GFP_KERNEL); 320 if (!tps->strobes) 321 return -ENOMEM; 322 323 for (i = 0; i < ARRAY_SIZE(regulators); i++) { 324 rdev = devm_regulator_register(&pdev->dev, ®ulators[i], 325 &config); 326 if (IS_ERR(rdev)) { 327 dev_err(tps->dev, "failed to register %s regulator\n", 328 pdev->name); 329 return PTR_ERR(rdev); 330 } 331 332 ret = regmap_read(tps->regmap, regulators[i].bypass_reg, &val); 333 if (ret) 334 return ret; 335 336 tps->strobes[i] = val & regulators[i].bypass_mask; 337 } 338 339 return 0; 340 } 341 342 static const struct platform_device_id tps65218_regulator_id_table[] = { 343 { .name = "tps65218-regulator" }, 344 { /* sentinel */ } 345 }; 346 MODULE_DEVICE_TABLE(platform, tps65218_regulator_id_table); 347 348 static struct platform_driver tps65218_regulator_driver = { 349 .driver = { 350 .name = "tps65218-pmic", 351 .probe_type = PROBE_PREFER_ASYNCHRONOUS, 352 }, 353 .probe = tps65218_regulator_probe, 354 .id_table = tps65218_regulator_id_table, 355 }; 356 357 module_platform_driver(tps65218_regulator_driver); 358 359 MODULE_AUTHOR("J Keerthy <j-keerthy@ti.com>"); 360 MODULE_DESCRIPTION("TPS65218 voltage regulator driver"); 361 MODULE_ALIAS("platform:tps65218-pmic"); 362 MODULE_LICENSE("GPL v2"); 363