linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 2/4] regulator-core: manage enable GPIO list
@ 2013-01-15  4:35 Kim, Milo
  2013-01-27  6:06 ` Mark Brown
  0 siblings, 1 reply; 2+ messages in thread
From: Kim, Milo @ 2013-01-15  4:35 UTC (permalink / raw)
  To: Mark Brown; +Cc: Axel Lin, Girdwood, Liam, linux-kernel

 To support shared enable GPIO pin, replace GPIO code with new static functions

 regulator_ena_gpio_ctrl()
   Find a GPIO information in the enable GPIO list and control the pin.

 _do_ena_gpio_ctrl()
   Balance the reference count of each GPIO and actual pin control.
   The count is incremented with enabling GPIO.
   On the other hand, it is decremented on disabling GPIO.
   Actual GPIO pin is enabled at the initial use.(enable_count = 0)
   The pin is disabled if it is not used(shared) any more. (enable_count = 1)
   This concept is derived from the 'use_count' of regulator.
   Regardless of the enable count, update GPIO state of the regulator.

Signed-off-by: Milo(Woogyom) Kim <milo.kim@ti.com>
---
 drivers/regulator/core.c |   54 ++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 47 insertions(+), 7 deletions(-)

diff --git a/drivers/regulator/core.c b/drivers/regulator/core.c
index 1273090..dc713c4 100644
--- a/drivers/regulator/core.c
+++ b/drivers/regulator/core.c
@@ -1501,6 +1501,51 @@ static int regulator_ena_gpio_request(struct regulator_dev *rdev,
 	return 0;
 }
 
+/**
+ * Balance enable_count of each GPIO and actual GPIO pin control.
+ * GPIO is enabled in case of initial use. (enable_count is 0)
+ * GPIO is disabled when it is not shared any more. (enable_count is 1)
+ */
+static void _do_ena_gpio_ctrl(struct regulator_enable_gpio *pin,
+			struct regulator_dev *rdev, bool enable)
+{
+	if (enable) {
+		/* Enable GPIO at initial use */
+		if (pin->enable_count == 0)
+			gpio_set_value_cansleep(rdev->ena_gpio,
+						!rdev->ena_gpio_invert);
+
+		rdev->ena_gpio_state = 1;
+		pin->enable_count++;
+
+	} else {
+		rdev->ena_gpio_state = 0;
+		if (pin->enable_count > 1) {
+			pin->enable_count--;
+			return;
+		}
+
+		/* Disable GPIO if not used */
+		if (pin->enable_count == 1) {
+			gpio_set_value_cansleep(rdev->ena_gpio,
+						rdev->ena_gpio_invert);
+			pin->enable_count = 0;
+		}
+	}
+}
+
+static void regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)
+{
+	struct regulator_enable_gpio *pin;
+
+	list_for_each_entry(pin, &regulator_ena_gpio_list, list) {
+		if (pin->gpio == rdev->ena_gpio) {
+			_do_ena_gpio_ctrl(pin, rdev, enable);
+			return;
+		}
+	}
+}
+
 static int _regulator_do_enable(struct regulator_dev *rdev)
 {
 	int ret, delay;
@@ -1517,9 +1562,7 @@ static int _regulator_do_enable(struct regulator_dev *rdev)
 	trace_regulator_enable(rdev_get_name(rdev));
 
 	if (rdev->ena_gpio) {
-		gpio_set_value_cansleep(rdev->ena_gpio,
-					!rdev->ena_gpio_invert);
-		rdev->ena_gpio_state = 1;
+		regulator_ena_gpio_ctrl(rdev, true);
 	} else if (rdev->desc->ops->enable) {
 		ret = rdev->desc->ops->enable(rdev);
 		if (ret < 0)
@@ -1621,10 +1664,7 @@ static int _regulator_do_disable(struct regulator_dev *rdev)
 	trace_regulator_disable(rdev_get_name(rdev));
 
 	if (rdev->ena_gpio) {
-		gpio_set_value_cansleep(rdev->ena_gpio,
-					rdev->ena_gpio_invert);
-		rdev->ena_gpio_state = 0;
-
+		regulator_ena_gpio_ctrl(rdev, false);
 	} else if (rdev->desc->ops->disable) {
 		ret = rdev->desc->ops->disable(rdev);
 		if (ret != 0)
-- 
1.7.9.5


Best Regards,
Milo



^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v2 2/4] regulator-core: manage enable GPIO list
  2013-01-15  4:35 [PATCH v2 2/4] regulator-core: manage enable GPIO list Kim, Milo
@ 2013-01-27  6:06 ` Mark Brown
  0 siblings, 0 replies; 2+ messages in thread
From: Mark Brown @ 2013-01-27  6:06 UTC (permalink / raw)
  To: Kim, Milo; +Cc: Axel Lin, Girdwood, Liam, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 1865 bytes --]

On Tue, Jan 15, 2013 at 04:35:46AM +0000, Kim, Milo wrote:

> +/**
> + * Balance enable_count of each GPIO and actual GPIO pin control.
> + * GPIO is enabled in case of initial use. (enable_count is 0)
> + * GPIO is disabled when it is not shared any more. (enable_count is 1)
> + */
> +static void _do_ena_gpio_ctrl(struct regulator_enable_gpio *pin,
> +			struct regulator_dev *rdev, bool enable)
> +{
> +	if (enable) {
> +		/* Enable GPIO at initial use */
> +		if (pin->enable_count == 0)
> +			gpio_set_value_cansleep(rdev->ena_gpio,
> +						!rdev->ena_gpio_invert);
> +
> +		rdev->ena_gpio_state = 1;

ena_gpio_state is redundant with this patch, we can just replace
references to it with pin->enable_count.

We'll also need a request count to keep track of how many regulators are
using the GPIO for use in cleanup.

> +	} else {
> +		rdev->ena_gpio_state = 0;
> +		if (pin->enable_count > 1) {
> +			pin->enable_count--;
> +			return;
> +		}
> +
> +		/* Disable GPIO if not used */
> +		if (pin->enable_count == 1) {
> +			gpio_set_value_cansleep(rdev->ena_gpio,
> +						rdev->ena_gpio_invert);
> +			pin->enable_count = 0;
> +		}

Ideally we should also check if we're trying to take the enable count
below zero and complain about that.

> +static void regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)
> +{
> +	struct regulator_enable_gpio *pin;
> +
> +	list_for_each_entry(pin, &regulator_ena_gpio_list, list) {
> +		if (pin->gpio == rdev->ena_gpio) {
> +			_do_ena_gpio_ctrl(pin, rdev, enable);
> +			return;
> +		}
> +	}
> +}

This should return an error code as the users return errors, the GPIO
API won't give us errors but we can generate them internally.  It'd be
better to just add a pointer to the GPIO struct to the regulator_dev (in
place of the GPIO) so we don't need to scan through the list every time
we look for the GPIO.

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 836 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2013-01-27  6:06 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-01-15  4:35 [PATCH v2 2/4] regulator-core: manage enable GPIO list Kim, Milo
2013-01-27  6:06 ` Mark Brown

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).