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