ti-thermal-common.c 11 KB

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