exynos_thermal_common.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /*
  2. * exynos_thermal_common.c - Samsung EXYNOS common thermal file
  3. *
  4. * Copyright (C) 2013 Samsung Electronics
  5. * Amit Daniel Kachhap <amit.daniel@samsung.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. */
  22. #include <linux/cpu_cooling.h>
  23. #include <linux/err.h>
  24. #include <linux/platform_data/exynos_thermal.h>
  25. #include <linux/slab.h>
  26. #include <linux/thermal.h>
  27. #include "exynos_thermal_common.h"
  28. struct exynos_thermal_zone {
  29. enum thermal_device_mode mode;
  30. struct thermal_zone_device *therm_dev;
  31. struct thermal_cooling_device *cool_dev[MAX_COOLING_DEVICE];
  32. unsigned int cool_dev_size;
  33. struct platform_device *exynos4_dev;
  34. struct thermal_sensor_conf *sensor_conf;
  35. bool bind;
  36. };
  37. static struct exynos_thermal_zone *th_zone;
  38. /* Get mode callback functions for thermal zone */
  39. static int exynos_get_mode(struct thermal_zone_device *thermal,
  40. enum thermal_device_mode *mode)
  41. {
  42. if (th_zone)
  43. *mode = th_zone->mode;
  44. return 0;
  45. }
  46. /* Set mode callback functions for thermal zone */
  47. static int exynos_set_mode(struct thermal_zone_device *thermal,
  48. enum thermal_device_mode mode)
  49. {
  50. if (!th_zone->therm_dev) {
  51. pr_notice("thermal zone not registered\n");
  52. return 0;
  53. }
  54. mutex_lock(&th_zone->therm_dev->lock);
  55. if (mode == THERMAL_DEVICE_ENABLED &&
  56. !th_zone->sensor_conf->trip_data.trigger_falling)
  57. th_zone->therm_dev->polling_delay = IDLE_INTERVAL;
  58. else
  59. th_zone->therm_dev->polling_delay = 0;
  60. mutex_unlock(&th_zone->therm_dev->lock);
  61. th_zone->mode = mode;
  62. thermal_zone_device_update(th_zone->therm_dev);
  63. pr_info("thermal polling set for duration=%d msec\n",
  64. th_zone->therm_dev->polling_delay);
  65. return 0;
  66. }
  67. /* Get trip type callback functions for thermal zone */
  68. static int exynos_get_trip_type(struct thermal_zone_device *thermal, int trip,
  69. enum thermal_trip_type *type)
  70. {
  71. switch (GET_ZONE(trip)) {
  72. case MONITOR_ZONE:
  73. case WARN_ZONE:
  74. *type = THERMAL_TRIP_ACTIVE;
  75. break;
  76. case PANIC_ZONE:
  77. *type = THERMAL_TRIP_CRITICAL;
  78. break;
  79. default:
  80. return -EINVAL;
  81. }
  82. return 0;
  83. }
  84. /* Get trip temperature callback functions for thermal zone */
  85. static int exynos_get_trip_temp(struct thermal_zone_device *thermal, int trip,
  86. unsigned long *temp)
  87. {
  88. if (trip < GET_TRIP(MONITOR_ZONE) || trip > GET_TRIP(PANIC_ZONE))
  89. return -EINVAL;
  90. *temp = th_zone->sensor_conf->trip_data.trip_val[trip];
  91. /* convert the temperature into millicelsius */
  92. *temp = *temp * MCELSIUS;
  93. return 0;
  94. }
  95. /* Get critical temperature callback functions for thermal zone */
  96. static int exynos_get_crit_temp(struct thermal_zone_device *thermal,
  97. unsigned long *temp)
  98. {
  99. int ret;
  100. /* Panic zone */
  101. ret = exynos_get_trip_temp(thermal, GET_TRIP(PANIC_ZONE), temp);
  102. return ret;
  103. }
  104. /* Bind callback functions for thermal zone */
  105. static int exynos_bind(struct thermal_zone_device *thermal,
  106. struct thermal_cooling_device *cdev)
  107. {
  108. int ret = 0, i, tab_size, level;
  109. struct freq_clip_table *tab_ptr, *clip_data;
  110. struct thermal_sensor_conf *data = th_zone->sensor_conf;
  111. tab_ptr = (struct freq_clip_table *)data->cooling_data.freq_data;
  112. tab_size = data->cooling_data.freq_clip_count;
  113. if (tab_ptr == NULL || tab_size == 0)
  114. return -EINVAL;
  115. /* find the cooling device registered*/
  116. for (i = 0; i < th_zone->cool_dev_size; i++)
  117. if (cdev == th_zone->cool_dev[i])
  118. break;
  119. /* No matching cooling device */
  120. if (i == th_zone->cool_dev_size)
  121. return 0;
  122. /* Bind the thermal zone to the cpufreq cooling device */
  123. for (i = 0; i < tab_size; i++) {
  124. clip_data = (struct freq_clip_table *)&(tab_ptr[i]);
  125. level = cpufreq_cooling_get_level(0, clip_data->freq_clip_max);
  126. if (level == THERMAL_CSTATE_INVALID)
  127. return 0;
  128. switch (GET_ZONE(i)) {
  129. case MONITOR_ZONE:
  130. case WARN_ZONE:
  131. if (thermal_zone_bind_cooling_device(thermal, i, cdev,
  132. level, 0)) {
  133. pr_err("error binding cdev inst %d\n", i);
  134. ret = -EINVAL;
  135. }
  136. th_zone->bind = true;
  137. break;
  138. default:
  139. ret = -EINVAL;
  140. }
  141. }
  142. return ret;
  143. }
  144. /* Unbind callback functions for thermal zone */
  145. static int exynos_unbind(struct thermal_zone_device *thermal,
  146. struct thermal_cooling_device *cdev)
  147. {
  148. int ret = 0, i, tab_size;
  149. struct thermal_sensor_conf *data = th_zone->sensor_conf;
  150. if (th_zone->bind == false)
  151. return 0;
  152. tab_size = data->cooling_data.freq_clip_count;
  153. if (tab_size == 0)
  154. return -EINVAL;
  155. /* find the cooling device registered*/
  156. for (i = 0; i < th_zone->cool_dev_size; i++)
  157. if (cdev == th_zone->cool_dev[i])
  158. break;
  159. /* No matching cooling device */
  160. if (i == th_zone->cool_dev_size)
  161. return 0;
  162. /* Bind the thermal zone to the cpufreq cooling device */
  163. for (i = 0; i < tab_size; i++) {
  164. switch (GET_ZONE(i)) {
  165. case MONITOR_ZONE:
  166. case WARN_ZONE:
  167. if (thermal_zone_unbind_cooling_device(thermal, i,
  168. cdev)) {
  169. pr_err("error unbinding cdev inst=%d\n", i);
  170. ret = -EINVAL;
  171. }
  172. th_zone->bind = false;
  173. break;
  174. default:
  175. ret = -EINVAL;
  176. }
  177. }
  178. return ret;
  179. }
  180. /* Get temperature callback functions for thermal zone */
  181. static int exynos_get_temp(struct thermal_zone_device *thermal,
  182. unsigned long *temp)
  183. {
  184. void *data;
  185. if (!th_zone->sensor_conf) {
  186. pr_info("Temperature sensor not initialised\n");
  187. return -EINVAL;
  188. }
  189. data = th_zone->sensor_conf->private_data;
  190. *temp = th_zone->sensor_conf->read_temperature(data);
  191. /* convert the temperature into millicelsius */
  192. *temp = *temp * MCELSIUS;
  193. return 0;
  194. }
  195. /* Get temperature callback functions for thermal zone */
  196. static int exynos_set_emul_temp(struct thermal_zone_device *thermal,
  197. unsigned long temp)
  198. {
  199. void *data;
  200. int ret = -EINVAL;
  201. if (!th_zone->sensor_conf) {
  202. pr_info("Temperature sensor not initialised\n");
  203. return -EINVAL;
  204. }
  205. data = th_zone->sensor_conf->private_data;
  206. if (th_zone->sensor_conf->write_emul_temp)
  207. ret = th_zone->sensor_conf->write_emul_temp(data, temp);
  208. return ret;
  209. }
  210. /* Get the temperature trend */
  211. static int exynos_get_trend(struct thermal_zone_device *thermal,
  212. int trip, enum thermal_trend *trend)
  213. {
  214. int ret;
  215. unsigned long trip_temp;
  216. ret = exynos_get_trip_temp(thermal, trip, &trip_temp);
  217. if (ret < 0)
  218. return ret;
  219. if (thermal->temperature >= trip_temp)
  220. *trend = THERMAL_TREND_RAISE_FULL;
  221. else
  222. *trend = THERMAL_TREND_DROP_FULL;
  223. return 0;
  224. }
  225. /* Operation callback functions for thermal zone */
  226. static struct thermal_zone_device_ops const exynos_dev_ops = {
  227. .bind = exynos_bind,
  228. .unbind = exynos_unbind,
  229. .get_temp = exynos_get_temp,
  230. .set_emul_temp = exynos_set_emul_temp,
  231. .get_trend = exynos_get_trend,
  232. .get_mode = exynos_get_mode,
  233. .set_mode = exynos_set_mode,
  234. .get_trip_type = exynos_get_trip_type,
  235. .get_trip_temp = exynos_get_trip_temp,
  236. .get_crit_temp = exynos_get_crit_temp,
  237. };
  238. /*
  239. * This function may be called from interrupt based temperature sensor
  240. * when threshold is changed.
  241. */
  242. void exynos_report_trigger(void)
  243. {
  244. unsigned int i;
  245. char data[10];
  246. char *envp[] = { data, NULL };
  247. if (!th_zone || !th_zone->therm_dev)
  248. return;
  249. if (th_zone->bind == false) {
  250. for (i = 0; i < th_zone->cool_dev_size; i++) {
  251. if (!th_zone->cool_dev[i])
  252. continue;
  253. exynos_bind(th_zone->therm_dev,
  254. th_zone->cool_dev[i]);
  255. }
  256. }
  257. thermal_zone_device_update(th_zone->therm_dev);
  258. mutex_lock(&th_zone->therm_dev->lock);
  259. /* Find the level for which trip happened */
  260. for (i = 0; i < th_zone->sensor_conf->trip_data.trip_count; i++) {
  261. if (th_zone->therm_dev->last_temperature <
  262. th_zone->sensor_conf->trip_data.trip_val[i] * MCELSIUS)
  263. break;
  264. }
  265. if (th_zone->mode == THERMAL_DEVICE_ENABLED &&
  266. !th_zone->sensor_conf->trip_data.trigger_falling) {
  267. if (i > 0)
  268. th_zone->therm_dev->polling_delay = ACTIVE_INTERVAL;
  269. else
  270. th_zone->therm_dev->polling_delay = IDLE_INTERVAL;
  271. }
  272. snprintf(data, sizeof(data), "%u", i);
  273. kobject_uevent_env(&th_zone->therm_dev->device.kobj, KOBJ_CHANGE, envp);
  274. mutex_unlock(&th_zone->therm_dev->lock);
  275. }
  276. /* Register with the in-kernel thermal management */
  277. int exynos_register_thermal(struct thermal_sensor_conf *sensor_conf)
  278. {
  279. int ret;
  280. struct cpumask mask_val;
  281. if (!sensor_conf || !sensor_conf->read_temperature) {
  282. pr_err("Temperature sensor not initialised\n");
  283. return -EINVAL;
  284. }
  285. th_zone = kzalloc(sizeof(struct exynos_thermal_zone), GFP_KERNEL);
  286. if (!th_zone)
  287. return -ENOMEM;
  288. th_zone->sensor_conf = sensor_conf;
  289. cpumask_set_cpu(0, &mask_val);
  290. th_zone->cool_dev[0] = cpufreq_cooling_register(&mask_val);
  291. if (IS_ERR(th_zone->cool_dev[0])) {
  292. pr_err("Failed to register cpufreq cooling device\n");
  293. ret = -EINVAL;
  294. goto err_unregister;
  295. }
  296. th_zone->cool_dev_size++;
  297. th_zone->therm_dev = thermal_zone_device_register(sensor_conf->name,
  298. EXYNOS_ZONE_COUNT, 0, NULL, &exynos_dev_ops, NULL, 0,
  299. sensor_conf->trip_data.trigger_falling ?
  300. 0 : IDLE_INTERVAL);
  301. if (IS_ERR(th_zone->therm_dev)) {
  302. pr_err("Failed to register thermal zone device\n");
  303. ret = PTR_ERR(th_zone->therm_dev);
  304. goto err_unregister;
  305. }
  306. th_zone->mode = THERMAL_DEVICE_ENABLED;
  307. pr_info("Exynos: Kernel Thermal management registered\n");
  308. return 0;
  309. err_unregister:
  310. exynos_unregister_thermal();
  311. return ret;
  312. }
  313. /* Un-Register with the in-kernel thermal management */
  314. void exynos_unregister_thermal(void)
  315. {
  316. int i;
  317. if (!th_zone)
  318. return;
  319. if (th_zone->therm_dev)
  320. thermal_zone_device_unregister(th_zone->therm_dev);
  321. for (i = 0; i < th_zone->cool_dev_size; i++) {
  322. if (th_zone->cool_dev[i])
  323. cpufreq_cooling_unregister(th_zone->cool_dev[i]);
  324. }
  325. kfree(th_zone);
  326. pr_info("Exynos: Kernel Thermal management unregistered\n");
  327. }