devfreq.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534
  1. /*
  2. * devfreq: Generic Dynamic Voltage and Frequency Scaling (DVFS) Framework
  3. * for Non-CPU Devices.
  4. *
  5. * Copyright (C) 2011 Samsung Electronics
  6. * MyungJoo Ham <myungjoo.ham@samsung.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/sched.h>
  14. #include <linux/errno.h>
  15. #include <linux/err.h>
  16. #include <linux/init.h>
  17. #include <linux/export.h>
  18. #include <linux/slab.h>
  19. #include <linux/stat.h>
  20. #include <linux/pm_opp.h>
  21. #include <linux/devfreq.h>
  22. #include <linux/workqueue.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/list.h>
  25. #include <linux/printk.h>
  26. #include <linux/hrtimer.h>
  27. #include <linux/of.h>
  28. #include "governor.h"
  29. #define MAX(a,b) ((a > b) ? a : b)
  30. #define MIN(a,b) ((a < b) ? a : b)
  31. static struct class *devfreq_class;
  32. /*
  33. * devfreq core provides delayed work based load monitoring helper
  34. * functions. Governors can use these or can implement their own
  35. * monitoring mechanism.
  36. */
  37. static struct workqueue_struct *devfreq_wq;
  38. /* The list of all device-devfreq governors */
  39. static LIST_HEAD(devfreq_governor_list);
  40. /* The list of all device-devfreq */
  41. static LIST_HEAD(devfreq_list);
  42. static DEFINE_MUTEX(devfreq_list_lock);
  43. /**
  44. * find_device_devfreq() - find devfreq struct using device pointer
  45. * @dev: device pointer used to lookup device devfreq.
  46. *
  47. * Search the list of device devfreqs and return the matched device's
  48. * devfreq info. devfreq_list_lock should be held by the caller.
  49. */
  50. static struct devfreq *find_device_devfreq(struct device *dev)
  51. {
  52. struct devfreq *tmp_devfreq;
  53. if (IS_ERR_OR_NULL(dev)) {
  54. pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
  55. return ERR_PTR(-EINVAL);
  56. }
  57. WARN(!mutex_is_locked(&devfreq_list_lock),
  58. "devfreq_list_lock must be locked.");
  59. list_for_each_entry(tmp_devfreq, &devfreq_list, node) {
  60. if (tmp_devfreq->dev.parent == dev)
  61. return tmp_devfreq;
  62. }
  63. return ERR_PTR(-ENODEV);
  64. }
  65. static unsigned long find_available_min_freq(struct devfreq *devfreq)
  66. {
  67. struct dev_pm_opp *opp;
  68. unsigned long min_freq = 0;
  69. opp = dev_pm_opp_find_freq_ceil(devfreq->dev.parent, &min_freq);
  70. if (IS_ERR(opp))
  71. min_freq = 0;
  72. else
  73. dev_pm_opp_put(opp);
  74. return min_freq;
  75. }
  76. static unsigned long find_available_max_freq(struct devfreq *devfreq)
  77. {
  78. struct dev_pm_opp *opp;
  79. unsigned long max_freq = ULONG_MAX;
  80. opp = dev_pm_opp_find_freq_floor(devfreq->dev.parent, &max_freq);
  81. if (IS_ERR(opp))
  82. max_freq = 0;
  83. else
  84. dev_pm_opp_put(opp);
  85. return max_freq;
  86. }
  87. /**
  88. * devfreq_get_freq_level() - Lookup freq_table for the frequency
  89. * @devfreq: the devfreq instance
  90. * @freq: the target frequency
  91. */
  92. static int devfreq_get_freq_level(struct devfreq *devfreq, unsigned long freq)
  93. {
  94. int lev;
  95. for (lev = 0; lev < devfreq->profile->max_state; lev++)
  96. if (freq == devfreq->profile->freq_table[lev])
  97. return lev;
  98. return -EINVAL;
  99. }
  100. static int set_freq_table(struct devfreq *devfreq)
  101. {
  102. struct devfreq_dev_profile *profile = devfreq->profile;
  103. struct dev_pm_opp *opp;
  104. unsigned long freq;
  105. int i, count;
  106. /* Initialize the freq_table from OPP table */
  107. count = dev_pm_opp_get_opp_count(devfreq->dev.parent);
  108. if (count <= 0)
  109. return -EINVAL;
  110. profile->max_state = count;
  111. profile->freq_table = devm_kcalloc(devfreq->dev.parent,
  112. profile->max_state,
  113. sizeof(*profile->freq_table),
  114. GFP_KERNEL);
  115. if (!profile->freq_table) {
  116. profile->max_state = 0;
  117. return -ENOMEM;
  118. }
  119. for (i = 0, freq = 0; i < profile->max_state; i++, freq++) {
  120. opp = dev_pm_opp_find_freq_ceil(devfreq->dev.parent, &freq);
  121. if (IS_ERR(opp)) {
  122. devm_kfree(devfreq->dev.parent, profile->freq_table);
  123. profile->max_state = 0;
  124. return PTR_ERR(opp);
  125. }
  126. dev_pm_opp_put(opp);
  127. profile->freq_table[i] = freq;
  128. }
  129. return 0;
  130. }
  131. /**
  132. * devfreq_update_status() - Update statistics of devfreq behavior
  133. * @devfreq: the devfreq instance
  134. * @freq: the update target frequency
  135. */
  136. int devfreq_update_status(struct devfreq *devfreq, unsigned long freq)
  137. {
  138. int lev, prev_lev, ret = 0;
  139. unsigned long cur_time;
  140. cur_time = jiffies;
  141. /* Immediately exit if previous_freq is not initialized yet. */
  142. if (!devfreq->previous_freq)
  143. goto out;
  144. prev_lev = devfreq_get_freq_level(devfreq, devfreq->previous_freq);
  145. if (prev_lev < 0) {
  146. ret = prev_lev;
  147. goto out;
  148. }
  149. devfreq->time_in_state[prev_lev] +=
  150. cur_time - devfreq->last_stat_updated;
  151. lev = devfreq_get_freq_level(devfreq, freq);
  152. if (lev < 0) {
  153. ret = lev;
  154. goto out;
  155. }
  156. if (lev != prev_lev) {
  157. devfreq->trans_table[(prev_lev *
  158. devfreq->profile->max_state) + lev]++;
  159. devfreq->total_trans++;
  160. }
  161. out:
  162. devfreq->last_stat_updated = cur_time;
  163. return ret;
  164. }
  165. EXPORT_SYMBOL(devfreq_update_status);
  166. /**
  167. * find_devfreq_governor() - find devfreq governor from name
  168. * @name: name of the governor
  169. *
  170. * Search the list of devfreq governors and return the matched
  171. * governor's pointer. devfreq_list_lock should be held by the caller.
  172. */
  173. static struct devfreq_governor *find_devfreq_governor(const char *name)
  174. {
  175. struct devfreq_governor *tmp_governor;
  176. if (IS_ERR_OR_NULL(name)) {
  177. pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
  178. return ERR_PTR(-EINVAL);
  179. }
  180. WARN(!mutex_is_locked(&devfreq_list_lock),
  181. "devfreq_list_lock must be locked.");
  182. list_for_each_entry(tmp_governor, &devfreq_governor_list, node) {
  183. if (!strncmp(tmp_governor->name, name, DEVFREQ_NAME_LEN))
  184. return tmp_governor;
  185. }
  186. return ERR_PTR(-ENODEV);
  187. }
  188. static int devfreq_notify_transition(struct devfreq *devfreq,
  189. struct devfreq_freqs *freqs, unsigned int state)
  190. {
  191. if (!devfreq)
  192. return -EINVAL;
  193. switch (state) {
  194. case DEVFREQ_PRECHANGE:
  195. srcu_notifier_call_chain(&devfreq->transition_notifier_list,
  196. DEVFREQ_PRECHANGE, freqs);
  197. break;
  198. case DEVFREQ_POSTCHANGE:
  199. srcu_notifier_call_chain(&devfreq->transition_notifier_list,
  200. DEVFREQ_POSTCHANGE, freqs);
  201. break;
  202. default:
  203. return -EINVAL;
  204. }
  205. return 0;
  206. }
  207. /* Load monitoring helper functions for governors use */
  208. /**
  209. * update_devfreq() - Reevaluate the device and configure frequency.
  210. * @devfreq: the devfreq instance.
  211. *
  212. * Note: Lock devfreq->lock before calling update_devfreq
  213. * This function is exported for governors.
  214. */
  215. int update_devfreq(struct devfreq *devfreq)
  216. {
  217. struct devfreq_freqs freqs;
  218. unsigned long freq, cur_freq, min_freq, max_freq;
  219. int err = 0;
  220. u32 flags = 0;
  221. if (!mutex_is_locked(&devfreq->lock)) {
  222. WARN(true, "devfreq->lock must be locked by the caller.\n");
  223. return -EINVAL;
  224. }
  225. if (!devfreq->governor)
  226. return -EINVAL;
  227. /* Reevaluate the proper frequency */
  228. err = devfreq->governor->get_target_freq(devfreq, &freq);
  229. if (err)
  230. return err;
  231. /*
  232. * Adjust the frequency with user freq, QoS and available freq.
  233. *
  234. * List from the highest priority
  235. * max_freq
  236. * min_freq
  237. */
  238. max_freq = MIN(devfreq->scaling_max_freq, devfreq->max_freq);
  239. min_freq = MAX(devfreq->scaling_min_freq, devfreq->min_freq);
  240. if (min_freq && freq < min_freq) {
  241. freq = min_freq;
  242. flags &= ~DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use GLB */
  243. }
  244. if (max_freq && freq > max_freq) {
  245. freq = max_freq;
  246. flags |= DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use LUB */
  247. }
  248. if (devfreq->profile->get_cur_freq)
  249. devfreq->profile->get_cur_freq(devfreq->dev.parent, &cur_freq);
  250. else
  251. cur_freq = devfreq->previous_freq;
  252. freqs.old = cur_freq;
  253. freqs.new = freq;
  254. devfreq_notify_transition(devfreq, &freqs, DEVFREQ_PRECHANGE);
  255. err = devfreq->profile->target(devfreq->dev.parent, &freq, flags);
  256. if (err) {
  257. freqs.new = cur_freq;
  258. devfreq_notify_transition(devfreq, &freqs, DEVFREQ_POSTCHANGE);
  259. return err;
  260. }
  261. freqs.new = freq;
  262. devfreq_notify_transition(devfreq, &freqs, DEVFREQ_POSTCHANGE);
  263. if (devfreq_update_status(devfreq, freq))
  264. dev_err(&devfreq->dev,
  265. "Couldn't update frequency transition information.\n");
  266. devfreq->previous_freq = freq;
  267. return err;
  268. }
  269. EXPORT_SYMBOL(update_devfreq);
  270. /**
  271. * devfreq_monitor() - Periodically poll devfreq objects.
  272. * @work: the work struct used to run devfreq_monitor periodically.
  273. *
  274. */
  275. static void devfreq_monitor(struct work_struct *work)
  276. {
  277. int err;
  278. struct devfreq *devfreq = container_of(work,
  279. struct devfreq, work.work);
  280. mutex_lock(&devfreq->lock);
  281. err = update_devfreq(devfreq);
  282. if (err)
  283. dev_err(&devfreq->dev, "dvfs failed with (%d) error\n", err);
  284. queue_delayed_work(devfreq_wq, &devfreq->work,
  285. msecs_to_jiffies(devfreq->profile->polling_ms));
  286. mutex_unlock(&devfreq->lock);
  287. }
  288. /**
  289. * devfreq_monitor_start() - Start load monitoring of devfreq instance
  290. * @devfreq: the devfreq instance.
  291. *
  292. * Helper function for starting devfreq device load monitoing. By
  293. * default delayed work based monitoring is supported. Function
  294. * to be called from governor in response to DEVFREQ_GOV_START
  295. * event when device is added to devfreq framework.
  296. */
  297. void devfreq_monitor_start(struct devfreq *devfreq)
  298. {
  299. INIT_DEFERRABLE_WORK(&devfreq->work, devfreq_monitor);
  300. if (devfreq->profile->polling_ms)
  301. queue_delayed_work(devfreq_wq, &devfreq->work,
  302. msecs_to_jiffies(devfreq->profile->polling_ms));
  303. }
  304. EXPORT_SYMBOL(devfreq_monitor_start);
  305. /**
  306. * devfreq_monitor_stop() - Stop load monitoring of a devfreq instance
  307. * @devfreq: the devfreq instance.
  308. *
  309. * Helper function to stop devfreq device load monitoing. Function
  310. * to be called from governor in response to DEVFREQ_GOV_STOP
  311. * event when device is removed from devfreq framework.
  312. */
  313. void devfreq_monitor_stop(struct devfreq *devfreq)
  314. {
  315. cancel_delayed_work_sync(&devfreq->work);
  316. }
  317. EXPORT_SYMBOL(devfreq_monitor_stop);
  318. /**
  319. * devfreq_monitor_suspend() - Suspend load monitoring of a devfreq instance
  320. * @devfreq: the devfreq instance.
  321. *
  322. * Helper function to suspend devfreq device load monitoing. Function
  323. * to be called from governor in response to DEVFREQ_GOV_SUSPEND
  324. * event or when polling interval is set to zero.
  325. *
  326. * Note: Though this function is same as devfreq_monitor_stop(),
  327. * intentionally kept separate to provide hooks for collecting
  328. * transition statistics.
  329. */
  330. void devfreq_monitor_suspend(struct devfreq *devfreq)
  331. {
  332. mutex_lock(&devfreq->lock);
  333. if (devfreq->stop_polling) {
  334. mutex_unlock(&devfreq->lock);
  335. return;
  336. }
  337. devfreq_update_status(devfreq, devfreq->previous_freq);
  338. devfreq->stop_polling = true;
  339. mutex_unlock(&devfreq->lock);
  340. cancel_delayed_work_sync(&devfreq->work);
  341. }
  342. EXPORT_SYMBOL(devfreq_monitor_suspend);
  343. /**
  344. * devfreq_monitor_resume() - Resume load monitoring of a devfreq instance
  345. * @devfreq: the devfreq instance.
  346. *
  347. * Helper function to resume devfreq device load monitoing. Function
  348. * to be called from governor in response to DEVFREQ_GOV_RESUME
  349. * event or when polling interval is set to non-zero.
  350. */
  351. void devfreq_monitor_resume(struct devfreq *devfreq)
  352. {
  353. unsigned long freq;
  354. mutex_lock(&devfreq->lock);
  355. if (!devfreq->stop_polling)
  356. goto out;
  357. if (!delayed_work_pending(&devfreq->work) &&
  358. devfreq->profile->polling_ms)
  359. queue_delayed_work(devfreq_wq, &devfreq->work,
  360. msecs_to_jiffies(devfreq->profile->polling_ms));
  361. devfreq->last_stat_updated = jiffies;
  362. devfreq->stop_polling = false;
  363. if (devfreq->profile->get_cur_freq &&
  364. !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
  365. devfreq->previous_freq = freq;
  366. out:
  367. mutex_unlock(&devfreq->lock);
  368. }
  369. EXPORT_SYMBOL(devfreq_monitor_resume);
  370. /**
  371. * devfreq_interval_update() - Update device devfreq monitoring interval
  372. * @devfreq: the devfreq instance.
  373. * @delay: new polling interval to be set.
  374. *
  375. * Helper function to set new load monitoring polling interval. Function
  376. * to be called from governor in response to DEVFREQ_GOV_INTERVAL event.
  377. */
  378. void devfreq_interval_update(struct devfreq *devfreq, unsigned int *delay)
  379. {
  380. unsigned int cur_delay = devfreq->profile->polling_ms;
  381. unsigned int new_delay = *delay;
  382. mutex_lock(&devfreq->lock);
  383. devfreq->profile->polling_ms = new_delay;
  384. if (devfreq->stop_polling)
  385. goto out;
  386. /* if new delay is zero, stop polling */
  387. if (!new_delay) {
  388. mutex_unlock(&devfreq->lock);
  389. cancel_delayed_work_sync(&devfreq->work);
  390. return;
  391. }
  392. /* if current delay is zero, start polling with new delay */
  393. if (!cur_delay) {
  394. queue_delayed_work(devfreq_wq, &devfreq->work,
  395. msecs_to_jiffies(devfreq->profile->polling_ms));
  396. goto out;
  397. }
  398. /* if current delay is greater than new delay, restart polling */
  399. if (cur_delay > new_delay) {
  400. mutex_unlock(&devfreq->lock);
  401. cancel_delayed_work_sync(&devfreq->work);
  402. mutex_lock(&devfreq->lock);
  403. if (!devfreq->stop_polling)
  404. queue_delayed_work(devfreq_wq, &devfreq->work,
  405. msecs_to_jiffies(devfreq->profile->polling_ms));
  406. }
  407. out:
  408. mutex_unlock(&devfreq->lock);
  409. }
  410. EXPORT_SYMBOL(devfreq_interval_update);
  411. /**
  412. * devfreq_notifier_call() - Notify that the device frequency requirements
  413. * has been changed out of devfreq framework.
  414. * @nb: the notifier_block (supposed to be devfreq->nb)
  415. * @type: not used
  416. * @devp: not used
  417. *
  418. * Called by a notifier that uses devfreq->nb.
  419. */
  420. static int devfreq_notifier_call(struct notifier_block *nb, unsigned long type,
  421. void *devp)
  422. {
  423. struct devfreq *devfreq = container_of(nb, struct devfreq, nb);
  424. int ret;
  425. mutex_lock(&devfreq->lock);
  426. devfreq->scaling_min_freq = find_available_min_freq(devfreq);
  427. if (!devfreq->scaling_min_freq) {
  428. mutex_unlock(&devfreq->lock);
  429. return -EINVAL;
  430. }
  431. devfreq->scaling_max_freq = find_available_max_freq(devfreq);
  432. if (!devfreq->scaling_max_freq) {
  433. mutex_unlock(&devfreq->lock);
  434. return -EINVAL;
  435. }
  436. ret = update_devfreq(devfreq);
  437. mutex_unlock(&devfreq->lock);
  438. return ret;
  439. }
  440. /**
  441. * devfreq_dev_release() - Callback for struct device to release the device.
  442. * @dev: the devfreq device
  443. *
  444. * Remove devfreq from the list and release its resources.
  445. */
  446. static void devfreq_dev_release(struct device *dev)
  447. {
  448. struct devfreq *devfreq = to_devfreq(dev);
  449. mutex_lock(&devfreq_list_lock);
  450. if (IS_ERR(find_device_devfreq(devfreq->dev.parent))) {
  451. mutex_unlock(&devfreq_list_lock);
  452. dev_warn(&devfreq->dev, "releasing devfreq which doesn't exist\n");
  453. return;
  454. }
  455. list_del(&devfreq->node);
  456. mutex_unlock(&devfreq_list_lock);
  457. if (devfreq->governor)
  458. devfreq->governor->event_handler(devfreq,
  459. DEVFREQ_GOV_STOP, NULL);
  460. if (devfreq->profile->exit)
  461. devfreq->profile->exit(devfreq->dev.parent);
  462. mutex_destroy(&devfreq->lock);
  463. kfree(devfreq);
  464. }
  465. /**
  466. * devfreq_add_device() - Add devfreq feature to the device
  467. * @dev: the device to add devfreq feature.
  468. * @profile: device-specific profile to run devfreq.
  469. * @governor_name: name of the policy to choose frequency.
  470. * @data: private data for the governor. The devfreq framework does not
  471. * touch this value.
  472. */
  473. struct devfreq *devfreq_add_device(struct device *dev,
  474. struct devfreq_dev_profile *profile,
  475. const char *governor_name,
  476. void *data)
  477. {
  478. struct devfreq *devfreq;
  479. struct devfreq_governor *governor;
  480. static atomic_t devfreq_no = ATOMIC_INIT(-1);
  481. int err = 0;
  482. if (!dev || !profile || !governor_name) {
  483. dev_err(dev, "%s: Invalid parameters.\n", __func__);
  484. return ERR_PTR(-EINVAL);
  485. }
  486. mutex_lock(&devfreq_list_lock);
  487. devfreq = find_device_devfreq(dev);
  488. mutex_unlock(&devfreq_list_lock);
  489. if (!IS_ERR(devfreq)) {
  490. dev_err(dev, "%s: Unable to create devfreq for the device.\n",
  491. __func__);
  492. err = -EINVAL;
  493. goto err_out;
  494. }
  495. devfreq = kzalloc(sizeof(struct devfreq), GFP_KERNEL);
  496. if (!devfreq) {
  497. err = -ENOMEM;
  498. goto err_out;
  499. }
  500. mutex_init(&devfreq->lock);
  501. mutex_lock(&devfreq->lock);
  502. devfreq->dev.parent = dev;
  503. devfreq->dev.class = devfreq_class;
  504. devfreq->dev.release = devfreq_dev_release;
  505. devfreq->profile = profile;
  506. strncpy(devfreq->governor_name, governor_name, DEVFREQ_NAME_LEN);
  507. devfreq->previous_freq = profile->initial_freq;
  508. devfreq->last_status.current_frequency = profile->initial_freq;
  509. devfreq->data = data;
  510. devfreq->nb.notifier_call = devfreq_notifier_call;
  511. if (!devfreq->profile->max_state && !devfreq->profile->freq_table) {
  512. mutex_unlock(&devfreq->lock);
  513. err = set_freq_table(devfreq);
  514. if (err < 0)
  515. goto err_out;
  516. mutex_lock(&devfreq->lock);
  517. }
  518. devfreq->min_freq = find_available_min_freq(devfreq);
  519. if (!devfreq->min_freq) {
  520. mutex_unlock(&devfreq->lock);
  521. err = -EINVAL;
  522. goto err_dev;
  523. }
  524. devfreq->scaling_min_freq = devfreq->min_freq;
  525. devfreq->max_freq = find_available_max_freq(devfreq);
  526. if (!devfreq->max_freq) {
  527. mutex_unlock(&devfreq->lock);
  528. err = -EINVAL;
  529. goto err_dev;
  530. }
  531. devfreq->scaling_max_freq = devfreq->max_freq;
  532. dev_set_name(&devfreq->dev, "devfreq%d",
  533. atomic_inc_return(&devfreq_no));
  534. err = device_register(&devfreq->dev);
  535. if (err) {
  536. mutex_unlock(&devfreq->lock);
  537. goto err_dev;
  538. }
  539. devfreq->trans_table = devm_kzalloc(&devfreq->dev,
  540. sizeof(unsigned int) *
  541. devfreq->profile->max_state *
  542. devfreq->profile->max_state,
  543. GFP_KERNEL);
  544. devfreq->time_in_state = devm_kzalloc(&devfreq->dev,
  545. sizeof(unsigned long) *
  546. devfreq->profile->max_state,
  547. GFP_KERNEL);
  548. devfreq->last_stat_updated = jiffies;
  549. srcu_init_notifier_head(&devfreq->transition_notifier_list);
  550. mutex_unlock(&devfreq->lock);
  551. mutex_lock(&devfreq_list_lock);
  552. list_add(&devfreq->node, &devfreq_list);
  553. governor = find_devfreq_governor(devfreq->governor_name);
  554. if (IS_ERR(governor)) {
  555. dev_err(dev, "%s: Unable to find governor for the device\n",
  556. __func__);
  557. err = PTR_ERR(governor);
  558. goto err_init;
  559. }
  560. devfreq->governor = governor;
  561. err = devfreq->governor->event_handler(devfreq, DEVFREQ_GOV_START,
  562. NULL);
  563. if (err) {
  564. dev_err(dev, "%s: Unable to start governor for the device\n",
  565. __func__);
  566. goto err_init;
  567. }
  568. mutex_unlock(&devfreq_list_lock);
  569. return devfreq;
  570. err_init:
  571. list_del(&devfreq->node);
  572. mutex_unlock(&devfreq_list_lock);
  573. device_unregister(&devfreq->dev);
  574. err_dev:
  575. if (devfreq)
  576. kfree(devfreq);
  577. err_out:
  578. return ERR_PTR(err);
  579. }
  580. EXPORT_SYMBOL(devfreq_add_device);
  581. /**
  582. * devfreq_remove_device() - Remove devfreq feature from a device.
  583. * @devfreq: the devfreq instance to be removed
  584. *
  585. * The opposite of devfreq_add_device().
  586. */
  587. int devfreq_remove_device(struct devfreq *devfreq)
  588. {
  589. if (!devfreq)
  590. return -EINVAL;
  591. device_unregister(&devfreq->dev);
  592. return 0;
  593. }
  594. EXPORT_SYMBOL(devfreq_remove_device);
  595. static int devm_devfreq_dev_match(struct device *dev, void *res, void *data)
  596. {
  597. struct devfreq **r = res;
  598. if (WARN_ON(!r || !*r))
  599. return 0;
  600. return *r == data;
  601. }
  602. static void devm_devfreq_dev_release(struct device *dev, void *res)
  603. {
  604. devfreq_remove_device(*(struct devfreq **)res);
  605. }
  606. /**
  607. * devm_devfreq_add_device() - Resource-managed devfreq_add_device()
  608. * @dev: the device to add devfreq feature.
  609. * @profile: device-specific profile to run devfreq.
  610. * @governor_name: name of the policy to choose frequency.
  611. * @data: private data for the governor. The devfreq framework does not
  612. * touch this value.
  613. *
  614. * This function manages automatically the memory of devfreq device using device
  615. * resource management and simplify the free operation for memory of devfreq
  616. * device.
  617. */
  618. struct devfreq *devm_devfreq_add_device(struct device *dev,
  619. struct devfreq_dev_profile *profile,
  620. const char *governor_name,
  621. void *data)
  622. {
  623. struct devfreq **ptr, *devfreq;
  624. ptr = devres_alloc(devm_devfreq_dev_release, sizeof(*ptr), GFP_KERNEL);
  625. if (!ptr)
  626. return ERR_PTR(-ENOMEM);
  627. devfreq = devfreq_add_device(dev, profile, governor_name, data);
  628. if (IS_ERR(devfreq)) {
  629. devres_free(ptr);
  630. return devfreq;
  631. }
  632. *ptr = devfreq;
  633. devres_add(dev, ptr);
  634. return devfreq;
  635. }
  636. EXPORT_SYMBOL(devm_devfreq_add_device);
  637. #ifdef CONFIG_OF
  638. /*
  639. * devfreq_get_devfreq_by_phandle - Get the devfreq device from devicetree
  640. * @dev - instance to the given device
  641. * @index - index into list of devfreq
  642. *
  643. * return the instance of devfreq device
  644. */
  645. struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, int index)
  646. {
  647. struct device_node *node;
  648. struct devfreq *devfreq;
  649. if (!dev)
  650. return ERR_PTR(-EINVAL);
  651. if (!dev->of_node)
  652. return ERR_PTR(-EINVAL);
  653. node = of_parse_phandle(dev->of_node, "devfreq", index);
  654. if (!node)
  655. return ERR_PTR(-ENODEV);
  656. mutex_lock(&devfreq_list_lock);
  657. list_for_each_entry(devfreq, &devfreq_list, node) {
  658. if (devfreq->dev.parent
  659. && devfreq->dev.parent->of_node == node) {
  660. mutex_unlock(&devfreq_list_lock);
  661. of_node_put(node);
  662. return devfreq;
  663. }
  664. }
  665. mutex_unlock(&devfreq_list_lock);
  666. of_node_put(node);
  667. return ERR_PTR(-EPROBE_DEFER);
  668. }
  669. #else
  670. struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, int index)
  671. {
  672. return ERR_PTR(-ENODEV);
  673. }
  674. #endif /* CONFIG_OF */
  675. EXPORT_SYMBOL_GPL(devfreq_get_devfreq_by_phandle);
  676. /**
  677. * devm_devfreq_remove_device() - Resource-managed devfreq_remove_device()
  678. * @dev: the device to add devfreq feature.
  679. * @devfreq: the devfreq instance to be removed
  680. */
  681. void devm_devfreq_remove_device(struct device *dev, struct devfreq *devfreq)
  682. {
  683. WARN_ON(devres_release(dev, devm_devfreq_dev_release,
  684. devm_devfreq_dev_match, devfreq));
  685. }
  686. EXPORT_SYMBOL(devm_devfreq_remove_device);
  687. /**
  688. * devfreq_suspend_device() - Suspend devfreq of a device.
  689. * @devfreq: the devfreq instance to be suspended
  690. *
  691. * This function is intended to be called by the pm callbacks
  692. * (e.g., runtime_suspend, suspend) of the device driver that
  693. * holds the devfreq.
  694. */
  695. int devfreq_suspend_device(struct devfreq *devfreq)
  696. {
  697. if (!devfreq)
  698. return -EINVAL;
  699. if (!devfreq->governor)
  700. return 0;
  701. return devfreq->governor->event_handler(devfreq,
  702. DEVFREQ_GOV_SUSPEND, NULL);
  703. }
  704. EXPORT_SYMBOL(devfreq_suspend_device);
  705. /**
  706. * devfreq_resume_device() - Resume devfreq of a device.
  707. * @devfreq: the devfreq instance to be resumed
  708. *
  709. * This function is intended to be called by the pm callbacks
  710. * (e.g., runtime_resume, resume) of the device driver that
  711. * holds the devfreq.
  712. */
  713. int devfreq_resume_device(struct devfreq *devfreq)
  714. {
  715. if (!devfreq)
  716. return -EINVAL;
  717. if (!devfreq->governor)
  718. return 0;
  719. return devfreq->governor->event_handler(devfreq,
  720. DEVFREQ_GOV_RESUME, NULL);
  721. }
  722. EXPORT_SYMBOL(devfreq_resume_device);
  723. /**
  724. * devfreq_add_governor() - Add devfreq governor
  725. * @governor: the devfreq governor to be added
  726. */
  727. int devfreq_add_governor(struct devfreq_governor *governor)
  728. {
  729. struct devfreq_governor *g;
  730. struct devfreq *devfreq;
  731. int err = 0;
  732. if (!governor) {
  733. pr_err("%s: Invalid parameters.\n", __func__);
  734. return -EINVAL;
  735. }
  736. mutex_lock(&devfreq_list_lock);
  737. g = find_devfreq_governor(governor->name);
  738. if (!IS_ERR(g)) {
  739. pr_err("%s: governor %s already registered\n", __func__,
  740. g->name);
  741. err = -EINVAL;
  742. goto err_out;
  743. }
  744. list_add(&governor->node, &devfreq_governor_list);
  745. list_for_each_entry(devfreq, &devfreq_list, node) {
  746. int ret = 0;
  747. struct device *dev = devfreq->dev.parent;
  748. if (!strncmp(devfreq->governor_name, governor->name,
  749. DEVFREQ_NAME_LEN)) {
  750. /* The following should never occur */
  751. if (devfreq->governor) {
  752. dev_warn(dev,
  753. "%s: Governor %s already present\n",
  754. __func__, devfreq->governor->name);
  755. ret = devfreq->governor->event_handler(devfreq,
  756. DEVFREQ_GOV_STOP, NULL);
  757. if (ret) {
  758. dev_warn(dev,
  759. "%s: Governor %s stop = %d\n",
  760. __func__,
  761. devfreq->governor->name, ret);
  762. }
  763. /* Fall through */
  764. }
  765. devfreq->governor = governor;
  766. ret = devfreq->governor->event_handler(devfreq,
  767. DEVFREQ_GOV_START, NULL);
  768. if (ret) {
  769. dev_warn(dev, "%s: Governor %s start=%d\n",
  770. __func__, devfreq->governor->name,
  771. ret);
  772. }
  773. }
  774. }
  775. err_out:
  776. mutex_unlock(&devfreq_list_lock);
  777. return err;
  778. }
  779. EXPORT_SYMBOL(devfreq_add_governor);
  780. /**
  781. * devfreq_remove_governor() - Remove devfreq feature from a device.
  782. * @governor: the devfreq governor to be removed
  783. */
  784. int devfreq_remove_governor(struct devfreq_governor *governor)
  785. {
  786. struct devfreq_governor *g;
  787. struct devfreq *devfreq;
  788. int err = 0;
  789. if (!governor) {
  790. pr_err("%s: Invalid parameters.\n", __func__);
  791. return -EINVAL;
  792. }
  793. mutex_lock(&devfreq_list_lock);
  794. g = find_devfreq_governor(governor->name);
  795. if (IS_ERR(g)) {
  796. pr_err("%s: governor %s not registered\n", __func__,
  797. governor->name);
  798. err = PTR_ERR(g);
  799. goto err_out;
  800. }
  801. list_for_each_entry(devfreq, &devfreq_list, node) {
  802. int ret;
  803. struct device *dev = devfreq->dev.parent;
  804. if (!strncmp(devfreq->governor_name, governor->name,
  805. DEVFREQ_NAME_LEN)) {
  806. /* we should have a devfreq governor! */
  807. if (!devfreq->governor) {
  808. dev_warn(dev, "%s: Governor %s NOT present\n",
  809. __func__, governor->name);
  810. continue;
  811. /* Fall through */
  812. }
  813. ret = devfreq->governor->event_handler(devfreq,
  814. DEVFREQ_GOV_STOP, NULL);
  815. if (ret) {
  816. dev_warn(dev, "%s: Governor %s stop=%d\n",
  817. __func__, devfreq->governor->name,
  818. ret);
  819. }
  820. devfreq->governor = NULL;
  821. }
  822. }
  823. list_del(&governor->node);
  824. err_out:
  825. mutex_unlock(&devfreq_list_lock);
  826. return err;
  827. }
  828. EXPORT_SYMBOL(devfreq_remove_governor);
  829. static ssize_t governor_show(struct device *dev,
  830. struct device_attribute *attr, char *buf)
  831. {
  832. if (!to_devfreq(dev)->governor)
  833. return -EINVAL;
  834. return sprintf(buf, "%s\n", to_devfreq(dev)->governor->name);
  835. }
  836. static ssize_t governor_store(struct device *dev, struct device_attribute *attr,
  837. const char *buf, size_t count)
  838. {
  839. struct devfreq *df = to_devfreq(dev);
  840. int ret;
  841. char str_governor[DEVFREQ_NAME_LEN + 1];
  842. struct devfreq_governor *governor;
  843. ret = sscanf(buf, "%" __stringify(DEVFREQ_NAME_LEN) "s", str_governor);
  844. if (ret != 1)
  845. return -EINVAL;
  846. mutex_lock(&devfreq_list_lock);
  847. governor = find_devfreq_governor(str_governor);
  848. if (IS_ERR(governor)) {
  849. ret = PTR_ERR(governor);
  850. goto out;
  851. }
  852. if (df->governor == governor) {
  853. ret = 0;
  854. goto out;
  855. } else if ((df->governor && df->governor->immutable) ||
  856. governor->immutable) {
  857. ret = -EINVAL;
  858. goto out;
  859. }
  860. if (df->governor) {
  861. ret = df->governor->event_handler(df, DEVFREQ_GOV_STOP, NULL);
  862. if (ret) {
  863. dev_warn(dev, "%s: Governor %s not stopped(%d)\n",
  864. __func__, df->governor->name, ret);
  865. goto out;
  866. }
  867. }
  868. df->governor = governor;
  869. strncpy(df->governor_name, governor->name, DEVFREQ_NAME_LEN);
  870. ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
  871. if (ret)
  872. dev_warn(dev, "%s: Governor %s not started(%d)\n",
  873. __func__, df->governor->name, ret);
  874. out:
  875. mutex_unlock(&devfreq_list_lock);
  876. if (!ret)
  877. ret = count;
  878. return ret;
  879. }
  880. static DEVICE_ATTR_RW(governor);
  881. static ssize_t available_governors_show(struct device *d,
  882. struct device_attribute *attr,
  883. char *buf)
  884. {
  885. struct devfreq *df = to_devfreq(d);
  886. ssize_t count = 0;
  887. mutex_lock(&devfreq_list_lock);
  888. /*
  889. * The devfreq with immutable governor (e.g., passive) shows
  890. * only own governor.
  891. */
  892. if (df->governor->immutable) {
  893. count = scnprintf(&buf[count], DEVFREQ_NAME_LEN,
  894. "%s ", df->governor_name);
  895. /*
  896. * The devfreq device shows the registered governor except for
  897. * immutable governors such as passive governor .
  898. */
  899. } else {
  900. struct devfreq_governor *governor;
  901. list_for_each_entry(governor, &devfreq_governor_list, node) {
  902. if (governor->immutable)
  903. continue;
  904. count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
  905. "%s ", governor->name);
  906. }
  907. }
  908. mutex_unlock(&devfreq_list_lock);
  909. /* Truncate the trailing space */
  910. if (count)
  911. count--;
  912. count += sprintf(&buf[count], "\n");
  913. return count;
  914. }
  915. static DEVICE_ATTR_RO(available_governors);
  916. static ssize_t cur_freq_show(struct device *dev, struct device_attribute *attr,
  917. char *buf)
  918. {
  919. unsigned long freq;
  920. struct devfreq *devfreq = to_devfreq(dev);
  921. if (devfreq->profile->get_cur_freq &&
  922. !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
  923. return sprintf(buf, "%lu\n", freq);
  924. return sprintf(buf, "%lu\n", devfreq->previous_freq);
  925. }
  926. static DEVICE_ATTR_RO(cur_freq);
  927. static ssize_t target_freq_show(struct device *dev,
  928. struct device_attribute *attr, char *buf)
  929. {
  930. return sprintf(buf, "%lu\n", to_devfreq(dev)->previous_freq);
  931. }
  932. static DEVICE_ATTR_RO(target_freq);
  933. static ssize_t polling_interval_show(struct device *dev,
  934. struct device_attribute *attr, char *buf)
  935. {
  936. return sprintf(buf, "%d\n", to_devfreq(dev)->profile->polling_ms);
  937. }
  938. static ssize_t polling_interval_store(struct device *dev,
  939. struct device_attribute *attr,
  940. const char *buf, size_t count)
  941. {
  942. struct devfreq *df = to_devfreq(dev);
  943. unsigned int value;
  944. int ret;
  945. if (!df->governor)
  946. return -EINVAL;
  947. ret = sscanf(buf, "%u", &value);
  948. if (ret != 1)
  949. return -EINVAL;
  950. df->governor->event_handler(df, DEVFREQ_GOV_INTERVAL, &value);
  951. ret = count;
  952. return ret;
  953. }
  954. static DEVICE_ATTR_RW(polling_interval);
  955. static ssize_t min_freq_store(struct device *dev, struct device_attribute *attr,
  956. const char *buf, size_t count)
  957. {
  958. struct devfreq *df = to_devfreq(dev);
  959. unsigned long value;
  960. int ret;
  961. unsigned long max;
  962. ret = sscanf(buf, "%lu", &value);
  963. if (ret != 1)
  964. return -EINVAL;
  965. mutex_lock(&df->lock);
  966. max = df->max_freq;
  967. if (value && max && value > max) {
  968. ret = -EINVAL;
  969. goto unlock;
  970. }
  971. df->min_freq = value;
  972. update_devfreq(df);
  973. ret = count;
  974. unlock:
  975. mutex_unlock(&df->lock);
  976. return ret;
  977. }
  978. static ssize_t min_freq_show(struct device *dev, struct device_attribute *attr,
  979. char *buf)
  980. {
  981. struct devfreq *df = to_devfreq(dev);
  982. return sprintf(buf, "%lu\n", MAX(df->scaling_min_freq, df->min_freq));
  983. }
  984. static ssize_t max_freq_store(struct device *dev, struct device_attribute *attr,
  985. const char *buf, size_t count)
  986. {
  987. struct devfreq *df = to_devfreq(dev);
  988. unsigned long value;
  989. int ret;
  990. unsigned long min;
  991. ret = sscanf(buf, "%lu", &value);
  992. if (ret != 1)
  993. return -EINVAL;
  994. mutex_lock(&df->lock);
  995. min = df->min_freq;
  996. if (value && min && value < min) {
  997. ret = -EINVAL;
  998. goto unlock;
  999. }
  1000. df->max_freq = value;
  1001. update_devfreq(df);
  1002. ret = count;
  1003. unlock:
  1004. mutex_unlock(&df->lock);
  1005. return ret;
  1006. }
  1007. static DEVICE_ATTR_RW(min_freq);
  1008. static ssize_t max_freq_show(struct device *dev, struct device_attribute *attr,
  1009. char *buf)
  1010. {
  1011. struct devfreq *df = to_devfreq(dev);
  1012. return sprintf(buf, "%lu\n", MIN(df->scaling_max_freq, df->max_freq));
  1013. }
  1014. static DEVICE_ATTR_RW(max_freq);
  1015. static ssize_t available_frequencies_show(struct device *d,
  1016. struct device_attribute *attr,
  1017. char *buf)
  1018. {
  1019. struct devfreq *df = to_devfreq(d);
  1020. ssize_t count = 0;
  1021. int i;
  1022. mutex_lock(&df->lock);
  1023. for (i = 0; i < df->profile->max_state; i++)
  1024. count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
  1025. "%lu ", df->profile->freq_table[i]);
  1026. mutex_unlock(&df->lock);
  1027. /* Truncate the trailing space */
  1028. if (count)
  1029. count--;
  1030. count += sprintf(&buf[count], "\n");
  1031. return count;
  1032. }
  1033. static DEVICE_ATTR_RO(available_frequencies);
  1034. static ssize_t trans_stat_show(struct device *dev,
  1035. struct device_attribute *attr, char *buf)
  1036. {
  1037. struct devfreq *devfreq = to_devfreq(dev);
  1038. ssize_t len;
  1039. int i, j;
  1040. unsigned int max_state = devfreq->profile->max_state;
  1041. if (!devfreq->stop_polling &&
  1042. devfreq_update_status(devfreq, devfreq->previous_freq))
  1043. return 0;
  1044. if (max_state == 0)
  1045. return sprintf(buf, "Not Supported.\n");
  1046. len = sprintf(buf, " From : To\n");
  1047. len += sprintf(buf + len, " :");
  1048. for (i = 0; i < max_state; i++)
  1049. len += sprintf(buf + len, "%10lu",
  1050. devfreq->profile->freq_table[i]);
  1051. len += sprintf(buf + len, " time(ms)\n");
  1052. for (i = 0; i < max_state; i++) {
  1053. if (devfreq->profile->freq_table[i]
  1054. == devfreq->previous_freq) {
  1055. len += sprintf(buf + len, "*");
  1056. } else {
  1057. len += sprintf(buf + len, " ");
  1058. }
  1059. len += sprintf(buf + len, "%10lu:",
  1060. devfreq->profile->freq_table[i]);
  1061. for (j = 0; j < max_state; j++)
  1062. len += sprintf(buf + len, "%10u",
  1063. devfreq->trans_table[(i * max_state) + j]);
  1064. len += sprintf(buf + len, "%10u\n",
  1065. jiffies_to_msecs(devfreq->time_in_state[i]));
  1066. }
  1067. len += sprintf(buf + len, "Total transition : %u\n",
  1068. devfreq->total_trans);
  1069. return len;
  1070. }
  1071. static DEVICE_ATTR_RO(trans_stat);
  1072. static struct attribute *devfreq_attrs[] = {
  1073. &dev_attr_governor.attr,
  1074. &dev_attr_available_governors.attr,
  1075. &dev_attr_cur_freq.attr,
  1076. &dev_attr_available_frequencies.attr,
  1077. &dev_attr_target_freq.attr,
  1078. &dev_attr_polling_interval.attr,
  1079. &dev_attr_min_freq.attr,
  1080. &dev_attr_max_freq.attr,
  1081. &dev_attr_trans_stat.attr,
  1082. NULL,
  1083. };
  1084. ATTRIBUTE_GROUPS(devfreq);
  1085. static int __init devfreq_init(void)
  1086. {
  1087. devfreq_class = class_create(THIS_MODULE, "devfreq");
  1088. if (IS_ERR(devfreq_class)) {
  1089. pr_err("%s: couldn't create class\n", __FILE__);
  1090. return PTR_ERR(devfreq_class);
  1091. }
  1092. devfreq_wq = create_freezable_workqueue("devfreq_wq");
  1093. if (!devfreq_wq) {
  1094. class_destroy(devfreq_class);
  1095. pr_err("%s: couldn't create workqueue\n", __FILE__);
  1096. return -ENOMEM;
  1097. }
  1098. devfreq_class->dev_groups = devfreq_groups;
  1099. return 0;
  1100. }
  1101. subsys_initcall(devfreq_init);
  1102. /*
  1103. * The following are helper functions for devfreq user device drivers with
  1104. * OPP framework.
  1105. */
  1106. /**
  1107. * devfreq_recommended_opp() - Helper function to get proper OPP for the
  1108. * freq value given to target callback.
  1109. * @dev: The devfreq user device. (parent of devfreq)
  1110. * @freq: The frequency given to target function
  1111. * @flags: Flags handed from devfreq framework.
  1112. *
  1113. * The callers are required to call dev_pm_opp_put() for the returned OPP after
  1114. * use.
  1115. */
  1116. struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
  1117. unsigned long *freq,
  1118. u32 flags)
  1119. {
  1120. struct dev_pm_opp *opp;
  1121. if (flags & DEVFREQ_FLAG_LEAST_UPPER_BOUND) {
  1122. /* The freq is an upper bound. opp should be lower */
  1123. opp = dev_pm_opp_find_freq_floor(dev, freq);
  1124. /* If not available, use the closest opp */
  1125. if (opp == ERR_PTR(-ERANGE))
  1126. opp = dev_pm_opp_find_freq_ceil(dev, freq);
  1127. } else {
  1128. /* The freq is an lower bound. opp should be higher */
  1129. opp = dev_pm_opp_find_freq_ceil(dev, freq);
  1130. /* If not available, use the closest opp */
  1131. if (opp == ERR_PTR(-ERANGE))
  1132. opp = dev_pm_opp_find_freq_floor(dev, freq);
  1133. }
  1134. return opp;
  1135. }
  1136. EXPORT_SYMBOL(devfreq_recommended_opp);
  1137. /**
  1138. * devfreq_register_opp_notifier() - Helper function to get devfreq notified
  1139. * for any changes in the OPP availability
  1140. * changes
  1141. * @dev: The devfreq user device. (parent of devfreq)
  1142. * @devfreq: The devfreq object.
  1143. */
  1144. int devfreq_register_opp_notifier(struct device *dev, struct devfreq *devfreq)
  1145. {
  1146. return dev_pm_opp_register_notifier(dev, &devfreq->nb);
  1147. }
  1148. EXPORT_SYMBOL(devfreq_register_opp_notifier);
  1149. /**
  1150. * devfreq_unregister_opp_notifier() - Helper function to stop getting devfreq
  1151. * notified for any changes in the OPP
  1152. * availability changes anymore.
  1153. * @dev: The devfreq user device. (parent of devfreq)
  1154. * @devfreq: The devfreq object.
  1155. *
  1156. * At exit() callback of devfreq_dev_profile, this must be included if
  1157. * devfreq_recommended_opp is used.
  1158. */
  1159. int devfreq_unregister_opp_notifier(struct device *dev, struct devfreq *devfreq)
  1160. {
  1161. return dev_pm_opp_unregister_notifier(dev, &devfreq->nb);
  1162. }
  1163. EXPORT_SYMBOL(devfreq_unregister_opp_notifier);
  1164. static void devm_devfreq_opp_release(struct device *dev, void *res)
  1165. {
  1166. devfreq_unregister_opp_notifier(dev, *(struct devfreq **)res);
  1167. }
  1168. /**
  1169. * devm_ devfreq_register_opp_notifier()
  1170. * - Resource-managed devfreq_register_opp_notifier()
  1171. * @dev: The devfreq user device. (parent of devfreq)
  1172. * @devfreq: The devfreq object.
  1173. */
  1174. int devm_devfreq_register_opp_notifier(struct device *dev,
  1175. struct devfreq *devfreq)
  1176. {
  1177. struct devfreq **ptr;
  1178. int ret;
  1179. ptr = devres_alloc(devm_devfreq_opp_release, sizeof(*ptr), GFP_KERNEL);
  1180. if (!ptr)
  1181. return -ENOMEM;
  1182. ret = devfreq_register_opp_notifier(dev, devfreq);
  1183. if (ret) {
  1184. devres_free(ptr);
  1185. return ret;
  1186. }
  1187. *ptr = devfreq;
  1188. devres_add(dev, ptr);
  1189. return 0;
  1190. }
  1191. EXPORT_SYMBOL(devm_devfreq_register_opp_notifier);
  1192. /**
  1193. * devm_devfreq_unregister_opp_notifier()
  1194. * - Resource-managed devfreq_unregister_opp_notifier()
  1195. * @dev: The devfreq user device. (parent of devfreq)
  1196. * @devfreq: The devfreq object.
  1197. */
  1198. void devm_devfreq_unregister_opp_notifier(struct device *dev,
  1199. struct devfreq *devfreq)
  1200. {
  1201. WARN_ON(devres_release(dev, devm_devfreq_opp_release,
  1202. devm_devfreq_dev_match, devfreq));
  1203. }
  1204. EXPORT_SYMBOL(devm_devfreq_unregister_opp_notifier);
  1205. /**
  1206. * devfreq_register_notifier() - Register a driver with devfreq
  1207. * @devfreq: The devfreq object.
  1208. * @nb: The notifier block to register.
  1209. * @list: DEVFREQ_TRANSITION_NOTIFIER.
  1210. */
  1211. int devfreq_register_notifier(struct devfreq *devfreq,
  1212. struct notifier_block *nb,
  1213. unsigned int list)
  1214. {
  1215. int ret = 0;
  1216. if (!devfreq)
  1217. return -EINVAL;
  1218. switch (list) {
  1219. case DEVFREQ_TRANSITION_NOTIFIER:
  1220. ret = srcu_notifier_chain_register(
  1221. &devfreq->transition_notifier_list, nb);
  1222. break;
  1223. default:
  1224. ret = -EINVAL;
  1225. }
  1226. return ret;
  1227. }
  1228. EXPORT_SYMBOL(devfreq_register_notifier);
  1229. /*
  1230. * devfreq_unregister_notifier() - Unregister a driver with devfreq
  1231. * @devfreq: The devfreq object.
  1232. * @nb: The notifier block to be unregistered.
  1233. * @list: DEVFREQ_TRANSITION_NOTIFIER.
  1234. */
  1235. int devfreq_unregister_notifier(struct devfreq *devfreq,
  1236. struct notifier_block *nb,
  1237. unsigned int list)
  1238. {
  1239. int ret = 0;
  1240. if (!devfreq)
  1241. return -EINVAL;
  1242. switch (list) {
  1243. case DEVFREQ_TRANSITION_NOTIFIER:
  1244. ret = srcu_notifier_chain_unregister(
  1245. &devfreq->transition_notifier_list, nb);
  1246. break;
  1247. default:
  1248. ret = -EINVAL;
  1249. }
  1250. return ret;
  1251. }
  1252. EXPORT_SYMBOL(devfreq_unregister_notifier);
  1253. struct devfreq_notifier_devres {
  1254. struct devfreq *devfreq;
  1255. struct notifier_block *nb;
  1256. unsigned int list;
  1257. };
  1258. static void devm_devfreq_notifier_release(struct device *dev, void *res)
  1259. {
  1260. struct devfreq_notifier_devres *this = res;
  1261. devfreq_unregister_notifier(this->devfreq, this->nb, this->list);
  1262. }
  1263. /**
  1264. * devm_devfreq_register_notifier()
  1265. - Resource-managed devfreq_register_notifier()
  1266. * @dev: The devfreq user device. (parent of devfreq)
  1267. * @devfreq: The devfreq object.
  1268. * @nb: The notifier block to be unregistered.
  1269. * @list: DEVFREQ_TRANSITION_NOTIFIER.
  1270. */
  1271. int devm_devfreq_register_notifier(struct device *dev,
  1272. struct devfreq *devfreq,
  1273. struct notifier_block *nb,
  1274. unsigned int list)
  1275. {
  1276. struct devfreq_notifier_devres *ptr;
  1277. int ret;
  1278. ptr = devres_alloc(devm_devfreq_notifier_release, sizeof(*ptr),
  1279. GFP_KERNEL);
  1280. if (!ptr)
  1281. return -ENOMEM;
  1282. ret = devfreq_register_notifier(devfreq, nb, list);
  1283. if (ret) {
  1284. devres_free(ptr);
  1285. return ret;
  1286. }
  1287. ptr->devfreq = devfreq;
  1288. ptr->nb = nb;
  1289. ptr->list = list;
  1290. devres_add(dev, ptr);
  1291. return 0;
  1292. }
  1293. EXPORT_SYMBOL(devm_devfreq_register_notifier);
  1294. /**
  1295. * devm_devfreq_unregister_notifier()
  1296. - Resource-managed devfreq_unregister_notifier()
  1297. * @dev: The devfreq user device. (parent of devfreq)
  1298. * @devfreq: The devfreq object.
  1299. * @nb: The notifier block to be unregistered.
  1300. * @list: DEVFREQ_TRANSITION_NOTIFIER.
  1301. */
  1302. void devm_devfreq_unregister_notifier(struct device *dev,
  1303. struct devfreq *devfreq,
  1304. struct notifier_block *nb,
  1305. unsigned int list)
  1306. {
  1307. WARN_ON(devres_release(dev, devm_devfreq_notifier_release,
  1308. devm_devfreq_dev_match, devfreq));
  1309. }
  1310. EXPORT_SYMBOL(devm_devfreq_unregister_notifier);