Skip to content

Commit 1de452a

Browse files
diandersbroonie
authored andcommitted
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>
1 parent 6eabfc0 commit 1de452a

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

drivers/regulator/devres.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,34 @@ int devm_regulator_bulk_get(struct device *dev, int num_consumers,
166166
}
167167
EXPORT_SYMBOL_GPL(devm_regulator_bulk_get);
168168

169+
/**
170+
* devm_regulator_bulk_get_const - devm_regulator_bulk_get() w/ const data
171+
*
172+
* @dev: device to supply
173+
* @num_consumers: number of consumers to register
174+
* @in_consumers: const configuration of consumers
175+
* @out_consumers: in_consumers is copied here and this is passed to
176+
* devm_regulator_bulk_get().
177+
*
178+
* This is a convenience function to allow bulk regulator configuration
179+
* to be stored "static const" in files.
180+
*
181+
* Return: 0 on success, an errno on failure.
182+
*/
183+
int devm_regulator_bulk_get_const(struct device *dev, int num_consumers,
184+
const struct regulator_bulk_data *in_consumers,
185+
struct regulator_bulk_data **out_consumers)
186+
{
187+
*out_consumers = devm_kmemdup(dev, in_consumers,
188+
num_consumers * sizeof(*in_consumers),
189+
GFP_KERNEL);
190+
if (*out_consumers == NULL)
191+
return -ENOMEM;
192+
193+
return devm_regulator_bulk_get(dev, num_consumers, *out_consumers);
194+
}
195+
EXPORT_SYMBOL_GPL(devm_regulator_bulk_get_const);
196+
169197
static void devm_rdev_release(struct device *dev, void *res)
170198
{
171199
regulator_unregister(*(struct regulator_dev **)res);

include/linux/regulator/consumer.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,10 @@ int __must_check regulator_bulk_get(struct device *dev, int num_consumers,
244244
struct regulator_bulk_data *consumers);
245245
int __must_check devm_regulator_bulk_get(struct device *dev, int num_consumers,
246246
struct regulator_bulk_data *consumers);
247+
int __must_check devm_regulator_bulk_get_const(
248+
struct device *dev, int num_consumers,
249+
const struct regulator_bulk_data *in_consumers,
250+
struct regulator_bulk_data **out_consumers);
247251
int __must_check regulator_bulk_enable(int num_consumers,
248252
struct regulator_bulk_data *consumers);
249253
int regulator_bulk_disable(int num_consumers,

0 commit comments

Comments
 (0)