All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/2] of: add support for reading s32 property value
@ 2013-12-12 13:24 ` Laxman Dewangan
  0 siblings, 0 replies; 7+ messages in thread
From: Laxman Dewangan @ 2013-12-12 13:24 UTC (permalink / raw)
  To: robh+dt, pawel.moll, mark.rutland, grant.likely
  Cc: ijc+devicetree, galak, linux-kernel, devicetree, swarren,
	kerwinw, Laxman Dewangan, david

Add of_property_read_s32() to read the signed 32bit number
from dt property value. This supports to pass the -ve numbers
from dt. Use 2's complement method for represnting negative number
and passed as u32 from dts. When reading back the value, again
converted to 2's complement if msb shows as 1.

Signed-off-by: Laxman Dewangan <ldewangan@nvidia.com>
CC: david@gibson.dropbear.id.au
CC: swarren@nvidia.com
---
We required to pass the -ve value from DT and after looking the discussion
http://www.mail-archive.com/linux-arm-msm@vger.kernel.org/msg05108.html
I created this patch.
I CCed to David and Stephen on this.


 include/linux/of.h |   16 ++++++++++++++++
 1 files changed, 16 insertions(+), 0 deletions(-)

diff --git a/include/linux/of.h b/include/linux/of.h
index 276c546..17c0327 100644
--- a/include/linux/of.h
+++ b/include/linux/of.h
@@ -601,6 +601,22 @@ static inline int of_property_read_u32(const struct device_node *np,
 	return of_property_read_u32_array(np, propname, out_value, 1);
 }
 
+static inline int of_property_read_s32(const struct device_node *np,
+				       const char *propname,
+				       s32 *out_value)
+{
+	u32 val;
+	int ret;
+
+	ret = of_property_read_u32(np, propname, &val);
+	if (ret < 0)
+		return ret;
+
+	/* 2's complement if MSB is 1 */
+	*out_value = (val & 0x80000000U) ? -((val ^ 0xFFFFFFFFU) + 1) : val;
+	return ret;
+}
+
 #define of_property_for_each_u32(np, propname, prop, p, u)	\
 	for (prop = of_find_property(np, propname, NULL),	\
 		p = of_prop_next_u32(prop, NULL, &u);		\
-- 
1.7.0.4


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

* [PATCH 1/2] of: add support for reading s32 property value
@ 2013-12-12 13:24 ` Laxman Dewangan
  0 siblings, 0 replies; 7+ messages in thread
From: Laxman Dewangan @ 2013-12-12 13:24 UTC (permalink / raw)
  To: robh+dt, pawel.moll, mark.rutland, grant.likely
  Cc: ijc+devicetree, galak, linux-kernel, devicetree, swarren,
	kerwinw, Laxman Dewangan, david

Add of_property_read_s32() to read the signed 32bit number
from dt property value. This supports to pass the -ve numbers
from dt. Use 2's complement method for represnting negative number
and passed as u32 from dts. When reading back the value, again
converted to 2's complement if msb shows as 1.

Signed-off-by: Laxman Dewangan <ldewangan@nvidia.com>
CC: david@gibson.dropbear.id.au
CC: swarren@nvidia.com
---
We required to pass the -ve value from DT and after looking the discussion
http://www.mail-archive.com/linux-arm-msm@vger.kernel.org/msg05108.html
I created this patch.
I CCed to David and Stephen on this.


 include/linux/of.h |   16 ++++++++++++++++
 1 files changed, 16 insertions(+), 0 deletions(-)

diff --git a/include/linux/of.h b/include/linux/of.h
index 276c546..17c0327 100644
--- a/include/linux/of.h
+++ b/include/linux/of.h
@@ -601,6 +601,22 @@ static inline int of_property_read_u32(const struct device_node *np,
 	return of_property_read_u32_array(np, propname, out_value, 1);
 }
 
