devfreq.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  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 */
  36. static LIST_HEAD(devfreq_list);
  37. static DEFINE_MUTEX(devfreq_list_lock);
  38. /**
  39. * find_device_devfreq() - find devfreq struct using device pointer
  40. * @dev: device pointer used to lookup device devfreq.
  41. *
  42. * Search the list of device devfreqs and return the matched device's
  43. * devfreq info. devfreq_list_lock should be held by the caller.
  44. */
  45. static struct devfreq *find_device_devfreq(struct device *dev)
  46. {
  47. struct devfreq *tmp_devfreq;
  48. if (unlikely(IS_ERR_OR_NULL(dev))) {
  49. pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
  50. return ERR_PTR(-EINVAL);
  51. }
  52. WARN(!mutex_is_locked(&devfreq_list_lock),
  53. "devfreq_list_lock must be locked.");
  54. list_for_each_entry(tmp_devfreq, &devfreq_list, node) {
  55. if (tmp_devfreq->dev.parent == dev)
  56. return tmp_devfreq;
  57. }
  58. return ERR_PTR(-ENODEV);
  59. }
  60. /**
  61. * devfreq_get_freq_level() - Lookup freq_table for the frequency
  62. * @devfreq: the devfreq instance
  63. * @freq: the target frequency
  64. */
  65. static int devfreq_get_freq_level(struct devfreq *devfreq, unsigned long freq)
  66. {
  67. int lev;
  68. for (lev = 0; lev < devfreq->profile->max_state; lev++)
  69. if (freq == devfreq->profile->freq_table[lev])
  70. return lev;
  71. return -EINVAL;
  72. }
  73. /**
  74. * devfreq_update_status() - Update statistics of devfreq behavior
  75. * @devfreq: the devfreq instance
  76. * @freq: the update target frequency
  77. */
  78. static int devfreq_update_status(struct devfreq *devfreq, unsigned long freq)
  79. {
  80. int lev, prev_lev;
  81. unsigned long cur_time;
  82. lev = devfreq_get_freq_level(devfreq, freq);
  83. if (lev < 0)
  84. return lev;
  85. cur_time = jiffies;
  86. devfreq->time_in_state[lev] +=
  87. cur_time - devfreq->last_stat_updated;
  88. if (freq != devfreq->previous_freq) {
  89. prev_lev = devfreq_get_freq_level(devfreq,
  90. devfreq->previous_freq);
  91. devfreq->trans_table[(prev_lev *
  92. devfreq->profile->max_state) + lev]++;
  93. devfreq->total_trans++;
  94. }
  95. devfreq->last_stat_updated = cur_time;
  96. return 0;
  97. }
  98. /* Load monitoring helper functions for governors use */
  99. /**
  100. * update_devfreq() - Reevaluate the device and configure frequency.
  101. * @devfreq: the devfreq instance.
  102. *
  103. * Note: Lock devfreq->lock before calling update_devfreq
  104. * This function is exported for governors.
  105. */
  106. int update_devfreq(struct devfreq *devfreq)
  107. {
  108. unsigned long freq;
  109. int err = 0;
  110. u32 flags = 0;
  111. if (!mutex_is_locked(&devfreq->lock)) {
  112. WARN(true, "devfreq->lock must be locked by the caller.\n");
  113. return -EINVAL;
  114. }
  115. /* Reevaluate the proper frequency */
  116. err = devfreq->governor->get_target_freq(devfreq, &freq);
  117. if (err)
  118. return err;
  119. /*
  120. * Adjust the freuqency with user freq and QoS.
  121. *
  122. * List from the highest proiority
  123. * max_freq (probably called by thermal when it's too hot)
  124. * min_freq
  125. */
  126. if (devfreq->min_freq && freq < devfreq->min_freq) {
  127. freq = devfreq->min_freq;
  128. flags &= ~DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use GLB */
  129. }
  130. if (devfreq->max_freq && freq > devfreq->max_freq) {
  131. freq = devfreq->max_freq;
  132. flags |= DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use LUB */
  133. }
  134. err = devfreq->profile->target(devfreq->dev.parent, &freq, flags);
  135. if (err)
  136. return err;
  137. if (devfreq->profile->freq_table)
  138. if (devfreq_update_status(devfreq, freq))
  139. dev_err(&devfreq->dev,
  140. "Couldn't update frequency transition information.\n");
  141. devfreq->previous_freq = freq;
  142. return err;
  143. }
  144. /**
  145. * devfreq_monitor() - Periodically poll devfreq objects.
  146. * @work: the work struct used to run devfreq_monitor periodically.
  147. *
  148. */
  149. static void devfreq_monitor(struct work_struct *work)
  150. {
  151. int err;
  152. struct devfreq *devfreq = container_of(work,
  153. struct devfreq, work.work);
  154. mutex_lock(&devfreq->lock);
  155. err = update_devfreq(devfreq);
  156. if (err)
  157. dev_err(&devfreq->dev, "dvfs failed with (%d) error\n", err);
  158. queue_delayed_work(devfreq_wq, &devfreq->work,
  159. msecs_to_jiffies(devfreq->profile->polling_ms));
  160. mutex_unlock(&devfreq->lock);
  161. }
  162. /**
  163. * devfreq_monitor_start() - Start load monitoring of devfreq instance
  164. * @devfreq: the devfreq instance.
  165. *
  166. * Helper function for starting devfreq device load monitoing. By
  167. * default delayed work based monitoring is supported. Function
  168. * to be called from governor in response to DEVFREQ_GOV_START
  169. * event when device is added to devfreq framework.
  170. */
  171. void devfreq_monitor_start(struct devfreq *devfreq)
  172. {
  173. INIT_DEFERRABLE_WORK(&devfreq->work, devfreq_monitor);
  174. if (devfreq->profile->polling_ms)
  175. queue_delayed_work(devfreq_wq, &devfreq->work,
  176. msecs_to_jiffies(devfreq->profile->polling_ms));
  177. }
  178. /**
  179. * devfreq_monitor_stop() - Stop load monitoring of a devfreq instance
  180. * @devfreq: the devfreq instance.
  181. *
  182. * Helper function to stop devfreq device load monitoing. Function
  183. * to be called from governor in response to DEVFREQ_GOV_STOP
  184. * event when device is removed from devfreq framework.
  185. */
  186. void devfreq_monitor_stop(struct devfreq *devfreq)
  187. {
  188. cancel_delayed_work_sync(&devfreq->work);
  189. }
  190. /**
  191. * devfreq_monitor_suspend() - Suspend load monitoring of a devfreq instance
  192. * @devfreq: the devfreq instance.
  193. *
  194. * Helper function to suspend devfreq device load monitoing. Function
  195. * to be called from governor in response to DEVFREQ_GOV_SUSPEND
  196. * event or when polling interval is set to zero.
  197. *
  198. * Note: Though this function is same as devfreq_monitor_stop(),
  199. * intentionally kept separate to provide hooks for collecting
  200. * transition statistics.
  201. */
  202. void devfreq_monitor_suspend(struct devfreq *devfreq)
  203. {
  204. mutex_lock(&devfreq->lock);
  205. if (devfreq->stop_polling) {
  206. mutex_unlock(&devfreq->lock);
  207. return;
  208. }
  209. devfreq->stop_polling = true;
  210. mutex_unlock(&devfreq->lock);
  211. cancel_delayed_work_sync(&devfreq->work);
  212. }
  213. /**
  214. * devfreq_monitor_resume() - Resume load monitoring of a devfreq instance
  215. * @devfreq: the devfreq instance.
  216. *
  217. * Helper function to resume devfreq device load monitoing. Function
  218. * to be called from governor in response to DEVFREQ_GOV_RESUME
  219. * event or when polling interval is set to non-zero.
  220. */
  221. void devfreq_monitor_resume(struct devfreq *devfreq)
  222. {
  223. mutex_lock(&devfreq->lock);
  224. if (!devfreq->stop_polling)
  225. goto out;
  226. if (!delayed_work_pending(&devfreq->work) &&
  227. devfreq->profile->polling_ms)
  228. queue_delayed_work(devfreq_wq, &devfreq->work,
  229. msecs_to_jiffies(devfreq->profile->polling_ms));
  230. devfreq->stop_polling = false;
  231. out:
  232. mutex_unlock(&devfreq->lock);
  233. }
  234. /**
  235. * devfreq_interval_update() - Update device devfreq monitoring interval
  236. * @devfreq: the devfreq instance.
  237. * @delay: new polling interval to be set.
  238. *
  239. * Helper function to set new load monitoring polling interval. Function
  240. * to be called from governor in response to DEVFREQ_GOV_INTERVAL event.
  241. */
  242. void devfreq_interval_update(struct devfreq *devfreq, unsigned int *delay)
  243. {
  244. unsigned int cur_delay = devfreq->profile->polling_ms;
  245. unsigned int new_delay = *delay;
  246. mutex_lock(&devfreq->lock);
  247. devfreq->profile->polling_ms = new_delay;
  248. if (devfreq->stop_polling)
  249. goto out;
  250. /* if new delay is zero, stop polling */
  251. if (!new_delay) {
  252. mutex_unlock(&devfreq->lock);
  253. cancel_delayed_work_sync(&devfreq->work);
  254. return;
  255. }
  256. /* if current delay is zero, start polling with new delay */
  257. if (!cur_delay) {
  258. queue_delayed_work(devfreq_wq, &devfreq->work,
  259. msecs_to_jiffies(devfreq->profile->polling_ms));
  260. goto out;
  261. }
  262. /* if current delay is greater than new delay, restart polling */
  263. if (cur_delay > new_delay) {
  264. mutex_unlock(&devfreq->lock);
  265. cancel_delayed_work_sync(&devfreq->work);
  266. mutex_lock(&devfreq->lock);
  267. if (!devfreq->stop_polling)
  268. queue_delayed_work(devfreq_wq, &devfreq->work,
  269. msecs_to_jiffies(devfreq->profile->polling_ms));
  270. }
  271. out:
  272. mutex_unlock(&devfreq->lock);
  273. }
  274. /**
  275. * devfreq_notifier_call() - Notify that the device frequency requirements
  276. * has been changed out of devfreq framework.
  277. * @nb: the notifier_block (supposed to be devfreq->nb)
  278. * @type: not used
  279. * @devp: not used
  280. *
  281. * Called by a notifier that uses devfreq->nb.
  282. */
  283. static int devfreq_notifier_call(struct notifier_block *nb, unsigned long type,
  284. void *devp)
  285. {
  286. struct devfreq *devfreq = container_of(nb, struct devfreq, nb);
  287. int ret;
  288. mutex_lock(&devfreq->lock);
  289. ret = update_devfreq(devfreq);
  290. mutex_unlock(&devfreq->lock);
  291. return ret;
  292. }
  293. /**
  294. * _remove_devfreq() - Remove devfreq from the list and release its resources.
  295. * @devfreq: the devfreq struct
  296. * @skip: skip calling device_unregister().
  297. */
  298. static void _remove_devfreq(struct devfreq *devfreq, bool skip)
  299. {
  300. mutex_lock(&devfreq_list_lock);
  301. if (IS_ERR(find_device_devfreq(devfreq->dev.parent))) {
  302. mutex_unlock(&devfreq_list_lock);
  303. dev_warn(&devfreq->dev, "releasing devfreq which doesn't exist\n");
  304. return;
  305. }
  306. list_del(&devfreq->node);
  307. mutex_unlock(&devfreq_list_lock);
  308. devfreq->governor->event_handler(devfreq, DEVFREQ_GOV_STOP, NULL);
  309. if (devfreq->profile->exit)
  310. devfreq->profile->exit(devfreq->dev.parent);
  311. if (!skip && get_device(&devfreq->dev)) {
  312. device_unregister(&devfreq->dev);
  313. put_device(&devfreq->dev);
  314. }
  315. mutex_destroy(&devfreq->lock);
  316. kfree(devfreq);
  317. }
  318. /**
  319. * devfreq_dev_release() - Callback for struct device to release the device.
  320. * @dev: the devfreq device
  321. *
  322. * This calls _remove_devfreq() if _remove_devfreq() is not called.
  323. * Note that devfreq_dev_release() could be called by _remove_devfreq() as
  324. * well as by others unregistering the device.
  325. */
  326. static void devfreq_dev_release(struct device *dev)
  327. {
  328. struct devfreq *devfreq = to_devfreq(dev);
  329. _remove_devfreq(devfreq, true);
  330. }
  331. /**
  332. * devfreq_add_device() - Add devfreq feature to the device
  333. * @dev: the device to add devfreq feature.
  334. * @profile: device-specific profile to run devfreq.
  335. * @governor: the policy to choose frequency.
  336. * @data: private data for the governor. The devfreq framework does not
  337. * touch this value.
  338. */
  339. struct devfreq *devfreq_add_device(struct device *dev,
  340. struct devfreq_dev_profile *profile,
  341. const struct devfreq_governor *governor,
  342. void *data)
  343. {
  344. struct devfreq *devfreq;
  345. int err = 0;
  346. if (!dev || !profile || !governor) {
  347. dev_err(dev, "%s: Invalid parameters.\n", __func__);
  348. return ERR_PTR(-EINVAL);
  349. }
  350. mutex_lock(&devfreq_list_lock);
  351. devfreq = find_device_devfreq(dev);
  352. mutex_unlock(&devfreq_list_lock);
  353. if (!IS_ERR(devfreq)) {
  354. dev_err(dev, "%s: Unable to create devfreq for the device. It already has one.\n", __func__);
  355. err = -EINVAL;
  356. goto err_out;
  357. }
  358. devfreq = kzalloc(sizeof(struct devfreq), GFP_KERNEL);
  359. if (!devfreq) {
  360. dev_err(dev, "%s: Unable to create devfreq for the device\n",
  361. __func__);
  362. err = -ENOMEM;
  363. goto err_out;
  364. }
  365. mutex_init(&devfreq->lock);
  366. mutex_lock(&devfreq->lock);
  367. devfreq->dev.parent = dev;
  368. devfreq->dev.class = devfreq_class;
  369. devfreq->dev.release = devfreq_dev_release;
  370. devfreq->profile = profile;
  371. devfreq->governor = governor;
  372. devfreq->previous_freq = profile->initial_freq;
  373. devfreq->data = data;
  374. devfreq->nb.notifier_call = devfreq_notifier_call;
  375. devfreq->trans_table = devm_kzalloc(dev, sizeof(unsigned int) *
  376. devfreq->profile->max_state *
  377. devfreq->profile->max_state,
  378. GFP_KERNEL);
  379. devfreq->time_in_state = devm_kzalloc(dev, sizeof(unsigned int) *
  380. devfreq->profile->max_state,
  381. GFP_KERNEL);
  382. devfreq->last_stat_updated = jiffies;
  383. dev_set_name(&devfreq->dev, dev_name(dev));
  384. err = device_register(&devfreq->dev);
  385. if (err) {
  386. put_device(&devfreq->dev);
  387. mutex_unlock(&devfreq->lock);
  388. goto err_dev;
  389. }
  390. mutex_unlock(&devfreq->lock);
  391. mutex_lock(&devfreq_list_lock);
  392. list_add(&devfreq->node, &devfreq_list);
  393. mutex_unlock(&devfreq_list_lock);
  394. err = devfreq->governor->event_handler(devfreq,
  395. DEVFREQ_GOV_START, NULL);
  396. if (err) {
  397. dev_err(dev, "%s: Unable to start governor for the device\n",
  398. __func__);
  399. goto err_init;
  400. }
  401. return devfreq;
  402. err_init:
  403. list_del(&devfreq->node);
  404. device_unregister(&devfreq->dev);
  405. err_dev:
  406. kfree(devfreq);
  407. err_out:
  408. return ERR_PTR(err);
  409. }
  410. EXPORT_SYMBOL(devfreq_add_device);
  411. /**
  412. * devfreq_remove_device() - Remove devfreq feature from a device.
  413. * @devfreq: the devfreq instance to be removed
  414. */
  415. int devfreq_remove_device(struct devfreq *devfreq)
  416. {
  417. if (!devfreq)
  418. return -EINVAL;
  419. _remove_devfreq(devfreq, false);
  420. return 0;
  421. }
  422. EXPORT_SYMBOL(devfreq_remove_device);
  423. /**
  424. * devfreq_suspend_device() - Suspend devfreq of a device.
  425. * @devfreq: the devfreq instance to be suspended
  426. */
  427. int devfreq_suspend_device(struct devfreq *devfreq)
  428. {
  429. if (!devfreq)
  430. return -EINVAL;
  431. return devfreq->governor->event_handler(devfreq,
  432. DEVFREQ_GOV_SUSPEND, NULL);
  433. }
  434. EXPORT_SYMBOL(devfreq_suspend_device);
  435. /**
  436. * devfreq_resume_device() - Resume devfreq of a device.
  437. * @devfreq: the devfreq instance to be resumed
  438. */
  439. int devfreq_resume_device(struct devfreq *devfreq)
  440. {
  441. if (!devfreq)
  442. return -EINVAL;
  443. return devfreq->governor->event_handler(devfreq,
  444. DEVFREQ_GOV_RESUME, NULL);
  445. }
  446. EXPORT_SYMBOL(devfreq_resume_device);
  447. static ssize_t show_governor(struct device *dev,
  448. struct device_attribute *attr, char *buf)
  449. {
  450. return sprintf(buf, "%s\n", to_devfreq(dev)->governor->name);
  451. }
  452. static ssize_t show_freq(struct device *dev,
  453. struct device_attribute *attr, char *buf)
  454. {
  455. unsigned long freq;
  456. struct devfreq *devfreq = to_devfreq(dev);
  457. if (devfreq->profile->get_cur_freq &&
  458. !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
  459. return sprintf(buf, "%lu\n", freq);
  460. return sprintf(buf, "%lu\n", devfreq->previous_freq);
  461. }
  462. static ssize_t show_target_freq(struct device *dev,
  463. struct device_attribute *attr, char *buf)
  464. {
  465. return sprintf(buf, "%lu\n", to_devfreq(dev)->previous_freq);
  466. }
  467. static ssize_t show_polling_interval(struct device *dev,
  468. struct device_attribute *attr, char *buf)
  469. {
  470. return sprintf(buf, "%d\n", to_devfreq(dev)->profile->polling_ms);
  471. }
  472. static ssize_t store_polling_interval(struct device *dev,
  473. struct device_attribute *attr,
  474. const char *buf, size_t count)
  475. {
  476. struct devfreq *df = to_devfreq(dev);
  477. unsigned int value;
  478. int ret;
  479. ret = sscanf(buf, "%u", &value);
  480. if (ret != 1)
  481. return -EINVAL;
  482. df->governor->event_handler(df, DEVFREQ_GOV_INTERVAL, &value);
  483. ret = count;
  484. return ret;
  485. }
  486. static ssize_t store_min_freq(struct device *dev, struct device_attribute *attr,
  487. const char *buf, size_t count)
  488. {
  489. struct devfreq *df = to_devfreq(dev);
  490. unsigned long value;
  491. int ret;
  492. unsigned long max;
  493. ret = sscanf(buf, "%lu", &value);
  494. if (ret != 1)
  495. return -EINVAL;
  496. mutex_lock(&df->lock);
  497. max = df->max_freq;
  498. if (value && max && value > max) {
  499. ret = -EINVAL;
  500. goto unlock;
  501. }
  502. df->min_freq = value;
  503. update_devfreq(df);
  504. ret = count;
  505. unlock:
  506. mutex_unlock(&df->lock);
  507. return ret;
  508. }
  509. static ssize_t show_min_freq(struct device *dev, struct device_attribute *attr,
  510. char *buf)
  511. {
  512. return sprintf(buf, "%lu\n", to_devfreq(dev)->min_freq);
  513. }
  514. static ssize_t store_max_freq(struct device *dev, struct device_attribute *attr,
  515. const char *buf, size_t count)
  516. {
  517. struct devfreq *df = to_devfreq(dev);
  518. unsigned long value;
  519. int ret;
  520. unsigned long min;
  521. ret = sscanf(buf, "%lu", &value);
  522. if (ret != 1)
  523. return -EINVAL;
  524. mutex_lock(&df->lock);
  525. min = df->min_freq;
  526. if (value && min && value < min) {
  527. ret = -EINVAL;
  528. goto unlock;
  529. }
  530. df->max_freq = value;
  531. update_devfreq(df);
  532. ret = count;
  533. unlock:
  534. mutex_unlock(&df->lock);
  535. return ret;
  536. }
  537. static ssize_t show_max_freq(struct device *dev, struct device_attribute *attr,
  538. char *buf)
  539. {
  540. return sprintf(buf, "%lu\n", to_devfreq(dev)->max_freq);
  541. }
  542. static ssize_t show_available_freqs(struct device *d,
  543. struct device_attribute *attr,
  544. char *buf)
  545. {
  546. struct devfreq *df = to_devfreq(d);
  547. struct device *dev = df->dev.parent;
  548. struct opp *opp;
  549. ssize_t count = 0;
  550. unsigned long freq = 0;
  551. rcu_read_lock();
  552. do {
  553. opp = opp_find_freq_ceil(dev, &freq);
  554. if (IS_ERR(opp))
  555. break;
  556. count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
  557. "%lu ", freq);
  558. freq++;
  559. } while (1);
  560. rcu_read_unlock();
  561. /* Truncate the trailing space */
  562. if (count)
  563. count--;
  564. count += sprintf(&buf[count], "\n");
  565. return count;
  566. }
  567. static ssize_t show_trans_table(struct device *dev, struct device_attribute *attr,
  568. char *buf)
  569. {
  570. struct devfreq *devfreq = to_devfreq(dev);
  571. ssize_t len;
  572. int i, j, err;
  573. unsigned int max_state = devfreq->profile->max_state;
  574. err = devfreq_update_status(devfreq, devfreq->previous_freq);
  575. if (err)
  576. return 0;
  577. len = sprintf(buf, " From : To\n");
  578. len += sprintf(buf + len, " :");
  579. for (i = 0; i < max_state; i++)
  580. len += sprintf(buf + len, "%8u",
  581. devfreq->profile->freq_table[i]);
  582. len += sprintf(buf + len, " time(ms)\n");
  583. for (i = 0; i < max_state; i++) {
  584. if (devfreq->profile->freq_table[i]
  585. == devfreq->previous_freq) {
  586. len += sprintf(buf + len, "*");
  587. } else {
  588. len += sprintf(buf + len, " ");
  589. }
  590. len += sprintf(buf + len, "%8u:",
  591. devfreq->profile->freq_table[i]);
  592. for (j = 0; j < max_state; j++)
  593. len += sprintf(buf + len, "%8u",
  594. devfreq->trans_table[(i * max_state) + j]);
  595. len += sprintf(buf + len, "%10u\n",
  596. jiffies_to_msecs(devfreq->time_in_state[i]));
  597. }
  598. len += sprintf(buf + len, "Total transition : %u\n",
  599. devfreq->total_trans);
  600. return len;
  601. }
  602. static struct device_attribute devfreq_attrs[] = {
  603. __ATTR(governor, S_IRUGO, show_governor, NULL),
  604. __ATTR(cur_freq, S_IRUGO, show_freq, NULL),
  605. __ATTR(available_frequencies, S_IRUGO, show_available_freqs, NULL),
  606. __ATTR(target_freq, S_IRUGO, show_target_freq, NULL),
  607. __ATTR(polling_interval, S_IRUGO | S_IWUSR, show_polling_interval,
  608. store_polling_interval),
  609. __ATTR(min_freq, S_IRUGO | S_IWUSR, show_min_freq, store_min_freq),
  610. __ATTR(max_freq, S_IRUGO | S_IWUSR, show_max_freq, store_max_freq),
  611. __ATTR(trans_stat, S_IRUGO, show_trans_table, NULL),
  612. { },
  613. };
  614. static int __init devfreq_init(void)
  615. {
  616. devfreq_class = class_create(THIS_MODULE, "devfreq");
  617. if (IS_ERR(devfreq_class)) {
  618. pr_err("%s: couldn't create class\n", __FILE__);
  619. return PTR_ERR(devfreq_class);
  620. }
  621. devfreq_wq = create_freezable_workqueue("devfreq_wq");
  622. if (IS_ERR(devfreq_wq)) {
  623. class_destroy(devfreq_class);
  624. pr_err("%s: couldn't create workqueue\n", __FILE__);
  625. return PTR_ERR(devfreq_wq);
  626. }
  627. devfreq_class->dev_attrs = devfreq_attrs;
  628. return 0;
  629. }
  630. subsys_initcall(devfreq_init);
  631. static void __exit devfreq_exit(void)
  632. {
  633. class_destroy(devfreq_class);
  634. destroy_workqueue(devfreq_wq);
  635. }
  636. module_exit(devfreq_exit);
  637. /*
  638. * The followings are helper functions for devfreq user device drivers with
  639. * OPP framework.
  640. */
  641. /**
  642. * devfreq_recommended_opp() - Helper function to get proper OPP for the
  643. * freq value given to target callback.
  644. * @dev: The devfreq user device. (parent of devfreq)
  645. * @freq: The frequency given to target function
  646. * @flags: Flags handed from devfreq framework.
  647. *
  648. */
  649. struct opp *devfreq_recommended_opp(struct device *dev, unsigned long *freq,
  650. u32 flags)
  651. {
  652. struct opp *opp;
  653. if (flags & DEVFREQ_FLAG_LEAST_UPPER_BOUND) {
  654. /* The freq is an upper bound. opp should be lower */
  655. opp = opp_find_freq_floor(dev, freq);
  656. /* If not available, use the closest opp */
  657. if (opp == ERR_PTR(-ENODEV))
  658. opp = opp_find_freq_ceil(dev, freq);
  659. } else {
  660. /* The freq is an lower bound. opp should be higher */
  661. opp = opp_find_freq_ceil(dev, freq);
  662. /* If not available, use the closest opp */
  663. if (opp == ERR_PTR(-ENODEV))
  664. opp = opp_find_freq_floor(dev, freq);
  665. }
  666. return opp;
  667. }
  668. /**
  669. * devfreq_register_opp_notifier() - Helper function to get devfreq notified
  670. * for any changes in the OPP availability
  671. * changes
  672. * @dev: The devfreq user device. (parent of devfreq)
  673. * @devfreq: The devfreq object.
  674. */
  675. int devfreq_register_opp_notifier(struct device *dev, struct devfreq *devfreq)
  676. {
  677. struct srcu_notifier_head *nh = opp_get_notifier(dev);
  678. if (IS_ERR(nh))
  679. return PTR_ERR(nh);
  680. return srcu_notifier_chain_register(nh, &devfreq->nb);
  681. }
  682. /**
  683. * devfreq_unregister_opp_notifier() - Helper function to stop getting devfreq
  684. * notified for any changes in the OPP
  685. * availability changes anymore.
  686. * @dev: The devfreq user device. (parent of devfreq)
  687. * @devfreq: The devfreq object.
  688. *
  689. * At exit() callback of devfreq_dev_profile, this must be included if
  690. * devfreq_recommended_opp is used.
  691. */
  692. int devfreq_unregister_opp_notifier(struct device *dev, struct devfreq *devfreq)
  693. {
  694. struct srcu_notifier_head *nh = opp_get_notifier(dev);
  695. if (IS_ERR(nh))
  696. return PTR_ERR(nh);
  697. return srcu_notifier_chain_unregister(nh, &devfreq->nb);
  698. }
  699. MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
  700. MODULE_DESCRIPTION("devfreq class support");
  701. MODULE_LICENSE("GPL");