opp.c 20 KB

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