Skip to content

Commit 9722c3b

Browse files
lucaceresolirobherring
authored andcommitted
of: remove internal arguments from of_property_for_each_u32()
The of_property_for_each_u32() macro needs five parameters, two of which are primarily meant as internal variables for the macro itself (in the for() clause). Yet these two parameters are used by a few drivers, and this can be considered misuse or at least bad practice. Now that the kernel uses C11 to build, these two parameters can be avoided by declaring them internally, thus changing this pattern: struct property *prop; const __be32 *p; u32 val; of_property_for_each_u32(np, "xyz", prop, p, val) { ... } to this: u32 val; of_property_for_each_u32(np, "xyz", val) { ... } However two variables cannot be declared in the for clause even with C11, so declare one struct that contain the two variables we actually need. As the variables inside this struct are not meant to be used by users of this macro, give the struct instance the noticeable name "_it" so it is visible during code reviews, helping to avoid new code to use it directly. Most usages are trivially converted as they do not use those two parameters, as expected. The non-trivial cases are: - drivers/clk/clk.c, of_clk_get_parent_name(): easily doable anyway - drivers/clk/clk-si5351.c, si5351_dt_parse(): this is more complex as the checks had to be replicated in a different way, making code more verbose and somewhat uglier, but I refrained from a full rework to keep as much of the original code untouched having no hardware to test my changes All the changes have been build tested. The few for which I have the hardware have been runtime-tested too. Reviewed-by: Andre Przywara <andre.przywara@arm.com> # drivers/clk/sunxi/clk-simple-gates.c, drivers/clk/sunxi/clk-sun8i-bus-gates.c Acked-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org> # drivers/gpio/gpio-brcmstb.c Acked-by: Nicolas Ferre <nicolas.ferre@microchip.com> # drivers/irqchip/irq-atmel-aic-common.c Acked-by: Jonathan Cameron <Jonathan.Cameron@huawei.com> # drivers/iio/adc/ti_am335x_adc.c Acked-by: Uwe Kleine-König <u.kleine-koenig@baylibre.com> # drivers/pwm/pwm-samsung.c Acked-by: Richard Leitner <richard.leitner@linux.dev> # drivers/usb/misc/usb251xb.c Acked-by: Mark Brown <broonie@kernel.org> # sound/soc/codecs/arizona.c Reviewed-by: Richard Fitzgerald <rf@opensource.cirrus.com> # sound/soc/codecs/arizona.c Acked-by: Michael Ellerman <mpe@ellerman.id.au> # arch/powerpc/sysdev/xive/spapr.c Acked-by: Stephen Boyd <sboyd@kernel.org> # clk Signed-off-by: Luca Ceresoli <luca.ceresoli@bootlin.com> Acked-by: Lee Jones <lee@kernel.org> Link: https://lore.kernel.org/r/20240724-of_property_for_each_u32-v3-1-bea82ce429e2@bootlin.com Signed-off-by: Rob Herring (Arm) <robh@kernel.org>
1 parent 7b52a9d commit 9722c3b

File tree

22 files changed

+61
-93
lines changed

22 files changed

+61
-93
lines changed

arch/powerpc/sysdev/xive/native.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -559,9 +559,7 @@ bool __init xive_native_init(void)
559559
struct device_node *np;
560560
struct resource r;
561561
void __iomem *tima;
562-
struct property *prop;
563562
u8 max_prio = 7;
564-
const __be32 *p;
565563
u32 val, cpu;
566564
s64 rc;
567565

@@ -592,7 +590,7 @@ bool __init xive_native_init(void)
592590
max_prio = val - 1;
593591

