1 /* 2 * max8998.c - Voltage regulator driver for the Maxim 8998 3 * 4 * Copyright (C) 2009-2010 Samsung Electronics 5 * Kyungmin Park <kyungmin.park@samsung.com> 6 * Marek Szyprowski <m.szyprowski@samsung.com> 7 * 8 * This program is free software; you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License as published by 10 * the Free Software Foundation; either version 2 of the License, or 11 * (at your option) any later version. 12 * 13 * This program is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * GNU General Public License for more details. 17 * 18 * You should have received a copy of the GNU General Public License 19 * along with this program; if not, write to the Free Software 20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 21 */ 22 23 #include <linux/module.h> 24 #include <linux/init.h> 25 #include <linux/i2c.h> 26 #include <linux/err.h> 27 #include <linux/gpio.h> 28 #include <linux/slab.h> 29 #include <linux/interrupt.h> 30 #include <linux/mutex.h> 31 #include <linux/delay.h> 32 #include <linux/platform_device.h> 33 #include <linux/regulator/driver.h> 34 #include <linux/mfd/max8998.h> 35 #include <linux/mfd/max8998-private.h> 36 37 struct max8998_data { 38 struct device *dev; 39 struct max8998_dev *iodev; 40 int num_regulators; 41 struct regulator_dev **rdev; 42 u8 buck1_vol[4]; /* voltages for selection */ 43 u8 buck2_vol[2]; 44 unsigned int buck1_idx; /* index to last changed voltage */ 45 /* value in a set */ 46 unsigned int buck2_idx; 47 }; 48 49 struct voltage_map_desc { 50 int min; 51 int max; 52 int step; 53 }; 54 55 /* Voltage maps */ 56 static const struct voltage_map_desc ldo23_voltage_map_desc = { 57 .min = 800, .step = 50, .max = 1300, 58 }; 59 static const struct voltage_map_desc ldo456711_voltage_map_desc = { 60 .min = 1600, .step = 100, .max = 3600, 61 }; 62 static const struct voltage_map_desc ldo8_voltage_map_desc = { 63 .min = 3000, .step = 100, .max = 3600, 64 }; 65 static const struct voltage_map_desc ldo9_voltage_map_desc = { 66 .min = 2800, .step = 100, .max = 3100, 67 }; 68 static const struct voltage_map_desc ldo10_voltage_map_desc = { 69 .min = 950, .step = 50, .max = 1300, 70 }; 71 static const struct voltage_map_desc ldo1213_voltage_map_desc = { 72 .min = 800, .step = 100, .max = 3300, 73 }; 74 static const struct voltage_map_desc ldo1415_voltage_map_desc = { 75 .min = 1200, .step = 100, .max = 3300, 76 }; 77 static const struct voltage_map_desc ldo1617_voltage_map_desc = { 78 .min = 1600, .step = 100, .max = 3600, 79 }; 80 static const struct voltage_map_desc buck12_voltage_map_desc = { 81 .min = 750, .step = 25, .max = 1525, 82 }; 83 static const struct voltage_map_desc buck3_voltage_map_desc = { 84 .min = 1600, .step = 100, .max = 3600, 85 }; 86 static const struct voltage_map_desc buck4_voltage_map_desc = { 87 .min = 800, .step = 100, .max = 2300, 88 }; 89 90 static const struct voltage_map_desc *ldo_voltage_map[] = { 91 NULL, 92 NULL, 93 &ldo23_voltage_map_desc, /* LDO2 */ 94 &ldo23_voltage_map_desc, /* LDO3 */ 95 &ldo456711_voltage_map_desc, /* LDO4 */ 96 &ldo456711_voltage_map_desc, /* LDO5 */ 97 &ldo456711_voltage_map_desc, /* LDO6 */ 98 &ldo456711_voltage_map_desc, /* LDO7 */ 99 &ldo8_voltage_map_desc, /* LDO8 */ 100 &ldo9_voltage_map_desc, /* LDO9 */ 101 &ldo10_voltage_map_desc, /* LDO10 */ 102 &ldo456711_voltage_map_desc, /* LDO11 */ 103 &ldo1213_voltage_map_desc, /* LDO12 */ 104 &ldo1213_voltage_map_desc, /* LDO13 */ 105 &ldo1415_voltage_map_desc, /* LDO14 */ 106 &ldo1415_voltage_map_desc, /* LDO15 */ 107 &ldo1617_voltage_map_desc, /* LDO16 */ 108 &ldo1617_voltage_map_desc, /* LDO17 */ 109 &buck12_voltage_map_desc, /* BUCK1 */ 110 &buck12_voltage_map_desc, /* BUCK2 */ 111 &buck3_voltage_map_desc, /* BUCK3 */ 112 &buck4_voltage_map_desc, /* BUCK4 */ 113 }; 114 115 static inline int max8998_get_ldo(struct regulator_dev *rdev) 116 { 117 return rdev_get_id(rdev); 118 } 119 120 static int max8998_list_voltage(struct regulator_dev *rdev, 121 unsigned int selector) 122 { 123 const struct voltage_map_desc *desc; 124 int ldo = max8998_get_ldo(rdev); 125 int val; 126 127 if (ldo >= ARRAY_SIZE(ldo_voltage_map)) 128 return -EINVAL; 129 130 desc = ldo_voltage_map[ldo]; 131 if (desc == NULL) 132 return -EINVAL; 133 134 val = desc->min + desc->step * selector; 135 if (val > desc->max) 136 return -EINVAL; 137 138 return val * 1000; 139 } 140 141 static int max8998_get_enable_register(struct regulator_dev *rdev, 142 int *reg, int *shift) 143 { 144 int ldo = max8998_get_ldo(rdev); 145 146 switch (ldo) { 147 case MAX8998_LDO2 ... MAX8998_LDO5: 148 *reg = MAX8998_REG_ONOFF1; 149 *shift = 3 - (ldo - MAX8998_LDO2); 150 break; 151 case MAX8998_LDO6 ... MAX8998_LDO13: 152 *reg = MAX8998_REG_ONOFF2; 153 *shift = 7 - (ldo - MAX8998_LDO6); 154 break; 155 case MAX8998_LDO14 ... MAX8998_LDO17: 156 *reg = MAX8998_REG_ONOFF3; 157 *shift = 7 - (ldo - MAX8998_LDO14); 158 break; 159 case MAX8998_BUCK1 ... MAX8998_BUCK4: 160 *reg = MAX8998_REG_ONOFF1; 161 *shift = 7 - (ldo - MAX8998_BUCK1); 162 break; 163 case MAX8998_EN32KHZ_AP ... MAX8998_ENVICHG: 164 *reg = MAX8998_REG_ONOFF4; 165 *shift = 7 - (ldo - MAX8998_EN32KHZ_AP); 166 break; 167 case MAX8998_ESAFEOUT1 ... MAX8998_ESAFEOUT2: 168 *reg = MAX8998_REG_CHGR2; 169 *shift = 7 - (ldo - MAX8998_ESAFEOUT1); 170 break; 171 default: 172 return -EINVAL; 173 } 174 175 return 0; 176 } 177 178 static int max8998_ldo_is_enabled(struct regulator_dev *rdev) 179 { 180 struct max8998_data *max8998 = rdev_get_drvdata(rdev); 181 struct i2c_client *i2c = max8998->iodev->i2c; 182 int ret, reg, shift = 8; 183 u8 val; 184 185 ret = max8998_get_enable_register(rdev, ®, &shift); 186 if (ret) 187 return ret; 188 189 ret = max8998_read_reg(i2c, reg, &val); 190 if (ret) 191 return ret; 192 193 return val & (1 << shift); 194 } 195 196 static int max8998_ldo_enable(struct regulator_dev *rdev) 197 { 198 struct max8998_data *max8998 = rdev_get_drvdata(rdev); 199 struct i2c_client *i2c = max8998->iodev->i2c; 200 int reg, shift = 8, ret; 201 202 ret = max8998_get_enable_register(rdev, ®, &shift); 203 if (ret) 204 return ret; 205 206 return max8998_update_reg(i2c, reg, 1<<shift, 1<<shift); 207 } 208 209 static int max8998_ldo_disable(struct regulator_dev *rdev) 210 { 211 struct max8998_data *max8998 = rdev_get_drvdata(rdev); 212 struct i2c_client *i2c = max8998->iodev->i2c; 213 int reg, shift = 8, ret; 214 215 ret = max8998_get_enable_register(rdev, ®, &shift); 216 if (ret) 217 return ret; 218 219 return max8998_update_reg(i2c, reg, 0, 1<<shift); 220 } 221 222 static int max8998_get_voltage_register(struct regulator_dev *rdev, 223 int *_reg, int *_shift, int *_mask) 224 { 225 int ldo = max8998_get_ldo(rdev); 226 struct max8998_data *max8998 = rdev_get_drvdata(rdev); 227 int reg, shift = 0, mask = 0xff; 228 229 switch (ldo) { 230 case MAX8998_LDO2 ... MAX8998_LDO3: 231 reg = MAX8998_REG_LDO2_LDO3; 232 mask = 0xf; 233 if (ldo == MAX8998_LDO2) 234 shift = 4; 235 else 236 shift = 0; 237 break; 238 case MAX8998_LDO4 ... MAX8998_LDO7: 239 reg = MAX8998_REG_LDO4 + (ldo - MAX8998_LDO4); 240 break; 241 case MAX8998_LDO8 ... MAX8998_LDO9: 242 reg = MAX8998_REG_LDO8_LDO9; 243 mask = 0xf; 244 if (ldo == MAX8998_LDO8) 245 shift = 4; 246 else 247 shift = 0; 248 break; 249 case MAX8998_LDO10 ... MAX8998_LDO11: 250 reg = MAX8998_REG_LDO10_LDO11; 251 if (ldo == MAX8998_LDO10) { 252 shift = 5; 253 mask = 0x7; 254 } else { 255 shift = 0; 256 mask = 0x1f; 257 } 258 break; 259 case MAX8998_LDO12 ... MAX8998_LDO17: 260 reg = MAX8998_REG_LDO12 + (ldo - MAX8998_LDO12); 261 break; 262 case MAX8998_BUCK1: 263 reg = MAX8998_REG_BUCK1_VOLTAGE1 + max8998->buck1_idx; 264 break; 265 case MAX8998_BUCK2: 266 reg = MAX8998_REG_BUCK2_VOLTAGE1 + max8998->buck2_idx; 267 break; 268 case MAX8998_BUCK3: 269 reg = MAX8998_REG_BUCK3; 270 break; 271 case MAX8998_BUCK4: 272 reg = MAX8998_REG_BUCK4; 273 break; 274 default: 275 return -EINVAL; 276 } 277 278 *_reg = reg; 279 *_shift = shift; 280 *_mask = mask; 281 282 return 0; 283 } 284 285 static int max8998_get_voltage(struct regulator_dev *rdev) 286 { 287 struct max8998_data *max8998 = rdev_get_drvdata(rdev); 288 struct i2c_client *i2c = max8998->iodev->i2c; 289 int reg, shift = 0, mask, ret; 290 u8 val; 291 292 ret = max8998_get_voltage_register(rdev, ®, &shift, &mask); 293 if (ret) 294 return ret; 295 296 ret = max8998_read_reg(i2c, reg, &val); 297 if (ret) 298 return ret; 299 300 val >>= shift; 301 val &= mask; 302 303 return max8998_list_voltage(rdev, val); 304 } 305 306 static int max8998_set_voltage_ldo(struct regulator_dev *rdev, 307 int min_uV, int max_uV, unsigned *selector) 308 { 309 struct max8998_data *max8998 = rdev_get_drvdata(rdev); 310 struct i2c_client *i2c = max8998->iodev->i2c; 311 int min_vol = min_uV / 1000, max_vol = max_uV / 1000; 312 const struct voltage_map_desc *desc; 313 int ldo = max8998_get_ldo(rdev); 314 int reg, shift = 0, mask, ret; 315 int i = 0; 316 317 if (ldo >= ARRAY_SIZE(ldo_voltage_map)) 318 return -EINVAL; 319 320 desc = ldo_voltage_map[ldo]; 321 if (desc == NULL) 322 return -EINVAL; 323 324 if (max_vol < desc->min || min_vol > desc->max) 325 return -EINVAL; 326 327 while (desc->min + desc->step*i < min_vol && 328 desc->min + desc->step*i < desc->max) 329 i++; 330 331 if (desc->min + desc->step*i > max_vol) 332 return -EINVAL; 333 334 *selector = i; 335 336 ret = max8998_get_voltage_register(rdev, ®, &shift, &mask); 337 if (ret) 338 return ret; 339 340 ret = max8998_update_reg(i2c, reg, i<<shift, mask<<shift); 341 342 return ret; 343 } 344 345 static inline void buck1_gpio_set(int gpio1, int gpio2, int v) 346 { 347 gpio_set_value(gpio1, v & 0x1); 348 gpio_set_value(gpio2, (v >> 1) & 0x1); 349 } 350 351 static inline void buck2_gpio_set(int gpio, int v) 352 { 353 gpio_set_value(gpio, v & 0x1); 354 } 355 356 static int max8998_set_voltage_buck(struct regulator_dev *rdev, 357 int min_uV, int max_uV, unsigned *selector) 358 { 359 struct max8998_data *max8998 = rdev_get_drvdata(rdev); 360 struct max8998_platform_data *pdata = 361 dev_get_platdata(max8998->iodev->dev); 362 struct i2c_client *i2c = max8998->iodev->i2c; 363 int min_vol = min_uV / 1000, max_vol = max_uV / 1000; 364 const struct voltage_map_desc *desc; 365 int buck = max8998_get_ldo(rdev); 366 int reg, shift = 0, mask, ret; 367 int difference = 0, i = 0, j = 0, previous_vol = 0; 368 u8 val = 0; 369 static u8 buck1_last_val; 370 371 if (buck >= ARRAY_SIZE(ldo_voltage_map)) 372 return -EINVAL; 373 374 desc = ldo_voltage_map[buck]; 375 376 if (desc == NULL) 377 return -EINVAL; 378 379 if (max_vol < desc->min || min_vol > desc->max) 380 return -EINVAL; 381 382 while (desc->min + desc->step*i < min_vol && 383 desc->min + desc->step*i < desc->max) 384 i++; 385 386 if (desc->min + desc->step*i > max_vol) 387 return -EINVAL; 388 389 *selector = i; 390 391 ret = max8998_get_voltage_register(rdev, ®, &shift, &mask); 392 if (ret) 393 return ret; 394 395 previous_vol = max8998_get_voltage(rdev); 396 397 /* Check if voltage needs to be changed */ 398 /* if previous_voltage equal new voltage, return */ 399 if (previous_vol == max8998_list_voltage(rdev, i)) { 400 dev_dbg(max8998->dev, "No voltage change, old:%d, new:%d\n", 401 previous_vol, max8998_list_voltage(rdev, i)); 402 return ret; 403 } 404 405 switch (buck) { 406 case MAX8998_BUCK1: 407 dev_dbg(max8998->dev, 408 "BUCK1, i:%d, buck1_vol1:%d, buck1_vol2:%d\n" 409 "buck1_vol3:%d, buck1_vol4:%d\n", 410 i, max8998->buck1_vol[0], max8998->buck1_vol[1], 411 max8998->buck1_vol[2], max8998->buck1_vol[3]); 412 413 if (gpio_is_valid(pdata->buck1_set1) && 414 gpio_is_valid(pdata->buck1_set2)) { 415 416 /* check if requested voltage */ 417 /* value is already defined */ 418 for (j = 0; j < ARRAY_SIZE(max8998->buck1_vol); j++) { 419 if (max8998->buck1_vol[j] == i) { 420 max8998->buck1_idx = j; 421 buck1_gpio_set(pdata->buck1_set1, 422 pdata->buck1_set2, j); 423 goto buck1_exit; 424 } 425 } 426 427 if (pdata->buck_voltage_lock) 428 return -EINVAL; 429 430 /* no predefine regulator found */ 431 max8998->buck1_idx = (buck1_last_val % 2) + 2; 432 dev_dbg(max8998->dev, "max8998->buck1_idx:%d\n", 433 max8998->buck1_idx); 434 max8998->buck1_vol[max8998->buck1_idx] = i; 435 ret = max8998_get_voltage_register(rdev, ®, 436 &shift, 437 &mask); 438 ret = max8998_write_reg(i2c, reg, i); 439 buck1_gpio_set(pdata->buck1_set1, 440 pdata->buck1_set2, max8998->buck1_idx); 441 buck1_last_val++; 442 buck1_exit: 443 dev_dbg(max8998->dev, "%s: SET1:%d, SET2:%d\n", 444 i2c->name, gpio_get_value(pdata->buck1_set1), 445 gpio_get_value(pdata->buck1_set2)); 446 break; 447 } else { 448 ret = max8998_write_reg(i2c, reg, i); 449 } 450 break; 451 452 case MAX8998_BUCK2: 453 dev_dbg(max8998->dev, 454 "BUCK2, i:%d buck2_vol1:%d, buck2_vol2:%d\n" 455 , i, max8998->buck2_vol[0], max8998->buck2_vol[1]); 456 if (gpio_is_valid(pdata->buck2_set3)) { 457 458 /* check if requested voltage */ 459 /* value is already defined */ 460 for (j = 0; j < ARRAY_SIZE(max8998->buck2_vol); j++) { 461 if (max8998->buck2_vol[j] == i) { 462 max8998->buck2_idx = j; 463 buck2_gpio_set(pdata->buck2_set3, j); 464 goto buck2_exit; 465 } 466 } 467 468 if (pdata->buck_voltage_lock) 469 return -EINVAL; 470 471 max8998_get_voltage_register(rdev, 472 ®, &shift, &mask); 473 ret = max8998_write_reg(i2c, reg, i); 474 max8998->buck2_vol[max8998->buck2_idx] = i; 475 buck2_gpio_set(pdata->buck2_set3, max8998->buck2_idx); 476 buck2_exit: 477 dev_dbg(max8998->dev, "%s: SET3:%d\n", i2c->name, 478 gpio_get_value(pdata->buck2_set3)); 479 } else { 480 ret = max8998_write_reg(i2c, reg, i); 481 } 482 break; 483 484 case MAX8998_BUCK3: 485 case MAX8998_BUCK4: 486 ret = max8998_update_reg(i2c, reg, i<<shift, mask<<shift); 487 break; 488 } 489 490 /* Voltage stabilization */ 491 max8998_read_reg(i2c, MAX8998_REG_ONOFF4, &val); 492 493 /* lp3974 hasn't got ENRAMP bit - ramp is assumed as true */ 494 /* MAX8998 has ENRAMP bit implemented, so test it*/ 495 if (max8998->iodev->type == TYPE_MAX8998 && !(val & MAX8998_ENRAMP)) 496 return ret; 497 498 difference = desc->min + desc->step*i - previous_vol/1000; 499 if (difference > 0) 500 udelay(difference / ((val & 0x0f) + 1)); 501 502 return ret; 503 } 504 505 static struct regulator_ops max8998_ldo_ops = { 506 .list_voltage = max8998_list_voltage, 507 .is_enabled = max8998_ldo_is_enabled, 508 .enable = max8998_ldo_enable, 509 .disable = max8998_ldo_disable, 510 .get_voltage = max8998_get_voltage, 511 .set_voltage = max8998_set_voltage_ldo, 512 .set_suspend_enable = max8998_ldo_enable, 513 .set_suspend_disable = max8998_ldo_disable, 514 }; 515 516 static struct regulator_ops max8998_buck_ops = { 517 .list_voltage = max8998_list_voltage, 518 .is_enabled = max8998_ldo_is_enabled, 519 .enable = max8998_ldo_enable, 520 .disable = max8998_ldo_disable, 521 .get_voltage = max8998_get_voltage, 522 .set_voltage = max8998_set_voltage_buck, 523 .set_suspend_enable = max8998_ldo_enable, 524 .set_suspend_disable = max8998_ldo_disable, 525 }; 526 527 static struct regulator_ops max8998_others_ops = { 528 .is_enabled = max8998_ldo_is_enabled, 529 .enable = max8998_ldo_enable, 530 .disable = max8998_ldo_disable, 531 .set_suspend_enable = max8998_ldo_enable, 532 .set_suspend_disable = max8998_ldo_disable, 533 }; 534 535 static struct regulator_desc regulators[] = { 536 { 537 .name = "LDO2", 538 .id = MAX8998_LDO2, 539 .ops = &max8998_ldo_ops, 540 .type = REGULATOR_VOLTAGE, 541 .owner = THIS_MODULE, 542 }, { 543 .name = "LDO3", 544 .id = MAX8998_LDO3, 545 .ops = &max8998_ldo_ops, 546 .type = REGULATOR_VOLTAGE, 547 .owner = THIS_MODULE, 548 }, { 549 .name = "LDO4", 550 .id = MAX8998_LDO4, 551 .ops = &max8998_ldo_ops, 552 .type = REGULATOR_VOLTAGE, 553 .owner = THIS_MODULE, 554 }, { 555 .name = "LDO5", 556 .id = MAX8998_LDO5, 557 .ops = &max8998_ldo_ops, 558 .type = REGULATOR_VOLTAGE, 559 .owner = THIS_MODULE, 560 }, { 561 .name = "LDO6", 562 .id = MAX8998_LDO6, 563 .ops = &max8998_ldo_ops, 564 .type = REGULATOR_VOLTAGE, 565 .owner = THIS_MODULE, 566 }, { 567 .name = "LDO7", 568 .id = MAX8998_LDO7, 569 .ops = &max8998_ldo_ops, 570 .type = REGULATOR_VOLTAGE, 571 .owner = THIS_MODULE, 572 }, { 573 .name = "LDO8", 574 .id = MAX8998_LDO8, 575 .ops = &max8998_ldo_ops, 576 .type = REGULATOR_VOLTAGE, 577 .owner = THIS_MODULE, 578 }, { 579 .name = "LDO9", 580 .id = MAX8998_LDO9, 581 .ops = &max8998_ldo_ops, 582 .type = REGULATOR_VOLTAGE, 583 .owner = THIS_MODULE, 584 }, { 585 .name = "LDO10", 586 .id = MAX8998_LDO10, 587 .ops = &max8998_ldo_ops, 588 .type = REGULATOR_VOLTAGE, 589 .owner = THIS_MODULE, 590 }, { 591 .name = "LDO11", 592 .id = MAX8998_LDO11, 593 .ops = &max8998_ldo_ops, 594 .type = REGULATOR_VOLTAGE, 595 .owner = THIS_MODULE, 596 }, { 597 .name = "LDO12", 598 .id = MAX8998_LDO12, 599 .ops = &max8998_ldo_ops, 600 .type = REGULATOR_VOLTAGE, 601 .owner = THIS_MODULE, 602 }, { 603 .name = "LDO13", 604 .id = MAX8998_LDO13, 605 .ops = &max8998_ldo_ops, 606 .type = REGULATOR_VOLTAGE, 607 .owner = THIS_MODULE, 608 }, { 609 .name = "LDO14", 610 .id = MAX8998_LDO14, 611 .ops = &max8998_ldo_ops, 612 .type = REGULATOR_VOLTAGE, 613 .owner = THIS_MODULE, 614 }, { 615 .name = "LDO15", 616 .id = MAX8998_LDO15, 617 .ops = &max8998_ldo_ops, 618 .type = REGULATOR_VOLTAGE, 619 .owner = THIS_MODULE, 620 }, { 621 .name = "LDO16", 622 .id = MAX8998_LDO16, 623 .ops = &max8998_ldo_ops, 624 .type = REGULATOR_VOLTAGE, 625 .owner = THIS_MODULE, 626 }, { 627 .name = "LDO17", 628 .id = MAX8998_LDO17, 629 .ops = &max8998_ldo_ops, 630 .type = REGULATOR_VOLTAGE, 631 .owner = THIS_MODULE, 632 }, { 633 .name = "BUCK1", 634 .id = MAX8998_BUCK1, 635 .ops = &max8998_buck_ops, 636 .type = REGULATOR_VOLTAGE, 637 .owner = THIS_MODULE, 638 }, { 639 .name = "BUCK2", 640 .id = MAX8998_BUCK2, 641 .ops = &max8998_buck_ops, 642 .type = REGULATOR_VOLTAGE, 643 .owner = THIS_MODULE, 644 }, { 645 .name = "BUCK3", 646 .id = MAX8998_BUCK3, 647 .ops = &max8998_buck_ops, 648 .type = REGULATOR_VOLTAGE, 649 .owner = THIS_MODULE, 650 }, { 651 .name = "BUCK4", 652 .id = MAX8998_BUCK4, 653 .ops = &max8998_buck_ops, 654 .type = REGULATOR_VOLTAGE, 655 .owner = THIS_MODULE, 656 }, { 657 .name = "EN32KHz AP", 658 .id = MAX8998_EN32KHZ_AP, 659 .ops = &max8998_others_ops, 660 .type = REGULATOR_VOLTAGE, 661 .owner = THIS_MODULE, 662 }, { 663 .name = "EN32KHz CP", 664 .id = MAX8998_EN32KHZ_CP, 665 .ops = &max8998_others_ops, 666 .type = REGULATOR_VOLTAGE, 667 .owner = THIS_MODULE, 668 }, { 669 .name = "ENVICHG", 670 .id = MAX8998_ENVICHG, 671 .ops = &max8998_others_ops, 672 .type = REGULATOR_VOLTAGE, 673 .owner = THIS_MODULE, 674 }, { 675 .name = "ESAFEOUT1", 676 .id = MAX8998_ESAFEOUT1, 677 .ops = &max8998_others_ops, 678 .type = REGULATOR_VOLTAGE, 679 .owner = THIS_MODULE, 680 }, { 681 .name = "ESAFEOUT2", 682 .id = MAX8998_ESAFEOUT2, 683 .ops = &max8998_others_ops, 684 .type = REGULATOR_VOLTAGE, 685 .owner = THIS_MODULE, 686 } 687 }; 688 689 static __devinit int max8998_pmic_probe(struct platform_device *pdev) 690 { 691 struct max8998_dev *iodev = dev_get_drvdata(pdev->dev.parent); 692 struct max8998_platform_data *pdata = dev_get_platdata(iodev->dev); 693 struct regulator_dev **rdev; 694 struct max8998_data *max8998; 695 struct i2c_client *i2c; 696 int i, ret, size; 697 698 if (!pdata) { 699 dev_err(pdev->dev.parent, "No platform init data supplied\n"); 700 return -ENODEV; 701 } 702 703 max8998 = kzalloc(sizeof(struct max8998_data), GFP_KERNEL); 704 if (!max8998) 705 return -ENOMEM; 706 707 size = sizeof(struct regulator_dev *) * pdata->num_regulators; 708 max8998->rdev = kzalloc(size, GFP_KERNEL); 709 if (!max8998->rdev) { 710 kfree(max8998); 711 return -ENOMEM; 712 } 713 714 rdev = max8998->rdev; 715 max8998->dev = &pdev->dev; 716 max8998->iodev = iodev; 717 max8998->num_regulators = pdata->num_regulators; 718 platform_set_drvdata(pdev, max8998); 719 i2c = max8998->iodev->i2c; 720 721 max8998->buck1_idx = pdata->buck1_default_idx; 722 max8998->buck2_idx = pdata->buck2_default_idx; 723 724 /* NOTE: */ 725 /* For unused GPIO NOT marked as -1 (thereof equal to 0) WARN_ON */ 726 /* will be displayed */ 727 728 /* Check if MAX8998 voltage selection GPIOs are defined */ 729 if (gpio_is_valid(pdata->buck1_set1) && 730 gpio_is_valid(pdata->buck1_set2)) { 731 /* Check if SET1 is not equal to 0 */ 732 if (!pdata->buck1_set1) { 733 printk(KERN_ERR "MAX8998 SET1 GPIO defined as 0 !\n"); 734 WARN_ON(!pdata->buck1_set1); 735 ret = -EIO; 736 goto err_free_mem; 737 } 738 /* Check if SET2 is not equal to 0 */ 739 if (!pdata->buck1_set2) { 740 printk(KERN_ERR "MAX8998 SET2 GPIO defined as 0 !\n"); 741 WARN_ON(!pdata->buck1_set2); 742 ret = -EIO; 743 goto err_free_mem; 744 } 745 746 gpio_request(pdata->buck1_set1, "MAX8998 BUCK1_SET1"); 747 gpio_direction_output(pdata->buck1_set1, 748 max8998->buck1_idx & 0x1); 749 750 751 gpio_request(pdata->buck1_set2, "MAX8998 BUCK1_SET2"); 752 gpio_direction_output(pdata->buck1_set2, 753 (max8998->buck1_idx >> 1) & 0x1); 754 /* Set predefined value for BUCK1 register 1 */ 755 i = 0; 756 while (buck12_voltage_map_desc.min + 757 buck12_voltage_map_desc.step*i 758 < (pdata->buck1_voltage1 / 1000)) 759 i++; 760 max8998->buck1_vol[0] = i; 761 ret = max8998_write_reg(i2c, MAX8998_REG_BUCK1_VOLTAGE1, i); 762 if (ret) 763 goto err_free_mem; 764 765 /* Set predefined value for BUCK1 register 2 */ 766 i = 0; 767 while (buck12_voltage_map_desc.min + 768 buck12_voltage_map_desc.step*i 769 < (pdata->buck1_voltage2 / 1000)) 770 i++; 771 772 max8998->buck1_vol[1] = i; 773 ret = max8998_write_reg(i2c, MAX8998_REG_BUCK1_VOLTAGE2, i); 774 if (ret) 775 goto err_free_mem; 776 777 /* Set predefined value for BUCK1 register 3 */ 778 i = 0; 779 while (buck12_voltage_map_desc.min + 780 buck12_voltage_map_desc.step*i 781 < (pdata->buck1_voltage3 / 1000)) 782 i++; 783 784 max8998->buck1_vol[2] = i; 785 ret = max8998_write_reg(i2c, MAX8998_REG_BUCK1_VOLTAGE3, i); 786 if (ret) 787 goto err_free_mem; 788 789 /* Set predefined value for BUCK1 register 4 */ 790 i = 0; 791 while (buck12_voltage_map_desc.min + 792 buck12_voltage_map_desc.step*i 793 < (pdata->buck1_voltage4 / 1000)) 794 i++; 795 796 max8998->buck1_vol[3] = i; 797 ret = max8998_write_reg(i2c, MAX8998_REG_BUCK1_VOLTAGE4, i); 798 if (ret) 799 goto err_free_mem; 800 801 } 802 803 if (gpio_is_valid(pdata->buck2_set3)) { 804 /* Check if SET3 is not equal to 0 */ 805 if (!pdata->buck2_set3) { 806 printk(KERN_ERR "MAX8998 SET3 GPIO defined as 0 !\n"); 807 WARN_ON(!pdata->buck2_set3); 808 ret = -EIO; 809 goto err_free_mem; 810 } 811 gpio_request(pdata->buck2_set3, "MAX8998 BUCK2_SET3"); 812 gpio_direction_output(pdata->buck2_set3, 813 max8998->buck2_idx & 0x1); 814 815 /* BUCK2 register 1 */ 816 i = 0; 817 while (buck12_voltage_map_desc.min + 818 buck12_voltage_map_desc.step*i 819 < (pdata->buck2_voltage1 / 1000)) 820 i++; 821 max8998->buck2_vol[0] = i; 822 ret = max8998_write_reg(i2c, MAX8998_REG_BUCK2_VOLTAGE1, i); 823 if (ret) 824 goto err_free_mem; 825 826 /* BUCK2 register 2 */ 827 i = 0; 828 while (buck12_voltage_map_desc.min + 829 buck12_voltage_map_desc.step*i 830 < (pdata->buck2_voltage2 / 1000)) 831 i++; 832 printk(KERN_ERR "i2:%d, buck2_idx:%d\n", i, max8998->buck2_idx); 833 max8998->buck2_vol[1] = i; 834 ret = max8998_write_reg(i2c, MAX8998_REG_BUCK2_VOLTAGE2, i); 835 if (ret) 836 goto err_free_mem; 837 } 838 839 for (i = 0; i < pdata->num_regulators; i++) { 840 const struct voltage_map_desc *desc; 841 int id = pdata->regulators[i].id; 842 int index = id - MAX8998_LDO2; 843 844 desc = ldo_voltage_map[id]; 845 if (desc && regulators[index].ops != &max8998_others_ops) { 846 int count = (desc->max - desc->min) / desc->step + 1; 847 regulators[index].n_voltages = count; 848 } 849 rdev[i] = regulator_register(®ulators[index], max8998->dev, 850 pdata->regulators[i].initdata, max8998, NULL); 851 if (IS_ERR(rdev[i])) { 852 ret = PTR_ERR(rdev[i]); 853 dev_err(max8998->dev, "regulator init failed\n"); 854 rdev[i] = NULL; 855 goto err; 856 } 857 } 858 859 860 return 0; 861 err: 862 for (i = 0; i < max8998->num_regulators; i++) 863 if (rdev[i]) 864 regulator_unregister(rdev[i]); 865 866 err_free_mem: 867 kfree(max8998->rdev); 868 kfree(max8998); 869 870 return ret; 871 } 872 873 static int __devexit max8998_pmic_remove(struct platform_device *pdev) 874 { 875 struct max8998_data *max8998 = platform_get_drvdata(pdev); 876 struct regulator_dev **rdev = max8998->rdev; 877 int i; 878 879 for (i = 0; i < max8998->num_regulators; i++) 880 if (rdev[i]) 881 regulator_unregister(rdev[i]); 882 883 kfree(max8998->rdev); 884 kfree(max8998); 885 886 return 0; 887 } 888 889 static const struct platform_device_id max8998_pmic_id[] = { 890 { "max8998-pmic", TYPE_MAX8998 }, 891 { "lp3974-pmic", TYPE_LP3974 }, 892 { } 893 }; 894 MODULE_DEVICE_TABLE(platform, max8998_pmic_id); 895 896 static struct platform_driver max8998_pmic_driver = { 897 .driver = { 898 .name = "max8998-pmic", 899 .owner = THIS_MODULE, 900 }, 901 .probe = max8998_pmic_probe, 902 .remove = __devexit_p(max8998_pmic_remove), 903 .id_table = max8998_pmic_id, 904 }; 905 906 static int __init max8998_pmic_init(void) 907 { 908 return platform_driver_register(&max8998_pmic_driver); 909 } 910 subsys_initcall(max8998_pmic_init); 911 912 static void __exit max8998_pmic_cleanup(void) 913 { 914 platform_driver_unregister(&max8998_pmic_driver); 915 } 916 module_exit(max8998_pmic_cleanup); 917 918 MODULE_DESCRIPTION("MAXIM 8998 voltage regulator driver"); 919 MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>"); 920 MODULE_LICENSE("GPL"); 921