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