imx_scu_thermal.c 5.0 KB

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