of.c 16 KB

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