Skip to contentSkip to conversation

pwm: add devm_pwm_get() and devm_pwm_put()

PATCH Superseded Alexandre Courbot 2 participants 4 messages Aug 1, 2012

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 pwm: add devm_pwm_get() and devm_pwm_put()

Add resource managed variants of pwm_get() and pwm_put() for convenience. Code is largely inspired by the equivalent devm functions of the regulator framework.

---
 Documentation/driver-model/devres.txt |  4 +++
 Documentation/pwm.txt                 |  5 +--
 drivers/pwm/core.c                    | 57 +++++++++++++++++++++++++++++++++++
 include/linux/pwm.h                   |  3 ++
 4 files changed, 67 insertions(+), 2 deletions(-)
Documentation/driver-model/devres.txt +4 -0
index 950856b..43cff70 100644
@@ -284,3 +284,7 @@ CLOCK
 PINCTRL
   devm_pinctrl_get()
   devm_pinctrl_put()
+
+PWM
+  devm_pwm_get()
+  devm_pwm_put()
Documentation/pwm.txt +3 -2
index 554290e..4719c12 100644
@@ -35,8 +35,9 @@ Using PWMs
 Legacy users can request a PWM device using pwm_request() and free it
 after usage with pwm_free().
 
-New users should use the pwm_get() function and pass to it the consumer
-device or a consumer name. pwm_put() is used to free the PWM device.
+New users should use the pwm_get() or devm_pwm_get() function and pass to it the
+consumer device or a consumer name. pwm_put() or devm_pwm_put() is used to free
+the PWM device.
 
 After being requested a PWM has to be configured using:
 
drivers/pwm/core.c +57 -0

63 lines not on this page · raw message

include/linux/pwm.h +3 -0
index 21d076c..af9c39a 100644
@@ -123,7 +123,10 @@ struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
 					 const char *label);
 
 struct pwm_device *pwm_get(struct device *dev, const char *consumer);
+struct pwm_device *devm_pwm_get(struct device *dev, const char *consumer);
+
 void pwm_put(struct pwm_device *pwm);
+void devm_pwm_put(struct device *dev, struct pwm_device *pwm);
 
 struct pwm_lookup {
 	struct list_head list;
Signature · 1 line
1.7.11.3
#1 lore raw
Quoted 29 lines · Alexandre Courbot

Add resource managed variants of pwm_get() and pwm_put() for convenience. Code is largely inspired by the equivalent devm functions of the regulator framework.

---
 Documentation/driver-model/devres.txt |  4 +++
 Documentation/pwm.txt                 |  5 +--
 drivers/pwm/core.c                    | 57 +++++++++++++++++++++++++++++++++++
 include/linux/pwm.h                   |  3 ++
 4 files changed, 67 insertions(+), 2 deletions(-)
Documentation/driver-model/devres.txt +4 -0
index 950856b..43cff70 100644
@@ -284,3 +284,7 @@ CLOCK
 PINCTRL
   devm_pinctrl_get()
   devm_pinctrl_put()
+
+PWM
+  devm_pwm_get()
+  devm_pwm_put()
Documentation/pwm.txt +0 -0
index 554290e..4719c12 100644

Documentation/pwm.txt Using PWMs · line 40

Quoted 8 lines
Documentation/pwm.txt +3 -2
@@ -35,8 +35,9 @@ Using PWMs
 Legacy users can request a PWM device using pwm_request() and free it
 after usage with pwm_free().
 
-New users should use the pwm_get() function and pass to it the consumer
-device or a consumer name. pwm_put() is used to free the PWM device.
+New users should use the pwm_get() or devm_pwm_get() function and pass to it the
+consumer device or a consumer name. pwm_put() or devm_pwm_put() is used to free
+the PWM device.

I think I'd prefer the original text with an additional line mentioning the managed variants.

Quoted 8 lines
Documentation/pwm.txt +0 -0
 After being requested a PWM has to be configured using:
 
drivers/pwm/core.c +0 -0
index ecb7690..5a5e19b 100644

drivers/pwm/core.c out: · line 633

Quoted 10 lines
drivers/pwm/core.c +7 -0
@@ -624,6 +624,63 @@ out:
 }
 EXPORT_SYMBOL_GPL(pwm_put);
 
