exynos_thermal_common.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. dev_err(&thermal->device,
  52. "thermal zone not registered\n");
  53. return 0;
  54. }
  55. mutex_lock(&thermal->lock);
  56. if (mode == THERMAL_DEVICE_ENABLED &&
  57. !th_zone->sensor_conf->trip_data.trigger_falling)
  58. thermal->polling_delay = IDLE_INTERVAL;
  59. else
  60. thermal->polling_delay = 0;
  61. mutex_unlock(&thermal->lock);
  62. th_zone->mode = mode;
  63. thermal_zone_device_update(thermal);
  64. dev_dbg(th_zone->sensor_conf->dev,
  65. "thermal polling set for duration=%d msec\n",
  66. thermal->polling_delay);
  67. return 0;
  68. }
  69. /* Get trip type callback functions for thermal zone */
  70. static int exynos_get_trip_type(struct thermal_zone_device *thermal, int trip,
  71. enum thermal_trip_type *type)
  72. {
  73. struct exynos_thermal_zone *th_zone = thermal->devdata;
  74. int max_trip = th_zone->sensor_conf->trip_data.trip_count;
  75. int trip_type;
  76. if (trip < 0 || trip >= max_trip)
  77. return -EINVAL;
  78. trip_type = th_zone->sensor_conf->trip_data.trip_type[trip];
  79. if (trip_type == SW_TRIP)
  80. *type = THERMAL_TRIP_CRITICAL;
  81. else if (trip_type == THROTTLE_ACTIVE)
  82. *type = THERMAL_TRIP_ACTIVE;
  83. else if (trip_type == THROTTLE_PASSIVE)
  84. *type = THERMAL_TRIP_PASSIVE;
  85. else
  86. return -EINVAL;
  87. return 0;
  88. }
  89. /* Get trip temperature callback functions for thermal zone */
  90. static int exynos_get_trip_temp(struct thermal_zone_device *thermal, int trip,
  91. unsigned long *temp)
  92. {
  93. struct exynos_thermal_zone *th_zone = thermal->devdata;
  94. int max_trip = th_zone->sensor_conf->trip_data.trip_count;
  95. if (trip < 0 || trip >= max_trip)
  96. return -EINVAL;
  97. *temp = th_zone->sensor_conf->trip_data.trip_val[trip];
  98. /* convert the temperature into millicelsius */
  99. *temp = *temp * MCELSIUS;
  100. return 0;
  101. }
  102. /* Get critical temperature callback functions for thermal zone */
  103. static int exynos_get_crit_temp(struct thermal_zone_device *thermal,
  104. unsigned long *temp)
  105. {
  106. struct exynos_thermal_zone *th_zone = thermal->devdata;
  107. int max_trip = th_zone->sensor_conf->trip_data.trip_count;
  108. /* Get the temp of highest trip*/
  109. return exynos_get_trip_temp(thermal, max_trip - 1, temp);
  110. }
  111. /* Bind callback functions for thermal zone */
  112. static int exynos_bind(struct thermal_zone_device *thermal,
  113. struct thermal_cooling_device *cdev)
  114. {
  115. int ret = 0, i, tab_size, level;
  116. struct freq_clip_table *tab_ptr, *clip_data;
  117. struct exynos_thermal_zone *th_zone = thermal->devdata;
  118. struct thermal_sensor_conf *data = th_zone->sensor_conf;
  119. tab_ptr = (struct freq_clip_table *)data->cooling_data.freq_data;
  120. tab_size = data->cooling_data.freq_clip_count;
  121. if (tab_ptr == NULL || tab_size == 0)
  122. return 0;
  123. /* find the cooling device registered*/
  124. for (i = 0; i < th_zone->cool_dev_size; i++)
  125. if (cdev == th_zone->cool_dev[i])
  126. break;
  127. /* No matching cooling device */
  128. if (i == th_zone->cool_dev_size)
  129. return 0;
  130. /* Bind the thermal zone to the cpufreq cooling device */
  131. for (i = 0; i < tab_size; i++) {
  132. clip_data = (struct freq_clip_table *)&(tab_ptr[i]);
  133. level = cpufreq_cooling_get_level(0, clip_data->freq_clip_max);
  134. if (level == THERMAL_CSTATE_INVALID)
  135. return 0;
  136. switch (GET_ZONE(i)) {
  137. case MONITOR_ZONE:
  138. case WARN_ZONE:
  139. if (thermal_zone_bind_cooling_device(thermal, i, cdev,
  140. level, 0)) {
  141. dev_err(data->dev,
  142. "error unbinding cdev inst=%d\n", i);
  143. ret = -EINVAL;
  144. }
  145. th_zone->bind = true;
  146. break;
  147. default:
  148. ret = -EINVAL;
  149. }
  150. }
  151. return ret;
  152. }
  153. /* Unbind callback functions for thermal zone */
  154. static int exynos_unbind(struct thermal_zone_device *thermal,
  155. struct thermal_cooling_device *cdev)
  156. {
  157. int ret = 0, i, tab_size;
  158. struct exynos_thermal_zone *th_zone = thermal->devdata;
  159. struct thermal_sensor_conf *data = th_zone->sensor_conf;
  160. if (th_zone->bind == false)
  161. return 0;
  162. tab_size = data->cooling_data.freq_clip_count;
  163. if (tab_size == 0)
  164. return 0;
  165. /* find the cooling device registered*/
  166. for (i = 0; i < th_zone->cool_dev_size; i++)
  167. if (cdev == th_zone->cool_dev[i])
  168. break;
  169. /* No matching cooling device */
  170. if (i == th_zone->cool_dev_size)
  171. return 0;
  172. /* Bind the thermal zone to the cpufreq cooling device */
  173. for (i = 0; i < tab_size; i++) {
  174. switch (GET_ZONE(i)) {
  175. case MONITOR_ZONE:
  176. case WARN_ZONE:
  177. if (thermal_zone_unbind_cooling_device(thermal, i,
  178. cdev)) {
  179. dev_err(data->dev,
  180. "error unbinding cdev inst=%d\n", i);
  181. ret = -EINVAL;
  182. }
  183. th_zone->bind = false;
  184. break;
  185. default:
  186. ret = -EINVAL;
  187. }
  188. }
  189. return ret;
  190. }
  191. /* Get temperature callback functions for thermal zone */
  192. static int exynos_get_temp(struct thermal_zone_device *thermal,
  193. unsigned long *temp)
  194. {
  195. struct exynos_thermal_zone *th_zone = thermal->devdata;
  196. void *data;
  197. if (!th_zone->sensor_conf) {
  198. dev_err(&thermal->device,
  199. "Temperature sensor not initialised\n");
  200. return -EINVAL;
  201. }
  202. data = th_zone->sensor_conf->driver_data;
  203. *temp = th_zone->sensor_conf->read_temperature(data);
  204. /* convert the temperature into millicelsius */
  205. *temp = *temp * MCELSIUS;
  206. return 0;
  207. }
  208. /* Get temperature callback functions for thermal zone */
  209. static int exynos_set_emul_temp(struct thermal_zone_device *thermal,
  210. unsigned long temp)
  211. {
  212. void *data;
  213. int ret = -EINVAL;
  214. struct exynos_thermal_zone *th_zone = thermal->devdata;
  215. if (!th_zone->sensor_conf) {
  216. dev_err(&thermal->device,
  217. "Temperature sensor not initialised\n");
  218. return -EINVAL;
  219. }
  220. data = th_zone->sensor_conf->driver_data;
  221. if (th_zone->sensor_conf->write_emul_temp)
  222. ret = th_zone->sensor_conf->write_emul_temp(data, temp);
  223. return ret;
  224. }
  225. /* Get the temperature trend */
  226. static int exynos_get_trend(struct thermal_zone_device *thermal,
  227. int trip, enum thermal_trend *trend)
  228. {
  229. int ret;
  230. unsigned long trip_temp;
  231. ret = exynos_get_trip_temp(thermal, trip, &trip_temp);
  232. if (ret < 0)
  233. return ret;
  234. if (thermal->temperature >= trip_temp)
  235. *trend = THERMAL_TREND_RAISE_FULL;
  236. else
  237. *trend = THERMAL_TREND_DROP_FULL;
  238. return 0;
  239. }
  240. /* Operation callback functions for thermal zone */
  241. static struct thermal_zone_device_ops const exynos_dev_ops = {
  242. .bind = exynos_bind,
  243. .unbind = exynos_unbind,
  244. .get_temp = exynos_get_temp,
  245. .set_emul_temp = exynos_set_emul_temp,
  246. .get_trend = exynos_get_trend,
  247. .get_mode = exynos_get_mode,
  248. .set_mode = exynos_set_mode,
  249. .get_trip_type = exynos_get_trip_type,
  250. .get_trip_temp = exynos_get_trip_temp,
  251. .get_crit_temp = exynos_get_crit_temp,
  252. };
  253. /*
  254. * This function may be called from interrupt based temperature sensor
  255. * when threshold is changed.
  256. */
  257. void exynos_report_trigger(struct thermal_sensor_conf *conf)
  258. {
  259. unsigned int i;
  260. char data[10];
  261. char *envp[] = { data, NULL };
  262. struct exynos_thermal_zone *th_zone;
  263. if (!conf || !conf->pzone_data) {
  264. pr_err("Invalid temperature sensor configuration data\n");
  265. return;
  266. }
  267. th_zone = conf->pzone_data;
  268. if (th_zone->therm_dev)
  269. return;
  270. if (th_zone->bind == false) {
  271. for (i = 0; i < th_zone->cool_dev_size; i++) {
  272. if (!th_zone->cool_dev[i])
  273. continue;
  274. exynos_bind(th_zone->therm_dev,
  275. th_zone->cool_dev[i]);
  276. }
  277. }
  278. thermal_zone_device_update(th_zone->therm_dev);
  279. mutex_lock(&th_zone->therm_dev->lock);
  280. /* Find the level for which trip happened */
  281. for (i = 0; i < th_zone->sensor_conf->trip_data.trip_count; i++) {
  282. if (th_zone->therm_dev->last_temperature <
  283. th_zone->sensor_conf->trip_data.trip_val[i] * MCELSIUS)
  284. break;
  285. }
  286. if (th_zone->mode == THERMAL_DEVICE_ENABLED &&
  287. !th_zone->sensor_conf->trip_data.trigger_falling) {
  288. if (i > 0)
  289. th_zone->therm_dev->polling_delay = ACTIVE_INTERVAL;
  290. else
  291. th_zone->therm_dev->polling_delay = IDLE_INTERVAL;
  292. }
  293. snprintf(data, sizeof(data), "%u", i);
  294. kobject_uevent_env(&th_zone->therm_dev->device.kobj, KOBJ_CHANGE, envp);
  295. mutex_unlock(&th_zone->therm_dev->lock);
  296. }
  297. /* Register with the in-kernel thermal management */
  298. int exynos_register_thermal(struct thermal_sensor_conf *sensor_conf)
  299. {
  300. int ret;
  301. struct cpumask mask_val;
  302. struct exynos_thermal_zone *th_zone;
  303. if (!sensor_conf || !sensor_conf->read_temperature) {
  304. pr_err("Temperature sensor not initialised\n");
  305. return -EINVAL;
  306. }
  307. th_zone = devm_kzalloc(sensor_conf->dev,
  308. sizeof(struct exynos_thermal_zone), GFP_KERNEL);
  309. if (!th_zone)
  310. return -ENOMEM;
  311. th_zone->sensor_conf = sensor_conf;
  312. /*
  313. * TODO: 1) Handle multiple cooling devices in a thermal zone
  314. * 2) Add a flag/name in cooling info to map to specific
  315. * sensor
  316. */
  317. if (sensor_conf->cooling_data.freq_clip_count > 0) {
  318. cpumask_set_cpu(0, &mask_val);
  319. th_zone->cool_dev[th_zone->cool_dev_size] =
  320. cpufreq_cooling_register(&mask_val);
  321. if (IS_ERR(th_zone->cool_dev[th_zone->cool_dev_size])) {
  322. dev_err(sensor_conf->dev,
  323. "Failed to register cpufreq cooling device\n");
  324. ret = -EINVAL;
  325. goto err_unregister;
  326. }
  327. th_zone->cool_dev_size++;
  328. }
  329. th_zone->therm_dev = thermal_zone_device_register(
  330. sensor_conf->name, sensor_conf->trip_data.trip_count,
  331. 0, th_zone, &exynos_dev_ops, NULL, 0,
  332. sensor_conf->trip_data.trigger_falling ? 0 :
  333. IDLE_INTERVAL);
  334. if (IS_ERR(th_zone->therm_dev)) {
  335. dev_err(sensor_conf->dev,
  336. "Failed to register thermal zone device\n");
  337. ret = PTR_ERR(th_zone->therm_dev);
  338. goto err_unregister;
  339. }
  340. th_zone->mode = THERMAL_DEVICE_ENABLED;
  341. sensor_conf->pzone_data = th_zone;
  342. dev_info(sensor_conf->dev,
  343. "Exynos: Thermal zone(%s) registered\n", sensor_conf->name);
  344. return 0;
  345. err_unregister:
  346. exynos_unregister_thermal(sensor_conf);
  347. return ret;
  348. }
  349. /* Un-Register with the in-kernel thermal management */
  350. void exynos_unregister_thermal(struct thermal_sensor_conf *sensor_conf)
  351. {
  352. int i;
  353. struct exynos_thermal_zone *th_zone;
  354. if (!sensor_conf || !sensor_conf->pzone_data) {
  355. pr_err("Invalid temperature sensor configuration data\n");
  356. return;
  357. }
  358. th_zone = sensor_conf->pzone_data;
  359. if (th_zone->therm_dev)
  360. thermal_zone_device_unregister(th_zone->therm_dev);
  361. for (i = 0; i < th_zone->cool_dev_size; i++) {
  362. if (th_zone->cool_dev[i])
  363. cpufreq_cooling_unregister(th_zone->cool_dev[i]);
  364. }
  365. dev_info(sensor_conf->dev,
  366. "Exynos: Kernel Thermal management unregistered\n");
  367. }