devfreq.c 37 KB

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