Skip to content

Commit 89b0b4e

Browse files
committed
Merge branch 'gpio-reserved-ranges' into devel
2 parents 6cb9215 + 691bf5d commit 89b0b4e

File tree

5 files changed

+167
-11
lines changed

5 files changed

+167
-11
lines changed

Documentation/devicetree/bindings/gpio/gpio.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,9 @@ in a lot of designs, some using all 32 bits, some using 18 and some using
151151
first 18 GPIOs, at local offset 0 .. 17, are in use.
152152

153153
If these GPIOs do not happen to be the first N GPIOs at offset 0...N-1, an
154-
additional bitmask is needed to specify which GPIOs are actually in use,
155-
and which are dummies. The bindings for this case has not yet been
156-
specified, but should be specified if/when such hardware appears.
154+
additional set of tuples is needed to specify which GPIOs are unusable, with
155+
the gpio-reserved-ranges binding. This property indicates the start and size
156+
of the GPIOs that can't be used.
157157

158158
Optionally, a GPIO controller may have a "gpio-line-names" property. This is
159159
an array of strings defining the names of the GPIO lines going out of the
@@ -178,6 +178,7 @@ gpio-controller@00000000 {
178178
gpio-controller;
179179
#gpio-cells = <2>;
180180
ngpios = <18>;
181+
gpio-reserved-ranges = <0 4>, <12 2>;
181182
gpio-line-names = "MMC-CD", "MMC-WP", "VDD eth", "RST eth", "LED R",
182183
"LED G", "LED B", "Col A", "Col B", "Col C", "Col D",
183184
"Row A", "Row B", "Row C", "Row D", "NMI button",

drivers/gpio/gpiolib-of.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,28 @@ void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc)
511511
}
512512
EXPORT_SYMBOL(of_mm_gpiochip_remove);
513513

514+
static void of_gpiochip_init_valid_mask(struct gpio_chip *chip)
515+
{
516+
int len, i;
517+
u32 start, count;
518+
struct device_node *np = chip->of_node;
519+
520+
len = of_property_count_u32_elems(np, "gpio-reserved-ranges");
521+
if (len < 0 || len % 2 != 0)
522+
return;
523+
524+
for (i = 0; i < len; i += 2) {
525+
of_property_read_u32_index(np, "gpio-reserved-ranges",
526+
i, &start);
527+
of_property_read_u32_index(np, "gpio-reserved-ranges",
528+
i + 1, &count);
529+
if (start >= chip->ngpio || start + count >= chip->ngpio)
530+
continue;
531+
532+
bitmap_clear(chip->valid_mask, start, count);
533+
}
534+
};
535+
514536
#ifdef CONFIG_PINCTRL
515537
static int of_gpiochip_add_pin_range(struct gpio_chip *chip)
516538
{
@@ -615,6 +637,8 @@ int of_gpiochip_add(struct gpio_chip *chip)
615637
if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS)
616638
return -EINVAL;
617639

640+
of_gpiochip_init_valid_mask(chip);
641+
618642
status = of_gpiochip_add_pin_range(chip);
619643
if (status)
620644
return status;

drivers/gpio/gpiolib.c

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,57 @@ static int gpiochip_set_desc_names(struct gpio_chip *gc)
337337
return 0;
338338
}
339339

