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

regulator: Use bitfield values for range selectors

Right now the regulator helpers expect raw register values for the range
selectors. This is different from the voltage selectors, which are
normalized as bitfield values. This leads to a bit of confusion. Also,
raw values are harder to copy from datasheets or match up with them,
as datasheets will typically have bitfield values.

Make the helpers expect bitfield values, and convert existing users. The
field in regulator_desc is renamed to |linear_range_selectors_bitfield|.
This is intended to cause drivers added in the same merge window and
out-of-tree drivers using the incorrect variable and values to break,
preventing incorrect values being used on actual hardware and potentially
producing magic smoke.

Also include bitops.h explicitly for ffs(), and reorder the header include
statements. While at it, also replace module.h with export.h, since the
only use is EXPORT_SYMBOL_GPL.

Signed-off-by: Chen-Yu Tsai <wenst@chromium.org>
Link: https://lore.kernel.org/r/20230714081408.274567-1-wenst@chromium.org
Signed-off-by: Mark Brown <broonie@kernel.org>

authored by

Chen-Yu Tsai and committed by
Mark Brown
269cb04b 55c8b8dd

+35 -31
+2 -2
drivers/regulator/atc260x-regulator.c
··· 38 38 }; 39 39 40 40 static const unsigned int atc260x_ldo_voltage_range_sel[] = { 41 - 0x0, 0x20, 41 + 0x0, 0x1, 42 42 }; 43 43 44 44 static int atc260x_dcdc_set_voltage_time_sel(struct regulator_dev *rdev, ··· 428 428 .vsel_mask = GENMASK(4, 1), \ 429 429 .vsel_range_reg = ATC2609A_PMU_LDO##num##_CTL0, \ 430 430 .vsel_range_mask = BIT(5), \ 431 - .linear_range_selectors = atc260x_ldo_voltage_range_sel, \ 431 + .linear_range_selectors_bitfield = atc260x_ldo_voltage_range_sel, \ 432 432 .enable_reg = ATC2609A_PMU_LDO##num##_CTL0, \ 433 433 .enable_mask = BIT(0), \ 434 434 .enable_time = 2000, \
+11 -11
drivers/regulator/bd718x7-regulator.c
··· 289 289 * and 0x1 for last 3 ranges. 290 290 */ 291 291 static const unsigned int bd71837_buck5_volt_range_sel[] = { 292 - 0x0, 0x0, 0x0, 0x80, 0x80, 0x80 292 + 0x0, 0x0, 0x0, 0x1, 0x1, 0x1 293 293 }; 294 294 295 295 /* ··· 309 309 }; 310 310 311 311 static const unsigned int bd71847_buck3_volt_range_sel[] = { 312 - 0x0, 0x0, 0x0, 0x40, 0x80, 0x80, 0x80 312 + 0x0, 0x0, 0x0, 0x1, 0x2, 0x2, 0x2 313 313 }; 314 314 315 315 static const struct linear_range bd71847_buck4_volts[] = { ··· 317 317 REGULATOR_LINEAR_RANGE(2600000, 0x00, 0x03, 100000), 318 318 }; 319 319 320 - static const unsigned int bd71847_buck4_volt_range_sel[] = { 0x0, 0x40 }; 320 + static const unsigned int bd71847_buck4_volt_range_sel[] = { 0x0, 0x1 }; 321 321 322 322 /* 323 323 * BUCK6 ··· 360 360 REGULATOR_LINEAR_RANGE(1600000, 0x00, 0x03, 100000), 361 361 }; 362 362 363 - static const unsigned int bd718xx_ldo1_volt_range_sel[] = { 0x0, 0x20 }; 363 + static const unsigned int bd718xx_ldo1_volt_range_sel[] = { 0x0, 0x1 }; 364 364 365 365 /* 366 366 * LDO2 ··· 403 403 REGULATOR_LINEAR_RANGE(800000, 0x00, 0x0F, 100000), 404 404 }; 405 405 406 - static const unsigned int bd71847_ldo5_volt_range_sel[] = { 0x0, 0x20 }; 406 + static const unsigned int bd71847_ldo5_volt_range_sel[] = { 0x0, 0x1 }; 407 407 408 408 /* 409 409 * LDO6 ··· 817 817 .vsel_mask = BD718XX_1ST_NODVS_BUCK_MASK, 818 818 .vsel_range_reg = BD718XX_REG_1ST_NODVS_BUCK_VOLT, 819 819 .vsel_range_mask = BD71847_BUCK3_RANGE_MASK, 820 - .linear_range_selectors = bd71847_buck3_volt_range_sel, 820 + .linear_range_selectors_bitfield = bd71847_buck3_volt_range_sel, 821 821 .enable_reg = BD718XX_REG_1ST_NODVS_BUCK_CTRL, 822 822 .enable_mask = BD718XX_BUCK_EN, 823 823 .enable_time = BD71847_BUCK3_STARTUP_TIME, ··· 845 845 .vsel_mask = BD71847_BUCK4_MASK, 846 846 .vsel_range_reg = BD718XX_REG_2ND_NODVS_BUCK_VOLT, 847 847 .vsel_range_mask = BD71847_BUCK4_RANGE_MASK, 848 - .linear_range_selectors = bd71847_buck4_volt_range_sel, 848 + .linear_range_selectors_bitfield = bd71847_buck4_volt_range_sel, 849 849 .enable_mask = BD718XX_BUCK_EN, 850 850 .enable_time = BD71847_BUCK4_STARTUP_TIME, 851 851 .owner = THIS_MODULE, ··· 916 916 .vsel_mask = BD718XX_LDO1_MASK, 917 917 .vsel_range_reg = BD718XX_REG_LDO1_VOLT, 918 918 .vsel_range_mask = BD718XX_LDO1_RANGE_MASK, 919 - .linear_range_selectors = bd718xx_ldo1_volt_range_sel, 919 + .linear_range_selectors_bitfield = bd718xx_ldo1_volt_range_sel, 920 920 .enable_reg = BD718XX_REG_LDO1_VOLT, 921 921 .enable_mask = BD718XX_LDO_EN, 922 922 .enable_time = BD71847_LDO1_STARTUP_TIME, ··· 1010 1010 .vsel_mask = BD71847_LDO5_MASK, 1011 1011 .vsel_range_reg = BD718XX_REG_LDO5_VOLT, 1012 1012 .vsel_range_mask = BD71847_LDO5_RANGE_MASK, 1013 - .linear_range_selectors = bd71847_ldo5_volt_range_sel, 1013 + .linear_range_selectors_bitfield = bd71847_ldo5_volt_range_sel, 1014 1014 .enable_reg = BD718XX_REG_LDO5_VOLT, 1015 1015 .enable_mask = BD718XX_LDO_EN, 1016 1016 .enable_time = BD71847_LDO5_STARTUP_TIME, ··· 1232 1232 .vsel_mask = BD71837_BUCK5_MASK, 1233 1233 .vsel_range_reg = BD718XX_REG_1ST_NODVS_BUCK_VOLT, 1234 1234 .vsel_range_mask = BD71837_BUCK5_RANGE_MASK, 1235 - .linear_range_selectors = bd71837_buck5_volt_range_sel, 1235 + .linear_range_selectors_bitfield = bd71837_buck5_volt_range_sel, 1236 1236 .enable_reg = BD718XX_REG_1ST_NODVS_BUCK_CTRL, 1237 1237 .enable_mask = BD718XX_BUCK_EN, 1238 1238 .enable_time = BD71837_BUCK5_STARTUP_TIME, ··· 1328 1328 .vsel_mask = BD718XX_LDO1_MASK, 1329 1329 .vsel_range_reg = BD718XX_REG_LDO1_VOLT, 1330 1330 .vsel_range_mask = BD718XX_LDO1_RANGE_MASK, 1331 - .linear_range_selectors = bd718xx_ldo1_volt_range_sel, 1331 + .linear_range_selectors_bitfield = bd718xx_ldo1_volt_range_sel, 1332 1332 .enable_reg = BD718XX_REG_LDO1_VOLT, 1333 1333 .enable_mask = BD718XX_LDO_EN, 1334 1334 .enable_time = BD71837_LDO1_STARTUP_TIME,
+9 -6
drivers/regulator/helpers.c
··· 5 5 // Copyright 2007, 2008 Wolfson Microelectronics PLC. 6 6 // Copyright 2008 SlimLogic Ltd. 7 7 8 - #include <linux/kernel.h> 9 - #include <linux/err.h> 8 + #include <linux/bitops.h> 10 9 #include <linux/delay.h> 10 + #include <linux/err.h> 11 + #include <linux/export.h> 12 + #include <linux/kernel.h> 11 13 #include <linux/regmap.h> 12 14 #include <linux/regulator/consumer.h> 13 15 #include <linux/regulator/driver.h> 14 - #include <linux/module.h> 15 16 16 17 #include "internal.h" 17 18 ··· 105 104 { 106 105 int i; 107 106 108 - if (!rdev->desc->linear_range_selectors) 107 + if (!rdev->desc->linear_range_selectors_bitfield) 109 108 return -EINVAL; 110 109 111 110 rval &= rdev->desc->vsel_range_mask; 111 + rval >>= ffs(rdev->desc->vsel_range_mask) - 1; 112 112 113 113 for (i = 0; i < rdev->desc->n_linear_ranges; i++) { 114 - if (rdev->desc->linear_range_selectors[i] == rval) 114 + if (rdev->desc->linear_range_selectors_bitfield[i] == rval) 115 115 return i; 116 116 } 117 117 return -EINVAL; ··· 196 194 sel <<= ffs(rdev->desc->vsel_mask) - 1; 197 195 sel += rdev->desc->linear_ranges[i].min_sel; 198 196 199 - range = rdev->desc->linear_range_selectors[i]; 197 + range = rdev->desc->linear_range_selectors_bitfield[i]; 198 + range <<= ffs(rdev->desc->vsel_mask) - 1; 200 199 201 200 if (rdev->desc->vsel_reg == rdev->desc->vsel_range_reg) { 202 201 ret = regmap_update_bits(rdev->regmap,
+3 -3
drivers/regulator/max77541-regulator.c
··· 44 44 }; 45 45 46 46 static const unsigned int max77541_buck_volt_range_sel[] = { 47 - 0x00, 0x00, 0x40, 0x40, 0x80, 0x80, 47 + 0x0, 0x0, 0x1, 0x1, 0x2, 0x2, 48 48 }; 49 49 50 50 enum max77541_regulators { ··· 67 67 .vsel_mask = MAX77541_BITS_MX_VOUT, \ 68 68 .vsel_range_reg = MAX77541_REG_M ## _id ## _CFG1, \ 69 69 .vsel_range_mask = MAX77541_BITS_MX_CFG1_RNG, \ 70 - .linear_range_selectors = max77541_buck_volt_range_sel, \ 70 + .linear_range_selectors_bitfield = max77541_buck_volt_range_sel, \ 71 71 .owner = THIS_MODULE, \ 72 72 } 73 73 ··· 86 86 .vsel_mask = MAX77541_BITS_MX_VOUT, \ 87 87 .vsel_range_reg = MAX77541_REG_M ## _id ## _CFG1, \ 88 88 .vsel_range_mask = MAX77541_BITS_MX_CFG1_RNG, \ 89 - .linear_range_selectors = max77541_buck_volt_range_sel, \ 89 + .linear_range_selectors_bitfield = max77541_buck_volt_range_sel, \ 90 90 .owner = THIS_MODULE, \ 91 91 } 92 92
+1 -1
drivers/regulator/max77650-regulator.c
··· 239 239 .supply_name = "in-sbb1", 240 240 .id = MAX77650_REGULATOR_ID_SBB1, 241 241 .ops = &max77651_SBB1_regulator_ops, 242 - .linear_range_selectors = max77651_sbb1_volt_range_sel, 242 + .linear_range_selectors_bitfield = max77651_sbb1_volt_range_sel, 243 243 .linear_ranges = max77651_sbb1_volt_ranges, 244 244 .n_linear_ranges = ARRAY_SIZE(max77651_sbb1_volt_ranges), 245 245 .n_voltages = 58,
+1 -1
drivers/regulator/rohm-regulator.c
··· 36 36 } 37 37 for (i = 0; i < desc->n_voltages; i++) { 38 38 /* NOTE to next hacker - Does not support pickable ranges */ 39 - if (desc->linear_range_selectors) 39 + if (desc->linear_range_selectors_bitfield) 40 40 return -EINVAL; 41 41 if (desc->n_linear_ranges) 42 42 ret = regulator_desc_list_voltage_linear_range(desc, i);
+2 -2
drivers/regulator/tps6287x-regulator.c
··· 41 41 }; 42 42 43 43 static const unsigned int tps6287x_voltage_range_sel[] = { 44 - 0x0, 0x4, 0x8, 0xC 44 + 0x0, 0x1, 0x2, 0x3 45 45 }; 46 46 47 47 static const unsigned int tps6287x_ramp_table[] = { ··· 122 122 .n_voltages = 256, 123 123 .linear_ranges = tps6287x_voltage_ranges, 124 124 .n_linear_ranges = ARRAY_SIZE(tps6287x_voltage_ranges), 125 - .linear_range_selectors = tps6287x_voltage_range_sel, 125 + .linear_range_selectors_bitfield = tps6287x_voltage_range_sel, 126 126 }; 127 127 128 128 static int tps6287x_i2c_probe(struct i2c_client *i2c)
+6 -5
include/linux/regulator/driver.h
··· 292 292 * @ramp_delay: Time to settle down after voltage change (unit: uV/us) 293 293 * @min_dropout_uV: The minimum dropout voltage this regulator can handle 294 294 * @linear_ranges: A constant table of possible voltage ranges. 295 - * @linear_range_selectors: A constant table of voltage range selectors. 296 - * If pickable ranges are used each range must 297 - * have corresponding selector here. 295 + * @linear_range_selectors_bitfield: A constant table of voltage range 296 + * selectors as bitfield values. If 297 + * pickable ranges are used each range 298 + * must have corresponding selector here. 298 299 * @n_linear_ranges: Number of entries in the @linear_ranges (and in 299 - * linear_range_selectors if used) table(s). 300 + * linear_range_selectors_bitfield if used) table(s). 300 301 * @volt_table: Voltage mapping table (if table based mapping) 301 302 * @curr_table: Current limit mapping table (if table based mapping) 302 303 * ··· 385 384 int min_dropout_uV; 386 385 387 386 const struct linear_range *linear_ranges; 388 - const unsigned int *linear_range_selectors; 387 + const unsigned int *linear_range_selectors_bitfield; 389 388 390 389 int n_linear_ranges; 391 390