devicetree.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Matti Vaittinen <mazziesaccount@gmail.com>
To: Jonathan Cameron <Jonathan.Cameron@Huawei.com>
Cc: Christophe JAILLET <christophe.jaillet@wanadoo.fr>,
	ak@it-klinger.de, andriy.shevchenko@linux.intel.com,
	ang.iglesiasg@gmail.com, bbara93@gmail.com, conor+dt@kernel.org,
	devicetree@vger.kernel.org, jic23@kernel.org,
	krzysztof.kozlowski+dt@linaro.org, lars@metafoo.de,
	linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org,
	matti.vaittinen@fi.rohmeurope.com, robh+dt@kernel.org
Subject: Re: [PATCH v2 2/3] iio: pressure: Support ROHM BU1390
Date: Fri, 22 Sep 2023 09:07:42 +0300	[thread overview]
Message-ID: <a7ee4d4b-ae2c-32fe-e471-5bd67f2bddbf@gmail.com> (raw)
In-Reply-To: <20230919153251.000024d3@Huawei.com>

On 9/19/23 17:32, Jonathan Cameron wrote:
> 
>>>> +static int bm1390_read_raw(struct iio_dev *idev,
>>>> +               struct iio_chan_spec const *chan,
>>>> +               int *val, int *val2, long mask)
>>>> +{
>>>> +    struct bm1390_data *data = iio_priv(idev);
>>>> +    int ret;
>>>> +
>>>> +    switch (mask) {
>>>> +    case IIO_CHAN_INFO_SCALE:
>>>> +        if (chan->type == IIO_TEMP) {
>>>> +            *val = 31;
>>>> +            *val2 = 250000;
>>>> +
>>>> +            return IIO_VAL_INT_PLUS_MICRO;
>>>> +        } else if (chan->type == IIO_PRESSURE) {
>>>> +            *val = 0;
>>>> +            /*
>>>> +             * pressure in hPa is register value divided by 2048.
>>>> +             * This means kPa is 1/20480 times the register value,
>>>> +             * which equals to 48828.125 * 10 ^ -9
>>>> +             * This is 48828.125 nano kPa.
>>>> +             *
>>>> +             * When we scale this using IIO_VAL_INT_PLUS_NANO we
>>>> +             * get 48828 - which means we lose some accuracy. Well,
>>>> +             * let's try to live with that.
>>>> +             */
>>>> +            *val2 = 48828;
>>>> +
>>>> +            return IIO_VAL_INT_PLUS_NANO;
>>>> +        }
>>>> +
>>>> +        return -EINVAL;
>>>> +    case IIO_CHAN_INFO_RAW:
>>>> +        ret = iio_device_claim_direct_mode(idev);
>>>> +        if (ret)
>>>> +            return ret;
>>>> +
>>>> +        ret = bm1390_read_data(data, chan, val, val2);
>>>> +        iio_device_release_direct_mode(idev);
>>>> +        if (ret)
>>>> +            return ret;
>>>> +
>>>> +        return IIO_VAL_INT;
>>>> +    default:
>>>> +        return -EINVAL;
>>>
>>> Certainly useless, but should we break and return -EINVAL after the
>>> switch, so that it is more explicit that bm1390_read_raw() always
>>> returns a value?
>>
>> I think there is also opposite opinions on this. For my eyes the return
>> at the end of the function would also be clearer - but I think I have
>> been asked to drop the useless return when I've been working with other
>> sensors in IIO domain :) My personal preference would definitely be:
>>
>> int ret;
>>
>> switch (foo)
>> {
>> case BAR:
>> 	ret = func1();
>> 	if (ret)
>> 		break;
>>
>> 	ret = func2();
>> 	if (ret)
>> 		break;
>>
>> ...
>> 	break;
>>
>> case BAZ:
>> 	ret = -EINVAL;
>> 	break;
>> }
>>
>> return ret;
>>
>> - but I've learned to think this is not the IIO preference.
> 
> Some static analyzers get confused (probably when there is a little
> bit more going on after the function) by that and moan that some
> cases are not considered in the switch.  I got annoyed enough with the
> noise they were generating to advocate always having explicit defaults.

Oh, yes. I see I omitted the default from the example - but this was not 
what I tried to highlight ;) With a bit more thought I would've added:

default:
	ret = -EINVAL;
	break;

As you probably guess, what I was after is that for a simple (not deeply 
nested) cases like this, I would rather use a variable for return value 
and a single point of exit at the end of the function - instead of 
having returns in the switch-case. That'd suit better _my_ taste.

Yours,
	-- Matti

-- 
Matti Vaittinen
Linux kernel developer at ROHM Semiconductors
Oulu Finland

~~ When things go utterly wrong vim users can always type :help! ~~


  reply	other threads:[~2023-09-22  6:07 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-15  6:55 [PATCH v2 0/3] Support ROHM BM1390 pressure sensor Matti Vaittinen
2023-09-15  6:55 ` [PATCH v2 1/3] dt-bindings: Add " Matti Vaittinen
2023-09-15  6:56 ` [PATCH v2 2/3] iio: pressure: Support ROHM BU1390 Matti Vaittinen
2023-09-16  8:01   ` Christophe JAILLET
2023-09-17  9:56     ` Jonathan Cameron
2023-09-18 11:39     ` Matti Vaittinen
2023-09-19 14:32       ` Jonathan Cameron
2023-09-22  6:07         ` Matti Vaittinen [this message]
2023-09-17 10:35   ` Jonathan Cameron
2023-09-18 12:56     ` Matti Vaittinen
2023-09-19 11:28       ` Matti Vaittinen
2023-09-19 14:53         ` Jonathan Cameron
2023-09-21  8:17           ` Matti Vaittinen
2023-09-21  9:00             ` Matti Vaittinen
2023-09-24 12:14               ` Jonathan Cameron
2023-09-24 12:12             ` Jonathan Cameron
2023-09-19 14:45       ` Jonathan Cameron
2023-09-15  6:56 ` [PATCH v2 3/3] MAINTAINERS: Add ROHM BM1390 Matti Vaittinen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=a7ee4d4b-ae2c-32fe-e471-5bd67f2bddbf@gmail.com \
    --to=mazziesaccount@gmail.com \
    --cc=Jonathan.Cameron@Huawei.com \
    --cc=ak@it-klinger.de \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=ang.iglesiasg@gmail.com \
    --cc=bbara93@gmail.com \
    --cc=christophe.jaillet@wanadoo.fr \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=jic23@kernel.org \
    --cc=krzysztof.kozlowski+dt@linaro.org \
    --cc=lars@metafoo.de \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=matti.vaittinen@fi.rohmeurope.com \
    --cc=robh+dt@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).