blob: 2d3a363a20c01a34c4041c6b6203cc6b970329fd [file] [log] [blame]
Peng Fan5ef5b6d2019-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 Glassf7ae49f2020-05-10 11:40:05 -060010#include <log.h>
Peng Fan5ef5b6d2019-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 Glass4d72caa2020-05-10 11:40:01 -060015#include <linux/libfdt.h>
Peng Fan5ef5b6d2019-05-05 13:23:54 +000016
17DECLARE_GLOBAL_DATA_PTR;
18
19struct imx_sc_thermal_plat {
20 int critical;
21 int alert;
22 int polling_delay;
23 int id;
24 bool zone_node;
25};
26
27static int read_temperature(struct udevice *dev, int *temp)
28{
29 s16 celsius;
30 s8 tenths;
31 int ret;
32
33 sc_rsrc_t *sensor_rsrc = (sc_rsrc_t *)dev_get_driver_data(dev);
34
35 struct imx_sc_thermal_plat *pdata = dev_get_platdata(dev);
36
37 if (!temp)
38 return -EINVAL;
39
40 ret = sc_misc_get_temp(-1, sensor_rsrc[pdata->id], SC_C_TEMP,
41 &celsius, &tenths);
42 if (ret) {
43 printf("Error: get temperature failed! (error = %d)\n", ret);
44 return ret;
45 }
46
47 *temp = celsius * 1000 + tenths * 100;
48
49 return 0;
50}
51
52int imx_sc_thermal_get_temp(struct udevice *dev, int *temp)
53{
54 struct imx_sc_thermal_plat *pdata = dev_get_platdata(dev);
55 int cpu_temp = 0;
56 int ret;
57
58 ret = read_temperature(dev, &cpu_temp);
59 if (ret)
60 return ret;
61
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);
68 if (ret)
69 return ret;
70 }
71
72 *temp = cpu_temp / 1000;
73
74 return 0;
75}
76
77static const struct dm_thermal_ops imx_sc_thermal_ops = {
78 .get_temp = imx_sc_thermal_get_temp,
79};
80
81static int imx_sc_thermal_probe(struct udevice *dev)
82{
83 debug("%s dev name %s\n", __func__, dev->name);
84 return 0;
85}
86
87static int imx_sc_thermal_bind(struct udevice *dev)
88{
89 struct imx_sc_thermal_plat *pdata = dev_get_platdata(dev);
90 int reg, ret;
91 int offset;
92 const char *name;
93 const void *prop;
94
95 debug("%s dev name %s\n", __func__, dev->name);
96
97 prop = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "compatible",
98 NULL);
99 if (!prop)
100 return 0;
101
102 pdata->zone_node = 1;
103
104 reg = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), "tsens-num", 0);
105 if (reg == 0) {
106 printf("%s: no temp sensor number provided!\n", __func__);
107 return -EINVAL;
108 }
109
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);
114
115 ret = device_bind_with_driver_data(dev, dev->driver, name,
116 dev->driver_data,
117 offset_to_ofnode(offset),
118 NULL);
119 if (ret)
120 printf("Error binding driver '%s': %d\n",
121 dev->driver->name, ret);
122 }
123 return 0;
124}
125
126static int imx_sc_thermal_ofdata_to_platdata(struct udevice *dev)
127{
128 struct imx_sc_thermal_plat *pdata = dev_get_platdata(dev);
129 struct fdtdec_phandle_args args;
130 const char *type;
131 int ret;
132 int trips_np;
133
134 debug("%s dev name %s\n", __func__, dev->name);
135
136 if (pdata->zone_node)
137 return 0;
138
139 ret = fdtdec_parse_phandle_with_args(gd->fdt_blob, dev_of_offset(dev),
140 "thermal-sensors",
141 "#thermal-sensor-cells",
142 0, 0, &args);
143 if (ret)
144 return ret;
145
146 if (args.node != dev_of_offset(dev->parent))
147 return -EFAULT;
148
149 if (args.args_count >= 1)
150 pdata->id = args.args[0];
151 else
152 pdata->id = 0;
153
154 debug("args.args_count %d, id %d\n", args.args_count, pdata->id);
155
156 pdata->polling_delay = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
157 "polling-delay", 1000);
158
159 trips_np = fdt_subnode_offset(gd->fdt_blob, dev_of_offset(dev),
160 "trips");
161 fdt_for_each_subnode(trips_np, gd->fdt_blob, trips_np) {
162 type = fdt_getprop(gd->fdt_blob, trips_np, "type", NULL);
163 if (type) {
164 if (strcmp(type, "critical") == 0) {
165 pdata->critical = fdtdec_get_int(gd->fdt_blob,
166 trips_np,
167 "temperature",
168 85);
169 } else if (strcmp(type, "passive") == 0) {
170 pdata->alert = fdtdec_get_int(gd->fdt_blob,
171 trips_np,
172 "temperature",
173 80);
174 }
175 }
176 }
177
178 debug("id %d polling_delay %d, critical %d, alert %d\n", pdata->id,
179 pdata->polling_delay, pdata->critical, pdata->alert);
180
181 return 0;
182}
183
Ye Li6039d662020-05-03 22:19:45 +0800184static 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,
188};
189
Peng Fan5ef5b6d2019-05-05 13:23:54 +0000190static 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,
193};
194
195static const struct udevice_id imx_sc_thermal_ids[] = {
Ye Li6039d662020-05-03 22:19:45 +0800196 { .compatible = "nxp,imx8qm-sc-tsens", .data =
197 (ulong)&imx8qm_sensor_rsrc, },
Peng Fan5ef5b6d2019-05-05 13:23:54 +0000198 { .compatible = "nxp,imx8qxp-sc-tsens", .data =
199 (ulong)&imx8qxp_sensor_rsrc, },
200 { }
201};
202
203U_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,
213};