@@ -68,6 +68,7 @@ LIST_HEAD(gpio_devices);
68
68
static void gpiochip_free_hogs (struct gpio_chip * chip );
69
69
static void gpiochip_irqchip_remove (struct gpio_chip * gpiochip );
70
70
71
+ static bool gpiolib_initialized ;
71
72
72
73
static inline void desc_set_label (struct gpio_desc * d , const char * label )
73
74
{
@@ -440,9 +441,63 @@ static void gpiodevice_release(struct device *dev)
440
441
cdev_del (& gdev -> chrdev );
441
442
list_del (& gdev -> list );
442
443
ida_simple_remove (& gpio_ida , gdev -> id );
444
+ kfree (gdev -> label );
445
+ kfree (gdev -> descs );
443
446
kfree (gdev );
444
447
}
445
448
449
+ static int gpiochip_setup_dev (struct gpio_device * gdev )
450
+ {
451
+ int status ;
452
+
453
+ cdev_init (& gdev -> chrdev , & gpio_fileops );
454
+ gdev -> chrdev .owner = THIS_MODULE ;
455
+ gdev -> chrdev .kobj .parent = & gdev -> dev .kobj ;
456
+ gdev -> dev .devt = MKDEV (MAJOR (gpio_devt ), gdev -> id );
457
+ status = cdev_add (& gdev -> chrdev , gdev -> dev .devt , 1 );
458
+ if (status < 0 )
459
+ chip_warn (gdev -> chip , "failed to add char device %d:%d\n" ,
460
+ MAJOR (gpio_devt ), gdev -> id );
461
+ else
462
+ chip_dbg (gdev -> chip , "added GPIO chardev (%d:%d)\n" ,
463
+ MAJOR (gpio_devt ), gdev -> id );
464
+ status = device_add (& gdev -> dev );
465
+ if (status )
466
+ goto err_remove_chardev ;
467
+
468
+ status = gpiochip_sysfs_register (gdev );
469
+ if (status )
470
+ goto err_remove_device ;
471
+
472
+ /* From this point, the .release() function cleans up gpio_device */
473
+ gdev -> dev .release = gpiodevice_release ;
474
+ get_device (& gdev -> dev );
475
+ pr_debug ("%s: registered GPIOs %d to %d on device: %s (%s)\n" ,
476
+ __func__ , gdev -> base , gdev -> base + gdev -> ngpio - 1 ,
477
+ dev_name (& gdev -> dev ), gdev -> chip -> label ? : "generic" );
478
+
479
+ return 0 ;
480
+
481
+ err_remove_device :
482
+ device_del (& gdev -> dev );
483
+ err_remove_chardev :
484
+ cdev_del (& gdev -> chrdev );
485
+ return status ;
486
+ }
487
+
488
+ static void gpiochip_setup_devs (void )
489
+ {
490
+ struct gpio_device * gdev ;
491
+ int err ;
492
+
493
+ list_for_each_entry (gdev , & gpio_devices , list ) {
494
+ err = gpiochip_setup_dev (gdev );
495
+ if (err )
496
+ pr_err ("%s: Failed to initialize gpio device (%d)\n" ,
497
+ dev_name (& gdev -> dev ), err );
498
+ }
499
+ }
500
+
446
501
/**
447
502
* gpiochip_add_data() - register a gpio_chip
448
503
* @chip: the chip to register, with chip->base initialized
@@ -457,6 +512,9 @@ static void gpiodevice_release(struct device *dev)
457
512
* the gpio framework's arch_initcall(). Otherwise sysfs initialization
458
513
* for GPIOs will fail rudely.
459
514
*
515
+ * gpiochip_add_data() must only be called after gpiolib initialization,
516
+ * ie after core_initcall().
517
+ *
460
518
* If chip->base is negative, this requests dynamic assignment of
461
519
* a range of valid GPIOs.
462
520
*/
@@ -504,8 +562,7 @@ int gpiochip_add_data(struct gpio_chip *chip, void *data)
504
562
else
505
563
gdev -> owner = THIS_MODULE ;
506
564
507
- gdev -> descs = devm_kcalloc (& gdev -> dev , chip -> ngpio ,
508
- sizeof (gdev -> descs [0 ]), GFP_KERNEL );
565
+ gdev -> descs = kcalloc (chip -> ngpio , sizeof (gdev -> descs [0 ]), GFP_KERNEL );
509
566
if (!gdev -> descs ) {
510
567
status = - ENOMEM ;
511
568
goto err_free_gdev ;
@@ -514,16 +571,16 @@ int gpiochip_add_data(struct gpio_chip *chip, void *data)
514
571
if (chip -> ngpio == 0 ) {
515
572
chip_err (chip , "tried to insert a GPIO chip with zero lines\n" );
516
573
status = - EINVAL ;
517
- goto err_free_gdev ;
574
+ goto err_free_descs ;
518
575
}
519
576
520
577
if (chip -> label )
521
- gdev -> label = devm_kstrdup ( & gdev -> dev , chip -> label , GFP_KERNEL );
578
+ gdev -> label = kstrdup ( chip -> label , GFP_KERNEL );
522
579
else
523
- gdev -> label = devm_kstrdup ( & gdev -> dev , "unknown" , GFP_KERNEL );
580
+ gdev -> label = kstrdup ( "unknown" , GFP_KERNEL );
524
581
if (!gdev -> label ) {
525
582
status = - ENOMEM ;
526
- goto err_free_gdev ;
583
+ goto err_free_descs ;
527
584
}
528
585
529
586
gdev -> ngpio = chip -> ngpio ;
@@ -543,7 +600,7 @@ int gpiochip_add_data(struct gpio_chip *chip, void *data)
543
600
if (base < 0 ) {
544
601
status = base ;
545
602
spin_unlock_irqrestore (& gpio_lock , flags );
546
- goto err_free_gdev ;
603
+ goto err_free_label ;
547
604
}
548
605
/*
549
606
* TODO: it should not be necessary to reflect the assigned
@@ -558,7 +615,7 @@ int gpiochip_add_data(struct gpio_chip *chip, void *data)
558
615
status = gpiodev_add_to_list (gdev );
559
616
if (status ) {
560
617
spin_unlock_irqrestore (& gpio_lock , flags );
561
- goto err_free_gdev ;
618
+ goto err_free_label ;
562
619
}
563
620
564
621
for (i = 0 ; i < chip -> ngpio ; i ++ ) {
@@ -596,39 +653,16 @@ int gpiochip_add_data(struct gpio_chip *chip, void *data)
596
653
* we get a device node entry in sysfs under
597
654
* /sys/bus/gpio/devices/gpiochipN/dev that can be used for
598
655
* coldplug of device nodes and other udev business.
656
+ * We can do this only if gpiolib has been initialized.
657
+ * Otherwise, defer until later.
599
658
*/
600
- cdev_init (& gdev -> chrdev , & gpio_fileops );
601
- gdev -> chrdev .owner = THIS_MODULE ;
602
- gdev -> chrdev .kobj .parent = & gdev -> dev .kobj ;
603
- gdev -> dev .devt = MKDEV (MAJOR (gpio_devt ), gdev -> id );
604
- status = cdev_add (& gdev -> chrdev , gdev -> dev .devt , 1 );
605
- if (status < 0 )
606
- chip_warn (chip , "failed to add char device %d:%d\n" ,
607
- MAJOR (gpio_devt ), gdev -> id );
608
- else
609
- chip_dbg (chip , "added GPIO chardev (%d:%d)\n" ,
610
- MAJOR (gpio_devt ), gdev -> id );
611
- status = device_add (& gdev -> dev );
612
- if (status )
613
- goto err_remove_chardev ;
614
-
615
- status = gpiochip_sysfs_register (gdev );
616
- if (status )
617
- goto err_remove_device ;
618
-
619
- /* From this point, the .release() function cleans up gpio_device */
620
- gdev -> dev .release = gpiodevice_release ;
621
- get_device (& gdev -> dev );
622
- pr_debug ("%s: registered GPIOs %d to %d on device: %s (%s)\n" ,
623
- __func__ , gdev -> base , gdev -> base + gdev -> ngpio - 1 ,
624
- dev_name (& gdev -> dev ), chip -> label ? : "generic" );
625
-
659
+ if (gpiolib_initialized ) {
660
+ status = gpiochip_setup_dev (gdev );
661
+ if (status )
662
+ goto err_remove_chip ;
663
+ }
626
664
return 0 ;
627
665
628
- err_remove_device :
629
- device_del (& gdev -> dev );
630
- err_remove_chardev :
631
- cdev_del (& gdev -> chrdev );
632
666
err_remove_chip :
633
667
acpi_gpiochip_remove (chip );
634
668
gpiochip_free_hogs (chip );
@@ -637,6 +671,10 @@ int gpiochip_add_data(struct gpio_chip *chip, void *data)
637
671
spin_lock_irqsave (& gpio_lock , flags );
638
672
list_del (& gdev -> list );
639
673
spin_unlock_irqrestore (& gpio_lock , flags );
674
+ err_free_label :
675
+ kfree (gdev -> label );
676
+ err_free_descs :
677
+ kfree (gdev -> descs );
640
678
err_free_gdev :
641
679
ida_simple_remove (& gpio_ida , gdev -> id );
642
680
/* failures here can mean systems won't boot... */
@@ -2231,9 +2269,11 @@ static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
2231
2269
return desc ;
2232
2270
}
2233
2271
2234
- static struct gpio_desc * acpi_find_gpio (struct device * dev , const char * con_id ,
2272
+ static struct gpio_desc * acpi_find_gpio (struct device * dev ,
2273
+ const char * con_id ,
2235
2274
unsigned int idx ,
2236
- enum gpio_lookup_flags * flags )
2275
+ enum gpiod_flags flags ,
2276
+ enum gpio_lookup_flags * lookupflags )
2237
2277
{
2238
2278
struct acpi_device * adev = ACPI_COMPANION (dev );
2239
2279
struct acpi_gpio_info info ;
@@ -2264,10 +2304,16 @@ static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id,
2264
2304
desc = acpi_get_gpiod_by_index (adev , NULL , idx , & info );
2265
2305
if (IS_ERR (desc ))
2266
2306
return desc ;
2307
+
2308
+ if ((flags == GPIOD_OUT_LOW || flags == GPIOD_OUT_HIGH ) &&
2309
+ info .gpioint ) {
2310
+ dev_dbg (dev , "refusing GpioInt() entry when doing GPIOD_OUT_* lookup\n" );
2311
+ return ERR_PTR (- ENOENT );
2312
+ }
2267
2313
}
2268
2314
2269
2315
if (info .polarity == GPIO_ACTIVE_LOW )
2270
- * flags |= GPIO_ACTIVE_LOW ;
2316
+ * lookupflags |= GPIO_ACTIVE_LOW ;
2271
2317
2272
2318
return desc ;
2273
2319
}
@@ -2530,7 +2576,7 @@ struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
2530
2576
desc = of_find_gpio (dev , con_id , idx , & lookupflags );
2531
2577
} else if (ACPI_COMPANION (dev )) {
2532
2578
dev_dbg (dev , "using ACPI for GPIO lookup\n" );
2533
- desc = acpi_find_gpio (dev , con_id , idx , & lookupflags );
2579
+ desc = acpi_find_gpio (dev , con_id , idx , flags , & lookupflags );
2534
2580
}
2535
2581
}
2536
2582
@@ -2829,6 +2875,9 @@ static int __init gpiolib_dev_init(void)
2829
2875
if (ret < 0 ) {
2830
2876
pr_err ("gpiolib: failed to allocate char dev region\n" );
2831
2877
bus_unregister (& gpio_bus_type );
2878
+ } else {
2879
+ gpiolib_initialized = true;
2880
+ gpiochip_setup_devs ();
2832
2881
}
2833
2882
return ret ;
2834
2883
}
0 commit comments