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/cpumask.h>
32 #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;
50 static void ti_thermal_work(struct work_struct *work)
52 struct ti_thermal_data *data = container_of(work,
53 struct ti_thermal_data, thermal_wq);
55 thermal_zone_device_update(data->ti_thermal, THERMAL_EVENT_UNSPECIFIED);
57 dev_dbg(&data->ti_thermal->device, "updated thermal zone %s\n",
58 data->ti_thermal->type);
62 * ti_thermal_hotspot_temperature - returns sensor extrapolated temperature
63 * @t: omap sensor temperature
64 * @s: omap sensor slope value
65 * @c: omap sensor const value
67 static inline int ti_thermal_hotspot_temperature(int t, int s, int c)
69 int delta = t * s / 1000 + c;
77 /* thermal zone ops */
78 /* Get temperature callback function for thermal zone */
79 static inline int __ti_thermal_get_temp(void *devdata, int *temp)
81 struct thermal_zone_device *pcb_tz = NULL;
82 struct ti_thermal_data *data = devdata;
83 struct ti_bandgap *bgp;
84 const struct ti_temp_sensor *s;
85 int ret, tmp, slope, constant;
92 s = &bgp->conf->sensors[data->sensor_id];
94 ret = ti_bandgap_read_temperature(bgp, data->sensor_id, &tmp);
98 /* Default constants */
99 slope = thermal_zone_get_slope(data->ti_thermal);
100 constant = thermal_zone_get_offset(data->ti_thermal);
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);
107 tmp -= pcb_temp; /* got a valid PCB temp */
108 slope = s->slope_pcb;
109 constant = s->constant_pcb;
112 "Failed to read PCB state. Using defaults\n");
116 *temp = ti_thermal_hotspot_temperature(tmp, slope, constant);
121 static inline int ti_thermal_get_temp(struct thermal_zone_device *thermal,
124 struct ti_thermal_data *data = thermal->devdata;
126 return __ti_thermal_get_temp(data, temp);
129 static int __ti_thermal_get_trend(void *p, int trip, enum thermal_trend *trend)
131 struct ti_thermal_data *data = p;
132 struct ti_bandgap *bgp;
136 id = data->sensor_id;
138 ret = ti_bandgap_get_trend(bgp, id, &tr);
143 *trend = THERMAL_TREND_RAISING;
145 *trend = THERMAL_TREND_DROPPING;
147 *trend = THERMAL_TREND_STABLE;
152 static const struct thermal_zone_of_device_ops ti_of_thermal_ops = {
153 .get_temp = __ti_thermal_get_temp,
154 .get_trend = __ti_thermal_get_trend,
157 static struct ti_thermal_data
158 *ti_thermal_build_data(struct ti_bandgap *bgp, int id)
160 struct ti_thermal_data *data;
162 data = devm_kzalloc(bgp->dev, sizeof(*data), GFP_KERNEL);
164 dev_err(bgp->dev, "kzalloc fail\n");
167 data->sensor_id = id;
169 data->mode = THERMAL_DEVICE_ENABLED;
170 /* pcb_tz will be either valid or PTR_ERR() */
171 data->pcb_tz = thermal_zone_get_zone_by_name("pcb");
172 INIT_WORK(&data->thermal_wq, ti_thermal_work);
177 int ti_thermal_expose_sensor(struct ti_bandgap *bgp, int id,
180 struct ti_thermal_data *data;
182 data = ti_bandgap_get_sensor_data(bgp, id);
184 if (!data || IS_ERR(data))
185 data = ti_thermal_build_data(bgp, id);
190 /* in case this is specified by DT */
191 data->ti_thermal = devm_thermal_zone_of_sensor_register(bgp->dev, id,
192 data, &ti_of_thermal_ops);
193 if (IS_ERR(data->ti_thermal)) {
194 dev_err(bgp->dev, "thermal zone device is NULL\n");
195 return PTR_ERR(data->ti_thermal);
198 ti_bandgap_set_sensor_data(bgp, id, data);
199 ti_bandgap_write_update_interval(bgp, data->sensor_id,
200 data->ti_thermal->polling_delay);
205 int ti_thermal_remove_sensor(struct ti_bandgap *bgp, int id)
207 struct ti_thermal_data *data;
209 data = ti_bandgap_get_sensor_data(bgp, id);
211 if (data && data->ti_thermal) {
213 thermal_zone_device_unregister(data->ti_thermal);
219 int ti_thermal_report_sensor_temperature(struct ti_bandgap *bgp, int id)
221 struct ti_thermal_data *data;
223 data = ti_bandgap_get_sensor_data(bgp, id);
225 schedule_work(&data->thermal_wq);
230 int ti_thermal_register_cpu_cooling(struct ti_bandgap *bgp, int id)
232 struct ti_thermal_data *data;
233 struct device_node *np = bgp->dev->of_node;
236 * We are assuming here that if one deploys the zone
237 * using DT, then it must be aware that the cooling device
238 * loading has to happen via cpufreq driver.
240 if (of_find_property(np, "#thermal-sensor-cells", NULL))
243 data = ti_bandgap_get_sensor_data(bgp, id);
244 if (!data || IS_ERR(data))
245 data = ti_thermal_build_data(bgp, id);
250 /* Register cooling device */
251 data->cool_dev = cpufreq_cooling_register(cpu_present_mask);
252 if (IS_ERR(data->cool_dev)) {
253 int ret = PTR_ERR(data->cool_dev);
255 if (ret != -EPROBE_DEFER)
257 "Failed to register cpu cooling device %d\n",
262 ti_bandgap_set_sensor_data(bgp, id, data);
267 int ti_thermal_unregister_cpu_cooling(struct ti_bandgap *bgp, int id)
269 struct ti_thermal_data *data;
271 data = ti_bandgap_get_sensor_data(bgp, id);
274 cpufreq_cooling_unregister(data->cool_dev);