Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

regulator: max8998: Factor out struct voltage_map_desc

The struct voltage_map_desc is used for storing min/max/step fileds, and
then setup the min_uV, uV_step and n_voltages fields for regulator_desc.
Introduce MAX8998_LINEAR_REG/MAX8998_OTHERS_REG macro to setup these filds
directly in regulator_desc to simplify the code.

Signed-off-by: Axel Lin <axel.lin@ingics.com>
Signed-off-by: Mark Brown <broonie@kernel.org>

authored by

Axel Lin and committed by
Mark Brown
0dceab33 7d695f20

+56 -238
+56 -238
drivers/regulator/max8998.c
··· 33 33 unsigned int buck2_idx; 34 34 }; 35 35 36 - struct voltage_map_desc { 37 - int min; 38 - int max; 39 - int step; 40 - }; 41 - 42 - /* Voltage maps in uV*/ 43 - static const struct voltage_map_desc ldo23_voltage_map_desc = { 44 - .min = 800000, .step = 50000, .max = 1300000, 45 - }; 46 - static const struct voltage_map_desc ldo456711_voltage_map_desc = { 47 - .min = 1600000, .step = 100000, .max = 3600000, 48 - }; 49 - static const struct voltage_map_desc ldo8_voltage_map_desc = { 50 - .min = 3000000, .step = 100000, .max = 3600000, 51 - }; 52 - static const struct voltage_map_desc ldo9_voltage_map_desc = { 53 - .min = 2800000, .step = 100000, .max = 3100000, 54 - }; 55 - static const struct voltage_map_desc ldo10_voltage_map_desc = { 56 - .min = 950000, .step = 50000, .max = 1300000, 57 - }; 58 - static const struct voltage_map_desc ldo1213_voltage_map_desc = { 59 - .min = 800000, .step = 100000, .max = 3300000, 60 - }; 61 - static const struct voltage_map_desc ldo1415_voltage_map_desc = { 62 - .min = 1200000, .step = 100000, .max = 3300000, 63 - }; 64 - static const struct voltage_map_desc ldo1617_voltage_map_desc = { 65 - .min = 1600000, .step = 100000, .max = 3600000, 66 - }; 67 - static const struct voltage_map_desc buck12_voltage_map_desc = { 68 - .min = 750000, .step = 25000, .max = 1525000, 69 - }; 70 - static const struct voltage_map_desc buck3_voltage_map_desc = { 71 - .min = 1600000, .step = 100000, .max = 3600000, 72 - }; 73 - static const struct voltage_map_desc buck4_voltage_map_desc = { 74 - .min = 800000, .step = 100000, .max = 2300000, 75 - }; 76 - 77 - static const struct voltage_map_desc *ldo_voltage_map[] = { 78 - NULL, 79 - NULL, 80 - &ldo23_voltage_map_desc, /* LDO2 */ 81 - &ldo23_voltage_map_desc, /* LDO3 */ 82 - &ldo456711_voltage_map_desc, /* LDO4 */ 83 - &ldo456711_voltage_map_desc, /* LDO5 */ 84 - &ldo456711_voltage_map_desc, /* LDO6 */ 85 - &ldo456711_voltage_map_desc, /* LDO7 */ 86 - &ldo8_voltage_map_desc, /* LDO8 */ 87 - &ldo9_voltage_map_desc, /* LDO9 */ 88 - &ldo10_voltage_map_desc, /* LDO10 */ 89 - &ldo456711_voltage_map_desc, /* LDO11 */ 90 - &ldo1213_voltage_map_desc, /* LDO12 */ 91 - &ldo1213_voltage_map_desc, /* LDO13 */ 92 - &ldo1415_voltage_map_desc, /* LDO14 */ 93 - &ldo1415_voltage_map_desc, /* LDO15 */ 94 - &ldo1617_voltage_map_desc, /* LDO16 */ 95 - &ldo1617_voltage_map_desc, /* LDO17 */ 96 - &buck12_voltage_map_desc, /* BUCK1 */ 97 - &buck12_voltage_map_desc, /* BUCK2 */ 98 - &buck3_voltage_map_desc, /* BUCK3 */ 99 - &buck4_voltage_map_desc, /* BUCK4 */ 100 - }; 101 - 102 36 static int max8998_get_enable_register(struct regulator_dev *rdev, 103 37 int *reg, int *shift) 104 38 { ··· 334 400 { 335 401 struct max8998_data *max8998 = rdev_get_drvdata(rdev); 336 402 struct i2c_client *i2c = max8998->iodev->i2c; 337 - const struct voltage_map_desc *desc; 338 403 int buck = rdev_get_id(rdev); 339 404 u8 val = 0; 340 405 int difference, ret; 341 406 342 407 if (buck < MAX8998_BUCK1 || buck > MAX8998_BUCK4) 343 408 return -EINVAL; 344 - 345 - desc = ldo_voltage_map[buck]; 346 409 347 410 /* Voltage stabilization */ 348 411 ret = max8998_read_reg(i2c, MAX8998_REG_ONOFF4, &val); ··· 351 420 if (max8998->iodev->type == TYPE_MAX8998 && !(val & MAX8998_ENRAMP)) 352 421 return 0; 353 422 354 - difference = (new_selector - old_selector) * desc->step / 1000; 423 + difference = (new_selector - old_selector) * rdev->desc->uV_step / 1000; 355 424 if (difference > 0) 356 425 return DIV_ROUND_UP(difference, (val & 0x0f) + 1); 357 426 ··· 385 454 .disable = max8998_ldo_disable, 386 455 }; 387 456 388 - static struct regulator_desc regulators[] = { 389 - { 390 - .name = "LDO2", 391 - .id = MAX8998_LDO2, 392 - .ops = &max8998_ldo_ops, 393 - .type = REGULATOR_VOLTAGE, 394 - .owner = THIS_MODULE, 395 - }, { 396 - .name = "LDO3", 397 - .id = MAX8998_LDO3, 398 - .ops = &max8998_ldo_ops, 399 - .type = REGULATOR_VOLTAGE, 400 - .owner = THIS_MODULE, 401 - }, { 402 - .name = "LDO4", 403 - .id = MAX8998_LDO4, 404 - .ops = &max8998_ldo_ops, 405 - .type = REGULATOR_VOLTAGE, 406 - .owner = THIS_MODULE, 407 - }, { 408 - .name = "LDO5", 409 - .id = MAX8998_LDO5, 410 - .ops = &max8998_ldo_ops, 411 - .type = REGULATOR_VOLTAGE, 412 - .owner = THIS_MODULE, 413 - }, { 414 - .name = "LDO6", 415 - .id = MAX8998_LDO6, 416 - .ops = &max8998_ldo_ops, 417 - .type = REGULATOR_VOLTAGE, 418 - .owner = THIS_MODULE, 419 - }, { 420 - .name = "LDO7", 421 - .id = MAX8998_LDO7, 422 - .ops = &max8998_ldo_ops, 423 - .type = REGULATOR_VOLTAGE, 424 - .owner = THIS_MODULE, 425 - }, { 426 - .name = "LDO8", 427 - .id = MAX8998_LDO8, 428 - .ops = &max8998_ldo_ops, 429 - .type = REGULATOR_VOLTAGE, 430 - .owner = THIS_MODULE, 431 - }, { 432 - .name = "LDO9", 433 - .id = MAX8998_LDO9, 434 - .ops = &max8998_ldo_ops, 435 - .type = REGULATOR_VOLTAGE, 436 - .owner = THIS_MODULE, 437 - }, { 438 - .name = "LDO10", 439 - .id = MAX8998_LDO10, 440 - .ops = &max8998_ldo_ops, 441 - .type = REGULATOR_VOLTAGE, 442 - .owner = THIS_MODULE, 443 - }, { 444 - .name = "LDO11", 445 - .id = MAX8998_LDO11, 446 - .ops = &max8998_ldo_ops, 447 - .type = REGULATOR_VOLTAGE, 448 - .owner = THIS_MODULE, 449 - }, { 450 - .name = "LDO12", 451 - .id = MAX8998_LDO12, 452 - .ops = &max8998_ldo_ops, 453 - .type = REGULATOR_VOLTAGE, 454 - .owner = THIS_MODULE, 455 - }, { 456 - .name = "LDO13", 457 - .id = MAX8998_LDO13, 458 - .ops = &max8998_ldo_ops, 459 - .type = REGULATOR_VOLTAGE, 460 - .owner = THIS_MODULE, 461 - }, { 462 - .name = "LDO14", 463 - .id = MAX8998_LDO14, 464 - .ops = &max8998_ldo_ops, 465 - .type = REGULATOR_VOLTAGE, 466 - .owner = THIS_MODULE, 467 - }, { 468 - .name = "LDO15", 469 - .id = MAX8998_LDO15, 470 - .ops = &max8998_ldo_ops, 471 - .type = REGULATOR_VOLTAGE, 472 - .owner = THIS_MODULE, 473 - }, { 474 - .name = "LDO16", 475 - .id = MAX8998_LDO16, 476 - .ops = &max8998_ldo_ops, 477 - .type = REGULATOR_VOLTAGE, 478 - .owner = THIS_MODULE, 479 - }, { 480 - .name = "LDO17", 481 - .id = MAX8998_LDO17, 482 - .ops = &max8998_ldo_ops, 483 - .type = REGULATOR_VOLTAGE, 484 - .owner = THIS_MODULE, 485 - }, { 486 - .name = "BUCK1", 487 - .id = MAX8998_BUCK1, 488 - .ops = &max8998_buck_ops, 489 - .type = REGULATOR_VOLTAGE, 490 - .owner = THIS_MODULE, 491 - }, { 492 - .name = "BUCK2", 493 - .id = MAX8998_BUCK2, 494 - .ops = &max8998_buck_ops, 495 - .type = REGULATOR_VOLTAGE, 496 - .owner = THIS_MODULE, 497 - }, { 498 - .name = "BUCK3", 499 - .id = MAX8998_BUCK3, 500 - .ops = &max8998_buck_ops, 501 - .type = REGULATOR_VOLTAGE, 502 - .owner = THIS_MODULE, 503 - }, { 504 - .name = "BUCK4", 505 - .id = MAX8998_BUCK4, 506 - .ops = &max8998_buck_ops, 507 - .type = REGULATOR_VOLTAGE, 508 - .owner = THIS_MODULE, 509 - }, { 510 - .name = "EN32KHz-AP", 511 - .id = MAX8998_EN32KHZ_AP, 512 - .ops = &max8998_others_ops, 513 - .type = REGULATOR_VOLTAGE, 514 - .owner = THIS_MODULE, 515 - }, { 516 - .name = "EN32KHz-CP", 517 - .id = MAX8998_EN32KHZ_CP, 518 - .ops = &max8998_others_ops, 519 - .type = REGULATOR_VOLTAGE, 520 - .owner = THIS_MODULE, 521 - }, { 522 - .name = "ENVICHG", 523 - .id = MAX8998_ENVICHG, 524 - .ops = &max8998_others_ops, 525 - .type = REGULATOR_VOLTAGE, 526 - .owner = THIS_MODULE, 527 - }, { 528 - .name = "ESAFEOUT1", 529 - .id = MAX8998_ESAFEOUT1, 530 - .ops = &max8998_others_ops, 531 - .type = REGULATOR_VOLTAGE, 532 - .owner = THIS_MODULE, 533 - }, { 534 - .name = "ESAFEOUT2", 535 - .id = MAX8998_ESAFEOUT2, 536 - .ops = &max8998_others_ops, 537 - .type = REGULATOR_VOLTAGE, 538 - .owner = THIS_MODULE, 457 + #define MAX8998_LINEAR_REG(_name, _ops, _min, _step, _max) \ 458 + { \ 459 + .name = #_name, \ 460 + .id = MAX8998_##_name, \ 461 + .ops = _ops, \ 462 + .min_uV = (_min), \ 463 + .uV_step = (_step), \ 464 + .n_voltages = ((_max) - (_min)) / (_step) + 1, \ 465 + .type = REGULATOR_VOLTAGE, \ 466 + .owner = THIS_MODULE, \ 539 467 } 468 + 469 + #define MAX8998_OTHERS_REG(_name, _id) \ 470 + { \ 471 + .name = #_name, \ 472 + .id = _id, \ 473 + .ops = &max8998_others_ops, \ 474 + .type = REGULATOR_VOLTAGE, \ 475 + .owner = THIS_MODULE, \ 476 + } 477 + 478 + static const struct regulator_desc regulators[] = { 479 + MAX8998_LINEAR_REG(LDO2, &max8998_ldo_ops, 800000, 50000, 1300000), 480 + MAX8998_LINEAR_REG(LDO3, &max8998_ldo_ops, 800000, 50000, 1300000), 481 + MAX8998_LINEAR_REG(LDO4, &max8998_ldo_ops, 1600000, 100000, 3600000), 482 + MAX8998_LINEAR_REG(LDO5, &max8998_ldo_ops, 1600000, 100000, 3600000), 483 + MAX8998_LINEAR_REG(LDO6, &max8998_ldo_ops, 1600000, 100000, 3600000), 484 + MAX8998_LINEAR_REG(LDO7, &max8998_ldo_ops, 1600000, 100000, 3600000), 485 + MAX8998_LINEAR_REG(LDO8, &max8998_ldo_ops, 3000000, 100000, 3600000), 486 + MAX8998_LINEAR_REG(LDO9, &max8998_ldo_ops, 2800000, 100000, 3100000), 487 + MAX8998_LINEAR_REG(LDO10, &max8998_ldo_ops, 950000, 50000, 1300000), 488 + MAX8998_LINEAR_REG(LDO11, &max8998_ldo_ops, 1600000, 100000, 3600000), 489 + MAX8998_LINEAR_REG(LDO12, &max8998_ldo_ops, 800000, 100000, 3300000), 490 + MAX8998_LINEAR_REG(LDO13, &max8998_ldo_ops, 800000, 100000, 3300000), 491 + MAX8998_LINEAR_REG(LDO14, &max8998_ldo_ops, 1200000, 100000, 3300000), 492 + MAX8998_LINEAR_REG(LDO15, &max8998_ldo_ops, 1200000, 100000, 3300000), 493 + MAX8998_LINEAR_REG(LDO16, &max8998_ldo_ops, 1600000, 100000, 3600000), 494 + MAX8998_LINEAR_REG(LDO17, &max8998_ldo_ops, 1600000, 100000, 3600000), 495 + MAX8998_LINEAR_REG(BUCK1, &max8998_buck_ops, 750000, 25000, 1525000), 496 + MAX8998_LINEAR_REG(BUCK2, &max8998_buck_ops, 750000, 25000, 1525000), 497 + MAX8998_LINEAR_REG(BUCK3, &max8998_buck_ops, 1600000, 100000, 3600000), 498 + MAX8998_LINEAR_REG(BUCK4, &max8998_buck_ops, 800000, 100000, 2300000), 499 + MAX8998_OTHERS_REG(EN32KHz-AP, MAX8998_EN32KHZ_AP), 500 + MAX8998_OTHERS_REG(EN32KHz-CP, MAX8998_EN32KHZ_CP), 501 + MAX8998_OTHERS_REG(ENVICHG, MAX8998_ENVICHG), 502 + MAX8998_OTHERS_REG(ESAFEOUT1, MAX8998_ESAFEOUT1), 503 + MAX8998_OTHERS_REG(ESAFEOUT2, MAX8998_ESAFEOUT2), 540 504 }; 541 505 542 506 static int max8998_pmic_dt_parse_dvs_gpio(struct max8998_dev *iodev, ··· 622 796 623 797 /* Set predefined values for BUCK1 registers */ 624 798 for (v = 0; v < ARRAY_SIZE(pdata->buck1_voltage); ++v) { 799 + int index = MAX8998_BUCK1 - MAX8998_LDO2; 800 + 625 801 i = 0; 626 - while (buck12_voltage_map_desc.min + 627 - buck12_voltage_map_desc.step*i 802 + while (regulators[index].min_uV + 803 + regulators[index].uV_step * i 628 804 < pdata->buck1_voltage[v]) 629 805 i++; 630 806 ··· 652 824 653 825 /* Set predefined values for BUCK2 registers */ 654 826 for (v = 0; v < ARRAY_SIZE(pdata->buck2_voltage); ++v) { 827 + int index = MAX8998_BUCK2 - MAX8998_LDO2; 828 + 655 829 i = 0; 656 - while (buck12_voltage_map_desc.min + 657 - buck12_voltage_map_desc.step*i 830 + while (regulators[index].min_uV + 831 + regulators[index].uV_step * i 658 832 < pdata->buck2_voltage[v]) 659 833 i++; 660 834 ··· 669 839 } 670 840 671 841 for (i = 0; i < pdata->num_regulators; i++) { 672 - const struct voltage_map_desc *desc; 673 - int id = pdata->regulators[i].id; 674 - int index = id - MAX8998_LDO2; 675 - 676 - desc = ldo_voltage_map[id]; 677 - if (desc && regulators[index].ops != &max8998_others_ops) { 678 - int count = (desc->max - desc->min) / desc->step + 1; 679 - 680 - regulators[index].n_voltages = count; 681 - regulators[index].min_uV = desc->min; 682 - regulators[index].uV_step = desc->step; 683 - } 842 + int index = pdata->regulators[i].id - MAX8998_LDO2; 684 843 685 844 config.dev = max8998->dev; 686 845 config.of_node = pdata->regulators[i].reg_node; ··· 685 866 return ret; 686 867 } 687 868 } 688 - 689 869 690 870 return 0; 691 871 }