1 /*
2  * OMAP thermal driver interface
3  *
4  * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com/
5  * Contact:
6  *   Eduardo Valentin <eduardo.valentin@ti.com>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * version 2 as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA
21  *
22  */
23 
24 #include <linux/device.h>
25 #include <linux/err.h>
26 #include <linux/mutex.h>
27 #include <linux/gfp.h>
28 #include <linux/kernel.h>
29 #include <linux/workqueue.h>
30 #include <linux/thermal.h>
31 #include <linux/cpumask.h>
32 #include <linux/cpu_cooling.h>
33 #include <linux/of.h>
34 
35 #include "ti-thermal.h"
36 #include "ti-bandgap.h"
37 
38 /* common data structures */
39 struct ti_thermal_data {
40 	struct thermal_zone_device *ti_thermal;
41 	struct thermal_zone_device *pcb_tz;
42 	struct thermal_cooling_device *cool_dev;
43 	struct ti_bandgap *bgp;
44 	enum thermal_device_mode mode;
45 	struct work_struct thermal_wq;
46 	int sensor_id;
47 	bool our_zone;
48 };
49 
50 static void ti_thermal_work(struct work_struct *work)
51 {
52 	struct ti_thermal_data *data = container_of(work,
53 					struct ti_thermal_data, thermal_wq);
54 
55 	thermal_zone_device_update(data->ti_thermal);
56 
57 	dev_dbg(&data->ti_thermal->device, "updated thermal zone %s\n",
58 		data->ti_thermal->type);
59 }
60 
61 /**
62  * ti_thermal_hotspot_temperature - returns sensor extrapolated temperature
63  * @t:	omap sensor temperature
64  * @s:	omap sensor slope value
65  * @c:	omap sensor const value
66  */
67 static inline int ti_thermal_hotspot_temperature(int t, int s, int c)
68 {
69 	int delta = t * s / 1000 + c;
70 
71 	if (delta < 0)
72 		delta = 0;
73 
74 	return t + delta;
75 }
76 
77 /* thermal zone ops */
78 /* Get temperature callback function for thermal zone */
79 static inline int __ti_thermal_get_temp(void *devdata, int *temp)
80 {
81 	struct thermal_zone_device *pcb_tz = NULL;
82 	struct ti_thermal_data *data = devdata;
83 	struct ti_bandgap *bgp;
84 	const struct ti_temp_sensor *s;
85 	int ret, tmp, slope, constant;
86 	int pcb_temp;
87 
88 	if (!data)
89 		return 0;
90 
91 	bgp = data->bgp;
92 	s = &bgp->conf->sensors[data->sensor_id];
93 
94 	ret = ti_bandgap_read_temperature(bgp, data->sensor_id, &tmp);
95 	if (ret)
96 		return ret;
97 
98 	/* Default constants */
99 	slope = s->slope;
100 	constant = s->constant;
101 
102 	pcb_tz = data->pcb_tz;
103 	/* In case pcb zone is available, use the extrapolation rule with it */
104 	if (!IS_ERR(pcb_tz)) {
105 		ret = thermal_zone_get_temp(pcb_tz, &pcb_temp);
106 		if (!ret) {
107 			tmp -= pcb_temp; /* got a valid PCB temp */
108 			slope = s->slope_pcb;
109 			constant = s->constant_pcb;
110 		} else {
111 			dev_err(bgp->dev,
112 				"Failed to read PCB state. Using defaults\n");
113 			ret = 0;
114 		}
115 	}
116 	*temp = ti_thermal_hotspot_temperature(tmp, slope, constant);
117 
118 	return ret;
119 }
120 
121 static inline int ti_thermal_get_temp(struct thermal_zone_device *thermal,
122 				      int *temp)
123 {
124 	struct ti_thermal_data *data = thermal->devdata;
125 
126 	return __ti_thermal_get_temp(data, temp);
127 }
128 
129 /* Bind callback functions for thermal zone */
130 static int ti_thermal_bind(struct thermal_zone_device *thermal,
131 			   struct thermal_cooling_device *cdev)
132 {
133 	struct ti_thermal_data *data = thermal->devdata;
134 	int id;
135 
136 	if (!data || IS_ERR(data))
137 		return -ENODEV;
138 
139 	/* check if this is the cooling device we registered */
140 	if (data->cool_dev != cdev)
141 		return 0;
142 
143 	id = data->sensor_id;
144 
145 	/* Simple thing, two trips, one passive another critical */
146 	return thermal_zone_bind_cooling_device(thermal, 0, cdev,
147 	/* bind with min and max states defined by cpu_cooling */
148 						THERMAL_NO_LIMIT,
149 						THERMAL_NO_LIMIT,
150 						THERMAL_WEIGHT_DEFAULT);
151 }
152 
153 /* Unbind callback functions for thermal zone */
154 static int ti_thermal_unbind(struct thermal_zone_device *thermal,
155 			     struct thermal_cooling_device *cdev)
156 {
157 	struct ti_thermal_data *data = thermal->devdata;
158 
159 	if (!data || IS_ERR(data))
160 		return -ENODEV;
161 
162 	/* check if this is the cooling device we registered */
163 	if (data->cool_dev != cdev)
164 		return 0;
165 
166 	/* Simple thing, two trips, one passive another critical */
167 	return thermal_zone_unbind_cooling_device(thermal, 0, cdev);
168 }
169 
170 /* Get mode callback functions for thermal zone */
171 static int ti_thermal_get_mode(struct thermal_zone_device *thermal,
172 			       enum thermal_device_mode *mode)
173 {
174 	struct ti_thermal_data *data = thermal->devdata;
175 
176 	if (data)
177 		*mode = data->mode;
178 
179 	return 0;
180 }
181 
182 /* Set mode callback functions for thermal zone */
183 static int ti_thermal_set_mode(struct thermal_zone_device *thermal,
184 			       enum thermal_device_mode mode)
185 {
186 	struct ti_thermal_data *data = thermal->devdata;
187 	struct ti_bandgap *bgp;
188 
189 	bgp = data->bgp;
190 
191 	if (!data->ti_thermal) {
192 		dev_notice(&thermal->device, "thermal zone not registered\n");
193 		return 0;
194 	}
195 
196 	mutex_lock(&data->ti_thermal->lock);
197 
198 	if (mode == THERMAL_DEVICE_ENABLED)
199 		data->ti_thermal->polling_delay = FAST_TEMP_MONITORING_RATE;
200 	else
201 		data->ti_thermal->polling_delay = 0;
202 
203 	mutex_unlock(&data->ti_thermal->lock);
204 
205 	data->mode = mode;
206 	ti_bandgap_write_update_interval(bgp, data->sensor_id,
207 					data->ti_thermal->polling_delay);
208 	thermal_zone_device_update(data->ti_thermal);
209 	dev_dbg(&thermal->device, "thermal polling set for duration=%d msec\n",
210 		data->ti_thermal->polling_delay);
211 
212 	return 0;
213 }
214 
215 /* Get trip type callback functions for thermal zone */
216 static int ti_thermal_get_trip_type(struct thermal_zone_device *thermal,
217 				    int trip, enum thermal_trip_type *type)
218 {
219 	if (!ti_thermal_is_valid_trip(trip))
220 		return -EINVAL;
221 
222 	if (trip + 1 == OMAP_TRIP_NUMBER)
223 		*type = THERMAL_TRIP_CRITICAL;
224 	else
225 		*type = THERMAL_TRIP_PASSIVE;
226 
227 	return 0;
228 }
229 
230 /* Get trip temperature callback functions for thermal zone */
231 static int ti_thermal_get_trip_temp(struct thermal_zone_device *thermal,
232 				    int trip, int *temp)
233 {
234 	if (!ti_thermal_is_valid_trip(trip))
235 		return -EINVAL;
236 
237 	*temp = ti_thermal_get_trip_value(trip);
238 
239 	return 0;
240 }
241 
242 static int __ti_thermal_get_trend(void *p, long *trend)
243 {
244 	struct ti_thermal_data *data = p;
245 	struct ti_bandgap *bgp;
246 	int id, tr, ret = 0;
247 
248 	bgp = data->bgp;
249 	id = data->sensor_id;
250 
251 	ret = ti_bandgap_get_trend(bgp, id, &tr);
252 	if (ret)
253 		return ret;
254 
255 	*trend = tr;
256 
257 	return 0;
258 }
259 
260 /* Get the temperature trend callback functions for thermal zone */
261 static int ti_thermal_get_trend(struct thermal_zone_device *thermal,
262 				int trip, enum thermal_trend *trend)
263 {
264 	int ret;
265 	long tr;
266 
267 	ret = __ti_thermal_get_trend(thermal->devdata, &tr);
268 	if (ret)
269 		return ret;
270 
271 	if (tr > 0)
272 		*trend = THERMAL_TREND_RAISING;
273 	else if (tr < 0)
274 		*trend = THERMAL_TREND_DROPPING;
275 	else
276 		*trend = THERMAL_TREND_STABLE;
277 
278 	return 0;
279 }
280 
281 /* Get critical temperature callback functions for thermal zone */
282 static int ti_thermal_get_crit_temp(struct thermal_zone_device *thermal,
283 				    int *temp)
284 {
285 	/* shutdown zone */
286 	return ti_thermal_get_trip_temp(thermal, OMAP_TRIP_NUMBER - 1, temp);
287 }
288 
289 static const struct thermal_zone_of_device_ops ti_of_thermal_ops = {
290 	.get_temp = __ti_thermal_get_temp,
291 	.get_trend = __ti_thermal_get_trend,
292 };
293 
294 static struct thermal_zone_device_ops ti_thermal_ops = {
295 	.get_temp = ti_thermal_get_temp,
296 	.get_trend = ti_thermal_get_trend,
297 	.bind = ti_thermal_bind,
298 	.unbind = ti_thermal_unbind,
299 	.get_mode = ti_thermal_get_mode,
300 	.set_mode = ti_thermal_set_mode,
301 	.get_trip_type = ti_thermal_get_trip_type,
302 	.get_trip_temp = ti_thermal_get_trip_temp,
303 	.get_crit_temp = ti_thermal_get_crit_temp,
304 };
305 
306 static struct ti_thermal_data
307 *ti_thermal_build_data(struct ti_bandgap *bgp, int id)
308 {
309 	struct ti_thermal_data *data;
310 
311 	data = devm_kzalloc(bgp->dev, sizeof(*data), GFP_KERNEL);
312 	if (!data) {
313 		dev_err(bgp->dev, "kzalloc fail\n");
314 		return NULL;
315 	}
316 	data->sensor_id = id;
317 	data->bgp = bgp;
318 	data->mode = THERMAL_DEVICE_ENABLED;
319 	/* pcb_tz will be either valid or PTR_ERR() */
320 	data->pcb_tz = thermal_zone_get_zone_by_name("pcb");
321 	INIT_WORK(&data->thermal_wq, ti_thermal_work);
322 
323 	return data;
324 }
325 
326 int ti_thermal_expose_sensor(struct ti_bandgap *bgp, int id,
327 			     char *domain)
328 {
329 	struct ti_thermal_data *data;
330 
331 	data = ti_bandgap_get_sensor_data(bgp, id);
332 
333 	if (!data || IS_ERR(data))
334 		data = ti_thermal_build_data(bgp, id);
335 
336 	if (!data)
337 		return -EINVAL;
338 
339 	/* in case this is specified by DT */
340 	data->ti_thermal = thermal_zone_of_sensor_register(bgp->dev, id,
341 					data, &ti_of_thermal_ops);
342 	if (IS_ERR(data->ti_thermal)) {
343 		/* Create thermal zone */
344 		data->ti_thermal = thermal_zone_device_register(domain,
345 				OMAP_TRIP_NUMBER, 0, data, &ti_thermal_ops,
346 				NULL, FAST_TEMP_MONITORING_RATE,
347 				FAST_TEMP_MONITORING_RATE);
348 		if (IS_ERR(data->ti_thermal)) {
349 			dev_err(bgp->dev, "thermal zone device is NULL\n");
350 			return PTR_ERR(data->ti_thermal);
351 		}
352 		data->ti_thermal->polling_delay = FAST_TEMP_MONITORING_RATE;
353 		data->our_zone = true;
354 	}
355 	ti_bandgap_set_sensor_data(bgp, id, data);
356 	ti_bandgap_write_update_interval(bgp, data->sensor_id,
357 					data->ti_thermal->polling_delay);
358 
359 	return 0;
360 }
361 
362 int ti_thermal_remove_sensor(struct ti_bandgap *bgp, int id)
363 {
364 	struct ti_thermal_data *data;
365 
366 	data = ti_bandgap_get_sensor_data(bgp, id);
367 
368 	if (data && data->ti_thermal) {
369 		if (data->our_zone)
370 			thermal_zone_device_unregister(data->ti_thermal);
371 		else
372 			thermal_zone_of_sensor_unregister(bgp->dev,
373 							  data->ti_thermal);
374 	}
375 
376 	return 0;
377 }
378 
379 int ti_thermal_report_sensor_temperature(struct ti_bandgap *bgp, int id)
380 {
381 	struct ti_thermal_data *data;
382 
383 	data = ti_bandgap_get_sensor_data(bgp, id);
384 
385 	schedule_work(&data->thermal_wq);
386 
387 	return 0;
388 }
389 
390 int ti_thermal_register_cpu_cooling(struct ti_bandgap *bgp, int id)
391 {
392 	struct ti_thermal_data *data;
393 	struct device_node *np = bgp->dev->of_node;
394 
395 	/*
396 	 * We are assuming here that if one deploys the zone
397 	 * using DT, then it must be aware that the cooling device
398 	 * loading has to happen via cpufreq driver.
399 	 */
400 	if (of_find_property(np, "#thermal-sensor-cells", NULL))
401 		return 0;
402 
403 	data = ti_bandgap_get_sensor_data(bgp, id);
404 	if (!data || IS_ERR(data))
405 		data = ti_thermal_build_data(bgp, id);
406 
407 	if (!data)
408 		return -EINVAL;
409 
410 	/* Register cooling device */
411 	data->cool_dev = cpufreq_cooling_register(cpu_present_mask);
412 	if (IS_ERR(data->cool_dev)) {
413 		int ret = PTR_ERR(data->cool_dev);
414 
415 		if (ret != -EPROBE_DEFER)
416 			dev_err(bgp->dev,
417 				"Failed to register cpu cooling device %d\n",
418 				ret);
419 
420 		return ret;
421 	}
422 	ti_bandgap_set_sensor_data(bgp, id, data);
423 
424 	return 0;
425 }
426 
427 int ti_thermal_unregister_cpu_cooling(struct ti_bandgap *bgp, int id)
428 {
429 	struct ti_thermal_data *data;
430 
431 	data = ti_bandgap_get_sensor_data(bgp, id);
432 
433 	if (data)
434 		cpufreq_cooling_unregister(data->cool_dev);
435 
436 	return 0;
437 }
438