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) has 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);
73 *temp = cpu_temp / 1000;
78 static const struct dm_thermal_ops imx_sc_thermal_ops = {
79 .get_temp = imx_sc_thermal_get_temp,
82 static int imx_sc_thermal_probe(struct udevice *dev)
84 debug("%s dev name %s\n", __func__, dev->name);
88 static int imx_sc_thermal_bind(struct udevice *dev)
90 struct imx_sc_thermal_plat *pdata = dev_get_platdata(dev);
96 debug("%s dev name %s\n", __func__, dev->name);
98 prop = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "compatible",
103 pdata->zone_node = 1;
105 reg = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), "tsens-num", 0);
107 printf("%s: no temp sensor number provided!\n", __func__);
111 offset = fdt_subnode_offset(gd->fdt_blob, 0, "thermal-zones");
112 fdt_for_each_subnode(offset, gd->fdt_blob, offset) {
113 /* Bind the subnode to this driver */
114 name = fdt_get_name(gd->fdt_blob, offset, NULL);
116 ret = device_bind_with_driver_data(dev, dev->driver, name,
118 offset_to_ofnode(offset),
121 printf("Error binding driver '%s': %d\n",
122 dev->driver->name, ret);
127 static int imx_sc_thermal_ofdata_to_platdata(struct udevice *dev)
129 struct imx_sc_thermal_plat *pdata = dev_get_platdata(dev);
130 struct fdtdec_phandle_args args;
135 debug("%s dev name %s\n", __func__, dev->name);
137 if (pdata->zone_node)
140 ret = fdtdec_parse_phandle_with_args(gd->fdt_blob, dev_of_offset(dev),
142 "#thermal-sensor-cells",
147 if (args.node != dev_of_offset(dev->parent))
150 if (args.args_count >= 1)
151 pdata->id = args.args[0];
155 debug("args.args_count %d, id %d\n", args.args_count, pdata->id);
157 pdata->polling_delay = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
158 "polling-delay", 1000);
160 trips_np = fdt_subnode_offset(gd->fdt_blob, dev_of_offset(dev),
162 fdt_for_each_subnode(trips_np, gd->fdt_blob, trips_np) {
163 type = fdt_getprop(gd->fdt_blob, trips_np, "type", NULL);
165 if (strcmp(type, "critical") == 0) {
166 pdata->critical = fdtdec_get_int(gd->fdt_blob,
170 } else if (strcmp(type, "passive") == 0) {
171 pdata->alert = fdtdec_get_int(gd->fdt_blob,
179 debug("id %d polling_delay %d, critical %d, alert %d\n", pdata->id,
180 pdata->polling_delay, pdata->critical, pdata->alert);
185 static const sc_rsrc_t imx8qm_sensor_rsrc[] = {
186 SC_R_A53, SC_R_A72, SC_R_GPU_0_PID0, SC_R_GPU_1_PID0,
187 SC_R_DRC_0, SC_R_DRC_1, SC_R_VPU_PID0, SC_R_PMIC_0,
188 SC_R_PMIC_1, SC_R_PMIC_2,
191 static const sc_rsrc_t imx8qxp_sensor_rsrc[] = {
192 SC_R_SYSTEM, SC_R_DRC_0, SC_R_PMIC_0,
193 SC_R_PMIC_1, SC_R_PMIC_2,
196 static const struct udevice_id imx_sc_thermal_ids[] = {
197 { .compatible = "nxp,imx8qm-sc-tsens", .data =
198 (ulong)&imx8qm_sensor_rsrc, },
199 { .compatible = "nxp,imx8qxp-sc-tsens", .data =
200 (ulong)&imx8qxp_sensor_rsrc, },
204 U_BOOT_DRIVER(imx_sc_thermal) = {
205 .name = "imx_sc_thermal",
206 .id = UCLASS_THERMAL,
207 .ops = &imx_sc_thermal_ops,
208 .of_match = imx_sc_thermal_ids,
209 .bind = imx_sc_thermal_bind,
210 .probe = imx_sc_thermal_probe,
211 .ofdata_to_platdata = imx_sc_thermal_ofdata_to_platdata,
212 .platdata_auto_alloc_size = sizeof(struct imx_sc_thermal_plat),
213 .flags = DM_FLAG_PRE_RELOC,