ti-thermal-common.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*
  2. * OMAP thermal driver interface
  3. *
  4. * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com/
  5. * Contact:
  6. * Eduardo Valentin <eduardo.valentin@ti.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * version 2 as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * 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., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA
  21. *
  22. */
  23. #include <linux/device.h>
  24. #include <linux/err.h>
  25. #include <linux/mutex.h>
  26. #include <linux/gfp.h>
  27. #include <linux/kernel.h>
  28. #include <linux/workqueue.h>
  29. #include <linux/thermal.h>
  30. #include <linux/cpufreq.h>
  31. #include <linux/cpumask.h>
  32. #include <linux/cpu_cooling.h>
  33. #include "ti-thermal.h"
  34. #include "ti-bandgap.h"
  35. /* common data structures */
  36. struct ti_thermal_data {
  37. struct thermal_zone_device *ti_thermal;
  38. struct thermal_cooling_device *cool_dev;
  39. struct ti_bandgap *bgp;
  40. enum thermal_device_mode mode;
  41. struct work_struct thermal_wq;
  42. int sensor_id;
  43. };
  44. static void ti_thermal_work(struct work_struct *work)
  45. {
  46. struct ti_thermal_data *data = container_of(work,
  47. struct ti_thermal_data, thermal_wq);
  48. thermal_zone_device_update(data->ti_thermal);
  49. dev_dbg(&data->ti_thermal->device, "updated thermal zone %s\n",
  50. data->ti_thermal->type);
  51. }
  52. /**
  53. * ti_thermal_hotspot_temperature - returns sensor extrapolated temperature
  54. * @t: omap sensor temperature
  55. * @s: omap sensor slope value
  56. * @c: omap sensor const value
  57. */
  58. static inline int ti_thermal_hotspot_temperature(int t, int s, int c)
  59. {
  60. int delta = t * s / 1000 + c;
  61. if (delta < 0)
  62. delta = 0;
  63. return t + delta;
  64. }
  65. /* thermal zone ops */
  66. /* Get temperature callback function for thermal zone*/
  67. static inline int ti_thermal_get_temp(struct thermal_zone_device *thermal,
  68. unsigned long *temp)
  69. {
  70. struct ti_thermal_data *data = thermal->devdata;
  71. struct ti_bandgap *bgp;
  72. const struct ti_temp_sensor *s;
  73. int ret, tmp, pcb_temp, slope, constant;
  74. if (!data)
  75. return 0;
  76. bgp = data->bgp;
  77. s = &bgp->conf->sensors[data->sensor_id];
  78. ret = ti_bandgap_read_temperature(bgp, data->sensor_id, &tmp);
  79. if (ret)
  80. return ret;
  81. pcb_temp = 0;
  82. /* TODO: Introduce pcb temperature lookup */
  83. /* In case pcb zone is available, use the extrapolation rule with it */
  84. if (pcb_temp) {
  85. tmp -= pcb_temp;
  86. slope = s->slope_pcb;
  87. constant = s->constant_pcb;
  88. } else {
  89. slope = s->slope;
  90. constant = s->constant;
  91. }
  92. *temp = ti_thermal_hotspot_temperature(tmp, slope, constant);
  93. return ret;
  94. }
  95. /* Bind callback functions for thermal zone */
  96. static int ti_thermal_bind(struct thermal_zone_device *thermal,
  97. struct thermal_cooling_device *cdev)
  98. {
  99. struct ti_thermal_data *data = thermal->devdata;
  100. int id;
  101. if (IS_ERR_OR_NULL(data))
  102. return -ENODEV;
  103. /* check if this is the cooling device we registered */
  104. if (data->cool_dev != cdev)
  105. return 0;
  106. id = data->sensor_id;
  107. /* Simple thing, two trips, one passive another critical */
  108. return thermal_zone_bind_cooling_device(thermal, 0, cdev,
  109. /* bind with min and max states defined by cpu_cooling */
  110. THERMAL_NO_LIMIT,
  111. THERMAL_NO_LIMIT);
  112. }
  113. /* Unbind callback functions for thermal zone */
  114. static int ti_thermal_unbind(struct thermal_zone_device *thermal,
  115. struct thermal_cooling_device *cdev)
  116. {
  117. struct ti_thermal_data *data = thermal->devdata;
  118. if (IS_ERR_OR_NULL(data))
  119. return -ENODEV;
  120. /* check if this is the cooling device we registered */
  121. if (data->cool_dev != cdev)
  122. return 0;
  123. /* Simple thing, two trips, one passive another critical */
  124. return thermal_zone_unbind_cooling_device(thermal, 0, cdev);
  125. }
  126. /* Get mode callback functions for thermal zone */
  127. static int ti_thermal_get_mode(struct thermal_zone_device *thermal,
  128. enum thermal_device_mode *mode)
  129. {
  130. struct ti_thermal_data *data = thermal->devdata;
  131. if (data)
  132. *mode = data->mode;
  133. return 0;
  134. }
  135. /* Set mode callback functions for thermal zone */
  136. static int ti_thermal_set_mode(struct thermal_zone_device *thermal,
  137. enum thermal_device_mode mode)
  138. {
  139. struct ti_thermal_data *data = thermal->devdata;
  140. if (!data->ti_thermal) {
  141. dev_notice(&thermal->device, "thermal zone not registered\n");
  142. return 0;
  143. }
  144. mutex_lock(&data->ti_thermal->lock);
  145. if (mode == THERMAL_DEVICE_ENABLED)
  146. data->ti_thermal->polling_delay = FAST_TEMP_MONITORING_RATE;
  147. else
  148. data->ti_thermal->polling_delay = 0;
  149. mutex_unlock(&data->ti_thermal->lock);
  150. data->mode = mode;
  151. thermal_zone_device_update(data->ti_thermal);
  152. dev_dbg(&thermal->device, "thermal polling set for duration=%d msec\n",
  153. data->ti_thermal->polling_delay);
  154. return 0;
  155. }
  156. /* Get trip type callback functions for thermal zone */
  157. static int ti_thermal_get_trip_type(struct thermal_zone_device *thermal,
  158. int trip, enum thermal_trip_type *type)
  159. {
  160. if (!ti_thermal_is_valid_trip(trip))
  161. return -EINVAL;
  162. if (trip + 1 == OMAP_TRIP_NUMBER)
  163. *type = THERMAL_TRIP_CRITICAL;
  164. else
  165. *type = THERMAL_TRIP_PASSIVE;
  166. return 0;
  167. }
  168. /* Get trip temperature callback functions for thermal zone */
  169. static int ti_thermal_get_trip_temp(struct thermal_zone_device *thermal,
  170. int trip, unsigned long *temp)
  171. {
  172. if (!ti_thermal_is_valid_trip(trip))
  173. return -EINVAL;
  174. *temp = ti_thermal_get_trip_value(trip);
  175. return 0;
  176. }
  177. /* Get the temperature trend callback functions for thermal zone */
  178. static int ti_thermal_get_trend(struct thermal_zone_device *thermal,
  179. int trip, enum thermal_trend *trend)
  180. {
  181. struct ti_thermal_data *data = thermal->devdata;
  182. struct ti_bandgap *bgp;
  183. int id, tr, ret = 0;
  184. bgp = data->bgp;
  185. id = data->sensor_id;
  186. ret = ti_bandgap_get_trend(bgp, id, &tr);
  187. if (ret)
  188. return ret;
  189. if (tr > 0)
  190. *trend = THERMAL_TREND_RAISING;
  191. else if (tr < 0)
  192. *trend = THERMAL_TREND_DROPPING;
  193. else
  194. *trend = THERMAL_TREND_STABLE;
  195. return 0;
  196. }
  197. /* Get critical temperature callback functions for thermal zone */
  198. static int ti_thermal_get_crit_temp(struct thermal_zone_device *thermal,
  199. unsigned long *temp)
  200. {
  201. /* shutdown zone */
  202. return ti_thermal_get_trip_temp(thermal, OMAP_TRIP_NUMBER - 1, temp);
  203. }
  204. static struct thermal_zone_device_ops ti_thermal_ops = {
  205. .get_temp = ti_thermal_get_temp,
  206. .get_trend = ti_thermal_get_trend,
  207. .bind = ti_thermal_bind,
  208. .unbind = ti_thermal_unbind,
  209. .get_mode = ti_thermal_get_mode,
  210. .set_mode = ti_thermal_set_mode,
  211. .get_trip_type = ti_thermal_get_trip_type,
  212. .get_trip_temp = ti_thermal_get_trip_temp,
  213. .get_crit_temp = ti_thermal_get_crit_temp,
  214. };
  215. static struct ti_thermal_data
  216. *ti_thermal_build_data(struct ti_bandgap *bgp, int id)
  217. {
  218. struct ti_thermal_data *data;
  219. data = devm_kzalloc(bgp->dev, sizeof(*data), GFP_KERNEL);
  220. if (!data) {
  221. dev_err(bgp->dev, "kzalloc fail\n");
  222. return NULL;
  223. }
  224. data->sensor_id = id;
  225. data->bgp = bgp;
  226. data->mode = THERMAL_DEVICE_ENABLED;
  227. INIT_WORK(&data->thermal_wq, ti_thermal_work);
  228. return data;
  229. }
  230. int ti_thermal_expose_sensor(struct ti_bandgap *bgp, int id,
  231. char *domain)
  232. {
  233. struct ti_thermal_data *data;
  234. data = ti_bandgap_get_sensor_data(bgp, id);
  235. if (IS_ERR_OR_NULL(data))
  236. data = ti_thermal_build_data(bgp, id);
  237. if (!data)
  238. return -EINVAL;
  239. /* Create thermal zone */
  240. data->ti_thermal = thermal_zone_device_register(domain,
  241. OMAP_TRIP_NUMBER, 0, data, &ti_thermal_ops,
  242. NULL, FAST_TEMP_MONITORING_RATE,
  243. FAST_TEMP_MONITORING_RATE);
  244. if (IS_ERR_OR_NULL(data->ti_thermal)) {
  245. dev_err(bgp->dev, "thermal zone device is NULL\n");
  246. return PTR_ERR(data->ti_thermal);
  247. }
  248. data->ti_thermal->polling_delay = FAST_TEMP_MONITORING_RATE;
  249. ti_bandgap_set_sensor_data(bgp, id, data);
  250. return 0;
  251. }
  252. int ti_thermal_remove_sensor(struct ti_bandgap *bgp, int id)
  253. {
  254. struct ti_thermal_data *data;
  255. data = ti_bandgap_get_sensor_data(bgp, id);
  256. thermal_zone_device_unregister(data->ti_thermal);
  257. return 0;
  258. }
  259. int ti_thermal_report_sensor_temperature(struct ti_bandgap *bgp, int id)
  260. {
  261. struct ti_thermal_data *data;
  262. data = ti_bandgap_get_sensor_data(bgp, id);
  263. schedule_work(&data->thermal_wq);
  264. return 0;
  265. }
  266. int ti_thermal_register_cpu_cooling(struct ti_bandgap *bgp, int id)
  267. {
  268. struct ti_thermal_data *data;
  269. data = ti_bandgap_get_sensor_data(bgp, id);
  270. if (IS_ERR_OR_NULL(data))
  271. data = ti_thermal_build_data(bgp, id);
  272. if (!data)
  273. return -EINVAL;
  274. if (!cpufreq_get_current_driver()) {
  275. dev_dbg(bgp->dev, "no cpufreq driver yet\n");
  276. return -EPROBE_DEFER;
  277. }
  278. /* Register cooling device */
  279. data->cool_dev = cpufreq_cooling_register(cpu_present_mask);
  280. if (IS_ERR_OR_NULL(data->cool_dev)) {
  281. dev_err(bgp->dev,
  282. "Failed to register cpufreq cooling device\n");
  283. return PTR_ERR(data->cool_dev);
  284. }
  285. ti_bandgap_set_sensor_data(bgp, id, data);
  286. return 0;
  287. }
  288. int ti_thermal_unregister_cpu_cooling(struct ti_bandgap *bgp, int id)
  289. {
  290. struct ti_thermal_data *data;
  291. data = ti_bandgap_get_sensor_data(bgp, id);
  292. cpufreq_cooling_unregister(data->cool_dev);
  293. return 0;
  294. }