diff mbox series

[v2,1/5] iio: adc: ad7192: use devm_regulator_get_enable_read_voltage

Message ID 20240612-iio-adc-ref-supply-refactor-v2-1-fa622e7354e9@baylibre.com (mailing list archive)
State Changes Requested
Headers show
Series iio: adc: use devm_regulator_get_enable_read_voltage round 1 | expand

Commit Message

David Lechner June 12, 2024, 9:03 p.m. UTC
This makes use of the new devm_regulator_get_enable_read_voltage()
function to reduce boilerplate code.

Error messages have changed slightly since there are now fewer places
where we print an error. The rest of the logic of selecting which
supply to use as the reference voltage remains the same.

Also 1000 is replaced by MILLI in a few places for consistency.

Signed-off-by: David Lechner <dlechner@baylibre.com>
---

v2 changes:
- avoid using else on return value check of "aincom" regulator
- also fall back to dummy regulator on "avdd" in case of ENODEV
---
 drivers/iio/adc/ad7192.c | 100 +++++++++++++++++------------------------------
 1 file changed, 35 insertions(+), 65 deletions(-)

Comments

Jonathan Cameron June 15, 2024, 12:08 p.m. UTC | #1
On Wed, 12 Jun 2024 16:03:05 -0500
David Lechner <dlechner@baylibre.com> wrote:

> This makes use of the new devm_regulator_get_enable_read_voltage()
> function to reduce boilerplate code.
> 
> Error messages have changed slightly since there are now fewer places
> where we print an error. The rest of the logic of selecting which
> supply to use as the reference voltage remains the same.
> 
> Also 1000 is replaced by MILLI in a few places for consistency.
> 
> Signed-off-by: David Lechner <dlechner@baylibre.com>

Complicated bit of code, but seems correct.
However, it crossed with Alisa-Dariana switching adding a
struct device *dev = &spi->dev to probe() that I picked up earlier
today.

I could unwind that but given Alisa-Dariana has a number of
other patches on this driver in flight, I'd like the two of you
to work out the best resolution between you.  Maybe easiest option
is that Alisa-Dariana sends this a first patch of the next
series I should pick up.

Thanks,

Jonathan
Alisa-Dariana Roman June 17, 2024, 9:35 a.m. UTC | #2
On 15.06.2024 15:08, Jonathan Cameron wrote:
> On Wed, 12 Jun 2024 16:03:05 -0500
> David Lechner <dlechner@baylibre.com> wrote:
> 
>> This makes use of the new devm_regulator_get_enable_read_voltage()
>> function to reduce boilerplate code.
>>
>> Error messages have changed slightly since there are now fewer places
>> where we print an error. The rest of the logic of selecting which
>> supply to use as the reference voltage remains the same.
>>
>> Also 1000 is replaced by MILLI in a few places for consistency.
>>
>> Signed-off-by: David Lechner <dlechner@baylibre.com>
> 
> Complicated bit of code, but seems correct.
> However, it crossed with Alisa-Dariana switching adding a
> struct device *dev = &spi->dev to probe() that I picked up earlier
> today.
> 
> I could unwind that but given Alisa-Dariana has a number of
> other patches on this driver in flight, I'd like the two of you
> to work out the best resolution between you.  Maybe easiest option
> is that Alisa-Dariana sends this a first patch of the next
> series I should pick up.
> 
> Thanks,
> 
> Jonathan
I will add this patch to my series and send it shortly.

Kind regards,
Alisa-Dariana Roman.
David Lechner June 17, 2024, 1:22 p.m. UTC | #3
On Mon, Jun 17, 2024 at 4:35 AM Alisa-Dariana Roman
<alisadariana@gmail.com> wrote:
>
> On 15.06.2024 15:08, Jonathan Cameron wrote:
> > On Wed, 12 Jun 2024 16:03:05 -0500
> > David Lechner <dlechner@baylibre.com> wrote:
> >
> >> This makes use of the new devm_regulator_get_enable_read_voltage()
> >> function to reduce boilerplate code.
> >>
> >> Error messages have changed slightly since there are now fewer places
> >> where we print an error. The rest of the logic of selecting which
> >> supply to use as the reference voltage remains the same.
> >>
> >> Also 1000 is replaced by MILLI in a few places for consistency.
> >>
> >> Signed-off-by: David Lechner <dlechner@baylibre.com>
> >
> > Complicated bit of code, but seems correct.
> > However, it crossed with Alisa-Dariana switching adding a
> > struct device *dev = &spi->dev to probe() that I picked up earlier
> > today.
> >
> > I could unwind that but given Alisa-Dariana has a number of
> > other patches on this driver in flight, I'd like the two of you
> > to work out the best resolution between you.  Maybe easiest option
> > is that Alisa-Dariana sends this a first patch of the next
> > series I should pick up.
> >
> > Thanks,
> >
> > Jonathan
> I will add this patch to my series and send it shortly.
>
> Kind regards,
> Alisa-Dariana Roman.

Great, thanks!
Alisa-Dariana Roman June 17, 2024, 1:38 p.m. UTC | #4
On 17.06.2024 16:22, David Lechner wrote:
> On Mon, Jun 17, 2024 at 4:35 AM Alisa-Dariana Roman
> <alisadariana@gmail.com> wrote:
>>
>> On 15.06.2024 15:08, Jonathan Cameron wrote:
>>> On Wed, 12 Jun 2024 16:03:05 -0500
>>> David Lechner <dlechner@baylibre.com> wrote:
>>>
>>>> This makes use of the new devm_regulator_get_enable_read_voltage()
>>>> function to reduce boilerplate code.
>>>>
>>>> Error messages have changed slightly since there are now fewer places
>>>> where we print an error. The rest of the logic of selecting which
>>>> supply to use as the reference voltage remains the same.
>>>>
>>>> Also 1000 is replaced by MILLI in a few places for consistency.
>>>>
>>>> Signed-off-by: David Lechner <dlechner@baylibre.com>
>>>
>>> Complicated bit of code, but seems correct.
>>> However, it crossed with Alisa-Dariana switching adding a
>>> struct device *dev = &spi->dev to probe() that I picked up earlier
>>> today.
>>>
>>> I could unwind that but given Alisa-Dariana has a number of
>>> other patches on this driver in flight, I'd like the two of you
>>> to work out the best resolution between you.  Maybe easiest option
>>> is that Alisa-Dariana sends this a first patch of the next
>>> series I should pick up.
>>>
>>> Thanks,
>>>
>>> Jonathan
>> I will add this patch to my series and send it shortly.
>>
>> Kind regards,
>> Alisa-Dariana Roman.
> 
> Great, thanks!

