devfreq.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469
  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_out;
  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_out:
  527. return ERR_PTR(err);
  528. }
  529. EXPORT_SYMBOL(devfreq_add_device);
  530. /**
  531. * devfreq_remove_device() - Remove devfreq feature from a device.
  532. * @devfreq: the devfreq instance to be removed
  533. *
  534. * The opposite of devfreq_add_device().
  535. */
  536. int devfreq_remove_device(struct devfreq *devfreq)
  537. {
  538. if (!devfreq)
  539. return -EINVAL;
  540. device_unregister(&devfreq->dev);
  541. return 0;
  542. }
  543. EXPORT_SYMBOL(devfreq_remove_device);
  544. static int devm_devfreq_dev_match(struct device *dev, void *res, void *data)
  545. {
  546. struct devfreq **r = res;
  547. if (WARN_ON(!r || !*r))
  548. return 0;
  549. return *r == data;
  550. }
  551. static void devm_devfreq_dev_release(struct device *dev, void *res)
  552. {
  553. devfreq_remove_device(*(struct devfreq **)res);
  554. }
  555. /**
  556. * devm_devfreq_add_device() - Resource-managed devfreq_add_device()
  557. * @dev: the device to add devfreq feature.
  558. * @profile: device-specific profile to run devfreq.
  559. * @governor_name: name of the policy to choose frequency.
  560. * @data: private data for the governor. The devfreq framework does not
  561. * touch this value.
  562. *
  563. * This function manages automatically the memory of devfreq device using device
  564. * resource management and simplify the free operation for memory of devfreq
  565. * device.
  566. */
  567. struct devfreq *devm_devfreq_add_device(struct device *dev,
  568. struct devfreq_dev_profile *profile,
  569. const char *governor_name,
  570. void *data)
  571. {
  572. struct devfreq **ptr, *devfreq;
  573. ptr = devres_alloc(devm_devfreq_dev_release, sizeof(*ptr), GFP_KERNEL);
  574. if (!ptr)
  575. return ERR_PTR(-ENOMEM);
  576. devfreq = devfreq_add_device(dev, profile, governor_name, data);
  577. if (IS_ERR(devfreq)) {
  578. devres_free(ptr);
  579. return ERR_PTR(-ENOMEM);
  580. }
  581. *ptr = devfreq;
  582. devres_add(dev, ptr);
  583. return devfreq;
  584. }
  585. EXPORT_SYMBOL(devm_devfreq_add_device);
  586. #ifdef CONFIG_OF
  587. /*
  588. * devfreq_get_devfreq_by_phandle - Get the devfreq device from devicetree
  589. * @dev - instance to the given device
  590. * @index - index into list of devfreq
  591. *
  592. * return the instance of devfreq device
  593. */
  594. struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, int index)
  595. {
  596. struct device_node *node;
  597. struct devfreq *devfreq;
  598. if (!dev)
  599. return ERR_PTR(-EINVAL);
  600. if (!dev->of_node)
  601. return ERR_PTR(-EINVAL);
  602. node = of_parse_phandle(dev->of_node, "devfreq", index);
  603. if (!node)
  604. return ERR_PTR(-ENODEV);
  605. mutex_lock(&devfreq_list_lock);
  606. list_for_each_entry(devfreq, &devfreq_list, node) {
  607. if (devfreq->dev.parent
  608. && devfreq->dev.parent->of_node == node) {
  609. mutex_unlock(&devfreq_list_lock);
  610. of_node_put(node);
  611. return devfreq;
  612. }
  613. }
  614. mutex_unlock(&devfreq_list_lock);
  615. of_node_put(node);
  616. return ERR_PTR(-EPROBE_DEFER);
  617. }
  618. #else
  619. struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, int index)
  620. {
  621. return ERR_PTR(-ENODEV);
  622. }
  623. #endif /* CONFIG_OF */
  624. EXPORT_SYMBOL_GPL(devfreq_get_devfreq_by_phandle);
  625. /**
  626. * devm_devfreq_remove_device() - Resource-managed devfreq_remove_device()
  627. * @dev: the device to add devfreq feature.
  628. * @devfreq: the devfreq instance to be removed
  629. */
  630. void devm_devfreq_remove_device(struct device *dev, struct devfreq *devfreq)
  631. {
  632. WARN_ON(devres_release(dev, devm_devfreq_dev_release,
  633. devm_devfreq_dev_match, devfreq));
  634. }
  635. EXPORT_SYMBOL(devm_devfreq_remove_device);
  636. /**
  637. * devfreq_suspend_device() - Suspend devfreq of a device.
  638. * @devfreq: the devfreq instance to be suspended
  639. *
  640. * This function is intended to be called by the pm callbacks
  641. * (e.g., runtime_suspend, suspend) of the device driver that
  642. * holds the devfreq.
  643. */
  644. int devfreq_suspend_device(struct devfreq *devfreq)
  645. {
  646. if (!devfreq)
  647. return -EINVAL;
  648. if (!devfreq->governor)
  649. return 0;
  650. return devfreq->governor->event_handler(devfreq,
  651. DEVFREQ_GOV_SUSPEND, NULL);
  652. }
  653. EXPORT_SYMBOL(devfreq_suspend_device);
  654. /**
  655. * devfreq_resume_device() - Resume devfreq of a device.
  656. * @devfreq: the devfreq instance to be resumed
  657. *
  658. * This function is intended to be called by the pm callbacks
  659. * (e.g., runtime_resume, resume) of the device driver that
  660. * holds the devfreq.
  661. */
  662. int devfreq_resume_device(struct devfreq *devfreq)
  663. {
  664. if (!devfreq)
  665. return -EINVAL;
  666. if (!devfreq->governor)
  667. return 0;
  668. return devfreq->governor->event_handler(devfreq,
  669. DEVFREQ_GOV_RESUME, NULL);
  670. }
  671. EXPORT_SYMBOL(devfreq_resume_device);
  672. /**
  673. * devfreq_add_governor() - Add devfreq governor
  674. * @governor: the devfreq governor to be added
  675. */
  676. int devfreq_add_governor(struct devfreq_governor *governor)
  677. {
  678. struct devfreq_governor *g;
  679. struct devfreq *devfreq;
  680. int err = 0;
  681. if (!governor) {
  682. pr_err("%s: Invalid parameters.\n", __func__);
  683. return -EINVAL;
  684. }
  685. mutex_lock(&devfreq_list_lock);
  686. g = find_devfreq_governor(governor->name);
  687. if (!IS_ERR(g)) {
  688. pr_err("%s: governor %s already registered\n", __func__,
  689. g->name);
  690. err = -EINVAL;
  691. goto err_out;
  692. }
  693. list_add(&governor->node, &devfreq_governor_list);
  694. list_for_each_entry(devfreq, &devfreq_list, node) {
  695. int ret = 0;
  696. struct device *dev = devfreq->dev.parent;
  697. if (!strncmp(devfreq->governor_name, governor->name,
  698. DEVFREQ_NAME_LEN)) {
  699. /* The following should never occur */
  700. if (devfreq->governor) {
  701. dev_warn(dev,
  702. "%s: Governor %s already present\n",
  703. __func__, devfreq->governor->name);
  704. ret = devfreq->governor->event_handler(devfreq,
  705. DEVFREQ_GOV_STOP, NULL);
  706. if (ret) {
  707. dev_warn(dev,
  708. "%s: Governor %s stop = %d\n",
  709. __func__,
  710. devfreq->governor->name, ret);
  711. }
  712. /* Fall through */
  713. }
  714. devfreq->governor = governor;
  715. ret = devfreq->governor->event_handler(devfreq,
  716. DEVFREQ_GOV_START, NULL);
  717. if (ret) {
  718. dev_warn(dev, "%s: Governor %s start=%d\n",
  719. __func__, devfreq->governor->name,
  720. ret);
  721. }
  722. }
  723. }
  724. err_out:
  725. mutex_unlock(&devfreq_list_lock);
  726. return err;
  727. }
  728. EXPORT_SYMBOL(devfreq_add_governor);
  729. /**
  730. * devfreq_remove_governor() - Remove devfreq feature from a device.
  731. * @governor: the devfreq governor to be removed
  732. */
  733. int devfreq_remove_governor(struct devfreq_governor *governor)
  734. {
  735. struct devfreq_governor *g;
  736. struct devfreq *devfreq;
  737. int err = 0;
  738. if (!governor) {
  739. pr_err("%s: Invalid parameters.\n", __func__);
  740. return -EINVAL;
  741. }
  742. mutex_lock(&devfreq_list_lock);
  743. g = find_devfreq_governor(governor->name);
  744. if (IS_ERR(g)) {
  745. pr_err("%s: governor %s not registered\n", __func__,
  746. governor->name);
  747. err = PTR_ERR(g);
  748. goto err_out;
  749. }
  750. list_for_each_entry(devfreq, &devfreq_list, node) {
  751. int ret;
  752. struct device *dev = devfreq->dev.parent;
  753. if (!strncmp(devfreq->governor_name, governor->name,
  754. DEVFREQ_NAME_LEN)) {
  755. /* we should have a devfreq governor! */
  756. if (!devfreq->governor) {
  757. dev_warn(dev, "%s: Governor %s NOT present\n",
  758. __func__, governor->name);
  759. continue;
  760. /* Fall through */
  761. }
  762. ret = devfreq->governor->event_handler(devfreq,
  763. DEVFREQ_GOV_STOP, NULL);
  764. if (ret) {
  765. dev_warn(dev, "%s: Governor %s stop=%d\n",
  766. __func__, devfreq->governor->name,
  767. ret);
  768. }
  769. devfreq->governor = NULL;
  770. }
  771. }
  772. list_del(&governor->node);
  773. err_out:
  774. mutex_unlock(&devfreq_list_lock);
  775. return err;
  776. }
  777. EXPORT_SYMBOL(devfreq_remove_governor);
  778. static ssize_t governor_show(struct device *dev,
  779. struct device_attribute *attr, char *buf)
  780. {
  781. if (!to_devfreq(dev)->governor)
  782. return -EINVAL;
  783. return sprintf(buf, "%s\n", to_devfreq(dev)->governor->name);
  784. }
  785. static ssize_t governor_store(struct device *dev, struct device_attribute *attr,
  786. const char *buf, size_t count)
  787. {
  788. struct devfreq *df = to_devfreq(dev);
  789. int ret;
  790. char str_governor[DEVFREQ_NAME_LEN + 1];
  791. struct devfreq_governor *governor;
  792. ret = sscanf(buf, "%" __stringify(DEVFREQ_NAME_LEN) "s", str_governor);
  793. if (ret != 1)
  794. return -EINVAL;
  795. mutex_lock(&devfreq_list_lock);
  796. governor = find_devfreq_governor(str_governor);
  797. if (IS_ERR(governor)) {
  798. ret = PTR_ERR(governor);
  799. goto out;
  800. }
  801. if (df->governor == governor) {
  802. ret = 0;
  803. goto out;
  804. } else if (df->governor->immutable || governor->immutable) {
  805. ret = -EINVAL;
  806. goto out;
  807. }
  808. if (df->governor) {
  809. ret = df->governor->event_handler(df, DEVFREQ_GOV_STOP, NULL);
  810. if (ret) {
  811. dev_warn(dev, "%s: Governor %s not stopped(%d)\n",
  812. __func__, df->governor->name, ret);
  813. goto out;
  814. }
  815. }
  816. df->governor = governor;
  817. strncpy(df->governor_name, governor->name, DEVFREQ_NAME_LEN);
  818. ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
  819. if (ret)
  820. dev_warn(dev, "%s: Governor %s not started(%d)\n",
  821. __func__, df->governor->name, ret);
  822. out:
  823. mutex_unlock(&devfreq_list_lock);
  824. if (!ret)
  825. ret = count;
  826. return ret;
  827. }
  828. static DEVICE_ATTR_RW(governor);
  829. static ssize_t available_governors_show(struct device *d,
  830. struct device_attribute *attr,
  831. char *buf)
  832. {
  833. struct devfreq *df = to_devfreq(d);
  834. ssize_t count = 0;
  835. mutex_lock(&devfreq_list_lock);
  836. /*
  837. * The devfreq with immutable governor (e.g., passive) shows
  838. * only own governor.
  839. */
  840. if (df->governor->immutable) {
  841. count = scnprintf(&buf[count], DEVFREQ_NAME_LEN,
  842. "%s ", df->governor_name);
  843. /*
  844. * The devfreq device shows the registered governor except for
  845. * immutable governors such as passive governor .
  846. */
  847. } else {
  848. struct devfreq_governor *governor;
  849. list_for_each_entry(governor, &devfreq_governor_list, node) {
  850. if (governor->immutable)
  851. continue;
  852. count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
  853. "%s ", governor->name);
  854. }
  855. }
  856. mutex_unlock(&devfreq_list_lock);
  857. /* Truncate the trailing space */
  858. if (count)
  859. count--;
  860. count += sprintf(&buf[count], "\n");
  861. return count;
  862. }
  863. static DEVICE_ATTR_RO(available_governors);
  864. static ssize_t cur_freq_show(struct device *dev, struct device_attribute *attr,
  865. char *buf)
  866. {
  867. unsigned long freq;
  868. struct devfreq *devfreq = to_devfreq(dev);
  869. if (devfreq->profile->get_cur_freq &&
  870. !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
  871. return sprintf(buf, "%lu\n", freq);
  872. return sprintf(buf, "%lu\n", devfreq->previous_freq);
  873. }
  874. static DEVICE_ATTR_RO(cur_freq);
  875. static ssize_t target_freq_show(struct device *dev,
  876. struct device_attribute *attr, char *buf)
  877. {
  878. return sprintf(buf, "%lu\n", to_devfreq(dev)->previous_freq);
  879. }
  880. static DEVICE_ATTR_RO(target_freq);
  881. static ssize_t polling_interval_show(struct device *dev,
  882. struct device_attribute *attr, char *buf)
  883. {
  884. return sprintf(buf, "%d\n", to_devfreq(dev)->profile->polling_ms);
  885. }
  886. static ssize_t polling_interval_store(struct device *dev,
  887. struct device_attribute *attr,
  888. const char *buf, size_t count)
  889. {
  890. struct devfreq *df = to_devfreq(dev);
  891. unsigned int value;
  892. int ret;
  893. if (!df->governor)
  894. return -EINVAL;
  895. ret = sscanf(buf, "%u", &value);
  896. if (ret != 1)
  897. return -EINVAL;
  898. df->governor->event_handler(df, DEVFREQ_GOV_INTERVAL, &value);
  899. ret = count;
  900. return ret;
  901. }
  902. static DEVICE_ATTR_RW(polling_interval);
  903. static ssize_t min_freq_store(struct device *dev, struct device_attribute *attr,
  904. const char *buf, size_t count)
  905. {
  906. struct devfreq *df = to_devfreq(dev);
  907. unsigned long value;
  908. int ret;
  909. unsigned long max;
  910. ret = sscanf(buf, "%lu", &value);
  911. if (ret != 1)
  912. return -EINVAL;
  913. mutex_lock(&df->lock);
  914. max = df->max_freq;
  915. if (value && max && value > max) {
  916. ret = -EINVAL;
  917. goto unlock;
  918. }
  919. df->min_freq = value;
  920. update_devfreq(df);
  921. ret = count;
  922. unlock:
  923. mutex_unlock(&df->lock);
  924. return ret;
  925. }
  926. static ssize_t max_freq_store(struct device *dev, struct device_attribute *attr,
  927. const char *buf, size_t count)
  928. {
  929. struct devfreq *df = to_devfreq(dev);
  930. unsigned long value;
  931. int ret;
  932. unsigned long min;
  933. ret = sscanf(buf, "%lu", &value);
  934. if (ret != 1)
  935. return -EINVAL;
  936. mutex_lock(&df->lock);
  937. min = df->min_freq;
  938. if (value && min && value < min) {
  939. ret = -EINVAL;
  940. goto unlock;
  941. }
  942. df->max_freq = value;
  943. update_devfreq(df);
  944. ret = count;
  945. unlock:
  946. mutex_unlock(&df->lock);
  947. return ret;
  948. }
  949. #define show_one(name) \
  950. static ssize_t name##_show \
  951. (struct device *dev, struct device_attribute *attr, char *buf) \
  952. { \
  953. return sprintf(buf, "%lu\n", to_devfreq(dev)->name); \
  954. }
  955. show_one(min_freq);
  956. show_one(max_freq);
  957. static DEVICE_ATTR_RW(min_freq);
  958. static DEVICE_ATTR_RW(max_freq);
  959. static ssize_t available_frequencies_show(struct device *d,
  960. struct device_attribute *attr,
  961. char *buf)
  962. {
  963. struct devfreq *df = to_devfreq(d);
  964. struct device *dev = df->dev.parent;
  965. struct dev_pm_opp *opp;
  966. ssize_t count = 0;
  967. unsigned long freq = 0;
  968. do {
  969. opp = dev_pm_opp_find_freq_ceil(dev, &freq);
  970. if (IS_ERR(opp))
  971. break;
  972. dev_pm_opp_put(opp);
  973. count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
  974. "%lu ", freq);
  975. freq++;
  976. } while (1);
  977. /* Truncate the trailing space */
  978. if (count)
  979. count--;
  980. count += sprintf(&buf[count], "\n");
  981. return count;
  982. }
  983. static DEVICE_ATTR_RO(available_frequencies);
  984. static ssize_t trans_stat_show(struct device *dev,
  985. struct device_attribute *attr, char *buf)
  986. {
  987. struct devfreq *devfreq = to_devfreq(dev);
  988. ssize_t len;
  989. int i, j;
  990. unsigned int max_state = devfreq->profile->max_state;
  991. if (!devfreq->stop_polling &&
  992. devfreq_update_status(devfreq, devfreq->previous_freq))
  993. return 0;
  994. if (max_state == 0)
  995. return sprintf(buf, "Not Supported.\n");
  996. len = sprintf(buf, " From : To\n");
  997. len += sprintf(buf + len, " :");
  998. for (i = 0; i < max_state; i++)
  999. len += sprintf(buf + len, "%10lu",
  1000. devfreq->profile->freq_table[i]);
  1001. len += sprintf(buf + len, " time(ms)\n");
  1002. for (i = 0; i < max_state; i++) {
  1003. if (devfreq->profile->freq_table[i]
  1004. == devfreq->previous_freq) {
  1005. len += sprintf(buf + len, "*");
  1006. } else {
  1007. len += sprintf(buf + len, " ");
  1008. }
  1009. len += sprintf(buf + len, "%10lu:",
  1010. devfreq->profile->freq_table[i]);
  1011. for (j = 0; j < max_state; j++)
  1012. len += sprintf(buf + len, "%10u",
  1013. devfreq->trans_table[(i * max_state) + j]);
  1014. len += sprintf(buf + len, "%10u\n",
  1015. jiffies_to_msecs(devfreq->time_in_state[i]));
  1016. }
  1017. len += sprintf(buf + len, "Total transition : %u\n",
  1018. devfreq->total_trans);
  1019. return len;
  1020. }
  1021. static DEVICE_ATTR_RO(trans_stat);
  1022. static struct attribute *devfreq_attrs[] = {
  1023. &dev_attr_governor.attr,
  1024. &dev_attr_available_governors.attr,
  1025. &dev_attr_cur_freq.attr,
  1026. &dev_attr_available_frequencies.attr,
  1027. &dev_attr_target_freq.attr,
  1028. &dev_attr_polling_interval.attr,
  1029. &dev_attr_min_freq.attr,
  1030. &dev_attr_max_freq.attr,
  1031. &dev_attr_trans_stat.attr,
  1032. NULL,
  1033. };
  1034. ATTRIBUTE_GROUPS(devfreq);
  1035. static int __init devfreq_init(void)
  1036. {
  1037. devfreq_class = class_create(THIS_MODULE, "devfreq");
  1038. if (IS_ERR(devfreq_class)) {
  1039. pr_err("%s: couldn't create class\n", __FILE__);
  1040. return PTR_ERR(devfreq_class);
  1041. }
  1042. devfreq_wq = create_freezable_workqueue("devfreq_wq");
  1043. if (!devfreq_wq) {
  1044. class_destroy(devfreq_class);
  1045. pr_err("%s: couldn't create workqueue\n", __FILE__);
  1046. return -ENOMEM;
  1047. }
  1048. devfreq_class->dev_groups = devfreq_groups;
  1049. return 0;
  1050. }
  1051. subsys_initcall(devfreq_init);
  1052. /*
  1053. * The followings are helper functions for devfreq user device drivers with
  1054. * OPP framework.
  1055. */
  1056. /**
  1057. * devfreq_recommended_opp() - Helper function to get proper OPP for the
  1058. * freq value given to target callback.
  1059. * @dev: The devfreq user device. (parent of devfreq)
  1060. * @freq: The frequency given to target function
  1061. * @flags: Flags handed from devfreq framework.
  1062. *
  1063. * The callers are required to call dev_pm_opp_put() for the returned OPP after
  1064. * use.
  1065. */
  1066. struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
  1067. unsigned long *freq,
  1068. u32 flags)
  1069. {
  1070. struct dev_pm_opp *opp;
  1071. if (flags & DEVFREQ_FLAG_LEAST_UPPER_BOUND) {
  1072. /* The freq is an upper bound. opp should be lower */
  1073. opp = dev_pm_opp_find_freq_floor(dev, freq);
  1074. /* If not available, use the closest opp */
  1075. if (opp == ERR_PTR(-ERANGE))
  1076. opp = dev_pm_opp_find_freq_ceil(dev, freq);
  1077. } else {
  1078. /* The freq is an lower bound. opp should be higher */
  1079. opp = dev_pm_opp_find_freq_ceil(dev, freq);
  1080. /* If not available, use the closest opp */
  1081. if (opp == ERR_PTR(-ERANGE))
  1082. opp = dev_pm_opp_find_freq_floor(dev, freq);
  1083. }
  1084. return opp;
  1085. }
  1086. EXPORT_SYMBOL(devfreq_recommended_opp);
  1087. /**
  1088. * devfreq_register_opp_notifier() - Helper function to get devfreq notified
  1089. * for any changes in the OPP availability
  1090. * changes
  1091. * @dev: The devfreq user device. (parent of devfreq)
  1092. * @devfreq: The devfreq object.
  1093. */
  1094. int devfreq_register_opp_notifier(struct device *dev, struct devfreq *devfreq)
  1095. {
  1096. return dev_pm_opp_register_notifier(dev, &devfreq->nb);
  1097. }
  1098. EXPORT_SYMBOL(devfreq_register_opp_notifier);
  1099. /**
  1100. * devfreq_unregister_opp_notifier() - Helper function to stop getting devfreq
  1101. * notified for any changes in the OPP
  1102. * availability changes anymore.
  1103. * @dev: The devfreq user device. (parent of devfreq)
  1104. * @devfreq: The devfreq object.
  1105. *
  1106. * At exit() callback of devfreq_dev_profile, this must be included if
  1107. * devfreq_recommended_opp is used.
  1108. */
  1109. int devfreq_unregister_opp_notifier(struct device *dev, struct devfreq *devfreq)
  1110. {
  1111. return dev_pm_opp_unregister_notifier(dev, &devfreq->nb);
  1112. }
  1113. EXPORT_SYMBOL(devfreq_unregister_opp_notifier);
  1114. static void devm_devfreq_opp_release(struct device *dev, void *res)
  1115. {
  1116. devfreq_unregister_opp_notifier(dev, *(struct devfreq **)res);
  1117. }
  1118. /**
  1119. * devm_ devfreq_register_opp_notifier()
  1120. * - Resource-managed devfreq_register_opp_notifier()
  1121. * @dev: The devfreq user device. (parent of devfreq)
  1122. * @devfreq: The devfreq object.
  1123. */
  1124. int devm_devfreq_register_opp_notifier(struct device *dev,
  1125. struct devfreq *devfreq)
  1126. {
  1127. struct devfreq **ptr;
  1128. int ret;
  1129. ptr = devres_alloc(devm_devfreq_opp_release, sizeof(*ptr), GFP_KERNEL);
  1130. if (!ptr)
  1131. return -ENOMEM;
  1132. ret = devfreq_register_opp_notifier(dev, devfreq);
  1133. if (ret) {
  1134. devres_free(ptr);
  1135. return ret;
  1136. }
  1137. *ptr = devfreq;
  1138. devres_add(dev, ptr);
  1139. return 0;
  1140. }
  1141. EXPORT_SYMBOL(devm_devfreq_register_opp_notifier);
  1142. /**
  1143. * devm_devfreq_unregister_opp_notifier()
  1144. * - Resource-managed devfreq_unregister_opp_notifier()
  1145. * @dev: The devfreq user device. (parent of devfreq)
  1146. * @devfreq: The devfreq object.
  1147. */
  1148. void devm_devfreq_unregister_opp_notifier(struct device *dev,
  1149. struct devfreq *devfreq)
  1150. {
  1151. WARN_ON(devres_release(dev, devm_devfreq_opp_release,
  1152. devm_devfreq_dev_match, devfreq));
  1153. }
  1154. EXPORT_SYMBOL(devm_devfreq_unregister_opp_notifier);
  1155. /**
  1156. * devfreq_register_notifier() - Register a driver with devfreq
  1157. * @devfreq: The devfreq object.
  1158. * @nb: The notifier block to register.
  1159. * @list: DEVFREQ_TRANSITION_NOTIFIER.
  1160. */
  1161. int devfreq_register_notifier(struct devfreq *devfreq,
  1162. struct notifier_block *nb,
  1163. unsigned int list)
  1164. {
  1165. int ret = 0;
  1166. if (!devfreq)
  1167. return -EINVAL;
  1168. switch (list) {
  1169. case DEVFREQ_TRANSITION_NOTIFIER:
  1170. ret = srcu_notifier_chain_register(
  1171. &devfreq->transition_notifier_list, nb);
  1172. break;
  1173. default:
  1174. ret = -EINVAL;
  1175. }
  1176. return ret;
  1177. }
  1178. EXPORT_SYMBOL(devfreq_register_notifier);
  1179. /*
  1180. * devfreq_unregister_notifier() - Unregister a driver with devfreq
  1181. * @devfreq: The devfreq object.
  1182. * @nb: The notifier block to be unregistered.
  1183. * @list: DEVFREQ_TRANSITION_NOTIFIER.
  1184. */
  1185. int devfreq_unregister_notifier(struct devfreq *devfreq,
  1186. struct notifier_block *nb,
  1187. unsigned int list)
  1188. {
  1189. int ret = 0;
  1190. if (!devfreq)
  1191. return -EINVAL;
  1192. switch (list) {
  1193. case DEVFREQ_TRANSITION_NOTIFIER:
  1194. ret = srcu_notifier_chain_unregister(
  1195. &devfreq->transition_notifier_list, nb);
  1196. break;
  1197. default:
  1198. ret = -EINVAL;
  1199. }
  1200. return ret;
  1201. }
  1202. EXPORT_SYMBOL(devfreq_unregister_notifier);
  1203. struct devfreq_notifier_devres {
  1204. struct devfreq *devfreq;
  1205. struct notifier_block *nb;
  1206. unsigned int list;
  1207. };
  1208. static void devm_devfreq_notifier_release(struct device *dev, void *res)
  1209. {
  1210. struct devfreq_notifier_devres *this = res;
  1211. devfreq_unregister_notifier(this->devfreq, this->nb, this->list);
  1212. }
  1213. /**
  1214. * devm_devfreq_register_notifier()
  1215. - Resource-managed devfreq_register_notifier()
  1216. * @dev: The devfreq user device. (parent of devfreq)
  1217. * @devfreq: The devfreq object.
  1218. * @nb: The notifier block to be unregistered.
  1219. * @list: DEVFREQ_TRANSITION_NOTIFIER.
  1220. */
  1221. int devm_devfreq_register_notifier(struct device *dev,
  1222. struct devfreq *devfreq,
  1223. struct notifier_block *nb,
  1224. unsigned int list)
  1225. {
  1226. struct devfreq_notifier_devres *ptr;
  1227. int ret;
  1228. ptr = devres_alloc(devm_devfreq_notifier_release, sizeof(*ptr),
  1229. GFP_KERNEL);
  1230. if (!ptr)
  1231. return -ENOMEM;
  1232. ret = devfreq_register_notifier(devfreq, nb, list);
  1233. if (ret) {
  1234. devres_free(ptr);
  1235. return ret;
  1236. }
  1237. ptr->devfreq = devfreq;
  1238. ptr->nb = nb;
  1239. ptr->list = list;
  1240. devres_add(dev, ptr);
  1241. return 0;
  1242. }
  1243. EXPORT_SYMBOL(devm_devfreq_register_notifier);
  1244. /**
  1245. * devm_devfreq_unregister_notifier()
  1246. - Resource-managed devfreq_unregister_notifier()
  1247. * @dev: The devfreq user device. (parent of devfreq)
  1248. * @devfreq: The devfreq object.
  1249. * @nb: The notifier block to be unregistered.
  1250. * @list: DEVFREQ_TRANSITION_NOTIFIER.
  1251. */
  1252. void devm_devfreq_unregister_notifier(struct device *dev,
  1253. struct devfreq *devfreq,
  1254. struct notifier_block *nb,
  1255. unsigned int list)
  1256. {
  1257. WARN_ON(devres_release(dev, devm_devfreq_notifier_release,
  1258. devm_devfreq_dev_match, devfreq));
  1259. }
  1260. EXPORT_SYMBOL(devm_devfreq_unregister_notifier);