1 // SPDX-License-Identifier: GPL-2.0+
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>
18 DECLARE_GLOBAL_DATA_PTR;
20 struct imx_sc_thermal_plat {
28 static int read_temperature(struct udevice *dev, int *temp)
34 sc_rsrc_t *sensor_rsrc = (sc_rsrc_t *)dev_get_driver_data(dev);
36 struct imx_sc_thermal_plat *pdata = dev_get_platdata(dev);
41 ret = sc_misc_get_temp(-1, sensor_rsrc[pdata->id], SC_C_TEMP,
44 printf("Error: get temperature failed! (error = %d)\n", ret);
48 *temp = celsius * 1000 + tenths * 100;
53 int imx_sc_thermal_get_temp(struct udevice *dev, int *temp)
55 struct imx_sc_thermal_plat *pdata = dev_get_platdata(dev);
59 ret = read_temperature(dev, &cpu_temp);
63 while (cpu_temp >= pdata->alert) {
64 printf("CPU Temperature (%dC) beyond alert (%dC), close to critical (%dC)",
65 cpu_temp, pdata->alert, pdata->critical);
66 puts(" waiting...\n");
67 mdelay(pdata->polling_delay);
68 ret = read_temperature(dev, &cpu_temp);
71 if (cpu_temp >= pdata->alert && !pdata->alert)
75 *temp = cpu_temp / 1000;
80 static const struct dm_thermal_ops imx_sc_thermal_ops = {
81 .get_temp = imx_sc_thermal_get_temp,
84 static int imx_sc_thermal_probe(struct udevice *dev)
86 debug("%s dev name %s\n", __func__, dev->name);
90 static int imx_sc_thermal_bind(struct udevice *dev)
92 struct imx_sc_thermal_plat *pdata = dev_get_platdata(dev);
98 debug("%s dev name %s\n", __func__, dev->name);
100 prop = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "compatible",
105 pdata->zone_node = 1;
107 reg = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), "tsens-num", 0);
109 printf("%s: no temp sensor number provided!\n", __func__);
113 offset = fdt_subnode_offset(gd->fdt_blob, 0, "thermal-zones");
114 fdt_for_each_subnode(offset, gd->fdt_blob, offset) {
115 /* Bind the subnode to this driver */
116 name = fdt_get_name(gd->fdt_blob, offset, NULL);
118 ret = device_bind_with_driver_data(dev, dev->driver, name,
120 offset_to_ofnode(offset),
123 printf("Error binding driver '%s': %d\n",
124 dev->driver->name, ret);
129 static int imx_sc_thermal_ofdata_to_platdata(struct udevice *dev)
131 struct imx_sc_thermal_plat *pdata = dev_get_platdata(dev);
132 struct fdtdec_phandle_args args;
137 debug("%s dev name %s\n", __func__, dev->name);
139 if (pdata->zone_node)
142 ret = fdtdec_parse_phandle_with_args(gd->fdt_blob, dev_of_offset(dev),
144 "#thermal-sensor-cells",
149 if (args.node != dev_of_offset(dev->parent))
152 if (args.args_count >= 1)
153 pdata->id = args.args[0];
157 debug("args.args_count %d, id %d\n", args.args_count, pdata->id);
159 pdata->polling_delay = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
160 "polling-delay", 1000);
162 trips_np = fdt_subnode_offset(gd->fdt_blob, dev_of_offset(dev),
164 fdt_for_each_subnode(trips_np, gd->fdt_blob, trips_np) {
165 type = fdt_getprop(gd->fdt_blob, trips_np, "type", NULL);
167 if (strcmp(type, "critical") == 0) {
168 pdata->critical = fdtdec_get_int(gd->fdt_blob,
172 } else if (strcmp(type, "passive") == 0) {
173 pdata->alert = fdtdec_get_int(gd->fdt_blob,
181 debug("id %d polling_delay %d, critical %d, alert %d\n", pdata->id,
182 pdata->polling_delay, pdata->critical, pdata->alert);
187 static const sc_rsrc_t imx8qm_sensor_rsrc[] = {
188 SC_R_A53, SC_R_A72, SC_R_GPU_0_PID0, SC_R_GPU_1_PID0,
189 SC_R_DRC_0, SC_R_DRC_1, SC_R_VPU_PID0, SC_R_PMIC_0,
190 SC_R_PMIC_1, SC_R_PMIC_2,
193 static const sc_rsrc_t imx8qxp_sensor_rsrc[] = {
194 SC_R_SYSTEM, SC_R_DRC_0, SC_R_PMIC_0,
195 SC_R_PMIC_1, SC_R_PMIC_2,
198 static const struct udevice_id imx_sc_thermal_ids[] = {
199 { .compatible = "nxp,imx8qm-sc-tsens", .data =
200 (ulong)&imx8qm_sensor_rsrc, },
201 { .compatible = "nxp,imx8qxp-sc-tsens", .data =
202 (ulong)&imx8qxp_sensor_rsrc, },
206 U_BOOT_DRIVER(imx_sc_thermal) = {
207 .name = "imx_sc_thermal",
208 .id = UCLASS_THERMAL,
209 .ops = &imx_sc_thermal_ops,
210 .of_match = imx_sc_thermal_ids,
211 .bind = imx_sc_thermal_bind,
212 .probe = imx_sc_thermal_probe,
213 .ofdata_to_platdata = imx_sc_thermal_ofdata_to_platdata,
214 .platdata_auto_alloc_size = sizeof(struct imx_sc_thermal_plat),
215 .flags = DM_FLAG_PRE_RELOC,