opp.c 22 KB

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