How to represent negative values for device tree property
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
Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, hosted by The Linux Foundation
Quoted 9 lines · David Collins
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.
Quoted 11 lines · Stephen Warren
Quoted 9 lines · David Collins
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:
Quoted 7 lines
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?
Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, hosted by The Linux Foundation
Quoted 12 lines · David Collins
Quoted 11 lines · Stephen Warren
Quoted 9 lines · David Collins
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:
Quoted 7 lines
Quoted 7 lines
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.
Quoted 9 lines
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.
Quoted 2 lines
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?
Signature · 11 lines
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>
Quoted 26 lines · David Gibson
Quoted 13 lines · David Collins
Quoted 12 lines · Stephen Warren
Quoted 10 lines · David Collins
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:
Quoted 7 lines
Quoted 7 lines
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.
Quoted 8 lines
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.
Quoted 9 lines · David Collins
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.
Quoted 10 lines
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.
Quoted 7 lines
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.
Quoted 3 lines
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).
Signature · 11 lines
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>