opp.c 23 KB

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