linux/drivers/thermal/gov_bang_bang.c
<<
>>
Prefs
   1/*
   2 *  gov_bang_bang.c - A simple thermal throttling governor using hysteresis
   3 *
   4 *  Copyright (C) 2014 Peter Feuerer <peter@piie.net>
   5 *
   6 *  Based on step_wise.c with following Copyrights:
   7 *  Copyright (C) 2012 Intel Corp
   8 *  Copyright (C) 2012 Durgadoss R <durgadoss.r@intel.com>
   9 *
  10 *
  11 * This program is free software; you can redistribute it and/or modify
  12 * it under the terms of the GNU General Public License as published by
  13 * the Free Software Foundation, version 2.
  14 *
  15 * This program is distributed in the hope that it will be useful,
  16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  18 * the GNU General Public License for more details.
  19 *
  20 */
  21
  22#include <linux/thermal.h>
  23
  24#include "thermal_core.h"
  25
  26static void thermal_zone_trip_update(struct thermal_zone_device *tz, int trip)
  27{
  28        int trip_temp, trip_hyst;
  29        struct thermal_instance *instance;
  30
  31        tz->ops->get_trip_temp(tz, trip, &trip_temp);
  32
  33        if (!tz->ops->get_trip_hyst) {
  34                pr_warn_once("Undefined get_trip_hyst for thermal zone %s - "
  35                                "running with default hysteresis zero\n", tz->type);
  36                trip_hyst = 0;
  37        } else
  38                tz->ops->get_trip_hyst(tz, trip, &trip_hyst);
  39
  40        dev_dbg(&tz->device, "Trip%d[temp=%d]:temp=%d:hyst=%d\n",
  41                                trip, trip_temp, tz->temperature,
  42                                trip_hyst);
  43
  44        mutex_lock(&tz->lock);
  45
  46        list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
  47                if (instance->trip != trip)
  48                        continue;
  49
  50                /* in case fan is in initial state, switch the fan off */
  51                if (instance->target == THERMAL_NO_TARGET)
  52                        instance->target = 0;
  53
  54                /* in case fan is neither on nor off set the fan to active */
  55                if (instance->target != 0 && instance->target != 1) {
  56                        pr_warn("Thermal instance %s controlled by bang-bang has unexpected state: %ld\n",
  57                                        instance->name, instance->target);
  58                        instance->target = 1;
  59                }
  60
  61                /*
  62                 * enable fan when temperature exceeds trip_temp and disable
  63                 * the fan in case it falls below trip_temp minus hysteresis
  64                 */
  65                if (instance->target == 0 && tz->temperature >= trip_temp)
  66                        instance->target = 1;
  67                else if (instance->target == 1 &&
  68                                tz->temperature <= trip_temp - trip_hyst)
  69                        instance->target = 0;
  70
  71                dev_dbg(&instance->cdev->device, "target=%d\n",
  72                                        (int)instance->target);
  73
  74                mutex_lock(&instance->cdev->lock);
  75                instance->cdev->updated = false; /* cdev needs update */
  76                mutex_unlock(&instance->cdev->lock);
  77        }
  78
  79        mutex_unlock(&tz->lock);
  80}
  81
  82/**
  83 * bang_bang_control - controls devices associated with the given zone
  84 * @tz - thermal_zone_device
  85 * @trip - the trip point
  86 *
  87 * Regulation Logic: a two point regulation, deliver cooling state depending
  88 * on the previous state shown in this diagram:
  89 *
  90 *                Fan:   OFF    ON
  91 *
  92 *                              |
  93 *                              |
  94 *          trip_temp:    +---->+
  95 *                        |     |        ^
  96 *                        |     |        |
  97 *                        |     |   Temperature
  98 * (trip_temp - hyst):    +<----+
  99 *                        |
 100 *                        |
 101 *                        |
 102 *
 103 *   * If the fan is not running and temperature exceeds trip_temp, the fan
 104 *     gets turned on.
 105 *   * In case the fan is running, temperature must fall below
 106 *     (trip_temp - hyst) so that the fan gets turned off again.
 107 *
 108 */
 109static int bang_bang_control(struct thermal_zone_device *tz, int trip)
 110{
 111        struct thermal_instance *instance;
 112
 113        thermal_zone_trip_update(tz, trip);
 114
 115        mutex_lock(&tz->lock);
 116
 117        list_for_each_entry(instance, &tz->thermal_instances, tz_node)
 118                thermal_cdev_update(instance->cdev);
 119
 120        mutex_unlock(&tz->lock);
 121
 122        return 0;
 123}
 124
 125static struct thermal_governor thermal_gov_bang_bang = {
 126        .name           = "bang_bang",
 127        .throttle       = bang_bang_control,
 128};
 129
 130int thermal_gov_bang_bang_register(void)
 131{
 132        return thermal_register_governor(&thermal_gov_bang_bang);
 133}
 134
 135void thermal_gov_bang_bang_unregister(void)
 136{
 137        thermal_unregister_governor(&thermal_gov_bang_bang);
 138}
 139