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