devfreq.c 37 KB

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