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