devfreq.c 32 KB

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