Commit 6a4b6b0a authored by Johan Hovold's avatar Johan Hovold Committed by Linus Walleij

gpio: sysfs: clean up chip class-device handling

Clean gpio-chip class device registration and deregistration.

The class device is registered when a gpio-chip is added (or from
gpiolib_sysfs_init post-core init call), and deregistered when the chip
is removed.

Store the class device in struct gpio_chip directly rather than do a
class-device lookup on deregistration. This also removes the need for
the exported flag.
Signed-off-by: default avatarJohan Hovold <johan@kernel.org>
Reviewed-by: default avatarAlexandre Courbot <acourbot@nvidia.com>
Signed-off-by: default avatarLinus Walleij <linus.walleij@linaro.org>
parent 3ff74be5
...@@ -582,7 +582,7 @@ int gpiod_export(struct gpio_desc *desc, bool direction_may_change) ...@@ -582,7 +582,7 @@ int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
mutex_lock(&sysfs_lock); mutex_lock(&sysfs_lock);
/* check if chip is being removed */ /* check if chip is being removed */
if (!chip || !chip->exported) { if (!chip || !chip->cdev) {
status = -ENODEV; status = -ENODEV;
goto fail_unlock; goto fail_unlock;
} }
...@@ -767,7 +767,6 @@ EXPORT_SYMBOL_GPL(gpiod_unexport); ...@@ -767,7 +767,6 @@ EXPORT_SYMBOL_GPL(gpiod_unexport);
int gpiochip_export(struct gpio_chip *chip) int gpiochip_export(struct gpio_chip *chip)
{ {
int status;
struct device *dev; struct device *dev;
/* Many systems register gpio chips for SOC support very early, /* Many systems register gpio chips for SOC support very early,
...@@ -783,41 +782,29 @@ int gpiochip_export(struct gpio_chip *chip) ...@@ -783,41 +782,29 @@ int gpiochip_export(struct gpio_chip *chip)
chip, gpiochip_groups, chip, gpiochip_groups,
"gpiochip%d", chip->base); "gpiochip%d", chip->base);
if (IS_ERR(dev)) if (IS_ERR(dev))
status = PTR_ERR(dev); return PTR_ERR(dev);
else
status = 0;
mutex_lock(&sysfs_lock); mutex_lock(&sysfs_lock);
chip->exported = (status == 0); chip->cdev = dev;
mutex_unlock(&sysfs_lock); mutex_unlock(&sysfs_lock);
if (status) return 0;
chip_dbg(chip, "%s: status %d\n", __func__, status);
return status;
} }
void gpiochip_unexport(struct gpio_chip *chip) void gpiochip_unexport(struct gpio_chip *chip)
{ {
int status;
struct device *dev;
struct gpio_desc *desc; struct gpio_desc *desc;
unsigned int i; unsigned int i;
dev = class_find_device(&gpio_class, NULL, chip, match_export); if (!chip->cdev)
if (dev) { return;
put_device(dev);
device_unregister(dev);
/* prevent further gpiod exports */
mutex_lock(&sysfs_lock);
chip->exported = false;
mutex_unlock(&sysfs_lock);
status = 0;
} else
status = -ENODEV;
if (status) device_unregister(chip->cdev);
chip_dbg(chip, "%s: status %d\n", __func__, status);
/* prevent further gpiod exports */
mutex_lock(&sysfs_lock);
chip->cdev = NULL;
mutex_unlock(&sysfs_lock);
/* unregister gpiod class devices owned by sysfs */ /* unregister gpiod class devices owned by sysfs */
for (i = 0; i < chip->ngpio; i++) { for (i = 0; i < chip->ngpio; i++) {
...@@ -845,7 +832,7 @@ static int __init gpiolib_sysfs_init(void) ...@@ -845,7 +832,7 @@ static int __init gpiolib_sysfs_init(void)
*/ */
spin_lock_irqsave(&gpio_lock, flags); spin_lock_irqsave(&gpio_lock, flags);
list_for_each_entry(chip, &gpio_chips, list) { list_for_each_entry(chip, &gpio_chips, list) {
if (chip->exported) if (chip->cdev)
continue; continue;
/* /*
......
...@@ -20,6 +20,7 @@ struct seq_file; ...@@ -20,6 +20,7 @@ struct seq_file;
* struct gpio_chip - abstract a GPIO controller * struct gpio_chip - abstract a GPIO controller
* @label: for diagnostics * @label: for diagnostics
* @dev: optional device providing the GPIOs * @dev: optional device providing the GPIOs
* @cdev: class device used by sysfs interface (may be NULL)
* @owner: helps prevent removal of modules exporting active GPIOs * @owner: helps prevent removal of modules exporting active GPIOs
* @list: links gpio_chips together for traversal * @list: links gpio_chips together for traversal
* @request: optional hook for chip-specific activation, such as * @request: optional hook for chip-specific activation, such as
...@@ -57,7 +58,6 @@ struct seq_file; ...@@ -57,7 +58,6 @@ struct seq_file;
* implies that if the chip supports IRQs, these IRQs need to be threaded * implies that if the chip supports IRQs, these IRQs need to be threaded
* as the chip access may sleep when e.g. reading out the IRQ status * as the chip access may sleep when e.g. reading out the IRQ status
* registers. * registers.
* @exported: flags if the gpiochip is exported for use from sysfs. Private.
* @irq_not_threaded: flag must be set if @can_sleep is set but the * @irq_not_threaded: flag must be set if @can_sleep is set but the
* IRQs don't need to be threaded * IRQs don't need to be threaded
* *
...@@ -74,6 +74,7 @@ struct seq_file; ...@@ -74,6 +74,7 @@ struct seq_file;
struct gpio_chip { struct gpio_chip {
const char *label; const char *label;
struct device *dev; struct device *dev;
struct device *cdev;
struct module *owner; struct module *owner;
struct list_head list; struct list_head list;
...@@ -109,7 +110,6 @@ struct gpio_chip { ...@@ -109,7 +110,6 @@ struct gpio_chip {
const char *const *names; const char *const *names;
bool can_sleep; bool can_sleep;
bool irq_not_threaded; bool irq_not_threaded;
bool exported;
#ifdef CONFIG_GPIOLIB_IRQCHIP #ifdef CONFIG_GPIOLIB_IRQCHIP
/* /*
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment