devfreq.c 37 KB

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