1 /* 2 * tps65023-regulator.c 3 * 4 * Supports TPS65023 Regulator 5 * 6 * Copyright (C) 2009 Texas Instrument 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 as 10 * published by the Free Software Foundation version 2. 11 * 12 * This program is distributed "as is" WITHOUT ANY WARRANTY of any kind, 13 * whether express or implied; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * General Public License for more details. 16 */ 17 18 #include <linux/kernel.h> 19 #include <linux/module.h> 20 #include <linux/init.h> 21 #include <linux/err.h> 22 #include <linux/platform_device.h> 23 #include <linux/regulator/driver.h> 24 #include <linux/regulator/machine.h> 25 #include <linux/i2c.h> 26 #include <linux/delay.h> 27 #include <linux/slab.h> 28 29 /* Register definitions */ 30 #define TPS65023_REG_VERSION 0 31 #define TPS65023_REG_PGOODZ 1 32 #define TPS65023_REG_MASK 2 33 #define TPS65023_REG_REG_CTRL 3 34 #define TPS65023_REG_CON_CTRL 4 35 #define TPS65023_REG_CON_CTRL2 5 36 #define TPS65023_REG_DEF_CORE 6 37 #define TPS65023_REG_DEFSLEW 7 38 #define TPS65023_REG_LDO_CTRL 8 39 40 /* PGOODZ bitfields */ 41 #define TPS65023_PGOODZ_PWRFAILZ BIT(7) 42 #define TPS65023_PGOODZ_LOWBATTZ BIT(6) 43 #define TPS65023_PGOODZ_VDCDC1 BIT(5) 44 #define TPS65023_PGOODZ_VDCDC2 BIT(4) 45 #define TPS65023_PGOODZ_VDCDC3 BIT(3) 46 #define TPS65023_PGOODZ_LDO2 BIT(2) 47 #define TPS65023_PGOODZ_LDO1 BIT(1) 48 49 /* MASK bitfields */ 50 #define TPS65023_MASK_PWRFAILZ BIT(7) 51 #define TPS65023_MASK_LOWBATTZ BIT(6) 52 #define TPS65023_MASK_VDCDC1 BIT(5) 53 #define TPS65023_MASK_VDCDC2 BIT(4) 54 #define TPS65023_MASK_VDCDC3 BIT(3) 55 #define TPS65023_MASK_LDO2 BIT(2) 56 #define TPS65023_MASK_LDO1 BIT(1) 57 58 /* REG_CTRL bitfields */ 59 #define TPS65023_REG_CTRL_VDCDC1_EN BIT(5) 60 #define TPS65023_REG_CTRL_VDCDC2_EN BIT(4) 61 #define TPS65023_REG_CTRL_VDCDC3_EN BIT(3) 62 #define TPS65023_REG_CTRL_LDO2_EN BIT(2) 63 #define TPS65023_REG_CTRL_LDO1_EN BIT(1) 64 65 /* LDO_CTRL bitfields */ 66 #define TPS65023_LDO_CTRL_LDOx_SHIFT(ldo_id) ((ldo_id)*4) 67 #define TPS65023_LDO_CTRL_LDOx_MASK(ldo_id) (0xF0 >> ((ldo_id)*4)) 68 69 /* Number of step-down converters available */ 70 #define TPS65023_NUM_DCDC 3 71 /* Number of LDO voltage regulators available */ 72 #define TPS65023_NUM_LDO 2 73 /* Number of total regulators available */ 74 #define TPS65023_NUM_REGULATOR (TPS65023_NUM_DCDC + TPS65023_NUM_LDO) 75 76 /* DCDCs */ 77 #define TPS65023_DCDC_1 0 78 #define TPS65023_DCDC_2 1 79 #define TPS65023_DCDC_3 2 80 /* LDOs */ 81 #define TPS65023_LDO_1 3 82 #define TPS65023_LDO_2 4 83 84 #define TPS65023_MAX_REG_ID TPS65023_LDO_2 85 86 /* Supported voltage values for regulators */ 87 static const u16 VDCDC1_VSEL_table[] = { 88 800, 825, 850, 875, 89 900, 925, 950, 975, 90 1000, 1025, 1050, 1075, 91 1100, 1125, 1150, 1175, 92 1200, 1225, 1250, 1275, 93 1300, 1325, 1350, 1375, 94 1400, 1425, 1450, 1475, 95 1500, 1525, 1550, 1600, 96 }; 97 98 static const u16 LDO1_VSEL_table[] = { 99 1000, 1100, 1300, 1800, 100 2200, 2600, 2800, 3150, 101 }; 102 103 static const u16 LDO2_VSEL_table[] = { 104 1050, 1200, 1300, 1800, 105 2500, 2800, 3000, 3300, 106 }; 107 108 static unsigned int num_voltages[] = {ARRAY_SIZE(VDCDC1_VSEL_table), 109 0, 0, ARRAY_SIZE(LDO1_VSEL_table), 110 ARRAY_SIZE(LDO2_VSEL_table)}; 111 112 /* Regulator specific details */ 113 struct tps_info { 114 const char *name; 115 unsigned min_uV; 116 unsigned max_uV; 117 bool fixed; 118 u8 table_len; 119 const u16 *table; 120 }; 121 122 /* PMIC details */ 123 struct tps_pmic { 124 struct regulator_desc desc[TPS65023_NUM_REGULATOR]; 125 struct i2c_client *client; 126 struct regulator_dev *rdev[TPS65023_NUM_REGULATOR]; 127 const struct tps_info *info[TPS65023_NUM_REGULATOR]; 128 struct mutex io_lock; 129 }; 130 131 static inline int tps_65023_read(struct tps_pmic *tps, u8 reg) 132 { 133 return i2c_smbus_read_byte_data(tps->client, reg); 134 } 135 136 static inline int tps_65023_write(struct tps_pmic *tps, u8 reg, u8 val) 137 { 138 return i2c_smbus_write_byte_data(tps->client, reg, val); 139 } 140 141 static int tps_65023_set_bits(struct tps_pmic *tps, u8 reg, u8 mask) 142 { 143 int err, data; 144 145 mutex_lock(&tps->io_lock); 146 147 data = tps_65023_read(tps, reg); 148 if (data < 0) { 149 dev_err(&tps->client->dev, "Read from reg 0x%x failed\n", reg); 150 err = data; 151 goto out; 152 } 153 154 data |= mask; 155 err = tps_65023_write(tps, reg, data); 156 if (err) 157 dev_err(&tps->client->dev, "Write for reg 0x%x failed\n", reg); 158 159 out: 160 mutex_unlock(&tps->io_lock); 161 return err; 162 } 163 164 static int tps_65023_clear_bits(struct tps_pmic *tps, u8 reg, u8 mask) 165 { 166 int err, data; 167 168 mutex_lock(&tps->io_lock); 169 170 data = tps_65023_read(tps, reg); 171 if (data < 0) { 172 dev_err(&tps->client->dev, "Read from reg 0x%x failed\n", reg); 173 err = data; 174 goto out; 175 } 176 177 data &= ~mask; 178 179 err = tps_65023_write(tps, reg, data); 180 if (err) 181 dev_err(&tps->client->dev, "Write for reg 0x%x failed\n", reg); 182 183 out: 184 mutex_unlock(&tps->io_lock); 185 return err; 186 187 } 188 189 static int tps_65023_reg_read(struct tps_pmic *tps, u8 reg) 190 { 191 int data; 192 193 mutex_lock(&tps->io_lock); 194 195 data = tps_65023_read(tps, reg); 196 if (data < 0) 197 dev_err(&tps->client->dev, "Read from reg 0x%x failed\n", reg); 198 199 mutex_unlock(&tps->io_lock); 200 return data; 201 } 202 203 static int tps_65023_reg_write(struct tps_pmic *tps, u8 reg, u8 val) 204 { 205 int err; 206 207 mutex_lock(&tps->io_lock); 208 209 err = tps_65023_write(tps, reg, val); 210 if (err < 0) 211 dev_err(&tps->client->dev, "Write for reg 0x%x failed\n", reg); 212 213 mutex_unlock(&tps->io_lock); 214 return err; 215 } 216 217 static int tps65023_dcdc_is_enabled(struct regulator_dev *dev) 218 { 219 struct tps_pmic *tps = rdev_get_drvdata(dev); 220 int data, dcdc = rdev_get_id(dev); 221 u8 shift; 222 223 if (dcdc < TPS65023_DCDC_1 || dcdc > TPS65023_DCDC_3) 224 return -EINVAL; 225 226 shift = TPS65023_NUM_REGULATOR - dcdc; 227 data = tps_65023_reg_read(tps, TPS65023_REG_REG_CTRL); 228 229 if (data < 0) 230 return data; 231 else 232 return (data & 1<<shift) ? 1 : 0; 233 } 234 235 static int tps65023_ldo_is_enabled(struct regulator_dev *dev) 236 { 237 struct tps_pmic *tps = rdev_get_drvdata(dev); 238 int data, ldo = rdev_get_id(dev); 239 u8 shift; 240 241 if (ldo < TPS65023_LDO_1 || ldo > TPS65023_LDO_2) 242 return -EINVAL; 243 244 shift = (ldo == TPS65023_LDO_1 ? 1 : 2); 245 data = tps_65023_reg_read(tps, TPS65023_REG_REG_CTRL); 246 247 if (data < 0) 248 return data; 249 else 250 return (data & 1<<shift) ? 1 : 0; 251 } 252 253 static int tps65023_dcdc_enable(struct regulator_dev *dev) 254 { 255 struct tps_pmic *tps = rdev_get_drvdata(dev); 256 int dcdc = rdev_get_id(dev); 257 u8 shift; 258 259 if (dcdc < TPS65023_DCDC_1 || dcdc > TPS65023_DCDC_3) 260 return -EINVAL; 261 262 shift = TPS65023_NUM_REGULATOR - dcdc; 263 return tps_65023_set_bits(tps, TPS65023_REG_REG_CTRL, 1 << shift); 264 } 265 266 static int tps65023_dcdc_disable(struct regulator_dev *dev) 267 { 268 struct tps_pmic *tps = rdev_get_drvdata(dev); 269 int dcdc = rdev_get_id(dev); 270 u8 shift; 271 272 if (dcdc < TPS65023_DCDC_1 || dcdc > TPS65023_DCDC_3) 273 return -EINVAL; 274 275 shift = TPS65023_NUM_REGULATOR - dcdc; 276 return tps_65023_clear_bits(tps, TPS65023_REG_REG_CTRL, 1 << shift); 277 } 278 279 static int tps65023_ldo_enable(struct regulator_dev *dev) 280 { 281 struct tps_pmic *tps = rdev_get_drvdata(dev); 282 int ldo = rdev_get_id(dev); 283 u8 shift; 284 285 if (ldo < TPS65023_LDO_1 || ldo > TPS65023_LDO_2) 286 return -EINVAL; 287 288 shift = (ldo == TPS65023_LDO_1 ? 1 : 2); 289 return tps_65023_set_bits(tps, TPS65023_REG_REG_CTRL, 1 << shift); 290 } 291 292 static int tps65023_ldo_disable(struct regulator_dev *dev) 293 { 294 struct tps_pmic *tps = rdev_get_drvdata(dev); 295 int ldo = rdev_get_id(dev); 296 u8 shift; 297 298 if (ldo < TPS65023_LDO_1 || ldo > TPS65023_LDO_2) 299 return -EINVAL; 300 301 shift = (ldo == TPS65023_LDO_1 ? 1 : 2); 302 return tps_65023_clear_bits(tps, TPS65023_REG_REG_CTRL, 1 << shift); 303 } 304 305 static int tps65023_dcdc_get_voltage(struct regulator_dev *dev) 306 { 307 struct tps_pmic *tps = rdev_get_drvdata(dev); 308 int data, dcdc = rdev_get_id(dev); 309 310 if (dcdc < TPS65023_DCDC_1 || dcdc > TPS65023_DCDC_3) 311 return -EINVAL; 312 313 if (dcdc == TPS65023_DCDC_1) { 314 data = tps_65023_reg_read(tps, TPS65023_REG_DEF_CORE); 315 if (data < 0) 316 return data; 317 data &= (tps->info[dcdc]->table_len - 1); 318 return tps->info[dcdc]->table[data] * 1000; 319 } else 320 return tps->info[dcdc]->min_uV; 321 } 322 323 static int tps65023_dcdc_set_voltage(struct regulator_dev *dev, 324 int min_uV, int max_uV) 325 { 326 struct tps_pmic *tps = rdev_get_drvdata(dev); 327 int dcdc = rdev_get_id(dev); 328 int vsel; 329 330 if (dcdc != TPS65023_DCDC_1) 331 return -EINVAL; 332 333 if (min_uV < tps->info[dcdc]->min_uV 334 || min_uV > tps->info[dcdc]->max_uV) 335 return -EINVAL; 336 if (max_uV < tps->info[dcdc]->min_uV 337 || max_uV > tps->info[dcdc]->max_uV) 338 return -EINVAL; 339 340 for (vsel = 0; vsel < tps->info[dcdc]->table_len; vsel++) { 341 int mV = tps->info[dcdc]->table[vsel]; 342 int uV = mV * 1000; 343 344 /* Break at the first in-range value */ 345 if (min_uV <= uV && uV <= max_uV) 346 break; 347 } 348 349 /* write to the register in case we found a match */ 350 if (vsel == tps->info[dcdc]->table_len) 351 return -EINVAL; 352 else 353 return tps_65023_reg_write(tps, TPS65023_REG_DEF_CORE, vsel); 354 } 355 356 static int tps65023_ldo_get_voltage(struct regulator_dev *dev) 357 { 358 struct tps_pmic *tps = rdev_get_drvdata(dev); 359 int data, ldo = rdev_get_id(dev); 360 361 if (ldo < TPS65023_LDO_1 || ldo > TPS65023_LDO_2) 362 return -EINVAL; 363 364 data = tps_65023_reg_read(tps, TPS65023_REG_LDO_CTRL); 365 if (data < 0) 366 return data; 367 368 data >>= (TPS65023_LDO_CTRL_LDOx_SHIFT(ldo - TPS65023_LDO_1)); 369 data &= (tps->info[ldo]->table_len - 1); 370 return tps->info[ldo]->table[data] * 1000; 371 } 372 373 static int tps65023_ldo_set_voltage(struct regulator_dev *dev, 374 int min_uV, int max_uV) 375 { 376 struct tps_pmic *tps = rdev_get_drvdata(dev); 377 int data, vsel, ldo = rdev_get_id(dev); 378 379 if (ldo < TPS65023_LDO_1 || ldo > TPS65023_LDO_2) 380 return -EINVAL; 381 382 if (min_uV < tps->info[ldo]->min_uV || min_uV > tps->info[ldo]->max_uV) 383 return -EINVAL; 384 if (max_uV < tps->info[ldo]->min_uV || max_uV > tps->info[ldo]->max_uV) 385 return -EINVAL; 386 387 for (vsel = 0; vsel < tps->info[ldo]->table_len; vsel++) { 388 int mV = tps->info[ldo]->table[vsel]; 389 int uV = mV * 1000; 390 391 /* Break at the first in-range value */ 392 if (min_uV <= uV && uV <= max_uV) 393 break; 394 } 395 396 if (vsel == tps->info[ldo]->table_len) 397 return -EINVAL; 398 399 data = tps_65023_reg_read(tps, TPS65023_REG_LDO_CTRL); 400 if (data < 0) 401 return data; 402 403 data &= TPS65023_LDO_CTRL_LDOx_MASK(ldo - TPS65023_LDO_1); 404 data |= (vsel << (TPS65023_LDO_CTRL_LDOx_SHIFT(ldo - TPS65023_LDO_1))); 405 return tps_65023_reg_write(tps, TPS65023_REG_LDO_CTRL, data); 406 } 407 408 static int tps65023_dcdc_list_voltage(struct regulator_dev *dev, 409 unsigned selector) 410 { 411 struct tps_pmic *tps = rdev_get_drvdata(dev); 412 int dcdc = rdev_get_id(dev); 413 414 if (dcdc < TPS65023_DCDC_1 || dcdc > TPS65023_DCDC_3) 415 return -EINVAL; 416 417 if (dcdc == TPS65023_DCDC_1) { 418 if (selector >= tps->info[dcdc]->table_len) 419 return -EINVAL; 420 else 421 return tps->info[dcdc]->table[selector] * 1000; 422 } else 423 return tps->info[dcdc]->min_uV; 424 } 425 426 static int tps65023_ldo_list_voltage(struct regulator_dev *dev, 427 unsigned selector) 428 { 429 struct tps_pmic *tps = rdev_get_drvdata(dev); 430 int ldo = rdev_get_id(dev); 431 432 if (ldo < TPS65023_LDO_1 || ldo > TPS65023_LDO_2) 433 return -EINVAL; 434 435 if (selector >= tps->info[ldo]->table_len) 436 return -EINVAL; 437 else 438 return tps->info[ldo]->table[selector] * 1000; 439 } 440 441 /* Operations permitted on VDCDCx */ 442 static struct regulator_ops tps65023_dcdc_ops = { 443 .is_enabled = tps65023_dcdc_is_enabled, 444 .enable = tps65023_dcdc_enable, 445 .disable = tps65023_dcdc_disable, 446 .get_voltage = tps65023_dcdc_get_voltage, 447 .set_voltage = tps65023_dcdc_set_voltage, 448 .list_voltage = tps65023_dcdc_list_voltage, 449 }; 450 451 /* Operations permitted on LDOx */ 452 static struct regulator_ops tps65023_ldo_ops = { 453 .is_enabled = tps65023_ldo_is_enabled, 454 .enable = tps65023_ldo_enable, 455 .disable = tps65023_ldo_disable, 456 .get_voltage = tps65023_ldo_get_voltage, 457 .set_voltage = tps65023_ldo_set_voltage, 458 .list_voltage = tps65023_ldo_list_voltage, 459 }; 460 461 static int __devinit tps_65023_probe(struct i2c_client *client, 462 const struct i2c_device_id *id) 463 { 464 static int desc_id; 465 const struct tps_info *info = (void *)id->driver_data; 466 struct regulator_init_data *init_data; 467 struct regulator_dev *rdev; 468 struct tps_pmic *tps; 469 int i; 470 int error; 471 472 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA)) 473 return -EIO; 474 475 /** 476 * init_data points to array of regulator_init structures 477 * coming from the board-evm file. 478 */ 479 init_data = client->dev.platform_data; 480 if (!init_data) 481 return -EIO; 482 483 tps = kzalloc(sizeof(*tps), GFP_KERNEL); 484 if (!tps) 485 return -ENOMEM; 486 487 mutex_init(&tps->io_lock); 488 489 /* common for all regulators */ 490 tps->client = client; 491 492 for (i = 0; i < TPS65023_NUM_REGULATOR; i++, info++, init_data++) { 493 /* Store regulator specific information */ 494 tps->info[i] = info; 495 496 tps->desc[i].name = info->name; 497 tps->desc[i].id = desc_id++; 498 tps->desc[i].n_voltages = num_voltages[i]; 499 tps->desc[i].ops = (i > TPS65023_DCDC_3 ? 500 &tps65023_ldo_ops : &tps65023_dcdc_ops); 501 tps->desc[i].type = REGULATOR_VOLTAGE; 502 tps->desc[i].owner = THIS_MODULE; 503 504 /* Register the regulators */ 505 rdev = regulator_register(&tps->desc[i], &client->dev, 506 init_data, tps); 507 if (IS_ERR(rdev)) { 508 dev_err(&client->dev, "failed to register %s\n", 509 id->name); 510 error = PTR_ERR(rdev); 511 goto fail; 512 } 513 514 /* Save regulator for cleanup */ 515 tps->rdev[i] = rdev; 516 } 517 518 i2c_set_clientdata(client, tps); 519 520 return 0; 521 522 fail: 523 while (--i >= 0) 524 regulator_unregister(tps->rdev[i]); 525 526 kfree(tps); 527 return error; 528 } 529 530 /** 531 * tps_65023_remove - TPS65023 driver i2c remove handler 532 * @client: i2c driver client device structure 533 * 534 * Unregister TPS driver as an i2c client device driver 535 */ 536 static int __devexit tps_65023_remove(struct i2c_client *client) 537 { 538 struct tps_pmic *tps = i2c_get_clientdata(client); 539 int i; 540 541 for (i = 0; i < TPS65023_NUM_REGULATOR; i++) 542 regulator_unregister(tps->rdev[i]); 543 544 kfree(tps); 545 546 return 0; 547 } 548 549 static const struct tps_info tps65023_regs[] = { 550 { 551 .name = "VDCDC1", 552 .min_uV = 800000, 553 .max_uV = 1600000, 554 .table_len = ARRAY_SIZE(VDCDC1_VSEL_table), 555 .table = VDCDC1_VSEL_table, 556 }, 557 { 558 .name = "VDCDC2", 559 .min_uV = 3300000, 560 .max_uV = 3300000, 561 .fixed = 1, 562 }, 563 { 564 .name = "VDCDC3", 565 .min_uV = 1800000, 566 .max_uV = 1800000, 567 .fixed = 1, 568 }, 569 { 570 .name = "LDO1", 571 .min_uV = 1000000, 572 .max_uV = 3150000, 573 .table_len = ARRAY_SIZE(LDO1_VSEL_table), 574 .table = LDO1_VSEL_table, 575 }, 576 { 577 .name = "LDO2", 578 .min_uV = 1050000, 579 .max_uV = 3300000, 580 .table_len = ARRAY_SIZE(LDO2_VSEL_table), 581 .table = LDO2_VSEL_table, 582 }, 583 }; 584 585 static const struct i2c_device_id tps_65023_id[] = { 586 {.name = "tps65023", 587 .driver_data = (unsigned long) tps65023_regs,}, 588 {.name = "tps65021", 589 .driver_data = (unsigned long) tps65023_regs,}, 590 { }, 591 }; 592 593 MODULE_DEVICE_TABLE(i2c, tps_65023_id); 594 595 static struct i2c_driver tps_65023_i2c_driver = { 596 .driver = { 597 .name = "tps65023", 598 .owner = THIS_MODULE, 599 }, 600 .probe = tps_65023_probe, 601 .remove = __devexit_p(tps_65023_remove), 602 .id_table = tps_65023_id, 603 }; 604 605 /** 606 * tps_65023_init 607 * 608 * Module init function 609 */ 610 static int __init tps_65023_init(void) 611 { 612 return i2c_add_driver(&tps_65023_i2c_driver); 613 } 614 subsys_initcall(tps_65023_init); 615 616 /** 617 * tps_65023_cleanup 618 * 619 * Module exit function 620 */ 621 static void __exit tps_65023_cleanup(void) 622 { 623 i2c_del_driver(&tps_65023_i2c_driver); 624 } 625 module_exit(tps_65023_cleanup); 626 627 MODULE_AUTHOR("Texas Instruments"); 628 MODULE_DESCRIPTION("TPS65023 voltage regulator driver"); 629 MODULE_LICENSE("GPL v2"); 630