exynos_thermal_common.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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/slab.h>
  25. #include <linux/thermal.h>
  26. #include "exynos_thermal_common.h"
  27. struct exynos_thermal_zone {
  28. enum thermal_device_mode mode;
  29. struct thermal_zone_device *therm_dev;
  30. struct thermal_cooling_device *cool_dev[MAX_COOLING_DEVICE];
  31. unsigned int cool_dev_size;
  32. struct platform_device *exynos4_dev;
  33. struct thermal_sensor_conf *sensor_conf;
  34. bool bind;
  35. };
  36. /* Get mode callback functions for thermal zone */
  37. static int exynos_get_mode(struct thermal_zone_device *thermal,
  38. enum thermal_device_mode *mode)
  39. {
  40. struct exynos_thermal_zone *th_zone = thermal->devdata;
  41. if (th_zone)
  42. *mode = th_zone->mode;
  43. return 0;
  44. }
  45. /* Set mode callback functions for thermal zone */
  46. static int exynos_set_mode(struct thermal_zone_device *thermal,
  47. enum thermal_device_mode mode)
  48. {
  49. struct exynos_thermal_zone *th_zone = thermal->devdata;
  50. if (!th_zone) {
  51. pr_notice("thermal zone not registered\n");
  52. return 0;
  53. }
  54. mutex_lock(&thermal->lock);
  55. if (mode == THERMAL_DEVICE_ENABLED &&
  56. !th_zone->sensor_conf->trip_data.trigger_falling)
  57. thermal->polling_delay = IDLE_INTERVAL;
  58. else
  59. thermal->polling_delay = 0;
  60. mutex_unlock(&thermal->lock);
  61. th_zone->mode = mode;
  62. thermal_zone_device_update(thermal);
  63. pr_info("thermal polling set for duration=%d msec\n",
  64. thermal->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. struct exynos_thermal_zone *th_zone = thermal->devdata;
  89. if (trip < GET_TRIP(MONITOR_ZONE) || trip > GET_TRIP(PANIC_ZONE))
  90. return -EINVAL;
  91. *temp = th_zone->sensor_conf->trip_data.trip_val[trip];
  92. /* convert the temperature into millicelsius */
  93. *temp = *temp * MCELSIUS;
  94. return 0;
  95. }
  96. /* Get critical temperature callback functions for thermal zone */
  97. static int exynos_get_crit_temp(struct thermal_zone_device *thermal,
  98. unsigned long *temp)
  99. {
  100. int ret;
  101. /* Panic zone */
  102. ret = exynos_get_trip_temp(thermal, GET_TRIP(PANIC_ZONE), temp);
  103. return ret;
  104. }
  105. /* Bind callback functions for thermal zone */
  106. static int exynos_bind(struct thermal_zone_device *thermal,
  107. struct thermal_cooling_device *cdev)
  108. {
  109. int ret = 0, i, tab_size, level;
  110. struct freq_clip_table *tab_ptr, *clip_data;
  111. struct exynos_thermal_zone *th_zone = thermal->devdata;
  112. struct thermal_sensor_conf *data = th_zone->sensor_conf;
  113. tab_ptr = (struct freq_clip_table *)data->cooling_data.freq_data;
  114. tab_size = data->cooling_data.freq_clip_count;
  115. if (tab_ptr == NULL || tab_size == 0)
  116. return -EINVAL;
  117. /* find the cooling device registered*/
  118. for (i = 0; i < th_zone->cool_dev_size; i++)
  119. if (cdev == th_zone->cool_dev[i])
  120. break;
  121. /* No matching cooling device */
  122. if (i == th_zone->cool_dev_size)
  123. return 0;
  124. /* Bind the thermal zone to the cpufreq cooling device */
  125. for (i = 0; i < tab_size; i++) {
  126. clip_data = (struct freq_clip_table *)&(tab_ptr[i]);
  127. level = cpufreq_cooling_get_level(0, clip_data->freq_clip_max);
  128. if (level == THERMAL_CSTATE_INVALID)
  129. return 0;
  130. switch (GET_ZONE(i)) {
  131. case MONITOR_ZONE:
  132. case WARN_ZONE:
  133. if (thermal_zone_bind_cooling_device(thermal, i, cdev,
  134. level, 0)) {
  135. pr_err("error binding cdev inst %d\n", i);
  136. ret = -EINVAL;
  137. }
  138. th_zone->bind = true;
  139. break;
  140. default:
  141. ret = -EINVAL;
  142. }
  143. }
  144. return ret;
  145. }
  146. /* Unbind callback functions for thermal zone */
  147. static int exynos_unbind(struct thermal_zone_device *thermal,
  148. struct thermal_cooling_device *cdev)
  149. {
  150. int ret = 0, i, tab_size;
  151. struct exynos_thermal_zone *th_zone = thermal->devdata;
  152. struct thermal_sensor_conf *data = th_zone->sensor_conf;
  153. if (th_zone->bind == false)
  154. return 0;
  155. tab_size = data->cooling_data.freq_clip_count;
  156. if (tab_size == 0)
  157. return -EINVAL;
  158. /* find the cooling device registered*/
  159. for (i = 0; i < th_zone->cool_dev_size; i++)
  160. if (cdev == th_zone->cool_dev[i])
  161. break;
  162. /* No matching cooling device */
  163. if (i == th_zone->cool_dev_size)
  164. return 0;
  165. /* Bind the thermal zone to the cpufreq cooling device */
  166. for (i = 0; i < tab_size; i++) {
  167. switch (GET_ZONE(i)) {
  168. case MONITOR_ZONE:
  169. case WARN_ZONE:
  170. if (thermal_zone_unbind_cooling_device(thermal, i,
  171. cdev)) {
  172. pr_err("error unbinding cdev inst=%d\n", i);
  173. ret = -EINVAL;
  174. }
  175. th_zone->bind = false;
  176. break;
  177. default:
  178. ret = -EINVAL;
  179. }
  180. }
  181. return ret;
  182. }
  183. /* Get temperature callback functions for thermal zone */
  184. static int exynos_get_temp(struct thermal_zone_device *thermal,
  185. unsigned long *temp)
  186. {
  187. struct exynos_thermal_zone *th_zone = thermal->devdata;
  188. void *data;
  189. if (!th_zone->sensor_conf) {
  190. pr_info("Temperature sensor not initialised\n");
  191. return -EINVAL;
  192. }
  193. data = th_zone->sensor_conf->private_data;
  194. *temp = th_zone->sensor_conf->read_temperature(data);
  195. /* convert the temperature into millicelsius */
  196. *temp = *temp * MCELSIUS;
  197. return 0;
  198. }
  199. /* Get temperature callback functions for thermal zone */
  200. static int exynos_set_emul_temp(struct thermal_zone_device *thermal,
  201. unsigned long temp)
  202. {
  203. void *data;
  204. int ret = -EINVAL;
  205. struct exynos_thermal_zone *th_zone = thermal->devdata;
  206. if (!th_zone->sensor_conf) {
  207. pr_info("Temperature sensor not initialised\n");
  208. return -EINVAL;
  209. }
  210. data = th_zone->sensor_conf->private_data;
  211. if (th_zone->sensor_conf->write_emul_temp)
  212. ret = th_zone->sensor_conf->write_emul_temp(data, temp);
  213. return ret;
  214. }
  215. /* Get the temperature trend */
  216. static int exynos_get_trend(struct thermal_zone_device *thermal,
  217. int trip, enum thermal_trend *trend)
  218. {
  219. int ret;
  220. unsigned long trip_temp;
  221. ret = exynos_get_trip_temp(thermal, trip, &trip_temp);
  222. if (ret < 0)
  223. return ret;
  224. if (thermal->temperature >= trip_temp)
  225. *trend = THERMAL_TREND_RAISE_FULL;
  226. else
  227. *trend = THERMAL_TREND_DROP_FULL;
  228. return 0;
  229. }
  230. /* Operation callback functions for thermal zone */
  231. static struct thermal_zone_device_ops const exynos_dev_ops = {
  232. .bind = exynos_bind,
  233. .unbind = exynos_unbind,
  234. .get_temp = exynos_get_temp,
  235. .set_emul_temp = exynos_set_emul_temp,
  236. .get_trend = exynos_get_trend,
  237. .get_mode = exynos_get_mode,
  238. .set_mode = exynos_set_mode,
  239. .get_trip_type = exynos_get_trip_type,
  240. .get_trip_temp = exynos_get_trip_temp,
  241. .get_crit_temp = exynos_get_crit_temp,
  242. };
  243. /*
  244. * This function may be called from interrupt based temperature sensor
  245. * when threshold is changed.
  246. */
  247. void exynos_report_trigger(struct thermal_sensor_conf *conf)
  248. {
  249. unsigned int i;
  250. char data[10];
  251. char *envp[] = { data, NULL };
  252. struct exynos_thermal_zone *th_zone;
  253. if (!conf || !conf->pzone_data) {
  254. pr_err("Invalid temperature sensor configuration data\n");
  255. return;
  256. }
  257. th_zone = conf->pzone_data;
  258. if (th_zone->therm_dev)
  259. return;
  260. if (th_zone->bind == false) {
  261. for (i = 0; i < th_zone->cool_dev_size; i++) {
  262. if (!th_zone->cool_dev[i])
  263. continue;
  264. exynos_bind(th_zone->therm_dev,
  265. th_zone->cool_dev[i]);
  266. }
  267. }
  268. thermal_zone_device_update(th_zone->therm_dev);
  269. mutex_lock(&th_zone->therm_dev->lock);
  270. /* Find the level for which trip happened */
  271. for (i = 0; i < th_zone->sensor_conf->trip_data.trip_count; i++) {
  272. if (th_zone->therm_dev->last_temperature <
  273. th_zone->sensor_conf->trip_data.trip_val[i] * MCELSIUS)
  274. break;
  275. }
  276. if (th_zone->mode == THERMAL_DEVICE_ENABLED &&
  277. !th_zone->sensor_conf->trip_data.trigger_falling) {
  278. if (i > 0)
  279. th_zone->therm_dev->polling_delay = ACTIVE_INTERVAL;
  280. else
  281. th_zone->therm_dev->polling_delay = IDLE_INTERVAL;
  282. }
  283. snprintf(data, sizeof(data), "%u", i);
  284. kobject_uevent_env(&th_zone->therm_dev->device.kobj, KOBJ_CHANGE, envp);
  285. mutex_unlock(&th_zone->therm_dev->lock);
  286. }
  287. /* Register with the in-kernel thermal management */
  288. int exynos_register_thermal(struct thermal_sensor_conf *sensor_conf)
  289. {
  290. int ret;
  291. struct cpumask mask_val;
  292. struct exynos_thermal_zone *th_zone;
  293. if (!sensor_conf || !sensor_conf->read_temperature) {
  294. pr_err("Temperature sensor not initialised\n");
  295. return -EINVAL;
  296. }
  297. th_zone = kzalloc(sizeof(struct exynos_thermal_zone), GFP_KERNEL);
  298. if (!th_zone)
  299. return -ENOMEM;
  300. th_zone->sensor_conf = sensor_conf;
  301. cpumask_set_cpu(0, &mask_val);
  302. th_zone->cool_dev[0] = cpufreq_cooling_register(&mask_val);
  303. if (IS_ERR(th_zone->cool_dev[0])) {
  304. pr_err("Failed to register cpufreq cooling device\n");
  305. ret = -EINVAL;
  306. goto err_unregister;
  307. }
  308. th_zone->cool_dev_size++;
  309. th_zone->therm_dev = thermal_zone_device_register(sensor_conf->name,
  310. EXYNOS_ZONE_COUNT, 0, th_zone, &exynos_dev_ops, NULL, 0,
  311. sensor_conf->trip_data.trigger_falling ?
  312. 0 : IDLE_INTERVAL);
  313. if (IS_ERR(th_zone->therm_dev)) {
  314. pr_err("Failed to register thermal zone device\n");
  315. ret = PTR_ERR(th_zone->therm_dev);
  316. goto err_unregister;
  317. }
  318. th_zone->mode = THERMAL_DEVICE_ENABLED;
  319. sensor_conf->pzone_data = th_zone;
  320. pr_info("Exynos: Kernel Thermal management registered\n");
  321. return 0;
  322. err_unregister:
  323. exynos_unregister_thermal(sensor_conf);
  324. return ret;
  325. }
  326. /* Un-Register with the in-kernel thermal management */
  327. void exynos_unregister_thermal(struct thermal_sensor_conf *sensor_conf)
  328. {
  329. int i;
  330. struct exynos_thermal_zone *th_zone;
  331. if (!sensor_conf || !sensor_conf->pzone_data) {
  332. pr_err("Invalid temperature sensor configuration data\n");
  333. return;
  334. }
  335. th_zone = sensor_conf->pzone_data;
  336. if (th_zone->therm_dev)
  337. thermal_zone_device_unregister(th_zone->therm_dev);
  338. for (i = 0; i < th_zone->cool_dev_size; i++) {
  339. if (th_zone->cool_dev[i])
  340. cpufreq_cooling_unregister(th_zone->cool_dev[i]);
  341. }
  342. kfree(th_zone);
  343. pr_info("Exynos: Kernel Thermal management unregistered\n");
  344. }