Skip to content

Commit a2ab170

Browse files
author
Linus Walleij
committed
Merge branch 'ib-array-bitmaps' into devel
2 parents c02980d + b17566a commit a2ab170

16 files changed

Lines changed: 400 additions & 190 deletions

File tree

‎Documentation/driver-api/gpio/board.rst‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,3 +193,18 @@ And the table can be added to the board code as follows::
193193

194194
The line will be hogged as soon as the gpiochip is created or - in case the
195195
chip was created earlier - when the hog table is registered.
196+
197+
Arrays of pins
198+
--------------
199+
In addition to requesting pins belonging to a function one by one, a device may
200+
also request an array of pins assigned to the function. The way those pins are
201+
mapped to the device determines if the array qualifies for fast bitmap
202+
processing. If yes, a bitmap is passed over get/set array functions directly
203+
between a caller and a respective .get/set_multiple() callback of a GPIO chip.
204+
205+
In order to qualify for fast bitmap processing, the pin mapping must meet the
206+
following requirements:
207+
- it must belong to the same chip as other 'fast' pins of the function,
208+
- its index within the function must match its hardware number within the chip.
209+
210+
Open drain and open source pins are excluded from fast bitmap output processing.

‎Documentation/driver-api/gpio/consumer.rst‎

Lines changed: 33 additions & 13 deletions
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
}
@@ -323,29 +325,37 @@ The following functions get or set the values of an array of GPIOs::
323325

324326
int gpiod_get_array_value(unsigned int array_size,
325327
struct gpio_desc **desc_array,
326-
int *value_array);
328+
struct gpio_array *array_info,
329+
unsigned long *value_bitmap);
327330
int gpiod_get_raw_array_value(unsigned int array_size,
328331
struct gpio_desc **desc_array,
329-
int *value_array);
332+
struct gpio_array *array_info,
333+
unsigned long *value_bitmap);
330334
int gpiod_get_array_value_cansleep(unsigned int array_size,
331335
struct gpio_desc **desc_array,
332-
int *value_array);
336+
struct gpio_array *array_info,
337+
unsigned long *value_bitmap);
333338
int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
334339
struct gpio_desc **desc_array,
335-
int *value_array);
340+
struct gpio_array *array_info,
341+
unsigned long *value_bitmap);
336342

337343
void gpiod_set_array_value(unsigned int array_size,
338344
struct gpio_desc **desc_array,
339-
int *value_array)
345+
struct gpio_array *array_info,
346+
unsigned long *value_bitmap)
340347
void gpiod_set_raw_array_value(unsigned int array_size,
341348
struct gpio_desc **desc_array,
342-
int *value_array)
349+
struct gpio_array *array_info,
350+
unsigned long *value_bitmap)
343351
void gpiod_set_array_value_cansleep(unsigned int array_size,
344352
struct gpio_desc **desc_array,
345-
int *value_array)
353+
struct gpio_array *array_info,
354+
unsigned long *value_bitmap)
346355
void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
347356
struct gpio_desc **desc_array,
348-
int *value_array)
357+
struct gpio_array *array_info,
358+
unsigned long *value_bitmap)
349359

350360
The array can be an arbitrary set of GPIOs. The functions will try to access
351361
GPIOs belonging to the same bank or chip simultaneously if supported by the
@@ -356,8 +366,9 @@ accessed sequentially.
356366
The functions take three arguments:
357367
* array_size - the number of array elements
358368
* desc_array - an array of GPIO descriptors
359-
* value_array - an array to store the GPIOs' values (get) or
360-
an array of values to assign to the GPIOs (set)
369+
* array_info - optional information obtained from gpiod_array_get()
370+
* value_bitmap - a bitmap to store the GPIOs' values (get) or
371+
a bitmap of values to assign to the GPIOs (set)
361372

362373
The descriptor array can be obtained using the gpiod_get_array() function
363374
or one of its variants. If the group of descriptors returned by that function
@@ -366,16 +377,25 @@ the struct gpio_descs returned by gpiod_get_array()::
366377

367378
struct gpio_descs *my_gpio_descs = gpiod_get_array(...);
368379
gpiod_set_array_value(my_gpio_descs->ndescs, my_gpio_descs->desc,
369-
my_gpio_values);
380+
my_gpio_descs->info, my_gpio_value_bitmap);
370381

371382
It is also possible to access a completely arbitrary array of descriptors. The
372383
descriptors may be obtained using any combination of gpiod_get() and
373384
gpiod_get_array(). Afterwards the array of descriptors has to be setup
374-
manually before it can be passed to one of the above functions.
385+
manually before it can be passed to one of the above functions. In that case,
386+
array_info should be set to NULL.
375387

