common: Drop linux/delay.h from common header
[oweals/u-boot.git] / drivers / thermal / imx_scu_thermal.c
1 // 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>
10 #include <log.h>
11 #include <thermal.h>
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>
17
18 DECLARE_GLOBAL_DATA_PTR;
19
20 struct imx_sc_thermal_plat {
21         int critical;
22         int alert;
23         int polling_delay;
24         int id;
25         bool zone_node;
26 };
27
28 static 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
53 int 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) {
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);
69                 if (ret)
70                         return ret;
71         }
72
73         *temp = cpu_temp / 1000;
74
75         return 0;
76 }
77
78 static const struct dm_thermal_ops imx_sc_thermal_ops = {
79         .get_temp       = imx_sc_thermal_get_temp,
80 };
81
82 static int imx_sc_thermal_probe(struct udevice *dev)
83 {
84         debug("%s dev name %s\n", __func__, dev->name);
85         return 0;
86 }
87
88 static int imx_sc_thermal_bind(struct udevice *dev)
89 {
90         struct imx_sc_thermal_plat *pdata = dev_get_platdata(dev);
91         int reg, ret;
92         int offset;
93         const char *name;
94         const void *prop;
95
96         debug("%s dev name %s\n", __func__, dev->name);
97
98         prop = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "compatible",
99                            NULL);
100         if (!prop)
101                 return 0;
102
103         pdata->zone_node = 1;
104
105         reg = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), "tsens-num", 0);
106         if (reg == 0) {
107                 printf("%s: no temp sensor number provided!\n", __func__);
108                 return -EINVAL;
109         }
110
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);
115
116                 ret = device_bind_with_driver_data(dev, dev->driver, name,
117                                                    dev->driver_data,
118                                                    offset_to_ofnode(offset),
119                                                    NULL);
120                 if (ret)
121                         printf("Error binding driver '%s': %d\n",
122                                dev->driver->name, ret);
123         }
124         return 0;
125 }
126
127 static int imx_sc_thermal_ofdata_to_platdata(struct udevice *dev)
128 {
129         struct imx_sc_thermal_plat *pdata = dev_get_platdata(dev);
130         struct fdtdec_phandle_args args;
131         const char *type;
132         int ret;
133         int trips_np;
134
135         debug("%s dev name %s\n", __func__, dev->name);
136
137         if (pdata->zone_node)
138                 return 0;
139
140         ret = fdtdec_parse_phandle_with_args(gd->fdt_blob, dev_of_offset(dev),
141                                              "thermal-sensors",
142                                              "#thermal-sensor-cells",
143                                              0, 0, &args);
144         if (ret)
145                 return ret;
146
147         if (args.node != dev_of_offset(dev->parent))
148                 return -EFAULT;
149
150         if (args.args_count >= 1)
151                 pdata->id = args.args[0];
152         else
153                 pdata->id = 0;
154
155         debug("args.args_count %d, id %d\n", args.args_count, pdata->id);
156
157         pdata->polling_delay = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
158                                               "polling-delay", 1000);
159
160         trips_np = fdt_subnode_offset(gd->fdt_blob, dev_of_offset(dev),
161                                       "trips");
162         fdt_for_each_subnode(trips_np, gd->fdt_blob, trips_np) {
163                 type = fdt_getprop(gd->fdt_blob, trips_np, "type", NULL);
164                 if (type) {
165                         if (strcmp(type, "critical") == 0) {
166                                 pdata->critical = fdtdec_get_int(gd->fdt_blob,
167                                                                  trips_np,
168                                                                  "temperature",
169                                                                  85);
170                         } else if (strcmp(type, "passive") == 0) {
171                                 pdata->alert = fdtdec_get_int(gd->fdt_blob,
172                                                               trips_np,
173                                                               "temperature",
174                                                               80);
175                         }
176                 }
177         }
178
179         debug("id %d polling_delay %d, critical %d, alert %d\n", pdata->id,
180               pdata->polling_delay, pdata->critical, pdata->alert);
181
182         return 0;
183 }
184
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,
189 };
190
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,
194 };
195
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, },
201         { }
202 };
203
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,
214 };