gpio: aggregator: Wrap access to gpiochip_fwd.tmp[]
authorGeert Uytterhoeven <geert+renesas@glider.be>
Mon, 4 Oct 2021 12:45:45 +0000 (14:45 +0200)
committerBartosz Golaszewski <brgl@bgdev.pl>
Wed, 13 Oct 2021 14:19:23 +0000 (16:19 +0200)
The tmp[] member of the gpiochip_fwd structure is used to store both the
temporary values bitmap and the desc pointers for operations on multiple
GPIOs.  As both are arrays with sizes unknown at compile-time, accessing
them requires offset calculations, which are currently duplicated in
gpio_fwd_get_multiple() and gpio_fwd_set_multiple().

Introduce (a) accessors for both arrays and (b) a macro to calculate the
needed storage size.  This confines the layout of the tmp[] member into
a single spot, to ease maintenance.

Signed-off-by: Geert Uytterhoeven <geert+renesas@glider.be>
Signed-off-by: Bartosz Golaszewski <brgl@bgdev.pl>
drivers/gpio/gpio-aggregator.c

index 34e35b64dcdc0581d5acce7f56ea11620a6eaa72..e9671d1660ef4b4006dcdc6865a502d0fc0c89bc 100644 (file)
@@ -247,6 +247,11 @@ struct gpiochip_fwd {
        unsigned long tmp[];            /* values and descs for multiple ops */
 };
 
+#define fwd_tmp_values(fwd)    &(fwd)->tmp[0]
+#define fwd_tmp_descs(fwd)     (void *)&(fwd)->tmp[BITS_TO_LONGS((fwd)->chip.ngpio)]
+
+#define fwd_tmp_size(ngpios)   (BITS_TO_LONGS((ngpios)) + (ngpios))
+
 static int gpio_fwd_get_direction(struct gpio_chip *chip, unsigned int offset)
 {
        struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
@@ -279,15 +284,11 @@ static int gpio_fwd_get(struct gpio_chip *chip, unsigned int offset)
 static int gpio_fwd_get_multiple(struct gpiochip_fwd *fwd, unsigned long *mask,
                                 unsigned long *bits)
 {
-       struct gpio_desc **descs;
-       unsigned long *values;
+       struct gpio_desc **descs = fwd_tmp_descs(fwd);
+       unsigned long *values = fwd_tmp_values(fwd);
        unsigned int i, j = 0;
        int error;
 
-       /* Both values bitmap and desc pointers are stored in tmp[] */
-       values = &fwd->tmp[0];
-       descs = (void *)&fwd->tmp[BITS_TO_LONGS(fwd->chip.ngpio)];
-
        bitmap_clear(values, 0, fwd->chip.ngpio);
        for_each_set_bit(i, mask, fwd->chip.ngpio)
                descs[j++] = fwd->descs[i];
@@ -333,14 +334,10 @@ static void gpio_fwd_set(struct gpio_chip *chip, unsigned int offset, int value)
 static void gpio_fwd_set_multiple(struct gpiochip_fwd *fwd, unsigned long *mask,
                                  unsigned long *bits)
 {
-       struct gpio_desc **descs;
-       unsigned long *values;
+       struct gpio_desc **descs = fwd_tmp_descs(fwd);
+       unsigned long *values = fwd_tmp_values(fwd);
        unsigned int i, j = 0;
 
-       /* Both values bitmap and desc pointers are stored in tmp[] */
-       values = &fwd->tmp[0];
-       descs = (void *)&fwd->tmp[BITS_TO_LONGS(fwd->chip.ngpio)];
-
        for_each_set_bit(i, mask, fwd->chip.ngpio) {
                __assign_bit(j, values, test_bit(i, bits));
                descs[j++] = fwd->descs[i];
@@ -398,8 +395,8 @@ static struct gpiochip_fwd *gpiochip_fwd_create(struct device *dev,
        unsigned int i;
        int error;
 
-       fwd = devm_kzalloc(dev, struct_size(fwd, tmp,
-                          BITS_TO_LONGS(ngpios) + ngpios), GFP_KERNEL);
+       fwd = devm_kzalloc(dev, struct_size(fwd, tmp, fwd_tmp_size(ngpios)),
+                          GFP_KERNEL);
        if (!fwd)
                return ERR_PTR(-ENOMEM);