imx8mm_thermal.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright 2020 NXP.
  4. *
  5. * Author: Anson Huang <[email protected]>
  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. /*
  56. * Do not validate against the V bit (bit 31) due to errata
  57. * ERR051272: TMU: Bit 31 of registers TMU_TSCR/TMU_TRITSR/TMU_TRATSR invalid
  58. */
  59. *temp = val * 1000;
  60. if (*temp < VER1_TEMP_LOW_LIMIT || *temp > VER2_TEMP_HIGH_LIMIT)
  61. return -EAGAIN;
  62. return 0;
  63. }
  64. static int imx8mp_tmu_get_temp(void *data, int *temp)
  65. {
  66. struct tmu_sensor *sensor = data;
  67. struct imx8mm_tmu *tmu = sensor->priv;
  68. unsigned long val;
  69. bool ready;
  70. val = readl_relaxed(tmu->base + TRITSR);
  71. ready = test_bit(probe_status_offset(sensor->hw_id), &val);
  72. if (!ready)
  73. return -EAGAIN;
  74. val = sensor->hw_id ? FIELD_GET(TRITSR_TEMP1_VAL_MASK, val) :
  75. FIELD_GET(TRITSR_TEMP0_VAL_MASK, val);
  76. if (val & SIGN_BIT) /* negative */
  77. val = (~(val & TEMP_VAL_MASK) + 1);
  78. *temp = val * 1000;
  79. if (*temp < VER2_TEMP_LOW_LIMIT || *temp > VER2_TEMP_HIGH_LIMIT)
  80. return -EAGAIN;
  81. return 0;
  82. }
  83. static int tmu_get_temp(struct thermal_zone_device *tz, int *temp)
  84. {
  85. struct tmu_sensor *sensor = tz->devdata;
  86. struct imx8mm_tmu *tmu = sensor->priv;
  87. return tmu->socdata->get_temp(sensor, temp);
  88. }
  89. static const struct thermal_zone_device_ops tmu_tz_ops = {
  90. .get_temp = tmu_get_temp,
  91. };
  92. static void imx8mm_tmu_enable(struct imx8mm_tmu *tmu, bool enable)
  93. {
  94. u32 val;
  95. val = readl_relaxed(tmu->base + TER);
  96. val = enable ? (val | TER_EN) : (val & ~TER_EN);
  97. if (tmu->socdata->version == TMU_VER2)
  98. val = enable ? (val & ~TER_ADC_PD) : (val | TER_ADC_PD);
  99. writel_relaxed(val, tmu->base + TER);
  100. }
  101. static void imx8mm_tmu_probe_sel_all(struct imx8mm_tmu *tmu)
  102. {
  103. u32 val;
  104. val = readl_relaxed(tmu->base + TPS);
  105. val |= PROBE_SEL_ALL;
  106. writel_relaxed(val, tmu->base + TPS);
  107. }
  108. static int imx8mm_tmu_probe(struct platform_device *pdev)
  109. {
  110. const struct thermal_soc_data *data;
  111. struct imx8mm_tmu *tmu;
  112. int ret;
  113. int i;
  114. data = of_device_get_match_data(&pdev->dev);
  115. tmu = devm_kzalloc(&pdev->dev, struct_size(tmu, sensors,
  116. data->num_sensors), GFP_KERNEL);
  117. if (!tmu)
  118. return -ENOMEM;
  119. tmu->socdata = data;
  120. tmu->base = devm_platform_ioremap_resource(pdev, 0);
  121. if (IS_ERR(tmu->base))
  122. return PTR_ERR(tmu->base);
  123. tmu->clk = devm_clk_get(&pdev->dev, NULL);
  124. if (IS_ERR(tmu->clk))
  125. return dev_err_probe(&pdev->dev, PTR_ERR(tmu->clk),
  126. "failed to get tmu clock\n");
  127. ret = clk_prepare_enable(tmu->clk);
  128. if (ret) {
  129. dev_err(&pdev->dev, "failed to enable tmu clock: %d\n", ret);
  130. return ret;
  131. }
  132. /* disable the monitor during initialization */
  133. imx8mm_tmu_enable(tmu, false);
  134. for (i = 0; i < data->num_sensors; i++) {
  135. tmu->sensors[i].priv = tmu;
  136. tmu->sensors[i].tzd =
  137. devm_thermal_of_zone_register(&pdev->dev, i,
  138. &tmu->sensors[i],
  139. &tmu_tz_ops);
  140. if (IS_ERR(tmu->sensors[i].tzd)) {
  141. ret = PTR_ERR(tmu->sensors[i].tzd);
  142. dev_err(&pdev->dev,
  143. "failed to register thermal zone sensor[%d]: %d\n",
  144. i, ret);
  145. goto disable_clk;
  146. }
  147. tmu->sensors[i].hw_id = i;
  148. }
  149. platform_set_drvdata(pdev, tmu);
  150. /* enable all the probes for V2 TMU */
  151. if (tmu->socdata->version == TMU_VER2)
  152. imx8mm_tmu_probe_sel_all(tmu);
  153. /* enable the monitor */
  154. imx8mm_tmu_enable(tmu, true);
  155. return 0;
  156. disable_clk:
  157. clk_disable_unprepare(tmu->clk);
  158. return ret;
  159. }
  160. static int imx8mm_tmu_remove(struct platform_device *pdev)
  161. {
  162. struct imx8mm_tmu *tmu = platform_get_drvdata(pdev);
  163. /* disable TMU */
  164. imx8mm_tmu_enable(tmu, false);
  165. clk_disable_unprepare(tmu->clk);
  166. platform_set_drvdata(pdev, NULL);
  167. return 0;
  168. }
  169. static struct thermal_soc_data imx8mm_tmu_data = {
  170. .num_sensors = 1,
  171. .version = TMU_VER1,
  172. .get_temp = imx8mm_tmu_get_temp,
  173. };
  174. static struct thermal_soc_data imx8mp_tmu_data = {
  175. .num_sensors = 2,
  176. .version = TMU_VER2,
  177. .get_temp = imx8mp_tmu_get_temp,
  178. };
  179. static const struct of_device_id imx8mm_tmu_table[] = {
  180. { .compatible = "fsl,imx8mm-tmu", .data = &imx8mm_tmu_data, },
  181. { .compatible = "fsl,imx8mp-tmu", .data = &imx8mp_tmu_data, },
  182. { },
  183. };
  184. MODULE_DEVICE_TABLE(of, imx8mm_tmu_table);
  185. static struct platform_driver imx8mm_tmu = {
  186. .driver = {
  187. .name = "i.mx8mm_thermal",
  188. .of_match_table = imx8mm_tmu_table,
  189. },
  190. .probe = imx8mm_tmu_probe,
  191. .remove = imx8mm_tmu_remove,
  192. };
  193. module_platform_driver(imx8mm_tmu);
  194. MODULE_AUTHOR("Anson Huang <[email protected]>");
  195. MODULE_DESCRIPTION("i.MX8MM Thermal Monitor Unit driver");
  196. MODULE_LICENSE("GPL v2");