devfreq.c 37 KB

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