of.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Generic OPP OF helpers
  4. *
  5. * Copyright (C) 2009-2010 Texas Instruments Incorporated.
  6. * Nishanth Menon
  7. * Romit Dasgupta
  8. * Kevin Hilman
  9. */
  10. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #include <linux/cpu.h>
  12. #include <linux/errno.h>
  13. #include <linux/device.h>
  14. #include <linux/of.h>
  15. #include <linux/pm_domain.h>
  16. #include <linux/slab.h>
  17. #include <linux/export.h>
  18. #include <linux/energy_model.h>
  19. #include "opp.h"
  20. /* OPP tables with uninitialized required OPPs, protected by opp_table_lock */
  21. static LIST_HEAD(lazy_opp_tables);
  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 __free(device_node) =
  42. _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. managed_table = dev_pm_opp_get_opp_table_ref(opp_table);
  56. break;
  57. }
  58. }
  59. return managed_table;
  60. }
  61. /* The caller must call dev_pm_opp_put() after the OPP is used */
  62. static struct dev_pm_opp *_find_opp_of_np(struct opp_table *opp_table,
  63. struct device_node *opp_np)
  64. {
  65. struct dev_pm_opp *opp;
  66. guard(mutex)(&opp_table->lock);
  67. list_for_each_entry(opp, &opp_table->opp_list, node) {
  68. if (opp->np == opp_np)
  69. return dev_pm_opp_get(opp);
  70. }
  71. return NULL;
  72. }
  73. static struct device_node *of_parse_required_opp(struct device_node *np,
  74. int index)
  75. {
  76. return of_parse_phandle(np, "required-opps", index);
  77. }
  78. /* The caller must call dev_pm_opp_put_opp_table() after the table is used */
  79. static struct opp_table *_find_table_of_opp_np(struct device_node *opp_np)
  80. {
  81. struct opp_table *opp_table;
  82. struct device_node *opp_table_np __free(device_node) =
  83. of_get_parent(opp_np);
  84. if (!opp_table_np)
  85. return ERR_PTR(-ENODEV);
  86. guard(mutex)(&opp_table_lock);
  87. list_for_each_entry(opp_table, &opp_tables, node) {
  88. if (opp_table_np == opp_table->np)
  89. return dev_pm_opp_get_opp_table_ref(opp_table);
  90. }
  91. return ERR_PTR(-ENODEV);
  92. }
  93. /* Free resources previously acquired by _opp_table_alloc_required_tables() */
  94. static void _opp_table_free_required_tables(struct opp_table *opp_table)
  95. {
  96. struct opp_table **required_opp_tables = opp_table->required_opp_tables;
  97. int i;
  98. if (!required_opp_tables)
  99. return;
  100. for (i = 0; i < opp_table->required_opp_count; i++) {
  101. if (IS_ERR_OR_NULL(required_opp_tables[i]))
  102. continue;
  103. dev_pm_opp_put_opp_table(required_opp_tables[i]);
  104. }
  105. kfree(required_opp_tables);
  106. opp_table->required_opp_count = 0;
  107. opp_table->required_opp_tables = NULL;
  108. guard(mutex)(&opp_table_lock);
  109. list_del(&opp_table->lazy);
  110. }
  111. /*
  112. * Populate all devices and opp tables which are part of "required-opps" list.
  113. * Checking only the first OPP node should be enough.
  114. */
  115. static void _opp_table_alloc_required_tables(struct opp_table *opp_table,
  116. struct device *dev,
  117. struct device_node *opp_np)
  118. {
  119. struct opp_table **required_opp_tables;
  120. bool lazy = false;
  121. int count, i, size;
  122. /* Traversing the first OPP node is all we need */
  123. struct device_node *np __free(device_node) =
  124. of_get_next_available_child(opp_np, NULL);
  125. if (!np) {
  126. dev_warn(dev, "Empty OPP table\n");
  127. return;
  128. }
  129. count = of_count_phandle_with_args(np, "required-opps", NULL);
  130. if (count <= 0)
  131. return;
  132. size = sizeof(*required_opp_tables) + sizeof(*opp_table->required_devs);
  133. required_opp_tables = kcalloc(count, size, GFP_KERNEL);
  134. if (!required_opp_tables)
  135. return;
  136. opp_table->required_opp_tables = required_opp_tables;
  137. opp_table->required_devs = (void *)(required_opp_tables + count);
  138. opp_table->required_opp_count = count;
  139. for (i = 0; i < count; i++) {
  140. struct device_node *required_np __free(device_node) =
  141. of_parse_required_opp(np, i);
  142. if (!required_np) {
  143. _opp_table_free_required_tables(opp_table);
  144. return;
  145. }
  146. required_opp_tables[i] = _find_table_of_opp_np(required_np);
  147. if (IS_ERR(required_opp_tables[i]))
  148. lazy = true;
  149. }
  150. /* Let's do the linking later on */
  151. if (lazy) {
  152. /*
  153. * The OPP table is not held while allocating the table, take it
  154. * now to avoid corruption to the lazy_opp_tables list.
  155. */
  156. guard(mutex)(&opp_table_lock);
  157. list_add(&opp_table->lazy, &lazy_opp_tables);
  158. }
  159. }
  160. void _of_init_opp_table(struct opp_table *opp_table, struct device *dev,
  161. int index)
  162. {
  163. struct device_node *opp_np;
  164. u32 val;
  165. /*
  166. * Only required for backward compatibility with v1 bindings, but isn't
  167. * harmful for other cases. And so we do it unconditionally.
  168. */
  169. struct device_node *np __free(device_node) = of_node_get(dev->of_node);
  170. if (!np)
  171. return;
  172. if (!of_property_read_u32(np, "clock-latency", &val))
  173. opp_table->clock_latency_ns_max = val;
  174. of_property_read_u32(np, "voltage-tolerance",
  175. &opp_table->voltage_tolerance_v1);
  176. if (of_property_present(np, "#power-domain-cells"))
  177. opp_table->is_genpd = true;
  178. /* Get OPP table node */
  179. opp_np = _opp_of_get_opp_desc_node(np, index);
  180. if (!opp_np)
  181. return;
  182. if (of_property_read_bool(opp_np, "opp-shared"))
  183. opp_table->shared_opp = OPP_TABLE_ACCESS_SHARED;
  184. else
  185. opp_table->shared_opp = OPP_TABLE_ACCESS_EXCLUSIVE;
  186. opp_table->np = opp_np;
  187. _opp_table_alloc_required_tables(opp_table, dev, opp_np);
  188. }
  189. void _of_clear_opp_table(struct opp_table *opp_table)
  190. {
  191. _opp_table_free_required_tables(opp_table);
  192. of_node_put(opp_table->np);
  193. }
  194. /*
  195. * Release all resources previously acquired with a call to
  196. * _of_opp_alloc_required_opps().
  197. */
  198. static void _of_opp_free_required_opps(struct opp_table *opp_table,
  199. struct dev_pm_opp *opp)
  200. {
  201. struct dev_pm_opp **required_opps = opp->required_opps;
  202. int i;
  203. if (!required_opps)
  204. return;
  205. for (i = 0; i < opp_table->required_opp_count; i++) {
  206. if (!required_opps[i])
  207. continue;
  208. /* Put the reference back */
  209. dev_pm_opp_put(required_opps[i]);
  210. }
  211. opp->required_opps = NULL;
  212. kfree(required_opps);
  213. }
  214. void _of_clear_opp(struct opp_table *opp_table, struct dev_pm_opp *opp)
  215. {
  216. _of_opp_free_required_opps(opp_table, opp);
  217. of_node_put(opp->np);
  218. }
  219. static int _link_required_opps(struct dev_pm_opp *opp,
  220. struct opp_table *required_table, int index)
  221. {
  222. struct device_node *np __free(device_node) =
  223. of_parse_required_opp(opp->np, index);
  224. if (unlikely(!np))
  225. return -ENODEV;
  226. opp->required_opps[index] = _find_opp_of_np(required_table, np);
  227. if (!opp->required_opps[index]) {
  228. pr_err("%s: Unable to find required OPP node: %pOF (%d)\n",
  229. __func__, opp->np, index);
  230. return -ENODEV;
  231. }
  232. return 0;
  233. }
  234. /* Populate all required OPPs which are part of "required-opps" list */
  235. static int _of_opp_alloc_required_opps(struct opp_table *opp_table,
  236. struct dev_pm_opp *opp)
  237. {
  238. struct opp_table *required_table;
  239. int i, ret, count = opp_table->required_opp_count;
  240. if (!count)
  241. return 0;
  242. opp->required_opps = kzalloc_objs(*opp->required_opps, count);
  243. if (!opp->required_opps)
  244. return -ENOMEM;
  245. for (i = 0; i < count; i++) {
  246. required_table = opp_table->required_opp_tables[i];
  247. /* Required table not added yet, we will link later */
  248. if (IS_ERR_OR_NULL(required_table))
  249. continue;
  250. ret = _link_required_opps(opp, required_table, i);
  251. if (ret)
  252. goto free_required_opps;
  253. }
  254. return 0;
  255. free_required_opps:
  256. _of_opp_free_required_opps(opp_table, opp);
  257. return ret;
  258. }
  259. /* Link required OPPs for an individual OPP */
  260. static int lazy_link_required_opps(struct opp_table *opp_table,
  261. struct opp_table *new_table, int index)
  262. {
  263. struct dev_pm_opp *opp;
  264. int ret;
  265. list_for_each_entry(opp, &opp_table->opp_list, node) {
  266. ret = _link_required_opps(opp, new_table, index);
  267. if (ret)
  268. return ret;
  269. }
  270. return 0;
  271. }
  272. /* Link required OPPs for all OPPs of the newly added OPP table */
  273. static void lazy_link_required_opp_table(struct opp_table *new_table)
  274. {
  275. struct opp_table *opp_table, *temp, **required_opp_tables;
  276. struct dev_pm_opp *opp;
  277. int i, ret;
  278. guard(mutex)(&opp_table_lock);
  279. list_for_each_entry_safe(opp_table, temp, &lazy_opp_tables, lazy) {
  280. bool lazy = false;
  281. /* opp_np can't be invalid here */
  282. struct device_node *opp_np __free(device_node) =
  283. of_get_next_available_child(opp_table->np, NULL);
  284. for (i = 0; i < opp_table->required_opp_count; i++) {
  285. required_opp_tables = opp_table->required_opp_tables;
  286. /* Required opp-table is already parsed */
  287. if (!IS_ERR(required_opp_tables[i]))
  288. continue;
  289. /* required_np can't be invalid here */
  290. struct device_node *required_np __free(device_node) =
  291. of_parse_required_opp(opp_np, i);
  292. struct device_node *required_table_np __free(device_node) =
  293. of_get_parent(required_np);
  294. /*
  295. * Newly added table isn't the required opp-table for
  296. * opp_table.
  297. */
  298. if (required_table_np != new_table->np) {
  299. lazy = true;
  300. continue;
  301. }
  302. required_opp_tables[i] = dev_pm_opp_get_opp_table_ref(new_table);
  303. /* Link OPPs now */
  304. ret = lazy_link_required_opps(opp_table, new_table, i);
  305. if (ret) {
  306. /* The OPPs will be marked unusable */
  307. lazy = false;
  308. break;
  309. }
  310. }
  311. /* All required opp-tables found, remove from lazy list */
  312. if (!lazy) {
  313. list_del_init(&opp_table->lazy);
  314. list_for_each_entry(opp, &opp_table->opp_list, node)
  315. _required_opps_available(opp, opp_table->required_opp_count);
  316. }
  317. }
  318. }
  319. static int _bandwidth_supported(struct device *dev, struct opp_table *opp_table)
  320. {
  321. struct device_node *opp_np __free(device_node) = NULL;
  322. struct property *prop;
  323. if (!opp_table) {
  324. struct device_node *np __free(device_node) =
  325. of_node_get(dev->of_node);
  326. if (!np)
  327. return -ENODEV;
  328. opp_np = _opp_of_get_opp_desc_node(np, 0);
  329. } else {
  330. opp_np = of_node_get(opp_table->np);
  331. }
  332. /* Lets not fail in case we are parsing opp-v1 bindings */
  333. if (!opp_np)
  334. return 0;
  335. /* Checking only first OPP is sufficient */
  336. struct device_node *np __free(device_node) =
  337. of_get_next_available_child(opp_np, NULL);
  338. if (!np) {
  339. dev_err(dev, "OPP table empty\n");
  340. return -EINVAL;
  341. }
  342. prop = of_find_property(np, "opp-peak-kBps", NULL);
  343. if (!prop || !prop->length)
  344. return 0;
  345. return 1;
  346. }
  347. int dev_pm_opp_of_find_icc_paths(struct device *dev,
  348. struct opp_table *opp_table)
  349. {
  350. struct device_node *np __free(device_node) = of_node_get(dev->of_node);
  351. int ret, i, count, num_paths;
  352. struct icc_path **paths;
  353. ret = _bandwidth_supported(dev, opp_table);
  354. if (ret == -EINVAL)
  355. return 0; /* Empty OPP table is a valid corner-case, let's not fail */
  356. else if (ret <= 0)
  357. return ret;
  358. if (!np)
  359. return 0;
  360. ret = 0;
  361. count = of_count_phandle_with_args(np, "interconnects",
  362. "#interconnect-cells");
  363. if (count < 0)
  364. return 0;
  365. /* two phandles when #interconnect-cells = <1> */
  366. if (count % 2) {
  367. dev_err(dev, "%s: Invalid interconnects values\n", __func__);
  368. return -EINVAL;
  369. }
  370. num_paths = count / 2;
  371. paths = kzalloc_objs(*paths, num_paths);
  372. if (!paths)
  373. return -ENOMEM;
  374. for (i = 0; i < num_paths; i++) {
  375. paths[i] = of_icc_get_by_index(dev, i);
  376. if (IS_ERR(paths[i])) {
  377. ret = dev_err_probe(dev, PTR_ERR(paths[i]), "%s: Unable to get path%d\n", __func__, i);
  378. goto err;
  379. }
  380. }
  381. if (opp_table) {
  382. opp_table->paths = paths;
  383. opp_table->path_count = num_paths;
  384. return 0;
  385. }
  386. err:
  387. while (i--)
  388. icc_put(paths[i]);
  389. kfree(paths);
  390. return ret;
  391. }
  392. EXPORT_SYMBOL_GPL(dev_pm_opp_of_find_icc_paths);
  393. static bool _opp_is_supported(struct device *dev, struct opp_table *opp_table,
  394. struct device_node *np)
  395. {
  396. unsigned int levels = opp_table->supported_hw_count;
  397. int count, versions, ret, i, j;
  398. u32 val;
  399. if (!opp_table->supported_hw) {
  400. /*
  401. * In the case that no supported_hw has been set by the
  402. * platform but there is an opp-supported-hw value set for
  403. * an OPP then the OPP should not be enabled as there is
  404. * no way to see if the hardware supports it.
  405. */
  406. if (of_property_present(np, "opp-supported-hw"))
  407. return false;
  408. else
  409. return true;
  410. }
  411. count = of_property_count_u32_elems(np, "opp-supported-hw");
  412. if (count <= 0 || count % levels) {
  413. dev_err(dev, "%s: Invalid opp-supported-hw property (%d)\n",
  414. __func__, count);
  415. return false;
  416. }
  417. versions = count / levels;
  418. /* All levels in at least one of the versions should match */
  419. for (i = 0; i < versions; i++) {
  420. bool supported = true;
  421. for (j = 0; j < levels; j++) {
  422. ret = of_property_read_u32_index(np, "opp-supported-hw",
  423. i * levels + j, &val);
  424. if (ret) {
  425. dev_warn(dev, "%s: failed to read opp-supported-hw property at index %d: %d\n",
  426. __func__, i * levels + j, ret);
  427. return false;
  428. }
  429. /* Check if the level is supported */
  430. if (!(val & opp_table->supported_hw[j])) {
  431. supported = false;
  432. break;
  433. }
  434. }
  435. if (supported)
  436. return true;
  437. }
  438. return false;
  439. }
  440. static u32 *_parse_named_prop(struct dev_pm_opp *opp, struct device *dev,
  441. struct opp_table *opp_table,
  442. const char *prop_type, bool *triplet)
  443. {
  444. struct property *prop = NULL;
  445. char name[NAME_MAX];
  446. int count, ret;
  447. u32 *out;
  448. /* Search for "opp-<prop_type>-<name>" */
  449. if (opp_table->prop_name) {
  450. snprintf(name, sizeof(name), "opp-%s-%s", prop_type,
  451. opp_table->prop_name);
  452. prop = of_find_property(opp->np, name, NULL);
  453. }
  454. if (!prop) {
  455. /* Search for "opp-<prop_type>" */
  456. snprintf(name, sizeof(name), "opp-%s", prop_type);
  457. prop = of_find_property(opp->np, name, NULL);
  458. if (!prop)
  459. return NULL;
  460. }
  461. count = of_property_count_u32_elems(opp->np, name);
  462. if (count < 0) {
  463. dev_err(dev, "%s: Invalid %s property (%d)\n", __func__, name,
  464. count);
  465. return ERR_PTR(count);
  466. }
  467. /*
  468. * Initialize regulator_count, if regulator information isn't provided
  469. * by the platform. Now that one of the properties is available, fix the
  470. * regulator_count to 1.
  471. */
  472. if (unlikely(opp_table->regulator_count == -1))
  473. opp_table->regulator_count = 1;
  474. if (count != opp_table->regulator_count &&
  475. (!triplet || count != opp_table->regulator_count * 3)) {
  476. dev_err(dev, "%s: Invalid number of elements in %s property (%u) with supplies (%d)\n",
  477. __func__, prop_type, count, opp_table->regulator_count);
  478. return ERR_PTR(-EINVAL);
  479. }
  480. out = kmalloc_array(count, sizeof(*out), GFP_KERNEL);
  481. if (!out)
  482. return ERR_PTR(-EINVAL);
  483. ret = of_property_read_u32_array(opp->np, name, out, count);
  484. if (ret) {
  485. dev_err(dev, "%s: error parsing %s: %d\n", __func__, name, ret);
  486. kfree(out);
  487. return ERR_PTR(-EINVAL);
  488. }
  489. if (triplet)
  490. *triplet = count != opp_table->regulator_count;
  491. return out;
  492. }
  493. static u32 *opp_parse_microvolt(struct dev_pm_opp *opp, struct device *dev,
  494. struct opp_table *opp_table, bool *triplet)
  495. {
  496. u32 *microvolt;
  497. microvolt = _parse_named_prop(opp, dev, opp_table, "microvolt", triplet);
  498. if (IS_ERR(microvolt))
  499. return microvolt;
  500. if (!microvolt) {
  501. /*
  502. * Missing property isn't a problem, but an invalid
  503. * entry is. This property isn't optional if regulator
  504. * information is provided. Check only for the first OPP, as
  505. * regulator_count may get initialized after that to a valid
  506. * value.
  507. */
  508. if (list_empty(&opp_table->opp_list) &&
  509. opp_table->regulator_count > 0) {
  510. dev_err(dev, "%s: opp-microvolt missing although OPP managing regulators\n",
  511. __func__);
  512. return ERR_PTR(-EINVAL);
  513. }
  514. }
  515. return microvolt;
  516. }
  517. static int opp_parse_supplies(struct dev_pm_opp *opp, struct device *dev,
  518. struct opp_table *opp_table)
  519. {
  520. u32 *microvolt, *microamp, *microwatt;
  521. int ret = 0, i, j;
  522. bool triplet;
  523. microvolt = opp_parse_microvolt(opp, dev, opp_table, &triplet);
  524. if (IS_ERR(microvolt))
  525. return PTR_ERR(microvolt);
  526. microamp = _parse_named_prop(opp, dev, opp_table, "microamp", NULL);
  527. if (IS_ERR(microamp)) {
  528. ret = PTR_ERR(microamp);
  529. goto free_microvolt;
  530. }
  531. microwatt = _parse_named_prop(opp, dev, opp_table, "microwatt", NULL);
  532. if (IS_ERR(microwatt)) {
  533. ret = PTR_ERR(microwatt);
  534. goto free_microamp;
  535. }
  536. /*
  537. * Initialize regulator_count if it is uninitialized and no properties
  538. * are found.
  539. */
  540. if (unlikely(opp_table->regulator_count == -1)) {
  541. opp_table->regulator_count = 0;
  542. return 0;
  543. }
  544. for (i = 0, j = 0; i < opp_table->regulator_count; i++) {
  545. if (microvolt) {
  546. opp->supplies[i].u_volt = microvolt[j++];
  547. if (triplet) {
  548. opp->supplies[i].u_volt_min = microvolt[j++];
  549. opp->supplies[i].u_volt_max = microvolt[j++];
  550. } else {
  551. opp->supplies[i].u_volt_min = opp->supplies[i].u_volt;
  552. opp->supplies[i].u_volt_max = opp->supplies[i].u_volt;
  553. }
  554. }
  555. if (microamp)
  556. opp->supplies[i].u_amp = microamp[i];
  557. if (microwatt)
  558. opp->supplies[i].u_watt = microwatt[i];
  559. }
  560. kfree(microwatt);
  561. free_microamp:
  562. kfree(microamp);
  563. free_microvolt:
  564. kfree(microvolt);
  565. return ret;
  566. }
  567. /**
  568. * dev_pm_opp_of_remove_table() - Free OPP table entries created from static DT
  569. * entries
  570. * @dev: device pointer used to lookup OPP table.
  571. *
  572. * Free OPPs created using static entries present in DT.
  573. */
  574. void dev_pm_opp_of_remove_table(struct device *dev)
  575. {
  576. dev_pm_opp_remove_table(dev);
  577. }
  578. EXPORT_SYMBOL_GPL(dev_pm_opp_of_remove_table);
  579. static int _read_rate(struct dev_pm_opp *new_opp, struct opp_table *opp_table,
  580. struct device_node *np)
  581. {
  582. struct property *prop;
  583. int i, count, ret;
  584. u64 *rates;
  585. prop = of_find_property(np, "opp-hz", NULL);
  586. if (!prop)
  587. return -ENODEV;
  588. count = prop->length / sizeof(u64);
  589. if (opp_table->clk_count != count) {
  590. pr_err("%s: Count mismatch between opp-hz and clk_count (%d %d)\n",
  591. __func__, count, opp_table->clk_count);
  592. return -EINVAL;
  593. }
  594. rates = kmalloc_array(count, sizeof(*rates), GFP_KERNEL);
  595. if (!rates)
  596. return -ENOMEM;
  597. ret = of_property_read_u64_array(np, "opp-hz", rates, count);
  598. if (ret) {
  599. pr_err("%s: Error parsing opp-hz: %d\n", __func__, ret);
  600. } else {
  601. /*
  602. * Rate is defined as an unsigned long in clk API, and so
  603. * casting explicitly to its type. Must be fixed once rate is 64
  604. * bit guaranteed in clk API.
  605. */
  606. for (i = 0; i < count; i++) {
  607. new_opp->rates[i] = (unsigned long)rates[i];
  608. /* This will happen for frequencies > 4.29 GHz */
  609. WARN_ON(new_opp->rates[i] != rates[i]);
  610. }
  611. }
  612. kfree(rates);
  613. return ret;
  614. }
  615. static int _read_bw(struct dev_pm_opp *new_opp, struct opp_table *opp_table,
  616. struct device_node *np, bool peak)
  617. {
  618. const char *name = peak ? "opp-peak-kBps" : "opp-avg-kBps";
  619. struct property *prop;
  620. int i, count, ret;
  621. u32 *bw;
  622. prop = of_find_property(np, name, NULL);
  623. if (!prop)
  624. return -ENODEV;
  625. count = prop->length / sizeof(u32);
  626. if (opp_table->path_count != count) {
  627. pr_err("%s: Mismatch between %s and paths (%d %d)\n",
  628. __func__, name, count, opp_table->path_count);
  629. return -EINVAL;
  630. }
  631. bw = kmalloc_array(count, sizeof(*bw), GFP_KERNEL);
  632. if (!bw)
  633. return -ENOMEM;
  634. ret = of_property_read_u32_array(np, name, bw, count);
  635. if (ret) {
  636. pr_err("%s: Error parsing %s: %d\n", __func__, name, ret);
  637. goto out;
  638. }
  639. for (i = 0; i < count; i++) {
  640. if (peak)
  641. new_opp->bandwidth[i].peak = kBps_to_icc(bw[i]);
  642. else
  643. new_opp->bandwidth[i].avg = kBps_to_icc(bw[i]);
  644. }
  645. out:
  646. kfree(bw);
  647. return ret;
  648. }
  649. static int _read_opp_key(struct dev_pm_opp *new_opp,
  650. struct opp_table *opp_table, struct device_node *np)
  651. {
  652. bool found = false;
  653. int ret;
  654. ret = _read_rate(new_opp, opp_table, np);
  655. if (!ret)
  656. found = true;
  657. else if (ret != -ENODEV)
  658. return ret;
  659. /*
  660. * Bandwidth consists of peak and average (optional) values:
  661. * opp-peak-kBps = <path1_value path2_value>;
  662. * opp-avg-kBps = <path1_value path2_value>;
  663. */
  664. ret = _read_bw(new_opp, opp_table, np, true);
  665. if (!ret) {
  666. found = true;
  667. ret = _read_bw(new_opp, opp_table, np, false);
  668. }
  669. /* The properties were found but we failed to parse them */
  670. if (ret && ret != -ENODEV)
  671. return ret;
  672. if (!of_property_read_u32(np, "opp-level", &new_opp->level))
  673. found = true;
  674. if (found)
  675. return 0;
  676. return ret;
  677. }
  678. /**
  679. * _opp_add_static_v2() - Allocate static OPPs (As per 'v2' DT bindings)
  680. * @opp_table: OPP table
  681. * @dev: device for which we do this operation
  682. * @np: device node
  683. *
  684. * This function adds an opp definition to the opp table and returns status. The
  685. * opp can be controlled using dev_pm_opp_enable/disable functions and may be
  686. * removed by dev_pm_opp_remove.
  687. *
  688. * Return:
  689. * Valid OPP pointer:
  690. * On success
  691. * NULL:
  692. * Duplicate OPPs (both freq and volt are same) and opp->available
  693. * OR if the OPP is not supported by hardware.
  694. * ERR_PTR(-EEXIST):
  695. * Freq are same and volt are different OR
  696. * Duplicate OPPs (both freq and volt are same) and !opp->available
  697. * ERR_PTR(-ENOMEM):
  698. * Memory allocation failure
  699. * ERR_PTR(-EINVAL):
  700. * Failed parsing the OPP node
  701. */
  702. static struct dev_pm_opp *_opp_add_static_v2(struct opp_table *opp_table,
  703. struct device *dev, struct device_node *np)
  704. {
  705. struct dev_pm_opp *new_opp;
  706. u32 val;
  707. int ret;
  708. new_opp = _opp_allocate(opp_table);
  709. if (!new_opp)
  710. return ERR_PTR(-ENOMEM);
  711. ret = _read_opp_key(new_opp, opp_table, np);
  712. if (ret < 0) {
  713. dev_err(dev, "%s: opp key field not found\n", __func__);
  714. goto free_opp;
  715. }
  716. /* Check if the OPP supports hardware's hierarchy of versions or not */
  717. if (!_opp_is_supported(dev, opp_table, np)) {
  718. dev_dbg(dev, "OPP not supported by hardware: %s\n",
  719. of_node_full_name(np));
  720. goto free_opp;
  721. }
  722. new_opp->turbo = of_property_read_bool(np, "turbo-mode");
  723. new_opp->np = of_node_get(np);
  724. new_opp->dynamic = false;
  725. new_opp->available = true;
  726. ret = _of_opp_alloc_required_opps(opp_table, new_opp);
  727. if (ret)
  728. goto put_node;
  729. if (!of_property_read_u32(np, "clock-latency-ns", &val))
  730. new_opp->clock_latency_ns = val;
  731. ret = opp_parse_supplies(new_opp, dev, opp_table);
  732. if (ret)
  733. goto free_required_opps;
  734. ret = _opp_add(dev, new_opp, opp_table);
  735. if (ret) {
  736. /* Don't return error for duplicate OPPs */
  737. if (ret == -EBUSY)
  738. ret = 0;
  739. goto free_required_opps;
  740. }
  741. /* OPP to select on device suspend */
  742. if (of_property_read_bool(np, "opp-suspend")) {
  743. if (opp_table->suspend_opp) {
  744. /* Pick the OPP with higher rate/bw/level as suspend OPP */
  745. if (_opp_compare_key(opp_table, new_opp, opp_table->suspend_opp) == 1) {
  746. opp_table->suspend_opp->suspend = false;
  747. new_opp->suspend = true;
  748. opp_table->suspend_opp = new_opp;
  749. }
  750. } else {
  751. new_opp->suspend = true;
  752. opp_table->suspend_opp = new_opp;
  753. }
  754. }
  755. if (new_opp->clock_latency_ns > opp_table->clock_latency_ns_max)
  756. opp_table->clock_latency_ns_max = new_opp->clock_latency_ns;
  757. pr_debug("%s: turbo:%d rate:%lu uv:%lu uvmin:%lu uvmax:%lu latency:%lu level:%u\n",
  758. __func__, new_opp->turbo, new_opp->rates[0],
  759. new_opp->supplies[0].u_volt, new_opp->supplies[0].u_volt_min,
  760. new_opp->supplies[0].u_volt_max, new_opp->clock_latency_ns,
  761. new_opp->level);
  762. /*
  763. * Notify the changes in the availability of the operable
  764. * frequency/voltage list.
  765. */
  766. blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp);
  767. return new_opp;
  768. free_required_opps:
  769. _of_opp_free_required_opps(opp_table, new_opp);
  770. put_node:
  771. of_node_put(np);
  772. free_opp:
  773. _opp_free(new_opp);
  774. return ret ? ERR_PTR(ret) : NULL;
  775. }
  776. /* Initializes OPP tables based on new bindings */
  777. static int _of_add_opp_table_v2(struct device *dev, struct opp_table *opp_table)
  778. {
  779. int ret, count = 0;
  780. struct dev_pm_opp *opp;
  781. /* OPP table is already initialized for the device */
  782. scoped_guard(mutex, &opp_table->lock) {
  783. if (opp_table->parsed_static_opps) {
  784. opp_table->parsed_static_opps++;
  785. return 0;
  786. }
  787. opp_table->parsed_static_opps = 1;
  788. }
  789. /* We have opp-table node now, iterate over it and add OPPs */
  790. for_each_available_child_of_node_scoped(opp_table->np, np) {
  791. opp = _opp_add_static_v2(opp_table, dev, np);
  792. if (IS_ERR(opp)) {
  793. ret = PTR_ERR(opp);
  794. dev_err(dev, "%s: Failed to add OPP, %d\n", __func__,
  795. ret);
  796. goto remove_static_opp;
  797. } else if (opp) {
  798. count++;
  799. }
  800. }
  801. /* There should be one or more OPPs defined */
  802. if (!count) {
  803. dev_err(dev, "%s: no supported OPPs", __func__);
  804. ret = -ENOENT;
  805. goto remove_static_opp;
  806. }
  807. lazy_link_required_opp_table(opp_table);
  808. return 0;
  809. remove_static_opp:
  810. _opp_remove_all_static(opp_table);
  811. return ret;
  812. }
  813. /* Initializes OPP tables based on old-deprecated bindings */
  814. static int _of_add_opp_table_v1(struct device *dev, struct opp_table *opp_table)
  815. {
  816. const struct property *prop;
  817. const __be32 *val;
  818. int nr, ret = 0;
  819. scoped_guard(mutex, &opp_table->lock) {
  820. if (opp_table->parsed_static_opps) {
  821. opp_table->parsed_static_opps++;
  822. return 0;
  823. }
  824. opp_table->parsed_static_opps = 1;
  825. }
  826. prop = of_find_property(dev->of_node, "operating-points", NULL);
  827. if (!prop) {
  828. ret = -ENODEV;
  829. goto remove_static_opp;
  830. }
  831. if (!prop->value) {
  832. ret = -ENODATA;
  833. goto remove_static_opp;
  834. }
  835. /*
  836. * Each OPP is a set of tuples consisting of frequency and
  837. * voltage like <freq-kHz vol-uV>.
  838. */
  839. nr = prop->length / sizeof(u32);
  840. if (nr % 2) {
  841. dev_err(dev, "%s: Invalid OPP table\n", __func__);
  842. ret = -EINVAL;
  843. goto remove_static_opp;
  844. }
  845. val = prop->value;
  846. while (nr) {
  847. unsigned long freq = be32_to_cpup(val++) * 1000;
  848. unsigned long volt = be32_to_cpup(val++);
  849. struct dev_pm_opp_data data = {
  850. .freq = freq,
  851. .u_volt = volt,
  852. };
  853. ret = _opp_add_v1(opp_table, dev, &data, false);
  854. if (ret) {
  855. dev_err(dev, "%s: Failed to add OPP %ld (%d)\n",
  856. __func__, data.freq, ret);
  857. goto remove_static_opp;
  858. }
  859. nr -= 2;
  860. }
  861. return 0;
  862. remove_static_opp:
  863. _opp_remove_all_static(opp_table);
  864. return ret;
  865. }
  866. static int _of_add_table_indexed(struct device *dev, int index)
  867. {
  868. struct opp_table *opp_table;
  869. int ret, count;
  870. if (index) {
  871. /*
  872. * If only one phandle is present, then the same OPP table
  873. * applies for all index requests.
  874. */
  875. count = of_count_phandle_with_args(dev->of_node,
  876. "operating-points-v2", NULL);
  877. if (count == 1)
  878. index = 0;
  879. }
  880. opp_table = _add_opp_table_indexed(dev, index, true);
  881. if (IS_ERR(opp_table))
  882. return PTR_ERR(opp_table);
  883. /*
  884. * OPPs have two version of bindings now. Also try the old (v1)
  885. * bindings for backward compatibility with older dtbs.
  886. */
  887. if (opp_table->np)
  888. ret = _of_add_opp_table_v2(dev, opp_table);
  889. else
  890. ret = _of_add_opp_table_v1(dev, opp_table);
  891. if (ret)
  892. dev_pm_opp_put_opp_table(opp_table);
  893. return ret;
  894. }
  895. static void devm_pm_opp_of_table_release(void *data)
  896. {
  897. dev_pm_opp_of_remove_table(data);
  898. }
  899. static int _devm_of_add_table_indexed(struct device *dev, int index)
  900. {
  901. int ret;
  902. ret = _of_add_table_indexed(dev, index);
  903. if (ret)
  904. return ret;
  905. return devm_add_action_or_reset(dev, devm_pm_opp_of_table_release, dev);
  906. }
  907. /**
  908. * devm_pm_opp_of_add_table() - Initialize opp table from device tree
  909. * @dev: device pointer used to lookup OPP table.
  910. *
  911. * Register the initial OPP table with the OPP library for given device.
  912. *
  913. * The opp_table structure will be freed after the device is destroyed.
  914. *
  915. * Return:
  916. * 0 On success OR
  917. * Duplicate OPPs (both freq and volt are same) and opp->available
  918. * -EEXIST Freq are same and volt are different OR
  919. * Duplicate OPPs (both freq and volt are same) and !opp->available
  920. * -ENOMEM Memory allocation failure
  921. * -ENODEV when 'operating-points' property is not found or is invalid data
  922. * in device node.
  923. * -ENODATA when empty 'operating-points' property is found
  924. * -EINVAL when invalid entries are found in opp-v2 table
  925. */
  926. int devm_pm_opp_of_add_table(struct device *dev)
  927. {
  928. return _devm_of_add_table_indexed(dev, 0);
  929. }
  930. EXPORT_SYMBOL_GPL(devm_pm_opp_of_add_table);
  931. /**
  932. * dev_pm_opp_of_add_table() - Initialize opp table from device tree
  933. * @dev: device pointer used to lookup OPP table.
  934. *
  935. * Register the initial OPP table with the OPP library for given device.
  936. *
  937. * Return:
  938. * 0 On success OR
  939. * Duplicate OPPs (both freq and volt are same) and opp->available
  940. * -EEXIST Freq are same and volt are different OR
  941. * Duplicate OPPs (both freq and volt are same) and !opp->available
  942. * -ENOMEM Memory allocation failure
  943. * -ENODEV when 'operating-points' property is not found or is invalid data
  944. * in device node.
  945. * -ENODATA when empty 'operating-points' property is found
  946. * -EINVAL when invalid entries are found in opp-v2 table
  947. */
  948. int dev_pm_opp_of_add_table(struct device *dev)
  949. {
  950. return _of_add_table_indexed(dev, 0);
  951. }
  952. EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table);
  953. /**
  954. * dev_pm_opp_of_add_table_indexed() - Initialize indexed opp table from device tree
  955. * @dev: device pointer used to lookup OPP table.
  956. * @index: Index number.
  957. *
  958. * Register the initial OPP table with the OPP library for given device only
  959. * using the "operating-points-v2" property.
  960. *
  961. * Return: Refer to dev_pm_opp_of_add_table() for return values.
  962. */
  963. int dev_pm_opp_of_add_table_indexed(struct device *dev, int index)
  964. {
  965. return _of_add_table_indexed(dev, index);
  966. }
  967. EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table_indexed);
  968. /**
  969. * devm_pm_opp_of_add_table_indexed() - Initialize indexed opp table from device tree
  970. * @dev: device pointer used to lookup OPP table.
  971. * @index: Index number.
  972. *
  973. * This is a resource-managed variant of dev_pm_opp_of_add_table_indexed().
  974. */
  975. int devm_pm_opp_of_add_table_indexed(struct device *dev, int index)
  976. {
  977. return _devm_of_add_table_indexed(dev, index);
  978. }
  979. EXPORT_SYMBOL_GPL(devm_pm_opp_of_add_table_indexed);
  980. /* CPU device specific helpers */
  981. /**
  982. * dev_pm_opp_of_cpumask_remove_table() - Removes OPP table for @cpumask
  983. * @cpumask: cpumask for which OPP table needs to be removed
  984. *
  985. * This removes the OPP tables for CPUs present in the @cpumask.
  986. * This should be used only to remove static entries created from DT.
  987. */
  988. void dev_pm_opp_of_cpumask_remove_table(const struct cpumask *cpumask)
  989. {
  990. _dev_pm_opp_cpumask_remove_table(cpumask, -1);
  991. }
  992. EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_remove_table);
  993. /**
  994. * dev_pm_opp_of_cpumask_add_table() - Adds OPP table for @cpumask
  995. * @cpumask: cpumask for which OPP table needs to be added.
  996. *
  997. * This adds the OPP tables for CPUs present in the @cpumask.
  998. */
  999. int dev_pm_opp_of_cpumask_add_table(const struct cpumask *cpumask)
  1000. {
  1001. struct device *cpu_dev;
  1002. int cpu, ret;
  1003. if (WARN_ON(cpumask_empty(cpumask)))
  1004. return -ENODEV;
  1005. for_each_cpu(cpu, cpumask) {
  1006. cpu_dev = get_cpu_device(cpu);
  1007. if (!cpu_dev) {
  1008. pr_err("%s: failed to get cpu%d device\n", __func__,
  1009. cpu);
  1010. ret = -ENODEV;
  1011. goto remove_table;
  1012. }
  1013. ret = dev_pm_opp_of_add_table(cpu_dev);
  1014. if (ret) {
  1015. /*
  1016. * OPP may get registered dynamically, don't print error
  1017. * message here.
  1018. */
  1019. pr_debug("%s: couldn't find opp table for cpu:%d, %d\n",
  1020. __func__, cpu, ret);
  1021. goto remove_table;
  1022. }
  1023. }
  1024. return 0;
  1025. remove_table:
  1026. /* Free all other OPPs */
  1027. _dev_pm_opp_cpumask_remove_table(cpumask, cpu);
  1028. return ret;
  1029. }
  1030. EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_add_table);
  1031. /*
  1032. * Works only for OPP v2 bindings.
  1033. *
  1034. * Returns -ENOENT if operating-points-v2 bindings aren't supported.
  1035. */
  1036. /**
  1037. * dev_pm_opp_of_get_sharing_cpus() - Get cpumask of CPUs sharing OPPs with
  1038. * @cpu_dev using operating-points-v2
  1039. * bindings.
  1040. *
  1041. * @cpu_dev: CPU device for which we do this operation
  1042. * @cpumask: cpumask to update with information of sharing CPUs
  1043. *
  1044. * This updates the @cpumask with CPUs that are sharing OPPs with @cpu_dev.
  1045. *
  1046. * Returns -ENOENT if operating-points-v2 isn't present for @cpu_dev.
  1047. */
  1048. int dev_pm_opp_of_get_sharing_cpus(struct device *cpu_dev,
  1049. struct cpumask *cpumask)
  1050. {
  1051. int cpu;
  1052. /* Get OPP descriptor node */
  1053. struct device_node *np __free(device_node) =
  1054. dev_pm_opp_of_get_opp_desc_node(cpu_dev);
  1055. if (!np) {
  1056. dev_dbg(cpu_dev, "%s: Couldn't find opp node.\n", __func__);
  1057. return -ENOENT;
  1058. }
  1059. cpumask_set_cpu(cpu_dev->id, cpumask);
  1060. /* OPPs are shared ? */
  1061. if (!of_property_read_bool(np, "opp-shared"))
  1062. return 0;
  1063. for_each_possible_cpu(cpu) {
  1064. if (cpu == cpu_dev->id)
  1065. continue;
  1066. struct device_node *cpu_np __free(device_node) =
  1067. of_cpu_device_node_get(cpu);
  1068. if (!cpu_np) {
  1069. dev_err(cpu_dev, "%s: failed to get cpu%d node\n",
  1070. __func__, cpu);
  1071. return -ENOENT;
  1072. }
  1073. /* Get OPP descriptor node */
  1074. struct device_node *tmp_np __free(device_node) =
  1075. _opp_of_get_opp_desc_node(cpu_np, 0);
  1076. if (!tmp_np) {
  1077. pr_err("%pOF: Couldn't find opp node\n", cpu_np);
  1078. return -ENOENT;
  1079. }
  1080. /* CPUs are sharing opp node */
  1081. if (np == tmp_np)
  1082. cpumask_set_cpu(cpu, cpumask);
  1083. }
  1084. return 0;
  1085. }
  1086. EXPORT_SYMBOL_GPL(dev_pm_opp_of_get_sharing_cpus);
  1087. /**
  1088. * of_get_required_opp_performance_state() - Search for required OPP and return its performance state.
  1089. * @np: Node that contains the "required-opps" property.
  1090. * @index: Index of the phandle to parse.
  1091. *
  1092. * Returns the performance state of the OPP pointed out by the "required-opps"
  1093. * property at @index in @np.
  1094. *
  1095. * Return: Zero or positive performance state on success, otherwise negative
  1096. * value on errors.
  1097. */
  1098. int of_get_required_opp_performance_state(struct device_node *np, int index)
  1099. {
  1100. int pstate = -EINVAL;
  1101. struct device_node *required_np __free(device_node) =
  1102. of_parse_required_opp(np, index);
  1103. if (!required_np)
  1104. return -ENODEV;
  1105. struct opp_table *opp_table __free(put_opp_table) =
  1106. _find_table_of_opp_np(required_np);
  1107. if (IS_ERR(opp_table)) {
  1108. pr_err("%s: Failed to find required OPP table %pOF: %ld\n",
  1109. __func__, np, PTR_ERR(opp_table));
  1110. return PTR_ERR(opp_table);
  1111. }
  1112. /* The OPP tables must belong to a genpd */
  1113. if (unlikely(!opp_table->is_genpd)) {
  1114. pr_err("%s: Performance state is only valid for genpds.\n", __func__);
  1115. return -EINVAL;
  1116. }
  1117. struct dev_pm_opp *opp __free(put_opp) =
  1118. _find_opp_of_np(opp_table, required_np);
  1119. if (opp) {
  1120. if (opp->level == OPP_LEVEL_UNSET) {
  1121. pr_err("%s: OPP levels aren't available for %pOF\n",
  1122. __func__, np);
  1123. } else {
  1124. pstate = opp->level;
  1125. }
  1126. }
  1127. return pstate;
  1128. }
  1129. EXPORT_SYMBOL_GPL(of_get_required_opp_performance_state);
  1130. /**
  1131. * dev_pm_opp_of_has_required_opp - Find out if a required-opps exists.
  1132. * @dev: The device to investigate.
  1133. *
  1134. * Returns true if the device's node has a "operating-points-v2" property and if
  1135. * the corresponding node for the opp-table describes opp nodes that uses the
  1136. * "required-opps" property.
  1137. *
  1138. * Return: True if a required-opps is present, else false.
  1139. */
  1140. bool dev_pm_opp_of_has_required_opp(struct device *dev)
  1141. {
  1142. int count;
  1143. struct device_node *opp_np __free(device_node) =
  1144. _opp_of_get_opp_desc_node(dev->of_node, 0);
  1145. if (!opp_np)
  1146. return false;
  1147. struct device_node *np __free(device_node) =
  1148. of_get_next_available_child(opp_np, NULL);
  1149. if (!np) {
  1150. dev_warn(dev, "Empty OPP table\n");
  1151. return false;
  1152. }
  1153. count = of_count_phandle_with_args(np, "required-opps", NULL);
  1154. return count > 0;
  1155. }
  1156. /**
  1157. * dev_pm_opp_get_of_node() - Gets the DT node corresponding to an opp
  1158. * @opp: opp for which DT node has to be returned for
  1159. *
  1160. * Return: DT node corresponding to the opp, else 0 on success.
  1161. *
  1162. * The caller needs to put the node with of_node_put() after using it.
  1163. */
  1164. struct device_node *dev_pm_opp_get_of_node(struct dev_pm_opp *opp)
  1165. {
  1166. if (IS_ERR_OR_NULL(opp)) {
  1167. pr_err("%s: Invalid parameters\n", __func__);
  1168. return NULL;
  1169. }
  1170. return of_node_get(opp->np);
  1171. }
  1172. EXPORT_SYMBOL_GPL(dev_pm_opp_get_of_node);
  1173. /*
  1174. * Callback function provided to the Energy Model framework upon registration.
  1175. * It provides the power used by @dev at @kHz if it is the frequency of an
  1176. * existing OPP, or at the frequency of the first OPP above @kHz otherwise
  1177. * (see dev_pm_opp_find_freq_ceil()). This function updates @kHz to the ceiled
  1178. * frequency and @uW to the associated power.
  1179. *
  1180. * Returns 0 on success or a proper -EINVAL value in case of error.
  1181. */
  1182. static int __maybe_unused
  1183. _get_dt_power(struct device *dev, unsigned long *uW, unsigned long *kHz)
  1184. {
  1185. unsigned long opp_freq, opp_power;
  1186. /* Find the right frequency and related OPP */
  1187. opp_freq = *kHz * 1000;
  1188. struct dev_pm_opp *opp __free(put_opp) =
  1189. dev_pm_opp_find_freq_ceil(dev, &opp_freq);
  1190. if (IS_ERR(opp))
  1191. return -EINVAL;
  1192. opp_power = dev_pm_opp_get_power(opp);
  1193. if (!opp_power)
  1194. return -EINVAL;
  1195. *kHz = opp_freq / 1000;
  1196. *uW = opp_power;
  1197. return 0;
  1198. }
  1199. /**
  1200. * dev_pm_opp_calc_power() - Calculate power value for device with EM
  1201. * @dev : Device for which an Energy Model has to be registered
  1202. * @uW : New power value that is calculated
  1203. * @kHz : Frequency for which the new power is calculated
  1204. *
  1205. * This computes the power estimated by @dev at @kHz if it is the frequency
  1206. * of an existing OPP, or at the frequency of the first OPP above @kHz otherwise
  1207. * (see dev_pm_opp_find_freq_ceil()). This function updates @kHz to the ceiled
  1208. * frequency and @uW to the associated power. The power is estimated as
  1209. * P = C * V^2 * f with C being the device's capacitance and V and f
  1210. * respectively the voltage and frequency of the OPP.
  1211. * It is also used as a callback function provided to the Energy Model
  1212. * framework upon registration.
  1213. *
  1214. * Returns -EINVAL if the power calculation failed because of missing
  1215. * parameters, 0 otherwise.
  1216. */
  1217. int dev_pm_opp_calc_power(struct device *dev, unsigned long *uW,
  1218. unsigned long *kHz)
  1219. {
  1220. unsigned long mV, Hz;
  1221. u32 cap;
  1222. u64 tmp;
  1223. int ret;
  1224. struct device_node *np __free(device_node) = of_node_get(dev->of_node);
  1225. if (!np)
  1226. return -EINVAL;
  1227. ret = of_property_read_u32(np, "dynamic-power-coefficient", &cap);
  1228. if (ret)
  1229. return -EINVAL;
  1230. Hz = *kHz * 1000;
  1231. struct dev_pm_opp *opp __free(put_opp) =
  1232. dev_pm_opp_find_freq_ceil(dev, &Hz);
  1233. if (IS_ERR(opp))
  1234. return -EINVAL;
  1235. mV = dev_pm_opp_get_voltage(opp) / 1000;
  1236. if (!mV)
  1237. return -EINVAL;
  1238. tmp = (u64)cap * mV * mV * (Hz / 1000000);
  1239. /* Provide power in micro-Watts */
  1240. do_div(tmp, 1000000);
  1241. *uW = (unsigned long)tmp;
  1242. *kHz = Hz / 1000;
  1243. return 0;
  1244. }
  1245. EXPORT_SYMBOL_GPL(dev_pm_opp_calc_power);
  1246. static bool _of_has_opp_microwatt_property(struct device *dev)
  1247. {
  1248. unsigned long freq = 0;
  1249. /* Check if at least one OPP has needed property */
  1250. struct dev_pm_opp *opp __free(put_opp) =
  1251. dev_pm_opp_find_freq_ceil(dev, &freq);
  1252. if (IS_ERR(opp))
  1253. return false;
  1254. return !!dev_pm_opp_get_power(opp);
  1255. }
  1256. /**
  1257. * dev_pm_opp_of_register_em() - Attempt to register an Energy Model
  1258. * @dev : Device for which an Energy Model has to be registered
  1259. * @cpus : CPUs for which an Energy Model has to be registered. For
  1260. * other type of devices it should be set to NULL.
  1261. *
  1262. * This checks whether the "dynamic-power-coefficient" devicetree property has
  1263. * been specified, and tries to register an Energy Model with it if it has.
  1264. * Having this property means the voltages are known for OPPs and the EM
  1265. * might be calculated.
  1266. */
  1267. int dev_pm_opp_of_register_em(struct device *dev, struct cpumask *cpus)
  1268. {
  1269. struct em_data_callback em_cb;
  1270. int ret, nr_opp;
  1271. u32 cap;
  1272. if (IS_ERR_OR_NULL(dev))
  1273. return -EINVAL;
  1274. struct device_node *np __free(device_node) = of_node_get(dev->of_node);
  1275. if (!np) {
  1276. ret = -EINVAL;
  1277. goto failed;
  1278. }
  1279. nr_opp = dev_pm_opp_get_opp_count(dev);
  1280. if (nr_opp <= 0) {
  1281. ret = -EINVAL;
  1282. goto failed;
  1283. }
  1284. /* First, try to find more precised Energy Model in DT */
  1285. if (_of_has_opp_microwatt_property(dev)) {
  1286. EM_SET_ACTIVE_POWER_CB(em_cb, _get_dt_power);
  1287. goto register_em;
  1288. }
  1289. /*
  1290. * Register an EM only if the 'dynamic-power-coefficient' property is
  1291. * set in devicetree. It is assumed the voltage values are known if that
  1292. * property is set since it is useless otherwise. If voltages are not
  1293. * known, just let the EM registration fail with an error to alert the
  1294. * user about the inconsistent configuration.
  1295. */
  1296. ret = of_property_read_u32(np, "dynamic-power-coefficient", &cap);
  1297. if (ret || !cap) {
  1298. dev_dbg(dev, "Couldn't find proper 'dynamic-power-coefficient' in DT\n");
  1299. ret = -EINVAL;
  1300. goto failed;
  1301. }
  1302. EM_SET_ACTIVE_POWER_CB(em_cb, dev_pm_opp_calc_power);
  1303. register_em:
  1304. ret = em_dev_register_perf_domain(dev, nr_opp, &em_cb, cpus, true);
  1305. if (ret)
  1306. goto failed;
  1307. return 0;
  1308. failed:
  1309. dev_dbg(dev, "Couldn't register Energy Model %d\n", ret);
  1310. return ret;
  1311. }
  1312. EXPORT_SYMBOL_GPL(dev_pm_opp_of_register_em);