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> |
Eduardo Valentin | 5035d48 | 2012-11-13 14:10:02 -0400 | [diff] [blame] | 31 | #include <linux/cpumask.h> |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 32 | #include <linux/cpu_cooling.h> |
Eduardo Valentin | 26d9cc6 | 2013-07-04 16:43:54 -0400 | [diff] [blame] | 33 | #include <linux/of.h> |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 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; |
Eduardo Valentin | 26d9cc6 | 2013-07-04 16:43:54 -0400 | [diff] [blame] | 47 | bool our_zone; |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 48 | }; |
| 49 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 50 | static void ti_thermal_work(struct work_struct *work) |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 51 | { |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 52 | struct ti_thermal_data *data = container_of(work, |
| 53 | struct ti_thermal_data, thermal_wq); |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 54 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 55 | thermal_zone_device_update(data->ti_thermal); |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 56 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 57 | dev_dbg(&data->ti_thermal->device, "updated thermal zone %s\n", |
| 58 | data->ti_thermal->type); |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | /** |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 62 | * ti_thermal_hotspot_temperature - returns sensor extrapolated temperature |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 63 | * @t: omap sensor temperature |
| 64 | * @s: omap sensor slope value |
| 65 | * @c: omap sensor const value |
| 66 | */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 67 | 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] | 68 | { |
| 69 | int delta = t * s / 1000 + c; |
| 70 | |
| 71 | if (delta < 0) |
| 72 | delta = 0; |
| 73 | |
| 74 | return t + delta; |
| 75 | } |
| 76 | |
| 77 | /* thermal zone ops */ |
| 78 | /* Get temperature callback function for thermal zone*/ |
Eduardo Valentin | 26d9cc6 | 2013-07-04 16:43:54 -0400 | [diff] [blame] | 79 | static inline int __ti_thermal_get_temp(void *devdata, 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 | 26d9cc6 | 2013-07-04 16:43:54 -0400 | [diff] [blame] | 82 | struct ti_thermal_data *data = devdata; |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 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 | 0c12b5a | 2013-05-29 15:07:43 +0000 | [diff] [blame] | 104 | if (!IS_ERR(pcb_tz)) { |
Eduardo Valentin | 359836e | 2013-05-29 15:07:41 +0000 | [diff] [blame] | 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"); |
Eduardo Valentin | df8f134 | 2013-09-15 15:21:51 -0400 | [diff] [blame] | 113 | ret = 0; |
Eduardo Valentin | 359836e | 2013-05-29 15:07:41 +0000 | [diff] [blame] | 114 | } |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 115 | } |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 116 | *temp = ti_thermal_hotspot_temperature(tmp, slope, constant); |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 117 | |
| 118 | return ret; |
| 119 | } |
| 120 | |
Eduardo Valentin | 26d9cc6 | 2013-07-04 16:43:54 -0400 | [diff] [blame] | 121 | static inline int ti_thermal_get_temp(struct thermal_zone_device *thermal, |
| 122 | unsigned long *temp) |
| 123 | { |
| 124 | struct ti_thermal_data *data = thermal->devdata; |
| 125 | |
| 126 | return __ti_thermal_get_temp(data, temp); |
| 127 | } |
| 128 | |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 129 | /* Bind callback functions for thermal zone */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 130 | static int ti_thermal_bind(struct thermal_zone_device *thermal, |
| 131 | struct thermal_cooling_device *cdev) |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 132 | { |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 133 | struct ti_thermal_data *data = thermal->devdata; |
Eduardo Valentin | 5035d48 | 2012-11-13 14:10:02 -0400 | [diff] [blame] | 134 | int id; |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 135 | |
Eduardo Valentin | 0c12b5a | 2013-05-29 15:07:43 +0000 | [diff] [blame] | 136 | if (!data || IS_ERR(data)) |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 137 | return -ENODEV; |
| 138 | |
| 139 | /* check if this is the cooling device we registered */ |
| 140 | if (data->cool_dev != cdev) |
| 141 | return 0; |
| 142 | |
| 143 | id = data->sensor_id; |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 144 | |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 145 | /* Simple thing, two trips, one passive another critical */ |
Eduardo Valentin | a7a3b8c | 2012-08-23 08:37:59 +0800 | [diff] [blame] | 146 | return thermal_zone_bind_cooling_device(thermal, 0, cdev, |
Eduardo Valentin | 67b4c47 | 2013-04-08 08:19:08 -0400 | [diff] [blame] | 147 | /* bind with min and max states defined by cpu_cooling */ |
Eduardo Valentin | a7a3b8c | 2012-08-23 08:37:59 +0800 | [diff] [blame] | 148 | THERMAL_NO_LIMIT, |
| 149 | THERMAL_NO_LIMIT); |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | /* Unbind callback functions for thermal zone */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 153 | static int ti_thermal_unbind(struct thermal_zone_device *thermal, |
| 154 | struct thermal_cooling_device *cdev) |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 155 | { |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 156 | struct ti_thermal_data *data = thermal->devdata; |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 157 | |
Eduardo Valentin | 0c12b5a | 2013-05-29 15:07:43 +0000 | [diff] [blame] | 158 | if (!data || IS_ERR(data)) |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 159 | return -ENODEV; |
| 160 | |
| 161 | /* check if this is the cooling device we registered */ |
| 162 | if (data->cool_dev != cdev) |
| 163 | return 0; |
| 164 | |
| 165 | /* Simple thing, two trips, one passive another critical */ |
| 166 | return thermal_zone_unbind_cooling_device(thermal, 0, cdev); |
| 167 | } |
| 168 | |
| 169 | /* Get mode callback functions for thermal zone */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 170 | static int ti_thermal_get_mode(struct thermal_zone_device *thermal, |
| 171 | enum thermal_device_mode *mode) |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 172 | { |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 173 | struct ti_thermal_data *data = thermal->devdata; |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 174 | |
| 175 | if (data) |
| 176 | *mode = data->mode; |
| 177 | |
| 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | /* Set mode callback functions for thermal zone */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 182 | static int ti_thermal_set_mode(struct thermal_zone_device *thermal, |
| 183 | enum thermal_device_mode mode) |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 184 | { |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 185 | struct ti_thermal_data *data = thermal->devdata; |
Ranganath Krishnan | 10ccff1 | 2013-08-23 11:08:22 -0500 | [diff] [blame] | 186 | struct ti_bandgap *bgp; |
| 187 | |
| 188 | bgp = data->bgp; |
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 | if (!data->ti_thermal) { |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 191 | dev_notice(&thermal->device, "thermal zone not registered\n"); |
| 192 | return 0; |
| 193 | } |
| 194 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 195 | mutex_lock(&data->ti_thermal->lock); |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 196 | |
| 197 | if (mode == THERMAL_DEVICE_ENABLED) |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 198 | data->ti_thermal->polling_delay = FAST_TEMP_MONITORING_RATE; |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 199 | else |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 200 | data->ti_thermal->polling_delay = 0; |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 201 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 202 | mutex_unlock(&data->ti_thermal->lock); |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 203 | |
| 204 | data->mode = mode; |
Ranganath Krishnan | 10ccff1 | 2013-08-23 11:08:22 -0500 | [diff] [blame] | 205 | ti_bandgap_write_update_interval(bgp, data->sensor_id, |
| 206 | data->ti_thermal->polling_delay); |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 207 | thermal_zone_device_update(data->ti_thermal); |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 208 | dev_dbg(&thermal->device, "thermal polling set for duration=%d msec\n", |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 209 | data->ti_thermal->polling_delay); |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 210 | |
| 211 | return 0; |
| 212 | } |
| 213 | |
| 214 | /* Get trip type callback functions for thermal zone */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 215 | static int ti_thermal_get_trip_type(struct thermal_zone_device *thermal, |
| 216 | int trip, enum thermal_trip_type *type) |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 217 | { |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 218 | if (!ti_thermal_is_valid_trip(trip)) |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 219 | return -EINVAL; |
| 220 | |
| 221 | if (trip + 1 == OMAP_TRIP_NUMBER) |
| 222 | *type = THERMAL_TRIP_CRITICAL; |
| 223 | else |
| 224 | *type = THERMAL_TRIP_PASSIVE; |
| 225 | |
| 226 | return 0; |
| 227 | } |
| 228 | |
| 229 | /* Get trip temperature callback functions for thermal zone */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 230 | static int ti_thermal_get_trip_temp(struct thermal_zone_device *thermal, |
| 231 | int trip, unsigned long *temp) |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 232 | { |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 233 | if (!ti_thermal_is_valid_trip(trip)) |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 234 | return -EINVAL; |
| 235 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 236 | *temp = ti_thermal_get_trip_value(trip); |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 237 | |
| 238 | return 0; |
| 239 | } |
| 240 | |
Eduardo Valentin | 26d9cc6 | 2013-07-04 16:43:54 -0400 | [diff] [blame] | 241 | static int __ti_thermal_get_trend(void *p, long *trend) |
J Keerthy | 03b7f67 | 2013-04-01 12:04:46 -0400 | [diff] [blame] | 242 | { |
Eduardo Valentin | 26d9cc6 | 2013-07-04 16:43:54 -0400 | [diff] [blame] | 243 | struct ti_thermal_data *data = p; |
J Keerthy | 03b7f67 | 2013-04-01 12:04:46 -0400 | [diff] [blame] | 244 | struct ti_bandgap *bgp; |
| 245 | int id, tr, ret = 0; |
| 246 | |
| 247 | bgp = data->bgp; |
| 248 | id = data->sensor_id; |
| 249 | |
| 250 | ret = ti_bandgap_get_trend(bgp, id, &tr); |
| 251 | if (ret) |
| 252 | return ret; |
| 253 | |
Eduardo Valentin | 26d9cc6 | 2013-07-04 16:43:54 -0400 | [diff] [blame] | 254 | *trend = tr; |
| 255 | |
| 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | /* Get the temperature trend callback functions for thermal zone */ |
| 260 | static int ti_thermal_get_trend(struct thermal_zone_device *thermal, |
| 261 | int trip, enum thermal_trend *trend) |
| 262 | { |
| 263 | int ret; |
| 264 | long tr; |
| 265 | |
| 266 | ret = __ti_thermal_get_trend(thermal->devdata, &tr); |
| 267 | if (ret) |
| 268 | return ret; |
| 269 | |
J Keerthy | 03b7f67 | 2013-04-01 12:04:46 -0400 | [diff] [blame] | 270 | if (tr > 0) |
| 271 | *trend = THERMAL_TREND_RAISING; |
| 272 | else if (tr < 0) |
| 273 | *trend = THERMAL_TREND_DROPPING; |
| 274 | else |
| 275 | *trend = THERMAL_TREND_STABLE; |
| 276 | |
| 277 | return 0; |
| 278 | } |
| 279 | |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 280 | /* Get critical temperature callback functions for thermal zone */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 281 | static int ti_thermal_get_crit_temp(struct thermal_zone_device *thermal, |
| 282 | unsigned long *temp) |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 283 | { |
| 284 | /* shutdown zone */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 285 | return ti_thermal_get_trip_temp(thermal, OMAP_TRIP_NUMBER - 1, temp); |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 286 | } |
| 287 | |
Eduardo Valentin | 2251aef | 2014-11-07 21:24:39 -0400 | [diff] [blame] | 288 | static const struct thermal_zone_of_device_ops ti_of_thermal_ops = { |
| 289 | .get_temp = __ti_thermal_get_temp, |
| 290 | .get_trend = __ti_thermal_get_trend, |
| 291 | }; |
| 292 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 293 | static struct thermal_zone_device_ops ti_thermal_ops = { |
| 294 | .get_temp = ti_thermal_get_temp, |
J Keerthy | 03b7f67 | 2013-04-01 12:04:46 -0400 | [diff] [blame] | 295 | .get_trend = ti_thermal_get_trend, |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 296 | .bind = ti_thermal_bind, |
| 297 | .unbind = ti_thermal_unbind, |
| 298 | .get_mode = ti_thermal_get_mode, |
| 299 | .set_mode = ti_thermal_set_mode, |
| 300 | .get_trip_type = ti_thermal_get_trip_type, |
| 301 | .get_trip_temp = ti_thermal_get_trip_temp, |
| 302 | .get_crit_temp = ti_thermal_get_crit_temp, |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 303 | }; |
| 304 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 305 | static struct ti_thermal_data |
| 306 | *ti_thermal_build_data(struct ti_bandgap *bgp, int id) |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 307 | { |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 308 | struct ti_thermal_data *data; |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 309 | |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 310 | data = devm_kzalloc(bgp->dev, sizeof(*data), GFP_KERNEL); |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 311 | if (!data) { |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 312 | dev_err(bgp->dev, "kzalloc fail\n"); |
Eduardo Valentin | 04a4d10 | 2012-09-11 19:06:55 +0300 | [diff] [blame] | 313 | return NULL; |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 314 | } |
| 315 | data->sensor_id = id; |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 316 | data->bgp = bgp; |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 317 | data->mode = THERMAL_DEVICE_ENABLED; |
Eduardo Valentin | 0c12b5a | 2013-05-29 15:07:43 +0000 | [diff] [blame] | 318 | /* pcb_tz will be either valid or PTR_ERR() */ |
Eduardo Valentin | 359836e | 2013-05-29 15:07:41 +0000 | [diff] [blame] | 319 | data->pcb_tz = thermal_zone_get_zone_by_name("pcb"); |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 320 | INIT_WORK(&data->thermal_wq, ti_thermal_work); |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 321 | |
Eduardo Valentin | 04a4d10 | 2012-09-11 19:06:55 +0300 | [diff] [blame] | 322 | return data; |
| 323 | } |
| 324 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 325 | int ti_thermal_expose_sensor(struct ti_bandgap *bgp, int id, |
| 326 | char *domain) |
Eduardo Valentin | 04a4d10 | 2012-09-11 19:06:55 +0300 | [diff] [blame] | 327 | { |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 328 | struct ti_thermal_data *data; |
Eduardo Valentin | 04a4d10 | 2012-09-11 19:06:55 +0300 | [diff] [blame] | 329 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 330 | data = ti_bandgap_get_sensor_data(bgp, id); |
Eduardo Valentin | 04a4d10 | 2012-09-11 19:06:55 +0300 | [diff] [blame] | 331 | |
Eduardo Valentin | 0c12b5a | 2013-05-29 15:07:43 +0000 | [diff] [blame] | 332 | if (!data || IS_ERR(data)) |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 333 | data = ti_thermal_build_data(bgp, id); |
Eduardo Valentin | 04a4d10 | 2012-09-11 19:06:55 +0300 | [diff] [blame] | 334 | |
| 335 | if (!data) |
| 336 | return -EINVAL; |
| 337 | |
Eduardo Valentin | 26d9cc6 | 2013-07-04 16:43:54 -0400 | [diff] [blame] | 338 | /* in case this is specified by DT */ |
| 339 | data->ti_thermal = thermal_zone_of_sensor_register(bgp->dev, id, |
Eduardo Valentin | 2251aef | 2014-11-07 21:24:39 -0400 | [diff] [blame] | 340 | data, &ti_of_thermal_ops); |
Eduardo Valentin | 26d9cc6 | 2013-07-04 16:43:54 -0400 | [diff] [blame] | 341 | if (IS_ERR(data->ti_thermal)) { |
| 342 | /* Create thermal zone */ |
| 343 | data->ti_thermal = thermal_zone_device_register(domain, |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 344 | OMAP_TRIP_NUMBER, 0, data, &ti_thermal_ops, |
Durgadoss R | 50125a9 | 2012-09-18 11:04:56 +0530 | [diff] [blame] | 345 | NULL, FAST_TEMP_MONITORING_RATE, |
Eduardo Valentin | 765a193 | 2012-09-11 19:06:54 +0300 | [diff] [blame] | 346 | FAST_TEMP_MONITORING_RATE); |
Eduardo Valentin | 26d9cc6 | 2013-07-04 16:43:54 -0400 | [diff] [blame] | 347 | if (IS_ERR(data->ti_thermal)) { |
| 348 | dev_err(bgp->dev, "thermal zone device is NULL\n"); |
| 349 | return PTR_ERR(data->ti_thermal); |
| 350 | } |
| 351 | data->ti_thermal->polling_delay = FAST_TEMP_MONITORING_RATE; |
| 352 | data->our_zone = true; |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 353 | } |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 354 | ti_bandgap_set_sensor_data(bgp, id, data); |
Ranganath Krishnan | 10ccff1 | 2013-08-23 11:08:22 -0500 | [diff] [blame] | 355 | ti_bandgap_write_update_interval(bgp, data->sensor_id, |
| 356 | data->ti_thermal->polling_delay); |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 357 | |
| 358 | return 0; |
| 359 | } |
| 360 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 361 | int ti_thermal_remove_sensor(struct ti_bandgap *bgp, int id) |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 362 | { |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 363 | struct ti_thermal_data *data; |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 364 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 365 | data = ti_bandgap_get_sensor_data(bgp, id); |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 366 | |
Eduardo Valentin | 26d9cc6 | 2013-07-04 16:43:54 -0400 | [diff] [blame] | 367 | if (data && data->ti_thermal) { |
| 368 | if (data->our_zone) |
| 369 | thermal_zone_device_unregister(data->ti_thermal); |
| 370 | else |
| 371 | thermal_zone_of_sensor_unregister(bgp->dev, |
| 372 | data->ti_thermal); |
| 373 | } |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 374 | |
| 375 | return 0; |
| 376 | } |
| 377 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 378 | int ti_thermal_report_sensor_temperature(struct ti_bandgap *bgp, int id) |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 379 | { |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 380 | struct ti_thermal_data *data; |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 381 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 382 | data = ti_bandgap_get_sensor_data(bgp, id); |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 383 | |
| 384 | schedule_work(&data->thermal_wq); |
| 385 | |
| 386 | return 0; |
| 387 | } |
| 388 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 389 | int ti_thermal_register_cpu_cooling(struct ti_bandgap *bgp, int id) |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 390 | { |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 391 | struct ti_thermal_data *data; |
Eduardo Valentin | 26d9cc6 | 2013-07-04 16:43:54 -0400 | [diff] [blame] | 392 | struct device_node *np = bgp->dev->of_node; |
| 393 | |
| 394 | /* |
| 395 | * We are assuming here that if one deploys the zone |
| 396 | * using DT, then it must be aware that the cooling device |
| 397 | * loading has to happen via cpufreq driver. |
| 398 | */ |
| 399 | if (of_find_property(np, "#thermal-sensor-cells", NULL)) |
| 400 | return 0; |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 401 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 402 | data = ti_bandgap_get_sensor_data(bgp, id); |
Eduardo Valentin | 0c12b5a | 2013-05-29 15:07:43 +0000 | [diff] [blame] | 403 | if (!data || IS_ERR(data)) |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 404 | data = ti_thermal_build_data(bgp, id); |
Eduardo Valentin | 04a4d10 | 2012-09-11 19:06:55 +0300 | [diff] [blame] | 405 | |
| 406 | if (!data) |
| 407 | return -EINVAL; |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 408 | |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 409 | /* Register cooling device */ |
Eduardo Valentin | 5035d48 | 2012-11-13 14:10:02 -0400 | [diff] [blame] | 410 | data->cool_dev = cpufreq_cooling_register(cpu_present_mask); |
Eduardo Valentin | 0c12b5a | 2013-05-29 15:07:43 +0000 | [diff] [blame] | 411 | if (IS_ERR(data->cool_dev)) { |
Eduardo Valentin | cffafc3 | 2014-12-12 10:05:39 -0400 | [diff] [blame] | 412 | int ret = PTR_ERR(data->cool_dev); |
| 413 | |
| 414 | if (ret != -EPROBE_DEFER) |
| 415 | dev_err(bgp->dev, |
| 416 | "Failed to register cpu cooling device %d\n", |
| 417 | ret); |
| 418 | |
| 419 | return ret; |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 420 | } |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 421 | ti_bandgap_set_sensor_data(bgp, id, data); |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 422 | |
| 423 | return 0; |
| 424 | } |
| 425 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 426 | int ti_thermal_unregister_cpu_cooling(struct ti_bandgap *bgp, int id) |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 427 | { |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 428 | struct ti_thermal_data *data; |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 429 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 430 | data = ti_bandgap_get_sensor_data(bgp, id); |
Eduardo Valentin | 26d9cc6 | 2013-07-04 16:43:54 -0400 | [diff] [blame] | 431 | |
Markus Elfring | 9ca9be2 | 2015-02-03 11:15:14 +0100 | [diff] [blame^] | 432 | if (data) |
Eduardo Valentin | 26d9cc6 | 2013-07-04 16:43:54 -0400 | [diff] [blame] | 433 | cpufreq_cooling_unregister(data->cool_dev); |
Eduardo Valentin | 445eaf8 | 2012-07-12 19:02:30 +0300 | [diff] [blame] | 434 | |
| 435 | return 0; |
| 436 | } |