d00b9493956d2931af4218bb13020eb58cfd2890
[firefly-linux-kernel-4.4.55.git] / drivers / regulator / of_regulator.c
1 /*
2  * OF helpers for regulator framework
3  *
4  * Copyright (C) 2011 Texas Instruments, Inc.
5  * Rajendra Nayak <rnayak@ti.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  */
12
13 #include <linux/module.h>
14 #include <linux/slab.h>
15 #include <linux/of.h>
16 #include <linux/regulator/machine.h>
17 #include <linux/regulator/of_regulator.h>
18
19 static void set_regulator_state_constraints(struct device_node *np,
20                 struct regulator_state *state)
21 {
22         of_property_read_u32(np, "regulator-state-uv", &state->uV);
23         of_property_read_u32(np, "regulator-state-mode", &state->mode);
24         state->enabled = of_property_read_bool(np, "regulator-state-enabled");
25         state->disabled = of_property_read_bool(np, "regulator-state-disabled");
26 }
27
28 static void of_get_regulation_constraints(struct device_node *np,
29                                         struct regulator_init_data **init_data)
30 {
31         const __be32 *min_uV, *max_uV, *uV_offset;
32         const __be32 *min_uA, *max_uA, *ramp_delay;
33         struct device_node *state;
34         struct regulation_constraints *constraints = &(*init_data)->constraints;
35
36         constraints->name = of_get_property(np, "regulator-name", NULL);
37
38         min_uV = of_get_property(np, "regulator-min-microvolt", NULL);
39         if (min_uV)
40                 constraints->min_uV = be32_to_cpu(*min_uV);
41         max_uV = of_get_property(np, "regulator-max-microvolt", NULL);
42         if (max_uV)
43                 constraints->max_uV = be32_to_cpu(*max_uV);
44
45         /* Voltage change possible? */
46         if (constraints->min_uV != constraints->max_uV)
47                 constraints->valid_ops_mask |= REGULATOR_CHANGE_VOLTAGE;
48         /* Only one voltage?  Then make sure it's set. */
49         if (min_uV && max_uV && constraints->min_uV == constraints->max_uV)
50                 constraints->apply_uV = true;
51
52         uV_offset = of_get_property(np, "regulator-microvolt-offset", NULL);
53         if (uV_offset)
54                 constraints->uV_offset = be32_to_cpu(*uV_offset);
55         min_uA = of_get_property(np, "regulator-min-microamp", NULL);
56         if (min_uA)
57                 constraints->min_uA = be32_to_cpu(*min_uA);
58         max_uA = of_get_property(np, "regulator-max-microamp", NULL);
59         if (max_uA)
60                 constraints->max_uA = be32_to_cpu(*max_uA);
61
62         /* Current change possible? */
63         if (constraints->min_uA != constraints->max_uA)
64                 constraints->valid_ops_mask |= REGULATOR_CHANGE_CURRENT;
65
66         if (of_find_property(np, "regulator-boot-on", NULL))
67                 constraints->boot_on = true;
68
69         if (of_find_property(np, "regulator-always-on", NULL))
70                 constraints->always_on = true;
71         else /* status change should be possible if not always on. */
72                 constraints->valid_ops_mask |= REGULATOR_CHANGE_STATUS;
73
74         ramp_delay = of_get_property(np, "regulator-ramp-delay", NULL);
75         if (ramp_delay)
76                 constraints->ramp_delay = be32_to_cpu(*ramp_delay);
77         
78         of_property_read_u32(np, "regulator-valid-modes-mask",
79                                         &constraints->valid_modes_mask);
80         if (constraints->valid_modes_mask)
81                 constraints->valid_ops_mask |=  REGULATOR_CHANGE_MODE;
82
83         of_property_read_u32(np, "regulator-input-uv",
84                                         &constraints->input_uV);
85         of_property_read_u32(np, "regulator-initial-mode",
86                                         &constraints->initial_mode);
87         of_property_read_u32(np, "regulator-initial-state",
88                                         &constraints->initial_state);
89
90         /* regulator state during low power system states */
91         state = of_find_node_by_name(np, "regulator-state-mem");
92         if (state)
93                 set_regulator_state_constraints(state,
94                                 &constraints->state_mem);
95
96         state = of_find_node_by_name(np, "regulator-state-disk");
97         if (state)
98                 set_regulator_state_constraints(state,
99                                 &constraints->state_disk);
100
101         state = of_find_node_by_name(np, "regulator-state-standby");
102         if (state)
103                 set_regulator_state_constraints(state,
104                                 &constraints->state_standby);
105
106         
107 }
108
109 /**
110  * of_get_regulator_init_data - extract regulator_init_data structure info
111  * @dev: device requesting for regulator_init_data
112  *
113  * Populates regulator_init_data structure by extracting data from device
114  * tree node, returns a pointer to the populated struture or NULL if memory
115  * alloc fails.
116  */
117 struct regulator_init_data *of_get_regulator_init_data(struct device *dev,
118                                                 struct device_node *node)
119 {
120         struct regulator_init_data *init_data;
121
122         if (!node)
123                 return NULL;
124
125         init_data = devm_kzalloc(dev, sizeof(*init_data), GFP_KERNEL);
126         if (!init_data)
127                 return NULL; /* Out of memory? */
128
129         of_get_regulation_constraints(node, &init_data);
130         return init_data;
131 }
132 EXPORT_SYMBOL_GPL(of_get_regulator_init_data);
133
134 /**
135  * of_regulator_match - extract multiple regulator init data from device tree.
136  * @dev: device requesting the data
137  * @node: parent device node of the regulators
138  * @matches: match table for the regulators
139  * @num_matches: number of entries in match table
140  *
141  * This function uses a match table specified by the regulator driver to
142  * parse regulator init data from the device tree. @node is expected to
143  * contain a set of child nodes, each providing the init data for one
144  * regulator. The data parsed from a child node will be matched to a regulator
145  * based on either the deprecated property regulator-compatible if present,
146  * or otherwise the child node's name. Note that the match table is modified
147  * in place.
148  *
149  * Returns the number of matches found or a negative error code on failure.
150  */
151 int of_regulator_match(struct device *dev, struct device_node *node,
152                        struct of_regulator_match *matches,
153                        unsigned int num_matches)
154 {
155         unsigned int count = 0;
156         unsigned int i;
157         const char *name;
158         struct device_node *child;
159
160         if (!dev || !node)
161                 return -EINVAL;
162
163         for (i = 0; i < num_matches; i++) {
164                 struct of_regulator_match *match = &matches[i];
165                 match->init_data = NULL;
166                 match->of_node = NULL;
167         }
168
169         for_each_child_of_node(node, child) {
170                 name = of_get_property(child,
171                                         "regulator-compatible", NULL);
172                 if (!name)
173                         name = child->name;
174                 for (i = 0; i < num_matches; i++) {
175                         struct of_regulator_match *match = &matches[i];
176                         if (match->of_node)
177                                 continue;
178
179                         if (strcmp(match->name, name))
180                                 continue;
181
182                         match->init_data =
183                                 of_get_regulator_init_data(dev, child);
184                         if (!match->init_data) {
185                                 dev_err(dev,
186                                         "failed to parse DT for regulator %s\n",
187                                         child->name);
188                                 return -EINVAL;
189                         }
190                         match->of_node = child;
191                         count++;
192                         break;
193                 }
194         }
195
196         return count;
197 }
198 EXPORT_SYMBOL_GPL(of_regulator_match);