imx_scu_thermal.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2019 NXP
  4. */
  5. #include <config.h>
  6. #include <common.h>
  7. #include <dm.h>
  8. #include <errno.h>
  9. #include <log.h>
  10. #include <thermal.h>
  11. #include <asm/global_data.h>
  12. #include <dm/device-internal.h>
  13. #include <dm/device.h>
  14. #include <asm/arch/sci/sci.h>
  15. #include <linux/delay.h>
  16. #include <linux/libfdt.h>
  17. DECLARE_GLOBAL_DATA_PTR;
  18. struct imx_sc_thermal_plat {
  19. int critical;
  20. int alert;
  21. int polling_delay;
  22. int id;
  23. bool zone_node;
  24. };
  25. static int read_temperature(struct udevice *dev, int *temp)
  26. {
  27. s16 celsius;
  28. s8 tenths;
  29. int ret;
  30. sc_rsrc_t *sensor_rsrc = (sc_rsrc_t *)dev_get_driver_data(dev);
  31. struct imx_sc_thermal_plat *pdata = dev_get_plat(dev);
  32. if (!temp)
  33. return -EINVAL;
  34. ret = sc_misc_get_temp(-1, sensor_rsrc[pdata->id], SC_C_TEMP,
  35. &celsius, &tenths);
  36. if (ret) {
  37. printf("Error: get temperature failed! (error = %d)\n", ret);
  38. return ret;
  39. }
  40. *temp = celsius * 1000 + tenths * 100;
  41. return 0;
  42. }
  43. int imx_sc_thermal_get_temp(struct udevice *dev, int *temp)
  44. {
  45. struct imx_sc_thermal_plat *pdata = dev_get_plat(dev);
  46. int cpu_temp = 0;
  47. int ret;
  48. ret = read_temperature(dev, &cpu_temp);
  49. if (ret)
  50. return ret;
  51. while (cpu_temp >= pdata->alert) {
  52. printf("CPU Temperature (%dC) beyond alert (%dC), close to critical (%dC)",
  53. cpu_temp, pdata->alert, pdata->critical);
  54. puts(" waiting...\n");
  55. mdelay(pdata->polling_delay);
  56. ret = read_temperature(dev, &cpu_temp);
  57. if (ret)
  58. return ret;
  59. if (cpu_temp >= pdata->alert && !pdata->alert)
  60. break;
  61. }
  62. *temp = cpu_temp / 1000;
  63. return 0;
  64. }
  65. static const struct dm_thermal_ops imx_sc_thermal_ops = {
  66. .get_temp = imx_sc_thermal_get_temp,
  67. };
  68. static int imx_sc_thermal_probe(struct udevice *dev)
  69. {
  70. debug("%s dev name %s\n", __func__, dev->name);
  71. return 0;
  72. }
  73. static int imx_sc_thermal_bind(struct udevice *dev)
  74. {
  75. struct imx_sc_thermal_plat *pdata = dev_get_plat(dev);
  76. int reg, ret;
  77. int offset;
  78. const char *name;
  79. const void *prop;
  80. debug("%s dev name %s\n", __func__, dev->name);
  81. prop = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "compatible",
  82. NULL);
  83. if (!prop)
  84. return 0;
  85. pdata->zone_node = 1;
  86. reg = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), "tsens-num", 0);
  87. if (reg == 0) {
  88. printf("%s: no temp sensor number provided!\n", __func__);
  89. return -EINVAL;
  90. }
  91. offset = fdt_subnode_offset(gd->fdt_blob, 0, "thermal-zones");
  92. fdt_for_each_subnode(offset, gd->fdt_blob, offset) {
  93. /* Bind the subnode to this driver */
  94. name = fdt_get_name(gd->fdt_blob, offset, NULL);
  95. ret = device_bind_with_driver_data(dev, dev->driver, name,
  96. dev->driver_data,
  97. offset_to_ofnode(offset),
  98. NULL);
  99. if (ret)
  100. printf("Error binding driver '%s': %d\n",
  101. dev->driver->name, ret);
  102. }
  103. return 0;
  104. }
  105. static int imx_sc_thermal_of_to_plat(struct udevice *dev)
  106. {
  107. struct imx_sc_thermal_plat *pdata = dev_get_plat(dev);
  108. struct fdtdec_phandle_args args;
  109. const char *type;
  110. int ret;
  111. int trips_np;
  112. debug("%s dev name %s\n", __func__, dev->name);
  113. if (pdata->zone_node)
  114. return 0;
  115. ret = fdtdec_parse_phandle_with_args(gd->fdt_blob, dev_of_offset(dev),
  116. "thermal-sensors",
  117. "#thermal-sensor-cells",
  118. 0, 0, &args);
  119. if (ret)
  120. return ret;
  121. if (args.node != dev_of_offset(dev->parent))
  122. return -EFAULT;
  123. if (args.args_count >= 1)
  124. pdata->id = args.args[0];
  125. else
  126. pdata->id = 0;
  127. debug("args.args_count %d, id %d\n", args.args_count, pdata->id);
  128. pdata->polling_delay = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
  129. "polling-delay", 1000);
  130. trips_np = fdt_subnode_offset(gd->fdt_blob, dev_of_offset(dev),
  131. "trips");
  132. fdt_for_each_subnode(trips_np, gd->fdt_blob, trips_np) {
  133. type = fdt_getprop(gd->fdt_blob, trips_np, "type", NULL);
  134. if (type) {
  135. if (strcmp(type, "critical") == 0) {
  136. pdata->critical = fdtdec_get_int(gd->fdt_blob,
  137. trips_np,
  138. "temperature",
  139. 85);
  140. } else if (strcmp(type, "passive") == 0) {
  141. pdata->alert = fdtdec_get_int(gd->fdt_blob,
  142. trips_np,
  143. "temperature",
  144. 80);
  145. }
  146. }
  147. }
  148. debug("id %d polling_delay %d, critical %d, alert %d\n", pdata->id,
  149. pdata->polling_delay, pdata->critical, pdata->alert);
  150. return 0;
  151. }
  152. static const sc_rsrc_t imx8qm_sensor_rsrc[] = {
  153. SC_R_A53, SC_R_A72, SC_R_GPU_0_PID0, SC_R_GPU_1_PID0,
  154. SC_R_DRC_0, SC_R_DRC_1, SC_R_VPU_PID0, SC_R_PMIC_0,
  155. SC_R_PMIC_1, SC_R_PMIC_2,
  156. };
  157. static const sc_rsrc_t imx8qxp_sensor_rsrc[] = {
  158. SC_R_SYSTEM, SC_R_DRC_0, SC_R_PMIC_0,
  159. SC_R_PMIC_1, SC_R_PMIC_2,
  160. };
  161. static const struct udevice_id imx_sc_thermal_ids[] = {
  162. { .compatible = "nxp,imx8qm-sc-tsens", .data =
  163. (ulong)&imx8qm_sensor_rsrc, },
  164. { .compatible = "nxp,imx8qxp-sc-tsens", .data =
  165. (ulong)&imx8qxp_sensor_rsrc, },
  166. { }
  167. };
  168. U_BOOT_DRIVER(imx_sc_thermal) = {
  169. .name = "imx_sc_thermal",
  170. .id = UCLASS_THERMAL,
  171. .ops = &imx_sc_thermal_ops,
  172. .of_match = imx_sc_thermal_ids,
  173. .bind = imx_sc_thermal_bind,
  174. .probe = imx_sc_thermal_probe,
  175. .of_to_plat = imx_sc_thermal_of_to_plat,
  176. .plat_auto = sizeof(struct imx_sc_thermal_plat),
  177. .flags = DM_FLAG_PRE_RELOC,
  178. };