devfreq.c 28 KB

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