Skip to contentSkip to conversation

gpio: acpi: Add managed variant of acpi_dev_add_driver_gpios()

PATCH In review Andy Shevchenko 3 participants 4 messages Mar 2, 2017 – Mar 14, 2017

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 gpio: acpi: Add managed variant of acpi_dev_add_driver_gpios()

Introduce device managed variant of acpi_dev_add_driver_gpios() and its counterpart acpi_dev_remove_driver_gpios().

The functions in most cases are used in driver's ->probe() and ->remove() callbacks, that's why it's useful to have managed variant of them.

---

This is supposed to go through GPIO tree.

Linus, can you create an immutable branch that other subsystems can use it if needed?

(We currently have one possible leak and devm_*() would help there)

 drivers/gpio/gpiolib-acpi.c | 31 +++++++++++++++++++++++++++++++
 include/linux/acpi.h        | 11 +++++++++++
 2 files changed, 42 insertions(+)
drivers/gpio/gpiolib-acpi.c +31 -0
index 1459e6183194..3fe63d8ed426 100644
@@ -362,6 +362,37 @@ int acpi_dev_add_driver_gpios(struct acpi_device *adev,
 }
 EXPORT_SYMBOL_GPL(acpi_dev_add_driver_gpios);
 
+static void devm_acpi_dev_release_driver_gpios(struct device *dev, void *res)
+{
+	acpi_dev_remove_driver_gpios(ACPI_COMPANION(dev));
+}
+
+int devm_acpi_dev_add_driver_gpios(struct device *dev,
+				   const struct acpi_gpio_mapping *gpios)
+{
+	void *res;
+	int ret;
+
+	res = devres_alloc(devm_acpi_dev_release_driver_gpios, 0, GFP_KERNEL);
+	if (!res)
+		return -ENOMEM;
+
+	ret = acpi_dev_add_driver_gpios(ACPI_COMPANION(dev), gpios);
+	if (ret) {
+		devres_free(res);
+		return ret;
+	}
+	devres_add(dev, res);
+	return 0;
+}
+EXPORT_SYMBOL_GPL(devm_acpi_dev_add_driver_gpios);
+
+void devm_acpi_dev_remove_driver_gpios(struct device *dev)
+{
+	WARN_ON(devres_release(dev, devm_acpi_dev_release_driver_gpios, NULL, NULL));
+}
+EXPORT_SYMBOL_GPL(devm_acpi_dev_remove_driver_gpios);
+
 static bool acpi_get_driver_gpio_data(struct acpi_device *adev,
 				      const char *name, int index,
 				      struct acpi_reference_args *args)
include/linux/acpi.h +11 -0
index 673acda012af..c8eaaad4a9ed 100644
@@ -952,6 +952,10 @@ static inline void acpi_dev_remove_driver_gpios(struct acpi_device *adev)
 		adev->driver_gpios = NULL;
 }
 
