of.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  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.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. return 0;
  114. }
  115. vcount = of_property_count_u32_elems(opp->np, name);
  116. if (vcount < 0) {
  117. dev_err(dev, "%s: Invalid %s property (%d)\n",
  118. __func__, name, vcount);
  119. return vcount;
  120. }
  121. /* There can be one or three elements per supply */
  122. if (vcount != supplies && vcount != supplies * 3) {
  123. dev_err(dev, "%s: Invalid number of elements in %s property (%d) with supplies (%d)\n",
  124. __func__, name, vcount, supplies);
  125. return -EINVAL;
  126. }
  127. microvolt = kmalloc_array(vcount, sizeof(*microvolt), GFP_KERNEL);
  128. if (!microvolt)
  129. return -ENOMEM;
  130. ret = of_property_read_u32_array(opp->np, name, microvolt, vcount);
  131. if (ret) {
  132. dev_err(dev, "%s: error parsing %s: %d\n", __func__, name, ret);
  133. ret = -EINVAL;
  134. goto free_microvolt;
  135. }
  136. /* Search for "opp-microamp-<name>" */
  137. prop = NULL;
  138. if (opp_table->prop_name) {
  139. snprintf(name, sizeof(name), "opp-microamp-%s",
  140. opp_table->prop_name);
  141. prop = of_find_property(opp->np, name, NULL);
  142. }
  143. if (!prop) {
  144. /* Search for "opp-microamp" */
  145. sprintf(name, "opp-microamp");
  146. prop = of_find_property(opp->np, name, NULL);
  147. }
  148. if (prop) {
  149. icount = of_property_count_u32_elems(opp->np, name);
  150. if (icount < 0) {
  151. dev_err(dev, "%s: Invalid %s property (%d)\n", __func__,
  152. name, icount);
  153. ret = icount;
  154. goto free_microvolt;
  155. }
  156. if (icount != supplies) {
  157. dev_err(dev, "%s: Invalid number of elements in %s property (%d) with supplies (%d)\n",
  158. __func__, name, icount, supplies);
  159. ret = -EINVAL;
  160. goto free_microvolt;
  161. }
  162. microamp = kmalloc_array(icount, sizeof(*microamp), GFP_KERNEL);
  163. if (!microamp) {
  164. ret = -EINVAL;
  165. goto free_microvolt;
  166. }
  167. ret = of_property_read_u32_array(opp->np, name, microamp,
  168. icount);
  169. if (ret) {
  170. dev_err(dev, "%s: error parsing %s: %d\n", __func__,
  171. name, ret);
  172. ret = -EINVAL;
  173. goto free_microamp;
  174. }
  175. }
  176. for (i = 0, j = 0; i < supplies; i++) {
  177. opp->supplies[i].u_volt = microvolt[j++];
  178. if (vcount == supplies) {
  179. opp->supplies[i].u_volt_min = opp->supplies[i].u_volt;
  180. opp->supplies[i].u_volt_max = opp->supplies[i].u_volt;
  181. } else {
  182. opp->supplies[i].u_volt_min = microvolt[j++];
  183. opp->supplies[i].u_volt_max = microvolt[j++];
  184. }
  185. if (microamp)
  186. opp->supplies[i].u_amp = microamp[i];
  187. }
  188. free_microamp:
  189. kfree(microamp);
  190. free_microvolt:
  191. kfree(microvolt);
  192. return ret;
  193. }
  194. /**
  195. * dev_pm_opp_of_remove_table() - Free OPP table entries created from static DT
  196. * entries
  197. * @dev: device pointer used to lookup OPP table.
  198. *
  199. * Free OPPs created using static entries present in DT.
  200. */
  201. void dev_pm_opp_of_remove_table(struct device *dev)
  202. {
  203. _dev_pm_opp_find_and_remove_table(dev, false);
  204. }
  205. EXPORT_SYMBOL_GPL(dev_pm_opp_of_remove_table);
  206. /* Returns opp descriptor node for a device, caller must do of_node_put() */
  207. struct device_node *dev_pm_opp_of_get_opp_desc_node(struct device *dev)
  208. {
  209. /*
  210. * There should be only ONE phandle present in "operating-points-v2"
  211. * property.
  212. */
  213. return of_parse_phandle(dev->of_node, "operating-points-v2", 0);
  214. }
  215. EXPORT_SYMBOL_GPL(dev_pm_opp_of_get_opp_desc_node);
  216. /**
  217. * _opp_add_static_v2() - Allocate static OPPs (As per 'v2' DT bindings)
  218. * @opp_table: OPP table
  219. * @dev: device for which we do this operation
  220. * @np: device node
  221. *
  222. * This function adds an opp definition to the opp table and returns status. The
  223. * opp can be controlled using dev_pm_opp_enable/disable functions and may be
  224. * removed by dev_pm_opp_remove.
  225. *
  226. * Return:
  227. * 0 On success OR
  228. * Duplicate OPPs (both freq and volt are same) and opp->available
  229. * -EEXIST Freq are same and volt are different OR
  230. * Duplicate OPPs (both freq and volt are same) and !opp->available
  231. * -ENOMEM Memory allocation failure
  232. * -EINVAL Failed parsing the OPP node
  233. */
  234. static int _opp_add_static_v2(struct opp_table *opp_table, struct device *dev,
  235. struct device_node *np)
  236. {
  237. struct dev_pm_opp *new_opp;
  238. u64 rate;
  239. u32 val;
  240. int ret;
  241. new_opp = _opp_allocate(opp_table);
  242. if (!new_opp)
  243. return -ENOMEM;
  244. ret = of_property_read_u64(np, "opp-hz", &rate);
  245. if (ret < 0) {
  246. dev_err(dev, "%s: opp-hz not found\n", __func__);
  247. goto free_opp;
  248. }
  249. /* Check if the OPP supports hardware's hierarchy of versions or not */
  250. if (!_opp_is_supported(dev, opp_table, np)) {
  251. dev_dbg(dev, "OPP not supported by hardware: %llu\n", rate);
  252. goto free_opp;
  253. }
  254. /*
  255. * Rate is defined as an unsigned long in clk API, and so casting
  256. * explicitly to its type. Must be fixed once rate is 64 bit
  257. * guaranteed in clk API.
  258. */
  259. new_opp->rate = (unsigned long)rate;
  260. new_opp->turbo = of_property_read_bool(np, "turbo-mode");
  261. new_opp->np = np;
  262. new_opp->dynamic = false;
  263. new_opp->available = true;
  264. if (!of_property_read_u32(np, "clock-latency-ns", &val))
  265. new_opp->clock_latency_ns = val;
  266. ret = opp_parse_supplies(new_opp, dev, opp_table);
  267. if (ret)
  268. goto free_opp;
  269. ret = _opp_add(dev, new_opp, opp_table);
  270. if (ret) {
  271. /* Don't return error for duplicate OPPs */
  272. if (ret == -EBUSY)
  273. ret = 0;
  274. goto free_opp;
  275. }
  276. /* OPP to select on device suspend */
  277. if (of_property_read_bool(np, "opp-suspend")) {
  278. if (opp_table->suspend_opp) {
  279. dev_warn(dev, "%s: Multiple suspend OPPs found (%lu %lu)\n",
  280. __func__, opp_table->suspend_opp->rate,
  281. new_opp->rate);
  282. } else {
  283. new_opp->suspend = true;
  284. opp_table->suspend_opp = new_opp;
  285. }
  286. }
  287. if (new_opp->clock_latency_ns > opp_table->clock_latency_ns_max)
  288. opp_table->clock_latency_ns_max = new_opp->clock_latency_ns;
  289. pr_debug("%s: turbo:%d rate:%lu uv:%lu uvmin:%lu uvmax:%lu latency:%lu\n",
  290. __func__, new_opp->turbo, new_opp->rate,
  291. new_opp->supplies[0].u_volt, new_opp->supplies[0].u_volt_min,
  292. new_opp->supplies[0].u_volt_max, new_opp->clock_latency_ns);
  293. /*
  294. * Notify the changes in the availability of the operable
  295. * frequency/voltage list.
  296. */
  297. blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp);
  298. return 0;
  299. free_opp:
  300. _opp_free(new_opp);
  301. return ret;
  302. }
  303. /* Initializes OPP tables based on new bindings */
  304. static int _of_add_opp_table_v2(struct device *dev, struct device_node *opp_np)
  305. {
  306. struct device_node *np;
  307. struct opp_table *opp_table;
  308. int ret = 0, count = 0;
  309. opp_table = _managed_opp(opp_np);
  310. if (opp_table) {
  311. /* OPPs are already managed */
  312. if (!_add_opp_dev(dev, opp_table))
  313. ret = -ENOMEM;
  314. goto put_opp_table;
  315. }
  316. opp_table = dev_pm_opp_get_opp_table(dev);
  317. if (!opp_table)
  318. return -ENOMEM;
  319. /* We have opp-table node now, iterate over it and add OPPs */
  320. for_each_available_child_of_node(opp_np, np) {
  321. count++;
  322. ret = _opp_add_static_v2(opp_table, dev, np);
  323. if (ret) {
  324. dev_err(dev, "%s: Failed to add OPP, %d\n", __func__,
  325. ret);
  326. _dev_pm_opp_remove_table(opp_table, dev, false);
  327. goto put_opp_table;
  328. }
  329. }
  330. /* There should be one of more OPP defined */
  331. if (WARN_ON(!count)) {
  332. ret = -ENOENT;
  333. goto put_opp_table;
  334. }
  335. opp_table->np = opp_np;
  336. if (of_property_read_bool(opp_np, "opp-shared"))
  337. opp_table->shared_opp = OPP_TABLE_ACCESS_SHARED;
  338. else
  339. opp_table->shared_opp = OPP_TABLE_ACCESS_EXCLUSIVE;
  340. put_opp_table:
  341. dev_pm_opp_put_opp_table(opp_table);
  342. return ret;
  343. }
  344. /* Initializes OPP tables based on old-deprecated bindings */
  345. static int _of_add_opp_table_v1(struct device *dev)
  346. {
  347. struct opp_table *opp_table;
  348. const struct property *prop;
  349. const __be32 *val;
  350. int nr, ret = 0;
  351. prop = of_find_property(dev->of_node, "operating-points", NULL);
  352. if (!prop)
  353. return -ENODEV;
  354. if (!prop->value)
  355. return -ENODATA;
  356. /*
  357. * Each OPP is a set of tuples consisting of frequency and
  358. * voltage like <freq-kHz vol-uV>.
  359. */
  360. nr = prop->length / sizeof(u32);
  361. if (nr % 2) {
  362. dev_err(dev, "%s: Invalid OPP table\n", __func__);
  363. return -EINVAL;
  364. }
  365. opp_table = dev_pm_opp_get_opp_table(dev);
  366. if (!opp_table)
  367. return -ENOMEM;
  368. val = prop->value;
  369. while (nr) {
  370. unsigned long freq = be32_to_cpup(val++) * 1000;
  371. unsigned long volt = be32_to_cpup(val++);
  372. ret = _opp_add_v1(opp_table, dev, freq, volt, false);
  373. if (ret) {
  374. dev_err(dev, "%s: Failed to add OPP %ld (%d)\n",
  375. __func__, freq, ret);
  376. _dev_pm_opp_remove_table(opp_table, dev, false);
  377. break;
  378. }
  379. nr -= 2;
  380. }
  381. dev_pm_opp_put_opp_table(opp_table);
  382. return ret;
  383. }
  384. /**
  385. * dev_pm_opp_of_add_table() - Initialize opp table from device tree
  386. * @dev: device pointer used to lookup OPP table.
  387. *
  388. * Register the initial OPP table with the OPP library for given device.
  389. *
  390. * Return:
  391. * 0 On success OR
  392. * Duplicate OPPs (both freq and volt are same) and opp->available
  393. * -EEXIST Freq are same and volt are different OR
  394. * Duplicate OPPs (both freq and volt are same) and !opp->available
  395. * -ENOMEM Memory allocation failure
  396. * -ENODEV when 'operating-points' property is not found or is invalid data
  397. * in device node.
  398. * -ENODATA when empty 'operating-points' property is found
  399. * -EINVAL when invalid entries are found in opp-v2 table
  400. */
  401. int dev_pm_opp_of_add_table(struct device *dev)
  402. {
  403. struct device_node *opp_np;
  404. int ret;
  405. /*
  406. * OPPs have two version of bindings now. The older one is deprecated,
  407. * try for the new binding first.
  408. */
  409. opp_np = dev_pm_opp_of_get_opp_desc_node(dev);
  410. if (!opp_np) {
  411. /*
  412. * Try old-deprecated bindings for backward compatibility with
  413. * older dtbs.
  414. */
  415. return _of_add_opp_table_v1(dev);
  416. }
  417. ret = _of_add_opp_table_v2(dev, opp_np);
  418. of_node_put(opp_np);
  419. return ret;
  420. }
  421. EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table);
  422. /* CPU device specific helpers */
  423. /**
  424. * dev_pm_opp_of_cpumask_remove_table() - Removes OPP table for @cpumask
  425. * @cpumask: cpumask for which OPP table needs to be removed
  426. *
  427. * This removes the OPP tables for CPUs present in the @cpumask.
  428. * This should be used only to remove static entries created from DT.
  429. */
  430. void dev_pm_opp_of_cpumask_remove_table(const struct cpumask *cpumask)
  431. {
  432. _dev_pm_opp_cpumask_remove_table(cpumask, true);
  433. }
  434. EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_remove_table);
  435. /**
  436. * dev_pm_opp_of_cpumask_add_table() - Adds OPP table for @cpumask
  437. * @cpumask: cpumask for which OPP table needs to be added.
  438. *
  439. * This adds the OPP tables for CPUs present in the @cpumask.
  440. */
  441. int dev_pm_opp_of_cpumask_add_table(const struct cpumask *cpumask)
  442. {
  443. struct device *cpu_dev;
  444. int cpu, ret = 0;
  445. WARN_ON(cpumask_empty(cpumask));
  446. for_each_cpu(cpu, cpumask) {
  447. cpu_dev = get_cpu_device(cpu);
  448. if (!cpu_dev) {
  449. pr_err("%s: failed to get cpu%d device\n", __func__,
  450. cpu);
  451. continue;
  452. }
  453. ret = dev_pm_opp_of_add_table(cpu_dev);
  454. if (ret) {
  455. pr_err("%s: couldn't find opp table for cpu:%d, %d\n",
  456. __func__, cpu, ret);
  457. /* Free all other OPPs */
  458. dev_pm_opp_of_cpumask_remove_table(cpumask);
  459. break;
  460. }
  461. }
  462. return ret;
  463. }
  464. EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_add_table);
  465. /*
  466. * Works only for OPP v2 bindings.
  467. *
  468. * Returns -ENOENT if operating-points-v2 bindings aren't supported.
  469. */
  470. /**
  471. * dev_pm_opp_of_get_sharing_cpus() - Get cpumask of CPUs sharing OPPs with
  472. * @cpu_dev using operating-points-v2
  473. * bindings.
  474. *
  475. * @cpu_dev: CPU device for which we do this operation
  476. * @cpumask: cpumask to update with information of sharing CPUs
  477. *
  478. * This updates the @cpumask with CPUs that are sharing OPPs with @cpu_dev.
  479. *
  480. * Returns -ENOENT if operating-points-v2 isn't present for @cpu_dev.
  481. */
  482. int dev_pm_opp_of_get_sharing_cpus(struct device *cpu_dev,
  483. struct cpumask *cpumask)
  484. {
  485. struct device_node *np, *tmp_np;
  486. struct device *tcpu_dev;
  487. int cpu, ret = 0;
  488. /* Get OPP descriptor node */
  489. np = dev_pm_opp_of_get_opp_desc_node(cpu_dev);
  490. if (!np) {
  491. dev_dbg(cpu_dev, "%s: Couldn't find opp node.\n", __func__);
  492. return -ENOENT;
  493. }
  494. cpumask_set_cpu(cpu_dev->id, cpumask);
  495. /* OPPs are shared ? */
  496. if (!of_property_read_bool(np, "opp-shared"))
  497. goto put_cpu_node;
  498. for_each_possible_cpu(cpu) {
  499. if (cpu == cpu_dev->id)
  500. continue;
  501. tcpu_dev = get_cpu_device(cpu);
  502. if (!tcpu_dev) {
  503. dev_err(cpu_dev, "%s: failed to get cpu%d device\n",
  504. __func__, cpu);
  505. ret = -ENODEV;
  506. goto put_cpu_node;
  507. }
  508. /* Get OPP descriptor node */
  509. tmp_np = dev_pm_opp_of_get_opp_desc_node(tcpu_dev);
  510. if (!tmp_np) {
  511. dev_err(tcpu_dev, "%s: Couldn't find opp node.\n",
  512. __func__);
  513. ret = -ENOENT;
  514. goto put_cpu_node;
  515. }
  516. /* CPUs are sharing opp node */
  517. if (np == tmp_np)
  518. cpumask_set_cpu(cpu, cpumask);
  519. of_node_put(tmp_np);
  520. }
  521. put_cpu_node:
  522. of_node_put(np);
  523. return ret;
  524. }
  525. EXPORT_SYMBOL_GPL(dev_pm_opp_of_get_sharing_cpus);