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