linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
* How to represent negative values for device tree property
@ 2013-04-01 21:08 David Collins
  2013-04-01 22:00 ` Stephen Warren
  2013-04-02  6:17 ` David Gibson
  0 siblings, 2 replies; 6+ messages in thread
From: David Collins @ 2013-04-01 21:08 UTC (permalink / raw)
  To: linux-arm-kernel

Hi,

I am working on a thermal driver which needs to be able to read a
temperature threshold from a device tree property.  The hardware supports
thresholds in the range -204.8 to +204.7 C in 0.1 C steps.  I have found,
as I am sure others have as well, that dtc treats a '-' before an integer
in a dtsi file as a syntax error.  Therefore, I need some artificial way
to represent negative numbers in device tree.  Here are the possibilities
that I have thought of so far:

1. Use a second integer to specify the sign of the threshold:
     20000 mC --> <0 20000>
    -20000 mC --> <1 20000>
2. Use a string instead of an integer to specify the threshold and
   then convert it to an integer in the driver software:
     20000 mC --> "20000"
    -20000 mC --> "-20000"
3. Use units of millikelvin instead of millicelcius.  0 mC == 273150 mK
     20000 mC --> <293150>
    -20000 mC --> <253150>
4. Use an arbitrary offset e.g. 0 mC == 1000000
     20000 mC --> <1020000>
    -20000 mC --> <980000>
5. Use the unsigned 32-bit representation for 2?s-compliment
   signed 32-bit integer
     20000 mC --> <20000>
    -20000 mC --> <0xffffb1e0> or <4294947296>

Which of these options would you recommend using?  Is there any better way
to handle negative values that I haven?t listed?  What is the best general
case solution for specifying negative numbers in device tree?

Thanks,
David

-- 
The Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum,
hosted by The Linux Foundation

^ permalink raw reply	[flat|nested] 6+ messages in thread

* How to represent negative values for device tree property
  2013-04-01 21:08 How to represent negative values for device tree property David Collins
@ 2013-04-01 22:00 ` Stephen Warren
  2013-04-02  0:24   ` David Collins
  2013-04-02  6:17 ` David Gibson
  1 sibling, 1 reply; 6+ messages in thread
From: Stephen Warren @ 2013-04-01 22:00 UTC (permalink / raw)
  To: linux-arm-kernel

On 04/01/2013 03:08 PM, David Collins wrote:
> Hi,
> 
> I am working on a thermal driver which needs to be able to read a
> temperature threshold from a device tree property.  The hardware supports
> thresholds in the range -204.8 to +204.7 C in 0.1 C steps.  I have found,
> as I am sure others have as well, that dtc treats a '-' before an integer
> in a dtsi file as a syntax error.  Therefore, I need some artificial way
> to represent negative numbers in device tree.  Here are the possibilities
> that I have thought of so far:

Doesn't the very latest dtc, which contains integer expression support,
allow unary -? I thought that code had been imported into the kernel
(goes and checks) yes it has. What's the error you're seeing;
over/underflow or syntax?

That said, DT cells are supposed to be u32 not s32, so perhaps this
isn't unexpected.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* How to represent negative values for device tree property
  2013-04-01 22:00 ` Stephen Warren
@ 2013-04-02  0:24   ` David Collins
  2013-04-02  6:29     ` David Gibson
  0 siblings, 1 reply; 6+ messages in thread
From: David Collins @ 2013-04-02  0:24 UTC (permalink / raw)
  To: linux-arm-kernel

On 04/01/2013 03:00 PM, Stephen Warren wrote:
> On 04/01/2013 03:08 PM, David Collins wrote:
>> Hi,
>>
>> I am working on a thermal driver which needs to be able to read a
>> temperature threshold from a device tree property.  The hardware supports
>> thresholds in the range -204.8 to +204.7 C in 0.1 C steps.  I have found,
>> as I am sure others have as well, that dtc treats a '-' before an integer
>> in a dtsi file as a syntax error.  Therefore, I need some artificial way
>> to represent negative numbers in device tree.  Here are the possibilities
>> that I have thought of so far:
> 
> Doesn't the very latest dtc, which contains integer expression support,
> allow unary -? I thought that code had been imported into the kernel
> (goes and checks) yes it has. What's the error you're seeing;
> over/underflow or syntax?
> 
> That said, DT cells are supposed to be u32 not s32, so perhaps this
> isn't unexpected.

It is likely that my dtc version is out of date.  dtc -v outputs 1.2.0.  I
will try updating to a newer version of dtc.  The error that I currently
get is a syntax error: "FATAL ERROR: Unable to parse input tree".

