Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1 | /* |
| 2 | * thermal.c - Generic Thermal Management Sysfs support. |
| 3 | * |
| 4 | * Copyright (C) 2008 Intel Corp |
| 5 | * Copyright (C) 2008 Zhang Rui <rui.zhang@intel.com> |
| 6 | * Copyright (C) 2008 Sujith Thomas <sujith.thomas@intel.com> |
| 7 | * |
| 8 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; version 2 of the License. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, but |
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | * General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License along |
| 20 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 21 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
| 22 | * |
| 23 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 24 | */ |
| 25 | |
| 26 | #include <linux/module.h> |
| 27 | #include <linux/device.h> |
| 28 | #include <linux/err.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 29 | #include <linux/slab.h> |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 30 | #include <linux/kdev_t.h> |
| 31 | #include <linux/idr.h> |
| 32 | #include <linux/thermal.h> |
| 33 | #include <linux/spinlock.h> |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 34 | #include <linux/reboot.h> |
R.Durgadoss | 4cb1872 | 2010-10-27 03:33:29 +0530 | [diff] [blame] | 35 | #include <net/netlink.h> |
| 36 | #include <net/genetlink.h> |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 37 | |
Zhang Rui | 63c4ec9 | 2008-04-21 16:07:13 +0800 | [diff] [blame] | 38 | MODULE_AUTHOR("Zhang Rui"); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 39 | MODULE_DESCRIPTION("Generic thermal management sysfs support"); |
| 40 | MODULE_LICENSE("GPL"); |
| 41 | |
| 42 | #define PREFIX "Thermal: " |
| 43 | |
| 44 | struct thermal_cooling_device_instance { |
| 45 | int id; |
| 46 | char name[THERMAL_NAME_LENGTH]; |
| 47 | struct thermal_zone_device *tz; |
| 48 | struct thermal_cooling_device *cdev; |
| 49 | int trip; |
| 50 | char attr_name[THERMAL_NAME_LENGTH]; |
| 51 | struct device_attribute attr; |
| 52 | struct list_head node; |
| 53 | }; |
| 54 | |
| 55 | static DEFINE_IDR(thermal_tz_idr); |
| 56 | static DEFINE_IDR(thermal_cdev_idr); |
| 57 | static DEFINE_MUTEX(thermal_idr_lock); |
| 58 | |
| 59 | static LIST_HEAD(thermal_tz_list); |
| 60 | static LIST_HEAD(thermal_cdev_list); |
| 61 | static DEFINE_MUTEX(thermal_list_lock); |
| 62 | |
| 63 | static int get_idr(struct idr *idr, struct mutex *lock, int *id) |
| 64 | { |
| 65 | int err; |
| 66 | |
| 67 | again: |
| 68 | if (unlikely(idr_pre_get(idr, GFP_KERNEL) == 0)) |
| 69 | return -ENOMEM; |
| 70 | |
| 71 | if (lock) |
| 72 | mutex_lock(lock); |
| 73 | err = idr_get_new(idr, NULL, id); |
| 74 | if (lock) |
| 75 | mutex_unlock(lock); |
| 76 | if (unlikely(err == -EAGAIN)) |
| 77 | goto again; |
| 78 | else if (unlikely(err)) |
| 79 | return err; |
| 80 | |
| 81 | *id = *id & MAX_ID_MASK; |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | static void release_idr(struct idr *idr, struct mutex *lock, int id) |
| 86 | { |
| 87 | if (lock) |
| 88 | mutex_lock(lock); |
| 89 | idr_remove(idr, id); |
| 90 | if (lock) |
| 91 | mutex_unlock(lock); |
| 92 | } |
| 93 | |
| 94 | /* sys I/F for thermal zone */ |
| 95 | |
| 96 | #define to_thermal_zone(_dev) \ |
| 97 | container_of(_dev, struct thermal_zone_device, device) |
| 98 | |
| 99 | static ssize_t |
| 100 | type_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 101 | { |
| 102 | struct thermal_zone_device *tz = to_thermal_zone(dev); |
| 103 | |
| 104 | return sprintf(buf, "%s\n", tz->type); |
| 105 | } |
| 106 | |
| 107 | static ssize_t |
| 108 | temp_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 109 | { |
| 110 | struct thermal_zone_device *tz = to_thermal_zone(dev); |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 111 | long temperature; |
| 112 | int ret; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 113 | |
| 114 | if (!tz->ops->get_temp) |
| 115 | return -EPERM; |
| 116 | |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 117 | ret = tz->ops->get_temp(tz, &temperature); |
| 118 | |
| 119 | if (ret) |
| 120 | return ret; |
| 121 | |
| 122 | return sprintf(buf, "%ld\n", temperature); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | static ssize_t |
| 126 | mode_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 127 | { |
| 128 | struct thermal_zone_device *tz = to_thermal_zone(dev); |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 129 | enum thermal_device_mode mode; |
| 130 | int result; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 131 | |
| 132 | if (!tz->ops->get_mode) |
| 133 | return -EPERM; |
| 134 | |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 135 | result = tz->ops->get_mode(tz, &mode); |
| 136 | if (result) |
| 137 | return result; |
| 138 | |
| 139 | return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled" |
| 140 | : "disabled"); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | static ssize_t |
| 144 | mode_store(struct device *dev, struct device_attribute *attr, |
| 145 | const char *buf, size_t count) |
| 146 | { |
| 147 | struct thermal_zone_device *tz = to_thermal_zone(dev); |
| 148 | int result; |
| 149 | |
| 150 | if (!tz->ops->set_mode) |
| 151 | return -EPERM; |
| 152 | |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 153 | if (!strncmp(buf, "enabled", sizeof("enabled"))) |
| 154 | result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED); |
| 155 | else if (!strncmp(buf, "disabled", sizeof("disabled"))) |
| 156 | result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED); |
| 157 | else |
| 158 | result = -EINVAL; |
| 159 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 160 | if (result) |
| 161 | return result; |
| 162 | |
| 163 | return count; |
| 164 | } |
| 165 | |
| 166 | static ssize_t |
| 167 | trip_point_type_show(struct device *dev, struct device_attribute *attr, |
| 168 | char *buf) |
| 169 | { |
| 170 | struct thermal_zone_device *tz = to_thermal_zone(dev); |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 171 | enum thermal_trip_type type; |
| 172 | int trip, result; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 173 | |
| 174 | if (!tz->ops->get_trip_type) |
| 175 | return -EPERM; |
| 176 | |
| 177 | if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip)) |
| 178 | return -EINVAL; |
| 179 | |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 180 | result = tz->ops->get_trip_type(tz, trip, &type); |
| 181 | if (result) |
| 182 | return result; |
| 183 | |
| 184 | switch (type) { |
| 185 | case THERMAL_TRIP_CRITICAL: |
Amit Kucheria | 625120a4 | 2009-10-16 12:46:02 +0300 | [diff] [blame] | 186 | return sprintf(buf, "critical\n"); |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 187 | case THERMAL_TRIP_HOT: |
Amit Kucheria | 625120a4 | 2009-10-16 12:46:02 +0300 | [diff] [blame] | 188 | return sprintf(buf, "hot\n"); |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 189 | case THERMAL_TRIP_PASSIVE: |
Amit Kucheria | 625120a4 | 2009-10-16 12:46:02 +0300 | [diff] [blame] | 190 | return sprintf(buf, "passive\n"); |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 191 | case THERMAL_TRIP_ACTIVE: |
Amit Kucheria | 625120a4 | 2009-10-16 12:46:02 +0300 | [diff] [blame] | 192 | return sprintf(buf, "active\n"); |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 193 | default: |
Amit Kucheria | 625120a4 | 2009-10-16 12:46:02 +0300 | [diff] [blame] | 194 | return sprintf(buf, "unknown\n"); |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 195 | } |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | static ssize_t |
| 199 | trip_point_temp_show(struct device *dev, struct device_attribute *attr, |
| 200 | char *buf) |
| 201 | { |
| 202 | struct thermal_zone_device *tz = to_thermal_zone(dev); |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 203 | int trip, ret; |
| 204 | long temperature; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 205 | |
| 206 | if (!tz->ops->get_trip_temp) |
| 207 | return -EPERM; |
| 208 | |
| 209 | if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip)) |
| 210 | return -EINVAL; |
| 211 | |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 212 | ret = tz->ops->get_trip_temp(tz, trip, &temperature); |
| 213 | |
| 214 | if (ret) |
| 215 | return ret; |
| 216 | |
| 217 | return sprintf(buf, "%ld\n", temperature); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 218 | } |
| 219 | |
Matthew Garrett | 03a971a | 2008-12-03 18:00:38 +0000 | [diff] [blame] | 220 | static ssize_t |
| 221 | passive_store(struct device *dev, struct device_attribute *attr, |
| 222 | const char *buf, size_t count) |
| 223 | { |
| 224 | struct thermal_zone_device *tz = to_thermal_zone(dev); |
| 225 | struct thermal_cooling_device *cdev = NULL; |
| 226 | int state; |
| 227 | |
| 228 | if (!sscanf(buf, "%d\n", &state)) |
| 229 | return -EINVAL; |
| 230 | |
Frans Pop | 3d8e3ad | 2009-10-26 08:39:02 +0100 | [diff] [blame] | 231 | /* sanity check: values below 1000 millicelcius don't make sense |
| 232 | * and can cause the system to go into a thermal heart attack |
| 233 | */ |
| 234 | if (state && state < 1000) |
| 235 | return -EINVAL; |
| 236 | |
Matthew Garrett | 03a971a | 2008-12-03 18:00:38 +0000 | [diff] [blame] | 237 | if (state && !tz->forced_passive) { |
| 238 | mutex_lock(&thermal_list_lock); |
| 239 | list_for_each_entry(cdev, &thermal_cdev_list, node) { |
| 240 | if (!strncmp("Processor", cdev->type, |
| 241 | sizeof("Processor"))) |
| 242 | thermal_zone_bind_cooling_device(tz, |
| 243 | THERMAL_TRIPS_NONE, |
| 244 | cdev); |
| 245 | } |
| 246 | mutex_unlock(&thermal_list_lock); |
Frans Pop | e4143b0 | 2009-10-26 08:39:03 +0100 | [diff] [blame] | 247 | if (!tz->passive_delay) |
| 248 | tz->passive_delay = 1000; |
Matthew Garrett | 03a971a | 2008-12-03 18:00:38 +0000 | [diff] [blame] | 249 | } else if (!state && tz->forced_passive) { |
| 250 | mutex_lock(&thermal_list_lock); |
| 251 | list_for_each_entry(cdev, &thermal_cdev_list, node) { |
| 252 | if (!strncmp("Processor", cdev->type, |
| 253 | sizeof("Processor"))) |
| 254 | thermal_zone_unbind_cooling_device(tz, |
| 255 | THERMAL_TRIPS_NONE, |
| 256 | cdev); |
| 257 | } |
| 258 | mutex_unlock(&thermal_list_lock); |
Frans Pop | e4143b0 | 2009-10-26 08:39:03 +0100 | [diff] [blame] | 259 | tz->passive_delay = 0; |
Matthew Garrett | 03a971a | 2008-12-03 18:00:38 +0000 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | tz->tc1 = 1; |
| 263 | tz->tc2 = 1; |
| 264 | |
Matthew Garrett | 03a971a | 2008-12-03 18:00:38 +0000 | [diff] [blame] | 265 | tz->forced_passive = state; |
| 266 | |
| 267 | thermal_zone_device_update(tz); |
| 268 | |
| 269 | return count; |
| 270 | } |
| 271 | |
| 272 | static ssize_t |
| 273 | passive_show(struct device *dev, struct device_attribute *attr, |
| 274 | char *buf) |
| 275 | { |
| 276 | struct thermal_zone_device *tz = to_thermal_zone(dev); |
| 277 | |
| 278 | return sprintf(buf, "%d\n", tz->forced_passive); |
| 279 | } |
| 280 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 281 | static DEVICE_ATTR(type, 0444, type_show, NULL); |
| 282 | static DEVICE_ATTR(temp, 0444, temp_show, NULL); |
| 283 | static DEVICE_ATTR(mode, 0644, mode_show, mode_store); |
Joe Perches | 886ee54 | 2012-03-21 12:55:01 -0700 | [diff] [blame] | 284 | static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 285 | |
| 286 | static struct device_attribute trip_point_attrs[] = { |
| 287 | __ATTR(trip_point_0_type, 0444, trip_point_type_show, NULL), |
| 288 | __ATTR(trip_point_0_temp, 0444, trip_point_temp_show, NULL), |
| 289 | __ATTR(trip_point_1_type, 0444, trip_point_type_show, NULL), |
| 290 | __ATTR(trip_point_1_temp, 0444, trip_point_temp_show, NULL), |
| 291 | __ATTR(trip_point_2_type, 0444, trip_point_type_show, NULL), |
| 292 | __ATTR(trip_point_2_temp, 0444, trip_point_temp_show, NULL), |
| 293 | __ATTR(trip_point_3_type, 0444, trip_point_type_show, NULL), |
| 294 | __ATTR(trip_point_3_temp, 0444, trip_point_temp_show, NULL), |
| 295 | __ATTR(trip_point_4_type, 0444, trip_point_type_show, NULL), |
| 296 | __ATTR(trip_point_4_temp, 0444, trip_point_temp_show, NULL), |
| 297 | __ATTR(trip_point_5_type, 0444, trip_point_type_show, NULL), |
| 298 | __ATTR(trip_point_5_temp, 0444, trip_point_temp_show, NULL), |
| 299 | __ATTR(trip_point_6_type, 0444, trip_point_type_show, NULL), |
| 300 | __ATTR(trip_point_6_temp, 0444, trip_point_temp_show, NULL), |
| 301 | __ATTR(trip_point_7_type, 0444, trip_point_type_show, NULL), |
| 302 | __ATTR(trip_point_7_temp, 0444, trip_point_temp_show, NULL), |
| 303 | __ATTR(trip_point_8_type, 0444, trip_point_type_show, NULL), |
| 304 | __ATTR(trip_point_8_temp, 0444, trip_point_temp_show, NULL), |
| 305 | __ATTR(trip_point_9_type, 0444, trip_point_type_show, NULL), |
| 306 | __ATTR(trip_point_9_temp, 0444, trip_point_temp_show, NULL), |
Krzysztof Helt | 5f1a3f2 | 2008-04-15 14:34:47 -0700 | [diff] [blame] | 307 | __ATTR(trip_point_10_type, 0444, trip_point_type_show, NULL), |
| 308 | __ATTR(trip_point_10_temp, 0444, trip_point_temp_show, NULL), |
| 309 | __ATTR(trip_point_11_type, 0444, trip_point_type_show, NULL), |
| 310 | __ATTR(trip_point_11_temp, 0444, trip_point_temp_show, NULL), |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 311 | }; |
| 312 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 313 | /* sys I/F for cooling device */ |
| 314 | #define to_cooling_device(_dev) \ |
| 315 | container_of(_dev, struct thermal_cooling_device, device) |
| 316 | |
| 317 | static ssize_t |
| 318 | thermal_cooling_device_type_show(struct device *dev, |
| 319 | struct device_attribute *attr, char *buf) |
| 320 | { |
| 321 | struct thermal_cooling_device *cdev = to_cooling_device(dev); |
| 322 | |
| 323 | return sprintf(buf, "%s\n", cdev->type); |
| 324 | } |
| 325 | |
| 326 | static ssize_t |
| 327 | thermal_cooling_device_max_state_show(struct device *dev, |
| 328 | struct device_attribute *attr, char *buf) |
| 329 | { |
| 330 | struct thermal_cooling_device *cdev = to_cooling_device(dev); |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 331 | unsigned long state; |
| 332 | int ret; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 333 | |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 334 | ret = cdev->ops->get_max_state(cdev, &state); |
| 335 | if (ret) |
| 336 | return ret; |
| 337 | return sprintf(buf, "%ld\n", state); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | static ssize_t |
| 341 | thermal_cooling_device_cur_state_show(struct device *dev, |
| 342 | struct device_attribute *attr, char *buf) |
| 343 | { |
| 344 | struct thermal_cooling_device *cdev = to_cooling_device(dev); |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 345 | unsigned long state; |
| 346 | int ret; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 347 | |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 348 | ret = cdev->ops->get_cur_state(cdev, &state); |
| 349 | if (ret) |
| 350 | return ret; |
| 351 | return sprintf(buf, "%ld\n", state); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | static ssize_t |
| 355 | thermal_cooling_device_cur_state_store(struct device *dev, |
| 356 | struct device_attribute *attr, |
| 357 | const char *buf, size_t count) |
| 358 | { |
| 359 | struct thermal_cooling_device *cdev = to_cooling_device(dev); |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 360 | unsigned long state; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 361 | int result; |
| 362 | |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 363 | if (!sscanf(buf, "%ld\n", &state)) |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 364 | return -EINVAL; |
| 365 | |
Roel Kluin | edb9491 | 2009-12-15 22:46:50 +0100 | [diff] [blame] | 366 | if ((long)state < 0) |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 367 | return -EINVAL; |
| 368 | |
| 369 | result = cdev->ops->set_cur_state(cdev, state); |
| 370 | if (result) |
| 371 | return result; |
| 372 | return count; |
| 373 | } |
| 374 | |
| 375 | static struct device_attribute dev_attr_cdev_type = |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 376 | __ATTR(type, 0444, thermal_cooling_device_type_show, NULL); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 377 | static DEVICE_ATTR(max_state, 0444, |
| 378 | thermal_cooling_device_max_state_show, NULL); |
| 379 | static DEVICE_ATTR(cur_state, 0644, |
| 380 | thermal_cooling_device_cur_state_show, |
| 381 | thermal_cooling_device_cur_state_store); |
| 382 | |
| 383 | static ssize_t |
| 384 | thermal_cooling_device_trip_point_show(struct device *dev, |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 385 | struct device_attribute *attr, char *buf) |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 386 | { |
| 387 | struct thermal_cooling_device_instance *instance; |
| 388 | |
| 389 | instance = |
| 390 | container_of(attr, struct thermal_cooling_device_instance, attr); |
| 391 | |
| 392 | if (instance->trip == THERMAL_TRIPS_NONE) |
| 393 | return sprintf(buf, "-1\n"); |
| 394 | else |
| 395 | return sprintf(buf, "%d\n", instance->trip); |
| 396 | } |
| 397 | |
| 398 | /* Device management */ |
| 399 | |
Rene Herman | 16d7523 | 2008-06-24 19:38:56 +0200 | [diff] [blame] | 400 | #if defined(CONFIG_THERMAL_HWMON) |
| 401 | |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 402 | /* hwmon sys I/F */ |
| 403 | #include <linux/hwmon.h> |
Jean Delvare | 31f5396 | 2011-07-28 13:48:42 -0700 | [diff] [blame] | 404 | |
| 405 | /* thermal zone devices with the same type share one hwmon device */ |
| 406 | struct thermal_hwmon_device { |
| 407 | char type[THERMAL_NAME_LENGTH]; |
| 408 | struct device *device; |
| 409 | int count; |
| 410 | struct list_head tz_list; |
| 411 | struct list_head node; |
| 412 | }; |
| 413 | |
| 414 | struct thermal_hwmon_attr { |
| 415 | struct device_attribute attr; |
| 416 | char name[16]; |
| 417 | }; |
| 418 | |
| 419 | /* one temperature input for each thermal zone */ |
| 420 | struct thermal_hwmon_temp { |
| 421 | struct list_head hwmon_node; |
| 422 | struct thermal_zone_device *tz; |
| 423 | struct thermal_hwmon_attr temp_input; /* hwmon sys attr */ |
| 424 | struct thermal_hwmon_attr temp_crit; /* hwmon sys attr */ |
| 425 | }; |
| 426 | |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 427 | static LIST_HEAD(thermal_hwmon_list); |
| 428 | |
| 429 | static ssize_t |
| 430 | name_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 431 | { |
Greg Kroah-Hartman | 0e968a3 | 2009-04-30 14:43:31 -0700 | [diff] [blame] | 432 | struct thermal_hwmon_device *hwmon = dev_get_drvdata(dev); |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 433 | return sprintf(buf, "%s\n", hwmon->type); |
| 434 | } |
| 435 | static DEVICE_ATTR(name, 0444, name_show, NULL); |
| 436 | |
| 437 | static ssize_t |
| 438 | temp_input_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 439 | { |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 440 | long temperature; |
| 441 | int ret; |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 442 | struct thermal_hwmon_attr *hwmon_attr |
| 443 | = container_of(attr, struct thermal_hwmon_attr, attr); |
Jean Delvare | 31f5396 | 2011-07-28 13:48:42 -0700 | [diff] [blame] | 444 | struct thermal_hwmon_temp *temp |
| 445 | = container_of(hwmon_attr, struct thermal_hwmon_temp, |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 446 | temp_input); |
Jean Delvare | 31f5396 | 2011-07-28 13:48:42 -0700 | [diff] [blame] | 447 | struct thermal_zone_device *tz = temp->tz; |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 448 | |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 449 | ret = tz->ops->get_temp(tz, &temperature); |
| 450 | |
| 451 | if (ret) |
| 452 | return ret; |
| 453 | |
| 454 | return sprintf(buf, "%ld\n", temperature); |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | static ssize_t |
| 458 | temp_crit_show(struct device *dev, struct device_attribute *attr, |
| 459 | char *buf) |
| 460 | { |
| 461 | struct thermal_hwmon_attr *hwmon_attr |
| 462 | = container_of(attr, struct thermal_hwmon_attr, attr); |
Jean Delvare | 31f5396 | 2011-07-28 13:48:42 -0700 | [diff] [blame] | 463 | struct thermal_hwmon_temp *temp |
| 464 | = container_of(hwmon_attr, struct thermal_hwmon_temp, |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 465 | temp_crit); |
Jean Delvare | 31f5396 | 2011-07-28 13:48:42 -0700 | [diff] [blame] | 466 | struct thermal_zone_device *tz = temp->tz; |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 467 | long temperature; |
| 468 | int ret; |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 469 | |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 470 | ret = tz->ops->get_trip_temp(tz, 0, &temperature); |
| 471 | if (ret) |
| 472 | return ret; |
| 473 | |
| 474 | return sprintf(buf, "%ld\n", temperature); |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | |
Jean Delvare | 0d97d7a | 2011-07-28 13:48:41 -0700 | [diff] [blame] | 478 | static struct thermal_hwmon_device * |
| 479 | thermal_hwmon_lookup_by_type(const struct thermal_zone_device *tz) |
| 480 | { |
| 481 | struct thermal_hwmon_device *hwmon; |
| 482 | |
| 483 | mutex_lock(&thermal_list_lock); |
| 484 | list_for_each_entry(hwmon, &thermal_hwmon_list, node) |
| 485 | if (!strcmp(hwmon->type, tz->type)) { |
| 486 | mutex_unlock(&thermal_list_lock); |
| 487 | return hwmon; |
| 488 | } |
| 489 | mutex_unlock(&thermal_list_lock); |
| 490 | |
| 491 | return NULL; |
| 492 | } |
| 493 | |
Jean Delvare | 31f5396 | 2011-07-28 13:48:42 -0700 | [diff] [blame] | 494 | /* Find the temperature input matching a given thermal zone */ |
| 495 | static struct thermal_hwmon_temp * |
| 496 | thermal_hwmon_lookup_temp(const struct thermal_hwmon_device *hwmon, |
| 497 | const struct thermal_zone_device *tz) |
| 498 | { |
| 499 | struct thermal_hwmon_temp *temp; |
| 500 | |
| 501 | mutex_lock(&thermal_list_lock); |
| 502 | list_for_each_entry(temp, &hwmon->tz_list, hwmon_node) |
| 503 | if (temp->tz == tz) { |
| 504 | mutex_unlock(&thermal_list_lock); |
| 505 | return temp; |
| 506 | } |
| 507 | mutex_unlock(&thermal_list_lock); |
| 508 | |
| 509 | return NULL; |
| 510 | } |
| 511 | |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 512 | static int |
| 513 | thermal_add_hwmon_sysfs(struct thermal_zone_device *tz) |
| 514 | { |
| 515 | struct thermal_hwmon_device *hwmon; |
Jean Delvare | 31f5396 | 2011-07-28 13:48:42 -0700 | [diff] [blame] | 516 | struct thermal_hwmon_temp *temp; |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 517 | int new_hwmon_device = 1; |
| 518 | int result; |
| 519 | |
Jean Delvare | 0d97d7a | 2011-07-28 13:48:41 -0700 | [diff] [blame] | 520 | hwmon = thermal_hwmon_lookup_by_type(tz); |
| 521 | if (hwmon) { |
| 522 | new_hwmon_device = 0; |
| 523 | goto register_sys_interface; |
| 524 | } |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 525 | |
| 526 | hwmon = kzalloc(sizeof(struct thermal_hwmon_device), GFP_KERNEL); |
| 527 | if (!hwmon) |
| 528 | return -ENOMEM; |
| 529 | |
| 530 | INIT_LIST_HEAD(&hwmon->tz_list); |
| 531 | strlcpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH); |
| 532 | hwmon->device = hwmon_device_register(NULL); |
| 533 | if (IS_ERR(hwmon->device)) { |
| 534 | result = PTR_ERR(hwmon->device); |
| 535 | goto free_mem; |
| 536 | } |
Greg Kroah-Hartman | 0e968a3 | 2009-04-30 14:43:31 -0700 | [diff] [blame] | 537 | dev_set_drvdata(hwmon->device, hwmon); |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 538 | result = device_create_file(hwmon->device, &dev_attr_name); |
| 539 | if (result) |
Durgadoss R | b299eb5 | 2011-03-03 04:30:13 +0530 | [diff] [blame] | 540 | goto free_mem; |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 541 | |
| 542 | register_sys_interface: |
Jean Delvare | 31f5396 | 2011-07-28 13:48:42 -0700 | [diff] [blame] | 543 | temp = kzalloc(sizeof(struct thermal_hwmon_temp), GFP_KERNEL); |
| 544 | if (!temp) { |
| 545 | result = -ENOMEM; |
| 546 | goto unregister_name; |
| 547 | } |
| 548 | |
| 549 | temp->tz = tz; |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 550 | hwmon->count++; |
| 551 | |
Jean Delvare | 31f5396 | 2011-07-28 13:48:42 -0700 | [diff] [blame] | 552 | snprintf(temp->temp_input.name, THERMAL_NAME_LENGTH, |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 553 | "temp%d_input", hwmon->count); |
Jean Delvare | 31f5396 | 2011-07-28 13:48:42 -0700 | [diff] [blame] | 554 | temp->temp_input.attr.attr.name = temp->temp_input.name; |
| 555 | temp->temp_input.attr.attr.mode = 0444; |
| 556 | temp->temp_input.attr.show = temp_input_show; |
| 557 | sysfs_attr_init(&temp->temp_input.attr.attr); |
| 558 | result = device_create_file(hwmon->device, &temp->temp_input.attr); |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 559 | if (result) |
Jean Delvare | 31f5396 | 2011-07-28 13:48:42 -0700 | [diff] [blame] | 560 | goto free_temp_mem; |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 561 | |
| 562 | if (tz->ops->get_crit_temp) { |
| 563 | unsigned long temperature; |
| 564 | if (!tz->ops->get_crit_temp(tz, &temperature)) { |
Jean Delvare | 31f5396 | 2011-07-28 13:48:42 -0700 | [diff] [blame] | 565 | snprintf(temp->temp_crit.name, THERMAL_NAME_LENGTH, |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 566 | "temp%d_crit", hwmon->count); |
Jean Delvare | 31f5396 | 2011-07-28 13:48:42 -0700 | [diff] [blame] | 567 | temp->temp_crit.attr.attr.name = temp->temp_crit.name; |
| 568 | temp->temp_crit.attr.attr.mode = 0444; |
| 569 | temp->temp_crit.attr.show = temp_crit_show; |
| 570 | sysfs_attr_init(&temp->temp_crit.attr.attr); |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 571 | result = device_create_file(hwmon->device, |
Jean Delvare | 31f5396 | 2011-07-28 13:48:42 -0700 | [diff] [blame] | 572 | &temp->temp_crit.attr); |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 573 | if (result) |
Durgadoss R | b299eb5 | 2011-03-03 04:30:13 +0530 | [diff] [blame] | 574 | goto unregister_input; |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 575 | } |
| 576 | } |
| 577 | |
| 578 | mutex_lock(&thermal_list_lock); |
| 579 | if (new_hwmon_device) |
| 580 | list_add_tail(&hwmon->node, &thermal_hwmon_list); |
Jean Delvare | 31f5396 | 2011-07-28 13:48:42 -0700 | [diff] [blame] | 581 | list_add_tail(&temp->hwmon_node, &hwmon->tz_list); |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 582 | mutex_unlock(&thermal_list_lock); |
| 583 | |
| 584 | return 0; |
| 585 | |
Durgadoss R | b299eb5 | 2011-03-03 04:30:13 +0530 | [diff] [blame] | 586 | unregister_input: |
Jean Delvare | 31f5396 | 2011-07-28 13:48:42 -0700 | [diff] [blame] | 587 | device_remove_file(hwmon->device, &temp->temp_input.attr); |
| 588 | free_temp_mem: |
| 589 | kfree(temp); |
Durgadoss R | b299eb5 | 2011-03-03 04:30:13 +0530 | [diff] [blame] | 590 | unregister_name: |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 591 | if (new_hwmon_device) { |
| 592 | device_remove_file(hwmon->device, &dev_attr_name); |
| 593 | hwmon_device_unregister(hwmon->device); |
| 594 | } |
| 595 | free_mem: |
| 596 | if (new_hwmon_device) |
| 597 | kfree(hwmon); |
| 598 | |
| 599 | return result; |
| 600 | } |
| 601 | |
| 602 | static void |
| 603 | thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz) |
| 604 | { |
Jean Delvare | 31f5396 | 2011-07-28 13:48:42 -0700 | [diff] [blame] | 605 | struct thermal_hwmon_device *hwmon; |
| 606 | struct thermal_hwmon_temp *temp; |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 607 | |
Jean Delvare | 31f5396 | 2011-07-28 13:48:42 -0700 | [diff] [blame] | 608 | hwmon = thermal_hwmon_lookup_by_type(tz); |
| 609 | if (unlikely(!hwmon)) { |
| 610 | /* Should never happen... */ |
| 611 | dev_dbg(&tz->device, "hwmon device lookup failed!\n"); |
| 612 | return; |
| 613 | } |
| 614 | |
| 615 | temp = thermal_hwmon_lookup_temp(hwmon, tz); |
| 616 | if (unlikely(!temp)) { |
| 617 | /* Should never happen... */ |
| 618 | dev_dbg(&tz->device, "temperature input lookup failed!\n"); |
| 619 | return; |
| 620 | } |
| 621 | |
| 622 | device_remove_file(hwmon->device, &temp->temp_input.attr); |
Durgadoss R | b299eb5 | 2011-03-03 04:30:13 +0530 | [diff] [blame] | 623 | if (tz->ops->get_crit_temp) |
Jean Delvare | 31f5396 | 2011-07-28 13:48:42 -0700 | [diff] [blame] | 624 | device_remove_file(hwmon->device, &temp->temp_crit.attr); |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 625 | |
| 626 | mutex_lock(&thermal_list_lock); |
Jean Delvare | 31f5396 | 2011-07-28 13:48:42 -0700 | [diff] [blame] | 627 | list_del(&temp->hwmon_node); |
| 628 | kfree(temp); |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 629 | if (!list_empty(&hwmon->tz_list)) { |
| 630 | mutex_unlock(&thermal_list_lock); |
| 631 | return; |
| 632 | } |
| 633 | list_del(&hwmon->node); |
| 634 | mutex_unlock(&thermal_list_lock); |
| 635 | |
| 636 | device_remove_file(hwmon->device, &dev_attr_name); |
| 637 | hwmon_device_unregister(hwmon->device); |
| 638 | kfree(hwmon); |
| 639 | } |
| 640 | #else |
| 641 | static int |
| 642 | thermal_add_hwmon_sysfs(struct thermal_zone_device *tz) |
| 643 | { |
| 644 | return 0; |
| 645 | } |
| 646 | |
| 647 | static void |
| 648 | thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz) |
| 649 | { |
| 650 | } |
| 651 | #endif |
| 652 | |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 653 | static void thermal_zone_device_set_polling(struct thermal_zone_device *tz, |
| 654 | int delay) |
| 655 | { |
| 656 | cancel_delayed_work(&(tz->poll_queue)); |
| 657 | |
| 658 | if (!delay) |
| 659 | return; |
| 660 | |
| 661 | if (delay > 1000) |
Rafael J. Wysocki | 51e20d0 | 2011-11-06 14:21:38 +0100 | [diff] [blame] | 662 | queue_delayed_work(system_freezable_wq, &(tz->poll_queue), |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 663 | round_jiffies(msecs_to_jiffies(delay))); |
| 664 | else |
Rafael J. Wysocki | 51e20d0 | 2011-11-06 14:21:38 +0100 | [diff] [blame] | 665 | queue_delayed_work(system_freezable_wq, &(tz->poll_queue), |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 666 | msecs_to_jiffies(delay)); |
| 667 | } |
| 668 | |
| 669 | static void thermal_zone_device_passive(struct thermal_zone_device *tz, |
| 670 | int temp, int trip_temp, int trip) |
| 671 | { |
| 672 | int trend = 0; |
| 673 | struct thermal_cooling_device_instance *instance; |
| 674 | struct thermal_cooling_device *cdev; |
| 675 | long state, max_state; |
| 676 | |
| 677 | /* |
| 678 | * Above Trip? |
| 679 | * ----------- |
| 680 | * Calculate the thermal trend (using the passive cooling equation) |
| 681 | * and modify the performance limit for all passive cooling devices |
| 682 | * accordingly. Note that we assume symmetry. |
| 683 | */ |
| 684 | if (temp >= trip_temp) { |
| 685 | tz->passive = true; |
| 686 | |
| 687 | trend = (tz->tc1 * (temp - tz->last_temperature)) + |
| 688 | (tz->tc2 * (temp - trip_temp)); |
| 689 | |
| 690 | /* Heating up? */ |
| 691 | if (trend > 0) { |
| 692 | list_for_each_entry(instance, &tz->cooling_devices, |
| 693 | node) { |
| 694 | if (instance->trip != trip) |
| 695 | continue; |
| 696 | cdev = instance->cdev; |
| 697 | cdev->ops->get_cur_state(cdev, &state); |
| 698 | cdev->ops->get_max_state(cdev, &max_state); |
| 699 | if (state++ < max_state) |
| 700 | cdev->ops->set_cur_state(cdev, state); |
| 701 | } |
| 702 | } else if (trend < 0) { /* Cooling off? */ |
| 703 | list_for_each_entry(instance, &tz->cooling_devices, |
| 704 | node) { |
| 705 | if (instance->trip != trip) |
| 706 | continue; |
| 707 | cdev = instance->cdev; |
| 708 | cdev->ops->get_cur_state(cdev, &state); |
| 709 | cdev->ops->get_max_state(cdev, &max_state); |
| 710 | if (state > 0) |
| 711 | cdev->ops->set_cur_state(cdev, --state); |
| 712 | } |
| 713 | } |
| 714 | return; |
| 715 | } |
| 716 | |
| 717 | /* |
| 718 | * Below Trip? |
| 719 | * ----------- |
| 720 | * Implement passive cooling hysteresis to slowly increase performance |
| 721 | * and avoid thrashing around the passive trip point. Note that we |
| 722 | * assume symmetry. |
| 723 | */ |
| 724 | list_for_each_entry(instance, &tz->cooling_devices, node) { |
| 725 | if (instance->trip != trip) |
| 726 | continue; |
| 727 | cdev = instance->cdev; |
| 728 | cdev->ops->get_cur_state(cdev, &state); |
| 729 | cdev->ops->get_max_state(cdev, &max_state); |
| 730 | if (state > 0) |
| 731 | cdev->ops->set_cur_state(cdev, --state); |
| 732 | if (state == 0) |
| 733 | tz->passive = false; |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | static void thermal_zone_device_check(struct work_struct *work) |
| 738 | { |
| 739 | struct thermal_zone_device *tz = container_of(work, struct |
| 740 | thermal_zone_device, |
| 741 | poll_queue.work); |
| 742 | thermal_zone_device_update(tz); |
| 743 | } |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 744 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 745 | /** |
| 746 | * thermal_zone_bind_cooling_device - bind a cooling device to a thermal zone |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 747 | * @tz: thermal zone device |
| 748 | * @trip: indicates which trip point the cooling devices is |
| 749 | * associated with in this thermal zone. |
| 750 | * @cdev: thermal cooling device |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 751 | * |
| 752 | * This function is usually called in the thermal zone device .bind callback. |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 753 | */ |
| 754 | int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz, |
| 755 | int trip, |
| 756 | struct thermal_cooling_device *cdev) |
| 757 | { |
| 758 | struct thermal_cooling_device_instance *dev; |
| 759 | struct thermal_cooling_device_instance *pos; |
Thomas Sujith | c751670 | 2008-02-15 00:58:50 -0500 | [diff] [blame] | 760 | struct thermal_zone_device *pos1; |
| 761 | struct thermal_cooling_device *pos2; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 762 | int result; |
| 763 | |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 764 | if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE)) |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 765 | return -EINVAL; |
| 766 | |
Thomas Sujith | c751670 | 2008-02-15 00:58:50 -0500 | [diff] [blame] | 767 | list_for_each_entry(pos1, &thermal_tz_list, node) { |
| 768 | if (pos1 == tz) |
| 769 | break; |
| 770 | } |
| 771 | list_for_each_entry(pos2, &thermal_cdev_list, node) { |
| 772 | if (pos2 == cdev) |
| 773 | break; |
| 774 | } |
| 775 | |
| 776 | if (tz != pos1 || cdev != pos2) |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 777 | return -EINVAL; |
| 778 | |
| 779 | dev = |
| 780 | kzalloc(sizeof(struct thermal_cooling_device_instance), GFP_KERNEL); |
| 781 | if (!dev) |
| 782 | return -ENOMEM; |
| 783 | dev->tz = tz; |
| 784 | dev->cdev = cdev; |
| 785 | dev->trip = trip; |
| 786 | result = get_idr(&tz->idr, &tz->lock, &dev->id); |
| 787 | if (result) |
| 788 | goto free_mem; |
| 789 | |
| 790 | sprintf(dev->name, "cdev%d", dev->id); |
| 791 | result = |
| 792 | sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name); |
| 793 | if (result) |
| 794 | goto release_idr; |
| 795 | |
| 796 | sprintf(dev->attr_name, "cdev%d_trip_point", dev->id); |
Sergey Senozhatsky | 975f8c5 | 2010-04-06 14:34:51 -0700 | [diff] [blame] | 797 | sysfs_attr_init(&dev->attr.attr); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 798 | dev->attr.attr.name = dev->attr_name; |
| 799 | dev->attr.attr.mode = 0444; |
| 800 | dev->attr.show = thermal_cooling_device_trip_point_show; |
| 801 | result = device_create_file(&tz->device, &dev->attr); |
| 802 | if (result) |
| 803 | goto remove_symbol_link; |
| 804 | |
| 805 | mutex_lock(&tz->lock); |
| 806 | list_for_each_entry(pos, &tz->cooling_devices, node) |
| 807 | if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) { |
| 808 | result = -EEXIST; |
| 809 | break; |
| 810 | } |
| 811 | if (!result) |
| 812 | list_add_tail(&dev->node, &tz->cooling_devices); |
| 813 | mutex_unlock(&tz->lock); |
| 814 | |
| 815 | if (!result) |
| 816 | return 0; |
| 817 | |
| 818 | device_remove_file(&tz->device, &dev->attr); |
| 819 | remove_symbol_link: |
| 820 | sysfs_remove_link(&tz->device.kobj, dev->name); |
| 821 | release_idr: |
| 822 | release_idr(&tz->idr, &tz->lock, dev->id); |
| 823 | free_mem: |
| 824 | kfree(dev); |
| 825 | return result; |
| 826 | } |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 827 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 828 | EXPORT_SYMBOL(thermal_zone_bind_cooling_device); |
| 829 | |
| 830 | /** |
| 831 | * thermal_zone_unbind_cooling_device - unbind a cooling device from a thermal zone |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 832 | * @tz: thermal zone device |
| 833 | * @trip: indicates which trip point the cooling devices is |
| 834 | * associated with in this thermal zone. |
| 835 | * @cdev: thermal cooling device |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 836 | * |
| 837 | * This function is usually called in the thermal zone device .unbind callback. |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 838 | */ |
| 839 | int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz, |
| 840 | int trip, |
| 841 | struct thermal_cooling_device *cdev) |
| 842 | { |
| 843 | struct thermal_cooling_device_instance *pos, *next; |
| 844 | |
| 845 | mutex_lock(&tz->lock); |
| 846 | list_for_each_entry_safe(pos, next, &tz->cooling_devices, node) { |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 847 | if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) { |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 848 | list_del(&pos->node); |
| 849 | mutex_unlock(&tz->lock); |
| 850 | goto unbind; |
| 851 | } |
| 852 | } |
| 853 | mutex_unlock(&tz->lock); |
| 854 | |
| 855 | return -ENODEV; |
| 856 | |
| 857 | unbind: |
| 858 | device_remove_file(&tz->device, &pos->attr); |
| 859 | sysfs_remove_link(&tz->device.kobj, pos->name); |
| 860 | release_idr(&tz->idr, &tz->lock, pos->id); |
| 861 | kfree(pos); |
| 862 | return 0; |
| 863 | } |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 864 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 865 | EXPORT_SYMBOL(thermal_zone_unbind_cooling_device); |
| 866 | |
| 867 | static void thermal_release(struct device *dev) |
| 868 | { |
| 869 | struct thermal_zone_device *tz; |
| 870 | struct thermal_cooling_device *cdev; |
| 871 | |
Kay Sievers | 354655e | 2009-01-06 10:44:37 -0800 | [diff] [blame] | 872 | if (!strncmp(dev_name(dev), "thermal_zone", sizeof "thermal_zone" - 1)) { |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 873 | tz = to_thermal_zone(dev); |
| 874 | kfree(tz); |
| 875 | } else { |
| 876 | cdev = to_cooling_device(dev); |
| 877 | kfree(cdev); |
| 878 | } |
| 879 | } |
| 880 | |
| 881 | static struct class thermal_class = { |
| 882 | .name = "thermal", |
| 883 | .dev_release = thermal_release, |
| 884 | }; |
| 885 | |
| 886 | /** |
| 887 | * thermal_cooling_device_register - register a new thermal cooling device |
| 888 | * @type: the thermal cooling device type. |
| 889 | * @devdata: device private data. |
| 890 | * @ops: standard thermal cooling devices callbacks. |
| 891 | */ |
Alan Cox | 5b275ce | 2010-11-11 15:27:29 +0000 | [diff] [blame] | 892 | struct thermal_cooling_device *thermal_cooling_device_register( |
| 893 | char *type, void *devdata, const struct thermal_cooling_device_ops *ops) |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 894 | { |
| 895 | struct thermal_cooling_device *cdev; |
| 896 | struct thermal_zone_device *pos; |
| 897 | int result; |
| 898 | |
| 899 | if (strlen(type) >= THERMAL_NAME_LENGTH) |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 900 | return ERR_PTR(-EINVAL); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 901 | |
| 902 | if (!ops || !ops->get_max_state || !ops->get_cur_state || |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 903 | !ops->set_cur_state) |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 904 | return ERR_PTR(-EINVAL); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 905 | |
| 906 | cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL); |
| 907 | if (!cdev) |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 908 | return ERR_PTR(-ENOMEM); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 909 | |
| 910 | result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id); |
| 911 | if (result) { |
| 912 | kfree(cdev); |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 913 | return ERR_PTR(result); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 914 | } |
| 915 | |
| 916 | strcpy(cdev->type, type); |
| 917 | cdev->ops = ops; |
| 918 | cdev->device.class = &thermal_class; |
| 919 | cdev->devdata = devdata; |
Kay Sievers | 354655e | 2009-01-06 10:44:37 -0800 | [diff] [blame] | 920 | dev_set_name(&cdev->device, "cooling_device%d", cdev->id); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 921 | result = device_register(&cdev->device); |
| 922 | if (result) { |
| 923 | release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id); |
| 924 | kfree(cdev); |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 925 | return ERR_PTR(result); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 926 | } |
| 927 | |
| 928 | /* sys I/F */ |
| 929 | if (type) { |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 930 | result = device_create_file(&cdev->device, &dev_attr_cdev_type); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 931 | if (result) |
| 932 | goto unregister; |
| 933 | } |
| 934 | |
| 935 | result = device_create_file(&cdev->device, &dev_attr_max_state); |
| 936 | if (result) |
| 937 | goto unregister; |
| 938 | |
| 939 | result = device_create_file(&cdev->device, &dev_attr_cur_state); |
| 940 | if (result) |
| 941 | goto unregister; |
| 942 | |
| 943 | mutex_lock(&thermal_list_lock); |
| 944 | list_add(&cdev->node, &thermal_cdev_list); |
| 945 | list_for_each_entry(pos, &thermal_tz_list, node) { |
| 946 | if (!pos->ops->bind) |
| 947 | continue; |
| 948 | result = pos->ops->bind(pos, cdev); |
| 949 | if (result) |
| 950 | break; |
| 951 | |
| 952 | } |
| 953 | mutex_unlock(&thermal_list_lock); |
| 954 | |
| 955 | if (!result) |
| 956 | return cdev; |
| 957 | |
| 958 | unregister: |
| 959 | release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id); |
| 960 | device_unregister(&cdev->device); |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 961 | return ERR_PTR(result); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 962 | } |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 963 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 964 | EXPORT_SYMBOL(thermal_cooling_device_register); |
| 965 | |
| 966 | /** |
| 967 | * thermal_cooling_device_unregister - removes the registered thermal cooling device |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 968 | * @cdev: the thermal cooling device to remove. |
| 969 | * |
| 970 | * thermal_cooling_device_unregister() must be called when the device is no |
| 971 | * longer needed. |
| 972 | */ |
| 973 | void thermal_cooling_device_unregister(struct |
| 974 | thermal_cooling_device |
| 975 | *cdev) |
| 976 | { |
| 977 | struct thermal_zone_device *tz; |
| 978 | struct thermal_cooling_device *pos = NULL; |
| 979 | |
| 980 | if (!cdev) |
| 981 | return; |
| 982 | |
| 983 | mutex_lock(&thermal_list_lock); |
| 984 | list_for_each_entry(pos, &thermal_cdev_list, node) |
| 985 | if (pos == cdev) |
| 986 | break; |
| 987 | if (pos != cdev) { |
| 988 | /* thermal cooling device not found */ |
| 989 | mutex_unlock(&thermal_list_lock); |
| 990 | return; |
| 991 | } |
| 992 | list_del(&cdev->node); |
| 993 | list_for_each_entry(tz, &thermal_tz_list, node) { |
| 994 | if (!tz->ops->unbind) |
| 995 | continue; |
| 996 | tz->ops->unbind(tz, cdev); |
| 997 | } |
| 998 | mutex_unlock(&thermal_list_lock); |
| 999 | if (cdev->type[0]) |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 1000 | device_remove_file(&cdev->device, &dev_attr_cdev_type); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1001 | device_remove_file(&cdev->device, &dev_attr_max_state); |
| 1002 | device_remove_file(&cdev->device, &dev_attr_cur_state); |
| 1003 | |
| 1004 | release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id); |
| 1005 | device_unregister(&cdev->device); |
| 1006 | return; |
| 1007 | } |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 1008 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1009 | EXPORT_SYMBOL(thermal_cooling_device_unregister); |
| 1010 | |
| 1011 | /** |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 1012 | * thermal_zone_device_update - force an update of a thermal zone's state |
| 1013 | * @ttz: the thermal zone to update |
| 1014 | */ |
| 1015 | |
| 1016 | void thermal_zone_device_update(struct thermal_zone_device *tz) |
| 1017 | { |
| 1018 | int count, ret = 0; |
| 1019 | long temp, trip_temp; |
| 1020 | enum thermal_trip_type trip_type; |
| 1021 | struct thermal_cooling_device_instance *instance; |
| 1022 | struct thermal_cooling_device *cdev; |
| 1023 | |
| 1024 | mutex_lock(&tz->lock); |
| 1025 | |
Michael Brunner | 0d28816 | 2009-08-26 14:29:25 -0700 | [diff] [blame] | 1026 | if (tz->ops->get_temp(tz, &temp)) { |
| 1027 | /* get_temp failed - retry it later */ |
| 1028 | printk(KERN_WARNING PREFIX "failed to read out thermal zone " |
| 1029 | "%d\n", tz->id); |
| 1030 | goto leave; |
| 1031 | } |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 1032 | |
| 1033 | for (count = 0; count < tz->trips; count++) { |
| 1034 | tz->ops->get_trip_type(tz, count, &trip_type); |
| 1035 | tz->ops->get_trip_temp(tz, count, &trip_temp); |
| 1036 | |
| 1037 | switch (trip_type) { |
| 1038 | case THERMAL_TRIP_CRITICAL: |
Vladimir Zajac | 2932135 | 2009-05-06 19:34:21 +0200 | [diff] [blame] | 1039 | if (temp >= trip_temp) { |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 1040 | if (tz->ops->notify) |
| 1041 | ret = tz->ops->notify(tz, count, |
| 1042 | trip_type); |
| 1043 | if (!ret) { |
| 1044 | printk(KERN_EMERG |
| 1045 | "Critical temperature reached (%ld C), shutting down.\n", |
| 1046 | temp/1000); |
| 1047 | orderly_poweroff(true); |
| 1048 | } |
| 1049 | } |
| 1050 | break; |
| 1051 | case THERMAL_TRIP_HOT: |
Vladimir Zajac | 2932135 | 2009-05-06 19:34:21 +0200 | [diff] [blame] | 1052 | if (temp >= trip_temp) |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 1053 | if (tz->ops->notify) |
| 1054 | tz->ops->notify(tz, count, trip_type); |
| 1055 | break; |
| 1056 | case THERMAL_TRIP_ACTIVE: |
| 1057 | list_for_each_entry(instance, &tz->cooling_devices, |
| 1058 | node) { |
| 1059 | if (instance->trip != count) |
| 1060 | continue; |
| 1061 | |
| 1062 | cdev = instance->cdev; |
| 1063 | |
Vladimir Zajac | 2932135 | 2009-05-06 19:34:21 +0200 | [diff] [blame] | 1064 | if (temp >= trip_temp) |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 1065 | cdev->ops->set_cur_state(cdev, 1); |
| 1066 | else |
| 1067 | cdev->ops->set_cur_state(cdev, 0); |
| 1068 | } |
| 1069 | break; |
| 1070 | case THERMAL_TRIP_PASSIVE: |
Vladimir Zajac | 2932135 | 2009-05-06 19:34:21 +0200 | [diff] [blame] | 1071 | if (temp >= trip_temp || tz->passive) |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 1072 | thermal_zone_device_passive(tz, temp, |
| 1073 | trip_temp, count); |
| 1074 | break; |
| 1075 | } |
| 1076 | } |
Matthew Garrett | 03a971a | 2008-12-03 18:00:38 +0000 | [diff] [blame] | 1077 | |
| 1078 | if (tz->forced_passive) |
| 1079 | thermal_zone_device_passive(tz, temp, tz->forced_passive, |
| 1080 | THERMAL_TRIPS_NONE); |
| 1081 | |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 1082 | tz->last_temperature = temp; |
Michael Brunner | 0d28816 | 2009-08-26 14:29:25 -0700 | [diff] [blame] | 1083 | |
| 1084 | leave: |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 1085 | if (tz->passive) |
| 1086 | thermal_zone_device_set_polling(tz, tz->passive_delay); |
| 1087 | else if (tz->polling_delay) |
| 1088 | thermal_zone_device_set_polling(tz, tz->polling_delay); |
Frans Pop | 3767cb5 | 2009-10-26 08:39:04 +0100 | [diff] [blame] | 1089 | else |
| 1090 | thermal_zone_device_set_polling(tz, 0); |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 1091 | mutex_unlock(&tz->lock); |
| 1092 | } |
| 1093 | EXPORT_SYMBOL(thermal_zone_device_update); |
| 1094 | |
| 1095 | /** |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1096 | * thermal_zone_device_register - register a new thermal zone device |
| 1097 | * @type: the thermal zone device type |
| 1098 | * @trips: the number of trip points the thermal zone support |
| 1099 | * @devdata: private device data |
| 1100 | * @ops: standard thermal zone device callbacks |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 1101 | * @tc1: thermal coefficient 1 for passive calculations |
| 1102 | * @tc2: thermal coefficient 2 for passive calculations |
| 1103 | * @passive_delay: number of milliseconds to wait between polls when |
| 1104 | * performing passive cooling |
| 1105 | * @polling_delay: number of milliseconds to wait between polls when checking |
| 1106 | * whether trip points have been crossed (0 for interrupt |
| 1107 | * driven systems) |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1108 | * |
| 1109 | * thermal_zone_device_unregister() must be called when the device is no |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 1110 | * longer needed. The passive cooling formula uses tc1 and tc2 as described in |
| 1111 | * section 11.1.5.1 of the ACPI specification 3.0. |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1112 | */ |
| 1113 | struct thermal_zone_device *thermal_zone_device_register(char *type, |
Alan Cox | 5b275ce | 2010-11-11 15:27:29 +0000 | [diff] [blame] | 1114 | int trips, void *devdata, |
| 1115 | const struct thermal_zone_device_ops *ops, |
| 1116 | int tc1, int tc2, int passive_delay, int polling_delay) |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1117 | { |
| 1118 | struct thermal_zone_device *tz; |
| 1119 | struct thermal_cooling_device *pos; |
Matthew Garrett | 03a971a | 2008-12-03 18:00:38 +0000 | [diff] [blame] | 1120 | enum thermal_trip_type trip_type; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1121 | int result; |
| 1122 | int count; |
Matthew Garrett | 03a971a | 2008-12-03 18:00:38 +0000 | [diff] [blame] | 1123 | int passive = 0; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1124 | |
| 1125 | if (strlen(type) >= THERMAL_NAME_LENGTH) |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 1126 | return ERR_PTR(-EINVAL); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1127 | |
| 1128 | if (trips > THERMAL_MAX_TRIPS || trips < 0) |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 1129 | return ERR_PTR(-EINVAL); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1130 | |
| 1131 | if (!ops || !ops->get_temp) |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 1132 | return ERR_PTR(-EINVAL); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1133 | |
| 1134 | tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL); |
| 1135 | if (!tz) |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 1136 | return ERR_PTR(-ENOMEM); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1137 | |
| 1138 | INIT_LIST_HEAD(&tz->cooling_devices); |
| 1139 | idr_init(&tz->idr); |
| 1140 | mutex_init(&tz->lock); |
| 1141 | result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id); |
| 1142 | if (result) { |
| 1143 | kfree(tz); |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 1144 | return ERR_PTR(result); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1145 | } |
| 1146 | |
| 1147 | strcpy(tz->type, type); |
| 1148 | tz->ops = ops; |
| 1149 | tz->device.class = &thermal_class; |
| 1150 | tz->devdata = devdata; |
| 1151 | tz->trips = trips; |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 1152 | tz->tc1 = tc1; |
| 1153 | tz->tc2 = tc2; |
| 1154 | tz->passive_delay = passive_delay; |
| 1155 | tz->polling_delay = polling_delay; |
| 1156 | |
Kay Sievers | 354655e | 2009-01-06 10:44:37 -0800 | [diff] [blame] | 1157 | dev_set_name(&tz->device, "thermal_zone%d", tz->id); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1158 | result = device_register(&tz->device); |
| 1159 | if (result) { |
| 1160 | release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id); |
| 1161 | kfree(tz); |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 1162 | return ERR_PTR(result); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1163 | } |
| 1164 | |
| 1165 | /* sys I/F */ |
| 1166 | if (type) { |
| 1167 | result = device_create_file(&tz->device, &dev_attr_type); |
| 1168 | if (result) |
| 1169 | goto unregister; |
| 1170 | } |
| 1171 | |
| 1172 | result = device_create_file(&tz->device, &dev_attr_temp); |
| 1173 | if (result) |
| 1174 | goto unregister; |
| 1175 | |
| 1176 | if (ops->get_mode) { |
| 1177 | result = device_create_file(&tz->device, &dev_attr_mode); |
| 1178 | if (result) |
| 1179 | goto unregister; |
| 1180 | } |
| 1181 | |
| 1182 | for (count = 0; count < trips; count++) { |
Joe Perches | ec79768 | 2012-03-21 12:55:02 -0700 | [diff] [blame^] | 1183 | result = device_create_file(&tz->device, |
| 1184 | &trip_point_attrs[count * 2]); |
| 1185 | if (result) |
| 1186 | break; |
| 1187 | result = device_create_file(&tz->device, |
| 1188 | &trip_point_attrs[count * 2 + 1]); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1189 | if (result) |
| 1190 | goto unregister; |
Matthew Garrett | 03a971a | 2008-12-03 18:00:38 +0000 | [diff] [blame] | 1191 | tz->ops->get_trip_type(tz, count, &trip_type); |
| 1192 | if (trip_type == THERMAL_TRIP_PASSIVE) |
| 1193 | passive = 1; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1194 | } |
| 1195 | |
Matthew Garrett | 03a971a | 2008-12-03 18:00:38 +0000 | [diff] [blame] | 1196 | if (!passive) |
| 1197 | result = device_create_file(&tz->device, |
| 1198 | &dev_attr_passive); |
| 1199 | |
| 1200 | if (result) |
| 1201 | goto unregister; |
| 1202 | |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 1203 | result = thermal_add_hwmon_sysfs(tz); |
| 1204 | if (result) |
| 1205 | goto unregister; |
| 1206 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1207 | mutex_lock(&thermal_list_lock); |
| 1208 | list_add_tail(&tz->node, &thermal_tz_list); |
| 1209 | if (ops->bind) |
| 1210 | list_for_each_entry(pos, &thermal_cdev_list, node) { |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 1211 | result = ops->bind(tz, pos); |
| 1212 | if (result) |
| 1213 | break; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1214 | } |
| 1215 | mutex_unlock(&thermal_list_lock); |
| 1216 | |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 1217 | INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check); |
| 1218 | |
| 1219 | thermal_zone_device_update(tz); |
| 1220 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1221 | if (!result) |
| 1222 | return tz; |
| 1223 | |
| 1224 | unregister: |
| 1225 | release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id); |
| 1226 | device_unregister(&tz->device); |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 1227 | return ERR_PTR(result); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1228 | } |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 1229 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1230 | EXPORT_SYMBOL(thermal_zone_device_register); |
| 1231 | |
| 1232 | /** |
| 1233 | * thermal_device_unregister - removes the registered thermal zone device |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1234 | * @tz: the thermal zone device to remove |
| 1235 | */ |
| 1236 | void thermal_zone_device_unregister(struct thermal_zone_device *tz) |
| 1237 | { |
| 1238 | struct thermal_cooling_device *cdev; |
| 1239 | struct thermal_zone_device *pos = NULL; |
| 1240 | int count; |
| 1241 | |
| 1242 | if (!tz) |
| 1243 | return; |
| 1244 | |
| 1245 | mutex_lock(&thermal_list_lock); |
| 1246 | list_for_each_entry(pos, &thermal_tz_list, node) |
| 1247 | if (pos == tz) |
| 1248 | break; |
| 1249 | if (pos != tz) { |
| 1250 | /* thermal zone device not found */ |
| 1251 | mutex_unlock(&thermal_list_lock); |
| 1252 | return; |
| 1253 | } |
| 1254 | list_del(&tz->node); |
| 1255 | if (tz->ops->unbind) |
| 1256 | list_for_each_entry(cdev, &thermal_cdev_list, node) |
| 1257 | tz->ops->unbind(tz, cdev); |
| 1258 | mutex_unlock(&thermal_list_lock); |
| 1259 | |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 1260 | thermal_zone_device_set_polling(tz, 0); |
| 1261 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1262 | if (tz->type[0]) |
| 1263 | device_remove_file(&tz->device, &dev_attr_type); |
| 1264 | device_remove_file(&tz->device, &dev_attr_temp); |
| 1265 | if (tz->ops->get_mode) |
| 1266 | device_remove_file(&tz->device, &dev_attr_mode); |
| 1267 | |
Joe Perches | ec79768 | 2012-03-21 12:55:02 -0700 | [diff] [blame^] | 1268 | for (count = 0; count < tz->trips; count++) { |
| 1269 | device_remove_file(&tz->device, |
| 1270 | &trip_point_attrs[count * 2]); |
| 1271 | device_remove_file(&tz->device, |
| 1272 | &trip_point_attrs[count * 2 + 1]); |
| 1273 | } |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 1274 | thermal_remove_hwmon_sysfs(tz); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1275 | release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id); |
| 1276 | idr_destroy(&tz->idr); |
| 1277 | mutex_destroy(&tz->lock); |
| 1278 | device_unregister(&tz->device); |
| 1279 | return; |
| 1280 | } |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 1281 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1282 | EXPORT_SYMBOL(thermal_zone_device_unregister); |
| 1283 | |
Rafael J. Wysocki | af06216 | 2011-03-01 01:12:19 +0100 | [diff] [blame] | 1284 | #ifdef CONFIG_NET |
| 1285 | static struct genl_family thermal_event_genl_family = { |
| 1286 | .id = GENL_ID_GENERATE, |
| 1287 | .name = THERMAL_GENL_FAMILY_NAME, |
| 1288 | .version = THERMAL_GENL_VERSION, |
| 1289 | .maxattr = THERMAL_GENL_ATTR_MAX, |
| 1290 | }; |
| 1291 | |
| 1292 | static struct genl_multicast_group thermal_event_mcgrp = { |
| 1293 | .name = THERMAL_GENL_MCAST_GROUP_NAME, |
| 1294 | }; |
| 1295 | |
Jean Delvare | 2d58d7e | 2011-11-04 10:31:04 +0100 | [diff] [blame] | 1296 | int thermal_generate_netlink_event(u32 orig, enum events event) |
R.Durgadoss | 4cb1872 | 2010-10-27 03:33:29 +0530 | [diff] [blame] | 1297 | { |
| 1298 | struct sk_buff *skb; |
| 1299 | struct nlattr *attr; |
| 1300 | struct thermal_genl_event *thermal_event; |
| 1301 | void *msg_header; |
| 1302 | int size; |
| 1303 | int result; |
Fabio Estevam | b11de07 | 2012-03-21 12:55:00 -0700 | [diff] [blame] | 1304 | static unsigned int thermal_event_seqnum; |
R.Durgadoss | 4cb1872 | 2010-10-27 03:33:29 +0530 | [diff] [blame] | 1305 | |
| 1306 | /* allocate memory */ |
Joe Perches | 886ee54 | 2012-03-21 12:55:01 -0700 | [diff] [blame] | 1307 | size = nla_total_size(sizeof(struct thermal_genl_event)) + |
| 1308 | nla_total_size(0); |
R.Durgadoss | 4cb1872 | 2010-10-27 03:33:29 +0530 | [diff] [blame] | 1309 | |
| 1310 | skb = genlmsg_new(size, GFP_ATOMIC); |
| 1311 | if (!skb) |
| 1312 | return -ENOMEM; |
| 1313 | |
| 1314 | /* add the genetlink message header */ |
| 1315 | msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++, |
| 1316 | &thermal_event_genl_family, 0, |
| 1317 | THERMAL_GENL_CMD_EVENT); |
| 1318 | if (!msg_header) { |
| 1319 | nlmsg_free(skb); |
| 1320 | return -ENOMEM; |
| 1321 | } |
| 1322 | |
| 1323 | /* fill the data */ |
Joe Perches | 886ee54 | 2012-03-21 12:55:01 -0700 | [diff] [blame] | 1324 | attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT, |
| 1325 | sizeof(struct thermal_genl_event)); |
R.Durgadoss | 4cb1872 | 2010-10-27 03:33:29 +0530 | [diff] [blame] | 1326 | |
| 1327 | if (!attr) { |
| 1328 | nlmsg_free(skb); |
| 1329 | return -EINVAL; |
| 1330 | } |
| 1331 | |
| 1332 | thermal_event = nla_data(attr); |
| 1333 | if (!thermal_event) { |
| 1334 | nlmsg_free(skb); |
| 1335 | return -EINVAL; |
| 1336 | } |
| 1337 | |
| 1338 | memset(thermal_event, 0, sizeof(struct thermal_genl_event)); |
| 1339 | |
| 1340 | thermal_event->orig = orig; |
| 1341 | thermal_event->event = event; |
| 1342 | |
| 1343 | /* send multicast genetlink message */ |
| 1344 | result = genlmsg_end(skb, msg_header); |
| 1345 | if (result < 0) { |
| 1346 | nlmsg_free(skb); |
| 1347 | return result; |
| 1348 | } |
| 1349 | |
| 1350 | result = genlmsg_multicast(skb, 0, thermal_event_mcgrp.id, GFP_ATOMIC); |
| 1351 | if (result) |
| 1352 | printk(KERN_INFO "failed to send netlink event:%d", result); |
| 1353 | |
| 1354 | return result; |
| 1355 | } |
Jean Delvare | 2d58d7e | 2011-11-04 10:31:04 +0100 | [diff] [blame] | 1356 | EXPORT_SYMBOL(thermal_generate_netlink_event); |
R.Durgadoss | 4cb1872 | 2010-10-27 03:33:29 +0530 | [diff] [blame] | 1357 | |
| 1358 | static int genetlink_init(void) |
| 1359 | { |
| 1360 | int result; |
| 1361 | |
| 1362 | result = genl_register_family(&thermal_event_genl_family); |
| 1363 | if (result) |
| 1364 | return result; |
| 1365 | |
| 1366 | result = genl_register_mc_group(&thermal_event_genl_family, |
| 1367 | &thermal_event_mcgrp); |
| 1368 | if (result) |
| 1369 | genl_unregister_family(&thermal_event_genl_family); |
| 1370 | return result; |
| 1371 | } |
| 1372 | |
Rafael J. Wysocki | af06216 | 2011-03-01 01:12:19 +0100 | [diff] [blame] | 1373 | static void genetlink_exit(void) |
| 1374 | { |
| 1375 | genl_unregister_family(&thermal_event_genl_family); |
| 1376 | } |
| 1377 | #else /* !CONFIG_NET */ |
| 1378 | static inline int genetlink_init(void) { return 0; } |
| 1379 | static inline void genetlink_exit(void) {} |
| 1380 | #endif /* !CONFIG_NET */ |
| 1381 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1382 | static int __init thermal_init(void) |
| 1383 | { |
| 1384 | int result = 0; |
| 1385 | |
| 1386 | result = class_register(&thermal_class); |
| 1387 | if (result) { |
| 1388 | idr_destroy(&thermal_tz_idr); |
| 1389 | idr_destroy(&thermal_cdev_idr); |
| 1390 | mutex_destroy(&thermal_idr_lock); |
| 1391 | mutex_destroy(&thermal_list_lock); |
| 1392 | } |
R.Durgadoss | 4cb1872 | 2010-10-27 03:33:29 +0530 | [diff] [blame] | 1393 | result = genetlink_init(); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1394 | return result; |
| 1395 | } |
| 1396 | |
| 1397 | static void __exit thermal_exit(void) |
| 1398 | { |
| 1399 | class_unregister(&thermal_class); |
| 1400 | idr_destroy(&thermal_tz_idr); |
| 1401 | idr_destroy(&thermal_cdev_idr); |
| 1402 | mutex_destroy(&thermal_idr_lock); |
| 1403 | mutex_destroy(&thermal_list_lock); |
R.Durgadoss | 4cb1872 | 2010-10-27 03:33:29 +0530 | [diff] [blame] | 1404 | genetlink_exit(); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1405 | } |
| 1406 | |
R.Durgadoss | 4cb1872 | 2010-10-27 03:33:29 +0530 | [diff] [blame] | 1407 | fs_initcall(thermal_init); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1408 | module_exit(thermal_exit); |