Skip to content

Commit c8587ee

Browse files
Christian RuppertLinus Walleij
authored andcommitted
pinctrl: add pin list based GPIO ranges
Traditionally, GPIO ranges are based on consecutive ranges of both GPIO and pin numbers. This patch allows for GPIO ranges with arbitrary lists of pin numbers. Signed-off-by: Christian Ruppert <christian.ruppert@abilis.com> Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
1 parent 5ace03f commit c8587ee

2 files changed

Lines changed: 52 additions & 11 deletions

File tree

‎drivers/pinctrl/core.c‎

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,29 @@ static int pinctrl_register_pins(struct pinctrl_dev *pctldev,
282282
return 0;
283283
}
284284

285+
/**
286+
* gpio_to_pin() - GPIO range GPIO number to pin number translation
287+
* @range: GPIO range used for the translation
288+
* @gpio: gpio pin to translate to a pin number
289+
*
290+
* Finds the pin number for a given GPIO using the specified GPIO range
291+
* as a base for translation. The distinction between linear GPIO ranges
292+
* and pin list based GPIO ranges is managed correctly by this function.
293+
*
294+
* This function assumes the gpio is part of the specified GPIO range, use
295+
* only after making sure this is the case (e.g. by calling it on the
296+
* result of successful pinctrl_get_device_gpio_range calls)!
297+
*/
298+
static inline int gpio_to_pin(struct pinctrl_gpio_range *range,
299+
unsigned int gpio)
300+
{
301+
unsigned int offset = gpio - range->base;
302+
if (range->pins)
303+
return range->pins[offset];
304+
else
305+
return range->pin_base + offset;
306+
}
307+
285308
/**
286309
* pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
287310
* @pctldev: pin controller device to check
@@ -448,8 +471,14 @@ pinctrl_find_gpio_range_from_pin(struct pinctrl_dev *pctldev,
448471
/* Loop over the ranges */
449472
list_for_each_entry(range, &pctldev->gpio_ranges, node) {
450473
/* Check if we're in the valid range */
451-
if (pin >= range->pin_base &&
452-
pin < range->pin_base + range->npins) {
474+
if (range->pins) {
475+
int a;
476+
for (a = 0; a < range->npins; a++) {
477+
if (range->pins[a] == pin)
478+
return range;
479+
}
480+
} else if (pin >= range->pin_base &&
481+
pin < range->pin_base + range->npins) {
453482
mutex_unlock(&pctldev->mutex);
454483
return range;
455484
}
@@ -529,7 +558,7 @@ int pinctrl_request_gpio(unsigned gpio)
529558
}
530559

531560
/* Convert to the pin controllers number space */
532-
pin = gpio - range->base + range->pin_base;
561+
pin = gpio_to_pin(range, gpio);
533562

534563
ret = pinmux_request_gpio(pctldev, range, pin, gpio);
535564

@@ -559,7 +588,7 @@ void pinctrl_free_gpio(unsigned gpio)
559588
mutex_lock(&pctldev->mutex);
560589

561590
/* Convert to the pin controllers number space */
562-
pin = gpio - range->base + range->pin_base;
591+
pin = gpio_to_pin(range, gpio);
563592

564593
pinmux_free_gpio(pctldev, pin, range);
565594

@@ -582,7 +611,7 @@ static int pinctrl_gpio_direction(unsigned gpio, bool input)
582611
mutex_lock(&pctldev->mutex);
583612

584613
/* Convert to the pin controllers number space */
585-
pin = gpio - range->base + range->pin_base;
614+
pin = gpio_to_pin(range, gpio);
586615
ret = pinmux_gpio_direction(pctldev, range, pin, input);
587616

588617
mutex_unlock(&pctldev->mutex);
@@ -1349,11 +1378,21 @@ static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
13491378

13501379
/* Loop over the ranges */
13511380
list_for_each_entry(range, &pctldev->gpio_ranges, node) {
1352-
seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
1353-
range->id, range->name,
1354-
range->base, (range->base + range->npins - 1),
1355-
range->pin_base,
1356-
(range->pin_base + range->npins - 1));
1381+
if (range->pins) {
1382+
int a;
1383+
seq_printf(s, "%u: %s GPIOS [%u - %u] PINS {",
1384+
range->id, range->name,
1385+
range->base, (range->base + range->npins - 1));
1386+
for (a = 0; a < range->npins - 1; a++)
1387+
seq_printf(s, "%u, ", range->pins[a]);
1388+
seq_printf(s, "%u}\n", range->pins[a]);
1389+
}
1390+
else
1391+
seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
1392+
range->id, range->name,
1393+
range->base, (range->base + range->npins - 1),
1394+
range->pin_base,
1395+
(range->pin_base + range->npins - 1));
13571396
}
13581397

13591398
mutex_unlock(&pctldev->mutex);

‎include/linux/pinctrl/pinctrl.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ struct pinctrl_pin_desc {
4949
* @name: a name for the chip in this range
5050
* @id: an ID number for the chip in this range
5151
* @base: base offset of the GPIO range
52-
* @pin_base: base pin number of the GPIO range
52+
* @pin_base: base pin number of the GPIO range if pins != NULL
53+
* @pins: enumeration of pins in GPIO range or NULL
5354
* @npins: number of pins in the GPIO range, including the base number
5455
* @gc: an optional pointer to a gpio_chip
5556
*/
@@ -59,6 +60,7 @@ struct pinctrl_gpio_range {
5960
unsigned int id;
6061
unsigned int base;
6162
unsigned int pin_base;
63+
unsigned const *pins;
6264
unsigned int npins;
6365
struct gpio_chip *gc;
6466
};

0 commit comments

Comments
 (0)