376388
Note that for optimal performance GPIOs belonging to the same chip should be
377389
contiguous within the array of descriptors.
378390

391+
Still better performance may be achieved if array indexes of the descriptors
392+
match hardware pin numbers of a single chip. If an array passed to a get/set
393+
array function matches the one obtained from gpiod_get_array() and array_info
394+
associated with the array is also passed, the function may take a fast bitmap
395+
processing path, passing the value_bitmap argument directly to the respective
396+
.get/set_multiple() callback of the chip. That allows for utilization of GPIO
397+
banks as data I/O ports without much loss of performance.
398+
379399
The return value of gpiod_get_array_value() and its variants is 0 on success
380400
or negative on error. Note the difference to gpiod_get_value(), which returns
381401
0 or 1 on success to convey the GPIO value. With the array functions, the GPIO

‎drivers/auxdisplay/hd44780.c‎

Lines changed: 21 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -62,53 +62,41 @@ static void hd44780_strobe_gpio(struct hd44780 *hd)
6262
/* write to an LCD panel register in 8 bit GPIO mode */
6363
static void hd44780_write_gpio8(struct hd44780 *hd, u8 val, unsigned int rs)
6464
{
65-
int values[10]; /* for DATA[0-7], RS, RW */
66-
unsigned int i, n;
67-
68-
for (i = 0; i < 8; i++)
69-
values[PIN_DATA0 + i] = !!(val & BIT(i));
70-
values[PIN_CTRL_RS] = rs;
71-
n = 9;
72-
if (hd->pins[PIN_CTRL_RW]) {
73-
values[PIN_CTRL_RW] = 0;
74-
n++;
75-
}
65+
DECLARE_BITMAP(values, 10); /* for DATA[0-7], RS, RW */
66+
unsigned int n;
67+
68+
values[0] = val;
69+
__assign_bit(8, values, rs);
70+
n = hd->pins[PIN_CTRL_RW] ? 10 : 9;
7671

7772
/* Present the data to the port */
78-
gpiod_set_array_value_cansleep(n, &hd->pins[PIN_DATA0], values);
73+
gpiod_set_array_value_cansleep(n, &hd->pins[PIN_DATA0], NULL, values);
7974

8075
hd44780_strobe_gpio(hd);
8176
}
8277

8378
/* write to an LCD panel register in 4 bit GPIO mode */
8479
static void hd44780_write_gpio4(struct hd44780 *hd, u8 val, unsigned int rs)
8580
{
86-
int values[10]; /* for DATA[0-7], RS, RW, but DATA[0-3] is unused */
87-
unsigned int i, n;
81+
DECLARE_BITMAP(values, 6); /* for DATA[4-7], RS, RW */
82+
unsigned int n;
8883

8984
/* High nibble + RS, RW */
90-
for (i = 4; i < 8; i++)
91-
values[PIN_DATA0 + i] = !!(val & BIT(i));
92-
values[PIN_CTRL_RS] = rs;
93-
n = 5;
94-
if (hd->pins[PIN_CTRL_RW]) {
95-
values[PIN_CTRL_RW] = 0;
96-
n++;
97-
}
85+
values[0] = val >> 4;
86+
__assign_bit(4, values, rs);
87+
n = hd->pins[PIN_CTRL_RW] ? 6 : 5;
9888

9989
/* Present the data to the port */
100-
gpiod_set_array_value_cansleep(n, &hd->pins[PIN_DATA4],
101-
&values[PIN_DATA4]);
90+
gpiod_set_array_value_cansleep(n, &hd->pins[PIN_DATA4], NULL, values);
10291

10392
hd44780_strobe_gpio(hd);
10493

10594
/* Low nibble */
106-
for (i = 0; i < 4; i++)
107-
values[PIN_DATA4 + i] = !!(val & BIT(i));
95+
values[0] &= ~0x0fUL;
96+
values[0] |= val & 0x0f;
10897

10998
/* Present the data to the port */
110-
gpiod_set_array_value_cansleep(n, &hd->pins[PIN_DATA4],
111-
&values[PIN_DATA4]);
99+
gpiod_set_array_value_cansleep(n, &hd->pins[PIN_DATA4], NULL, values);
112100

113101
hd44780_strobe_gpio(hd);
114102
}
@@ -155,23 +143,16 @@ static void hd44780_write_cmd_gpio4(struct charlcd *lcd, int cmd)
155143
/* Send 4-bits of a command to the LCD panel in raw 4 bit GPIO mode */
156144
static void hd44780_write_cmd_raw_gpio4(struct charlcd *lcd, int cmd)
157145
{
158-
int values[10]; /* for DATA[0-7], RS, RW, but DATA[0-3] is unused */
146+
DECLARE_BITMAP(values, 6); /* for DATA[4-7], RS, RW */
159147
struct hd44780 *hd = lcd->drvdata;
160-
unsigned int i, n;
148+
unsigned int n;
161149

162150
/* Command nibble + RS, RW */
163-
for (i = 0; i < 4; i++)
164-
values[PIN_DATA4 + i] = !!(cmd & BIT(i));
165-
values[PIN_CTRL_RS] = 0;
166-
n = 5;
167-
if (hd->pins[PIN_CTRL_RW]) {
168-
values[PIN_CTRL_RW] = 0;
169-
n++;
170-
}
151+
values[0] = cmd & 0x0f;
152+
n = hd->pins[PIN_CTRL_RW] ? 6 : 5;
171153

172154
/* Present the data to the port */
173-
gpiod_set_array_value_cansleep(n, &hd->pins[PIN_DATA4],
174-
&values[PIN_DATA4]);
155+
gpiod_set_array_value_cansleep(n, &hd->pins[PIN_DATA4], NULL, values);
175156

176157
hd44780_strobe_gpio(hd);
177158
}

