1 // SPDX-License-Identifier: GPL-2.0+
12 #include <asm/global_data.h>
13 #include <dm/device-internal.h>
14 #include <dm/device.h>
15 #include <firmware/imx/sci/sci.h>
16 #include <linux/delay.h>
17 #include <linux/libfdt.h>
19 DECLARE_GLOBAL_DATA_PTR;
21 struct imx_sc_thermal_plat {
29 static int read_temperature(struct udevice *dev, int *temp)
35 sc_rsrc_t *sensor_rsrc = (sc_rsrc_t *)dev_get_driver_data(dev);
37 struct imx_sc_thermal_plat *pdata = dev_get_plat(dev);
42 ret = sc_misc_get_temp(-1, sensor_rsrc[pdata->id], SC_C_TEMP,
45 printf("Error: get temperature failed! (error = %d)\n", ret);
49 *temp = celsius * 1000 + tenths * 100;
54 int imx_sc_thermal_get_temp(struct udevice *dev, int *temp)
56 struct imx_sc_thermal_plat *pdata = dev_get_plat(dev);
60 ret = read_temperature(dev, &cpu_temp);
64 while (cpu_temp >= pdata->alert) {
65 printf("CPU Temperature (%dC) beyond alert (%dC), close to critical (%dC)",
66 cpu_temp, pdata->alert, pdata->critical);
67 puts(" waiting...\n");
68 mdelay(pdata->polling_delay);
69 ret = read_temperature(dev, &cpu_temp);
72 if (cpu_temp >= pdata->alert && !pdata->alert)
76 *temp = cpu_temp / 1000;
81 static const struct dm_thermal_ops imx_sc_thermal_ops = {
82 .get_temp = imx_sc_thermal_get_temp,
85 static int imx_sc_thermal_probe(struct udevice *dev)
87 debug("%s dev name %s\n", __func__, dev->name);
91 static int imx_sc_thermal_bind(struct udevice *dev)
93 struct imx_sc_thermal_plat *pdata = dev_get_plat(dev);
99 debug("%s dev name %s\n", __func__, dev->name);
101 prop = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "compatible",
106 pdata->zone_node = 1;
108 reg = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), "tsens-num", 0);
110 printf("%s: no temp sensor number provided!\n", __func__);
114 offset = fdt_subnode_offset(gd->fdt_blob, 0, "thermal-zones");
115 fdt_for_each_subnode(offset, gd->fdt_blob, offset) {
116 /* Bind the subnode to this driver */
117 name = fdt_get_name(gd->fdt_blob, offset, NULL);
119 ret = device_bind_with_driver_data(dev, dev->driver, name,
121 offset_to_ofnode(offset),
124 printf("Error binding driver '%s': %d\n",
125 dev->driver->name, ret);
130 static int imx_sc_thermal_of_to_plat(struct udevice *dev)
132 struct imx_sc_thermal_plat *pdata = dev_get_plat(dev);
133 struct fdtdec_phandle_args args;
138 debug("%s dev name %s\n", __func__, dev->name);
140 if (pdata->zone_node)
143 ret = fdtdec_parse_phandle_with_args(gd->fdt_blob, dev_of_offset(dev),
145 "#thermal-sensor-cells",
150 if (args.node != dev_of_offset(dev->parent))
153 if (args.args_count >= 1)
154 pdata->id = args.args[0];
158 debug("args.args_count %d, id %d\n", args.args_count, pdata->id);
160 pdata->polling_delay = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
161 "polling-delay", 1000);
163 trips_np = fdt_subnode_offset(gd->fdt_blob, dev_of_offset(dev),
165 fdt_for_each_subnode(trips_np, gd->fdt_blob, trips_np) {
166 type = fdt_getprop(gd->fdt_blob, trips_np, "type", NULL);
168 if (strcmp(type, "critical") == 0) {
169 pdata->critical = fdtdec_get_int(gd->fdt_blob,
173 } else if (strcmp(type, "passive") == 0) {
174 pdata->alert = fdtdec_get_int(gd->fdt_blob,
182 debug("id %d polling_delay %d, critical %d, alert %d\n", pdata->id,
183 pdata->polling_delay, pdata->critical, pdata->alert);
188 static const sc_rsrc_t imx8qm_sensor_rsrc[] = {
189 SC_R_A53, SC_R_A72, SC_R_GPU_0_PID0, SC_R_GPU_1_PID0,
190 SC_R_DRC_0, SC_R_DRC_1, SC_R_VPU_PID0, SC_R_PMIC_0,
191 SC_R_PMIC_1, SC_R_PMIC_2,
194 static const sc_rsrc_t imx8qxp_sensor_rsrc[] = {
195 SC_R_SYSTEM, SC_R_DRC_0, SC_R_PMIC_0,
196 SC_R_PMIC_1, SC_R_PMIC_2,
199 static const struct udevice_id imx_sc_thermal_ids[] = {
200 { .compatible = "nxp,imx8qm-sc-tsens", .data =
201 (ulong)&imx8qm_sensor_rsrc, },
202 { .compatible = "nxp,imx8qxp-sc-tsens", .data =
203 (ulong)&imx8qxp_sensor_rsrc, },
207 U_BOOT_DRIVER(imx_sc_thermal) = {
208 .name = "imx_sc_thermal",
209 .id = UCLASS_THERMAL,
210 .ops = &imx_sc_thermal_ops,
211 .of_match = imx_sc_thermal_ids,
212 .bind = imx_sc_thermal_bind,
213 .probe = imx_sc_thermal_probe,
214 .of_to_plat = imx_sc_thermal_of_to_plat,
215 .plat_auto = sizeof(struct imx_sc_thermal_plat),
216 .flags = DM_FLAG_PRE_RELOC,