134 lines · c
1// SPDX-License-Identifier: GPL-2.0-only2/*3 * gov_bang_bang.c - A simple thermal throttling governor using hysteresis4 *5 * Copyright (C) 2014 Peter Kaestle <peter@piie.net>6 *7 * Based on step_wise.c with following Copyrights:8 * Copyright (C) 2012 Intel Corp9 * Copyright (C) 2012 Durgadoss R <durgadoss.r@intel.com>10 */11 12#include <linux/thermal.h>13 14#include "thermal_core.h"15 16static void bang_bang_set_instance_target(struct thermal_instance *instance,17 unsigned int target)18{19 if (instance->target != 0 && instance->target != 1 &&20 instance->target != THERMAL_NO_TARGET)21 pr_debug("Unexpected state %ld of thermal instance %s in bang-bang\n",22 instance->target, instance->name);23 24 /*25 * Enable the fan when the trip is crossed on the way up and disable it26 * when the trip is crossed on the way down.27 */28 instance->target = target;29 instance->initialized = true;30 31 dev_dbg(&instance->cdev->device, "target=%ld\n", instance->target);32 33 mutex_lock(&instance->cdev->lock);34 __thermal_cdev_update(instance->cdev);35 mutex_unlock(&instance->cdev->lock);36}37 38/**39 * bang_bang_control - controls devices associated with the given zone40 * @tz: thermal_zone_device41 * @trip: the trip point42 * @crossed_up: whether or not the trip has been crossed on the way up43 *44 * Regulation Logic: a two point regulation, deliver cooling state depending45 * on the previous state shown in this diagram:46 *47 * Fan: OFF ON48 *49 * |50 * |51 * trip_temp: +---->+52 * | | ^53 * | | |54 * | | Temperature55 * (trip_temp - hyst): +<----+56 * |57 * |58 * |59 *60 * * If the fan is not running and temperature exceeds trip_temp, the fan61 * gets turned on.62 * * In case the fan is running, temperature must fall below63 * (trip_temp - hyst) so that the fan gets turned off again.64 *65 */66static void bang_bang_control(struct thermal_zone_device *tz,67 const struct thermal_trip *trip,68 bool crossed_up)69{70 struct thermal_instance *instance;71 72 lockdep_assert_held(&tz->lock);73 74 dev_dbg(&tz->device, "Trip%d[temp=%d]:temp=%d:hyst=%d\n",75 thermal_zone_trip_id(tz, trip), trip->temperature,76 tz->temperature, trip->hysteresis);77 78 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {79 if (instance->trip == trip)80 bang_bang_set_instance_target(instance, crossed_up);81 }82}83 84static void bang_bang_manage(struct thermal_zone_device *tz)85{86 const struct thermal_trip_desc *td;87 struct thermal_instance *instance;88 89 /* If the code below has run already, nothing needs to be done. */90 if (tz->governor_data)91 return;92 93 for_each_trip_desc(tz, td) {94 const struct thermal_trip *trip = &td->trip;95 bool turn_on;96 97 if (trip->temperature == THERMAL_TEMP_INVALID ||98 trip->type == THERMAL_TRIP_CRITICAL ||99 trip->type == THERMAL_TRIP_HOT)100 continue;101 102 /*103 * Adjust the target states for uninitialized thermal instances104 * to the thermal zone temperature and the trip point threshold.105 */106 turn_on = tz->temperature >= td->threshold;107 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {108 if (!instance->initialized && instance->trip == trip)109 bang_bang_set_instance_target(instance, turn_on);110 }111 }112 113 tz->governor_data = (void *)true;114}115 116static void bang_bang_update_tz(struct thermal_zone_device *tz,117 enum thermal_notify_event reason)118{119 /*120 * Let bang_bang_manage() know that it needs to walk trips after binding121 * a new cdev and after system resume.122 */123 if (reason == THERMAL_TZ_BIND_CDEV || reason == THERMAL_TZ_RESUME)124 tz->governor_data = NULL;125}126 127static struct thermal_governor thermal_gov_bang_bang = {128 .name = "bang_bang",129 .trip_crossed = bang_bang_control,130 .manage = bang_bang_manage,131 .update_tz = bang_bang_update_tz,132};133THERMAL_GOVERNOR_DECLARE(thermal_gov_bang_bang);134