blob: 83ec753802acc0e99d016bad41f349ef221f9df2 [file] [log] [blame]
Peng Fan8a021112019-05-05 13:23:54 +00001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2019 NXP
4 */
5
6#include <config.h>
7#include <common.h>
8#include <dm.h>
9#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Peng Fan8a021112019-05-05 13:23:54 +000011#include <thermal.h>
12#include <dm/device-internal.h>
13#include <dm/device.h>
14#include <asm/arch/sci/sci.h>
Simon Glassdbd79542020-05-10 11:40:11 -060015#include <linux/delay.h>
Simon Glass2dc9c342020-05-10 11:40:01 -060016#include <linux/libfdt.h>
Peng Fan8a021112019-05-05 13:23:54 +000017
18DECLARE_GLOBAL_DATA_PTR;
19
20struct imx_sc_thermal_plat {
21 int critical;
22 int alert;
23 int polling_delay;
24 int id;
25 bool zone_node;
26};
27
28static int read_temperature(struct udevice *dev, int *temp)
29{
30 s16 celsius;
31 s8 tenths;
32 int ret;
33
34 sc_rsrc_t *sensor_rsrc = (sc_rsrc_t *)dev_get_driver_data(dev);
35
36 struct imx_sc_thermal_plat *pdata = dev_get_platdata(dev);
37
38 if (!temp)
39 return -EINVAL;
40
41 ret = sc_misc_get_temp(-1, sensor_rsrc[pdata->id], SC_C_TEMP,
42 &celsius, &tenths);
43 if (ret) {
44 printf("Error: get temperature failed! (error = %d)\n", ret);
45 return ret;
46 }
47
48 *temp = celsius * 1000 + tenths * 100;
49
50 return 0;
51}
52
53int imx_sc_thermal_get_temp(struct udevice *dev, int *temp)
54{
55 struct imx_sc_thermal_plat *pdata = dev_get_platdata(dev);
56 int cpu_temp = 0;
57 int ret;
58
59 ret = read_temperature(dev, &cpu_temp);
60 if (ret)
61 return ret;
62
63 while (cpu_temp >= pdata->alert) {
Anatolij Gustschinf2aed092020-05-20 10:21:53 +020064 printf("CPU Temperature (%dC) beyond alert (%dC), close to critical (%dC)",
Peng Fan8a021112019-05-05 13:23:54 +000065 cpu_temp, pdata->alert, pdata->critical);
66 puts(" waiting...\n");
67 mdelay(pdata->polling_delay);
68 ret = read_temperature(dev, &cpu_temp);
69 if (ret)
70 return ret;
Anatolij Gustschinf2aed092020-05-20 10:21:53 +020071 if (cpu_temp >= pdata->alert && !pdata->alert)
72 break;
Peng Fan8a021112019-05-05 13:23:54 +000073 }
74
75 *temp = cpu_temp / 1000;
76
77 return 0;
78}
79
80static const struct dm_thermal_ops imx_sc_thermal_ops = {
81 .get_temp = imx_sc_thermal_get_temp,
82};
83
84static int imx_sc_thermal_probe(struct udevice *dev)
85{
86 debug("%s dev name %s\n", __func__, dev->name);
87 return 0;
88}
89
90static int imx_sc_thermal_bind(struct udevice *dev)
91{
92 struct imx_sc_thermal_plat *pdata = dev_get_platdata(dev);
93 int reg, ret;
94 int offset;
95 const char *name;
96 const void *prop;
97
98 debug("%s dev name %s\n", __func__, dev->name);
99
100 prop = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "compatible",
101 NULL);
102 if (!prop)
103 return 0;
104
105 pdata->zone_node = 1;
106
107 reg = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), "tsens-num", 0);
108 if (reg == 0) {
109 printf("%s: no temp sensor number provided!\n", __func__);
110 return -EINVAL;
111 }
112
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);
117
118 ret = device_bind_with_driver_data(dev, dev->driver, name,
119 dev->driver_data,
120 offset_to_ofnode(offset),
121 NULL);
122 if (ret)
123 printf("Error binding driver '%s': %d\n",
124 dev->driver->name, ret);
125 }
126 return 0;
127}
128
129static int imx_sc_thermal_ofdata_to_platdata(struct udevice *dev)
130{
131 struct imx_sc_thermal_plat *pdata = dev_get_platdata(dev);
132 struct fdtdec_phandle_args args;
133 const char *type;
134 int ret;
135 int trips_np;
136
137 debug("%s dev name %s\n", __func__, dev->name);
138
139 if (pdata->zone_node)
140 return 0;
141
142 ret = fdtdec_parse_phandle_with_args(gd->fdt_blob, dev_of_offset(dev),
143 "thermal-sensors",
144 "#thermal-sensor-cells",
145 0, 0, &args);
146 if (ret)
147 return ret;
148
149 if (args.node != dev_of_offset(dev->parent))
150 return -EFAULT;
151
152 if (args.args_count >= 1)
153 pdata->id = args.args[0];
154 else
155 pdata->id = 0;
156
157 debug("args.args_count %d, id %d\n", args.args_count, pdata->id);
158
159 pdata->polling_delay = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
160 "polling-delay", 1000);
161
162 trips_np = fdt_subnode_offset(gd->fdt_blob, dev_of_offset(dev),
163 "trips");
164 fdt_for_each_subnode(trips_np, gd->fdt_blob, trips_np) {
165 type = fdt_getprop(gd->fdt_blob, trips_np, "type", NULL);
166 if (type) {
167 if (strcmp(type, "critical") == 0) {
168 pdata->critical = fdtdec_get_int(gd->fdt_blob,
169 trips_np,
170 "temperature",
171 85);
172 } else if (strcmp(type, "passive") == 0) {
173 pdata->alert = fdtdec_get_int(gd->fdt_blob,
174 trips_np,
175 "temperature",
176 80);
177 }
178 }
179 }
180
181 debug("id %d polling_delay %d, critical %d, alert %d\n", pdata->id,
182 pdata->polling_delay, pdata->critical, pdata->alert);
183
184 return 0;
185}
186
Ye Li4489a2e2020-05-03 22:19:45 +0800187static 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,
191};
192
Peng Fan8a021112019-05-05 13:23:54 +0000193static 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,
196};
197
198static const struct udevice_id imx_sc_thermal_ids[] = {
Ye Li4489a2e2020-05-03 22:19:45 +0800199 { .compatible = "nxp,imx8qm-sc-tsens", .data =
200 (ulong)&imx8qm_sensor_rsrc, },
Peng Fan8a021112019-05-05 13:23:54 +0000201 { .compatible = "nxp,imx8qxp-sc-tsens", .data =
202 (ulong)&imx8qxp_sensor_rsrc, },
203 { }
204};
205
206U_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,
216};