@@ -71,6 +71,8 @@ LIST_HEAD(gpio_devices);
7171
7272static void gpiochip_free_hogs (struct gpio_chip * chip );
7373static 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
7577static 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 );
11951202err_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}
14451489EXPORT_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
16761730static 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
0 commit comments