Does the device tree binary documentation define any format for negative
numbers?  Unsigned 32-bit integers are clearly defined as bytes in
big-endian order.  I suppose that you could assume 32-bit signed integers
are 2's complement with bytes in big-endian order, but that would need to
be well defined somewhere.

Assuming that dtb has no well defined means of holding a negative integer
value, then what is the most elegant workaround to specify a negative value?

-- 
The Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum,
hosted by The Linux Foundation

^ permalink raw reply	[flat|nested] 6+ messages in thread

* How to represent negative values for device tree property
  2013-04-01 21:08 How to represent negative values for device tree property David Collins
  2013-04-01 22:00 ` Stephen Warren
@ 2013-04-02  6:17 ` David Gibson
  1 sibling, 0 replies; 6+ messages in thread
From: David Gibson @ 2013-04-02  6:17 UTC (permalink / raw)
  To: linux-arm-kernel

On Mon, Apr 01, 2013 at 02:08:48PM -0700, David Collins wrote:
> Hi,
> 
> I am working on a thermal driver which needs to be able to read a
> temperature threshold from a device tree property.  The hardware supports
> thresholds in the range -204.8 to +204.7 C in 0.1 C steps.  I have found,
> as I am sure others have as well, that dtc treats a '-' before an integer
> in a dtsi file as a syntax error.  Therefore, I need some artificial way
> to represent negative numbers in device tree.  Here are the possibilities
> that I have thought of so far:

So, as a later posted suggests, dtc does now have integer expression
support, including unary -.  The catch is you have to put your
expression in parentheses, so < (-200) > should work where < -200 >
does not.  The reason for this is that otherwise something like
	<500 -300>
is ambiguous.  It could either mean <(500 -300)> == <200> or it could
mean <(500) (-300)>.

It's a bit of a pain, but there's not really any way of fixing it that
doesn't cause more pain along the way.

Well.. while writing that I realised its a bit more complicated.  We
could allow negative literals (similar but different in parsing from
unary -).  But that causes lexing/parsing problems since then
(500-300) (no spaces) would, without extra fiddling, get lexed as:
	"(" LITERAL(500) LITERAL(-300) ")"
whereas the parser would need to see it as
	"(" LITERAL(500) "-" LITERAL(300) ")"
So, maybe there's a way but I haven't thought of it yet.


> 1. Use a second integer to specify the sign of the threshold:
>      20000 mC --> <0 20000>
>     -20000 mC --> <1 20000>
> 2. Use a string instead of an integer to specify the threshold and
>    then convert it to an integer in the driver software:
>      20000 mC --> "20000"
>     -20000 mC --> "-20000"
> 3. Use units of millikelvin instead of millicelcius.  0 mC == 273150 mK
>      20000 mC --> <293150>
>     -20000 mC --> <253150>

There's always something to be said for using SI units, regardless of
any other considerations.

> 4. Use an arbitrary offset e.g. 0 mC == 1000000
>      20000 mC --> <1020000>
>     -20000 mC --> <980000>
> 5. Use the unsigned 32-bit representation for 2?s-compliment
>    signed 32-bit integer
>      20000 mC --> <20000>
>     -20000 mC --> <0xffffb1e0> or <4294947296>

This is effectively what use of unary - in dtc will do.

> Which of these options would you recommend using?  Is there any better way
> to handle negative values that I haven?t listed?  What is the best general
> case solution for specifying negative numbers in device tree?

In short both (3) and (5) are reasonable, and dtc will help you with
(5).  For the love of god, don't do (1), (2) or (4).

