Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include <linux/string.h>
0003 #include <linux/err.h>
0004 #include <linux/slab.h>
0005 #include <linux/of.h>
0006 #include <asm/prom.h>
0007 
0008 #include "of_helpers.h"
0009 
0010 /**
0011  * pseries_of_derive_parent - basically like dirname(1)
0012  * @path:  the full_name of a node to be added to the tree
0013  *
0014  * Returns the node which should be the parent of the node
0015  * described by path.  E.g., for path = "/foo/bar", returns
0016  * the node with full_name = "/foo".
0017  */
0018 struct device_node *pseries_of_derive_parent(const char *path)
0019 {
0020     struct device_node *parent;
0021     char *parent_path = "/";
0022     const char *tail;
0023 
0024     /* We do not want the trailing '/' character */
0025     tail = kbasename(path) - 1;
0026 
0027     /* reject if path is "/" */
0028     if (!strcmp(path, "/"))
0029         return ERR_PTR(-EINVAL);
0030 
0031     if (tail > path) {
0032         parent_path = kstrndup(path, tail - path, GFP_KERNEL);
0033         if (!parent_path)
0034             return ERR_PTR(-ENOMEM);
0035     }
0036     parent = of_find_node_by_path(parent_path);
0037     if (strcmp(parent_path, "/"))
0038         kfree(parent_path);
0039     return parent ? parent : ERR_PTR(-EINVAL);
0040 }
0041 
0042 
0043 /* Helper Routines to convert between drc_index to cpu numbers */
0044 
0045 int of_read_drc_info_cell(struct property **prop, const __be32 **curval,
0046             struct of_drc_info *data)
0047 {
0048     const char *p = (char *)(*curval);
0049     const __be32 *p2;
0050 
0051     if (!data)
0052         return -EINVAL;
0053 
0054     /* Get drc-type:encode-string */
0055     data->drc_type = (char *)p;
0056     p = of_prop_next_string(*prop, p);
0057     if (!p)
0058         return -EINVAL;
0059 
0060     /* Get drc-name-prefix:encode-string */
0061     data->drc_name_prefix = (char *)p;
0062     p = of_prop_next_string(*prop, p);
0063     if (!p)
0064         return -EINVAL;
0065 
0066     /* Get drc-index-start:encode-int */
0067     p2 = (const __be32 *)p;
0068     data->drc_index_start = be32_to_cpu(*p2);
0069 
0070     /* Get drc-name-suffix-start:encode-int */
0071     p2 = of_prop_next_u32(*prop, p2, &data->drc_name_suffix_start);
0072     if (!p2)
0073         return -EINVAL;
0074 
0075     /* Get number-sequential-elements:encode-int */
0076     p2 = of_prop_next_u32(*prop, p2, &data->num_sequential_elems);
0077     if (!p2)
0078         return -EINVAL;
0079 
0080     /* Get sequential-increment:encode-int */
0081     p2 = of_prop_next_u32(*prop, p2, &data->sequential_inc);
0082     if (!p2)
0083         return -EINVAL;
0084 
0085     /* Get drc-power-domain:encode-int */
0086     p2 = of_prop_next_u32(*prop, p2, &data->drc_power_domain);
0087     if (!p2)
0088         return -EINVAL;
0089 
0090     /* Should now know end of current entry */
0091     (*curval) = (void *)(++p2);
0092     data->last_drc_index = data->drc_index_start +
0093         ((data->num_sequential_elems - 1) * data->sequential_inc);
0094 
0095     return 0;
0096 }
0097 EXPORT_SYMBOL(of_read_drc_info_cell);