of.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  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. new_opp = _opp_allocate(opp_table);
  253. if (!new_opp)
  254. return -ENOMEM;
  255. ret = of_property_read_u64(np, "opp-hz", &rate);
  256. if (ret < 0) {
  257. dev_err(dev, "%s: opp-hz not found\n", __func__);
  258. goto free_opp;
  259. }
  260. /* Check if the OPP supports hardware's hierarchy of versions or not */
  261. if (!_opp_is_supported(dev, opp_table, np)) {
  262. dev_dbg(dev, "OPP not supported by hardware: %llu\n", rate);
  263. goto free_opp;
  264. }
  265. /*
  266. * Rate is defined as an unsigned long in clk API, and so casting
  267. * explicitly to its type. Must be fixed once rate is 64 bit
  268. * guaranteed in clk API.
  269. */
  270. new_opp->rate = (unsigned long)rate;
  271. new_opp->turbo = of_property_read_bool(np, "turbo-mode");
  272. new_opp->np = np;
  273. new_opp->dynamic = false;
  274. new_opp->available = true;
  275. if (!of_property_read_u32(np, "clock-latency-ns", &val))
  276. new_opp->clock_latency_ns = val;
  277. ret = opp_parse_supplies(new_opp, dev, opp_table);
  278. if (ret)
  279. goto free_opp;
  280. ret = _opp_add(dev, new_opp, opp_table);
  281. if (ret) {
  282. /* Don't return error for duplicate OPPs */
  283. if (ret == -EBUSY)
  284. ret = 0;
  285. goto free_opp;
  286. }
  287. /* OPP to select on device suspend */
  288. if (of_property_read_bool(np, "opp-suspend")) {
  289. if (opp_table->suspend_opp) {
  290. dev_warn(dev, "%s: Multiple suspend OPPs found (%lu %lu)\n",
  291. __func__, opp_table->suspend_opp->rate,
  292. new_opp->rate);
  293. } else {
  294. new_opp->suspend = true;
  295. opp_table->suspend_opp = new_opp;
  296. }
  297. }
  298. if (new_opp->clock_latency_ns > opp_table->clock_latency_ns_max)
  299. opp_table->clock_latency_ns_max = new_opp->clock_latency_ns;
  300. pr_debug("%s: turbo:%d rate:%lu uv:%lu uvmin:%lu uvmax:%lu latency:%lu\n",
  301. __func__, new_opp->turbo, new_opp->rate,
  302. new_opp->supplies[0].u_volt, new_opp->supplies[0].u_volt_min,
  303. new_opp->supplies[0].u_volt_max, new_opp->clock_latency_ns);
  304. /*
  305. * Notify the changes in the availability of the operable
  306. * frequency/voltage list.
  307. */
  308. blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp);
  309. return 0;
  310. free_opp:
  311. _opp_free(new_opp);
  312. return ret;
  313. }
  314. /* Initializes OPP tables based on new bindings */
  315. static int _of_add_opp_table_v2(struct device *dev, struct device_node *opp_np)
  316. {
  317. struct device_node *np;
  318. struct opp_table *opp_table;
  319. int ret = 0, count = 0;
  320. opp_table = _managed_opp(opp_np);
  321. if (opp_table) {
  322. /* OPPs are already managed */
  323. if (!_add_opp_dev(dev, opp_table))
  324. ret = -ENOMEM;
  325. goto put_opp_table;
  326. }
  327. opp_table = dev_pm_opp_get_opp_table(dev);
  328. if (!opp_table)
  329. return -ENOMEM;
  330. /* We have opp-table node now, iterate over it and add OPPs */
  331. for_each_available_child_of_node(opp_np, np) {
  332. count++;
  333. ret = _opp_add_static_v2(opp_table, dev, np);
  334. if (ret) {
  335. dev_err(dev, "%s: Failed to add OPP, %d\n", __func__,
  336. ret);
  337. _dev_pm_opp_remove_table(opp_table, dev, false);
  338. of_node_put(np);
  339. goto put_opp_table;
  340. }
  341. }
  342. /* There should be one of more OPP defined */
  343. if (WARN_ON(!count)) {
  344. ret = -ENOENT;
  345. goto put_opp_table;
  346. }
  347. opp_table->np = opp_np;
  348. if (of_property_read_bool(opp_np, "opp-shared"))
  349. opp_table->shared_opp = OPP_TABLE_ACCESS_SHARED;
  350. else
  351. opp_table->shared_opp = OPP_TABLE_ACCESS_EXCLUSIVE;
  352. put_opp_table:
  353. dev_pm_opp_put_opp_table(opp_table);
  354. return ret;
  355. }
  356. /* Initializes OPP tables based on old-deprecated bindings */
  357. static int _of_add_opp_table_v1(struct device *dev)
  358. {
  359. struct opp_table *opp_table;
  360. const struct property *prop;
  361. const __be32 *val;
  362. int nr, ret = 0;
  363. prop = of_find_property(dev->of_node, "operating-points", NULL);
  364. if (!prop)
  365. return -ENODEV;
  366. if (!prop->value)
  367. return -ENODATA;
  368. /*
  369. * Each OPP is a set of tuples consisting of frequency and
  370. * voltage like <freq-kHz vol-uV>.
  371. */
  372. nr = prop->length / sizeof(u32);
  373. if (nr % 2) {
  374. dev_err(dev, "%s: Invalid OPP table\n", __func__);
  375. return -EINVAL;
  376. }
  377. opp_table = dev_pm_opp_get_opp_table(dev);
  378. if (!opp_table)
  379. return -ENOMEM;
  380. val = prop->value;
  381. while (nr) {
  382. unsigned long freq = be32_to_cpup(val++) * 1000;
  383. unsigned long volt = be32_to_cpup(val++);
  384. ret = _opp_add_v1(opp_table, dev, freq, volt, false);
  385. if (ret) {
  386. dev_err(dev, "%s: Failed to add OPP %ld (%d)\n",
  387. __func__, freq, ret);
  388. _dev_pm_opp_remove_table(opp_table, dev, false);
  389. break;
  390. }
  391. nr -= 2;
  392. }
  393. dev_pm_opp_put_opp_table(opp_table);
  394. return ret;
  395. }
  396. /**
  397. * dev_pm_opp_of_add_table() - Initialize opp table from device tree
  398. * @dev: device pointer used to lookup OPP table.
  399. *
  400. * Register the initial OPP table with the OPP library for given device.
  401. *
  402. * Return:
  403. * 0 On success OR
  404. * Duplicate OPPs (both freq and volt are same) and opp->available
  405. * -EEXIST Freq are same and volt are different OR
  406. * Duplicate OPPs (both freq and volt are same) and !opp->available
  407. * -ENOMEM Memory allocation failure
  408. * -ENODEV when 'operating-points' property is not found or is invalid data
  409. * in device node.
  410. * -ENODATA when empty 'operating-points' property is found
  411. * -EINVAL when invalid entries are found in opp-v2 table
  412. */
  413. int dev_pm_opp_of_add_table(struct device *dev)
  414. {
  415. struct device_node *opp_np;
  416. int ret;
  417. /*
  418. * OPPs have two version of bindings now. The older one is deprecated,
  419. * try for the new binding first.
  420. */
  421. opp_np = dev_pm_opp_of_get_opp_desc_node(dev);
  422. if (!opp_np) {
  423. /*
  424. * Try old-deprecated bindings for backward compatibility with
  425. * older dtbs.
  426. */
  427. return _of_add_opp_table_v1(dev);
  428. }
  429. ret = _of_add_opp_table_v2(dev, opp_np);
  430. of_node_put(opp_np);
  431. return ret;
  432. }
  433. EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table);
  434. /* CPU device specific helpers */
  435. /**
  436. * dev_pm_opp_of_cpumask_remove_table() - Removes OPP table for @cpumask
  437. * @cpumask: cpumask for which OPP table needs to be removed
  438. *
  439. * This removes the OPP tables for CPUs present in the @cpumask.
  440. * This should be used only to remove static entries created from DT.
  441. */
  442. void dev_pm_opp_of_cpumask_remove_table(const struct cpumask *cpumask)
  443. {
  444. _dev_pm_opp_cpumask_remove_table(cpumask, true);
  445. }
  446. EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_remove_table);
  447. /**
  448. * dev_pm_opp_of_cpumask_add_table() - Adds OPP table for @cpumask
  449. * @cpumask: cpumask for which OPP table needs to be added.
  450. *
  451. * This adds the OPP tables for CPUs present in the @cpumask.
  452. */
  453. int dev_pm_opp_of_cpumask_add_table(const struct cpumask *cpumask)
  454. {
  455. struct device *cpu_dev;
  456. int cpu, ret = 0;
  457. WARN_ON(cpumask_empty(cpumask));
  458. for_each_cpu(cpu, cpumask) {
  459. cpu_dev = get_cpu_device(cpu);
  460. if (!cpu_dev) {
  461. pr_err("%s: failed to get cpu%d device\n", __func__,
  462. cpu);
  463. continue;
  464. }
  465. ret = dev_pm_opp_of_add_table(cpu_dev);
  466. if (ret) {
  467. /*
  468. * OPP may get registered dynamically, don't print error
  469. * message here.
  470. */
  471. pr_debug("%s: couldn't find opp table for cpu:%d, %d\n",
  472. __func__, cpu, ret);
  473. /* Free all other OPPs */
  474. dev_pm_opp_of_cpumask_remove_table(cpumask);
  475. break;
  476. }
  477. }
  478. return ret;
  479. }
  480. EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_add_table);
  481. /*
  482. * Works only for OPP v2 bindings.
  483. *
  484. * Returns -ENOENT if operating-points-v2 bindings aren't supported.
  485. */
  486. /**
  487. * dev_pm_opp_of_get_sharing_cpus() - Get cpumask of CPUs sharing OPPs with
  488. * @cpu_dev using operating-points-v2
  489. * bindings.
  490. *
  491. * @cpu_dev: CPU device for which we do this operation
  492. * @cpumask: cpumask to update with information of sharing CPUs
  493. *
  494. * This updates the @cpumask with CPUs that are sharing OPPs with @cpu_dev.
  495. *
  496. * Returns -ENOENT if operating-points-v2 isn't present for @cpu_dev.
  497. */
  498. int dev_pm_opp_of_get_sharing_cpus(struct device *cpu_dev,
  499. struct cpumask *cpumask)
  500. {
  501. struct device_node *np, *tmp_np, *cpu_np;
  502. int cpu, ret = 0;
  503. /* Get OPP descriptor node */
  504. np = dev_pm_opp_of_get_opp_desc_node(cpu_dev);
  505. if (!np) {
  506. dev_dbg(cpu_dev, "%s: Couldn't find opp node.\n", __func__);
  507. return -ENOENT;
  508. }
  509. cpumask_set_cpu(cpu_dev->id, cpumask);
  510. /* OPPs are shared ? */
  511. if (!of_property_read_bool(np, "opp-shared"))
  512. goto put_cpu_node;
  513. for_each_possible_cpu(cpu) {
  514. if (cpu == cpu_dev->id)
  515. continue;
  516. cpu_np = of_cpu_device_node_get(cpu);
  517. if (!cpu_np) {
  518. dev_err(cpu_dev, "%s: failed to get cpu%d node\n",
  519. __func__, cpu);
  520. ret = -ENOENT;
  521. goto put_cpu_node;
  522. }
  523. /* Get OPP descriptor node */
  524. tmp_np = _opp_of_get_opp_desc_node(cpu_np);
  525. of_node_put(cpu_np);
  526. if (!tmp_np) {
  527. pr_err("%pOF: Couldn't find opp node\n", cpu_np);
  528. ret = -ENOENT;
  529. goto put_cpu_node;
  530. }
  531. /* CPUs are sharing opp node */
  532. if (np == tmp_np)
  533. cpumask_set_cpu(cpu, cpumask);
  534. of_node_put(tmp_np);
  535. }
  536. put_cpu_node:
  537. of_node_put(np);
  538. return ret;
  539. }
  540. EXPORT_SYMBOL_GPL(dev_pm_opp_of_get_sharing_cpus);