thermal: exynos: Provide initial setting for TMU's test MUX address at Exynos4412
[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");
113 }
445eaf87 114 }
03e859d3 115 *temp = ti_thermal_hotspot_temperature(tmp, slope, constant);
445eaf87
EV
116
117 return ret;
118}
119
120/* Bind callback functions for thermal zone */
03e859d3
EV
121static int ti_thermal_bind(struct thermal_zone_device *thermal,
122 struct thermal_cooling_device *cdev)
445eaf87 123{
03e859d3 124 struct ti_thermal_data *data = thermal->devdata;
5035d48d 125 int id;
445eaf87 126
0c12b5ac 127 if (!data || IS_ERR(data))
445eaf87
EV
128 return -ENODEV;
129
130 /* check if this is the cooling device we registered */
131 if (data->cool_dev != cdev)
132 return 0;
133
134 id = data->sensor_id;
445eaf87 135
445eaf87 136 /* Simple thing, two trips, one passive another critical */
a7a3b8c8 137 return thermal_zone_bind_cooling_device(thermal, 0, cdev,
67b4c474 138 /* bind with min and max states defined by cpu_cooling */
a7a3b8c8
EV
139 THERMAL_NO_LIMIT,
140 THERMAL_NO_LIMIT);
445eaf87
EV
141}
142
143/* Unbind callback functions for thermal zone */
03e859d3
EV
144static int ti_thermal_unbind(struct thermal_zone_device *thermal,
145 struct thermal_cooling_device *cdev)
445eaf87 146{
03e859d3 147 struct ti_thermal_data *data = thermal->devdata;
445eaf87 148
0c12b5ac 149 if (!data || IS_ERR(data))
445eaf87
EV
150 return -ENODEV;
151
152 /* check if this is the cooling device we registered */
153 if (data->cool_dev != cdev)
154 return 0;
155
156 /* Simple thing, two trips, one passive another critical */
157 return thermal_zone_unbind_cooling_device(thermal, 0, cdev);
158}
159
160/* Get mode callback functions for thermal zone */
03e859d3
EV
161static int ti_thermal_get_mode(struct thermal_zone_device *thermal,
162 enum thermal_device_mode *mode)
445eaf87 163{
03e859d3 164 struct ti_thermal_data *data = thermal->devdata;
445eaf87
EV
165
166 if (data)
167 *mode = data->mode;
168
169 return 0;
170}
171
172/* Set mode callback functions for thermal zone */
03e859d3
EV
173static int ti_thermal_set_mode(struct thermal_zone_device *thermal,
174 enum thermal_device_mode mode)
445eaf87 175{
03e859d3 176 struct ti_thermal_data *data = thermal->devdata;
10ccff1b
RK
177 struct ti_bandgap *bgp;
178
179 bgp = data->bgp;
445eaf87 180
03e859d3 181 if (!data->ti_thermal) {
445eaf87
EV
182 dev_notice(&thermal->device, "thermal zone not registered\n");
183 return 0;
184 }
185
03e859d3 186 mutex_lock(&data->ti_thermal->lock);
445eaf87
EV
187
188 if (mode == THERMAL_DEVICE_ENABLED)
03e859d3 189 data->ti_thermal->polling_delay = FAST_TEMP_MONITORING_RATE;
445eaf87 190 else
03e859d3 191 data->ti_thermal->polling_delay = 0;
445eaf87 192
03e859d3 193 mutex_unlock(&data->ti_thermal->lock);
445eaf87
EV
194
195 data->mode = mode;
10ccff1b
RK
196 ti_bandgap_write_update_interval(bgp, data->sensor_id,
197 data->ti_thermal->polling_delay);
03e859d3 198 thermal_zone_device_update(data->ti_thermal);
445eaf87 199 dev_dbg(&thermal->device, "thermal polling set for duration=%d msec\n",
03e859d3 200 data->ti_thermal->polling_delay);
445eaf87
EV
201
202 return 0;
203}
204
205/* Get trip type callback functions for thermal zone */
03e859d3
EV
206static int ti_thermal_get_trip_type(struct thermal_zone_device *thermal,
207 int trip, enum thermal_trip_type *type)
445eaf87 208{
03e859d3 209 if (!ti_thermal_is_valid_trip(trip))
445eaf87
EV
210 return -EINVAL;
211
212 if (trip + 1 == OMAP_TRIP_NUMBER)
213 *type = THERMAL_TRIP_CRITICAL;
214 else
215 *type = THERMAL_TRIP_PASSIVE;
216
217 return 0;
218}
219
220/* Get trip temperature callback functions for thermal zone */
03e859d3
EV
221static int ti_thermal_get_trip_temp(struct thermal_zone_device *thermal,
222 int trip, unsigned long *temp)
445eaf87 223{
03e859d3 224 if (!ti_thermal_is_valid_trip(trip))
445eaf87
EV
225 return -EINVAL;
226
03e859d3 227 *temp = ti_thermal_get_trip_value(trip);
445eaf87
EV
228
229 return 0;
230}
231
03b7f67b
K
232/* Get the temperature trend callback functions for thermal zone */
233static int ti_thermal_get_trend(struct thermal_zone_device *thermal,
234 int trip, enum thermal_trend *trend)
235{
236 struct ti_thermal_data *data = thermal->devdata;
237 struct ti_bandgap *bgp;
238 int id, tr, ret = 0;
239
240 bgp = data->bgp;
241 id = data->sensor_id;
242
243 ret = ti_bandgap_get_trend(bgp, id, &tr);
244 if (ret)
245 return ret;
246
247 if (tr > 0)
248 *trend = THERMAL_TREND_RAISING;
249 else if (tr < 0)
250 *trend = THERMAL_TREND_DROPPING;
251 else
252 *trend = THERMAL_TREND_STABLE;
253
254 return 0;
255}
256
445eaf87 257/* Get critical temperature callback functions for thermal zone */
03e859d3
EV
258static int ti_thermal_get_crit_temp(struct thermal_zone_device *thermal,
259 unsigned long *temp)
445eaf87
EV
260{
261 /* shutdown zone */
03e859d3 262 return ti_thermal_get_trip_temp(thermal, OMAP_TRIP_NUMBER - 1, temp);
445eaf87
EV
263}
264
03e859d3
EV
265static struct thermal_zone_device_ops ti_thermal_ops = {
266 .get_temp = ti_thermal_get_temp,
03b7f67b 267 .get_trend = ti_thermal_get_trend,
03e859d3
EV
268 .bind = ti_thermal_bind,
269 .unbind = ti_thermal_unbind,
270 .get_mode = ti_thermal_get_mode,
271 .set_mode = ti_thermal_set_mode,
272 .get_trip_type = ti_thermal_get_trip_type,
273 .get_trip_temp = ti_thermal_get_trip_temp,
274 .get_crit_temp = ti_thermal_get_crit_temp,
445eaf87
EV
275};
276
03e859d3
EV
277static struct ti_thermal_data
278*ti_thermal_build_data(struct ti_bandgap *bgp, int id)
445eaf87 279{
03e859d3 280 struct ti_thermal_data *data;
445eaf87 281
d7f080e6 282 data = devm_kzalloc(bgp->dev, sizeof(*data), GFP_KERNEL);
445eaf87 283 if (!data) {
d7f080e6 284 dev_err(bgp->dev, "kzalloc fail\n");
04a4d10d 285 return NULL;
445eaf87
EV
286 }
287 data->sensor_id = id;
d7f080e6 288 data->bgp = bgp;
445eaf87 289 data->mode = THERMAL_DEVICE_ENABLED;
0c12b5ac 290 /* pcb_tz will be either valid or PTR_ERR() */
359836e1 291 data->pcb_tz = thermal_zone_get_zone_by_name("pcb");
03e859d3 292 INIT_WORK(&data->thermal_wq, ti_thermal_work);
445eaf87 293
04a4d10d
EV
294 return data;
295}
296
03e859d3
EV
297int ti_thermal_expose_sensor(struct ti_bandgap *bgp, int id,
298 char *domain)
04a4d10d 299{
03e859d3 300 struct ti_thermal_data *data;
04a4d10d 301
03e859d3 302 data = ti_bandgap_get_sensor_data(bgp, id);
04a4d10d 303
0c12b5ac 304 if (!data || IS_ERR(data))
03e859d3 305 data = ti_thermal_build_data(bgp, id);
04a4d10d
EV
306
307 if (!data)
308 return -EINVAL;
309
445eaf87 310 /* Create thermal zone */
03e859d3
EV
311 data->ti_thermal = thermal_zone_device_register(domain,
312 OMAP_TRIP_NUMBER, 0, data, &ti_thermal_ops,
50125a9b 313 NULL, FAST_TEMP_MONITORING_RATE,
765a1939 314 FAST_TEMP_MONITORING_RATE);
0c12b5ac 315 if (IS_ERR(data->ti_thermal)) {
d7f080e6 316 dev_err(bgp->dev, "thermal zone device is NULL\n");
03e859d3 317 return PTR_ERR(data->ti_thermal);
445eaf87 318 }
03e859d3
EV
319 data->ti_thermal->polling_delay = FAST_TEMP_MONITORING_RATE;
320 ti_bandgap_set_sensor_data(bgp, id, data);
10ccff1b
RK
321 ti_bandgap_write_update_interval(bgp, data->sensor_id,
322 data->ti_thermal->polling_delay);
445eaf87
EV
323
324 return 0;
325}
326
03e859d3 327int ti_thermal_remove_sensor(struct ti_bandgap *bgp, int id)
445eaf87 328{
03e859d3 329 struct ti_thermal_data *data;
445eaf87 330
03e859d3 331 data = ti_bandgap_get_sensor_data(bgp, id);
445eaf87 332
03e859d3 333 thermal_zone_device_unregister(data->ti_thermal);
445eaf87
EV
334
335 return 0;
336}
337
03e859d3 338int ti_thermal_report_sensor_temperature(struct ti_bandgap *bgp, int id)
445eaf87 339{
03e859d3 340 struct ti_thermal_data *data;
445eaf87 341
03e859d3 342 data = ti_bandgap_get_sensor_data(bgp, id);
445eaf87
EV
343
344 schedule_work(&data->thermal_wq);
345
346 return 0;
347}
348
03e859d3 349int ti_thermal_register_cpu_cooling(struct ti_bandgap *bgp, int id)
445eaf87 350{
03e859d3 351 struct ti_thermal_data *data;
445eaf87 352
03e859d3 353 data = ti_bandgap_get_sensor_data(bgp, id);
0c12b5ac 354 if (!data || IS_ERR(data))
03e859d3 355 data = ti_thermal_build_data(bgp, id);
04a4d10d
EV
356
357 if (!data)
358 return -EINVAL;
445eaf87 359
f1553334
EV
360 if (!cpufreq_get_current_driver()) {
361 dev_dbg(bgp->dev, "no cpufreq driver yet\n");
362 return -EPROBE_DEFER;
363 }
364
445eaf87 365 /* Register cooling device */
5035d48d 366 data->cool_dev = cpufreq_cooling_register(cpu_present_mask);
0c12b5ac 367 if (IS_ERR(data->cool_dev)) {
d7f080e6 368 dev_err(bgp->dev,
445eaf87
EV
369 "Failed to register cpufreq cooling device\n");
370 return PTR_ERR(data->cool_dev);
371 }
03e859d3 372 ti_bandgap_set_sensor_data(bgp, id, data);
445eaf87
EV
373
374 return 0;
375}
376
03e859d3 377int ti_thermal_unregister_cpu_cooling(struct ti_bandgap *bgp, int id)
445eaf87 378{
03e859d3 379 struct ti_thermal_data *data;
445eaf87 380
03e859d3 381 data = ti_bandgap_get_sensor_data(bgp, id);
445eaf87
EV
382 cpufreq_cooling_unregister(data->cool_dev);
383
384 return 0;
385}
This page took 0.164363 seconds and 5 git commands to generate.