340+
static unsigned long *gpiochip_allocate_mask(struct gpio_chip *chip)
341+
{
342+
unsigned long *p;
343+
344+
p = kmalloc_array(BITS_TO_LONGS(chip->ngpio), sizeof(*p), GFP_KERNEL);
345+
if (!p)
346+
return NULL;
347+
348+
/* Assume by default all GPIOs are valid */
349+
bitmap_fill(p, chip->ngpio);
350+
351+
return p;
352+
}
353+
354+
static int gpiochip_init_valid_mask(struct gpio_chip *gpiochip)
355+
{
356+
#ifdef CONFIG_OF_GPIO
357+
int size;
358+
struct device_node *np = gpiochip->of_node;
359+
360+
size = of_property_count_u32_elems(np, "gpio-reserved-ranges");
361+
if (size > 0 && size % 2 == 0)
362+
gpiochip->need_valid_mask = true;
363+
#endif
364+
365+
if (!gpiochip->need_valid_mask)
366+
return 0;
367+
368+
gpiochip->valid_mask = gpiochip_allocate_mask(gpiochip);
369+
if (!gpiochip->valid_mask)
370+
return -ENOMEM;
371+
372+
return 0;
373+
}
374+
375+
static void gpiochip_free_valid_mask(struct gpio_chip *gpiochip)
376+
{
377+
kfree(gpiochip->valid_mask);
378+
gpiochip->valid_mask = NULL;
379+
}
380+
381+
bool gpiochip_line_is_valid(const struct gpio_chip *gpiochip,
382+
unsigned int offset)
383+
{
384+
/* No mask means all valid */
385+
if (likely(!gpiochip->valid_mask))
386+
return true;
387+
return test_bit(offset, gpiochip->valid_mask);
388+
}
389+
EXPORT_SYMBOL_GPL(gpiochip_line_is_valid);
390+
340391
/*
341392
* GPIO line handle management
342393
*/
@@ -1261,6 +1312,10 @@ int gpiochip_add_data_with_key(struct gpio_chip *chip, void *data,
12611312
if (status)
12621313
goto err_remove_from_list;
12631314

1315+
status = gpiochip_init_valid_mask(chip);
1316+
if (status)
1317+
goto err_remove_irqchip_mask;
1318+
12641319
status = gpiochip_add_irqchip(chip, lock_key, request_key);
12651320
if (status)
12661321
goto err_remove_chip;
@@ -1290,6 +1345,8 @@ int gpiochip_add_data_with_key(struct gpio_chip *chip, void *data,
12901345
acpi_gpiochip_remove(chip);
12911346
gpiochip_free_hogs(chip);
12921347
of_gpiochip_remove(chip);
1348+
gpiochip_free_valid_mask(chip);
1349+
err_remove_irqchip_mask:
12931350
gpiochip_irqchip_free_valid_mask(chip);
12941351
err_remove_from_list:
12951352
spin_lock_irqsave(&gpio_lock, flags);
@@ -1346,6 +1403,7 @@ void gpiochip_remove(struct gpio_chip *chip)
13461403
acpi_gpiochip_remove(chip);
13471404
gpiochip_remove_pin_ranges(chip);
13481405
of_gpiochip_remove(chip);
1406+
gpiochip_free_valid_mask(chip);
13491407
/*
13501408
* We accept no more calls into the driver from this point, so
13511409
* NULL the driver data pointer
@@ -1506,14 +1564,10 @@ static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
15061564
if (!gpiochip->irq.need_valid_mask)
15071565
return 0;
15081566

1509-
gpiochip->irq.valid_mask = kcalloc(BITS_TO_LONGS(gpiochip->ngpio),
1510-
sizeof(long), GFP_KERNEL);
1567+
gpiochip->irq.valid_mask = gpiochip_allocate_mask(gpiochip);
15111568
if (!gpiochip->irq.valid_mask)
15121569
return -ENOMEM;
15131570

1514-
/* Assume by default all GPIOs are valid */
1515-
bitmap_fill(gpiochip->irq.valid_mask, gpiochip->ngpio);
1516-
15171571
return 0;
15181572
}
15191573

@@ -1526,6 +1580,8 @@ static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
15261580
bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gpiochip,
15271581
unsigned int offset)
15281582
{
1583+
if (!gpiochip_line_is_valid(gpiochip, offset))
1584+
return false;
15291585
/* No mask means all valid */
15301586
if (likely(!gpiochip->irq.valid_mask))
15311587
return true;

drivers/pinctrl/qcom/pinctrl-msm.c

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,14 @@ static const struct pinctrl_ops msm_pinctrl_ops = {
105105
.dt_free_map = pinctrl_utils_free_map,
106106
};
107107

108+
static int msm_pinmux_request(struct pinctrl_dev *pctldev, unsigned offset)
109+
{
110+
struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
111+
struct gpio_chip *chip = &pctrl->chip;
112+
113+
return gpiochip_line_is_valid(chip, offset) ? 0 : -EINVAL;
114+
}
115+
108116
static int msm_get_functions_count(struct pinctrl_dev *pctldev)
109117
{
110118
struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
@@ -166,6 +174,7 @@ static int msm_pinmux_set_mux(struct pinctrl_dev *pctldev,
166174
}
167175

168176
static const struct pinmux_ops msm_pinmux_ops = {
177+
.request = msm_pinmux_request,
169178
.get_functions_count = msm_get_functions_count,
170179
.get_function_name = msm_get_function_name,
171180
.get_function_groups = msm_get_function_groups,
@@ -506,6 +515,9 @@ static void msm_gpio_dbg_show_one(struct seq_file *s,
506515
"pull up"
507516
};
508517

518+
if (!gpiochip_line_is_valid(chip, offset))
519+
return;
520+
509521
g = &pctrl->soc->groups[offset];
510522
ctl_reg = readl(pctrl->regs + g->ctl_reg);
511523

@@ -517,17 +529,16 @@ static void msm_gpio_dbg_show_one(struct seq_file *s,
517529
seq_printf(s, " %-8s: %-3s %d", g->name, is_out ? "out" : "in", func);
518530
seq_printf(s, " %dmA", msm_regval_to_drive(drive));
519531
seq_printf(s, " %s", pulls[pull]);
532+
seq_puts(s, "\n");
520533
}
521534

522535
static void msm_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
523536
{
524537
unsigned gpio = chip->base;
525538
unsigned i;
526539

527-
for (i = 0; i < chip->ngpio; i++, gpio++) {
540+
for (i = 0; i < chip->ngpio; i++, gpio++)
528541
msm_gpio_dbg_show_one(s, NULL, chip, i, gpio);
529-
seq_puts(s, "\n");
530-
}
531542
}
532543

533544
#else
@@ -808,6 +819,46 @@ static void msm_gpio_irq_handler(struct irq_desc *desc)
808819
chained_irq_exit(chip, desc);
809820
}
810821

822+
static int msm_gpio_init_valid_mask(struct gpio_chip *chip,
823+
struct msm_pinctrl *pctrl)
824+
{
825+
int ret;
826+
unsigned int len, i;
827+
unsigned int max_gpios = pctrl->soc->ngpios;
828+
u16 *tmp;
829+
830+
/* The number of GPIOs in the ACPI tables */
831+
len = ret = device_property_read_u16_array(pctrl->dev, "gpios", NULL, 0);
832+
if (ret < 0)
833+
return 0;
834+
835+
if (ret > max_gpios)
836+
return -EINVAL;
837+
838+
tmp = kmalloc_array(len, sizeof(*tmp), GFP_KERNEL);
839+
if (!tmp)
840+
return -ENOMEM;
841+
842+
ret = device_property_read_u16_array(pctrl->dev, "gpios", tmp, len);
843+
if (ret < 0) {
844+
dev_err(pctrl->dev, "could not read list of GPIOs\n");
845+
goto out;
846+
}
847+
848+
bitmap_zero(chip->valid_mask, max_gpios);
849+
for (i = 0; i < len; i++)
850+
set_bit(tmp[i], chip->valid_mask);
851+
852+
out:
853+
kfree(tmp);
854+
return ret;
855+
}
856+
857+
static bool msm_gpio_needs_valid_mask(struct msm_pinctrl *pctrl)
858+
{
859+
return device_property_read_u16_array(pctrl->dev, "gpios", NULL, 0) > 0;
860+
}
861+
811862
static int msm_gpio_init(struct msm_pinctrl *pctrl)
812863
{
813864
struct gpio_chip *chip;
@@ -824,13 +875,21 @@ static int msm_gpio_init(struct msm_pinctrl *pctrl)
824875
chip->parent = pctrl->dev;
825876
chip->owner = THIS_MODULE;
826877
chip->of_node = pctrl->dev->of_node;
878+
chip->need_valid_mask = msm_gpio_needs_valid_mask(pctrl);
827879

828880
ret = gpiochip_add_data(&pctrl->chip, pctrl);
829881
if (ret) {
830882
dev_err(pctrl->dev, "Failed register gpiochip\n");
831883
return ret;
832884
}
833885

886+
ret = msm_gpio_init_valid_mask(chip, pctrl);
887+
if (ret) {
888+
dev_err(pctrl->dev, "Failed to setup irq valid bits\n");
889+
gpiochip_remove(&pctrl->chip);
890+
return ret;
891+
}
892+
834893
ret = gpiochip_add_pin_range(&pctrl->chip, dev_name(pctrl->dev), 0, 0, chip->ngpio);
835894
if (ret) {
836895
dev_err(pctrl->dev, "Failed to add pin range\n");

include/linux/gpio/driver.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,21 @@ struct gpio_chip {
288288
struct gpio_irq_chip irq;
289289
#endif
290290

291+
/**
292+
* @need_valid_mask:
293+
*
294+
* If set core allocates @valid_mask with all bits set to one.
295+
*/
296+
bool need_valid_mask;
297+
298+
/**
299+
* @valid_mask:
300+
*
301+
* If not %NULL holds bitmask of GPIOs which are valid to be used
302+
* from the chip.
303+
*/
304+
unsigned long *valid_mask;
305+
291306
#if defined(CONFIG_OF_GPIO)
292307
/*
293308
* If CONFIG_OF is enabled, then all GPIO controllers described in the
@@ -384,6 +399,7 @@ bool gpiochip_line_is_open_source(struct gpio_chip *chip, unsigned int offset);
384399

385400
/* Sleep persistence inquiry for drivers */
386401
bool gpiochip_line_is_persistent(struct gpio_chip *chip, unsigned int offset);
402+
bool gpiochip_line_is_valid(const struct gpio_chip *chip, unsigned int offset);
387403

388404
/* get driver data */
389405
void *gpiochip_get_data(struct gpio_chip *chip);

0 commit comments

Comments
 (0)