2 * OMAP thermal driver interface
4 * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com/
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
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_cooling_device
*cool_dev
;
42 struct ti_bandgap
*bgp
;
43 enum thermal_device_mode mode
;
44 struct work_struct thermal_wq
;
48 static void ti_thermal_work(struct work_struct
*work
)
50 struct ti_thermal_data
*data
= container_of(work
,
51 struct ti_thermal_data
, thermal_wq
);
53 thermal_zone_device_update(data
->ti_thermal
);
55 dev_dbg(&data
->ti_thermal
->device
, "updated thermal zone %s\n",
56 data
->ti_thermal
->type
);
60 * ti_thermal_hotspot_temperature - returns sensor extrapolated temperature
61 * @t: omap sensor temperature
62 * @s: omap sensor slope value
63 * @c: omap sensor const value
65 static inline int ti_thermal_hotspot_temperature(int t
, int s
, int c
)
67 int delta
= t
* s
/ 1000 + c
;
75 /* thermal zone ops */
76 /* Get temperature callback function for thermal zone*/
77 static inline int ti_thermal_get_temp(struct thermal_zone_device
*thermal
,
80 struct ti_thermal_data
*data
= thermal
->devdata
;
81 struct ti_bandgap
*bgp
;
82 struct ti_temp_sensor
*s
;
83 int ret
, tmp
, pcb_temp
, slope
, constant
;
89 s
= &bgp
->conf
->sensors
[data
->sensor_id
];
91 ret
= ti_bandgap_read_temperature(bgp
, data
->sensor_id
, &tmp
);
96 /* TODO: Introduce pcb temperature lookup */
97 /* In case pcb zone is available, use the extrapolation rule with it */
100 slope
= s
->slope_pcb
;
101 constant
= s
->constant_pcb
;
104 constant
= s
->constant
;
106 *temp
= ti_thermal_hotspot_temperature(tmp
, slope
, constant
);
111 /* Bind callback functions for thermal zone */
112 static int ti_thermal_bind(struct thermal_zone_device
*thermal
,
113 struct thermal_cooling_device
*cdev
)
115 struct ti_thermal_data
*data
= thermal
->devdata
;
118 if (IS_ERR_OR_NULL(data
))
121 /* check if this is the cooling device we registered */
122 if (data
->cool_dev
!= cdev
)
125 id
= data
->sensor_id
;
127 /* TODO: bind with min and max states */
128 /* Simple thing, two trips, one passive another critical */
129 return thermal_zone_bind_cooling_device(thermal
, 0, cdev
,
134 /* Unbind callback functions for thermal zone */
135 static int ti_thermal_unbind(struct thermal_zone_device
*thermal
,
136 struct thermal_cooling_device
*cdev
)
138 struct ti_thermal_data
*data
= thermal
->devdata
;
140 if (IS_ERR_OR_NULL(data
))
143 /* check if this is the cooling device we registered */
144 if (data
->cool_dev
!= cdev
)
147 /* Simple thing, two trips, one passive another critical */
148 return thermal_zone_unbind_cooling_device(thermal
, 0, cdev
);
151 /* Get mode callback functions for thermal zone */
152 static int ti_thermal_get_mode(struct thermal_zone_device
*thermal
,
153 enum thermal_device_mode
*mode
)
155 struct ti_thermal_data
*data
= thermal
->devdata
;
163 /* Set mode callback functions for thermal zone */
164 static int ti_thermal_set_mode(struct thermal_zone_device
*thermal
,
165 enum thermal_device_mode mode
)
167 struct ti_thermal_data
*data
= thermal
->devdata
;
169 if (!data
->ti_thermal
) {
170 dev_notice(&thermal
->device
, "thermal zone not registered\n");
174 mutex_lock(&data
->ti_thermal
->lock
);
176 if (mode
== THERMAL_DEVICE_ENABLED
)
177 data
->ti_thermal
->polling_delay
= FAST_TEMP_MONITORING_RATE
;
179 data
->ti_thermal
->polling_delay
= 0;
181 mutex_unlock(&data
->ti_thermal
->lock
);
184 thermal_zone_device_update(data
->ti_thermal
);
185 dev_dbg(&thermal
->device
, "thermal polling set for duration=%d msec\n",
186 data
->ti_thermal
->polling_delay
);
191 /* Get trip type callback functions for thermal zone */
192 static int ti_thermal_get_trip_type(struct thermal_zone_device
*thermal
,
193 int trip
, enum thermal_trip_type
*type
)
195 if (!ti_thermal_is_valid_trip(trip
))
198 if (trip
+ 1 == OMAP_TRIP_NUMBER
)
199 *type
= THERMAL_TRIP_CRITICAL
;
201 *type
= THERMAL_TRIP_PASSIVE
;
206 /* Get trip temperature callback functions for thermal zone */
207 static int ti_thermal_get_trip_temp(struct thermal_zone_device
*thermal
,
208 int trip
, unsigned long *temp
)
210 if (!ti_thermal_is_valid_trip(trip
))
213 *temp
= ti_thermal_get_trip_value(trip
);
218 /* Get critical temperature callback functions for thermal zone */
219 static int ti_thermal_get_crit_temp(struct thermal_zone_device
*thermal
,
223 return ti_thermal_get_trip_temp(thermal
, OMAP_TRIP_NUMBER
- 1, temp
);
226 static struct thermal_zone_device_ops ti_thermal_ops
= {
227 .get_temp
= ti_thermal_get_temp
,
228 /* TODO: add .get_trend */
229 .bind
= ti_thermal_bind
,
230 .unbind
= ti_thermal_unbind
,
231 .get_mode
= ti_thermal_get_mode
,
232 .set_mode
= ti_thermal_set_mode
,
233 .get_trip_type
= ti_thermal_get_trip_type
,
234 .get_trip_temp
= ti_thermal_get_trip_temp
,
235 .get_crit_temp
= ti_thermal_get_crit_temp
,
238 static struct ti_thermal_data
239 *ti_thermal_build_data(struct ti_bandgap
*bgp
, int id
)
241 struct ti_thermal_data
*data
;
243 data
= devm_kzalloc(bgp
->dev
, sizeof(*data
), GFP_KERNEL
);
245 dev_err(bgp
->dev
, "kzalloc fail\n");
248 data
->sensor_id
= id
;
250 data
->mode
= THERMAL_DEVICE_ENABLED
;
251 INIT_WORK(&data
->thermal_wq
, ti_thermal_work
);
256 int ti_thermal_expose_sensor(struct ti_bandgap
*bgp
, int id
,
259 struct ti_thermal_data
*data
;
261 data
= ti_bandgap_get_sensor_data(bgp
, id
);
263 if (IS_ERR_OR_NULL(data
))
264 data
= ti_thermal_build_data(bgp
, id
);
269 /* TODO: remove TC1 TC2 */
270 /* Create thermal zone */
271 data
->ti_thermal
= thermal_zone_device_register(domain
,
272 OMAP_TRIP_NUMBER
, 0, data
, &ti_thermal_ops
,
273 NULL
, FAST_TEMP_MONITORING_RATE
,
274 FAST_TEMP_MONITORING_RATE
);
275 if (IS_ERR_OR_NULL(data
->ti_thermal
)) {
276 dev_err(bgp
->dev
, "thermal zone device is NULL\n");
277 return PTR_ERR(data
->ti_thermal
);
279 data
->ti_thermal
->polling_delay
= FAST_TEMP_MONITORING_RATE
;
280 ti_bandgap_set_sensor_data(bgp
, id
, data
);
285 int ti_thermal_remove_sensor(struct ti_bandgap
*bgp
, int id
)
287 struct ti_thermal_data
*data
;
289 data
= ti_bandgap_get_sensor_data(bgp
, id
);
291 thermal_zone_device_unregister(data
->ti_thermal
);
296 int ti_thermal_report_sensor_temperature(struct ti_bandgap
*bgp
, int id
)
298 struct ti_thermal_data
*data
;
300 data
= ti_bandgap_get_sensor_data(bgp
, id
);
302 schedule_work(&data
->thermal_wq
);
307 int ti_thermal_register_cpu_cooling(struct ti_bandgap
*bgp
, int id
)
309 struct ti_thermal_data
*data
;
311 data
= ti_bandgap_get_sensor_data(bgp
, id
);
312 if (IS_ERR_OR_NULL(data
))
313 data
= ti_thermal_build_data(bgp
, id
);
318 /* Register cooling device */
319 data
->cool_dev
= cpufreq_cooling_register(cpu_present_mask
);
320 if (IS_ERR_OR_NULL(data
->cool_dev
)) {
322 "Failed to register cpufreq cooling device\n");
323 return PTR_ERR(data
->cool_dev
);
325 ti_bandgap_set_sensor_data(bgp
, id
, data
);
330 int ti_thermal_unregister_cpu_cooling(struct ti_bandgap
*bgp
, int id
)
332 struct ti_thermal_data
*data
;
334 data
= ti_bandgap_get_sensor_data(bgp
, id
);
335 cpufreq_cooling_unregister(data
->cool_dev
);