db8500_thermal.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * db8500_thermal.c - DB8500 Thermal Management Implementation
  4. *
  5. * Copyright (C) 2012 ST-Ericsson
  6. * Copyright (C) 2012-2019 Linaro Ltd.
  7. *
  8. * Authors: Hongbo Zhang, Linus Walleij
  9. */
  10. #include <linux/cpu_cooling.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/mfd/dbx500-prcmu.h>
  13. #include <linux/module.h>
  14. #include <linux/of.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/slab.h>
  17. #include <linux/thermal.h>
  18. #define PRCMU_DEFAULT_MEASURE_TIME 0xFFF
  19. #define PRCMU_DEFAULT_LOW_TEMP 0
  20. /**
  21. * db8500_thermal_points - the interpolation points that trigger
  22. * interrupts
  23. */
  24. static const unsigned long db8500_thermal_points[] = {
  25. 15000,
  26. 20000,
  27. 25000,
  28. 30000,
  29. 35000,
  30. 40000,
  31. 45000,
  32. 50000,
  33. 55000,
  34. 60000,
  35. 65000,
  36. 70000,
  37. 75000,
  38. 80000,
  39. /*
  40. * This is where things start to get really bad for the
  41. * SoC and the thermal zones should be set up to trigger
  42. * critical temperature at 85000 mC so we don't get above
  43. * this point.
  44. */
  45. 85000,
  46. 90000,
  47. 95000,
  48. 100000,
  49. };
  50. struct db8500_thermal_zone {
  51. struct thermal_zone_device *tz;
  52. enum thermal_trend trend;
  53. unsigned long interpolated_temp;
  54. unsigned int cur_index;
  55. };
  56. /* Callback to get current temperature */
  57. static int db8500_thermal_get_temp(void *data, int *temp)
  58. {
  59. struct db8500_thermal_zone *th = data;
  60. /*
  61. * TODO: There is no PRCMU interface to get temperature data currently,
  62. * so a pseudo temperature is returned , it works for thermal framework
  63. * and this will be fixed when the PRCMU interface is available.
  64. */
  65. *temp = th->interpolated_temp;
  66. return 0;
  67. }
  68. /* Callback to get temperature changing trend */
  69. static int db8500_thermal_get_trend(void *data, int trip, enum thermal_trend *trend)
  70. {
  71. struct db8500_thermal_zone *th = data;
  72. *trend = th->trend;
  73. return 0;
  74. }
  75. static struct thermal_zone_of_device_ops thdev_ops = {
  76. .get_temp = db8500_thermal_get_temp,
  77. .get_trend = db8500_thermal_get_trend,
  78. };
  79. static void db8500_thermal_update_config(struct db8500_thermal_zone *th,
  80. unsigned int idx,
  81. enum thermal_trend trend,
  82. unsigned long next_low,
  83. unsigned long next_high)
  84. {
  85. prcmu_stop_temp_sense();
  86. th->cur_index = idx;
  87. th->interpolated_temp = (next_low + next_high)/2;
  88. th->trend = trend;
  89. /*
  90. * The PRCMU accept absolute temperatures in celsius so divide
  91. * down the millicelsius with 1000
  92. */
  93. prcmu_config_hotmon((u8)(next_low/1000), (u8)(next_high/1000));
  94. prcmu_start_temp_sense(PRCMU_DEFAULT_MEASURE_TIME);
  95. }
  96. static irqreturn_t prcmu_low_irq_handler(int irq, void *irq_data)
  97. {
  98. struct db8500_thermal_zone *th = irq_data;
  99. unsigned int idx = th->cur_index;
  100. unsigned long next_low, next_high;
  101. if (idx == 0)
  102. /* Meaningless for thermal management, ignoring it */
  103. return IRQ_HANDLED;
  104. if (idx == 1) {
  105. next_high = db8500_thermal_points[0];
  106. next_low = PRCMU_DEFAULT_LOW_TEMP;
  107. } else {
  108. next_high = db8500_thermal_points[idx - 1];
  109. next_low = db8500_thermal_points[idx - 2];
  110. }
  111. idx -= 1;
  112. db8500_thermal_update_config(th, idx, THERMAL_TREND_DROPPING,
  113. next_low, next_high);
  114. dev_dbg(&th->tz->device,
  115. "PRCMU set max %ld, min %ld\n", next_high, next_low);
  116. thermal_zone_device_update(th->tz, THERMAL_EVENT_UNSPECIFIED);
  117. return IRQ_HANDLED;
  118. }
  119. static irqreturn_t prcmu_high_irq_handler(int irq, void *irq_data)
  120. {
  121. struct db8500_thermal_zone *th = irq_data;
  122. unsigned int idx = th->cur_index;
  123. unsigned long next_low, next_high;
  124. int num_points = ARRAY_SIZE(db8500_thermal_points);
  125. if (idx < num_points - 1) {
  126. next_high = db8500_thermal_points[idx+1];
  127. next_low = db8500_thermal_points[idx];
  128. idx += 1;
  129. db8500_thermal_update_config(th, idx, THERMAL_TREND_RAISING,
  130. next_low, next_high);
  131. dev_dbg(&th->tz->device,
  132. "PRCMU set max %ld, min %ld\n", next_high, next_low);
  133. } else if (idx == num_points - 1)
  134. /* So we roof out 1 degree over the max point */
  135. th->interpolated_temp = db8500_thermal_points[idx] + 1;
  136. thermal_zone_device_update(th->tz, THERMAL_EVENT_UNSPECIFIED);
  137. return IRQ_HANDLED;
  138. }
  139. static int db8500_thermal_probe(struct platform_device *pdev)
  140. {
  141. struct db8500_thermal_zone *th = NULL;
  142. struct device *dev = &pdev->dev;
  143. int low_irq, high_irq, ret = 0;
  144. th = devm_kzalloc(dev, sizeof(*th), GFP_KERNEL);
  145. if (!th)
  146. return -ENOMEM;
  147. low_irq = platform_get_irq_byname(pdev, "IRQ_HOTMON_LOW");
  148. if (low_irq < 0) {
  149. dev_err(dev, "Get IRQ_HOTMON_LOW failed\n");
  150. return low_irq;
  151. }
  152. ret = devm_request_threaded_irq(dev, low_irq, NULL,
  153. prcmu_low_irq_handler, IRQF_NO_SUSPEND | IRQF_ONESHOT,
  154. "dbx500_temp_low", th);
  155. if (ret < 0) {
  156. dev_err(dev, "failed to allocate temp low irq\n");
  157. return ret;
  158. }
  159. high_irq = platform_get_irq_byname(pdev, "IRQ_HOTMON_HIGH");
  160. if (high_irq < 0) {
  161. dev_err(dev, "Get IRQ_HOTMON_HIGH failed\n");
  162. return high_irq;
  163. }
  164. ret = devm_request_threaded_irq(dev, high_irq, NULL,
  165. prcmu_high_irq_handler, IRQF_NO_SUSPEND | IRQF_ONESHOT,
  166. "dbx500_temp_high", th);
  167. if (ret < 0) {
  168. dev_err(dev, "failed to allocate temp high irq\n");
  169. return ret;
  170. }
  171. /* register of thermal sensor and get info from DT */
  172. th->tz = devm_thermal_zone_of_sensor_register(dev, 0, th, &thdev_ops);
  173. if (IS_ERR(th->tz)) {
  174. dev_err(dev, "register thermal zone sensor failed\n");
  175. return PTR_ERR(th->tz);
  176. }
  177. dev_info(dev, "thermal zone sensor registered\n");
  178. /* Start measuring at the lowest point */
  179. db8500_thermal_update_config(th, 0, THERMAL_TREND_STABLE,
  180. PRCMU_DEFAULT_LOW_TEMP,
  181. db8500_thermal_points[0]);
  182. platform_set_drvdata(pdev, th);
  183. return 0;
  184. }
  185. static int db8500_thermal_suspend(struct platform_device *pdev,
  186. pm_message_t state)
  187. {
  188. prcmu_stop_temp_sense();
  189. return 0;
  190. }
  191. static int db8500_thermal_resume(struct platform_device *pdev)
  192. {
  193. struct db8500_thermal_zone *th = platform_get_drvdata(pdev);
  194. /* Resume and start measuring at the lowest point */
  195. db8500_thermal_update_config(th, 0, THERMAL_TREND_STABLE,
  196. PRCMU_DEFAULT_LOW_TEMP,
  197. db8500_thermal_points[0]);
  198. return 0;
  199. }
  200. static const struct of_device_id db8500_thermal_match[] = {
  201. { .compatible = "stericsson,db8500-thermal" },
  202. {},
  203. };
  204. MODULE_DEVICE_TABLE(of, db8500_thermal_match);
  205. static struct platform_driver db8500_thermal_driver = {
  206. .driver = {
  207. .name = "db8500-thermal",
  208. .of_match_table = of_match_ptr(db8500_thermal_match),
  209. },
  210. .probe = db8500_thermal_probe,
  211. .suspend = db8500_thermal_suspend,
  212. .resume = db8500_thermal_resume,
  213. };
  214. module_platform_driver(db8500_thermal_driver);
  215. MODULE_AUTHOR("Hongbo Zhang <hongbo.zhang@stericsson.com>");
  216. MODULE_DESCRIPTION("DB8500 thermal driver");
  217. MODULE_LICENSE("GPL");