devfreq.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537
  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->scaling_min_freq = find_available_min_freq(devfreq);
  519. if (!devfreq->scaling_min_freq) {
  520. mutex_unlock(&devfreq->lock);
  521. err = -EINVAL;
  522. goto err_dev;
  523. }
  524. devfreq->min_freq = devfreq->scaling_min_freq;
  525. devfreq->scaling_max_freq = find_available_max_freq(devfreq);
  526. if (!devfreq->scaling_max_freq) {
  527. mutex_unlock(&devfreq->lock);
  528. err = -EINVAL;
  529. goto err_dev;
  530. }
  531. devfreq->max_freq = devfreq->scaling_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. put_device(&devfreq->dev);
  538. goto err_out;
  539. }
  540. devfreq->trans_table =
  541. devm_kzalloc(&devfreq->dev,
  542. array3_size(sizeof(unsigned int),
  543. devfreq->profile->max_state,
  544. devfreq->profile->max_state),
  545. GFP_KERNEL);
  546. devfreq->time_in_state = devm_kcalloc(&devfreq->dev,
  547. devfreq->profile->max_state,
  548. sizeof(unsigned long),
  549. GFP_KERNEL);
  550. devfreq->last_stat_updated = jiffies;
  551. srcu_init_notifier_head(&devfreq->transition_notifier_list);
  552. mutex_unlock(&devfreq->lock);
  553. mutex_lock(&devfreq_list_lock);
  554. list_add(&devfreq->node, &devfreq_list);
  555. governor = find_devfreq_governor(devfreq->governor_name);
  556. if (IS_ERR(governor)) {
  557. dev_err(dev, "%s: Unable to find governor for the device\n",
  558. __func__);
  559. err = PTR_ERR(governor);
  560. goto err_init;
  561. }
  562. devfreq->governor = governor;
  563. err = devfreq->governor->event_handler(devfreq, DEVFREQ_GOV_START,
  564. NULL);
  565. if (err) {
  566. dev_err(dev, "%s: Unable to start governor for the device\n",
  567. __func__);
  568. goto err_init;
  569. }
  570. mutex_unlock(&devfreq_list_lock);
  571. return devfreq;
  572. err_init:
  573. list_del(&devfreq->node);
  574. mutex_unlock(&devfreq_list_lock);
  575. device_unregister(&devfreq->dev);
  576. devfreq = NULL;
  577. err_dev:
  578. if (devfreq)
  579. kfree(devfreq);
  580. err_out:
  581. return ERR_PTR(err);
  582. }
  583. EXPORT_SYMBOL(devfreq_add_device);
  584. /**
  585. * devfreq_remove_device() - Remove devfreq feature from a device.
  586. * @devfreq: the devfreq instance to be removed
  587. *
  588. * The opposite of devfreq_add_device().
  589. */
  590. int devfreq_remove_device(struct devfreq *devfreq)
  591. {
  592. if (!devfreq)
  593. return -EINVAL;
  594. device_unregister(&devfreq->dev);
  595. return 0;
  596. }
  597. EXPORT_SYMBOL(devfreq_remove_device);
  598. static int devm_devfreq_dev_match(struct device *dev, void *res, void *data)
  599. {
  600. struct devfreq **r = res;
  601. if (WARN_ON(!r || !*r))
  602. return 0;
  603. return *r == data;
  604. }
  605. static void devm_devfreq_dev_release(struct device *dev, void *res)
  606. {
  607. devfreq_remove_device(*(struct devfreq **)res);
  608. }
  609. /**
  610. * devm_devfreq_add_device() - Resource-managed devfreq_add_device()
  611. * @dev: the device to add devfreq feature.
  612. * @profile: device-specific profile to run devfreq.
  613. * @governor_name: name of the policy to choose frequency.
  614. * @data: private data for the governor. The devfreq framework does not
  615. * touch this value.
  616. *
  617. * This function manages automatically the memory of devfreq device using device
  618. * resource management and simplify the free operation for memory of devfreq
  619. * device.
  620. */
  621. struct devfreq *devm_devfreq_add_device(struct device *dev,
  622. struct devfreq_dev_profile *profile,
  623. const char *governor_name,
  624. void *data)
  625. {
  626. struct devfreq **ptr, *devfreq;
  627. ptr = devres_alloc(devm_devfreq_dev_release, sizeof(*ptr), GFP_KERNEL);
  628. if (!ptr)
  629. return ERR_PTR(-ENOMEM);
  630. devfreq = devfreq_add_device(dev, profile, governor_name, data);
  631. if (IS_ERR(devfreq)) {
  632. devres_free(ptr);
  633. return devfreq;
  634. }
  635. *ptr = devfreq;
  636. devres_add(dev, ptr);
  637. return devfreq;
  638. }
  639. EXPORT_SYMBOL(devm_devfreq_add_device);
  640. #ifdef CONFIG_OF
  641. /*
  642. * devfreq_get_devfreq_by_phandle - Get the devfreq device from devicetree
  643. * @dev - instance to the given device
  644. * @index - index into list of devfreq
  645. *
  646. * return the instance of devfreq device
  647. */
  648. struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, int index)
  649. {
  650. struct device_node *node;
  651. struct devfreq *devfreq;
  652. if (!dev)
  653. return ERR_PTR(-EINVAL);
  654. if (!dev->of_node)
  655. return ERR_PTR(-EINVAL);
  656. node = of_parse_phandle(dev->of_node, "devfreq", index);
  657. if (!node)
  658. return ERR_PTR(-ENODEV);
  659. mutex_lock(&devfreq_list_lock);
  660. list_for_each_entry(devfreq, &devfreq_list, node) {
  661. if (devfreq->dev.parent
  662. && devfreq->dev.parent->of_node == node) {
  663. mutex_unlock(&devfreq_list_lock);
  664. of_node_put(node);
  665. return devfreq;
  666. }
  667. }
  668. mutex_unlock(&devfreq_list_lock);
  669. of_node_put(node);
  670. return ERR_PTR(-EPROBE_DEFER);
  671. }
  672. #else
  673. struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, int index)
  674. {
  675. return ERR_PTR(-ENODEV);
  676. }
  677. #endif /* CONFIG_OF */
  678. EXPORT_SYMBOL_GPL(devfreq_get_devfreq_by_phandle);
  679. /**
  680. * devm_devfreq_remove_device() - Resource-managed devfreq_remove_device()
  681. * @dev: the device to add devfreq feature.
  682. * @devfreq: the devfreq instance to be removed
  683. */
  684. void devm_devfreq_remove_device(struct device *dev, struct devfreq *devfreq)
  685. {
  686. WARN_ON(devres_release(dev, devm_devfreq_dev_release,
  687. devm_devfreq_dev_match, devfreq));
  688. }
  689. EXPORT_SYMBOL(devm_devfreq_remove_device);
  690. /**
  691. * devfreq_suspend_device() - Suspend devfreq of a device.
  692. * @devfreq: the devfreq instance to be suspended
  693. *
  694. * This function is intended to be called by the pm callbacks
  695. * (e.g., runtime_suspend, suspend) of the device driver that
  696. * holds the devfreq.
  697. */
  698. int devfreq_suspend_device(struct devfreq *devfreq)
  699. {
  700. if (!devfreq)
  701. return -EINVAL;
  702. if (!devfreq->governor)
  703. return 0;
  704. return devfreq->governor->event_handler(devfreq,
  705. DEVFREQ_GOV_SUSPEND, NULL);
  706. }
  707. EXPORT_SYMBOL(devfreq_suspend_device);
  708. /**
  709. * devfreq_resume_device() - Resume devfreq of a device.
  710. * @devfreq: the devfreq instance to be resumed
  711. *
  712. * This function is intended to be called by the pm callbacks
  713. * (e.g., runtime_resume, resume) of the device driver that
  714. * holds the devfreq.
  715. */
  716. int devfreq_resume_device(struct devfreq *devfreq)
  717. {
  718. if (!devfreq)
  719. return -EINVAL;
  720. if (!devfreq->governor)
  721. return 0;
  722. return devfreq->governor->event_handler(devfreq,
  723. DEVFREQ_GOV_RESUME, NULL);
  724. }
  725. EXPORT_SYMBOL(devfreq_resume_device);
  726. /**
  727. * devfreq_add_governor() - Add devfreq governor
  728. * @governor: the devfreq governor to be added
  729. */
  730. int devfreq_add_governor(struct devfreq_governor *governor)
  731. {
  732. struct devfreq_governor *g;
  733. struct devfreq *devfreq;
  734. int err = 0;
  735. if (!governor) {
  736. pr_err("%s: Invalid parameters.\n", __func__);
  737. return -EINVAL;
  738. }
  739. mutex_lock(&devfreq_list_lock);
  740. g = find_devfreq_governor(governor->name);
  741. if (!IS_ERR(g)) {
  742. pr_err("%s: governor %s already registered\n", __func__,
  743. g->name);
  744. err = -EINVAL;
  745. goto err_out;
  746. }
  747. list_add(&governor->node, &devfreq_governor_list);
  748. list_for_each_entry(devfreq, &devfreq_list, node) {
  749. int ret = 0;
  750. struct device *dev = devfreq->dev.parent;
  751. if (!strncmp(devfreq->governor_name, governor->name,
  752. DEVFREQ_NAME_LEN)) {
  753. /* The following should never occur */
  754. if (devfreq->governor) {
  755. dev_warn(dev,
  756. "%s: Governor %s already present\n",
  757. __func__, devfreq->governor->name);
  758. ret = devfreq->governor->event_handler(devfreq,
  759. DEVFREQ_GOV_STOP, NULL);
  760. if (ret) {
  761. dev_warn(dev,
  762. "%s: Governor %s stop = %d\n",
  763. __func__,
  764. devfreq->governor->name, ret);
  765. }
  766. /* Fall through */
  767. }
  768. devfreq->governor = governor;
  769. ret = devfreq->governor->event_handler(devfreq,
  770. DEVFREQ_GOV_START, NULL);
  771. if (ret) {
  772. dev_warn(dev, "%s: Governor %s start=%d\n",
  773. __func__, devfreq->governor->name,
  774. ret);
  775. }
  776. }
  777. }
  778. err_out:
  779. mutex_unlock(&devfreq_list_lock);
  780. return err;
  781. }
  782. EXPORT_SYMBOL(devfreq_add_governor);
  783. /**
  784. * devfreq_remove_governor() - Remove devfreq feature from a device.
  785. * @governor: the devfreq governor to be removed
  786. */
  787. int devfreq_remove_governor(struct devfreq_governor *governor)
  788. {
  789. struct devfreq_governor *g;
  790. struct devfreq *devfreq;
  791. int err = 0;
  792. if (!governor) {
  793. pr_err("%s: Invalid parameters.\n", __func__);
  794. return -EINVAL;
  795. }
  796. mutex_lock(&devfreq_list_lock);
  797. g = find_devfreq_governor(governor->name);
  798. if (IS_ERR(g)) {
  799. pr_err("%s: governor %s not registered\n", __func__,
  800. governor->name);
  801. err = PTR_ERR(g);
  802. goto err_out;
  803. }
  804. list_for_each_entry(devfreq, &devfreq_list, node) {
  805. int ret;
  806. struct device *dev = devfreq->dev.parent;
  807. if (!strncmp(devfreq->governor_name, governor->name,
  808. DEVFREQ_NAME_LEN)) {
  809. /* we should have a devfreq governor! */
  810. if (!devfreq->governor) {
  811. dev_warn(dev, "%s: Governor %s NOT present\n",
  812. __func__, governor->name);
  813. continue;
  814. /* Fall through */
  815. }
  816. ret = devfreq->governor->event_handler(devfreq,
  817. DEVFREQ_GOV_STOP, NULL);
  818. if (ret) {
  819. dev_warn(dev, "%s: Governor %s stop=%d\n",
  820. __func__, devfreq->governor->name,
  821. ret);
  822. }
  823. devfreq->governor = NULL;
  824. }
  825. }
  826. list_del(&governor->node);
  827. err_out:
  828. mutex_unlock(&devfreq_list_lock);
  829. return err;
  830. }
  831. EXPORT_SYMBOL(devfreq_remove_governor);
  832. static ssize_t governor_show(struct device *dev,
  833. struct device_attribute *attr, char *buf)
  834. {
  835. if (!to_devfreq(dev)->governor)
  836. return -EINVAL;
  837. return sprintf(buf, "%s\n", to_devfreq(dev)->governor->name);
  838. }
  839. static ssize_t governor_store(struct device *dev, struct device_attribute *attr,
  840. const char *buf, size_t count)
  841. {
  842. struct devfreq *df = to_devfreq(dev);
  843. int ret;
  844. char str_governor[DEVFREQ_NAME_LEN + 1];
  845. struct devfreq_governor *governor;
  846. ret = sscanf(buf, "%" __stringify(DEVFREQ_NAME_LEN) "s", str_governor);
  847. if (ret != 1)
  848. return -EINVAL;
  849. mutex_lock(&devfreq_list_lock);
  850. governor = find_devfreq_governor(str_governor);
  851. if (IS_ERR(governor)) {
  852. ret = PTR_ERR(governor);
  853. goto out;
  854. }
  855. if (df->governor == governor) {
  856. ret = 0;
  857. goto out;
  858. } else if ((df->governor && df->governor->immutable) ||
  859. governor->immutable) {
  860. ret = -EINVAL;
  861. goto out;
  862. }
  863. if (df->governor) {
  864. ret = df->governor->event_handler(df, DEVFREQ_GOV_STOP, NULL);
  865. if (ret) {
  866. dev_warn(dev, "%s: Governor %s not stopped(%d)\n",
  867. __func__, df->governor->name, ret);
  868. goto out;
  869. }
  870. }
  871. df->governor = governor;
  872. strncpy(df->governor_name, governor->name, DEVFREQ_NAME_LEN);
  873. ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
  874. if (ret)
  875. dev_warn(dev, "%s: Governor %s not started(%d)\n",
  876. __func__, df->governor->name, ret);
  877. out:
  878. mutex_unlock(&devfreq_list_lock);
  879. if (!ret)
  880. ret = count;
  881. return ret;
  882. }
  883. static DEVICE_ATTR_RW(governor);
  884. static ssize_t available_governors_show(struct device *d,
  885. struct device_attribute *attr,
  886. char *buf)
  887. {
  888. struct devfreq *df = to_devfreq(d);
  889. ssize_t count = 0;
  890. mutex_lock(&devfreq_list_lock);
  891. /*
  892. * The devfreq with immutable governor (e.g., passive) shows
  893. * only own governor.
  894. */
  895. if (df->governor->immutable) {
  896. count = scnprintf(&buf[count], DEVFREQ_NAME_LEN,
  897. "%s ", df->governor_name);
  898. /*
  899. * The devfreq device shows the registered governor except for
  900. * immutable governors such as passive governor .
  901. */
  902. } else {
  903. struct devfreq_governor *governor;
  904. list_for_each_entry(governor, &devfreq_governor_list, node) {
  905. if (governor->immutable)
  906. continue;
  907. count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
  908. "%s ", governor->name);
  909. }
  910. }
  911. mutex_unlock(&devfreq_list_lock);
  912. /* Truncate the trailing space */
  913. if (count)
  914. count--;
  915. count += sprintf(&buf[count], "\n");
  916. return count;
  917. }
  918. static DEVICE_ATTR_RO(available_governors);
  919. static ssize_t cur_freq_show(struct device *dev, struct device_attribute *attr,
  920. char *buf)
  921. {
  922. unsigned long freq;
  923. struct devfreq *devfreq = to_devfreq(dev);
  924. if (devfreq->profile->get_cur_freq &&
  925. !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
  926. return sprintf(buf, "%lu\n", freq);
  927. return sprintf(buf, "%lu\n", devfreq->previous_freq);
  928. }
  929. static DEVICE_ATTR_RO(cur_freq);
  930. static ssize_t target_freq_show(struct device *dev,
  931. struct device_attribute *attr, char *buf)
  932. {
  933. return sprintf(buf, "%lu\n", to_devfreq(dev)->previous_freq);
  934. }
  935. static DEVICE_ATTR_RO(target_freq);
  936. static ssize_t polling_interval_show(struct device *dev,
  937. struct device_attribute *attr, char *buf)
  938. {
  939. return sprintf(buf, "%d\n", to_devfreq(dev)->profile->polling_ms);
  940. }
  941. static ssize_t polling_interval_store(struct device *dev,
  942. struct device_attribute *attr,
  943. const char *buf, size_t count)
  944. {
  945. struct devfreq *df = to_devfreq(dev);
  946. unsigned int value;
  947. int ret;
  948. if (!df->governor)
  949. return -EINVAL;
  950. ret = sscanf(buf, "%u", &value);
  951. if (ret != 1)
  952. return -EINVAL;
  953. df->governor->event_handler(df, DEVFREQ_GOV_INTERVAL, &value);
  954. ret = count;
  955. return ret;
  956. }
  957. static DEVICE_ATTR_RW(polling_interval);
  958. static ssize_t min_freq_store(struct device *dev, struct device_attribute *attr,
  959. const char *buf, size_t count)
  960. {
  961. struct devfreq *df = to_devfreq(dev);
  962. unsigned long value;
  963. int ret;
  964. unsigned long max;
  965. ret = sscanf(buf, "%lu", &value);
  966. if (ret != 1)
  967. return -EINVAL;
  968. mutex_lock(&df->lock);
  969. max = df->max_freq;
  970. if (value && max && value > max) {
  971. ret = -EINVAL;
  972. goto unlock;
  973. }
  974. df->min_freq = value;
  975. update_devfreq(df);
  976. ret = count;
  977. unlock:
  978. mutex_unlock(&df->lock);
  979. return ret;
  980. }
  981. static ssize_t min_freq_show(struct device *dev, struct device_attribute *attr,
  982. char *buf)
  983. {
  984. struct devfreq *df = to_devfreq(dev);
  985. return sprintf(buf, "%lu\n", MAX(df->scaling_min_freq, df->min_freq));
  986. }
  987. static ssize_t max_freq_store(struct device *dev, struct device_attribute *attr,
  988. const char *buf, size_t count)
  989. {
  990. struct devfreq *df = to_devfreq(dev);
  991. unsigned long value;
  992. int ret;
  993. unsigned long min;
  994. ret = sscanf(buf, "%lu", &value);
  995. if (ret != 1)
  996. return -EINVAL;
  997. mutex_lock(&df->lock);
  998. min = df->min_freq;
  999. if (value && min && value < min) {
  1000. ret = -EINVAL;
  1001. goto unlock;
  1002. }
  1003. df->max_freq = value;
  1004. update_devfreq(df);
  1005. ret = count;
  1006. unlock:
  1007. mutex_unlock(&df->lock);
  1008. return ret;
  1009. }
  1010. static DEVICE_ATTR_RW(min_freq);
  1011. static ssize_t max_freq_show(struct device *dev, struct device_attribute *attr,
  1012. char *buf)
  1013. {
  1014. struct devfreq *df = to_devfreq(dev);
  1015. return sprintf(buf, "%lu\n", MIN(df->scaling_max_freq, df->max_freq));
  1016. }
  1017. static DEVICE_ATTR_RW(max_freq);
  1018. static ssize_t available_frequencies_show(struct device *d,
  1019. struct device_attribute *attr,
  1020. char *buf)
  1021. {
  1022. struct devfreq *df = to_devfreq(d);
  1023. ssize_t count = 0;
  1024. int i;
  1025. mutex_lock(&df->lock);
  1026. for (i = 0; i < df->profile->max_state; i++)
  1027. count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
  1028. "%lu ", df->profile->freq_table[i]);
  1029. mutex_unlock(&df->lock);
  1030. /* Truncate the trailing space */
  1031. if (count)
  1032. count--;
  1033. count += sprintf(&buf[count], "\n");
  1034. return count;
  1035. }
  1036. static DEVICE_ATTR_RO(available_frequencies);
  1037. static ssize_t trans_stat_show(struct device *dev,
  1038. struct device_attribute *attr, char *buf)
  1039. {
  1040. struct devfreq *devfreq = to_devfreq(dev);
  1041. ssize_t len;
  1042. int i, j;
  1043. unsigned int max_state = devfreq->profile->max_state;
  1044. if (!devfreq->stop_polling &&
  1045. devfreq_update_status(devfreq, devfreq->previous_freq))
  1046. return 0;
  1047. if (max_state == 0)
  1048. return sprintf(buf, "Not Supported.\n");
  1049. len = sprintf(buf, " From : To\n");
  1050. len += sprintf(buf + len, " :");
  1051. for (i = 0; i < max_state; i++)
  1052. len += sprintf(buf + len, "%10lu",
  1053. devfreq->profile->freq_table[i]);
  1054. len += sprintf(buf + len, " time(ms)\n");
  1055. for (i = 0; i < max_state; i++) {
  1056. if (devfreq->profile->freq_table[i]
  1057. == devfreq->previous_freq) {
  1058. len += sprintf(buf + len, "*");
  1059. } else {
  1060. len += sprintf(buf + len, " ");
  1061. }
  1062. len += sprintf(buf + len, "%10lu:",
  1063. devfreq->profile->freq_table[i]);
  1064. for (j = 0; j < max_state; j++)
  1065. len += sprintf(buf + len, "%10u",
  1066. devfreq->trans_table[(i * max_state) + j]);
  1067. len += sprintf(buf + len, "%10u\n",
  1068. jiffies_to_msecs(devfreq->time_in_state[i]));
  1069. }
  1070. len += sprintf(buf + len, "Total transition : %u\n",
  1071. devfreq->total_trans);
  1072. return len;
  1073. }
  1074. static DEVICE_ATTR_RO(trans_stat);
  1075. static struct attribute *devfreq_attrs[] = {
  1076. &dev_attr_governor.attr,
  1077. &dev_attr_available_governors.attr,
  1078. &dev_attr_cur_freq.attr,
  1079. &dev_attr_available_frequencies.attr,
  1080. &dev_attr_target_freq.attr,
  1081. &dev_attr_polling_interval.attr,
  1082. &dev_attr_min_freq.attr,
  1083. &dev_attr_max_freq.attr,
  1084. &dev_attr_trans_stat.attr,
  1085. NULL,
  1086. };
  1087. ATTRIBUTE_GROUPS(devfreq);
  1088. static int __init devfreq_init(void)
  1089. {
  1090. devfreq_class = class_create(THIS_MODULE, "devfreq");
  1091. if (IS_ERR(devfreq_class)) {
  1092. pr_err("%s: couldn't create class\n", __FILE__);
  1093. return PTR_ERR(devfreq_class);
  1094. }
  1095. devfreq_wq = create_freezable_workqueue("devfreq_wq");
  1096. if (!devfreq_wq) {
  1097. class_destroy(devfreq_class);
  1098. pr_err("%s: couldn't create workqueue\n", __FILE__);
  1099. return -ENOMEM;
  1100. }
  1101. devfreq_class->dev_groups = devfreq_groups;
  1102. return 0;
  1103. }
  1104. subsys_initcall(devfreq_init);
  1105. /*
  1106. * The following are helper functions for devfreq user device drivers with
  1107. * OPP framework.
  1108. */
  1109. /**
  1110. * devfreq_recommended_opp() - Helper function to get proper OPP for the
  1111. * freq value given to target callback.
  1112. * @dev: The devfreq user device. (parent of devfreq)
  1113. * @freq: The frequency given to target function
  1114. * @flags: Flags handed from devfreq framework.
  1115. *
  1116. * The callers are required to call dev_pm_opp_put() for the returned OPP after
  1117. * use.
  1118. */
  1119. struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
  1120. unsigned long *freq,
  1121. u32 flags)
  1122. {
  1123. struct dev_pm_opp *opp;
  1124. if (flags & DEVFREQ_FLAG_LEAST_UPPER_BOUND) {
  1125. /* The freq is an upper bound. opp should be lower */
  1126. opp = dev_pm_opp_find_freq_floor(dev, freq);
  1127. /* If not available, use the closest opp */
  1128. if (opp == ERR_PTR(-ERANGE))
  1129. opp = dev_pm_opp_find_freq_ceil(dev, freq);
  1130. } else {
  1131. /* The freq is an lower bound. opp should be higher */
  1132. opp = dev_pm_opp_find_freq_ceil(dev, freq);
  1133. /* If not available, use the closest opp */
  1134. if (opp == ERR_PTR(-ERANGE))
  1135. opp = dev_pm_opp_find_freq_floor(dev, freq);
  1136. }
  1137. return opp;
  1138. }
  1139. EXPORT_SYMBOL(devfreq_recommended_opp);
  1140. /**
  1141. * devfreq_register_opp_notifier() - Helper function to get devfreq notified
  1142. * for any changes in the OPP availability
  1143. * changes
  1144. * @dev: The devfreq user device. (parent of devfreq)
  1145. * @devfreq: The devfreq object.
  1146. */
  1147. int devfreq_register_opp_notifier(struct device *dev, struct devfreq *devfreq)
  1148. {
  1149. return dev_pm_opp_register_notifier(dev, &devfreq->nb);
  1150. }
  1151. EXPORT_SYMBOL(devfreq_register_opp_notifier);
  1152. /**
  1153. * devfreq_unregister_opp_notifier() - Helper function to stop getting devfreq
  1154. * notified for any changes in the OPP
  1155. * availability changes anymore.
  1156. * @dev: The devfreq user device. (parent of devfreq)
  1157. * @devfreq: The devfreq object.
  1158. *
  1159. * At exit() callback of devfreq_dev_profile, this must be included if
  1160. * devfreq_recommended_opp is used.
  1161. */
  1162. int devfreq_unregister_opp_notifier(struct device *dev, struct devfreq *devfreq)
  1163. {
  1164. return dev_pm_opp_unregister_notifier(dev, &devfreq->nb);
  1165. }
  1166. EXPORT_SYMBOL(devfreq_unregister_opp_notifier);
  1167. static void devm_devfreq_opp_release(struct device *dev, void *res)
  1168. {
  1169. devfreq_unregister_opp_notifier(dev, *(struct devfreq **)res);
  1170. }
  1171. /**
  1172. * devm_ devfreq_register_opp_notifier()
  1173. * - Resource-managed devfreq_register_opp_notifier()
  1174. * @dev: The devfreq user device. (parent of devfreq)
  1175. * @devfreq: The devfreq object.
  1176. */
  1177. int devm_devfreq_register_opp_notifier(struct device *dev,
  1178. struct devfreq *devfreq)
  1179. {
  1180. struct devfreq **ptr;
  1181. int ret;
  1182. ptr = devres_alloc(devm_devfreq_opp_release, sizeof(*ptr), GFP_KERNEL);
  1183. if (!ptr)
  1184. return -ENOMEM;
  1185. ret = devfreq_register_opp_notifier(dev, devfreq);
  1186. if (ret) {
  1187. devres_free(ptr);
  1188. return ret;
  1189. }
  1190. *ptr = devfreq;
  1191. devres_add(dev, ptr);
  1192. return 0;
  1193. }
  1194. EXPORT_SYMBOL(devm_devfreq_register_opp_notifier);
  1195. /**
  1196. * devm_devfreq_unregister_opp_notifier()
  1197. * - Resource-managed devfreq_unregister_opp_notifier()
  1198. * @dev: The devfreq user device. (parent of devfreq)
  1199. * @devfreq: The devfreq object.
  1200. */
  1201. void devm_devfreq_unregister_opp_notifier(struct device *dev,
  1202. struct devfreq *devfreq)
  1203. {
  1204. WARN_ON(devres_release(dev, devm_devfreq_opp_release,
  1205. devm_devfreq_dev_match, devfreq));
  1206. }
  1207. EXPORT_SYMBOL(devm_devfreq_unregister_opp_notifier);
  1208. /**
  1209. * devfreq_register_notifier() - Register a driver with devfreq
  1210. * @devfreq: The devfreq object.
  1211. * @nb: The notifier block to register.
  1212. * @list: DEVFREQ_TRANSITION_NOTIFIER.
  1213. */
  1214. int devfreq_register_notifier(struct devfreq *devfreq,
  1215. struct notifier_block *nb,
  1216. unsigned int list)
  1217. {
  1218. int ret = 0;
  1219. if (!devfreq)
  1220. return -EINVAL;
  1221. switch (list) {
  1222. case DEVFREQ_TRANSITION_NOTIFIER:
  1223. ret = srcu_notifier_chain_register(
  1224. &devfreq->transition_notifier_list, nb);
  1225. break;
  1226. default:
  1227. ret = -EINVAL;
  1228. }
  1229. return ret;
  1230. }
  1231. EXPORT_SYMBOL(devfreq_register_notifier);
  1232. /*
  1233. * devfreq_unregister_notifier() - Unregister a driver with devfreq
  1234. * @devfreq: The devfreq object.
  1235. * @nb: The notifier block to be unregistered.
  1236. * @list: DEVFREQ_TRANSITION_NOTIFIER.
  1237. */
  1238. int devfreq_unregister_notifier(struct devfreq *devfreq,
  1239. struct notifier_block *nb,
  1240. unsigned int list)
  1241. {
  1242. int ret = 0;
  1243. if (!devfreq)
  1244. return -EINVAL;
  1245. switch (list) {
  1246. case DEVFREQ_TRANSITION_NOTIFIER:
  1247. ret = srcu_notifier_chain_unregister(
  1248. &devfreq->transition_notifier_list, nb);
  1249. break;
  1250. default:
  1251. ret = -EINVAL;
  1252. }
  1253. return ret;
  1254. }
  1255. EXPORT_SYMBOL(devfreq_unregister_notifier);
  1256. struct devfreq_notifier_devres {
  1257. struct devfreq *devfreq;
  1258. struct notifier_block *nb;
  1259. unsigned int list;
  1260. };
  1261. static void devm_devfreq_notifier_release(struct device *dev, void *res)
  1262. {
  1263. struct devfreq_notifier_devres *this = res;
  1264. devfreq_unregister_notifier(this->devfreq, this->nb, this->list);
  1265. }
  1266. /**
  1267. * devm_devfreq_register_notifier()
  1268. - Resource-managed devfreq_register_notifier()
  1269. * @dev: The devfreq user device. (parent of devfreq)
  1270. * @devfreq: The devfreq object.
  1271. * @nb: The notifier block to be unregistered.
  1272. * @list: DEVFREQ_TRANSITION_NOTIFIER.
  1273. */
  1274. int devm_devfreq_register_notifier(struct device *dev,
  1275. struct devfreq *devfreq,
  1276. struct notifier_block *nb,
  1277. unsigned int list)
  1278. {
  1279. struct devfreq_notifier_devres *ptr;
  1280. int ret;
  1281. ptr = devres_alloc(devm_devfreq_notifier_release, sizeof(*ptr),
  1282. GFP_KERNEL);
  1283. if (!ptr)
  1284. return -ENOMEM;
  1285. ret = devfreq_register_notifier(devfreq, nb, list);
  1286. if (ret) {
  1287. devres_free(ptr);
  1288. return ret;
  1289. }
  1290. ptr->devfreq = devfreq;
  1291. ptr->nb = nb;
  1292. ptr->list = list;
  1293. devres_add(dev, ptr);
  1294. return 0;
  1295. }
  1296. EXPORT_SYMBOL(devm_devfreq_register_notifier);
  1297. /**
  1298. * devm_devfreq_unregister_notifier()
  1299. - Resource-managed devfreq_unregister_notifier()
  1300. * @dev: The devfreq user device. (parent of devfreq)
  1301. * @devfreq: The devfreq object.
  1302. * @nb: The notifier block to be unregistered.
  1303. * @list: DEVFREQ_TRANSITION_NOTIFIER.
  1304. */
  1305. void devm_devfreq_unregister_notifier(struct device *dev,
  1306. struct devfreq *devfreq,
  1307. struct notifier_block *nb,
  1308. unsigned int list)
  1309. {
  1310. WARN_ON(devres_release(dev, devm_devfreq_notifier_release,
  1311. devm_devfreq_dev_match, devfreq));
  1312. }
  1313. EXPORT_SYMBOL(devm_devfreq_unregister_notifier);