db8500_thermal.c 5.9 KB

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