Skip to content

Commit eec1d56

Browse files
l1kLinus Walleij
authored andcommitted
gpio: Introduce ->get_multiple callback
SPI-attached GPIO controllers typically read out all inputs in one go. If callers desire the values of multipe inputs, ideally a single readout should take place to return the desired values. However the current driver API only offers a ->get callback but no ->get_multiple (unlike ->set_multiple, which is present). Thus, to read multiple inputs, a full readout needs to be performed for every single value (barring driver-internal caching), which is inefficient. In fact, the lack of a ->get_multiple callback has been bemoaned repeatedly by the gpio subsystem maintainer: http://www.spinics.net/lists/linux-gpio/msg10571.html http://www.spinics.net/lists/devicetree/msg121734.html Introduce the missing callback. Add corresponding consumer functions such as gpiod_get_array_value(). Amend linehandle_ioctl() to take advantage of the newly added infrastructure. Update the documentation. Cc: Rojhalat Ibrahim <imr@rtschenk.de> Signed-off-by: Lukas Wunner <lukas@wunner.de> Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
1 parent 5307e2a commit eec1d56

5 files changed

Lines changed: 250 additions & 22 deletions

File tree

Documentation/gpio/consumer.txt

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -295,9 +295,22 @@ as possible, especially by drivers which should not care about the actual
295295
physical line level and worry about the logical value instead.
296296

297297

298-
Set multiple GPIO outputs with a single function call
299-
-----------------------------------------------------
300-
The following functions set the output values of an array of GPIOs:
298+
Access multiple GPIOs with a single function call
299+
-------------------------------------------------
300+
The following functions get or set the values of an array of GPIOs:
301+
302+
int gpiod_get_array_value(unsigned int array_size,
303+
struct gpio_desc **desc_array,
304+
int *value_array);
305+
int gpiod_get_raw_array_value(unsigned int array_size,
306+
struct gpio_desc **desc_array,
307+
int *value_array);
308+
int gpiod_get_array_value_cansleep(unsigned int array_size,
309+
struct gpio_desc **desc_array,
310+
int *value_array);
311+
int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
312+
struct gpio_desc **desc_array,
313+
int *value_array);
301314

302315
void gpiod_set_array_value(unsigned int array_size,
303316
struct gpio_desc **desc_array,
@@ -312,34 +325,40 @@ The following functions set the output values of an array of GPIOs:
312325
struct gpio_desc **desc_array,
313326
int *value_array)
314327

315-
The array can be an arbitrary set of GPIOs. The functions will try to set
328+
The array can be an arbitrary set of GPIOs. The functions will try to access
316329
GPIOs belonging to the same bank or chip simultaneously if supported by the
317330
corresponding chip driver. In that case a significantly improved performance
318-
can be expected. If simultaneous setting is not possible the GPIOs will be set
319-
sequentially.
331+
can be expected. If simultaneous access is not possible the GPIOs will be
332+
accessed sequentially.
320333

321-
The gpiod_set_array() functions take three arguments:
334+
The functions take three arguments:
322335
* array_size - the number of array elements
323336
* desc_array - an array of GPIO descriptors
324-
* value_array - an array of values to assign to the GPIOs
337+
* value_array - an array to store the GPIOs' values (get) or
338+
an array of values to assign to the GPIOs (set)
325339

326340
The descriptor array can be obtained using the gpiod_get_array() function
327341
or one of its variants. If the group of descriptors returned by that function
328-
matches the desired group of GPIOs, those GPIOs can be set by simply using
342+
matches the desired group of GPIOs, those GPIOs can be accessed by simply using
329343
the struct gpio_descs returned by gpiod_get_array():
330344

331345
struct gpio_descs *my_gpio_descs = gpiod_get_array(...);
332346
gpiod_set_array_value(my_gpio_descs->ndescs, my_gpio_descs->desc,
333347
my_gpio_values);
334348

335-
It is also possible to set a completely arbitrary array of descriptors. The
349+
It is also possible to access a completely arbitrary array of descriptors. The
336350
descriptors may be obtained using any combination of gpiod_get() and
337351
gpiod_get_array(). Afterwards the array of descriptors has to be setup
338-
manually before it can be used with gpiod_set_array().
352+
manually before it can be passed to one of the above functions.
339353

340354
Note that for optimal performance GPIOs belonging to the same chip should be
341355
contiguous within the array of descriptors.
342356