594592
/* Iterate the EQ sizes and pick one */
595-
of_property_for_each_u32(np, "ibm,xive-eq-sizes", prop, p, val) {
593+
of_property_for_each_u32(np, "ibm,xive-eq-sizes", val) {
596594
xive_queue_shift = val;
597595
if (val == PAGE_SHIFT)
598596
break;

arch/powerpc/sysdev/xive/spapr.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,6 @@ bool __init xive_spapr_init(void)
814814
struct device_node *np;
815815
struct resource r;
816816
void __iomem *tima;
817-
struct property *prop;
818817
u8 max_prio;
819818
u32 val;
820819
u32 len;
@@ -866,7 +865,7 @@ bool __init xive_spapr_init(void)
866865
}
867866

868867
/* Iterate the EQ sizes and pick one */
869-
of_property_for_each_u32(np, "ibm,xive-eq-sizes", prop, reg, val) {
868+
of_property_for_each_u32(np, "ibm,xive-eq-sizes", val) {
870869
xive_queue_shift = val;
871870
if (val == PAGE_SHIFT)
872871
break;

drivers/bus/ti-sysc.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2291,11 +2291,9 @@ static int sysc_init_idlemode(struct sysc *ddata, u8 *idlemodes,
22912291
const char *name)
22922292
{
22932293
struct device_node *np = ddata->dev->of_node;
2294-
struct property *prop;
2295-
const __be32 *p;
22962294
u32 val;
22972295

2298-
of_property_for_each_u32(np, name, prop, p, val) {
2296+
of_property_for_each_u32(np, name, val) {
22992297
if (val >= SYSC_NR_IDLEMODES) {
23002298
dev_err(ddata->dev, "invalid idlemode: %i\n", val);
23012299
return -EINVAL;

drivers/clk/clk-conf.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,11 @@ static int __set_clk_parents(struct device_node *node, bool clk_supplier)
8181
static int __set_clk_rates(struct device_node *node, bool clk_supplier)
8282
{
8383
struct of_phandle_args clkspec;
84-
struct property *prop;
85-
const __be32 *cur;
8684
int rc, index = 0;
8785
struct clk *clk;
8886
u32 rate;
8987

90-
of_property_for_each_u32(node, "assigned-clock-rates", prop, cur, rate) {
88+
of_property_for_each_u32(node, "assigned-clock-rates", rate) {
9189
if (rate) {
9290
rc = of_parse_phandle_with_args(node, "assigned-clocks",
9391
"#clock-cells", index, &clkspec);

drivers/clk/clk-si5351.c

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,8 +1175,8 @@ static int si5351_dt_parse(struct i2c_client *client,
11751175
{
11761176
struct device_node *child, *np = client->dev.of_node;
11771177
struct si5351_platform_data *pdata;
1178-
struct property *prop;
1179-
const __be32 *p;
1178+
u32 array[4];
1179+
int sz, i;
11801180
int num = 0;
11811181
u32 val;
11821182

@@ -1191,20 +1191,24 @@ static int si5351_dt_parse(struct i2c_client *client,
11911191
* property silabs,pll-source : <num src>, [<..>]
11921192
* allow to selectively set pll source
11931193
*/
1194-
of_property_for_each_u32(np, "silabs,pll-source", prop, p, num) {
1194+
sz = of_property_read_variable_u32_array(np, "silabs,pll-source", array, 2, 4);
1195+
sz = (sz == -EINVAL) ? 0 : sz; /* Missing property is OK */
1196+
if (sz < 0)
1197+
return dev_err_probe(&client->dev, sz, "invalid pll-source\n");
1198+
if (sz % 2)
1199+
return dev_err_probe(&client->dev, -EINVAL,
1200+
"missing pll-source for pll %d\n", array[sz - 1]);
1201+
1202+
for (i = 0; i < sz; i += 2) {
1203+
num = array[i];
1204+
val = array[i + 1];
1205+
11951206
if (num >= 2) {
11961207
dev_err(&client->dev,
11971208
"invalid pll %d on pll-source prop\n", num);
11981209
return -EINVAL;
11991210
}
12001211

1201-
p = of_prop_next_u32(prop, p, &val);
1202-
if (!p) {
1203-
dev_err(&client->dev,
1204-
"missing pll-source for pll %d\n", num);
1205-
return -EINVAL;
1206-
}
1207-
12081212
switch (val) {
12091213
case 0:
12101214
pdata->pll_src[num] = SI5351_PLL_SRC_XTAL;
@@ -1232,19 +1236,24 @@ static int si5351_dt_parse(struct i2c_client *client,
12321236
pdata->pll_reset[0] = true;
12331237
pdata->pll_reset[1] = true;
12341238

1235-
of_property_for_each_u32(np, "silabs,pll-reset-mode", prop, p, num) {
1239+
sz = of_property_read_variable_u32_array(np, "silabs,pll-reset-mode", array, 2, 4);
1240+
sz = (sz == -EINVAL) ? 0 : sz; /* Missing property is OK */
1241+
if (sz < 0)
1242+
return dev_err_probe(&client->dev, sz, "invalid pll-reset-mode\n");
1243+
if (sz % 2)
1244+
return dev_err_probe(&client->dev, -EINVAL,
1245+
"missing pll-reset-mode for pll %d\n", array[sz - 1]);
1246+
1247+
for (i = 0; i < sz; i += 2) {
1248+
num = array[i];
1249+
val = array[i + 1];
1250+
12361251
if (num >= 2) {
12371252
dev_err(&client->dev,
12381253
"invalid pll %d on pll-reset-mode prop\n", num);
12391254
return -EINVAL;
12401255
}
12411256

1242-
p = of_prop_next_u32(prop, p, &val);
1243-
if (!p) {
1244-
dev_err(&client->dev,
1245-
"missing pll-reset-mode for pll %d\n", num);
1246-
return -EINVAL;
1247-
}
12481257

12491258
switch (val) {
12501259
case 0:

drivers/clk/clk.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5364,9 +5364,8 @@ EXPORT_SYMBOL_GPL(of_clk_get_parent_count);
53645364
const char *of_clk_get_parent_name(const struct device_node *np, int index)
53655365
{
53665366
struct of_phandle_args clkspec;
5367-
struct property *prop;
53685367
const char *clk_name;
5369-
const __be32 *vp;
5368+
bool found = false;
53705369
u32 pv;
53715370
int rc;
53725371
int count;
@@ -5383,15 +5382,16 @@ const char *of_clk_get_parent_name(const struct device_node *np, int index)
53835382
/* if there is an indices property, use it to transfer the index
53845383
* specified into an array offset for the clock-output-names property.
53855384
*/
5386-
of_property_for_each_u32(clkspec.np, "clock-indices", prop, vp, pv) {
5385+
of_property_for_each_u32(clkspec.np, "clock-indices", pv) {
53875386
if (index == pv) {
53885387
index = count;
5388+
found = true;
53895389
break;
53905390
}
53915391
count++;
53925392
}
53935393
/* We went off the end of 'clock-indices' without finding it */
5394-
if (prop && !vp)
5394+
if (of_property_present(clkspec.np, "clock-indices") && !found)
53955395
return NULL;
53965396

53975397
if (of_property_read_string_index(clkspec.np, "clock-output-names",
@@ -5504,14 +5504,12 @@ static int parent_ready(struct device_node *np)
55045504
int of_clk_detect_critical(struct device_node *np, int index,
55055505
unsigned long *flags)
55065506
{
5507-
struct property *prop;
5508-
const __be32 *cur;
55095507
uint32_t idx;
55105508

55115509
if (!np || !flags)
55125510
return -EINVAL;
55135511

5514-
of_property_for_each_u32(np, "clock-critical", prop, cur, idx)
5512+
of_property_for_each_u32(np, "clock-critical", idx)
55155513
if (index == idx)
55165514
*flags |= CLK_IS_CRITICAL;
55175515

drivers/clk/qcom/common.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,11 +227,9 @@ EXPORT_SYMBOL_GPL(qcom_cc_register_sleep_clk);
227227
static void qcom_cc_drop_protected(struct device *dev, struct qcom_cc *cc)
228228
{
229229
struct device_node *np = dev->of_node;
230-
struct property *prop;
231-
const __be32 *p;
232230
u32 i;
233231

234-
of_property_for_each_u32(np, "protected-clocks", prop, p, i) {
232+
of_property_for_each_u32(np, "protected-clocks", i) {
235233
if (i >= cc->num_rclks)
236234
continue;
237235

drivers/clk/sunxi/clk-simple-gates.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,9 @@ static void __init sunxi_simple_gates_setup(struct device_node *node,
2121
{
2222
struct clk_onecell_data *clk_data;
2323
const char *clk_parent, *clk_name;
24-
struct property *prop;
2524
struct resource res;
2625
void __iomem *clk_reg;
2726
void __iomem *reg;
28-
const __be32 *p;
2927
int number, i = 0, j;
3028
u8 clk_bit;
3129
u32 index;
@@ -47,7 +45,7 @@ static void __init sunxi_simple_gates_setup(struct device_node *node,
4745
if (!clk_data->clks)
4846
goto err_free_data;
4947

50-
of_property_for_each_u32(node, "clock-indices", prop, p, index) {
48+
of_property_for_each_u32(node, "clock-indices", index) {
5149
of_property_read_string_index(node, "clock-output-names",
5250
i, &clk_name);
5351

drivers/clk/sunxi/clk-sun8i-bus-gates.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,9 @@ static void __init sun8i_h3_bus_gates_init(struct device_node *node)
2424
const char *parents[PARENT_MAX];
2525
struct clk_onecell_data *clk_data;
2626
const char *clk_name;
27-
struct property *prop;
2827
struct resource res;
2928
void __iomem *clk_reg;
3029
void __iomem *reg;
31-
const __be32 *p;
3230
int number, i;
3331
u8 clk_bit;
3432
int index;
@@ -58,7 +56,7 @@ static void __init sun8i_h3_bus_gates_init(struct device_node *node)
5856
goto err_free_data;
5957

6058
i = 0;
61-
of_property_for_each_u32(node, "clock-indices", prop, p, index) {
59+
of_property_for_each_u32(node, "clock-indices", index) {
6260
of_property_read_string_index(node, "clock-output-names",
6361
i, &clk_name);
6462

drivers/clocksource/samsung_pwm_timer.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -418,16 +418,14 @@ void __init samsung_pwm_clocksource_init(void __iomem *base,
418418
static int __init samsung_pwm_alloc(struct device_node *np,
419419
const struct samsung_pwm_variant *variant)
420420
{
421-
struct property *prop;
422-
const __be32 *cur;
423421
u32 val;
424422
int i, ret;
425423

426424
memcpy(&pwm.variant, variant, sizeof(pwm.variant));
427425
for (i = 0; i < SAMSUNG_PWM_NUM; ++i)
428426
pwm.irq[i] = irq_of_parse_and_map(np, i);
429427

430-
of_property_for_each_u32(np, "samsung,pwm-outputs", prop, cur, val) {
428+
of_property_for_each_u32(np, "samsung,pwm-outputs", val) {
431429
if (val >= SAMSUNG_PWM_NUM) {
432430
pr_warn("%s: invalid channel index in samsung,pwm-outputs property\n", __func__);
433431
continue;

0 commit comments

Comments
 (0)