devfreq.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535
  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/export.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 <linux/of.h>
  28. #include "governor.h"
  29. #define MAX(a,b) ((a > b) ? a : b)
  30. #define MIN(a,b) ((a < b) ? a : b)
  31. static struct class *devfreq_class;
  32. /*
  33. * devfreq core provides delayed work based load monitoring helper
  34. * functions. Governors can use these or can implement their own
  35. * monitoring mechanism.
  36. */
  37. static struct workqueue_struct *devfreq_wq;
  38. /* The list of all device-devfreq governors */
  39. static LIST_HEAD(devfreq_governor_list);
  40. /* The list of all device-devfreq */
  41. static LIST_HEAD(devfreq_list);
  42. static DEFINE_MUTEX(devfreq_list_lock);
  43. /**
  44. * find_device_devfreq() - find devfreq struct using device pointer
  45. * @dev: device pointer used to lookup device devfreq.
  46. *
  47. * Search the list of device devfreqs and return the matched device's
  48. * devfreq info. devfreq_list_lock should be held by the caller.
  49. */
  50. static struct devfreq *find_device_devfreq(struct device *dev)
  51. {
  52. struct devfreq *tmp_devfreq;
  53. if (IS_ERR_OR_NULL(dev)) {
  54. pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
  55. return ERR_PTR(-EINVAL);
  56. }
  57. WARN(!mutex_is_locked(&devfreq_list_lock),
  58. "devfreq_list_lock must be locked.");
  59. list_for_each_entry(tmp_devfreq, &devfreq_list, node) {
  60. if (tmp_devfreq->dev.parent == dev)
  61. return tmp_devfreq;
  62. }
  63. return ERR_PTR(-ENODEV);
  64. }
  65. static unsigned long find_available_min_freq(struct devfreq *devfreq)
  66. {
  67. struct dev_pm_opp *opp;
  68. unsigned long min_freq = 0;
  69. opp = dev_pm_opp_find_freq_ceil(devfreq->dev.parent, &min_freq);
  70. if (IS_ERR(opp))
  71. min_freq = 0;
  72. else
  73. dev_pm_opp_put(opp);
  74. return min_freq;
  75. }
  76. static unsigned long find_available_max_freq(struct devfreq *devfreq)
  77. {
  78. struct dev_pm_opp *opp;
  79. unsigned long max_freq = ULONG_MAX;
  80. opp = dev_pm_opp_find_freq_floor(devfreq->dev.parent, &max_freq);
  81. if (IS_ERR(opp))
  82. max_freq = 0;
  83. else
  84. dev_pm_opp_put(opp);
  85. return max_freq;
  86. }
  87. /**
  88. * devfreq_get_freq_level() - Lookup freq_table for the frequency
  89. * @devfreq: the devfreq instance
  90. * @freq: the target frequency
  91. */
  92. static int devfreq_get_freq_level(struct devfreq *devfreq, unsigned long freq)
  93. {
  94. int lev;
  95. for (lev = 0; lev < devfreq->profile->max_state; lev++)
  96. if (freq == devfreq->profile->freq_table[lev])
  97. return lev;
  98. return -EINVAL;
  99. }
  100. static int set_freq_table(struct devfreq *devfreq)
  101. {
  102. struct devfreq_dev_profile *profile = devfreq->profile;
  103. struct dev_pm_opp *opp;
  104. unsigned long freq;
  105. int i, count;
  106. /* Initialize the freq_table from OPP table */
  107. count = dev_pm_opp_get_opp_count(devfreq->dev.parent);
  108. if (count <= 0)
  109. return -EINVAL;
  110. profile->max_state = count;
  111. profile->freq_table = devm_kcalloc(devfreq->dev.parent,
  112. profile->max_state,
  113. sizeof(*profile->freq_table),
  114. GFP_KERNEL);
  115. if (!profile->freq_table) {
  116. profile->max_state = 0;
  117. return -ENOMEM;
  118. }
  119. for (i = 0, freq = 0; i < profile->max_state; i++, freq++) {
  120. opp = dev_pm_opp_find_freq_ceil(devfreq->dev.parent, &freq);
  121. if (IS_ERR(opp)) {
  122. devm_kfree(devfreq->dev.parent, profile->freq_table);
  123. profile->max_state = 0;
  124. return PTR_ERR(opp);
  125. }
  126. dev_pm_opp_put(opp);
  127. profile->freq_table[i] = freq;
  128. }
  129. return 0;
  130. }
  131. /**
  132. * devfreq_update_status() - Update statistics of devfreq behavior
  133. * @devfreq: the devfreq instance
  134. * @freq: the update target frequency
  135. */
  136. int devfreq_update_status(struct devfreq *devfreq, unsigned long freq)
  137. {
  138. int lev, prev_lev, ret = 0;
  139. unsigned long cur_time;
  140. cur_time = jiffies;
  141. /* Immediately exit if previous_freq is not initialized yet. */
  142. if (!devfreq->previous_freq)
  143. goto out;
  144. prev_lev = devfreq_get_freq_level(devfreq, devfreq->previous_freq);
  145. if (prev_lev < 0) {
  146. ret = prev_lev;
  147. goto out;
  148. }
  149. devfreq->time_in_state[prev_lev] +=
  150. cur_time - devfreq->last_stat_updated;
  151. lev = devfreq_get_freq_level(devfreq, freq);
  152. if (lev < 0) {
  153. ret = lev;
  154. goto out;
  155. }
  156. if (lev != prev_lev) {
  157. devfreq->trans_table[(prev_lev *
  158. devfreq->profile->max_state) + lev]++;
  159. devfreq->total_trans++;
  160. }
  161. out:
  162. devfreq->last_stat_updated = cur_time;
  163. return ret;
  164. }
  165. EXPORT_SYMBOL(devfreq_update_status);
  166. /**
  167. * find_devfreq_governor() - find devfreq governor from name
  168. * @name: name of the governor
  169. *
  170. * Search the list of devfreq governors and return the matched
  171. * governor's pointer. devfreq_list_lock should be held by the caller.
  172. */
  173. static struct devfreq_governor *find_devfreq_governor(const char *name)
  174. {
  175. struct devfreq_governor *tmp_governor;
  176. if (IS_ERR_OR_NULL(name)) {
  177. pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
  178. return ERR_PTR(-EINVAL);
  179. }
  180. WARN(!mutex_is_locked(&devfreq_list_lock),
  181. "devfreq_list_lock must be locked.");
  182. list_for_each_entry(tmp_governor, &devfreq_governor_list, node) {
  183. if (!strncmp(tmp_governor->name, name, DEVFREQ_NAME_LEN))
  184. return tmp_governor;
  185. }
  186. return ERR_PTR(-ENODEV);
  187. }
  188. static int devfreq_notify_transition(struct devfreq *devfreq,
  189. struct devfreq_freqs *freqs, unsigned int state)
  190. {
  191. if (!devfreq)
  192. return -EINVAL;
  193. switch (state) {
  194. case DEVFREQ_PRECHANGE:
  195. srcu_notifier_call_chain(&devfreq->transition_notifier_list,
  196. DEVFREQ_PRECHANGE, freqs);
  197. break;
  198. case DEVFREQ_POSTCHANGE:
  199. srcu_notifier_call_chain(&devfreq->transition_notifier_list,
  200. DEVFREQ_POSTCHANGE, freqs);
  201. break;
  202. default:
  203. return -EINVAL;
  204. }
  205. return 0;
  206. }
  207. /* Load monitoring helper functions for governors use */
  208. /**
  209. * update_devfreq() - Reevaluate the device and configure frequency.
  210. * @devfreq: the devfreq instance.
  211. *
  212. * Note: Lock devfreq->lock before calling update_devfreq
  213. * This function is exported for governors.
  214. */
  215. int update_devfreq(struct devfreq *devfreq)
  216. {
  217. struct devfreq_freqs freqs;
  218. unsigned long freq, cur_freq, min_freq, max_freq;
  219. int err = 0;
  220. u32 flags = 0;
  221. if (!mutex_is_locked(&devfreq->lock)) {
  222. WARN(true, "devfreq->lock must be locked by the caller.\n");
  223. return -EINVAL;
  224. }
  225. if (!devfreq->governor)
  226. return -EINVAL;
  227. /* Reevaluate the proper frequency */
  228. err = devfreq->governor->get_target_freq(devfreq, &freq);
  229. if (err)
  230. return err;
  231. /*
  232. * Adjust the frequency with user freq, QoS and available freq.
  233. *
  234. * List from the highest priority
  235. * max_freq
  236. * min_freq
  237. */
  238. max_freq = MIN(devfreq->scaling_max_freq, devfreq->max_freq);
  239. min_freq = MAX(devfreq->scaling_min_freq, devfreq->min_freq);
  240. if (min_freq && freq < min_freq) {
  241. freq = min_freq;
  242. flags &= ~DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use GLB */
  243. }
  244. if (max_freq && freq > max_freq) {
  245. freq = max_freq;
  246. flags |= DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use LUB */
  247. }
  248. if (devfreq->profile->get_cur_freq)
  249. devfreq->profile->get_cur_freq(devfreq->dev.parent, &cur_freq);
  250. else
  251. cur_freq = devfreq->previous_freq;
  252. freqs.old = cur_freq;
  253. freqs.new = freq;
  254. devfreq_notify_transition(devfreq, &freqs, DEVFREQ_PRECHANGE);
  255. err = devfreq->profile->target(devfreq->dev.parent, &freq, flags);
  256. if (err) {
  257. freqs.new = cur_freq;
  258. devfreq_notify_transition(devfreq, &freqs, DEVFREQ_POSTCHANGE);
  259. return err;
  260. }
  261. freqs.new = freq;
  262. devfreq_notify_transition(devfreq, &freqs, DEVFREQ_POSTCHANGE);
  263. if (devfreq_update_status(devfreq, freq))
  264. dev_err(&devfreq->dev,
  265. "Couldn't update frequency transition information.\n");
  266. devfreq->previous_freq = freq;
  267. return err;
  268. }
  269. EXPORT_SYMBOL(update_devfreq);
  270. /**
  271. * devfreq_monitor() - Periodically poll devfreq objects.
  272. * @work: the work struct used to run devfreq_monitor periodically.
  273. *
  274. */
  275. static void devfreq_monitor(struct work_struct *work)
  276. {
  277. int err;
  278. struct devfreq *devfreq = container_of(work,
  279. struct devfreq, work.work);
  280. mutex_lock(&devfreq->lock);
  281. err = update_devfreq(devfreq);
  282. if (err)
  283. dev_err(&devfreq->dev, "dvfs failed with (%d) error\n", err);
  284. queue_delayed_work(devfreq_wq, &devfreq->work,
  285. msecs_to_jiffies(devfreq->profile->polling_ms));
  286. mutex_unlock(&devfreq->lock);
  287. }
  288. /**
  289. * devfreq_monitor_start() - Start load monitoring of devfreq instance
  290. * @devfreq: the devfreq instance.
  291. *
  292. * Helper function for starting devfreq device load monitoing. By
  293. * default delayed work based monitoring is supported. Function
  294. * to be called from governor in response to DEVFREQ_GOV_START
  295. * event when device is added to devfreq framework.
  296. */
  297. void devfreq_monitor_start(struct devfreq *devfreq)
  298. {
  299. INIT_DEFERRABLE_WORK(&devfreq->work, devfreq_monitor);
  300. if (devfreq->profile->polling_ms)
  301. queue_delayed_work(devfreq_wq, &devfreq->work,
  302. msecs_to_jiffies(devfreq->profile->polling_ms));
  303. }
  304. EXPORT_SYMBOL(devfreq_monitor_start);
  305. /**
  306. * devfreq_monitor_stop() - Stop load monitoring of a devfreq instance
  307. * @devfreq: the devfreq instance.
  308. *
  309. * Helper function to stop devfreq device load monitoing. Function
  310. * to be called from governor in response to DEVFREQ_GOV_STOP
  311. * event when device is removed from devfreq framework.
  312. */
  313. void devfreq_monitor_stop(struct devfreq *devfreq)
  314. {
  315. cancel_delayed_work_sync(&devfreq->work);
  316. }
  317. EXPORT_SYMBOL(devfreq_monitor_stop);
  318. /**
  319. * devfreq_monitor_suspend() - Suspend load monitoring of a devfreq instance
  320. * @devfreq: the devfreq instance.
  321. *
  322. * Helper function to suspend devfreq device load monitoing. Function
  323. * to be called from governor in response to DEVFREQ_GOV_SUSPEND
  324. * event or when polling interval is set to zero.
  325. *
  326. * Note: Though this function is same as devfreq_monitor_stop(),
  327. * intentionally kept separate to provide hooks for collecting
  328. * transition statistics.
  329. */
  330. void devfreq_monitor_suspend(struct devfreq *devfreq)
  331. {
  332. mutex_lock(&devfreq->lock);
  333. if (devfreq->stop_polling) {
  334. mutex_unlock(&devfreq->lock);
  335. return;
  336. }
  337. devfreq_update_status(devfreq, devfreq->previous_freq);
  338. devfreq->stop_polling = true;
  339. mutex_unlock(&devfreq->lock);
  340. cancel_delayed_work_sync(&devfreq->work);
  341. }
  342. EXPORT_SYMBOL(devfreq_monitor_suspend);
  343. /**
  344. * devfreq_monitor_resume() - Resume load monitoring of a devfreq instance
  345. * @devfreq: the devfreq instance.
  346. *
  347. * Helper function to resume devfreq device load monitoing. Function
  348. * to be called from governor in response to DEVFREQ_GOV_RESUME
  349. * event or when polling interval is set to non-zero.
  350. */
  351. void devfreq_monitor_resume(struct devfreq *devfreq)
  352. {
  353. unsigned long freq;
  354. mutex_lock(&devfreq->lock);
  355. if (!devfreq->stop_polling)
  356. goto out;
  357. if (!delayed_work_pending(&devfreq->work) &&
  358. devfreq->profile->polling_ms)
  359. queue_delayed_work(devfreq_wq, &devfreq->work,
  360. msecs_to_jiffies(devfreq->profile->polling_ms));
  361. devfreq->last_stat_updated = jiffies;
  362. devfreq->stop_polling = false;
  363. if (devfreq->profile->get_cur_freq &&
  364. !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
  365. devfreq->previous_freq = freq;
  366. out:
  367. mutex_unlock(&devfreq->lock);
  368. }
  369. EXPORT_SYMBOL(devfreq_monitor_resume);
  370. /**
  371. * devfreq_interval_update() - Update device devfreq monitoring interval
  372. * @devfreq: the devfreq instance.
  373. * @delay: new polling interval to be set.
  374. *
  375. * Helper function to set new load monitoring polling interval. Function
  376. * to be called from governor in response to DEVFREQ_GOV_INTERVAL event.
  377. */
  378. void devfreq_interval_update(struct devfreq *devfreq, unsigned int *delay)
  379. {
  380. unsigned int cur_delay = devfreq->profile->polling_ms;
  381. unsigned int new_delay = *delay;
  382. mutex_lock(&devfreq->lock);
  383. devfreq->profile->polling_ms = new_delay;
  384. if (devfreq->stop_polling)
  385. goto out;
  386. /* if new delay is zero, stop polling */
  387. if (!new_delay) {
  388. mutex_unlock(&devfreq->lock);
  389. cancel_delayed_work_sync(&devfreq->work);
  390. return;
  391. }
  392. /* if current delay is zero, start polling with new delay */
  393. if (!cur_delay) {
  394. queue_delayed_work(devfreq_wq, &devfreq->work,
  395. msecs_to_jiffies(devfreq->profile->polling_ms));
  396. goto out;
  397. }
  398. /* if current delay is greater than new delay, restart polling */
  399. if (cur_delay > new_delay) {
  400. mutex_unlock(&devfreq->lock);
  401. cancel_delayed_work_sync(&devfreq->work);
  402. mutex_lock(&devfreq->lock);
  403. if (!devfreq->stop_polling)
  404. queue_delayed_work(devfreq_wq, &devfreq->work,
  405. msecs_to_jiffies(devfreq->profile->polling_ms));
  406. }
  407. out:
  408. mutex_unlock(&devfreq->lock);
  409. }
  410. EXPORT_SYMBOL(devfreq_interval_update);
  411. /**
  412. * devfreq_notifier_call() - Notify that the device frequency requirements
  413. * has been changed out of devfreq framework.
  414. * @nb: the notifier_block (supposed to be devfreq->nb)
  415. * @type: not used
  416. * @devp: not used
  417. *
  418. * Called by a notifier that uses devfreq->nb.
  419. */
  420. static int devfreq_notifier_call(struct notifier_block *nb, unsigned long type,
  421. void *devp)
  422. {
  423. struct devfreq *devfreq = container_of(nb, struct devfreq, nb);
  424. int ret;
  425. mutex_lock(&devfreq->lock);
  426. devfreq->scaling_min_freq = find_available_min_freq(devfreq);
  427. if (!devfreq->scaling_min_freq) {
  428. mutex_unlock(&devfreq->lock);
  429. return -EINVAL;
  430. }
  431. devfreq->scaling_max_freq = find_available_max_freq(devfreq);
  432. if (!devfreq->scaling_max_freq) {
  433. mutex_unlock(&devfreq->lock);
  434. return -EINVAL;
  435. }
  436. ret = update_devfreq(devfreq);
  437. mutex_unlock(&devfreq->lock);
  438. return ret;
  439. }
  440. /**
  441. * devfreq_dev_release() - Callback for struct device to release the device.
  442. * @dev: the devfreq device
  443. *
  444. * Remove devfreq from the list and release its resources.
  445. */
  446. static void devfreq_dev_release(struct device *dev)
  447. {
  448. struct devfreq *devfreq = to_devfreq(dev);
  449. mutex_lock(&devfreq_list_lock);
  450. if (IS_ERR(find_device_devfreq(devfreq->dev.parent))) {
  451. mutex_unlock(&devfreq_list_lock);
  452. dev_warn(&devfreq->dev, "releasing devfreq which doesn't exist\n");
  453. return;
  454. }
  455. list_del(&devfreq->node);
  456. mutex_unlock(&devfreq_list_lock);
  457. if (devfreq->governor)
  458. devfreq->governor->event_handler(devfreq,
  459. DEVFREQ_GOV_STOP, NULL);
  460. if (devfreq->profile->exit)
  461. devfreq->profile->exit(devfreq->dev.parent);
  462. mutex_destroy(&devfreq->lock);
  463. kfree(devfreq);
  464. }
  465. /**
  466. * devfreq_add_device() - Add devfreq feature to the device
  467. * @dev: the device to add devfreq feature.
  468. * @profile: device-specific profile to run devfreq.
  469. * @governor_name: name of the policy to choose frequency.
  470. * @data: private data for the governor. The devfreq framework does not
  471. * touch this value.
  472. */
  473. struct devfreq *devfreq_add_device(struct device *dev,
  474. struct devfreq_dev_profile *profile,
  475. const char *governor_name,
  476. void *data)
  477. {
  478. struct devfreq *devfreq;
  479. struct devfreq_governor *governor;
  480. static atomic_t devfreq_no = ATOMIC_INIT(-1);
  481. int err = 0;
  482. if (!dev || !profile || !governor_name) {
  483. dev_err(dev, "%s: Invalid parameters.\n", __func__);
  484. return ERR_PTR(-EINVAL);
  485. }
  486. mutex_lock(&devfreq_list_lock);
  487. devfreq = find_device_devfreq(dev);
  488. mutex_unlock(&devfreq_list_lock);
  489. if (!IS_ERR(devfreq)) {
  490. dev_err(dev, "%s: Unable to create devfreq for the device.\n",
  491. __func__);
  492. err = -EINVAL;
  493. goto err_out;
  494. }
  495. devfreq = kzalloc(sizeof(struct devfreq), GFP_KERNEL);
  496. if (!devfreq) {
  497. err = -ENOMEM;
  498. goto err_out;
  499. }
  500. mutex_init(&devfreq->lock);
  501. mutex_lock(&devfreq->lock);
  502. devfreq->dev.parent = dev;
  503. devfreq->dev.class = devfreq_class;
  504. devfreq->dev.release = devfreq_dev_release;
  505. devfreq->profile = profile;
  506. strncpy(devfreq->governor_name, governor_name, DEVFREQ_NAME_LEN);
  507. devfreq->previous_freq = profile->initial_freq;
  508. devfreq->last_status.current_frequency = profile->initial_freq;
  509. devfreq->data = data;
  510. devfreq->nb.notifier_call = devfreq_notifier_call;
  511. if (!devfreq->profile->max_state && !devfreq->profile->freq_table) {
  512. mutex_unlock(&devfreq->lock);
  513. err = set_freq_table(devfreq);
  514. if (err < 0)
  515. goto err_out;
  516. mutex_lock(&devfreq->lock);
  517. }
  518. devfreq->min_freq = find_available_min_freq(devfreq);
  519. if (!devfreq->min_freq) {
  520. mutex_unlock(&devfreq->lock);
  521. err = -EINVAL;
  522. goto err_dev;
  523. }
  524. devfreq->scaling_min_freq = devfreq->min_freq;
  525. devfreq->max_freq = find_available_max_freq(devfreq);
  526. if (!devfreq->max_freq) {
  527. mutex_unlock(&devfreq->lock);
  528. err = -EINVAL;
  529. goto err_dev;
  530. }
  531. devfreq->scaling_max_freq = devfreq->max_freq;
  532. dev_set_name(&devfreq->dev, "devfreq%d",
  533. atomic_inc_return(&devfreq_no));
  534. err = device_register(&devfreq->dev);
  535. if (err) {
  536. mutex_unlock(&devfreq->lock);
  537. goto err_dev;
  538. }
  539. devfreq->trans_table =
  540. devm_kzalloc(&devfreq->dev,
  541. array3_size(sizeof(unsigned int),
  542. devfreq->profile->max_state,
  543. devfreq->profile->max_state),
  544. GFP_KERNEL);
  545. devfreq->time_in_state = devm_kcalloc(&devfreq->dev,
  546. devfreq->profile->max_state,
  547. sizeof(unsigned long),
  548. GFP_KERNEL);
  549. devfreq->last_stat_updated = jiffies;
  550. srcu_init_notifier_head(&devfreq->transition_notifier_list);
  551. mutex_unlock(&devfreq->lock);
  552. mutex_lock(&devfreq_list_lock);
  553. list_add(&devfreq->node, &devfreq_list);
  554. governor = find_devfreq_governor(devfreq->governor_name);
  555. if (IS_ERR(governor)) {
  556. dev_err(dev, "%s: Unable to find governor for the device\n",
  557. __func__);
  558. err = PTR_ERR(governor);
  559. goto err_init;
  560. }
  561. devfreq->governor = governor;
  562. err = devfreq->governor->event_handler(devfreq, DEVFREQ_GOV_START,
  563. NULL);
  564. if (err) {
  565. dev_err(dev, "%s: Unable to start governor for the device\n",
  566. __func__);
  567. goto err_init;
  568. }
  569. mutex_unlock(&devfreq_list_lock);
  570. return devfreq;
  571. err_init:
  572. list_del(&devfreq->node);
  573. mutex_unlock(&devfreq_list_lock);
  574. device_unregister(&devfreq->dev);
  575. err_dev:
  576. if (devfreq)
  577. kfree(devfreq);
  578. err_out:
  579. return ERR_PTR(err);
  580. }
  581. EXPORT_SYMBOL(devfreq_add_device);
  582. /**
  583. * devfreq_remove_device() - Remove devfreq feature from a device.
  584. * @devfreq: the devfreq instance to be removed
  585. *
  586. * The opposite of devfreq_add_device().
  587. */
  588. int devfreq_remove_device(struct devfreq *devfreq)
  589. {
  590. if (!devfreq)
  591. return -EINVAL;
  592. device_unregister(&devfreq->dev);
  593. return 0;
  594. }
  595. EXPORT_SYMBOL(devfreq_remove_device);
  596. static int devm_devfreq_dev_match(struct device *dev, void *res, void *data)
  597. {
  598. struct devfreq **r = res;
  599. if (WARN_ON(!r || !*r))
  600. return 0;
  601. return *r == data;
  602. }
  603. static void devm_devfreq_dev_release(struct device *dev, void *res)
  604. {
  605. devfreq_remove_device(*(struct devfreq **)res);
  606. }
  607. /**
  608. * devm_devfreq_add_device() - Resource-managed devfreq_add_device()
  609. * @dev: the device to add devfreq feature.
  610. * @profile: device-specific profile to run devfreq.
  611. * @governor_name: name of the policy to choose frequency.
  612. * @data: private data for the governor. The devfreq framework does not
  613. * touch this value.
  614. *
  615. * This function manages automatically the memory of devfreq device using device
  616. * resource management and simplify the free operation for memory of devfreq
  617. * device.
  618. */
  619. struct devfreq *devm_devfreq_add_device(struct device *dev,
  620. struct devfreq_dev_profile *profile,
  621. const char *governor_name,
  622. void *data)
  623. {
  624. struct devfreq **ptr, *devfreq;
  625. ptr = devres_alloc(devm_devfreq_dev_release, sizeof(*ptr), GFP_KERNEL);
  626. if (!ptr)
  627. return ERR_PTR(-ENOMEM);
  628. devfreq = devfreq_add_device(dev, profile, governor_name, data);
  629. if (IS_ERR(devfreq)) {
  630. devres_free(ptr);
  631. return devfreq;
  632. }
  633. *ptr = devfreq;
  634. devres_add(dev, ptr);
  635. return devfreq;
  636. }
  637. EXPORT_SYMBOL(devm_devfreq_add_device);
  638. #ifdef CONFIG_OF
  639. /*
  640. * devfreq_get_devfreq_by_phandle - Get the devfreq device from devicetree
  641. * @dev - instance to the given device
  642. * @index - index into list of devfreq
  643. *
  644. * return the instance of devfreq device
  645. */
  646. struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, int index)
  647. {
  648. struct device_node *node;
  649. struct devfreq *devfreq;
  650. if (!dev)
  651. return ERR_PTR(-EINVAL);
  652. if (!dev->of_node)
  653. return ERR_PTR(-EINVAL);
  654. node = of_parse_phandle(dev->of_node, "devfreq", index);
  655. if (!node)
  656. return ERR_PTR(-ENODEV);
  657. mutex_lock(&devfreq_list_lock);
  658. list_for_each_entry(devfreq, &devfreq_list, node) {
  659. if (devfreq->dev.parent
  660. && devfreq->dev.parent->of_node == node) {
  661. mutex_unlock(&devfreq_list_lock);
  662. of_node_put(node);
  663. return devfreq;
  664. }
  665. }
  666. mutex_unlock(&devfreq_list_lock);
  667. of_node_put(node);
  668. return ERR_PTR(-EPROBE_DEFER);
  669. }
  670. #else
  671. struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, int index)
  672. {
  673. return ERR_PTR(-ENODEV);
  674. }
  675. #endif /* CONFIG_OF */
  676. EXPORT_SYMBOL_GPL(devfreq_get_devfreq_by_phandle);
  677. /**
  678. * devm_devfreq_remove_device() - Resource-managed devfreq_remove_device()
  679. * @dev: the device to add devfreq feature.
  680. * @devfreq: the devfreq instance to be removed
  681. */
  682. void devm_devfreq_remove_device(struct device *dev, struct devfreq *devfreq)
  683. {
  684. WARN_ON(devres_release(dev, devm_devfreq_dev_release,
  685. devm_devfreq_dev_match, devfreq));
  686. }
  687. EXPORT_SYMBOL(devm_devfreq_remove_device);
  688. /**
  689. * devfreq_suspend_device() - Suspend devfreq of a device.
  690. * @devfreq: the devfreq instance to be suspended
  691. *
  692. * This function is intended to be called by the pm callbacks
  693. * (e.g., runtime_suspend, suspend) of the device driver that
  694. * holds the devfreq.
  695. */
  696. int devfreq_suspend_device(struct devfreq *devfreq)
  697. {
  698. if (!devfreq)
  699. return -EINVAL;
  700. if (!devfreq->governor)
  701. return 0;
  702. return devfreq->governor->event_handler(devfreq,
  703. DEVFREQ_GOV_SUSPEND, NULL);
  704. }
  705. EXPORT_SYMBOL(devfreq_suspend_device);
  706. /**
  707. * devfreq_resume_device() - Resume devfreq of a device.
  708. * @devfreq: the devfreq instance to be resumed
  709. *
  710. * This function is intended to be called by the pm callbacks
  711. * (e.g., runtime_resume, resume) of the device driver that
  712. * holds the devfreq.
  713. */
  714. int devfreq_resume_device(struct devfreq *devfreq)
  715. {
  716. if (!devfreq)
  717. return -EINVAL;
  718. if (!devfreq->governor)
  719. return 0;
  720. return devfreq->governor->event_handler(devfreq,
  721. DEVFREQ_GOV_RESUME, NULL);
  722. }
  723. EXPORT_SYMBOL(devfreq_resume_device);
  724. /**
  725. * devfreq_add_governor() - Add devfreq governor
  726. * @governor: the devfreq governor to be added
  727. */
  728. int devfreq_add_governor(struct devfreq_governor *governor)
  729. {
  730. struct devfreq_governor *g;
  731. struct devfreq *devfreq;
  732. int err = 0;
  733. if (!governor) {
  734. pr_err("%s: Invalid parameters.\n", __func__);
  735. return -EINVAL;
  736. }
  737. mutex_lock(&devfreq_list_lock);
  738. g = find_devfreq_governor(governor->name);
  739. if (!IS_ERR(g)) {
  740. pr_err("%s: governor %s already registered\n", __func__,
  741. g->name);
  742. err = -EINVAL;
  743. goto err_out;
  744. }
  745. list_add(&governor->node, &devfreq_governor_list);
  746. list_for_each_entry(devfreq, &devfreq_list, node) {
  747. int ret = 0;
  748. struct device *dev = devfreq->dev.parent;
  749. if (!strncmp(devfreq->governor_name, governor->name,
  750. DEVFREQ_NAME_LEN)) {
  751. /* The following should never occur */
  752. if (devfreq->governor) {
  753. dev_warn(dev,
  754. "%s: Governor %s already present\n",
  755. __func__, devfreq->governor->name);
  756. ret = devfreq->governor->event_handler(devfreq,
  757. DEVFREQ_GOV_STOP, NULL);
  758. if (ret) {
  759. dev_warn(dev,
  760. "%s: Governor %s stop = %d\n",
  761. __func__,
  762. devfreq->governor->name, ret);
  763. }
  764. /* Fall through */
  765. }
  766. devfreq->governor = governor;
  767. ret = devfreq->governor->event_handler(devfreq,
  768. DEVFREQ_GOV_START, NULL);
  769. if (ret) {
  770. dev_warn(dev, "%s: Governor %s start=%d\n",
  771. __func__, devfreq->governor->name,
  772. ret);
  773. }
  774. }
  775. }
  776. err_out:
  777. mutex_unlock(&devfreq_list_lock);
  778. return err;
  779. }
  780. EXPORT_SYMBOL(devfreq_add_governor);
  781. /**
  782. * devfreq_remove_governor() - Remove devfreq feature from a device.
  783. * @governor: the devfreq governor to be removed
  784. */
  785. int devfreq_remove_governor(struct devfreq_governor *governor)
  786. {
  787. struct devfreq_governor *g;
  788. struct devfreq *devfreq;
  789. int err = 0;
  790. if (!governor) {
  791. pr_err("%s: Invalid parameters.\n", __func__);
  792. return -EINVAL;
  793. }
  794. mutex_lock(&devfreq_list_lock);
  795. g = find_devfreq_governor(governor->name);
  796. if (IS_ERR(g)) {
  797. pr_err("%s: governor %s not registered\n", __func__,
  798. governor->name);
  799. err = PTR_ERR(g);
  800. goto err_out;
  801. }
  802. list_for_each_entry(devfreq, &devfreq_list, node) {
  803. int ret;
  804. struct device *dev = devfreq->dev.parent;
  805. if (!strncmp(devfreq->governor_name, governor->name,
  806. DEVFREQ_NAME_LEN)) {
  807. /* we should have a devfreq governor! */
  808. if (!devfreq->governor) {
  809. dev_warn(dev, "%s: Governor %s NOT present\n",
  810. __func__, governor->name);
  811. continue;
  812. /* Fall through */
  813. }
  814. ret = devfreq->governor->event_handler(devfreq,
  815. DEVFREQ_GOV_STOP, NULL);
  816. if (ret) {
  817. dev_warn(dev, "%s: Governor %s stop=%d\n",
  818. __func__, devfreq->governor->name,
  819. ret);
  820. }
  821. devfreq->governor = NULL;
  822. }
  823. }
  824. list_del(&governor->node);
  825. err_out:
  826. mutex_unlock(&devfreq_list_lock);
  827. return err;
  828. }
  829. EXPORT_SYMBOL(devfreq_remove_governor);
  830. static ssize_t governor_show(struct device *dev,
  831. struct device_attribute *attr, char *buf)
  832. {
  833. if (!to_devfreq(dev)->governor)
  834. return -EINVAL;
  835. return sprintf(buf, "%s\n", to_devfreq(dev)->governor->name);
  836. }
  837. static ssize_t governor_store(struct device *dev, struct device_attribute *attr,
  838. const char *buf, size_t count)
  839. {
  840. struct devfreq *df = to_devfreq(dev);
  841. int ret;
  842. char str_governor[DEVFREQ_NAME_LEN + 1];
  843. struct devfreq_governor *governor;
  844. ret = sscanf(buf, "%" __stringify(DEVFREQ_NAME_LEN) "s", str_governor);
  845. if (ret != 1)
  846. return -EINVAL;
  847. mutex_lock(&devfreq_list_lock);
  848. governor = find_devfreq_governor(str_governor);
  849. if (IS_ERR(governor)) {
  850. ret = PTR_ERR(governor);
  851. goto out;
  852. }
  853. if (df->governor == governor) {
  854. ret = 0;
  855. goto out;
  856. } else if ((df->governor && df->governor->immutable) ||
  857. governor->immutable) {
  858. ret = -EINVAL;
  859. goto out;
  860. }
  861. if (df->governor) {
  862. ret = df->governor->event_handler(df, DEVFREQ_GOV_STOP, NULL);
  863. if (ret) {
  864. dev_warn(dev, "%s: Governor %s not stopped(%d)\n",
  865. __func__, df->governor->name, ret);
  866. goto out;
  867. }
  868. }
  869. df->governor = governor;
  870. strncpy(df->governor_name, governor->name, DEVFREQ_NAME_LEN);
  871. ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
  872. if (ret)
  873. dev_warn(dev, "%s: Governor %s not started(%d)\n",
  874. __func__, df->governor->name, ret);
  875. out:
  876. mutex_unlock(&devfreq_list_lock);
  877. if (!ret)
  878. ret = count;
  879. return ret;
  880. }
  881. static DEVICE_ATTR_RW(governor);
  882. static ssize_t available_governors_show(struct device *d,
  883. struct device_attribute *attr,
  884. char *buf)
  885. {
  886. struct devfreq *df = to_devfreq(d);
  887. ssize_t count = 0;
  888. mutex_lock(&devfreq_list_lock);
  889. /*
  890. * The devfreq with immutable governor (e.g., passive) shows
  891. * only own governor.
  892. */
  893. if (df->governor->immutable) {
  894. count = scnprintf(&buf[count], DEVFREQ_NAME_LEN,
  895. "%s ", df->governor_name);
  896. /*
  897. * The devfreq device shows the registered governor except for
  898. * immutable governors such as passive governor .
  899. */
  900. } else {
  901. struct devfreq_governor *governor;
  902. list_for_each_entry(governor, &devfreq_governor_list, node) {
  903. if (governor->immutable)
  904. continue;
  905. count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
  906. "%s ", governor->name);
  907. }
  908. }
  909. mutex_unlock(&devfreq_list_lock);
  910. /* Truncate the trailing space */
  911. if (count)
  912. count--;
  913. count += sprintf(&buf[count], "\n");
  914. return count;
  915. }
  916. static DEVICE_ATTR_RO(available_governors);
  917. static ssize_t cur_freq_show(struct device *dev, struct device_attribute *attr,
  918. char *buf)
  919. {
  920. unsigned long freq;
  921. struct devfreq *devfreq = to_devfreq(dev);
  922. if (devfreq->profile->get_cur_freq &&
  923. !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
  924. return sprintf(buf, "%lu\n", freq);
  925. return sprintf(buf, "%lu\n", devfreq->previous_freq);
  926. }
  927. static DEVICE_ATTR_RO(cur_freq);
  928. static ssize_t target_freq_show(struct device *dev,
  929. struct device_attribute *attr, char *buf)
  930. {
  931. return sprintf(buf, "%lu\n", to_devfreq(dev)->previous_freq);
  932. }
  933. static DEVICE_ATTR_RO(target_freq);
  934. static ssize_t polling_interval_show(struct device *dev,
  935. struct device_attribute *attr, char *buf)
  936. {
  937. return sprintf(buf, "%d\n", to_devfreq(dev)->profile->polling_ms);
  938. }
  939. static ssize_t polling_interval_store(struct device *dev,
  940. struct device_attribute *attr,
  941. const char *buf, size_t count)
  942. {
  943. struct devfreq *df = to_devfreq(dev);
  944. unsigned int value;
  945. int ret;
  946. if (!df->governor)
  947. return -EINVAL;
  948. ret = sscanf(buf, "%u", &value);
  949. if (ret != 1)
  950. return -EINVAL;
  951. df->governor->event_handler(df, DEVFREQ_GOV_INTERVAL, &value);
  952. ret = count;
  953. return ret;
  954. }
  955. static DEVICE_ATTR_RW(polling_interval);
  956. static ssize_t min_freq_store(struct device *dev, struct device_attribute *attr,
  957. const char *buf, size_t count)
  958. {
  959. struct devfreq *df = to_devfreq(dev);
  960. unsigned long value;
  961. int ret;
  962. unsigned long max;
  963. ret = sscanf(buf, "%lu", &value);
  964. if (ret != 1)
  965. return -EINVAL;
  966. mutex_lock(&df->lock);
  967. max = df->max_freq;
  968. if (value && max && value > max) {
  969. ret = -EINVAL;
  970. goto unlock;
  971. }
  972. df->min_freq = value;
  973. update_devfreq(df);
  974. ret = count;
  975. unlock:
  976. mutex_unlock(&df->lock);
  977. return ret;
  978. }
  979. static ssize_t min_freq_show(struct device *dev, struct device_attribute *attr,
  980. char *buf)
  981. {
  982. struct devfreq *df = to_devfreq(dev);
  983. return sprintf(buf, "%lu\n", MAX(df->scaling_min_freq, df->min_freq));
  984. }
  985. static ssize_t max_freq_store(struct device *dev, struct device_attribute *attr,
  986. const char *buf, size_t count)
  987. {
  988. struct devfreq *df = to_devfreq(dev);
  989. unsigned long value;
  990. int ret;
  991. unsigned long min;
  992. ret = sscanf(buf, "%lu", &value);
  993. if (ret != 1)
  994. return -EINVAL;
  995. mutex_lock(&df->lock);
  996. min = df->min_freq;
  997. if (value && min && value < min) {
  998. ret = -EINVAL;
  999. goto unlock;
  1000. }
  1001. df->max_freq = value;
  1002. update_devfreq(df);
  1003. ret = count;
  1004. unlock:
  1005. mutex_unlock(&df->lock);
  1006. return ret;
  1007. }
  1008. static DEVICE_ATTR_RW(min_freq);
  1009. static ssize_t max_freq_show(struct device *dev, struct device_attribute *attr,
  1010. char *buf)
  1011. {
  1012. struct devfreq *df = to_devfreq(dev);
  1013. return sprintf(buf, "%lu\n", MIN(df->scaling_max_freq, df->max_freq));
  1014. }
  1015. static DEVICE_ATTR_RW(max_freq);
  1016. static ssize_t available_frequencies_show(struct device *d,
  1017. struct device_attribute *attr,
  1018. char *buf)
  1019. {
  1020. struct devfreq *df = to_devfreq(d);
  1021. ssize_t count = 0;
  1022. int i;
  1023. mutex_lock(&df->lock);
  1024. for (i = 0; i < df->profile->max_state; i++)
  1025. count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
  1026. "%lu ", df->profile->freq_table[i]);
  1027. mutex_unlock(&df->lock);
  1028. /* Truncate the trailing space */
  1029. if (count)
  1030. count--;
  1031. count += sprintf(&buf[count], "\n");
  1032. return count;
  1033. }
  1034. static DEVICE_ATTR_RO(available_frequencies);
  1035. static ssize_t trans_stat_show(struct device *dev,
  1036. struct device_attribute *attr, char *buf)
  1037. {
  1038. struct devfreq *devfreq = to_devfreq(dev);
  1039. ssize_t len;
  1040. int i, j;
  1041. unsigned int max_state = devfreq->profile->max_state;
  1042. if (!devfreq->stop_polling &&
  1043. devfreq_update_status(devfreq, devfreq->previous_freq))
  1044. return 0;
  1045. if (max_state == 0)
  1046. return sprintf(buf, "Not Supported.\n");
  1047. len = sprintf(buf, " From : To\n");
  1048. len += sprintf(buf + len, " :");
  1049. for (i = 0; i < max_state; i++)
  1050. len += sprintf(buf + len, "%10lu",
  1051. devfreq->profile->freq_table[i]);
  1052. len += sprintf(buf + len, " time(ms)\n");
  1053. for (i = 0; i < max_state; i++) {
  1054. if (devfreq->profile->freq_table[i]
  1055. == devfreq->previous_freq) {
  1056. len += sprintf(buf + len, "*");
  1057. } else {
  1058. len += sprintf(buf + len, " ");
  1059. }
  1060. len += sprintf(buf + len, "%10lu:",
  1061. devfreq->profile->freq_table[i]);
  1062. for (j = 0; j < max_state; j++)
  1063. len += sprintf(buf + len, "%10u",
  1064. devfreq->trans_table[(i * max_state) + j]);
  1065. len += sprintf(buf + len, "%10u\n",
  1066. jiffies_to_msecs(devfreq->time_in_state[i]));
  1067. }
  1068. len += sprintf(buf + len, "Total transition : %u\n",
  1069. devfreq->total_trans);
  1070. return len;
  1071. }
  1072. static DEVICE_ATTR_RO(trans_stat);
  1073. static struct attribute *devfreq_attrs[] = {
  1074. &dev_attr_governor.attr,
  1075. &dev_attr_available_governors.attr,
  1076. &dev_attr_cur_freq.attr,
  1077. &dev_attr_available_frequencies.attr,
  1078. &dev_attr_target_freq.attr,
  1079. &dev_attr_polling_interval.attr,
  1080. &dev_attr_min_freq.attr,
  1081. &dev_attr_max_freq.attr,
  1082. &dev_attr_trans_stat.attr,
  1083. NULL,
  1084. };
  1085. ATTRIBUTE_GROUPS(devfreq);
  1086. static int __init devfreq_init(void)
  1087. {
  1088. devfreq_class = class_create(THIS_MODULE, "devfreq");
  1089. if (IS_ERR(devfreq_class)) {
  1090. pr_err("%s: couldn't create class\n", __FILE__);
  1091. return PTR_ERR(devfreq_class);
  1092. }
  1093. devfreq_wq = create_freezable_workqueue("devfreq_wq");
  1094. if (!devfreq_wq) {
  1095. class_destroy(devfreq_class);
  1096. pr_err("%s: couldn't create workqueue\n", __FILE__);
  1097. return -ENOMEM;
  1098. }
  1099. devfreq_class->dev_groups = devfreq_groups;
  1100. return 0;
  1101. }
  1102. subsys_initcall(devfreq_init);
  1103. /*
  1104. * The following are helper functions for devfreq user device drivers with
  1105. * OPP framework.
  1106. */
  1107. /**
  1108. * devfreq_recommended_opp() - Helper function to get proper OPP for the
  1109. * freq value given to target callback.
  1110. * @dev: The devfreq user device. (parent of devfreq)
  1111. * @freq: The frequency given to target function
  1112. * @flags: Flags handed from devfreq framework.
  1113. *
  1114. * The callers are required to call dev_pm_opp_put() for the returned OPP after
  1115. * use.
  1116. */
  1117. struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
  1118. unsigned long *freq,
  1119. u32 flags)
  1120. {
  1121. struct dev_pm_opp *opp;
  1122. if (flags & DEVFREQ_FLAG_LEAST_UPPER_BOUND) {
  1123. /* The freq is an upper bound. opp should be lower */
  1124. opp = dev_pm_opp_find_freq_floor(dev, freq);
  1125. /* If not available, use the closest opp */
  1126. if (opp == ERR_PTR(-ERANGE))
  1127. opp = dev_pm_opp_find_freq_ceil(dev, freq);
  1128. } else {
  1129. /* The freq is an lower bound. opp should be higher */
  1130. opp = dev_pm_opp_find_freq_ceil(dev, freq);
  1131. /* If not available, use the closest opp */
  1132. if (opp == ERR_PTR(-ERANGE))
  1133. opp = dev_pm_opp_find_freq_floor(dev, freq);
  1134. }
  1135. return opp;
  1136. }
  1137. EXPORT_SYMBOL(devfreq_recommended_opp);
  1138. /**
  1139. * devfreq_register_opp_notifier() - Helper function to get devfreq notified
  1140. * for any changes in the OPP availability
  1141. * changes
  1142. * @dev: The devfreq user device. (parent of devfreq)
  1143. * @devfreq: The devfreq object.
  1144. */
  1145. int devfreq_register_opp_notifier(struct device *dev, struct devfreq *devfreq)
  1146. {
  1147. return dev_pm_opp_register_notifier(dev, &devfreq->nb);
  1148. }
  1149. EXPORT_SYMBOL(devfreq_register_opp_notifier);
  1150. /**
  1151. * devfreq_unregister_opp_notifier() - Helper function to stop getting devfreq
  1152. * notified for any changes in the OPP
  1153. * availability changes anymore.
  1154. * @dev: The devfreq user device. (parent of devfreq)
  1155. * @devfreq: The devfreq object.
  1156. *
  1157. * At exit() callback of devfreq_dev_profile, this must be included if
  1158. * devfreq_recommended_opp is used.
  1159. */
  1160. int devfreq_unregister_opp_notifier(struct device *dev, struct devfreq *devfreq)
  1161. {
  1162. return dev_pm_opp_unregister_notifier(dev, &devfreq->nb);
  1163. }
  1164. EXPORT_SYMBOL(devfreq_unregister_opp_notifier);
  1165. static void devm_devfreq_opp_release(struct device *dev, void *res)
  1166. {
  1167. devfreq_unregister_opp_notifier(dev, *(struct devfreq **)res);
  1168. }
  1169. /**
  1170. * devm_ devfreq_register_opp_notifier()
  1171. * - Resource-managed devfreq_register_opp_notifier()
  1172. * @dev: The devfreq user device. (parent of devfreq)
  1173. * @devfreq: The devfreq object.
  1174. */
  1175. int devm_devfreq_register_opp_notifier(struct device *dev,
  1176. struct devfreq *devfreq)
  1177. {
  1178. struct devfreq **ptr;
  1179. int ret;
  1180. ptr = devres_alloc(devm_devfreq_opp_release, sizeof(*ptr), GFP_KERNEL);
  1181. if (!ptr)
  1182. return -ENOMEM;
  1183. ret = devfreq_register_opp_notifier(dev, devfreq);
  1184. if (ret) {
  1185. devres_free(ptr);
  1186. return ret;
  1187. }
  1188. *ptr = devfreq;
  1189. devres_add(dev, ptr);
  1190. return 0;
  1191. }
  1192. EXPORT_SYMBOL(devm_devfreq_register_opp_notifier);
  1193. /**
  1194. * devm_devfreq_unregister_opp_notifier()
  1195. * - Resource-managed devfreq_unregister_opp_notifier()
  1196. * @dev: The devfreq user device. (parent of devfreq)
  1197. * @devfreq: The devfreq object.
  1198. */
  1199. void devm_devfreq_unregister_opp_notifier(struct device *dev,
  1200. struct devfreq *devfreq)
  1201. {
  1202. WARN_ON(devres_release(dev, devm_devfreq_opp_release,
  1203. devm_devfreq_dev_match, devfreq));
  1204. }
  1205. EXPORT_SYMBOL(devm_devfreq_unregister_opp_notifier);
  1206. /**
  1207. * devfreq_register_notifier() - Register a driver with devfreq
  1208. * @devfreq: The devfreq object.
  1209. * @nb: The notifier block to register.
  1210. * @list: DEVFREQ_TRANSITION_NOTIFIER.
  1211. */
  1212. int devfreq_register_notifier(struct devfreq *devfreq,
  1213. struct notifier_block *nb,
  1214. unsigned int list)
  1215. {
  1216. int ret = 0;
  1217. if (!devfreq)
  1218. return -EINVAL;
  1219. switch (list) {
  1220. case DEVFREQ_TRANSITION_NOTIFIER:
  1221. ret = srcu_notifier_chain_register(
  1222. &devfreq->transition_notifier_list, nb);
  1223. break;
  1224. default:
  1225. ret = -EINVAL;
  1226. }
  1227. return ret;
  1228. }
  1229. EXPORT_SYMBOL(devfreq_register_notifier);
  1230. /*
  1231. * devfreq_unregister_notifier() - Unregister a driver with devfreq
  1232. * @devfreq: The devfreq object.
  1233. * @nb: The notifier block to be unregistered.
  1234. * @list: DEVFREQ_TRANSITION_NOTIFIER.
  1235. */
  1236. int devfreq_unregister_notifier(struct devfreq *devfreq,
  1237. struct notifier_block *nb,
  1238. unsigned int list)
  1239. {
  1240. int ret = 0;
  1241. if (!devfreq)
  1242. return -EINVAL;
  1243. switch (list) {
  1244. case DEVFREQ_TRANSITION_NOTIFIER:
  1245. ret = srcu_notifier_chain_unregister(
  1246. &devfreq->transition_notifier_list, nb);
  1247. break;
  1248. default:
  1249. ret = -EINVAL;
  1250. }
  1251. return ret;
  1252. }
  1253. EXPORT_SYMBOL(devfreq_unregister_notifier);
  1254. struct devfreq_notifier_devres {
  1255. struct devfreq *devfreq;
  1256. struct notifier_block *nb;
  1257. unsigned int list;
  1258. };
  1259. static void devm_devfreq_notifier_release(struct device *dev, void *res)
  1260. {
  1261. struct devfreq_notifier_devres *this = res;
  1262. devfreq_unregister_notifier(this->devfreq, this->nb, this->list);
  1263. }
  1264. /**
  1265. * devm_devfreq_register_notifier()
  1266. - Resource-managed devfreq_register_notifier()
  1267. * @dev: The devfreq user device. (parent of devfreq)
  1268. * @devfreq: The devfreq object.
  1269. * @nb: The notifier block to be unregistered.
  1270. * @list: DEVFREQ_TRANSITION_NOTIFIER.
  1271. */
  1272. int devm_devfreq_register_notifier(struct device *dev,
  1273. struct devfreq *devfreq,
  1274. struct notifier_block *nb,
  1275. unsigned int list)
  1276. {
  1277. struct devfreq_notifier_devres *ptr;
  1278. int ret;
  1279. ptr = devres_alloc(devm_devfreq_notifier_release, sizeof(*ptr),
  1280. GFP_KERNEL);
  1281. if (!ptr)
  1282. return -ENOMEM;
  1283. ret = devfreq_register_notifier(devfreq, nb, list);
  1284. if (ret) {
  1285. devres_free(ptr);
  1286. return ret;
  1287. }
  1288. ptr->devfreq = devfreq;
  1289. ptr->nb = nb;
  1290. ptr->list = list;
  1291. devres_add(dev, ptr);
  1292. return 0;
  1293. }
  1294. EXPORT_SYMBOL(devm_devfreq_register_notifier);
  1295. /**
  1296. * devm_devfreq_unregister_notifier()
  1297. - Resource-managed devfreq_unregister_notifier()
  1298. * @dev: The devfreq user device. (parent of devfreq)
  1299. * @devfreq: The devfreq object.
  1300. * @nb: The notifier block to be unregistered.
  1301. * @list: DEVFREQ_TRANSITION_NOTIFIER.
  1302. */
  1303. void devm_devfreq_unregister_notifier(struct device *dev,
  1304. struct devfreq *devfreq,
  1305. struct notifier_block *nb,
  1306. unsigned int list)
  1307. {
  1308. WARN_ON(devres_release(dev, devm_devfreq_notifier_release,
  1309. devm_devfreq_dev_match, devfreq));
  1310. }
  1311. EXPORT_SYMBOL(devm_devfreq_unregister_notifier);