1/* 2 * Copyright (C) ST-Ericsson SA 2010 3 * 4 * License Terms: GNU General Public License v2 5 * 6 * Author: Sundar Iyer <sundar.iyer@stericsson.com> for ST-Ericsson 7 * 8 * AB8500 peripheral regulators 9 * 10 * AB8500 supports the following regulators, 11 * LDOs - VAUDIO, VANAMIC2/2, VDIGMIC, VINTCORE12, VTVOUT, 12 * VAUX1/2/3, VANA 13 * 14 * for DB8500 cut 1.0 and previous versions of the silicon, all accesses 15 * to registers are through the DB8500 SPI. In cut 1.1 onwards, these 16 * accesses are through the DB8500 PRCMU I2C 17 * 18 */ 19#include <linux/init.h> 20#include <linux/kernel.h> 21#include <linux/err.h> 22#include <linux/platform_device.h> 23#include <linux/mfd/ab8500.h> 24#include <linux/regulator/driver.h> 25#include <linux/regulator/machine.h> 26#include <linux/regulator/ab8500.h> 27 28/** 29 * struct ab8500_regulator_info - ab8500 regulator information 30 * @desc: regulator description 31 * @ab8500: ab8500 parent 32 * @regulator_dev: regulator device 33 * @max_uV: maximum voltage (for variable voltage supplies) 34 * @min_uV: minimum voltage (for variable voltage supplies) 35 * @fixed_uV: typical voltage (for fixed voltage supplies) 36 * @update_reg: register to control on/off 37 * @mask: mask to enable/disable regulator 38 * @enable: bits to enable the regulator in normal(high power) mode 39 * @voltage_reg: register to control regulator voltage 40 * @voltage_mask: mask to control regulator voltage 41 * @supported_voltages: supported voltage table 42 * @voltages_len: number of supported voltages for the regulator 43 */ 44struct ab8500_regulator_info { 45 struct device *dev; 46 struct regulator_desc desc; 47 struct ab8500 *ab8500; 48 struct regulator_dev *regulator; 49 int max_uV; 50 int min_uV; 51 int fixed_uV; 52 int update_reg; 53 int mask; 54 int enable; 55 int voltage_reg; 56 int voltage_mask; 57 int const *supported_voltages; 58 int voltages_len; 59}; 60 61/* voltage tables for the vauxn/vintcore supplies */ 62static const int ldo_vauxn_voltages[] = { 63 1100000, 64 1200000, 65 1300000, 66 1400000, 67 1500000, 68 1800000, 69 1850000, 70 1900000, 71 2500000, 72 2650000, 73 2700000, 74 2750000, 75 2800000, 76 2900000, 77 3000000, 78 3300000, 79}; 80 81static const int ldo_vintcore_voltages[] = { 82 1200000, 83 1225000, 84 1250000, 85 1275000, 86 1300000, 87 1325000, 88 1350000, 89}; 90 91static int ab8500_regulator_enable(struct regulator_dev *rdev) 92{ 93 int regulator_id, ret; 94 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev); 95 96 regulator_id = rdev_get_id(rdev); 97 if (regulator_id >= AB8500_NUM_REGULATORS) 98 return -EINVAL; 99 100 ret = ab8500_set_bits(info->ab8500, info->update_reg, 101 info->mask, info->enable); 102 if (ret < 0) 103 dev_err(rdev_get_dev(rdev), 104 "couldn't set enable bits for regulator\n"); 105 return ret; 106} 107 108static int ab8500_regulator_disable(struct regulator_dev *rdev) 109{ 110 int regulator_id, ret; 111 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev); 112 113 regulator_id = rdev_get_id(rdev); 114 if (regulator_id >= AB8500_NUM_REGULATORS) 115 return -EINVAL; 116 117 ret = ab8500_set_bits(info->ab8500, info->update_reg, 118 info->mask, 0x0); 119 if (ret < 0) 120 dev_err(rdev_get_dev(rdev), 121 "couldn't set disable bits for regulator\n"); 122 return ret; 123} 124 125static int ab8500_regulator_is_enabled(struct regulator_dev *rdev) 126{ 127 int regulator_id, ret; 128 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev); 129 130 regulator_id = rdev_get_id(rdev); 131 if (regulator_id >= AB8500_NUM_REGULATORS) 132 return -EINVAL; 133 134 ret = ab8500_read(info->ab8500, info->update_reg); 135 if (ret < 0) { 136 dev_err(rdev_get_dev(rdev), 137 "couldn't read 0x%x register\n", info->update_reg); 138 return ret; 139 } 140 141 if (ret & info->mask) 142 return true; 143 else 144 return false; 145} 146 147static int ab8500_list_voltage(struct regulator_dev *rdev, unsigned selector) 148{ 149 int regulator_id; 150 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev); 151 152 regulator_id = rdev_get_id(rdev); 153 if (regulator_id >= AB8500_NUM_REGULATORS) 154 return -EINVAL; 155 156 /* return the uV for the fixed regulators */ 157 if (info->fixed_uV) 158 return info->fixed_uV; 159 160 if (selector >= info->voltages_len) 161 return -EINVAL; 162 163 return info->supported_voltages[selector]; 164} 165 166static int ab8500_regulator_get_voltage(struct regulator_dev *rdev) 167{ 168 int regulator_id, ret, val; 169 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev); 170 171 regulator_id = rdev_get_id(rdev); 172 if (regulator_id >= AB8500_NUM_REGULATORS) 173 return -EINVAL; 174 175 ret = ab8500_read(info->ab8500, info->voltage_reg); 176 if (ret < 0) { 177 dev_err(rdev_get_dev(rdev), 178 "couldn't read voltage reg for regulator\n"); 179 return ret; 180 } 181 182 /* vintcore has a different layout */ 183 val = ret & info->voltage_mask; 184 if (regulator_id == AB8500_LDO_INTCORE) 185 ret = info->supported_voltages[val >> 0x3]; 186 else 187 ret = info->supported_voltages[val]; 188 189 return ret; 190} 191 192static int ab8500_get_best_voltage_index(struct regulator_dev *rdev, 193 int min_uV, int max_uV) 194{ 195 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev); 196 int i; 197 198 /* check the supported voltage */ 199 for (i = 0; i < info->voltages_len; i++) { 200 if ((info->supported_voltages[i] >= min_uV) && 201 (info->supported_voltages[i] <= max_uV)) 202 return i; 203 } 204 205 return -EINVAL; 206} 207 208static int ab8500_regulator_set_voltage(struct regulator_dev *rdev, 209 int min_uV, int max_uV) 210{ 211 int regulator_id, ret; 212 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev); 213 214 regulator_id = rdev_get_id(rdev); 215 if (regulator_id >= AB8500_NUM_REGULATORS) 216 return -EINVAL; 217 218 /* get the appropriate voltages within the range */ 219 ret = ab8500_get_best_voltage_index(rdev, min_uV, max_uV); 220 if (ret < 0) { 221 dev_err(rdev_get_dev(rdev), 222 "couldn't get best voltage for regulator\n"); 223 return ret; 224 } 225 226 /* set the registers for the request */ 227 ret = ab8500_set_bits(info->ab8500, info->voltage_reg, 228 info->voltage_mask, ret); 229 if (ret < 0) 230 dev_err(rdev_get_dev(rdev), 231 "couldn't set voltage reg for regulator\n"); 232 233 return ret; 234} 235 236static struct regulator_ops ab8500_regulator_ops = { 237 .enable = ab8500_regulator_enable, 238 .disable = ab8500_regulator_disable, 239 .is_enabled = ab8500_regulator_is_enabled, 240 .get_voltage = ab8500_regulator_get_voltage, 241 .set_voltage = ab8500_regulator_set_voltage, 242 .list_voltage = ab8500_list_voltage, 243}; 244 245static int ab8500_fixed_get_voltage(struct regulator_dev *rdev) 246{ 247 int regulator_id; 248 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev); 249 250 regulator_id = rdev_get_id(rdev); 251 if (regulator_id >= AB8500_NUM_REGULATORS) 252 return -EINVAL; 253 254 return info->fixed_uV; 255} 256 257static struct regulator_ops ab8500_ldo_fixed_ops = { 258 .enable = ab8500_regulator_enable, 259 .disable = ab8500_regulator_disable, 260 .is_enabled = ab8500_regulator_is_enabled, 261 .get_voltage = ab8500_fixed_get_voltage, 262 .list_voltage = ab8500_list_voltage, 263}; 264 265#define AB8500_LDO(_id, min, max, reg, reg_mask, reg_enable, \ 266 volt_reg, volt_mask, voltages, \ 267 len_volts) \ 268{ \ 269 .desc = { \ 270 .name = "LDO-" #_id, \ 271 .ops = &ab8500_regulator_ops, \ 272 .type = REGULATOR_VOLTAGE, \ 273 .id = AB8500_LDO_##_id, \ 274 .owner = THIS_MODULE, \ 275 }, \ 276 .min_uV = (min) * 1000, \ 277 .max_uV = (max) * 1000, \ 278 .update_reg = reg, \ 279 .mask = reg_mask, \ 280 .enable = reg_enable, \ 281 .voltage_reg = volt_reg, \ 282 .voltage_mask = volt_mask, \ 283 .supported_voltages = voltages, \ 284 .voltages_len = len_volts, \ 285 .fixed_uV = 0, \ 286} 287 288#define AB8500_FIXED_LDO(_id, fixed, reg, reg_mask, \ 289 reg_enable) \ 290{ \ 291 .desc = { \ 292 .name = "LDO-" #_id, \ 293 .ops = &ab8500_ldo_fixed_ops, \ 294 .type = REGULATOR_VOLTAGE, \ 295 .id = AB8500_LDO_##_id, \ 296 .owner = THIS_MODULE, \ 297 }, \ 298 .fixed_uV = fixed * 1000, \ 299 .update_reg = reg, \ 300 .mask = reg_mask, \ 301 .enable = reg_enable, \ 302} 303 304static struct ab8500_regulator_info ab8500_regulator_info[] = { 305 /* 306 * Variable Voltage LDOs 307 * name, min uV, max uV, ctrl reg, reg mask, enable mask, 308 * volt ctrl reg, volt ctrl mask, volt table, num supported volts 309 */ 310 AB8500_LDO(AUX1, 1100, 3300, 0x0409, 0x3, 0x1, 0x041f, 0xf, 311 ldo_vauxn_voltages, ARRAY_SIZE(ldo_vauxn_voltages)), 312 AB8500_LDO(AUX2, 1100, 3300, 0x0409, 0xc, 0x4, 0x0420, 0xf, 313 ldo_vauxn_voltages, ARRAY_SIZE(ldo_vauxn_voltages)), 314 AB8500_LDO(AUX3, 1100, 3300, 0x040a, 0x3, 0x1, 0x0421, 0xf, 315 ldo_vauxn_voltages, ARRAY_SIZE(ldo_vauxn_voltages)), 316 AB8500_LDO(INTCORE, 1100, 3300, 0x0380, 0x4, 0x4, 0x0380, 0x38, 317 ldo_vintcore_voltages, ARRAY_SIZE(ldo_vintcore_voltages)), 318 319 /* 320 * Fixed Voltage LDOs 321 * name, o/p uV, ctrl reg, enable, disable 322 */ 323 AB8500_FIXED_LDO(TVOUT, 2000, 0x0380, 0x2, 0x2), 324 AB8500_FIXED_LDO(AUDIO, 2000, 0x0383, 0x2, 0x2), 325 AB8500_FIXED_LDO(ANAMIC1, 2050, 0x0383, 0x4, 0x4), 326 AB8500_FIXED_LDO(ANAMIC2, 2050, 0x0383, 0x8, 0x8), 327 AB8500_FIXED_LDO(DMIC, 1800, 0x0383, 0x10, 0x10), 328 AB8500_FIXED_LDO(ANA, 1200, 0x0383, 0xc, 0x4), 329}; 330 331static inline struct ab8500_regulator_info *find_regulator_info(int id) 332{ 333 struct ab8500_regulator_info *info; 334 int i; 335 336 for (i = 0; i < ARRAY_SIZE(ab8500_regulator_info); i++) { 337 info = &ab8500_regulator_info[i]; 338 if (info->desc.id == id) 339 return info; 340 } 341 return NULL; 342} 343 344static __devinit int ab8500_regulator_probe(struct platform_device *pdev) 345{ 346 struct ab8500 *ab8500 = dev_get_drvdata(pdev->dev.parent); 347 struct ab8500_platform_data *pdata; 348 int i, err; 349 350 if (!ab8500) { 351 dev_err(&pdev->dev, "null mfd parent\n"); 352 return -EINVAL; 353 } 354 pdata = dev_get_platdata(ab8500->dev); 355 356 /* register all regulators */ 357 for (i = 0; i < ARRAY_SIZE(ab8500_regulator_info); i++) { 358 struct ab8500_regulator_info *info = NULL; 359 360 /* assign per-regulator data */ 361 info = &ab8500_regulator_info[i]; 362 info->dev = &pdev->dev; 363 info->ab8500 = ab8500; 364 365 info->regulator = regulator_register(&info->desc, &pdev->dev, 366 pdata->regulator[i], info); 367 if (IS_ERR(info->regulator)) { 368 err = PTR_ERR(info->regulator); 369 dev_err(&pdev->dev, "failed to register regulator %s\n", 370 info->desc.name); 371 /* when we fail, un-register all earlier regulators */ 372 while (--i >= 0) { 373 info = &ab8500_regulator_info[i]; 374 regulator_unregister(info->regulator); 375 } 376 return err; 377 } 378 } 379 380 return 0; 381} 382 383static __devexit int ab8500_regulator_remove(struct platform_device *pdev) 384{ 385 int i; 386 387 for (i = 0; i < ARRAY_SIZE(ab8500_regulator_info); i++) { 388 struct ab8500_regulator_info *info = NULL; 389 info = &ab8500_regulator_info[i]; 390 regulator_unregister(info->regulator); 391 } 392 393 return 0; 394} 395 396static struct platform_driver ab8500_regulator_driver = { 397 .probe = ab8500_regulator_probe, 398 .remove = __devexit_p(ab8500_regulator_remove), 399 .driver = { 400 .name = "ab8500-regulator", 401 .owner = THIS_MODULE, 402 }, 403}; 404 405static int __init ab8500_regulator_init(void) 406{ 407 int ret; 408 409 ret = platform_driver_register(&ab8500_regulator_driver); 410 if (ret != 0) 411 pr_err("Failed to register ab8500 regulator: %d\n", ret); 412 413 return ret; 414} 415subsys_initcall(ab8500_regulator_init); 416 417static void __exit ab8500_regulator_exit(void) 418{ 419 platform_driver_unregister(&ab8500_regulator_driver); 420} 421module_exit(ab8500_regulator_exit); 422 423MODULE_LICENSE("GPL v2"); 424MODULE_AUTHOR("Sundar Iyer <sundar.iyer@stericsson.com>"); 425MODULE_DESCRIPTION("Regulator Driver for ST-Ericsson AB8500 Mixed-Sig PMIC"); 426MODULE_ALIAS("platform:ab8500-regulator"); 427