1 // SPDX-License-Identifier: GPL-2.0+ 2 // 3 // max8998.c - Voltage regulator driver for the Maxim 8998 4 // 5 // Copyright (C) 2009-2010 Samsung Electronics 6 // Kyungmin Park <kyungmin.park@samsung.com> 7 // Marek Szyprowski <m.szyprowski@samsung.com> 8 9 #include <linux/module.h> 10 #include <linux/init.h> 11 #include <linux/i2c.h> 12 #include <linux/err.h> 13 #include <linux/gpio.h> 14 #include <linux/slab.h> 15 #include <linux/interrupt.h> 16 #include <linux/mutex.h> 17 #include <linux/of.h> 18 #include <linux/of_gpio.h> 19 #include <linux/platform_device.h> 20 #include <linux/regulator/driver.h> 21 #include <linux/regulator/of_regulator.h> 22 #include <linux/mfd/max8998.h> 23 #include <linux/mfd/max8998-private.h> 24 25 struct max8998_data { 26 struct device *dev; 27 struct max8998_dev *iodev; 28 int num_regulators; 29 u8 buck1_vol[4]; /* voltages for selection */ 30 u8 buck2_vol[2]; 31 unsigned int buck1_idx; /* index to last changed voltage */ 32 /* value in a set */ 33 unsigned int buck2_idx; 34 }; 35 36 static const unsigned int charger_current_table[] = { 37 90000, 380000, 475000, 550000, 570000, 600000, 700000, 800000, 38 }; 39 40 static int max8998_get_enable_register(struct regulator_dev *rdev, 41 int *reg, int *shift) 42 { 43 int ldo = rdev_get_id(rdev); 44 45 switch (ldo) { 46 case MAX8998_LDO2 ... MAX8998_LDO5: 47 *reg = MAX8998_REG_ONOFF1; 48 *shift = 3 - (ldo - MAX8998_LDO2); 49 break; 50 case MAX8998_LDO6 ... MAX8998_LDO13: 51 *reg = MAX8998_REG_ONOFF2; 52 *shift = 7 - (ldo - MAX8998_LDO6); 53 break; 54 case MAX8998_LDO14 ... MAX8998_LDO17: 55 *reg = MAX8998_REG_ONOFF3; 56 *shift = 7 - (ldo - MAX8998_LDO14); 57 break; 58 case MAX8998_BUCK1 ... MAX8998_BUCK4: 59 *reg = MAX8998_REG_ONOFF1; 60 *shift = 7 - (ldo - MAX8998_BUCK1); 61 break; 62 case MAX8998_EN32KHZ_AP ... MAX8998_ENVICHG: 63 *reg = MAX8998_REG_ONOFF4; 64 *shift = 7 - (ldo - MAX8998_EN32KHZ_AP); 65 break; 66 case MAX8998_ESAFEOUT1 ... MAX8998_ESAFEOUT2: 67 *reg = MAX8998_REG_CHGR2; 68 *shift = 7 - (ldo - MAX8998_ESAFEOUT1); 69 break; 70 case MAX8998_CHARGER: 71 *reg = MAX8998_REG_CHGR2; 72 *shift = 0; 73 break; 74 default: 75 return -EINVAL; 76 } 77 78 return 0; 79 } 80 81 static int max8998_ldo_is_enabled(struct regulator_dev *rdev) 82 { 83 struct max8998_data *max8998 = rdev_get_drvdata(rdev); 84 struct i2c_client *i2c = max8998->iodev->i2c; 85 int ret, reg, shift = 8; 86 u8 val; 87 88 ret = max8998_get_enable_register(rdev, ®, &shift); 89 if (ret) 90 return ret; 91 92 ret = max8998_read_reg(i2c, reg, &val); 93 if (ret) 94 return ret; 95 96 return val & (1 << shift); 97 } 98 99 static int max8998_ldo_is_enabled_inverted(struct regulator_dev *rdev) 100 { 101 return (!max8998_ldo_is_enabled(rdev)); 102 } 103 104 static int max8998_ldo_enable(struct regulator_dev *rdev) 105 { 106 struct max8998_data *max8998 = rdev_get_drvdata(rdev); 107 struct i2c_client *i2c = max8998->iodev->i2c; 108 int reg, shift = 8, ret; 109 110 ret = max8998_get_enable_register(rdev, ®, &shift); 111 if (ret) 112 return ret; 113 114 return max8998_update_reg(i2c, reg, 1<<shift, 1<<shift); 115 } 116 117 static int max8998_ldo_disable(struct regulator_dev *rdev) 118 { 119 struct max8998_data *max8998 = rdev_get_drvdata(rdev); 120 struct i2c_client *i2c = max8998->iodev->i2c; 121 int reg, shift = 8, ret; 122 123 ret = max8998_get_enable_register(rdev, ®, &shift); 124 if (ret) 125 return ret; 126 127 return max8998_update_reg(i2c, reg, 0, 1<<shift); 128 } 129 130 static int max8998_get_voltage_register(struct regulator_dev *rdev, 131 int *_reg, int *_shift, int *_mask) 132 { 133 int ldo = rdev_get_id(rdev); 134 struct max8998_data *max8998 = rdev_get_drvdata(rdev); 135 int reg, shift = 0, mask = 0xff; 136 137 switch (ldo) { 138 case MAX8998_LDO2 ... MAX8998_LDO3: 139 reg = MAX8998_REG_LDO2_LDO3; 140 mask = 0xf; 141 if (ldo == MAX8998_LDO2) 142 shift = 4; 143 else 144 shift = 0; 145 break; 146 case MAX8998_LDO4 ... MAX8998_LDO7: 147 reg = MAX8998_REG_LDO4 + (ldo - MAX8998_LDO4); 148 break; 149 case MAX8998_LDO8 ... MAX8998_LDO9: 150 reg = MAX8998_REG_LDO8_LDO9; 151 mask = 0xf; 152 if (ldo == MAX8998_LDO8) 153 shift = 4; 154 else 155 shift = 0; 156 break; 157 case MAX8998_LDO10 ... MAX8998_LDO11: 158 reg = MAX8998_REG_LDO10_LDO11; 159 if (ldo == MAX8998_LDO10) { 160 shift = 5; 161 mask = 0x7; 162 } else { 163 shift = 0; 164 mask = 0x1f; 165 } 166 break; 167 case MAX8998_LDO12 ... MAX8998_LDO17: 168 reg = MAX8998_REG_LDO12 + (ldo - MAX8998_LDO12); 169 break; 170 case MAX8998_BUCK1: 171 reg = MAX8998_REG_BUCK1_VOLTAGE1 + max8998->buck1_idx; 172 break; 173 case MAX8998_BUCK2: 174 reg = MAX8998_REG_BUCK2_VOLTAGE1 + max8998->buck2_idx; 175 break; 176 case MAX8998_BUCK3: 177 reg = MAX8998_REG_BUCK3; 178 break; 179 case MAX8998_BUCK4: 180 reg = MAX8998_REG_BUCK4; 181 break; 182 default: 183 return -EINVAL; 184 } 185 186 *_reg = reg; 187 *_shift = shift; 188 *_mask = mask; 189 190 return 0; 191 } 192 193 static int max8998_get_voltage_sel(struct regulator_dev *rdev) 194 { 195 struct max8998_data *max8998 = rdev_get_drvdata(rdev); 196 struct i2c_client *i2c = max8998->iodev->i2c; 197 int reg, shift = 0, mask, ret; 198 u8 val; 199 200 ret = max8998_get_voltage_register(rdev, ®, &shift, &mask); 201 if (ret) 202 return ret; 203 204 ret = max8998_read_reg(i2c, reg, &val); 205 if (ret) 206 return ret; 207 208 val >>= shift; 209 val &= mask; 210 211 return val; 212 } 213 214 static int max8998_set_voltage_ldo_sel(struct regulator_dev *rdev, 215 unsigned selector) 216 { 217 struct max8998_data *max8998 = rdev_get_drvdata(rdev); 218 struct i2c_client *i2c = max8998->iodev->i2c; 219 int reg, shift = 0, mask, ret; 220 221 ret = max8998_get_voltage_register(rdev, ®, &shift, &mask); 222 if (ret) 223 return ret; 224 225 ret = max8998_update_reg(i2c, reg, selector<<shift, mask<<shift); 226 227 return ret; 228 } 229 230 static inline void buck1_gpio_set(int gpio1, int gpio2, int v) 231 { 232 gpio_set_value(gpio1, v & 0x1); 233 gpio_set_value(gpio2, (v >> 1) & 0x1); 234 } 235 236 static inline void buck2_gpio_set(int gpio, int v) 237 { 238 gpio_set_value(gpio, v & 0x1); 239 } 240 241 static int max8998_set_voltage_buck_sel(struct regulator_dev *rdev, 242 unsigned selector) 243 { 244 struct max8998_data *max8998 = rdev_get_drvdata(rdev); 245 struct max8998_platform_data *pdata = max8998->iodev->pdata; 246 struct i2c_client *i2c = max8998->iodev->i2c; 247 int buck = rdev_get_id(rdev); 248 int reg, shift = 0, mask, ret, j; 249 static u8 buck1_last_val; 250 251 ret = max8998_get_voltage_register(rdev, ®, &shift, &mask); 252 if (ret) 253 return ret; 254 255 switch (buck) { 256 case MAX8998_BUCK1: 257 dev_dbg(max8998->dev, 258 "BUCK1, selector:%d, buck1_vol1:%d, buck1_vol2:%d\n" 259 "buck1_vol3:%d, buck1_vol4:%d\n", 260 selector, max8998->buck1_vol[0], max8998->buck1_vol[1], 261 max8998->buck1_vol[2], max8998->buck1_vol[3]); 262 263 if (gpio_is_valid(pdata->buck1_set1) && 264 gpio_is_valid(pdata->buck1_set2)) { 265 266 /* check if requested voltage */ 267 /* value is already defined */ 268 for (j = 0; j < ARRAY_SIZE(max8998->buck1_vol); j++) { 269 if (max8998->buck1_vol[j] == selector) { 270 max8998->buck1_idx = j; 271 buck1_gpio_set(pdata->buck1_set1, 272 pdata->buck1_set2, j); 273 goto buck1_exit; 274 } 275 } 276 277 if (pdata->buck_voltage_lock) 278 return -EINVAL; 279 280 /* no predefine regulator found */ 281 max8998->buck1_idx = (buck1_last_val % 2) + 2; 282 dev_dbg(max8998->dev, "max8998->buck1_idx:%d\n", 283 max8998->buck1_idx); 284 max8998->buck1_vol[max8998->buck1_idx] = selector; 285 ret = max8998_get_voltage_register(rdev, ®, 286 &shift, 287 &mask); 288 ret = max8998_write_reg(i2c, reg, selector); 289 buck1_gpio_set(pdata->buck1_set1, 290 pdata->buck1_set2, max8998->buck1_idx); 291 buck1_last_val++; 292 buck1_exit: 293 dev_dbg(max8998->dev, "%s: SET1:%d, SET2:%d\n", 294 i2c->name, gpio_get_value(pdata->buck1_set1), 295 gpio_get_value(pdata->buck1_set2)); 296 break; 297 } else { 298 ret = max8998_write_reg(i2c, reg, selector); 299 } 300 break; 301 302 case MAX8998_BUCK2: 303 dev_dbg(max8998->dev, 304 "BUCK2, selector:%d buck2_vol1:%d, buck2_vol2:%d\n", 305 selector, max8998->buck2_vol[0], max8998->buck2_vol[1]); 306 if (gpio_is_valid(pdata->buck2_set3)) { 307 308 /* check if requested voltage */ 309 /* value is already defined */ 310 for (j = 0; j < ARRAY_SIZE(max8998->buck2_vol); j++) { 311 if (max8998->buck2_vol[j] == selector) { 312 max8998->buck2_idx = j; 313 buck2_gpio_set(pdata->buck2_set3, j); 314 goto buck2_exit; 315 } 316 } 317 318 if (pdata->buck_voltage_lock) 319 return -EINVAL; 320 321 max8998_get_voltage_register(rdev, 322 ®, &shift, &mask); 323 ret = max8998_write_reg(i2c, reg, selector); 324 max8998->buck2_vol[max8998->buck2_idx] = selector; 325 buck2_gpio_set(pdata->buck2_set3, max8998->buck2_idx); 326 buck2_exit: 327 dev_dbg(max8998->dev, "%s: SET3:%d\n", i2c->name, 328 gpio_get_value(pdata->buck2_set3)); 329 } else { 330 ret = max8998_write_reg(i2c, reg, selector); 331 } 332 break; 333 334 case MAX8998_BUCK3: 335 case MAX8998_BUCK4: 336 ret = max8998_update_reg(i2c, reg, selector<<shift, 337 mask<<shift); 338 break; 339 } 340 341 return ret; 342 } 343 344 static int max8998_set_voltage_buck_time_sel(struct regulator_dev *rdev, 345 unsigned int old_selector, 346 unsigned int new_selector) 347 { 348 struct max8998_data *max8998 = rdev_get_drvdata(rdev); 349 struct i2c_client *i2c = max8998->iodev->i2c; 350 int buck = rdev_get_id(rdev); 351 u8 val = 0; 352 int difference, ret; 353 354 if (buck < MAX8998_BUCK1 || buck > MAX8998_BUCK4) 355 return -EINVAL; 356 357 /* Voltage stabilization */ 358 ret = max8998_read_reg(i2c, MAX8998_REG_ONOFF4, &val); 359 if (ret) 360 return ret; 361 362 /* lp3974 hasn't got ENRAMP bit - ramp is assumed as true */ 363 /* MAX8998 has ENRAMP bit implemented, so test it*/ 364 if (max8998->iodev->type == TYPE_MAX8998 && !(val & MAX8998_ENRAMP)) 365 return 0; 366 367 difference = (new_selector - old_selector) * rdev->desc->uV_step / 1000; 368 if (difference > 0) 369 return DIV_ROUND_UP(difference, (val & 0x0f) + 1); 370 371 return 0; 372 } 373 374 static int max8998_set_current_limit(struct regulator_dev *rdev, 375 int min_uA, int max_uA) 376 { 377 struct max8998_data *max8998 = rdev_get_drvdata(rdev); 378 struct i2c_client *i2c = max8998->iodev->i2c; 379 unsigned int n_currents = rdev->desc->n_current_limits; 380 int i, sel = -1; 381 382 if (n_currents == 0) 383 return -EINVAL; 384 385 if (rdev->desc->curr_table) { 386 const unsigned int *curr_table = rdev->desc->curr_table; 387 bool ascend = curr_table[n_currents - 1] > curr_table[0]; 388 389 /* search for closest to maximum */ 390 if (ascend) { 391 for (i = n_currents - 1; i >= 0; i--) { 392 if (min_uA <= curr_table[i] && 393 curr_table[i] <= max_uA) { 394 sel = i; 395 break; 396 } 397 } 398 } else { 399 for (i = 0; i < n_currents; i++) { 400 if (min_uA <= curr_table[i] && 401 curr_table[i] <= max_uA) { 402 sel = i; 403 break; 404 } 405 } 406 } 407 } 408 409 if (sel < 0) 410 return -EINVAL; 411 412 sel <<= ffs(rdev->desc->csel_mask) - 1; 413 414 return max8998_update_reg(i2c, rdev->desc->csel_reg, 415 sel, rdev->desc->csel_mask); 416 } 417 418 static int max8998_get_current_limit(struct regulator_dev *rdev) 419 { 420 struct max8998_data *max8998 = rdev_get_drvdata(rdev); 421 struct i2c_client *i2c = max8998->iodev->i2c; 422 u8 val; 423 int ret; 424 425 ret = max8998_read_reg(i2c, rdev->desc->csel_reg, &val); 426 if (ret != 0) 427 return ret; 428 429 val &= rdev->desc->csel_mask; 430 val >>= ffs(rdev->desc->csel_mask) - 1; 431 432 if (rdev->desc->curr_table) { 433 if (val >= rdev->desc->n_current_limits) 434 return -EINVAL; 435 436 return rdev->desc->curr_table[val]; 437 } 438 439 return -EINVAL; 440 } 441 442 static const struct regulator_ops max8998_ldo_ops = { 443 .list_voltage = regulator_list_voltage_linear, 444 .map_voltage = regulator_map_voltage_linear, 445 .is_enabled = max8998_ldo_is_enabled, 446 .enable = max8998_ldo_enable, 447 .disable = max8998_ldo_disable, 448 .get_voltage_sel = max8998_get_voltage_sel, 449 .set_voltage_sel = max8998_set_voltage_ldo_sel, 450 }; 451 452 static const struct regulator_ops max8998_buck_ops = { 453 .list_voltage = regulator_list_voltage_linear, 454 .map_voltage = regulator_map_voltage_linear, 455 .is_enabled = max8998_ldo_is_enabled, 456 .enable = max8998_ldo_enable, 457 .disable = max8998_ldo_disable, 458 .get_voltage_sel = max8998_get_voltage_sel, 459 .set_voltage_sel = max8998_set_voltage_buck_sel, 460 .set_voltage_time_sel = max8998_set_voltage_buck_time_sel, 461 }; 462 463 static const struct regulator_ops max8998_charger_ops = { 464 .set_current_limit = max8998_set_current_limit, 465 .get_current_limit = max8998_get_current_limit, 466 .is_enabled = max8998_ldo_is_enabled_inverted, 467 /* Swapped as register is inverted */ 468 .enable = max8998_ldo_disable, 469 .disable = max8998_ldo_enable, 470 }; 471 472 static const struct regulator_ops max8998_others_ops = { 473 .is_enabled = max8998_ldo_is_enabled, 474 .enable = max8998_ldo_enable, 475 .disable = max8998_ldo_disable, 476 }; 477 478 #define MAX8998_LINEAR_REG(_name, _ops, _min, _step, _max) \ 479 { \ 480 .name = #_name, \ 481 .id = MAX8998_##_name, \ 482 .ops = _ops, \ 483 .min_uV = (_min), \ 484 .uV_step = (_step), \ 485 .n_voltages = ((_max) - (_min)) / (_step) + 1, \ 486 .type = REGULATOR_VOLTAGE, \ 487 .owner = THIS_MODULE, \ 488 } 489 490 #define MAX8998_CURRENT_REG(_name, _ops, _table, _reg, _mask) \ 491 { \ 492 .name = #_name, \ 493 .id = MAX8998_##_name, \ 494 .ops = _ops, \ 495 .curr_table = _table, \ 496 .n_current_limits = ARRAY_SIZE(_table), \ 497 .csel_reg = _reg, \ 498 .csel_mask = _mask, \ 499 .type = REGULATOR_CURRENT, \ 500 .owner = THIS_MODULE, \ 501 } 502 503 #define MAX8998_OTHERS_REG(_name, _id) \ 504 { \ 505 .name = #_name, \ 506 .id = _id, \ 507 .ops = &max8998_others_ops, \ 508 .type = REGULATOR_VOLTAGE, \ 509 .owner = THIS_MODULE, \ 510 } 511 512 static const struct regulator_desc regulators[] = { 513 MAX8998_LINEAR_REG(LDO2, &max8998_ldo_ops, 800000, 50000, 1300000), 514 MAX8998_LINEAR_REG(LDO3, &max8998_ldo_ops, 800000, 50000, 1300000), 515 MAX8998_LINEAR_REG(LDO4, &max8998_ldo_ops, 1600000, 100000, 3600000), 516 MAX8998_LINEAR_REG(LDO5, &max8998_ldo_ops, 1600000, 100000, 3600000), 517 MAX8998_LINEAR_REG(LDO6, &max8998_ldo_ops, 1600000, 100000, 3600000), 518 MAX8998_LINEAR_REG(LDO7, &max8998_ldo_ops, 1600000, 100000, 3600000), 519 MAX8998_LINEAR_REG(LDO8, &max8998_ldo_ops, 3000000, 100000, 3600000), 520 MAX8998_LINEAR_REG(LDO9, &max8998_ldo_ops, 2800000, 100000, 3100000), 521 MAX8998_LINEAR_REG(LDO10, &max8998_ldo_ops, 950000, 50000, 1300000), 522 MAX8998_LINEAR_REG(LDO11, &max8998_ldo_ops, 1600000, 100000, 3600000), 523 MAX8998_LINEAR_REG(LDO12, &max8998_ldo_ops, 800000, 100000, 3300000), 524 MAX8998_LINEAR_REG(LDO13, &max8998_ldo_ops, 800000, 100000, 3300000), 525 MAX8998_LINEAR_REG(LDO14, &max8998_ldo_ops, 1200000, 100000, 3300000), 526 MAX8998_LINEAR_REG(LDO15, &max8998_ldo_ops, 1200000, 100000, 3300000), 527 MAX8998_LINEAR_REG(LDO16, &max8998_ldo_ops, 1600000, 100000, 3600000), 528 MAX8998_LINEAR_REG(LDO17, &max8998_ldo_ops, 1600000, 100000, 3600000), 529 MAX8998_LINEAR_REG(BUCK1, &max8998_buck_ops, 750000, 25000, 1525000), 530 MAX8998_LINEAR_REG(BUCK2, &max8998_buck_ops, 750000, 25000, 1525000), 531 MAX8998_LINEAR_REG(BUCK3, &max8998_buck_ops, 1600000, 100000, 3600000), 532 MAX8998_LINEAR_REG(BUCK4, &max8998_buck_ops, 800000, 100000, 2300000), 533 MAX8998_OTHERS_REG(EN32KHz-AP, MAX8998_EN32KHZ_AP), 534 MAX8998_OTHERS_REG(EN32KHz-CP, MAX8998_EN32KHZ_CP), 535 MAX8998_OTHERS_REG(ENVICHG, MAX8998_ENVICHG), 536 MAX8998_OTHERS_REG(ESAFEOUT1, MAX8998_ESAFEOUT1), 537 MAX8998_OTHERS_REG(ESAFEOUT2, MAX8998_ESAFEOUT2), 538 MAX8998_CURRENT_REG(CHARGER, &max8998_charger_ops, 539 charger_current_table, MAX8998_REG_CHGR1, 0x7), 540 }; 541 542 static int max8998_pmic_dt_parse_dvs_gpio(struct max8998_dev *iodev, 543 struct max8998_platform_data *pdata, 544 struct device_node *pmic_np) 545 { 546 int gpio; 547 548 gpio = of_get_named_gpio(pmic_np, "max8998,pmic-buck1-dvs-gpios", 0); 549 if (!gpio_is_valid(gpio)) { 550 dev_err(iodev->dev, "invalid buck1 gpio[0]: %d\n", gpio); 551 return -EINVAL; 552 } 553 pdata->buck1_set1 = gpio; 554 555 gpio = of_get_named_gpio(pmic_np, "max8998,pmic-buck1-dvs-gpios", 1); 556 if (!gpio_is_valid(gpio)) { 557 dev_err(iodev->dev, "invalid buck1 gpio[1]: %d\n", gpio); 558 return -EINVAL; 559 } 560 pdata->buck1_set2 = gpio; 561 562 gpio = of_get_named_gpio(pmic_np, "max8998,pmic-buck2-dvs-gpio", 0); 563 if (!gpio_is_valid(gpio)) { 564 dev_err(iodev->dev, "invalid buck 2 gpio: %d\n", gpio); 565 return -EINVAL; 566 } 567 pdata->buck2_set3 = gpio; 568 569 return 0; 570 } 571 572 static int max8998_pmic_dt_parse_pdata(struct max8998_dev *iodev, 573 struct max8998_platform_data *pdata) 574 { 575 struct device_node *pmic_np = iodev->dev->of_node; 576 struct device_node *regulators_np, *reg_np; 577 struct max8998_regulator_data *rdata; 578 unsigned int i; 579 int ret; 580 581 regulators_np = of_get_child_by_name(pmic_np, "regulators"); 582 if (!regulators_np) { 583 dev_err(iodev->dev, "could not find regulators sub-node\n"); 584 return -EINVAL; 585 } 586 587 /* count the number of regulators to be supported in pmic */ 588 pdata->num_regulators = of_get_child_count(regulators_np); 589 590 rdata = devm_kcalloc(iodev->dev, 591 pdata->num_regulators, sizeof(*rdata), 592 GFP_KERNEL); 593 if (!rdata) { 594 of_node_put(regulators_np); 595 return -ENOMEM; 596 } 597 598 pdata->regulators = rdata; 599 for (i = 0; i < ARRAY_SIZE(regulators); ++i) { 600 reg_np = of_get_child_by_name(regulators_np, 601 regulators[i].name); 602 if (!reg_np) 603 continue; 604 605 rdata->id = regulators[i].id; 606 rdata->initdata = of_get_regulator_init_data(iodev->dev, 607 reg_np, 608 ®ulators[i]); 609 rdata->reg_node = reg_np; 610 ++rdata; 611 } 612 pdata->num_regulators = rdata - pdata->regulators; 613 614 of_node_put(reg_np); 615 of_node_put(regulators_np); 616 617 ret = max8998_pmic_dt_parse_dvs_gpio(iodev, pdata, pmic_np); 618 if (ret) 619 return -EINVAL; 620 621 if (of_find_property(pmic_np, "max8998,pmic-buck-voltage-lock", NULL)) 622 pdata->buck_voltage_lock = true; 623 624 ret = of_property_read_u32(pmic_np, 625 "max8998,pmic-buck1-default-dvs-idx", 626 &pdata->buck1_default_idx); 627 if (!ret && pdata->buck1_default_idx >= 4) { 628 pdata->buck1_default_idx = 0; 629 dev_warn(iodev->dev, "invalid value for default dvs index, using 0 instead\n"); 630 } 631 632 ret = of_property_read_u32(pmic_np, 633 "max8998,pmic-buck2-default-dvs-idx", 634 &pdata->buck2_default_idx); 635 if (!ret && pdata->buck2_default_idx >= 2) { 636 pdata->buck2_default_idx = 0; 637 dev_warn(iodev->dev, "invalid value for default dvs index, using 0 instead\n"); 638 } 639 640 ret = of_property_read_u32_array(pmic_np, 641 "max8998,pmic-buck1-dvs-voltage", 642 pdata->buck1_voltage, 643 ARRAY_SIZE(pdata->buck1_voltage)); 644 if (ret) { 645 dev_err(iodev->dev, "buck1 voltages not specified\n"); 646 return -EINVAL; 647 } 648 649 ret = of_property_read_u32_array(pmic_np, 650 "max8998,pmic-buck2-dvs-voltage", 651 pdata->buck2_voltage, 652 ARRAY_SIZE(pdata->buck2_voltage)); 653 if (ret) { 654 dev_err(iodev->dev, "buck2 voltages not specified\n"); 655 return -EINVAL; 656 } 657 658 return 0; 659 } 660 661 static int max8998_pmic_probe(struct platform_device *pdev) 662 { 663 struct max8998_dev *iodev = dev_get_drvdata(pdev->dev.parent); 664 struct max8998_platform_data *pdata = iodev->pdata; 665 struct regulator_config config = { }; 666 struct regulator_dev *rdev; 667 struct max8998_data *max8998; 668 struct i2c_client *i2c; 669 int i, ret; 670 unsigned int v; 671 672 if (!pdata) { 673 dev_err(pdev->dev.parent, "No platform init data supplied\n"); 674 return -ENODEV; 675 } 676 677 if (IS_ENABLED(CONFIG_OF) && iodev->dev->of_node) { 678 ret = max8998_pmic_dt_parse_pdata(iodev, pdata); 679 if (ret) 680 return ret; 681 } 682 683 max8998 = devm_kzalloc(&pdev->dev, sizeof(struct max8998_data), 684 GFP_KERNEL); 685 if (!max8998) 686 return -ENOMEM; 687 688 max8998->dev = &pdev->dev; 689 max8998->iodev = iodev; 690 max8998->num_regulators = pdata->num_regulators; 691 platform_set_drvdata(pdev, max8998); 692 i2c = max8998->iodev->i2c; 693 694 max8998->buck1_idx = pdata->buck1_default_idx; 695 max8998->buck2_idx = pdata->buck2_default_idx; 696 697 /* NOTE: */ 698 /* For unused GPIO NOT marked as -1 (thereof equal to 0) WARN_ON */ 699 /* will be displayed */ 700 701 /* Check if MAX8998 voltage selection GPIOs are defined */ 702 if (gpio_is_valid(pdata->buck1_set1) && 703 gpio_is_valid(pdata->buck1_set2)) { 704 /* Check if SET1 is not equal to 0 */ 705 if (!pdata->buck1_set1) { 706 dev_err(&pdev->dev, 707 "MAX8998 SET1 GPIO defined as 0 !\n"); 708 WARN_ON(!pdata->buck1_set1); 709 return -EIO; 710 } 711 /* Check if SET2 is not equal to 0 */ 712 if (!pdata->buck1_set2) { 713 dev_err(&pdev->dev, 714 "MAX8998 SET2 GPIO defined as 0 !\n"); 715 WARN_ON(!pdata->buck1_set2); 716 return -EIO; 717 } 718 719 gpio_request(pdata->buck1_set1, "MAX8998 BUCK1_SET1"); 720 gpio_direction_output(pdata->buck1_set1, 721 max8998->buck1_idx & 0x1); 722 723 724 gpio_request(pdata->buck1_set2, "MAX8998 BUCK1_SET2"); 725 gpio_direction_output(pdata->buck1_set2, 726 (max8998->buck1_idx >> 1) & 0x1); 727 728 /* Set predefined values for BUCK1 registers */ 729 for (v = 0; v < ARRAY_SIZE(pdata->buck1_voltage); ++v) { 730 int index = MAX8998_BUCK1 - MAX8998_LDO2; 731 732 i = 0; 733 while (regulators[index].min_uV + 734 regulators[index].uV_step * i 735 < pdata->buck1_voltage[v]) 736 i++; 737 738 max8998->buck1_vol[v] = i; 739 ret = max8998_write_reg(i2c, 740 MAX8998_REG_BUCK1_VOLTAGE1 + v, i); 741 if (ret) 742 return ret; 743 } 744 } 745 746 if (gpio_is_valid(pdata->buck2_set3)) { 747 /* Check if SET3 is not equal to 0 */ 748 if (!pdata->buck2_set3) { 749 dev_err(&pdev->dev, 750 "MAX8998 SET3 GPIO defined as 0 !\n"); 751 WARN_ON(!pdata->buck2_set3); 752 return -EIO; 753 } 754 gpio_request(pdata->buck2_set3, "MAX8998 BUCK2_SET3"); 755 gpio_direction_output(pdata->buck2_set3, 756 max8998->buck2_idx & 0x1); 757 758 /* Set predefined values for BUCK2 registers */ 759 for (v = 0; v < ARRAY_SIZE(pdata->buck2_voltage); ++v) { 760 int index = MAX8998_BUCK2 - MAX8998_LDO2; 761 762 i = 0; 763 while (regulators[index].min_uV + 764 regulators[index].uV_step * i 765 < pdata->buck2_voltage[v]) 766 i++; 767 768 max8998->buck2_vol[v] = i; 769 ret = max8998_write_reg(i2c, 770 MAX8998_REG_BUCK2_VOLTAGE1 + v, i); 771 if (ret) 772 return ret; 773 } 774 } 775 776 for (i = 0; i < pdata->num_regulators; i++) { 777 int index = pdata->regulators[i].id - MAX8998_LDO2; 778 779 config.dev = max8998->dev; 780 config.of_node = pdata->regulators[i].reg_node; 781 config.init_data = pdata->regulators[i].initdata; 782 config.driver_data = max8998; 783 784 rdev = devm_regulator_register(&pdev->dev, ®ulators[index], 785 &config); 786 if (IS_ERR(rdev)) { 787 ret = PTR_ERR(rdev); 788 dev_err(max8998->dev, "regulator %s init failed (%d)\n", 789 regulators[index].name, ret); 790 return ret; 791 } 792 } 793 794 return 0; 795 } 796 797 static const struct platform_device_id max8998_pmic_id[] = { 798 { "max8998-pmic", TYPE_MAX8998 }, 799 { "lp3974-pmic", TYPE_LP3974 }, 800 { } 801 }; 802 MODULE_DEVICE_TABLE(platform, max8998_pmic_id); 803 804 static struct platform_driver max8998_pmic_driver = { 805 .driver = { 806 .name = "max8998-pmic", 807 }, 808 .probe = max8998_pmic_probe, 809 .id_table = max8998_pmic_id, 810 }; 811 812 static int __init max8998_pmic_init(void) 813 { 814 return platform_driver_register(&max8998_pmic_driver); 815 } 816 subsys_initcall(max8998_pmic_init); 817 818 static void __exit max8998_pmic_cleanup(void) 819 { 820 platform_driver_unregister(&max8998_pmic_driver); 821 } 822 module_exit(max8998_pmic_cleanup); 823 824 MODULE_DESCRIPTION("MAXIM 8998 voltage regulator driver"); 825 MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>"); 826 MODULE_LICENSE("GPL"); 827