Skip to contentSkip to conversation

regulator: ad5398: Fix min/max current limit boundary checking

PATCH Superseded Axel Lin 3 participants 12 messages Jul 3, 2012 – Jul 4, 2012

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Axel Lin

author
#0 lore raw

PATCH v2 1/1 regulator: ad5398: Fix min/max current limit boundary checking

It is ok to request current limit with min_uA < chip->min_uA and max_uA > chip->max_uA.

We need to set min_uA = chip->min_uA if (min_uA < chip->min_uA), this ensures the equation to calcuate selator does not return negative number.

~

---
 drivers/regulator/ad5398.c |    7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/regulator/ad5398.c +4 -3
index 46d05f3..84fdcda 100644
@@ -89,9 +89,10 @@ static int ad5398_set_current_limit(struct regulator_dev *rdev, int min_uA, int
 	unsigned short data;
 	int ret;
 
-	if (min_uA > chip->max_uA || min_uA < chip->min_uA)
-		return -EINVAL;
-	if (max_uA > chip->max_uA || max_uA < chip->min_uA)
+	if (min_uA < chip->min_uA)
+		min_uA = chip->min_uA;
+
+	if (min_uA > chip->max_uA || max_uA < chip->min_uA)
 		return -EINVAL;
 
 	selector = DIV_ROUND_UP((min_uA - chip->min_uA) * chip->current_level,
Signature · 1 line
1.7.9.5
#1 lore raw
Quoted 11 lines

-----Original Message-----
From: Axel Lin [mailto:[email protected]]
Sent: Tuesday, July 03, 2012 3:43 PM
To: Mark Brown
Cc: [email protected]; Zhang, Sonic; Lars-Peter Clausen; Liam Girdwood
Subject: [PATCH v2] regulator: ad5398: Fix min/max current limit boundary checking

Quoted 6 lines

It is ok to request current limit with min_uA < chip->min_uA and max_uA > chip->max_uA.

We need to set min_uA = chip->min_uA if (min_uA < chip->min_uA), this ensures the equation to calcuate selator does not return negative number.

You should not do it in driver. Set a correct min_uA value in your application.

Regards,

Sonic

Quoted 25 lines

~
---
drivers/regulator/ad5398.c | 7 ++++---

1 file changed, 4 insertions(+), 3 deletions(-)
drivers/regulator/ad5398.c +4 -3
index 46d05f3..84fdcda 100644
@@ -89,9 +89,10 @@ static int ad5398_set_current_limit(struct regulator_dev
 *rdev, int min_uA, int
      unsigned short data;
      int ret;
 
-      if (min_uA > chip->max_uA || min_uA < chip->min_uA)
-              return -EINVAL;
-      if (max_uA > chip->max_uA || max_uA < chip->min_uA)
+      if (min_uA < chip->min_uA)
+              min_uA = chip->min_uA;
+
+      if (min_uA > chip->max_uA || max_uA < chip->min_uA)
              return -EINVAL;
 
      selector = DIV_ROUND_UP((min_uA - chip->min_uA) * chip->current_level,
Quoted 5 lines

--
1.7.9.5

���{.n�+�������+%�����ݶ��w��{.n�+����{��G�����{ay�ʇڙ�,j��f���h��������z_�(�階�ݢj"���m������G������?����&���~��iO��z��v�^�m���� ������?�I�

#2 lore raw
Quoted 11 lines · Sonic
Quoted 11 lines

-----Original Message-----
From: Axel Lin [mailto:[email protected]]
Sent: Tuesday, July 03, 2012 3:43 PM
To: Mark Brown
Cc: [email protected]; Zhang, Sonic; Lars-Peter Clausen; Liam Girdwood
Subject: [PATCH v2] regulator: ad5398: Fix min/max current limit boundary checking

Quoted 8 lines
Quoted 6 lines

It is ok to request current limit with min_uA < chip->min_uA and max_uA > chip->max_uA.

We need to set min_uA = chip->min_uA if (min_uA < chip->min_uA), this ensures the equation to calcuate selator does not return negative number.

You should not do it in driver. Set a correct min_uA value in your application.

I think the patch makes sense. If a application request a current range which overlaps with the range support by the chip, but either the requested min is smaller than the supported min or the requested max is larger than the supported max the driver will fail with an error. E.g.

req-min req-max

   |-----------|
        |------------|
     chip-min  chip-max

or even

req-min req-max

   |----------------------|
        |------------|
     chip-min  chip-max

While it is obviously possible for the chip to fulfill this request. Axel's patch takes care of this situation and ensures that the request is satisfied and the output current is set to a current within the requested range and the supported range.

- Lars

Quoted 36 lines

Regards,

Sonic

Quoted 30 lines

~
---
drivers/regulator/ad5398.c | 7 ++++---

1 file changed, 4 insertions(+), 3 deletions(-)
drivers/regulator/ad5398.c +4 -3
index 46d05f3..84fdcda 100644
@@ -89,9 +89,10 @@ static int ad5398_set_current_limit(struct regulator_dev
 *rdev, int min_uA, int
      unsigned short data;
      int ret;
 
-      if (min_uA > chip->max_uA || min_uA < chip->min_uA)
-              return -EINVAL;
-      if (max_uA > chip->max_uA || max_uA < chip->min_uA)
+      if (min_uA < chip->min_uA)
+              min_uA = chip->min_uA;
+
+      if (min_uA > chip->max_uA || max_uA < chip->min_uA)
              return -EINVAL;
 
      selector = DIV_ROUND_UP((min_uA - chip->min_uA) * chip->current_level,

--
1.7.9.5

#3 lore raw
Quoted 49 lines

-----Original Message-----
From: Lars-Peter Clausen [mailto:[email protected]]
Sent: Tuesday, July 03, 2012 4:06 PM
To: Zhang, Sonic
Cc: Axel Lin; Mark Brown; [email protected]; Liam Girdwood
Subject: Re: [PATCH v2] regulator: ad5398: Fix min/max current limit boundary checking

Quoted 11 lines · Sonic
Quoted 11 lines

-----Original Message-----
From: Axel Lin [mailto:[email protected]]
Sent: Tuesday, July 03, 2012 3:43 PM
To: Mark Brown
Cc: [email protected]; Zhang, Sonic; Lars-Peter Clausen; Liam Girdwood
Subject: [PATCH v2] regulator: ad5398: Fix min/max current limit boundary checking

Quoted 8 lines
Quoted 6 lines

It is ok to request current limit with min_uA < chip->min_uA and max_uA > chip->max_uA.

We need to set min_uA = chip->min_uA if (min_uA < chip->min_uA), this ensures the equation to calcuate selator does not return negative number.

You should not do it in driver. Set a correct min_uA value in your application.

I think the patch makes sense. If a application request a current range which overlaps with the range support by the chip, but either the requested min is smaller than the supported min or the requested max is larger than the supported max the driver will fail with an error. E.g.

req-min req-max

  |-----------|
       |------------|
    chip-min  chip-max

or even

req-min req-max

  |----------------------|
       |------------|
    chip-min  chip-max
Quoted 4 lines

While it is obviously possible for the chip to fulfill this request. Axel's patch takes care of this situation and ensures that the request is satisfied and the output current is set to a current within the requested range and the supported range.

If the requested minimum current is smaller than the capability of the hardware, does a bigger min value fulfill this request?

If this logic is correct, I am fine to ACK.

Regards,

Sonic

Quoted 40 lines

- Lars

Quoted 36 lines

Regards,

Sonic

Quoted 30 lines

~
---
drivers/regulator/ad5398.c | 7 ++++---

1 file changed, 4 insertions(+), 3 deletions(-)
drivers/regulator/ad5398.c +4 -3
index 46d05f3..84fdcda 100644
@@ -89,9 +89,10 @@ static int ad5398_set_current_limit(struct regulator_dev
 *rdev, int min_uA, int
      unsigned short data;
      int ret;
 
-      if (min_uA > chip->max_uA || min_uA < chip->min_uA)
-              return -EINVAL;
-      if (max_uA > chip->max_uA || max_uA < chip->min_uA)
+      if (min_uA < chip->min_uA)
+              min_uA = chip->min_uA;
+
+      if (min_uA > chip->max_uA || max_uA < chip->min_uA)
              return -EINVAL;
 
      selector = DIV_ROUND_UP((min_uA - chip->min_uA) * chip->current_level,

--
1.7.9.5

���{.n�+�������+%�����ݶ��w��{.n�+����{��G�����{ay�ʇڙ�,j��f���h��������z_�(�階�ݢj"���m������G������?����&���~��iO��z��v�^�m���� ������?�I�

#4 lore raw
Quoted 49 lines · Sonic
Quoted 49 lines

-----Original Message-----
From: Lars-Peter Clausen [mailto:[email protected]]
Sent: Tuesday, July 03, 2012 4:06 PM
To: Zhang, Sonic
Cc: Axel Lin; Mark Brown; [email protected]; Liam Girdwood
Subject: Re: [PATCH v2] regulator: ad5398: Fix min/max current limit boundary checking

Quoted 11 lines · Sonic
Quoted 11 lines

-----Original Message-----
From: Axel Lin [mailto:[email protected]]
Sent: Tuesday, July 03, 2012 3:43 PM
To: Mark Brown
Cc: [email protected]; Zhang, Sonic; Lars-Peter Clausen; Liam Girdwood
Subject: [PATCH v2] regulator: ad5398: Fix min/max current limit boundary checking

Quoted 8 lines
Quoted 6 lines

It is ok to request current limit with min_uA < chip->min_uA and max_uA > chip->max_uA.

We need to set min_uA = chip->min_uA if (min_uA < chip->min_uA), this ensures the equation to calcuate selator does not return negative number.

You should not do it in driver. Set a correct min_uA value in your application.

I think the patch makes sense. If a application request a current range which overlaps with the range support by the chip, but either the requested min is smaller than the supported min or the requested max is larger than the supported max the driver will fail with an error. E.g.

req-min req-max

  |-----------|
       |------------|
    chip-min  chip-max

or even

req-min req-max

  |----------------------|
       |------------|
    chip-min  chip-max
Quoted 6 lines
Quoted 4 lines

While it is obviously possible for the chip to fulfill this request. Axel's patch takes care of this situation and ensures that the request is satisfied and the output current is set to a current within the requested range and the supported range.

If the requested minimum current is smaller than the capability of the hardware, does a bigger min value fulfill this request?

As long as it is smaller than the maximum requested current, yes. You request a current range with the regulator API and any value within this range is fine as the actual output current.

Quoted 50 lines

If this logic is correct, I am fine to ACK.

Regards,

Sonic

Quoted 40 lines

- Lars

Quoted 36 lines

Regards,

Sonic

Quoted 30 lines

~
---
drivers/regulator/ad5398.c | 7 ++++---

1 file changed, 4 insertions(+), 3 deletions(-)
drivers/regulator/ad5398.c +4 -3
index 46d05f3..84fdcda 100644
@@ -89,9 +89,10 @@ static int ad5398_set_current_limit(struct regulator_dev
 *rdev, int min_uA, int
      unsigned short data;
      int ret;
 
-      if (min_uA > chip->max_uA || min_uA < chip->min_uA)
-              return -EINVAL;
-      if (max_uA > chip->max_uA || max_uA < chip->min_uA)
+      if (min_uA < chip->min_uA)
+              min_uA = chip->min_uA;
+
+      if (min_uA > chip->max_uA || max_uA < chip->min_uA)
              return -EINVAL;
 
      selector = DIV_ROUND_UP((min_uA - chip->min_uA) * chip->current_level,

--
1.7.9.5

#5 lore raw
Quoted 66 lines

-----Original Message-----
From: Lars-Peter Clausen [mailto:[email protected]]
Sent: Tuesday, July 03, 2012 4:24 PM
To: Zhang, Sonic
Cc: Axel Lin; Mark Brown; [email protected]; Liam Girdwood
Subject: Re: [PATCH v2] regulator: ad5398: Fix min/max current limit boundary checking

Quoted 22 lines · Sonic
Quoted 22 lines

-----Original Message-----
From: Lars-Peter Clausen [mailto:[email protected]]
Sent: Tuesday, July 03, 2012 4:06 PM
To: Zhang, Sonic
Cc: Axel Lin; Mark Brown; [email protected]; Liam Girdwood
Subject: Re: [PATCH v2] regulator: ad5398: Fix min/max current limit boundary checking

Quoted 11 lines · Sonic
Quoted 11 lines

-----Original Message-----
From: Axel Lin [mailto:[email protected]]
Sent: Tuesday, July 03, 2012 3:43 PM
To: Mark Brown
Cc: [email protected]; Zhang, Sonic; Lars-Peter Clausen; Liam Girdwood
Subject: [PATCH v2] regulator: ad5398: Fix min/max current limit boundary checking

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

It is ok to request current limit with min_uA < chip->min_uA and max_uA > chip->max_uA.

We need to set min_uA = chip->min_uA if (min_uA < chip->min_uA), this ensures the equation to calcuate selator does not return negative

number.

Quoted 27 lines
Quoted 26 lines
Quoted 3 lines
Quoted 1 line

You should not do it in driver. Set a correct min_uA value in your application.

I think the patch makes sense. If a application request a current range which overlaps with the range support by the chip, but either the requested min is smaller than the supported min or the requested max is larger than the supported max the driver will fail with an error. E.g.

req-min req-max

  |-----------|
       |------------|
    chip-min  chip-max

or even

req-min req-max

  |----------------------|
       |------------|
    chip-min  chip-max

While it is obviously possible for the chip to fulfill this request. Axel's patch takes care of this situation and ensures that the request is satisfied and the output current is set to a current within the requested range and the supported range.

Quoted 7 lines
Quoted 1 line

If the requested minimum current is smaller than the capability of the hardware,

does a bigger min value fulfill this request?

As long as it is smaller than the maximum requested current, yes. You request a current range with the regulator API and any value within this range is fine as the actual output current.

If so, please also set the max_uA as well.

Sonic

Quoted 23 lines
Quoted 23 lines

If this logic is correct, I am fine to ACK.

Regards,

Sonic

Quoted 14 lines

- Lars

Quoted 11 lines

Regards,

Sonic

Quoted 6 lines

~
---
drivers/regulator/ad5398.c | 7 ++++---

1 file changed, 4 insertions(+), 3 deletions(-)
Quoted 30 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

drivers/regulator/ad5398.c static int ad5398_set_current_limit(struct

Quoted 5 lines
drivers/regulator/ad5398.c +0 -0
index 46d05f3..84fdcda 100644
@@ -89,9 +89,10 @@ static int ad5398_set_current_limit(struct

regulator_dev

Quoted 23 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
drivers/regulator/ad5398.c +4 -3
 *rdev, int min_uA, int
      unsigned short data;
      int ret;
 
-      if (min_uA > chip->max_uA || min_uA < chip->min_uA)
-              return -EINVAL;
-      if (max_uA > chip->max_uA || max_uA < chip->min_uA)
+      if (min_uA < chip->min_uA)
+              min_uA = chip->min_uA;
+
+      if (min_uA > chip->max_uA || max_uA < chip->min_uA)
              return -EINVAL;
 
Quoted 1 line
Quoted 1 line
Quoted 1 line
      selector = DIV_ROUND_UP((min_uA - chip->min_uA) * chip-

current_level,

Quoted 7 lines
Quoted 6 lines
Quoted 5 lines

--
1.7.9.5

���{.n�+�������+%�����ݶ��w��{.n�+����{��G�����{ay�ʇڙ�,j��f���h��������z_�(�階�ݢj"���m������G������?����&���~��iO��z��v�^�m���� ������?�I�

author
#6 lore raw
Quoted 6 lines
Quoted 4 lines

As long as it is smaller than the maximum requested current, yes. You request a current range with the regulator API and any value within this range is fine as the actual output current.

If so, please also set the max_uA as well.

The equation to calculate the selector does not depend on max_uA. So I think we don't need to set the requested max_uA.

Current does check the selected current by:
if (ad5398_calc_current(chip, selector) > max_uA)

        return -EINVAL;

This ensures the selected current falls within specified range.

Regards,
Axel

#7 lore raw
Quoted 16 lines

-----Original Message-----
From: Axel Lin [mailto:[email protected]]
Sent: Tuesday, July 03, 2012 5:44 PM
To: Zhang, Sonic
Cc: Lars-Peter Clausen; Mark Brown; [email protected]; Liam Girdwood
Subject: Re: [PATCH v2] regulator: ad5398: Fix min/max current limit boundary checking

Quoted 5 lines
Quoted 4 lines

As long as it is smaller than the maximum requested current, yes. You request a current range with the regulator API and any value within this range is fine as the actual output current.

Quoted 5 lines
Quoted 1 line

If so, please also set the max_uA as well.

The equation to calculate the selector does not depend on max_uA. So I think we don't need to set the requested max_uA.

But, ad5398_set_current_limit() behaves different for min_uA and max_uA with you patch. Is this expected?

Sonic

Quoted 8 lines

Current does check the selected current by:
if (ad5398_calc_current(chip, selector) > max_uA)

       return -EINVAL;

This ensures the selected current falls within specified range.

Regards,
Axel

author
#8 lore raw
Quoted 7 lines
Quoted 4 lines

The equation to calculate the selector does not depend on max_uA. So I think we don't need to set the requested max_uA.

But, ad5398_set_current_limit() behaves different for min_uA and max_uA with you patch. Is this expected?

What we want is to set the smallest current supported by this hardware within the range you requested.

Current code uses below equation to choose selector:
selector = DIV_ROUND_UP((min_uA - chip->min_uA) * chip->current_level,

                        range_uA);

With this equation, we need to ensure min_uA >= chip->min_uA, otherwise it returns a negative selector.

That is why we need to add:
if (min_uA < chip->min_uA)

        min_uA = chip->min_uA;

Axel

#9 lore raw
Quoted 25 lines

-----Original Message-----
From: Axel Lin [mailto:[email protected]]
Sent: Tuesday, July 03, 2012 7:36 PM
To: Zhang, Sonic
Cc: Lars-Peter Clausen; Mark Brown; [email protected]; Liam Girdwood
Subject: RE: [PATCH v2] regulator: ad5398: Fix min/max current limit boundary checking

Quoted 5 lines
Quoted 3 lines

The equation to calculate the selector does not depend on max_uA. So I think we don't need to set the requested max_uA.

But, ad5398_set_current_limit() behaves different for min_uA and max_uA with

you patch. Is this expected?

Quoted 1 line

What we want is to set the smallest current supported by this hardware within the range you requested.

Current code uses below equation to choose selector:
selector = DIV_ROUND_UP((min_uA - chip->min_uA) * chip->current_level,

Quoted 9 lines
                       range_uA);

With this equation, we need to ensure min_uA >= chip->min_uA, otherwise it returns a negative selector.

That is why we need to add:
if (min_uA < chip->min_uA)

       min_uA = chip->min_uA;

Yes, but if you apply this logic to min_uA, you should apply the same logic to max_uA, even though it is not used in your application.

Sonic

Quoted 3 lines

Axel

���{.n�+�������+%�����ݶ��w��{.n�+����{��G�����{ay�ʇڙ�,j��f���h��������z_�(�階�ݢj"���m������G������?����&���~��iO��z��v�^�m���� ������?�I�

author
#10 lore raw
Quoted 7 lines
Quoted 5 lines

That is why we need to add:
if (min_uA < chip->min_uA)

       min_uA = chip->min_uA;

Yes, but if you apply this logic to min_uA, you should apply the same logic to max_uA, even though it is not used in your application.

Actually, the logic is the same:
to find a supported (minmal) current in specified range.

The question is the equation used in current code does not allow min_uA < chip->min_uA.
Setting min_uA = chip->min_uA if min_uA < chip->min_uA does make sense because botch request actually returns the same current value. ( I mean no user visible change )

Adding below logic is not necessary.
( Note: Adding this or not does not have any user visible change, it's just not necessary)

if (max_uA > chip->min_uA)

        max_uA = chip->max_uA;

It is not necessary because the equation to choose selector does not depends on max_uA. No matter if we set max_uA = chip->max_uA or not in this case, it does not impact the equation to choose the selector.

But, well, if you really prefer adding it. I'll send a v3 for it. Just let me know how do you think.

Regards,
Axel

#11 lore raw
Quoted 34 lines

-----Original Message-----
From: Axel Lin [mailto:[email protected]]
Sent: Wednesday, July 04, 2012 11:19 AM
To: Zhang, Sonic
Cc: Lars-Peter Clausen; Mark Brown; [email protected]; Liam Girdwood
Subject: RE: [PATCH v2] regulator: ad5398: Fix min/max current limit boundary checking

Quoted 6 lines
Quoted 4 lines

That is why we need to add:
if (min_uA < chip->min_uA)

       min_uA = chip->min_uA;

Yes, but if you apply this logic to min_uA, you should apply the same logic to

max_uA, even though it is not used in your application.

Actually, the logic is the same:
to find a supported (minmal) current in specified range.

The question is the equation used in current code does not allow min_uA < chip->min_uA.
Setting min_uA = chip->min_uA if min_uA < chip->min_uA does make sense because botch request actually returns the same current value. ( I mean no user visible change )

Adding below logic is not necessary.
( Note: Adding this or not does not have any user visible change, it's just not necessary)

if (max_uA > chip->min_uA)

Quoted 8 lines
       max_uA = chip->max_uA;

It is not necessary because the equation to choose selector does not depends on max_uA. No matter if we set max_uA = chip->max_uA or not in this case, it does not impact the equation to choose the selector.

But, well, if you really prefer adding it. I'll send a v3 for it. Just let me know how do you think.

Yes, v3 please.

Sonic

Quoted 5 lines

Regards,
Axel

���{.n�+�������+%�����ݶ��w��{.n�+����{��G�����{ay�ʇڙ�,j��f���h��������z_�(�階�ݢj"���m������G������?����&���~��iO��z��v�^�m���� ������?�I