From patchwork Thu Jun 30 15:56:10 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: thomas.abraham@linaro.org X-Patchwork-Id: 2391 Return-Path: X-Original-To: patchwork@peony.canonical.com Delivered-To: patchwork@peony.canonical.com Received: from fiordland.canonical.com (fiordland.canonical.com [91.189.94.145]) by peony.canonical.com (Postfix) with ESMTP id 62BE523F4E for ; Thu, 30 Jun 2011 15:45:51 +0000 (UTC) Received: from mail-qw0-f52.google.com (mail-qw0-f52.google.com [209.85.216.52]) by fiordland.canonical.com (Postfix) with ESMTP id 18256A186CD for ; Thu, 30 Jun 2011 15:45:51 +0000 (UTC) Received: by qwb8 with SMTP id 8so1663924qwb.11 for ; Thu, 30 Jun 2011 08:45:50 -0700 (PDT) Received: by 10.229.102.98 with SMTP id f34mr1711618qco.42.1309448750493; Thu, 30 Jun 2011 08:45:50 -0700 (PDT) X-Forwarded-To: linaro-patchwork@canonical.com X-Forwarded-For: patch@linaro.org linaro-patchwork@canonical.com Delivered-To: patches@linaro.org Received: by 10.229.48.135 with SMTP id r7cs76988qcf; Thu, 30 Jun 2011 08:45:49 -0700 (PDT) Received: by 10.68.4.72 with SMTP id i8mr2891503pbi.269.1309448748512; Thu, 30 Jun 2011 08:45:48 -0700 (PDT) Received: from mailout1.samsung.com (mailout1.samsung.com [203.254.224.24]) by mx.google.com with ESMTP id q21si7015754wfq.21.2011.06.30.08.45.47; Thu, 30 Jun 2011 08:45:48 -0700 (PDT) Received-SPF: neutral (google.com: 203.254.224.24 is neither permitted nor denied by best guess record for domain of thomas.abraham@linaro.org) client-ip=203.254.224.24; Authentication-Results: mx.google.com; spf=neutral (google.com: 203.254.224.24 is neither permitted nor denied by best guess record for domain of thomas.abraham@linaro.org) smtp.mail=thomas.abraham@linaro.org Received: from epcpsbgm1.samsung.com (mailout1.samsung.com [203.254.224.24]) by mailout1.samsung.com (Oracle Communications Messaging Exchange Server 7u4-19.01 64bit (built Sep 7 2010)) with ESMTP id <0LNM00M4J14A1VS0@mailout1.samsung.com> for patches@linaro.org; Fri, 01 Jul 2011 00:45:47 +0900 (KST) X-AuditID: cbfee61a-b7c53ae000002dc1-49-4e0c9a2aa965 Received: from epmmp2 ( [203.254.227.17]) by epcpsbgm1.samsung.com (MMPCPMTA) with SMTP id 17.59.11713.A2A9C0E4; Fri, 01 Jul 2011 00:45:46 +0900 (KST) Received: from localhost.localdomain ([107.108.73.37]) by mmp2.samsung.com (iPlanet Messaging Server 5.2 Patch 2 (built Jul 14 2004)) with ESMTPA id <0LNM00DPI146XD@mmp2.samsung.com> for patches@linaro.org; Fri, 01 Jul 2011 00:45:47 +0900 (KST) Date: Thu, 30 Jun 2011 21:26:10 +0530 From: Thomas Abraham Subject: [PATCH] dt: add helper functions to read u32 and string property values To: devicetree-discuss@lists.ozlabs.org Cc: grant.likely@secretlab.ca, patches@linaro.org Message-id: <1309449370-9554-1-git-send-email-thomas.abraham@linaro.org> X-Mailer: git-send-email 1.6.6.rc2 Content-transfer-encoding: 7BIT X-Brightmail-Tracker: AAAAAA== Add helper functions to retrive unsigned integer and string property values from properties of a device node. These helper functions can be used to lookup a property in a device node, perform error checking and read the property value. [grant.likely@secretlab.ca: Proposal and initial implementation] Signed-off-by: Thomas Abraham --- drivers/of/base.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++ include/linux/of.h | 4 +++ 2 files changed, 63 insertions(+), 0 deletions(-) diff --git a/drivers/of/base.c b/drivers/of/base.c index 632ebae..4823b53 100644 --- a/drivers/of/base.c +++ b/drivers/of/base.c @@ -596,6 +596,65 @@ struct device_node *of_find_node_by_phandle(phandle handle) EXPORT_SYMBOL(of_find_node_by_phandle); /** + * of_property_read_u32 - Find a property in a device node and read the + * 32-bit property value + * @np: device node from which the property value is to be read. + * @propname: name of the property to be searched. + * @out_value: returned value, valid if return value is 0. + * + * Search for a property in a device node and read a 32-bit value from a + * property. Returns -EINVAL, if the property does not exist, -ENODATA, if + * property does not have a value, -EOVERFLOW, if the property data isn't + * large enough, and 0 on success. + * + * The out_value is modified only if a valid u32 value can be decoded. + */ +int of_property_read_u32(struct device_node *np, char *propname, u32 *out_value) +{ + struct property *prop = of_find_property(np, propname, NULL); + + if (!prop) + return -EINVAL; + if (!prop->value) + return -ENODATA; + if (sizeof(*out_value) > prop->length) + return -EOVERFLOW; + *out_value = be32_to_cpup(prop->value); + return 0; +} +EXPORT_SYMBOL_GPL(of_property_read_u32); + +/** + * of_property_read_string - Find a property in a device node and return a + * pointer to the property string value. + * @np: device node from which the property value is to be read. + * @propname: name of the property to be searched. + * @out_string: pointer to a null terminated string, valid only if the return + * value is 0. + * + * Returns a pointer to a null terminated property value string. Returns + * -EINVAL, if the property does not exist, -ENODATA, if property does not have + * a value, -EILSEQ, if the string is not null-terminated within the length of + * the property value, and 0 on success. + * + * The out_string value is modified only if a valid string can be decoded. + */ +int of_property_read_string(struct device_node *np, char *propname, + char **out_string) +{ + struct property *prop = of_find_property(np, propname, NULL); + if (!prop) + return -EINVAL; + if (!prop->value) + return -ENODATA; + if (strnlen(prop->value, prop->length) == prop->length) + return -EILSEQ; + *out_string = prop->value; + return 0; +} +EXPORT_SYMBOL_GPL(of_property_read_string); + +/** * of_parse_phandle - Resolve a phandle property to a device_node pointer * @np: Pointer to device node holding phandle property * @phandle_name: Name of property holding a phandle value diff --git a/include/linux/of.h b/include/linux/of.h index bfc0ed1..4fc4c1b 100644 --- a/include/linux/of.h +++ b/include/linux/of.h @@ -195,6 +195,10 @@ extern struct device_node *of_find_node_with_property( extern struct property *of_find_property(const struct device_node *np, const char *name, int *lenp); +extern int of_property_read_u32(struct device_node *np, char *propname, + u32 *out_value); +extern int of_property_read_string(struct device_node *np, char *propname, + char **out_string); extern int of_device_is_compatible(const struct device_node *device, const char *); extern int of_device_is_available(const struct device_node *device);