of.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  14. #include <linux/cpu.h>
  15. #include <linux/errno.h>
  16. #include <linux/device.h>
  17. #include <linux/of_device.h>
  18. #include <linux/pm_domain.h>
  19. #include <linux/slab.h>
  20. #include <linux/export.h>
  21. #include "opp.h"
  22. static struct opp_table *_managed_opp(const struct device_node *np)
  23. {
  24. struct opp_table *opp_table, *managed_table = NULL;
  25. mutex_lock(&opp_table_lock);
  26. list_for_each_entry(opp_table, &opp_tables, node) {
  27. if (opp_table->np == np) {
  28. /*
  29. * Multiple devices can point to the same OPP table and
  30. * so will have same node-pointer, np.
  31. *
  32. * But the OPPs will be considered as shared only if the
  33. * OPP table contains a "opp-shared" property.
  34. */
  35. if (opp_table->shared_opp == OPP_TABLE_ACCESS_SHARED) {
  36. _get_opp_table_kref(opp_table);
  37. managed_table = opp_table;
  38. }
  39. break;
  40. }
  41. }
  42. mutex_unlock(&opp_table_lock);
  43. return managed_table;
  44. }
  45. void _of_init_opp_table(struct opp_table *opp_table, struct device *dev)
  46. {
  47. struct device_node *np;
  48. /*
  49. * Only required for backward compatibility with v1 bindings, but isn't
  50. * harmful for other cases. And so we do it unconditionally.
  51. */
  52. np = of_node_get(dev->of_node);
  53. if (np) {
  54. u32 val;
  55. if (!of_property_read_u32(np, "clock-latency", &val))
  56. opp_table->clock_latency_ns_max = val;
  57. of_property_read_u32(np, "voltage-tolerance",
  58. &opp_table->voltage_tolerance_v1);
  59. of_node_put(np);
  60. }
  61. }
  62. static bool _opp_is_supported(struct device *dev, struct opp_table *opp_table,
  63. struct device_node *np)
  64. {
  65. unsigned int count = opp_table->supported_hw_count;
  66. u32 version;
  67. int ret;
  68. if (!opp_table->supported_hw) {
  69. /*
  70. * In the case that no supported_hw has been set by the
  71. * platform but there is an opp-supported-hw value set for
  72. * an OPP then the OPP should not be enabled as there is
  73. * no way to see if the hardware supports it.
  74. */
  75. if (of_find_property(np, "opp-supported-hw", NULL))
  76. return false;
  77. else
  78. return true;
  79. }
  80. while (count--) {
  81. ret = of_property_read_u32_index(np, "opp-supported-hw", count,
  82. &version);
  83. if (ret) {
  84. dev_warn(dev, "%s: failed to read opp-supported-hw property at index %d: %d\n",
  85. __func__, count, ret);
  86. return false;
  87. }
  88. /* Both of these are bitwise masks of the versions */
  89. if (!(version & opp_table->supported_hw[count]))
  90. return false;
  91. }
  92. return true;
  93. }
  94. static int opp_parse_supplies(struct dev_pm_opp *opp, struct device *dev,
  95. struct opp_table *opp_table)
  96. {
  97. u32 *microvolt, *microamp = NULL;
  98. int supplies, vcount, icount, ret, i, j;
  99. struct property *prop = NULL;
  100. char name[NAME_MAX];
  101. supplies = opp_table->regulator_count ? opp_table->regulator_count : 1;
  102. /* Search for "opp-microvolt-<name>" */
  103. if (opp_table->prop_name) {
  104. snprintf(name, sizeof(name), "opp-microvolt-%s",
  105. opp_table->prop_name);
  106. prop = of_find_property(opp->np, name, NULL);
  107. }
  108. if (!prop) {
  109. /* Search for "opp-microvolt" */
  110. sprintf(name, "opp-microvolt");
  111. prop = of_find_property(opp->np, name, NULL);
  112. /* Missing property isn't a problem, but an invalid entry is */
  113. if (!prop) {
  114. if (!opp_table->regulator_count)
  115. return 0;
  116. dev_err(dev, "%s: opp-microvolt missing although OPP managing regulators\n",
  117. __func__);
  118. return -EINVAL;
  119. }
  120. }
  121. vcount = of_property_count_u32_elems(opp->np, name);
  122. if (vcount < 0) {
  123. dev_err(dev, "%s: Invalid %s property (%d)\n",
  124. __func__, name, vcount);
  125. return vcount;
  126. }
  127. /* There can be one or three elements per supply */
  128. if (vcount != supplies && vcount != supplies * 3) {
  129. dev_err(dev, "%s: Invalid number of elements in %s property (%d) with supplies (%d)\n",
  130. __func__, name, vcount, supplies);
  131. return -EINVAL;
  132. }
  133. microvolt = kmalloc_array(vcount, sizeof(*microvolt), GFP_KERNEL);
  134. if (!microvolt)
  135. return -ENOMEM;
  136. ret = of_property_read_u32_array(opp->np, name, microvolt, vcount);
  137. if (ret) {
  138. dev_err(dev, "%s: error parsing %s: %d\n", __func__, name, ret);
  139. ret = -EINVAL;
  140. goto free_microvolt;
  141. }
  142. /* Search for "opp-microamp-<name>" */
  143. prop = NULL;
  144. if (opp_table->prop_name) {
  145. snprintf(name, sizeof(name), "opp-microamp-%s",
  146. opp_table->prop_name);
  147. prop = of_find_property(opp->np, name, NULL);
  148. }
  149. if (!prop) {
  150. /* Search for "opp-microamp" */
  151. sprintf(name, "opp-microamp");
  152. prop = of_find_property(opp->np, name, NULL);
  153. }
  154. if (prop) {
  155. icount = of_property_count_u32_elems(opp->np, name);
  156. if (icount < 0) {
  157. dev_err(dev, "%s: Invalid %s property (%d)\n", __func__,
  158. name, icount);
  159. ret = icount;
  160. goto free_microvolt;
  161. }
  162. if (icount != supplies) {
  163. dev_err(dev, "%s: Invalid number of elements in %s property (%d) with supplies (%d)\n",
  164. __func__, name, icount, supplies);
  165. ret = -EINVAL;
  166. goto free_microvolt;
  167. }
  168. microamp = kmalloc_array(icount, sizeof(*microamp), GFP_KERNEL);
  169. if (!microamp) {
  170. ret = -EINVAL;
  171. goto free_microvolt;
  172. }
  173. ret = of_property_read_u32_array(opp->np, name, microamp,
  174. icount);
  175. if (ret) {
  176. dev_err(dev, "%s: error parsing %s: %d\n", __func__,
  177. name, ret);
  178. ret = -EINVAL;
  179. goto free_microamp;
  180. }
  181. }
  182. for (i = 0, j = 0; i < supplies; i++) {
  183. opp->supplies[i].u_volt = microvolt[j++];
  184. if (vcount == supplies) {
  185. opp->supplies[i].u_volt_min = opp->supplies[i].u_volt;
  186. opp->supplies[i].u_volt_max = opp->supplies[i].u_volt;
  187. } else {
  188. opp->supplies[i].u_volt_min = microvolt[j++];
  189. opp->supplies[i].u_volt_max = microvolt[j++];
  190. }
  191. if (microamp)
  192. opp->supplies[i].u_amp = microamp[i];
  193. }
  194. free_microamp:
  195. kfree(microamp);
  196. free_microvolt:
  197. kfree(microvolt);
  198. return ret;
  199. }
  200. /**
  201. * dev_pm_opp_of_remove_table() - Free OPP table entries created from static DT
  202. * entries
  203. * @dev: device pointer used to lookup OPP table.
  204. *
  205. * Free OPPs created using static entries present in DT.
  206. */
  207. void dev_pm_opp_of_remove_table(struct device *dev)
  208. {
  209. _dev_pm_opp_find_and_remove_table(dev, false);
  210. }
  211. EXPORT_SYMBOL_GPL(dev_pm_opp_of_remove_table);
  212. /* Returns opp descriptor node for a device node, caller must
  213. * do of_node_put() */
  214. static struct device_node *_opp_of_get_opp_desc_node(struct device_node *np,
  215. int index)
  216. {
  217. /* "operating-points-v2" can be an array for power domain providers */
  218. return of_parse_phandle(np, "operating-points-v2", index);
  219. }
  220. /* Returns opp descriptor node for a device, caller must do of_node_put() */
  221. struct device_node *dev_pm_opp_of_get_opp_desc_node(struct device *dev)
  222. {
  223. return _opp_of_get_opp_desc_node(dev->of_node, 0);
  224. }
  225. EXPORT_SYMBOL_GPL(dev_pm_opp_of_get_opp_desc_node);
  226. /**
  227. * _opp_add_static_v2() - Allocate static OPPs (As per 'v2' DT bindings)
  228. * @opp_table: OPP table
  229. * @dev: device for which we do this operation
  230. * @np: device node
  231. *
  232. * This function adds an opp definition to the opp table and returns status. The
  233. * opp can be controlled using dev_pm_opp_enable/disable functions and may be
  234. * removed by dev_pm_opp_remove.
  235. *
  236. * Return:
  237. * 0 On success OR
  238. * Duplicate OPPs (both freq and volt are same) and opp->available
  239. * -EEXIST Freq are same and volt are different OR
  240. * Duplicate OPPs (both freq and volt are same) and !opp->available
  241. * -ENOMEM Memory allocation failure
  242. * -EINVAL Failed parsing the OPP node
  243. */
  244. static int _opp_add_static_v2(struct opp_table *opp_table, struct device *dev,
  245. struct device_node *np)
  246. {
  247. struct dev_pm_opp *new_opp;
  248. u64 rate = 0;
  249. u32 val;
  250. int ret;
  251. bool rate_not_available = false;
  252. new_opp = _opp_allocate(opp_table);
  253. if (!new_opp)
  254. return -ENOMEM;
  255. ret = of_property_read_u64(np, "opp-hz", &rate);
  256. if (ret < 0) {
  257. /* "opp-hz" is optional for devices like power domains. */
  258. if (!of_find_property(dev->of_node, "#power-domain-cells",
  259. NULL)) {
  260. dev_err(dev, "%s: opp-hz not found\n", __func__);
  261. goto free_opp;
  262. }
  263. rate_not_available = true;
  264. } else {
  265. /*
  266. * Rate is defined as an unsigned long in clk API, and so
  267. * casting explicitly to its type. Must be fixed once rate is 64
  268. * bit guaranteed in clk API.
  269. */
  270. new_opp->rate = (unsigned long)rate;
  271. }
  272. /* Check if the OPP supports hardware's hierarchy of versions or not */
  273. if (!_opp_is_supported(dev, opp_table, np)) {
  274. dev_dbg(dev, "OPP not supported by hardware: %llu\n", rate);
  275. goto free_opp;
  276. }
  277. new_opp->turbo = of_property_read_bool(np, "turbo-mode");
  278. new_opp->np = np;
  279. new_opp->dynamic = false;
  280. new_opp->available = true;
  281. if (!of_property_read_u32(np, "clock-latency-ns", &val))
  282. new_opp->clock_latency_ns = val;
  283. new_opp->pstate = of_genpd_opp_to_performance_state(dev, np);
  284. ret = opp_parse_supplies(new_opp, dev, opp_table);
  285. if (ret)
  286. goto free_opp;
  287. ret = _opp_add(dev, new_opp, opp_table, rate_not_available);
  288. if (ret) {
  289. /* Don't return error for duplicate OPPs */
  290. if (ret == -EBUSY)
  291. ret = 0;
  292. goto free_opp;
  293. }
  294. /* OPP to select on device suspend */
  295. if (of_property_read_bool(np, "opp-suspend")) {
  296. if (opp_table->suspend_opp) {
  297. dev_warn(dev, "%s: Multiple suspend OPPs found (%lu %lu)\n",
  298. __func__, opp_table->suspend_opp->rate,
  299. new_opp->rate);
  300. } else {
  301. new_opp->suspend = true;
  302. opp_table->suspend_opp = new_opp;
  303. }
  304. }
  305. if (new_opp->clock_latency_ns > opp_table->clock_latency_ns_max)
  306. opp_table->clock_latency_ns_max = new_opp->clock_latency_ns;
  307. pr_debug("%s: turbo:%d rate:%lu uv:%lu uvmin:%lu uvmax:%lu latency:%lu\n",
  308. __func__, new_opp->turbo, new_opp->rate,
  309. new_opp->supplies[0].u_volt, new_opp->supplies[0].u_volt_min,
  310. new_opp->supplies[0].u_volt_max, new_opp->clock_latency_ns);
  311. /*
  312. * Notify the changes in the availability of the operable
  313. * frequency/voltage list.
  314. */
  315. blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp);
  316. return 0;
  317. free_opp:
  318. _opp_free(new_opp);
  319. return ret;
  320. }
  321. /* Initializes OPP tables based on new bindings */
  322. static int _of_add_opp_table_v2(struct device *dev, struct device_node *opp_np)
  323. {
  324. struct device_node *np;
  325. struct opp_table *opp_table;
  326. int ret = 0, count = 0, pstate_count = 0;
  327. struct dev_pm_opp *opp;
  328. opp_table = _managed_opp(opp_np);
  329. if (opp_table) {
  330. /* OPPs are already managed */
  331. if (!_add_opp_dev(dev, opp_table))
  332. ret = -ENOMEM;
  333. goto put_opp_table;
  334. }
  335. opp_table = dev_pm_opp_get_opp_table(dev);
  336. if (!opp_table)
  337. return -ENOMEM;
  338. /* We have opp-table node now, iterate over it and add OPPs */
  339. for_each_available_child_of_node(opp_np, np) {
  340. count++;
  341. ret = _opp_add_static_v2(opp_table, dev, np);
  342. if (ret) {
  343. dev_err(dev, "%s: Failed to add OPP, %d\n", __func__,
  344. ret);
  345. _dev_pm_opp_remove_table(opp_table, dev, false);
  346. of_node_put(np);
  347. goto put_opp_table;
  348. }
  349. }
  350. /* There should be one of more OPP defined */
  351. if (WARN_ON(!count)) {
  352. ret = -ENOENT;
  353. goto put_opp_table;
  354. }
  355. list_for_each_entry(opp, &opp_table->opp_list, node)
  356. pstate_count += !!opp->pstate;
  357. /* Either all or none of the nodes shall have performance state set */
  358. if (pstate_count && pstate_count != count) {
  359. dev_err(dev, "Not all nodes have performance state set (%d: %d)\n",
  360. count, pstate_count);
  361. ret = -ENOENT;
  362. goto put_opp_table;
  363. }
  364. if (pstate_count)
  365. opp_table->genpd_performance_state = true;
  366. opp_table->np = opp_np;
  367. if (of_property_read_bool(opp_np, "opp-shared"))
  368. opp_table->shared_opp = OPP_TABLE_ACCESS_SHARED;
  369. else
  370. opp_table->shared_opp = OPP_TABLE_ACCESS_EXCLUSIVE;
  371. put_opp_table:
  372. dev_pm_opp_put_opp_table(opp_table);
  373. return ret;
  374. }
  375. /* Initializes OPP tables based on old-deprecated bindings */
  376. static int _of_add_opp_table_v1(struct device *dev)
  377. {
  378. struct opp_table *opp_table;
  379. const struct property *prop;
  380. const __be32 *val;
  381. int nr, ret = 0;
  382. prop = of_find_property(dev->of_node, "operating-points", NULL);
  383. if (!prop)
  384. return -ENODEV;
  385. if (!prop->value)
  386. return -ENODATA;
  387. /*
  388. * Each OPP is a set of tuples consisting of frequency and
  389. * voltage like <freq-kHz vol-uV>.
  390. */
  391. nr = prop->length / sizeof(u32);
  392. if (nr % 2) {
  393. dev_err(dev, "%s: Invalid OPP table\n", __func__);
  394. return -EINVAL;
  395. }
  396. opp_table = dev_pm_opp_get_opp_table(dev);
  397. if (!opp_table)
  398. return -ENOMEM;
  399. val = prop->value;
  400. while (nr) {
  401. unsigned long freq = be32_to_cpup(val++) * 1000;
  402. unsigned long volt = be32_to_cpup(val++);
  403. ret = _opp_add_v1(opp_table, dev, freq, volt, false);
  404. if (ret) {
  405. dev_err(dev, "%s: Failed to add OPP %ld (%d)\n",
  406. __func__, freq, ret);
  407. _dev_pm_opp_remove_table(opp_table, dev, false);
  408. break;
  409. }
  410. nr -= 2;
  411. }
  412. dev_pm_opp_put_opp_table(opp_table);
  413. return ret;
  414. }
  415. /**
  416. * dev_pm_opp_of_add_table() - Initialize opp table from device tree
  417. * @dev: device pointer used to lookup OPP table.
  418. *
  419. * Register the initial OPP table with the OPP library for given device.
  420. *
  421. * Return:
  422. * 0 On success OR
  423. * Duplicate OPPs (both freq and volt are same) and opp->available
  424. * -EEXIST Freq are same and volt are different OR
  425. * Duplicate OPPs (both freq and volt are same) and !opp->available
  426. * -ENOMEM Memory allocation failure
  427. * -ENODEV when 'operating-points' property is not found or is invalid data
  428. * in device node.
  429. * -ENODATA when empty 'operating-points' property is found
  430. * -EINVAL when invalid entries are found in opp-v2 table
  431. */
  432. int dev_pm_opp_of_add_table(struct device *dev)
  433. {
  434. struct device_node *opp_np;
  435. int ret;
  436. /*
  437. * OPPs have two version of bindings now. The older one is deprecated,
  438. * try for the new binding first.
  439. */
  440. opp_np = dev_pm_opp_of_get_opp_desc_node(dev);
  441. if (!opp_np) {
  442. /*
  443. * Try old-deprecated bindings for backward compatibility with
  444. * older dtbs.
  445. */
  446. return _of_add_opp_table_v1(dev);
  447. }
  448. ret = _of_add_opp_table_v2(dev, opp_np);
  449. of_node_put(opp_np);
  450. return ret;
  451. }
  452. EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table);
  453. /**
  454. * dev_pm_opp_of_add_table_indexed() - Initialize indexed opp table from device tree
  455. * @dev: device pointer used to lookup OPP table.
  456. * @index: Index number.
  457. *
  458. * Register the initial OPP table with the OPP library for given device only
  459. * using the "operating-points-v2" property.
  460. *
  461. * Return:
  462. * 0 On success OR
  463. * Duplicate OPPs (both freq and volt are same) and opp->available
  464. * -EEXIST Freq are same and volt are different OR
  465. * Duplicate OPPs (both freq and volt are same) and !opp->available
  466. * -ENOMEM Memory allocation failure
  467. * -ENODEV when 'operating-points' property is not found or is invalid data
  468. * in device node.
  469. * -ENODATA when empty 'operating-points' property is found
  470. * -EINVAL when invalid entries are found in opp-v2 table
  471. */
  472. int dev_pm_opp_of_add_table_indexed(struct device *dev, int index)
  473. {
  474. struct device_node *opp_np;
  475. int ret, count;
  476. again:
  477. opp_np = _opp_of_get_opp_desc_node(dev->of_node, index);
  478. if (!opp_np) {
  479. /*
  480. * If only one phandle is present, then the same OPP table
  481. * applies for all index requests.
  482. */
  483. count = of_count_phandle_with_args(dev->of_node,
  484. "operating-points-v2", NULL);
  485. if (count == 1 && index) {
  486. index = 0;
  487. goto again;
  488. }
  489. return -ENODEV;
  490. }
  491. ret = _of_add_opp_table_v2(dev, opp_np);
  492. of_node_put(opp_np);
  493. return ret;
  494. }
  495. EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table_indexed);
  496. /* CPU device specific helpers */
  497. /**
  498. * dev_pm_opp_of_cpumask_remove_table() - Removes OPP table for @cpumask
  499. * @cpumask: cpumask for which OPP table needs to be removed
  500. *
  501. * This removes the OPP tables for CPUs present in the @cpumask.
  502. * This should be used only to remove static entries created from DT.
  503. */
  504. void dev_pm_opp_of_cpumask_remove_table(const struct cpumask *cpumask)
  505. {
  506. _dev_pm_opp_cpumask_remove_table(cpumask, true);
  507. }
  508. EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_remove_table);
  509. /**
  510. * dev_pm_opp_of_cpumask_add_table() - Adds OPP table for @cpumask
  511. * @cpumask: cpumask for which OPP table needs to be added.
  512. *
  513. * This adds the OPP tables for CPUs present in the @cpumask.
  514. */
  515. int dev_pm_opp_of_cpumask_add_table(const struct cpumask *cpumask)
  516. {
  517. struct device *cpu_dev;
  518. int cpu, ret = 0;
  519. WARN_ON(cpumask_empty(cpumask));
  520. for_each_cpu(cpu, cpumask) {
  521. cpu_dev = get_cpu_device(cpu);
  522. if (!cpu_dev) {
  523. pr_err("%s: failed to get cpu%d device\n", __func__,
  524. cpu);
  525. continue;
  526. }
  527. ret = dev_pm_opp_of_add_table(cpu_dev);
  528. if (ret) {
  529. /*
  530. * OPP may get registered dynamically, don't print error
  531. * message here.
  532. */
  533. pr_debug("%s: couldn't find opp table for cpu:%d, %d\n",
  534. __func__, cpu, ret);
  535. /* Free all other OPPs */
  536. dev_pm_opp_of_cpumask_remove_table(cpumask);
  537. break;
  538. }
  539. }
  540. return ret;
  541. }
  542. EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_add_table);
  543. /*
  544. * Works only for OPP v2 bindings.
  545. *
  546. * Returns -ENOENT if operating-points-v2 bindings aren't supported.
  547. */
  548. /**
  549. * dev_pm_opp_of_get_sharing_cpus() - Get cpumask of CPUs sharing OPPs with
  550. * @cpu_dev using operating-points-v2
  551. * bindings.
  552. *
  553. * @cpu_dev: CPU device for which we do this operation
  554. * @cpumask: cpumask to update with information of sharing CPUs
  555. *
  556. * This updates the @cpumask with CPUs that are sharing OPPs with @cpu_dev.
  557. *
  558. * Returns -ENOENT if operating-points-v2 isn't present for @cpu_dev.
  559. */
  560. int dev_pm_opp_of_get_sharing_cpus(struct device *cpu_dev,
  561. struct cpumask *cpumask)
  562. {
  563. struct device_node *np, *tmp_np, *cpu_np;
  564. int cpu, ret = 0;
  565. /* Get OPP descriptor node */
  566. np = dev_pm_opp_of_get_opp_desc_node(cpu_dev);
  567. if (!np) {
  568. dev_dbg(cpu_dev, "%s: Couldn't find opp node.\n", __func__);
  569. return -ENOENT;
  570. }
  571. cpumask_set_cpu(cpu_dev->id, cpumask);
  572. /* OPPs are shared ? */
  573. if (!of_property_read_bool(np, "opp-shared"))
  574. goto put_cpu_node;
  575. for_each_possible_cpu(cpu) {
  576. if (cpu == cpu_dev->id)
  577. continue;
  578. cpu_np = of_cpu_device_node_get(cpu);
  579. if (!cpu_np) {
  580. dev_err(cpu_dev, "%s: failed to get cpu%d node\n",
  581. __func__, cpu);
  582. ret = -ENOENT;
  583. goto put_cpu_node;
  584. }
  585. /* Get OPP descriptor node */
  586. tmp_np = _opp_of_get_opp_desc_node(cpu_np, 0);
  587. of_node_put(cpu_np);
  588. if (!tmp_np) {
  589. pr_err("%pOF: Couldn't find opp node\n", cpu_np);
  590. ret = -ENOENT;
  591. goto put_cpu_node;
  592. }
  593. /* CPUs are sharing opp node */
  594. if (np == tmp_np)
  595. cpumask_set_cpu(cpu, cpumask);
  596. of_node_put(tmp_np);
  597. }
  598. put_cpu_node:
  599. of_node_put(np);
  600. return ret;
  601. }
  602. EXPORT_SYMBOL_GPL(dev_pm_opp_of_get_sharing_cpus);
  603. /**
  604. * of_dev_pm_opp_find_required_opp() - Search for required OPP.
  605. * @dev: The device whose OPP node is referenced by the 'np' DT node.
  606. * @np: Node that contains the "required-opps" property.
  607. *
  608. * Returns the OPP of the device 'dev', whose phandle is present in the "np"
  609. * node. Although the "required-opps" property supports having multiple
  610. * phandles, this helper routine only parses the very first phandle in the list.
  611. *
  612. * Return: Matching opp, else returns ERR_PTR in case of error and should be
  613. * handled using IS_ERR.
  614. *
  615. * The callers are required to call dev_pm_opp_put() for the returned OPP after
  616. * use.
  617. */
  618. struct dev_pm_opp *of_dev_pm_opp_find_required_opp(struct device *dev,
  619. struct device_node *np)
  620. {
  621. struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ENODEV);
  622. struct device_node *required_np;
  623. struct opp_table *opp_table;
  624. opp_table = _find_opp_table(dev);
  625. if (IS_ERR(opp_table))
  626. return ERR_CAST(opp_table);
  627. required_np = of_parse_phandle(np, "required-opps", 0);
  628. if (unlikely(!required_np)) {
  629. dev_err(dev, "Unable to parse required-opps\n");
  630. goto put_opp_table;
  631. }
  632. mutex_lock(&opp_table->lock);
  633. list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
  634. if (temp_opp->available && temp_opp->np == required_np) {
  635. opp = temp_opp;
  636. /* Increment the reference count of OPP */
  637. dev_pm_opp_get(opp);
  638. break;
  639. }
  640. }
  641. mutex_unlock(&opp_table->lock);
  642. of_node_put(required_np);
  643. put_opp_table:
  644. dev_pm_opp_put_opp_table(opp_table);
  645. return opp;
  646. }
  647. EXPORT_SYMBOL_GPL(of_dev_pm_opp_find_required_opp);
  648. /**
  649. * dev_pm_opp_get_of_node() - Gets the DT node corresponding to an opp
  650. * @opp: opp for which DT node has to be returned for
  651. *
  652. * Return: DT node corresponding to the opp, else 0 on success.
  653. *
  654. * The caller needs to put the node with of_node_put() after using it.
  655. */
  656. struct device_node *dev_pm_opp_get_of_node(struct dev_pm_opp *opp)
  657. {
  658. if (IS_ERR_OR_NULL(opp)) {
  659. pr_err("%s: Invalid parameters\n", __func__);
  660. return NULL;
  661. }
  662. return of_node_get(opp->np);
  663. }
  664. EXPORT_SYMBOL_GPL(dev_pm_opp_get_of_node);