Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 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> |
Eduardo Valentin | 5035d48 | 2012-11-13 14:10:02 -0400 | [diff] [blame] | 32 | #include <linux/cpumask.h> |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 33 | #include <linux/cpu_cooling.h> |
| 34 | |
Eduardo Valentin | 7372add | 2013-03-19 10:54:19 -0400 | [diff] [blame] | 35 | #include "ti-thermal.h" |
| 36 | #include "ti-bandgap.h" |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 37 | |
| 38 | /* common data structures */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 39 | struct ti_thermal_data { |
| 40 | struct thermal_zone_device *ti_thermal; |
Eduardo Valentin | 359836e | 2013-05-29 15:07:41 +0000 | [diff] [blame^] | 41 | struct thermal_zone_device *pcb_tz; |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 42 | struct thermal_cooling_device *cool_dev; |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 43 | struct ti_bandgap *bgp; |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 44 | enum thermal_device_mode mode; |
| 45 | struct work_struct thermal_wq; |
| 46 | int sensor_id; |
| 47 | }; |
| 48 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 49 | static void ti_thermal_work(struct work_struct *work) |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 50 | { |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 51 | struct ti_thermal_data *data = container_of(work, |
| 52 | struct ti_thermal_data, thermal_wq); |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 53 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 54 | thermal_zone_device_update(data->ti_thermal); |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 55 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 56 | dev_dbg(&data->ti_thermal->device, "updated thermal zone %s\n", |
| 57 | data->ti_thermal->type); |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | /** |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 61 | * ti_thermal_hotspot_temperature - returns sensor extrapolated temperature |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 62 | * @t: omap sensor temperature |
| 63 | * @s: omap sensor slope value |
| 64 | * @c: omap sensor const value |
| 65 | */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 66 | static inline int ti_thermal_hotspot_temperature(int t, int s, int c) |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 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*/ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 78 | static inline int ti_thermal_get_temp(struct thermal_zone_device *thermal, |
| 79 | unsigned long *temp) |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 80 | { |
Eduardo Valentin | 359836e | 2013-05-29 15:07:41 +0000 | [diff] [blame^] | 81 | struct thermal_zone_device *pcb_tz = NULL; |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 82 | struct ti_thermal_data *data = thermal->devdata; |
| 83 | struct ti_bandgap *bgp; |
Eduardo Valentin | 9879b2c | 2013-03-19 10:54:23 -0400 | [diff] [blame] | 84 | const struct ti_temp_sensor *s; |
Eduardo Valentin | 359836e | 2013-05-29 15:07:41 +0000 | [diff] [blame^] | 85 | int ret, tmp, slope, constant; |
| 86 | unsigned long pcb_temp; |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 87 | |
Eduardo Valentin | 04a4d10 | 2012-09-11 19:06:55 +0300 | [diff] [blame] | 88 | if (!data) |
| 89 | return 0; |
| 90 | |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 91 | bgp = data->bgp; |
| 92 | s = &bgp->conf->sensors[data->sensor_id]; |
Eduardo Valentin | 04a4d10 | 2012-09-11 19:06:55 +0300 | [diff] [blame] | 93 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 94 | ret = ti_bandgap_read_temperature(bgp, data->sensor_id, &tmp); |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 95 | if (ret) |
| 96 | return ret; |
| 97 | |
Eduardo Valentin | 359836e | 2013-05-29 15:07:41 +0000 | [diff] [blame^] | 98 | /* Default constants */ |
| 99 | slope = s->slope; |
| 100 | constant = s->constant; |
| 101 | |
| 102 | pcb_tz = data->pcb_tz; |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 103 | /* In case pcb zone is available, use the extrapolation rule with it */ |
Eduardo Valentin | 359836e | 2013-05-29 15:07:41 +0000 | [diff] [blame^] | 104 | if (!IS_ERR_OR_NULL(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 | } |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 114 | } |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 115 | *temp = ti_thermal_hotspot_temperature(tmp, slope, constant); |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 116 | |
| 117 | return ret; |
| 118 | } |
| 119 | |
| 120 | /* Bind callback functions for thermal zone */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 121 | static int ti_thermal_bind(struct thermal_zone_device *thermal, |
| 122 | struct thermal_cooling_device *cdev) |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 123 | { |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 124 | struct ti_thermal_data *data = thermal->devdata; |
Eduardo Valentin | 5035d48 | 2012-11-13 14:10:02 -0400 | [diff] [blame] | 125 | int id; |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 126 | |
| 127 | if (IS_ERR_OR_NULL(data)) |
| 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; |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 135 | |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 136 | /* Simple thing, two trips, one passive another critical */ |
Eduardo Valentin | a7a3b8c | 2012-08-23 08:37:59 +0800 | [diff] [blame] | 137 | return thermal_zone_bind_cooling_device(thermal, 0, cdev, |
Eduardo Valentin | 67b4c47 | 2013-04-08 08:19:08 -0400 | [diff] [blame] | 138 | /* bind with min and max states defined by cpu_cooling */ |
Eduardo Valentin | a7a3b8c | 2012-08-23 08:37:59 +0800 | [diff] [blame] | 139 | THERMAL_NO_LIMIT, |
| 140 | THERMAL_NO_LIMIT); |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | /* Unbind callback functions for thermal zone */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 144 | static int ti_thermal_unbind(struct thermal_zone_device *thermal, |
| 145 | struct thermal_cooling_device *cdev) |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 146 | { |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 147 | struct ti_thermal_data *data = thermal->devdata; |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 148 | |
| 149 | if (IS_ERR_OR_NULL(data)) |
| 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 */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 161 | static int ti_thermal_get_mode(struct thermal_zone_device *thermal, |
| 162 | enum thermal_device_mode *mode) |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 163 | { |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 164 | struct ti_thermal_data *data = thermal->devdata; |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 165 | |
| 166 | if (data) |
| 167 | *mode = data->mode; |
| 168 | |
| 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | /* Set mode callback functions for thermal zone */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 173 | static int ti_thermal_set_mode(struct thermal_zone_device *thermal, |
| 174 | enum thermal_device_mode mode) |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 175 | { |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 176 | struct ti_thermal_data *data = thermal->devdata; |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 177 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 178 | if (!data->ti_thermal) { |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 179 | dev_notice(&thermal->device, "thermal zone not registered\n"); |
| 180 | return 0; |
| 181 | } |
| 182 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 183 | mutex_lock(&data->ti_thermal->lock); |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 184 | |
| 185 | if (mode == THERMAL_DEVICE_ENABLED) |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 186 | data->ti_thermal->polling_delay = FAST_TEMP_MONITORING_RATE; |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 187 | else |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 188 | data->ti_thermal->polling_delay = 0; |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 189 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 190 | mutex_unlock(&data->ti_thermal->lock); |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 191 | |
| 192 | data->mode = mode; |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 193 | thermal_zone_device_update(data->ti_thermal); |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 194 | dev_dbg(&thermal->device, "thermal polling set for duration=%d msec\n", |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 195 | data->ti_thermal->polling_delay); |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 196 | |
| 197 | return 0; |
| 198 | } |
| 199 | |
| 200 | /* Get trip type callback functions for thermal zone */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 201 | static int ti_thermal_get_trip_type(struct thermal_zone_device *thermal, |
| 202 | int trip, enum thermal_trip_type *type) |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 203 | { |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 204 | if (!ti_thermal_is_valid_trip(trip)) |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 205 | return -EINVAL; |
| 206 | |
| 207 | if (trip + 1 == OMAP_TRIP_NUMBER) |
| 208 | *type = THERMAL_TRIP_CRITICAL; |
| 209 | else |
| 210 | *type = THERMAL_TRIP_PASSIVE; |
| 211 | |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | /* Get trip temperature callback functions for thermal zone */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 216 | static int ti_thermal_get_trip_temp(struct thermal_zone_device *thermal, |
| 217 | int trip, unsigned long *temp) |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 218 | { |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 219 | if (!ti_thermal_is_valid_trip(trip)) |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 220 | return -EINVAL; |
| 221 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 222 | *temp = ti_thermal_get_trip_value(trip); |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 223 | |
| 224 | return 0; |
| 225 | } |
| 226 | |
J Keerthy | 03b7f67 | 2013-04-01 12:04:46 -0400 | [diff] [blame] | 227 | /* Get the temperature trend callback functions for thermal zone */ |
| 228 | static int ti_thermal_get_trend(struct thermal_zone_device *thermal, |
| 229 | int trip, enum thermal_trend *trend) |
| 230 | { |
| 231 | struct ti_thermal_data *data = thermal->devdata; |
| 232 | struct ti_bandgap *bgp; |
| 233 | int id, tr, ret = 0; |
| 234 | |
| 235 | bgp = data->bgp; |
| 236 | id = data->sensor_id; |
| 237 | |
| 238 | ret = ti_bandgap_get_trend(bgp, id, &tr); |
| 239 | if (ret) |
| 240 | return ret; |
| 241 | |
| 242 | if (tr > 0) |
| 243 | *trend = THERMAL_TREND_RAISING; |
| 244 | else if (tr < 0) |
| 245 | *trend = THERMAL_TREND_DROPPING; |
| 246 | else |
| 247 | *trend = THERMAL_TREND_STABLE; |
| 248 | |
| 249 | return 0; |
| 250 | } |
| 251 | |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 252 | /* Get critical temperature callback functions for thermal zone */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 253 | static int ti_thermal_get_crit_temp(struct thermal_zone_device *thermal, |
| 254 | unsigned long *temp) |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 255 | { |
| 256 | /* shutdown zone */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 257 | return ti_thermal_get_trip_temp(thermal, OMAP_TRIP_NUMBER - 1, temp); |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 258 | } |
| 259 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 260 | static struct thermal_zone_device_ops ti_thermal_ops = { |
| 261 | .get_temp = ti_thermal_get_temp, |
J Keerthy | 03b7f67 | 2013-04-01 12:04:46 -0400 | [diff] [blame] | 262 | .get_trend = ti_thermal_get_trend, |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 263 | .bind = ti_thermal_bind, |
| 264 | .unbind = ti_thermal_unbind, |
| 265 | .get_mode = ti_thermal_get_mode, |
| 266 | .set_mode = ti_thermal_set_mode, |
| 267 | .get_trip_type = ti_thermal_get_trip_type, |
| 268 | .get_trip_temp = ti_thermal_get_trip_temp, |
| 269 | .get_crit_temp = ti_thermal_get_crit_temp, |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 270 | }; |
| 271 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 272 | static struct ti_thermal_data |
| 273 | *ti_thermal_build_data(struct ti_bandgap *bgp, int id) |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 274 | { |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 275 | struct ti_thermal_data *data; |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 276 | |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 277 | data = devm_kzalloc(bgp->dev, sizeof(*data), GFP_KERNEL); |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 278 | if (!data) { |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 279 | dev_err(bgp->dev, "kzalloc fail\n"); |
Eduardo Valentin | 04a4d10 | 2012-09-11 19:06:55 +0300 | [diff] [blame] | 280 | return NULL; |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 281 | } |
| 282 | data->sensor_id = id; |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 283 | data->bgp = bgp; |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 284 | data->mode = THERMAL_DEVICE_ENABLED; |
Eduardo Valentin | 359836e | 2013-05-29 15:07:41 +0000 | [diff] [blame^] | 285 | data->pcb_tz = thermal_zone_get_zone_by_name("pcb"); |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 286 | INIT_WORK(&data->thermal_wq, ti_thermal_work); |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 287 | |
Eduardo Valentin | 04a4d10 | 2012-09-11 19:06:55 +0300 | [diff] [blame] | 288 | return data; |
| 289 | } |
| 290 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 291 | int ti_thermal_expose_sensor(struct ti_bandgap *bgp, int id, |
| 292 | char *domain) |
Eduardo Valentin | 04a4d10 | 2012-09-11 19:06:55 +0300 | [diff] [blame] | 293 | { |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 294 | struct ti_thermal_data *data; |
Eduardo Valentin | 04a4d10 | 2012-09-11 19:06:55 +0300 | [diff] [blame] | 295 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 296 | data = ti_bandgap_get_sensor_data(bgp, id); |
Eduardo Valentin | 04a4d10 | 2012-09-11 19:06:55 +0300 | [diff] [blame] | 297 | |
Eduardo Valentin | 35b052a | 2013-03-15 08:59:54 -0400 | [diff] [blame] | 298 | if (IS_ERR_OR_NULL(data)) |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 299 | data = ti_thermal_build_data(bgp, id); |
Eduardo Valentin | 04a4d10 | 2012-09-11 19:06:55 +0300 | [diff] [blame] | 300 | |
| 301 | if (!data) |
| 302 | return -EINVAL; |
| 303 | |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 304 | /* Create thermal zone */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 305 | data->ti_thermal = thermal_zone_device_register(domain, |
| 306 | OMAP_TRIP_NUMBER, 0, data, &ti_thermal_ops, |
Durgadoss R | 50125a9 | 2012-09-18 11:04:56 +0530 | [diff] [blame] | 307 | NULL, FAST_TEMP_MONITORING_RATE, |
Eduardo Valentin | 765a193 | 2012-09-11 19:06:54 +0300 | [diff] [blame] | 308 | FAST_TEMP_MONITORING_RATE); |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 309 | if (IS_ERR_OR_NULL(data->ti_thermal)) { |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 310 | dev_err(bgp->dev, "thermal zone device is NULL\n"); |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 311 | return PTR_ERR(data->ti_thermal); |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 312 | } |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 313 | data->ti_thermal->polling_delay = FAST_TEMP_MONITORING_RATE; |
| 314 | ti_bandgap_set_sensor_data(bgp, id, data); |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 315 | |
| 316 | return 0; |
| 317 | } |
| 318 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 319 | int ti_thermal_remove_sensor(struct ti_bandgap *bgp, int id) |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 320 | { |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 321 | struct ti_thermal_data *data; |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 322 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 323 | data = ti_bandgap_get_sensor_data(bgp, id); |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 324 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 325 | thermal_zone_device_unregister(data->ti_thermal); |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 326 | |
| 327 | return 0; |
| 328 | } |
| 329 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 330 | int ti_thermal_report_sensor_temperature(struct ti_bandgap *bgp, int id) |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 331 | { |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 332 | struct ti_thermal_data *data; |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 333 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 334 | data = ti_bandgap_get_sensor_data(bgp, id); |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 335 | |
| 336 | schedule_work(&data->thermal_wq); |
| 337 | |
| 338 | return 0; |
| 339 | } |
| 340 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 341 | int ti_thermal_register_cpu_cooling(struct ti_bandgap *bgp, int id) |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 342 | { |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 343 | struct ti_thermal_data *data; |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 344 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 345 | data = ti_bandgap_get_sensor_data(bgp, id); |
Eduardo Valentin | 35b052a | 2013-03-15 08:59:54 -0400 | [diff] [blame] | 346 | if (IS_ERR_OR_NULL(data)) |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 347 | data = ti_thermal_build_data(bgp, id); |
Eduardo Valentin | 04a4d10 | 2012-09-11 19:06:55 +0300 | [diff] [blame] | 348 | |
| 349 | if (!data) |
| 350 | return -EINVAL; |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 351 | |
Eduardo Valentin | f155333 | 2013-04-08 08:19:13 -0400 | [diff] [blame] | 352 | if (!cpufreq_get_current_driver()) { |
| 353 | dev_dbg(bgp->dev, "no cpufreq driver yet\n"); |
| 354 | return -EPROBE_DEFER; |
| 355 | } |
| 356 | |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 357 | /* Register cooling device */ |
Eduardo Valentin | 5035d48 | 2012-11-13 14:10:02 -0400 | [diff] [blame] | 358 | data->cool_dev = cpufreq_cooling_register(cpu_present_mask); |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 359 | if (IS_ERR_OR_NULL(data->cool_dev)) { |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 360 | dev_err(bgp->dev, |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 361 | "Failed to register cpufreq cooling device\n"); |
| 362 | return PTR_ERR(data->cool_dev); |
| 363 | } |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 364 | ti_bandgap_set_sensor_data(bgp, id, data); |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 365 | |
| 366 | return 0; |
| 367 | } |
| 368 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 369 | int ti_thermal_unregister_cpu_cooling(struct ti_bandgap *bgp, int id) |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 370 | { |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 371 | struct ti_thermal_data *data; |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 372 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 373 | data = ti_bandgap_get_sensor_data(bgp, id); |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 374 | cpufreq_cooling_unregister(data->cool_dev); |
| 375 | |
| 376 | return 0; |
| 377 | } |