Just one quick question:

I am getting two such warnings when running the checkpatch script:

WARNING: else is not generally useful after a break or return
#1335: FILE: ./drivers/iio/adc/ad7192.c:1335:
+		return dev_err_probe(dev, ret, "Failed to get AVDD voltage\n");
+	} else {

Should I switch the last two branches to get rid of the warnings or just 
ignore them?
David Lechner June 17, 2024, 1:48 p.m. UTC | #5
On 6/17/24 8:38 AM, Alisa-Dariana Roman wrote:
> On 17.06.2024 16:22, David Lechner wrote:
>> On Mon, Jun 17, 2024 at 4:35 AM Alisa-Dariana Roman
>> <alisadariana@gmail.com> wrote:
>>>
>>> On 15.06.2024 15:08, Jonathan Cameron wrote:
>>>> On Wed, 12 Jun 2024 16:03:05 -0500
>>>> David Lechner <dlechner@baylibre.com> wrote:
>>>>
>>>>> This makes use of the new devm_regulator_get_enable_read_voltage()
>>>>> function to reduce boilerplate code.
>>>>>
>>>>> Error messages have changed slightly since there are now fewer places
>>>>> where we print an error. The rest of the logic of selecting which
>>>>> supply to use as the reference voltage remains the same.
>>>>>
>>>>> Also 1000 is replaced by MILLI in a few places for consistency.
>>>>>
>>>>> Signed-off-by: David Lechner <dlechner@baylibre.com>
>>>>
>>>> Complicated bit of code, but seems correct.
>>>> However, it crossed with Alisa-Dariana switching adding a
>>>> struct device *dev = &spi->dev to probe() that I picked up earlier
>>>> today.
>>>>
>>>> I could unwind that but given Alisa-Dariana has a number of
>>>> other patches on this driver in flight, I'd like the two of you
>>>> to work out the best resolution between you.  Maybe easiest option
>>>> is that Alisa-Dariana sends this a first patch of the next
>>>> series I should pick up.
>>>>
>>>> Thanks,
>>>>
>>>> Jonathan
>>> I will add this patch to my series and send it shortly.
>>>
>>> Kind regards,
>>> Alisa-Dariana Roman.
>>
>> Great, thanks!
> 
> Just one quick question:
> 
> I am getting two such warnings when running the checkpatch script:
> 
> WARNING: else is not generally useful after a break or return
> #1335: FILE: ./drivers/iio/adc/ad7192.c:1335:
> +        return dev_err_probe(dev, ret, "Failed to get AVDD voltage\n");
> +    } else {
> 
> Should I switch the last two branches to get rid of the warnings or just ignore them?
> 

In the other patches, I was able to reorder things to avoid this
warning, but since this one was more complicated, I just ignored
this warning.

We can't just remove the else in this case because the return
is inside of an `else if`.
Alisa-Dariana Roman June 17, 2024, 2:10 p.m. UTC | #6
On 17.06.2024 16:48, David Lechner wrote:
> On 6/17/24 8:38 AM, Alisa-Dariana Roman wrote:
>> On 17.06.2024 16:22, David Lechner wrote:
>>> On Mon, Jun 17, 2024 at 4:35 AM Alisa-Dariana Roman
>>> <alisadariana@gmail.com> wrote:
>>>>
>>>> On 15.06.2024 15:08, Jonathan Cameron wrote:
>>>>> On Wed, 12 Jun 2024 16:03:05 -0500
>>>>> David Lechner <dlechner@baylibre.com> wrote:
>>>>>
>>>>>> This makes use of the new devm_regulator_get_enable_read_voltage()
>>>>>> function to reduce boilerplate code.
>>>>>>
>>>>>> Error messages have changed slightly since there are now fewer places
>>>>>> where we print an error. The rest of the logic of selecting which
>>>>>> supply to use as the reference voltage remains the same.
>>>>>>
>>>>>> Also 1000 is replaced by MILLI in a few places for consistency.
>>>>>>
>>>>>> Signed-off-by: David Lechner <dlechner@baylibre.com>
>>>>>
>>>>> Complicated bit of code, but seems correct.
>>>>> However, it crossed with Alisa-Dariana switching adding a
>>>>> struct device *dev = &spi->dev to probe() that I picked up earlier
>>>>> today.
>>>>>
>>>>> I could unwind that but given Alisa-Dariana has a number of
>>>>> other patches on this driver in flight, I'd like the two of you
>>>>> to work out the best resolution between you.  Maybe easiest option
>>>>> is that Alisa-Dariana sends this a first patch of the next
>>>>> series I should pick up.
>>>>>
>>>>> Thanks,
>>>>>
>>>>> Jonathan
>>>> I will add this patch to my series and send it shortly.
>>>>
>>>> Kind regards,
>>>> Alisa-Dariana Roman.
>>>
>>> Great, thanks!
>>
>> Just one quick question:
>>
>> I am getting two such warnings when running the checkpatch script:
>>
>> WARNING: else is not generally useful after a break or return
>> #1335: FILE: ./drivers/iio/adc/ad7192.c:1335:
>> +        return dev_err_probe(dev, ret, "Failed to get AVDD voltage\n");
>> +    } else {
>>
>> Should I switch the last two branches to get rid of the warnings or just ignore them?
>>
> 
> In the other patches, I was able to reorder things to avoid this
> warning, but since this one was more complicated, I just ignored
> this warning.
> 
> We can't just remove the else in this case because the return
> is inside of an `else if`.

	/* AVDD can optionally be used as reference voltage */
	ret = devm_regulator_get_enable_read_voltage(dev, "avdd");
	if (ret == -ENODEV || ret == -EINVAL) {
		/*
		 * We get -EINVAL if avdd is a supply with unknown voltage. We
		 * still need to enable it since it is also a power supply.
		 */
		ret = devm_regulator_get_enable(dev, "avdd");
		if (ret)
			return dev_err_probe(dev, ret,
					     "Failed to enable AVDD supply\n");

		avdd_mv = 0;
	} else if (ret >= 0) {
		avdd_mv = ret / MILLI;
	} else {
		return dev_err_probe(dev, ret, "Failed to get AVDD voltage\n");
	}

Would switching the last two branches, in order to get rid of the 
warnings, make the code harder to understand?
David Lechner June 17, 2024, 3:28 p.m. UTC | #7
On Mon, Jun 17, 2024 at 9:10 AM Alisa-Dariana Roman
<alisadariana@gmail.com> wrote:
>
> On 17.06.2024 16:48, David Lechner wrote:
> > On 6/17/24 8:38 AM, Alisa-Dariana Roman wrote:
> >> On 17.06.2024 16:22, David Lechner wrote:
> >>> On Mon, Jun 17, 2024 at 4:35 AM Alisa-Dariana Roman
> >>> <alisadariana@gmail.com> wrote:
> >>>>
> >>>> On 15.06.2024 15:08, Jonathan Cameron wrote:
> >>>>> On Wed, 12 Jun 2024 16:03:05 -0500
> >>>>> David Lechner <dlechner@baylibre.com> wrote:
> >>>>>
> >>>>>> This makes use of the new devm_regulator_get_enable_read_voltage()
> >>>>>> function to reduce boilerplate code.
> >>>>>>
> >>>>>> Error messages have changed slightly since there are now fewer places
> >>>>>> where we print an error. The rest of the logic of selecting which
> >>>>>> supply to use as the reference voltage remains the same.
> >>>>>>
> >>>>>> Also 1000 is replaced by MILLI in a few places for consistency.
> >>>>>>
> >>>>>> Signed-off-by: David Lechner <dlechner@baylibre.com>
> >>>>>
> >>>>> Complicated bit of code, but seems correct.
> >>>>> However, it crossed with Alisa-Dariana switching adding a
> >>>>> struct device *dev = &spi->dev to probe() that I picked up earlier
> >>>>> today.
> >>>>>
> >>>>> I could unwind that but given Alisa-Dariana has a number of
> >>>>> other patches on this driver in flight, I'd like the two of you
> >>>>> to work out the best resolution between you.  Maybe easiest option
> >>>>> is that Alisa-Dariana sends this a first patch of the next
> >>>>> series I should pick up.
> >>>>>
> >>>>> Thanks,
> >>>>>
> >>>>> Jonathan
> >>>> I will add this patch to my series and send it shortly.
> >>>>
> >>>> Kind regards,
> >>>> Alisa-Dariana Roman.
> >>>
> >>> Great, thanks!
> >>
> >> Just one quick question:
> >>
> >> I am getting two such warnings when running the checkpatch script:
> >>
> >> WARNING: else is not generally useful after a break or return
> >> #1335: FILE: ./drivers/iio/adc/ad7192.c:1335:
> >> +        return dev_err_probe(dev, ret, "Failed to get AVDD voltage\n");
> >> +    } else {
> >>
> >> Should I switch the last two branches to get rid of the warnings or just ignore them?
> >>
> >
> > In the other patches, I was able to reorder things to avoid this
> > warning, but since this one was more complicated, I just ignored
> > this warning.
> >
> > We can't just remove the else in this case because the return
> > is inside of an `else if`.
>
>         /* AVDD can optionally be used as reference voltage */
>         ret = devm_regulator_get_enable_read_voltage(dev, "avdd");
>         if (ret == -ENODEV || ret == -EINVAL) {
>                 /*
>                  * We get -EINVAL if avdd is a supply with unknown voltage. We
>                  * still need to enable it since it is also a power supply.
>                  */
>                 ret = devm_regulator_get_enable(dev, "avdd");
>                 if (ret)
>                         return dev_err_probe(dev, ret,
>                                              "Failed to enable AVDD supply\n");
>
>                 avdd_mv = 0;
>         } else if (ret >= 0) {
>                 avdd_mv = ret / MILLI;
>         } else {
>                 return dev_err_probe(dev, ret, "Failed to get AVDD voltage\n");
>         }
>
> Would switching the last two branches, in order to get rid of the
> warnings, make the code harder to understand?
>

I did it in the other order because usually we like to handle the
error case first.

To make it more like the other patches, we could do something like
this. The only thing i don't like about it is that `ret` on the very
last line could come from two different places. But it is logically
sound in the current form.

    /* AVDD can optionally be used as reference voltage */
    ret = devm_regulator_get_enable_read_voltage(dev, "avdd");
    if (ret == -ENODEV || ret == -EINVAL) {
        /*
         * We get -EINVAL if avdd is a supply with unknown voltage. We
         * still need to enable it since it is also a power supply.
         */
        ret = devm_regulator_get_enable(dev, "avdd");
        if (ret)
            return dev_err_probe(dev, ret,
                         "Failed to enable AVDD supply\n");
    } else if (ret < 0) {
        return dev_err_probe(dev, ret, "Failed to get AVDD voltage\n");
    }

    avdd_mv = ret <= 0 ? 0 : ret / MILLI;
Alisa-Dariana Roman June 17, 2024, 4:01 p.m. UTC | #8
On 17.06.2024 18:28, David Lechner wrote:
> On Mon, Jun 17, 2024 at 9:10 AM Alisa-Dariana Roman
> <alisadariana@gmail.com> wrote:
>>
>> On 17.06.2024 16:48, David Lechner wrote:
>>> On 6/17/24 8:38 AM, Alisa-Dariana Roman wrote:
>>>> On 17.06.2024 16:22, David Lechner wrote:
>>>>> On Mon, Jun 17, 2024 at 4:35 AM Alisa-Dariana Roman
>>>>> <alisadariana@gmail.com> wrote:
>>>>>>
>>>>>> On 15.06.2024 15:08, Jonathan Cameron wrote:
>>>>>>> On Wed, 12 Jun 2024 16:03:05 -0500
>>>>>>> David Lechner <dlechner@baylibre.com> wrote:
>>>>>>>
>>>>>>>> This makes use of the new devm_regulator_get_enable_read_voltage()
>>>>>>>> function to reduce boilerplate code.
>>>>>>>>
>>>>>>>> Error messages have changed slightly since there are now fewer places
>>>>>>>> where we print an error. The rest of the logic of selecting which
>>>>>>>> supply to use as the reference voltage remains the same.
>>>>>>>>
>>>>>>>> Also 1000 is replaced by MILLI in a few places for consistency.
>>>>>>>>
>>>>>>>> Signed-off-by: David Lechner <dlechner@baylibre.com>
>>>>>>>
>>>>>>> Complicated bit of code, but seems correct.
>>>>>>> However, it crossed with Alisa-Dariana switching adding a
>>>>>>> struct device *dev = &spi->dev to probe() that I picked up earlier
>>>>>>> today.
>>>>>>>
>>>>>>> I could unwind that but given Alisa-Dariana has a number of
>>>>>>> other patches on this driver in flight, I'd like the two of you
>>>>>>> to work out the best resolution between you.  Maybe easiest option
>>>>>>> is that Alisa-Dariana sends this a first patch of the next
>>>>>>> series I should pick up.
>>>>>>>
>>>>>>> Thanks,
>>>>>>>
>>>>>>> Jonathan
>>>>>> I will add this patch to my series and send it shortly.
>>>>>>
>>>>>> Kind regards,
>>>>>> Alisa-Dariana Roman.
>>>>>
>>>>> Great, thanks!
>>>>
>>>> Just one quick question:
>>>>
>>>> I am getting two such warnings when running the checkpatch script:
>>>>
>>>> WARNING: else is not generally useful after a break or return
>>>> #1335: FILE: ./drivers/iio/adc/ad7192.c:1335:
>>>> +        return dev_err_probe(dev, ret, "Failed to get AVDD voltage\n");
>>>> +    } else {
>>>>
>>>> Should I switch the last two branches to get rid of the warnings or just ignore them?
>>>>
>>>
>>> In the other patches, I was able to reorder things to avoid this
>>> warning, but since this one was more complicated, I just ignored
>>> this warning.
>>>
>>> We can't just remove the else in this case because the return
>>> is inside of an `else if`.
>>
>>          /* AVDD can optionally be used as reference voltage */
>>          ret = devm_regulator_get_enable_read_voltage(dev, "avdd");
>>          if (ret == -ENODEV || ret == -EINVAL) {
>>                  /*
>>                   * We get -EINVAL if avdd is a supply with unknown voltage. We
>>                   * still need to enable it since it is also a power supply.
>>                   */
>>                  ret = devm_regulator_get_enable(dev, "avdd");
>>                  if (ret)
>>                          return dev_err_probe(dev, ret,
>>                                               "Failed to enable AVDD supply\n");
>>
>>                  avdd_mv = 0;
>>          } else if (ret >= 0) {
>>                  avdd_mv = ret / MILLI;
>>          } else {
>>                  return dev_err_probe(dev, ret, "Failed to get AVDD voltage\n");
>>          }
>>
>> Would switching the last two branches, in order to get rid of the
>> warnings, make the code harder to understand?
>>
> 
> I did it in the other order because usually we like to handle the
> error case first.
> 
> To make it more like the other patches, we could do something like
> this. The only thing i don't like about it is that `ret` on the very
> last line could come from two different places. But it is logically
> sound in the current form.
> 
>      /* AVDD can optionally be used as reference voltage */
>      ret = devm_regulator_get_enable_read_voltage(dev, "avdd");
>      if (ret == -ENODEV || ret == -EINVAL) {
>          /*
>           * We get -EINVAL if avdd is a supply with unknown voltage. We
>           * still need to enable it since it is also a power supply.
>           */
>          ret = devm_regulator_get_enable(dev, "avdd");
>          if (ret)
>              return dev_err_probe(dev, ret,
>                           "Failed to enable AVDD supply\n");
>      } else if (ret < 0) {
>          return dev_err_probe(dev, ret, "Failed to get AVDD voltage\n");
>      }
> 
>      avdd_mv = ret <= 0 ? 0 : ret / MILLI;

Maybe this would make it a bit clearer, but yes, the ret == 0 could 
still come from two different places :(.

avdd_mv = ret == 0 ? 0 : ret / MILLI;
David Lechner June 17, 2024, 8 p.m. UTC | #9
On 6/17/24 11:01 AM, Alisa-Dariana Roman wrote:
> On 17.06.2024 18:28, David Lechner wrote:
>> On Mon, Jun 17, 2024 at 9:10 AM Alisa-Dariana Roman
>> <alisadariana@gmail.com> wrote:
>>>
>>> On 17.06.2024 16:48, David Lechner wrote:
>>>> On 6/17/24 8:38 AM, Alisa-Dariana Roman wrote:
>>>>> On 17.06.2024 16:22, David Lechner wrote:
>>>>>> On Mon, Jun 17, 2024 at 4:35 AM Alisa-Dariana Roman
>>>>>> <alisadariana@gmail.com> wrote:
>>>>>>>
>>>>>>> On 15.06.2024 15:08, Jonathan Cameron wrote:
>>>>>>>> On Wed, 12 Jun 2024 16:03:05 -0500
>>>>>>>> David Lechner <dlechner@baylibre.com> wrote:
>>>>>>>>
>>>>>>>>> This makes use of the new devm_regulator_get_enable_read_voltage()
>>>>>>>>> function to reduce boilerplate code.
>>>>>>>>>
>>>>>>>>> Error messages have changed slightly since there are now fewer places
>>>>>>>>> where we print an error. The rest of the logic of selecting which
>>>>>>>>> supply to use as the reference voltage remains the same.
>>>>>>>>>
>>>>>>>>> Also 1000 is replaced by MILLI in a few places for consistency.
>>>>>>>>>
>>>>>>>>> Signed-off-by: David Lechner <dlechner@baylibre.com>
>>>>>>>>
>>>>>>>> Complicated bit of code, but seems correct.
>>>>>>>> However, it crossed with Alisa-Dariana switching adding a
>>>>>>>> struct device *dev = &spi->dev to probe() that I picked up earlier
>>>>>>>> today.
>>>>>>>>
>>>>>>>> I could unwind that but given Alisa-Dariana has a number of
>>>>>>>> other patches on this driver in flight, I'd like the two of you
>>>>>>>> to work out the best resolution between you.  Maybe easiest option
>>>>>>>> is that Alisa-Dariana sends this a first patch of the next
>>>>>>>> series I should pick up.
>>>>>>>>
>>>>>>>> Thanks,
>>>>>>>>
>>>>>>>> Jonathan
>>>>>>> I will add this patch to my series and send it shortly.
>>>>>>>
>>>>>>> Kind regards,
>>>>>>> Alisa-Dariana Roman.
>>>>>>
>>>>>> Great, thanks!
>>>>>
>>>>> Just one quick question:
>>>>>
>>>>> I am getting two such warnings when running the checkpatch script:
>>>>>
>>>>> WARNING: else is not generally useful after a break or return
>>>>> #1335: FILE: ./drivers/iio/adc/ad7192.c:1335:
>>>>> +        return dev_err_probe(dev, ret, "Failed to get AVDD voltage\n");
>>>>> +    } else {
>>>>>
>>>>> Should I switch the last two branches to get rid of the warnings or just ignore them?
>>>>>
>>>>
>>>> In the other patches, I was able to reorder things to avoid this
>>>> warning, but since this one was more complicated, I just ignored
>>>> this warning.
>>>>
>>>> We can't just remove the else in this case because the return
>>>> is inside of an `else if`.
>>>
>>>          /* AVDD can optionally be used as reference voltage */
>>>          ret = devm_regulator_get_enable_read_voltage(dev, "avdd");
>>>          if (ret == -ENODEV || ret == -EINVAL) {
>>>                  /*
>>>                   * We get -EINVAL if avdd is a supply with unknown voltage. We
>>>                   * still need to enable it since it is also a power supply.
>>>                   */
>>>                  ret = devm_regulator_get_enable(dev, "avdd");
>>>                  if (ret)
>>>                          return dev_err_probe(dev, ret,
>>>                                               "Failed to enable AVDD supply\n");
>>>
>>>                  avdd_mv = 0;
>>>          } else if (ret >= 0) {
>>>                  avdd_mv = ret / MILLI;
>>>          } else {
>>>                  return dev_err_probe(dev, ret, "Failed to get AVDD voltage\n");
>>>          }
>>>
>>> Would switching the last two branches, in order to get rid of the
>>> warnings, make the code harder to understand?
>>>
>>
>> I did it in the other order because usually we like to handle the
>> error case first.
>>
>> To make it more like the other patches, we could do something like
>> this. The only thing i don't like about it is that `ret` on the very
>> last line could come from two different places. But it is logically
>> sound in the current form.
>>
>>      /* AVDD can optionally be used as reference voltage */
>>      ret = devm_regulator_get_enable_read_voltage(dev, "avdd");
>>      if (ret == -ENODEV || ret == -EINVAL) {
>>          /*
>>           * We get -EINVAL if avdd is a supply with unknown voltage. We
>>           * still need to enable it since it is also a power supply.
>>           */
>>          ret = devm_regulator_get_enable(dev, "avdd");
>>          if (ret)
>>              return dev_err_probe(dev, ret,
>>                           "Failed to enable AVDD supply\n");
>>      } else if (ret < 0) {
>>          return dev_err_probe(dev, ret, "Failed to get AVDD voltage\n");
>>      }
>>
>>      avdd_mv = ret <= 0 ? 0 : ret / MILLI;
> 
> Maybe this would make it a bit clearer, but yes, the ret == 0 could still come from two different places :(.
> 
> avdd_mv = ret == 0 ? 0 : ret / MILLI;
> 

We could make a ret2 local variable inside of the if block to avoid writing over ret.
Alisa-Dariana Roman June 18, 2024, 9:45 a.m. UTC | #10
On 17.06.2024 23:00, David Lechner wrote:
...

> 
> We could make a ret2 local variable inside of the if block to avoid writing over ret.
> 

Nice! If this looks alright, I will send it along my updated series.

 From f84206b85b04f89d57b9293dd93e017efe8b350c Mon Sep 17 00:00:00 2001
From: David Lechner <dlechner@baylibre.com>
Date: Wed, 12 Jun 2024 16:03:05 -0500
Subject: [PATCH] iio: adc: ad7192: use 
devm_regulator_get_enable_read_voltage

This makes use of the new devm_regulator_get_enable_read_voltage()
function to reduce boilerplate code.

Error messages have changed slightly since there are now fewer places
where we print an error. The rest of the logic of selecting which
supply to use as the reference voltage remains the same.

Also 1000 is replaced by MILLI in a few places for consistency.

Signed-off-by: David Lechner <dlechner@baylibre.com>
---
  drivers/iio/adc/ad7192.c | 101 +++++++++++++--------------------------
  1 file changed, 34 insertions(+), 67 deletions(-)

diff --git a/drivers/iio/adc/ad7192.c b/drivers/iio/adc/ad7192.c
index c7fb51a90e87..2448b01e0d59 100644
--- a/drivers/iio/adc/ad7192.c
+++ b/drivers/iio/adc/ad7192.c
@@ -200,8 +200,6 @@ struct ad7192_chip_info {

  struct ad7192_state {
  	const struct ad7192_chip_info	*chip_info;
-	struct regulator		*avdd;
-	struct regulator		*vref;
  	struct clk			*mclk;
  	u16				int_vref_mv;
  	u32				aincom_mv;
@@ -1189,18 +1187,12 @@ static const struct ad7192_chip_info 
ad7192_chip_info_tbl[] = {
  	},
  };

-static void ad7192_reg_disable(void *reg)
-{
-	regulator_disable(reg);
-}
-
  static int ad7192_probe(struct spi_device *spi)
  {
  	struct device *dev = &spi->dev;
  	struct ad7192_state *st;
  	struct iio_dev *indio_dev;
-	struct regulator *aincom;
-	int ret;
+	int ret, ret2, avdd_mv;

  	if (!spi->irq)
  		return dev_err_probe(dev, -ENODEV, "Failed to get IRQ\n");
@@ -1218,72 +1210,47 @@ static int ad7192_probe(struct spi_device *spi)
  	 * Newer firmware should provide a zero volt fixed supply if wired to
  	 * ground.
  	 */
-	aincom = devm_regulator_get_optional(dev, "aincom");
-	if (IS_ERR(aincom)) {
-		if (PTR_ERR(aincom) != -ENODEV)
-			return dev_err_probe(dev, PTR_ERR(aincom),
-					     "Failed to get AINCOM supply\n");
-
-		st->aincom_mv = 0;
-	} else {
-		ret = regulator_enable(aincom);
-		if (ret)
-			return dev_err_probe(dev, ret,
-					     "Failed to enable specified AINCOM supply\n");
-
-		ret = devm_add_action_or_reset(dev, ad7192_reg_disable, aincom);
-		if (ret)
-			return ret;
-
-		ret = regulator_get_voltage(aincom);
-		if (ret < 0)
-			return dev_err_probe(dev, ret,
-					     "Device tree error, AINCOM voltage undefined\n");
-		st->aincom_mv = ret / MILLI;
+	ret = devm_regulator_get_enable_read_voltage(dev, "aincom");
+	if (ret < 0 && ret != -ENODEV)
+		return dev_err_probe(dev, ret, "Failed to get AINCOM voltage\n");
+
+	st->aincom_mv = ret == -ENODEV ? 0 : ret / MILLI;
+
+	/* AVDD can optionally be used as reference voltage */
+	ret = devm_regulator_get_enable_read_voltage(dev, "avdd");
+	if (ret == -ENODEV || ret == -EINVAL) {
+		/*
+		 * We get -EINVAL if avdd is a supply with unknown voltage. We
+		 * still need to enable it since it is also a power supply.
+		 */
+		ret2 = devm_regulator_get_enable(dev, "avdd");
+		if (ret2)
+			return dev_err_probe(dev, ret2,
+					     "Failed to enable AVDD supply\n");
+	} else if (ret < 0) {
+		return dev_err_probe(dev, ret, "Failed to get AVDD voltage\n");
  	}

-	st->avdd = devm_regulator_get(dev, "avdd");
-	if (IS_ERR(st->avdd))
-		return PTR_ERR(st->avdd);
-
-	ret = regulator_enable(st->avdd);
-	if (ret)
-		return dev_err_probe(dev, ret,
-				     "Failed to enable specified AVdd supply\n");
-
-	ret = devm_add_action_or_reset(dev, ad7192_reg_disable, st->avdd);
-	if (ret)
-		return ret;
+	avdd_mv = (ret == -ENODEV) | (ret == -EINVAL) ? 0 : ret / MILLI;

  	ret = devm_regulator_get_enable(dev, "dvdd");
  	if (ret)
  		return dev_err_probe(dev, ret, "Failed to enable specified DVdd 
supply\n");

-	st->vref = devm_regulator_get_optional(dev, "vref");
-	if (IS_ERR(st->vref)) {
-		if (PTR_ERR(st->vref) != -ENODEV)
-			return PTR_ERR(st->vref);
-
-		ret = regulator_get_voltage(st->avdd);
-		if (ret < 0)
-			return dev_err_probe(dev, ret,
-					     "Device tree error, AVdd voltage undefined\n");
-	} else {
-		ret = regulator_enable(st->vref);
-		if (ret)
-			return dev_err_probe(dev, ret,
-					     "Failed to enable specified Vref supply\n");
-
-		ret = devm_add_action_or_reset(dev, ad7192_reg_disable, st->vref);
-		if (ret)
-			return ret;
-
-		ret = regulator_get_voltage(st->vref);
-		if (ret < 0)
-			return dev_err_probe(dev, ret,
-					     "Device tree error, Vref voltage undefined\n");
+	/*
+	 * This is either REFIN1 or REFIN2 depending on adi,refin2-pins-enable.
+	 * If this supply is not present, fall back to AVDD as reference.
+	 */
+	ret = devm_regulator_get_enable_read_voltage(dev, "vref");
+	if (ret == -ENODEV) {
+		if (avdd_mv == 0)
+			return dev_err_probe(dev, -ENODEV,
+					     "No reference voltage available\n");
+	} else if (ret < 0) {
+		return ret;
  	}
-	st->int_vref_mv = ret / 1000;
+
+	st->int_vref_mv = ret == -ENODEV ? avdd_mv : ret / MILLI;

  	st->chip_info = spi_get_device_match_data(spi);
  	indio_dev->name = st->chip_info->name;
David Lechner June 18, 2024, 1:30 p.m. UTC | #11
On 6/18/24 4:45 AM, Alisa-Dariana Roman wrote:
> On 17.06.2024 23:00, David Lechner wrote:
> ...
> 
>>
>> We could make a ret2 local variable inside of the if block to avoid writing over ret.
>>
> 
> Nice! If this looks alright, I will send it along my updated series.
> 
> From f84206b85b04f89d57b9293dd93e017efe8b350c Mon Sep 17 00:00:00 2001
> From: David Lechner <dlechner@baylibre.com>
> Date: Wed, 12 Jun 2024 16:03:05 -0500
> Subject: [PATCH] iio: adc: ad7192: use devm_regulator_get_enable_read_voltage
> 
> This makes use of the new devm_regulator_get_enable_read_voltage()
> function to reduce boilerplate code.
> 
> Error messages have changed slightly since there are now fewer places
> where we print an error. The rest of the logic of selecting which
> supply to use as the reference voltage remains the same.
> 
> Also 1000 is replaced by MILLI in a few places for consistency.
> 
> Signed-off-by: David Lechner <dlechner@baylibre.com>
> ---
>  drivers/iio/adc/ad7192.c | 101 +++++++++++++--------------------------
>  1 file changed, 34 insertions(+), 67 deletions(-)
> 
> diff --git a/drivers/iio/adc/ad7192.c b/drivers/iio/adc/ad7192.c
> index c7fb51a90e87..2448b01e0d59 100644
> --- a/drivers/iio/adc/ad7192.c
> +++ b/drivers/iio/adc/ad7192.c
> @@ -200,8 +200,6 @@ struct ad7192_chip_info {
> 
>  struct ad7192_state {
>      const struct ad7192_chip_info    *chip_info;
> -    struct regulator        *avdd;
> -    struct regulator        *vref;
>      struct clk            *mclk;
>      u16                int_vref_mv;
>      u32                aincom_mv;
> @@ -1189,18 +1187,12 @@ static const struct ad7192_chip_info ad7192_chip_info_tbl[] = {
>      },
>  };
> 
> -static void ad7192_reg_disable(void *reg)
> -{
> -    regulator_disable(reg);
> -}
> -
>  static int ad7192_probe(struct spi_device *spi)
>  {
>      struct device *dev = &spi->dev;
>      struct ad7192_state *st;
>      struct iio_dev *indio_dev;
> -    struct regulator *aincom;
> -    int ret;
> +    int ret, ret2, avdd_mv;
> 
>      if (!spi->irq)
>          return dev_err_probe(dev, -ENODEV, "Failed to get IRQ\n");
> @@ -1218,72 +1210,47 @@ static int ad7192_probe(struct spi_device *spi)
>       * Newer firmware should provide a zero volt fixed supply if wired to
>       * ground.
>       */
> -    aincom = devm_regulator_get_optional(dev, "aincom");
> -    if (IS_ERR(aincom)) {
> -        if (PTR_ERR(aincom) != -ENODEV)
> -            return dev_err_probe(dev, PTR_ERR(aincom),
> -                         "Failed to get AINCOM supply\n");
> -
> -        st->aincom_mv = 0;
> -    } else {
> -        ret = regulator_enable(aincom);
> -        if (ret)
> -            return dev_err_probe(dev, ret,
> -                         "Failed to enable specified AINCOM supply\n");
> -
> -        ret = devm_add_action_or_reset(dev, ad7192_reg_disable, aincom);
> -        if (ret)
> -            return ret;
> -
> -        ret = regulator_get_voltage(aincom);
> -        if (ret < 0)
> -            return dev_err_probe(dev, ret,
> -                         "Device tree error, AINCOM voltage undefined\n");
> -        st->aincom_mv = ret / MILLI;
> +    ret = devm_regulator_get_enable_read_voltage(dev, "aincom");
> +    if (ret < 0 && ret != -ENODEV)
> +        return dev_err_probe(dev, ret, "Failed to get AINCOM voltage\n");
> +
> +    st->aincom_mv = ret == -ENODEV ? 0 : ret / MILLI;
> +
> +    /* AVDD can optionally be used as reference voltage */
> +    ret = devm_regulator_get_enable_read_voltage(dev, "avdd");
> +    if (ret == -ENODEV || ret == -EINVAL) {

               int ret2;

I would declare ret2 here since it is the only place it is used.

> +        /*
> +         * We get -EINVAL if avdd is a supply with unknown voltage. We
> +         * still need to enable it since it is also a power supply.
> +         */
> +        ret2 = devm_regulator_get_enable(dev, "avdd");
> +        if (ret2)
> +            return dev_err_probe(dev, ret2,
> +                         "Failed to enable AVDD supply\n");
> +    } else if (ret < 0) {
> +        return dev_err_probe(dev, ret, "Failed to get AVDD voltage\n");
>      }
> 
> -    st->avdd = devm_regulator_get(dev, "avdd");
> -    if (IS_ERR(st->avdd))
> -        return PTR_ERR(st->avdd);
> -
> -    ret = regulator_enable(st->avdd);
> -    if (ret)
> -        return dev_err_probe(dev, ret,
> -                     "Failed to enable specified AVdd supply\n");
> -
> -    ret = devm_add_action_or_reset(dev, ad7192_reg_disable, st->avdd);
> -    if (ret)
> -        return ret;
> +    avdd_mv = (ret == -ENODEV) | (ret == -EINVAL) ? 0 : ret / MILLI;

This could be simplified to ret < 0.

Or if you want to leave it explicit, use || instead of |.

And () aren't really needed either.

> 
>      ret = devm_regulator_get_enable(dev, "dvdd");
>      if (ret)
>          return dev_err_probe(dev, ret, "Failed to enable specified DVdd supply\n");
> 
> -    st->vref = devm_regulator_get_optional(dev, "vref");
> -    if (IS_ERR(st->vref)) {
> -        if (PTR_ERR(st->vref) != -ENODEV)
> -            return PTR_ERR(st->vref);
> -
> -        ret = regulator_get_voltage(st->avdd);
> -        if (ret < 0)
> -            return dev_err_probe(dev, ret,
> -                         "Device tree error, AVdd voltage undefined\n");
> -    } else {
> -        ret = regulator_enable(st->vref);
> -        if (ret)
> -            return dev_err_probe(dev, ret,
> -                         "Failed to enable specified Vref supply\n");
> -
> -        ret = devm_add_action_or_reset(dev, ad7192_reg_disable, st->vref);
> -        if (ret)
> -            return ret;
> -
> -        ret = regulator_get_voltage(st->vref);
> -        if (ret < 0)
> -            return dev_err_probe(dev, ret,
> -                         "Device tree error, Vref voltage undefined\n");
> +    /*
> +     * This is either REFIN1 or REFIN2 depending on adi,refin2-pins-enable.
> +     * If this supply is not present, fall back to AVDD as reference.
> +     */
> +    ret = devm_regulator_get_enable_read_voltage(dev, "vref");
> +    if (ret == -ENODEV) {
> +        if (avdd_mv == 0)
> +            return dev_err_probe(dev, -ENODEV,
> +                         "No reference voltage available\n");
> +    } else if (ret < 0) {
> +        return ret;
>      }
> -    st->int_vref_mv = ret / 1000;
> +
> +    st->int_vref_mv = ret == -ENODEV ? avdd_mv : ret / MILLI;
> 
>      st->chip_info = spi_get_device_match_data(spi);
>      indio_dev->name = st->chip_info->name;
diff mbox series

Patch

diff --git a/drivers/iio/adc/ad7192.c b/drivers/iio/adc/ad7192.c
index 0789121236d6..409c73d16460 100644
--- a/drivers/iio/adc/ad7192.c
+++ b/drivers/iio/adc/ad7192.c
@@ -200,8 +200,6 @@  struct ad7192_chip_info {
 
 struct ad7192_state {
 	const struct ad7192_chip_info	*chip_info;
-	struct regulator		*avdd;
-	struct regulator		*vref;
 	struct clk			*mclk;
 	u16				int_vref_mv;
 	u32				aincom_mv;
@@ -1189,17 +1187,11 @@  static const struct ad7192_chip_info ad7192_chip_info_tbl[] = {
 	},
 };
 
-static void ad7192_reg_disable(void *reg)
-{
-	regulator_disable(reg);
-}
-
 static int ad7192_probe(struct spi_device *spi)
 {
 	struct ad7192_state *st;
 	struct iio_dev *indio_dev;
-	struct regulator *aincom;
-	int ret;
+	int ret, avdd_mv;
 
 	if (!spi->irq) {
 		dev_err(&spi->dev, "no IRQ?\n");
@@ -1219,74 +1211,52 @@  static int ad7192_probe(struct spi_device *spi)
 	 * Newer firmware should provide a zero volt fixed supply if wired to
 	 * ground.
 	 */
-	aincom = devm_regulator_get_optional(&spi->dev, "aincom");
-	if (IS_ERR(aincom)) {
-		if (PTR_ERR(aincom) != -ENODEV)
-			return dev_err_probe(&spi->dev, PTR_ERR(aincom),
-					     "Failed to get AINCOM supply\n");
-
-		st->aincom_mv = 0;
-	} else {
-		ret = regulator_enable(aincom);
+	ret = devm_regulator_get_enable_read_voltage(&spi->dev, "aincom");
+	if (ret < 0 && ret != -ENODEV)
+		return dev_err_probe(&spi->dev, ret, "Failed to get AINCOM voltage\n");
+
+	st->aincom_mv = ret == -ENODEV ? 0 : ret / MILLI;
+
+	/* AVDD can optionally be used as reference voltage */
+	ret = devm_regulator_get_enable_read_voltage(&spi->dev, "avdd");
+	if (ret == -ENODEV || ret == -EINVAL) {
+		/*
+		 * We get -EINVAL if avdd is a supply with unknown voltage. We
+		 * still need to enable it since it is also a power supply.
+		 */
+		ret = devm_regulator_get_enable(&spi->dev, "avdd");
 		if (ret)
 			return dev_err_probe(&spi->dev, ret,
-					     "Failed to enable specified AINCOM supply\n");
+					     "Failed to enable AVDD supply\n");
 
-		ret = devm_add_action_or_reset(&spi->dev, ad7192_reg_disable, aincom);
-		if (ret)
-			return ret;
-
-		ret = regulator_get_voltage(aincom);
-		if (ret < 0)
-			return dev_err_probe(&spi->dev, ret,
-					     "Device tree error, AINCOM voltage undefined\n");
-		st->aincom_mv = ret / MILLI;
+		avdd_mv = 0;
+	} else if (ret < 0) {
+		return dev_err_probe(&spi->dev, ret, "Failed to get AVDD voltage\n");
+	} else {
+		avdd_mv = ret / MILLI;
 	}
 
-	st->avdd = devm_regulator_get(&spi->dev, "avdd");
-	if (IS_ERR(st->avdd))
-		return PTR_ERR(st->avdd);
-
-	ret = regulator_enable(st->avdd);
-	if (ret) {
-		dev_err(&spi->dev, "Failed to enable specified AVdd supply\n");
-		return ret;
-	}
-
-	ret = devm_add_action_or_reset(&spi->dev, ad7192_reg_disable, st->avdd);
-	if (ret)
-		return ret;
 
 	ret = devm_regulator_get_enable(&spi->dev, "dvdd");
 	if (ret)
 		return dev_err_probe(&spi->dev, ret, "Failed to enable specified DVdd supply\n");
 
-	st->vref = devm_regulator_get_optional(&spi->dev, "vref");
-	if (IS_ERR(st->vref)) {
-		if (PTR_ERR(st->vref) != -ENODEV)
-			return PTR_ERR(st->vref);
-
-		ret = regulator_get_voltage(st->avdd);
-		if (ret < 0)
-			return dev_err_probe(&spi->dev, ret,
-					     "Device tree error, AVdd voltage undefined\n");
+	/*
+	 * This is either REFIN1 or REFIN2 depending on adi,refin2-pins-enable.
+	 * If this supply is not present, fall back to AVDD as reference.
+	 */
+	ret = devm_regulator_get_enable_read_voltage(&spi->dev, "vref");
+	if (ret == -ENODEV) {
+		if (avdd_mv == 0)
+			return dev_err_probe(&spi->dev, -ENODEV,
+					     "No reference voltage available\n");
+
+		st->int_vref_mv = avdd_mv;
+	} else if (ret < 0) {
+		return ret;
 	} else {
-		ret = regulator_enable(st->vref);
-		if (ret) {
-			dev_err(&spi->dev, "Failed to enable specified Vref supply\n");
-			return ret;
-		}
-
-		ret = devm_add_action_or_reset(&spi->dev, ad7192_reg_disable, st->vref);
-		if (ret)
-			return ret;
-
-		ret = regulator_get_voltage(st->vref);
-		if (ret < 0)
-			return dev_err_probe(&spi->dev, ret,
-					     "Device tree error, Vref voltage undefined\n");
+		st->int_vref_mv = ret / MILLI;
 	}
-	st->int_vref_mv = ret / 1000;
 
 	st->chip_info = spi_get_device_match_data(spi);
 	indio_dev->name = st->chip_info->name;