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

regulator: qcom_spmi: Constify struct spmi_voltage_range

'struct spmi_voltage_range' are only modified at runtime to compile a
field, n_voltages, that could be computed at compile time.

So, simplify spmi_calculate_num_voltages() and compute n_voltages at
compile time within the SPMI_VOLTAGE_RANGE macro.

Constifying these structures moves some data to a read-only section, so
increase overall security.

On a x86_64, with allmodconfig:
Before:
======
text data bss dec hex filename
85437 26776 512 112725 1b855 drivers/regulator/qcom_spmi-regulator.o

After:
=====
text data bss dec hex filename
86857 24760 512 112129 1b601 drivers/regulator/qcom_spmi-regulator.o

Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
Reviewed-by: Dmitry Baryshkov <dmitry.baryshkov@oss.qualcomm.com>
Link: https://patch.msgid.link/ef2a4b6df61e19470ddf6cbd1f3ca1ce88a3c1a0.1747570556.git.christophe.jaillet@wanadoo.fr
Signed-off-by: Mark Brown <broonie@kernel.org>

authored by

Christophe JAILLET and committed by
Mark Brown
e9be7713 9cfdd775

+31 -36
+31 -36
drivers/regulator/qcom_spmi-regulator.c
··· 400 400 * so that range[i].set_point_max_uV < range[i+1].set_point_min_uV. 401 401 */ 402 402 struct spmi_voltage_set_points { 403 - struct spmi_voltage_range *range; 403 + const struct spmi_voltage_range *range; 404 404 int count; 405 405 unsigned n_voltages; 406 406 }; ··· 474 474 .set_point_max_uV = _set_point_max_uV, \ 475 475 .step_uV = _step_uV, \ 476 476 .range_sel = _range_sel, \ 477 + .n_voltages = (_set_point_max_uV != 0) ? \ 478 + ((_set_point_max_uV - _set_point_min_uV) / _step_uV) + 1 : \ 479 + 0, \ 477 480 } 478 481 479 482 #define DEFINE_SPMI_SET_POINTS(name) \ ··· 492 489 * increasing and unique. The set_voltage callback functions expect these 493 490 * properties to hold. 494 491 */ 495 - static struct spmi_voltage_range pldo_ranges[] = { 492 + static const struct spmi_voltage_range pldo_ranges[] = { 496 493 SPMI_VOLTAGE_RANGE(2, 750000, 750000, 1537500, 1537500, 12500), 497 494 SPMI_VOLTAGE_RANGE(3, 1500000, 1550000, 3075000, 3075000, 25000), 498 495 SPMI_VOLTAGE_RANGE(4, 1750000, 3100000, 4900000, 4900000, 50000), 499 496 }; 500 497 501 - static struct spmi_voltage_range nldo1_ranges[] = { 498 + static const struct spmi_voltage_range nldo1_ranges[] = { 502 499 SPMI_VOLTAGE_RANGE(2, 750000, 750000, 1537500, 1537500, 12500), 503 500 }; 504 501 505 - static struct spmi_voltage_range nldo2_ranges[] = { 502 + static const struct spmi_voltage_range nldo2_ranges[] = { 506 503 SPMI_VOLTAGE_RANGE(0, 375000, 0, 0, 1537500, 12500), 507 504 SPMI_VOLTAGE_RANGE(1, 375000, 375000, 768750, 768750, 6250), 508 505 SPMI_VOLTAGE_RANGE(2, 750000, 775000, 1537500, 1537500, 12500), 509 506 }; 510 507 511 - static struct spmi_voltage_range nldo3_ranges[] = { 508 + static const struct spmi_voltage_range nldo3_ranges[] = { 512 509 SPMI_VOLTAGE_RANGE(0, 375000, 375000, 1537500, 1537500, 12500), 513 510 SPMI_VOLTAGE_RANGE(1, 375000, 0, 0, 1537500, 12500), 514 511 SPMI_VOLTAGE_RANGE(2, 750000, 0, 0, 1537500, 12500), 515 512 }; 516 513 517 - static struct spmi_voltage_range ln_ldo_ranges[] = { 514 + static const struct spmi_voltage_range ln_ldo_ranges[] = { 518 515 SPMI_VOLTAGE_RANGE(1, 690000, 690000, 1110000, 1110000, 60000), 519 516 SPMI_VOLTAGE_RANGE(0, 1380000, 1380000, 2220000, 2220000, 120000), 520 517 }; 521 518 522 - static struct spmi_voltage_range smps_ranges[] = { 519 + static const struct spmi_voltage_range smps_ranges[] = { 523 520 SPMI_VOLTAGE_RANGE(0, 375000, 375000, 1562500, 1562500, 12500), 524 521 SPMI_VOLTAGE_RANGE(1, 1550000, 1575000, 3125000, 3125000, 25000), 525 522 }; 526 523 527 - static struct spmi_voltage_range ftsmps_ranges[] = { 524 + static const struct spmi_voltage_range ftsmps_ranges[] = { 528 525 SPMI_VOLTAGE_RANGE(0, 0, 350000, 1275000, 1275000, 5000), 529 526 SPMI_VOLTAGE_RANGE(1, 0, 1280000, 2040000, 2040000, 10000), 530 527 }; 531 528 532 - static struct spmi_voltage_range ftsmps2p5_ranges[] = { 529 + static const struct spmi_voltage_range ftsmps2p5_ranges[] = { 533 530 SPMI_VOLTAGE_RANGE(0, 80000, 350000, 1355000, 1355000, 5000), 534 531 SPMI_VOLTAGE_RANGE(1, 160000, 1360000, 2200000, 2200000, 10000), 535 532 }; 536 533 537 - static struct spmi_voltage_range ftsmps426_ranges[] = { 534 + static const struct spmi_voltage_range ftsmps426_ranges[] = { 538 535 SPMI_VOLTAGE_RANGE(0, 0, 320000, 1352000, 1352000, 4000), 539 536 }; 540 537 541 - static struct spmi_voltage_range boost_ranges[] = { 538 + static const struct spmi_voltage_range boost_ranges[] = { 542 539 SPMI_VOLTAGE_RANGE(0, 4000000, 4000000, 5550000, 5550000, 50000), 543 540 }; 544 541 545 - static struct spmi_voltage_range boost_byp_ranges[] = { 542 + static const struct spmi_voltage_range boost_byp_ranges[] = { 546 543 SPMI_VOLTAGE_RANGE(0, 2500000, 2500000, 5200000, 5650000, 50000), 547 544 }; 548 545 549 - static struct spmi_voltage_range ult_lo_smps_ranges[] = { 546 + static const struct spmi_voltage_range ult_lo_smps_ranges[] = { 550 547 SPMI_VOLTAGE_RANGE(0, 375000, 375000, 1562500, 1562500, 12500), 551 548 SPMI_VOLTAGE_RANGE(1, 750000, 0, 0, 1525000, 25000), 552 549 }; 553 550 554 - static struct spmi_voltage_range ult_ho_smps_ranges[] = { 551 + static const struct spmi_voltage_range ult_ho_smps_ranges[] = { 555 552 SPMI_VOLTAGE_RANGE(0, 1550000, 1550000, 2325000, 2325000, 25000), 556 553 }; 557 554 558 - static struct spmi_voltage_range ult_nldo_ranges[] = { 555 + static const struct spmi_voltage_range ult_nldo_ranges[] = { 559 556 SPMI_VOLTAGE_RANGE(0, 375000, 375000, 1537500, 1537500, 12500), 560 557 }; 561 558 562 - static struct spmi_voltage_range ult_pldo_ranges[] = { 559 + static const struct spmi_voltage_range ult_pldo_ranges[] = { 563 560 SPMI_VOLTAGE_RANGE(0, 1750000, 1750000, 3337500, 3337500, 12500), 564 561 }; 565 562 566 - static struct spmi_voltage_range pldo660_ranges[] = { 563 + static const struct spmi_voltage_range pldo660_ranges[] = { 567 564 SPMI_VOLTAGE_RANGE(0, 1504000, 1504000, 3544000, 3544000, 8000), 568 565 }; 569 566 570 - static struct spmi_voltage_range nldo660_ranges[] = { 567 + static const struct spmi_voltage_range nldo660_ranges[] = { 571 568 SPMI_VOLTAGE_RANGE(0, 320000, 320000, 1304000, 1304000, 8000), 572 569 }; 573 570 574 - static struct spmi_voltage_range ht_lvpldo_ranges[] = { 571 + static const struct spmi_voltage_range ht_lvpldo_ranges[] = { 575 572 SPMI_VOLTAGE_RANGE(0, 1504000, 1504000, 2000000, 2000000, 8000), 576 573 }; 577 574 578 - static struct spmi_voltage_range ht_nldo_ranges[] = { 575 + static const struct spmi_voltage_range ht_nldo_ranges[] = { 579 576 SPMI_VOLTAGE_RANGE(0, 312000, 312000, 1304000, 1304000, 8000), 580 577 }; 581 578 582 - static struct spmi_voltage_range hfs430_ranges[] = { 579 + static const struct spmi_voltage_range hfs430_ranges[] = { 583 580 SPMI_VOLTAGE_RANGE(0, 320000, 320000, 2040000, 2040000, 8000), 584 581 }; 585 582 586 - static struct spmi_voltage_range ht_p150_ranges[] = { 583 + static const struct spmi_voltage_range ht_p150_ranges[] = { 587 584 SPMI_VOLTAGE_RANGE(0, 1616000, 1616000, 3304000, 3304000, 8000), 588 585 }; 589 586 590 - static struct spmi_voltage_range ht_p600_ranges[] = { 587 + static const struct spmi_voltage_range ht_p600_ranges[] = { 591 588 SPMI_VOLTAGE_RANGE(0, 1704000, 1704000, 1896000, 1896000, 8000), 592 589 }; 593 590 594 - static struct spmi_voltage_range nldo_510_ranges[] = { 591 + static const struct spmi_voltage_range nldo_510_ranges[] = { 595 592 SPMI_VOLTAGE_RANGE(0, 320000, 320000, 1304000, 1304000, 8000), 596 593 }; 597 594 598 - static struct spmi_voltage_range ftsmps510_ranges[] = { 595 + static const struct spmi_voltage_range ftsmps510_ranges[] = { 599 596 SPMI_VOLTAGE_RANGE(0, 300000, 300000, 1372000, 1372000, 4000), 600 597 }; 601 598 ··· 1679 1676 1680 1677 static void spmi_calculate_num_voltages(struct spmi_voltage_set_points *points) 1681 1678 { 1682 - unsigned int n; 1683 - struct spmi_voltage_range *range = points->range; 1679 + const struct spmi_voltage_range *range = points->range; 1684 1680 1685 - for (; range < points->range + points->count; range++) { 1686 - n = 0; 1687 - if (range->set_point_max_uV) { 1688 - n = range->set_point_max_uV - range->set_point_min_uV; 1689 - n = (n / range->step_uV) + 1; 1690 - } 1691 - range->n_voltages = n; 1692 - points->n_voltages += n; 1693 - } 1681 + for (; range < points->range + points->count; range++) 1682 + points->n_voltages += range->n_voltages; 1694 1683 } 1695 1684 1696 1685 static int spmi_regulator_match(struct spmi_regulator *vreg, u16 force_type)