Skip to content

Commit 22a5db9

Browse files
author
Linus Walleij
committed
Merge branch 'gpio-irq-validmask' of /home/linus/linux-pinctrl into devel
2 parents 9132ce4 + 79b804c commit 22a5db9

3 files changed

Lines changed: 75 additions & 3 deletions

File tree

‎Documentation/gpio/driver.txt‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,12 @@ symbol:
262262
to the container using container_of().
263263
(See Documentation/driver-model/design-patterns.txt)
264264

265+
If there is a need to exclude certain GPIOs from the IRQ domain, one can
266+
set .irq_need_valid_mask of the gpiochip before gpiochip_add_data() is
267+
called. This allocates .irq_valid_mask with as many bits set as there are
268+
GPIOs in the chip. Drivers can exclude GPIOs by clearing bits from this
269+
mask. The mask must be filled in before gpiochip_irqchip_add() is called.
270+
265271
* gpiochip_set_chained_irqchip(): sets up a chained irq handler for a
266272
gpio_chip from a parent IRQ and passes the struct gpio_chip* as handler
267273
data. (Notice handler data, since the irqchip data is likely used by the

‎drivers/gpio/gpiolib.c‎

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ LIST_HEAD(gpio_devices);
7171

7272
static void gpiochip_free_hogs(struct gpio_chip *chip);
7373
static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip);
74+
static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip);
75+
static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip);
7476

7577
static bool gpiolib_initialized;
7678

@@ -1167,6 +1169,10 @@ int gpiochip_add_data(struct gpio_chip *chip, void *data)
11671169
if (status)
11681170
goto err_remove_from_list;
11691171

1172+
status = gpiochip_irqchip_init_valid_mask(chip);
1173+
if (status)
1174+
goto err_remove_from_list;
1175+
11701176
status = of_gpiochip_add(chip);
11711177
if (status)
11721178
goto err_remove_chip;
@@ -1192,6 +1198,7 @@ int gpiochip_add_data(struct gpio_chip *chip, void *data)
11921198
acpi_gpiochip_remove(chip);
11931199
gpiochip_free_hogs(chip);
11941200
of_gpiochip_remove(chip);
1201+
gpiochip_irqchip_free_valid_mask(chip);
11951202
err_remove_from_list:
11961203
spin_lock_irqsave(&gpio_lock, flags);
11971204
list_del(&gdev->list);
@@ -1397,6 +1404,40 @@ static struct gpio_chip *find_chip_by_name(const char *name)
13971404
* The following is irqchip helper code for gpiochips.
13981405
*/
13991406

1407+
static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
1408+
{
1409+
int i;
1410+
1411+
if (!gpiochip->irq_need_valid_mask)
1412+
return 0;
1413+
1414+
gpiochip->irq_valid_mask = kcalloc(BITS_TO_LONGS(gpiochip->ngpio),
1415+
sizeof(long), GFP_KERNEL);
1416+
if (!gpiochip->irq_valid_mask)
1417+
return -ENOMEM;
1418+
1419+
/* Assume by default all GPIOs are valid */
1420+
for (i = 0; i < gpiochip->ngpio; i++)
1421+
set_bit(i, gpiochip->irq_valid_mask);
1422+
1423+
return 0;
1424+
}
1425+
1426+
static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
1427+
{
1428+
kfree(gpiochip->irq_valid_mask);
1429+
gpiochip->irq_valid_mask = NULL;
1430+
}
1431+
1432+
static bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gpiochip,
1433+
unsigned int offset)
1434+
{
1435+
/* No mask means all valid */
1436+
if (likely(!gpiochip->irq_valid_mask))
1437+
return true;
1438+
return test_bit(offset, gpiochip->irq_valid_mask);
1439+
}
1440+
14001441
/**
14011442
* gpiochip_set_chained_irqchip() - sets a chained irqchip to a gpiochip
14021443
* @gpiochip: the gpiochip to set the irqchip chain to
@@ -1438,9 +1479,12 @@ void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
14381479
}
14391480

14401481
/* Set the parent IRQ for all affected IRQs */
1441-
for (offset = 0; offset < gpiochip->ngpio; offset++)
1482+
for (offset = 0; offset < gpiochip->ngpio; offset++) {
1483+
if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1484+
continue;
14421485
irq_set_parent(irq_find_mapping(gpiochip->irqdomain, offset),
14431486
parent_irq);
1487+
}
14441488
}
14451489
EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
14461490

