1 // SPDX-License-Identifier: GPL-2.0+
11 #include <dm/device-internal.h>
12 #include <dm/device.h>
13 #include <asm/arch/sci/sci.h>
14 #include <linux/libfdt.h>
16 DECLARE_GLOBAL_DATA_PTR;
18 struct imx_sc_thermal_plat {
26 static int read_temperature(struct udevice *dev, int *temp)
32 sc_rsrc_t *sensor_rsrc = (sc_rsrc_t *)dev_get_driver_data(dev);
34 struct imx_sc_thermal_plat *pdata = dev_get_platdata(dev);
39 ret = sc_misc_get_temp(-1, sensor_rsrc[pdata->id], SC_C_TEMP,
42 printf("Error: get temperature failed! (error = %d)\n", ret);
46 *temp = celsius * 1000 + tenths * 100;
51 int imx_sc_thermal_get_temp(struct udevice *dev, int *temp)
53 struct imx_sc_thermal_plat *pdata = dev_get_platdata(dev);
57 ret = read_temperature(dev, &cpu_temp);
61 while (cpu_temp >= pdata->alert) {
62 printf("CPU Temperature (%dC) has beyond alert (%dC), close to critical (%dC)",
63 cpu_temp, pdata->alert, pdata->critical);
64 puts(" waiting...\n");
65 mdelay(pdata->polling_delay);
66 ret = read_temperature(dev, &cpu_temp);
71 *temp = cpu_temp / 1000;
76 static const struct dm_thermal_ops imx_sc_thermal_ops = {
77 .get_temp = imx_sc_thermal_get_temp,
80 static int imx_sc_thermal_probe(struct udevice *dev)
82 debug("%s dev name %s\n", __func__, dev->name);
86 static int imx_sc_thermal_bind(struct udevice *dev)
88 struct imx_sc_thermal_plat *pdata = dev_get_platdata(dev);
94 debug("%s dev name %s\n", __func__, dev->name);
96 prop = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "compatible",
101 pdata->zone_node = 1;
103 reg = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), "tsens-num", 0);
105 printf("%s: no temp sensor number provided!\n", __func__);
109 offset = fdt_subnode_offset(gd->fdt_blob, 0, "thermal-zones");
110 fdt_for_each_subnode(offset, gd->fdt_blob, offset) {
111 /* Bind the subnode to this driver */
112 name = fdt_get_name(gd->fdt_blob, offset, NULL);
114 ret = device_bind_with_driver_data(dev, dev->driver, name,
116 offset_to_ofnode(offset),
119 printf("Error binding driver '%s': %d\n",
120 dev->driver->name, ret);
125 static int imx_sc_thermal_ofdata_to_platdata(struct udevice *dev)
127 struct imx_sc_thermal_plat *pdata = dev_get_platdata(dev);
128 struct fdtdec_phandle_args args;
133 debug("%s dev name %s\n", __func__, dev->name);
135 if (pdata->zone_node)
138 ret = fdtdec_parse_phandle_with_args(gd->fdt_blob, dev_of_offset(dev),
140 "#thermal-sensor-cells",
145 if (args.node != dev_of_offset(dev->parent))
148 if (args.args_count >= 1)
149 pdata->id = args.args[0];
153 debug("args.args_count %d, id %d\n", args.args_count, pdata->id);
155 pdata->polling_delay = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
156 "polling-delay", 1000);
158 trips_np = fdt_subnode_offset(gd->fdt_blob, dev_of_offset(dev),
160 fdt_for_each_subnode(trips_np, gd->fdt_blob, trips_np) {
161 type = fdt_getprop(gd->fdt_blob, trips_np, "type", NULL);
163 if (strcmp(type, "critical") == 0) {
164 pdata->critical = fdtdec_get_int(gd->fdt_blob,
168 } else if (strcmp(type, "passive") == 0) {
169 pdata->alert = fdtdec_get_int(gd->fdt_blob,
177 debug("id %d polling_delay %d, critical %d, alert %d\n", pdata->id,
178 pdata->polling_delay, pdata->critical, pdata->alert);
183 static const sc_rsrc_t imx8qm_sensor_rsrc[] = {
184 SC_R_A53, SC_R_A72, SC_R_GPU_0_PID0, SC_R_GPU_1_PID0,
185 SC_R_DRC_0, SC_R_DRC_1, SC_R_VPU_PID0, SC_R_PMIC_0,
186 SC_R_PMIC_1, SC_R_PMIC_2,
189 static const sc_rsrc_t imx8qxp_sensor_rsrc[] = {
190 SC_R_SYSTEM, SC_R_DRC_0, SC_R_PMIC_0,
191 SC_R_PMIC_1, SC_R_PMIC_2,
194 static const struct udevice_id imx_sc_thermal_ids[] = {
195 { .compatible = "nxp,imx8qm-sc-tsens", .data =
196 (ulong)&imx8qm_sensor_rsrc, },
197 { .compatible = "nxp,imx8qxp-sc-tsens", .data =
198 (ulong)&imx8qxp_sensor_rsrc, },
202 U_BOOT_DRIVER(imx_sc_thermal) = {
203 .name = "imx_sc_thermal",
204 .id = UCLASS_THERMAL,
205 .ops = &imx_sc_thermal_ops,
206 .of_match = imx_sc_thermal_ids,
207 .bind = imx_sc_thermal_bind,
208 .probe = imx_sc_thermal_probe,
209 .ofdata_to_platdata = imx_sc_thermal_ofdata_to_platdata,
210 .platdata_auto_alloc_size = sizeof(struct imx_sc_thermal_plat),
211 .flags = DM_FLAG_PRE_RELOC,