imx8mm_thermal.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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/nvmem-consumer.h>
  13. #include <linux/of.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/slab.h>
  16. #include <linux/thermal.h>
  17. #include "thermal_hwmon.h"
  18. #define TER 0x0 /* TMU enable */
  19. #define TPS 0x4
  20. #define TRITSR 0x20 /* TMU immediate temp */
  21. /* TMU calibration data registers */
  22. #define TASR 0x28
  23. #define TASR_BUF_SLOPE_MASK GENMASK(19, 16)
  24. #define TASR_BUF_VREF_MASK GENMASK(4, 0) /* TMU_V1 */
  25. #define TASR_BUF_VERF_SEL_MASK GENMASK(1, 0) /* TMU_V2 */
  26. #define TCALIV(n) (0x30 + ((n) * 4))
  27. #define TCALIV_EN BIT(31)
  28. #define TCALIV_HR_MASK GENMASK(23, 16) /* TMU_V1 */
  29. #define TCALIV_RT_MASK GENMASK(7, 0) /* TMU_V1 */
  30. #define TCALIV_SNSR105C_MASK GENMASK(27, 16) /* TMU_V2 */
  31. #define TCALIV_SNSR25C_MASK GENMASK(11, 0) /* TMU_V2 */
  32. #define TRIM 0x3c
  33. #define TRIM_BJT_CUR_MASK GENMASK(23, 20)
  34. #define TRIM_BGR_MASK GENMASK(31, 28)
  35. #define TRIM_VLSB_MASK GENMASK(15, 12)
  36. #define TRIM_EN_CH BIT(7)
  37. #define TER_ADC_PD BIT(30)
  38. #define TER_EN BIT(31)
  39. #define TRITSR_TEMP0_VAL_MASK GENMASK(7, 0)
  40. #define TRITSR_TEMP1_VAL_MASK GENMASK(23, 16)
  41. #define PROBE_SEL_ALL GENMASK(31, 30)
  42. #define probe_status_offset(x) (30 + x)
  43. #define SIGN_BIT BIT(7)
  44. #define TEMP_VAL_MASK GENMASK(6, 0)
  45. /* TMU OCOTP calibration data bitfields */
  46. #define ANA0_EN BIT(25)
  47. #define ANA0_BUF_VREF_MASK GENMASK(24, 20)
  48. #define ANA0_BUF_SLOPE_MASK GENMASK(19, 16)
  49. #define ANA0_HR_MASK GENMASK(15, 8)
  50. #define ANA0_RT_MASK GENMASK(7, 0)
  51. #define TRIM2_VLSB_MASK GENMASK(23, 20)
  52. #define TRIM2_BGR_MASK GENMASK(19, 16)
  53. #define TRIM2_BJT_CUR_MASK GENMASK(15, 12)
  54. #define TRIM2_BUF_SLOP_SEL_MASK GENMASK(11, 8)
  55. #define TRIM2_BUF_VERF_SEL_MASK GENMASK(7, 6)
  56. #define TRIM3_TCA25_0_LSB_MASK GENMASK(31, 28)
  57. #define TRIM3_TCA40_0_MASK GENMASK(27, 16)
  58. #define TRIM4_TCA40_1_MASK GENMASK(31, 20)
  59. #define TRIM4_TCA105_0_MASK GENMASK(19, 8)
  60. #define TRIM4_TCA25_0_MSB_MASK GENMASK(7, 0)
  61. #define TRIM5_TCA105_1_MASK GENMASK(23, 12)
  62. #define TRIM5_TCA25_1_MASK GENMASK(11, 0)
  63. #define VER1_TEMP_LOW_LIMIT 10000
  64. #define VER2_TEMP_LOW_LIMIT -40000
  65. #define VER2_TEMP_HIGH_LIMIT 125000
  66. #define TMU_VER1 0x1
  67. #define TMU_VER2 0x2
  68. struct thermal_soc_data {
  69. u32 num_sensors;
  70. u32 version;
  71. int (*get_temp)(void *data, int *temp);
  72. };
  73. struct tmu_sensor {
  74. struct imx8mm_tmu *priv;
  75. u32 hw_id;
  76. struct thermal_zone_device *tzd;
  77. };
  78. struct imx8mm_tmu {
  79. void __iomem *base;
  80. struct clk *clk;
  81. const struct thermal_soc_data *socdata;
  82. struct tmu_sensor sensors[];
  83. };
  84. static int imx8mm_tmu_get_temp(void *data, int *temp)
  85. {
  86. struct tmu_sensor *sensor = data;
  87. struct imx8mm_tmu *tmu = sensor->priv;
  88. u32 val;
  89. val = readl_relaxed(tmu->base + TRITSR) & TRITSR_TEMP0_VAL_MASK;
  90. /*
  91. * Do not validate against the V bit (bit 31) due to errata
  92. * ERR051272: TMU: Bit 31 of registers TMU_TSCR/TMU_TRITSR/TMU_TRATSR invalid
  93. */
  94. *temp = val * 1000;
  95. if (*temp < VER1_TEMP_LOW_LIMIT || *temp > VER2_TEMP_HIGH_LIMIT)
  96. return -EAGAIN;
  97. return 0;
  98. }
  99. static int imx8mp_tmu_get_temp(void *data, int *temp)
  100. {
  101. struct tmu_sensor *sensor = data;
  102. struct imx8mm_tmu *tmu = sensor->priv;
  103. unsigned long val;
  104. bool ready;
  105. val = readl_relaxed(tmu->base + TRITSR);
  106. ready = test_bit(probe_status_offset(sensor->hw_id), &val);
  107. if (!ready)
  108. return -EAGAIN;
  109. val = sensor->hw_id ? FIELD_GET(TRITSR_TEMP1_VAL_MASK, val) :
  110. FIELD_GET(TRITSR_TEMP0_VAL_MASK, val);
  111. if (val & SIGN_BIT) /* negative */
  112. val = (~(val & TEMP_VAL_MASK) + 1);
  113. *temp = val * 1000;
  114. if (*temp < VER2_TEMP_LOW_LIMIT || *temp > VER2_TEMP_HIGH_LIMIT)
  115. return -EAGAIN;
  116. return 0;
  117. }
  118. static int tmu_get_temp(struct thermal_zone_device *tz, int *temp)
  119. {
  120. struct tmu_sensor *sensor = thermal_zone_device_priv(tz);
  121. struct imx8mm_tmu *tmu = sensor->priv;
  122. return tmu->socdata->get_temp(sensor, temp);
  123. }
  124. static const struct thermal_zone_device_ops tmu_tz_ops = {
  125. .get_temp = tmu_get_temp,
  126. };
  127. static void imx8mm_tmu_enable(struct imx8mm_tmu *tmu, bool enable)
  128. {
  129. u32 val;
  130. val = readl_relaxed(tmu->base + TER);
  131. val = enable ? (val | TER_EN) : (val & ~TER_EN);
  132. if (tmu->socdata->version == TMU_VER2)
  133. val = enable ? (val & ~TER_ADC_PD) : (val | TER_ADC_PD);
  134. writel_relaxed(val, tmu->base + TER);
  135. }
  136. static void imx8mm_tmu_probe_sel_all(struct imx8mm_tmu *tmu)
  137. {
  138. u32 val;
  139. val = readl_relaxed(tmu->base + TPS);
  140. val |= PROBE_SEL_ALL;
  141. writel_relaxed(val, tmu->base + TPS);
  142. }
  143. static int imx8mm_tmu_probe_set_calib_v1(struct platform_device *pdev,
  144. struct imx8mm_tmu *tmu)
  145. {
  146. struct device *dev = &pdev->dev;
  147. u32 ana0;
  148. int ret;
  149. ret = nvmem_cell_read_u32(&pdev->dev, "calib", &ana0);
  150. if (ret)
  151. return dev_err_probe(dev, ret, "Failed to read OCOTP nvmem cell\n");
  152. writel(FIELD_PREP(TASR_BUF_VREF_MASK,
  153. FIELD_GET(ANA0_BUF_VREF_MASK, ana0)) |
  154. FIELD_PREP(TASR_BUF_SLOPE_MASK,
  155. FIELD_GET(ANA0_BUF_SLOPE_MASK, ana0)),
  156. tmu->base + TASR);
  157. writel(FIELD_PREP(TCALIV_RT_MASK, FIELD_GET(ANA0_RT_MASK, ana0)) |
  158. FIELD_PREP(TCALIV_HR_MASK, FIELD_GET(ANA0_HR_MASK, ana0)) |
  159. ((ana0 & ANA0_EN) ? TCALIV_EN : 0),
  160. tmu->base + TCALIV(0));
  161. return 0;
  162. }
  163. static int imx8mm_tmu_probe_set_calib_v2(struct platform_device *pdev,
  164. struct imx8mm_tmu *tmu)
  165. {
  166. struct device *dev = &pdev->dev;
  167. struct nvmem_cell *cell;
  168. u32 trim[4] = { 0 };
  169. size_t len;
  170. void *buf;
  171. cell = nvmem_cell_get(dev, "calib");
  172. if (IS_ERR(cell))
  173. return PTR_ERR(cell);
  174. buf = nvmem_cell_read(cell, &len);
  175. nvmem_cell_put(cell);
  176. if (IS_ERR(buf))
  177. return PTR_ERR(buf);
  178. memcpy(trim, buf, min(len, sizeof(trim)));
  179. kfree(buf);
  180. if (len != 16) {
  181. dev_err(dev,
  182. "OCOTP nvmem cell length is %zu, must be 16.\n", len);
  183. return -EINVAL;
  184. }
  185. /* Blank sample hardware */
  186. if (!trim[0] && !trim[1] && !trim[2] && !trim[3]) {
  187. /* Use a default 25C binary codes */
  188. writel(FIELD_PREP(TCALIV_SNSR25C_MASK, 0x63c),
  189. tmu->base + TCALIV(0));
  190. writel(FIELD_PREP(TCALIV_SNSR25C_MASK, 0x63c),
  191. tmu->base + TCALIV(1));
  192. return 0;
  193. }
  194. writel(FIELD_PREP(TASR_BUF_VERF_SEL_MASK,
  195. FIELD_GET(TRIM2_BUF_VERF_SEL_MASK, trim[0])) |
  196. FIELD_PREP(TASR_BUF_SLOPE_MASK,
  197. FIELD_GET(TRIM2_BUF_SLOP_SEL_MASK, trim[0])),
  198. tmu->base + TASR);
  199. writel(FIELD_PREP(TRIM_BJT_CUR_MASK,
  200. FIELD_GET(TRIM2_BJT_CUR_MASK, trim[0])) |
  201. FIELD_PREP(TRIM_BGR_MASK, FIELD_GET(TRIM2_BGR_MASK, trim[0])) |
  202. FIELD_PREP(TRIM_VLSB_MASK, FIELD_GET(TRIM2_VLSB_MASK, trim[0])) |
  203. TRIM_EN_CH,
  204. tmu->base + TRIM);
  205. writel(FIELD_PREP(TCALIV_SNSR25C_MASK,
  206. FIELD_GET(TRIM3_TCA25_0_LSB_MASK, trim[1]) |
  207. (FIELD_GET(TRIM4_TCA25_0_MSB_MASK, trim[2]) << 4)) |
  208. FIELD_PREP(TCALIV_SNSR105C_MASK,
  209. FIELD_GET(TRIM4_TCA105_0_MASK, trim[2])),
  210. tmu->base + TCALIV(0));
  211. writel(FIELD_PREP(TCALIV_SNSR25C_MASK,
  212. FIELD_GET(TRIM5_TCA25_1_MASK, trim[3])) |
  213. FIELD_PREP(TCALIV_SNSR105C_MASK,
  214. FIELD_GET(TRIM5_TCA105_1_MASK, trim[3])),
  215. tmu->base + TCALIV(1));
  216. writel(FIELD_PREP(TCALIV_SNSR25C_MASK,
  217. FIELD_GET(TRIM3_TCA40_0_MASK, trim[1])) |
  218. FIELD_PREP(TCALIV_SNSR105C_MASK,
  219. FIELD_GET(TRIM4_TCA40_1_MASK, trim[2])),
  220. tmu->base + TCALIV(2));
  221. return 0;
  222. }
  223. static int imx8mm_tmu_probe_set_calib(struct platform_device *pdev,
  224. struct imx8mm_tmu *tmu)
  225. {
  226. struct device *dev = &pdev->dev;
  227. /*
  228. * Lack of calibration data OCOTP reference is not considered
  229. * fatal to retain compatibility with old DTs. It is however
  230. * strongly recommended to update such old DTs to get correct
  231. * temperature compensation values for each SoC.
  232. */
  233. if (!of_property_present(pdev->dev.of_node, "nvmem-cells")) {
  234. dev_warn(dev,
  235. "No OCOTP nvmem reference found, SoC-specific calibration not loaded. Please update your DT.\n");
  236. return 0;
  237. }
  238. if (tmu->socdata->version == TMU_VER1)
  239. return imx8mm_tmu_probe_set_calib_v1(pdev, tmu);
  240. return imx8mm_tmu_probe_set_calib_v2(pdev, tmu);
  241. }
  242. static int imx8mm_tmu_probe(struct platform_device *pdev)
  243. {
  244. const struct thermal_soc_data *data;
  245. struct imx8mm_tmu *tmu;
  246. int ret;
  247. int i;
  248. data = of_device_get_match_data(&pdev->dev);
  249. tmu = devm_kzalloc(&pdev->dev, struct_size(tmu, sensors,
  250. data->num_sensors), GFP_KERNEL);
  251. if (!tmu)
  252. return -ENOMEM;
  253. tmu->socdata = data;
  254. tmu->base = devm_platform_ioremap_resource(pdev, 0);
  255. if (IS_ERR(tmu->base))
  256. return PTR_ERR(tmu->base);
  257. tmu->clk = devm_clk_get(&pdev->dev, NULL);
  258. if (IS_ERR(tmu->clk))
  259. return dev_err_probe(&pdev->dev, PTR_ERR(tmu->clk),
  260. "failed to get tmu clock\n");
  261. ret = clk_prepare_enable(tmu->clk);
  262. if (ret) {
  263. dev_err(&pdev->dev, "failed to enable tmu clock: %d\n", ret);
  264. return ret;
  265. }
  266. /* disable the monitor during initialization */
  267. imx8mm_tmu_enable(tmu, false);
  268. for (i = 0; i < data->num_sensors; i++) {
  269. tmu->sensors[i].priv = tmu;
  270. tmu->sensors[i].tzd =
  271. devm_thermal_of_zone_register(&pdev->dev, i,
  272. &tmu->sensors[i],
  273. &tmu_tz_ops);
  274. if (IS_ERR(tmu->sensors[i].tzd)) {
  275. ret = PTR_ERR(tmu->sensors[i].tzd);
  276. dev_err(&pdev->dev,
  277. "failed to register thermal zone sensor[%d]: %d\n",
  278. i, ret);
  279. goto disable_clk;
  280. }
  281. tmu->sensors[i].hw_id = i;
  282. devm_thermal_add_hwmon_sysfs(&pdev->dev, tmu->sensors[i].tzd);
  283. }
  284. platform_set_drvdata(pdev, tmu);
  285. ret = imx8mm_tmu_probe_set_calib(pdev, tmu);
  286. if (ret)
  287. goto disable_clk;
  288. /* enable all the probes for V2 TMU */
  289. if (tmu->socdata->version == TMU_VER2)
  290. imx8mm_tmu_probe_sel_all(tmu);
  291. /* enable the monitor */
  292. imx8mm_tmu_enable(tmu, true);
  293. return 0;
  294. disable_clk:
  295. clk_disable_unprepare(tmu->clk);
  296. return ret;
  297. }
  298. static void imx8mm_tmu_remove(struct platform_device *pdev)
  299. {
  300. struct imx8mm_tmu *tmu = platform_get_drvdata(pdev);
  301. /* disable TMU */
  302. imx8mm_tmu_enable(tmu, false);
  303. clk_disable_unprepare(tmu->clk);
  304. platform_set_drvdata(pdev, NULL);
  305. }
  306. static struct thermal_soc_data imx8mm_tmu_data = {
  307. .num_sensors = 1,
  308. .version = TMU_VER1,
  309. .get_temp = imx8mm_tmu_get_temp,
  310. };
  311. static struct thermal_soc_data imx8mp_tmu_data = {
  312. .num_sensors = 2,
  313. .version = TMU_VER2,
  314. .get_temp = imx8mp_tmu_get_temp,
  315. };
  316. static const struct of_device_id imx8mm_tmu_table[] = {
  317. { .compatible = "fsl,imx8mm-tmu", .data = &imx8mm_tmu_data, },
  318. { .compatible = "fsl,imx8mp-tmu", .data = &imx8mp_tmu_data, },
  319. { },
  320. };
  321. MODULE_DEVICE_TABLE(of, imx8mm_tmu_table);
  322. static struct platform_driver imx8mm_tmu = {
  323. .driver = {
  324. .name = "i.mx8mm_thermal",
  325. .of_match_table = imx8mm_tmu_table,
  326. },
  327. .probe = imx8mm_tmu_probe,
  328. .remove = imx8mm_tmu_remove,
  329. };
  330. module_platform_driver(imx8mm_tmu);
  331. MODULE_AUTHOR("Anson Huang <Anson.Huang@nxp.com>");
  332. MODULE_DESCRIPTION("i.MX8MM Thermal Monitor Unit driver");
  333. MODULE_LICENSE("GPL v2");