357+
The return value of gpiod_get_array_value() and its variants is 0 on success
358+
or negative on error. Note the difference to gpiod_get_value(), which returns
359+
0 or 1 on success to convey the GPIO value. With the array functions, the GPIO
360+
values are stored in value_array rather than passed back as return value.
361+
343362

344363
GPIOs mapped to IRQs
345364
--------------------

drivers/gpio/gpiolib.c

Lines changed: 168 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -365,28 +365,28 @@ static long linehandle_ioctl(struct file *filep, unsigned int cmd,
365365
struct linehandle_state *lh = filep->private_data;
366366
void __user *ip = (void __user *)arg;
367367
struct gpiohandle_data ghd;
368+
int vals[GPIOHANDLES_MAX];
368369
int i;
369370

370371
if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
371-
int val;
372+
/* TODO: check if descriptors are really input */
373+
int ret = gpiod_get_array_value_complex(false,
374+
true,
375+
lh->numdescs,
376+
lh->descs,
377+
vals);
378+
if (ret)
379+
return ret;
372380

373381
memset(&ghd, 0, sizeof(ghd));
374-
375-
/* TODO: check if descriptors are really input */
376-
for (i = 0; i < lh->numdescs; i++) {
377-
val = gpiod_get_value_cansleep(lh->descs[i]);
378-
if (val < 0)
379-
return val;
380-
ghd.values[i] = val;
381-
}
382+
for (i = 0; i < lh->numdescs; i++)
383+
ghd.values[i] = vals[i];
382384

383385
if (copy_to_user(ip, &ghd, sizeof(ghd)))
384386
return -EFAULT;
385387

386388
return 0;
387389
} else if (cmd == GPIOHANDLE_SET_LINE_VALUES_IOCTL) {
388-
int vals[GPIOHANDLES_MAX];
389-
390390
/* TODO: check if descriptors are really output */
391391
if (copy_from_user(&ghd, ip, sizeof(ghd)))
392392
return -EFAULT;
@@ -2466,6 +2466,71 @@ static int gpiod_get_raw_value_commit(const struct gpio_desc *desc)
24662466
return value;
24672467
}
24682468

