linux/drivers/acpi/fan.c
<<
>>
Prefs
   1/*
   2 *  acpi_fan.c - ACPI Fan Driver ($Revision: 29 $)
   3 *
   4 *  Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
   5 *  Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
   6 *
   7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   8 *
   9 *  This program is free software; you can redistribute it and/or modify
  10 *  it under the terms of the GNU General Public License as published by
  11 *  the Free Software Foundation; either version 2 of the License, or (at
  12 *  your option) any later version.
  13 *
  14 *  This program is distributed in the hope that it will be useful, but
  15 *  WITHOUT ANY WARRANTY; without even the implied warranty of
  16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  17 *  General Public License for more details.
  18 *
  19 *  You should have received a copy of the GNU General Public License along
  20 *  with this program; if not, write to the Free Software Foundation, Inc.,
  21 *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  22 *
  23 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  24 */
  25
  26#include <linux/kernel.h>
  27#include <linux/module.h>
  28#include <linux/init.h>
  29#include <linux/types.h>
  30#include <linux/uaccess.h>
  31#include <linux/thermal.h>
  32#include <acpi/acpi_bus.h>
  33#include <acpi/acpi_drivers.h>
  34#include <linux/platform_device.h>
  35#include <linux/sort.h>
  36
  37MODULE_AUTHOR("Paul Diefenbaugh");
  38MODULE_DESCRIPTION("ACPI Fan Driver");
  39MODULE_LICENSE("GPL");
  40
  41static int acpi_fan_probe(struct platform_device *pdev);
  42static int acpi_fan_remove(struct platform_device *pdev);
  43
  44static const struct acpi_device_id fan_device_ids[] = {
  45        {"PNP0C0B", 0},
  46        {"INT3404", 0},
  47        {"", 0},
  48};
  49MODULE_DEVICE_TABLE(acpi, fan_device_ids);
  50
  51#ifdef CONFIG_PM_SLEEP
  52static int acpi_fan_suspend(struct device *dev);
  53static int acpi_fan_resume(struct device *dev);
  54static struct dev_pm_ops acpi_fan_pm = {
  55        .resume = acpi_fan_resume,
  56        .freeze = acpi_fan_suspend,
  57        .thaw = acpi_fan_resume,
  58        .restore = acpi_fan_resume,
  59};
  60#define FAN_PM_OPS_PTR (&acpi_fan_pm)
  61#else
  62#define FAN_PM_OPS_PTR NULL
  63#endif
  64
  65struct acpi_fan_fps {
  66        u64 control;
  67        u64 trip_point;
  68        u64 speed;
  69        u64 noise_level;
  70        u64 power;
  71};
  72
  73struct acpi_fan_fif {
  74        u64 revision;
  75        u64 fine_grain_ctrl;
  76        u64 step_size;
  77        u64 low_speed_notification;
  78};
  79
  80struct acpi_fan {
  81        bool acpi4;
  82        struct acpi_fan_fif fif;
  83        struct acpi_fan_fps *fps;
  84        int fps_count;
  85        struct thermal_cooling_device *cdev;
  86};
  87
  88static struct platform_driver acpi_fan_driver = {
  89        .probe = acpi_fan_probe,
  90        .remove = acpi_fan_remove,
  91        .driver = {
  92                .name = "acpi-fan",
  93                .acpi_match_table = fan_device_ids,
  94                .pm = FAN_PM_OPS_PTR,
  95        },
  96};
  97
  98/* thermal cooling device callbacks */
  99static int fan_get_max_state(struct thermal_cooling_device *cdev, unsigned long
 100                             *state)
 101{
 102        struct acpi_device *device = cdev->devdata;
 103        struct acpi_fan *fan = acpi_driver_data(device);
 104
 105        if (fan->acpi4)
 106                *state = fan->fps_count - 1;
 107        else
 108                *state = 1;
 109        return 0;
 110}
 111
 112static int fan_get_state_acpi4(struct acpi_device *device, unsigned long *state)
 113{
 114        struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
 115        struct acpi_fan *fan = acpi_driver_data(device);
 116        union acpi_object *obj;
 117        acpi_status status;
 118        int control, i;
 119
 120        status = acpi_evaluate_object(device->handle, "_FST", NULL, &buffer);
 121        if (ACPI_FAILURE(status)) {
 122                dev_err(&device->dev, "Get fan state failed\n");
 123                return status;
 124        }
 125
 126        obj = buffer.pointer;
 127        if (!obj || obj->type != ACPI_TYPE_PACKAGE ||
 128            obj->package.count != 3 ||
 129            obj->package.elements[1].type != ACPI_TYPE_INTEGER) {
 130                dev_err(&device->dev, "Invalid _FST data\n");
 131                status = -EINVAL;
 132                goto err;
 133        }
 134
 135        control = obj->package.elements[1].integer.value;
 136        for (i = 0; i < fan->fps_count; i++) {
 137                if (control == fan->fps[i].control)
 138                        break;
 139        }
 140        if (i == fan->fps_count) {
 141                dev_dbg(&device->dev, "Invalid control value returned\n");
 142                status = -EINVAL;
 143                goto err;
 144        }
 145
 146        *state = i;
 147
 148err:
 149        kfree(obj);
 150        return status;
 151}
 152
 153static int fan_get_state(struct acpi_device *device, unsigned long *state)
 154{
 155        int result;
 156        int acpi_state = ACPI_STATE_D0;
 157
 158        result = acpi_device_update_power(device, &acpi_state);
 159        if (result)
 160                return result;
 161
 162        *state = (acpi_state == ACPI_STATE_D3_COLD ? 0 :
 163                 (acpi_state == ACPI_STATE_D0 ? 1 : -1));
 164        return 0;
 165}
 166
 167static int fan_get_cur_state(struct thermal_cooling_device *cdev, unsigned long
 168                             *state)
 169{
 170        struct acpi_device *device = cdev->devdata;
 171        struct acpi_fan *fan = acpi_driver_data(device);
 172
 173        if (fan->acpi4)
 174                return fan_get_state_acpi4(device, state);
 175        else
 176                return fan_get_state(device, state);
 177}
 178
 179static int fan_set_state(struct acpi_device *device, unsigned long state)
 180{
 181        if (state != 0 && state != 1)
 182                return -EINVAL;
 183
 184        return acpi_device_set_power(device,
 185                                     state ? ACPI_STATE_D0 : ACPI_STATE_D3_COLD);
 186}
 187
 188static int fan_set_state_acpi4(struct acpi_device *device, unsigned long state)
 189{
 190        struct acpi_fan *fan = acpi_driver_data(device);
 191        acpi_status status;
 192
 193        if (state >= fan->fps_count)
 194                return -EINVAL;
 195
 196        status = acpi_execute_simple_method(device->handle, "_FSL",
 197                                            fan->fps[state].control);
 198        if (ACPI_FAILURE(status)) {
 199                dev_dbg(&device->dev, "Failed to set state by _FSL\n");
 200                return status;
 201        }
 202
 203        return 0;
 204}
 205
 206static int
 207fan_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state)
 208{
 209        struct acpi_device *device = cdev->devdata;
 210        struct acpi_fan *fan = acpi_driver_data(device);
 211
 212        if (fan->acpi4)
 213                return fan_set_state_acpi4(device, state);
 214        else
 215                return fan_set_state(device, state);
 216 }
 217
 218static const struct thermal_cooling_device_ops fan_cooling_ops = {
 219        .get_max_state = fan_get_max_state,
 220        .get_cur_state = fan_get_cur_state,
 221        .set_cur_state = fan_set_cur_state,
 222};
 223
 224/* --------------------------------------------------------------------------
 225 *                               Driver Interface
 226 * --------------------------------------------------------------------------
 227*/
 228
 229static bool acpi_fan_is_acpi4(struct acpi_device *device)
 230{
 231        return acpi_has_method(device->handle, "_FIF") &&
 232               acpi_has_method(device->handle, "_FPS") &&
 233               acpi_has_method(device->handle, "_FSL") &&
 234               acpi_has_method(device->handle, "_FST");
 235}
 236
 237static int acpi_fan_get_fif(struct acpi_device *device)
 238{
 239        struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
 240        struct acpi_fan *fan = acpi_driver_data(device);
 241        struct acpi_buffer format = { sizeof("NNNN"), "NNNN" };
 242        struct acpi_buffer fif = { sizeof(fan->fif), &fan->fif };
 243        union acpi_object *obj;
 244        acpi_status status;
 245
 246        status = acpi_evaluate_object(device->handle, "_FIF", NULL, &buffer);
 247        if (ACPI_FAILURE(status))
 248                return status;
 249
 250        obj = buffer.pointer;
 251        if (!obj || obj->type != ACPI_TYPE_PACKAGE) {
 252                dev_err(&device->dev, "Invalid _FIF data\n");
 253                status = -EINVAL;
 254                goto err;
 255        }
 256
 257        status = acpi_extract_package(obj, &format, &fif);
 258        if (ACPI_FAILURE(status)) {
 259                dev_err(&device->dev, "Invalid _FIF element\n");
 260                status = -EINVAL;
 261        }
 262
 263err:
 264        kfree(obj);
 265        return status;
 266}
 267
 268static int acpi_fan_speed_cmp(const void *a, const void *b)
 269{
 270        const struct acpi_fan_fps *fps1 = a;
 271        const struct acpi_fan_fps *fps2 = b;
 272        return fps1->speed - fps2->speed;
 273}
 274
 275static int acpi_fan_get_fps(struct acpi_device *device)
 276{
 277        struct acpi_fan *fan = acpi_driver_data(device);
 278        struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
 279        union acpi_object *obj;
 280        acpi_status status;
 281        int i;
 282
 283        status = acpi_evaluate_object(device->handle, "_FPS", NULL, &buffer);
 284        if (ACPI_FAILURE(status))
 285                return status;
 286
 287        obj = buffer.pointer;
 288        if (!obj || obj->type != ACPI_TYPE_PACKAGE || obj->package.count < 2) {
 289                dev_err(&device->dev, "Invalid _FPS data\n");
 290                status = -EINVAL;
 291                goto err;
 292        }
 293
 294        fan->fps_count = obj->package.count - 1; /* minus revision field */
 295        fan->fps = devm_kzalloc(&device->dev,
 296                                fan->fps_count * sizeof(struct acpi_fan_fps),
 297                                GFP_KERNEL);
 298        if (!fan->fps) {
 299                dev_err(&device->dev, "Not enough memory\n");
 300                status = -ENOMEM;
 301                goto err;
 302        }
 303        for (i = 0; i < fan->fps_count; i++) {
 304                struct acpi_buffer format = { sizeof("NNNNN"), "NNNNN" };
 305                struct acpi_buffer fps = { sizeof(fan->fps[i]), &fan->fps[i] };
 306                status = acpi_extract_package(&obj->package.elements[i + 1],
 307                                              &format, &fps);
 308                if (ACPI_FAILURE(status)) {
 309                        dev_err(&device->dev, "Invalid _FPS element\n");
 310                        break;
 311                }
 312        }
 313
 314        /* sort the state array according to fan speed in increase order */
 315        sort(fan->fps, fan->fps_count, sizeof(*fan->fps),
 316             acpi_fan_speed_cmp, NULL);
 317
 318err:
 319        kfree(obj);
 320        return status;
 321}
 322
 323static int acpi_fan_probe(struct platform_device *pdev)
 324{
 325        int result = 0;
 326        struct thermal_cooling_device *cdev;
 327        struct acpi_fan *fan;
 328        struct acpi_device *device = ACPI_COMPANION(&pdev->dev);
 329        char *name;
 330
 331        fan = devm_kzalloc(&pdev->dev, sizeof(*fan), GFP_KERNEL);
 332        if (!fan) {
 333                dev_err(&device->dev, "No memory for fan\n");
 334                return -ENOMEM;
 335        }
 336        device->driver_data = fan;
 337        platform_set_drvdata(pdev, fan);
 338
 339        if (acpi_fan_is_acpi4(device)) {
 340                if (acpi_fan_get_fif(device) || acpi_fan_get_fps(device))
 341                        goto end;
 342                fan->acpi4 = true;
 343        } else {
 344                result = acpi_device_update_power(device, NULL);
 345                if (result) {
 346                        dev_err(&device->dev, "Setting initial power state\n");
 347                        goto end;
 348                }
 349        }
 350
 351        if (!strncmp(pdev->name, "PNP0C0B", strlen("PNP0C0B")))
 352                name = "Fan";
 353        else
 354                name = acpi_device_bid(device);
 355
 356        cdev = thermal_cooling_device_register(name, device,
 357                                                &fan_cooling_ops);
 358        if (IS_ERR(cdev)) {
 359                result = PTR_ERR(cdev);
 360                goto end;
 361        }
 362
 363        dev_dbg(&pdev->dev, "registered as cooling_device%d\n", cdev->id);
 364
 365        fan->cdev = cdev;
 366        result = sysfs_create_link(&pdev->dev.kobj,
 367                                   &cdev->device.kobj,
 368                                   "thermal_cooling");
 369        if (result)
 370                dev_err(&pdev->dev, "Failed to create sysfs link "
 371                        "'thermal_cooling'\n");
 372
 373        result = sysfs_create_link(&cdev->device.kobj,
 374                                   &pdev->dev.kobj,
 375                                   "device");
 376        if (result)
 377                dev_err(&device->dev, "Failed to create sysfs link 'device'\n");
 378
 379      end:
 380        return result;
 381}
 382
 383static int acpi_fan_remove(struct platform_device *pdev)
 384{
 385        struct acpi_fan *fan = platform_get_drvdata(pdev);
 386
 387        sysfs_remove_link(&pdev->dev.kobj, "thermal_cooling");
 388        sysfs_remove_link(&fan->cdev->device.kobj, "device");
 389        thermal_cooling_device_unregister(fan->cdev);
 390
 391        return 0;
 392}
 393
 394#ifdef CONFIG_PM_SLEEP
 395static int acpi_fan_suspend(struct device *dev)
 396{
 397        struct acpi_fan *fan = dev_get_drvdata(dev);
 398        if (fan->acpi4)
 399                return 0;
 400
 401        acpi_device_set_power(ACPI_COMPANION(dev), ACPI_STATE_D0);
 402
 403        return AE_OK;
 404}
 405
 406static int acpi_fan_resume(struct device *dev)
 407{
 408        int result;
 409        struct acpi_fan *fan = dev_get_drvdata(dev);
 410
 411        if (fan->acpi4)
 412                return 0;
 413
 414        result = acpi_device_update_power(ACPI_COMPANION(dev), NULL);
 415        if (result)
 416                dev_err(dev, "Error updating fan power state\n");
 417
 418        return result;
 419}
 420#endif
 421
 422module_platform_driver(acpi_fan_driver);
 423