opp.c 21 KB

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