1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * OMAP thermal driver interface
4  *
5  * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com/
6  * Contact:
7  *   Eduardo Valentin <eduardo.valentin@ti.com>
8  */
9 
10 #include <linux/device.h>
11 #include <linux/err.h>
12 #include <linux/mutex.h>
13 #include <linux/gfp.h>
14 #include <linux/kernel.h>
15 #include <linux/workqueue.h>
16 #include <linux/thermal.h>
17 #include <linux/cpufreq.h>
18 #include <linux/cpumask.h>
19 #include <linux/cpu_cooling.h>
20 #include <linux/of.h>
21 
22 #include "ti-thermal.h"
23 #include "ti-bandgap.h"
24 #include "../thermal_hwmon.h"
25 
26 #define TI_BANDGAP_UPDATE_INTERVAL_MS 250
27 
28 /* common data structures */
29 struct ti_thermal_data {
30 	struct cpufreq_policy *policy;
31 	struct thermal_zone_device *ti_thermal;
32 	struct thermal_zone_device *pcb_tz;
33 	struct thermal_cooling_device *cool_dev;
34 	struct ti_bandgap *bgp;
35 	enum thermal_device_mode mode;
36 	struct work_struct thermal_wq;
37 	int sensor_id;
38 	bool our_zone;
39 };
40 
41 static void ti_thermal_work(struct work_struct *work)
42 {
43 	struct ti_thermal_data *data = container_of(work,
44 					struct ti_thermal_data, thermal_wq);
45 
46 	thermal_zone_device_update(data->ti_thermal, THERMAL_EVENT_UNSPECIFIED);
47 
48 	dev_dbg(data->bgp->dev, "updated thermal zone %s\n",
49 		thermal_zone_device_type(data->ti_thermal));
50 }
51 
52 /**
53  * ti_thermal_hotspot_temperature - returns sensor extrapolated temperature
54  * @t:	omap sensor temperature
55  * @s:	omap sensor slope value
56  * @c:	omap sensor const value
57  */
58 static inline int ti_thermal_hotspot_temperature(int t, int s, int c)
59 {
60 	int delta = t * s / 1000 + c;
61 
62 	if (delta < 0)
63 		delta = 0;
64 
65 	return t + delta;
66 }
67 
68 /* thermal zone ops */
69 /* Get temperature callback function for thermal zone */
70 static inline int __ti_thermal_get_temp(struct thermal_zone_device *tz, int *temp)
71 {
72 	struct thermal_zone_device *pcb_tz = NULL;
73 	struct ti_thermal_data *data = thermal_zone_device_priv(tz);
74 	struct ti_bandgap *bgp;
75 	const struct ti_temp_sensor *s;
76 	int ret, tmp, slope, constant;
77 	int pcb_temp;
78 
79 	if (!data)
80 		return 0;
81 
82 	bgp = data->bgp;
83 	s = &bgp->conf->sensors[data->sensor_id];
84 
85 	ret = ti_bandgap_read_temperature(bgp, data->sensor_id, &tmp);
86 	if (ret)
87 		return ret;
88 
89 	/* Default constants */
90 	slope = thermal_zone_get_slope(tz);
91 	constant = thermal_zone_get_offset(tz);
92 
93 	pcb_tz = data->pcb_tz;
94 	/* In case pcb zone is available, use the extrapolation rule with it */
95 	if (!IS_ERR(pcb_tz)) {
96 		ret = thermal_zone_get_temp(pcb_tz, &pcb_temp);
97 		if (!ret) {
98 			tmp -= pcb_temp; /* got a valid PCB temp */
99 			slope = s->slope_pcb;
100 			constant = s->constant_pcb;
101 		} else {
102 			dev_err(bgp->dev,
103 				"Failed to read PCB state. Using defaults\n");
104 			ret = 0;
105 		}
106 	}
107 	*temp = ti_thermal_hotspot_temperature(tmp, slope, constant);
108 
109 	return ret;
110 }
111 
112 static int __ti_thermal_get_trend(struct thermal_zone_device *tz, int trip, enum thermal_trend *trend)
113 {
114 	struct ti_thermal_data *data = thermal_zone_device_priv(tz);
115 	struct ti_bandgap *bgp;
116 	int id, tr, ret = 0;
117 
118 	bgp = data->bgp;
119 	id = data->sensor_id;
120 
121 	ret = ti_bandgap_get_trend(bgp, id, &tr);
122 	if (ret)
123 		return ret;
124 
125 	if (tr > 0)
126 		*trend = THERMAL_TREND_RAISING;
127 	else if (tr < 0)
128 		*trend = THERMAL_TREND_DROPPING;
129 	else
130 		*trend = THERMAL_TREND_STABLE;
131 
132 	return 0;
133 }
134 
135 static const struct thermal_zone_device_ops ti_of_thermal_ops = {
136 	.get_temp = __ti_thermal_get_temp,
137 	.get_trend = __ti_thermal_get_trend,
138 };
139 
140 static struct ti_thermal_data
141 *ti_thermal_build_data(struct ti_bandgap *bgp, int id)
142 {
143 	struct ti_thermal_data *data;
144 
145 	data = devm_kzalloc(bgp->dev, sizeof(*data), GFP_KERNEL);
146 	if (!data) {
147 		dev_err(bgp->dev, "kzalloc fail\n");
148 		return NULL;
149 	}
150 	data->sensor_id = id;
151 	data->bgp = bgp;
152 	data->mode = THERMAL_DEVICE_ENABLED;
153 	/* pcb_tz will be either valid or PTR_ERR() */
154 	data->pcb_tz = thermal_zone_get_zone_by_name("pcb");
155 	INIT_WORK(&data->thermal_wq, ti_thermal_work);
156 
157 	return data;
158 }
159 
160 int ti_thermal_expose_sensor(struct ti_bandgap *bgp, int id,
161 			     char *domain)
162 {
163 	struct ti_thermal_data *data;
164 
165 	data = ti_bandgap_get_sensor_data(bgp, id);
166 
167 	if (IS_ERR_OR_NULL(data))
168 		data = ti_thermal_build_data(bgp, id);
169 
170 	if (!data)
171 		return -EINVAL;
172 
173 	/* in case this is specified by DT */
174 	data->ti_thermal = devm_thermal_of_zone_register(bgp->dev, id,
175 					data, &ti_of_thermal_ops);
176 	if (IS_ERR(data->ti_thermal)) {
177 		dev_err(bgp->dev, "thermal zone device is NULL\n");
178 		return PTR_ERR(data->ti_thermal);
179 	}
180 
181 	ti_bandgap_set_sensor_data(bgp, id, data);
182 	ti_bandgap_write_update_interval(bgp, data->sensor_id,
183 					 TI_BANDGAP_UPDATE_INTERVAL_MS);
184 
185 	devm_thermal_add_hwmon_sysfs(bgp->dev, data->ti_thermal);
186 
187 	return 0;
188 }
189 
190 int ti_thermal_remove_sensor(struct ti_bandgap *bgp, int id)
191 {
192 	struct ti_thermal_data *data;
193 
194 	data = ti_bandgap_get_sensor_data(bgp, id);
195 
196 	if (!IS_ERR_OR_NULL(data) && data->ti_thermal) {
197 		if (data->our_zone)
198 			thermal_zone_device_unregister(data->ti_thermal);
199 	}
200 
201 	return 0;
202 }
203 
204 int ti_thermal_report_sensor_temperature(struct ti_bandgap *bgp, int id)
205 {
206 	struct ti_thermal_data *data;
207 
208 	data = ti_bandgap_get_sensor_data(bgp, id);
209 
210 	schedule_work(&data->thermal_wq);
211 
212 	return 0;
213 }
214 
215 int ti_thermal_register_cpu_cooling(struct ti_bandgap *bgp, int id)
216 {
217 	struct ti_thermal_data *data;
218 	struct device_node *np = bgp->dev->of_node;
219 
220 	/*
221 	 * We are assuming here that if one deploys the zone
222 	 * using DT, then it must be aware that the cooling device
223 	 * loading has to happen via cpufreq driver.
224 	 */
225 	if (of_property_present(np, "#thermal-sensor-cells"))
226 		return 0;
227 
228 	data = ti_bandgap_get_sensor_data(bgp, id);
229 	if (!data || IS_ERR(data))
230 		data = ti_thermal_build_data(bgp, id);
231 
232 	if (!data)
233 		return -EINVAL;
234 
235 	data->policy = cpufreq_cpu_get(0);
236 	if (!data->policy) {
237 		pr_debug("%s: CPUFreq policy not found\n", __func__);
238 		return -EPROBE_DEFER;
239 	}
240 
241 	/* Register cooling device */
242 	data->cool_dev = cpufreq_cooling_register(data->policy);
243 	if (IS_ERR(data->cool_dev)) {
244 		int ret = PTR_ERR(data->cool_dev);
245 		dev_err(bgp->dev, "Failed to register cpu cooling device %d\n",
246 			ret);
247 		cpufreq_cpu_put(data->policy);
248 
249 		return ret;
250 	}
251 	ti_bandgap_set_sensor_data(bgp, id, data);
252 
253 	return 0;
254 }
255 
256 int ti_thermal_unregister_cpu_cooling(struct ti_bandgap *bgp, int id)
257 {
258 	struct ti_thermal_data *data;
259 
260 	data = ti_bandgap_get_sensor_data(bgp, id);
261 
262 	if (!IS_ERR_OR_NULL(data)) {
263 		cpufreq_cooling_unregister(data->cool_dev);
264 		if (data->policy)
265 			cpufreq_cpu_put(data->policy);
266 	}
267 
268 	return 0;
269 }
270