ti-thermal-common.c 6.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/*
 * OMAP thermal driver interface
 *
 * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com/
 * Contact:
 *   Eduardo Valentin <eduardo.valentin@ti.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

#include <linux/device.h>
#include <linux/err.h>
#include <linux/mutex.h>
#include <linux/gfp.h>
#include <linux/kernel.h>
#include <linux/workqueue.h>
#include <linux/thermal.h>
31
#include <linux/cpufreq.h>
32
#include <linux/cpumask.h>
33
#include <linux/cpu_cooling.h>
34
#include <linux/of.h>
35

36 37
#include "ti-thermal.h"
#include "ti-bandgap.h"
38 39

/* common data structures */
40
struct ti_thermal_data {
41
	struct cpufreq_policy *policy;
42
	struct thermal_zone_device *ti_thermal;
43
	struct thermal_zone_device *pcb_tz;
44
	struct thermal_cooling_device *cool_dev;
45
	struct ti_bandgap *bgp;
46 47 48
	enum thermal_device_mode mode;
	struct work_struct thermal_wq;
	int sensor_id;
49
	bool our_zone;
50 51
};

52
static void ti_thermal_work(struct work_struct *work)
53
{
54 55
	struct ti_thermal_data *data = container_of(work,
					struct ti_thermal_data, thermal_wq);
56

57
	thermal_zone_device_update(data->ti_thermal, THERMAL_EVENT_UNSPECIFIED);
58

59 60
	dev_dbg(&data->ti_thermal->device, "updated thermal zone %s\n",
		data->ti_thermal->type);
61 62 63
}

/**
64
 * ti_thermal_hotspot_temperature - returns sensor extrapolated temperature
65 66 67 68
 * @t:	omap sensor temperature
 * @s:	omap sensor slope value
 * @c:	omap sensor const value
 */
69
static inline int ti_thermal_hotspot_temperature(int t, int s, int c)
70 71 72 73 74 75 76 77 78 79
{
	int delta = t * s / 1000 + c;

	if (delta < 0)
		delta = 0;

	return t + delta;
}

/* thermal zone ops */
P
Pavel Machek 已提交
80
/* Get temperature callback function for thermal zone */
81
static inline int __ti_thermal_get_temp(void *devdata, int *temp)
82
{
83
	struct thermal_zone_device *pcb_tz = NULL;
84
	struct ti_thermal_data *data = devdata;
85
	struct ti_bandgap *bgp;
86
	const struct ti_temp_sensor *s;
87
	int ret, tmp, slope, constant;
88
	int pcb_temp;
89

90 91 92
	if (!data)
		return 0;

93 94
	bgp = data->bgp;
	s = &bgp->conf->sensors[data->sensor_id];
95

96
	ret = ti_bandgap_read_temperature(bgp, data->sensor_id, &tmp);
97 98 99
	if (ret)
		return ret;

100
	/* Default constants */
101 102
	slope = thermal_zone_get_slope(data->ti_thermal);
	constant = thermal_zone_get_offset(data->ti_thermal);
103 104

	pcb_tz = data->pcb_tz;
105
	/* In case pcb zone is available, use the extrapolation rule with it */
106
	if (!IS_ERR(pcb_tz)) {
107 108 109 110 111 112 113 114
		ret = thermal_zone_get_temp(pcb_tz, &pcb_temp);
		if (!ret) {
			tmp -= pcb_temp; /* got a valid PCB temp */
			slope = s->slope_pcb;
			constant = s->constant_pcb;
		} else {
			dev_err(bgp->dev,
				"Failed to read PCB state. Using defaults\n");
115
			ret = 0;
116
		}
117
	}
118
	*temp = ti_thermal_hotspot_temperature(tmp, slope, constant);
119 120 121 122

	return ret;
}

123
static inline int ti_thermal_get_temp(struct thermal_zone_device *thermal,
124
				      int *temp)
125 126 127 128 129 130
{
	struct ti_thermal_data *data = thermal->devdata;

	return __ti_thermal_get_temp(data, temp);
}

131
static int __ti_thermal_get_trend(void *p, int trip, enum thermal_trend *trend)
132
{
133
	struct ti_thermal_data *data = p;
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
	struct ti_bandgap *bgp;
	int id, tr, ret = 0;

	bgp = data->bgp;
	id = data->sensor_id;

	ret = ti_bandgap_get_trend(bgp, id, &tr);
	if (ret)
		return ret;

	if (tr > 0)
		*trend = THERMAL_TREND_RAISING;
	else if (tr < 0)
		*trend = THERMAL_TREND_DROPPING;
	else
		*trend = THERMAL_TREND_STABLE;

	return 0;
}

