Skip to content

Commit bf9346f

Browse files
jkrzysztlinusw
authored andcommitted
gpiolib: Identify arrays matching GPIO hardware
Certain GPIO array lookup results may map directly to GPIO pins of a single GPIO chip in hardware order. If that condition is recognized and handled efficiently, significant performance gain of get/set array functions may be possible. While processing a request for an array of GPIO descriptors, identify those which represent corresponding pins of a single GPIO chip. Skip over pins which require open source or open drain special processing. Moreover, identify pins which require inversion. Pass a pointer to that information with the array to the caller so it can benefit from enhanced performance as soon as get/set array functions can accept and make efficient use of it. Cc: Jonathan Corbet <corbet@lwn.net> Signed-off-by: Janusz Krzysztofik <jmkrzyszt@gmail.com> Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
1 parent b9762be commit bf9346f

File tree

4 files changed

+92
-2
lines changed

4 files changed

+92
-2
lines changed

Documentation/driver-api/gpio/consumer.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,11 @@ For a function using multiple GPIOs all of those can be obtained with one call::
109109
enum gpiod_flags flags)
110110

111111
This function returns a struct gpio_descs which contains an array of
112-
descriptors::
112+
descriptors. It also contains a pointer to a gpiolib private structure which,
113+
if passed back to get/set array functions, may speed up I/O proocessing::
113114

114115
struct gpio_descs {
116+
struct gpio_array *info;
115117
unsigned int ndescs;
116118
struct gpio_desc *desc[];
117119
}

drivers/gpio/gpiolib.c

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4174,7 +4174,9 @@ struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
41744174
{
41754175
struct gpio_desc *desc;
41764176
struct gpio_descs *descs;
4177-
int count;
4177+
struct gpio_array *array_info = NULL;
4178+
struct gpio_chip *chip;
4179+
int count, bitmap_size;
41784180

41794181
count = gpiod_count(dev, con_id);
41804182
if (count < 0)
@@ -4190,9 +4192,77 @@ struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
41904192
gpiod_put_array(descs);
41914193
return ERR_CAST(desc);
41924194
}
4195+
41934196
descs->desc[descs->ndescs] = desc;
4197+
4198+
chip = gpiod_to_chip(desc);
4199+
/*
4200+
* Select a chip of first array member
4201+
* whose index matches its pin hardware number
4202+
* as a candidate for fast bitmap processing.
4203+
*/
4204+
if (!array_info && gpio_chip_hwgpio(desc) == descs->ndescs) {
4205+
struct gpio_descs *array;
4206+
4207+
bitmap_size = BITS_TO_LONGS(chip->ngpio > count ?
4208+
chip->ngpio : count);
4209+
4210+
array = kzalloc(struct_size(descs, desc, count) +
4211+
struct_size(array_info, invert_mask,
4212+
3 * bitmap_size), GFP_KERNEL);
4213+
if (!array) {
4214+
gpiod_put_array(descs);
4215+
return ERR_PTR(-ENOMEM);
4216+
}
4217+
4218+
memcpy(array, descs,
4219+
struct_size(descs, desc, descs->ndescs + 1));
4220+
kfree(descs);
4221+
4222+
descs = array;
4223+
array_info = (void *)(descs->desc + count);
4224+
array_info->get_mask = array_info->invert_mask +
4225+
bitmap_size;
4226+
array_info->set_mask = array_info->get_mask +
4227+
bitmap_size;
4228+
4229+
array_info->desc = descs->desc;
4230+
array_info->size = count;
4231+
array_info->chip = chip;
4232+
bitmap_set(array_info->get_mask, descs->ndescs,
4233+
count - descs->ndescs);
4234+
bitmap_set(array_info->set_mask, descs->ndescs,
4235+
count - descs->ndescs);
4236+
descs->info = array_info;
4237+
}
4238+
/*
4239+
* Unmark members which don't qualify for fast bitmap
4240+
* processing (different chip, not in hardware order)
4241+
*/
4242+
if (array_info && (chip != array_info->chip ||
4243+
gpio_chip_hwgpio(desc) != descs->ndescs)) {
4244+
__clear_bit(descs->ndescs, array_info->get_mask);
4245+
__clear_bit(descs->ndescs, array_info->set_mask);
4246+
} else if (array_info) {
4247+
/* Exclude open drain or open source from fast output */
4248+
if (gpiochip_line_is_open_drain(chip, descs->ndescs) ||
4249+
gpiochip_line_is_open_source(chip, descs->ndescs))
4250+
__clear_bit(descs->ndescs,
4251+
array_info->set_mask);
4252+
/* Identify 'fast' pins which require invertion */
4253+
if (gpiod_is_active_low(desc))
4254+
__set_bit(descs->ndescs,
4255+
array_info->invert_mask);
4256+
}
4257+
41944258
descs->ndescs++;
41954259
}
4260+
if (array_info)
4261+
dev_dbg(dev,
4262+
"GPIO array info: chip=%s, size=%d, get_mask=%lx, set_mask=%lx, invert_mask=%lx\n",
4263+
array_info->chip->label, array_info->size,
4264+
*array_info->get_mask, *array_info->set_mask,
4265+
*array_info->invert_mask);
41964266
return descs;
41974267
}
41984268
EXPORT_SYMBOL_GPL(gpiod_get_array);

drivers/gpio/gpiolib.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,15 @@ static inline bool acpi_can_fallback_to_crs(struct acpi_device *adev,
183183
}
184184
#endif
185185

186+
struct gpio_array {
187+
struct gpio_desc **desc;
188+
unsigned int size;
189+
struct gpio_chip *chip;
190+
unsigned long *get_mask;
191+
unsigned long *set_mask;
192+
unsigned long invert_mask[];
193+
};
194+
186195
struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip, u16 hwnum);
187196
int gpiod_get_array_value_complex(bool raw, bool can_sleep,
188197
unsigned int array_size,

include/linux/gpio/consumer.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,20 @@ struct device;
1717
*/
1818
struct gpio_desc;
1919

20+
/**
21+
* Opaque descriptor for a structure of GPIO array attributes. This structure
22+
* is attached to struct gpiod_descs obtained from gpiod_get_array() and can be
23+
* passed back to get/set array functions in order to activate fast processing
24+
* path if applicable.
25+
*/
26+
struct gpio_array;
27+
2028
/**
2129
* Struct containing an array of descriptors that can be obtained using
2230
* gpiod_get_array().
2331
*/
2432
struct gpio_descs {
33+
struct gpio_array *info;
2534
unsigned int ndescs;
2635
struct gpio_desc *desc[];
2736
};

0 commit comments

Comments
 (0)