Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Generic OPP OF helpers |
| 3 | * |
| 4 | * Copyright (C) 2009-2010 Texas Instruments Incorporated. |
| 5 | * Nishanth Menon |
| 6 | * Romit Dasgupta |
| 7 | * Kevin Hilman |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License version 2 as |
| 11 | * published by the Free Software Foundation. |
| 12 | */ |
| 13 | |
| 14 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 15 | |
| 16 | #include <linux/cpu.h> |
| 17 | #include <linux/errno.h> |
| 18 | #include <linux/device.h> |
Sudeep Holla | 9867999 | 2017-10-12 11:32:23 +0100 | [diff] [blame] | 19 | #include <linux/of_device.h> |
Viresh Kumar | 3ba9832 | 2016-11-18 15:47:46 +0530 | [diff] [blame] | 20 | #include <linux/pm_domain.h> |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 21 | #include <linux/slab.h> |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 22 | #include <linux/export.h> |
| 23 | |
| 24 | #include "opp.h" |
| 25 | |
Viresh Kumar | f06ed90 | 2018-06-14 12:04:43 +0530 | [diff] [blame] | 26 | /* |
| 27 | * Returns opp descriptor node for a device node, caller must |
| 28 | * do of_node_put(). |
| 29 | */ |
| 30 | static struct device_node *_opp_of_get_opp_desc_node(struct device_node *np, |
| 31 | int index) |
| 32 | { |
| 33 | /* "operating-points-v2" can be an array for power domain providers */ |
| 34 | return of_parse_phandle(np, "operating-points-v2", index); |
| 35 | } |
| 36 | |
| 37 | /* Returns opp descriptor node for a device, caller must do of_node_put() */ |
| 38 | struct device_node *dev_pm_opp_of_get_opp_desc_node(struct device *dev) |
| 39 | { |
| 40 | return _opp_of_get_opp_desc_node(dev->of_node, 0); |
| 41 | } |
| 42 | EXPORT_SYMBOL_GPL(dev_pm_opp_of_get_opp_desc_node); |
| 43 | |
Viresh Kumar | 283d55e | 2018-09-07 09:01:54 +0530 | [diff] [blame] | 44 | struct opp_table *_managed_opp(struct device *dev, int index) |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 45 | { |
Viresh Kumar | b83c189 | 2017-01-23 10:11:45 +0530 | [diff] [blame] | 46 | struct opp_table *opp_table, *managed_table = NULL; |
Viresh Kumar | 283d55e | 2018-09-07 09:01:54 +0530 | [diff] [blame] | 47 | struct device_node *np; |
Viresh Kumar | b83c189 | 2017-01-23 10:11:45 +0530 | [diff] [blame] | 48 | |
Viresh Kumar | 283d55e | 2018-09-07 09:01:54 +0530 | [diff] [blame] | 49 | np = _opp_of_get_opp_desc_node(dev->of_node, index); |
| 50 | if (!np) |
| 51 | return NULL; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 52 | |
Viresh Kumar | 052c6f1 | 2017-01-23 10:11:49 +0530 | [diff] [blame] | 53 | list_for_each_entry(opp_table, &opp_tables, node) { |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 54 | if (opp_table->np == np) { |
| 55 | /* |
| 56 | * Multiple devices can point to the same OPP table and |
| 57 | * so will have same node-pointer, np. |
| 58 | * |
| 59 | * But the OPPs will be considered as shared only if the |
| 60 | * OPP table contains a "opp-shared" property. |
| 61 | */ |
Viresh Kumar | b83c189 | 2017-01-23 10:11:45 +0530 | [diff] [blame] | 62 | if (opp_table->shared_opp == OPP_TABLE_ACCESS_SHARED) { |
| 63 | _get_opp_table_kref(opp_table); |
| 64 | managed_table = opp_table; |
| 65 | } |
Viresh Kumar | 79ee2e8 | 2016-06-16 19:03:11 +0530 | [diff] [blame] | 66 | |
Viresh Kumar | b83c189 | 2017-01-23 10:11:45 +0530 | [diff] [blame] | 67 | break; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | |
Viresh Kumar | 283d55e | 2018-09-07 09:01:54 +0530 | [diff] [blame] | 71 | of_node_put(np); |
Viresh Kumar | b83c189 | 2017-01-23 10:11:45 +0530 | [diff] [blame] | 72 | |
| 73 | return managed_table; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 74 | } |
| 75 | |
Viresh Kumar | 5d6d106 | 2018-06-07 14:50:43 +0530 | [diff] [blame^] | 76 | /* The caller must call dev_pm_opp_put() after the OPP is used */ |
| 77 | static struct dev_pm_opp *_find_opp_of_np(struct opp_table *opp_table, |
| 78 | struct device_node *opp_np) |
| 79 | { |
| 80 | struct dev_pm_opp *opp; |
| 81 | |
| 82 | lockdep_assert_held(&opp_table_lock); |
| 83 | |
| 84 | mutex_lock(&opp_table->lock); |
| 85 | |
| 86 | list_for_each_entry(opp, &opp_table->opp_list, node) { |
| 87 | if (opp->np == opp_np) { |
| 88 | dev_pm_opp_get(opp); |
| 89 | mutex_unlock(&opp_table->lock); |
| 90 | return opp; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | mutex_unlock(&opp_table->lock); |
| 95 | |
| 96 | return NULL; |
| 97 | } |
| 98 | |
| 99 | static struct device_node *of_parse_required_opp(struct device_node *np, |
| 100 | int index) |
| 101 | { |
| 102 | struct device_node *required_np; |
| 103 | |
| 104 | required_np = of_parse_phandle(np, "required-opps", index); |
| 105 | if (unlikely(!required_np)) { |
| 106 | pr_err("%s: Unable to parse required-opps: %pOF, index: %d\n", |
| 107 | __func__, np, index); |
| 108 | } |
| 109 | |
| 110 | return required_np; |
| 111 | } |
| 112 | |
| 113 | /* The caller must call dev_pm_opp_put_opp_table() after the table is used */ |
| 114 | static struct opp_table *_find_table_of_opp_np(struct device_node *opp_np) |
| 115 | { |
| 116 | struct opp_table *opp_table; |
| 117 | struct dev_pm_opp *opp; |
| 118 | |
| 119 | lockdep_assert_held(&opp_table_lock); |
| 120 | |
| 121 | list_for_each_entry(opp_table, &opp_tables, node) { |
| 122 | opp = _find_opp_of_np(opp_table, opp_np); |
| 123 | if (opp) { |
| 124 | dev_pm_opp_put(opp); |
| 125 | _get_opp_table_kref(opp_table); |
| 126 | return opp_table; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | return ERR_PTR(-ENODEV); |
| 131 | } |
| 132 | |
| 133 | /* Free resources previously acquired by _opp_table_alloc_required_tables() */ |
| 134 | static void _opp_table_free_required_tables(struct opp_table *opp_table) |
| 135 | { |
| 136 | struct opp_table **required_opp_tables = opp_table->required_opp_tables; |
| 137 | int i; |
| 138 | |
| 139 | if (!required_opp_tables) |
| 140 | return; |
| 141 | |
| 142 | for (i = 0; i < opp_table->required_opp_count; i++) { |
| 143 | if (IS_ERR_OR_NULL(required_opp_tables[i])) |
| 144 | break; |
| 145 | |
| 146 | dev_pm_opp_put_opp_table(required_opp_tables[i]); |
| 147 | } |
| 148 | |
| 149 | kfree(required_opp_tables); |
| 150 | |
| 151 | opp_table->required_opp_count = 0; |
| 152 | opp_table->required_opp_tables = NULL; |
| 153 | } |
| 154 | |
| 155 | /* |
| 156 | * Populate all devices and opp tables which are part of "required-opps" list. |
| 157 | * Checking only the first OPP node should be enough. |
| 158 | */ |
| 159 | static void _opp_table_alloc_required_tables(struct opp_table *opp_table, |
| 160 | struct device *dev, |
| 161 | struct device_node *opp_np) |
| 162 | { |
| 163 | struct opp_table **required_opp_tables; |
| 164 | struct device_node *required_np, *np; |
| 165 | int count, i; |
| 166 | |
| 167 | /* Traversing the first OPP node is all we need */ |
| 168 | np = of_get_next_available_child(opp_np, NULL); |
| 169 | if (!np) { |
| 170 | dev_err(dev, "Empty OPP table\n"); |
| 171 | return; |
| 172 | } |
| 173 | |
| 174 | count = of_count_phandle_with_args(np, "required-opps", NULL); |
| 175 | if (!count) |
| 176 | goto put_np; |
| 177 | |
| 178 | required_opp_tables = kcalloc(count, sizeof(*required_opp_tables), |
| 179 | GFP_KERNEL); |
| 180 | if (!required_opp_tables) |
| 181 | goto put_np; |
| 182 | |
| 183 | opp_table->required_opp_tables = required_opp_tables; |
| 184 | opp_table->required_opp_count = count; |
| 185 | |
| 186 | for (i = 0; i < count; i++) { |
| 187 | required_np = of_parse_required_opp(np, i); |
| 188 | if (!required_np) |
| 189 | goto free_required_tables; |
| 190 | |
| 191 | required_opp_tables[i] = _find_table_of_opp_np(required_np); |
| 192 | of_node_put(required_np); |
| 193 | |
| 194 | if (IS_ERR(required_opp_tables[i])) |
| 195 | goto free_required_tables; |
| 196 | |
| 197 | /* |
| 198 | * We only support genpd's OPPs in the "required-opps" for now, |
| 199 | * as we don't know how much about other cases. Error out if the |
| 200 | * required OPP doesn't belong to a genpd. |
| 201 | */ |
| 202 | if (!required_opp_tables[i]->is_genpd) { |
| 203 | dev_err(dev, "required-opp doesn't belong to genpd: %pOF\n", |
| 204 | required_np); |
| 205 | goto free_required_tables; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | goto put_np; |
| 210 | |
| 211 | free_required_tables: |
| 212 | _opp_table_free_required_tables(opp_table); |
| 213 | put_np: |
| 214 | of_node_put(np); |
| 215 | } |
| 216 | |
Viresh Kumar | eb7c8743 | 2018-09-05 16:17:14 +0530 | [diff] [blame] | 217 | void _of_init_opp_table(struct opp_table *opp_table, struct device *dev, |
| 218 | int index) |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 219 | { |
Viresh Kumar | f06ed90 | 2018-06-14 12:04:43 +0530 | [diff] [blame] | 220 | struct device_node *np, *opp_np; |
| 221 | u32 val; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 222 | |
| 223 | /* |
| 224 | * Only required for backward compatibility with v1 bindings, but isn't |
| 225 | * harmful for other cases. And so we do it unconditionally. |
| 226 | */ |
| 227 | np = of_node_get(dev->of_node); |
Viresh Kumar | f06ed90 | 2018-06-14 12:04:43 +0530 | [diff] [blame] | 228 | if (!np) |
| 229 | return; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 230 | |
Viresh Kumar | f06ed90 | 2018-06-14 12:04:43 +0530 | [diff] [blame] | 231 | if (!of_property_read_u32(np, "clock-latency", &val)) |
| 232 | opp_table->clock_latency_ns_max = val; |
| 233 | of_property_read_u32(np, "voltage-tolerance", |
| 234 | &opp_table->voltage_tolerance_v1); |
| 235 | |
Viresh Kumar | 61d8e7c | 2018-06-13 16:00:11 +0530 | [diff] [blame] | 236 | if (of_find_property(np, "#power-domain-cells", NULL)) |
| 237 | opp_table->is_genpd = true; |
| 238 | |
Viresh Kumar | f06ed90 | 2018-06-14 12:04:43 +0530 | [diff] [blame] | 239 | /* Get OPP table node */ |
| 240 | opp_np = _opp_of_get_opp_desc_node(np, index); |
| 241 | of_node_put(np); |
| 242 | |
| 243 | if (!opp_np) |
| 244 | return; |
| 245 | |
| 246 | if (of_property_read_bool(opp_np, "opp-shared")) |
| 247 | opp_table->shared_opp = OPP_TABLE_ACCESS_SHARED; |
| 248 | else |
| 249 | opp_table->shared_opp = OPP_TABLE_ACCESS_EXCLUSIVE; |
| 250 | |
| 251 | opp_table->np = opp_np; |
| 252 | |
Viresh Kumar | 5d6d106 | 2018-06-07 14:50:43 +0530 | [diff] [blame^] | 253 | _opp_table_alloc_required_tables(opp_table, dev, opp_np); |
Viresh Kumar | f06ed90 | 2018-06-14 12:04:43 +0530 | [diff] [blame] | 254 | of_node_put(opp_np); |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 255 | } |
| 256 | |
Viresh Kumar | 5d6d106 | 2018-06-07 14:50:43 +0530 | [diff] [blame^] | 257 | void _of_clear_opp_table(struct opp_table *opp_table) |
| 258 | { |
| 259 | _opp_table_free_required_tables(opp_table); |
| 260 | } |
| 261 | |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 262 | static bool _opp_is_supported(struct device *dev, struct opp_table *opp_table, |
| 263 | struct device_node *np) |
| 264 | { |
| 265 | unsigned int count = opp_table->supported_hw_count; |
| 266 | u32 version; |
| 267 | int ret; |
| 268 | |
Dave Gerlach | a4ee454 | 2016-09-23 15:07:47 -0500 | [diff] [blame] | 269 | if (!opp_table->supported_hw) { |
| 270 | /* |
| 271 | * In the case that no supported_hw has been set by the |
| 272 | * platform but there is an opp-supported-hw value set for |
| 273 | * an OPP then the OPP should not be enabled as there is |
| 274 | * no way to see if the hardware supports it. |
| 275 | */ |
| 276 | if (of_find_property(np, "opp-supported-hw", NULL)) |
| 277 | return false; |
| 278 | else |
| 279 | return true; |
| 280 | } |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 281 | |
| 282 | while (count--) { |
| 283 | ret = of_property_read_u32_index(np, "opp-supported-hw", count, |
| 284 | &version); |
| 285 | if (ret) { |
| 286 | dev_warn(dev, "%s: failed to read opp-supported-hw property at index %d: %d\n", |
| 287 | __func__, count, ret); |
| 288 | return false; |
| 289 | } |
| 290 | |
| 291 | /* Both of these are bitwise masks of the versions */ |
| 292 | if (!(version & opp_table->supported_hw[count])) |
| 293 | return false; |
| 294 | } |
| 295 | |
| 296 | return true; |
| 297 | } |
| 298 | |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 299 | static int opp_parse_supplies(struct dev_pm_opp *opp, struct device *dev, |
| 300 | struct opp_table *opp_table) |
| 301 | { |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 302 | u32 *microvolt, *microamp = NULL; |
| 303 | int supplies, vcount, icount, ret, i, j; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 304 | struct property *prop = NULL; |
| 305 | char name[NAME_MAX]; |
| 306 | |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 307 | supplies = opp_table->regulator_count ? opp_table->regulator_count : 1; |
| 308 | |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 309 | /* Search for "opp-microvolt-<name>" */ |
| 310 | if (opp_table->prop_name) { |
| 311 | snprintf(name, sizeof(name), "opp-microvolt-%s", |
| 312 | opp_table->prop_name); |
| 313 | prop = of_find_property(opp->np, name, NULL); |
| 314 | } |
| 315 | |
| 316 | if (!prop) { |
| 317 | /* Search for "opp-microvolt" */ |
| 318 | sprintf(name, "opp-microvolt"); |
| 319 | prop = of_find_property(opp->np, name, NULL); |
| 320 | |
| 321 | /* Missing property isn't a problem, but an invalid entry is */ |
Viresh Kumar | 688a48b | 2017-05-23 09:32:12 +0530 | [diff] [blame] | 322 | if (!prop) { |
| 323 | if (!opp_table->regulator_count) |
| 324 | return 0; |
| 325 | |
| 326 | dev_err(dev, "%s: opp-microvolt missing although OPP managing regulators\n", |
| 327 | __func__); |
| 328 | return -EINVAL; |
| 329 | } |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 330 | } |
| 331 | |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 332 | vcount = of_property_count_u32_elems(opp->np, name); |
| 333 | if (vcount < 0) { |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 334 | dev_err(dev, "%s: Invalid %s property (%d)\n", |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 335 | __func__, name, vcount); |
| 336 | return vcount; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 337 | } |
| 338 | |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 339 | /* There can be one or three elements per supply */ |
| 340 | if (vcount != supplies && vcount != supplies * 3) { |
| 341 | dev_err(dev, "%s: Invalid number of elements in %s property (%d) with supplies (%d)\n", |
| 342 | __func__, name, vcount, supplies); |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 343 | return -EINVAL; |
| 344 | } |
| 345 | |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 346 | microvolt = kmalloc_array(vcount, sizeof(*microvolt), GFP_KERNEL); |
| 347 | if (!microvolt) |
| 348 | return -ENOMEM; |
| 349 | |
| 350 | ret = of_property_read_u32_array(opp->np, name, microvolt, vcount); |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 351 | if (ret) { |
| 352 | dev_err(dev, "%s: error parsing %s: %d\n", __func__, name, ret); |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 353 | ret = -EINVAL; |
| 354 | goto free_microvolt; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | /* Search for "opp-microamp-<name>" */ |
| 358 | prop = NULL; |
| 359 | if (opp_table->prop_name) { |
| 360 | snprintf(name, sizeof(name), "opp-microamp-%s", |
| 361 | opp_table->prop_name); |
| 362 | prop = of_find_property(opp->np, name, NULL); |
| 363 | } |
| 364 | |
| 365 | if (!prop) { |
| 366 | /* Search for "opp-microamp" */ |
| 367 | sprintf(name, "opp-microamp"); |
| 368 | prop = of_find_property(opp->np, name, NULL); |
| 369 | } |
| 370 | |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 371 | if (prop) { |
| 372 | icount = of_property_count_u32_elems(opp->np, name); |
| 373 | if (icount < 0) { |
| 374 | dev_err(dev, "%s: Invalid %s property (%d)\n", __func__, |
| 375 | name, icount); |
| 376 | ret = icount; |
| 377 | goto free_microvolt; |
| 378 | } |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 379 | |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 380 | if (icount != supplies) { |
| 381 | dev_err(dev, "%s: Invalid number of elements in %s property (%d) with supplies (%d)\n", |
| 382 | __func__, name, icount, supplies); |
| 383 | ret = -EINVAL; |
| 384 | goto free_microvolt; |
| 385 | } |
| 386 | |
| 387 | microamp = kmalloc_array(icount, sizeof(*microamp), GFP_KERNEL); |
| 388 | if (!microamp) { |
| 389 | ret = -EINVAL; |
| 390 | goto free_microvolt; |
| 391 | } |
| 392 | |
| 393 | ret = of_property_read_u32_array(opp->np, name, microamp, |
| 394 | icount); |
| 395 | if (ret) { |
| 396 | dev_err(dev, "%s: error parsing %s: %d\n", __func__, |
| 397 | name, ret); |
| 398 | ret = -EINVAL; |
| 399 | goto free_microamp; |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | for (i = 0, j = 0; i < supplies; i++) { |
| 404 | opp->supplies[i].u_volt = microvolt[j++]; |
| 405 | |
| 406 | if (vcount == supplies) { |
| 407 | opp->supplies[i].u_volt_min = opp->supplies[i].u_volt; |
| 408 | opp->supplies[i].u_volt_max = opp->supplies[i].u_volt; |
| 409 | } else { |
| 410 | opp->supplies[i].u_volt_min = microvolt[j++]; |
| 411 | opp->supplies[i].u_volt_max = microvolt[j++]; |
| 412 | } |
| 413 | |
| 414 | if (microamp) |
| 415 | opp->supplies[i].u_amp = microamp[i]; |
| 416 | } |
| 417 | |
| 418 | free_microamp: |
| 419 | kfree(microamp); |
| 420 | free_microvolt: |
| 421 | kfree(microvolt); |
| 422 | |
| 423 | return ret; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | /** |
| 427 | * dev_pm_opp_of_remove_table() - Free OPP table entries created from static DT |
| 428 | * entries |
| 429 | * @dev: device pointer used to lookup OPP table. |
| 430 | * |
| 431 | * Free OPPs created using static entries present in DT. |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 432 | */ |
| 433 | void dev_pm_opp_of_remove_table(struct device *dev) |
| 434 | { |
Viresh Kumar | 2a4eb73 | 2018-09-13 13:14:36 +0530 | [diff] [blame] | 435 | _dev_pm_opp_find_and_remove_table(dev); |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 436 | } |
| 437 | EXPORT_SYMBOL_GPL(dev_pm_opp_of_remove_table); |
| 438 | |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 439 | /** |
| 440 | * _opp_add_static_v2() - Allocate static OPPs (As per 'v2' DT bindings) |
Viresh Kumar | 8cd2f6e | 2017-01-02 14:41:01 +0530 | [diff] [blame] | 441 | * @opp_table: OPP table |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 442 | * @dev: device for which we do this operation |
| 443 | * @np: device node |
| 444 | * |
| 445 | * This function adds an opp definition to the opp table and returns status. The |
| 446 | * opp can be controlled using dev_pm_opp_enable/disable functions and may be |
| 447 | * removed by dev_pm_opp_remove. |
| 448 | * |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 449 | * Return: |
Dave Gerlach | deac870 | 2018-10-03 16:13:15 +0530 | [diff] [blame] | 450 | * Valid OPP pointer: |
| 451 | * On success |
| 452 | * NULL: |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 453 | * Duplicate OPPs (both freq and volt are same) and opp->available |
Dave Gerlach | deac870 | 2018-10-03 16:13:15 +0530 | [diff] [blame] | 454 | * OR if the OPP is not supported by hardware. |
| 455 | * ERR_PTR(-EEXIST): |
| 456 | * Freq are same and volt are different OR |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 457 | * Duplicate OPPs (both freq and volt are same) and !opp->available |
Dave Gerlach | deac870 | 2018-10-03 16:13:15 +0530 | [diff] [blame] | 458 | * ERR_PTR(-ENOMEM): |
| 459 | * Memory allocation failure |
| 460 | * ERR_PTR(-EINVAL): |
| 461 | * Failed parsing the OPP node |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 462 | */ |
Dave Gerlach | deac870 | 2018-10-03 16:13:15 +0530 | [diff] [blame] | 463 | static struct dev_pm_opp *_opp_add_static_v2(struct opp_table *opp_table, |
| 464 | struct device *dev, struct device_node *np) |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 465 | { |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 466 | struct dev_pm_opp *new_opp; |
Dan Carpenter | 6a89e01 | 2018-05-16 12:52:41 +0300 | [diff] [blame] | 467 | u64 rate = 0; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 468 | u32 val; |
| 469 | int ret; |
Viresh Kumar | a1e8c13 | 2018-04-06 14:35:45 +0530 | [diff] [blame] | 470 | bool rate_not_available = false; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 471 | |
Viresh Kumar | 8cd2f6e | 2017-01-02 14:41:01 +0530 | [diff] [blame] | 472 | new_opp = _opp_allocate(opp_table); |
| 473 | if (!new_opp) |
Dave Gerlach | deac870 | 2018-10-03 16:13:15 +0530 | [diff] [blame] | 474 | return ERR_PTR(-ENOMEM); |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 475 | |
| 476 | ret = of_property_read_u64(np, "opp-hz", &rate); |
| 477 | if (ret < 0) { |
Viresh Kumar | a1e8c13 | 2018-04-06 14:35:45 +0530 | [diff] [blame] | 478 | /* "opp-hz" is optional for devices like power domains. */ |
Viresh Kumar | 61d8e7c | 2018-06-13 16:00:11 +0530 | [diff] [blame] | 479 | if (!opp_table->is_genpd) { |
Viresh Kumar | a1e8c13 | 2018-04-06 14:35:45 +0530 | [diff] [blame] | 480 | dev_err(dev, "%s: opp-hz not found\n", __func__); |
| 481 | goto free_opp; |
| 482 | } |
| 483 | |
| 484 | rate_not_available = true; |
| 485 | } else { |
| 486 | /* |
| 487 | * Rate is defined as an unsigned long in clk API, and so |
| 488 | * casting explicitly to its type. Must be fixed once rate is 64 |
| 489 | * bit guaranteed in clk API. |
| 490 | */ |
| 491 | new_opp->rate = (unsigned long)rate; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | /* Check if the OPP supports hardware's hierarchy of versions or not */ |
| 495 | if (!_opp_is_supported(dev, opp_table, np)) { |
| 496 | dev_dbg(dev, "OPP not supported by hardware: %llu\n", rate); |
| 497 | goto free_opp; |
| 498 | } |
| 499 | |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 500 | new_opp->turbo = of_property_read_bool(np, "turbo-mode"); |
| 501 | |
| 502 | new_opp->np = np; |
| 503 | new_opp->dynamic = false; |
| 504 | new_opp->available = true; |
| 505 | |
| 506 | if (!of_property_read_u32(np, "clock-latency-ns", &val)) |
| 507 | new_opp->clock_latency_ns = val; |
| 508 | |
Viresh Kumar | 3ba9832 | 2016-11-18 15:47:46 +0530 | [diff] [blame] | 509 | new_opp->pstate = of_genpd_opp_to_performance_state(dev, np); |
| 510 | |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 511 | ret = opp_parse_supplies(new_opp, dev, opp_table); |
| 512 | if (ret) |
| 513 | goto free_opp; |
| 514 | |
Viresh Kumar | a1e8c13 | 2018-04-06 14:35:45 +0530 | [diff] [blame] | 515 | ret = _opp_add(dev, new_opp, opp_table, rate_not_available); |
Viresh Kumar | 7f8538e | 2017-01-02 14:40:55 +0530 | [diff] [blame] | 516 | if (ret) { |
| 517 | /* Don't return error for duplicate OPPs */ |
| 518 | if (ret == -EBUSY) |
| 519 | ret = 0; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 520 | goto free_opp; |
Viresh Kumar | 7f8538e | 2017-01-02 14:40:55 +0530 | [diff] [blame] | 521 | } |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 522 | |
| 523 | /* OPP to select on device suspend */ |
| 524 | if (of_property_read_bool(np, "opp-suspend")) { |
| 525 | if (opp_table->suspend_opp) { |
| 526 | dev_warn(dev, "%s: Multiple suspend OPPs found (%lu %lu)\n", |
| 527 | __func__, opp_table->suspend_opp->rate, |
| 528 | new_opp->rate); |
| 529 | } else { |
| 530 | new_opp->suspend = true; |
| 531 | opp_table->suspend_opp = new_opp; |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | if (new_opp->clock_latency_ns > opp_table->clock_latency_ns_max) |
| 536 | opp_table->clock_latency_ns_max = new_opp->clock_latency_ns; |
| 537 | |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 538 | pr_debug("%s: turbo:%d rate:%lu uv:%lu uvmin:%lu uvmax:%lu latency:%lu\n", |
Viresh Kumar | 0f0fe7e | 2016-12-01 16:28:17 +0530 | [diff] [blame] | 539 | __func__, new_opp->turbo, new_opp->rate, |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 540 | new_opp->supplies[0].u_volt, new_opp->supplies[0].u_volt_min, |
| 541 | new_opp->supplies[0].u_volt_max, new_opp->clock_latency_ns); |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 542 | |
| 543 | /* |
| 544 | * Notify the changes in the availability of the operable |
| 545 | * frequency/voltage list. |
| 546 | */ |
Viresh Kumar | 052c6f1 | 2017-01-23 10:11:49 +0530 | [diff] [blame] | 547 | blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp); |
Dave Gerlach | deac870 | 2018-10-03 16:13:15 +0530 | [diff] [blame] | 548 | return new_opp; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 549 | |
| 550 | free_opp: |
Viresh Kumar | 8cd2f6e | 2017-01-02 14:41:01 +0530 | [diff] [blame] | 551 | _opp_free(new_opp); |
| 552 | |
Dave Gerlach | deac870 | 2018-10-03 16:13:15 +0530 | [diff] [blame] | 553 | return ERR_PTR(ret); |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | /* Initializes OPP tables based on new bindings */ |
Viresh Kumar | 5ed4cec | 2018-09-12 11:21:17 +0530 | [diff] [blame] | 557 | static int _of_add_opp_table_v2(struct device *dev, struct opp_table *opp_table) |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 558 | { |
| 559 | struct device_node *np; |
Viresh Kumar | 283d55e | 2018-09-07 09:01:54 +0530 | [diff] [blame] | 560 | int ret, count = 0, pstate_count = 0; |
Viresh Kumar | 3ba9832 | 2016-11-18 15:47:46 +0530 | [diff] [blame] | 561 | struct dev_pm_opp *opp; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 562 | |
Viresh Kumar | 283d55e | 2018-09-07 09:01:54 +0530 | [diff] [blame] | 563 | /* OPP table is already initialized for the device */ |
| 564 | if (opp_table->parsed_static_opps) { |
| 565 | kref_get(&opp_table->list_kref); |
| 566 | return 0; |
| 567 | } |
| 568 | |
Viresh Kumar | d0e8ae6 | 2018-09-11 11:14:11 +0530 | [diff] [blame] | 569 | kref_init(&opp_table->list_kref); |
| 570 | |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 571 | /* We have opp-table node now, iterate over it and add OPPs */ |
Viresh Kumar | 5ed4cec | 2018-09-12 11:21:17 +0530 | [diff] [blame] | 572 | for_each_available_child_of_node(opp_table->np, np) { |
Dave Gerlach | deac870 | 2018-10-03 16:13:15 +0530 | [diff] [blame] | 573 | opp = _opp_add_static_v2(opp_table, dev, np); |
| 574 | if (IS_ERR(opp)) { |
| 575 | ret = PTR_ERR(opp); |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 576 | dev_err(dev, "%s: Failed to add OPP, %d\n", __func__, |
| 577 | ret); |
Tobias Jordan | 7978db3 | 2017-10-04 11:35:03 +0530 | [diff] [blame] | 578 | of_node_put(np); |
Viresh Kumar | cdd6ed9 | 2018-09-12 12:35:19 +0530 | [diff] [blame] | 579 | goto put_list_kref; |
Dave Gerlach | deac870 | 2018-10-03 16:13:15 +0530 | [diff] [blame] | 580 | } else if (opp) { |
| 581 | count++; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 582 | } |
| 583 | } |
| 584 | |
| 585 | /* There should be one of more OPP defined */ |
Viresh Kumar | 8cd2f6e | 2017-01-02 14:41:01 +0530 | [diff] [blame] | 586 | if (WARN_ON(!count)) { |
| 587 | ret = -ENOENT; |
Viresh Kumar | cdd6ed9 | 2018-09-12 12:35:19 +0530 | [diff] [blame] | 588 | goto put_list_kref; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 589 | } |
| 590 | |
Viresh Kumar | 3ba9832 | 2016-11-18 15:47:46 +0530 | [diff] [blame] | 591 | list_for_each_entry(opp, &opp_table->opp_list, node) |
| 592 | pstate_count += !!opp->pstate; |
| 593 | |
| 594 | /* Either all or none of the nodes shall have performance state set */ |
| 595 | if (pstate_count && pstate_count != count) { |
| 596 | dev_err(dev, "Not all nodes have performance state set (%d: %d)\n", |
| 597 | count, pstate_count); |
| 598 | ret = -ENOENT; |
Viresh Kumar | cdd6ed9 | 2018-09-12 12:35:19 +0530 | [diff] [blame] | 599 | goto put_list_kref; |
Viresh Kumar | 3ba9832 | 2016-11-18 15:47:46 +0530 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | if (pstate_count) |
| 603 | opp_table->genpd_performance_state = true; |
| 604 | |
Viresh Kumar | f06ed90 | 2018-06-14 12:04:43 +0530 | [diff] [blame] | 605 | opp_table->parsed_static_opps = true; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 606 | |
Viresh Kumar | cdd6ed9 | 2018-09-12 12:35:19 +0530 | [diff] [blame] | 607 | return 0; |
| 608 | |
| 609 | put_list_kref: |
| 610 | _put_opp_list_kref(opp_table); |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 611 | |
| 612 | return ret; |
| 613 | } |
| 614 | |
| 615 | /* Initializes OPP tables based on old-deprecated bindings */ |
Viresh Kumar | 5ed4cec | 2018-09-12 11:21:17 +0530 | [diff] [blame] | 616 | static int _of_add_opp_table_v1(struct device *dev, struct opp_table *opp_table) |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 617 | { |
| 618 | const struct property *prop; |
| 619 | const __be32 *val; |
Viresh Kumar | 8cd2f6e | 2017-01-02 14:41:01 +0530 | [diff] [blame] | 620 | int nr, ret = 0; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 621 | |
| 622 | prop = of_find_property(dev->of_node, "operating-points", NULL); |
| 623 | if (!prop) |
| 624 | return -ENODEV; |
| 625 | if (!prop->value) |
| 626 | return -ENODATA; |
| 627 | |
| 628 | /* |
| 629 | * Each OPP is a set of tuples consisting of frequency and |
| 630 | * voltage like <freq-kHz vol-uV>. |
| 631 | */ |
| 632 | nr = prop->length / sizeof(u32); |
| 633 | if (nr % 2) { |
| 634 | dev_err(dev, "%s: Invalid OPP table\n", __func__); |
| 635 | return -EINVAL; |
| 636 | } |
| 637 | |
Viresh Kumar | d0e8ae6 | 2018-09-11 11:14:11 +0530 | [diff] [blame] | 638 | kref_init(&opp_table->list_kref); |
| 639 | |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 640 | val = prop->value; |
| 641 | while (nr) { |
| 642 | unsigned long freq = be32_to_cpup(val++) * 1000; |
| 643 | unsigned long volt = be32_to_cpup(val++); |
| 644 | |
Viresh Kumar | 8cd2f6e | 2017-01-02 14:41:01 +0530 | [diff] [blame] | 645 | ret = _opp_add_v1(opp_table, dev, freq, volt, false); |
Viresh Kumar | 04a86a8 | 2017-01-02 14:40:58 +0530 | [diff] [blame] | 646 | if (ret) { |
| 647 | dev_err(dev, "%s: Failed to add OPP %ld (%d)\n", |
| 648 | __func__, freq, ret); |
Viresh Kumar | cdd6ed9 | 2018-09-12 12:35:19 +0530 | [diff] [blame] | 649 | _put_opp_list_kref(opp_table); |
Viresh Kumar | cdd6ed9 | 2018-09-12 12:35:19 +0530 | [diff] [blame] | 650 | return ret; |
Viresh Kumar | 04a86a8 | 2017-01-02 14:40:58 +0530 | [diff] [blame] | 651 | } |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 652 | nr -= 2; |
| 653 | } |
| 654 | |
Viresh Kumar | 8cd2f6e | 2017-01-02 14:41:01 +0530 | [diff] [blame] | 655 | return ret; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | /** |
| 659 | * dev_pm_opp_of_add_table() - Initialize opp table from device tree |
| 660 | * @dev: device pointer used to lookup OPP table. |
| 661 | * |
| 662 | * Register the initial OPP table with the OPP library for given device. |
| 663 | * |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 664 | * Return: |
| 665 | * 0 On success OR |
| 666 | * Duplicate OPPs (both freq and volt are same) and opp->available |
| 667 | * -EEXIST Freq are same and volt are different OR |
| 668 | * Duplicate OPPs (both freq and volt are same) and !opp->available |
| 669 | * -ENOMEM Memory allocation failure |
| 670 | * -ENODEV when 'operating-points' property is not found or is invalid data |
| 671 | * in device node. |
| 672 | * -ENODATA when empty 'operating-points' property is found |
| 673 | * -EINVAL when invalid entries are found in opp-v2 table |
| 674 | */ |
| 675 | int dev_pm_opp_of_add_table(struct device *dev) |
| 676 | { |
Viresh Kumar | 5ed4cec | 2018-09-12 11:21:17 +0530 | [diff] [blame] | 677 | struct opp_table *opp_table; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 678 | int ret; |
| 679 | |
Viresh Kumar | 5ed4cec | 2018-09-12 11:21:17 +0530 | [diff] [blame] | 680 | opp_table = dev_pm_opp_get_opp_table_indexed(dev, 0); |
| 681 | if (!opp_table) |
| 682 | return -ENOMEM; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 683 | |
Viresh Kumar | 5ed4cec | 2018-09-12 11:21:17 +0530 | [diff] [blame] | 684 | /* |
| 685 | * OPPs have two version of bindings now. Also try the old (v1) |
| 686 | * bindings for backward compatibility with older dtbs. |
| 687 | */ |
| 688 | if (opp_table->np) |
| 689 | ret = _of_add_opp_table_v2(dev, opp_table); |
| 690 | else |
| 691 | ret = _of_add_opp_table_v1(dev, opp_table); |
| 692 | |
| 693 | if (ret) |
| 694 | dev_pm_opp_put_opp_table(opp_table); |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 695 | |
| 696 | return ret; |
| 697 | } |
| 698 | EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table); |
| 699 | |
Viresh Kumar | fa9b274 | 2017-04-26 10:45:46 +0530 | [diff] [blame] | 700 | /** |
| 701 | * dev_pm_opp_of_add_table_indexed() - Initialize indexed opp table from device tree |
| 702 | * @dev: device pointer used to lookup OPP table. |
| 703 | * @index: Index number. |
| 704 | * |
| 705 | * Register the initial OPP table with the OPP library for given device only |
| 706 | * using the "operating-points-v2" property. |
| 707 | * |
| 708 | * Return: |
| 709 | * 0 On success OR |
| 710 | * Duplicate OPPs (both freq and volt are same) and opp->available |
| 711 | * -EEXIST Freq are same and volt are different OR |
| 712 | * Duplicate OPPs (both freq and volt are same) and !opp->available |
| 713 | * -ENOMEM Memory allocation failure |
| 714 | * -ENODEV when 'operating-points' property is not found or is invalid data |
| 715 | * in device node. |
| 716 | * -ENODATA when empty 'operating-points' property is found |
| 717 | * -EINVAL when invalid entries are found in opp-v2 table |
| 718 | */ |
| 719 | int dev_pm_opp_of_add_table_indexed(struct device *dev, int index) |
| 720 | { |
Viresh Kumar | 5ed4cec | 2018-09-12 11:21:17 +0530 | [diff] [blame] | 721 | struct opp_table *opp_table; |
Viresh Kumar | 8a352fd | 2018-05-30 15:19:38 +0530 | [diff] [blame] | 722 | int ret, count; |
Viresh Kumar | fa9b274 | 2017-04-26 10:45:46 +0530 | [diff] [blame] | 723 | |
Viresh Kumar | 5ed4cec | 2018-09-12 11:21:17 +0530 | [diff] [blame] | 724 | if (index) { |
Viresh Kumar | 8a352fd | 2018-05-30 15:19:38 +0530 | [diff] [blame] | 725 | /* |
| 726 | * If only one phandle is present, then the same OPP table |
| 727 | * applies for all index requests. |
| 728 | */ |
| 729 | count = of_count_phandle_with_args(dev->of_node, |
| 730 | "operating-points-v2", NULL); |
Viresh Kumar | 5ed4cec | 2018-09-12 11:21:17 +0530 | [diff] [blame] | 731 | if (count != 1) |
| 732 | return -ENODEV; |
Viresh Kumar | 8a352fd | 2018-05-30 15:19:38 +0530 | [diff] [blame] | 733 | |
Viresh Kumar | 5ed4cec | 2018-09-12 11:21:17 +0530 | [diff] [blame] | 734 | index = 0; |
Viresh Kumar | 8a352fd | 2018-05-30 15:19:38 +0530 | [diff] [blame] | 735 | } |
Viresh Kumar | fa9b274 | 2017-04-26 10:45:46 +0530 | [diff] [blame] | 736 | |
Viresh Kumar | 5ed4cec | 2018-09-12 11:21:17 +0530 | [diff] [blame] | 737 | opp_table = dev_pm_opp_get_opp_table_indexed(dev, index); |
| 738 | if (!opp_table) |
| 739 | return -ENOMEM; |
| 740 | |
| 741 | ret = _of_add_opp_table_v2(dev, opp_table); |
| 742 | if (ret) |
| 743 | dev_pm_opp_put_opp_table(opp_table); |
Viresh Kumar | fa9b274 | 2017-04-26 10:45:46 +0530 | [diff] [blame] | 744 | |
| 745 | return ret; |
| 746 | } |
| 747 | EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table_indexed); |
| 748 | |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 749 | /* CPU device specific helpers */ |
| 750 | |
| 751 | /** |
| 752 | * dev_pm_opp_of_cpumask_remove_table() - Removes OPP table for @cpumask |
| 753 | * @cpumask: cpumask for which OPP table needs to be removed |
| 754 | * |
| 755 | * This removes the OPP tables for CPUs present in the @cpumask. |
| 756 | * This should be used only to remove static entries created from DT. |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 757 | */ |
| 758 | void dev_pm_opp_of_cpumask_remove_table(const struct cpumask *cpumask) |
| 759 | { |
Viresh Kumar | 2a4eb73 | 2018-09-13 13:14:36 +0530 | [diff] [blame] | 760 | _dev_pm_opp_cpumask_remove_table(cpumask, -1); |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 761 | } |
| 762 | EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_remove_table); |
| 763 | |
| 764 | /** |
| 765 | * dev_pm_opp_of_cpumask_add_table() - Adds OPP table for @cpumask |
| 766 | * @cpumask: cpumask for which OPP table needs to be added. |
| 767 | * |
| 768 | * This adds the OPP tables for CPUs present in the @cpumask. |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 769 | */ |
| 770 | int dev_pm_opp_of_cpumask_add_table(const struct cpumask *cpumask) |
| 771 | { |
| 772 | struct device *cpu_dev; |
Viresh Kumar | 50b6b87 | 2018-10-03 15:12:06 +0530 | [diff] [blame] | 773 | int cpu, ret; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 774 | |
Viresh Kumar | 50b6b87 | 2018-10-03 15:12:06 +0530 | [diff] [blame] | 775 | if (WARN_ON(cpumask_empty(cpumask))) |
| 776 | return -ENODEV; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 777 | |
| 778 | for_each_cpu(cpu, cpumask) { |
| 779 | cpu_dev = get_cpu_device(cpu); |
| 780 | if (!cpu_dev) { |
| 781 | pr_err("%s: failed to get cpu%d device\n", __func__, |
| 782 | cpu); |
Viresh Kumar | 50b6b87 | 2018-10-03 15:12:06 +0530 | [diff] [blame] | 783 | ret = -ENODEV; |
| 784 | goto remove_table; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | ret = dev_pm_opp_of_add_table(cpu_dev); |
| 788 | if (ret) { |
Viresh Kumar | 5b60697 | 2017-07-12 09:21:49 +0530 | [diff] [blame] | 789 | /* |
| 790 | * OPP may get registered dynamically, don't print error |
| 791 | * message here. |
| 792 | */ |
| 793 | pr_debug("%s: couldn't find opp table for cpu:%d, %d\n", |
| 794 | __func__, cpu, ret); |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 795 | |
Viresh Kumar | 50b6b87 | 2018-10-03 15:12:06 +0530 | [diff] [blame] | 796 | goto remove_table; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 797 | } |
| 798 | } |
| 799 | |
Viresh Kumar | 50b6b87 | 2018-10-03 15:12:06 +0530 | [diff] [blame] | 800 | return 0; |
| 801 | |
| 802 | remove_table: |
| 803 | /* Free all other OPPs */ |
| 804 | _dev_pm_opp_cpumask_remove_table(cpumask, cpu); |
| 805 | |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 806 | return ret; |
| 807 | } |
| 808 | EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_add_table); |
| 809 | |
| 810 | /* |
| 811 | * Works only for OPP v2 bindings. |
| 812 | * |
| 813 | * Returns -ENOENT if operating-points-v2 bindings aren't supported. |
| 814 | */ |
| 815 | /** |
| 816 | * dev_pm_opp_of_get_sharing_cpus() - Get cpumask of CPUs sharing OPPs with |
| 817 | * @cpu_dev using operating-points-v2 |
| 818 | * bindings. |
| 819 | * |
| 820 | * @cpu_dev: CPU device for which we do this operation |
| 821 | * @cpumask: cpumask to update with information of sharing CPUs |
| 822 | * |
| 823 | * This updates the @cpumask with CPUs that are sharing OPPs with @cpu_dev. |
| 824 | * |
| 825 | * Returns -ENOENT if operating-points-v2 isn't present for @cpu_dev. |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 826 | */ |
| 827 | int dev_pm_opp_of_get_sharing_cpus(struct device *cpu_dev, |
| 828 | struct cpumask *cpumask) |
| 829 | { |
Waldemar Rymarkiewicz | 7627929 | 2017-07-27 12:01:17 +0200 | [diff] [blame] | 830 | struct device_node *np, *tmp_np, *cpu_np; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 831 | int cpu, ret = 0; |
| 832 | |
| 833 | /* Get OPP descriptor node */ |
Dave Gerlach | 0764c60 | 2017-02-03 11:29:26 -0600 | [diff] [blame] | 834 | np = dev_pm_opp_of_get_opp_desc_node(cpu_dev); |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 835 | if (!np) { |
Masahiro Yamada | 349aa92 | 2016-10-20 16:12:49 +0900 | [diff] [blame] | 836 | dev_dbg(cpu_dev, "%s: Couldn't find opp node.\n", __func__); |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 837 | return -ENOENT; |
| 838 | } |
| 839 | |
| 840 | cpumask_set_cpu(cpu_dev->id, cpumask); |
| 841 | |
| 842 | /* OPPs are shared ? */ |
| 843 | if (!of_property_read_bool(np, "opp-shared")) |
| 844 | goto put_cpu_node; |
| 845 | |
| 846 | for_each_possible_cpu(cpu) { |
| 847 | if (cpu == cpu_dev->id) |
| 848 | continue; |
| 849 | |
Sudeep Holla | 9867999 | 2017-10-12 11:32:23 +0100 | [diff] [blame] | 850 | cpu_np = of_cpu_device_node_get(cpu); |
Waldemar Rymarkiewicz | 7627929 | 2017-07-27 12:01:17 +0200 | [diff] [blame] | 851 | if (!cpu_np) { |
| 852 | dev_err(cpu_dev, "%s: failed to get cpu%d node\n", |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 853 | __func__, cpu); |
Waldemar Rymarkiewicz | 7627929 | 2017-07-27 12:01:17 +0200 | [diff] [blame] | 854 | ret = -ENOENT; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 855 | goto put_cpu_node; |
| 856 | } |
| 857 | |
| 858 | /* Get OPP descriptor node */ |
Viresh Kumar | fa9b274 | 2017-04-26 10:45:46 +0530 | [diff] [blame] | 859 | tmp_np = _opp_of_get_opp_desc_node(cpu_np, 0); |
Sudeep Holla | 9867999 | 2017-10-12 11:32:23 +0100 | [diff] [blame] | 860 | of_node_put(cpu_np); |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 861 | if (!tmp_np) { |
Waldemar Rymarkiewicz | 7627929 | 2017-07-27 12:01:17 +0200 | [diff] [blame] | 862 | pr_err("%pOF: Couldn't find opp node\n", cpu_np); |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 863 | ret = -ENOENT; |
| 864 | goto put_cpu_node; |
| 865 | } |
| 866 | |
| 867 | /* CPUs are sharing opp node */ |
| 868 | if (np == tmp_np) |
| 869 | cpumask_set_cpu(cpu, cpumask); |
| 870 | |
| 871 | of_node_put(tmp_np); |
| 872 | } |
| 873 | |
| 874 | put_cpu_node: |
| 875 | of_node_put(np); |
| 876 | return ret; |
| 877 | } |
| 878 | EXPORT_SYMBOL_GPL(dev_pm_opp_of_get_sharing_cpus); |
Viresh Kumar | a88bd2a | 2017-11-29 15:18:36 +0530 | [diff] [blame] | 879 | |
| 880 | /** |
| 881 | * of_dev_pm_opp_find_required_opp() - Search for required OPP. |
| 882 | * @dev: The device whose OPP node is referenced by the 'np' DT node. |
| 883 | * @np: Node that contains the "required-opps" property. |
| 884 | * |
| 885 | * Returns the OPP of the device 'dev', whose phandle is present in the "np" |
| 886 | * node. Although the "required-opps" property supports having multiple |
| 887 | * phandles, this helper routine only parses the very first phandle in the list. |
| 888 | * |
| 889 | * Return: Matching opp, else returns ERR_PTR in case of error and should be |
| 890 | * handled using IS_ERR. |
| 891 | * |
| 892 | * The callers are required to call dev_pm_opp_put() for the returned OPP after |
| 893 | * use. |
| 894 | */ |
| 895 | struct dev_pm_opp *of_dev_pm_opp_find_required_opp(struct device *dev, |
| 896 | struct device_node *np) |
| 897 | { |
| 898 | struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ENODEV); |
| 899 | struct device_node *required_np; |
| 900 | struct opp_table *opp_table; |
| 901 | |
| 902 | opp_table = _find_opp_table(dev); |
| 903 | if (IS_ERR(opp_table)) |
| 904 | return ERR_CAST(opp_table); |
| 905 | |
| 906 | required_np = of_parse_phandle(np, "required-opps", 0); |
| 907 | if (unlikely(!required_np)) { |
| 908 | dev_err(dev, "Unable to parse required-opps\n"); |
| 909 | goto put_opp_table; |
| 910 | } |
| 911 | |
| 912 | mutex_lock(&opp_table->lock); |
| 913 | |
| 914 | list_for_each_entry(temp_opp, &opp_table->opp_list, node) { |
| 915 | if (temp_opp->available && temp_opp->np == required_np) { |
| 916 | opp = temp_opp; |
| 917 | |
| 918 | /* Increment the reference count of OPP */ |
| 919 | dev_pm_opp_get(opp); |
| 920 | break; |
| 921 | } |
| 922 | } |
| 923 | |
| 924 | mutex_unlock(&opp_table->lock); |
| 925 | |
| 926 | of_node_put(required_np); |
| 927 | put_opp_table: |
| 928 | dev_pm_opp_put_opp_table(opp_table); |
| 929 | |
| 930 | return opp; |
| 931 | } |
| 932 | EXPORT_SYMBOL_GPL(of_dev_pm_opp_find_required_opp); |
Viresh Kumar | e2f4b5f | 2018-01-12 10:03:45 +0530 | [diff] [blame] | 933 | |
| 934 | /** |
| 935 | * dev_pm_opp_get_of_node() - Gets the DT node corresponding to an opp |
| 936 | * @opp: opp for which DT node has to be returned for |
| 937 | * |
| 938 | * Return: DT node corresponding to the opp, else 0 on success. |
| 939 | * |
| 940 | * The caller needs to put the node with of_node_put() after using it. |
| 941 | */ |
| 942 | struct device_node *dev_pm_opp_get_of_node(struct dev_pm_opp *opp) |
| 943 | { |
| 944 | if (IS_ERR_OR_NULL(opp)) { |
| 945 | pr_err("%s: Invalid parameters\n", __func__); |
| 946 | return NULL; |
| 947 | } |
| 948 | |
| 949 | return of_node_get(opp->np); |
| 950 | } |
| 951 | EXPORT_SYMBOL_GPL(dev_pm_opp_get_of_node); |