of.c 20 KB

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