1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun #include <linux/string.h>
3*4882a593Smuzhiyun #include <linux/err.h>
4*4882a593Smuzhiyun #include <linux/slab.h>
5*4882a593Smuzhiyun #include <linux/of.h>
6*4882a593Smuzhiyun #include <asm/prom.h>
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun #include "of_helpers.h"
9*4882a593Smuzhiyun
10*4882a593Smuzhiyun /**
11*4882a593Smuzhiyun * pseries_of_derive_parent - basically like dirname(1)
12*4882a593Smuzhiyun * @path: the full_name of a node to be added to the tree
13*4882a593Smuzhiyun *
14*4882a593Smuzhiyun * Returns the node which should be the parent of the node
15*4882a593Smuzhiyun * described by path. E.g., for path = "/foo/bar", returns
16*4882a593Smuzhiyun * the node with full_name = "/foo".
17*4882a593Smuzhiyun */
pseries_of_derive_parent(const char * path)18*4882a593Smuzhiyun struct device_node *pseries_of_derive_parent(const char *path)
19*4882a593Smuzhiyun {
20*4882a593Smuzhiyun struct device_node *parent;
21*4882a593Smuzhiyun char *parent_path = "/";
22*4882a593Smuzhiyun const char *tail;
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun /* We do not want the trailing '/' character */
25*4882a593Smuzhiyun tail = kbasename(path) - 1;
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun /* reject if path is "/" */
28*4882a593Smuzhiyun if (!strcmp(path, "/"))
29*4882a593Smuzhiyun return ERR_PTR(-EINVAL);
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun if (tail > path) {
32*4882a593Smuzhiyun parent_path = kstrndup(path, tail - path, GFP_KERNEL);
33*4882a593Smuzhiyun if (!parent_path)
34*4882a593Smuzhiyun return ERR_PTR(-ENOMEM);
35*4882a593Smuzhiyun }
36*4882a593Smuzhiyun parent = of_find_node_by_path(parent_path);
37*4882a593Smuzhiyun if (strcmp(parent_path, "/"))
38*4882a593Smuzhiyun kfree(parent_path);
39*4882a593Smuzhiyun return parent ? parent : ERR_PTR(-EINVAL);
40*4882a593Smuzhiyun }
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun /* Helper Routines to convert between drc_index to cpu numbers */
44*4882a593Smuzhiyun
of_read_drc_info_cell(struct property ** prop,const __be32 ** curval,struct of_drc_info * data)45*4882a593Smuzhiyun int of_read_drc_info_cell(struct property **prop, const __be32 **curval,
46*4882a593Smuzhiyun struct of_drc_info *data)
47*4882a593Smuzhiyun {
48*4882a593Smuzhiyun const char *p = (char *)(*curval);
49*4882a593Smuzhiyun const __be32 *p2;
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun if (!data)
52*4882a593Smuzhiyun return -EINVAL;
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun /* Get drc-type:encode-string */
55*4882a593Smuzhiyun data->drc_type = (char *)p;
56*4882a593Smuzhiyun p = of_prop_next_string(*prop, p);
57*4882a593Smuzhiyun if (!p)
58*4882a593Smuzhiyun return -EINVAL;
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun /* Get drc-name-prefix:encode-string */
61*4882a593Smuzhiyun data->drc_name_prefix = (char *)p;
62*4882a593Smuzhiyun p = of_prop_next_string(*prop, p);
63*4882a593Smuzhiyun if (!p)
64*4882a593Smuzhiyun return -EINVAL;
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun /* Get drc-index-start:encode-int */
67*4882a593Smuzhiyun p2 = (const __be32 *)p;
68*4882a593Smuzhiyun data->drc_index_start = be32_to_cpu(*p2);
69*4882a593Smuzhiyun
70*4882a593Smuzhiyun /* Get drc-name-suffix-start:encode-int */
71*4882a593Smuzhiyun p2 = of_prop_next_u32(*prop, p2, &data->drc_name_suffix_start);
72*4882a593Smuzhiyun if (!p2)
73*4882a593Smuzhiyun return -EINVAL;
74*4882a593Smuzhiyun
75*4882a593Smuzhiyun /* Get number-sequential-elements:encode-int */
76*4882a593Smuzhiyun p2 = of_prop_next_u32(*prop, p2, &data->num_sequential_elems);
77*4882a593Smuzhiyun if (!p2)
78*4882a593Smuzhiyun return -EINVAL;
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun /* Get sequential-increment:encode-int */
81*4882a593Smuzhiyun p2 = of_prop_next_u32(*prop, p2, &data->sequential_inc);
82*4882a593Smuzhiyun if (!p2)
83*4882a593Smuzhiyun return -EINVAL;
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun /* Get drc-power-domain:encode-int */
86*4882a593Smuzhiyun p2 = of_prop_next_u32(*prop, p2, &data->drc_power_domain);
87*4882a593Smuzhiyun if (!p2)
88*4882a593Smuzhiyun return -EINVAL;
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun /* Should now know end of current entry */
91*4882a593Smuzhiyun (*curval) = (void *)(++p2);
92*4882a593Smuzhiyun data->last_drc_index = data->drc_index_start +
93*4882a593Smuzhiyun ((data->num_sequential_elems - 1) * data->sequential_inc);
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun return 0;
96*4882a593Smuzhiyun }
97*4882a593Smuzhiyun EXPORT_SYMBOL(of_read_drc_info_cell);
98