devfreq.c 31 KB

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