‎drivers/bus/ts-nbus.c‎

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,12 @@ static void ts_nbus_set_direction(struct ts_nbus *ts_nbus, int direction)
110110
*/
111111
static void ts_nbus_reset_bus(struct ts_nbus *ts_nbus)
112112
{
113-
int i;
114-
int values[8];
113+
DECLARE_BITMAP(values, 8);
115114

116-
for (i = 0; i < 8; i++)
117-
values[i] = 0;
115+
values[0] = 0;
118116

119-
gpiod_set_array_value_cansleep(8, ts_nbus->data->desc, values);
117+
gpiod_set_array_value_cansleep(8, ts_nbus->data->desc,
118+
ts_nbus->data->info, values);
120119
gpiod_set_value_cansleep(ts_nbus->csn, 0);
121120
gpiod_set_value_cansleep(ts_nbus->strobe, 0);
122121
gpiod_set_value_cansleep(ts_nbus->ale, 0);
@@ -157,16 +156,11 @@ static int ts_nbus_read_byte(struct ts_nbus *ts_nbus, u8 *val)
157156
static void ts_nbus_write_byte(struct ts_nbus *ts_nbus, u8 byte)
158157
{
159158
struct gpio_descs *gpios = ts_nbus->data;
160-
int i;
161-
int values[8];
159+
DECLARE_BITMAP(values, 8);
162160

163-
for (i = 0; i < 8; i++)
164-
if (byte & BIT(i))
165-
values[i] = 1;
166-
else
167-
values[i] = 0;
161+
values[0] = byte;
168162

169-
gpiod_set_array_value_cansleep(8, gpios->desc, values);
163+
gpiod_set_array_value_cansleep(8, gpios->desc, gpios->info, values);
170164
}
171165

172166
/*

‎drivers/gpio/gpio-max3191x.c‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -313,18 +313,21 @@ static int max3191x_set_config(struct gpio_chip *gpio, unsigned int offset,
313313

314314
static void gpiod_set_array_single_value_cansleep(unsigned int ndescs,
315315
struct gpio_desc **desc,
316+
struct gpio_array *info,
316317
int value)
317318
{
318-
int i, *values;
319+
unsigned long *values;
319320

320-
values = kmalloc_array(ndescs, sizeof(*values), GFP_KERNEL);
321+
values = bitmap_alloc(ndescs, GFP_KERNEL);
321322
if (!values)
322323
return;
323324

324-
for (i = 0; i < ndescs; i++)
325-
values[i] = value;
325+
if (value)
326+
bitmap_fill(values, ndescs);
327+
else
328+
bitmap_zero(values, ndescs);
326329

327-
gpiod_set_array_value_cansleep(ndescs, desc, values);
330+
gpiod_set_array_value_cansleep(ndescs, desc, info, values);
328331
kfree(values);
329332
}
330333

@@ -397,7 +400,8 @@ static int max3191x_probe(struct spi_device *spi)
397400
if (max3191x->modesel_pins)
398401
gpiod_set_array_single_value_cansleep(
399402
max3191x->modesel_pins->ndescs,
400-
max3191x->modesel_pins->desc, max3191x->mode);
403+
max3191x->modesel_pins->desc,
404+
max3191x->modesel_pins->info, max3191x->mode);
401405

402406
max3191x->ignore_uv = device_property_read_bool(dev,
403407
"maxim,ignore-undervoltage");

0 commit comments

Comments
 (0)