+static inline int of_property_read_s32(const struct device_node *np,
+				       const char *propname,
+				       s32 *out_value)
+{
+	u32 val;
+	int ret;
+
+	ret = of_property_read_u32(np, propname, &val);
+	if (ret < 0)
+		return ret;
+
+	/* 2's complement if MSB is 1 */
+	*out_value = (val & 0x80000000U) ? -((val ^ 0xFFFFFFFFU) + 1) : val;
+	return ret;
+}
+
 #define of_property_for_each_u32(np, propname, prop, p, u)	\
 	for (prop = of_find_property(np, propname, NULL),	\
 		p = of_prop_next_u32(prop, NULL, &u);		\
-- 
1.7.0.4

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

* [PATCH 2/2] dt-binding: add header to define types and conversion
  2013-12-12 13:24 ` Laxman Dewangan
@ 2013-12-12 13:24   ` Laxman Dewangan
  -1 siblings, 0 replies; 7+ messages in thread
From: Laxman Dewangan @ 2013-12-12 13:24 UTC (permalink / raw)
  To: robh+dt, pawel.moll, mark.rutland, grant.likely
  Cc: ijc+devicetree, galak, linux-kernel, devicetree, swarren,
	kerwinw, Laxman Dewangan

Add a header file to define the macros for type conversion
to be used by DTS file.

Add macro for the S32 to U32 conversion by using 2's complement.

Signed-off-by: Laxman Dewangan <ldewangan@nvidia.com>
---
 include/dt-bindings/types.h |   18 ++++++++++++++++++
 1 files changed, 18 insertions(+), 0 deletions(-)
 create mode 100644 include/dt-bindings/types.h

diff --git a/include/dt-bindings/types.h b/include/dt-bindings/types.h
new file mode 100644
index 0000000..2b7ea75
--- /dev/null
+++ b/include/dt-bindings/types.h
@@ -0,0 +1,18 @@
+/*
+ * This header provides macros for different types and conversions
+ */
+
+#ifndef _DT_BINDINGS_TYPES_H_
+#define _DT_BINDINGS_TYPES_H_
+
+/*
+ * S32_TO_U32: This macro converts the signed number to 2's complement
+ * unisgned number. E.g. S32_TO_U32(-3) will be 0xfffffffd and
+ * S32_TO_U32(3) will be 0x3;
+ * Use of_property_read_s32() for getting back the correct signed value
+ * in driver.
+ */
+#define S32_TO_U32(x) (((x) < 0) ? (((-(x)) ^ 0xFFFFFFFFU) + 1) : (x))
+
+#endif
+
-- 
1.7.0.4


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

* [PATCH 2/2] dt-binding: add header to define types and conversion
@ 2013-12-12 13:24   ` Laxman Dewangan
  0 siblings, 0 replies; 7+ messages in thread
From: Laxman Dewangan @ 2013-12-12 13:24 UTC (permalink / raw)
  To: robh+dt, pawel.moll, mark.rutland, grant.likely
  Cc: ijc+devicetree, galak, linux-kernel, devicetree, swarren,
	kerwinw, Laxman Dewangan

Add a header file to define the macros for type conversion
to be used by DTS file.

Add macro for the S32 to U32 conversion by using 2's complement.

Signed-off-by: Laxman Dewangan <ldewangan@nvidia.com>
---
 include/dt-bindings/types.h |   18 ++++++++++++++++++
 1 files changed, 18 insertions(+), 0 deletions(-)
 create mode 100644 include/dt-bindings/types.h

diff --git a/include/dt-bindings/types.h b/include/dt-bindings/types.h
new file mode 100644
index 0000000..2b7ea75
--- /dev/null
+++ b/include/dt-bindings/types.h
@@ -0,0 +1,18 @@
+/*
+ * This header provides macros for different types and conversions
+ */
+
+#ifndef _DT_BINDINGS_TYPES_H_
+#define _DT_BINDINGS_TYPES_H_
+
+/*
+ * S32_TO_U32: This macro converts the signed number to 2's complement
+ * unisgned number. E.g. S32_TO_U32(-3) will be 0xfffffffd and
+ * S32_TO_U32(3) will be 0x3;
+ * Use of_property_read_s32() for getting back the correct signed value
+ * in driver.
+ */
+#define S32_TO_U32(x) (((x) < 0) ? (((-(x)) ^ 0xFFFFFFFFU) + 1) : (x))
+
+#endif
+
-- 
1.7.0.4

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

* Re: [PATCH 1/2] of: add support for reading s32 property value
  2013-12-12 13:24 ` Laxman Dewangan
  (?)
  (?)
@ 2013-12-12 18:01 ` Stephen Warren
  2013-12-13  6:32     ` Laxman Dewangan
  -1 siblings, 1 reply; 7+ messages in thread
From: Stephen Warren @ 2013-12-12 18:01 UTC (permalink / raw)
  To: Laxman Dewangan, robh+dt, pawel.moll, mark.rutland, grant.likely
  Cc: ijc+devicetree, galak, linux-kernel, devicetree, swarren, kerwinw, david

On 12/12/2013 06:24 AM, Laxman Dewangan wrote:
> Add of_property_read_s32() to read the signed 32bit number
> from dt property value. This supports to pass the -ve numbers
> from dt. Use 2's complement method for represnting negative number
> and passed as u32 from dts. When reading back the value, again
> converted to 2's complement if msb shows as 1.

> diff --git a/include/linux/of.h b/include/linux/of.h

> +static inline int of_property_read_s32(const struct device_node *np,
> +				       const char *propname,
> +				       s32 *out_value)
> +{
> +	u32 val;
> +	int ret;
> +
> +	ret = of_property_read_u32(np, propname, &val);
> +	if (ret < 0)
> +		return ret;
> +
> +	/* 2's complement if MSB is 1 */
> +	*out_value = (val & 0x80000000U) ? -((val ^ 0xFFFFFFFFU) + 1) : val;

I may not be thinking straight today since I have a cold, but doesn't
patch 2/2 encode negative values as 2's complement, and an s32 variable
in the kernel is also encoded as 2's complement, so all you need here is
a cast:

	*out_value = (s32)val;

... since the cast doesn't change the binary representation?

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

* Re: [PATCH 1/2] of: add support for reading s32 property value
@ 2013-12-13  6:32     ` Laxman Dewangan
  0 siblings, 0 replies; 7+ messages in thread
From: Laxman Dewangan @ 2013-12-13  6:32 UTC (permalink / raw)
  To: Stephen Warren
  Cc: robh+dt, pawel.moll, mark.rutland, grant.likely, ijc+devicetree,
	galak, linux-kernel, devicetree, Stephen Warren, Kerwin Wan,
	david

On Thursday 12 December 2013 11:31 PM, Stephen Warren wrote:
> On 12/12/2013 06:24 AM, Laxman Dewangan wrote:
>> Add of_property_read_s32() to read the signed 32bit number
>> from dt property value. This supports to pass the -ve numbers
>> from dt. Use 2's complement method for represnting negative number
>> and passed as u32 from dts. When reading back the value, again
>> converted to 2's complement if msb shows as 1.
>> diff --git a/include/linux/of.h b/include/linux/of.h
>> +static inline int of_property_read_s32(const struct device_node *np,
>> +				       const char *propname,
>> +				       s32 *out_value)
>> +{
>> +	u32 val;
>> +	int ret;
>> +
>> +	ret = of_property_read_u32(np, propname, &val);
>> +	if (ret < 0)
>> +		return ret;
>> +
>> +	/* 2's complement if MSB is 1 */
>> +	*out_value = (val & 0x80000000U) ? -((val ^ 0xFFFFFFFFU) + 1) : val;
> I may not be thinking straight today since I have a cold, but doesn't
> patch 2/2 encode negative values as 2's complement, and an s32 variable
> in the kernel is also encoded as 2's complement, so all you need here is
> a cast:
>
> 	*out_value = (s32)val;
>
> ... since the cast doesn't change the binary representation?

This is correct if all architecture follows the 2's complement for -Ve 
number representation.
If this is true then the patch will become more easy.

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

* Re: [PATCH 1/2] of: add support for reading s32 property value
@ 2013-12-13  6:32     ` Laxman Dewangan
  0 siblings, 0 replies; 7+ messages in thread
From: Laxman Dewangan @ 2013-12-13  6:32 UTC (permalink / raw)
  To: Stephen Warren
  Cc: robh+dt-DgEjT+Ai2ygdnm+yROfE0A, pawel.moll-5wv7dgnIgG8,
	mark.rutland-5wv7dgnIgG8, grant.likely-QSEj5FYQhm4dnm+yROfE0A,
	ijc+devicetree-KcIKpvwj1kUDXYZnReoRVg,
	galak-sgV2jX0FEOL9JmXXK+q4OQ,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA,
	devicetree-u79uwXL29TY76Z2rM5mHXA, Stephen Warren, Kerwin Wan,
	david-xT8FGy+AXnRB3Ne2BGzF6laj5H9X9Tb+

On Thursday 12 December 2013 11:31 PM, Stephen Warren wrote:
> On 12/12/2013 06:24 AM, Laxman Dewangan wrote:
>> Add of_property_read_s32() to read the signed 32bit number
>> from dt property value. This supports to pass the -ve numbers
>> from dt. Use 2's complement method for represnting negative number
>> and passed as u32 from dts. When reading back the value, again
>> converted to 2's complement if msb shows as 1.
>> diff --git a/include/linux/of.h b/include/linux/of.h
>> +static inline int of_property_read_s32(const struct device_node *np,
>> +				       const char *propname,
>> +				       s32 *out_value)
>> +{
>> +	u32 val;
>> +	int ret;
>> +
>> +	ret = of_property_read_u32(np, propname, &val);
>> +	if (ret < 0)
>> +		return ret;
>> +
>> +	/* 2's complement if MSB is 1 */
>> +	*out_value = (val & 0x80000000U) ? -((val ^ 0xFFFFFFFFU) + 1) : val;
> I may not be thinking straight today since I have a cold, but doesn't
> patch 2/2 encode negative values as 2's complement, and an s32 variable
> in the kernel is also encoded as 2's complement, so all you need here is
> a cast:
>
> 	*out_value = (s32)val;
>
> ... since the cast doesn't change the binary representation?

This is correct if all architecture follows the 2's complement for -Ve 
number representation.
If this is true then the patch will become more easy.
--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

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

end of thread, other threads:[~2013-12-13  6:36 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-12-12 13:24 [PATCH 1/2] of: add support for reading s32 property value Laxman Dewangan
2013-12-12 13:24 ` Laxman Dewangan
2013-12-12 13:24 ` [PATCH 2/2] dt-binding: add header to define types and conversion Laxman Dewangan
2013-12-12 13:24   ` Laxman Dewangan
2013-12-12 18:01 ` [PATCH 1/2] of: add support for reading s32 property value Stephen Warren
2013-12-13  6:32   ` Laxman Dewangan
2013-12-13  6:32     ` Laxman Dewangan

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.