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