devfreq.c 32 KB

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