ti-thermal-common.c 6.3 KB

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