+int devm_acpi_dev_add_driver_gpios(struct device *dev,
+				   const struct acpi_gpio_mapping *gpios);
+void devm_acpi_dev_remove_driver_gpios(struct device *dev);
+
 int acpi_dev_gpio_irq_get(struct acpi_device *adev, int index);
 #else
 static inline int acpi_dev_add_driver_gpios(struct acpi_device *adev,
@@ -961,6 +965,13 @@ static inline int acpi_dev_add_driver_gpios(struct acpi_device *adev,
 }
 static inline void acpi_dev_remove_driver_gpios(struct acpi_device *adev) {}
 
+static inline int devm_acpi_dev_add_driver_gpios(struct device *dev,
+			      const struct acpi_gpio_mapping *gpios)
+{
+	return -ENXIO;
+}
+static inline void devm_acpi_dev_remove_driver_gpios(struct device *dev) {}
+
 static inline int acpi_dev_gpio_irq_get(struct acpi_device *adev, int index)
 {
 	return -ENXIO;
Signature · 1 line
2.11.0
#1 lore raw
Quoted 83 lines · Andy Shevchenko

Introduce device managed variant of acpi_dev_add_driver_gpios() and its counterpart acpi_dev_remove_driver_gpios().

The functions in most cases are used in driver's ->probe() and ->remove() callbacks, that's why it's useful to have managed variant of them.

---

This is supposed to go through GPIO tree.

Linus, can you create an immutable branch that other subsystems can use it if needed?

(We currently have one possible leak and devm_*() would help there)

 drivers/gpio/gpiolib-acpi.c | 31 +++++++++++++++++++++++++++++++
 include/linux/acpi.h        | 11 +++++++++++
 2 files changed, 42 insertions(+)
drivers/gpio/gpiolib-acpi.c +31 -0
index 1459e6183194..3fe63d8ed426 100644
@@ -362,6 +362,37 @@ int acpi_dev_add_driver_gpios(struct acpi_device *adev,
 }
 EXPORT_SYMBOL_GPL(acpi_dev_add_driver_gpios);
 
+static void devm_acpi_dev_release_driver_gpios(struct device *dev, void *res)
+{
+	acpi_dev_remove_driver_gpios(ACPI_COMPANION(dev));
+}
+
+int devm_acpi_dev_add_driver_gpios(struct device *dev,
+				   const struct acpi_gpio_mapping *gpios)
+{
+	void *res;
+	int ret;
+
+	res = devres_alloc(devm_acpi_dev_release_driver_gpios, 0, GFP_KERNEL);
+	if (!res)
+		return -ENOMEM;
+
+	ret = acpi_dev_add_driver_gpios(ACPI_COMPANION(dev), gpios);
+	if (ret) {
+		devres_free(res);
+		return ret;
+	}
+	devres_add(dev, res);
+	return 0;
+}
+EXPORT_SYMBOL_GPL(devm_acpi_dev_add_driver_gpios);
+
+void devm_acpi_dev_remove_driver_gpios(struct device *dev)
+{
+	WARN_ON(devres_release(dev, devm_acpi_dev_release_driver_gpios, NULL, NULL));
+}
+EXPORT_SYMBOL_GPL(devm_acpi_dev_remove_driver_gpios);
+
 static bool acpi_get_driver_gpio_data(struct acpi_device *adev,
 				      const char *name, int index,
 				      struct acpi_reference_args *args)
include/linux/acpi.h +4 -0
index 673acda012af..c8eaaad4a9ed 100644
@@ -952,6 +952,10 @@ static inline void acpi_dev_remove_driver_gpios(struct acpi_device *adev)
 		adev->driver_gpios = NULL;
 }
 
+int devm_acpi_dev_add_driver_gpios(struct device *dev,
+				   const struct acpi_gpio_mapping *gpios);
+void devm_acpi_dev_remove_driver_gpios(struct device *dev);
+
 int acpi_dev_gpio_irq_get(struct acpi_device *adev, int index);
 #else
 static inline int acpi_dev_add_driver_gpios(struct acpi_device *adev,
@@ -961,6 +965,13 @@ static inline int acpi_dev_add_driver_gpios(struct acpi_device *adev,
 }
 static inline void acpi_dev_remove_driver_gpios(struct acpi_device *adev) {}
 

include/linux/acpi.h static inline int acpi_dev_add_driver_gpios(struct acpi_device *adev, · line 977

Quoted 10 lines
include/linux/acpi.h +7 -0
+static inline int devm_acpi_dev_add_driver_gpios(struct device *dev,
+			      const struct acpi_gpio_mapping *gpios)
+{
+	return -ENXIO;
+}
+static inline void devm_acpi_dev_remove_driver_gpios(struct device *dev) {}
+
 static inline int acpi_dev_gpio_irq_get(struct acpi_device *adev, int index)
 {
 	return -ENXIO;

ACK for the ACPI header update.

Thanks,
Rafael

#2 lore raw
In reply to Andy Shevchenko
Quoted 7 lines · Andy Shevchenko

Introduce device managed variant of acpi_dev_add_driver_gpios() and its counterpart acpi_dev_remove_driver_gpios().

The functions in most cases are used in driver's ->probe() and ->remove() callbacks, that's why it's useful to have managed variant of them.

Quoted 7 lines

---

This is supposed to go through GPIO tree.

Linus, can you create an immutable branch that other subsystems can use it if needed?

I have created ib-acpi-devm in the GPIO tree based on v4.11-rc1, applied this patch and merged that into my devel branch.

Anyone who needs it can grab that branch, I will push it out later.

Yours,
Linus Walleij

#3 lore raw
Quoted 21 lines · Linus Walleij
Quoted 9 lines · Andy Shevchenko

Introduce device managed variant of acpi_dev_add_driver_gpios() and its
counterpart acpi_dev_remove_driver_gpios().

The functions in most cases are used in driver's ->probe() and ->remove() callbacks, that's why it's useful to have managed variant of
them.

Quoted 8 lines

---

This is supposed to go through GPIO tree.

Linus, can you create an immutable branch that other subsystems can use
it if needed?

Quoted 5 lines

I have created ib-acpi-devm in the GPIO tree based on v4.11-rc1, applied
this patch and merged that into my devel branch.

Anyone who needs it can grab that branch, I will push it out later.

Thanks!

Signature · 2 lines
Andy Shevchenko <[email protected]>
Intel Finland Oy