@@ -1547,9 +1591,12 @@ static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
15471591

15481592
/* Remove all IRQ mappings and delete the domain */
15491593
if (gpiochip->irqdomain) {
1550-
for (offset = 0; offset < gpiochip->ngpio; offset++)
1594+
for (offset = 0; offset < gpiochip->ngpio; offset++) {
1595+
if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1596+
continue;
15511597
irq_dispose_mapping(
15521598
irq_find_mapping(gpiochip->irqdomain, offset));
1599+
}
15531600
irq_domain_remove(gpiochip->irqdomain);
15541601
}
15551602

@@ -1558,6 +1605,8 @@ static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
15581605
gpiochip->irqchip->irq_release_resources = NULL;
15591606
gpiochip->irqchip = NULL;
15601607
}
1608+
1609+
gpiochip_irqchip_free_valid_mask(gpiochip);
15611610
}
15621611

15631612
/**
@@ -1593,6 +1642,7 @@ int _gpiochip_irqchip_add(struct gpio_chip *gpiochip,
15931642
struct lock_class_key *lock_key)
15941643
{
15951644
struct device_node *of_node;
1645+
bool irq_base_set = false;
15961646
unsigned int offset;
15971647
unsigned irq_base = 0;
15981648

@@ -1656,13 +1706,17 @@ int _gpiochip_irqchip_add(struct gpio_chip *gpiochip,
16561706
* necessary to allocate descriptors for all IRQs.
16571707
*/
16581708
for (offset = 0; offset < gpiochip->ngpio; offset++) {
1709+
if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1710+
continue;
16591711
irq_base = irq_create_mapping(gpiochip->irqdomain, offset);
1660-
if (offset == 0)
1712+
if (!irq_base_set) {
16611713
/*
16621714
* Store the base into the gpiochip to be used when
16631715
* unmapping the irqs.
16641716
*/
16651717
gpiochip->irq_base = irq_base;
1718+
irq_base_set = true;
1719+
}
16661720
}
16671721

16681722
acpi_gpiochip_request_interrupts(gpiochip);
@@ -1674,6 +1728,12 @@ EXPORT_SYMBOL_GPL(_gpiochip_irqchip_add);
16741728
#else /* CONFIG_GPIOLIB_IRQCHIP */
16751729

16761730
static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
1731+
static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
1732+
{
1733+
return 0;
1734+
}
1735+
static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
1736+
{ }
16771737

16781738
#endif /* CONFIG_GPIOLIB_IRQCHIP */
16791739

‎include/linux/gpio/driver.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ enum single_ended_mode {
112112
* initialization, provided by GPIO driver
113113
* @irq_parent: GPIO IRQ chip parent/bank linux irq number,
114114
* provided by GPIO driver
115+
* @irq_need_valid_mask: If set core allocates @irq_valid_mask with all
116+
* bits set to one
117+
* @irq_valid_mask: If not %NULL holds bitmask of GPIOs which are valid to
118+
* be included in IRQ domain of the chip
115119
* @lock_key: per GPIO IRQ chip lockdep class
116120
*
117121
* A gpio_chip can help platforms abstract various sources of GPIOs so
@@ -190,6 +194,8 @@ struct gpio_chip {
190194
irq_flow_handler_t irq_handler;
191195
unsigned int irq_default_type;
192196
int irq_parent;
197+
bool irq_need_valid_mask;
198+
unsigned long *irq_valid_mask;
193199
struct lock_class_key *lock_key;
194200
#endif
195201

0 commit comments

Comments
 (0)