clock_cooling.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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_IDA(clock_ida);
  68. /* Below code defines functions to be used for clock as cooling device */
  69. enum clock_cooling_property {
  70. GET_LEVEL,
  71. GET_FREQ,
  72. GET_MAXL,
  73. };
  74. /**
  75. * clock_cooling_get_property - fetch a property of interest for a give cpu.
  76. * @ccdev: clock cooling device reference
  77. * @input: query parameter
  78. * @output: query return
  79. * @property: type of query (frequency, level, max level)
  80. *
  81. * This is the common function to
  82. * 1. get maximum clock cooling states
  83. * 2. translate frequency to cooling state
  84. * 3. translate cooling state to frequency
  85. * Note that the code may be not in good shape
  86. * but it is written in this way in order to:
  87. * a) reduce duplicate code as most of the code can be shared.
  88. * b) make sure the logic is consistent when translating between
  89. * cooling states and frequencies.
  90. *
  91. * Return: 0 on success, -EINVAL when invalid parameters are passed.
  92. */
  93. static int clock_cooling_get_property(struct clock_cooling_device *ccdev,
  94. unsigned long input,
  95. unsigned long *output,
  96. enum clock_cooling_property property)
  97. {
  98. int i;
  99. unsigned long max_level = 0, level = 0;
  100. unsigned int freq = CPUFREQ_ENTRY_INVALID;
  101. int descend = -1;
  102. struct cpufreq_frequency_table *pos, *table = ccdev->freq_table;
  103. if (!output)
  104. return -EINVAL;
  105. if (!table)
  106. return -EINVAL;
  107. cpufreq_for_each_valid_entry(pos, table) {
  108. /* ignore duplicate entry */
  109. if (freq == pos->frequency)
  110. continue;
  111. /* get the frequency order */
  112. if (freq != CPUFREQ_ENTRY_INVALID && descend == -1)
  113. descend = freq > pos->frequency;
  114. freq = pos->frequency;
  115. max_level++;
  116. }
  117. /* No valid cpu frequency entry */
  118. if (max_level == 0)
  119. return -EINVAL;
  120. /* max_level is an index, not a counter */
  121. max_level--;
  122. /* get max level */
  123. if (property == GET_MAXL) {
  124. *output = max_level;
  125. return 0;
  126. }
  127. if (property == GET_FREQ)
  128. level = descend ? input : (max_level - input);
  129. i = 0;
  130. cpufreq_for_each_valid_entry(pos, table) {
  131. /* ignore duplicate entry */
  132. if (freq == pos->frequency)
  133. continue;
  134. /* now we have a valid frequency entry */
  135. freq = pos->frequency;
  136. if (property == GET_LEVEL && (unsigned int)input == freq) {
  137. /* get level by frequency */
  138. *output = descend ? i : (max_level - i);
  139. return 0;
  140. }
  141. if (property == GET_FREQ && level == i) {
  142. /* get frequency by level */
  143. *output = freq;
  144. return 0;
  145. }
  146. i++;
  147. }
  148. return -EINVAL;
  149. }
  150. /**
  151. * clock_cooling_get_level - return the cooling level of given clock cooling.
  152. * @cdev: reference of a thermal cooling device of used as clock cooling device
  153. * @freq: the frequency of interest
  154. *
  155. * This function will match the cooling level corresponding to the
  156. * requested @freq and return it.
  157. *
  158. * Return: The matched cooling level on success or THERMAL_CSTATE_INVALID
  159. * otherwise.
  160. */
  161. unsigned long clock_cooling_get_level(struct thermal_cooling_device *cdev,
  162. unsigned long freq)
  163. {
  164. struct clock_cooling_device *ccdev = cdev->devdata;
  165. unsigned long val;
  166. if (clock_cooling_get_property(ccdev, (unsigned long)freq, &val,
  167. GET_LEVEL))
  168. return THERMAL_CSTATE_INVALID;
  169. return val;
  170. }
  171. EXPORT_SYMBOL_GPL(clock_cooling_get_level);
  172. /**
  173. * clock_cooling_get_frequency - get the absolute value of frequency from level.
  174. * @ccdev: clock cooling device reference
  175. * @level: cooling level
  176. *
  177. * This function matches cooling level with frequency. Based on a cooling level
  178. * of frequency, equals cooling state of cpu cooling device, it will return
  179. * the corresponding frequency.
  180. * e.g level=0 --> 1st MAX FREQ, level=1 ---> 2nd MAX FREQ, .... etc
  181. *
  182. * Return: 0 on error, the corresponding frequency otherwise.
  183. */
  184. static unsigned long
  185. clock_cooling_get_frequency(struct clock_cooling_device *ccdev,
  186. unsigned long level)
  187. {
  188. int ret = 0;
  189. unsigned long freq;
  190. ret = clock_cooling_get_property(ccdev, level, &freq, GET_FREQ);
  191. if (ret)
  192. return 0;
  193. return freq;
  194. }
  195. /**
  196. * clock_cooling_apply - function to apply frequency clipping.
  197. * @ccdev: clock_cooling_device pointer containing frequency clipping data.
  198. * @cooling_state: value of the cooling state.
  199. *
  200. * Function used to make sure the clock layer is aware of current thermal
  201. * limits. The limits are applied by updating the clock rate in case it is
  202. * higher than the corresponding frequency based on the requested cooling_state.
  203. *
  204. * Return: 0 on success, an error code otherwise (-EINVAL in case wrong
  205. * cooling state).
  206. */
  207. static int clock_cooling_apply(struct clock_cooling_device *ccdev,
  208. unsigned long cooling_state)
  209. {
  210. unsigned long clip_freq, cur_freq;
  211. int ret = 0;
  212. /* Here we write the clipping */
  213. /* Check if the old cooling action is same as new cooling action */
  214. if (ccdev->clock_state == cooling_state)
  215. return 0;
  216. clip_freq = clock_cooling_get_frequency(ccdev, cooling_state);
  217. if (!clip_freq)
  218. return -EINVAL;
  219. cur_freq = clk_get_rate(ccdev->clk);
  220. mutex_lock(&ccdev->lock);
  221. ccdev->clock_state = cooling_state;
  222. ccdev->clock_val = clip_freq;
  223. /* enforce clock level */
  224. if (cur_freq > clip_freq)
  225. ret = clk_set_rate(ccdev->clk, clip_freq);
  226. mutex_unlock(&ccdev->lock);
  227. return ret;
  228. }
  229. /**
  230. * clock_cooling_clock_notifier - notifier callback on clock rate changes.
  231. * @nb: struct notifier_block * with callback info.
  232. * @event: value showing clock event for which this function invoked.
  233. * @data: callback-specific data
  234. *
  235. * Callback to hijack the notification on clock transition.
  236. * Every time there is a clock change, we intercept all pre change events
  237. * and block the transition in case the new rate infringes thermal limits.
  238. *
  239. * Return: NOTIFY_DONE (success) or NOTIFY_BAD (new_rate > thermal limit).
  240. */
  241. static int clock_cooling_clock_notifier(struct notifier_block *nb,
  242. unsigned long event, void *data)
  243. {
  244. struct clk_notifier_data *ndata = data;
  245. struct clock_cooling_device *ccdev = to_clock_cooling_device(nb);
  246. switch (event) {
  247. case PRE_RATE_CHANGE:
  248. /*
  249. * checks on current state
  250. * TODO: current method is not best we can find as it
  251. * allows possibly voltage transitions, in case DVFS
  252. * layer is also hijacking clock pre notifications.
  253. */
  254. if (ndata->new_rate > ccdev->clock_val)
  255. return NOTIFY_BAD;
  256. /* fall through */
  257. case POST_RATE_CHANGE:
  258. case ABORT_RATE_CHANGE:
  259. default:
  260. return NOTIFY_DONE;
  261. }
  262. }
  263. /* clock cooling device thermal callback functions are defined below */
  264. /**
  265. * clock_cooling_get_max_state - callback function to get the max cooling state.
  266. * @cdev: thermal cooling device pointer.
  267. * @state: fill this variable with the max cooling state.
  268. *
  269. * Callback for the thermal cooling device to return the clock
  270. * max cooling state.
  271. *
  272. * Return: 0 on success, an error code otherwise.
  273. */
  274. static int clock_cooling_get_max_state(struct thermal_cooling_device *cdev,
  275. unsigned long *state)
  276. {
  277. struct clock_cooling_device *ccdev = cdev->devdata;
  278. unsigned long count = 0;
  279. int ret;
  280. ret = clock_cooling_get_property(ccdev, 0, &count, GET_MAXL);
  281. if (!ret)
  282. *state = count;
  283. return ret;
  284. }
  285. /**
  286. * clock_cooling_get_cur_state - function to get the current cooling state.
  287. * @cdev: thermal cooling device pointer.
  288. * @state: fill this variable with the current cooling state.
  289. *
  290. * Callback for the thermal cooling device to return the clock
  291. * current cooling state.
  292. *
  293. * Return: 0 (success)
  294. */
  295. static int clock_cooling_get_cur_state(struct thermal_cooling_device *cdev,
  296. unsigned long *state)
  297. {
  298. struct clock_cooling_device *ccdev = cdev->devdata;
  299. *state = ccdev->clock_state;
  300. return 0;
  301. }
  302. /**
  303. * clock_cooling_set_cur_state - function to set the current cooling state.
  304. * @cdev: thermal cooling device pointer.
  305. * @state: set this variable to the current cooling state.
  306. *
  307. * Callback for the thermal cooling device to change the clock cooling
  308. * current cooling state.
  309. *
  310. * Return: 0 on success, an error code otherwise.
  311. */
  312. static int clock_cooling_set_cur_state(struct thermal_cooling_device *cdev,
  313. unsigned long state)
  314. {
  315. struct clock_cooling_device *clock_device = cdev->devdata;
  316. return clock_cooling_apply(clock_device, state);
  317. }
  318. /* Bind clock callbacks to thermal cooling device ops */
  319. static struct thermal_cooling_device_ops const clock_cooling_ops = {
  320. .get_max_state = clock_cooling_get_max_state,
  321. .get_cur_state = clock_cooling_get_cur_state,
  322. .set_cur_state = clock_cooling_set_cur_state,
  323. };
  324. /**
  325. * clock_cooling_register - function to create clock cooling device.
  326. * @dev: struct device pointer to the device used as clock cooling device.
  327. * @clock_name: string containing the clock used as cooling mechanism.
  328. *
  329. * This interface function registers the clock cooling device with the name
  330. * "thermal-clock-%x". The cooling device is based on clock frequencies.
  331. * The struct device is assumed to be capable of DVFS transitions.
  332. * The OPP layer is used to fetch and fill the available frequencies for
  333. * the referred device. The ordered frequency table is used to control
  334. * the clock cooling device cooling states and to limit clock transitions
  335. * based on the cooling state requested by the thermal framework.
  336. *
  337. * Return: a valid struct thermal_cooling_device pointer on success,
  338. * on failure, it returns a corresponding ERR_PTR().
  339. */
  340. struct thermal_cooling_device *
  341. clock_cooling_register(struct device *dev, const char *clock_name)
  342. {
  343. struct thermal_cooling_device *cdev;
  344. struct clock_cooling_device *ccdev = NULL;
  345. char dev_name[THERMAL_NAME_LENGTH];
  346. int ret = 0;
  347. ccdev = devm_kzalloc(dev, sizeof(*ccdev), GFP_KERNEL);
  348. if (!ccdev)
  349. return ERR_PTR(-ENOMEM);
  350. mutex_init(&ccdev->lock);
  351. ccdev->dev = dev;
  352. ccdev->clk = devm_clk_get(dev, clock_name);
  353. if (IS_ERR(ccdev->clk))
  354. return ERR_CAST(ccdev->clk);
  355. ret = ida_simple_get(&clock_ida, 0, 0, GFP_KERNEL);
  356. if (ret < 0)
  357. return ERR_PTR(ret);
  358. ccdev->id = ret;
  359. snprintf(dev_name, sizeof(dev_name), "thermal-clock-%d", ccdev->id);
  360. cdev = thermal_cooling_device_register(dev_name, ccdev,
  361. &clock_cooling_ops);
  362. if (IS_ERR(cdev)) {
  363. ida_simple_remove(&clock_ida, ccdev->id);
  364. return ERR_PTR(-EINVAL);
  365. }
  366. ccdev->cdev = cdev;
  367. ccdev->clk_rate_change_nb.notifier_call = clock_cooling_clock_notifier;
  368. /* Assuming someone has already filled the opp table for this device */
  369. ret = dev_pm_opp_init_cpufreq_table(dev, &ccdev->freq_table);
  370. if (ret) {
  371. ida_simple_remove(&clock_ida, ccdev->id);
  372. return ERR_PTR(ret);
  373. }
  374. ccdev->clock_state = 0;
  375. ccdev->clock_val = clock_cooling_get_frequency(ccdev, 0);
  376. clk_notifier_register(ccdev->clk, &ccdev->clk_rate_change_nb);
  377. return cdev;
  378. }
  379. EXPORT_SYMBOL_GPL(clock_cooling_register);
  380. /**
  381. * clock_cooling_unregister - function to remove clock cooling device.
  382. * @cdev: thermal cooling device pointer.
  383. *
  384. * This interface function unregisters the "thermal-clock-%x" cooling device.
  385. */
  386. void clock_cooling_unregister(struct thermal_cooling_device *cdev)
  387. {
  388. struct clock_cooling_device *ccdev;
  389. if (!cdev)
  390. return;
  391. ccdev = cdev->devdata;
  392. clk_notifier_unregister(ccdev->clk, &ccdev->clk_rate_change_nb);
  393. dev_pm_opp_free_cpufreq_table(ccdev->dev, &ccdev->freq_table);
  394. thermal_cooling_device_unregister(ccdev->cdev);
  395. ida_simple_remove(&clock_ida, ccdev->id);
  396. }
  397. EXPORT_SYMBOL_GPL(clock_cooling_unregister);