2469+
static int gpio_chip_get_multiple(struct gpio_chip *chip,
2470+
unsigned long *mask, unsigned long *bits)
2471+
{
2472+
if (chip->get_multiple) {
2473+
return chip->get_multiple(chip, mask, bits);
2474+
} else if (chip->get) {
2475+
int i, value;
2476+
2477+
for_each_set_bit(i, mask, chip->ngpio) {
2478+
value = chip->get(chip, i);
2479+
if (value < 0)
2480+
return value;
2481+
__assign_bit(i, bits, value);
2482+
}
2483+
return 0;
2484+
}
2485+
return -EIO;
2486+
}
2487+
2488+
int gpiod_get_array_value_complex(bool raw, bool can_sleep,
2489+
unsigned int array_size,
2490+
struct gpio_desc **desc_array,
2491+
int *value_array)
2492+
{
2493+
int i = 0;
2494+
2495+
while (i < array_size) {
2496+
struct gpio_chip *chip = desc_array[i]->gdev->chip;
2497+
unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
2498+
unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
2499+
int first, j, ret;
2500+
2501+
if (!can_sleep)
2502+
WARN_ON(chip->can_sleep);
2503+
2504+
/* collect all inputs belonging to the same chip */
2505+
first = i;
2506+
memset(mask, 0, sizeof(mask));
2507+
do {
2508+
const struct gpio_desc *desc = desc_array[i];
2509+
int hwgpio = gpio_chip_hwgpio(desc);
2510+
2511+
__set_bit(hwgpio, mask);
2512+
i++;
2513+
} while ((i < array_size) &&
2514+
(desc_array[i]->gdev->chip == chip));
2515+
2516+
ret = gpio_chip_get_multiple(chip, mask, bits);
2517+
if (ret)
2518+
return ret;
2519+
2520+
for (j = first; j < i; j++) {
2521+
const struct gpio_desc *desc = desc_array[j];
2522+
int hwgpio = gpio_chip_hwgpio(desc);
2523+
int value = test_bit(hwgpio, bits);
2524+
2525+
if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2526+
value = !value;
2527+
value_array[j] = value;
2528+
trace_gpio_value(desc_to_gpio(desc), 1, value);
2529+
}
2530+
}
2531+
return 0;
2532+
}
2533+
24692534
/**
24702535
* gpiod_get_raw_value() - return a gpio's raw value
24712536
* @desc: gpio whose value will be returned
@@ -2514,6 +2579,51 @@ int gpiod_get_value(const struct gpio_desc *desc)
25142579
}
25152580
EXPORT_SYMBOL_GPL(gpiod_get_value);
25162581

2582+
/**
2583+
* gpiod_get_raw_array_value() - read raw values from an array of GPIOs
2584+
* @array_size: number of elements in the descriptor / value arrays
2585+
* @desc_array: array of GPIO descriptors whose values will be read
2586+
* @value_array: array to store the read values
2587+
*
2588+
* Read the raw values of the GPIOs, i.e. the values of the physical lines
2589+
* without regard for their ACTIVE_LOW status. Return 0 in case of success,
2590+
* else an error code.
2591+
*
2592+
* This function should be called from contexts where we cannot sleep,
2593+
* and it will complain if the GPIO chip functions potentially sleep.
2594+
*/
2595+
int gpiod_get_raw_array_value(unsigned int array_size,
2596+
struct gpio_desc **desc_array, int *value_array)
2597+
{
2598+
if (!desc_array)
2599+
return -EINVAL;
2600+
return gpiod_get_array_value_complex(true, false, array_size,
2601+
desc_array, value_array);
2602+
}
2603+
EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value);
2604+
2605+
/**
2606+
* gpiod_get_array_value() - read values from an array of GPIOs
2607+
* @array_size: number of elements in the descriptor / value arrays
2608+
* @desc_array: array of GPIO descriptors whose values will be read
2609+
* @value_array: array to store the read values
2610+
*
2611+
* Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2612+
* into account. Return 0 in case of success, else an error code.
2613+
*
2614+
* This function should be called from contexts where we cannot sleep,
2615+
* and it will complain if the GPIO chip functions potentially sleep.
2616+
*/
2617+
int gpiod_get_array_value(unsigned int array_size,
2618+
struct gpio_desc **desc_array, int *value_array)
2619+
{
2620+
if (!desc_array)
2621+
return -EINVAL;
2622+
return gpiod_get_array_value_complex(false, false, array_size,
2623+
desc_array, value_array);
2624+
}
2625+
EXPORT_SYMBOL_GPL(gpiod_get_array_value);
2626+
25172627
/*
25182628
* gpio_set_open_drain_value_commit() - Set the open drain gpio's value.
25192629
* @desc: gpio descriptor whose state need to be set.
@@ -2942,6 +3052,53 @@ int gpiod_get_value_cansleep(const struct gpio_desc *desc)
29423052
}
29433053
EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
29443054

3055+
/**
3056+
* gpiod_get_raw_array_value_cansleep() - read raw values from an array of GPIOs
3057+
* @array_size: number of elements in the descriptor / value arrays
3058+
* @desc_array: array of GPIO descriptors whose values will be read
3059+
* @value_array: array to store the read values
3060+
*
3061+
* Read the raw values of the GPIOs, i.e. the values of the physical lines
3062+
* without regard for their ACTIVE_LOW status. Return 0 in case of success,
3063+
* else an error code.
3064+
*
3065+
* This function is to be called from contexts that can sleep.
3066+
*/
3067+
int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
3068+
struct gpio_desc **desc_array,
3069+
int *value_array)
3070+
{
3071+
might_sleep_if(extra_checks);
3072+
if (!desc_array)
3073+
return -EINVAL;
3074+
return gpiod_get_array_value_complex(true, true, array_size,
3075+
desc_array, value_array);
3076+
}
3077+
EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value_cansleep);
3078+
3079+
/**
3080+
* gpiod_get_array_value_cansleep() - read values from an array of GPIOs
3081+
* @array_size: number of elements in the descriptor / value arrays
3082+
* @desc_array: array of GPIO descriptors whose values will be read
3083+
* @value_array: array to store the read values
3084+
*
3085+
* Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3086+
* into account. Return 0 in case of success, else an error code.
3087+
*
3088+
* This function is to be called from contexts that can sleep.
3089+
*/
3090+
int gpiod_get_array_value_cansleep(unsigned int array_size,
3091+
struct gpio_desc **desc_array,
3092+
int *value_array)
3093+
{
3094+
might_sleep_if(extra_checks);
3095+
if (!desc_array)
3096+
return -EINVAL;
3097+
return gpiod_get_array_value_complex(false, true, array_size,
3098+
desc_array, value_array);
3099+
}
3100+
EXPORT_SYMBOL_GPL(gpiod_get_array_value_cansleep);
3101+
29453102
/**
29463103
* gpiod_set_raw_value_cansleep() - assign a gpio's raw value
29473104
* @desc: gpio whose value will be assigned

drivers/gpio/gpiolib.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,10 @@ static inline bool acpi_can_fallback_to_crs(struct acpi_device *adev,
180180
#endif
181181

182182
struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip, u16 hwnum);
183+
int gpiod_get_array_value_complex(bool raw, bool can_sleep,
184+
unsigned int array_size,
185+
struct gpio_desc **desc_array,
186+
int *value_array);
183187
void gpiod_set_array_value_complex(bool raw, bool can_sleep,
184188
unsigned int array_size,
185189
struct gpio_desc **desc_array,

include/linux/gpio/consumer.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,22 +99,33 @@ int gpiod_direction_output_raw(struct gpio_desc *desc, int value);
9999

100100
/* Value get/set from non-sleeping context */
101101
int gpiod_get_value(const struct gpio_desc *desc);
102+
int gpiod_get_array_value(unsigned int array_size,
103+
struct gpio_desc **desc_array, int *value_array);
102104
void gpiod_set_value(struct gpio_desc *desc, int value);
103105
void gpiod_set_array_value(unsigned int array_size,
104106
struct gpio_desc **desc_array, int *value_array);
105107
int gpiod_get_raw_value(const struct gpio_desc *desc);
108+
int gpiod_get_raw_array_value(unsigned int array_size,
109+
struct gpio_desc **desc_array,
110+
int *value_array);
106111
void gpiod_set_raw_value(struct gpio_desc *desc, int value);
107112
void gpiod_set_raw_array_value(unsigned int array_size,
108113
struct gpio_desc **desc_array,
109114
int *value_array);
110115

