devfreq.c 37 KB

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