ti-thermal-common.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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_zone_device *pcb_tz;
  39. struct thermal_cooling_device *cool_dev;
  40. struct ti_bandgap *bgp;
  41. enum thermal_device_mode mode;
  42. struct work_struct thermal_wq;
  43. int sensor_id;
  44. };
  45. static void ti_thermal_work(struct work_struct *work)
  46. {
  47. struct ti_thermal_data *data = container_of(work,
  48. struct ti_thermal_data, thermal_wq);
  49. thermal_zone_device_update(data->ti_thermal);
  50. dev_dbg(&data->ti_thermal->device, "updated thermal zone %s\n",
  51. data->ti_thermal->type);
  52. }
  53. /**
  54. * ti_thermal_hotspot_temperature - returns sensor extrapolated temperature
  55. * @t: omap sensor temperature
  56. * @s: omap sensor slope value
  57. * @c: omap sensor const value
  58. */
  59. static inline int ti_thermal_hotspot_temperature(int t, int s, int c)
  60. {
  61. int delta = t * s / 1000 + c;
  62. if (delta < 0)
  63. delta = 0;
  64. return t + delta;
  65. }
  66. /* thermal zone ops */
  67. /* Get temperature callback function for thermal zone*/
  68. static inline int ti_thermal_get_temp(struct thermal_zone_device *thermal,
  69. unsigned long *temp)
  70. {
  71. struct thermal_zone_device *pcb_tz = NULL;
  72. struct ti_thermal_data *data = thermal->devdata;
  73. struct ti_bandgap *bgp;
  74. const struct ti_temp_sensor *s;
  75. int ret, tmp, slope, constant;
  76. unsigned long pcb_temp;
  77. if (!data)
  78. return 0;
  79. bgp = data->bgp;
  80. s = &bgp->conf->sensors[data->sensor_id];
  81. ret = ti_bandgap_read_temperature(bgp, data->sensor_id, &tmp);
  82. if (ret)
  83. return ret;
  84. /* Default constants */
  85. slope = s->slope;
  86. constant = s->constant;
  87. pcb_tz = data->pcb_tz;
  88. /* In case pcb zone is available, use the extrapolation rule with it */
  89. if (!IS_ERR(pcb_tz)) {
  90. ret = thermal_zone_get_temp(pcb_tz, &pcb_temp);
  91. if (!ret) {
  92. tmp -= pcb_temp; /* got a valid PCB temp */
  93. slope = s->slope_pcb;
  94. constant = s->constant_pcb;
  95. } else {
  96. dev_err(bgp->dev,
  97. "Failed to read PCB state. Using defaults\n");
  98. ret = 0;
  99. }
  100. }
  101. *temp = ti_thermal_hotspot_temperature(tmp, slope, constant);
  102. return ret;
  103. }
  104. /* Bind callback functions for thermal zone */
  105. static int ti_thermal_bind(struct thermal_zone_device *thermal,
  106. struct thermal_cooling_device *cdev)
  107. {
  108. struct ti_thermal_data *data = thermal->devdata;
  109. int id;
  110. if (!data || IS_ERR(data))
  111. return -ENODEV;
  112. /* check if this is the cooling device we registered */
  113. if (data->cool_dev != cdev)
  114. return 0;
  115. id = data->sensor_id;
  116. /* Simple thing, two trips, one passive another critical */
  117. return thermal_zone_bind_cooling_device(thermal, 0, cdev,
  118. /* bind with min and max states defined by cpu_cooling */
  119. THERMAL_NO_LIMIT,
  120. THERMAL_NO_LIMIT);
  121. }
  122. /* Unbind callback functions for thermal zone */
  123. static int ti_thermal_unbind(struct thermal_zone_device *thermal,
  124. struct thermal_cooling_device *cdev)
  125. {
  126. struct ti_thermal_data *data = thermal->devdata;
  127. if (!data || IS_ERR(data))
  128. return -ENODEV;
  129. /* check if this is the cooling device we registered */
  130. if (data->cool_dev != cdev)
  131. return 0;
  132. /* Simple thing, two trips, one passive another critical */
  133. return thermal_zone_unbind_cooling_device(thermal, 0, cdev);
  134. }
  135. /* Get mode callback functions for thermal zone */
  136. static int ti_thermal_get_mode(struct thermal_zone_device *thermal,
  137. enum thermal_device_mode *mode)
  138. {
  139. struct ti_thermal_data *data = thermal->devdata;
  140. if (data)
  141. *mode = data->mode;
  142. return 0;
  143. }
  144. /* Set mode callback functions for thermal zone */
  145. static int ti_thermal_set_mode(struct thermal_zone_device *thermal,
  146. enum thermal_device_mode mode)
  147. {
  148. struct ti_thermal_data *data = thermal->devdata;
  149. struct ti_bandgap *bgp;
  150. bgp = data->bgp;
  151. if (!data->ti_thermal) {
  152. dev_notice(&thermal->device, "thermal zone not registered\n");
  153. return 0;
  154. }
  155. mutex_lock(&data->ti_thermal->lock);
  156. if (mode == THERMAL_DEVICE_ENABLED)
  157. data->ti_thermal->polling_delay = FAST_TEMP_MONITORING_RATE;
  158. else
  159. data->ti_thermal->polling_delay = 0;
  160. mutex_unlock(&data->ti_thermal->lock);
  161. data->mode = mode;
  162. ti_bandgap_write_update_interval(bgp, data->sensor_id,
  163. data->ti_thermal->polling_delay);
  164. thermal_zone_device_update(data->ti_thermal);
  165. dev_dbg(&thermal->device, "thermal polling set for duration=%d msec\n",
  166. data->ti_thermal->polling_delay);
  167. return 0;
  168. }
  169. /* Get trip type callback functions for thermal zone */
  170. static int ti_thermal_get_trip_type(struct thermal_zone_device *thermal,
  171. int trip, enum thermal_trip_type *type)
  172. {
  173. if (!ti_thermal_is_valid_trip(trip))
  174. return -EINVAL;
  175. if (trip + 1 == OMAP_TRIP_NUMBER)
  176. *type = THERMAL_TRIP_CRITICAL;
  177. else
  178. *type = THERMAL_TRIP_PASSIVE;
  179. return 0;
  180. }
  181. /* Get trip temperature callback functions for thermal zone */
  182. static int ti_thermal_get_trip_temp(struct thermal_zone_device *thermal,
  183. int trip, unsigned long *temp)
  184. {
  185. if (!ti_thermal_is_valid_trip(trip))
  186. return -EINVAL;
  187. *temp = ti_thermal_get_trip_value(trip);
  188. return 0;
  189. }
  190. /* Get the temperature trend callback functions for thermal zone */
  191. static int ti_thermal_get_trend(struct thermal_zone_device *thermal,
  192. int trip, enum thermal_trend *trend)
  193. {
  194. struct ti_thermal_data *data = thermal->devdata;
  195. struct ti_bandgap *bgp;
  196. int id, tr, ret = 0;
  197. bgp = data->bgp;
  198. id = data->sensor_id;
  199. ret = ti_bandgap_get_trend(bgp, id, &tr);
  200. if (ret)
  201. return ret;
  202. if (tr > 0)
  203. *trend = THERMAL_TREND_RAISING;
  204. else if (tr < 0)
  205. *trend = THERMAL_TREND_DROPPING;
  206. else
  207. *trend = THERMAL_TREND_STABLE;
  208. return 0;
  209. }
  210. /* Get critical temperature callback functions for thermal zone */
  211. static int ti_thermal_get_crit_temp(struct thermal_zone_device *thermal,
  212. unsigned long *temp)
  213. {
  214. /* shutdown zone */
  215. return ti_thermal_get_trip_temp(thermal, OMAP_TRIP_NUMBER - 1, temp);
  216. }
  217. static struct thermal_zone_device_ops ti_thermal_ops = {
  218. .get_temp = ti_thermal_get_temp,
  219. .get_trend = ti_thermal_get_trend,
  220. .bind = ti_thermal_bind,
  221. .unbind = ti_thermal_unbind,
  222. .get_mode = ti_thermal_get_mode,
  223. .set_mode = ti_thermal_set_mode,
  224. .get_trip_type = ti_thermal_get_trip_type,
  225. .get_trip_temp = ti_thermal_get_trip_temp,
  226. .get_crit_temp = ti_thermal_get_crit_temp,
  227. };
  228. static struct ti_thermal_data
  229. *ti_thermal_build_data(struct ti_bandgap *bgp, int id)
  230. {
  231. struct ti_thermal_data *data;
  232. data = devm_kzalloc(bgp->dev, sizeof(*data), GFP_KERNEL);
  233. if (!data) {
  234. dev_err(bgp->dev, "kzalloc fail\n");
  235. return NULL;
  236. }
  237. data->sensor_id = id;
  238. data->bgp = bgp;
  239. data->mode = THERMAL_DEVICE_ENABLED;
  240. /* pcb_tz will be either valid or PTR_ERR() */
  241. data->pcb_tz = thermal_zone_get_zone_by_name("pcb");
  242. INIT_WORK(&data->thermal_wq, ti_thermal_work);
  243. return data;
  244. }
  245. int ti_thermal_expose_sensor(struct ti_bandgap *bgp, int id,
  246. char *domain)
  247. {
  248. struct ti_thermal_data *data;
  249. data = ti_bandgap_get_sensor_data(bgp, id);
  250. if (!data || IS_ERR(data))
  251. data = ti_thermal_build_data(bgp, id);
  252. if (!data)
  253. return -EINVAL;
  254. /* Create thermal zone */
  255. data->ti_thermal = thermal_zone_device_register(domain,
  256. OMAP_TRIP_NUMBER, 0, data, &ti_thermal_ops,
  257. NULL, FAST_TEMP_MONITORING_RATE,
  258. FAST_TEMP_MONITORING_RATE);
  259. if (IS_ERR(data->ti_thermal)) {
  260. dev_err(bgp->dev, "thermal zone device is NULL\n");
  261. return PTR_ERR(data->ti_thermal);
  262. }
  263. data->ti_thermal->polling_delay = FAST_TEMP_MONITORING_RATE;
  264. ti_bandgap_set_sensor_data(bgp, id, data);
  265. ti_bandgap_write_update_interval(bgp, data->sensor_id,
  266. data->ti_thermal->polling_delay);
  267. return 0;
  268. }
  269. int ti_thermal_remove_sensor(struct ti_bandgap *bgp, int id)
  270. {
  271. struct ti_thermal_data *data;
  272. data = ti_bandgap_get_sensor_data(bgp, id);
  273. thermal_zone_device_unregister(data->ti_thermal);
  274. return 0;
  275. }
  276. int ti_thermal_report_sensor_temperature(struct ti_bandgap *bgp, int id)
  277. {
  278. struct ti_thermal_data *data;
  279. data = ti_bandgap_get_sensor_data(bgp, id);
  280. schedule_work(&data->thermal_wq);
  281. return 0;
  282. }
  283. int ti_thermal_register_cpu_cooling(struct ti_bandgap *bgp, int id)
  284. {
  285. struct ti_thermal_data *data;
  286. data = ti_bandgap_get_sensor_data(bgp, id);
  287. if (!data || IS_ERR(data))
  288. data = ti_thermal_build_data(bgp, id);
  289. if (!data)
  290. return -EINVAL;
  291. if (!cpufreq_get_current_driver()) {
  292. dev_dbg(bgp->dev, "no cpufreq driver yet\n");
  293. return -EPROBE_DEFER;
  294. }
  295. /* Register cooling device */
  296. data->cool_dev = cpufreq_cooling_register(cpu_present_mask);
  297. if (IS_ERR(data->cool_dev)) {
  298. dev_err(bgp->dev,
  299. "Failed to register cpufreq cooling device\n");
  300. return PTR_ERR(data->cool_dev);
  301. }
  302. ti_bandgap_set_sensor_data(bgp, id, data);
  303. return 0;
  304. }
  305. int ti_thermal_unregister_cpu_cooling(struct ti_bandgap *bgp, int id)
  306. {
  307. struct ti_thermal_data *data;
  308. data = ti_bandgap_get_sensor_data(bgp, id);
  309. cpufreq_cooling_unregister(data->cool_dev);
  310. return 0;
  311. }