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