hwmon: tmp102: expose to thermal fw via DT nodes
[deliverable/linux.git] / drivers / thermal / ti-soc-thermal / ti-thermal-common.c
CommitLineData
445eaf87
EV
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/cpufreq.h>
5035d48d 32#include <linux/cpumask.h>
445eaf87
EV
33#include <linux/cpu_cooling.h>
34
7372add4
EV
35#include "ti-thermal.h"
36#include "ti-bandgap.h"
445eaf87
EV
37
38/* common data structures */
03e859d3
EV
39struct ti_thermal_data {
40 struct thermal_zone_device *ti_thermal;
359836e1 41 struct thermal_zone_device *pcb_tz;
445eaf87 42 struct thermal_cooling_device *cool_dev;
03e859d3 43 struct ti_bandgap *bgp;
445eaf87
EV
44 enum thermal_device_mode mode;
45 struct work_struct thermal_wq;
46 int sensor_id;
47};
48
03e859d3 49static void ti_thermal_work(struct work_struct *work)
445eaf87 50{
03e859d3
EV
51 struct ti_thermal_data *data = container_of(work,
52 struct ti_thermal_data, thermal_wq);
445eaf87 53
03e859d3 54 thermal_zone_device_update(data->ti_thermal);
445eaf87 55
03e859d3
EV
56 dev_dbg(&data->ti_thermal->device, "updated thermal zone %s\n",
57 data->ti_thermal->type);
445eaf87
EV
58}
59
60/**
03e859d3 61 * ti_thermal_hotspot_temperature - returns sensor extrapolated temperature
445eaf87
EV
62 * @t: omap sensor temperature
63 * @s: omap sensor slope value
64 * @c: omap sensor const value
65 */
03e859d3 66static inline int ti_thermal_hotspot_temperature(int t, int s, int c)
445eaf87
EV
67{
68 int delta = t * s / 1000 + c;
69
70 if (delta < 0)
71 delta = 0;
72
73 return t + delta;
74}
75
76/* thermal zone ops */
77/* Get temperature callback function for thermal zone*/
03e859d3
EV
78static inline int ti_thermal_get_temp(struct thermal_zone_device *thermal,
79 unsigned long *temp)
445eaf87 80{
359836e1 81 struct thermal_zone_device *pcb_tz = NULL;
03e859d3
EV
82 struct ti_thermal_data *data = thermal->devdata;
83 struct ti_bandgap *bgp;
9879b2c4 84 const struct ti_temp_sensor *s;
359836e1
EV
85 int ret, tmp, slope, constant;
86 unsigned long pcb_temp;
445eaf87 87
04a4d10d
EV
88 if (!data)
89 return 0;
90
d7f080e6
EV
91 bgp = data->bgp;
92 s = &bgp->conf->sensors[data->sensor_id];
04a4d10d 93
03e859d3 94 ret = ti_bandgap_read_temperature(bgp, data->sensor_id, &tmp);
445eaf87
EV
95 if (ret)
96 return ret;
97
359836e1
EV
98 /* Default constants */
99 slope = s->slope;
100 constant = s->constant;
101
102 pcb_tz = data->pcb_tz;
445eaf87 103 /* In case pcb zone is available, use the extrapolation rule with it */
0c12b5ac 104 if (!IS_ERR(pcb_tz)) {
359836e1
EV
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");
df8f1347 113 ret = 0;
359836e1 114 }
445eaf87 115 }
03e859d3 116 *temp = ti_thermal_hotspot_temperature(tmp, slope, constant);
445eaf87
EV
117
118 return ret;
119}
120
121/* Bind callback functions for thermal zone */
03e859d3
EV
122static int ti_thermal_bind(struct thermal_zone_device *thermal,
123 struct thermal_cooling_device *cdev)
445eaf87 124{
03e859d3 125 struct ti_thermal_data *data = thermal->devdata;
5035d48d 126 int id;
445eaf87 127
0c12b5ac 128 if (!data || IS_ERR(data))
445eaf87
EV
129 return -ENODEV;
130
131 /* check if this is the cooling device we registered */
132 if (data->cool_dev != cdev)
133 return 0;
134
135 id = data->sensor_id;
445eaf87 136
445eaf87 137 /* Simple thing, two trips, one passive another critical */
a7a3b8c8 138 return thermal_zone_bind_cooling_device(thermal, 0, cdev,
67b4c474 139 /* bind with min and max states defined by cpu_cooling */
a7a3b8c8
EV
140 THERMAL_NO_LIMIT,
141 THERMAL_NO_LIMIT);
445eaf87
EV
142}
143
144/* Unbind callback functions for thermal zone */
03e859d3
EV
145static int ti_thermal_unbind(struct thermal_zone_device *thermal,
146 struct thermal_cooling_device *cdev)
445eaf87 147{
03e859d3 148 struct ti_thermal_data *data = thermal->devdata;
445eaf87 149
0c12b5ac 150 if (!data || IS_ERR(data))
445eaf87
EV
151 return -ENODEV;
152
153 /* check if this is the cooling device we registered */
154 if (data->cool_dev != cdev)
155 return 0;
156
157 /* Simple thing, two trips, one passive another critical */
158 return thermal_zone_unbind_cooling_device(thermal, 0, cdev);
159}
160
161/* Get mode callback functions for thermal zone */
03e859d3
EV
162static int ti_thermal_get_mode(struct thermal_zone_device *thermal,
163 enum thermal_device_mode *mode)
445eaf87 164{
03e859d3 165 struct ti_thermal_data *data = thermal->devdata;
445eaf87
EV
166
167 if (data)
168 *mode = data->mode;
169
170 return 0;
171}
172
173/* Set mode callback functions for thermal zone */
03e859d3
EV
174static int ti_thermal_set_mode(struct thermal_zone_device *thermal,
175 enum thermal_device_mode mode)
445eaf87 176{
03e859d3 177 struct ti_thermal_data *data = thermal->devdata;
10ccff1b
RK
178 struct ti_bandgap *bgp;
179
180 bgp = data->bgp;
445eaf87 181
03e859d3 182 if (!data->ti_thermal) {
445eaf87
EV
183 dev_notice(&thermal->device, "thermal zone not registered\n");
184 return 0;
185 }
186
03e859d3 187 mutex_lock(&data->ti_thermal->lock);
445eaf87
EV
188
189 if (mode == THERMAL_DEVICE_ENABLED)
03e859d3 190 data->ti_thermal->polling_delay = FAST_TEMP_MONITORING_RATE;
445eaf87 191 else
03e859d3 192 data->ti_thermal->polling_delay = 0;
445eaf87 193
03e859d3 194 mutex_unlock(&data->ti_thermal->lock);
445eaf87
EV
195
196 data->mode = mode;
10ccff1b
RK
197 ti_bandgap_write_update_interval(bgp, data->sensor_id,
198 data->ti_thermal->polling_delay);
03e859d3 199 thermal_zone_device_update(data->ti_thermal);
445eaf87 200 dev_dbg(&thermal->device, "thermal polling set for duration=%d msec\n",
03e859d3 201 data->ti_thermal->polling_delay);
445eaf87
EV
202
203 return 0;
204}
205
206/* Get trip type callback functions for thermal zone */
03e859d3
EV
207static int ti_thermal_get_trip_type(struct thermal_zone_device *thermal,
208 int trip, enum thermal_trip_type *type)
445eaf87 209{
03e859d3 210 if (!ti_thermal_is_valid_trip(trip))
445eaf87
EV
211 return -EINVAL;
212
213 if (trip + 1 == OMAP_TRIP_NUMBER)
214 *type = THERMAL_TRIP_CRITICAL;
215 else
216 *type = THERMAL_TRIP_PASSIVE;
217
218 return 0;
219}
220
221/* Get trip temperature callback functions for thermal zone */
03e859d3
EV
222static int ti_thermal_get_trip_temp(struct thermal_zone_device *thermal,
223 int trip, unsigned long *temp)
445eaf87 224{
03e859d3 225 if (!ti_thermal_is_valid_trip(trip))
445eaf87
EV
226 return -EINVAL;
227
03e859d3 228 *temp = ti_thermal_get_trip_value(trip);
445eaf87
EV
229
230 return 0;
231}
232
03b7f67b
K
233/* Get the temperature trend callback functions for thermal zone */
234static int ti_thermal_get_trend(struct thermal_zone_device *thermal,
235 int trip, enum thermal_trend *trend)
236{
237 struct ti_thermal_data *data = thermal->devdata;
238 struct ti_bandgap *bgp;
239 int id, tr, ret = 0;
240
241 bgp = data->bgp;
242 id = data->sensor_id;
243
244 ret = ti_bandgap_get_trend(bgp, id, &tr);
245 if (ret)
246 return ret;
247
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;
254
255 return 0;
256}
257
445eaf87 258/* Get critical temperature callback functions for thermal zone */
03e859d3
EV
259static int ti_thermal_get_crit_temp(struct thermal_zone_device *thermal,
260 unsigned long *temp)
445eaf87
EV
261{
262 /* shutdown zone */
03e859d3 263 return ti_thermal_get_trip_temp(thermal, OMAP_TRIP_NUMBER - 1, temp);
445eaf87
EV
264}
265
03e859d3
EV
266static struct thermal_zone_device_ops ti_thermal_ops = {
267 .get_temp = ti_thermal_get_temp,
03b7f67b 268 .get_trend = ti_thermal_get_trend,
03e859d3
EV
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,
445eaf87
EV
276};
277
03e859d3
EV
278static struct ti_thermal_data
279*ti_thermal_build_data(struct ti_bandgap *bgp, int id)
445eaf87 280{
03e859d3 281 struct ti_thermal_data *data;
445eaf87 282
d7f080e6 283 data = devm_kzalloc(bgp->dev, sizeof(*data), GFP_KERNEL);
445eaf87 284 if (!data) {
d7f080e6 285 dev_err(bgp->dev, "kzalloc fail\n");
04a4d10d 286 return NULL;
445eaf87
EV
287 }
288 data->sensor_id = id;
d7f080e6 289 data->bgp = bgp;
445eaf87 290 data->mode = THERMAL_DEVICE_ENABLED;
0c12b5ac 291 /* pcb_tz will be either valid or PTR_ERR() */
359836e1 292 data->pcb_tz = thermal_zone_get_zone_by_name("pcb");
03e859d3 293 INIT_WORK(&data->thermal_wq, ti_thermal_work);
445eaf87 294
04a4d10d
EV
295 return data;
296}
297
03e859d3
EV
298int ti_thermal_expose_sensor(struct ti_bandgap *bgp, int id,
299 char *domain)
04a4d10d 300{
03e859d3 301 struct ti_thermal_data *data;
04a4d10d 302
03e859d3 303 data = ti_bandgap_get_sensor_data(bgp, id);
04a4d10d 304
0c12b5ac 305 if (!data || IS_ERR(data))
03e859d3 306 data = ti_thermal_build_data(bgp, id);
04a4d10d
EV
307
308 if (!data)
309 return -EINVAL;
310
445eaf87 311 /* Create thermal zone */
03e859d3
EV
312 data->ti_thermal = thermal_zone_device_register(domain,
313 OMAP_TRIP_NUMBER, 0, data, &ti_thermal_ops,
50125a9b 314 NULL, FAST_TEMP_MONITORING_RATE,
765a1939 315 FAST_TEMP_MONITORING_RATE);
0c12b5ac 316 if (IS_ERR(data->ti_thermal)) {
d7f080e6 317 dev_err(bgp->dev, "thermal zone device is NULL\n");
03e859d3 318 return PTR_ERR(data->ti_thermal);
445eaf87 319 }
03e859d3
EV
320 data->ti_thermal->polling_delay = FAST_TEMP_MONITORING_RATE;
321 ti_bandgap_set_sensor_data(bgp, id, data);
10ccff1b
RK
322 ti_bandgap_write_update_interval(bgp, data->sensor_id,
323 data->ti_thermal->polling_delay);
445eaf87
EV
324
325 return 0;
326}
327
03e859d3 328int ti_thermal_remove_sensor(struct ti_bandgap *bgp, int id)
445eaf87 329{
03e859d3 330 struct ti_thermal_data *data;
445eaf87 331
03e859d3 332 data = ti_bandgap_get_sensor_data(bgp, id);
445eaf87 333
03e859d3 334 thermal_zone_device_unregister(data->ti_thermal);
445eaf87
EV
335
336 return 0;
337}
338
03e859d3 339int ti_thermal_report_sensor_temperature(struct ti_bandgap *bgp, int id)
445eaf87 340{
03e859d3 341 struct ti_thermal_data *data;
445eaf87 342
03e859d3 343 data = ti_bandgap_get_sensor_data(bgp, id);
445eaf87
EV
344
345 schedule_work(&data->thermal_wq);
346
347 return 0;
348}
349
03e859d3 350int ti_thermal_register_cpu_cooling(struct ti_bandgap *bgp, int id)
445eaf87 351{
03e859d3 352 struct ti_thermal_data *data;
445eaf87 353
03e859d3 354 data = ti_bandgap_get_sensor_data(bgp, id);
0c12b5ac 355 if (!data || IS_ERR(data))
03e859d3 356 data = ti_thermal_build_data(bgp, id);
04a4d10d
EV
357
358 if (!data)
359 return -EINVAL;
445eaf87 360
f1553334
EV
361 if (!cpufreq_get_current_driver()) {
362 dev_dbg(bgp->dev, "no cpufreq driver yet\n");
363 return -EPROBE_DEFER;
364 }
365
445eaf87 366 /* Register cooling device */
5035d48d 367 data->cool_dev = cpufreq_cooling_register(cpu_present_mask);
0c12b5ac 368 if (IS_ERR(data->cool_dev)) {
d7f080e6 369 dev_err(bgp->dev,
445eaf87
EV
370 "Failed to register cpufreq cooling device\n");
371 return PTR_ERR(data->cool_dev);
372 }
03e859d3 373 ti_bandgap_set_sensor_data(bgp, id, data);
445eaf87
EV
374
375 return 0;
376}
377
03e859d3 378int ti_thermal_unregister_cpu_cooling(struct ti_bandgap *bgp, int id)
445eaf87 379{
03e859d3 380 struct ti_thermal_data *data;
445eaf87 381
03e859d3 382 data = ti_bandgap_get_sensor_data(bgp, id);
445eaf87
EV
383 cpufreq_cooling_unregister(data->cool_dev);
384
385 return 0;
386}
This page took 0.210296 seconds and 5 git commands to generate.