of.c 21 KB

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