154 155 156 157 158
static const struct thermal_zone_of_device_ops ti_of_thermal_ops = {
	.get_temp = __ti_thermal_get_temp,
	.get_trend = __ti_thermal_get_trend,
};

159 160
static struct ti_thermal_data
*ti_thermal_build_data(struct ti_bandgap *bgp, int id)
161
{
162
	struct ti_thermal_data *data;
163

164
	data = devm_kzalloc(bgp->dev, sizeof(*data), GFP_KERNEL);
165
	if (!data) {
166
		dev_err(bgp->dev, "kzalloc fail\n");
167
		return NULL;
168 169
	}
	data->sensor_id = id;
170
	data->bgp = bgp;
171
	data->mode = THERMAL_DEVICE_ENABLED;
172
	/* pcb_tz will be either valid or PTR_ERR() */
173
	data->pcb_tz = thermal_zone_get_zone_by_name("pcb");
174
	INIT_WORK(&data->thermal_wq, ti_thermal_work);
175

176 177 178
	return data;
}

179 180
int ti_thermal_expose_sensor(struct ti_bandgap *bgp, int id,
			     char *domain)
181
{
182
	struct ti_thermal_data *data;
183

184
	data = ti_bandgap_get_sensor_data(bgp, id);
185

186
	if (!data || IS_ERR(data))
187
		data = ti_thermal_build_data(bgp, id);
188 189 190 191

	if (!data)
		return -EINVAL;

192
	/* in case this is specified by DT */
193
	data->ti_thermal = devm_thermal_zone_of_sensor_register(bgp->dev, id,
194
					data, &ti_of_thermal_ops);
195
	if (IS_ERR(data->ti_thermal)) {
196 197
		dev_err(bgp->dev, "thermal zone device is NULL\n");
		return PTR_ERR(data->ti_thermal);
198
	}
199

200
	ti_bandgap_set_sensor_data(bgp, id, data);
201 202
	ti_bandgap_write_update_interval(bgp, data->sensor_id,
					data->ti_thermal->polling_delay);
203 204 205 206

	return 0;
}

207
int ti_thermal_remove_sensor(struct ti_bandgap *bgp, int id)
208
{
209
	struct ti_thermal_data *data;
210

211
	data = ti_bandgap_get_sensor_data(bgp, id);
212

213 214 215 216
	if (data && data->ti_thermal) {
		if (data->our_zone)
			thermal_zone_device_unregister(data->ti_thermal);
	}
217 218 219 220

	return 0;
}

221
int ti_thermal_report_sensor_temperature(struct ti_bandgap *bgp, int id)
222
{
223
	struct ti_thermal_data *data;
224

225
	data = ti_bandgap_get_sensor_data(bgp, id);
226 227 228 229 230 231

	schedule_work(&data->thermal_wq);

	return 0;
}

232
int ti_thermal_register_cpu_cooling(struct ti_bandgap *bgp, int id)
233
{
234
	struct ti_thermal_data *data;
235 236 237 238 239 240 241 242 243
	struct device_node *np = bgp->dev->of_node;

	/*
	 * We are assuming here that if one deploys the zone
	 * using DT, then it must be aware that the cooling device
	 * loading has to happen via cpufreq driver.
	 */
	if (of_find_property(np, "#thermal-sensor-cells", NULL))
		return 0;
244

245
	data = ti_bandgap_get_sensor_data(bgp, id);
246
	if (!data || IS_ERR(data))
247
		data = ti_thermal_build_data(bgp, id);
248 249 250

	if (!data)
		return -EINVAL;
251

252 253 254 255 256 257
	data->policy = cpufreq_cpu_get(0);
	if (!data->policy) {
		pr_debug("%s: CPUFreq policy not found\n", __func__);
		return -EPROBE_DEFER;
	}

258
	/* Register cooling device */
259
	data->cool_dev = cpufreq_cooling_register(data->policy);
260
	if (IS_ERR(data->cool_dev)) {
261
		int ret = PTR_ERR(data->cool_dev);
262 263 264
		dev_err(bgp->dev, "Failed to register cpu cooling device %d\n",
			ret);
		cpufreq_cpu_put(data->policy);
265 266

		return ret;
267
	}
268
	ti_bandgap_set_sensor_data(bgp, id, data);
269 270 271 272

	return 0;
}

273
int ti_thermal_unregister_cpu_cooling(struct ti_bandgap *bgp, int id)
274
{
275
	struct ti_thermal_data *data;
276

277
	data = ti_bandgap_get_sensor_data(bgp, id);
278

279
	if (data) {
280
		cpufreq_cooling_unregister(data->cool_dev);
281 282
		if (data->policy)
			cpufreq_cpu_put(data->policy);
283
	}
284 285 286

	return 0;
}