111116
/* Value get/set from sleeping context */
112117
int gpiod_get_value_cansleep(const struct gpio_desc *desc);
118+
int gpiod_get_array_value_cansleep(unsigned int array_size,
119+
struct gpio_desc **desc_array,
120+
int *value_array);
113121
void gpiod_set_value_cansleep(struct gpio_desc *desc, int value);
114122
void gpiod_set_array_value_cansleep(unsigned int array_size,
115123
struct gpio_desc **desc_array,
116124
int *value_array);
117125
int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc);
126+
int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
127+
struct gpio_desc **desc_array,
128+
int *value_array);
118129
void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value);
119130
void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
120131
struct gpio_desc **desc_array,
@@ -305,6 +316,14 @@ static inline int gpiod_get_value(const struct gpio_desc *desc)
305316
WARN_ON(1);
306317
return 0;
307318
}
319+
static inline int gpiod_get_array_value(unsigned int array_size,
320+
struct gpio_desc **desc_array,
321+
int *value_array)
322+
{
323+
/* GPIO can never have been requested */
324+
WARN_ON(1);
325+
return 0;
326+
}
308327
static inline void gpiod_set_value(struct gpio_desc *desc, int value)
309328
{
310329
/* GPIO can never have been requested */
@@ -323,6 +342,14 @@ static inline int gpiod_get_raw_value(const struct gpio_desc *desc)
323342
WARN_ON(1);
324343
return 0;
325344
}
345+
static inline int gpiod_get_raw_array_value(unsigned int array_size,
346+
struct gpio_desc **desc_array,
347+
int *value_array)
348+
{
349+
/* GPIO can never have been requested */
350+
WARN_ON(1);
351+
return 0;
352+
}
326353
static inline void gpiod_set_raw_value(struct gpio_desc *desc, int value)
327354
{
328355
/* GPIO can never have been requested */
@@ -342,6 +369,14 @@ static inline int gpiod_get_value_cansleep(const struct gpio_desc *desc)
342369
WARN_ON(1);
343370
return 0;
344371
}
372+
static inline int gpiod_get_array_value_cansleep(unsigned int array_size,
373+
struct gpio_desc **desc_array,
374+
int *value_array)
375+
{
376+
/* GPIO can never have been requested */
377+
WARN_ON(1);
378+
return 0;
379+
}
345380
static inline void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
346381
{
347382
/* GPIO can never have been requested */
@@ -360,6 +395,14 @@ static inline int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
360395
WARN_ON(1);
361396
return 0;
362397
}
398+
static inline int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
399+
struct gpio_desc **desc_array,
400+
int *value_array)
401+
{
402+
/* GPIO can never have been requested */
403+
WARN_ON(1);
404+
return 0;
405+
}
363406
static inline void gpiod_set_raw_value_cansleep(struct gpio_desc *desc,
364407
int value)
365408
{

0 commit comments

Comments
 (0)