Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Rajendra Nayak | 8f446e6 | 2011-11-18 16:47:17 +0530 | [diff] [blame] | 2 | /* |
| 3 | * OF helpers for regulator framework |
| 4 | * |
| 5 | * Copyright (C) 2011 Texas Instruments, Inc. |
| 6 | * Rajendra Nayak <rnayak@ti.com> |
Rajendra Nayak | 8f446e6 | 2011-11-18 16:47:17 +0530 | [diff] [blame] | 7 | */ |
| 8 | |
Axel Lin | e69af5e | 2011-11-26 18:50:58 +0800 | [diff] [blame] | 9 | #include <linux/module.h> |
Rajendra Nayak | 8f446e6 | 2011-11-18 16:47:17 +0530 | [diff] [blame] | 10 | #include <linux/slab.h> |
| 11 | #include <linux/of.h> |
| 12 | #include <linux/regulator/machine.h> |
Mark Brown | a0c7b16 | 2014-09-09 23:13:57 +0100 | [diff] [blame] | 13 | #include <linux/regulator/driver.h> |
Thierry Reding | 1c8fa58 | 2012-04-26 16:52:20 +0200 | [diff] [blame] | 14 | #include <linux/regulator/of_regulator.h> |
Rajendra Nayak | 8f446e6 | 2011-11-18 16:47:17 +0530 | [diff] [blame] | 15 | |
Mark Brown | a0c7b16 | 2014-09-09 23:13:57 +0100 | [diff] [blame] | 16 | #include "internal.h" |
| 17 | |
Krzysztof Kozlowski | f32fa89 | 2014-10-31 17:04:14 +0100 | [diff] [blame] | 18 | static const char *const regulator_states[PM_SUSPEND_MAX + 1] = { |
Andrei.Stefanescu@microchip.com | f2b4076 | 2018-11-13 14:47:37 +0000 | [diff] [blame] | 19 | [PM_SUSPEND_STANDBY] = "regulator-state-standby", |
Chanwoo Choi | 40e20d6 | 2014-10-10 20:35:33 +0900 | [diff] [blame] | 20 | [PM_SUSPEND_MEM] = "regulator-state-mem", |
| 21 | [PM_SUSPEND_MAX] = "regulator-state-disk", |
| 22 | }; |
| 23 | |
Matti Vaittinen | 89a6a5e | 2021-06-03 08:42:12 +0300 | [diff] [blame] | 24 | static void fill_limit(int *limit, int val) |
| 25 | { |
| 26 | if (val) |
| 27 | if (val == 1) |
| 28 | *limit = REGULATOR_NOTIF_LIMIT_ENABLE; |
| 29 | else |
| 30 | *limit = val; |
| 31 | else |
| 32 | *limit = REGULATOR_NOTIF_LIMIT_DISABLE; |
| 33 | } |
| 34 | |
| 35 | static void of_get_regulator_prot_limits(struct device_node *np, |
| 36 | struct regulation_constraints *constraints) |
| 37 | { |
| 38 | u32 pval; |
| 39 | int i; |
| 40 | static const char *const props[] = { |
| 41 | "regulator-oc-%s-microamp", |
| 42 | "regulator-ov-%s-microvolt", |
| 43 | "regulator-temp-%s-kelvin", |
| 44 | "regulator-uv-%s-microvolt", |
| 45 | }; |
| 46 | struct notification_limit *limits[] = { |
| 47 | &constraints->over_curr_limits, |
| 48 | &constraints->over_voltage_limits, |
| 49 | &constraints->temp_limits, |
| 50 | &constraints->under_voltage_limits, |
| 51 | }; |
| 52 | bool set[4] = {0}; |
| 53 | |
| 54 | /* Protection limits: */ |
| 55 | for (i = 0; i < ARRAY_SIZE(props); i++) { |
| 56 | char prop[255]; |
| 57 | bool found; |
| 58 | int j; |
| 59 | static const char *const lvl[] = { |
| 60 | "protection", "error", "warn" |
| 61 | }; |
| 62 | int *l[] = { |
| 63 | &limits[i]->prot, &limits[i]->err, &limits[i]->warn, |
| 64 | }; |
| 65 | |
| 66 | for (j = 0; j < ARRAY_SIZE(lvl); j++) { |
| 67 | snprintf(prop, 255, props[i], lvl[j]); |
| 68 | found = !of_property_read_u32(np, prop, &pval); |
| 69 | if (found) |
| 70 | fill_limit(l[j], pval); |
| 71 | set[i] |= found; |
| 72 | } |
| 73 | } |
| 74 | constraints->over_current_detection = set[0]; |
| 75 | constraints->over_voltage_detection = set[1]; |
| 76 | constraints->over_temp_detection = set[2]; |
| 77 | constraints->under_voltage_detection = set[3]; |
| 78 | } |
| 79 | |
Dmitry Osipenko | d8ca7d1 | 2019-06-24 00:08:31 +0300 | [diff] [blame] | 80 | static int of_get_regulation_constraints(struct device *dev, |
| 81 | struct device_node *np, |
Javier Martinez Canillas | 5e5e3a4 | 2014-11-10 14:43:55 +0100 | [diff] [blame] | 82 | struct regulator_init_data **init_data, |
| 83 | const struct regulator_desc *desc) |
Rajendra Nayak | 8f446e6 | 2011-11-18 16:47:17 +0530 | [diff] [blame] | 84 | { |
Rajendra Nayak | 8f446e6 | 2011-11-18 16:47:17 +0530 | [diff] [blame] | 85 | struct regulation_constraints *constraints = &(*init_data)->constraints; |
Chanwoo Choi | 40e20d6 | 2014-10-10 20:35:33 +0900 | [diff] [blame] | 86 | struct regulator_state *suspend_state; |
| 87 | struct device_node *suspend_np; |
Douglas Anderson | 02f3703 | 2018-04-18 08:54:18 -0700 | [diff] [blame] | 88 | unsigned int mode; |
David Collins | 54557ad | 2018-05-11 18:46:47 -0700 | [diff] [blame] | 89 | int ret, i, len; |
Dmitry Osipenko | d8ca7d1 | 2019-06-24 00:08:31 +0300 | [diff] [blame] | 90 | int n_phandles; |
Laxman Dewangan | 00c877c | 2013-09-18 18:18:02 +0530 | [diff] [blame] | 91 | u32 pval; |
Rajendra Nayak | 8f446e6 | 2011-11-18 16:47:17 +0530 | [diff] [blame] | 92 | |
Dmitry Osipenko | d8ca7d1 | 2019-06-24 00:08:31 +0300 | [diff] [blame] | 93 | n_phandles = of_count_phandle_with_args(np, "regulator-coupled-with", |
| 94 | NULL); |
| 95 | n_phandles = max(n_phandles, 0); |
| 96 | |
Rajendra Nayak | 8f446e6 | 2011-11-18 16:47:17 +0530 | [diff] [blame] | 97 | constraints->name = of_get_property(np, "regulator-name", NULL); |
| 98 | |
Laxman Dewangan | a34785f | 2016-03-10 17:42:47 +0530 | [diff] [blame] | 99 | if (!of_property_read_u32(np, "regulator-min-microvolt", &pval)) |
| 100 | constraints->min_uV = pval; |
| 101 | |
| 102 | if (!of_property_read_u32(np, "regulator-max-microvolt", &pval)) |
| 103 | constraints->max_uV = pval; |
Rajendra Nayak | 8f446e6 | 2011-11-18 16:47:17 +0530 | [diff] [blame] | 104 | |
| 105 | /* Voltage change possible? */ |
Mark Brown | 45fa203 | 2016-03-30 08:26:09 -0700 | [diff] [blame] | 106 | if (constraints->min_uV != constraints->max_uV) |
Rajendra Nayak | 8f446e6 | 2011-11-18 16:47:17 +0530 | [diff] [blame] | 107 | constraints->valid_ops_mask |= REGULATOR_CHANGE_VOLTAGE; |
Mark Brown | 45fa203 | 2016-03-30 08:26:09 -0700 | [diff] [blame] | 108 | |
| 109 | /* Do we have a voltage range, if so try to apply it? */ |
| 110 | if (constraints->min_uV && constraints->max_uV) |
Mark Brown | ab62aa9 | 2011-12-05 10:58:41 +0000 | [diff] [blame] | 111 | constraints->apply_uV = true; |
Rajendra Nayak | 8f446e6 | 2011-11-18 16:47:17 +0530 | [diff] [blame] | 112 | |
Sergei Shtylyov | 1e050ea | 2014-05-27 00:27:19 +0400 | [diff] [blame] | 113 | if (!of_property_read_u32(np, "regulator-microvolt-offset", &pval)) |
| 114 | constraints->uV_offset = pval; |
| 115 | if (!of_property_read_u32(np, "regulator-min-microamp", &pval)) |
| 116 | constraints->min_uA = pval; |
| 117 | if (!of_property_read_u32(np, "regulator-max-microamp", &pval)) |
| 118 | constraints->max_uA = pval; |
Rajendra Nayak | 8f446e6 | 2011-11-18 16:47:17 +0530 | [diff] [blame] | 119 | |
Stephen Boyd | 36e4f83 | 2015-06-11 17:37:06 -0700 | [diff] [blame] | 120 | if (!of_property_read_u32(np, "regulator-input-current-limit-microamp", |
| 121 | &pval)) |
| 122 | constraints->ilim_uA = pval; |
| 123 | |
Rajendra Nayak | 8f446e6 | 2011-11-18 16:47:17 +0530 | [diff] [blame] | 124 | /* Current change possible? */ |
| 125 | if (constraints->min_uA != constraints->max_uA) |
| 126 | constraints->valid_ops_mask |= REGULATOR_CHANGE_CURRENT; |
| 127 | |
Sergei Shtylyov | 1e050ea | 2014-05-27 00:27:19 +0400 | [diff] [blame] | 128 | constraints->boot_on = of_property_read_bool(np, "regulator-boot-on"); |
| 129 | constraints->always_on = of_property_read_bool(np, "regulator-always-on"); |
| 130 | if (!constraints->always_on) /* status change should be possible. */ |
Rajendra Nayak | 8f446e6 | 2011-11-18 16:47:17 +0530 | [diff] [blame] | 131 | constraints->valid_ops_mask |= REGULATOR_CHANGE_STATUS; |
Yadwinder Singh Brar | 6f0b2c6 | 2012-06-11 17:41:08 +0530 | [diff] [blame] | 132 | |
Stephen Boyd | 23c779b | 2015-06-11 17:37:04 -0700 | [diff] [blame] | 133 | constraints->pull_down = of_property_read_bool(np, "regulator-pull-down"); |
| 134 | |
Kishon Vijay Abraham I | 93134c7 | 2013-06-20 14:07:37 +0530 | [diff] [blame] | 135 | if (of_property_read_bool(np, "regulator-allow-bypass")) |
| 136 | constraints->valid_ops_mask |= REGULATOR_CHANGE_BYPASS; |
| 137 | |
Bjorn Andersson | b263d20 | 2015-08-18 15:14:06 -0700 | [diff] [blame] | 138 | if (of_property_read_bool(np, "regulator-allow-set-load")) |
| 139 | constraints->valid_ops_mask |= REGULATOR_CHANGE_DRMS; |
| 140 | |
Sergei Shtylyov | 1e050ea | 2014-05-27 00:27:19 +0400 | [diff] [blame] | 141 | ret = of_property_read_u32(np, "regulator-ramp-delay", &pval); |
| 142 | if (!ret) { |
| 143 | if (pval) |
| 144 | constraints->ramp_delay = pval; |
Yadwinder Singh Brar | 1653ccf | 2013-06-29 18:21:15 +0530 | [diff] [blame] | 145 | else |
| 146 | constraints->ramp_disable = true; |
| 147 | } |
Laxman Dewangan | 00c877c | 2013-09-18 18:18:02 +0530 | [diff] [blame] | 148 | |
Laxman Dewangan | d6c1dc3 | 2017-04-04 18:59:50 +0530 | [diff] [blame] | 149 | ret = of_property_read_u32(np, "regulator-settling-time-us", &pval); |
| 150 | if (!ret) |
| 151 | constraints->settling_time = pval; |
| 152 | |
Matthias Kaehlcke | 3ffad46 | 2017-05-16 11:43:43 -0700 | [diff] [blame] | 153 | ret = of_property_read_u32(np, "regulator-settling-time-up-us", &pval); |
| 154 | if (!ret) |
| 155 | constraints->settling_time_up = pval; |
| 156 | if (constraints->settling_time_up && constraints->settling_time) { |
Rob Herring | 0c9721a | 2018-08-27 20:52:42 -0500 | [diff] [blame] | 157 | pr_warn("%pOFn: ambiguous configuration for settling time, ignoring 'regulator-settling-time-up-us'\n", |
| 158 | np); |
Matthias Kaehlcke | 3ffad46 | 2017-05-16 11:43:43 -0700 | [diff] [blame] | 159 | constraints->settling_time_up = 0; |
| 160 | } |
| 161 | |
| 162 | ret = of_property_read_u32(np, "regulator-settling-time-down-us", |
| 163 | &pval); |
| 164 | if (!ret) |
| 165 | constraints->settling_time_down = pval; |
| 166 | if (constraints->settling_time_down && constraints->settling_time) { |
Rob Herring | 0c9721a | 2018-08-27 20:52:42 -0500 | [diff] [blame] | 167 | pr_warn("%pOFn: ambiguous configuration for settling time, ignoring 'regulator-settling-time-down-us'\n", |
| 168 | np); |
Matthias Kaehlcke | 3ffad46 | 2017-05-16 11:43:43 -0700 | [diff] [blame] | 169 | constraints->settling_time_down = 0; |
| 170 | } |
| 171 | |
Laxman Dewangan | 00c877c | 2013-09-18 18:18:02 +0530 | [diff] [blame] | 172 | ret = of_property_read_u32(np, "regulator-enable-ramp-delay", &pval); |
| 173 | if (!ret) |
| 174 | constraints->enable_time = pval; |
Chanwoo Choi | 40e20d6 | 2014-10-10 20:35:33 +0900 | [diff] [blame] | 175 | |
Stephen Boyd | 57f66b7 | 2015-06-11 17:37:05 -0700 | [diff] [blame] | 176 | constraints->soft_start = of_property_read_bool(np, |
| 177 | "regulator-soft-start"); |
Laxman Dewangan | 670666b | 2016-03-02 16:24:46 +0530 | [diff] [blame] | 178 | ret = of_property_read_u32(np, "regulator-active-discharge", &pval); |
| 179 | if (!ret) { |
| 180 | constraints->active_discharge = |
| 181 | (pval) ? REGULATOR_ACTIVE_DISCHARGE_ENABLE : |
| 182 | REGULATOR_ACTIVE_DISCHARGE_DISABLE; |
| 183 | } |
Stephen Boyd | 57f66b7 | 2015-06-11 17:37:05 -0700 | [diff] [blame] | 184 | |
Javier Martinez Canillas | 5e5e3a4 | 2014-11-10 14:43:55 +0100 | [diff] [blame] | 185 | if (!of_property_read_u32(np, "regulator-initial-mode", &pval)) { |
| 186 | if (desc && desc->of_map_mode) { |
Douglas Anderson | 02f3703 | 2018-04-18 08:54:18 -0700 | [diff] [blame] | 187 | mode = desc->of_map_mode(pval); |
| 188 | if (mode == REGULATOR_MODE_INVALID) |
Rob Herring | 0c9721a | 2018-08-27 20:52:42 -0500 | [diff] [blame] | 189 | pr_err("%pOFn: invalid mode %u\n", np, pval); |
Javier Martinez Canillas | 5e5e3a4 | 2014-11-10 14:43:55 +0100 | [diff] [blame] | 190 | else |
Douglas Anderson | 02f3703 | 2018-04-18 08:54:18 -0700 | [diff] [blame] | 191 | constraints->initial_mode = mode; |
Javier Martinez Canillas | 5e5e3a4 | 2014-11-10 14:43:55 +0100 | [diff] [blame] | 192 | } else { |
Rob Herring | 0c9721a | 2018-08-27 20:52:42 -0500 | [diff] [blame] | 193 | pr_warn("%pOFn: mapping for mode %d not defined\n", |
| 194 | np, pval); |
Javier Martinez Canillas | 5e5e3a4 | 2014-11-10 14:43:55 +0100 | [diff] [blame] | 195 | } |
| 196 | } |
| 197 | |
David Collins | 54557ad | 2018-05-11 18:46:47 -0700 | [diff] [blame] | 198 | len = of_property_count_elems_of_size(np, "regulator-allowed-modes", |
| 199 | sizeof(u32)); |
| 200 | if (len > 0) { |
| 201 | if (desc && desc->of_map_mode) { |
| 202 | for (i = 0; i < len; i++) { |
| 203 | ret = of_property_read_u32_index(np, |
| 204 | "regulator-allowed-modes", i, &pval); |
| 205 | if (ret) { |
Rob Herring | 0c9721a | 2018-08-27 20:52:42 -0500 | [diff] [blame] | 206 | pr_err("%pOFn: couldn't read allowed modes index %d, ret=%d\n", |
| 207 | np, i, ret); |
David Collins | 54557ad | 2018-05-11 18:46:47 -0700 | [diff] [blame] | 208 | break; |
| 209 | } |
| 210 | mode = desc->of_map_mode(pval); |
| 211 | if (mode == REGULATOR_MODE_INVALID) |
Rob Herring | 0c9721a | 2018-08-27 20:52:42 -0500 | [diff] [blame] | 212 | pr_err("%pOFn: invalid regulator-allowed-modes element %u\n", |
| 213 | np, pval); |
David Collins | 54557ad | 2018-05-11 18:46:47 -0700 | [diff] [blame] | 214 | else |
| 215 | constraints->valid_modes_mask |= mode; |
| 216 | } |
| 217 | if (constraints->valid_modes_mask) |
| 218 | constraints->valid_ops_mask |
| 219 | |= REGULATOR_CHANGE_MODE; |
| 220 | } else { |
Rob Herring | 0c9721a | 2018-08-27 20:52:42 -0500 | [diff] [blame] | 221 | pr_warn("%pOFn: mode mapping not defined\n", np); |
David Collins | 54557ad | 2018-05-11 18:46:47 -0700 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | |
Stephen Boyd | 22a10bc | 2015-06-11 17:37:03 -0700 | [diff] [blame] | 225 | if (!of_property_read_u32(np, "regulator-system-load", &pval)) |
| 226 | constraints->system_load = pval; |
| 227 | |
Dmitry Osipenko | d8ca7d1 | 2019-06-24 00:08:31 +0300 | [diff] [blame] | 228 | if (n_phandles) { |
| 229 | constraints->max_spread = devm_kzalloc(dev, |
| 230 | sizeof(*constraints->max_spread) * n_phandles, |
| 231 | GFP_KERNEL); |
| 232 | |
| 233 | if (!constraints->max_spread) |
| 234 | return -ENOMEM; |
| 235 | |
| 236 | of_property_read_u32_array(np, "regulator-coupled-max-spread", |
| 237 | constraints->max_spread, n_phandles); |
| 238 | } |
Maciej Purski | a085a31 | 2018-04-23 16:33:39 +0200 | [diff] [blame] | 239 | |
Dmitry Osipenko | 85254bc | 2018-10-05 18:36:35 +0300 | [diff] [blame] | 240 | if (!of_property_read_u32(np, "regulator-max-step-microvolt", |
| 241 | &pval)) |
| 242 | constraints->max_uV_step = pval; |
| 243 | |
Stephen Boyd | 3a003ba | 2015-07-17 14:41:54 -0700 | [diff] [blame] | 244 | constraints->over_current_protection = of_property_read_bool(np, |
| 245 | "regulator-over-current-protection"); |
| 246 | |
Matti Vaittinen | 89a6a5e | 2021-06-03 08:42:12 +0300 | [diff] [blame] | 247 | of_get_regulator_prot_limits(np, constraints); |
| 248 | |
Chanwoo Choi | 40e20d6 | 2014-10-10 20:35:33 +0900 | [diff] [blame] | 249 | for (i = 0; i < ARRAY_SIZE(regulator_states); i++) { |
| 250 | switch (i) { |
| 251 | case PM_SUSPEND_MEM: |
| 252 | suspend_state = &constraints->state_mem; |
| 253 | break; |
| 254 | case PM_SUSPEND_MAX: |
| 255 | suspend_state = &constraints->state_disk; |
| 256 | break; |
Andrei.Stefanescu@microchip.com | f2b4076 | 2018-11-13 14:47:37 +0000 | [diff] [blame] | 257 | case PM_SUSPEND_STANDBY: |
| 258 | suspend_state = &constraints->state_standby; |
| 259 | break; |
Chanwoo Choi | 40e20d6 | 2014-10-10 20:35:33 +0900 | [diff] [blame] | 260 | case PM_SUSPEND_ON: |
Rafael J. Wysocki | 690cbb9 | 2017-08-10 00:13:07 +0200 | [diff] [blame] | 261 | case PM_SUSPEND_TO_IDLE: |
Chanwoo Choi | 40e20d6 | 2014-10-10 20:35:33 +0900 | [diff] [blame] | 262 | default: |
| 263 | continue; |
Krzysztof Kozlowski | 7cf225b | 2015-04-23 21:24:32 +0900 | [diff] [blame] | 264 | } |
Chanwoo Choi | 40e20d6 | 2014-10-10 20:35:33 +0900 | [diff] [blame] | 265 | |
| 266 | suspend_np = of_get_child_by_name(np, regulator_states[i]); |
| 267 | if (!suspend_np || !suspend_state) |
| 268 | continue; |
| 269 | |
Javier Martinez Canillas | 5e5e3a4 | 2014-11-10 14:43:55 +0100 | [diff] [blame] | 270 | if (!of_property_read_u32(suspend_np, "regulator-mode", |
| 271 | &pval)) { |
| 272 | if (desc && desc->of_map_mode) { |
Douglas Anderson | 02f3703 | 2018-04-18 08:54:18 -0700 | [diff] [blame] | 273 | mode = desc->of_map_mode(pval); |
| 274 | if (mode == REGULATOR_MODE_INVALID) |
Rob Herring | 0c9721a | 2018-08-27 20:52:42 -0500 | [diff] [blame] | 275 | pr_err("%pOFn: invalid mode %u\n", |
| 276 | np, pval); |
Javier Martinez Canillas | 5e5e3a4 | 2014-11-10 14:43:55 +0100 | [diff] [blame] | 277 | else |
Douglas Anderson | 02f3703 | 2018-04-18 08:54:18 -0700 | [diff] [blame] | 278 | suspend_state->mode = mode; |
Javier Martinez Canillas | 5e5e3a4 | 2014-11-10 14:43:55 +0100 | [diff] [blame] | 279 | } else { |
Rob Herring | 0c9721a | 2018-08-27 20:52:42 -0500 | [diff] [blame] | 280 | pr_warn("%pOFn: mapping for mode %d not defined\n", |
| 281 | np, pval); |
Javier Martinez Canillas | 5e5e3a4 | 2014-11-10 14:43:55 +0100 | [diff] [blame] | 282 | } |
| 283 | } |
| 284 | |
Chanwoo Choi | 40e20d6 | 2014-10-10 20:35:33 +0900 | [diff] [blame] | 285 | if (of_property_read_bool(suspend_np, |
| 286 | "regulator-on-in-suspend")) |
Chunyan Zhang | 72069f9 | 2018-01-26 21:08:45 +0800 | [diff] [blame] | 287 | suspend_state->enabled = ENABLE_IN_SUSPEND; |
Chanwoo Choi | 40e20d6 | 2014-10-10 20:35:33 +0900 | [diff] [blame] | 288 | else if (of_property_read_bool(suspend_np, |
| 289 | "regulator-off-in-suspend")) |
Chunyan Zhang | 72069f9 | 2018-01-26 21:08:45 +0800 | [diff] [blame] | 290 | suspend_state->enabled = DISABLE_IN_SUSPEND; |
Chanwoo Choi | 40e20d6 | 2014-10-10 20:35:33 +0900 | [diff] [blame] | 291 | |
Marco Felsch | 131cb12 | 2019-09-17 17:40:20 +0200 | [diff] [blame] | 292 | if (!of_property_read_u32(suspend_np, |
| 293 | "regulator-suspend-min-microvolt", &pval)) |
Chunyan Zhang | f7efad1 | 2018-01-26 21:08:47 +0800 | [diff] [blame] | 294 | suspend_state->min_uV = pval; |
| 295 | |
Marco Felsch | 131cb12 | 2019-09-17 17:40:20 +0200 | [diff] [blame] | 296 | if (!of_property_read_u32(suspend_np, |
| 297 | "regulator-suspend-max-microvolt", &pval)) |
Chunyan Zhang | f7efad1 | 2018-01-26 21:08:47 +0800 | [diff] [blame] | 298 | suspend_state->max_uV = pval; |
Chanwoo Choi | 40e20d6 | 2014-10-10 20:35:33 +0900 | [diff] [blame] | 299 | |
Doug Anderson | 8cbcaea | 2014-10-31 20:52:58 -0700 | [diff] [blame] | 300 | if (!of_property_read_u32(suspend_np, |
| 301 | "regulator-suspend-microvolt", &pval)) |
| 302 | suspend_state->uV = pval; |
Chunyan Zhang | f7efad1 | 2018-01-26 21:08:47 +0800 | [diff] [blame] | 303 | else /* otherwise use min_uV as default suspend voltage */ |
| 304 | suspend_state->uV = suspend_state->min_uV; |
| 305 | |
| 306 | if (of_property_read_bool(suspend_np, |
| 307 | "regulator-changeable-in-suspend")) |
| 308 | suspend_state->changeable = true; |
Doug Anderson | 8cbcaea | 2014-10-31 20:52:58 -0700 | [diff] [blame] | 309 | |
Keerthy | a0f78bc | 2016-06-20 14:13:31 +0530 | [diff] [blame] | 310 | if (i == PM_SUSPEND_MEM) |
| 311 | constraints->initial_state = PM_SUSPEND_MEM; |
| 312 | |
Javier Martinez Canillas | 4eafec8 | 2014-10-20 16:47:48 +0200 | [diff] [blame] | 313 | of_node_put(suspend_np); |
Chanwoo Choi | 40e20d6 | 2014-10-10 20:35:33 +0900 | [diff] [blame] | 314 | suspend_state = NULL; |
| 315 | suspend_np = NULL; |
| 316 | } |
Dmitry Osipenko | d8ca7d1 | 2019-06-24 00:08:31 +0300 | [diff] [blame] | 317 | |
| 318 | return 0; |
Rajendra Nayak | 8f446e6 | 2011-11-18 16:47:17 +0530 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | /** |
| 322 | * of_get_regulator_init_data - extract regulator_init_data structure info |
| 323 | * @dev: device requesting for regulator_init_data |
Javier Martinez Canillas | 072e78b | 2014-11-10 14:43:53 +0100 | [diff] [blame] | 324 | * @node: regulator device node |
| 325 | * @desc: regulator description |
Rajendra Nayak | 8f446e6 | 2011-11-18 16:47:17 +0530 | [diff] [blame] | 326 | * |
| 327 | * Populates regulator_init_data structure by extracting data from device |
Krzysztof Kozlowski | 48f1b4e | 2019-01-08 13:12:33 +0100 | [diff] [blame] | 328 | * tree node, returns a pointer to the populated structure or NULL if memory |
Rajendra Nayak | 8f446e6 | 2011-11-18 16:47:17 +0530 | [diff] [blame] | 329 | * alloc fails. |
| 330 | */ |
Shawn Guo | d9a861c | 2011-12-01 17:21:06 +0800 | [diff] [blame] | 331 | 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] | 332 | struct device_node *node, |
| 333 | const struct regulator_desc *desc) |
Rajendra Nayak | 8f446e6 | 2011-11-18 16:47:17 +0530 | [diff] [blame] | 334 | { |
| 335 | struct regulator_init_data *init_data; |
| 336 | |
Shawn Guo | d9a861c | 2011-12-01 17:21:06 +0800 | [diff] [blame] | 337 | if (!node) |
Rajendra Nayak | 8f446e6 | 2011-11-18 16:47:17 +0530 | [diff] [blame] | 338 | return NULL; |
| 339 | |
| 340 | init_data = devm_kzalloc(dev, sizeof(*init_data), GFP_KERNEL); |
| 341 | if (!init_data) |
| 342 | return NULL; /* Out of memory? */ |
| 343 | |
Dmitry Osipenko | d8ca7d1 | 2019-06-24 00:08:31 +0300 | [diff] [blame] | 344 | if (of_get_regulation_constraints(dev, node, &init_data, desc)) |
| 345 | return NULL; |
| 346 | |
Rajendra Nayak | 8f446e6 | 2011-11-18 16:47:17 +0530 | [diff] [blame] | 347 | return init_data; |
| 348 | } |
Axel Lin | e69af5e | 2011-11-26 18:50:58 +0800 | [diff] [blame] | 349 | EXPORT_SYMBOL_GPL(of_get_regulator_init_data); |
Thierry Reding | 1c8fa58 | 2012-04-26 16:52:20 +0200 | [diff] [blame] | 350 | |
Charles Keepax | 13b3fde | 2014-04-15 13:34:36 +0100 | [diff] [blame] | 351 | struct devm_of_regulator_matches { |
| 352 | struct of_regulator_match *matches; |
| 353 | unsigned int num_matches; |
| 354 | }; |
| 355 | |
| 356 | static void devm_of_regulator_put_matches(struct device *dev, void *res) |
| 357 | { |
| 358 | struct devm_of_regulator_matches *devm_matches = res; |
| 359 | int i; |
| 360 | |
| 361 | for (i = 0; i < devm_matches->num_matches; i++) |
| 362 | of_node_put(devm_matches->matches[i].of_node); |
| 363 | } |
| 364 | |
Thierry Reding | 1c8fa58 | 2012-04-26 16:52:20 +0200 | [diff] [blame] | 365 | /** |
Stephen Warren | 13511de | 2012-09-24 13:25:00 -0600 | [diff] [blame] | 366 | * of_regulator_match - extract multiple regulator init data from device tree. |
Thierry Reding | 1c8fa58 | 2012-04-26 16:52:20 +0200 | [diff] [blame] | 367 | * @dev: device requesting the data |
| 368 | * @node: parent device node of the regulators |
| 369 | * @matches: match table for the regulators |
| 370 | * @num_matches: number of entries in match table |
| 371 | * |
Stephen Warren | 13511de | 2012-09-24 13:25:00 -0600 | [diff] [blame] | 372 | * This function uses a match table specified by the regulator driver to |
| 373 | * parse regulator init data from the device tree. @node is expected to |
| 374 | * contain a set of child nodes, each providing the init data for one |
| 375 | * regulator. The data parsed from a child node will be matched to a regulator |
| 376 | * based on either the deprecated property regulator-compatible if present, |
| 377 | * 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] | 378 | * in place and an additional of_node reference is taken for each matched |
| 379 | * regulator. |
Thierry Reding | 1c8fa58 | 2012-04-26 16:52:20 +0200 | [diff] [blame] | 380 | * |
| 381 | * Returns the number of matches found or a negative error code on failure. |
| 382 | */ |
| 383 | int of_regulator_match(struct device *dev, struct device_node *node, |
| 384 | struct of_regulator_match *matches, |
| 385 | unsigned int num_matches) |
| 386 | { |
| 387 | unsigned int count = 0; |
| 388 | unsigned int i; |
Stephen Warren | 13511de | 2012-09-24 13:25:00 -0600 | [diff] [blame] | 389 | const char *name; |
Laxman Dewangan | 5260cd2 | 2012-06-20 17:53:06 +0530 | [diff] [blame] | 390 | struct device_node *child; |
Charles Keepax | 13b3fde | 2014-04-15 13:34:36 +0100 | [diff] [blame] | 391 | struct devm_of_regulator_matches *devm_matches; |
Thierry Reding | 1c8fa58 | 2012-04-26 16:52:20 +0200 | [diff] [blame] | 392 | |
| 393 | if (!dev || !node) |
| 394 | return -EINVAL; |
| 395 | |
Charles Keepax | 13b3fde | 2014-04-15 13:34:36 +0100 | [diff] [blame] | 396 | devm_matches = devres_alloc(devm_of_regulator_put_matches, |
| 397 | sizeof(struct devm_of_regulator_matches), |
| 398 | GFP_KERNEL); |
| 399 | if (!devm_matches) |
| 400 | return -ENOMEM; |
| 401 | |
| 402 | devm_matches->matches = matches; |
| 403 | devm_matches->num_matches = num_matches; |
| 404 | |
| 405 | devres_add(dev, devm_matches); |
| 406 | |
Stephen Warren | a2f95c3 | 2013-01-29 12:01:13 -0700 | [diff] [blame] | 407 | for (i = 0; i < num_matches; i++) { |
| 408 | struct of_regulator_match *match = &matches[i]; |
| 409 | match->init_data = NULL; |
| 410 | match->of_node = NULL; |
| 411 | } |
| 412 | |
Laxman Dewangan | 5260cd2 | 2012-06-20 17:53:06 +0530 | [diff] [blame] | 413 | for_each_child_of_node(node, child) { |
Stephen Warren | 13511de | 2012-09-24 13:25:00 -0600 | [diff] [blame] | 414 | name = of_get_property(child, |
Laxman Dewangan | 5260cd2 | 2012-06-20 17:53:06 +0530 | [diff] [blame] | 415 | "regulator-compatible", NULL); |
Stephen Warren | 13511de | 2012-09-24 13:25:00 -0600 | [diff] [blame] | 416 | if (!name) |
| 417 | name = child->name; |
Laxman Dewangan | 5260cd2 | 2012-06-20 17:53:06 +0530 | [diff] [blame] | 418 | for (i = 0; i < num_matches; i++) { |
| 419 | struct of_regulator_match *match = &matches[i]; |
| 420 | if (match->of_node) |
| 421 | continue; |
Thierry Reding | 1c8fa58 | 2012-04-26 16:52:20 +0200 | [diff] [blame] | 422 | |
Stephen Warren | 13511de | 2012-09-24 13:25:00 -0600 | [diff] [blame] | 423 | if (strcmp(match->name, name)) |
Laxman Dewangan | 5260cd2 | 2012-06-20 17:53:06 +0530 | [diff] [blame] | 424 | continue; |
| 425 | |
| 426 | match->init_data = |
Javier Martinez Canillas | 75d6b2f | 2014-11-10 14:43:54 +0100 | [diff] [blame] | 427 | of_get_regulator_init_data(dev, child, |
| 428 | match->desc); |
Laxman Dewangan | 5260cd2 | 2012-06-20 17:53:06 +0530 | [diff] [blame] | 429 | if (!match->init_data) { |
| 430 | dev_err(dev, |
Rob Herring | 0c9721a | 2018-08-27 20:52:42 -0500 | [diff] [blame] | 431 | "failed to parse DT for regulator %pOFn\n", |
| 432 | child); |
Christophe JAILLET | 3096686 | 2018-01-26 23:13:44 +0100 | [diff] [blame] | 433 | of_node_put(child); |
Laxman Dewangan | 5260cd2 | 2012-06-20 17:53:06 +0530 | [diff] [blame] | 434 | return -EINVAL; |
| 435 | } |
Charles Keepax | bd0dda74 | 2014-04-03 15:32:15 +0100 | [diff] [blame] | 436 | match->of_node = of_node_get(child); |
Laxman Dewangan | 5260cd2 | 2012-06-20 17:53:06 +0530 | [diff] [blame] | 437 | count++; |
| 438 | break; |
| 439 | } |
Thierry Reding | 1c8fa58 | 2012-04-26 16:52:20 +0200 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | return count; |
| 443 | } |
| 444 | EXPORT_SYMBOL_GPL(of_regulator_match); |
Mark Brown | a0c7b16 | 2014-09-09 23:13:57 +0100 | [diff] [blame] | 445 | |
YueHaibing | 7a67eb1 | 2019-03-19 23:23:19 +0800 | [diff] [blame] | 446 | static struct |
| 447 | device_node *regulator_of_get_init_node(struct device *dev, |
| 448 | const struct regulator_desc *desc) |
Mark Brown | a0c7b16 | 2014-09-09 23:13:57 +0100 | [diff] [blame] | 449 | { |
| 450 | struct device_node *search, *child; |
Mark Brown | a0c7b16 | 2014-09-09 23:13:57 +0100 | [diff] [blame] | 451 | const char *name; |
| 452 | |
| 453 | if (!dev->of_node || !desc->of_match) |
| 454 | return NULL; |
| 455 | |
Charles Keepax | eba9473 | 2018-11-29 10:28:21 +0000 | [diff] [blame] | 456 | if (desc->regulators_node) { |
Mark Brown | a0c7b16 | 2014-09-09 23:13:57 +0100 | [diff] [blame] | 457 | search = of_get_child_by_name(dev->of_node, |
| 458 | desc->regulators_node); |
Charles Keepax | eba9473 | 2018-11-29 10:28:21 +0000 | [diff] [blame] | 459 | } else { |
Frank Rowand | 423a116 | 2017-07-18 16:36:37 -0700 | [diff] [blame] | 460 | search = of_node_get(dev->of_node); |
Mark Brown | a0c7b16 | 2014-09-09 23:13:57 +0100 | [diff] [blame] | 461 | |
Charles Keepax | eba9473 | 2018-11-29 10:28:21 +0000 | [diff] [blame] | 462 | if (!strcmp(desc->of_match, search->name)) |
| 463 | return search; |
| 464 | } |
| 465 | |
Mark Brown | a0c7b16 | 2014-09-09 23:13:57 +0100 | [diff] [blame] | 466 | if (!search) { |
Mark Brown | 7de79a1 | 2014-10-08 23:08:13 +0100 | [diff] [blame] | 467 | dev_dbg(dev, "Failed to find regulator container node '%s'\n", |
| 468 | desc->regulators_node); |
Mark Brown | a0c7b16 | 2014-09-09 23:13:57 +0100 | [diff] [blame] | 469 | return NULL; |
| 470 | } |
| 471 | |
Stephen Boyd | 130daa3 | 2015-05-12 14:42:07 -0700 | [diff] [blame] | 472 | for_each_available_child_of_node(search, child) { |
Mark Brown | a0c7b16 | 2014-09-09 23:13:57 +0100 | [diff] [blame] | 473 | name = of_get_property(child, "regulator-compatible", NULL); |
Cristian Marussi | e7095c3 | 2020-11-19 19:10:49 +0000 | [diff] [blame] | 474 | if (!name) { |
| 475 | if (!desc->of_match_full_name) |
| 476 | name = child->name; |
| 477 | else |
| 478 | name = child->full_name; |
| 479 | } |
Mark Brown | a0c7b16 | 2014-09-09 23:13:57 +0100 | [diff] [blame] | 480 | |
Nishka Dasgupta | 811ba48 | 2019-07-24 14:02:31 +0530 | [diff] [blame] | 481 | if (!strcmp(desc->of_match, name)) { |
| 482 | of_node_put(search); |
Christophe JAILLET | 8a065ce | 2021-04-20 21:31:51 +0200 | [diff] [blame] | 483 | /* |
| 484 | * 'of_node_get(child)' is already performed by the |
| 485 | * for_each loop. |
| 486 | */ |
| 487 | return child; |
Nishka Dasgupta | 811ba48 | 2019-07-24 14:02:31 +0530 | [diff] [blame] | 488 | } |
Mark Brown | a0c7b16 | 2014-09-09 23:13:57 +0100 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | of_node_put(search); |
| 492 | |
Charles Keepax | 925c85e | 2018-11-29 10:28:20 +0000 | [diff] [blame] | 493 | return NULL; |
| 494 | } |
| 495 | |
| 496 | struct regulator_init_data *regulator_of_get_init_data(struct device *dev, |
| 497 | const struct regulator_desc *desc, |
| 498 | struct regulator_config *config, |
| 499 | struct device_node **node) |
| 500 | { |
| 501 | struct device_node *child; |
| 502 | struct regulator_init_data *init_data = NULL; |
| 503 | |
| 504 | child = regulator_of_get_init_node(dev, desc); |
| 505 | if (!child) |
| 506 | return NULL; |
| 507 | |
| 508 | init_data = of_get_regulator_init_data(dev, child, desc); |
| 509 | if (!init_data) { |
| 510 | dev_err(dev, "failed to parse DT for regulator %pOFn\n", child); |
| 511 | goto error; |
| 512 | } |
| 513 | |
Marco Felsch | f8970d3 | 2019-09-17 17:40:21 +0200 | [diff] [blame] | 514 | if (desc->of_parse_cb) { |
| 515 | int ret; |
| 516 | |
| 517 | ret = desc->of_parse_cb(child, desc, config); |
| 518 | if (ret) { |
| 519 | if (ret == -EPROBE_DEFER) { |
| 520 | of_node_put(child); |
| 521 | return ERR_PTR(-EPROBE_DEFER); |
| 522 | } |
| 523 | dev_err(dev, |
| 524 | "driver callback failed to parse DT for regulator %pOFn\n", |
| 525 | child); |
| 526 | goto error; |
| 527 | } |
Charles Keepax | 925c85e | 2018-11-29 10:28:20 +0000 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | *node = child; |
| 531 | |
Mark Brown | a0c7b16 | 2014-09-09 23:13:57 +0100 | [diff] [blame] | 532 | return init_data; |
Charles Keepax | 925c85e | 2018-11-29 10:28:20 +0000 | [diff] [blame] | 533 | |
| 534 | error: |
| 535 | of_node_put(child); |
| 536 | |
| 537 | return NULL; |
Mark Brown | a0c7b16 | 2014-09-09 23:13:57 +0100 | [diff] [blame] | 538 | } |
Maciej Purski | 148096a | 2018-01-22 15:30:06 +0100 | [diff] [blame] | 539 | |
Maciej Purski | 148096a | 2018-01-22 15:30:06 +0100 | [diff] [blame] | 540 | struct regulator_dev *of_find_regulator_by_node(struct device_node *np) |
| 541 | { |
| 542 | struct device *dev; |
| 543 | |
Suzuki K Poulose | cfba5de | 2019-07-23 23:18:33 +0100 | [diff] [blame] | 544 | dev = class_find_device_by_of_node(®ulator_class, np); |
Maciej Purski | 148096a | 2018-01-22 15:30:06 +0100 | [diff] [blame] | 545 | |
| 546 | return dev ? dev_to_rdev(dev) : NULL; |
| 547 | } |
Maciej Purski | a085a31 | 2018-04-23 16:33:39 +0200 | [diff] [blame] | 548 | |
| 549 | /* |
| 550 | * Returns number of regulators coupled with rdev. |
| 551 | */ |
| 552 | int of_get_n_coupled(struct regulator_dev *rdev) |
| 553 | { |
| 554 | struct device_node *node = rdev->dev.of_node; |
| 555 | int n_phandles; |
| 556 | |
| 557 | n_phandles = of_count_phandle_with_args(node, |
| 558 | "regulator-coupled-with", |
| 559 | NULL); |
| 560 | |
| 561 | return (n_phandles > 0) ? n_phandles : 0; |
| 562 | } |
| 563 | |
| 564 | /* Looks for "to_find" device_node in src's "regulator-coupled-with" property */ |
| 565 | static bool of_coupling_find_node(struct device_node *src, |
Dmitry Osipenko | d8ca7d1 | 2019-06-24 00:08:31 +0300 | [diff] [blame] | 566 | struct device_node *to_find, |
| 567 | int *index) |
Maciej Purski | a085a31 | 2018-04-23 16:33:39 +0200 | [diff] [blame] | 568 | { |
| 569 | int n_phandles, i; |
| 570 | bool found = false; |
| 571 | |
| 572 | n_phandles = of_count_phandle_with_args(src, |
| 573 | "regulator-coupled-with", |
| 574 | NULL); |
| 575 | |
| 576 | for (i = 0; i < n_phandles; i++) { |
| 577 | struct device_node *tmp = of_parse_phandle(src, |
| 578 | "regulator-coupled-with", i); |
| 579 | |
| 580 | if (!tmp) |
| 581 | break; |
| 582 | |
| 583 | /* found */ |
| 584 | if (tmp == to_find) |
| 585 | found = true; |
| 586 | |
| 587 | of_node_put(tmp); |
| 588 | |
Dmitry Osipenko | d8ca7d1 | 2019-06-24 00:08:31 +0300 | [diff] [blame] | 589 | if (found) { |
| 590 | *index = i; |
Maciej Purski | a085a31 | 2018-04-23 16:33:39 +0200 | [diff] [blame] | 591 | break; |
Dmitry Osipenko | d8ca7d1 | 2019-06-24 00:08:31 +0300 | [diff] [blame] | 592 | } |
Maciej Purski | a085a31 | 2018-04-23 16:33:39 +0200 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | return found; |
| 596 | } |
| 597 | |
| 598 | /** |
| 599 | * of_check_coupling_data - Parse rdev's coupling properties and check data |
| 600 | * consistency |
Lee Jones | 45e8446 | 2020-07-08 13:48:31 +0100 | [diff] [blame] | 601 | * @rdev: pointer to regulator_dev whose data is checked |
Maciej Purski | a085a31 | 2018-04-23 16:33:39 +0200 | [diff] [blame] | 602 | * |
| 603 | * Function checks if all the following conditions are met: |
| 604 | * - rdev's max_spread is greater than 0 |
| 605 | * - all coupled regulators have the same max_spread |
| 606 | * - all coupled regulators have the same number of regulator_dev phandles |
| 607 | * - all regulators are linked to each other |
| 608 | * |
| 609 | * Returns true if all conditions are met. |
| 610 | */ |
| 611 | bool of_check_coupling_data(struct regulator_dev *rdev) |
| 612 | { |
Maciej Purski | a085a31 | 2018-04-23 16:33:39 +0200 | [diff] [blame] | 613 | struct device_node *node = rdev->dev.of_node; |
| 614 | int n_phandles = of_get_n_coupled(rdev); |
| 615 | struct device_node *c_node; |
Dmitry Osipenko | d8ca7d1 | 2019-06-24 00:08:31 +0300 | [diff] [blame] | 616 | int index; |
Maciej Purski | a085a31 | 2018-04-23 16:33:39 +0200 | [diff] [blame] | 617 | int i; |
| 618 | bool ret = true; |
| 619 | |
Maciej Purski | a085a31 | 2018-04-23 16:33:39 +0200 | [diff] [blame] | 620 | /* iterate over rdev's phandles */ |
| 621 | for (i = 0; i < n_phandles; i++) { |
Dmitry Osipenko | d8ca7d1 | 2019-06-24 00:08:31 +0300 | [diff] [blame] | 622 | int max_spread = rdev->constraints->max_spread[i]; |
Maciej Purski | a085a31 | 2018-04-23 16:33:39 +0200 | [diff] [blame] | 623 | int c_max_spread, c_n_phandles; |
| 624 | |
Dmitry Osipenko | d8ca7d1 | 2019-06-24 00:08:31 +0300 | [diff] [blame] | 625 | if (max_spread <= 0) { |
| 626 | dev_err(&rdev->dev, "max_spread value invalid\n"); |
| 627 | return false; |
| 628 | } |
| 629 | |
Maciej Purski | a085a31 | 2018-04-23 16:33:39 +0200 | [diff] [blame] | 630 | c_node = of_parse_phandle(node, |
| 631 | "regulator-coupled-with", i); |
| 632 | |
| 633 | if (!c_node) |
| 634 | ret = false; |
| 635 | |
| 636 | c_n_phandles = of_count_phandle_with_args(c_node, |
| 637 | "regulator-coupled-with", |
| 638 | NULL); |
| 639 | |
| 640 | if (c_n_phandles != n_phandles) { |
Krzysztof Kozlowski | 48f1b4e | 2019-01-08 13:12:33 +0100 | [diff] [blame] | 641 | dev_err(&rdev->dev, "number of coupled reg phandles mismatch\n"); |
Maciej Purski | a085a31 | 2018-04-23 16:33:39 +0200 | [diff] [blame] | 642 | ret = false; |
| 643 | goto clean; |
| 644 | } |
| 645 | |
Dmitry Osipenko | d8ca7d1 | 2019-06-24 00:08:31 +0300 | [diff] [blame] | 646 | if (!of_coupling_find_node(c_node, node, &index)) { |
| 647 | dev_err(&rdev->dev, "missing 2-way linking for coupled regulators\n"); |
| 648 | ret = false; |
| 649 | goto clean; |
| 650 | } |
| 651 | |
| 652 | if (of_property_read_u32_index(c_node, "regulator-coupled-max-spread", |
| 653 | index, &c_max_spread)) { |
Maciej Purski | a085a31 | 2018-04-23 16:33:39 +0200 | [diff] [blame] | 654 | ret = false; |
| 655 | goto clean; |
| 656 | } |
| 657 | |
| 658 | if (c_max_spread != max_spread) { |
| 659 | dev_err(&rdev->dev, |
| 660 | "coupled regulators max_spread mismatch\n"); |
| 661 | ret = false; |
| 662 | goto clean; |
| 663 | } |
| 664 | |
Maciej Purski | a085a31 | 2018-04-23 16:33:39 +0200 | [diff] [blame] | 665 | clean: |
| 666 | of_node_put(c_node); |
| 667 | if (!ret) |
| 668 | break; |
| 669 | } |
| 670 | |
| 671 | return ret; |
| 672 | } |
| 673 | |
| 674 | /** |
| 675 | * of_parse_coupled regulator - Get regulator_dev pointer from rdev's property |
| 676 | * @rdev: Pointer to regulator_dev, whose DTS is used as a source to parse |
| 677 | * "regulator-coupled-with" property |
| 678 | * @index: Index in phandles array |
| 679 | * |
| 680 | * Returns the regulator_dev pointer parsed from DTS. If it has not been yet |
| 681 | * registered, returns NULL |
| 682 | */ |
| 683 | struct regulator_dev *of_parse_coupled_regulator(struct regulator_dev *rdev, |
| 684 | int index) |
| 685 | { |
| 686 | struct device_node *node = rdev->dev.of_node; |
| 687 | struct device_node *c_node; |
| 688 | struct regulator_dev *c_rdev; |
| 689 | |
| 690 | c_node = of_parse_phandle(node, "regulator-coupled-with", index); |
| 691 | if (!c_node) |
| 692 | return NULL; |
| 693 | |
| 694 | c_rdev = of_find_regulator_by_node(c_node); |
| 695 | |
| 696 | of_node_put(c_node); |
| 697 | |
| 698 | return c_rdev; |
| 699 | } |