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/libfdt.h>
17 DECLARE_GLOBAL_DATA_PTR;
19 struct imx_sc_thermal_plat {
27 static int read_temperature(struct udevice *dev, int *temp)
33 sc_rsrc_t *sensor_rsrc = (sc_rsrc_t *)dev_get_driver_data(dev);
35 struct imx_sc_thermal_plat *pdata = dev_get_platdata(dev);
40 ret = sc_misc_get_temp(-1, sensor_rsrc[pdata->id], SC_C_TEMP,
43 printf("Error: get temperature failed! (error = %d)\n", ret);
47 *temp = celsius * 1000 + tenths * 100;
52 int imx_sc_thermal_get_temp(struct udevice *dev, int *temp)
54 struct imx_sc_thermal_plat *pdata = dev_get_platdata(dev);
58 ret = read_temperature(dev, &cpu_temp);
62 while (cpu_temp >= pdata->alert) {
63 printf("CPU Temperature (%dC) has beyond alert (%dC), close to critical (%dC)",
64 cpu_temp, pdata->alert, pdata->critical);
65 puts(" waiting...\n");
66 mdelay(pdata->polling_delay);
67 ret = read_temperature(dev, &cpu_temp);
72 *temp = cpu_temp / 1000;
77 static const struct dm_thermal_ops imx_sc_thermal_ops = {
78 .get_temp = imx_sc_thermal_get_temp,
81 static int imx_sc_thermal_probe(struct udevice *dev)
83 debug("%s dev name %s\n", __func__, dev->name);
87 static int imx_sc_thermal_bind(struct udevice *dev)
89 struct imx_sc_thermal_plat *pdata = dev_get_platdata(dev);
95 debug("%s dev name %s\n", __func__, dev->name);
97 prop = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "compatible",
102 pdata->zone_node = 1;
104 reg = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), "tsens-num", 0);
106 printf("%s: no temp sensor number provided!\n", __func__);
110 offset = fdt_subnode_offset(gd->fdt_blob, 0, "thermal-zones");
111 fdt_for_each_subnode(offset, gd->fdt_blob, offset) {
112 /* Bind the subnode to this driver */
113 name = fdt_get_name(gd->fdt_blob, offset, NULL);
115 ret = device_bind_with_driver_data(dev, dev->driver, name,
117 offset_to_ofnode(offset),
120 printf("Error binding driver '%s': %d\n",
121 dev->driver->name, ret);
126 static int imx_sc_thermal_ofdata_to_platdata(struct udevice *dev)
128 struct imx_sc_thermal_plat *pdata = dev_get_platdata(dev);
129 struct fdtdec_phandle_args args;
134 debug("%s dev name %s\n", __func__, dev->name);
136 if (pdata->zone_node)
139 ret = fdtdec_parse_phandle_with_args(gd->fdt_blob, dev_of_offset(dev),
141 "#thermal-sensor-cells",
146 if (args.node != dev_of_offset(dev->parent))
149 if (args.args_count >= 1)
150 pdata->id = args.args[0];
154 debug("args.args_count %d, id %d\n", args.args_count, pdata->id);
156 pdata->polling_delay = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
157 "polling-delay", 1000);
159 trips_np = fdt_subnode_offset(gd->fdt_blob, dev_of_offset(dev),
161 fdt_for_each_subnode(trips_np, gd->fdt_blob, trips_np) {
162 type = fdt_getprop(gd->fdt_blob, trips_np, "type", NULL);
164 if (strcmp(type, "critical") == 0) {
165 pdata->critical = fdtdec_get_int(gd->fdt_blob,
169 } else if (strcmp(type, "passive") == 0) {
170 pdata->alert = fdtdec_get_int(gd->fdt_blob,
178 debug("id %d polling_delay %d, critical %d, alert %d\n", pdata->id,
179 pdata->polling_delay, pdata->critical, pdata->alert);
184 static const sc_rsrc_t imx8qm_sensor_rsrc[] = {
185 SC_R_A53, SC_R_A72, SC_R_GPU_0_PID0, SC_R_GPU_1_PID0,
186 SC_R_DRC_0, SC_R_DRC_1, SC_R_VPU_PID0, SC_R_PMIC_0,
187 SC_R_PMIC_1, SC_R_PMIC_2,
190 static const sc_rsrc_t imx8qxp_sensor_rsrc[] = {
191 SC_R_SYSTEM, SC_R_DRC_0, SC_R_PMIC_0,
192 SC_R_PMIC_1, SC_R_PMIC_2,
195 static const struct udevice_id imx_sc_thermal_ids[] = {
196 { .compatible = "nxp,imx8qm-sc-tsens", .data =
197 (ulong)&imx8qm_sensor_rsrc, },
198 { .compatible = "nxp,imx8qxp-sc-tsens", .data =
199 (ulong)&imx8qxp_sensor_rsrc, },
203 U_BOOT_DRIVER(imx_sc_thermal) = {
204 .name = "imx_sc_thermal",
205 .id = UCLASS_THERMAL,
206 .ops = &imx_sc_thermal_ops,
207 .of_match = imx_sc_thermal_ids,
208 .bind = imx_sc_thermal_bind,
209 .probe = imx_sc_thermal_probe,
210 .ofdata_to_platdata = imx_sc_thermal_ofdata_to_platdata,
211 .platdata_auto_alloc_size = sizeof(struct imx_sc_thermal_plat),
212 .flags = DM_FLAG_PRE_RELOC,