Merge tag 'gpio-v3.13-3' of git://git.kernel.org/pub/scm/linux/kernel/git/linusw...
[linux-2.6.git] / drivers / thermal / ti-soc-thermal / ti-thermal-common.c
blob5a47cc8c8f85ae1771c3faa1042b8f068978d570
1 /*
2 * OMAP thermal driver interface
4 * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com/
5 * Contact:
6 * Eduardo Valentin <eduardo.valentin@ti.com>
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.
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.
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
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/cpufreq.h>
32 #include <linux/cpumask.h>
33 #include <linux/cpu_cooling.h>
35 #include "ti-thermal.h"
36 #include "ti-bandgap.h"
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;
49 static void ti_thermal_work(struct work_struct *work)
51 struct ti_thermal_data *data = container_of(work,
52 struct ti_thermal_data, thermal_wq);
54 thermal_zone_device_update(data->ti_thermal);
56 dev_dbg(&data->ti_thermal->device, "updated thermal zone %s\n",
57 data->ti_thermal->type);
60 /**
61 * ti_thermal_hotspot_temperature - returns sensor extrapolated temperature
62 * @t: omap sensor temperature
63 * @s: omap sensor slope value
64 * @c: omap sensor const value
66 static inline int ti_thermal_hotspot_temperature(int t, int s, int c)
68 int delta = t * s / 1000 + c;
70 if (delta < 0)
71 delta = 0;
73 return t + delta;
76 /* thermal zone ops */
77 /* Get temperature callback function for thermal zone*/
78 static inline int ti_thermal_get_temp(struct thermal_zone_device *thermal,
79 unsigned long *temp)
81 struct thermal_zone_device *pcb_tz = NULL;
82 struct ti_thermal_data *data = thermal->devdata;
83 struct ti_bandgap *bgp;
84 const struct ti_temp_sensor *s;
85 int ret, tmp, slope, constant;
86 unsigned long pcb_temp;
88 if (!data)
89 return 0;
91 bgp = data->bgp;
92 s = &bgp->conf->sensors[data->sensor_id];
94 ret = ti_bandgap_read_temperature(bgp, data->sensor_id, &tmp);
95 if (ret)
96 return ret;
98 /* Default constants */
99 slope = s->slope;
100 constant = s->constant;
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;
116 *temp = ti_thermal_hotspot_temperature(tmp, slope, constant);
118 return ret;
121 /* Bind callback functions for thermal zone */
122 static int ti_thermal_bind(struct thermal_zone_device *thermal,
123 struct thermal_cooling_device *cdev)
125 struct ti_thermal_data *data = thermal->devdata;
126 int id;
128 if (!data || IS_ERR(data))
129 return -ENODEV;
131 /* check if this is the cooling device we registered */
132 if (data->cool_dev != cdev)
133 return 0;
135 id = data->sensor_id;
137 /* Simple thing, two trips, one passive another critical */
138 return thermal_zone_bind_cooling_device(thermal, 0, cdev,
139 /* bind with min and max states defined by cpu_cooling */
140 THERMAL_NO_LIMIT,
141 THERMAL_NO_LIMIT);
144 /* Unbind callback functions for thermal zone */
145 static int ti_thermal_unbind(struct thermal_zone_device *thermal,
146 struct thermal_cooling_device *cdev)
148 struct ti_thermal_data *data = thermal->devdata;
150 if (!data || IS_ERR(data))
151 return -ENODEV;
153 /* check if this is the cooling device we registered */
154 if (data->cool_dev != cdev)
155 return 0;
157 /* Simple thing, two trips, one passive another critical */
158 return thermal_zone_unbind_cooling_device(thermal, 0, cdev);
161 /* Get mode callback functions for thermal zone */
162 static int ti_thermal_get_mode(struct thermal_zone_device *thermal,
163 enum thermal_device_mode *mode)
165 struct ti_thermal_data *data = thermal->devdata;
167 if (data)
168 *mode = data->mode;
170 return 0;
173 /* Set mode callback functions for thermal zone */
174 static int ti_thermal_set_mode(struct thermal_zone_device *thermal,
175 enum thermal_device_mode mode)
177 struct ti_thermal_data *data = thermal->devdata;
178 struct ti_bandgap *bgp;
180 bgp = data->bgp;
182 if (!data->ti_thermal) {
183 dev_notice(&thermal->device, "thermal zone not registered\n");
184 return 0;
187 mutex_lock(&data->ti_thermal->lock);
189 if (mode == THERMAL_DEVICE_ENABLED)
190 data->ti_thermal->polling_delay = FAST_TEMP_MONITORING_RATE;
191 else
192 data->ti_thermal->polling_delay = 0;
194 mutex_unlock(&data->ti_thermal->lock);
196 data->mode = mode;
197 ti_bandgap_write_update_interval(bgp, data->sensor_id,
198 data->ti_thermal->polling_delay);
199 thermal_zone_device_update(data->ti_thermal);
200 dev_dbg(&thermal->device, "thermal polling set for duration=%d msec\n",
201 data->ti_thermal->polling_delay);
203 return 0;
206 /* Get trip type callback functions for thermal zone */
207 static int ti_thermal_get_trip_type(struct thermal_zone_device *thermal,
208 int trip, enum thermal_trip_type *type)
210 if (!ti_thermal_is_valid_trip(trip))
211 return -EINVAL;
213 if (trip + 1 == OMAP_TRIP_NUMBER)
214 *type = THERMAL_TRIP_CRITICAL;
215 else
216 *type = THERMAL_TRIP_PASSIVE;
218 return 0;
221 /* Get trip temperature callback functions for thermal zone */
222 static int ti_thermal_get_trip_temp(struct thermal_zone_device *thermal,
223 int trip, unsigned long *temp)
225 if (!ti_thermal_is_valid_trip(trip))
226 return -EINVAL;
228 *temp = ti_thermal_get_trip_value(trip);
230 return 0;
233 /* Get the temperature trend callback functions for thermal zone */
234 static int ti_thermal_get_trend(struct thermal_zone_device *thermal,
235 int trip, enum thermal_trend *trend)
237 struct ti_thermal_data *data = thermal->devdata;
238 struct ti_bandgap *bgp;
239 int id, tr, ret = 0;
241 bgp = data->bgp;
242 id = data->sensor_id;
244 ret = ti_bandgap_get_trend(bgp, id, &tr);
245 if (ret)
246 return ret;
248 if (tr > 0)
249 *trend = THERMAL_TREND_RAISING;
250 else if (tr < 0)
251 *trend = THERMAL_TREND_DROPPING;
252 else
253 *trend = THERMAL_TREND_STABLE;
255 return 0;
258 /* Get critical temperature callback functions for thermal zone */
259 static int ti_thermal_get_crit_temp(struct thermal_zone_device *thermal,
260 unsigned long *temp)
262 /* shutdown zone */
263 return ti_thermal_get_trip_temp(thermal, OMAP_TRIP_NUMBER - 1, temp);
266 static struct thermal_zone_device_ops ti_thermal_ops = {
267 .get_temp = ti_thermal_get_temp,
268 .get_trend = ti_thermal_get_trend,
269 .bind = ti_thermal_bind,
270 .unbind = ti_thermal_unbind,
271 .get_mode = ti_thermal_get_mode,
272 .set_mode = ti_thermal_set_mode,
273 .get_trip_type = ti_thermal_get_trip_type,
274 .get_trip_temp = ti_thermal_get_trip_temp,
275 .get_crit_temp = ti_thermal_get_crit_temp,
278 static struct ti_thermal_data
279 *ti_thermal_build_data(struct ti_bandgap *bgp, int id)
281 struct ti_thermal_data *data;
283 data = devm_kzalloc(bgp->dev, sizeof(*data), GFP_KERNEL);
284 if (!data) {
285 dev_err(bgp->dev, "kzalloc fail\n");
286 return NULL;
288 data->sensor_id = id;
289 data->bgp = bgp;
290 data->mode = THERMAL_DEVICE_ENABLED;
291 /* pcb_tz will be either valid or PTR_ERR() */
292 data->pcb_tz = thermal_zone_get_zone_by_name("pcb");
293 INIT_WORK(&data->thermal_wq, ti_thermal_work);
295 return data;
298 int ti_thermal_expose_sensor(struct ti_bandgap *bgp, int id,
299 char *domain)
301 struct ti_thermal_data *data;
303 data = ti_bandgap_get_sensor_data(bgp, id);
305 if (!data || IS_ERR(data))
306 data = ti_thermal_build_data(bgp, id);
308 if (!data)
309 return -EINVAL;
311 /* Create thermal zone */
312 data->ti_thermal = thermal_zone_device_register(domain,
313 OMAP_TRIP_NUMBER, 0, data, &ti_thermal_ops,
314 NULL, FAST_TEMP_MONITORING_RATE,
315 FAST_TEMP_MONITORING_RATE);
316 if (IS_ERR(data->ti_thermal)) {
317 dev_err(bgp->dev, "thermal zone device is NULL\n");
318 return PTR_ERR(data->ti_thermal);
320 data->ti_thermal->polling_delay = FAST_TEMP_MONITORING_RATE;
321 ti_bandgap_set_sensor_data(bgp, id, data);
322 ti_bandgap_write_update_interval(bgp, data->sensor_id,
323 data->ti_thermal->polling_delay);
325 return 0;
328 int ti_thermal_remove_sensor(struct ti_bandgap *bgp, int id)
330 struct ti_thermal_data *data;
332 data = ti_bandgap_get_sensor_data(bgp, id);
334 thermal_zone_device_unregister(data->ti_thermal);
336 return 0;
339 int ti_thermal_report_sensor_temperature(struct ti_bandgap *bgp, int id)
341 struct ti_thermal_data *data;
343 data = ti_bandgap_get_sensor_data(bgp, id);
345 schedule_work(&data->thermal_wq);
347 return 0;
350 int ti_thermal_register_cpu_cooling(struct ti_bandgap *bgp, int id)
352 struct ti_thermal_data *data;
354 data = ti_bandgap_get_sensor_data(bgp, id);
355 if (!data || IS_ERR(data))
356 data = ti_thermal_build_data(bgp, id);
358 if (!data)
359 return -EINVAL;
361 if (!cpufreq_get_current_driver()) {
362 dev_dbg(bgp->dev, "no cpufreq driver yet\n");
363 return -EPROBE_DEFER;
366 /* Register cooling device */
367 data->cool_dev = cpufreq_cooling_register(cpu_present_mask);
368 if (IS_ERR(data->cool_dev)) {
369 dev_err(bgp->dev,
370 "Failed to register cpufreq cooling device\n");
371 return PTR_ERR(data->cool_dev);
373 ti_bandgap_set_sensor_data(bgp, id, data);
375 return 0;
378 int ti_thermal_unregister_cpu_cooling(struct ti_bandgap *bgp, int id)
380 struct ti_thermal_data *data;
382 data = ti_bandgap_get_sensor_data(bgp, id);
383 cpufreq_cooling_unregister(data->cool_dev);
385 return 0;