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