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