devfreq.c 37 KB

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