devfreq.c 30 KB

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