+static void devm_pwm_release(struct device *dev, void *res)
+{
+	pwm_put(*(struct pwm_device **)res);
+}
+
+/**
+ * devm_pwm_get() - Resource managed pwm_get()

This is missing parameter descriptions here.

drivers/pwm/core.c out: · line 635

Quoted 2 lines
drivers/pwm/core.c +2 -0
+ *
+ * This works are pwm_get() but the acquired pwm will automatically be released

"works like"? Also "acquired PWM" or "acquired PWM device".

Quoted 29 lines
drivers/pwm/core.c +29 -0
+ * on driver detach. See pwm_get() for more details.
+ */
+struct pwm_device *devm_pwm_get(struct device *dev, const char *consumer)
+{
+	struct pwm_device **ptr, *pwm;
+
+	ptr = devres_alloc(devm_pwm_release, sizeof(**ptr), GFP_KERNEL);
+	if (!ptr)
+		return ERR_PTR(-ENOMEM);
+
+	pwm = pwm_get(dev, consumer);
+	if (!IS_ERR(pwm)) {
+		*ptr = pwm;
+		devres_add(dev, ptr);
+	} else {
+		devres_free(ptr);
+	}
+
+	return pwm;
+}
+EXPORT_SYMBOL_GPL(devm_pwm_get);
+
+static int devm_pwm_match(struct device *dev, void *res, void *data)
+{
+	struct pwm_device **p = res;
+
+	if (WARN_ON(!p || !*p))
+		return 0;
+

drivers/pwm/core.c out: · line 674

Quoted 10 lines
drivers/pwm/core.c +10 -0
+	return *p == data;
+}
+
+/**
+ * devm_pwm_put() - Resource managed pwm_put()
+ *
+ * Releases a pwm previously allocated using devm_pwm_get. Calling this function
+ * is usually not needed as the devm-allocated pwm will automatically be
+ * released on driver detach.
+ */

Same comments as for devm_pwm_get().

Quoted 17 lines
drivers/pwm/core.c +9 -0
+void devm_pwm_put(struct device *dev, struct pwm_device *pwm)
+{
+	int ret;
+
+	ret = devres_release(dev, devm_pwm_release, devm_pwm_match, pwm);
+	WARN_ON(ret);
+}
+EXPORT_SYMBOL_GPL(devm_pwm_put);
+
 #ifdef CONFIG_DEBUG_FS
 static void pwm_dbg_show(struct pwm_chip *chip, struct seq_file *s)
 {
include/linux/pwm.h +0 -0
index 21d076c..af9c39a 100644

include/linux/pwm.h struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip, · line 129

Quoted 7 lines
include/linux/pwm.h +3 -0
@@ -123,7 +123,10 @@ struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
 					 const char *label);
 
 struct pwm_device *pwm_get(struct device *dev, const char *consumer);
+struct pwm_device *devm_pwm_get(struct device *dev, const char *consumer);
+
 void pwm_put(struct pwm_device *pwm);
+void devm_pwm_put(struct device *dev, struct pwm_device *pwm);

Can the managed variants be grouped together, please?

Looks good otherwise. Thanks for taking care of it.

Thierry

#2 lore raw
Quoted 55 lines · Thierry Reding

* PGP Signed by an unknown key

Quoted 29 lines · Alexandre Courbot

Add resource managed variants of pwm_get() and pwm_put() for convenience. Code is largely inspired by the equivalent devm functions of the regulator framework.

---
  Documentation/driver-model/devres.txt |  4 +++
  Documentation/pwm.txt                 |  5 +--
  drivers/pwm/core.c                    | 57 +++++++++++++++++++++++++++++++++++
  include/linux/pwm.h                   |  3 ++
  4 files changed, 67 insertions(+), 2 deletions(-)
Documentation/driver-model/devres.txt +4 -0
index 950856b..43cff70 100644
@@ -284,3 +284,7 @@ CLOCK
  PINCTRL
    devm_pinctrl_get()
    devm_pinctrl_put()
+
+PWM
+  devm_pwm_get()
+  devm_pwm_put()
Documentation/pwm.txt +0 -0
index 554290e..4719c12 100644

Documentation/pwm.txt Using PWMs · line 40

Quoted 8 lines
Documentation/pwm.txt +3 -2
@@ -35,8 +35,9 @@ Using PWMs
  Legacy users can request a PWM device using pwm_request() and free it
  after usage with pwm_free().
 
-New users should use the pwm_get() function and pass to it the consumer
-device or a consumer name. pwm_put() is used to free the PWM device.
+New users should use the pwm_get() or devm_pwm_get() function and pass to it the
+consumer device or a consumer name. pwm_put() or devm_pwm_put() is used to free
+the PWM device.

I think I'd prefer the original text with an additional line mentioning the managed variants.

Quoted 11 lines
Documentation/pwm.txt +0 -0
  After being requested a PWM has to be configured using:
 
