clock_cooling.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. /*
  2. * drivers/thermal/clock_cooling.c
  3. *
  4. * Copyright (C) 2014 Eduardo Valentin <edubezval@gmail.com>
  5. *
  6. * Copyright (C) 2013 Texas Instruments Inc.
  7. * Contact: Eduardo Valentin <eduardo.valentin@ti.com>
  8. *
  9. * Highly based on cpu_cooling.c.
  10. * Copyright (C) 2012 Samsung Electronics Co., Ltd(http://www.samsung.com)
  11. * Copyright (C) 2012 Amit Daniel <amit.kachhap@linaro.org>
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; version 2 of the License.
  16. *
  17. * This program is distributed in the hope that it will be useful, but
  18. * WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. * General Public License for more details.
  21. */
  22. #include <linux/clk.h>
  23. #include <linux/cpufreq.h>
  24. #include <linux/device.h>
  25. #include <linux/err.h>
  26. #include <linux/idr.h>
  27. #include <linux/mutex.h>
  28. #include <linux/pm_opp.h>
  29. #include <linux/slab.h>
  30. #include <linux/thermal.h>
  31. #include <linux/clock_cooling.h>
  32. /**
  33. * struct clock_cooling_device - data for cooling device with clock
  34. * @id: unique integer value corresponding to each clock_cooling_device
  35. * registered.
  36. * @dev: struct device pointer to the device being used to cool off using
  37. * clock frequencies.
  38. * @cdev: thermal_cooling_device pointer to keep track of the
  39. * registered cooling device.
  40. * @clk_rate_change_nb: reference to notifier block used to receive clock
  41. * rate changes.
  42. * @freq_table: frequency table used to keep track of available frequencies.
  43. * @clock_state: integer value representing the current state of clock
  44. * cooling devices.
  45. * @clock_val: integer value representing the absolute value of the clipped
  46. * frequency.
  47. * @clk: struct clk reference used to enforce clock limits.
  48. * @lock: mutex lock to protect this struct.
  49. *
  50. * This structure is required for keeping information of each
  51. * clock_cooling_device registered. In order to prevent corruption of this a
  52. * mutex @lock is used.
  53. */
  54. struct clock_cooling_device {
  55. int id;
  56. struct device *dev;
  57. struct thermal_cooling_device *cdev;
  58. struct notifier_block clk_rate_change_nb;
  59. struct cpufreq_frequency_table *freq_table;
  60. unsigned long clock_state;
  61. unsigned long clock_val;
  62. struct clk *clk;
  63. struct mutex lock; /* lock to protect the content of this struct */
  64. };
  65. #define to_clock_cooling_device(x) \
  66. container_of(x, struct clock_cooling_device, clk_rate_change_nb)
  67. static DEFINE_IDR(clock_idr);
  68. static DEFINE_MUTEX(cooling_clock_lock);
  69. /**
  70. * clock_cooling_get_idr - function to get an unique id.
  71. * @id: int * value generated by this function.
  72. *
  73. * This function will populate @id with an unique
  74. * id, using the idr API.
  75. *
  76. * Return: 0 on success, an error code on failure.
  77. */
  78. static int clock_cooling_get_idr(int *id)
  79. {
  80. int ret;
  81. mutex_lock(&cooling_clock_lock);
  82. ret = idr_alloc(&clock_idr, NULL, 0, 0, GFP_KERNEL);
  83. mutex_unlock(&cooling_clock_lock);
  84. if (unlikely(ret < 0))
  85. return ret;
  86. *id = ret;
  87. return 0;
  88. }
  89. /**
  90. * release_idr - function to free the unique id.
  91. * @id: int value representing the unique id.
  92. */
  93. static void release_idr(int id)
  94. {
  95. mutex_lock(&cooling_clock_lock);
  96. idr_remove(&clock_idr, id);
  97. mutex_unlock(&cooling_clock_lock);
  98. }
  99. /* Below code defines functions to be used for clock as cooling device */
  100. enum clock_cooling_property {
  101. GET_LEVEL,
  102. GET_FREQ,
  103. GET_MAXL,
  104. };
  105. /**
  106. * clock_cooling_get_property - fetch a property of interest for a give cpu.
  107. * @ccdev: clock cooling device reference
  108. * @input: query parameter
  109. * @output: query return
  110. * @property: type of query (frequency, level, max level)
  111. *
  112. * This is the common function to
  113. * 1. get maximum clock cooling states
  114. * 2. translate frequency to cooling state
  115. * 3. translate cooling state to frequency
  116. * Note that the code may be not in good shape
  117. * but it is written in this way in order to:
  118. * a) reduce duplicate code as most of the code can be shared.
  119. * b) make sure the logic is consistent when translating between
  120. * cooling states and frequencies.
  121. *
  122. * Return: 0 on success, -EINVAL when invalid parameters are passed.
  123. */
  124. static int clock_cooling_get_property(struct clock_cooling_device *ccdev,
  125. unsigned long input,
  126. unsigned long *output,
  127. enum clock_cooling_property property)
  128. {
  129. int i;
  130. unsigned long max_level = 0, level = 0;
  131. unsigned int freq = CPUFREQ_ENTRY_INVALID;
  132. int descend = -1;
  133. struct cpufreq_frequency_table *pos, *table = ccdev->freq_table;
  134. if (!output)
  135. return -EINVAL;
  136. if (!table)
  137. return -EINVAL;
  138. cpufreq_for_each_valid_entry(pos, table) {
  139. /* ignore duplicate entry */
  140. if (freq == pos->frequency)
  141. continue;
  142. /* get the frequency order */
  143. if (freq != CPUFREQ_ENTRY_INVALID && descend == -1)
  144. descend = freq > pos->frequency;
  145. freq = pos->frequency;
  146. max_level++;
  147. }
  148. /* No valid cpu frequency entry */
  149. if (max_level == 0)
  150. return -EINVAL;
  151. /* max_level is an index, not a counter */
  152. max_level--;
  153. /* get max level */
  154. if (property == GET_MAXL) {
  155. *output = max_level;
  156. return 0;
  157. }
  158. if (property == GET_FREQ)
  159. level = descend ? input : (max_level - input);
  160. i = 0;
  161. cpufreq_for_each_valid_entry(pos, table) {
  162. /* ignore duplicate entry */
  163. if (freq == pos->frequency)
  164. continue;
  165. /* now we have a valid frequency entry */
  166. freq = pos->frequency;
  167. if (property == GET_LEVEL && (unsigned int)input == freq) {
  168. /* get level by frequency */
  169. *output = descend ? i : (max_level - i);
  170. return 0;
  171. }
  172. if (property == GET_FREQ && level == i) {
  173. /* get frequency by level */
  174. *output = freq;
  175. return 0;
  176. }
  177. i++;
  178. }
  179. return -EINVAL;
  180. }
  181. /**
  182. * clock_cooling_get_level - return the cooling level of given clock cooling.
  183. * @cdev: reference of a thermal cooling device of used as clock cooling device
  184. * @freq: the frequency of interest
  185. *
  186. * This function will match the cooling level corresponding to the
  187. * requested @freq and return it.
  188. *
  189. * Return: The matched cooling level on success or THERMAL_CSTATE_INVALID
  190. * otherwise.
  191. */
  192. unsigned long clock_cooling_get_level(struct thermal_cooling_device *cdev,
  193. unsigned long freq)
  194. {
  195. struct clock_cooling_device *ccdev = cdev->devdata;
  196. unsigned long val;
  197. if (clock_cooling_get_property(ccdev, (unsigned long)freq, &val,
  198. GET_LEVEL))
  199. return THERMAL_CSTATE_INVALID;
  200. return val;
  201. }
  202. EXPORT_SYMBOL_GPL(clock_cooling_get_level);
  203. /**
  204. * clock_cooling_get_frequency - get the absolute value of frequency from level.
  205. * @ccdev: clock cooling device reference
  206. * @level: cooling level
  207. *
  208. * This function matches cooling level with frequency. Based on a cooling level
  209. * of frequency, equals cooling state of cpu cooling device, it will return
  210. * the corresponding frequency.
  211. * e.g level=0 --> 1st MAX FREQ, level=1 ---> 2nd MAX FREQ, .... etc
  212. *
  213. * Return: 0 on error, the corresponding frequency otherwise.
  214. */
  215. static unsigned long
  216. clock_cooling_get_frequency(struct clock_cooling_device *ccdev,
  217. unsigned long level)
  218. {
  219. int ret = 0;
  220. unsigned long freq;
  221. ret = clock_cooling_get_property(ccdev, level, &freq, GET_FREQ);
  222. if (ret)
  223. return 0;
  224. return freq;
  225. }
  226. /**
  227. * clock_cooling_apply - function to apply frequency clipping.
  228. * @ccdev: clock_cooling_device pointer containing frequency clipping data.
  229. * @cooling_state: value of the cooling state.
  230. *
  231. * Function used to make sure the clock layer is aware of current thermal
  232. * limits. The limits are applied by updating the clock rate in case it is
  233. * higher than the corresponding frequency based on the requested cooling_state.
  234. *
  235. * Return: 0 on success, an error code otherwise (-EINVAL in case wrong
  236. * cooling state).
  237. */
  238. static int clock_cooling_apply(struct clock_cooling_device *ccdev,
  239. unsigned long cooling_state)
  240. {
  241. unsigned long clip_freq, cur_freq;
  242. int ret = 0;
  243. /* Here we write the clipping */
  244. /* Check if the old cooling action is same as new cooling action */
  245. if (ccdev->clock_state == cooling_state)
  246. return 0;
  247. clip_freq = clock_cooling_get_frequency(ccdev, cooling_state);
  248. if (!clip_freq)
  249. return -EINVAL;
  250. cur_freq = clk_get_rate(ccdev->clk);
  251. mutex_lock(&ccdev->lock);
  252. ccdev->clock_state = cooling_state;
  253. ccdev->clock_val = clip_freq;
  254. /* enforce clock level */
  255. if (cur_freq > clip_freq)
  256. ret = clk_set_rate(ccdev->clk, clip_freq);
  257. mutex_unlock(&ccdev->lock);
  258. return ret;
  259. }
  260. /**
  261. * clock_cooling_clock_notifier - notifier callback on clock rate changes.
  262. * @nb: struct notifier_block * with callback info.
  263. * @event: value showing clock event for which this function invoked.
  264. * @data: callback-specific data
  265. *
  266. * Callback to hijack the notification on clock transition.
  267. * Every time there is a clock change, we intercept all pre change events
  268. * and block the transition in case the new rate infringes thermal limits.
  269. *
  270. * Return: NOTIFY_DONE (success) or NOTIFY_BAD (new_rate > thermal limit).
  271. */
  272. static int clock_cooling_clock_notifier(struct notifier_block *nb,
  273. unsigned long event, void *data)
  274. {
  275. struct clk_notifier_data *ndata = data;
  276. struct clock_cooling_device *ccdev = to_clock_cooling_device(nb);
  277. switch (event) {
  278. case PRE_RATE_CHANGE:
  279. /*
  280. * checks on current state
  281. * TODO: current method is not best we can find as it
  282. * allows possibly voltage transitions, in case DVFS
  283. * layer is also hijacking clock pre notifications.
  284. */
  285. if (ndata->new_rate > ccdev->clock_val)
  286. return NOTIFY_BAD;
  287. /* fall through */
  288. case POST_RATE_CHANGE:
  289. case ABORT_RATE_CHANGE:
  290. default:
  291. return NOTIFY_DONE;
  292. }
  293. }
  294. /* clock cooling device thermal callback functions are defined below */
  295. /**
  296. * clock_cooling_get_max_state - callback function to get the max cooling state.
  297. * @cdev: thermal cooling device pointer.
  298. * @state: fill this variable with the max cooling state.
  299. *
  300. * Callback for the thermal cooling device to return the clock
  301. * max cooling state.
  302. *
  303. * Return: 0 on success, an error code otherwise.
  304. */
  305. static int clock_cooling_get_max_state(struct thermal_cooling_device *cdev,
  306. unsigned long *state)
  307. {
  308. struct clock_cooling_device *ccdev = cdev->devdata;
  309. unsigned long count = 0;
  310. int ret;
  311. ret = clock_cooling_get_property(ccdev, 0, &count, GET_MAXL);
  312. if (!ret)
  313. *state = count;
  314. return ret;
  315. }
  316. /**
  317. * clock_cooling_get_cur_state - function to get the current cooling state.
  318. * @cdev: thermal cooling device pointer.
  319. * @state: fill this variable with the current cooling state.
  320. *
  321. * Callback for the thermal cooling device to return the clock
  322. * current cooling state.
  323. *
  324. * Return: 0 (success)
  325. */
  326. static int clock_cooling_get_cur_state(struct thermal_cooling_device *cdev,
  327. unsigned long *state)
  328. {
  329. struct clock_cooling_device *ccdev = cdev->devdata;
  330. *state = ccdev->clock_state;
  331. return 0;
  332. }
  333. /**
  334. * clock_cooling_set_cur_state - function to set the current cooling state.
  335. * @cdev: thermal cooling device pointer.
  336. * @state: set this variable to the current cooling state.
  337. *
  338. * Callback for the thermal cooling device to change the clock cooling
  339. * current cooling state.
  340. *
  341. * Return: 0 on success, an error code otherwise.
  342. */
  343. static int clock_cooling_set_cur_state(struct thermal_cooling_device *cdev,
  344. unsigned long state)
  345. {
  346. struct clock_cooling_device *clock_device = cdev->devdata;
  347. return clock_cooling_apply(clock_device, state);
  348. }
  349. /* Bind clock callbacks to thermal cooling device ops */
  350. static struct thermal_cooling_device_ops const clock_cooling_ops = {
  351. .get_max_state = clock_cooling_get_max_state,
  352. .get_cur_state = clock_cooling_get_cur_state,
  353. .set_cur_state = clock_cooling_set_cur_state,
  354. };
  355. /**
  356. * clock_cooling_register - function to create clock cooling device.
  357. * @dev: struct device pointer to the device used as clock cooling device.
  358. * @clock_name: string containing the clock used as cooling mechanism.
  359. *
  360. * This interface function registers the clock cooling device with the name
  361. * "thermal-clock-%x". The cooling device is based on clock frequencies.
  362. * The struct device is assumed to be capable of DVFS transitions.
  363. * The OPP layer is used to fetch and fill the available frequencies for
  364. * the referred device. The ordered frequency table is used to control
  365. * the clock cooling device cooling states and to limit clock transitions
  366. * based on the cooling state requested by the thermal framework.
  367. *
  368. * Return: a valid struct thermal_cooling_device pointer on success,
  369. * on failure, it returns a corresponding ERR_PTR().
  370. */
  371. struct thermal_cooling_device *
  372. clock_cooling_register(struct device *dev, const char *clock_name)
  373. {
  374. struct thermal_cooling_device *cdev;
  375. struct clock_cooling_device *ccdev = NULL;
  376. char dev_name[THERMAL_NAME_LENGTH];
  377. int ret = 0;
  378. ccdev = devm_kzalloc(dev, sizeof(*ccdev), GFP_KERNEL);
  379. if (!ccdev)
  380. return ERR_PTR(-ENOMEM);
  381. ccdev->dev = dev;
  382. ccdev->clk = devm_clk_get(dev, clock_name);
  383. if (IS_ERR(ccdev->clk))
  384. return ERR_CAST(ccdev->clk);
  385. ret = clock_cooling_get_idr(&ccdev->id);
  386. if (ret)
  387. return ERR_PTR(-EINVAL);
  388. snprintf(dev_name, sizeof(dev_name), "thermal-clock-%d", ccdev->id);
  389. cdev = thermal_cooling_device_register(dev_name, ccdev,
  390. &clock_cooling_ops);
  391. if (IS_ERR(cdev)) {
  392. release_idr(ccdev->id);
  393. return ERR_PTR(-EINVAL);
  394. }
  395. ccdev->cdev = cdev;
  396. ccdev->clk_rate_change_nb.notifier_call = clock_cooling_clock_notifier;
  397. /* Assuming someone has already filled the opp table for this device */
  398. ret = dev_pm_opp_init_cpufreq_table(dev, &ccdev->freq_table);
  399. if (ret) {
  400. release_idr(ccdev->id);
  401. return ERR_PTR(ret);
  402. }
  403. ccdev->clock_state = 0;
  404. ccdev->clock_val = clock_cooling_get_frequency(ccdev, 0);
  405. clk_notifier_register(ccdev->clk, &ccdev->clk_rate_change_nb);
  406. return cdev;
  407. }
  408. EXPORT_SYMBOL_GPL(clock_cooling_register);
  409. /**
  410. * clock_cooling_unregister - function to remove clock cooling device.
  411. * @cdev: thermal cooling device pointer.
  412. *
  413. * This interface function unregisters the "thermal-clock-%x" cooling device.
  414. */
  415. void clock_cooling_unregister(struct thermal_cooling_device *cdev)
  416. {
  417. struct clock_cooling_device *ccdev;
  418. if (!cdev)
  419. return;
  420. ccdev = cdev->devdata;
  421. clk_notifier_unregister(ccdev->clk, &ccdev->clk_rate_change_nb);
  422. dev_pm_opp_free_cpufreq_table(ccdev->dev, &ccdev->freq_table);
  423. thermal_cooling_device_unregister(ccdev->cdev);
  424. release_idr(ccdev->id);
  425. }
  426. EXPORT_SYMBOL_GPL(clock_cooling_unregister);