imx8mm_thermal.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright 2020 NXP.
  4. *
  5. * Author: Anson Huang <Anson.Huang@nxp.com>
  6. */
  7. #include <linux/bitfield.h>
  8. #include <linux/clk.h>
  9. #include <linux/err.h>
  10. #include <linux/io.h>
  11. #include <linux/module.h>
  12. #include <linux/of.h>
  13. #include <linux/of_device.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/thermal.h>
  16. #include "thermal_core.h"
  17. #define TER 0x0 /* TMU enable */
  18. #define TPS 0x4
  19. #define TRITSR 0x20 /* TMU immediate temp */
  20. #define TER_ADC_PD BIT(30)
  21. #define TER_EN BIT(31)
  22. #define TRITSR_TEMP0_VAL_MASK 0xff
  23. #define TRITSR_TEMP1_VAL_MASK 0xff0000
  24. #define PROBE_SEL_ALL GENMASK(31, 30)
  25. #define probe_status_offset(x) (30 + x)
  26. #define SIGN_BIT BIT(7)
  27. #define TEMP_VAL_MASK GENMASK(6, 0)
  28. #define VER1_TEMP_LOW_LIMIT 10000
  29. #define VER2_TEMP_LOW_LIMIT -40000
  30. #define VER2_TEMP_HIGH_LIMIT 125000
  31. #define TMU_VER1 0x1
  32. #define TMU_VER2 0x2
  33. struct thermal_soc_data {
  34. u32 num_sensors;
  35. u32 version;
  36. int (*get_temp)(void *, int *);
  37. };
  38. struct tmu_sensor {
  39. struct imx8mm_tmu *priv;
  40. u32 hw_id;
  41. struct thermal_zone_device *tzd;
  42. };
  43. struct imx8mm_tmu {
  44. void __iomem *base;
  45. struct clk *clk;
  46. const struct thermal_soc_data *socdata;
  47. struct tmu_sensor sensors[];
  48. };
  49. static int imx8mm_tmu_get_temp(void *data, int *temp)
  50. {
  51. struct tmu_sensor *sensor = data;
  52. struct imx8mm_tmu *tmu = sensor->priv;
  53. u32 val;
  54. val = readl_relaxed(tmu->base + TRITSR) & TRITSR_TEMP0_VAL_MASK;
  55. *temp = val * 1000;
  56. if (*temp < VER1_TEMP_LOW_LIMIT)
  57. return -EAGAIN;
  58. return 0;
  59. }
  60. static int imx8mp_tmu_get_temp(void *data, int *temp)
  61. {
  62. struct tmu_sensor *sensor = data;
  63. struct imx8mm_tmu *tmu = sensor->priv;
  64. unsigned long val;
  65. bool ready;
  66. val = readl_relaxed(tmu->base + TRITSR);
  67. ready = test_bit(probe_status_offset(sensor->hw_id), &val);
  68. if (!ready)
  69. return -EAGAIN;
  70. val = sensor->hw_id ? FIELD_GET(TRITSR_TEMP1_VAL_MASK, val) :
  71. FIELD_GET(TRITSR_TEMP0_VAL_MASK, val);
  72. if (val & SIGN_BIT) /* negative */
  73. val = (~(val & TEMP_VAL_MASK) + 1);
  74. *temp = val * 1000;
  75. if (*temp < VER2_TEMP_LOW_LIMIT || *temp > VER2_TEMP_HIGH_LIMIT)
  76. return -EAGAIN;
  77. return 0;
  78. }
  79. static int tmu_get_temp(void *data, int *temp)
  80. {
  81. struct tmu_sensor *sensor = data;
  82. struct imx8mm_tmu *tmu = sensor->priv;
  83. return tmu->socdata->get_temp(data, temp);
  84. }
  85. static struct thermal_zone_of_device_ops tmu_tz_ops = {
  86. .get_temp = tmu_get_temp,
  87. };
  88. static void imx8mm_tmu_enable(struct imx8mm_tmu *tmu, bool enable)
  89. {
  90. u32 val;
  91. val = readl_relaxed(tmu->base + TER);
  92. val = enable ? (val | TER_EN) : (val & ~TER_EN);
  93. if (tmu->socdata->version == TMU_VER2)
  94. val = enable ? (val & ~TER_ADC_PD) : (val | TER_ADC_PD);
  95. writel_relaxed(val, tmu->base + TER);
  96. }
  97. static void imx8mm_tmu_probe_sel_all(struct imx8mm_tmu *tmu)
  98. {
  99. u32 val;
  100. val = readl_relaxed(tmu->base + TPS);
  101. val |= PROBE_SEL_ALL;
  102. writel_relaxed(val, tmu->base + TPS);
  103. }
  104. static int imx8mm_tmu_probe(struct platform_device *pdev)
  105. {
  106. const struct thermal_soc_data *data;
  107. struct imx8mm_tmu *tmu;
  108. int ret;
  109. int i;
  110. data = of_device_get_match_data(&pdev->dev);
  111. tmu = devm_kzalloc(&pdev->dev, struct_size(tmu, sensors,
  112. data->num_sensors), GFP_KERNEL);
  113. if (!tmu)
  114. return -ENOMEM;
  115. tmu->socdata = data;
  116. tmu->base = devm_platform_ioremap_resource(pdev, 0);
  117. if (IS_ERR(tmu->base))
  118. return PTR_ERR(tmu->base);
  119. tmu->clk = devm_clk_get(&pdev->dev, NULL);
  120. if (IS_ERR(tmu->clk))
  121. return dev_err_probe(&pdev->dev, PTR_ERR(tmu->clk),
  122. "failed to get tmu clock\n");
  123. ret = clk_prepare_enable(tmu->clk);
  124. if (ret) {
  125. dev_err(&pdev->dev, "failed to enable tmu clock: %d\n", ret);
  126. return ret;
  127. }
  128. /* disable the monitor during initialization */
  129. imx8mm_tmu_enable(tmu, false);
  130. for (i = 0; i < data->num_sensors; i++) {
  131. tmu->sensors[i].priv = tmu;
  132. tmu->sensors[i].tzd =
  133. devm_thermal_zone_of_sensor_register(&pdev->dev, i,
  134. &tmu->sensors[i],
  135. &tmu_tz_ops);
  136. if (IS_ERR(tmu->sensors[i].tzd)) {
  137. dev_err(&pdev->dev,
  138. "failed to register thermal zone sensor[%d]: %d\n",
  139. i, ret);
  140. return PTR_ERR(tmu->sensors[i].tzd);
  141. }
  142. tmu->sensors[i].hw_id = i;
  143. }
  144. platform_set_drvdata(pdev, tmu);
  145. /* enable all the probes for V2 TMU */
  146. if (tmu->socdata->version == TMU_VER2)
  147. imx8mm_tmu_probe_sel_all(tmu);
  148. /* enable the monitor */
  149. imx8mm_tmu_enable(tmu, true);
  150. return 0;
  151. }
  152. static int imx8mm_tmu_remove(struct platform_device *pdev)
  153. {
  154. struct imx8mm_tmu *tmu = platform_get_drvdata(pdev);
  155. /* disable TMU */
  156. imx8mm_tmu_enable(tmu, false);
  157. clk_disable_unprepare(tmu->clk);
  158. platform_set_drvdata(pdev, NULL);
  159. return 0;
  160. }
  161. static struct thermal_soc_data imx8mm_tmu_data = {
  162. .num_sensors = 1,
  163. .version = TMU_VER1,
  164. .get_temp = imx8mm_tmu_get_temp,
  165. };
  166. static struct thermal_soc_data imx8mp_tmu_data = {
  167. .num_sensors = 2,
  168. .version = TMU_VER2,
  169. .get_temp = imx8mp_tmu_get_temp,
  170. };
  171. static const struct of_device_id imx8mm_tmu_table[] = {
  172. { .compatible = "fsl,imx8mm-tmu", .data = &imx8mm_tmu_data, },
  173. { .compatible = "fsl,imx8mp-tmu", .data = &imx8mp_tmu_data, },
  174. { },
  175. };
  176. MODULE_DEVICE_TABLE(of, imx8mm_tmu_table);
  177. static struct platform_driver imx8mm_tmu = {
  178. .driver = {
  179. .name = "i.mx8mm_thermal",
  180. .of_match_table = imx8mm_tmu_table,
  181. },
  182. .probe = imx8mm_tmu_probe,
  183. .remove = imx8mm_tmu_remove,
  184. };
  185. module_platform_driver(imx8mm_tmu);
  186. MODULE_AUTHOR("Anson Huang <Anson.Huang@nxp.com>");
  187. MODULE_DESCRIPTION("i.MX8MM Thermal Monitor Unit driver");
  188. MODULE_LICENSE("GPL v2");