Re: [PATCH v2 2/4] regulator-core: manage enable GPIO list

2013-01-26 Thread Mark Brown
On Tue, Jan 15, 2013 at 04:35:46AM +, 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, _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.


signature.asc
Description: Digital signature


Re: [PATCH v2 2/4] regulator-core: manage enable GPIO list

2013-01-26 Thread Mark Brown
On Tue, Jan 15, 2013 at 04:35:46AM +, 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.


signature.asc
Description: Digital signature


[PATCH v2 2/4] regulator-core: manage enable GPIO list

2013-01-14 Thread Kim, Milo
 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 
---
 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, _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


--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


[PATCH v2 2/4] regulator-core: manage enable GPIO list

2013-01-14 Thread Kim, Milo
 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@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


--
To unsubscribe from this list: send the line unsubscribe linux-kernel in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/