opp.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. /*
  2. * Generic OPP Interface
  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. #include <linux/kernel.h>
  14. #include <linux/errno.h>
  15. #include <linux/err.h>
  16. #include <linux/slab.h>
  17. #include <linux/device.h>
  18. #include <linux/list.h>
  19. #include <linux/rculist.h>
  20. #include <linux/rcupdate.h>
  21. #include <linux/pm_opp.h>
  22. #include <linux/of.h>
  23. #include <linux/export.h>
  24. /*
  25. * Internal data structure organization with the OPP layer library is as
  26. * follows:
  27. * dev_opp_list (root)
  28. * |- device 1 (represents voltage domain 1)
  29. * | |- opp 1 (availability, freq, voltage)
  30. * | |- opp 2 ..
  31. * ... ...
  32. * | `- opp n ..
  33. * |- device 2 (represents the next voltage domain)
  34. * ...
  35. * `- device m (represents mth voltage domain)
  36. * device 1, 2.. are represented by dev_opp structure while each opp
  37. * is represented by the opp structure.
  38. */
  39. /**
  40. * struct dev_pm_opp - Generic OPP description structure
  41. * @node: opp list node. The nodes are maintained throughout the lifetime
  42. * of boot. It is expected only an optimal set of OPPs are
  43. * added to the library by the SoC framework.
  44. * RCU usage: opp list is traversed with RCU locks. node
  45. * modification is possible realtime, hence the modifications
  46. * are protected by the dev_opp_list_lock for integrity.
  47. * IMPORTANT: the opp nodes should be maintained in increasing
  48. * order.
  49. * @available: true/false - marks if this OPP as available or not
  50. * @rate: Frequency in hertz
  51. * @u_volt: Nominal voltage in microvolts corresponding to this OPP
  52. * @dev_opp: points back to the device_opp struct this opp belongs to
  53. * @head: RCU callback head used for deferred freeing
  54. *
  55. * This structure stores the OPP information for a given device.
  56. */
  57. struct dev_pm_opp {
  58. struct list_head node;
  59. bool available;
  60. unsigned long rate;
  61. unsigned long u_volt;
  62. struct device_opp *dev_opp;
  63. struct rcu_head head;
  64. };
  65. /**
  66. * struct device_opp - Device opp structure
  67. * @node: list node - contains the devices with OPPs that
  68. * have been registered. Nodes once added are not modified in this
  69. * list.
  70. * RCU usage: nodes are not modified in the list of device_opp,
  71. * however addition is possible and is secured by dev_opp_list_lock
  72. * @dev: device pointer
  73. * @head: notifier head to notify the OPP availability changes.
  74. * @opp_list: list of opps
  75. *
  76. * This is an internal data structure maintaining the link to opps attached to
  77. * a device. This structure is not meant to be shared to users as it is
  78. * meant for book keeping and private to OPP library
  79. */
  80. struct device_opp {
  81. struct list_head node;
  82. struct device *dev;
  83. struct srcu_notifier_head head;
  84. struct list_head opp_list;
  85. };
  86. /*
  87. * The root of the list of all devices. All device_opp structures branch off
  88. * from here, with each device_opp containing the list of opp it supports in
  89. * various states of availability.
  90. */
  91. static LIST_HEAD(dev_opp_list);
  92. /* Lock to allow exclusive modification to the device and opp lists */
  93. static DEFINE_MUTEX(dev_opp_list_lock);
  94. /**
  95. * find_device_opp() - find device_opp struct using device pointer
  96. * @dev: device pointer used to lookup device OPPs
  97. *
  98. * Search list of device OPPs for one containing matching device. Does a RCU
  99. * reader operation to grab the pointer needed.
  100. *
  101. * Returns pointer to 'struct device_opp' if found, otherwise -ENODEV or
  102. * -EINVAL based on type of error.
  103. *
  104. * Locking: This function must be called under rcu_read_lock(). device_opp
  105. * is a RCU protected pointer. This means that device_opp is valid as long
  106. * as we are under RCU lock.
  107. */
  108. static struct device_opp *find_device_opp(struct device *dev)
  109. {
  110. struct device_opp *tmp_dev_opp, *dev_opp = ERR_PTR(-ENODEV);
  111. if (unlikely(IS_ERR_OR_NULL(dev))) {
  112. pr_err("%s: Invalid parameters\n", __func__);
  113. return ERR_PTR(-EINVAL);
  114. }
  115. list_for_each_entry_rcu(tmp_dev_opp, &dev_opp_list, node) {
  116. if (tmp_dev_opp->dev == dev) {
  117. dev_opp = tmp_dev_opp;
  118. break;
  119. }
  120. }
  121. return dev_opp;
  122. }
  123. /**
  124. * dev_pm_opp_get_voltage() - Gets the voltage corresponding to an available opp
  125. * @opp: opp for which voltage has to be returned for
  126. *
  127. * Return voltage in micro volt corresponding to the opp, else
  128. * return 0
  129. *
  130. * Locking: This function must be called under rcu_read_lock(). opp is a rcu
  131. * protected pointer. This means that opp which could have been fetched by
  132. * opp_find_freq_{exact,ceil,floor} functions is valid as long as we are
  133. * under RCU lock. The pointer returned by the opp_find_freq family must be
  134. * used in the same section as the usage of this function with the pointer
  135. * prior to unlocking with rcu_read_unlock() to maintain the integrity of the
  136. * pointer.
  137. */
  138. unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp)
  139. {
  140. struct dev_pm_opp *tmp_opp;
  141. unsigned long v = 0;
  142. tmp_opp = rcu_dereference(opp);
  143. if (unlikely(IS_ERR_OR_NULL(tmp_opp)) || !tmp_opp->available)
  144. pr_err("%s: Invalid parameters\n", __func__);
  145. else
  146. v = tmp_opp->u_volt;
  147. return v;
  148. }
  149. EXPORT_SYMBOL_GPL(dev_pm_opp_get_voltage);
  150. /**
  151. * dev_pm_opp_get_freq() - Gets the frequency corresponding to an available opp
  152. * @opp: opp for which frequency has to be returned for
  153. *
  154. * Return frequency in hertz corresponding to the opp, else
  155. * return 0
  156. *
  157. * Locking: This function must be called under rcu_read_lock(). opp is a rcu
  158. * protected pointer. This means that opp which could have been fetched by
  159. * opp_find_freq_{exact,ceil,floor} functions is valid as long as we are
  160. * under RCU lock. The pointer returned by the opp_find_freq family must be
  161. * used in the same section as the usage of this function with the pointer
  162. * prior to unlocking with rcu_read_unlock() to maintain the integrity of the
  163. * pointer.
  164. */
  165. unsigned long dev_pm_opp_get_freq(struct dev_pm_opp *opp)
  166. {
  167. struct dev_pm_opp *tmp_opp;
  168. unsigned long f = 0;
  169. tmp_opp = rcu_dereference(opp);
  170. if (unlikely(IS_ERR_OR_NULL(tmp_opp)) || !tmp_opp->available)
  171. pr_err("%s: Invalid parameters\n", __func__);
  172. else
  173. f = tmp_opp->rate;
  174. return f;
  175. }
  176. EXPORT_SYMBOL_GPL(dev_pm_opp_get_freq);
  177. /**
  178. * dev_pm_opp_get_opp_count() - Get number of opps available in the opp list
  179. * @dev: device for which we do this operation
  180. *
  181. * This function returns the number of available opps if there are any,
  182. * else returns 0 if none or the corresponding error value.
  183. *
  184. * Locking: This function must be called under rcu_read_lock(). This function
  185. * internally references two RCU protected structures: device_opp and opp which
  186. * are safe as long as we are under a common RCU locked section.
  187. */
  188. int dev_pm_opp_get_opp_count(struct device *dev)
  189. {
  190. struct device_opp *dev_opp;
  191. struct dev_pm_opp *temp_opp;
  192. int count = 0;
  193. dev_opp = find_device_opp(dev);
  194. if (IS_ERR(dev_opp)) {
  195. int r = PTR_ERR(dev_opp);
  196. dev_err(dev, "%s: device OPP not found (%d)\n", __func__, r);
  197. return r;
  198. }
  199. list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
  200. if (temp_opp->available)
  201. count++;
  202. }
  203. return count;
  204. }
  205. EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count);
  206. /**
  207. * dev_pm_opp_find_freq_exact() - search for an exact frequency
  208. * @dev: device for which we do this operation
  209. * @freq: frequency to search for
  210. * @available: true/false - match for available opp
  211. *
  212. * Searches for exact match in the opp list and returns pointer to the matching
  213. * opp if found, else returns ERR_PTR in case of error and should be handled
  214. * using IS_ERR. Error return values can be:
  215. * EINVAL: for bad pointer
  216. * ERANGE: no match found for search
  217. * ENODEV: if device not found in list of registered devices
  218. *
  219. * Note: available is a modifier for the search. if available=true, then the
  220. * match is for exact matching frequency and is available in the stored OPP
  221. * table. if false, the match is for exact frequency which is not available.
  222. *
  223. * This provides a mechanism to enable an opp which is not available currently
  224. * or the opposite as well.
  225. *
  226. * Locking: This function must be called under rcu_read_lock(). opp is a rcu
  227. * protected pointer. The reason for the same is that the opp pointer which is
  228. * returned will remain valid for use with opp_get_{voltage, freq} only while
  229. * under the locked area. The pointer returned must be used prior to unlocking
  230. * with rcu_read_unlock() to maintain the integrity of the pointer.
  231. */
  232. struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev,
  233. unsigned long freq,
  234. bool available)
  235. {
  236. struct device_opp *dev_opp;
  237. struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
  238. dev_opp = find_device_opp(dev);
  239. if (IS_ERR(dev_opp)) {
  240. int r = PTR_ERR(dev_opp);
  241. dev_err(dev, "%s: device OPP not found (%d)\n", __func__, r);
  242. return ERR_PTR(r);
  243. }
  244. list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
  245. if (temp_opp->available == available &&
  246. temp_opp->rate == freq) {
  247. opp = temp_opp;
  248. break;
  249. }
  250. }
  251. return opp;
  252. }
  253. EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact);
  254. /**
  255. * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq
  256. * @dev: device for which we do this operation
  257. * @freq: Start frequency
  258. *
  259. * Search for the matching ceil *available* OPP from a starting freq
  260. * for a device.
  261. *
  262. * Returns matching *opp and refreshes *freq accordingly, else returns
  263. * ERR_PTR in case of error and should be handled using IS_ERR. Error return
  264. * values can be:
  265. * EINVAL: for bad pointer
  266. * ERANGE: no match found for search
  267. * ENODEV: if device not found in list of registered devices
  268. *
  269. * Locking: This function must be called under rcu_read_lock(). opp is a rcu
  270. * protected pointer. The reason for the same is that the opp pointer which is
  271. * returned will remain valid for use with opp_get_{voltage, freq} only while
  272. * under the locked area. The pointer returned must be used prior to unlocking
  273. * with rcu_read_unlock() to maintain the integrity of the pointer.
  274. */
  275. struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev,
  276. unsigned long *freq)
  277. {
  278. struct device_opp *dev_opp;
  279. struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
  280. if (!dev || !freq) {
  281. dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
  282. return ERR_PTR(-EINVAL);
  283. }
  284. dev_opp = find_device_opp(dev);
  285. if (IS_ERR(dev_opp))
  286. return ERR_CAST(dev_opp);
  287. list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
  288. if (temp_opp->available && temp_opp->rate >= *freq) {
  289. opp = temp_opp;
  290. *freq = opp->rate;
  291. break;
  292. }
  293. }
  294. return opp;
  295. }
  296. EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil);
  297. /**
  298. * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq
  299. * @dev: device for which we do this operation
  300. * @freq: Start frequency
  301. *
  302. * Search for the matching floor *available* OPP from a starting freq
  303. * for a device.
  304. *
  305. * Returns matching *opp and refreshes *freq accordingly, else returns
  306. * ERR_PTR in case of error and should be handled using IS_ERR. Error return
  307. * values can be:
  308. * EINVAL: for bad pointer
  309. * ERANGE: no match found for search
  310. * ENODEV: if device not found in list of registered devices
  311. *
  312. * Locking: This function must be called under rcu_read_lock(). opp is a rcu
  313. * protected pointer. The reason for the same is that the opp pointer which is
  314. * returned will remain valid for use with opp_get_{voltage, freq} only while
  315. * under the locked area. The pointer returned must be used prior to unlocking
  316. * with rcu_read_unlock() to maintain the integrity of the pointer.
  317. */
  318. struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev,
  319. unsigned long *freq)
  320. {
  321. struct device_opp *dev_opp;
  322. struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
  323. if (!dev || !freq) {
  324. dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
  325. return ERR_PTR(-EINVAL);
  326. }
  327. dev_opp = find_device_opp(dev);
  328. if (IS_ERR(dev_opp))
  329. return ERR_CAST(dev_opp);
  330. list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
  331. if (temp_opp->available) {
  332. /* go to the next node, before choosing prev */
  333. if (temp_opp->rate > *freq)
  334. break;
  335. else
  336. opp = temp_opp;
  337. }
  338. }
  339. if (!IS_ERR(opp))
  340. *freq = opp->rate;
  341. return opp;
  342. }
  343. EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor);
  344. /**
  345. * dev_pm_opp_add() - Add an OPP table from a table definitions
  346. * @dev: device for which we do this operation
  347. * @freq: Frequency in Hz for this OPP
  348. * @u_volt: Voltage in uVolts for this OPP
  349. *
  350. * This function adds an opp definition to the opp list and returns status.
  351. * The opp is made available by default and it can be controlled using
  352. * dev_pm_opp_enable/disable functions.
  353. *
  354. * Locking: The internal device_opp and opp structures are RCU protected.
  355. * Hence this function internally uses RCU updater strategy with mutex locks
  356. * to keep the integrity of the internal data structures. Callers should ensure
  357. * that this function is *NOT* called under RCU protection or in contexts where
  358. * mutex cannot be locked.
  359. *
  360. * Return:
  361. * 0: On success OR
  362. * Duplicate OPPs (both freq and volt are same) and opp->available
  363. * -EEXIST: Freq are same and volt are different OR
  364. * Duplicate OPPs (both freq and volt are same) and !opp->available
  365. * -ENOMEM: Memory allocation failure
  366. */
  367. int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
  368. {
  369. struct device_opp *dev_opp = NULL;
  370. struct dev_pm_opp *opp, *new_opp;
  371. struct list_head *head;
  372. /* allocate new OPP node */
  373. new_opp = kzalloc(sizeof(*new_opp), GFP_KERNEL);
  374. if (!new_opp) {
  375. dev_warn(dev, "%s: Unable to create new OPP node\n", __func__);
  376. return -ENOMEM;
  377. }
  378. /* Hold our list modification lock here */
  379. mutex_lock(&dev_opp_list_lock);
  380. /* Check for existing list for 'dev' */
  381. dev_opp = find_device_opp(dev);
  382. if (IS_ERR(dev_opp)) {
  383. /*
  384. * Allocate a new device OPP table. In the infrequent case
  385. * where a new device is needed to be added, we pay this
  386. * penalty.
  387. */
  388. dev_opp = kzalloc(sizeof(struct device_opp), GFP_KERNEL);
  389. if (!dev_opp) {
  390. mutex_unlock(&dev_opp_list_lock);
  391. kfree(new_opp);
  392. dev_warn(dev,
  393. "%s: Unable to create device OPP structure\n",
  394. __func__);
  395. return -ENOMEM;
  396. }
  397. dev_opp->dev = dev;
  398. srcu_init_notifier_head(&dev_opp->head);
  399. INIT_LIST_HEAD(&dev_opp->opp_list);
  400. /* Secure the device list modification */
  401. list_add_rcu(&dev_opp->node, &dev_opp_list);
  402. }
  403. /* populate the opp table */
  404. new_opp->dev_opp = dev_opp;
  405. new_opp->rate = freq;
  406. new_opp->u_volt = u_volt;
  407. new_opp->available = true;
  408. /*
  409. * Insert new OPP in order of increasing frequency
  410. * and discard if already present
  411. */
  412. head = &dev_opp->opp_list;
  413. list_for_each_entry_rcu(opp, &dev_opp->opp_list, node) {
  414. if (new_opp->rate <= opp->rate)
  415. break;
  416. else
  417. head = &opp->node;
  418. }
  419. /* Duplicate OPPs ? */
  420. if (new_opp->rate == opp->rate) {
  421. int ret = opp->available && new_opp->u_volt == opp->u_volt ?
  422. 0 : -EEXIST;
  423. dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n",
  424. __func__, opp->rate, opp->u_volt, opp->available,
  425. new_opp->rate, new_opp->u_volt, new_opp->available);
  426. mutex_unlock(&dev_opp_list_lock);
  427. kfree(new_opp);
  428. return ret;
  429. }
  430. list_add_rcu(&new_opp->node, head);
  431. mutex_unlock(&dev_opp_list_lock);
  432. /*
  433. * Notify the changes in the availability of the operable
  434. * frequency/voltage list.
  435. */
  436. srcu_notifier_call_chain(&dev_opp->head, OPP_EVENT_ADD, new_opp);
  437. return 0;
  438. }
  439. EXPORT_SYMBOL_GPL(dev_pm_opp_add);
  440. /**
  441. * opp_set_availability() - helper to set the availability of an opp
  442. * @dev: device for which we do this operation
  443. * @freq: OPP frequency to modify availability
  444. * @availability_req: availability status requested for this opp
  445. *
  446. * Set the availability of an OPP with an RCU operation, opp_{enable,disable}
  447. * share a common logic which is isolated here.
  448. *
  449. * Returns -EINVAL for bad pointers, -ENOMEM if no memory available for the
  450. * copy operation, returns 0 if no modifcation was done OR modification was
  451. * successful.
  452. *
  453. * Locking: The internal device_opp and opp structures are RCU protected.
  454. * Hence this function internally uses RCU updater strategy with mutex locks to
  455. * keep the integrity of the internal data structures. Callers should ensure
  456. * that this function is *NOT* called under RCU protection or in contexts where
  457. * mutex locking or synchronize_rcu() blocking calls cannot be used.
  458. */
  459. static int opp_set_availability(struct device *dev, unsigned long freq,
  460. bool availability_req)
  461. {
  462. struct device_opp *tmp_dev_opp, *dev_opp = ERR_PTR(-ENODEV);
  463. struct dev_pm_opp *new_opp, *tmp_opp, *opp = ERR_PTR(-ENODEV);
  464. int r = 0;
  465. /* keep the node allocated */
  466. new_opp = kmalloc(sizeof(*new_opp), GFP_KERNEL);
  467. if (!new_opp) {
  468. dev_warn(dev, "%s: Unable to create OPP\n", __func__);
  469. return -ENOMEM;
  470. }
  471. mutex_lock(&dev_opp_list_lock);
  472. /* Find the device_opp */
  473. list_for_each_entry(tmp_dev_opp, &dev_opp_list, node) {
  474. if (dev == tmp_dev_opp->dev) {
  475. dev_opp = tmp_dev_opp;
  476. break;
  477. }
  478. }
  479. if (IS_ERR(dev_opp)) {
  480. r = PTR_ERR(dev_opp);
  481. dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
  482. goto unlock;
  483. }
  484. /* Do we have the frequency? */
  485. list_for_each_entry(tmp_opp, &dev_opp->opp_list, node) {
  486. if (tmp_opp->rate == freq) {
  487. opp = tmp_opp;
  488. break;
  489. }
  490. }
  491. if (IS_ERR(opp)) {
  492. r = PTR_ERR(opp);
  493. goto unlock;
  494. }
  495. /* Is update really needed? */
  496. if (opp->available == availability_req)
  497. goto unlock;
  498. /* copy the old data over */
  499. *new_opp = *opp;
  500. /* plug in new node */
  501. new_opp->available = availability_req;
  502. list_replace_rcu(&opp->node, &new_opp->node);
  503. mutex_unlock(&dev_opp_list_lock);
  504. kfree_rcu(opp, head);
  505. /* Notify the change of the OPP availability */
  506. if (availability_req)
  507. srcu_notifier_call_chain(&dev_opp->head, OPP_EVENT_ENABLE,
  508. new_opp);
  509. else
  510. srcu_notifier_call_chain(&dev_opp->head, OPP_EVENT_DISABLE,
  511. new_opp);
  512. return 0;
  513. unlock:
  514. mutex_unlock(&dev_opp_list_lock);
  515. kfree(new_opp);
  516. return r;
  517. }
  518. /**
  519. * dev_pm_opp_enable() - Enable a specific OPP
  520. * @dev: device for which we do this operation
  521. * @freq: OPP frequency to enable
  522. *
  523. * Enables a provided opp. If the operation is valid, this returns 0, else the
  524. * corresponding error value. It is meant to be used for users an OPP available
  525. * after being temporarily made unavailable with dev_pm_opp_disable.
  526. *
  527. * Locking: The internal device_opp and opp structures are RCU protected.
  528. * Hence this function indirectly uses RCU and mutex locks to keep the
  529. * integrity of the internal data structures. Callers should ensure that
  530. * this function is *NOT* called under RCU protection or in contexts where
  531. * mutex locking or synchronize_rcu() blocking calls cannot be used.
  532. */
  533. int dev_pm_opp_enable(struct device *dev, unsigned long freq)
  534. {
  535. return opp_set_availability(dev, freq, true);
  536. }
  537. EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
  538. /**
  539. * dev_pm_opp_disable() - Disable a specific OPP
  540. * @dev: device for which we do this operation
  541. * @freq: OPP frequency to disable
  542. *
  543. * Disables a provided opp. If the operation is valid, this returns
  544. * 0, else the corresponding error value. It is meant to be a temporary
  545. * control by users to make this OPP not available until the circumstances are
  546. * right to make it available again (with a call to dev_pm_opp_enable).
  547. *
  548. * Locking: The internal device_opp and opp structures are RCU protected.
  549. * Hence this function indirectly uses RCU and mutex locks to keep the
  550. * integrity of the internal data structures. Callers should ensure that
  551. * this function is *NOT* called under RCU protection or in contexts where
  552. * mutex locking or synchronize_rcu() blocking calls cannot be used.
  553. */
  554. int dev_pm_opp_disable(struct device *dev, unsigned long freq)
  555. {
  556. return opp_set_availability(dev, freq, false);
  557. }
  558. EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
  559. /**
  560. * dev_pm_opp_get_notifier() - find notifier_head of the device with opp
  561. * @dev: device pointer used to lookup device OPPs.
  562. */
  563. struct srcu_notifier_head *dev_pm_opp_get_notifier(struct device *dev)
  564. {
  565. struct device_opp *dev_opp = find_device_opp(dev);
  566. if (IS_ERR(dev_opp))
  567. return ERR_CAST(dev_opp); /* matching type */
  568. return &dev_opp->head;
  569. }
  570. #ifdef CONFIG_OF
  571. /**
  572. * of_init_opp_table() - Initialize opp table from device tree
  573. * @dev: device pointer used to lookup device OPPs.
  574. *
  575. * Register the initial OPP table with the OPP library for given device.
  576. */
  577. int of_init_opp_table(struct device *dev)
  578. {
  579. const struct property *prop;
  580. const __be32 *val;
  581. int nr;
  582. prop = of_find_property(dev->of_node, "operating-points", NULL);
  583. if (!prop)
  584. return -ENODEV;
  585. if (!prop->value)
  586. return -ENODATA;
  587. /*
  588. * Each OPP is a set of tuples consisting of frequency and
  589. * voltage like <freq-kHz vol-uV>.
  590. */
  591. nr = prop->length / sizeof(u32);
  592. if (nr % 2) {
  593. dev_err(dev, "%s: Invalid OPP list\n", __func__);
  594. return -EINVAL;
  595. }
  596. val = prop->value;
  597. while (nr) {
  598. unsigned long freq = be32_to_cpup(val++) * 1000;
  599. unsigned long volt = be32_to_cpup(val++);
  600. if (dev_pm_opp_add(dev, freq, volt))
  601. dev_warn(dev, "%s: Failed to add OPP %ld\n",
  602. __func__, freq);
  603. nr -= 2;
  604. }
  605. return 0;
  606. }
  607. EXPORT_SYMBOL_GPL(of_init_opp_table);
  608. #endif