All of lore.kernel.org
 help / color / mirror / Atom feed
From: Peng Fan <peng.fan@oss.nxp.com>
To: Rob Herring <robh@kernel.org>
Cc: Saravana Kannan <saravanak@kernel.org>,
	Liam Girdwood <lgirdwood@gmail.com>,
	Mark Brown <broonie@kernel.org>,
	Alexis Czezar Torreno <alexisczezar.torreno@analog.com>,
	devicetree@vger.kernel.org, linux-kernel@vger.kernel.org,
	Peng Fan <peng.fan@nxp.com>
Subject: Re: [PATCH 1/5] of: Add of_property_read_[u32,s32]_default
Date: Fri, 23 Jan 2026 14:58:05 +0800	[thread overview]
Message-ID: <aXMb/bkMoaOiM6sX@shlinux89> (raw)
In-Reply-To: <20260120222155.GA1226342-robh@kernel.org>

Hi Rob,

On Tue, Jan 20, 2026 at 04:21:55PM -0600, Rob Herring wrote:
>On Mon, Jan 19, 2026 at 10:02:54AM +0800, Peng Fan (OSS) wrote:
>> From: Peng Fan <peng.fan@nxp.com>
>> 
>> Introduce new helper functions of_property_read_u32_default() and
>> of_property_read_s32_default() to simplify reading optional device tree
>> properties with a default value.
>> 
>> A very common pattern in drivers is to provide a default value and let
>> of_property_read_*() override it when the property is present, e.g.:
>> 
>>     Y = Y_DEFAULT;
>>     of_property_read_u32(np, "prop", &Y);
>
>This is how defaults were intended to be handled.
>> 
>> or equivalently, checking the return value explicitly:
>> 
>>     ret = of_property_read_u32(np, "prop", &val);
>>     if (ret)
>>         Y = Y_DEFAULT;
>>     else
>>         Y = val;
>
>This is usually only needed if the variable type is different. Probably 
>the better fix is fix the type difference.

I see. There are a few places that use above style to set default value,
no type conversion.

such as
drivers/clk/socfpga/clk-gate-a10.c:71
drivers/clk/socfpga/clk-periph-a10.c:90

>
>> Both forms express the same intent: the property is optional and a
>> well-defined default should be used if it cannot be read.
>> 
>> With the new helper, this can be expressed more directly as:
>> 
>>     Y = of_property_read_u32_default(np, "prop", Y_DEFAULT);
>> 
>> The helpers intentionally ignore the error code and return either the
>> parsed value or the supplied default. They are meant for optional
>> properties only. Callers that need to handle or propagate errors should
>> continue using of_property_read_*() directly.
>
>What about u8, u16, etc. and device_property_read_*? I'm really on the 
>fence whether this is all worth it...

My cocci only reports some u32 usage using style "if.. else..".
Let me write a cocci pattern to scan device_property_read_* usage.

>
>We may also want to do something like of_property_read() implemented 
>using C11 _Generic(). Not sure if that's worth the churn either. It 
>would make doing some type checks harder. For example I could extract 
>all property names from of_property_read_u32() calls and check their 
>size against the schemas. (I have the first half of that already.) Using 
>_Generic() would make that harder or impossible.

I see. _Generic may not be a good idea if it makes type checks harder.

Thanks
Peng

>
>Rob
>

  reply	other threads:[~2026-01-23  6:58 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-19  2:02 [PATCH 0/5] of: Introduce *_read_*_default helpers and convert regulator drivers Peng Fan (OSS)
2026-01-19  2:02 ` [PATCH 1/5] of: Add of_property_read_[u32,s32]_default Peng Fan (OSS)
2026-01-20 22:21   ` Rob Herring
2026-01-23  6:58     ` Peng Fan [this message]
2026-01-19  2:02 ` [PATCH 2/5] regulator: of: Use of_property_read_u32_default() Peng Fan (OSS)
2026-01-19  2:02 ` [PATCH 3/5] regulator: adp5055: use of_property_read_[u32|s32]_default() Peng Fan (OSS)
2026-01-19  2:02 ` [PATCH 4/5] regulator: max77620: Use of_property_read_u32_default() for DT parsing Peng Fan (OSS)
2026-01-19  2:02 ` [PATCH 5/5] regulator: fan53555: " Peng Fan (OSS)

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=aXMb/bkMoaOiM6sX@shlinux89 \
    --to=peng.fan@oss.nxp.com \
    --cc=alexisczezar.torreno@analog.com \
    --cc=broonie@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=lgirdwood@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=peng.fan@nxp.com \
    --cc=robh@kernel.org \
    --cc=saravanak@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.