devfreq.c 38 KB

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