drivers/pwm/core.c +0 -0
index ecb7690..5a5e19b 100644
@@ -624,6 +624,63 @@ out:
  }
  EXPORT_SYMBOL_GPL(pwm_put);
 
Quoted 9 lines

drivers/pwm/core.c out: · line 633

Quoted 7 lines
drivers/pwm/core.c +7 -0
+static void devm_pwm_release(struct device *dev, void *res)
+{
+	pwm_put(*(struct pwm_device **)res);
+}
+
+/**
+ * devm_pwm_get() - Resource managed pwm_get()

This is missing parameter descriptions here.

This is because the documentation for this function just refers to pwm_get(), and the parameters are the same. I think it's better to avoid duplicating documentation as much as possible as this makes two maintenance points instead of one.

Quoted 5 lines

drivers/pwm/core.c out: · line 635

Quoted 3 lines
drivers/pwm/core.c +2 -0
+ *
+ * This works are pwm_get() but the acquired pwm will automatically be released

"works like"? Also "acquired PWM" or "acquired PWM device".

Ooops, yes. I really should proof-read what I write. Twice.

Quoted 62 lines
Quoted 30 lines
drivers/pwm/core.c +29 -0
+ * on driver detach. See pwm_get() for more details.
+ */
+struct pwm_device *devm_pwm_get(struct device *dev, const char *consumer)
+{
+	struct pwm_device **ptr, *pwm;
+
+	ptr = devres_alloc(devm_pwm_release, sizeof(**ptr), GFP_KERNEL);
+	if (!ptr)
+		return ERR_PTR(-ENOMEM);
+
+	pwm = pwm_get(dev, consumer);
+	if (!IS_ERR(pwm)) {
+		*ptr = pwm;
+		devres_add(dev, ptr);
+	} else {
+		devres_free(ptr);
+	}
+
+	return pwm;
+}
+EXPORT_SYMBOL_GPL(devm_pwm_get);
+
+static int devm_pwm_match(struct device *dev, void *res, void *data)
+{
+	struct pwm_device **p = res;
+
+	if (WARN_ON(!p || !*p))
+		return 0;
+

drivers/pwm/core.c out: · line 674

Quoted 10 lines
drivers/pwm/core.c +10 -0
+	return *p == data;
+}
+
+/**
+ * devm_pwm_put() - Resource managed pwm_put()
+ *
+ * Releases a pwm previously allocated using devm_pwm_get. Calling this function
+ * is usually not needed as the devm-allocated pwm will automatically be
+ * released on driver detach.
+ */

Same comments as for devm_pwm_get().

Quoted 19 lines
drivers/pwm/core.c +9 -0
+void devm_pwm_put(struct device *dev, struct pwm_device *pwm)
+{
+	int ret;
+
+	ret = devres_release(dev, devm_pwm_release, devm_pwm_match, pwm);
+	WARN_ON(ret);
+}
+EXPORT_SYMBOL_GPL(devm_pwm_put);
+
  #ifdef CONFIG_DEBUG_FS
  static void pwm_dbg_show(struct pwm_chip *chip, struct seq_file *s)
  {
include/linux/pwm.h +0 -0
index 21d076c..af9c39a 100644
@@ -123,7 +123,10 @@ struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
  					 const char *label);
 
Quoted 7 lines

include/linux/pwm.h struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip, · line 129

Quoted 5 lines
include/linux/pwm.h +3 -0
  struct pwm_device *pwm_get(struct device *dev, const char *consumer);
+struct pwm_device *devm_pwm_get(struct device *dev, const char *consumer);
+
  void pwm_put(struct pwm_device *pwm);
+void devm_pwm_put(struct device *dev, struct pwm_device *pwm);

Can the managed variants be grouped together, please?

Sure.

Thanks,
Alex.

#3 lore raw
Quoted 7 lines · Alex Courbot
Quoted 6 lines · Thierry Reding

* PGP Signed by an unknown key

Quoted 2 lines · Alexandre Courbot

+/**
+ * devm_pwm_get() - Resource managed pwm_get()

Quoted 6 lines
Quoted 1 line

This is missing parameter descriptions here.

This is because the documentation for this function just refers to pwm_get(), and the parameters are the same. I think it's better to avoid duplicating documentation as much as possible as this makes two maintenance points instead of one.

devm_pwm_get() and devm_pwm_put() aren't very likely to change. The problem is that they are public APIs and people could be running kerneldoc over the files to have documentation generated and the parameters would be missing in this case. We really don't have a lot of these public functions, but those that are should be properly documented.

Thierry