exynos_thermal_common.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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. struct exynos_thermal_zone *th_zone = thermal->devdata;
  116. struct thermal_sensor_conf *data = th_zone->sensor_conf;
  117. struct device_node *child, *gchild, *np;
  118. struct of_phandle_args cooling_spec;
  119. unsigned long max, state = 0;
  120. int ret = 0, i = 0;
  121. /*
  122. * Below code is necessary to skip binding when cpufreq's
  123. * frequency table is not yet initialized.
  124. */
  125. cdev->ops->get_max_state(cdev, &state);
  126. if (!state && !th_zone->cool_dev_size) {
  127. th_zone->cool_dev_size = 1;
  128. th_zone->cool_dev[0] = cdev;
  129. th_zone->bind = false;
  130. return 0;
  131. }
  132. np = of_find_node_by_path("/thermal-zones/cpu-thermal");
  133. if (!np) {
  134. pr_err("failed to find thmerla-zones/cpu-thermal node\n");
  135. return -ENOENT;
  136. }
  137. child = of_get_child_by_name(np, "cooling-maps");
  138. for_each_child_of_node(child, gchild) {
  139. ret = of_parse_phandle_with_args(gchild, "cooling-device",
  140. "#cooling-cells",
  141. 0, &cooling_spec);
  142. if (ret < 0) {
  143. pr_err("missing cooling_device property\n");
  144. goto end;
  145. }
  146. if (cooling_spec.args_count < 2) {
  147. ret = -EINVAL;
  148. goto end;
  149. }
  150. max = cooling_spec.args[0];
  151. if (thermal_zone_bind_cooling_device(thermal, i, cdev,
  152. max, 0)) {
  153. dev_err(data->dev,
  154. "thermal error unbinding cdev inst=%d\n", i);
  155. ret = -EINVAL;
  156. goto end;
  157. }
  158. i++;
  159. }
  160. th_zone->bind = true;
  161. end:
  162. of_node_put(child);
  163. of_node_put(np);
  164. return ret;
  165. }
  166. /* Unbind callback functions for thermal zone */
  167. static int exynos_unbind(struct thermal_zone_device *thermal,
  168. struct thermal_cooling_device *cdev)
  169. {
  170. int ret = 0, i;
  171. struct exynos_thermal_zone *th_zone = thermal->devdata;
  172. struct thermal_sensor_conf *data = th_zone->sensor_conf;
  173. struct device_node *child, *gchild, *np;
  174. if (th_zone->bind == false || !th_zone->cool_dev_size)
  175. return 0;
  176. /* find the cooling device registered*/
  177. for (i = 0; i < th_zone->cool_dev_size; i++)
  178. if (cdev == th_zone->cool_dev[i])
  179. break;
  180. /* No matching cooling device */
  181. if (i == th_zone->cool_dev_size)
  182. return 0;
  183. np = of_find_node_by_path("/thermal-zones/cpu-thermal");
  184. if (!np) {
  185. pr_err("failed to find thmerla-zones/cpu-thermal node\n");
  186. return -ENOENT;
  187. }
  188. child = of_get_child_by_name(np, "cooling-maps");
  189. i = 0;
  190. for_each_child_of_node(child, gchild) {
  191. if (thermal_zone_unbind_cooling_device(thermal, i,
  192. cdev)) {
  193. dev_err(data->dev,
  194. "error unbinding cdev inst=%d\n", i);
  195. ret = -EINVAL;
  196. goto end;
  197. }
  198. i++;
  199. }
  200. th_zone->bind = false;
  201. end:
  202. of_node_put(child);
  203. of_node_put(np);
  204. return ret;
  205. }
  206. /* Get temperature callback functions for thermal zone */
  207. static int exynos_get_temp(struct thermal_zone_device *thermal,
  208. unsigned long *temp)
  209. {
  210. struct exynos_thermal_zone *th_zone = thermal->devdata;
  211. void *data;
  212. if (!th_zone->sensor_conf) {
  213. dev_err(&thermal->device,
  214. "Temperature sensor not initialised\n");
  215. return -EINVAL;
  216. }
  217. data = th_zone->sensor_conf->driver_data;
  218. *temp = th_zone->sensor_conf->read_temperature(data);
  219. /* convert the temperature into millicelsius */
  220. *temp = *temp * MCELSIUS;
  221. return 0;
  222. }
  223. /* Get temperature callback functions for thermal zone */
  224. static int exynos_set_emul_temp(struct thermal_zone_device *thermal,
  225. unsigned long temp)
  226. {
  227. void *data;
  228. int ret = -EINVAL;
  229. struct exynos_thermal_zone *th_zone = thermal->devdata;
  230. if (!th_zone->sensor_conf) {
  231. dev_err(&thermal->device,
  232. "Temperature sensor not initialised\n");
  233. return -EINVAL;
  234. }
  235. data = th_zone->sensor_conf->driver_data;
  236. if (th_zone->sensor_conf->write_emul_temp)
  237. ret = th_zone->sensor_conf->write_emul_temp(data, temp);
  238. return ret;
  239. }
  240. /* Get the temperature trend */
  241. static int exynos_get_trend(struct thermal_zone_device *thermal,
  242. int trip, enum thermal_trend *trend)
  243. {
  244. int ret;
  245. unsigned long trip_temp;
  246. ret = exynos_get_trip_temp(thermal, trip, &trip_temp);
  247. if (ret < 0)
  248. return ret;
  249. if (thermal->temperature >= trip_temp)
  250. *trend = THERMAL_TREND_RAISE_FULL;
  251. else
  252. *trend = THERMAL_TREND_DROP_FULL;
  253. return 0;
  254. }
  255. /* Operation callback functions for thermal zone */
  256. static struct thermal_zone_device_ops exynos_dev_ops = {
  257. .bind = exynos_bind,
  258. .unbind = exynos_unbind,
  259. .get_temp = exynos_get_temp,
  260. .set_emul_temp = exynos_set_emul_temp,
  261. .get_trend = exynos_get_trend,
  262. .get_mode = exynos_get_mode,
  263. .set_mode = exynos_set_mode,
  264. .get_trip_type = exynos_get_trip_type,
  265. .get_trip_temp = exynos_get_trip_temp,
  266. .get_crit_temp = exynos_get_crit_temp,
  267. };
  268. /*
  269. * This function may be called from interrupt based temperature sensor
  270. * when threshold is changed.
  271. */
  272. void exynos_report_trigger(struct thermal_sensor_conf *conf)
  273. {
  274. unsigned int i;
  275. char data[10];
  276. char *envp[] = { data, NULL };
  277. struct exynos_thermal_zone *th_zone;
  278. if (!conf || !conf->pzone_data) {
  279. pr_err("Invalid temperature sensor configuration data\n");
  280. return;
  281. }
  282. th_zone = conf->pzone_data;
  283. if (th_zone->bind == false) {
  284. for (i = 0; i < th_zone->cool_dev_size; i++) {
  285. if (!th_zone->cool_dev[i])
  286. continue;
  287. exynos_bind(th_zone->therm_dev,
  288. th_zone->cool_dev[i]);
  289. }
  290. }
  291. thermal_zone_device_update(th_zone->therm_dev);
  292. mutex_lock(&th_zone->therm_dev->lock);
  293. /* Find the level for which trip happened */
  294. for (i = 0; i < th_zone->sensor_conf->trip_data.trip_count; i++) {
  295. if (th_zone->therm_dev->last_temperature <
  296. th_zone->sensor_conf->trip_data.trip_val[i] * MCELSIUS)
  297. break;
  298. }
  299. if (th_zone->mode == THERMAL_DEVICE_ENABLED &&
  300. !th_zone->sensor_conf->trip_data.trigger_falling) {
  301. if (i > 0)
  302. th_zone->therm_dev->polling_delay = ACTIVE_INTERVAL;
  303. else
  304. th_zone->therm_dev->polling_delay = IDLE_INTERVAL;
  305. }
  306. snprintf(data, sizeof(data), "%u", i);
  307. kobject_uevent_env(&th_zone->therm_dev->device.kobj, KOBJ_CHANGE, envp);
  308. mutex_unlock(&th_zone->therm_dev->lock);
  309. }
  310. /* Register with the in-kernel thermal management */
  311. int exynos_register_thermal(struct thermal_sensor_conf *sensor_conf)
  312. {
  313. int ret;
  314. struct exynos_thermal_zone *th_zone;
  315. if (!sensor_conf || !sensor_conf->read_temperature) {
  316. pr_err("Temperature sensor not initialised\n");
  317. return -EINVAL;
  318. }
  319. th_zone = devm_kzalloc(sensor_conf->dev,
  320. sizeof(struct exynos_thermal_zone), GFP_KERNEL);
  321. if (!th_zone)
  322. return -ENOMEM;
  323. th_zone->sensor_conf = sensor_conf;
  324. /*
  325. * TODO: 1) Handle multiple cooling devices in a thermal zone
  326. * 2) Add a flag/name in cooling info to map to specific
  327. * sensor
  328. */
  329. if (sensor_conf->cooling_data.freq_clip_count > 0) {
  330. th_zone->cool_dev[th_zone->cool_dev_size] =
  331. cpufreq_cooling_register(cpu_present_mask);
  332. if (IS_ERR(th_zone->cool_dev[th_zone->cool_dev_size])) {
  333. ret = PTR_ERR(th_zone->cool_dev[th_zone->cool_dev_size]);
  334. if (ret != -EPROBE_DEFER)
  335. dev_err(sensor_conf->dev,
  336. "Failed to register cpufreq cooling device: %d\n",
  337. ret);
  338. goto err_unregister;
  339. }
  340. th_zone->cool_dev_size++;
  341. }
  342. th_zone->therm_dev = thermal_zone_device_register(
  343. sensor_conf->name, sensor_conf->trip_data.trip_count,
  344. 0, th_zone, &exynos_dev_ops, NULL, 0,
  345. sensor_conf->trip_data.trigger_falling ? 0 :
  346. IDLE_INTERVAL);
  347. if (IS_ERR(th_zone->therm_dev)) {
  348. dev_err(sensor_conf->dev,
  349. "Failed to register thermal zone device\n");
  350. ret = PTR_ERR(th_zone->therm_dev);
  351. goto err_unregister;
  352. }
  353. th_zone->mode = THERMAL_DEVICE_ENABLED;
  354. sensor_conf->pzone_data = th_zone;
  355. dev_info(sensor_conf->dev,
  356. "Exynos: Thermal zone(%s) registered\n", sensor_conf->name);
  357. return 0;
  358. err_unregister:
  359. exynos_unregister_thermal(sensor_conf);
  360. return ret;
  361. }
  362. /* Un-Register with the in-kernel thermal management */
  363. void exynos_unregister_thermal(struct thermal_sensor_conf *sensor_conf)
  364. {
  365. int i;
  366. struct exynos_thermal_zone *th_zone;
  367. if (!sensor_conf || !sensor_conf->pzone_data) {
  368. pr_err("Invalid temperature sensor configuration data\n");
  369. return;
  370. }
  371. th_zone = sensor_conf->pzone_data;
  372. thermal_zone_device_unregister(th_zone->therm_dev);
  373. for (i = 0; i < th_zone->cool_dev_size; ++i)
  374. cpufreq_cooling_unregister(th_zone->cool_dev[i]);
  375. dev_info(sensor_conf->dev,
  376. "Exynos: Kernel Thermal management unregistered\n");
  377. }