Rajendra Nayak | 8f446e6 | 2011-11-18 16:47:17 +0530 | [diff] [blame] | 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 | |
Axel Lin | e69af5e | 2011-11-26 18:50:58 +0800 | [diff] [blame] | 13 | #include <linux/module.h> |
Rajendra Nayak | 8f446e6 | 2011-11-18 16:47:17 +0530 | [diff] [blame] | 14 | #include <linux/slab.h> |
| 15 | #include <linux/of.h> |
| 16 | #include <linux/regulator/machine.h> |
Mark Brown | a0c7b16 | 2014-09-09 23:13:57 +0100 | [diff] [blame] | 17 | #include <linux/regulator/driver.h> |
Thierry Reding | 1c8fa58 | 2012-04-26 16:52:20 +0200 | [diff] [blame] | 18 | #include <linux/regulator/of_regulator.h> |
Rajendra Nayak | 8f446e6 | 2011-11-18 16:47:17 +0530 | [diff] [blame] | 19 | |
Mark Brown | a0c7b16 | 2014-09-09 23:13:57 +0100 | [diff] [blame] | 20 | #include "internal.h" |
| 21 | |
Krzysztof Kozlowski | f32fa89 | 2014-10-31 17:04:14 +0100 | [diff] [blame] | 22 | static const char *const regulator_states[PM_SUSPEND_MAX + 1] = { |
Chanwoo Choi | 40e20d6 | 2014-10-10 20:35:33 +0900 | [diff] [blame] | 23 | [PM_SUSPEND_MEM] = "regulator-state-mem", |
| 24 | [PM_SUSPEND_MAX] = "regulator-state-disk", |
| 25 | }; |
| 26 | |
Rajendra Nayak | 8f446e6 | 2011-11-18 16:47:17 +0530 | [diff] [blame] | 27 | static void of_get_regulation_constraints(struct device_node *np, |
Javier Martinez Canillas | 5e5e3a4 | 2014-11-10 14:43:55 +0100 | [diff] [blame] | 28 | struct regulator_init_data **init_data, |
| 29 | const struct regulator_desc *desc) |
Rajendra Nayak | 8f446e6 | 2011-11-18 16:47:17 +0530 | [diff] [blame] | 30 | { |
Sergei Shtylyov | 1e050ea | 2014-05-27 00:27:19 +0400 | [diff] [blame] | 31 | const __be32 *min_uV, *max_uV; |
Rajendra Nayak | 8f446e6 | 2011-11-18 16:47:17 +0530 | [diff] [blame] | 32 | struct regulation_constraints *constraints = &(*init_data)->constraints; |
Chanwoo Choi | 40e20d6 | 2014-10-10 20:35:33 +0900 | [diff] [blame] | 33 | struct regulator_state *suspend_state; |
| 34 | struct device_node *suspend_np; |
| 35 | int ret, i; |
Laxman Dewangan | 00c877c | 2013-09-18 18:18:02 +0530 | [diff] [blame] | 36 | u32 pval; |
Rajendra Nayak | 8f446e6 | 2011-11-18 16:47:17 +0530 | [diff] [blame] | 37 | |
| 38 | constraints->name = of_get_property(np, "regulator-name", NULL); |
| 39 | |
| 40 | min_uV = of_get_property(np, "regulator-min-microvolt", NULL); |
| 41 | if (min_uV) |
| 42 | constraints->min_uV = be32_to_cpu(*min_uV); |
| 43 | max_uV = of_get_property(np, "regulator-max-microvolt", NULL); |
| 44 | if (max_uV) |
| 45 | constraints->max_uV = be32_to_cpu(*max_uV); |
| 46 | |
| 47 | /* Voltage change possible? */ |
| 48 | if (constraints->min_uV != constraints->max_uV) |
| 49 | constraints->valid_ops_mask |= REGULATOR_CHANGE_VOLTAGE; |
Mark Brown | ab62aa9 | 2011-12-05 10:58:41 +0000 | [diff] [blame] | 50 | /* Only one voltage? Then make sure it's set. */ |
Karol Lewandowski | 8a09304 | 2012-01-25 10:31:45 +0100 | [diff] [blame] | 51 | if (min_uV && max_uV && constraints->min_uV == constraints->max_uV) |
Mark Brown | ab62aa9 | 2011-12-05 10:58:41 +0000 | [diff] [blame] | 52 | constraints->apply_uV = true; |
Rajendra Nayak | 8f446e6 | 2011-11-18 16:47:17 +0530 | [diff] [blame] | 53 | |
Sergei Shtylyov | 1e050ea | 2014-05-27 00:27:19 +0400 | [diff] [blame] | 54 | if (!of_property_read_u32(np, "regulator-microvolt-offset", &pval)) |
| 55 | constraints->uV_offset = pval; |
| 56 | if (!of_property_read_u32(np, "regulator-min-microamp", &pval)) |
| 57 | constraints->min_uA = pval; |
| 58 | if (!of_property_read_u32(np, "regulator-max-microamp", &pval)) |
| 59 | constraints->max_uA = pval; |
Rajendra Nayak | 8f446e6 | 2011-11-18 16:47:17 +0530 | [diff] [blame] | 60 | |
| 61 | /* Current change possible? */ |
| 62 | if (constraints->min_uA != constraints->max_uA) |
| 63 | constraints->valid_ops_mask |= REGULATOR_CHANGE_CURRENT; |
| 64 | |
Sergei Shtylyov | 1e050ea | 2014-05-27 00:27:19 +0400 | [diff] [blame] | 65 | constraints->boot_on = of_property_read_bool(np, "regulator-boot-on"); |
| 66 | constraints->always_on = of_property_read_bool(np, "regulator-always-on"); |
| 67 | if (!constraints->always_on) /* status change should be possible. */ |
Rajendra Nayak | 8f446e6 | 2011-11-18 16:47:17 +0530 | [diff] [blame] | 68 | constraints->valid_ops_mask |= REGULATOR_CHANGE_STATUS; |
Yadwinder Singh Brar | 6f0b2c6 | 2012-06-11 17:41:08 +0530 | [diff] [blame] | 69 | |
Stephen Boyd | 23c779b | 2015-06-11 17:37:04 -0700 | [diff] [blame] | 70 | constraints->pull_down = of_property_read_bool(np, "regulator-pull-down"); |
| 71 | |
Kishon Vijay Abraham I | 93134c7 | 2013-06-20 14:07:37 +0530 | [diff] [blame] | 72 | if (of_property_read_bool(np, "regulator-allow-bypass")) |
| 73 | constraints->valid_ops_mask |= REGULATOR_CHANGE_BYPASS; |
| 74 | |
Sergei Shtylyov | 1e050ea | 2014-05-27 00:27:19 +0400 | [diff] [blame] | 75 | ret = of_property_read_u32(np, "regulator-ramp-delay", &pval); |
| 76 | if (!ret) { |
| 77 | if (pval) |
| 78 | constraints->ramp_delay = pval; |
Yadwinder Singh Brar | 1653ccf | 2013-06-29 18:21:15 +0530 | [diff] [blame] | 79 | else |
| 80 | constraints->ramp_disable = true; |
| 81 | } |
Laxman Dewangan | 00c877c | 2013-09-18 18:18:02 +0530 | [diff] [blame] | 82 | |
| 83 | ret = of_property_read_u32(np, "regulator-enable-ramp-delay", &pval); |
| 84 | if (!ret) |
| 85 | constraints->enable_time = pval; |
Chanwoo Choi | 40e20d6 | 2014-10-10 20:35:33 +0900 | [diff] [blame] | 86 | |
Stephen Boyd | 57f66b7 | 2015-06-11 17:37:05 -0700 | [diff] [blame^] | 87 | constraints->soft_start = of_property_read_bool(np, |
| 88 | "regulator-soft-start"); |
| 89 | |
Javier Martinez Canillas | 5e5e3a4 | 2014-11-10 14:43:55 +0100 | [diff] [blame] | 90 | if (!of_property_read_u32(np, "regulator-initial-mode", &pval)) { |
| 91 | if (desc && desc->of_map_mode) { |
| 92 | ret = desc->of_map_mode(pval); |
| 93 | if (ret == -EINVAL) |
| 94 | pr_err("%s: invalid mode %u\n", np->name, pval); |
| 95 | else |
| 96 | constraints->initial_mode = ret; |
| 97 | } else { |
| 98 | pr_warn("%s: mapping for mode %d not defined\n", |
| 99 | np->name, pval); |
| 100 | } |
| 101 | } |
| 102 | |
Stephen Boyd | 22a10bc | 2015-06-11 17:37:03 -0700 | [diff] [blame] | 103 | if (!of_property_read_u32(np, "regulator-system-load", &pval)) |
| 104 | constraints->system_load = pval; |
| 105 | |
Chanwoo Choi | 40e20d6 | 2014-10-10 20:35:33 +0900 | [diff] [blame] | 106 | for (i = 0; i < ARRAY_SIZE(regulator_states); i++) { |
| 107 | switch (i) { |
| 108 | case PM_SUSPEND_MEM: |
| 109 | suspend_state = &constraints->state_mem; |
| 110 | break; |
| 111 | case PM_SUSPEND_MAX: |
| 112 | suspend_state = &constraints->state_disk; |
| 113 | break; |
| 114 | case PM_SUSPEND_ON: |
| 115 | case PM_SUSPEND_FREEZE: |
| 116 | case PM_SUSPEND_STANDBY: |
| 117 | default: |
| 118 | continue; |
| 119 | }; |
| 120 | |
| 121 | suspend_np = of_get_child_by_name(np, regulator_states[i]); |
| 122 | if (!suspend_np || !suspend_state) |
| 123 | continue; |
| 124 | |
Javier Martinez Canillas | 5e5e3a4 | 2014-11-10 14:43:55 +0100 | [diff] [blame] | 125 | if (!of_property_read_u32(suspend_np, "regulator-mode", |
| 126 | &pval)) { |
| 127 | if (desc && desc->of_map_mode) { |
| 128 | ret = desc->of_map_mode(pval); |
| 129 | if (ret == -EINVAL) |
| 130 | pr_err("%s: invalid mode %u\n", |
| 131 | np->name, pval); |
| 132 | else |
| 133 | suspend_state->mode = ret; |
| 134 | } else { |
| 135 | pr_warn("%s: mapping for mode %d not defined\n", |
| 136 | np->name, pval); |
| 137 | } |
| 138 | } |
| 139 | |
Chanwoo Choi | 40e20d6 | 2014-10-10 20:35:33 +0900 | [diff] [blame] | 140 | if (of_property_read_bool(suspend_np, |
| 141 | "regulator-on-in-suspend")) |
| 142 | suspend_state->enabled = true; |
| 143 | else if (of_property_read_bool(suspend_np, |
| 144 | "regulator-off-in-suspend")) |
| 145 | suspend_state->disabled = true; |
| 146 | |
Doug Anderson | 8cbcaea | 2014-10-31 20:52:58 -0700 | [diff] [blame] | 147 | if (!of_property_read_u32(suspend_np, |
| 148 | "regulator-suspend-microvolt", &pval)) |
| 149 | suspend_state->uV = pval; |
| 150 | |
Javier Martinez Canillas | 4eafec8 | 2014-10-20 16:47:48 +0200 | [diff] [blame] | 151 | of_node_put(suspend_np); |
Chanwoo Choi | 40e20d6 | 2014-10-10 20:35:33 +0900 | [diff] [blame] | 152 | suspend_state = NULL; |
| 153 | suspend_np = NULL; |
| 154 | } |
Rajendra Nayak | 8f446e6 | 2011-11-18 16:47:17 +0530 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | /** |
| 158 | * of_get_regulator_init_data - extract regulator_init_data structure info |
| 159 | * @dev: device requesting for regulator_init_data |
Javier Martinez Canillas | 072e78b | 2014-11-10 14:43:53 +0100 | [diff] [blame] | 160 | * @node: regulator device node |
| 161 | * @desc: regulator description |
Rajendra Nayak | 8f446e6 | 2011-11-18 16:47:17 +0530 | [diff] [blame] | 162 | * |
| 163 | * Populates regulator_init_data structure by extracting data from device |
| 164 | * tree node, returns a pointer to the populated struture or NULL if memory |
| 165 | * alloc fails. |
| 166 | */ |
Shawn Guo | d9a861c | 2011-12-01 17:21:06 +0800 | [diff] [blame] | 167 | struct regulator_init_data *of_get_regulator_init_data(struct device *dev, |
Javier Martinez Canillas | 072e78b | 2014-11-10 14:43:53 +0100 | [diff] [blame] | 168 | struct device_node *node, |
| 169 | const struct regulator_desc *desc) |
Rajendra Nayak | 8f446e6 | 2011-11-18 16:47:17 +0530 | [diff] [blame] | 170 | { |
| 171 | struct regulator_init_data *init_data; |
| 172 | |
Shawn Guo | d9a861c | 2011-12-01 17:21:06 +0800 | [diff] [blame] | 173 | if (!node) |
Rajendra Nayak | 8f446e6 | 2011-11-18 16:47:17 +0530 | [diff] [blame] | 174 | return NULL; |
| 175 | |
| 176 | init_data = devm_kzalloc(dev, sizeof(*init_data), GFP_KERNEL); |
| 177 | if (!init_data) |
| 178 | return NULL; /* Out of memory? */ |
| 179 | |
Javier Martinez Canillas | 5e5e3a4 | 2014-11-10 14:43:55 +0100 | [diff] [blame] | 180 | of_get_regulation_constraints(node, &init_data, desc); |
Rajendra Nayak | 8f446e6 | 2011-11-18 16:47:17 +0530 | [diff] [blame] | 181 | return init_data; |
| 182 | } |
Axel Lin | e69af5e | 2011-11-26 18:50:58 +0800 | [diff] [blame] | 183 | EXPORT_SYMBOL_GPL(of_get_regulator_init_data); |
Thierry Reding | 1c8fa58 | 2012-04-26 16:52:20 +0200 | [diff] [blame] | 184 | |
Charles Keepax | 13b3fde | 2014-04-15 13:34:36 +0100 | [diff] [blame] | 185 | struct devm_of_regulator_matches { |
| 186 | struct of_regulator_match *matches; |
| 187 | unsigned int num_matches; |
| 188 | }; |
| 189 | |
| 190 | static void devm_of_regulator_put_matches(struct device *dev, void *res) |
| 191 | { |
| 192 | struct devm_of_regulator_matches *devm_matches = res; |
| 193 | int i; |
| 194 | |
| 195 | for (i = 0; i < devm_matches->num_matches; i++) |
| 196 | of_node_put(devm_matches->matches[i].of_node); |
| 197 | } |
| 198 | |
Thierry Reding | 1c8fa58 | 2012-04-26 16:52:20 +0200 | [diff] [blame] | 199 | /** |
Stephen Warren | 13511de | 2012-09-24 13:25:00 -0600 | [diff] [blame] | 200 | * of_regulator_match - extract multiple regulator init data from device tree. |
Thierry Reding | 1c8fa58 | 2012-04-26 16:52:20 +0200 | [diff] [blame] | 201 | * @dev: device requesting the data |
| 202 | * @node: parent device node of the regulators |
| 203 | * @matches: match table for the regulators |
| 204 | * @num_matches: number of entries in match table |
| 205 | * |
Stephen Warren | 13511de | 2012-09-24 13:25:00 -0600 | [diff] [blame] | 206 | * This function uses a match table specified by the regulator driver to |
| 207 | * parse regulator init data from the device tree. @node is expected to |
| 208 | * contain a set of child nodes, each providing the init data for one |
| 209 | * regulator. The data parsed from a child node will be matched to a regulator |
| 210 | * based on either the deprecated property regulator-compatible if present, |
| 211 | * or otherwise the child node's name. Note that the match table is modified |
Charles Keepax | bd0dda74 | 2014-04-03 15:32:15 +0100 | [diff] [blame] | 212 | * in place and an additional of_node reference is taken for each matched |
| 213 | * regulator. |
Thierry Reding | 1c8fa58 | 2012-04-26 16:52:20 +0200 | [diff] [blame] | 214 | * |
| 215 | * Returns the number of matches found or a negative error code on failure. |
| 216 | */ |
| 217 | int of_regulator_match(struct device *dev, struct device_node *node, |
| 218 | struct of_regulator_match *matches, |
| 219 | unsigned int num_matches) |
| 220 | { |
| 221 | unsigned int count = 0; |
| 222 | unsigned int i; |
Stephen Warren | 13511de | 2012-09-24 13:25:00 -0600 | [diff] [blame] | 223 | const char *name; |
Laxman Dewangan | 5260cd2 | 2012-06-20 17:53:06 +0530 | [diff] [blame] | 224 | struct device_node *child; |
Charles Keepax | 13b3fde | 2014-04-15 13:34:36 +0100 | [diff] [blame] | 225 | struct devm_of_regulator_matches *devm_matches; |
Thierry Reding | 1c8fa58 | 2012-04-26 16:52:20 +0200 | [diff] [blame] | 226 | |
| 227 | if (!dev || !node) |
| 228 | return -EINVAL; |
| 229 | |
Charles Keepax | 13b3fde | 2014-04-15 13:34:36 +0100 | [diff] [blame] | 230 | devm_matches = devres_alloc(devm_of_regulator_put_matches, |
| 231 | sizeof(struct devm_of_regulator_matches), |
| 232 | GFP_KERNEL); |
| 233 | if (!devm_matches) |
| 234 | return -ENOMEM; |
| 235 | |
| 236 | devm_matches->matches = matches; |
| 237 | devm_matches->num_matches = num_matches; |
| 238 | |
| 239 | devres_add(dev, devm_matches); |
| 240 | |
Stephen Warren | a2f95c3 | 2013-01-29 12:01:13 -0700 | [diff] [blame] | 241 | for (i = 0; i < num_matches; i++) { |
| 242 | struct of_regulator_match *match = &matches[i]; |
| 243 | match->init_data = NULL; |
| 244 | match->of_node = NULL; |
| 245 | } |
| 246 | |
Laxman Dewangan | 5260cd2 | 2012-06-20 17:53:06 +0530 | [diff] [blame] | 247 | for_each_child_of_node(node, child) { |
Stephen Warren | 13511de | 2012-09-24 13:25:00 -0600 | [diff] [blame] | 248 | name = of_get_property(child, |
Laxman Dewangan | 5260cd2 | 2012-06-20 17:53:06 +0530 | [diff] [blame] | 249 | "regulator-compatible", NULL); |
Stephen Warren | 13511de | 2012-09-24 13:25:00 -0600 | [diff] [blame] | 250 | if (!name) |
| 251 | name = child->name; |
Laxman Dewangan | 5260cd2 | 2012-06-20 17:53:06 +0530 | [diff] [blame] | 252 | for (i = 0; i < num_matches; i++) { |
| 253 | struct of_regulator_match *match = &matches[i]; |
| 254 | if (match->of_node) |
| 255 | continue; |
Thierry Reding | 1c8fa58 | 2012-04-26 16:52:20 +0200 | [diff] [blame] | 256 | |
Stephen Warren | 13511de | 2012-09-24 13:25:00 -0600 | [diff] [blame] | 257 | if (strcmp(match->name, name)) |
Laxman Dewangan | 5260cd2 | 2012-06-20 17:53:06 +0530 | [diff] [blame] | 258 | continue; |
| 259 | |
| 260 | match->init_data = |
Javier Martinez Canillas | 75d6b2f | 2014-11-10 14:43:54 +0100 | [diff] [blame] | 261 | of_get_regulator_init_data(dev, child, |
| 262 | match->desc); |
Laxman Dewangan | 5260cd2 | 2012-06-20 17:53:06 +0530 | [diff] [blame] | 263 | if (!match->init_data) { |
| 264 | dev_err(dev, |
| 265 | "failed to parse DT for regulator %s\n", |
| 266 | child->name); |
| 267 | return -EINVAL; |
| 268 | } |
Charles Keepax | bd0dda74 | 2014-04-03 15:32:15 +0100 | [diff] [blame] | 269 | match->of_node = of_node_get(child); |
Laxman Dewangan | 5260cd2 | 2012-06-20 17:53:06 +0530 | [diff] [blame] | 270 | count++; |
| 271 | break; |
| 272 | } |
Thierry Reding | 1c8fa58 | 2012-04-26 16:52:20 +0200 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | return count; |
| 276 | } |
| 277 | EXPORT_SYMBOL_GPL(of_regulator_match); |
Mark Brown | a0c7b16 | 2014-09-09 23:13:57 +0100 | [diff] [blame] | 278 | |
| 279 | struct regulator_init_data *regulator_of_get_init_data(struct device *dev, |
| 280 | const struct regulator_desc *desc, |
Krzysztof Kozlowski | bfa21a0 | 2015-01-05 12:48:42 +0100 | [diff] [blame] | 281 | struct regulator_config *config, |
Mark Brown | a0c7b16 | 2014-09-09 23:13:57 +0100 | [diff] [blame] | 282 | struct device_node **node) |
| 283 | { |
| 284 | struct device_node *search, *child; |
| 285 | struct regulator_init_data *init_data = NULL; |
| 286 | const char *name; |
| 287 | |
| 288 | if (!dev->of_node || !desc->of_match) |
| 289 | return NULL; |
| 290 | |
| 291 | if (desc->regulators_node) |
| 292 | search = of_get_child_by_name(dev->of_node, |
| 293 | desc->regulators_node); |
| 294 | else |
| 295 | search = dev->of_node; |
| 296 | |
| 297 | if (!search) { |
Mark Brown | 7de79a1 | 2014-10-08 23:08:13 +0100 | [diff] [blame] | 298 | dev_dbg(dev, "Failed to find regulator container node '%s'\n", |
| 299 | desc->regulators_node); |
Mark Brown | a0c7b16 | 2014-09-09 23:13:57 +0100 | [diff] [blame] | 300 | return NULL; |
| 301 | } |
| 302 | |
| 303 | for_each_child_of_node(search, child) { |
| 304 | name = of_get_property(child, "regulator-compatible", NULL); |
| 305 | if (!name) |
| 306 | name = child->name; |
| 307 | |
| 308 | if (strcmp(desc->of_match, name)) |
| 309 | continue; |
| 310 | |
Javier Martinez Canillas | 072e78b | 2014-11-10 14:43:53 +0100 | [diff] [blame] | 311 | init_data = of_get_regulator_init_data(dev, child, desc); |
Mark Brown | a0c7b16 | 2014-09-09 23:13:57 +0100 | [diff] [blame] | 312 | if (!init_data) { |
| 313 | dev_err(dev, |
| 314 | "failed to parse DT for regulator %s\n", |
| 315 | child->name); |
| 316 | break; |
| 317 | } |
| 318 | |
Krzysztof Kozlowski | bfa21a0 | 2015-01-05 12:48:42 +0100 | [diff] [blame] | 319 | if (desc->of_parse_cb) { |
| 320 | if (desc->of_parse_cb(child, desc, config)) { |
| 321 | dev_err(dev, |
| 322 | "driver callback failed to parse DT for regulator %s\n", |
| 323 | child->name); |
| 324 | init_data = NULL; |
| 325 | break; |
| 326 | } |
| 327 | } |
| 328 | |
Mark Brown | a0c7b16 | 2014-09-09 23:13:57 +0100 | [diff] [blame] | 329 | of_node_get(child); |
| 330 | *node = child; |
| 331 | break; |
| 332 | } |
| 333 | |
| 334 | of_node_put(search); |
| 335 | |
| 336 | return init_data; |
| 337 | } |