Skip to content

Commit

Permalink
drivers: regulator: fix variable sized voltages fallback
Browse files Browse the repository at this point in the history
Fix build issue reported by Clang on variable size field desc
not being located at the end of struct voltages_fallback. The
error was reported with a trace message like below:

core/include/drivers/regulator.h:118:4: warning: field 'voltages_fallback' with variable sized type 'struct voltages_fallback' not at the end of a struct or class is a GNU extension [-Wgnu-variable-sized-type-not-at-end]
        } voltages_fallback;
          ^
core/drivers/regulator/regulator_fixed.c:27:19: warning: field 'regulator' with variable sized type 'struct regulator' not at the end of a struct or class is a GNU extension [-Wgnu-variable-sized-type-not-at-end]
        struct regulator regulator;
                         ^
2 warnings generated.

API function regulator_supported_voltages() asserts that
struct regulator_voltages and struct voltages_fallback do match.

By the way, remove empty line in struct regulator_voltages inline
description comment.

Fixes: 43c155b ("drivers: regulator: list supported levels")
Signed-off-by: Etienne Carriere <[email protected]>
  • Loading branch information
etienne-lms committed Nov 23, 2023
1 parent c7f9abc commit 1b004f4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
17 changes: 13 additions & 4 deletions core/drivers/regulator/regulator.c
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,14 @@ TEE_Result regulator_set_voltage(struct regulator *regulator, int level_uv)
TEE_Result regulator_supported_voltages(struct regulator *regulator,
struct regulator_voltages **voltages)
{
struct voltages_fallback __unused fb = { };

static_assert(offsetof(struct regulator_voltages, type) ==
offsetof(struct voltages_fallback, type) &&
offsetof(struct regulator_voltages, num_levels) ==
offsetof(struct voltages_fallback, num_levels) &&
sizeof(*(*voltages)->entries) == sizeof(*fb.levels));

assert(regulator && voltages);

if (regulator->ops->supported_voltages) {
Expand All @@ -227,13 +235,14 @@ TEE_Result regulator_supported_voltages(struct regulator *regulator,
return res;
}

*voltages = &regulator->voltages_fallback.desc;
*voltages = (void *)(vaddr_t)&regulator->voltages_fallback;

return TEE_SUCCESS;
}

TEE_Result regulator_register(struct regulator *regulator)
{
struct regulator_voltages __unused voltages = { };
TEE_Result res = TEE_SUCCESS;
int min_uv = 0;
int max_uv = 0;
Expand Down Expand Up @@ -272,11 +281,11 @@ TEE_Result regulator_register(struct regulator *regulator)

/* Preset voltage list in case ops::supported_voltages is NULL */
if (regulator->min_uv == regulator->max_uv) {
regulator->voltages_fallback.desc.type = VOLTAGE_TYPE_FULL_LIST;
regulator->voltages_fallback.desc.num_levels = 1;
regulator->voltages_fallback.type = VOLTAGE_TYPE_FULL_LIST;
regulator->voltages_fallback.num_levels = 1;
regulator->voltages_fallback.levels[0] = regulator->min_uv;
} else {
regulator->voltages_fallback.desc.type = VOLTAGE_TYPE_INCREMENT;
regulator->voltages_fallback.type = VOLTAGE_TYPE_INCREMENT;
regulator->voltages_fallback.levels[0] = regulator->min_uv;
regulator->voltages_fallback.levels[1] = regulator->max_uv;
regulator->voltages_fallback.levels[2] = 1;
Expand Down
18 changes: 13 additions & 5 deletions core/include/drivers/regulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,25 @@ enum voltage_type {
* @type: Type of level description
* @num_levels: Number of cells of @entries when @type is VOLTAGE_TYPE_FULL_LIST
* @entries: Voltage level information in uV
*
*/
struct regulator_voltages {
enum voltage_type type;
size_t num_levels;
int entries[];
};

/*
* struct struct voltages_fallback - Default Voltage levels description
* @type: Type of level description
* @num_levels: Number of cells of @entries when @type is VOLTAGE_TYPE_FULL_LIST
* @levels: Voltage level information in uV
*/
struct voltages_fallback {
enum voltage_type type;
size_t num_levels;
int levels[3];
};

/*
* struct regulator - A regulator instance
* @ops: Operation handlers for the regulator
Expand Down Expand Up @@ -107,10 +118,7 @@ struct regulator {
unsigned int flags;
unsigned int refcount;
struct mutex lock; /* Concurrent access protection */
struct voltages_fallback {
struct regulator_voltages desc;
int levels[3];
} voltages_fallback;
struct voltages_fallback voltages_fallback;
size_t levels_count_fallback;
SLIST_ENTRY(regulator) link;
};
Expand Down

0 comments on commit 1b004f4

Please sign in to comment.