-- 
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: Digital signature
URL: <http://lists.infradead.org/pipermail/linux-arm-kernel/attachments/20130402/10434b1e/attachment-0001.sig>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* How to represent negative values for device tree property
  2013-04-02  0:24   ` David Collins
@ 2013-04-02  6:29     ` David Gibson
  2013-04-02 15:36       ` Stephen Warren
  0 siblings, 1 reply; 6+ messages in thread
From: David Gibson @ 2013-04-02  6:29 UTC (permalink / raw)
  To: linux-arm-kernel

On Mon, Apr 01, 2013 at 05:24:19PM -0700, David Collins wrote:
> On 04/01/2013 03:00 PM, Stephen Warren wrote:
> > On 04/01/2013 03:08 PM, David Collins wrote:
> >> Hi,
> >>
> >> I am working on a thermal driver which needs to be able to read a
> >> temperature threshold from a device tree property.  The hardware supports
> >> thresholds in the range -204.8 to +204.7 C in 0.1 C steps.  I have found,
> >> as I am sure others have as well, that dtc treats a '-' before an integer
> >> in a dtsi file as a syntax error.  Therefore, I need some artificial way
> >> to represent negative numbers in device tree.  Here are the possibilities
> >> that I have thought of so far:
> > 
> > Doesn't the very latest dtc, which contains integer expression support,
> > allow unary -? I thought that code had been imported into the kernel
> > (goes and checks) yes it has. What's the error you're seeing;
> > over/underflow or syntax?
> > 
> > That said, DT cells are supposed to be u32 not s32, so perhaps this
> > isn't unexpected.

That's.. sort of true, but misleading.  As far as the dtb format is
concerned, properties are just a bag of bytes.  Individual device
bindings define how software should interpret those bytes.

The dtc data "types" are really just convenient ways to enter various
sorts of commonly used bytestrings.  Some of the dtc code treats cells
as u32 because that works for its purposes (and in particular avoids
some nasty C standard gotchas where signed overflows may have
undefined results), but there's no reason you can't treat it as an
s32.  On dtc versions recent enough to have arithmetic, unary minus
and subtractive overflow will use 2's complement, as you'd expect.

> It is likely that my dtc version is out of date.  dtc -v outputs 1.2.0.  I
> will try updating to a newer version of dtc.  The error that I currently
> get is a syntax error: "FATAL ERROR: Unable to parse input tree".
> 
> Does the device tree binary documentation define any format for negative
> numbers?  Unsigned 32-bit integers are clearly defined as bytes in
> big-endian order.  I suppose that you could assume 32-bit signed integers
> are 2's complement with bytes in big-endian order, but that would need to
> be well defined somewhere.

Yes, it should be defined somewhere, but that only needs to be the
binding for your specific device, not anywhere more general.

> Assuming that dtb has no well defined means of holding a negative integer
> value, then what is the most elegant workaround to specify a negative value?

-- 
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: Digital signature
URL: <http://lists.infradead.org/pipermail/linux-arm-kernel/attachments/20130402/d6b9d463/attachment.sig>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* How to represent negative values for device tree property
  2013-04-02  6:29     ` David Gibson
@ 2013-04-02 15:36       ` Stephen Warren
  0 siblings, 0 replies; 6+ messages in thread
From: Stephen Warren @ 2013-04-02 15:36 UTC (permalink / raw)
  To: linux-arm-kernel

On 04/02/2013 12:29 AM, David Gibson wrote:
> On Mon, Apr 01, 2013 at 05:24:19PM -0700, David Collins wrote:
>> On 04/01/2013 03:00 PM, Stephen Warren wrote:
>>> On 04/01/2013 03:08 PM, David Collins wrote:
>>>> Hi,
>>>> 
>>>> I am working on a thermal driver which needs to be able to
>>>> read a temperature threshold from a device tree property.
>>>> The hardware supports thresholds in the range -204.8 to
>>>> +204.7 C in 0.1 C steps.  I have found, as I am sure others
>>>> have as well, that dtc treats a '-' before an integer in a
>>>> dtsi file as a syntax error.  Therefore, I need some
>>>> artificial way to represent negative numbers in device tree.
>>>> Here are the possibilities that I have thought of so far:
>>> 
>>> Doesn't the very latest dtc, which contains integer expression
>>> support, allow unary -? I thought that code had been imported
>>> into the kernel (goes and checks) yes it has. What's the error
>>> you're seeing; over/underflow or syntax?
>>> 
>>> That said, DT cells are supposed to be u32 not s32, so perhaps
>>> this isn't unexpected.
> 
> That's.. sort of true, but misleading.  As far as the dtb format
> is concerned, properties are just a bag of bytes.  Individual
> device bindings define how software should interpret those bytes.
> 
> The dtc data "types" are really just convenient ways to enter
> various sorts of commonly used bytestrings.  Some of the dtc code
> treats cells as u32 because that works for its purposes (and in
> particular avoids some nasty C standard gotchas where signed
> overflows may have undefined results), but there's no reason you
> can't treat it as an s32.  On dtc versions recent enough to have
> arithmetic, unary minus and subtractive overflow will use 2's
> complement, as you'd expect.

OK, perhaps I was extrapolating too far from the fact that all the
DT-related code in the kernel (just happens to) only read integers as
u32. It sounds like it'd be fine to add e.g. of_property_read_s32()
alongside the existing of_property_read_u32() then. I imagine that
would be quite useful.

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2013-04-02 15:36 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-04-01 21:08 How to represent negative values for device tree property David Collins
2013-04-01 22:00 ` Stephen Warren
2013-04-02  0:24   ` David Collins
2013-04-02  6:29     ` David Gibson
2013-04-02 15:36       ` Stephen Warren
2013-04-02  6:17 ` David Gibson

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).