of.c 16 KB

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