ti-thermal-common.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 <linux/of.h>
  34. #include "ti-thermal.h"
  35. #include "ti-bandgap.h"
  36. /* common data structures */
  37. struct ti_thermal_data {
  38. struct cpufreq_policy *policy;
  39. struct thermal_zone_device *ti_thermal;
  40. struct thermal_zone_device *pcb_tz;
  41. struct thermal_cooling_device *cool_dev;
  42. struct ti_bandgap *bgp;
  43. enum thermal_device_mode mode;
  44. struct work_struct thermal_wq;
  45. int sensor_id;
  46. bool our_zone;
  47. };
  48. static void ti_thermal_work(struct work_struct *work)
  49. {
  50. struct ti_thermal_data *data = container_of(work,
  51. struct ti_thermal_data, thermal_wq);
  52. thermal_zone_device_update(data->ti_thermal, THERMAL_EVENT_UNSPECIFIED);
  53. dev_dbg(&data->ti_thermal->device, "updated thermal zone %s\n",
  54. data->ti_thermal->type);
  55. }
  56. /**
  57. * ti_thermal_hotspot_temperature - returns sensor extrapolated temperature
  58. * @t: omap sensor temperature
  59. * @s: omap sensor slope value
  60. * @c: omap sensor const value
  61. */
  62. static inline int ti_thermal_hotspot_temperature(int t, int s, int c)
  63. {
  64. int delta = t * s / 1000 + c;
  65. if (delta < 0)
  66. delta = 0;
  67. return t + delta;
  68. }
  69. /* thermal zone ops */
  70. /* Get temperature callback function for thermal zone */
  71. static inline int __ti_thermal_get_temp(void *devdata, int *temp)
  72. {
  73. struct thermal_zone_device *pcb_tz = NULL;
  74. struct ti_thermal_data *data = devdata;
  75. struct ti_bandgap *bgp;
  76. const struct ti_temp_sensor *s;
  77. int ret, tmp, slope, constant;
  78. int pcb_temp;
  79. if (!data)
  80. return 0;
  81. bgp = data->bgp;
  82. s = &bgp->conf->sensors[data->sensor_id];
  83. ret = ti_bandgap_read_temperature(bgp, data->sensor_id, &tmp);
  84. if (ret)
  85. return ret;
  86. /* Default constants */
  87. slope = thermal_zone_get_slope(data->ti_thermal);
  88. constant = thermal_zone_get_offset(data->ti_thermal);
  89. pcb_tz = data->pcb_tz;
  90. /* In case pcb zone is available, use the extrapolation rule with it */
  91. if (!IS_ERR(pcb_tz)) {
  92. ret = thermal_zone_get_temp(pcb_tz, &pcb_temp);
  93. if (!ret) {
  94. tmp -= pcb_temp; /* got a valid PCB temp */
  95. slope = s->slope_pcb;
  96. constant = s->constant_pcb;
  97. } else {
  98. dev_err(bgp->dev,
  99. "Failed to read PCB state. Using defaults\n");
  100. ret = 0;
  101. }
  102. }
  103. *temp = ti_thermal_hotspot_temperature(tmp, slope, constant);
  104. return ret;
  105. }
  106. static inline int ti_thermal_get_temp(struct thermal_zone_device *thermal,
  107. int *temp)
  108. {
  109. struct ti_thermal_data *data = thermal->devdata;
  110. return __ti_thermal_get_temp(data, temp);
  111. }
  112. static int __ti_thermal_get_trend(void *p, int trip, enum thermal_trend *trend)
  113. {
  114. struct ti_thermal_data *data = p;
  115. struct ti_bandgap *bgp;
  116. int id, tr, ret = 0;
  117. bgp = data->bgp;
  118. id = data->sensor_id;
  119. ret = ti_bandgap_get_trend(bgp, id, &tr);
  120. if (ret)
  121. return ret;
  122. if (tr > 0)
  123. *trend = THERMAL_TREND_RAISING;
  124. else if (tr < 0)
  125. *trend = THERMAL_TREND_DROPPING;
  126. else
  127. *trend = THERMAL_TREND_STABLE;
  128. return 0;
  129. }
  130. static const struct thermal_zone_of_device_ops ti_of_thermal_ops = {
  131. .get_temp = __ti_thermal_get_temp,
  132. .get_trend = __ti_thermal_get_trend,
  133. };
  134. static struct ti_thermal_data
  135. *ti_thermal_build_data(struct ti_bandgap *bgp, int id)
  136. {
  137. struct ti_thermal_data *data;
  138. data = devm_kzalloc(bgp->dev, sizeof(*data), GFP_KERNEL);
  139. if (!data) {
  140. dev_err(bgp->dev, "kzalloc fail\n");
  141. return NULL;
  142. }
  143. data->sensor_id = id;
  144. data->bgp = bgp;
  145. data->mode = THERMAL_DEVICE_ENABLED;
  146. /* pcb_tz will be either valid or PTR_ERR() */
  147. data->pcb_tz = thermal_zone_get_zone_by_name("pcb");
  148. INIT_WORK(&data->thermal_wq, ti_thermal_work);
  149. return data;
  150. }
  151. int ti_thermal_expose_sensor(struct ti_bandgap *bgp, int id,
  152. char *domain)
  153. {
  154. struct ti_thermal_data *data;
  155. data = ti_bandgap_get_sensor_data(bgp, id);
  156. if (IS_ERR_OR_NULL(data))
  157. data = ti_thermal_build_data(bgp, id);
  158. if (!data)
  159. return -EINVAL;
  160. /* in case this is specified by DT */
  161. data->ti_thermal = devm_thermal_zone_of_sensor_register(bgp->dev, id,
  162. data, &ti_of_thermal_ops);
  163. if (IS_ERR(data->ti_thermal)) {
  164. dev_err(bgp->dev, "thermal zone device is NULL\n");
  165. return PTR_ERR(data->ti_thermal);
  166. }
  167. ti_bandgap_set_sensor_data(bgp, id, data);
  168. ti_bandgap_write_update_interval(bgp, data->sensor_id,
  169. data->ti_thermal->polling_delay);
  170. return 0;
  171. }
  172. int ti_thermal_remove_sensor(struct ti_bandgap *bgp, int id)
  173. {
  174. struct ti_thermal_data *data;
  175. data = ti_bandgap_get_sensor_data(bgp, id);
  176. if (!IS_ERR_OR_NULL(data) && data->ti_thermal) {
  177. if (data->our_zone)
  178. thermal_zone_device_unregister(data->ti_thermal);
  179. }
  180. return 0;
  181. }
  182. int ti_thermal_report_sensor_temperature(struct ti_bandgap *bgp, int id)
  183. {
  184. struct ti_thermal_data *data;
  185. data = ti_bandgap_get_sensor_data(bgp, id);
  186. schedule_work(&data->thermal_wq);
  187. return 0;
  188. }
  189. int ti_thermal_register_cpu_cooling(struct ti_bandgap *bgp, int id)
  190. {
  191. struct ti_thermal_data *data;
  192. struct device_node *np = bgp->dev->of_node;
  193. /*
  194. * We are assuming here that if one deploys the zone
  195. * using DT, then it must be aware that the cooling device
  196. * loading has to happen via cpufreq driver.
  197. */
  198. if (of_find_property(np, "#thermal-sensor-cells", NULL))
  199. return 0;
  200. data = ti_bandgap_get_sensor_data(bgp, id);
  201. if (!data || IS_ERR(data))
  202. data = ti_thermal_build_data(bgp, id);
  203. if (!data)
  204. return -EINVAL;
  205. data->policy = cpufreq_cpu_get(0);
  206. if (!data->policy) {
  207. pr_debug("%s: CPUFreq policy not found\n", __func__);
  208. return -EPROBE_DEFER;
  209. }
  210. /* Register cooling device */
  211. data->cool_dev = cpufreq_cooling_register(data->policy);
  212. if (IS_ERR(data->cool_dev)) {
  213. int ret = PTR_ERR(data->cool_dev);
  214. dev_err(bgp->dev, "Failed to register cpu cooling device %d\n",
  215. ret);
  216. cpufreq_cpu_put(data->policy);
  217. return ret;
  218. }
  219. ti_bandgap_set_sensor_data(bgp, id, data);
  220. return 0;
  221. }
  222. int ti_thermal_unregister_cpu_cooling(struct ti_bandgap *bgp, int id)
  223. {
  224. struct ti_thermal_data *data;
  225. data = ti_bandgap_get_sensor_data(bgp, id);
  226. if (!IS_ERR_OR_NULL(data)) {
  227. cpufreq_cooling_unregister(data->cool_dev);
  228. cpufreq_cpu_put(data->policy);
  229. }
  230. return 0;
  231. }