db8500_thermal.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. struct device *dev;
  53. unsigned long interpolated_temp;
  54. unsigned int cur_index;
  55. };
  56. /* Callback to get current temperature */
  57. static int db8500_thermal_get_temp(struct thermal_zone_device *tz, int *temp)
  58. {
  59. struct db8500_thermal_zone *th = thermal_zone_device_priv(tz);
  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. static const struct thermal_zone_device_ops thdev_ops = {
  69. .get_temp = db8500_thermal_get_temp,
  70. };
  71. static void db8500_thermal_update_config(struct db8500_thermal_zone *th,
  72. unsigned int idx,
  73. unsigned long next_low,
  74. unsigned long next_high)
  75. {
  76. prcmu_stop_temp_sense();
  77. th->cur_index = idx;
  78. th->interpolated_temp = (next_low + next_high)/2;
  79. /*
  80. * The PRCMU accept absolute temperatures in celsius so divide
  81. * down the millicelsius with 1000
  82. */
  83. prcmu_config_hotmon((u8)(next_low/1000), (u8)(next_high/1000));
  84. prcmu_start_temp_sense(PRCMU_DEFAULT_MEASURE_TIME);
  85. }
  86. static irqreturn_t prcmu_low_irq_handler(int irq, void *irq_data)
  87. {
  88. struct db8500_thermal_zone *th = irq_data;
  89. unsigned int idx = th->cur_index;
  90. unsigned long next_low, next_high;
  91. if (idx == 0)
  92. /* Meaningless for thermal management, ignoring it */
  93. return IRQ_HANDLED;
  94. if (idx == 1) {
  95. next_high = db8500_thermal_points[0];
  96. next_low = PRCMU_DEFAULT_LOW_TEMP;
  97. } else {
  98. next_high = db8500_thermal_points[idx - 1];
  99. next_low = db8500_thermal_points[idx - 2];
  100. }
  101. idx -= 1;
  102. db8500_thermal_update_config(th, idx, next_low, next_high);
  103. dev_dbg(th->dev,
  104. "PRCMU set max %ld, min %ld\n", next_high, next_low);
  105. thermal_zone_device_update(th->tz, THERMAL_EVENT_UNSPECIFIED);
  106. return IRQ_HANDLED;
  107. }
  108. static irqreturn_t prcmu_high_irq_handler(int irq, void *irq_data)
  109. {
  110. struct db8500_thermal_zone *th = irq_data;
  111. unsigned int idx = th->cur_index;
  112. unsigned long next_low, next_high;
  113. int num_points = ARRAY_SIZE(db8500_thermal_points);
  114. if (idx < num_points - 1) {
  115. next_high = db8500_thermal_points[idx+1];
  116. next_low = db8500_thermal_points[idx];
  117. idx += 1;
  118. db8500_thermal_update_config(th, idx, next_low, next_high);
  119. dev_dbg(th->dev,
  120. "PRCMU set max %ld, min %ld\n", next_high, next_low);
  121. } else if (idx == num_points - 1)
  122. /* So we roof out 1 degree over the max point */
  123. th->interpolated_temp = db8500_thermal_points[idx] + 1;
  124. thermal_zone_device_update(th->tz, THERMAL_EVENT_UNSPECIFIED);
  125. return IRQ_HANDLED;
  126. }
  127. static int db8500_thermal_probe(struct platform_device *pdev)
  128. {
  129. struct db8500_thermal_zone *th = NULL;
  130. struct device *dev = &pdev->dev;
  131. int low_irq, high_irq, ret = 0;
  132. th = devm_kzalloc(dev, sizeof(*th), GFP_KERNEL);
  133. if (!th)
  134. return -ENOMEM;
  135. th->dev = dev;
  136. low_irq = platform_get_irq_byname(pdev, "IRQ_HOTMON_LOW");
  137. if (low_irq < 0)
  138. return low_irq;
  139. ret = devm_request_threaded_irq(dev, low_irq, NULL,
  140. prcmu_low_irq_handler, IRQF_NO_SUSPEND | IRQF_ONESHOT,
  141. "dbx500_temp_low", th);
  142. if (ret < 0) {
  143. dev_err(dev, "failed to allocate temp low irq\n");
  144. return ret;
  145. }
  146. high_irq = platform_get_irq_byname(pdev, "IRQ_HOTMON_HIGH");
  147. if (high_irq < 0)
  148. return high_irq;
  149. ret = devm_request_threaded_irq(dev, high_irq, NULL,
  150. prcmu_high_irq_handler, IRQF_NO_SUSPEND | IRQF_ONESHOT,
  151. "dbx500_temp_high", th);
  152. if (ret < 0) {
  153. dev_err(dev, "failed to allocate temp high irq\n");
  154. return ret;
  155. }
  156. /* register of thermal sensor and get info from DT */
  157. th->tz = devm_thermal_of_zone_register(dev, 0, th, &thdev_ops);
  158. if (IS_ERR(th->tz)) {
  159. dev_err(dev, "register thermal zone sensor failed\n");
  160. return PTR_ERR(th->tz);
  161. }
  162. dev_info(dev, "thermal zone sensor registered\n");
  163. /* Start measuring at the lowest point */
  164. db8500_thermal_update_config(th, 0, PRCMU_DEFAULT_LOW_TEMP,
  165. db8500_thermal_points[0]);
  166. platform_set_drvdata(pdev, th);
  167. return 0;
  168. }
  169. static int db8500_thermal_suspend(struct platform_device *pdev,
  170. pm_message_t state)
  171. {
  172. prcmu_stop_temp_sense();
  173. return 0;
  174. }
  175. static int db8500_thermal_resume(struct platform_device *pdev)
  176. {
  177. struct db8500_thermal_zone *th = platform_get_drvdata(pdev);
  178. /* Resume and start measuring at the lowest point */
  179. db8500_thermal_update_config(th, 0, PRCMU_DEFAULT_LOW_TEMP,
  180. db8500_thermal_points[0]);
  181. return 0;
  182. }
  183. static const struct of_device_id db8500_thermal_match[] = {
  184. { .compatible = "stericsson,db8500-thermal" },
  185. {},
  186. };
  187. MODULE_DEVICE_TABLE(of, db8500_thermal_match);
  188. static struct platform_driver db8500_thermal_driver = {
  189. .driver = {
  190. .name = "db8500-thermal",
  191. .of_match_table = db8500_thermal_match,
  192. },
  193. .probe = db8500_thermal_probe,
  194. .suspend = db8500_thermal_suspend,
  195. .resume = db8500_thermal_resume,
  196. };
  197. module_platform_driver(db8500_thermal_driver);
  198. MODULE_AUTHOR("Hongbo Zhang <hongbo.zhang@stericsson.com>");
  199. MODULE_DESCRIPTION("DB8500 thermal driver");
  200. MODULE_LICENSE("GPL");