Re: [PATCH 3/3] iio: adc: xilinx-ams: refactor alarm mapping to table-driven approach

From: Andy Shevchenko

Date: Tue Apr 14 2026 - 06:04:48 EST


On Tue, Apr 14, 2026 at 06:29:30AM -0300, Guilherme Ivo Bozi wrote:
> Replace multiple open-coded switch statements that map between
> scan_index, alarm bits, and register offsets with a centralized
> table-driven approach.
>
> Introduce a struct-based alarm_map to describe the relationship
> between scan indices and alarm offsets, and add a helper to
> translate scan_index to event IDs. This removes duplicated logic
> across ams_get_alarm_offset(), ams_event_to_channel(), and
> ams_get_alarm_mask().
>
> The new approach improves maintainability, reduces code size,
> and makes it easier to extend or modify alarm mappings in the
> future, while preserving existing behavior.

...

> +#define AMS_ALARM_INVALID -1

This value sounds like out of the range, also signed.
Can't 0x000 be used instead?

#define AMS_ALARM_NONE 0x000 /* not a real offset */

> #define AMS_ALARM_TEMP 0x140
> #define AMS_ALARM_SUPPLY1 0x144
> #define AMS_ALARM_SUPPLY2 0x148

...

> +struct ams_alarm_map {
> + enum ams_ps_pl_seq scan_index;
> + int base_offset;

With the above we put this as u32 (or whatever is used for offsets).

> +};

...

> +static int ams_scan_index_to_event(int scan_index)
> +{
> + int i;
> +
> + for (i = 0; i < ARRAY_SIZE(alarm_map); i++) {

for (unsigned int i = 0; i < ARRAY_SIZE(alarm_map); i++) {

> + if (alarm_map[i].base_offset == AMS_ALARM_INVALID)
> + continue;
> +
> + if (alarm_map[i].scan_index == scan_index)
> + return i;
> + }
> +
> + return -EINVAL;
> +}

--
With Best Regards,
Andy Shevchenko