of.c 20 KB

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