regulator: core: Allow drivers to define their init data as const
authorDouglas Anderson <dianders@chromium.org>
Tue, 26 Jul 2022 17:38:23 +0000 (10:38 -0700)
committerMark Brown <broonie@kernel.org>
Wed, 27 Jul 2022 12:47:30 +0000 (13:47 +0100)
commit1de452a0edda26f1483d1d934f692eab13ba669a
tree2dec252bd004db3f6a322791a17492166842e32c
parent6eabfc018e8d1033e7fc1efce30a872e2dccb537
regulator: core: Allow drivers to define their init data as const

Drivers tend to want to define the names of their regulators somewhere
in their source file as "static const". This means, inevitable, that
every driver out there open codes something like this:

static const char * const supply_names[] = {
 "vcc", "vccl",
};

static int get_regulators(struct my_data *data)
{
  int i;

  data->supplies = devm_kzalloc(...)
  if (!data->supplies)
    return -ENOMEM;

  for (i = 0; i < ARRAY_SIZE(supply_names); i++)
    data->supplies[i].supply = supply_names[i];

  return devm_regulator_bulk_get(data->dev,
                                 ARRAY_SIZE(supply_names),
 data->supplies);
}

Let's make this more convenient by doing providing a helper that does
the copy.

I have chosen to have the "const" input structure here be the exact
same structure as the normal one passed to
devm_regulator_bulk_get(). This is slightly inefficent since the input
data can't possibly have anything useful for "ret" or consumer and
thus we waste 8 bytes per structure. This seems an OK tradeoff for not
introducing an extra structure.

Signed-off-by: Douglas Anderson <dianders@chromium.org>
Link: https://lore.kernel.org/r/20220726103631.v2.6.I38fc508a73135a5c1b873851f3553ff2a3a625f5@changeid
Signed-off-by: Mark Brown <broonie@kernel.org>
drivers/regulator/devres.c
include/linux/regulator/consumer.h